annotate write_amplicon_info_file.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
8
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
1 #!/usr/bin/env python
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
2
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
3 import argparse
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
4 import re
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
5
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
6
9
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
7 AMPLICON_PAT = re.compile(r'.*_(?P<num>\d+).*_(?P<name>L(?:EFT)?|R(?:IGHT)?)')
8
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
8
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
9
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
10 def write_amplicon_info_file(bed_file, amplicon_info_file):
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
11 amplicon_sets = {}
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
12 for line in bed_file:
10
0135f1d487a0 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 8ce6fd9aee543d9e62db33a9c95f79d8dc4e6dea
iuc
parents: 9
diff changeset
13 line = line.strip()
0135f1d487a0 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 8ce6fd9aee543d9e62db33a9c95f79d8dc4e6dea
iuc
parents: 9
diff changeset
14 if not line:
0135f1d487a0 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 8ce6fd9aee543d9e62db33a9c95f79d8dc4e6dea
iuc
parents: 9
diff changeset
15 continue
0135f1d487a0 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 8ce6fd9aee543d9e62db33a9c95f79d8dc4e6dea
iuc
parents: 9
diff changeset
16 fields = line.split('\t')
9
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
17 start = int(fields[1])
8
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
18 name = fields[3]
9
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
19 re_match = AMPLICON_PAT.match(name)
8
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
20 if re_match is None:
9
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
21 raise ValueError(
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
22 '{} does not match expected amplicon name format'.format(name)
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
23 )
8
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
24 amplicon_id = int(re_match.group('num'))
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
25 amplicon_set = amplicon_sets.get(amplicon_id, [])
9
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
26 amplicon_set.append((name, start))
8
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
27 amplicon_sets[amplicon_id] = amplicon_set
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
28
9
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
29 # write amplicons sorted by number with primers sorted by start position
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
30 for id in sorted(amplicon_sets):
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
31 amplicon_info = '\t'.join(
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
32 [name for name, start in sorted(
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
33 amplicon_sets[id], key=lambda x: x[1]
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
34 )]
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
35 ) + '\n'
8
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
36 amplicon_info_file.write(amplicon_info)
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
37 amplicon_info_file.close()
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
38
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
39
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
40 if __name__ == '__main__':
9
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
41 parser = argparse.ArgumentParser(
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
42 description='Write an amplicon info file for iVar '
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
43 'from a BED file describing primer positions'
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
44 )
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
45 parser.add_argument(
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
46 'bed_file', type=argparse.FileType(), help='Primer BED file'
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
47 )
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
48 parser.add_argument(
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
49 'amplicon_info_file', type=argparse.FileType('w'),
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
50 help='Output file: amplicon info file in TSV format'
47a27906d750 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit f09d0bee3e957564beccb1bdb3610de02f639ec7"
iuc
parents: 8
diff changeset
51 )
8
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
52 args = parser.parse_args()
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
53
b93845a04cc8 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 6dae6f97a45a61b1f10be4227d978584624c3b3d"
iuc
parents:
diff changeset
54 write_amplicon_info_file(args.bed_file, args.amplicon_info_file)