Created
June 26, 2013 19:28
-
-
Save eclarke/5870729 to your computer and use it in GitHub Desktop.
Taxonomy fixer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Usage: python taxonomy_fixer.py [FILE] | |
Converts an ITS taxonomy file to eliminate taxa marked as unidentified, | |
swaps [kpcofg]__unidentified;s__Fungi to k__Fungi, and eliminates species | |
taxa that are simply s__[genus]_sp. | |
Writes to stdout. | |
""" | |
# Erik Clarke <ecl@mail.med.upenn.edu> | |
import sys | |
try: | |
inf = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin | |
except: | |
print(__doc__) | |
sys.exit(1) | |
for line in inf: | |
if not line: | |
continue | |
else: | |
line = line.strip('\n') | |
otu, taxa = line.split('\t') | |
taxa = taxa.split(';') | |
sys.stdout.write(otu + '\t') | |
for i, taxon in enumerate(taxa): | |
if taxon == 'k__unidentified': | |
sys.stdout.write('k__Fungi') | |
break | |
elif 'unidentified' in taxon or 'uncultured' in taxon: | |
break | |
elif i == 6 and taxon.endswith('_sp.'): | |
break | |
else: | |
sys.stdout.write(';') if i > 0 else None | |
sys.stdout.write(taxon) | |
sys.stdout.write('\n') | |
inf.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment