ArneBinder commited on
Commit
6641849
1 Parent(s): 2979298

from https://github.com/ArneBinder/pie-datasets/pull/141

Browse files
Files changed (3) hide show
  1. README.md +56 -0
  2. comagc.py +321 -0
  3. requirements.txt +1 -0
README.md ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # PIE Dataset Card for "CoMAGC"
2
+
3
+ This is a [PyTorch-IE](https://github.com/ChristophAlt/pytorch-ie) wrapper for the
4
+ [CoMAGC Huggingface dataset loading script](https://huggingface.co/datasets/DFKI-SLT/CoMAGC).
5
+
6
+ ## Data Schema
7
+
8
+ The document type for this dataset is `ComagcDocument` which defines the following data fields:
9
+
10
+ - `pmid` (str): unique sentence identifier
11
+ - `sentence` (str)
12
+ - `cancer_type` (str)
13
+ - `cge` (str): change in gene expression
14
+ - `ccs` (str): change in cell state
15
+ - `pt` (str, optional): proposition type
16
+ - `ige` (str, optional): initial gene expression level
17
+
18
+ and the following annotation layers:
19
+
20
+ - `gene` (annotation type: `NamedSpan`, target: `sentence`)
21
+ - `cancer` (annotation type: `NamedSpan`, target: `sentence`)
22
+ - `expression_change_keyword1` (annotation type: `SpanWithNameAndType`, target: `sentence`)
23
+ - `expression_change_keyword2` (annotation type: `SpanWithNameAndType`, target: `sentence`)
24
+
25
+ `NamedSpan` is a custom annotation type that extends typical `Span` with the following data fields:
26
+
27
+ - `name` (str): entity string between span start and end
28
+
29
+ `SpanWithNameAndType` is a custom annotation type that extends typical `Span` with the following data fields:
30
+
31
+ - `name` (str): entity string between span start and end
32
+ - `type` (str): entity type classifying the expression
33
+
34
+ See [here](https://github.com/ArneBinder/pie-modules/blob/main/src/pie_modules/annotations.py) and
35
+ [here](https://github.com/ChristophAlt/pytorch-ie/blob/main/src/pytorch_ie/annotations.py) for the annotation
36
+ type definitions.
37
+
38
+ ## Document Converters
39
+
40
+ The dataset provides predefined document converters for the following target document types:
41
+
42
+ - `pie_modules.documents.TextDocumentWithLabeledSpansAndBinaryRelations`:
43
+
44
+ - **labeled_spans**: There are always two labeled spans in each sentence.
45
+ The first one refers to the gene, while the second one refers to the cancer.
46
+ Therefore, the `label` is either `"GENE"` or `"CANCER"`.
47
+ - **binary_relations**: There is always one binary relation in each sentence.
48
+ This relation is always established between the gene as `head` and the cancer as `tail`.
49
+ The specific `label` is the related **gene-class**. It is obtained from inference rules (cf [here](https://bmcbioinformatics.biomedcentral.com/articles/10.1186/1471-2105-14-323/tables/3)),
50
+ that are based on the values of the columns CGE, CCS, IGE and PT. In case no gene-class can be inferred,
51
+ no binary relation is added to the document. In total to 303 of the 821 examples,
52
+ there is no rule is applicable (cf [here](https://bmcbioinformatics.biomedcentral.com/articles/10.1186/1471-2105-14-323/tables/7)).
53
+
54
+ See [here](https://github.com/ArneBinder/pie-modules/blob/main/src/pie_modules/documents.py) and
55
+ [here](https://github.com/ChristophAlt/pytorch-ie/blob/main/src/pytorch_ie/documents.py) for the document type
56
+ definitions.
comagc.py ADDED
@@ -0,0 +1,321 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ from dataclasses import dataclass
3
+ from typing import Any, Dict, Optional
4
+
5
+ import datasets
6
+ from pytorch_ie import AnnotationLayer, Document, annotation_field
7
+ from pytorch_ie.annotations import BinaryRelation, LabeledSpan, Span
8
+ from pytorch_ie.documents import TextDocumentWithLabeledSpansAndBinaryRelations
9
+
10
+ from pie_datasets import ArrowBasedBuilder
11
+
12
+ logger = logging.getLogger(__name__)
13
+
14
+
15
+ @dataclass(frozen=True)
16
+ class NamedSpan(Span):
17
+ name: str
18
+
19
+ def resolve(self) -> Any:
20
+ return self.name, super().resolve()
21
+
22
+
23
+ @dataclass(frozen=True)
24
+ class SpanWithNameAndType(Span):
25
+ name: str
26
+ type: str
27
+
28
+ def resolve(self) -> Any:
29
+ return self.name, self.type, super().resolve()
30
+
31
+
32
+ @dataclass
33
+ class ComagcDocument(Document):
34
+ pmid: str
35
+ sentence: str
36
+ cge: str
37
+ ccs: str
38
+ cancer_type: str
39
+ gene: AnnotationLayer[NamedSpan] = annotation_field(target="sentence")
40
+ cancer: AnnotationLayer[NamedSpan] = annotation_field(target="sentence")
41
+ pt: Optional[str] = None
42
+ ige: Optional[str] = None
43
+ expression_change_keyword1: AnnotationLayer[SpanWithNameAndType] = annotation_field(
44
+ target="sentence"
45
+ )
46
+ expression_change_keyword2: AnnotationLayer[SpanWithNameAndType] = annotation_field(
47
+ target="sentence"
48
+ )
49
+
50
+
51
+ def example_to_document(example) -> ComagcDocument:
52
+ doc = ComagcDocument(
53
+ pmid=example["pmid"],
54
+ sentence=example["sentence"],
55
+ cancer_type=example["cancer_type"],
56
+ cge=example["CGE"],
57
+ ccs=example["CCS"],
58
+ pt=example["PT"],
59
+ ige=example["IGE"],
60
+ )
61
+
62
+ # Gene and cancer entities
63
+ # name is (almost) always the text of the gene/cancer (between the start and end position)
64
+ gene = NamedSpan(
65
+ start=example["gene"]["pos"][0],
66
+ end=example["gene"]["pos"][1] + 1,
67
+ name=example["gene"]["name"],
68
+ )
69
+ doc.gene.extend([gene])
70
+
71
+ cancer = NamedSpan(
72
+ start=example["cancer"]["pos"][0],
73
+ end=example["cancer"]["pos"][1] + 1,
74
+ name=example["cancer"]["name"],
75
+ )
76
+ doc.cancer.extend([cancer])
77
+
78
+ # Expression change keywords
79
+ # expression_change_keyword_1 might have no values
80
+ if example["expression_change_keyword_1"]["pos"] is not None:
81
+ expression_change_keyword1 = SpanWithNameAndType(
82
+ start=example["expression_change_keyword_1"]["pos"][0],
83
+ end=example["expression_change_keyword_1"]["pos"][1] + 1,
84
+ name=example["expression_change_keyword_1"]["name"],
85
+ type=example["expression_change_keyword_1"]["type"],
86
+ )
87
+ doc.expression_change_keyword1.extend([expression_change_keyword1])
88
+
89
+ expression_change_keyword2 = SpanWithNameAndType(
90
+ start=example["expression_change_keyword_2"]["pos"][0],
91
+ end=example["expression_change_keyword_2"]["pos"][1] + 1,
92
+ name=example["expression_change_keyword_2"]["name"],
93
+ type=example["expression_change_keyword_2"]["type"],
94
+ )
95
+ doc.expression_change_keyword2.extend([expression_change_keyword2])
96
+
97
+ return doc
98
+
99
+
100
+ def document_to_example(doc: ComagcDocument) -> Dict[str, Any]:
101
+ gene = {
102
+ "name": doc.gene[0].name,
103
+ "pos": [doc.gene[0].start, doc.gene[0].end - 1],
104
+ }
105
+ cancer = {
106
+ "name": doc.cancer[0].name,
107
+ "pos": [doc.cancer[0].start, doc.cancer[0].end - 1],
108
+ }
109
+
110
+ if not doc.expression_change_keyword1.resolve():
111
+ expression_change_keyword_1 = {
112
+ "name": "\nNone\n",
113
+ "pos": None,
114
+ "type": None,
115
+ }
116
+ else:
117
+ expression_change_keyword_1 = {
118
+ "name": doc.expression_change_keyword1[0].name,
119
+ "pos": [
120
+ doc.expression_change_keyword1[0].start,
121
+ doc.expression_change_keyword1[0].end - 1,
122
+ ],
123
+ "type": doc.expression_change_keyword1[0].type,
124
+ }
125
+
126
+ expression_change_keyword_2 = {
127
+ "name": doc.expression_change_keyword2[0].name,
128
+ "pos": [
129
+ doc.expression_change_keyword2[0].start,
130
+ doc.expression_change_keyword2[0].end - 1,
131
+ ],
132
+ "type": doc.expression_change_keyword2[0].type,
133
+ }
134
+
135
+ return {
136
+ "pmid": doc.pmid,
137
+ "sentence": doc.sentence,
138
+ "cancer_type": doc.cancer_type,
139
+ "gene": gene,
140
+ "cancer": cancer,
141
+ "CGE": doc.cge,
142
+ "CCS": doc.ccs,
143
+ "PT": doc.pt,
144
+ "IGE": doc.ige,
145
+ "expression_change_keyword_1": expression_change_keyword_1,
146
+ "expression_change_keyword_2": expression_change_keyword_2,
147
+ }
148
+
149
+
150
+ def convert_to_text_document_with_labeled_spans_and_binary_relations(
151
+ document: ComagcDocument,
152
+ ) -> TextDocumentWithLabeledSpansAndBinaryRelations:
153
+ metadata = {
154
+ "cancer_type": document.cancer_type,
155
+ "CGE": document.cge,
156
+ "CCS": document.ccs,
157
+ "PT": document.pt,
158
+ "IGE": document.ige,
159
+ "expression_change_keyword_1": document_to_example(document)[
160
+ "expression_change_keyword_1"
161
+ ],
162
+ "expression_change_keyword_2": document_to_example(document)[
163
+ "expression_change_keyword_2"
164
+ ],
165
+ }
166
+
167
+ text_document = TextDocumentWithLabeledSpansAndBinaryRelations(
168
+ id=document.pmid, text=document.sentence, metadata=metadata
169
+ )
170
+
171
+ gene = LabeledSpan(
172
+ start=document.gene[0].start,
173
+ end=document.gene[0].end,
174
+ label="GENE",
175
+ )
176
+ text_document.labeled_spans.append(gene)
177
+
178
+ cancer = LabeledSpan(
179
+ start=document.cancer[0].start,
180
+ end=document.cancer[0].end,
181
+ label="CANCER",
182
+ )
183
+ text_document.labeled_spans.append(cancer)
184
+
185
+ label = get_relation_label(
186
+ cge=document.cge, ccs=document.ccs, ige=document.ige, pt=document.pt
187
+ )
188
+
189
+ if label is not None:
190
+ relation = BinaryRelation(
191
+ head=gene,
192
+ tail=cancer,
193
+ label=label,
194
+ )
195
+ text_document.binary_relations.append(relation)
196
+
197
+ return text_document
198
+
199
+
200
+ class Comagc(ArrowBasedBuilder):
201
+ DOCUMENT_TYPE = ComagcDocument
202
+ BASE_DATASET_PATH = "DFKI-SLT/CoMAGC"
203
+ BASE_DATASET_REVISION = "8e2950b8a3967c2f45de86f60dd5c8ccb9ad3815"
204
+
205
+ BUILDER_CONFIGS = [
206
+ datasets.BuilderConfig(
207
+ version=datasets.Version("1.0.0"),
208
+ description="CoMAGC dataset",
209
+ )
210
+ ]
211
+
212
+ DOCUMENT_CONVERTERS = {
213
+ TextDocumentWithLabeledSpansAndBinaryRelations: convert_to_text_document_with_labeled_spans_and_binary_relations
214
+ }
215
+
216
+ def _generate_document(self, example, **kwargs):
217
+ return example_to_document(example)
218
+
219
+ def _generate_example(self, document: ComagcDocument, **kwargs) -> Dict[str, Any]:
220
+ return document_to_example(document)
221
+
222
+
223
+ def get_relation_label(cge: str, ccs: str, pt: str, ige: str) -> Optional[str]:
224
+ """Simple rule-based function to determine the relation between the gene and the cancer.
225
+
226
+ As this dataset contains a multi-faceted annotation scheme
227
+ for gene-cancer relations, it does not only label the relation
228
+ between gene and cancer, but provides further information.
229
+ However, the relation of interest stays the gene-class,
230
+ which can be derived from inference rules
231
+ (https://bmcbioinformatics.biomedcentral.com/articles/10.1186/1471-2105-14-323/tables/3), based on the
232
+ information given in columns CGE, CCS, IGE, PT.
233
+ """
234
+
235
+ rules = [
236
+ {
237
+ "CGE": "increased",
238
+ "CCS": "normalTOcancer",
239
+ "IGE": "*",
240
+ "PT": "causality",
241
+ "Gene class": "oncogene",
242
+ },
243
+ {
244
+ "CGE": "decreased",
245
+ "CCS": "cancerTOnormal",
246
+ "IGE": "unidentifiable",
247
+ "PT": "causality",
248
+ "Gene class": "oncogene",
249
+ },
250
+ {
251
+ "CGE": "decreased",
252
+ "CCS": "cancerTOnormal",
253
+ "IGE": "up-regulated",
254
+ "PT": "*",
255
+ "Gene class": "oncogene",
256
+ },
257
+ {
258
+ "CGE": "decreased",
259
+ "CCS": "normalTOcancer",
260
+ "IGE": "*",
261
+ "PT": "causality",
262
+ "Gene class": "tumor suppressor gene",
263
+ },
264
+ {
265
+ "CGE": "increased",
266
+ "CCS": "cancerTOnormal",
267
+ "IGE": "unidentifiable",
268
+ "PT": "causality",
269
+ "Gene class": "tumor suppressor gene",
270
+ },
271
+ {
272
+ "CGE": "increased",
273
+ "CCS": "cancerTOnormal",
274
+ "IGE": "down-regulated",
275
+ "PT": "*",
276
+ "Gene class": "tumor suppressor gene",
277
+ },
278
+ {
279
+ "CGE": "*",
280
+ "CCS": "normalTOcancer",
281
+ "IGE": "*",
282
+ "PT": "observation",
283
+ "Gene class": "biomarker",
284
+ },
285
+ {
286
+ "CGE": "*",
287
+ "CCS": "cancerTOnormal",
288
+ "IGE": "unidentifiable",
289
+ "PT": "observation",
290
+ "Gene class": "biomarker",
291
+ },
292
+ {
293
+ "CGE": "decreased",
294
+ "CCS": "cancerTOcancer",
295
+ "IGE": "up-regulated",
296
+ "PT": "observation",
297
+ "Gene class": "biomarker",
298
+ },
299
+ {
300
+ "CGE": "increased",
301
+ "CCS": "cancerTOcancer",
302
+ "IGE": "down-regulated",
303
+ "PT": "observation",
304
+ "Gene class": "biomarker",
305
+ },
306
+ ]
307
+
308
+ for rule in rules:
309
+ if (
310
+ (rule["CGE"] == "*" or cge == rule["CGE"])
311
+ and (rule["CCS"] == "*" or ccs == rule["CCS"])
312
+ and (rule["IGE"] == "*" or ige == rule["IGE"])
313
+ and (rule["PT"] == "*" or pt == rule["PT"])
314
+ ):
315
+ return rule["Gene class"]
316
+
317
+ # Commented out to avoid spamming the logs
318
+ # logger.warning("No rule matched. cge: " + cge + " - ccs: " + ccs + " - ige: " + ige + " - pt: " + pt)
319
+ # NOTE: In case no inference rule is applicable, no relation is returned and
320
+ # eventually no relation is added to the document.
321
+ return None
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ pie-datasets>=0.6.0,<0.11.0