Skip to content

Instantly share code, notes, and snippets.

@tpoisot
Last active December 25, 2015 11:19
Show Gist options
  • Save tpoisot/6968121 to your computer and use it in GitHub Desktop.
Save tpoisot/6968121 to your computer and use it in GitHub Desktop.
python2 script to read an OTU file in mothur format, select a cutoff level, and write the file in qiime format Usage: ./transf.py mothurfile cutoff LICENSE: BSD 2
#! /usr/bin/python
# Timothee Poisot
# [email protected]
# released under the BSD 2 license
import sys
def onlyNonEmpty(table):
N_non_empty = int(table[1][2]) + 3
table = [l[:N_non_empty] for l in table]
return table
def revert(otu_file, cutoff):
output_string = ""
otu_lines = [l.split() for l in open(otu_file)]
otu_lines = onlyNonEmpty([otu_lines[i] for i in xrange(len(otu_lines)) if ((i==0) or (otu_lines[i][0] == cutoff))])
NROW = len(otu_lines)
NCOL = len(otu_lines[0])
## Pivot the table
for col in xrange(NCOL):
if (not (col in [0, 2])):
for row in xrange(NROW):
to_write = str(otu_lines[row][col])
if (col > 2) and (row == 0):
to_write = int(to_write[3:])
if (col==1) and (row == 0):
to_write = "#OTU ID"
output_string += str(to_write)
if row < (NROW-1):
output_string += "\t"
if col < NCOL:
output_string += "\n"
## Write the file
qiime_file = "qiime_"+cutoff+"_"+otu_file
of = open(qiime_file, "w")
of.write(output_string)
of.close()
del output_string
del otu_lines
print "["+otu_file+"] Cutoff "+cutoff+": "+str(NCOL-3)+" OTUs in "+str(NROW-1)+" samples\n"
if __name__ == "__main__":
revert(sys.argv[1], str(sys.argv[2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment