我有一个表格格式的大blast文件,目标序列的数量不受限制,所以解析需要很长时间。我希望将每个查询序列的命中数减少到前10个。 我的python是基本的,但是到目前为止我所拥有的
import sys
blastfile = open(sys.argv[1],"r")
column1list=[]
for line in blastfile:
b = line.split()[0]
column1list.append(b)
uniqcolumn1 = list(set(column1list))
counter = 0
for val in uniqcolumn1:
#print val
for line in blastfile:
#print line
while counter <= 10:
if line.startswith(val):
print line
counter =+ 1
下面是blast输出文件的一行示例,查询序列的标题在第一列中,在本例中为“c8208”g1_i2
^{pr2}$我认为代码的第一部分工作正常,直到' uniqcolumn1=list(set(column1list))',那么我无法让它打印以列表中每个字符串开头的前十行。在
此一次性版本按标题在文件中出现的顺序打印每个标题的前10个:
这里的问题似乎是您在文件对象中迭代了两次。在Python中,文件对象的工作方式非常类似于遍历每一行的指针。如果不向后移动指针,它就没有可读取的内容。在
您需要做的是使用
.seek
函数将该指针移回起始位置。例如,假设你有一个file_to_read.txt
和你的python_script.py
。在文件_阅读.txt
Python_脚本.py
^{pr2}$如果要运行这段代码(并且没有出现有关目录的错误),则只需打印一次
file_to_read.txt
。要解决这个问题,您只需在阅读之间添加一个f.seek(0, 0)
。例如:现在,回到您的上下文,您可以看到这是如何应用于您的代码的:
编辑
这是代码的后半部分,已修复。你也可以这样做:
这样做的好处是for循环提前终止,它不会读取整个文件。在
另一种方法是:
这样做的好处是看起来更简单。在
相关问题 更多 >
编程相关推荐