-
Notifications
You must be signed in to change notification settings - Fork 2
/
SAMchromosomeOrder.py
65 lines (54 loc) · 1.43 KB
/
SAMchromosomeOrder.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
##################################
# #
# Last modified 06/26/2012 #
# #
# Georgi Marinov #
# #
##################################
import sys
import random
import pysam
import string
from sets import Set
try:
import psyco
psyco.full()
except:
pass
def main(argv):
if len(argv) < 2:
print 'usage: python %s SAMfilename outputfilename [-bam]' % argv[0]
sys.exit(1)
SAM = argv[1]
outputfilename = argv[2]
doBAM = False
if '-bam' in argv:
doBAM = True
outfile=open(outputfilename, 'w')
ChrDict={}
if doBAM:
samfile = pysam.Samfile(SAM, "rb" )
for read in samfile.fetch(until_eof=True):
chr = samfile.getrname(read.tid)
if ChrDict.has_key(chr):
continue
else:
ChrDict[chr]=''
print chr
outfile.write(chr + '\n')
else:
lineslist = open(SAM)
for line in lineslist:
if line.startswith('@'):
continue
fields = line.strip().split('\t')
chr = fields[2]
if ChrDict.has_key(chr):
continue
else:
ChrDict[chr]=''
print chr
outfile.write(chr + '\n')
outfile.close()
if __name__ == '__main__':
main(sys.argv)