zinc20 / zinc20.py
zpn's picture
zinc (#1)
104c00a
raw
history blame
9.86 kB
# 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"],
}