Monday, April 16, 2012

Looping over lines with Python

So I have a file that contains this:



SequenceName 4.6e-38 810..924
SequenceName_810..924 VAWNCRQNVFWAPLFQGPYTPARYYYAPEEPKHYQEMKQCFSQTYHGMSFCDGCQIGMCH
SequenceName 1.6e-38 887..992
SequenceName_887..992 PLFQGPYTPARYYYAPEEPKHYQEMKQCFSQTYHGMSFCDGCQIGMCH


I want my program to read only the lines that contain these protein sequences. Up until now I got this, which skips the first line and read the second one:



handle = open(filename, "r")
handle.readline()
linearr = handle.readline().split()
handle.close()

filenamealpha = filename + ".txt"
handle = open(filenamealpha, "w")
handle.write(">%s\n%s\n" % (linearr[0], linearr[1]))
handle.close()


But it only processes the first sequence and I need it to process every line that contains a sequence, so I need a loop, how can I do it?
The part that saves to a txt file is really important too so I need to find a way in which I can combine these two objectives.
My output with the above code is:



SequenceName_810..924
VAWNCRQNVFWAPLFQGPYTPARYYYAPEEPKHYQEMKQCFSQTYHGMSFCDGCQIGMCH




No comments:

Post a Comment