Datasets:
EMBO
/

Languages:
English
ArXiv:
DOI:
License:
File size: 18,458 Bytes
59171e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ed364d
59171e9
 
a1e6622
59171e9
4ed364d
59171e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ed364d
 
 
 
 
 
 
 
59171e9
 
 
 
 
4ed364d
 
 
 
 
 
 
59171e9
 
 
 
 
 
 
 
 
 
 
f2bda57
4ed364d
06354d4
59171e9
4ed364d
 
 
 
 
59fb3d8
7285026
 
 
 
 
4ed364d
 
 
 
 
59fb3d8
 
4ed364d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59fb3d8
4ed364d
08f5766
59171e9
 
 
 
4ed364d
 
 
 
59171e9
b114e70
674333f
59171e9
 
 
 
 
 
 
 
 
 
4ed364d
59171e9
 
b114e70
674333f
59171e9
 
 
 
 
 
 
 
 
 
4ed364d
59171e9
 
b114e70
674333f
59171e9
 
 
 
 
 
 
 
 
 
4ed364d
59171e9
 
 
 
4ed364d
ef7da8f
 
674333f
59171e9
 
 
 
 
 
 
 
4ed364d
 
 
 
59171e9
674333f
59171e9
 
 
4ed364d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59171e9
 
 
 
 
 
 
 
4ed364d
59171e9
 
4ed364d
 
59171e9
94e03fc
1961a1a
4ed364d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94e03fc
59171e9
4ed364d
59171e9
 
 
 
4ed364d
59171e9
 
 
4ed364d
59171e9
 
 
4ed364d
59171e9
 
 
 
 
 
 
 
4ed364d
 
 
 
 
59171e9
 
 
4ed364d
49caefd
59171e9
 
 
 
4ed364d
59171e9
 
 
 
 
 
4ed364d
59171e9
 
 
4ed364d
59171e9
 
 
 
2a46d4a
4ed364d
59171e9
 
 
 
 
 
4ed364d
59171e9
 
 
 
 
 
 
 
 
 
4ed364d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59171e9
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# coding=utf-8
# 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.


# template from : https://github.com/huggingface/datasets/blob/master/templates/new_dataset_script.py

from __future__ import absolute_import, division, print_function

import json
import os
import datasets

_BASE_URL = "https://huggingface.co/datasets/EMBO/SourceData/resolve/main/"


class SourceData(datasets.GeneratorBasedBuilder):
    """SourceDataNLP provides datasets to train NLP tasks in cell and molecular biology."""

    _NER_LABEL_NAMES = [
        "O",
        "B-SMALL_MOLECULE",
        "I-SMALL_MOLECULE",
        "B-GENEPROD",
        "I-GENEPROD",
        "B-SUBCELLULAR",
        "I-SUBCELLULAR",
        "B-CELL_TYPE",
        "I-CELL_TYPE",
        "B-TISSUE",
        "I-TISSUE",
        "B-ORGANISM",
        "I-ORGANISM",
        "B-EXP_ASSAY",
        "I-EXP_ASSAY",
        "B-DISEASE",
        "I-DISEASE",
        "B-CELL_LINE",
        "I-CELL_LINE",
    ]
    _SEMANTIC_ROLES = [
        "O",
        "B-CONTROLLED_VAR",
        "I-CONTROLLED_VAR",
        "B-MEASURED_VAR",
        "I-MEASURED_VAR",
    ]
    _PANEL_START_NAMES = ["O", "B-PANEL_START", "I-PANEL_START"]
    _ROLES_MULTI = ["O", "GENEPROD", "SMALL_MOLECULE"]

    _CITATION = """\
@article{abreu2023sourcedata,
  title={The SourceData-NLP dataset: integrating curation into scientific publishing
  for training large language models},
  author={Abreu-Vicente, Jorge and Sonntag, Hannah and Eidens, Thomas and Lemberger, Thomas},
  journal={arXiv preprint arXiv:2310.20440},
  year={2023}
}
    """

    _DESCRIPTION = """\
    This dataset is based on the SourceData database and is intented to facilitate training of NLP tasks in the cell and molecualr biology domain.
    """

    _HOMEPAGE = "https://huggingface.co/datasets/EMBO/SourceData"

    _LICENSE = "CC-BY 4.0"

    DEFAULT_CONFIG_NAME = "NER"

    _LATEST_VERSION = "2.0.3"  # Should this be updated to 2.0.3

    def _info(self):
        VERSION = (
            self.config.version
            if self.config.version not in ["0.0.0", "latest"]
            else self._LATEST_VERSION
        )
        self._URLS = {
            "NER": f"{_BASE_URL}token_classification/v_{VERSION}/ner/",
            "PANELIZATION": f"{_BASE_URL}token_classification/v_{VERSION}/panelization/",
            "ROLES_GP": f"{_BASE_URL}token_classification/v_{VERSION}/roles_gene/",
            "ROLES_SM": f"{_BASE_URL}token_classification/v_{VERSION}/roles_small_mol/",
            "ROLES_MULTI": f"{_BASE_URL}token_classification/v_{VERSION}/roles_multi/",
            "FULL": os.path.join(
                _BASE_URL,
                "bigbio",
                # f"v_{VERSION}",
            ),
        }
        self.BUILDER_CONFIGS = [
            datasets.BuilderConfig(
                name="NER",
                version=VERSION,
                description="Dataset for named-entity recognition.",
            ),
            datasets.BuilderConfig(
                name="PANELIZATION",
                version=VERSION,
                description="Dataset to separate figure captions into panels.",
            ),
            datasets.BuilderConfig(
                name="ROLES_GP",
                version=VERSION,
                description="Dataset for semantic roles of gene products.",
            ),
            datasets.BuilderConfig(
                name="ROLES_SM",
                version=VERSION,
                description="Dataset for semantic roles of small molecules.",
            ),
            datasets.BuilderConfig(
                name="ROLES_MULTI",
                version=VERSION,
                description="Dataset to train roles. ROLES_GP and ROLES_SM at once.",
            ),
            datasets.BuilderConfig(
                name="FULL",
                version=VERSION,
                description="Full dataset including all NER + entity linking annotations, links to figure images, etc.",
            ),
            # datasets.BuilderConfig(
            #     name="BIGBIO_KB",
            #     version=VERSION,
            #     description="Full dataset formatted according to BigBio KB schema (see https://huggingface.co/bigbio).  Includes all NER + entity linking annotations.",
            # ),
        ]

        if self.config.name in ["NER", "default"]:
            features = datasets.Features(
                {
                    "words": datasets.Sequence(feature=datasets.Value("string")),
                    "labels": datasets.Sequence(
                        feature=datasets.ClassLabel(
                            num_classes=len(self._NER_LABEL_NAMES),
                            names=self._NER_LABEL_NAMES,
                        )
                    ),
                    # "is_category": datasets.Sequence(feature=datasets.Value("int8")),
                    "tag_mask": datasets.Sequence(feature=datasets.Value("int8")),
                    "text": datasets.Value("string"),
                }
            )
        elif self.config.name == "ROLES_GP":
            features = datasets.Features(
                {
                    "words": datasets.Sequence(feature=datasets.Value("string")),
                    "labels": datasets.Sequence(
                        feature=datasets.ClassLabel(
                            num_classes=len(self._SEMANTIC_ROLES),
                            names=self._SEMANTIC_ROLES,
                        )
                    ),
                    # "is_category": datasets.Sequence(feature=datasets.Value("int8")),
                    "tag_mask": datasets.Sequence(feature=datasets.Value("int8")),
                    "text": datasets.Value("string"),
                }
            )
        elif self.config.name == "ROLES_SM":
            features = datasets.Features(
                {
                    "words": datasets.Sequence(feature=datasets.Value("string")),
                    "labels": datasets.Sequence(
                        feature=datasets.ClassLabel(
                            num_classes=len(self._SEMANTIC_ROLES),
                            names=self._SEMANTIC_ROLES,
                        )
                    ),
                    # "is_category": datasets.Sequence(feature=datasets.Value("int8")),
                    "tag_mask": datasets.Sequence(feature=datasets.Value("int8")),
                    "text": datasets.Value("string"),
                }
            )
        elif self.config.name == "ROLES_MULTI":
            features = datasets.Features(
                {
                    "words": datasets.Sequence(feature=datasets.Value("string")),
                    "labels": datasets.Sequence(
                        feature=datasets.ClassLabel(
                            num_classes=len(self._SEMANTIC_ROLES),
                            names=self._SEMANTIC_ROLES,
                        )
                    ),
                    "is_category": datasets.Sequence(
                        feature=datasets.ClassLabel(
                            num_classes=len(self._ROLES_MULTI), names=self._ROLES_MULTI
                        )
                    ),
                    "tag_mask": datasets.Sequence(feature=datasets.Value("int8")),
                    "text": datasets.Value("string"),
                }
            )
        elif self.config.name == "PANELIZATION":
            features = datasets.Features(
                {
                    "words": datasets.Sequence(feature=datasets.Value("string")),
                    "labels": datasets.Sequence(
                        feature=datasets.ClassLabel(
                            num_classes=len(self._PANEL_START_NAMES),
                            names=self._PANEL_START_NAMES,
                        )
                    ),
                    "tag_mask": datasets.Sequence(feature=datasets.Value("int8")),
                }
            )

        elif self.config.name == "FULL":
            features = datasets.Features(
                {
                    "doi": datasets.Value("string"),
                    "abstract": datasets.Value("string"),
                    # "split": datasets.Value("string"),
                    "figures": [
                        {
                            "fig_id": datasets.Value("string"),
                            "label": datasets.Value("string"),
                            "fig_graphic_url": datasets.Value("string"),
                            "panels": [
                                {
                                    "panel_id": datasets.Value("string"),
                                    "text": datasets.Value("string"),
                                    "panel_graphic_url": datasets.Value("string"),
                                    "entities": [
                                        {
                                            "annotation_id": datasets.Value("string"),
                                            "source": datasets.Value("string"),
                                            "category": datasets.Value("string"),
                                            "entity_type": datasets.Value("string"),
                                            "role": datasets.Value("string"),
                                            "text": datasets.Value("string"),
                                            "ext_ids": datasets.Value("string"),
                                            "norm_text": datasets.Value("string"),
                                            "ext_dbs": datasets.Value("string"),
                                            "in_caption": datasets.Value("bool"),
                                            "ext_names": datasets.Value("string"),
                                            "ext_tax_ids": datasets.Value("string"),
                                            "ext_tax_names": datasets.Value("string"),
                                            "ext_urls": datasets.Value("string"),
                                            "offsets": [datasets.Value("int64")],
                                        }
                                    ],
                                }
                            ],
                        }
                    ],
                }
            )

        return datasets.DatasetInfo(
            description=self._DESCRIPTION,
            features=features,
            supervised_keys=("words", "label_ids"),
            homepage=self._HOMEPAGE,
            license=self._LICENSE,
            citation=self._CITATION,
        )

    def _split_generators(self, dl_manager: datasets.DownloadManager):
        """Returns SplitGenerators.
        Uses local files if a data_dir is specified. Otherwise downloads the files from their official url.
        """

        try:
            config_name = self.config.name if self.config.name != "default" else "NER"

            if config_name == "FULL":
                url = os.path.join(
                    self._URLS[config_name],
                    #    "source_data_full.zip"
                    "source_data_json_splits_2.0.2.zip",
                )
                data_dir = dl_manager.download_and_extract(url)
                data_files = [
                    os.path.join(data_dir, filename)
                    for filename in ["train.jsonl", "test.jsonl", "validation.jsonl"]
                ]
            else:
                urls = [
                    os.path.join(self._URLS[config_name], "train.jsonl"),
                    os.path.join(self._URLS[config_name], "test.jsonl"),
                    os.path.join(self._URLS[config_name], "validation.jsonl"),
                ]
                data_files = dl_manager.download(urls)
        except:
            raise ValueError(f"unkonwn config name: {self.config.name}")

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={"filepath": data_files[0]},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={"filepath": data_files[1]},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={"filepath": data_files[2]},
            ),
        ]

    def _generate_examples(self, filepath):
        """Yields examples. This method will receive as arguments the `gen_kwargs` defined in the previous `_split_generators` method.
        It is in charge of opening the given file and yielding (key, example) tuples from the dataset
        The key is not important, it's more here for legacy reason (legacy from tfds)"""

        no_panels = 0
        no_entities = 0
        has_panels = 0
        has_entities = 0

        with open(filepath, encoding="utf-8") as f:
            # logger.info("⏳ Generating examples from = %s", filepath)
            for id_, row in enumerate(f):
                data = json.loads(row.strip())
                if self.config.name in ["NER", "default"]:
                    yield id_, {
                        "words": data["words"],
                        "labels": data["labels"],
                        "tag_mask": data["is_category"],
                        "text": data["text"],
                    }
                elif self.config.name == "ROLES_GP":
                    yield id_, {
                        "words": data["words"],
                        "labels": data["labels"],
                        "tag_mask": data["is_category"],
                        "text": data["text"],
                    }
                elif self.config.name == "ROLES_MULTI":
                    labels = data["labels"]
                    tag_mask = [1 if t != 0 else 0 for t in labels]
                    yield id_, {
                        "words": data["words"],
                        "labels": data["labels"],
                        "tag_mask": tag_mask,
                        "is_category": data["is_category"],
                        "text": data["text"],
                    }
                elif self.config.name == "ROLES_SM":
                    yield id_, {
                        "words": data["words"],
                        "labels": data["labels"],
                        "tag_mask": data["is_category"],
                        "text": data["text"],
                    }
                elif self.config.name == "PANELIZATION":
                    labels = data["labels"]
                    tag_mask = [1 if t == "B-PANEL_START" else 0 for t in labels]
                    yield id_, {
                        "words": data["words"],
                        "labels": data["labels"],
                        "tag_mask": tag_mask,
                    }

                elif self.config.name == "FULL":
                    doc_figs = data["figures"]
                    all_figures = []
                    for fig in doc_figs:
                        all_panels = []
                        figure = {
                            "fig_id": fig["fig_id"],
                            "label": fig["label"],
                            "fig_graphic_url": fig["fig_graphic_url"],
                        }

                        for p in fig["panels"]:
                            panel = {
                                "panel_id": p["panel_id"],
                                "text": p["text"].strip(),
                                "panel_graphic_url": p["panel_graphic_url"],
                                "entities": [
                                    {
                                        "annotation_id": t["tag_id"],
                                        "source": t["source"],
                                        "category": t["category"],
                                        "entity_type": t["entity_type"],
                                        "role": t["role"],
                                        "text": t["text"],
                                        "ext_ids": t["ext_ids"],
                                        "norm_text": t["norm_text"],
                                        "ext_dbs": t["ext_dbs"],
                                        "in_caption": bool(t["in_caption"]),
                                        "ext_names": t["ext_names"],
                                        "ext_tax_ids": t["ext_tax_ids"],
                                        "ext_tax_names": t["ext_tax_names"],
                                        "ext_urls": t["ext_urls"],
                                        "offsets": t["local_offsets"],
                                    }
                                    for t in p["tags"]
                                ],
                            }
                            for e in panel["entities"]:
                                assert type(e["offsets"]) == list
                            if len(panel["entities"]) == 0:
                                no_entities += 1
                                continue
                            else:
                                has_entities += 1
                            all_panels.append(panel)

                        figure["panels"] = all_panels

                        # Pass on all figures that aren't split into panels
                        if len(all_panels) == 0:
                            no_panels += 1
                            continue
                        else:
                            has_panels += 1
                        all_figures.append(figure)

                    output = {
                        "doi": data["doi"],
                        "abstract": data["abstract"],
                        "figures": all_figures,
                    }
                    yield id_, output