how to keep data from lines in file until condition met later in file python -


i suspect repeat question, have searched while , don't seem have wording right find answer question. sorry if repeat in advance!

i trying print following information file reading in line line.

gene-1 gene-2 gene 0* gene1 gene2

*referred ncrna gene in code

i have been able gene0, gene1, gene2, having trouble trying figure out how buffer gene-1 , gene-2 until condition gene 0 (data[2] = ncrna) met.

in other words, need have variable information previous lines, when condition in current line met. have thought in commented out section below, seems there must better way (it nesting mess). file looking through gff file.

i don't know how make placeholder 'previous information' until condition met.

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")         ### not important question, me dealing file format         try:             #my mis-guided attempts @ issue             #if data[2] == gene:             #line0 = f.readline()             #data0 = line.strip().split("\t")             #if data0[2] == gene           ### relevant information in third column of line             if data[2] == 'ncrna':                 ncrnagene = true                  print "ncrna gene:", line                  while fgene_count <= 1 , ncrnagene:                     line = f.readline()                     data2 = line.strip().split("\t")                     if data2[2] == 'gene':                         fgene_count = fgene_count + 1                          print "this 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() 

this part of file i'm interested in looks like: put brackets around stuff i'm interested in.

211000022279165 flybase [exon] 14 1118 . - . parent=fbtr0300167;parent_type=ncrna

211000022279165 flybase [gene] 14 1118 . - . id=fbgn0259870;name=su(ste):cr42439;fullname=su(ste):cr42439;alias=cr42439;ontology_term=so:0000011,so:0000087;dbxref=flybase_annotation_ids:cr42439,entrezgene:7354392,genomernai:7354392

211000022279165 flybase [ncrna] 14 1118 . - . id=fbtr0300167;name=su(ste):cr42439-ra;parent=fbgn0259870;alias=cr42439-ra;dbxref=flybase_annotation_ids:cr42439-ra,refseq:nr_026633;score_text=weakly supported;score=0

it's hard tell mean here, general idea problems pretty simple: store gene1 , gene2 in local variables update whenever find gene1 or gene2 line, use local variables when find gene0 line.

for example:

gene1, gene2 = none, none line in file:     if matches_gene1(line):         gene1 = parse_gene1(line)     elif matches_gene2(line):         gene2 = parse_gene2(line)     elif matches_gene0(line):         gene0 = parse_gene0(line)         do_stuff_with(gene0, gene1, gene2)         gene1, gene2 = none, none 

or, if there can multiple gene1 , gene2 lines before each gene0, use list of them:

gene1, gene2 = [], [] line in file:     if matches_gene1(line):         gene1.append(parse_gene1(line))     elif matches_gene2(line):         gene2.append(parse_gene2(line))     elif matches_gene0(line):         gene0 = parse_gene0(line)         do_stuff_with(gene0, gene1, gene2)         gene1, gene2 = [], [] 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -