python - how to retrieve string from a large file -
i have written code in "ids.txt" tab deliminated text file contains id in manner given below in first column represent id second starting index , third column ending index.
ids.txt-------
"complete.txt"
the script have write given bellow retrieve string fragment according "ids.txt" it's not
working please changes should make correct code
with open("\users\zebrafish\desktop\ids.txt") f: # input text line in f: c = line.split("\t") i, x in enumerate(c): #passing values start , end variables if == 1: start = x elif == 2: end = x elif == 0: gene_name = x infile = open("/users/zebrafish/desktop/complete.txt") #file large string data seq in infile: seqnew = seq.split("\t") # data single line retrived = seqnew[int(start):int(end)] #retrieve fragment print retrived
i don't know why splitting on \t
in complete.txt
file, here code optimized:
ids = {} open('/users/zebrafish/desktop/ashish/ids.txt') f: line in f: if len(line.strip()): # makes sure skip blank lines id,start,end = line.split('\t') ids[id] = (int(start),int(end)) # here, assume `complete.txt` file 1 long line. open('/users/zebrafish/desktop/ashish/complete.txt') f: sequence = f.readline() # each id, fetch sequence "chunk: id,value in ids.iteritems(): start, end = value print('{} {}'.format(id,sequence[start-1:end]))
Comments
Post a Comment