annotate prepare_amplicon_info.py @ 7:43aac8d29685 draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 80ad6146e8852fb04fbdbe7b14ab120eee605e3a"
author iuc
date Fri, 11 Jun 2021 15:41:24 +0000
parents 75c279fa403a
children ee29337f905c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
1 #!/usr/bin/env python
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
2
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
3 # extends ivar trim's amplicon info parsing abilities
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
4 # to include calculation of amplicon regions from
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
5 # sets of nested (more than two) primers
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
6
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
7 import sys
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
8
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
9
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
10 # parse primers and their start positions from BED file
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
11 primer_starts = {}
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
12 with open(sys.argv[1]) as i:
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
13 for line in i:
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
14 f = line.strip().split('\t')
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
15 try:
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
16 if f[5] == '+':
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
17 primer_starts[f[3]] = int(f[1])
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
18 elif f[5] == '-':
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
19 primer_starts[f[3]] = int(f[2]) - 1
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
20 else:
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
21 raise ValueError()
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
22 except (IndexError, ValueError):
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
23 sys.exit(
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
24 'Primer BED file needs to be TAB-separated with the '
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
25 'following columns: '
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
26 'chrom, chromStart, chromEnd, name, score, strand, '
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
27 'where "chromStart", "chromEnd" need to be integer values '
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
28 'and "strand" needs to be either "+" or "-".'
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
29 )
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
30
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
31 # parse amplicon info and record outer primer names
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
32 with open(sys.argv[2]) as i:
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
33 ret_lines = []
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
34 for line in i:
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
35 first = last = None
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
36 for pname in line.strip().split('\t'):
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
37 try:
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
38 primer_start = primer_starts[pname]
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
39 except KeyError:
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
40 sys.exit(
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
41 'Amplicon info with primer name not found in '
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
42 f'primer BED file: "{pname}"'
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
43 )
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
44 if first is None or primer_start < primer_starts[first]:
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
45 first = pname
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
46 if last is None or primer_start > primer_starts[last]:
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
47 last = pname
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
48 if first == last:
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
49 sys.exit(
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
50 line
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
51 + 'is not a proper amplicon info line.'
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
52 )
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
53 ret_lines.append(f'{first}\t{last}\n')
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
54
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
55 # write amended amplicon info
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
56 with open(sys.argv[3], 'w') as o:
75c279fa403a "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
57 o.writelines(ret_lines)