Mercurial > repos > iuc > ivar_variants
comparison prepare_amplicon_info.py @ 5:49236b03e4fd draft
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
author | iuc |
---|---|
date | Wed, 19 May 2021 16:49:13 +0000 |
parents | |
children | 584beffa972b |
comparison
equal
deleted
inserted
replaced
4:f95f403841ad | 5:49236b03e4fd |
---|---|
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 f = line.strip().split('\t') | |
15 try: | |
16 if f[5] == '+': | |
17 primer_starts[f[3]] = int(f[1]) | |
18 elif f[5] == '-': | |
19 primer_starts[f[3]] = int(f[2]) - 1 | |
20 else: | |
21 raise ValueError() | |
22 except (IndexError, ValueError): | |
23 sys.exit( | |
24 'Primer BED file needs to be TAB-separated with the ' | |
25 'following columns: ' | |
26 'chrom, chromStart, chromEnd, name, score, strand, ' | |
27 'where "chromStart", "chromEnd" need to be integer values ' | |
28 'and "strand" needs to be either "+" or "-".' | |
29 ) | |
30 | |
31 # parse amplicon info and record outer primer names | |
32 with open(sys.argv[2]) as i: | |
33 ret_lines = [] | |
34 for line in i: | |
35 first = last = None | |
36 for pname in line.strip().split('\t'): | |
37 try: | |
38 primer_start = primer_starts[pname] | |
39 except KeyError: | |
40 sys.exit( | |
41 'Amplicon info with primer name not found in ' | |
42 f'primer BED file: "{pname}"' | |
43 ) | |
44 if first is None or primer_start < primer_starts[first]: | |
45 first = pname | |
46 if last is None or primer_start > primer_starts[last]: | |
47 last = pname | |
48 if first == last: | |
49 sys.exit( | |
50 line | |
51 + 'is not a proper amplicon info line.' | |
52 ) | |
53 ret_lines.append(f'{first}\t{last}\n') | |
54 | |
55 # write amended amplicon info | |
56 with open(sys.argv[3], 'w') as o: | |
57 o.writelines(ret_lines) |