comparison prepare_amplicon_info.py @ 22:6606a8c97889 draft default tip

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