-
Notifications
You must be signed in to change notification settings - Fork 0
/
tidy_otu_table.py
executable file
·51 lines (38 loc) · 1.31 KB
/
tidy_otu_table.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
#!/usr/bin/env python
import argparse as ap
import os
__author__ = 'Connor Morgan-Lang'
def get_sampleID(filename):
basename = filename.split(os.sep)[-1]
return '.'.join(basename.split('.')[:-1])
def getOptions():
parser = ap.ArgumentParser()
parser.add_argument("-t", "--otuTable",
help="seqs_otu.txt output from QIIME containing picked OTUs",
required=True)
parser.add_argument("-o", "--output",
help="The output otu table in 'tidy' format (i.e., one row for each OTU-sequence pair)",
required=False,
default=".tsv")
args = parser.parse_args()
if args.output == ".tsv":
args.output = get_sampleID(args.otuTable) + args.output
return args
def load_otuTable(otuTable):
otus = dict()
with open(otuTable) as table:
for line in table:
fields = line.strip().split("\t")
otus[fields[0]] = fields[1:]
return otus
def tidy_table(OTUs, output):
with open(output, 'w') as otus_out:
for otu in OTUs:
for seq in OTUs[otu]:
otus_out.write(otu + "\t" + seq + "\n")
return
def main():
args = getOptions()
OTUs = load_otuTable(args.otuTable)
tidy_table(OTUs, args.output)
main()