comparison customizemapping.py @ 0:506d4a349aa1 draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/humann commit 077b8f34e081e6c427acb0fde0fbb97d1b241e0b"
author iuc
date Wed, 12 May 2021 09:04:36 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:506d4a349aa1
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import argparse
5 from pathlib import Path
6
7
8 if __name__ == '__main__':
9 # Read command line
10 parser = argparse.ArgumentParser(description='Customize HUMAnN utility mapping')
11 parser.add_argument('--in_mapping', help="Path to mapping file to reduce")
12 parser.add_argument('--features', help="Path to tabular file with features to keep in first column")
13 parser.add_argument('--elements', help="Path to tabular file with elements to keep in other columns")
14 parser.add_argument('--out_mapping', help="Path to reduced mapping file")
15 args = parser.parse_args()
16
17 in_mapping_fp = Path(args.in_mapping)
18 feature_fp = Path(args.features)
19 element_fp = Path(args.elements)
20 out_mapping_fp = Path(args.out_mapping)
21
22 # extract features to keep
23 features = set()
24 with open(feature_fp, 'r') as feature_f:
25 for line in feature_f.readlines():
26 features.add(line.split("\t")[0])
27 print(features)
28
29 # extract elements to keep
30 elements = set()
31 with open(element_fp, 'r') as element_f:
32 for line in element_f.readlines():
33 elements.add(line.split("\t")[0])
34 print(elements)
35
36 # write mapping for features to keep while keeping only elements
37 with open(in_mapping_fp, 'r') as in_mapping_f:
38 with open(out_mapping_fp, 'w') as out_mapping_f:
39 for line in in_mapping_f.readlines():
40 l_split = line.split("\t")
41 feat = l_split[0]
42 if feat in features:
43 to_write = [feat]
44 for e in l_split[1:]:
45 if e in elements:
46 to_write.append(e)
47 out_mapping_f.write("%s\n" % '\t'.join(to_write))