-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMoGECE.py
executable file
·350 lines (242 loc) · 9.88 KB
/
MoGECE.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
344
345
346
347
348
349
350
#!/usr/bin/env python3
#
# MoGECE: extract coordinates for mobile genetic elements from outputs
# produced by MGE detection software
#
# Version 1.0.2 - May 28, 2018
#
# Copyright © 2018 Danillo Oliveira Alvarenga
#
# MoGECE is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# MoGECE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with MoGECE. If not, see <http://www.gnu.org/licenses/agpl-3.0.html>.
#
import argparse
# Get command line arguments.
parser = argparse.ArgumentParser(
description="Mobile Genetic Element Coordinates Extractor")
parser.add_argument("-v", "--version", action="version",
version="%(prog)s 1.0.2", help="show version and exit")
parser.add_argument("-f", "--file", metavar="File", required=True,
help="prediction file")
xgroup = parser.add_mutually_exclusive_group(required=True)
xgroup.add_argument("-l", "--alienhunter", action="store_true",
help="using Alien_Hunter sco prediction file")
xgroup.add_argument("-i", "--issaga", action="store_true",
help="using ISsaga csv prediction file")
xgroup.add_argument("-m", "--minced", action="store_true",
help="using MinCED gff prediction file")
xgroup.add_argument("-o", "--oasis", action="store_true",
help="using OASIS gff prediction file")
xgroup.add_argument("-w", "--oligowords", action="store_true",
help="using OligoWords out prediction file")
xgroup.add_argument("-p", "--phispy", action="store_true",
help="using PhiSpy tbl prediction file")
xgroup.add_argument("-s", "--sniffer", action="store_true",
help="using SeqWordSniffer out prediction file")
xgroup.add_argument("-r", "--virsorter", action="store_true",
help="using VirSorter fasta prediction file")
parser.add_argument("-a", "--artemis", action="store_true",
help="generate an Artemis gff file")
parser.add_argument("-g", "--gview", action="store_true",
help="generate a GView csv file")
args = parser.parse_args()
if not (args.gview or args.artemis):
parser.error("Please indicate a visualization program")
# Create lists for MGE coordinates and scores.
beginnings = []
ends = []
scores = []
isfamilies = []
# Get coordinates from an Alien_Hunter output file.
def alien_hunter():
with open(args.file, "rt") as output:
for line in output:
line = line.split(' ')
if "misc_feature" in line:
beginning = line[-1].split("..")[0].strip()
end = line[-1].split("..")[1].strip()
add_coordinates(beginning, end)
else:
score = line[-1].split("=")[1].strip()
score = str(round(float(score), 1))
scores.append(score)
return ("alienhunter", "genomic island", "Alien_Hunter")
# Get coordinates from an ISsaga output file.
def is_saga():
with open(args.file, "rt") as output:
IS = False
ORF_L = 0
ORF_R = 0
family = 0
for line in output:
if "false positive" in line:
continue
line = line.split(',')
if len(line) > ORF_R:
if 'orf_L' and 'orf_R' in line:
ORF_L = line.index('orf_L')
ORF_R = line.index('orf_R')
family = line.index('family')
elif ORF_L is not 0 and ORF_R is not 0:
beginning = line[ORF_L]
end = line[ORF_R]
add_coordinates(beginning, end)
scores.append('1')
isfamilies.append(line[family].replace("/IS", "|IS"))
return ("issaga", "transposase", "ISsaga")
# Get coordinates from a MinCED output file.
def min_ced():
with open(args.file, "rt") as output:
for line in output:
if "##gff" in line:
continue
line = line.split("\t")
beginning = line[line.index("repeat_region") + 1]
end = line[line.index("repeat_region") + 2]
add_coordinates(beginning, end)
scores.append('1')
return ("minced", "CRISPR", "MinCED")
# Get coordinates from an OASIS output file.
def oas_is():
with open(args.file, "rt") as output:
for line in output:
line_items = list(line.split("\t"))
beginning = line_items[3]
end = line_items[4]
add_coordinates(beginning, end)
scores.append('1')
return ("oasis", "transposase", "OASIS")
# Get coordinates from an OligoWords output file.
def oligo_words():
with open(args.file, "rt") as output:
island = False
for line in output:
if island:
if not line.rstrip():
break
line_items = list(line.split("\t"))
beginning = line_items[0]
end = line_items[1]
add_coordinates(beginning, end)
score = str(round(float(line_items[2]), 1))
scores.append(score)
if "n0_4mer:PS" in line:
island = True
return ("oligowords", "genomic island", "OligoWords")
# Get coordinates from a PhiSpy output file.
def phi_spy():
with open(args.file, "rt") as output:
for line in output:
line_items = list(line.strip().split("\t"))
beginning = line_items[1].split('_')[-2]
end = line_items[1].split('_')[-1]
add_coordinates(beginning, end)
scores.append('1')
return ("phispy", "prophage", "PhiSpy")
# Get coordinates from a SeqWord Gene Island Sniffer output file.
def seqword_sniffer():
with open(args.file, "rt") as output:
for line in output:
if "<COORDINATES>" in line:
line = line.split(' ')
coordinates = line.index("<COORDINATES>") + 1
beginning = line[coordinates].split("-")[0]
end = line[coordinates].split("-")[1]
add_coordinates(beginning, end)
score = str(round(float(line[-1].strip()), 1))
scores.append(score)
return ("sniffer", "genomic island", "SeqWord Sniffer")
# Get coordinates from a VirSorter output file.
def vir_sorter():
with open(args.file, "rt") as output:
for line in output:
if '>' in line:
line = line.split("_gene_")
beginning = line[2].split("-")[1]
end = line[2].split("-")[2]
add_coordinates(beginning, end)
scores.append('1')
return ("virsorter", "prophage", "VirSorter")
# Add coordinates to lists making sure the ORF is correct.
def add_coordinates(beginning, end):
if int(beginning) > int(end):
beginning, end = end, beginning
beginnings.append(beginning)
ends.append(end)
# Generate a range file containing coordinates for visualization in GView.
def create_csv(feature):
with open(feature[0] + ".csv", "wt") as csv:
i = 0
while i < len(scores):
csv.write(beginnings[i] + ',' + ends[i] + ',' + scores[i] + '\n')
i += 1
# Generate a feature table from coordinates for visualization in Artemis.
def create_ft(feature):
with open(feature[0] + ".ft", "wt") as ft:
i = 0
while i < len(scores):
if feature[1] == "transposase":
ft.write("FT repeat_region " +
beginnings[i] + ".." + ends[i] + "\n")
elif feature[1] == "prophage":
ft.write("FT misc_feature " +
beginnings[i] + ".." + ends[i] + "\n")
elif feature[1] == "CRISPR":
ft.write("FT misc_feature " +
beginnings[i] + ".." + ends[i] + "\n")
elif feature[1] == "genomic island":
ft.write("FT mobile_element " +
beginnings[i] + ".." + ends[i] + "\n")
if not isfamilies:
ft.write("FT /note=" + feature[1] + "\n")
else:
if isfamilies[i]:
ft.write("FT /note=" + feature[1] +
" from the " + isfamilies[i].rstrip() +
" family\n")
else:
ft.write("FT /note=" + feature[1] +
" from an unidentified family\n")
ft.write("FT " +
"/annotation_source=" + feature[2] + "\n")
if scores[i] is '1':
i += 1
continue
else:
ft.write("FT " +
"/score=" + scores[i] + "\n")
i += 1
# Run functions according to corresponding arguments.
def main():
if args.alienhunter:
feature = alien_hunter()
elif args.issaga:
feature = is_saga()
elif args.minced:
feature = min_ced()
elif args.oasis:
feature = oas_is()
elif args.phispy:
feature = phi_spy()
elif args.virsorter:
feature = vir_sorter()
elif args.sniffer:
feature = seqword_sniffer()
elif args.oligowords:
feature = oligo_words()
if args.artemis:
create_ft(feature)
if args.gview:
create_csv(feature)
if __name__ == "__main__":
main()