以后遇到条件我怀疑这是一个重复的问题,但我已经搜索了一段时间,似乎没有正确的措辞找到这个问题的答案。对不起,如果它是提前重复!如何保持文件中的行数据,直到文件python
我试图从我正在逐行阅读的文件中打印下列信息。
基因1基因2基因0 *基因1基因2
*代码
我已经能够得到gene0,基因1,基因2被称为非编码RNA基因,但我有麻烦试图找出如何缓冲基因-1和基因-2,直到符合条件基因0(数据[2] = ncRNA)。
换句话说,我需要从先前的行中获得可变信息,但只有当满足当前行中的条件时才可以。我已经在下面的注释部分中想到了它,但似乎必须有更好的方法来做到这一点(这将是一个嵌套混乱)。我正在浏览的文件是一个gff文件。
我不知道如何为'先前的信息'做一个占位符,直到满足条件。
import sys
import re
gff3 = sys.argv[1]
f = open(gff3, 'r')
ncRNAgene= False
fgene_count=0
while True:
line = f.readline()
if not line.startswith('#'):
data = line.strip().split("\t")
### this is not important to the question, just me dealing with the file format
try:
#my mis-guided attempts to get at this issue
#if data[2] == gene:
#line0 = f.readline()
#data0 = line.strip().split("\t")
#if data0[2] == gene
### the relevant information is in the third column of the line
if data[2] == 'ncRNA':
ncRNAgene = True
print "ncRNA gene:", line
while fgene_count <= 1 and ncRNAgene:
line = f.readline()
data2 = line.strip().split("\t")
if data2[2] == 'gene':
fgene_count = fgene_count + 1
print "this is gene %s : %s" %(fgene_count, line)
if fgene_count > 1:
fgene_count = 0
ncRNAgene= False
else:
continue
except IndexError:
if line.startswith('>'):
break
if not line:
break
f.close()
这是我很感兴趣的样子文件的一部分:我在方括号内的东西,我很感兴趣,
211000022279165 FlyBase [外显子] 14 1118。 - 。父母= FBtr0300167; parent_type = ncRNA
211000022279165 FlyBase [基因] 14 1118。 - 。 ID = FBgn0259870;名称= Su(Ste):CR42439;全名= Su(Ste):CR42439;别名= CR42439; Ontology_term = SO:0000011,SO:0000087; Dbxref = FlyBase_Annotation_IDs:CR42439,EntrezGene:7354392,GenomeRNAi:7354392
211000022279165 FlyBase [ncRNA] 14 1118。 - 。 ID = FBtr0300167;名称=苏(STE):CR42439-RA;父= FBgn0259870; ALIAS = CR42439-RA; Dbxref = FlyBase_Annotation_IDs:CR42439-RA,REFSEQ:NR_026633; score_text =弱支持;得分= 0
这是您的实际缩进?因为刚才所引发的问题很多,你必须先解决。即使该行以'#'开始,这意味着您将重新使用前一行的“data”,您已经有了试图使用'data'的代码。你有一个'else',看起来它的目的是匹配'try'而不是'if',这并不意味着什么。等等。 – abarnert 2014-12-05 19:39:53
同时,从描述中不完全清楚你想要什么。你的描述中有哪些“gene-1”和“gene-2”? – abarnert 2014-12-05 19:40:37
感谢您的意见。注释部分不是我的实际缩进,我会看看我是否可以修复其余部分。对不起,目前尚不清楚基因-1和基因-2是什么。我基本上试图找到ncRNA(或gene0)周围区域的'基因'信息。 – wubbina 2014-12-05 19:55:59