Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何检查给定文件是否为FASTA?_Python_User Input - Fatal编程技术网

Python 如何检查给定文件是否为FASTA?

Python 如何检查给定文件是否为FASTA?,python,user-input,Python,User Input,我正在设计一个需要在早期阶段输入.fasta文件的代码。现在,我正在使用以下函数验证输入: def file_validation(fasta): while True: try: file_name= str(raw_input(fasta)) except IOError: print("Please give the name of the fasta file that exists in the f

我正在设计一个需要在早期阶段输入.fasta文件的代码。现在,我正在使用以下函数验证输入:

def file_validation(fasta):
    while True:
        try:
            file_name= str(raw_input(fasta))
        except IOError:
            print("Please give the name of the fasta file that exists in the folder!")
            continue

        if not(file_name.endswith(".fasta")):
            print("Please give the name of the file with the .fasta extension!")
        else:
            break
    return file_name

现在,尽管这个函数工作正常,但仍然存在一些错误空间,用户可能会输入一个文件,该文件名以.fasta结尾,但其中可能包含一些非.fasta内容。我可以做些什么来防止这种情况发生,并让用户知道他/她的.fasta文件已损坏?

为什么不干脆像解析fasta一样解析文件,看看它是否损坏

使用,通过在非FASTA文件上返回空生成器,默认失败:

from Bio import SeqIO

my_file = "example.csv"  # Obviously not FASTA

def is_fasta(filename):
    with open(filename, "r") as handle:
        fasta = SeqIO.parse(handle, "fasta")
        return any(fasta)  # False when `fasta` is empty, i.e. wasn't a FASTA file

is_fasta(my_file)
# False

写一个fasta解析器或者在线查找一个现有的。我自己对fasta一无所知。然而,有一个答案可能对你有用。我会说,试着解析几百个字符,假设这是有意义的。如果解析成功,请接受该文件。@Kevin我尝试将Biopython SeqIO'SeqIO.parse(文件名'fasta'))作为条件树的一部分,以查看是否可以进行解析。遗憾的是,它不能警告用户该文件不是有效的.fasta文件。这就是你的意思吗?@BillBell很遗憾,我无法理解你链接的代码的某些部分。你认为我可以复制并粘贴到我的函数中,让它告诉用户他们的文件不是正确的.fasta格式吗?我知道你的问题已经得到了回答!我正试图找出如何将以“with open(filename)…”开头的代码部分集成到我的函数中,但我很难让它仍然工作。你认为我应该把它作为一个单独的函数,然后在我原来的验证函数中调用它吗?这已经是一个独立的函数了,所以你应该能够从验证函数中调用它。你能澄清一下你的意图吗?我只是想确保我的代码没有被破坏,并会继续提示用户提供有效的答案。而你的设置最终起了作用!非常感谢。@Aryamcarthy你能对我的评论吗