annotate tools/samtools/pileup_interval.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
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
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 Condenses pileup format into ranges of bases.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 usage: %prog [options]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 -i, --input=i: Input pileup file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 -o, --output=o: Output pileup
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 -c, --coverage=c: Coverage
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 -f, --format=f: Pileup format
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 -b, --base=b: Base to select
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 -s, --seq_column=s: Sequence column
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 -l, --loc_column=l: Base location column
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 -r, --base_column=r: Reference base column
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 -C, --cvrg_column=C: Coverage column
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 import pkg_resources; pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 from bx.cookbook import doc_optparse
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 def stop_err( msg ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 sys.stderr.write( msg )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 def __main__():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 strout = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 #Parse Command Line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 options, args = doc_optparse.parse( __doc__ )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 coverage = int(options.coverage)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 fin = file(options.input, 'r')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 fout = file(options.output, 'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 inLine = fin.readline()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 if options.format == 'six':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 seqIndex = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 locIndex = 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 baseIndex = 2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 covIndex = 3
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 elif options.format == 'ten':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 seqIndex = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 locIndex = 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 if options.base == 'first':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 baseIndex = 2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 baseIndex = 3
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 covIndex = 7
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 seqIndex = int(options.seq_column) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 locIndex = int(options.loc_column) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 baseIndex = int(options.base_column) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 covIndex = int(options.cvrg_column) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 lastSeq = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 lastLoc = -1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 locs = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 startLoc = -1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 bases = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 while inLine.strip() != '':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 lineParts = inLine.split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 seq, loc, base, cov = lineParts[seqIndex], int(lineParts[locIndex]), lineParts[baseIndex], int(lineParts[covIndex])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 except IndexError, ei:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 if options.format == 'ten':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 stop_err( 'It appears that you have selected 10 columns while your file has 6. Make sure that the number of columns you specify matches the number in your file.\n' + str( ei ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 stop_err( 'There appears to be something wrong with your column index values.\n' + str( ei ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 except ValueError, ev:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 if options.format == 'six':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 stop_err( 'It appears that you have selected 6 columns while your file has 10. Make sure that the number of columns you specify matches the number in your file.\n' + str( ev ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 stop_err( 'There appears to be something wrong with your column index values.\n' + str( ev ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 # strout += str(startLoc) + '\n'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 # strout += str(bases) + '\n'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 # strout += '%s\t%s\t%s\t%s\n' % (seq, loc, base, cov)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 if loc == lastLoc+1 or lastLoc == -1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 if cov >= coverage:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 if seq == lastSeq or lastSeq == '':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 if startLoc == -1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 startLoc = loc
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 locs.append(loc)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 bases.append(base)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 if len(bases) > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 fout.write('%s\t%s\t%s\t%s\n' % (lastSeq, startLoc-1, lastLoc, ''.join(bases)))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 startLoc = loc
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 locs = [loc]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 bases = [base]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 if len(bases) > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 fout.write('%s\t%s\t%s\t%s\n' % (lastSeq, startLoc-1, lastLoc, ''.join(bases)))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 startLoc = -1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 locs = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 bases = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 if len(bases) > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 fout.write('%s\t%s\t%s\t%s\n' % (lastSeq, startLoc-1, lastLoc, ''.join(bases)))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 if cov >= coverage:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 startLoc = loc
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 locs = [loc]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 bases = [base]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 startLoc = -1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 locs = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 bases = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 lastSeq = seq
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 lastLoc = loc
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 inLine = fin.readline()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 if len(bases) > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 fout.write('%s\t%s\t%s\t%s\n' % (lastSeq, startLoc-1, lastLoc, ''.join(bases)))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 fout.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 fin.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 # import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 # strout += file(fout.name,'r').read()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 # sys.stderr.write(strout)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 if __name__ == "__main__" : __main__()