forked from brianpenghe/bamsam-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BAMalignedReads.py
60 lines (53 loc) · 1.5 KB
/
BAMalignedReads.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
##################################
# #
# Last modified 05/27/2014 #
# #
# Georgi Marinov #
# #
##################################
import sys
import pysam
import string
from sets import Set
import os
import subprocess
def main(argv):
if len(argv) < 2:
print 'usage: python %s BAM samtools' % argv[0]
print '\tthe script will print to standard output - capture it with samtools and direct it to a bam file'
print '\tspaces in optional fields will be treated as tabs'
sys.exit(1)
BAM = argv[1]
samtools = argv[2]
cmd = samtools + ' view -H ' + BAM
p = os.popen(cmd, "r")
currentLine = p.readline()
line = currentLine
print line.strip()
while line != '':
line = p.readline()
if line == '':
continue
print line.strip()
cmd = samtools + ' view ' + BAM
p = os.popen(cmd, "r")
currentLine = p.readline()
fields = currentLine.strip().split('\t')
aligned = True
if fields[2] == '*':
aligned = False
line = currentLine
if aligned:
print line.strip()
while line != '':
line = p.readline()
if line == '':
continue
fields = line.strip().split('\t')
aligned = True
if fields[2] == '*':
aligned = False
if aligned:
print line.strip()
if __name__ == '__main__':
main(sys.argv)