-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblastParseAmbiguityFlags.py
More file actions
executable file
·66 lines (59 loc) · 2.62 KB
/
blastParseAmbiguityFlags.py
File metadata and controls
executable file
·66 lines (59 loc) · 2.62 KB
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
#!/usr/bin/env python
import argparse
import logging
import os
from collections import defaultdict as dd
def getOptions():
""" Function to pull in arguments """
parser = argparse.ArgumentParser(description="Takes a single-end (SE) or paired-end (PE) file and splits out unique and duplicate reads.")
parser.add_argument("-i", dest="fname", action='store', required=True, help="Input BLAST file in outfmt 6 [Required]")
parser.add_argument("-o", dest="oname", action='store', required=True, help="Output file for counts in csv format [Required]")
parser.add_argument("-g", "--log", dest="log", action='store', required=False, help="Log File")
args = parser.parse_args()
#args = parser.parse_args(['-i', '/home/jfear/tmp/blast/ambiguity_blast_fb551_non-redundant_fusions.tsv', '-o', '/home/jfear/tmp/blast/test.csv', '-g', '/home/jfear/tmp/blast/test.log'])
return(args)
def setLogger(fname,loglevel):
""" Function to handle error logging """
logging.basicConfig(filename=fname, filemode='w', level=loglevel, format='%(asctime)s - %(levelname)s - %(message)s')
def main():
""" MAIN Function to execute everything """
# Turn on Logging if option -g was given
args = getOptions()
if args.log:
setLogger(args.log,logging.INFO)
else:
setLogger(os.devnull,logging.INFO)
mydict = dd(dict)
with open(args.oname, 'w') as OUT:
header = ['fusion_id','flag_self','flag_ambig','cnt_ambig','ambig_fusion_cat']
OUT.write(','.join(header) + "\n")
with open(args.fname,'r') as IN:
logging.info('Reading Blast File and Building Dictionary.')
for row in IN:
query, hit = row.split('\t')[:2]
if query == hit:
mydict[query]['self'] = 1
else:
try:
mydict[query]['ambig'].append(hit)
except:
mydict[query]['ambig'] = [hit]
logging.info('Wirting Output.')
for key in mydict:
myout = [key,'0','0','0','NA']
if mydict[key]['self']:
myout[1] = '1'
try:
ambig = mydict[key]['ambig']
cnt = len(ambig)
myout[2] = '1'
myout[3] = str(cnt)
myout[4] = '|'.join([str(x) for x in ambig])
except:
pass
else:
logging.error("Exonic region %s did not map to itself." % key)
OUT.write(','.join(myout) + "\n")
if __name__=='__main__':
main()
logging.info("Script complete.")