forked from LangilleLab/microbiome_helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_picrust_metadata.py
executable file
·67 lines (50 loc) · 2.03 KB
/
add_picrust_metadata.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
#!/usr/bin/python
import sys
import argparse
parser = argparse.ArgumentParser(
description="Basic script to output function metadata "
"in same order as predicted table. "
"Metadata file should contain 1 function "
"per line.",
epilog="Usage example:\n"
"add_picrust_metadata.py -i "
"predicted.tab -m metadata.txt"
"-o metadata_out.txt",
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("-i", "--input", help="Predicted trait table.",
required=True, type=str)
parser.add_argument("-m", "--meta", help="File containing metadata.",
required=True, type=str)
parser.add_argument("-o", "--output", help="Output metadata line to concatenate.",
required=True, type=str)
args = parser.parse_args()
count_file = args.input
metadata_file = args.meta
new_file = args.output
out_fh=open(new_file,'w')
def load_metadata(metadata_file_name):
metadata={}
metadata_fh=open(metadata_file_name)
meta_name=metadata_fh.readline().strip().split('\t')[1]
for line in metadata_fh:
meta_fields=line.strip().split('\t')
if len(meta_fields)> 1:
metadata[meta_fields[0]]=meta_fields[1]
return metadata,meta_name
count_fh=open(count_file)
header=count_fh.readline()
fields=header.strip().split('\t')
fields=fields[1:-1]
metadata,meta_name=load_metadata(metadata_file)
meta_line=[meta_name]
for field in fields:
if field in metadata:
if metadata[field] =='':
meta_line.append('None')
else:
meta_line.append(metadata[field])
else:
print "Couldn't find metadata for "+field
meta_line.append('None')
meta_line_str = "\t".join(meta_line)+"\n"
out_fh.write(meta_line_str)