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