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
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2022 The HuggingFace Datasets Authors and Simon Ott, github: nomisto
|
| 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 |
+
|
| 16 |
+
"""
|
| 17 |
+
The LLL05 challenge task is to learn rules to extract protein/gene interactions from biology abstracts from the Medline
|
| 18 |
+
bibliography database. The goal of the challenge is to test the ability of the participating IE systems to identify the
|
| 19 |
+
interactions and the gene/proteins that interact. The participants will test their IE patterns on a test set with the
|
| 20 |
+
aim of extracting the correct agent and target.The challenge focuses on information extraction of gene interactions in
|
| 21 |
+
Bacillus subtilis. Extracting gene interaction is the most popular event IE task in biology. Bacillus subtilis (Bs) is
|
| 22 |
+
a model bacterium and many papers have been published on direct gene interactions involved in sporulation. The gene
|
| 23 |
+
interactions are generally mentioned in the abstract and the full text of the paper is not needed. Extracting gene
|
| 24 |
+
interaction means, extracting the agent (proteins) and the target (genes) of all couples of genic interactions from
|
| 25 |
+
sentences.
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
# NOTE:
|
| 29 |
+
# word stop offsets are increased by one to be consistent with python slicing.
|
| 30 |
+
# test set does not include entity relation information
|
| 31 |
+
|
| 32 |
+
import itertools as it
|
| 33 |
+
from typing import List
|
| 34 |
+
|
| 35 |
+
import datasets
|
| 36 |
+
|
| 37 |
+
from .bigbiohub import kb_features
|
| 38 |
+
from .bigbiohub import BigBioConfig
|
| 39 |
+
from .bigbiohub import Tasks
|
| 40 |
+
|
| 41 |
+
_LANGUAGES = ['English']
|
| 42 |
+
_PUBMED = True
|
| 43 |
+
_LOCAL = False
|
| 44 |
+
_CITATION = """\
|
| 45 |
+
@article{article,
|
| 46 |
+
author = {Nédellec, C.},
|
| 47 |
+
year = {2005},
|
| 48 |
+
month = {01},
|
| 49 |
+
pages = {},
|
| 50 |
+
title = {Learning Language in Logic - Genic Interaction Extraction Challenge},
|
| 51 |
+
journal = {Proceedings of the Learning Language in Logic 2005 Workshop at the \
|
| 52 |
+
International Conference on Machine Learning}
|
| 53 |
+
}
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
_DATASETNAME = "lll"
|
| 57 |
+
_DISPLAYNAME = "LLL05"
|
| 58 |
+
|
| 59 |
+
_DESCRIPTION = """\
|
| 60 |
+
The LLL05 challenge task is to learn rules to extract protein/gene interactions from biology abstracts from the Medline
|
| 61 |
+
bibliography database. The goal of the challenge is to test the ability of the participating IE systems to identify the
|
| 62 |
+
interactions and the gene/proteins that interact. The participants will test their IE patterns on a test set with the
|
| 63 |
+
aim of extracting the correct agent and target.The challenge focuses on information extraction of gene interactions in
|
| 64 |
+
Bacillus subtilis. Extracting gene interaction is the most popular event IE task in biology. Bacillus subtilis (Bs) is
|
| 65 |
+
a model bacterium and many papers have been published on direct gene interactions involved in sporulation. The gene
|
| 66 |
+
interactions are generally mentioned in the abstract and the full text of the paper is not needed. Extracting gene
|
| 67 |
+
interaction means, extracting the agent (proteins) and the target (genes) of all couples of genic interactions from
|
| 68 |
+
sentences.
|
| 69 |
+
"""
|
| 70 |
+
|
| 71 |
+
_HOMEPAGE = "http://genome.jouy.inra.fr/texte/LLLchallenge"
|
| 72 |
+
|
| 73 |
+
_LICENSE = 'License information unavailable'
|
| 74 |
+
|
| 75 |
+
_URLS = {
|
| 76 |
+
_DATASETNAME: [
|
| 77 |
+
"http://genome.jouy.inra.fr/texte/LLLchallenge/data/LLLChalenge05/data/train/task2/genic_interaction_linguistic_data.txt", # noqa
|
| 78 |
+
"http://genome.jouy.inra.fr/texte/LLLchallenge/data/LLLChalenge05/data/train/task2/genic_interaction_linguistic_data_coref.txt", # noqa
|
| 79 |
+
"http://genome.jouy.inra.fr/texte/LLLchallenge/data/LLLChalenge05/data/test/task2/enriched_test_data.txt", # noqa
|
| 80 |
+
]
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
_SUPPORTED_TASKS = [Tasks.RELATION_EXTRACTION]
|
| 84 |
+
|
| 85 |
+
_SOURCE_VERSION = "1.0.0"
|
| 86 |
+
|
| 87 |
+
_BIGBIO_VERSION = "1.0.0"
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
class LLLDataset(datasets.GeneratorBasedBuilder):
|
| 91 |
+
"""LLL dataset for gene interaction extraction (RE)"""
|
| 92 |
+
|
| 93 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
| 94 |
+
BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
|
| 95 |
+
|
| 96 |
+
BUILDER_CONFIGS = [
|
| 97 |
+
BigBioConfig(
|
| 98 |
+
name="lll_source",
|
| 99 |
+
version=SOURCE_VERSION,
|
| 100 |
+
description="LLL source schema",
|
| 101 |
+
schema="source",
|
| 102 |
+
subset_id="lll",
|
| 103 |
+
),
|
| 104 |
+
BigBioConfig(
|
| 105 |
+
name="lll_bigbio_kb",
|
| 106 |
+
version=BIGBIO_VERSION,
|
| 107 |
+
description="LLL BigBio schema",
|
| 108 |
+
schema="bigbio_kb",
|
| 109 |
+
subset_id="lll",
|
| 110 |
+
),
|
| 111 |
+
]
|
| 112 |
+
|
| 113 |
+
DEFAULT_CONFIG_NAME = "lll_source"
|
| 114 |
+
|
| 115 |
+
def _info(self) -> datasets.DatasetInfo:
|
| 116 |
+
|
| 117 |
+
if self.config.schema == "source":
|
| 118 |
+
features = datasets.Features(
|
| 119 |
+
{
|
| 120 |
+
"id": datasets.Value("string"),
|
| 121 |
+
"sentence": datasets.Value("string"),
|
| 122 |
+
"words": [
|
| 123 |
+
{
|
| 124 |
+
"id": datasets.Value("string"),
|
| 125 |
+
"text": datasets.Value("string"),
|
| 126 |
+
"offsets": datasets.Sequence(datasets.Value("int32")),
|
| 127 |
+
}
|
| 128 |
+
],
|
| 129 |
+
"genic_interactions": [
|
| 130 |
+
{
|
| 131 |
+
"ref_id1": datasets.Value("string"),
|
| 132 |
+
"ref_id2": datasets.Value("string"),
|
| 133 |
+
}
|
| 134 |
+
],
|
| 135 |
+
"agents": [
|
| 136 |
+
{
|
| 137 |
+
"ref_id": datasets.Value("string"),
|
| 138 |
+
}
|
| 139 |
+
],
|
| 140 |
+
"targets": [
|
| 141 |
+
{
|
| 142 |
+
"ref_id": datasets.Value("string"),
|
| 143 |
+
}
|
| 144 |
+
],
|
| 145 |
+
"lemmas": [
|
| 146 |
+
{
|
| 147 |
+
"ref_id": datasets.Value("string"),
|
| 148 |
+
"lemma": datasets.Value("string"),
|
| 149 |
+
}
|
| 150 |
+
],
|
| 151 |
+
"syntactic_relations": [
|
| 152 |
+
{
|
| 153 |
+
"type": datasets.Value("string"),
|
| 154 |
+
"ref_id1": datasets.Value("string"),
|
| 155 |
+
"ref_id2": datasets.Value("string"),
|
| 156 |
+
}
|
| 157 |
+
],
|
| 158 |
+
}
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
elif self.config.schema == "bigbio_kb":
|
| 162 |
+
features = kb_features
|
| 163 |
+
|
| 164 |
+
return datasets.DatasetInfo(
|
| 165 |
+
description=_DESCRIPTION,
|
| 166 |
+
features=features,
|
| 167 |
+
homepage=_HOMEPAGE,
|
| 168 |
+
license=str(_LICENSE),
|
| 169 |
+
citation=_CITATION,
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
def _split_generators(self, dl_manager) -> List[datasets.SplitGenerator]:
|
| 173 |
+
|
| 174 |
+
urls = _URLS[_DATASETNAME]
|
| 175 |
+
train_path, train_coref_path, test_path = dl_manager.download_and_extract(urls)
|
| 176 |
+
|
| 177 |
+
return [
|
| 178 |
+
datasets.SplitGenerator(
|
| 179 |
+
name=datasets.Split.TRAIN,
|
| 180 |
+
gen_kwargs={
|
| 181 |
+
"data_paths": [train_path, train_coref_path],
|
| 182 |
+
"split": "train",
|
| 183 |
+
},
|
| 184 |
+
),
|
| 185 |
+
datasets.SplitGenerator(
|
| 186 |
+
name=datasets.Split.TEST,
|
| 187 |
+
gen_kwargs={"data_paths": [test_path], "split": "test"},
|
| 188 |
+
),
|
| 189 |
+
]
|
| 190 |
+
|
| 191 |
+
def _generate_examples(self, data_paths, split):
|
| 192 |
+
|
| 193 |
+
if self.config.schema == "source":
|
| 194 |
+
for path in data_paths:
|
| 195 |
+
with open(path, encoding="utf8") as documents:
|
| 196 |
+
for document in self._generate_parsed_documents(documents, split):
|
| 197 |
+
yield document["id"], document
|
| 198 |
+
|
| 199 |
+
elif self.config.schema == "bigbio_kb":
|
| 200 |
+
uid = it.count(0)
|
| 201 |
+
for path in data_paths:
|
| 202 |
+
with open(path, encoding="utf8") as documents:
|
| 203 |
+
for document in self._generate_parsed_documents(documents, split):
|
| 204 |
+
document_ = {}
|
| 205 |
+
document_["id"] = next(uid)
|
| 206 |
+
document_["document_id"] = document["id"]
|
| 207 |
+
|
| 208 |
+
document_["passages"] = [
|
| 209 |
+
{
|
| 210 |
+
"id": next(uid),
|
| 211 |
+
"type": BigBioValues.NULL,
|
| 212 |
+
"text": [document["sentence"]],
|
| 213 |
+
"offsets": [[0, len(document["sentence"])]],
|
| 214 |
+
}
|
| 215 |
+
]
|
| 216 |
+
|
| 217 |
+
id_to_word = {i["id"]: i for i in document["words"]}
|
| 218 |
+
document_["entities"] = []
|
| 219 |
+
for agent in document["agents"]:
|
| 220 |
+
word = id_to_word[agent["ref_id"]]
|
| 221 |
+
document_["entities"].append(
|
| 222 |
+
{
|
| 223 |
+
"id": f"{document_['id']}-agent-{word['id']}",
|
| 224 |
+
"type": "agent",
|
| 225 |
+
"text": [word["text"]],
|
| 226 |
+
"offsets": [
|
| 227 |
+
[word["offsets"][0], word["offsets"][1]]
|
| 228 |
+
],
|
| 229 |
+
"normalized": [],
|
| 230 |
+
}
|
| 231 |
+
)
|
| 232 |
+
for agent in document["targets"]:
|
| 233 |
+
word = id_to_word[agent["ref_id"]]
|
| 234 |
+
document_["entities"].append(
|
| 235 |
+
{
|
| 236 |
+
"id": f"{document_['id']}-target-{word['id']}",
|
| 237 |
+
"type": "target",
|
| 238 |
+
"text": [word["text"]],
|
| 239 |
+
"offsets": [
|
| 240 |
+
[word["offsets"][0], word["offsets"][1]]
|
| 241 |
+
],
|
| 242 |
+
"normalized": [],
|
| 243 |
+
}
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
document_["relations"] = [
|
| 247 |
+
{
|
| 248 |
+
"id": next(uid),
|
| 249 |
+
"type": "genic_interaction",
|
| 250 |
+
"arg1_id": f"{document_['id']}-agent-{relation['ref_id1']}",
|
| 251 |
+
"arg2_id": f"{document_['id']}-target-{relation['ref_id2']}",
|
| 252 |
+
"normalized": [],
|
| 253 |
+
}
|
| 254 |
+
for relation in document["genic_interactions"]
|
| 255 |
+
]
|
| 256 |
+
|
| 257 |
+
document_["events"] = []
|
| 258 |
+
document_["coreferences"] = []
|
| 259 |
+
yield document_["document_id"], document_
|
| 260 |
+
|
| 261 |
+
def _generate_parsed_documents(self, fstream, split):
|
| 262 |
+
for raw_document in self._generate_raw_documents(fstream):
|
| 263 |
+
yield self._parse_document(raw_document, split)
|
| 264 |
+
|
| 265 |
+
def _generate_raw_documents(self, fstream):
|
| 266 |
+
raw_document = []
|
| 267 |
+
for line in fstream:
|
| 268 |
+
if "%" in line:
|
| 269 |
+
continue
|
| 270 |
+
elif line.strip():
|
| 271 |
+
raw_document.append(line.strip())
|
| 272 |
+
elif raw_document:
|
| 273 |
+
if raw_document:
|
| 274 |
+
yield raw_document
|
| 275 |
+
raw_document = []
|
| 276 |
+
# needed for last document
|
| 277 |
+
if raw_document:
|
| 278 |
+
yield raw_document
|
| 279 |
+
|
| 280 |
+
def _parse_document(self, raw_document, split):
|
| 281 |
+
document = {}
|
| 282 |
+
for line in raw_document:
|
| 283 |
+
key, value = line.split("\t", 1)
|
| 284 |
+
if key in ["ID", "sentence"]:
|
| 285 |
+
document[key.lower()] = value
|
| 286 |
+
elif key in [
|
| 287 |
+
"words",
|
| 288 |
+
"genic_interactions",
|
| 289 |
+
"agents",
|
| 290 |
+
"targets",
|
| 291 |
+
"lemmas",
|
| 292 |
+
"syntactic_relations",
|
| 293 |
+
]:
|
| 294 |
+
document[key.lower()] = self._parse_elements(value, key)
|
| 295 |
+
else:
|
| 296 |
+
raise NotImplementedError()
|
| 297 |
+
|
| 298 |
+
# Needed as testset does not contain agents, targets and genic_interactions (dataset was part of a challenge)
|
| 299 |
+
if split == "test":
|
| 300 |
+
document.setdefault("genic_interactions", [])
|
| 301 |
+
document.setdefault("agents", [])
|
| 302 |
+
document.setdefault("targets", [])
|
| 303 |
+
|
| 304 |
+
return document
|
| 305 |
+
|
| 306 |
+
def _parse_elements(self, values, type):
|
| 307 |
+
return [self._parse_element(atom, type) for atom in values.split("\t")]
|
| 308 |
+
|
| 309 |
+
def _parse_element(self, atom, type):
|
| 310 |
+
# Sorry for that abomination, parses the arguments from atoms like rel(arg1, ..., argn)
|
| 311 |
+
args = atom.split("(", 1)[1][:-1].split(",")
|
| 312 |
+
if type == "words":
|
| 313 |
+
# fix offsets for python slicing
|
| 314 |
+
return {
|
| 315 |
+
"id": args[0],
|
| 316 |
+
"text": args[1].strip("'"),
|
| 317 |
+
"offsets": [int(args[2]), int(args[3]) + 1],
|
| 318 |
+
}
|
| 319 |
+
elif type == "genic_interactions":
|
| 320 |
+
return {"ref_id1": args[0], "ref_id2": args[1]}
|
| 321 |
+
elif type == "agents":
|
| 322 |
+
return {"ref_id": args[0]}
|
| 323 |
+
elif type == "targets":
|
| 324 |
+
return {"ref_id": args[0]}
|
| 325 |
+
elif type == "lemmas":
|
| 326 |
+
return {"ref_id": args[0], "lemma": args[1].strip("'")}
|
| 327 |
+
elif type == "syntactic_relations":
|
| 328 |
+
return {"type": args[0].strip("'"), "ref_id1": args[1], "ref_id2": args[2]}
|