File size: 5,557 Bytes
b026c7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f2b059
 
 
 
 
 
 
b026c7c
 
 
 
8f2b059
 
 
b026c7c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import csv
import json
import os
import datasets

_CITATION = """
@inproceedings{gebhard2022inferring,
  title={Inferring molecular complexity from mass spectrometry data using machine learning},
  author={Gebhard, Timothy D and Bell, Aaron C and Gong, Jian and Hastings, Jaden J. A. and Fricke, G. Matthew and Cabrol, Nathalie and Sandford, Scott and Phillips, Michael and Warren-Rhodes, Kimberley and Baydin, Atilim Gunes},
  booktitle={NeurIPS Workshop on Machine Learning and the Physical Sciences},
  year={2022}
}
"""

_DESCRIPTION = """
SaganMC is a molecular dataset designed to support machine learning research in molecular complexity inference. It includes over 400,000 molecules with computed structural, physico-chemical, and complexity descriptors, and a subset of ~16k molecules that additionally include experimental mass spectra.
"""

_HOMEPAGE = "https://huggingface.co/datasets/oxai4science/sagan-mc"
_LICENSE = "CC-BY-4.0"

_URLS = {
    "sagan-mc-400k": "https://huggingface.co/datasets/oxai4science/sagan-mc/resolve/main/sagan-mc-400k.csv",
    "sagan-mc-spectra-16k": "https://huggingface.co/datasets/oxai4science/sagan-mc/resolve/main/sagan-mc-spectra-16k.csv",
}

class SaganMC(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="sagan-mc-400k", version=VERSION, description="Full dataset with ~400k molecules"),
        datasets.BuilderConfig(name="sagan-mc-spectra-16k", version=VERSION, description="Subset with mass spectra (~16k molecules)"),
    ]

    DEFAULT_CONFIG_NAME = "sagan-mc-400k"

    def _info(self):
        features = datasets.Features({
            "inchi": datasets.Value("string"),
            "inchikey": datasets.Value("string"),
            "selfies": datasets.Value("string"),
            "smiles": datasets.Value("string"),
            "smiles_scaffold": datasets.Value("string"),
            "formula": datasets.Value("string"),
            "fingerprint_morgan": datasets.Value("string"),
            "num_atoms": datasets.Value("int32"),
            "num_atoms_all": datasets.Value("int32"),
            "num_bonds": datasets.Value("int32"),
            "num_bonds_all": datasets.Value("int32"),
            "num_rings": datasets.Value("int32"),
            "num_aromatic_rings": datasets.Value("int32"),
            "physchem_mol_weight": datasets.Value("float"),
            "physchem_logp": datasets.Value("float"),
            "physchem_tpsa": datasets.Value("float"),
            "physchem_qed": datasets.Value("float"),
            "physchem_h_acceptors": datasets.Value("int32"),
            "physchem_h_donors": datasets.Value("int32"),
            "physchem_rotatable_bonds": datasets.Value("int32"),
            "physchem_fraction_csp3": datasets.Value("float"),
            "mass_spectrum_nist": datasets.Value("string"),
            "complex_ma_score": datasets.Value("int32"),
            "complex_ma_runtime": datasets.Value("float"),
            "complex_bertz_score": datasets.Value("float"),
            "complex_bertz_runtime": datasets.Value("float"),
            "complex_boettcher_score": datasets.Value("float"),
            "complex_boettcher_runtime": datasets.Value("float"),
            "synth_sa_score": datasets.Value("float"),
            "meta_cas_number": datasets.Value("string"),
            "meta_names": datasets.Value("string"),
            "meta_iupac_name": datasets.Value("string"),
            "meta_comment": datasets.Value("string"),
            "meta_origin": datasets.Value("string"),
            "meta_reference": datasets.Value("string"),
            "split": datasets.ClassLabel(names=["train", "val", "test"])
        })
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        url = _URLS[self.config.name]
        data_path = dl_manager.download_and_extract(url)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"filepath": data_path, "split_name": "train"},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={"filepath": data_path, "split_name": "val"},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={"filepath": data_path, "split_name": "test"},
            ),
        ]

    def _generate_examples(self, filepath, split_name):
        numeric_fields = [
            "num_atoms", "num_atoms_all", "num_bonds", "num_bonds_all", "num_rings", "num_aromatic_rings",
            "physchem_mol_weight", "physchem_logp", "physchem_tpsa", "physchem_qed",
            "physchem_h_acceptors", "physchem_h_donors", "physchem_rotatable_bonds", "physchem_fraction_csp3",
            "complex_ma_score", "complex_ma_runtime", "complex_bertz_score", "complex_bertz_runtime",
            "complex_boettcher_score", "complex_boettcher_runtime", "synth_sa_score"
        ]
        with open(filepath, encoding="utf-8") as f:
            reader = csv.DictReader(f)
            for idx, row in enumerate(reader):
                if row["split"] == split_name:
                    for field in numeric_fields:
                        if field in row and row[field] == "":
                            row[field] = None
                    yield idx, row