Python 将文本拆分为短语并枚举它们
我有这样的顺序: >my_sequence atccagcaaaaacgctccaaggattctcgactggactcattacttaatcagtattcgcaagcggacgccgaggtcgtaaaggctgaaaccgcacaatcggatgcgcccagtgatgacgcactxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcgccttgcccacccaccgacaaccggtgagtgaaaaattggaacggtgattaaaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxttgtgctttatttctggagggcggtgtttaggggtaggcgcgccatgttttttgccttcagcgatcccaggtacaaccagtccccatattcgcgcactgtcgtgatcggcgagtaattacctgtgctcgcatcttgcaggttggcaatcaccttgccgtccaagtccagacccagtgcaaaggcacgcttttccatgggtttgggcagtaccgtcaatgcccgaacaatcattttgc >my_序列 ATCCAGCCAAACGCTCCAAGGCAAGGATCTCGACTGGACTCATACTTAATCAGCAAGCCGAGGCGTAAAGGCGAATCGGAATCGGATCCACTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX TTGTGCTTTTAGGGGCGGTGTTAGGGCGCCATGGTTTTGCCTGCTCAGGCGAGCAGGTACACAGTCCCATTCGCGCGCGCGCATGTCGTCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCATATGCGCGCGCGCATGCGCGCGCGCATGCGCATGCGCGCATGCGCGCGCATGCGCGCATGCGCGCGCGCATGCGCGCATGCGCGCGCGCGCGCATGCATGCGCGCGCATGCGCGCGCGCGC 我想拆分这个长序列,消除“xxxxx”,并创建如下分离序列: >1 atccagcaaaaacgctccaaggattctcgactggactcattacttaatcagtattcgcaagcggacgccgaggtcgtaaaggctgaaaccgcacaatcggatgcgcccagtgatgacgcact >2 cgccttgcccacccaccgacaaccggtgagtgaaaaattggaacggtgattaaa >3 ttgtgctttatttctggagggcggtgtttaggggtaggcgcgccatgttttttgccttcagcgatcccaggtacaaccagtccccatattcgcgcactgtcgtgatcggcgagtaattacctgtgctcgcatcttgcaggttggcaatcaccttgccgtccaagtccagacccagtgcaaaggcacgcttttccatgggtttgggcagtaccgtcaatgcccgaacaatcattttgc >1 ATCCAGCAAAACGCTCCAAGATCTCGACTGGACTCATACTTAATCAGTCCAGGCGCAGGGTCGTAAAGGCGAACCACACATCGATGCGCCCAGTGATCGCACT >2 CGCCTTGCCCACCCACACACCGGTGAGGAAAATTGGAAATTGGAACGGTGATTAAA >3 TTGTGCTTTTTTCTGGGGGCGGTGTTAGGGCGCCATGTTTTTGCCTTCAGCCAGGTACAGACCAGTCACATTCGCGCACTGTCGTGTCGGCGAGTAATATACCTGTGCTCGCGCTGCAGGTGCAGGCAATCATCCAGAGCCAGCAGCACAGGCTTCCAGTCATCCAGGTGCAGATCCAGATCCAGTTGCAGTCAGTCAGTCAGTCAGGCGCAGATATACCATCCAGATACCATATATCCTGCTCGCTGCAGATGCAGAGATCCAGATCCAGATTCAGATTCAGATTCAGATTCAGATGCAGATGCATGCATGC 有人有开始的想法吗Python 将文本拆分为短语并枚举它们,python,sequencing,Python,Sequencing,我有这样的顺序: >my_sequence atccagcaaaaacgctccaaggattctcgactggactcattacttaatcagtattcgcaagcggacgccgaggtcgtaaaggctgaaaccgcacaatcggatgcgcccagtgatgacgcactxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcgccttgcccacccaccgacaaccggtgagtgaa
谢谢。一种简单的方法是首先对每个“x”字符进行拆分,然后过滤掉空结果:
sequences = filter(None, my_sequence.split("x"))
这里,filter的None
参数意味着只保留真实值–空字符串被视为false
,因此它们将从结果中删除
注意:在Python 3中,filter
返回一个迭代器,因此如果需要列表,请使用:
sequences = list(filter(None, my_sequence.split("x")))
例如:
In [5]: filter(str, my_sequence.split("x"))
Out[5]:
['atccagcaaaaacgctccaaggattctcgactggactcattacttaatcagtattcgcaagcggacgccgaggtcgtaaaggctgaaaccgcacaatcggatgcgcccagtgatgacgcact',
'cgccttgcccacccaccgacaaccggtgagtgaaaaattggaacggtgattaaa',
'ttgtgctttatttctggagggcggtgtttaggggtaggcgcgccatgttttttgccttcagcgatcccaggtacaaccagtccccatattcgcgcactgtcgtgatcggcgagtaattacctgtgctcgcatcttgcaggttggcaatcaccttgccgtccaagtccagacccagtgcaaaggcacgcttttccatgggtttgggcagtaccgtcaatgcccgaacaatcattttgc']
In [6]: import re
In [7]: p = re.compile(r'x+')
In [8]: p.split(my_sequence)
Out[8]:
['atccagcaaaaacgctccaaggattctcgactggactcattacttaatcagtattcgcaagcggacgccgaggtcgtaaaggctgaaaccgcacaatcggatgcgcccagtgatgacgcact',
'cgccttgcccacccaccgacaaccggtgagtgaaaaattggaacggtgattaaa',
'ttgtgctttatttctggagggcggtgtttaggggtaggcgcgccatgttttttgccttcagcgatcccaggtacaaccagtccccatattcgcgcactgtcgtgatcggcgagtaattacctgtgctcgcatcttgcaggttggcaatcaccttgccgtccaagtccagacccagtgcaaaggcacgcttttccatgggtttgggcagtaccgtcaatgcccgaacaatcattttgc']
另一个解决方案是使用正则表达式。如果序列之间的“x”字符数量可变,则可以在
x+
模式上拆分,该模式匹配一行中的一个或多个x
例如:
In [5]: filter(str, my_sequence.split("x"))
Out[5]:
['atccagcaaaaacgctccaaggattctcgactggactcattacttaatcagtattcgcaagcggacgccgaggtcgtaaaggctgaaaccgcacaatcggatgcgcccagtgatgacgcact',
'cgccttgcccacccaccgacaaccggtgagtgaaaaattggaacggtgattaaa',
'ttgtgctttatttctggagggcggtgtttaggggtaggcgcgccatgttttttgccttcagcgatcccaggtacaaccagtccccatattcgcgcactgtcgtgatcggcgagtaattacctgtgctcgcatcttgcaggttggcaatcaccttgccgtccaagtccagacccagtgcaaaggcacgcttttccatgggtttgggcagtaccgtcaatgcccgaacaatcattttgc']
In [6]: import re
In [7]: p = re.compile(r'x+')
In [8]: p.split(my_sequence)
Out[8]:
['atccagcaaaaacgctccaaggattctcgactggactcattacttaatcagtattcgcaagcggacgccgaggtcgtaaaggctgaaaccgcacaatcggatgcgcccagtgatgacgcact',
'cgccttgcccacccaccgacaaccggtgagtgaaaaattggaacggtgattaaa',
'ttgtgctttatttctggagggcggtgtttaggggtaggcgcgccatgttttttgccttcagcgatcccaggtacaaccagtccccatattcgcgcactgtcgtgatcggcgagtaattacctgtgctcgcatcttgcaggttggcaatcaccttgccgtccaagtccagacccagtgcaaaggcacgcttttccatgggtttgggcagtaccgtcaatgcccgaacaatcattttgc']
如果希望每个x-s序列都有一个条目,可以使用正则表达式:
import re
x = 'atccagcaaaaacgctccaaggattctcgactggactcattacttaatcagtattcgcaagcggacgccgaggtcgtaaaggctgaaaccgcacaatcggatgcgcccagtgatgacgcactxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcgccttgcccacccaccgacaaccggtgagtgaaaaattggaacggtgattaaaxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxttgtgctttatttctggagggcggtgtttaggggtaggcgcgccatgttttttgccttcagcgatcccaggtacaaccagtccccatattcgcgcactgtcgtgatcggcgagtaattacctgtgctcgcatcttgcaggttggcaatcaccttgccgtccaagtcc
agacccagtgcaaaggcacgcttttccatgggtttgggcagtaccgtcaatgcccgaacaatcattttgc'
re.split(r'x+', x)
>['atccagcaaaaacgctccaaggattctcgactggactcattacttaatcagtattcgcaagcggacgccgaggtcgtaaaggctgaaaccgcacaatcggatgcgcccagtgatgacgcact',
'cgccttgcccacccaccgacaaccggtgagtgaaaaattggaacggtgattaaa',
'ttgtgctttatttctggagggcggtgtttaggggtaggcgcgccatgttttttgccttcagcgatcccaggtacaaccagtccccatattcgcgcactgtcgtgatcggcgagtaattacctgtgctcgcatcttgcaggttggcaatcaccttgccgtccaagtccagacccagtgcaaaggcacgcttttccatgggtttgggcagtaccgtcaatgcccgaacaatcattttgc']
这里的
r'x+'
意味着我将在一个或多个x-s序列上拆分原始字符串。获得所需输出的另一种方法是使用列表理解
这是一个例子:
# Or maybe a generator
# data = (k for k in my_sequence.split("x") if k)
data = [k for k in my_sequence.split("x") if k]
for k,v in enumerate(data):
print("{0} >>> {1}".format(k,v))
输出:
0 >>> atccagcaaaaacgctccaaggattctcgactggactcattacttaatcagtattcgcaagcggacgccgaggtcgtaaaggctgaaaccgcacaatcggatgcgcccagtgatgacgcact
1 >>> cgccttgcccacccaccgacaaccggtgagtgaaaaattggaacggtgattaaa
2 >>> ttgtgctttatttctggagggcggtgtttaggggtaggcgcgccatgttttttgccttcagcgatcccaggtacaaccagtccccatattcgcgcactgtcgtgatcggcgagtaattacctgtgctcgcatcttgcaggttggcaatcaccttgccgtccaagtccagacccagtgcaaaggcacgcttttccatgggtttgggcagtaccgtcaatgcccgaacaatcattttgc
你能为Python3用户将其转换为list
吗?第一步:biopython
用于文件。第二步:使用split
函数或regex
第三步,使用biopython
保存和格式化output@JoseRicardoBustosM. 把所有这些步骤都放在一个答案里怎么样?@Ev.Kounis问题是有人有什么想法开始吗?
。。。。这不像是结束