我有一个类似这样的文件:
AAACAACAGGGTACAAAGAGTCACGCTTATCCTGTTGATACT
TCTCAATGGGCAGTACATATCATCTCTNNNNNNNNNNNNNNNNNNNNNNN
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
NNNNNNNNNNNNNNNNNNNNNNNNNNNAAAACGTGTGCATGAACAAAAAA
CGTAGCAGATCGTGACTGGCTATTGTATTGTGTCAATTTCGCTTCGTCAC
TAAATCAACGGACATGTGTTGC
我需要把它分成“非N”序列,所以两个独立的文件如下:
^{pr2}$我现在拥有的是:
UMfile = open ("C:\Users\Manuel\Desktop\sequence.txt","r")
contignumber = 1
contigfile = open ("contig "+str(contignumber), "w")
DNA = UMfile.read()
DNAstring = str(DNA)
for s in DNAstring:
DNAstring.split("NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN",1)
contigfile.write(DNAstring)
contigfile.close()
contignumber = contignumber+1
contigfile = open ("contig "+str(contignumber), "w")
问题是我意识到在“Ns”之间有一个换行符,这就是为什么它没有拆分我的文件,但是我显示的“file”只是一个大得多的文件的一部分。因此,有时“Ns”看起来像“NNNNNN\n”,有时类似于“NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN”,但我的序列之间总有1000个Ns需要拆分。在
所以我的问题是:如果知道每行中有不同数量的n,如何告诉python每1000xNs将其拆分成不同的文件?在
非常感谢大家,我真的没有信息学背景,而且我的python技能充其量只是基本的。在
假设你可以一次读取整个文件
您可以简单地将每个N和\N替换为一个空格,然后拆分。在
这将返回一个字符串列表,并且'ACGT'序列也将与每一个新行分割。在
如果这不是您的目标,您希望在“ACGT”中保留\n并且不沿着它拆分,则可以执行以下操作:
^{pr2}$这只会在n序列的中间移除\n。在
要在1000 Ns之后拆分字符串,请执行以下操作:
只需在
'N'
上拆分字符串,然后删除所有空字符串,或者只包含新行。像这样:输出
^{pr2}$上面的代码片段还使用
.replace('\n', '')
删除序列中的新行。在以下是一些你可能会发现有用的程序。在
首先,一个行缓冲类。用文件名和行宽初始化它。然后你可以给它随机长度的字符串,它会自动保存到文本文件,一行一行,所有的行(可能除了最后一行)都有给定的长度。您可以在其他程序中使用这个类,使您的输出看起来整洁。在
将此文件另存为
linebuffer.py
到Python路径中的某个位置;最简单的方法是在保存Python程序的任何位置保存它,并在运行程序时将其作为当前目录。在linebuffer.py
这是一个程序,它可以按照你在问题中描述的形式随机生成DNA序列。它使用
linebuffer.py
来处理输出。我写这个是为了能正确地测试我的DNA序列拆分器。在随机
最后,我们有一个程序可以分割你的随机DNA序列。它再次使用
linebuffer.py
来处理输出。在DNA拆分器0.py
相关问题 更多 >
编程相关推荐