How to correct this for loop over tuple function in Python? -
here program each line split pairs using tuples, such every alphabet had corresponding numeric a:6, b:6, c:35 ..etc if value less 10, alphabets converted n. following code. find code not loop on tuple function in last part of code. takes in single sequence , not loop on other
tutorial = open('c:/test/z.txt','r') ## input file looks >qrst abcde-- 6 6 35 25 10 >qqqq abbde-- 7 7 28 29 2 org = [] seqlist = [] seqstring = "" line in tutorial: if line.startswith(">"): if seqstring!= "": seqlist.append(seqstring) seqstring = "" org.append(line.rstrip("\n")) else: seqstring += line.rstrip("\n") seqlist.append(seqstring) l = seqlist #print l j = [] ll = len(seqlist) in range(0,ll): sq = l[i] sequence = sq.split(" ")[0] ## stores alphabets qualities = sq.split(" ")[1:] ## stores numeric qualities = filter(none, qualities) sub in sequence: if sub == "-": ## if sequences have "-", inserts "0" in position in corresponding number idx = list(sequence).index(sub) qualities.insert(idx,"0") # error in steps below pairs = [] sub in l: print sub new_list = [] x in range(len(sequence)): print x new_tuple = (sequence[x], qualities[x]) #printing step, notice 1 of sequences printed twice. error print new_tuple if int(qualities[x]) < 10: new_tuple = ("z", qualities[x]) new_list.append(new_tuple) pairs.append(new_list) print pairs # when print pairs looks this: [[('z', '7'), ('z', '7'), ('b', '28'), ('d', '29'), ('z', '2'), ('z', '0'), ('z', '0')], [('z', '7'), ('z', '7'), ('b', '28'), ('d', '29'), ('z', '2'), ('z', '0'), ('z', '0')]] # sequence#2 printed twice over. first 1 not taken in
all_inputs = [] # <---- add in range(0,ll): sq = l[i] sequence = sq.split(" ")[0] ## stores alphabets qualities = sq.split(" ")[1:] ## stores numeric qualities = filter(none, qualities) sub in sequence: if sub == "-": idx = list(sequence).index(sub) qualities.insert(idx,"0") # add *********************** all_inputs.append((sequence, qualities)) pairs = [] # change ******************************* sequence, qualities in all_inputs: print sub new_list = [] x in range(len(sequence)): print x new_tuple = (sequence[x], qualities[x]) print new_tuple if int(qualities[x]) < 10: new_tuple = ("z", qualities[x]) new_list.append(new_tuple) pairs.append(new_list) print pairs
gives:
[[('z', '6'), ('z', '6'), ('c', '35'), ('d', '25'), ('e', '10'), ('z', '0'), ('z', '0')], [('z', '7'), ('z', '7'), ('b ', '28'), ('d', '29'), ('z', '2'), ('z', '0'), ('z', '0')]]
Comments
Post a Comment