-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommon.py
343 lines (279 loc) · 10.9 KB
/
common.py
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
#!/usr/bin/env python3
# Common functionality for working with STRING DB / JensenLab tagger data
import sys
import re
from collections.abc import Iterator
from itertools import tee
from collections import namedtuple
from itertools import zip_longest
# From https://bitbucket.org/larsjuhljensen/tagger/
TYPE_MAP = {
-1: 'Chemical',
-2: 'Organism', # NCBI species taxonomy id (tagging species)
-3: 'Organism', # NCBI species taxonomy id (tagging proteins)
-11: 'Wikipedia',
-21: 'Biological_process', # GO biological process
-22: 'Cellular_component', # GO cellular component
-23: 'Molecular_function', # GO molecular function
-24: 'GO_other', # GO other (unused)
-25: 'Tissue', # BTO tissues
-26: 'Disease', # DOID diseases
-27: 'Environment', # ENVO environments
-28: 'Phenotype', # APO phenotypes
-29: 'Phenotype', # FYPO phenotypes
-30: 'Phenotype', # MPheno phenotypes
-31: 'Behaviour', # NBO behaviors
-36: 'Phenotype', # mammalian phenotypes
}
# Regex for STRING identifiers that should be filtered out. Katerina:
# COG/KOG are EggNOG identifiers and irrelevant for normalization.
FILTER_NORM_RE = re.compile(r'^[KC]OG.*')
# Regex for STRING identifiers prefixed by a taxnomy ID that should be
# filtered out. Katerina: everything that has a txid 1 (root of
# taxonomic tree) and 2759 (Eukaryota) should be filtered out.
FILTER_PREFIXED_NORM_RE = re.compile(r'^(1|2759)\..*')
def type_name(type_):
"""Map JensenLab tagger numeric types to names. No-op for non-numeric."""
if isinstance(type_, str):
try:
type_ = int(type_)
except ValueError:
return type_ # Not numeric
if type_ > 0:
return 'Protein'
else:
return TYPE_MAP.get(type_, 'UNKNOWN-TYPE')
class StringDocument:
def __init__(self, id_, other_ids, authors, forum, year, text):
self.id = id_
self.other_ids = other_ids
self.authors = authors
self.forum = forum
self.year = year
#self.text = stringdb_unescape_text(text)
self.text = text
def __str__(self):
return '\t'.join([
self.id,
self.other_ids,
self.authors,
self.forum,
self.year,
self.text
#stringdb_escape_text(self.text)
])
class StringSpan:
def __init__(self, doc_id, par_num, sent_num, start, end, text, type_,
serial, source=None, line_no=None, no_type_mapping=False):
self.doc_id = doc_id
self.par_num = par_num
self.sent_num = sent_num
self.start = start
self.end = end
self.text = text
self.type = type_
self.serials = serial.split(',')
self.source = source
self.line_no = line_no
self.serials = [
s for s in self.serials
if not FILTER_NORM_RE.match(s)
]
if not no_type_mapping:
orig_type = self.type
self.type = type_name(self.type)
if self.type == 'Protein' and orig_type != 'Protein':
# STRING norm IDs should be prefixed by the organism ID
self.serials = [f'{orig_type}.{s}' for s in self.serials]
# And normalization to normalization to orthologous groups
# dropped
self.serials = [
s for s in self.serials
if not FILTER_PREFIXED_NORM_RE.match(s)
]
def matches(self, other):
return self.span_matches(other) and self.type_matches(other)
def overlap_matches(self, other):
return self.overlaps(other) and self.type_matches(other)
def type_matches(self, other):
# type matching is case-insensitive
return (self.type == other.type or
self.type.lower() == other.type.lower())
def span_matches(self, other):
return self.start == other.start and self.end == other.end
def overlaps(self, other):
return not (self.end+1 <= other.start or other.end+1 <= self.start)
def contains(self, other):
return ((self.start <= other.start and self.end > other.end) or
(self.start < other.start and self.end >= other.end))
def __lt__(self, other):
if self.start != other.start:
return self.start < other.start
elif self.end != other.end:
return self.end > other.end
else:
return self.type < other.type # arbitrary but fixed
def __str__(self):
fields = [
self.doc_id, self.par_num, self.sent_num,
str(self.start), str(self.end),
self.text, self.type, ','.join(self.serials)
]
if self.source is not None:
fields.append(self.source)
return '\t'.join(fields)
class LookaheadIterator(Iterator):
"""Lookahead iterator from http://stackoverflow.com/a/1518097."""
def __init__(self, it, start=0):
self._it, self._nextit = tee(iter(it))
self.index = start - 1
self._advance()
def _advance(self):
self.lookahead = next(self._nextit, None)
self.index = self.index + 1
def __next__(self):
self._advance()
return next(self._it)
def __bool__(self):
return self.lookahead is not None
class DocReader(Iterator):
"""Reader for database_documents.tsv format."""
def __init__(self, stream):
self.stream = stream
self.iter = LookaheadIterator(stream, start=1)
def current_doc_id(self):
"""Return id of document at the current position of the stream."""
if self.iter.lookahead is None:
return None
else:
return self.iter.lookahead.split()[0]
def __next__(self):
ln = self.iter.index
line = next(self.iter)
try:
doc = parse_stringdb_input_line(line)
except:
raise ValueError(f'error parsing {self.stream.name} line {ln}: '
f'{line}')
return doc
class SpanReader:
"""Reader for all_matches.tsv format."""
def __init__(self, stream, source=None, raise_on_error=False,
no_type_mapping=False):
self.stream = stream
self.source = source
self.raise_on_error = raise_on_error
self.no_type_mapping = no_type_mapping
self.iter = LookaheadIterator(stream, start=1)
self.errors = 0
def current_doc_id(self):
"""Return id of document at the current position of the stream."""
if self.iter.lookahead is None:
return None
else:
return self.iter.lookahead.split()[0]
def document_lines(self, doc_id):
"""Return lines for document doc_id and advance past them."""
spans = []
while self.current_doc_id() == doc_id:
spans.append(next(self.iter))
return spans
def document_spans(self, doc_id):
"""Return spans for document doc_id and advance past them.
If doc_id does not match the current position of the stream, returns
an empty list without advancing in the stream.
"""
spans = []
while self.current_doc_id() == doc_id:
try:
line = self.iter.lookahead.rstrip('\n')
span = parse_stringdb_span_line(
line,
source=self.source,
no_type_mapping=self.no_type_mapping
)
span.line_no = self.iter.index
spans.append(span)
except Exception as e:
self.errors += 1
print(f'error parsing {self.stream.name} line '
f'{self.iter.index}: {e}: {line}', file=sys.stderr)
if self.raise_on_error:
raise
next(self.iter)
return spans
def stringdb_escape_text(text):
"""Escape text for database_documents.tsv format."""
return text.replace('\\', '\\\\').replace('\t', '\\t')
def stringdb_unescape_text(text):
"""Unescape text field in database_documents.tsv format."""
unescaped = []
pair_iter = zip_longest(text, text[1:])
for char, next_ in pair_iter:
if char == '\\' and next_ == '\\':
# Double backslash -> single backslash
unescaped.append('\\')
next(pair_iter)
elif char == '\\' and next_ == 't':
# Backslash + t -> tab character
unescaped.append('\t')
next(pair_iter)
else:
unescaped.append(char)
return ''.join(unescaped)
def parse_stringdb_input_line(line):
"""Parse line in database_documents.tsv format, return StringDocument."""
line = line.rstrip('\n')
fields = line.split('\t', maxsplit=5) # with max split 6 the entire last field should be the text with title
doc_id, other_ids, authors, forum, year, text = fields
return StringDocument(doc_id, other_ids, authors, forum, year, text)
def parse_stringdb_span_line(line, source=None, no_type_mapping=False):
"""Parse line in all_matches.tsv format, return StringSpan."""
line = line.rstrip('\n')
fields = line.split('\t')
doc_id, par_num, sent_num, start, end, text, type_, serial = fields[:8]
if source is True: # source as separate field
source = fields[8]
assert len(fields) == 9
else:
assert len(fields) == 8
start, end = int(start), int(end)
return StringSpan(
doc_id, par_num, sent_num, start, end, text, type_, serial,
source=source, no_type_mapping=no_type_mapping
)
def stream_documents(fn):
with open(fn) as f:
for ln, l in enumerate(f, start=1):
try:
document = parse_stringdb_input_line(l)
except Exception as e:
raise ValueError('failed to parse {} line {}'.format(fn, ln))
yield document
def open_file(fn, mode, options):
if options.char_offsets:
return open(fn, mode)
else:
# https://www.python.org/dev/peps/pep-0383/ (Python 3.1+)
return open(fn, mode, encoding='ascii', errors='surrogateescape')
def load_ids(fn, options):
ids = set()
with open_file(fn, 'r', options) as f:
for ln, l in enumerate(f, start=1):
l = l.strip()
ids.add(l)
print(f'read {len(ids)} ids from {fn}', file=sys.stderr)
return ids
def safe_str(string):
# workaround for 'utf-8' codec can't encode [...]: surrogates not allowed
return string.encode('utf-8', 'replace').decode()
def unique(sequence):
"""Return unique items in sequence, preserving order."""
# https://www.peterbe.com/plog/fastest-way-to-uniquify-a-list-in-python-3.6
return list(dict.fromkeys(sequence))
if __name__ == '__main__':
import sys
# Test I/O
for fn in sys.argv[1:]:
for doc in stream_documents(fn):
print(doc.doc_id, len(doc.text.split()), 'tokens')