annotate formatoutput.py @ 1:b89b0765695d draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
author iuc
date Mon, 17 May 2021 20:10:24 +0000
parents
children a92a632c4d9b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
1 #!/usr/bin/env python
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
2 # -*- coding: utf-8 -*-
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
3
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
4 import argparse
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
5 import re
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
6 from pathlib import Path
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
7
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
8 taxo_level = {
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
9 'k': 'kingdom',
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
10 'p': 'phylum',
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
11 'c': 'class',
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
12 'o': 'order',
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
13 'f': 'family',
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
14 'g': 'genus',
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
15 's': 'species',
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
16 't': 'strains'}
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
17
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
18
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
19 def split_levels(metaphlan_output_fp, out_dp, legacy_output):
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
20 '''
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
21 Split default MetaPhlAn into a report for each taxonomic level
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
22
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
23 :param metaphlan_output_fp: Path default MetaPhlAn output
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
24 :param out_dp: Path to output directory
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
25 :param legacy_output: Boolean for legacy output
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
26 '''
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
27 # prepare output files
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
28 abund_f = {
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
29 'k': open(out_dp / Path('kingdom'), 'w'),
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
30 'p': open(out_dp / Path('phylum'), 'w'),
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
31 'c': open(out_dp / Path('class'), 'w'),
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
32 'o': open(out_dp / Path('order'), 'w'),
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
33 'f': open(out_dp / Path('family'), 'w'),
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
34 'g': open(out_dp / Path('genus'), 'w'),
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
35 's': open(out_dp / Path('species'), 'w'),
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
36 't': open(out_dp / Path('strains'), 'w')
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
37 }
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
38 for level in abund_f:
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
39 abund_f[level].write("%s\t" % taxo_level[level])
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
40 if not legacy_output:
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
41 abund_f[level].write("%s_id\t" % taxo_level[level])
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
42 abund_f[level].write("abundance\n")
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
43
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
44 levels_number = len(taxo_level)
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
45
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
46 with open(metaphlan_output_fp, 'r') as metaphlan_output_f:
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
47 with open(out_dp / Path('all'), 'w') as all_level_f:
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
48 # write header in all leve file
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
49 for level in ['k', 'p', 'c', 'o', 'f', 'g', 's', 't']:
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
50 all_level_f.write("%s\t" % taxo_level[level])
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
51 if not legacy_output:
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
52 all_level_f.write("%s_id\t" % taxo_level[level])
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
53 all_level_f.write("abundance\n")
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
54
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
55 # parse metaphlan file
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
56 for line in metaphlan_output_f.readlines():
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
57 # skip headers
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
58 if line.startswith("#"):
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
59 continue
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
60
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
61 # spit lines
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
62 split_line = line[:-1].split('\t')
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
63 taxo_n = split_line[0].split('|')
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
64 if legacy_output:
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
65 abundance = split_line[1]
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
66 else:
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
67 taxo_id = split_line[1].split('|')
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
68 abundance = split_line[2]
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
69
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
70 # get taxon name and ids
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
71 for i in range(len(taxo_n)):
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
72 taxo = taxo_n[i].split('__')[1]
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
73 taxo = taxo.replace("_", " ")
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
74 all_level_f.write("%s\t" % taxo)
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
75 if not legacy_output:
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
76 all_level_f.write("%s\t" % taxo_id[i])
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
77
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
78 # if not all taxon levels
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
79 for i in range(len(taxo_n), levels_number):
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
80 all_level_f.write('\t')
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
81
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
82 all_level_f.write("%s\n" % abundance)
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
83
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
84 # write
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
85 last_taxo_level = taxo_n[-1].split('__')
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
86 taxo = last_taxo_level[1].replace("_", " ")
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
87 level = last_taxo_level[0]
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
88 abund_f[level].write("%s\t" % taxo)
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
89 if not legacy_output:
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
90 abund_f[level].write("%s\t" % taxo_id[-1])
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
91 abund_f[level].write("%s\n" % abundance)
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
92
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
93 # close files
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
94 for taxo_level_f in abund_f:
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
95 abund_f[taxo_level_f].close()
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
96
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
97
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
98 def format_for_krona(metaphlan_output_fp, krona_out_fp):
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
99 '''
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
100 Split default MetaPhlAn into a report for each taxonomic levKRONAel
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
101
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
102 :param metaphlan_output_fp: Path default MetaPhlAn output
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
103 :param krona_out: Path to output file for Krona
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
104 '''
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
105 re_replace = re.compile(r"\w__")
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
106 re_bar = re.compile(r"\|")
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
107 re_underscore = re.compile(r"_")
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
108
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
109 with open(metaphlan_output_fp, 'r') as metaphlan_output_f:
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
110 with open(krona_out_fp, 'w') as krona_out_f:
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
111 for line in metaphlan_output_f.readlines():
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
112 if "s__" in line:
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
113 x = line.rstrip().split('\t')
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
114 lineage = re.sub(re_bar, '', x[0])
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
115 lineage = re.sub(re_replace, '\t', lineage)
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
116 lineage = re.sub(re_underscore, ' ', lineage)
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
117 krona_out_f.write("%s\t%s\n" % (x[-1], lineage))
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
118
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
119
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
120 if __name__ == '__main__':
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
121 parser = argparse.ArgumentParser(description='Format MetaPhlAn output')
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
122 subparsers = parser.add_subparsers(dest='function')
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
123 # split_levels
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
124 split_levels_parser = subparsers.add_parser('split_levels', help='Split default MetaPhlAn into a report for each taxonomic level')
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
125 split_levels_parser.add_argument('--metaphlan_output', help="Path to default MetaPhlAn output")
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
126 split_levels_parser.add_argument('--outdir', help="Path to output directory")
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
127 split_levels_parser.add_argument('--legacy-output', dest='legacy_output', action='store_true', help="Old MetaPhlAn2 two columns output")
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
128 split_levels_parser.set_defaults(legacy_output=False)
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
129 # format_for_krona
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
130 format_for_krona_parser = subparsers.add_parser('format_for_krona', help='Split default MetaPhlAn into a report for each taxonomic level')
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
131 format_for_krona_parser.add_argument('--metaphlan_output', help="Path to default MetaPhlAn output")
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
132 format_for_krona_parser.add_argument('--krona_output', help="Path to Krona output directory")
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
133
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
134 args = parser.parse_args()
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
135
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
136 if args.function == 'split_levels':
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
137 split_levels(
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
138 Path(args.metaphlan_output),
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
139 Path(args.outdir),
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
140 args.legacy_output)
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
141 elif args.function == 'format_for_krona':
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
142 format_for_krona(
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
143 Path(args.metaphlan_output),
b89b0765695d "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/metaphlan/ commit 2b87bc7417360e2b2c9ec0605d475909f6f0482f"
iuc
parents:
diff changeset
144 Path(args.krona_output))