-
Notifications
You must be signed in to change notification settings - Fork 1
/
NessieOutParser.py
517 lines (456 loc) · 15.5 KB
/
NessieOutParser.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#!/usr/bin/env python
######################################################################################
#
## NessieOutParser.py
# Script to format the output from NeSSie related to Palindromes, Mirrors or
# Triplexes.
# The -s flag allows to assign a score to the retrieved motifs, that are ordered
# from highest to lowest.
# Alternatively the results can be ordered by indexes (default), or by counts (-c).
# The script allows also to visualize the optimal alignment using the -a flag.
# Finally -g will generate a gff file to easy the visualization of the results.
#
# Author: Michele Berselli
# University of Padova
#
## LICENSE:
# Copyright (C) 2017 Michele Berselli
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
######################################################################################
# Import libraries
import sys, argparse
# Functions definition
def routine_score(seq, seq_len, align):
# Variables
score_increment, score_idls, score_mm = -2, -1, -1
score = seq_len
if align:
align_string = ''
aln, last_aln = '', ''
for i in range(0, len(align), 2):
aln += align[i]
aln += align[i + 1]
if aln == '00':
if last_aln == '01':
score += score_increment
#score_increment -= 1
else:
score += score_idls
#end if
align_string += 'u'
elif aln == '11':
if last_aln == '01':
score += score_increment
#score_increment -= 1
else:
score += score_idls
#end if
align_string += 'l'
elif aln == '01':
align_string += 'M'
else:
if last_aln == '01':
score += score_increment
#score_increment -= 1
else:
score += score_mm
#end if
align_string += 'm'
#end if
last_aln = aln
aln = ''
#end for
else:
align_string = ''
#end if
return score, align_string
#end def routine_score
def routine_print_score_align(of, seq, seq_len, align, palindrome, print_aln):
score, align_string = routine_score(seq, seq_len, align)
# Writing score
of.write('{0}\n'.format(score))
if print_aln:
routine_print_align(of, seq, seq_len, align, palindrome, align_string)
#end if
#end def routine_print_score_align
def routine_print_align(of, seq, seq_len, align, palindrome, align_string):
# Variables
equal_len = False
dict_conversion = {'A': 'T', 'C': 'G', 'T': 'A', 'G': 'C'}
if align_string:
# Write first sequence
of.write('@aln: ')
i = 0
for l in align_string:
if l == 'M' or l == 'm' or l == 'u':
of.write('{0}'.format(seq[i]))
i += 1
else:
of.write('-')
#end if
#end for
of.write('\n')
# Write align
of.write('@aln: ')
for l in align_string:
if l == 'M':
of.write('|')
else:
of.write(' ')
#end if
#end for
of.write('\n')
# Write second sequence
of.write('@aln: ')
i = 0
for l in align_string:
if l == 'M' or l == 'm' or l == 'l':
of.write('{0}'.format(seq[seq_len - i - 1]))
i += 1
else:
of.write('-')
#end if
#end for
of.write('\n')
else:
first_half = seq_len / 2
second_half = seq_len - first_half
if first_half == second_half:
equal_len = True
#end if
# Write first sequence
of.write('@aln: ')
for i in range(first_half):
of.write('{0}'.format(seq[i]))
#end for
if not equal_len:
of.write('-')
#end if
of.write('\n')
# Write align
of.write('@aln: ')
if not palindrome:
for i in range(first_half):
if seq[i] == seq[seq_len - i - 1]:
of.write('|')
else:
of.write(' ')
#end if
#end for
else:
for i in range(first_half):
if seq[i] == dict_conversion[seq[seq_len - i - 1]]:
of.write('|')
else:
of.write(' ')
#end if
#end for
#end if
of.write('\n')
# Write align
of.write('@aln: ')
for i in range(second_half):
of.write('{0}'.format(seq[seq_len - i - 1]))
#end for
of.write('\n')
#end if
#end def routine_print_align
def routine_print_ordered(key, value, of, counts_only, indexes_only, show_align, palindrome):
of.write('$|{0}|{1}|'.format(value['seq_len'], key))
if show_align:
routine_print_score_align(of, key, value['seq_len'], value['align'], palindrome, True)
else:
routine_print_score_align(of, key, value['seq_len'], value['align'], palindrome, False)
#end if
if counts_only:
of.write('@counts: {0}\n'.format(value['counts']))
elif indexes_only:
of.write('@indexes: ')
[of.write('{0}|'.format(i)) for i in value['indexes']]
of.write('\n')
else:
of.write('@counts: {0}\n'.format(value['counts']))
of.write('@indexes: ')
[of.write('{0}|'.format(i)) for i in value['indexes']]
of.write('\n')
#end if
#end def routine_print_ordered
#def routine_print_gff(dict_to_print, of, counts_only, indexes_only, show_align, palindrome, seq_ID, seq_type, strand):
## Variables
#if seq_type == 0:
#tipo = 'mirror'
#elif seq_type == 1:
#tipo = 'palindrome'
#else:
#tipo = 'triplex'
##end if
#dict_idxs_key = {}
#for (key, value) in dict_to_print.iteritems():
#for idx in value['indexes']:
#dict_idxs_key.setdefault(idx, key)
##end for
##end for
#for (idx, key) in sorted(dict_idxs_key.iteritems()):
#score, _ = routine_score(key, dict_to_print[key]['seq_len'], dict_to_print[key]['align'])
#of.write('{0}\tNeSSie\t{1}\t{2}\t{3}\t{4}\t{5}\t.\tID={6}\n'.format(
#seq_ID,
#tipo,
#idx,
#idx + dict_to_print[key]['seq_len'],
#score,
#strand,
#key
#))
##end for
##end def routine_print_gff
def routine_print_gff(dict_to_print, of, counts_only, indexes_only, show_align, palindrome, seq_ID, seq_type, strand):
# Variables
if seq_type == 0:
tipo = 'mirror'
elif seq_type == 1:
tipo = 'palindrome'
else:
tipo = 'triplex'
#end if
dict_idxs_key = {}
max_len = 1
for (key, value) in dict_to_print.iteritems():
if value['seq_len'] > max_len:
max_len = value['seq_len']
#end if
for idx in value['indexes']:
dict_idxs_key.setdefault(idx, key)
#end for
#end for
# Color scale
color_dict = {0: 'FF0000', 1: 'FF0000', 2: 'FFEE50', 3: '0080FF', 4: '00FF80', 5: '00FF80', 6: '00FF80', 7: '00FF80'}
incr = max_len / 5
for (idx, key) in sorted(dict_idxs_key.iteritems()):
score, _ = routine_score(key, dict_to_print[key]['seq_len'], dict_to_print[key]['align'])
of.write('{0}\tNeSSie\t{1}\t{2}\t{3}\t{4}\t{5}\t.\tID={6};color=#{7};score={8}\n'.format(
seq_ID,
tipo,
idx + 1,
idx + dict_to_print[key]['seq_len'],
score,
strand,
key,
color_dict[score / incr],
score
))
#end for
#end def routine_print_gff
def print_ordered(dict_to_print, flag_counts, of, counts_only, indexes_only, show_align, palindrome, flag_gff, seq_ID, seq_type, strand):
if not flag_gff:
if flag_counts:
for (key, value) in sorted(dict_to_print.iteritems(), key=lambda(x, y): y['counts'], reverse=True):
routine_print_ordered(key, value, of, counts_only, indexes_only, show_align, palindrome)
#end for
else:
for (key, value) in sorted(dict_to_print.iteritems(), key=lambda(x, y): y['indexes'][0]):
routine_print_ordered(key, value, of, counts_only, indexes_only, show_align, palindrome)
#end for
#end if
else:
routine_print_gff(dict_to_print, of, counts_only, indexes_only, show_align, palindrome, seq_ID, seq_type, strand)
#end if
#end def print_ordered
def main(args):
## Variables
flag_counts = True if args['orderbycounts'] else False
show_align = True if args['alignshow'] else False
flag_gff = True if args['gff'] else False
counts_only = False
indexes_only = False
palindrome = False
seq_type = 0 #0=mirror, 1=palindrome, 2=triplex
strand = '+'
## Init data structures
dict_hits = {} # {seq: {seq_len: s, align: 'a', counts: c, intervals: [(i, i + s), ...], indexes: [i, ...]}, ...}
dict_sorted = {}
## Opening output file
of = open(args['outputfile'], 'w')
## Reading input file
with open(args['inputfile']) as fi:
first_seq = True
for line in fi:
if line.startswith('#'):
command_line = line.rstrip().split()
if ('-P' in command_line) or ('--palindrome' in command_line):
palindrome = True
seq_type = 1
elif ('-T' in command_line) or ('--triplex' in command_line):
seq_type = 2
#end if
if ('-C' in command_line) or ('--complement' in command_line):
strand = '-'
#end if
if ('-c' in command_line) or ('--counts' in command_line):
flag_counts = True # having only counts hits will be ordered by counts
counts_only = True
elif ('-i' in command_line) or ('--indexes' in command_line):
if flag_counts:
print '\nTo order by counts provide output with counts, results will be ordered by indexes!\n'
#end if
indexes_only = True
flag_counts = False # having only indexes hits will be ordered by indexes
#end if
elif line.startswith('>') and first_seq:
seq_ID = line.rstrip().split()[0][1:]
if not flag_gff:
of.write('{0}\n'.format(line.rstrip()))
#end if
first_seq = False
elif line.startswith('>') and not first_seq:
print_ordered(dict_hits, flag_counts, of, counts_only, indexes_only, show_align, palindrome, flag_gff, seq_ID, seq_type, strand)
seq_ID = line.rstrip().split()[0][1:]
if not flag_gff:
of.write('{0}\n'.format(line.rstrip()))
#end if
dict_hits = {} # re-initializing dict_hits for new sequence
elif line.startswith('$'):
seq_len, seq = int(line.split('|')[1]), line.split('|')[2].rstrip()
if len(line.split('|')) == 4:
align = line.split('|')[3].rstrip()
else:
align = None
#end if
dict_hits.setdefault(seq, {'seq_len' : seq_len, 'align': align, 'counts': 0, 'indexes': []})
elif line.startswith('@counts'):
counts = int(line.rstrip().split()[1])
dict_hits[seq]['counts'] += counts
elif line.startswith('@indexes'):
indexes = map(lambda x: int(x), line.rstrip().split()[1].split('|')[:-1])
dict_hits[seq]['indexes'] += indexes
#end if
#end for
print_ordered(dict_hits, flag_counts, of, counts_only, indexes_only, show_align, palindrome, flag_gff, seq_ID, seq_type, strand)
#end if
#end with
## Closing output file
of.close()
## Order by score
dict_to_order_score = {}
#if args['score']:
#if flag_gff:
#print '\nNot possible to order a gff file by score, a normal gff have been produced!\n'
#else:
#old_of = open(args['outputfile'], 'r')
#for line in old_of:
#if line.startswith('#'):
#command_line = line.rstrip()
#elif line.startswith('>'):
#seq_code = line.rstrip()[1:]
#dict_to_order_score.setdefault(seq_code, {})
#elif line.startswith('$'):
#seq_len, seq, score = int(line.split('|')[1]), line.split('|')[2].rstrip(), int(line.split('|')[3].rstrip())
#dict_to_order_score[seq_code].setdefault(seq, {'seq_len' : seq_len, 'align': align, 'counts': 0, 'indexes': [], 'score': score})
#elif line.startswith('@counts'):
#counts = int(line.rstrip().split()[1])
#dict_to_order_score[seq_code][seq]['counts'] += counts
#elif line.startswith('@indexes'):
#indexes = map(lambda x: int(x), line.rstrip().split()[1].split('|')[:-1])
#dict_to_order_score[seq_code][seq]['indexes'] += indexes
##end if
##end for
#old_of.close()
## Write
#of = open(args['outputfile'], 'w')
#for key, dict_key in dict_to_order_score.iteritems():
#of.write('>{0}\n'.format(key))
#for key_i, value_i in sorted(dict_key.iteritems(), key=lambda(x, y): y['score'], reverse = True):
#of.write('$|{0}|{1}|{2}\n'.format(value_i['seq_len'], key_i, value_i['score']))
#if value_i['counts']:
#of.write('@counts: {0}\n'.format(value_i['counts']))
##end if
#if value_i['indexes']:
#of.write('@indexes: ')
#[of.write('{0}|'.format(index)) for index in value_i['indexes']]
#of.write('\n')
##end if
##end for
##end for
##end if
##end if
if args['score']:
if flag_gff:
print '\nNot possible to order a gff file by score, a normal gff have been produced!\n'
else:
old_of = open(args['outputfile'], 'r')
idx_seq = 0
c = 1
for line in old_of:
if line.startswith('#'):
command_line = line.rstrip()
elif line.startswith('>'):
idx_seq += 1
seq_code = line.rstrip()[1:]
dict_to_order_score.setdefault((seq_code, idx_seq), {})
elif line.startswith('$'):
seq_len, seq, score = int(line.split('|')[1]), line.split('|')[2].rstrip(), int(line.split('|')[3].rstrip())
dict_to_order_score[(seq_code, idx_seq)].setdefault(seq, {'seq_len' : seq_len, 'align_1': '', 'align_2': '', 'align_3': '', 'counts': 0, 'indexes': [], 'score': score})
elif line.startswith('@counts'):
counts = int(line.rstrip().split()[1])
dict_to_order_score[(seq_code, idx_seq)][seq]['counts'] += counts
elif line.startswith('@indexes'):
indexes = map(lambda x: int(x), line.rstrip().split()[1].split('|')[:-1])
dict_to_order_score[(seq_code, idx_seq)][seq]['indexes'] += indexes
elif line.startswith('@aln') and c == 1:
c += 1
dict_to_order_score[(seq_code, idx_seq)][seq]['align_1'] = line.rstrip()
elif line.startswith('@aln') and c == 2:
c += 1
dict_to_order_score[(seq_code, idx_seq)][seq]['align_2'] = line.rstrip()
elif line.startswith('@aln') and c == 3:
c = 1
dict_to_order_score[(seq_code, idx_seq)][seq]['align_3'] = line.rstrip()
#end if
#end for
old_of.close()
# Write
of = open(args['outputfile'], 'w')
for (key, idx_key), dict_key in sorted(dict_to_order_score.iteritems(), key=lambda(x,y): x[1]):
of.write('>{0}\n'.format(key))
for key_i, value_i in sorted(dict_key.iteritems(), key=lambda(x, y): y['score'], reverse = True):
of.write('$|{0}|{1}|{2}\n'.format(value_i['seq_len'], key_i, value_i['score']))
if value_i['align_1']:
of.write('{0}\n'.format(value_i['align_1']))
of.write('{0}\n'.format(value_i['align_2']))
of.write('{0}\n'.format(value_i['align_3']))
if value_i['counts']:
of.write('@counts: {0}\n'.format(value_i['counts']))
#end if
if value_i['indexes']:
of.write('@indexes: ')
[of.write('{0}|'.format(index)) for index in value_i['indexes']]
of.write('\n')
#end if
#end for
#end for
#end if
#end if
#end def main
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Script to format the output from NeSSIe')
parser.add_argument('-i','--inputfile', help='output file from NeSSIe as input', required=True)
parser.add_argument('-o','--outputfile', help='file to store formatted output', required=True)
parser.add_argument('-c','--orderbycounts', help='order the results by counts and not by indexes', action='store_true', required=False)
parser.add_argument('-a','--alignshow', help='shows optimal alignment', action='store_true', required=False)
parser.add_argument('-g','--gff', help='generate GFF file', action='store_true', required=False)
parser.add_argument('-s','--score', help='order by score', action='store_true', required=False)
args = vars(parser.parse_args())
main(args)
#end if