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