forked from brianpenghe/fasta-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastReadLengthDist.py
88 lines (78 loc) · 2.17 KB
/
fastReadLengthDist.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
##################################
# #
# Last modified 10/16/2013 #
# #
# Georgi Marinov #
# #
##################################
import sys
try:
import psyco
psyco.full()
except:
pass
def main(argv):
if len(argv) < 3:
print 'usage: python %s inputfilename outfilename [-f | -q]' % argv[0]
print '\tuse - for stdinout instead a input filename'
sys.exit(1)
inputfilename = argv[1]
outputfilename = argv[2]
type = argv[3]
outfile = open(outputfilename, 'w')
if inputfilename == '-':
input_stream = sys.stdin
else:
input_stream = open(inputfilename)
i=0
LenDict={}
seq=''
if type == '-f':
for line in input_stream:
if i % 1000000 == 0:
print i, 'reads processed'
i=i+1
if line[0]=='>':
if seq=='':
continue
length=len(seq)
if LenDict.has_key(length):
pass
else:
LenDict[length]=0
LenDict[length]+=1
seq=''
else:
seq=seq+line.strip()
skipNext=False
seqNext=False
if type == '-q':
for line in input_stream:
if line[0]=='+':
skipNext=True
continue
if skipNext:
skipNext=False
continue
if line[0]=='@':
seqNext=True
continue
if seqNext:
length=len(line.strip())
if LenDict.has_key(length):
LenDict[length]+=1
else:
LenDict[length]=1
seqNext=False
i=i+1
if i % 1000000 == 0:
print i, 'reads processed'
continue
Lengths=LenDict.keys()
Lengths.sort()
for L in Lengths:
outline=str(L)+'\t'+str(LenDict[L])+'\n'
outfile.write(outline)
outfile.close()
if __name__ == '__main__':
main(sys.argv)