在Python中提取序列

在Python中提取序列,python,string,split,sequence,bioinformatics,Python,String,Split,Sequence,Bioinformatics,我有一个文件如下所示: >sequence_name_16hj51 CAACCTTGGCCAT >sequence_name_158ghni52 AATTGGCCTTGGA >sequence_name_468rth AAGGTTCCA 我想获得以下信息: ['caacttggccat','AATTGGCCTTGGA','AAGGTTCCA'] 我有一个所有序列名的列表,标题为title\u finder。当我尝试使用: for i in range(0,len(title_

我有一个文件如下所示:

>sequence_name_16hj51
CAACCTTGGCCAT
>sequence_name_158ghni52
AATTGGCCTTGGA
>sequence_name_468rth
AAGGTTCCA
我想获得以下信息:
['caacttggccat','AATTGGCCTTGGA','AAGGTTCCA']

我有一个所有序列名的列表,标题为
title\u finder
。当我尝试使用:

for i in range(0,len(title_finder)):
    seq = seq.split(title_finder[i])
    print seq
我得到了这个回溯:

Traceback (most recent call last):
  File "D:/Desktop/Python/consensus new.py", line 23, in <module>
    seq = seq.split(title_finder[i])
AttributeError: 'list' object has no attribute 'split'
回溯(最近一次呼叫最后一次):
文件“D:/Desktop/Python/consensus new.py”,第23行,在
序号=序号拆分(标题查找者[i])
AttributeError:“list”对象没有属性“split”
有人能帮我吗


编辑:有时一些序列跨越多行,因此当我使用for循环时,我会得到多个字符串。

您试图拆分一个列表,该列表为您提供了
AttributeError
,而不是您可以读取文件行并检查该行是否以
开头,然后保留它

With open('file_nam') as f:
    my_patterns=[line.rstrip() for line in f in not line.startswith('>')]   
另外,如果您确定模式为奇数行,您可以使用
itertools.islice
对文件对象进行切片,这也是一种可选的Python方式:

from itertools import islice
With open('file_nam') as f:
     my_my_patterns=list(islice(f,1,None,2))

请注意,如果您只想循环您的模式,您不需要将
islice
的结果转换为list,您只需在迭代器上迭代即可。

您正在尝试拆分一个列表,该列表为您提供了
AttributeError
,相反,您可以读取文件行并检查该行是否以
开头,然后将其保留

With open('file_nam') as f:
    my_patterns=[line.rstrip() for line in f in not line.startswith('>')]   
另外,如果您确定模式为奇数行,您可以使用
itertools.islice
对文件对象进行切片,这也是一种可选的Python方式:

from itertools import islice
With open('file_nam') as f:
     my_my_patterns=list(islice(f,1,None,2))

注意,如果你只想循环你的模式,你不需要转换<代码> ISLICE的结果,你可以简单地迭代你的迭代器。

< P>如果你在做生物信息学,你应该真正考虑安装。 如果您想在纯Python中执行此操作,那么以下命令将起作用:

with open('your_file.fasta') as f:
    print [line.rstrip() for line in f if not line.startswith('>')]

如果你在做生物信息学,你应该考虑安装。< /P> 如果您想在纯Python中执行此操作,那么以下命令将起作用:

with open('your_file.fasta') as f:
    print [line.rstrip() for line in f if not line.startswith('>')]

假设您的文件是seq.in,那么您可以这样做来获取列表:

In [17]: with open ('seq.in','r') as f:
          extracted_list=[line[:-1] for line in f if line[0]!='>']

In [18]: extracted_list
Out[18]: ['CAACCTTGGCCAT', 'AATTGGCCTTGGA', 'AAGGTTCCA']

假设您的文件是seq.in,那么您可以这样做来获取列表:

In [17]: with open ('seq.in','r') as f:
          extracted_list=[line[:-1] for line in f if line[0]!='>']

In [18]: extracted_list
Out[18]: ['CAACCTTGGCCAT', 'AATTGGCCTTGGA', 'AAGGTTCCA']
打印(行)

['caacttggccat'、'AATTGGCCTTGGA'、'AAGGTTCCA']

打印(行)


['caacttggccat','AATTGGCCTTGGA','AAGGTTCCA']

您只能拆分一个字符串,并得到一个列表。循环会重复拆分,因此在第一次循环后失败。使用BioPython只能拆分一个字符串,然后会得到一个列表。您的循环会重复拆分,因此在第一次循环后失败。使用BioPython您需要在
之后添加
rstrip()
,因为现在序列的末尾包含换行符。
islice
也包括
\n
您需要在
之后添加
rstrip()
,因为现在序列的末尾包含了换行符。
islice
也包含了
\n
,我支持使用Biopython,它为您处理FASTA文件并做了大量的脏活。如果你真的需要一个字符串,你可以随时将它转换成字符串。我支持使用Biopython,它为你处理FASTA文件,并且做了很多脏活。如果您真的需要字符串,可以随时将其转换为字符串。