File size: 9,857 Bytes
104c00a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""TODO: File Description"""

import json
import gzip
import os

import datasets

# Using https://www.bibtex.com/c/doi-to-bibtex-converter/ (doi: 10.1093/nar/gkv951)
_CITATION = """\
@article{Irwin2020,
  doi = {10.1021/acs.jcim.0c00675},
  url = {https://doi.org/10.1021/acs.jcim.0c00675},
  year = {2020},
  month = oct,
  publisher = {American Chemical Society ({ACS})},
  volume = {60},
  number = {12},
  pages = {6065--6073},
  author = {John J. Irwin and Khanh G. Tang and Jennifer Young and Chinzorig Dandarchuluun and Benjamin R. Wong and Munkhzul Khurelbaatar and Yurii S. Moroz and John Mayfield and Roger A. Sayle},
  title = {{ZINC}20{\textemdash}A Free Ultralarge-Scale Chemical Database for Ligand Discovery},
  journal = {Journal of Chemical Information and Modeling}
}

"""

# You can copy an official description
_DESCRIPTION = """\
This dataset contains ~1B molecules from ZINC20, with their SMILES and SELFIES representations.
"""

_HOMEPAGE = "https://zinc20.docking.org/"

_LICENSE = "Open Data Commons Open Database License"

# this list of files has been preshuffled so we can use the same splits everytime
FILES = [
   "zinc_processed/smiles_all_01_clean.jsonl.gz",
   "zinc_processed/smiles_all_57_clean.jsonl.gz",
   "zinc_processed/smiles_all_86_clean.jsonl.gz",
   "zinc_processed/smiles_all_90_clean.jsonl.gz",
   "zinc_processed/smiles_all_66_clean.jsonl.gz",
   "zinc_processed/smiles_all_37_clean.jsonl.gz",
   "zinc_processed/smiles_all_98_clean.jsonl.gz",
   "zinc_processed/smiles_all_24_clean.jsonl.gz",
   "zinc_processed/smiles_all_69_clean.jsonl.gz",
   "zinc_processed/smiles_all_95_clean.jsonl.gz",
   "zinc_processed/smiles_all_42_clean.jsonl.gz",
   "zinc_processed/smiles_all_13_clean.jsonl.gz",
   "zinc_processed/smiles_all_70_clean.jsonl.gz",
   "zinc_processed/smiles_all_58_clean.jsonl.gz",
   "zinc_processed/smiles_all_67_clean.jsonl.gz",
   "zinc_processed/smiles_all_94_clean.jsonl.gz",
   "zinc_processed/smiles_all_54_clean.jsonl.gz",
   "zinc_processed/smiles_all_89_clean.jsonl.gz",
   "zinc_processed/smiles_all_41_clean.jsonl.gz",
   "zinc_processed/smiles_all_15_clean.jsonl.gz",
   "zinc_processed/smiles_all_81_clean.jsonl.gz",
   "zinc_processed/smiles_all_33_clean.jsonl.gz",
   "zinc_processed/smiles_all_25_clean.jsonl.gz",
   "zinc_processed/smiles_all_07_clean.jsonl.gz",
   "zinc_processed/smiles_all_28_clean.jsonl.gz",
   "zinc_processed/smiles_all_20_clean.jsonl.gz",
   "zinc_processed/smiles_all_05_clean.jsonl.gz",
   "zinc_processed/smiles_all_74_clean.jsonl.gz",
   "zinc_processed/smiles_all_84_clean.jsonl.gz",
   "zinc_processed/smiles_all_34_clean.jsonl.gz",
   "zinc_processed/smiles_all_02_clean.jsonl.gz",
   "zinc_processed/smiles_all_43_clean.jsonl.gz",
   "zinc_processed/smiles_all_62_clean.jsonl.gz",
   "zinc_processed/smiles_all_72_clean.jsonl.gz",
   "zinc_processed/smiles_all_16_clean.jsonl.gz",
   "zinc_processed/smiles_all_64_clean.jsonl.gz",
   "zinc_processed/smiles_all_63_clean.jsonl.gz",
   "zinc_processed/smiles_all_59_clean.jsonl.gz",
   "zinc_processed/smiles_all_35_clean.jsonl.gz",
   "zinc_processed/smiles_all_21_clean.jsonl.gz",
   "zinc_processed/smiles_all_04_clean.jsonl.gz",
   "zinc_processed/smiles_all_39_clean.jsonl.gz",
   "zinc_processed/smiles_all_78_clean.jsonl.gz",
   "zinc_processed/smiles_all_96_clean.jsonl.gz",
   "zinc_processed/smiles_all_08_clean.jsonl.gz",
   "zinc_processed/smiles_all_52_clean.jsonl.gz",
   "zinc_processed/smiles_all_09_clean.jsonl.gz",
   "zinc_processed/smiles_all_56_clean.jsonl.gz",
   "zinc_processed/smiles_all_44_clean.jsonl.gz",
   "zinc_processed/smiles_all_77_clean.jsonl.gz",
   "zinc_processed/smiles_all_32_clean.jsonl.gz",
   "zinc_processed/smiles_all_30_clean.jsonl.gz",
   "zinc_processed/smiles_all_40_clean.jsonl.gz",
   "zinc_processed/smiles_all_23_clean.jsonl.gz",
   "zinc_processed/smiles_all_82_clean.jsonl.gz",
   "zinc_processed/smiles_all_53_clean.jsonl.gz",
   "zinc_processed/smiles_all_46_clean.jsonl.gz",
   "zinc_processed/smiles_all_18_clean.jsonl.gz",
   "zinc_processed/smiles_all_60_clean.jsonl.gz",
   "zinc_processed/smiles_all_19_clean.jsonl.gz",
   "zinc_processed/smiles_all_50_clean.jsonl.gz",
   "zinc_processed/smiles_all_31_clean.jsonl.gz",
   "zinc_processed/smiles_all_65_clean.jsonl.gz",
   "zinc_processed/smiles_all_10_clean.jsonl.gz",
   "zinc_processed/smiles_all_55_clean.jsonl.gz",
   "zinc_processed/smiles_all_48_clean.jsonl.gz",
   "zinc_processed/smiles_all_27_clean.jsonl.gz",
   "zinc_processed/smiles_all_17_clean.jsonl.gz",
   "zinc_processed/smiles_all_29_clean.jsonl.gz",
   "zinc_processed/smiles_all_88_clean.jsonl.gz",
   "zinc_processed/smiles_all_99_clean.jsonl.gz",
   "zinc_processed/smiles_all_45_clean.jsonl.gz",
   "zinc_processed/smiles_all_49_clean.jsonl.gz",
   "zinc_processed/smiles_all_92_clean.jsonl.gz",
   "zinc_processed/smiles_all_36_clean.jsonl.gz",
   "zinc_processed/smiles_all_11_clean.jsonl.gz",
   "zinc_processed/smiles_all_85_clean.jsonl.gz",
   "zinc_processed/smiles_all_26_clean.jsonl.gz",
   "zinc_processed/smiles_all_76_clean.jsonl.gz",
   "zinc_processed/smiles_all_83_clean.jsonl.gz",
   "zinc_processed/smiles_all_97_clean.jsonl.gz",
   "zinc_processed/smiles_all_93_clean.jsonl.gz",
   "zinc_processed/smiles_all_87_clean.jsonl.gz",
   "zinc_processed/smiles_all_79_clean.jsonl.gz",
   "zinc_processed/smiles_all_06_clean.jsonl.gz",
   "zinc_processed/smiles_all_12_clean.jsonl.gz",
   "zinc_processed/smiles_all_71_clean.jsonl.gz",
   "zinc_processed/smiles_all_73_clean.jsonl.gz",
   "zinc_processed/smiles_all_68_clean.jsonl.gz",
   "zinc_processed/smiles_all_00_clean.jsonl.gz",
   "zinc_processed/smiles_all_61_clean.jsonl.gz",
   "zinc_processed/smiles_all_14_clean.jsonl.gz",
   "zinc_processed/smiles_all_51_clean.jsonl.gz",
   "zinc_processed/smiles_all_80_clean.jsonl.gz",
   "zinc_processed/smiles_all_47_clean.jsonl.gz",
   "zinc_processed/smiles_all_22_clean.jsonl.gz",
   "zinc_processed/smiles_all_75_clean.jsonl.gz",
   "zinc_processed/smiles_all_38_clean.jsonl.gz",
   "zinc_processed/smiles_all_03_clean.jsonl.gz",
   "zinc_processed/smiles_all_91_clean.jsonl.gz"
]

class PubchemSelfies(datasets.GeneratorBasedBuilder):
    """A dataset of ZINC20 molecules represented as SELFIES."""

    VERSION = datasets.Version("1.1.0")

    # You will be able to load one or the other configurations in the following list with
    BUILDER_CONFIG = datasets.BuilderConfig(
        version=VERSION, description="A dataset of PubChem molecules represented as SELFIES."
    )

    def _info(self):
        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # This defines the different columns of the dataset and their types
            features=datasets.Features(
                {
                    "selfies": datasets.Value("string"),
                    "smiles": datasets.Value("string"),
                    "id": datasets.Value("string"),
                }
            ),
            # Homepage of the dataset for documentation
            homepage=_HOMEPAGE,
            # License for the dataset if available
            license=_LICENSE,
            # Citation for the dataset
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        downloaded_files = dl_manager.download(FILES)

        train_len = int(len(downloaded_files) * 0.8)
        valid_len = int(len(downloaded_files) * 0.1)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "subdirs": downloaded_files[:train_len],
                    "split": "train",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "subdirs": downloaded_files[train_len : train_len + valid_len],
                    "split": "valid",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "subdirs": downloaded_files[-valid_len:],
                    "split": "test",
                },
            ),
        ]

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, subdirs, split):
        # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
        for filepath in subdirs:
            with gzip.open(open(filepath, "rb"), "rt", encoding="utf-8") as f:
                for row_idx, row in enumerate(f):
                    data = json.loads(row)
                    key = f"{os.path.basename(filepath)}_{row_idx}"
                    yield key, {
                        "smiles": data["smiles"],
                        "selfies": data["selfies"],
                        "id": data["id"],
                    }