File size: 11,786 Bytes
e917b46 |
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 |
# 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.
"""
This dataset contains 500 PubMed articles manually annotated with mutation
mentions of various kinds and dbsnp normalizations for each of them. In
addition, it contains variant normalization options such as allele-specific
identifiers from the ClinGen Allele Registry It can be used for NER tasks and
NED tasks, This dataset does NOT have splits.
"""
import itertools
import datasets
from bioc import pubtator
from .bigbiohub import kb_features
from .bigbiohub import BigBioConfig
from .bigbiohub import Tasks
_CITATION = """\
@misc{https://doi.org/10.48550/arxiv.2204.03637,
title = {tmVar 3.0: an improved variant concept recognition and normalization tool},
author = {
Wei, Chih-Hsuan and Allot, Alexis and Riehle, Kevin and Milosavljevic,
Aleksandar and Lu, Zhiyong
},
year = 2022,
publisher = {arXiv},
doi = {10.48550/ARXIV.2204.03637},
url = {https://arxiv.org/abs/2204.03637},
copyright = {Creative Commons Attribution 4.0 International},
keywords = {
Computation and Language (cs.CL), FOS: Computer and information sciences,
FOS: Computer and information sciences
}
}
"""
_LANGUAGES = ['English']
_PUBMED = True
_LOCAL = False
_DATASETNAME = "tmvar_v3"
_DISPLAYNAME = "tmVar v3"
_DESCRIPTION = """\
This dataset contains 500 PubMed articles manually annotated with mutation \
mentions of various kinds and dbsnp normalizations for each of them. In \
addition, it contains variant normalization options such as allele-specific \
identifiers from the ClinGen Allele Registry It can be used for NER tasks and \
NED tasks, This dataset does NOT have splits.
"""
_HOMEPAGE = "https://www.ncbi.nlm.nih.gov/research/bionlp/Tools/tmvar/"
_LICENSE = 'License information unavailable'
_URLS = {_DATASETNAME: "ftp://ftp.ncbi.nlm.nih.gov/pub/lu/tmVar3/tmVar3Corpus.txt"}
_SUPPORTED_TASKS = [Tasks.NAMED_ENTITY_RECOGNITION, Tasks.NAMED_ENTITY_DISAMBIGUATION]
_SOURCE_VERSION = "3.0.0"
_BIGBIO_VERSION = "1.0.0"
logger = datasets.utils.logging.get_logger(__name__)
class TmvarV3Dataset(datasets.GeneratorBasedBuilder):
"""
This dataset contains 500 PubMed articles manually annotated with mutation mentions of various kinds and various normalizations for each of them.
"""
DEFAULT_CONFIG_NAME = "tmvar_v3_source"
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
BUILDER_CONFIGS = []
BUILDER_CONFIGS.append(
BigBioConfig(
name=f"{_DATASETNAME}_source",
version=SOURCE_VERSION,
description=f"{_DATASETNAME} source schema",
schema="source",
subset_id=f"{_DATASETNAME}",
)
)
BUILDER_CONFIGS.append(
BigBioConfig(
name=f"{_DATASETNAME}_bigbio_kb",
version=BIGBIO_VERSION,
description=f"{_DATASETNAME} BigBio schema",
schema="bigbio_kb",
subset_id=f"{_DATASETNAME}",
)
)
def _info(self) -> datasets.DatasetInfo:
type_to_db_mapping = {
"CorrespondingGene": "NCBI Gene",
"tmVar": "tmVar",
"dbSNP": "dbSNP",
"VariantGroup": "VariantGroup",
"NCBI Taxonomy": "NCBI Taxonomy",
}
if self.config.schema == "source":
features = datasets.Features(
{
"pmid": datasets.Value("string"),
"passages": [
{
"type": datasets.Value("string"),
"text": datasets.Sequence(datasets.Value("string")),
"offsets": datasets.Sequence([datasets.Value("int32")]),
}
],
"entities": [
{
"text": datasets.Sequence(datasets.Value("string")),
"offsets": datasets.Sequence([datasets.Value("int32")]),
"semantic_type_id": datasets.Sequence(
datasets.Value("string")
),
"normalized": {
key: datasets.Sequence(datasets.Value("string"))
for key in type_to_db_mapping.keys()
},
}
],
}
)
elif self.config.schema == "bigbio_kb":
features = kb_features
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=str(_LICENSE),
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
url = _URLS[_DATASETNAME]
test_filepath = dl_manager.download(url)
return [
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"filepath": test_filepath,
},
)
]
def get_normalizations(self, id, type, doc_id):
"""
Given a type and a number of normalizations ids, this function returns a dictionary of the normalized ids
"""
base_dict = {
key: []
for key in [
"tmVar",
"CorrespondingGene",
"dbSNP",
"VariantGroup",
"NCBI Taxonomy",
]
}
ids = id.split(";")
if type in ["CellLine", "Species"]:
id_vals = ids[0].split(",")
base_dict["NCBI Taxonomy"] = id_vals
elif type == "Gene":
id_vals = ids[0].split(",")
base_dict["CorrespondingGene"] = id_vals
else:
for id in ids:
if "|" in id:
base_dict["tmVar"].append(id)
elif id[:2] == "rs":
base_dict["dbSNP"].append(id[2:])
elif ":" in id:
db_name, db_id = id.split(":")
if db_name == "RS#":
db_name = "dbSNP"
# Hacky fix below for doc ID: 18272172
elif db_name == "Va1iantGroup":
db_name = "VariantGroup"
elif db_name == "Gene":
db_name = "CorrespondingGene"
elif db_name == "Disease":
continue
db_ids = db_id.split(",")
base_dict[db_name].extend(db_ids)
else:
logger.info(
f"Malformed normalization in Document {doc_id}. Type: {type}, Number: {id}"
)
continue
return base_dict
def pubtator_to_source(self, filepath):
"""
Converts pubtator to source schema
"""
with open(filepath, "r", encoding="utf8") as fstream:
for doc in pubtator.iterparse(fstream):
document = {}
document["pmid"] = doc.pmid
title = doc.title
abstract = doc.abstract
document["passages"] = [
{"type": "title", "text": [title], "offsets": [[0, len(title)]]},
{
"type": "abstract",
"text": [abstract],
"offsets": [[len(title) + 1, len(title) + len(abstract) + 1]],
},
]
document["entities"] = [
{
"offsets": [[mention.start, mention.end]],
"text": [mention.text],
"semantic_type_id": [mention.type],
"normalized": self.get_normalizations(
mention.id,
mention.type,
doc.pmid,
),
}
for mention in doc.annotations
]
yield document
def pubtator_to_bigbio_kb(self, filepath):
"""
Converts pubtator to bigbio_kb schema
"""
with open(filepath, "r", encoding="utf8") as fstream:
uid = itertools.count(0)
for doc in pubtator.iterparse(fstream):
document = {}
title = doc.title
abstract = doc.abstract
document["id"] = next(uid)
document["document_id"] = doc.pmid
document["passages"] = [
{
"id": next(uid),
"type": "title",
"text": [title],
"offsets": [[0, len(title)]],
},
{
"id": next(uid),
"type": "abstract",
"text": [abstract],
"offsets": [[len(title) + 1, len(title) + len(abstract) + 1]],
},
]
document["entities"] = [
{
"id": next(uid),
"offsets": [[mention.start, mention.end]],
"text": [mention.text],
"type": [mention.type],
"normalized": self.get_normalizations(
mention.id, mention.type, doc.pmid
),
}
for mention in doc.annotations
]
db_id_mapping = {
"dbSNP": "dbSNP",
"CorrespondingGene": "NCBI Gene",
"tmVar": "dbSNP",
}
for entity in document["entities"]:
normalized_bigbio_kb = []
for key, id_list in entity["normalized"].items():
if key in db_id_mapping.keys():
normalized_bigbio_kb.extend(
[
{"db_name": db_id_mapping[key], "db_id": id}
for id in id_list
]
)
entity["normalized"] = normalized_bigbio_kb
document["relations"] = []
document["events"] = []
document["coreferences"] = []
yield document
def _generate_examples(self, filepath):
"""Yields examples as (key, example) tuples."""
if self.config.schema == "source":
for source_example in self.pubtator_to_source(filepath):
yield source_example["pmid"], source_example
elif self.config.schema == "bigbio_kb":
for bigbio_example in self.pubtator_to_bigbio_kb(filepath):
yield bigbio_example["document_id"], bigbio_example
|