Created
January 27, 2017 19:23
-
-
Save walterst/9147f9405cadf67a88471cc87b508333 to your computer and use it in GitHub Desktop.
Use to append a tab-delimited fasta string to a fasta file
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 add_taxa_to_fasta.py input_taxa_file input_fasta_file output_fasta | |
""" | |
from sys import argv | |
from cogent.parse.fasta import MinimalFastaParser | |
# taxa mapping file expected to be tab separated | |
input_taxa = open(argv[1], "U") | |
# labels in fasta (before first whitespace) need to be exact match to ids in taxa mapping file | |
input_fasta = open(argv[2], "U") | |
output_fasta = open(argv[3], "w") | |
ids_taxa = {} | |
for line in input_taxa: | |
if len(line.strip()) == 0: | |
continue | |
ids_taxa[line.split('\t')[0]] = line.split('\t')[1] | |
for label,seq in MinimalFastaParser(input_fasta): | |
curr_taxa = ids_taxa[label.split()[0]] | |
curr_label = "%s\t%s" % (label, curr_taxa) | |
output_fasta.write(">%s\n%s\n" % (curr_label, seq)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Tony! 🥇
EDIT: And so fast, too! 💨