Skip to content

Instantly share code, notes, and snippets.

@walterst
Created January 27, 2017 19:23
Show Gist options
  • Save walterst/9147f9405cadf67a88471cc87b508333 to your computer and use it in GitHub Desktop.
Save walterst/9147f9405cadf67a88471cc87b508333 to your computer and use it in GitHub Desktop.
Use to append a tab-delimited fasta string to a fasta file
#!/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))
@colinbrislawn
Copy link

colinbrislawn commented Jan 27, 2017

Thanks Tony! 🥇
EDIT: And so fast, too! 💨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment