-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge-operons.py
338 lines (263 loc) · 7.28 KB
/
merge-operons.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
import argparse
import io
import re
import sys
from dataclasses import dataclass
from functools import wraps
from pathlib import Path
from typing import Dict, Literal, NamedTuple
from BCBio import GFF
O_ANTIGEN_GENES = {
"wzz",
"wzx",
"wzy",
"wzt",
"wzm",
"rfbA",
"rfbB",
"rfbC",
"rfbD",
"vioA",
"ugd",
"galE",
"manB",
"manC",
"gmd",
"rmd",
"rfaL",
"waaL",
"rmlA",
"rmlB",
"rmlC",
"rmlD",
"manA",
######
"yibK",
"wzc",
"wzb",
"wza",
"galE",
"gne",
"wpaD",
"ugd",
"wpaC",
"wpaB",
"wzy",
"wpaA",
"wzx",
"qdtB",
"qdtA",
"rmlA",
"qdtf",
"cpxA",
"wec",
"rffG",
"rffH",
}
H_ANTIGEN_GENES = {
"fliC",
"flkA",
"fllA",
"flmA",
"flgA",
"flgB",
"flgC",
"flgD",
"flgE",
"flgF",
"flhA",
"flhB",
"fliA",
"fliB",
"fliD",
"fliE",
}
K_ANTIGEN_GENES = {
"kpsM",
"kpsT",
"kpsD",
"kpsE",
"cpsA",
"cpsB",
"cpsC",
"cpsD",
"wza",
"wzb",
"wzc",
}
ANTIGEN_GENES = {
"O": O_ANTIGEN_GENES,
"H": H_ANTIGEN_GENES,
"K": K_ANTIGEN_GENES,
}
@wraps(print)
def err(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
AntigenType = Literal["O", "H", "K"]
@dataclass
class InputArgs:
type: set[AntigenType]
#
annotation: Path
operons: Path
coordinates: Path
#
output: Path
@dataclass(frozen=True, slots=True)
class OperonGene:
operon: int
id_gene: str
NODE_ID_PATTERN = re.compile(r"(\d+)")
def print_feature(feature):
q = feature.qualifiers
loc = feature.location
print(
loc.start,
loc.end,
loc.strand,
q.get("gene")[0],
q.get("Name")[0],
sep="\t",
)
def contig_key(record):
"""parse contig id
ie when we have NODE_36_length_1620_cov_15 from operonfinder, but
contig_36 from annotation, parse 36 as key
"""
ids = NODE_ID_PATTERN.findall(record.id)
if ids:
return int(ids[0])
raise ValueError(f"No obvious id for {record}")
def mark_operonfinder_operons(records, operons: Dict[str, int]):
for record in records:
for feature in record.features:
id = feature.qualifiers["ID"][0]
if id not in operons:
continue
feature.qualifiers["operon"] = [operons[id]]
yield record
def parse_list_of_operons(list_file: io.FileIO):
skipheader = False
current_operon = None
for line in list_file:
line = line.strip()
if not skipheader:
skipheader = True
continue
line = line.split("\t")
if len(line) == 1:
current_operon = int(line[0])
continue
id_gene = line[0]
if id_gene == "NA":
continue
assert len(line) == 7, line
yield OperonGene(current_operon, id_gene)
def is_antigen(feature, antigen_type: AntigenType):
q = feature.qualifiers
gene_names = (q.get("gene", []) + q.get("Gene", [])) or q.get("Name", [])
if not gene_names:
return False
if len(gene_names) > 1:
raise NotImplementedError(f"More than one gene name: {gene_names} in {feature}")
gene = gene_names[0]
# replace to just flat `in` if substring is not needed,
# and the whole gene in 'gene' qualifier
for antigene_name in ANTIGEN_GENES[antigen_type]:
if antigene_name.find(gene) != -1:
return True
return False
def mark_antigenes(records, antigen_types: set[AntigenType]):
for record in records:
for feature in record.features:
for antigen_type in antigen_types:
#
antigen = is_antigen(feature, antigen_type=antigen_type)
prev = feature.qualifiers.get("antigen", [False])[0]
feature.qualifiers["antigen"] = [antigen or prev]
if antigen:
feature.qualifiers["antigen_type"] = [
antigen_type
] + feature.qualifiers.get("antigen_type", [])
if antigen:
print(f"{antigen_type}-antigen", record.id, end="\t", sep="\t")
print_feature(feature)
yield record
def parse_gff(gff_file: io.FileIO):
return GFF.parse(gff_file)
class Location(NamedTuple):
start: int
end: int
strand: str
@classmethod
def from_feature(cls, feature):
return cls(
start=feature.location.start,
end=feature.location.end,
strand=feature.location.strand,
)
def pairwise_gffs(a_records, b_records):
b_keys = {contig_key(b): b for b in b_records}
for a in a_records:
yield a, b_keys.get(contig_key(a), None)
def match_gffs(annoation, operons):
for ann, oper in pairwise_gffs(annoation, operons):
if oper is not None:
match_records(ann, oper)
yield ann
def match_records(annotation, ofinder):
loc_operons = {
Location.from_feature(f): f.qualifiers["operon"][0]
for f in ofinder.features
if "operon" in f.qualifiers
}
for feature in annotation.features:
loc = Location.from_feature(feature)
if loc in loc_operons:
feature.qualifiers["operon"] = [loc_operons[loc]]
continue
def main(args: InputArgs):
print(f"Looking for {','.join(args.type)}-antigens in the annotation")
with args.operons.open() as operonsh:
operon_ids = {o.id_gene: o.operon for o in parse_list_of_operons(operonsh)}
with args.annotation.open() as inputh, args.coordinates.open() as coordsh:
gff = mark_antigenes(parse_gff(inputh), args.type)
operons = mark_operonfinder_operons(parse_gff(coordsh), operon_ids)
matched = match_gffs(gff, operons)
with args.output.open("w") as outh:
GFF.write(matched, out_handle=outh)
def parse_args(args: list[str] = None):
arp = argparse.ArgumentParser(
"merge-operons",
description=(
"combines annotation and the output of the operon finder into one gff"
+ "where each feature have an operon number and antigen mark qualifiers"
),
)
arp.add_argument(
"-t",
"--type",
choices=["O", "H", "K"],
action="append",
help="Antigen type",
)
g = arp.add_argument_group("annotation file")
g.add_argument("annotation", type=Path, help="gff annotation file path")
g = arp.add_argument_group(
"Operon mapper",
description="The files you will get as a result from the Operon Mapper run",
)
g.add_argument("operons", type=Path, help="list_of_operons file path")
g.add_argument("coordinates", type=Path, help="ORFs_coordinates file path")
g = arp.add_argument_group("output options")
g.add_argument("-o", "--output", type=Path, help="output file path")
###########################################################
parsed = InputArgs(**arp.parse_args(args=args).__dict__)
if parsed.output is None:
prefix = "operons-"
out_name = prefix + parsed.annotation.name
parsed.output = parsed.annotation.parent / out_name
parsed.type = set(parsed.type)
return parsed
if __name__ == "__main__":
main(parse_args())