gabrielaltay commited on
Commit
601420d
1 Parent(s): facb80c

upload hubscripts/hallmarks_of_cancer_hub.py to hub from bigbio repo

Browse files
Files changed (1) hide show
  1. hallmarks_of_cancer.py +214 -0
hallmarks_of_cancer.py ADDED
@@ -0,0 +1,214 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 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
+ from pathlib import Path
16
+
17
+ import datasets
18
+
19
+ from .bigbiohub import text_features
20
+ from .bigbiohub import BigBioConfig
21
+ from .bigbiohub import Tasks
22
+
23
+ _LANGUAGES = ['English']
24
+ _PUBMED = True
25
+ _LOCAL = False
26
+ _CITATION = """\
27
+ @article{DBLP:journals/bioinformatics/BakerSGAHSK16,
28
+ author = {Simon Baker and
29
+ Ilona Silins and
30
+ Yufan Guo and
31
+ Imran Ali and
32
+ Johan H{\"{o}}gberg and
33
+ Ulla Stenius and
34
+ Anna Korhonen},
35
+ title = {Automatic semantic classification of scientific literature
36
+ according to the hallmarks of cancer},
37
+ journal = {Bioinform.},
38
+ volume = {32},
39
+ number = {3},
40
+ pages = {432--440},
41
+ year = {2016},
42
+ url = {https://doi.org/10.1093/bioinformatics/btv585},
43
+ doi = {10.1093/bioinformatics/btv585},
44
+ timestamp = {Thu, 14 Oct 2021 08:57:44 +0200},
45
+ biburl = {https://dblp.org/rec/journals/bioinformatics/BakerSGAHSK16.bib},
46
+ bibsource = {dblp computer science bibliography, https://dblp.org}
47
+ }
48
+ """
49
+
50
+ _DATASETNAME = "hallmarks_of_cancer"
51
+ _DISPLAYNAME = "Hallmarks of Cancer"
52
+
53
+ _DESCRIPTION = """\
54
+ The Hallmarks of Cancer (HOC) Corpus consists of 1852 PubMed publication
55
+ abstracts manually annotated by experts according to a taxonomy. The taxonomy
56
+ consists of 37 classes in a hierarchy. Zero or more class labels are assigned
57
+ to each sentence in the corpus. The labels are found under the "labels"
58
+ directory, while the tokenized text can be found under "text" directory.
59
+ The filenames are the corresponding PubMed IDs (PMID).
60
+ """
61
+
62
+ _HOMEPAGE = "https://github.com/sb895/Hallmarks-of-Cancer"
63
+
64
+ _LICENSE = 'GNU General Public License v3.0 only'
65
+
66
+ _URLs = {
67
+ "corpus": "https://github.com/sb895/Hallmarks-of-Cancer/archive/refs/heads/master.zip",
68
+ "split_indices": "https://microsoft.github.io/BLURB/sample_code/data_generation.tar.gz",
69
+ }
70
+
71
+ _SUPPORTED_TASKS = [Tasks.TEXT_CLASSIFICATION]
72
+ _SOURCE_VERSION = "1.0.0"
73
+ _BIGBIO_VERSION = "1.0.0"
74
+
75
+ _CLASS_NAMES = [
76
+ "evading growth suppressors",
77
+ "tumor promoting inflammation",
78
+ "enabling replicative immortality",
79
+ "cellular energetics",
80
+ "resisting cell death",
81
+ "activating invasion and metastasis",
82
+ "genomic instability and mutation",
83
+ "none",
84
+ "inducing angiogenesis",
85
+ "sustaining proliferative signaling",
86
+ "avoiding immune destruction",
87
+ ]
88
+
89
+
90
+ class HallmarksOfCancerDataset(datasets.GeneratorBasedBuilder):
91
+ """Hallmarks Of Cancer Dataset"""
92
+
93
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
94
+ BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
95
+
96
+ BUILDER_CONFIGS = [
97
+ BigBioConfig(
98
+ name="hallmarks_of_cancer_source",
99
+ version=SOURCE_VERSION,
100
+ description="Hallmarks of Cancer source schema",
101
+ schema="source",
102
+ subset_id="hallmarks_of_cancer",
103
+ ),
104
+ BigBioConfig(
105
+ name="hallmarks_of_cancer_bigbio_text",
106
+ version=BIGBIO_VERSION,
107
+ description="Hallmarks of Cancer Biomedical schema",
108
+ schema="bigbio_text",
109
+ subset_id="hallmarks_of_cancer",
110
+ ),
111
+ ]
112
+ DEFAULT_CONFIG_NAME = "hallmarks_of_cancer_source"
113
+
114
+ def _info(self):
115
+
116
+ if self.config.schema == "source":
117
+ features = datasets.Features(
118
+ {
119
+ "document_id": datasets.Value("string"),
120
+ "text": datasets.Value("string"),
121
+ "label": [datasets.ClassLabel(names=_CLASS_NAMES)],
122
+ }
123
+ )
124
+
125
+ elif self.config.schema == "bigbio_text":
126
+ features = text_features
127
+
128
+ return datasets.DatasetInfo(
129
+ description=_DESCRIPTION,
130
+ features=features,
131
+ supervised_keys=None,
132
+ homepage=_HOMEPAGE,
133
+ license=str(_LICENSE),
134
+ citation=_CITATION,
135
+ )
136
+
137
+ def _split_generators(self, dl_manager):
138
+ """Returns SplitGenerators."""
139
+ data_dir = dl_manager.download_and_extract(_URLs)
140
+
141
+ return [
142
+ datasets.SplitGenerator(
143
+ name=datasets.Split.TRAIN,
144
+ gen_kwargs={
145
+ "corpuspath": Path(data_dir["corpus"]),
146
+ "indicespath": Path(data_dir["split_indices"])
147
+ / "data_generation/indexing/HoC/train_pmid.tsv",
148
+ },
149
+ ),
150
+ datasets.SplitGenerator(
151
+ name=datasets.Split.TEST,
152
+ gen_kwargs={
153
+ "corpuspath": Path(data_dir["corpus"]),
154
+ "indicespath": Path(data_dir["split_indices"])
155
+ / "data_generation/indexing/HoC/test_pmid.tsv",
156
+ },
157
+ ),
158
+ datasets.SplitGenerator(
159
+ name=datasets.Split.VALIDATION,
160
+ gen_kwargs={
161
+ "corpuspath": Path(data_dir["corpus"]),
162
+ "indicespath": Path(data_dir["split_indices"])
163
+ / "data_generation/indexing/HoC/dev_pmid.tsv",
164
+ },
165
+ ),
166
+ ]
167
+
168
+ def _generate_examples(self, corpuspath: Path, indicespath: Path):
169
+
170
+ indices = indicespath.read_text(encoding="utf8").strip("\n").split(",")
171
+ dataset_dir = corpuspath / "Hallmarks-of-Cancer-master"
172
+ texts_dir = dataset_dir / "text"
173
+ labels_dir = dataset_dir / "labels"
174
+
175
+ uid = 1
176
+ for document_index, document in enumerate(indices):
177
+ text_file = texts_dir / document
178
+ label_file = labels_dir / document
179
+ text = text_file.read_text(encoding="utf8").strip("\n")
180
+ labels = label_file.read_text(encoding="utf8").strip("\n")
181
+
182
+ sentences = text.split("\n")
183
+ labels = labels.split("<")[1:]
184
+
185
+ for example_index, example_pair in enumerate(zip(sentences, labels)):
186
+ sentence, label = example_pair
187
+
188
+ label = label.strip()
189
+
190
+ if label == "":
191
+ label = "none"
192
+
193
+ multi_labels = [m_label.strip() for m_label in label.split("AND")]
194
+ unique_multi_labels = {
195
+ m_label.split("--")[0].lower().lstrip()
196
+ for m_label in multi_labels
197
+ if m_label != "NULL"
198
+ }
199
+
200
+ arrow_file_unique_key = 100 * document_index + example_index
201
+ if self.config.schema == "source":
202
+ yield arrow_file_unique_key, {
203
+ "document_id": f"{text_file.name.split('.')[0]}_{example_index}",
204
+ "text": sentence,
205
+ "label": list(unique_multi_labels),
206
+ }
207
+ elif self.config.schema == "bigbio_text":
208
+ yield arrow_file_unique_key, {
209
+ "id": uid,
210
+ "document_id": f"{text_file.name.split('.')[0]}_{example_index}",
211
+ "text": sentence,
212
+ "labels": list(unique_multi_labels),
213
+ }
214
+ uid += 1