我创建了一个小程序来从fasta文件中提取选定的id +序列。感兴趣的ID是包含该基因的几个seq的文件名。这是程序:
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()
我有一个包含900个基因的fasta文件,我想提取并写入700个其他文件。程序运行正常,但只将一个基因及其序列写入新文件。我不明白为什么这个循环只运行一次。最初可以帮助我解决问题吗? 提前谢谢。
答案 0 :(得分:1)
这个问题很明显,但也许这就是你要找的东西:
results = []
for record in records:
recordID=record.id #.split('_')
if recordID == geneID:
results.append(record)
else:
continue
SeqIO.write(" ".join(text for text in results), output_handle, 'fasta')
return new_reference
如果这不是你想要的。请提供您需要的问题和解决方案的更多详细信息。
答案 1 :(得分:0)
我遇到的问题是缩进问题。如果你查看上面的代码,你可以看到主函数中的 for loop 中没有调用def record_extraction()
( def main()) 。我已经改变了缩进,现在它确实完美地工作了。
见上面的新脚本:
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,genelist):
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.split('_')
recordID=recordid[0]+'_'+recordid[1]
if recordID in genelist:
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')
#run the programme
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]
genelist=[]
if geneID not in genelist:
genelist.append(geneID)
new_reference=record_extraction(geneID,opts.reference,opts.output_directory,genelist)
print 'The new reference fasta file has been create'
main()