Mercurial > repos > devteam > pileup_interval
annotate pileup_interval.py @ 4:9c1c0b947e46 draft default tip
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
author | devteam |
---|---|
date | Fri, 15 Jan 2021 11:38:56 +0000 |
parents | a110f9d6ae24 |
children |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
3 """ | |
4 Condenses pileup format into ranges of bases. | |
5 | |
6 usage: %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 """ | |
17 import sys | |
18 | |
4
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
19 from bx.cookbook import doc_optparse |
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
20 |
0 | 21 |
22 def __main__(): | |
4
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
23 # Parse Command Line |
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
24 options, args = doc_optparse.parse(__doc__) |
0 | 25 coverage = int(options.coverage) |
4
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
26 fin = open(options.input, 'r') |
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
27 fout = open(options.output, 'w') |
0 | 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]) | |
4
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
56 except IndexError as ei: |
0 | 57 if options.format == 'ten': |
4
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
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)) |
0 | 59 else: |
4
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
60 sys.exit('There appears to be something wrong with your column index values.\n' + str(ei)) |
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
61 except ValueError as ev: |
0 | 62 if options.format == 'six': |
4
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
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)) |
0 | 64 else: |
4
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
65 sys.exit('There appears to be something wrong with your column index values.\n' + str(ev)) |
0 | 66 # strout += str(startLoc) + '\n' |
67 # strout += str(bases) + '\n' | |
68 # strout += '%s\t%s\t%s\t%s\n' % (seq, loc, base, cov) | |
4
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
69 if loc == lastLoc + 1 or lastLoc == -1: |
0 | 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: | |
4
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
78 fout.write('%s\t%s\t%s\t%s\n' % (lastSeq, startLoc - 1, lastLoc, ''.join(bases))) |
0 | 79 startLoc = loc |
80 locs = [loc] | |
81 bases = [base] | |
82 else: | |
83 if len(bases) > 0: | |
4
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
84 fout.write('%s\t%s\t%s\t%s\n' % (lastSeq, startLoc - 1, lastLoc, ''.join(bases))) |
0 | 85 startLoc = -1 |
86 locs = [] | |
87 bases = [] | |
88 else: | |
89 if len(bases) > 0: | |
4
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
90 fout.write('%s\t%s\t%s\t%s\n' % (lastSeq, startLoc - 1, lastLoc, ''.join(bases))) |
0 | 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: | |
4
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
103 fout.write('%s\t%s\t%s\t%s\n' % (lastSeq, startLoc - 1, lastLoc, ''.join(bases))) |
0 | 104 fout.close() |
105 fin.close() | |
106 # import sys | |
107 # strout += file(fout.name,'r').read() | |
108 # sys.stderr.write(strout) | |
109 | |
4
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
110 |
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
111 if __name__ == "__main__": |
9c1c0b947e46
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/pileup_interval commit 8b2095c59ecc2e94c58a42e2e04dbcecdc823dbf"
devteam
parents:
0
diff
changeset
|
112 __main__() |