annotate tools/solid_tools/solid_qual_stats.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 #Guruprasad Ananda
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 import sys, os, zipfile, tempfile
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 QUAL_UPPER_BOUND = 41
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 QUAL_LOWER_BOUND = 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 def stop_err( msg ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 sys.stderr.write( "%s\n" % msg )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 def unzip( filename ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 zip_file = zipfile.ZipFile( filename, 'r' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 tmpfilename = tempfile.NamedTemporaryFile().name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 for name in zip_file.namelist():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 file( tmpfilename, 'a' ).write( zip_file.read( name ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 zip_file.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 return tmpfilename
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 def __main__():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 infile_score_name = sys.argv[1].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 fout = open(sys.argv[2].strip(),'r+w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 infile_is_zipped = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 if zipfile.is_zipfile( infile_score_name ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 infile_is_zipped = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 infile_name = unzip( infile_score_name )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 infile_name = infile_score_name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 readlen = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 invalid_lines = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 j = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 for line in file( infile_name ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 line = line.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 if not(line) or line.startswith("#") or line.startswith(">"):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 elems = line.split()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 for item in elems:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 int(item)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 if not readlen:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 readlen = len(elems)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 if len(elems) != readlen:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 print "Note: Reads in the input dataset are of variable lengths."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 j += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 except ValueError:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 invalid_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 if j > 10:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 position_dict = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 print >>fout, "column\tcount\tmin\tmax\tsum\tmean\tQ1\tmed\tQ3\tIQR\tlW\trW"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 for k,line in enumerate(file( infile_name )):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 line = line.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 if not(line) or line.startswith("#") or line.startswith(">"):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 elems = line.split()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 if position_dict == {}:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 for pos in range(readlen):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 position_dict[pos] = [0]*QUAL_UPPER_BOUND
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 if len(elems) != readlen:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 invalid_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 for ind,item in enumerate(elems):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 item = int(item)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 position_dict[ind][item]+=1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 invalid_positions = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 for pos in position_dict:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 carr = position_dict[pos] #count array for position pos
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 total = sum(carr) #number of bases found in this column.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 med_elem = int(round(total/2.0))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 lowest = None #Lowest quality score value found in this column.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 highest = None #Highest quality score value found in this column.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 median = None #Median quality score value found in this column.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 qsum = 0.0 #Sum of quality score values for this column.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 q1 = None #1st quartile quality score.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 q3 = None #3rd quartile quality score.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 q1_elem = int(round((total+1)/4.0))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 q3_elem = int(round((total+1)*3/4.0))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 for ind,cnt in enumerate(carr):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 qsum += ind*cnt
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 if cnt!=0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 highest = ind
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 if lowest==None and cnt!=0: #first non-zero count
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 lowest = ind
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 if q1==None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 if sum(carr[:ind+1]) >= q1_elem:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 q1 = ind
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 if median==None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 if sum(carr[:ind+1]) < med_elem:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 median = ind
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 if total%2 == 0: #even number of elements
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 median2 = median
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 if sum(carr[:ind+1]) < med_elem+1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 for ind2,elem in enumerate(carr[ind+1:]):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 if elem != 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 median2 = ind+ind2+1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 median = (median + median2)/2.0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 if q3==None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 if sum(carr[:ind+1]) >= q3_elem:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 q3 = ind
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 mean = qsum/total #Mean quality score value for this column.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 iqr = q3-q1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 left_whisker = max(q1 - 1.5*iqr,lowest)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 right_whisker = min(q3 + 1.5*iqr,highest)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 print >>fout,"%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" %(pos+1,total,lowest,highest,qsum,mean,q1,median,q3,iqr,left_whisker,right_whisker)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 invalid_positions += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 nullvals = ['NA']*11
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 print >>fout,"%s\t%s" %(pos+1,'\t'.join(nullvals))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 if invalid_lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 print "Skipped %d reads as invalid." %invalid_lines
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 if invalid_positions:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 print "Skipped stats computation for %d read positions." %invalid_positions
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 if __name__=="__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 __main__()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140