annotate pileup_interval.py @ 0:a110f9d6ae24 draft

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