File size: 13,102 Bytes
af21eb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bac84af
af21eb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# coding=utf-8
# Copyright 2022 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.


import os
from typing import Dict, Iterator, List, Tuple

import bioc
import datasets
import pandas as pd

from .bigbiohub import BigBioConfig, Tasks, kb_features

_LOCAL = False
_PUBMED = True
_LANGUAGES = ["English"]

_CITATION = """\
@inproceedings{arighi2017bio,
  title={Bio-ID track overview},
  author={Arighi, Cecilia and Hirschman, Lynette and Lemberger, Thomas and Bayer, Samuel and Liechti, Robin and Comeau, Donald and Wu, Cathy},
  booktitle={Proc. BioCreative Workshop},
  volume={482},
  pages={376},
  year={2017}
}
"""

_DATASETNAME = "bioid"
_DISPLAYNAME = "BIOID"

_DESCRIPTION = """\
The Bio-ID track focuses on entity tagging and ID assignment to selected bioentity types.
The task is to annotate text from figure legends with the entity types and IDs for taxon (organism), gene, protein, miRNA, small molecules,
cellular components, cell types and cell lines, tissues and organs. The track draws on SourceData annotated figure
legends (by panel), in BioC format, and the corresponding full text articles (also BioC format) provided for context.
"""

_HOMEPAGE = "https://biocreative.bioinformatics.udel.edu/tasks/biocreative-vi/track-1/"

_LICENSE = "UNKNOWN"

_URLS = {
    _DATASETNAME: "https://biocreative.bioinformatics.udel.edu/media/store/files/2017/BioIDtraining_2.tar.gz",
}

_SUPPORTED_TASKS = [
    Tasks.NAMED_ENTITY_RECOGNITION,
    Tasks.NAMED_ENTITY_DISAMBIGUATION,
]

_SOURCE_VERSION = "2.0.0"

_BIGBIO_VERSION = "1.0.0"


class BioidDataset(datasets.GeneratorBasedBuilder):
    """TODO: Short description of my dataset."""

    SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
    BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)

    BUILDER_CONFIGS = [
        BigBioConfig(
            name="bioid_source",
            version=SOURCE_VERSION,
            description="bioid source schema",
            schema="source",
            subset_id="bioid",
        ),
        BigBioConfig(
            name="bioid_bigbio_kb",
            version=BIGBIO_VERSION,
            description="bioid BigBio schema",
            schema="bigbio_kb",
            subset_id="bioid",
        ),
    ]

    DEFAULT_CONFIG_NAME = "bioid_source"

    ENTITY_TYPES_NOT_NORMALIZED = [
        "cell",
        "gene",
        "molecule",
        "protein",
        "subcellular",
        "tissue",
        "organism",
    ]

    DB_NAME_TO_ENTITY_TYPE = {
        "BAO": "assay",  # https://www.ebi.ac.uk/ols/ontologies/bao
        "CHEBI": "chemical",
        "CL": "cell",  # https://www.ebi.ac.uk/ols/ontologies/cl
        "Corum": "protein",  # https://mips.helmholtz-muenchen.de/corum/
        "GO": "gene",  # https://geneontology.org/
        "PubChem": "chemical",
        "Rfam": "rna",  # https://rfam.org/
        "Uberon": "anatomy",
        "Cellosaurus": "cellline",
        "NCBI gene": "gene",
        "NCBI taxon": "species",
        "Uniprot": "protein",
    }

    def _info(self) -> datasets.DatasetInfo:

        # Create the source schema; this schema will keep all keys/information/labels as close to the original dataset as possible.
        # You can arbitrarily nest lists and dictionaries.
        # For iterables, use lists over tuples or `datasets.Sequence`
        if self.config.schema == "source":
            features = datasets.Features(
                {
                    "sourcedata_document": datasets.Value("string"),
                    "doi": datasets.Value("string"),
                    "pmc_id": datasets.Value("string"),
                    "figure": datasets.Value("string"),
                    "sourcedata_figure_dir": datasets.Value("string"),
                    "passages": [
                        {
                            "text": datasets.Value("string"),
                            "offset": datasets.Value("int32"),
                            "annotations": [
                                {
                                    "thomas_article": datasets.Value("string"),
                                    "doi": datasets.Value("string"),
                                    "don_article": datasets.Value("int32"),
                                    "figure": datasets.Value("string"),
                                    "annot id": datasets.Value("int32"),
                                    "paper id": datasets.Value("int32"),
                                    "first left": datasets.Value("int32"),
                                    "last right": datasets.Value("int32"),
                                    "length": datasets.Value("int32"),
                                    "byte length": datasets.Value("int32"),
                                    "left alphanum": datasets.Value("string"),
                                    "text": datasets.Value("string"),
                                    "right alphanum": datasets.Value("string"),
                                    "obj": datasets.Value("string"),
                                    "overlap": datasets.Value("string"),
                                    "identical span": datasets.Value("string"),
                                    "overlap_label_count": datasets.Value("int32"),
                                }
                            ],
                        }
                    ],
                }
            )

        # Choose the appropriate bigbio schema for your task and copy it here. You can find information on the schemas in the CONTRIBUTING guide.
        # In rare cases you may get a dataset that supports multiple tasks requiring multiple schemas. In that case you can define multiple bigbio configs with a bigbio_[bigbio_schema_name] format.
        # For example bigbio_kb, bigbio_t2t
        elif self.config.schema == "bigbio_kb":
            features = kb_features

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager) -> List[datasets.SplitGenerator]:
        """Returns SplitGenerators."""
        urls = _URLS[_DATASETNAME]
        data_dir = dl_manager.download_and_extract(urls)

        # Not all datasets have predefined canonical train/val/test splits.
        # If your dataset has no predefined splits, use datasets.Split.TRAIN for all of the data.

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # Whatever you put in gen_kwargs will be passed to _generate_examples
                gen_kwargs={
                    "data_dir": data_dir,
                    "split": "train",
                },
            ),
        ]

    def load_annotations(self, path: str) -> Dict[str, Dict]:
        """
        We load annotations from `annotations.csv`
        becuase the one in the BioC xml files have offsets issues.
        """

        df = pd.read_csv(path, sep=",")

        df.fillna(-1, inplace=True)

        annotations: Dict[str, Dict] = {}

        for record in df.to_dict("records"):

            article_id = str(record["don_article"])

            if article_id not in annotations:
                annotations[article_id] = {}

            figure = record["figure"]

            if figure not in annotations:
                annotations[article_id][figure] = []

            annotations[article_id][figure].append(record)

        return annotations

    def load_data(self, data_dir: str) -> List[Dict]:
        """
        Compose text from BioC files with annotations from `annotations.csv`.
        We load annotations from `annotations.csv`
        becuase the one in the BioC xml files have offsets issues.
        """

        text_dir = os.path.join(data_dir, "BioIDtraining_2", "caption_bioc")
        annotation_file = os.path.join(data_dir, "BioIDtraining_2", "annotations.csv")

        annotations = self.load_annotations(path=annotation_file)

        data = []

        for file_name in os.listdir(text_dir):

            if file_name.startswith(".") or not file_name.endswith(".xml"):
                continue

            collection = bioc.load(os.path.join(text_dir, file_name))

            for document in collection.documents:

                item = document.infons

                assert (
                    len(document.passages) == 1
                ), "Document contains more than one passage (figure caption). This is not expected!"

                passage = document.passages[0]

                article_id = document.infons["pmc_id"]
                figure = document.infons["sourcedata_figure_dir"]

                try:
                    passage.annotations = annotations[article_id][figure]
                except KeyError:
                    passage.annotations = []

                item["passages"] = [
                    {
                        "text": passage.text,
                        "annotations": passage.annotations,
                        "offset": passage.offset,
                    }
                ]

                data.append(item)

        return data

    def get_entity(self, normalization: str) -> Tuple[str, List[Dict]]:
        """
        Compile normalization information from annotation
        """

        db_name_ids = normalization.split(":")

        db_ids = None

        # ids from cellosaurus do not have db name
        if len(db_name_ids) == 1:
            db_name = "Cellosaurus"
            db_ids = db_name_ids[0].split("|")
        else:
            # quirk
            if db_name_ids[0] == "CVCL_6412|CL":
                db_name = "Cellosaurus"
                db_ids = ["CVCL_6412"]
            else:
                db_name = db_name_ids[0]
                # db_name hints for entity type: skip if does not provide normalization
                if db_name not in self.ENTITY_TYPES_NOT_NORMALIZED:
                    # Uberon:UBERON:0001891
                    # NCBI gene:9341
                    db_id_idx = 2 if db_name == "Uberon" else 1
                    db_ids = [i.split(":")[db_id_idx] for i in normalization.split("|")]

        normalized = (
            [{"db_name": db_name, "db_id": i} for i in db_ids]
            if db_ids is not None
            else []
        )

        # ideally we should have canonical entity types w/ a  dedicated enum like `Tasks`

        if db_name in self.ENTITY_TYPES_NOT_NORMALIZED:
            entity_type = db_name
        else:
            entity_type = self.DB_NAME_TO_ENTITY_TYPE[db_name]

        return entity_type, normalized

    def _generate_examples(
        self, data_dir: str, split: str
    ) -> Iterator[Tuple[int, Dict]]:
        """Yields examples as (key, example) tuples."""

        data = self.load_data(data_dir=data_dir)

        if self.config.schema == "source":
            for uid, document in enumerate(data):
                yield uid, document

        elif self.config.schema == "bigbio_kb":

            uid = 0  # global unique id

            for document in data:

                kb_document = {
                    "id": uid,
                    "document_id": document["pmc_id"],
                    "passages": [],
                    "entities": [],
                    "relations": [],
                    "events": [],
                    "coreferences": [],
                }

                uid += 1

                for passage in document["passages"]:
                    kb_document["passages"].append(
                        {
                            "id": uid,
                            "type": "figure_caption",
                            "text": [passage["text"]],
                            "offsets": [[0, len(passage["text"])]],
                        }
                    )
                    uid += 1

                    for a in passage["annotations"]:

                        entity_type, normalized = self.get_entity(a["obj"])

                        kb_document["entities"].append(
                            {
                                "id": uid,
                                "text": [a["text"]],
                                "type": entity_type,
                                "offsets": [[a["first left"], a["last right"]]],
                                "normalized": normalized,
                            }
                        )

                        uid += 1

                yield uid, kb_document