string - Python - Writing to file using seek -
i'm beginner python , playing around various ways simple task of reverse-complementing dna or rna sequence learn string functions etc. latest method works minor irritant can't find answer to, because there using don't understand properly. function designed write blank file (this works!) , open file containing sequence, loop through 1 character @ time writing reverse complement new file. here's code:
def func_rev_seq(in_path,out_path): """ read file 1 character @ time , retrun reverse complement of each nucleotide new file """ # write blank file (out_path) fb = open(out_path,"w") fb.write("") fb.close() # dictionary key nucleotide , value reverse complement base = {"a":"t", "c":"g", "g":"c", "t":"a", "a":"t", "c":"g", "g":"c", "t":"a", "k":"m", "m":"k", "y":"r", "r":"y", "b":"v", "v":"b", "d":"h", "h":"d", "k":"m", "m":"k", "y":"r", "r":"y", "b":"v", "v":"b", "d":"h", "h":"d", "u":"a", "u":"a"} # open source file (in_path) fi fi=open(in_path,"r") = fi.read(1) # loop through source file 1 character @ time , write reverse complement output file while != "": = fi.read(1) if in base: b = base[i] else: b = open(out_path, 'r+') fo: body = fo.read() fo.seek(0, 0) fo.write(b + body) fi.close() fo.close()
the problem when run function, string in output file firstly truncated single character , secondly below blank line don't want. screen shot of input , output file examples understand it, seek function (0, 0) ought refer start of file, may have misunderstood. appreciated, thanks!
when put i = fi.read(1)
, i
equaled first character in file, @ beginning of while
loop, assigned second character i
same statement, without doing first character. if want loop on every character in file without problem, it's better use for
loop. iterating character character in reverse bit challenging, works:
def nucleo_complement(ifilename, ofilename): """reads file 1 character @ time , returns reverse complement of each nucleotide.""" complements = {'a': 't', 't': 'a', 'c': 'g', 'g': 'c'} ifile = open(ifilename) ofile = open(ofilename, 'w') pos in range(ifile.seek(0, 2) + 1, 0, -1): ifile.seek(pos - 1) char = ifile.read(1) ofile.write(complements.get(char.upper(), char)) ifile.close() ofile.close()
seek
returns new file position, , seek(0, 2)
goes last character in file. whenever invoke read(1)
, position in file advances 1 character, had have pos
equal position of last character plus one, , end loop @ second character instead of first. each iteration, went character ifile.seek(pos - 1')
, read next (original) character. beginner, example might bit much, if have questions, please feel free ask. really, need think first 2 statements in for
loop, , fact had both files open simultaneously.
Comments
Post a Comment