读取用户输入的.fasta文件并使用Biopython进行解析?

问题描述 投票:0回答:1

我正在尝试创建一个python脚本,用户可以在其中键入自己的FASTA文件,然后将使用Biopython对该文件进行解析。我正在努力使它起作用。到目前为止,我的脚本是:

#!/usr/bin/python3

file_name = input("Insert full file name including the fasta extension: ")
with open(file_name, "r") as inf:
  seq = inf.read()

from Bio.SeqIO.FastaIO import SimpleFastaParser
count = 0
total_len = 0
with open(inf) as in_file:
  for title, seq in SimpleFastaParser(in_file):
    count += 1
    total_len += len(seq)
print("%i records with total sequence length %i" % (count, total_len))

我希望提示用户输入文件及其扩展名,并且应该使用该文件与Biopython进行解析,以便输出输出。我也想将打印输出发送到日志文件。任何帮助将不胜感激。

该脚本的目的是获取一个fasta文件,解析并修剪底漆。我知道有一种简单的方法可以完全使用Biopython来执行此操作,但是按照说明,Biopython只能用于解析而不是修剪。任何对此的见识也将不胜感激。

python bioinformatics biopython fasta
1个回答
0
投票

首先,您有两个打开fasta文件的地方

将内容存储在seq中的位置

然后您尝试打开inf,但在此代码段中未将inf分配为变量。您可能需要进行一些检查,以确保使用了有效的文件路径另外,这是使用argparse的好情况:

#!/usr/bin/python3
import argparse
from Bio.SeqIO
import os
import sys

def main(infile):
    # check that the file exists
    if not os.path.is_file(infile):
        print("file not found")
        sys.exit()
    count = 0
    total_len = 0
    for seq_record in SeqIO.parse(open(infile), "fasta"):
        count += 1
        total_len += len(seq_record.seq)

    print("%i records with total sequence length %i" % (count, total_len))


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='some script to do something with fasta files',
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument('-in', '--infile', type=str, default=None, required=True,
                        help='The path and file name of the fasta file')

    args = parser.parse_args()
    infile = args.infile

    main(infile)

如果需要使用input

#!/usr/bin/python3
from Bio.SeqIO
import os
import sys

infile = input("Insert full file name including the fasta extension: ")
# remove any white space
infile = infile.strip()

# check that the file exists
if not os.path.is_file(infile):
    print("file not found")
    sys.exit()

count = 0
total_len = 0
for seq_record in SeqIO.parse(open(infile), "fasta"):
    count += 1
    total_len += len(seq_record.seq)

print("%i records with total sequence length %i" % (count, total_len))
© www.soinside.com 2019 - 2024. All rights reserved.