Skip to content

Instantly share code, notes, and snippets.

@altanner
Created November 23, 2022 10:21
Show Gist options
  • Save altanner/a8ae7d11f49ab95e879032f105135f98 to your computer and use it in GitHub Desktop.
Save altanner/a8ae7d11f49ab95e879032f105135f98 to your computer and use it in GitHub Desktop.
How to search through a fasta file and print out the sequence associated with a record name.
from Bio import SeqIO
def fasta_search(fastas, target):
"""
Search for a sequence motif in multiple FASTA records.
Args: fastas - SeqIO iterable
target - the sequence name to find (string)
Returns: Nothing.
"""
for record in fasta_batch:
if target == record.name:
print(record.seq)
# assign name of file to variable
filename = "five_seqs.fas"
# parse in my fasta file
fasta_batch = list(SeqIO.parse(filename, "fasta"))
# declare what I am looking for, as a string
seq_name = "seq3"
# call the function!
fasta_search(fasta_batch, seq_name)
@altanner
Copy link
Author

altanner commented Nov 23, 2022

an example input file would be

> seq1
ABC

> seq2
DEF

> seq3
HIJ

> seq4
KLM

> seq5
NOP

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