-
Notifications
You must be signed in to change notification settings - Fork 204
/
metaphlan2_stamp_to_biom.py
executable file
·78 lines (50 loc) · 2.15 KB
/
metaphlan2_stamp_to_biom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
from __future__ import division, print_function
__author__ = "Gavin Douglas"
__credits__ = ["Gavin Douglas"]
__license__ = "GPL"
__version__ = "0.1"
__maintainer__ = "Gavin Douglas"
__email__ = "[email protected]"
__status__ = "Development"
import argparse
parser = argparse.ArgumentParser(description="Convert Metaphlan2 taxonomic relative abundances in STAMP format to legacy (tsv) BIOM format",
epilog='''Usage example:
metaphlan2_stamp_to_biom.py -i metaphlan2_taxonomy.spf -o metaphlan2_taxonomy_tsv.biom
'''
,formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("-i","--input",help="Input STAMP file", required=True)
parser.add_argument("-o","--output",help="Output legacy TSV BIOM file", required=True)
def main():
# Each metaphlan2 taxa will be given an unique number as an id
taxa_num = 1
header_marker = 0
args = parser.parse_args()
outfile = open( args.output , "w" )
with open( args.input , "r" ) as infile:
for line in infile:
# remove newline character
line = line.rstrip()
# split by tab
line_split = line.split("\t")
# if this integer == 0 then that means it's the first line, i.e. the header
if ( header_marker == 0 ):
header_marker += 1
header = ["#taxa"] + line_split[8:] + ["taxonomy"]
# convert each element of header to a string
headerline = [str(e) for e in header]
col = "\t".join( headerline )
print( col , file=outfile )
# go to next iteration of for loop once finished with header
continue
tax = ";".join( line_split[0:8] )
row_list = [ taxa_num ] + line_split[8:] + [tax]
# convert each element of row_list to string
row = [str(e) for e in row_list]
line_out = "\t".join( row )
print( line_out , file=outfile)
# add 1 to the taxa id
taxa_num += 1
outfile.close()
if __name__ == "__main__":
main()