This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Looking for matching names in fasta file and genbank records

Is there a python way to look at genbank files in a different directory to see if the names of the files are listed on a fasta file?

biopython
find DIR1 DIR2 -type f -name "*.gb" | grep -F -f <(grep "^>" in.fa | cut -c 2-)

1 answer

In Python you can do this:

import fnmatch
import os
from Bio import SeqIO

filenames = [f for f in os.listdir("your_folder") if fnmatch.fnmatch(f, '*.gb')]

records = SeqIO.parse('records.fasta', 'fasta')
for rec in records:
    for title in filenames:
        if title in rec.description:
        print('Match for ' + str(title))
            print('In: ', rec.id)       

Alternatively for making filenames, use Python's glob module to make a list of full pathnames with matches in the specified folder combined with os.path.basename() to retain just the filename:

import glob
filenames = [os.path.basename(x) for x in glob.glob(os.path.join('your_folder','*.gb'))]

Log in to answer this question.