python - 如何在 python 中读取 fasta 文件?

标签 python fasta

我正在尝试读取一个 FASTA 文件,然后找到特定的 motif(string)并打印出它出现的顺序和次数。 FASTA file只是一系列以标题行开头的序列(字符串),标题或新序列开头的签名是“>”。在标题之后的新行中是字母序列。我还没有完成代码,但到目前为止我有这个并且它给了我这个错误:

AttributeError: 'str' object has no attribute 'next'

我不确定这里出了什么问题。

import re

header=""
counts=0
newline=""

f1=open('fpprotein_fasta(2).txt','r')
f2=open('motifs.xls','w')
for line in f1:
    if line.startswith('>'):
        header=line
        #print header
        nextline=line.next()
        for i in nextline:
            motif="ML[A-Z][A-Z][IV]R"
            if re.findall(motif,nextline):
                counts+=1
                #print (header+'\t'+counts+'\t'+motif+'\n')
        fout.write(header+'\t'+counts+'\t'+motif+'\n')

f1.close()
f2.close()

最佳答案

错误可能来自以下行:

nextline=line.next()

line是你已经读过的字符串,没有next()方法就可以了。

部分问题是您试图混合两种不同的文件读取方式 - 您正在使用 for line in f1 遍历行和 <handle>.next() .

此外,如果您正在使用 FASTA 文件,我建议使用 Biopython :它使处理序列集合变得更加容易。特别是,Chapter 14您将对主题特别感兴趣。这可能需要您了解更多有关 Python 的知识才能实现您的目标,但如果您要做的生物信息学工作比此处示例所显示的要多得多,那么绝对值得投入时间。

关于python - 如何在 python 中读取 fasta 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20580657/

相关文章:

regex - 找到一行末尾的任意字母,删除换行符而不替换目标

python - 使用数据框更改 fasta 文件中的 seq 名称

python - 使用 Biopython 更改 fasta 文件中的 DNA 序列

python - 根据属性名称打印类的属性

python - 属性错误 : StringVar instance has no attribute 'endswith' while trying to call from a Tkinter button

python - Django TemplateSyntaxError at/'staticfiles' 不是注册的标签库。必须是 : admin_list admin_modify admin_urls 之一

python - 错误 : 'module' object is not callable when using logmmse

python - 为什么类方法的 super 需要第二个参数?

linux - 选择一个fasta文件中大于300个aa和 "C"的序列至少出现4次

python - 在 for 循环中直接调用 SeqIO.parse() 可以,但是事先单独使用它不行吗?为什么?