annotate prepare_amplicon_info.py @ 13:9537184d421f draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 3839802e0a790cb519cffdc2d8a6b92d73e4c9ef
author iuc
date Sat, 25 Mar 2023 12:54:22 +0000
parents 0135f1d487a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
51afc8a64951 "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
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
2
51afc8a64951 "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
51afc8a64951 "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
51afc8a64951 "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
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
6
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
7 import sys
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
8
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
9
51afc8a64951 "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
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
11 primer_starts = {}
51afc8a64951 "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:
51afc8a64951 "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:
10
0135f1d487a0 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 8ce6fd9aee543d9e62db33a9c95f79d8dc4e6dea
iuc
parents: 5
diff changeset
14 line = line.strip()
0135f1d487a0 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 8ce6fd9aee543d9e62db33a9c95f79d8dc4e6dea
iuc
parents: 5
diff changeset
15 if not line:
0135f1d487a0 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 8ce6fd9aee543d9e62db33a9c95f79d8dc4e6dea
iuc
parents: 5
diff changeset
16 continue
0135f1d487a0 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 8ce6fd9aee543d9e62db33a9c95f79d8dc4e6dea
iuc
parents: 5
diff changeset
17 f = line.split('\t')
5
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
18 try:
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
19 if f[5] == '+':
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
20 primer_starts[f[3]] = int(f[1])
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
21 elif f[5] == '-':
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
22 primer_starts[f[3]] = int(f[2]) - 1
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
23 else:
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
24 raise ValueError()
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
25 except (IndexError, ValueError):
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
26 sys.exit(
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
27 'Primer BED file needs to be TAB-separated with the '
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
28 'following columns: '
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
29 'chrom, chromStart, chromEnd, name, score, strand, '
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
30 'where "chromStart", "chromEnd" need to be integer values '
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
31 'and "strand" needs to be either "+" or "-".'
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
32 )
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
33
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
34 # parse amplicon info and record outer primer names
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
35 with open(sys.argv[2]) as i:
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
36 ret_lines = []
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
37 for line in i:
10
0135f1d487a0 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 8ce6fd9aee543d9e62db33a9c95f79d8dc4e6dea
iuc
parents: 5
diff changeset
38 line = line.strip()
0135f1d487a0 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 8ce6fd9aee543d9e62db33a9c95f79d8dc4e6dea
iuc
parents: 5
diff changeset
39 if not line:
0135f1d487a0 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 8ce6fd9aee543d9e62db33a9c95f79d8dc4e6dea
iuc
parents: 5
diff changeset
40 continue
5
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
41 first = last = None
10
0135f1d487a0 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 8ce6fd9aee543d9e62db33a9c95f79d8dc4e6dea
iuc
parents: 5
diff changeset
42 for pname in line.split('\t'):
5
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
43 try:
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
44 primer_start = primer_starts[pname]
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
45 except KeyError:
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
46 sys.exit(
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
47 'Amplicon info with primer name not found in '
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
48 f'primer BED file: "{pname}"'
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
49 )
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
50 if first is None or primer_start < primer_starts[first]:
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
51 first = pname
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
52 if last is None or primer_start > primer_starts[last]:
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
53 last = pname
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
54 if first == last:
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
55 sys.exit(
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
56 line
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
57 + 'is not a proper amplicon info line.'
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
58 )
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
59 ret_lines.append(f'{first}\t{last}\n')
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
60
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
61 # write amended amplicon info
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
62 with open(sys.argv[3], 'w') as o:
51afc8a64951 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a5ff06c631a2a5a0d5d44edd6cb58a599d50918b"
iuc
parents:
diff changeset
63 o.writelines(ret_lines)