comparison write_amplicon_info_file.py @ 8:aea7008fe1f1 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:44:05 +0000
parents
children 3888bbe7a9ca
comparison
equal deleted inserted replaced
7:252dfb042563 8:aea7008fe1f1
1 #!/usr/bin/env python
2
3 import argparse
4 import re
5
6 AMPLICON_NAME_RE = r'.*_(?P<num>\d+)_[^0-9]*(?P<name>L(?:EFT)?|R(?:IGHT)?)'
7
8
9 def primer_info_to_position(name):
10 position = 0
11 re_match = re.match(AMPLICON_NAME_RE, name)
12 if re_match is None:
13 raise ValueError("{} does not match expected amplicon name format".format(name))
14 side = re_match.group('name')
15 num = re_match.group('num')
16 if side == 'RIGHT' or side == 'R':
17 position += 1000
18 if num is not None:
19 position += int(num)
20 return position
21
22
23 def write_amplicon_info_file(bed_file, amplicon_info_file):
24 amplicon_sets = {}
25 amplicon_ids = set()
26 for line in bed_file:
27 fields = line.strip().split('\t')
28 name = fields[3]
29 re_match = re.match(AMPLICON_NAME_RE, name)
30 if re_match is None:
31 raise ValueError("{} does not match expected amplicon name format".format(name))
32 amplicon_id = int(re_match.group('num'))
33 amplicon_set = amplicon_sets.get(amplicon_id, [])
34 amplicon_set.append(name)
35 amplicon_sets[amplicon_id] = amplicon_set
36 amplicon_ids.add(amplicon_id)
37
38 for id in sorted(list(amplicon_ids)):
39 amplicon_info = '\t'.join([name for name in sorted(amplicon_sets[id], key=primer_info_to_position)]) + '\n'
40 amplicon_info_file.write(amplicon_info)
41 amplicon_info_file.close()
42
43
44 if __name__ == '__main__':
45 parser = argparse.ArgumentParser(description='Write an amplicon info file for iVar from a BED file describing primer positions')
46 parser.add_argument('bed_file', type=argparse.FileType(), help='Primer BED file')
47 parser.add_argument('amplicon_info_file', type=argparse.FileType('w'), help='Output file: amplicon info file in TSV format')
48 args = parser.parse_args()
49
50 write_amplicon_info_file(args.bed_file, args.amplicon_info_file)