Update README.md
Browse files
README.md
CHANGED
@@ -87,6 +87,199 @@ dataset_info:
|
|
87 |
download_size: 180380355
|
88 |
dataset_size: 199395693
|
89 |
---
|
90 |
-
# Dataset Card for "
|
91 |
|
92 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
download_size: 180380355
|
88 |
dataset_size: 199395693
|
89 |
---
|
90 |
+
# Dataset Card for "QM9"
|
91 |
|
92 |
+
QM9 dataset from [Ruddigkeit et al., 2012](https://pubs.acs.org/doi/full/10.1021/ci300415d);
|
93 |
+
[Ramakrishnan et al., 2014](https://www.nature.com/articles/sdata201422).
|
94 |
+
|
95 |
+
Original data downloaded from: http://quantum-machine.org/datasets.
|
96 |
+
Additional annotations (QED, logP, SA score, NP score, bond and ring counts) added using [`rdkit`](https://www.rdkit.org/docs/index.html) library.
|
97 |
+
|
98 |
+
## Quick start usage:
|
99 |
+
```python
|
100 |
+
from datasets import load_dataset
|
101 |
+
|
102 |
+
ds = load_dataset("yairschiff/qm9")
|
103 |
+
|
104 |
+
# Random train/test splits as recommended by:
|
105 |
+
# https://moleculenet.org/datasets-1
|
106 |
+
test_size = 0.1
|
107 |
+
seed = 1
|
108 |
+
dataset.train_test_split(test_size=test_size, seed=seed)
|
109 |
+
```
|
110 |
+
|
111 |
+
|
112 |
+
## Full processing steps
|
113 |
+
|
114 |
+
```python
|
115 |
+
import os
|
116 |
+
import typing
|
117 |
+
|
118 |
+
import datasets
|
119 |
+
import numpy as np
|
120 |
+
import pandas as pd
|
121 |
+
import rdkit
|
122 |
+
import torch
|
123 |
+
from rdkit import Chem as rdChem
|
124 |
+
from rdkit.Chem import Crippen, QED
|
125 |
+
from rdkit.Contrib.NP_Score import npscorer
|
126 |
+
from rdkit.Contrib.SA_Score import sascorer
|
127 |
+
from tqdm.auto import tqdm
|
128 |
+
|
129 |
+
# TODO: Update to 2024.03.6 release when available instead of suppressing warning!
|
130 |
+
# See: https://github.com/rdkit/rdkit/issues/7625#
|
131 |
+
rdkit.rdBase.DisableLog('rdApp.warning')
|
132 |
+
|
133 |
+
def parse_float(
|
134 |
+
s: str
|
135 |
+
) -> float:
|
136 |
+
"""Parses floats potentially written as exponentiated values.
|
137 |
+
|
138 |
+
Copied from https://www.kaggle.com/code/tawe141/extracting-data-from-qm9-xyz-files/code
|
139 |
+
"""
|
140 |
+
try:
|
141 |
+
return float(s)
|
142 |
+
except ValueError:
|
143 |
+
base, power = s.split('*^')
|
144 |
+
return float(base) * 10**float(power)
|
145 |
+
|
146 |
+
|
147 |
+
def count_rings_and_bonds(
|
148 |
+
mol: rdChem.Mol, max_ring_size: int = -1
|
149 |
+
) -> typing.Dict[str, int]:
|
150 |
+
"""Counts bond and ring (by type)."""
|
151 |
+
|
152 |
+
# Counting rings
|
153 |
+
ssr = rdChem.GetSymmSSSR(mol)
|
154 |
+
ring_count = len(ssr)
|
155 |
+
|
156 |
+
ring_sizes = {} if max_ring_size < 0 else {i: 0 for i in range(3, max_ring_size+1)}
|
157 |
+
for ring in ssr:
|
158 |
+
ring_size = len(ring)
|
159 |
+
if ring_size not in ring_sizes:
|
160 |
+
ring_sizes[ring_size] = 0
|
161 |
+
ring_sizes[ring_size] += 1
|
162 |
+
|
163 |
+
# Counting bond types
|
164 |
+
bond_counts = {
|
165 |
+
'single': 0,
|
166 |
+
'double': 0,
|
167 |
+
'triple': 0,
|
168 |
+
'aromatic': 0
|
169 |
+
}
|
170 |
+
|
171 |
+
for bond in mol.GetBonds():
|
172 |
+
if bond.GetIsAromatic():
|
173 |
+
bond_counts['aromatic'] += 1
|
174 |
+
elif bond.GetBondType() == rdChem.BondType.SINGLE:
|
175 |
+
bond_counts['single'] += 1
|
176 |
+
elif bond.GetBondType() == rdChem.BondType.DOUBLE:
|
177 |
+
bond_counts['double'] += 1
|
178 |
+
elif bond.GetBondType() == rdChem.BondType.TRIPLE:
|
179 |
+
bond_counts['triple'] += 1
|
180 |
+
result = {
|
181 |
+
'ring_count': ring_count,
|
182 |
+
}
|
183 |
+
for k, v in ring_sizes.items():
|
184 |
+
result[f"R{k}"] = v
|
185 |
+
|
186 |
+
for k, v in bond_counts.items():
|
187 |
+
result[f"{k}_bond"] = v
|
188 |
+
return result
|
189 |
+
|
190 |
+
|
191 |
+
def parse_xyz(
|
192 |
+
filename: str,
|
193 |
+
max_ring_size: int = -1,
|
194 |
+
npscorer_model: typing.Optional[dict] = None,
|
195 |
+
array_format: str = 'np'
|
196 |
+
) -> typing.Dict[str, typing.Any]:
|
197 |
+
"""Parses QM9 specific xyz files.
|
198 |
+
|
199 |
+
See https://www.nature.com/articles/sdata201422/tables/2 for reference.
|
200 |
+
Adapted from https://www.kaggle.com/code/tawe141/extracting-data-from-qm9-xyz-files/code
|
201 |
+
"""
|
202 |
+
assert array_format in ['np', 'pt'], \
|
203 |
+
f"Invalid array_format: `{array_format}` provided. Must be one of `np` (numpy.array), `pt` (torch.tensor)."
|
204 |
+
|
205 |
+
num_atoms = 0
|
206 |
+
scalar_properties = []
|
207 |
+
atomic_symbols = []
|
208 |
+
xyz = []
|
209 |
+
charges = []
|
210 |
+
harmonic_vibrational_frequencies = []
|
211 |
+
smiles = ''
|
212 |
+
inchi = ''
|
213 |
+
with open(filename, 'r') as f:
|
214 |
+
for line_num, line in enumerate(f):
|
215 |
+
if line_num == 0:
|
216 |
+
num_atoms = int(line)
|
217 |
+
elif line_num == 1:
|
218 |
+
scalar_properties = [float(i) for i in line.split()[2:]]
|
219 |
+
elif 2 <= line_num <= 1 + num_atoms:
|
220 |
+
atom_symbol, x, y, z, charge = line.split()
|
221 |
+
atomic_symbols.append(atom_symbol)
|
222 |
+
xyz.append([parse_float(x), parse_float(y), parse_float(z)])
|
223 |
+
charges.append(parse_float(charge))
|
224 |
+
elif line_num == num_atoms + 2:
|
225 |
+
harmonic_vibrational_frequencies = [float(i) for i in line.split()]
|
226 |
+
elif line_num == num_atoms + 3:
|
227 |
+
smiles = line.split()[0]
|
228 |
+
elif line_num == num_atoms + 4:
|
229 |
+
inchi = line.split()[0]
|
230 |
+
|
231 |
+
array_wrap = np.array if array_format == 'np' else torch.tensor
|
232 |
+
result = {
|
233 |
+
'num_atoms': num_atoms,
|
234 |
+
'atomic_symbols': atomic_symbols,
|
235 |
+
'pos': array_wrap(xyz),
|
236 |
+
'charges': array_wrap(charges),
|
237 |
+
'harmonic_oscillator_frequencies': array_wrap(harmonic_vibrational_frequencies),
|
238 |
+
'smiles': smiles,
|
239 |
+
'inchi': inchi
|
240 |
+
}
|
241 |
+
scalar_property_labels = [
|
242 |
+
'A', 'B', 'C', 'mu', 'alpha', 'homo', 'lumo', 'gap', 'r2', 'zpve', 'u0', 'u', 'h', 'g', 'cv'
|
243 |
+
]
|
244 |
+
scalar_properties = dict(zip(scalar_property_labels, scalar_properties))
|
245 |
+
result.update(scalar_properties)
|
246 |
+
|
247 |
+
# RdKit
|
248 |
+
result['canonical_smiles'] = rdChem.CanonSmiles(result['smiles'])
|
249 |
+
m = rdChem.MolFromSmiles(result['canonical_smiles'])
|
250 |
+
result['logP'] = Crippen.MolLogP(m)
|
251 |
+
result['qed'] = QED.qed(m)
|
252 |
+
if npscorer_model is not None:
|
253 |
+
result['np_score'] = npscorer.scoreMol(m, npscorer_model)
|
254 |
+
result['sa_score'] = sascorer.calculateScore(m)
|
255 |
+
result.update(count_rings_and_bonds(m, max_ring_size=max_ring_size))
|
256 |
+
|
257 |
+
return result
|
258 |
+
|
259 |
+
"""
|
260 |
+
Download xyz files from:
|
261 |
+
https://figshare.com/collections/Quantum_chemistry_structures_and_properties_of_134_kilo_molecules/978904
|
262 |
+
> wget https://figshare.com/ndownloader/files/3195389/dsgdb9nsd.xyz.tar.bz2
|
263 |
+
> mkdir dsgdb9nsd.xyz
|
264 |
+
> tar -xvjf dsgdb9nsd.xyz.tar.bz2 -C dsgdb9nsd.xyz
|
265 |
+
"""
|
266 |
+
MAX_RING_SIZE = 9
|
267 |
+
fscore = npscorer.readNPModel()
|
268 |
+
xyz_dir_path = '<PATH TO dsgdb9nsd.xyz>'
|
269 |
+
parsed_xyz = []
|
270 |
+
for file in tqdm(sorted(os.listdir(xyz_dir_path)), desc='Parsing'):
|
271 |
+
parsed = parse_xyz(os.path.join(xyz_dir_path, file),
|
272 |
+
max_ring_size=MAX_RING_SIZE,
|
273 |
+
npscorer_model=fscore,
|
274 |
+
array_format='np')
|
275 |
+
parsed_xyz.append(parsed)
|
276 |
+
|
277 |
+
qm9_df = pd.DataFrame(data=parsed_xyz)
|
278 |
+
|
279 |
+
# Conversion below is needed to avoid:
|
280 |
+
# `ArrowInvalid: ('Can only convert 1-dimensional array values',
|
281 |
+
# 'Conversion failed for column pos with type object')`
|
282 |
+
qm9_df['pos'] = qm9_df['pos'].apply(lambda x: [xi for xi in x])
|
283 |
+
|
284 |
+
dataset = datasets.Dataset.from_pandas(qm9_df)
|
285 |
+
```
|