jglaser commited on
Commit
434fe63
1 Parent(s): 89a844e

add datasets script

Browse files
Files changed (1) hide show
  1. binding_affinity.py +164 -0
binding_affinity.py ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """TODO: A dataset of protein sequences, ligand SMILES and binding affinities."""
16
+
17
+ import pandas
18
+ import os
19
+
20
+ import datasets
21
+
22
+
23
+ # TODO: Add BibTeX citation
24
+ # Find for instance the citation on arxiv or on the dataset repo/website
25
+ _CITATION = """\
26
+ @InProceedings{huggingface:dataset,
27
+ title = {jglaser/binding_affinity},
28
+ author={Jens Glaser, ORNL
29
+ },
30
+ year={2021}
31
+ }
32
+ """
33
+
34
+ # TODO: Add description of the dataset here
35
+ # You can copy an official description
36
+ _DESCRIPTION = """\
37
+ A dataset to refine language models on protein-ligand binding affinity prediction.
38
+ """
39
+
40
+ # TODO: Add a link to an official homepage for the dataset here
41
+ _HOMEPAGE = ""
42
+
43
+ # TODO: Add the licence for the dataset here if you can find it
44
+ _LICENSE = "BSD two-clause"
45
+
46
+ # TODO: Add link to the official dataset URLs here
47
+ # The HuggingFace dataset library don't host the datasets but only point to the original files
48
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
49
+ _URLs = {
50
+ 'affinities': "https://huggingface.co/datasets/jglaser/binding_affinity/tree/main/data/all.parquet",
51
+ }
52
+
53
+
54
+ # TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
55
+ class BindingAffinity(datasets.ArrowBasedBuilder):
56
+ """List of protein sequences, ligand SMILES and binding affinities."""
57
+
58
+ VERSION = datasets.Version("1.0.0")
59
+
60
+ # If you don't want/need to define several sub-sets in your dataset,
61
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
62
+
63
+ # If you need to make complex sub-parts in the datasets with configurable options
64
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
65
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
66
+
67
+ # You will be able to load one or the other configurations in the following list with
68
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
69
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
70
+ # BUILDER_CONFIGS = [
71
+ # datasets.BuilderConfig(name="first_domain", version=VERSION, description="This part of my dataset covers a first domain"),
72
+ # datasets.BuilderConfig(name="second_domain", version=VERSION, description="This part of my dataset covers a second domain"),
73
+ #]
74
+
75
+ DEFAULT_CONFIG_NAME = "affinities" # It's not mandatory to have a default configuration. Just use one if it make sense.
76
+
77
+ def _info(self):
78
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
79
+ #if self.config.name == "first_domain": # This is the name of the configuration selected in BUILDER_CONFIGS above
80
+ # features = datasets.Features(
81
+ # {
82
+ # "sentence": datasets.Value("string"),
83
+ # "option1": datasets.Value("string"),
84
+ # "answer": datasets.Value("string")
85
+ # # These are the features of your dataset like images, labels ...
86
+ # }
87
+ # )
88
+ #else: # This is an example to show how to have different features for "first_domain" and "second_domain"
89
+ features = datasets.Features(
90
+ {
91
+ "affinity_uM": datasets.Value("float"),
92
+ "neg_log10_affinity_M": datasets.Value("float"),
93
+ # These are the features of your dataset like images, labels ...
94
+ }
95
+ )
96
+ return datasets.DatasetInfo(
97
+ # This is the description that will appear on the datasets page.
98
+ description=_DESCRIPTION,
99
+ # This defines the different columns of the dataset and their types
100
+ features=features, # Here we define them above because they are different between the two configurations
101
+ # If there's a common (input, target) tuple from the features,
102
+ # specify them here. They'll be used if as_supervised=True in
103
+ # builder.as_dataset.
104
+ supervised_keys=None,
105
+ # Homepage of the dataset for documentation
106
+ homepage=_HOMEPAGE,
107
+ # License for the dataset if available
108
+ license=_LICENSE,
109
+ # Citation for the dataset
110
+ citation=_CITATION,
111
+ )
112
+
113
+ def _split_generators(self, dl_manager):
114
+ """Returns SplitGenerators."""
115
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
116
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
117
+
118
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
119
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
120
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
121
+ my_urls = _URLs[self.config.name]
122
+ data_dir = dl_manager.download_and_extract(my_urls)
123
+ return [
124
+ datasets.SplitGenerator(
125
+ name=datasets.Split.TRAIN,
126
+ # These kwargs will be passed to _generate_examples
127
+ gen_kwargs={
128
+ "filepath": os.path.join(data_dir, "train.parquet"),
129
+ "split": "train",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ # These kwargs will be passed to _generate_examples
135
+ gen_kwargs={
136
+ "filepath": os.path.join(data_dir, "test.parquet"),
137
+ "split": "test"
138
+ },
139
+ ),
140
+ datasets.SplitGenerator(
141
+ name=datasets.Split.VALIDATION,
142
+ # These kwargs will be passed to _generate_examples
143
+ gen_kwargs={
144
+ "filepath": os.path.join(data_dir, "dev.parquet"),
145
+ "split": "dev",
146
+ },
147
+ ),
148
+ ]
149
+
150
+ def _generate_examples(
151
+ self, filepath, split # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
152
+ ):
153
+ """ Yields examples as (key, example) tuples. """
154
+ # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
155
+ # The `key` is here for legacy reason (tfds) and is not important in itself.
156
+
157
+ df = pd.read_parquet(filepath)
158
+ for k, row in df.iterrows():
159
+ yield id_, {
160
+ "seq": row["seq"],
161
+ "smiles": row["smiles"],
162
+ "neg_log10_affinity_M": row["neg_log10_affinity_M"],
163
+ "affinity_uM": row["affinity_uM"],
164
+ }