5 if len(sys
.argv
) == 1 :
6 print('[x]No arguments given!',file=sys
.stderr
,flush
=True)
8 print("Name of the script:",sys
.argv
[0])
9 print("Number of arguments:", len(sys
.argv
))
10 print("Arguments:" , str(sys
.argv
))
11 if len(sys
.argv
) < 3 :
12 print('Usage:',sys
.argv
[0],'<in.bam> <outprefix> [verbose=0]',file=sys
.stderr
,flush
=True)
15 verbose
= int(sys
.argv
[3])
16 except: # `except IndexError:` and `except ValueError:`
19 inBAMname
= sys
.argv
[1]
20 outPrefix
= sys
.argv
[2]
21 print('From:[{}], To:[{}.(Watson|Crick|Dropped).bam].\nVerbose: [{}].'.format(inBAMname
,outPrefix
,verbose
),file=sys
.stderr
,flush
=True)
22 splitBSbam(inBAMname
,outPrefix
,3,verbose
)
25 def __init__(self
, name
=None, verbose
=1):
27 self
.verbose
= verbose
36 def write(self
, read
):
38 print ("[{}]\t{}\t{}".format(self
.name
, read
.query_name
, read
.flag
))
42 def splitBSbam(inBAMname
,outPrefix
,n_threads
=3,verbose
=0):
44 samfile
= pysam
.AlignmentFile(inBAMname
, "rb")
46 fileWatson
= pysam
.AlignmentFile('.'.join((outPrefix
,'Watson','bam')), "wb", template
=samfile
, threads
=n_threads
)
47 fileCrick
= pysam
.AlignmentFile('.'.join((outPrefix
,'Crick','bam')), "wb", template
=samfile
, threads
=n_threads
)
48 fileDropped
= pysam
.AlignmentFile('.'.join((outPrefix
,'Dropped','bam')), "wb", template
=samfile
, threads
=n_threads
)
50 fileWatson
= DebugWrite('oWatson',verbose
)
51 fileCrick
= DebugWrite('oCrick',verbose
)
52 fileDropped
= DebugWrite('oDropped',verbose
)
53 from collections
import defaultdict
54 stats
= defaultdict(int)
55 stats
['Watson']=stats
['Crick']=0 # make them be first in `str(stats)`.
57 if read
.flag
& 0xF00 :
58 fileDropped
.write(read
)
59 stats
['Dropped_Supplementary'] += 1
62 tagYD
= read
.get_tag('YD')
65 fileWatson
.write(read
)
71 print(tagYD
,read
.query_name
,read
.flag
)
75 #print('[!]',sys.exc_info()[1]) # (<class 'KeyError'>, KeyError("tag 'YD' not present"), <traceback object at 0x10f664ec8>)
78 stats
['etc_nonYD'] += 1
79 if read
.mate_is_unmapped
:
80 fileDropped
.write(read
)
81 stats
['Dropped_PEUnmapped'] += 1
82 elif (read
.is_read2
and (not read
.mate_is_reverse
)) or (read
.is_read1
and read
.mate_is_reverse
) :
83 fileWatson
.write(read
)
85 elif (read
.is_read2
and read
.mate_is_reverse
) or (read
.is_read1
and (not read
.mate_is_reverse
)) :
92 stats
['etc_nonBWA-meth'] += 1
93 if (read
.is_read1
and (not read
.is_reverse
)) or (read
.is_read2
and read
.is_reverse
) :
94 fileWatson
.write(read
)
96 elif (read
.is_read1
and read
.is_reverse
) or (read
.is_read2
and (not read
.is_reverse
)) :
103 print(str(stats
),file=sys
.stderr
,flush
=True)
105 #pprint.pprint(stats, indent=2, stream=sys.stderr)
111 if __name__
== "__main__":
112 main() # time ./samflagfilter.py t.bam tt2