I have created a little programe to extract selected ids + sequence from a fasta file. The ids of interest are file names that contains several seq for that gene. This is the programe:
import glob, sys, os
from Bio import SeqIO, SearchIO
from Bio.SeqRecord import SeqRecord
import argparse
def help_function():
print """
usage: to_extract_seq_and_id.py [-h] [-i input_file:path to data] [-r reference file: path_to_file ]
[-o output_directory: path_to_store_new_file] """
parser = argparse.ArgumentParser()
parser.add_argument('-input_files', '-i',type=str,help='path_to_data')
parser.add_argument('-reference', '-r',type=str,help='path_to_the_fasta_reference_file')
parser.add_argument('-output_directory','-o', type=str, help='path_to_store_new_file')
opts = parser.parse_args()
#first function to check if the files exits.
def check_file_exists(filepath, file_description):
if not os.path.exists(filepath):
print("The " + file_description + " (" + filepath + ") does not exist")
sys.exit(1)
else:
print file_description + " detected"
def record_extraction(geneID,reference,output):
records=list(SeqIO.parse(opts.reference,'fasta'))
new_reference=output + '/new_reference_common_genes.fa'
output_handle=open(new_reference, 'a')
with open (opts.reference, 'rU') as input_handle:
for record in records:
recordID=record.id
if recordID == geneID:
SeqIO.write(record, output_handle, 'fasta')
else:
continue
return new_reference
def main():
if len(sys.argv) <=2:
parser.print_help()
sys.exit()
else:
check_file_exists(opts.input_files, 'input_files')
check_file_exists(opts.reference, 'reference_file')
check_file_exists(opts.output_directory, 'output_directory')
files=(glob.glob(opts.input_files + '/*.fa'))
for f in files:
database_files=glob.glob(f)[0]
file_name=os.path.basename(f)
gene_id=file_name.split('.')
gene_name=gene_id[0].split('_')
geneID=gene_name[1] + '_' + gene_name[2]
print 'The new reference fasta file has been create'
new_reference=record_extraction(geneID,opts.reference,opts.output_directory)
main()
I have a fasta file with 900 genes, and I want to extract and write in another file 700 of them. The programe runs OK but only write one gene and its sequence into the new file. I don't understand why that loop is running only once. Initially Can anyone help me to correct the problem, please? Thanks in advance.