annotate prepare_amplicon_info.py @ 8:397e5f0eb3ef draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
author iuc
date Thu, 05 Aug 2021 12:46:37 +0000
parents cf65217ad61c
children 5671e1d3d5ee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
cf65217ad61c "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
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
2
cf65217ad61c "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
cf65217ad61c "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
cf65217ad61c "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
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
6
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
7 import sys
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
8
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
9
cf65217ad61c "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
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
11 primer_starts = {}
cf65217ad61c "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:
cf65217ad61c "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:
cf65217ad61c "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')
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
15 try:
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
16 if f[5] == '+':
cf65217ad61c "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])
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
18 elif f[5] == '-':
cf65217ad61c "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
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
20 else:
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
21 raise ValueError()
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
22 except (IndexError, ValueError):
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
23 sys.exit(
cf65217ad61c "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 '
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
25 'following columns: '
cf65217ad61c "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, '
cf65217ad61c "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 '
cf65217ad61c "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 "-".'
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
29 )
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
30
cf65217ad61c "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
cf65217ad61c "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:
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
33 ret_lines = []
cf65217ad61c "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:
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
35 first = last = None
cf65217ad61c "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'):
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
37 try:
cf65217ad61c "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]
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
39 except KeyError:
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
40 sys.exit(
cf65217ad61c "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 '
cf65217ad61c "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}"'
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
43 )
cf65217ad61c "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]:
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
45 first = pname
cf65217ad61c "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]:
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
47 last = pname
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
48 if first == last:
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
49 sys.exit(
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
50 line
cf65217ad61c "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.'
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
52 )
cf65217ad61c "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')
cf65217ad61c "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
54
cf65217ad61c "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
cf65217ad61c "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:
cf65217ad61c "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)