Reputation: 569
I have written a code in which "IDS.txt" is a tab deliminated text file contains ID in the manner given below in which first column represent ID second starting index and third column ending index.
IDs.txt-------
"complete.txt"
the script which i have write given bellow to retrieve the string fragment according to "IDs.txt" it's NOT
working please help what changes should i make to correct the code
with open("\Users\Zebrafish\Desktop\IDs.txt") as f: # will get input from the text
for line in f:
c = line.split("\t")
for i, x in enumerate(c): #passing values to start and end variables
if i == 1:
start = x
elif i == 2:
end = x
elif i == 0:
gene_name = x
infile = open("/Users/Zebrafish/Desktop/complete.txt") #file to get large string data
for seq in infile:
seqnew = seq.split("\t") # get data as single line
retrived = seqnew[int(start):int(end)] #retrieve fragment
print retrived
Upvotes: 0
Views: 158
Reputation: 414325
3MB is not huge (on a computer that can run Windows). Just load the second file into memory as a single string, to get the fragments:
# populate `id -> (start, end)` map
ids = {}
with open(r"\Users\Zebrafish\Desktop\ASHISH\IDs.txt") as id_file:
for line in id_file:
if line.strip(): # non-blank line
id, start, end = line.split()
ids[id] = int(start), int(end)
# load the file as a single string (ignoring whitespace)
with open("/Users/Zebrafish/Desktop/ASHISH/complete.txt") as seq_file:
s = "".join(seq_file.read().split()) # or re.sub("\s+", "", seq_file.read())
# print fragments
for id, (start, end) in ids.items():
print("{id} -> {fragment}".format(id=id, fragment=s[start:end]))
If complete.txt
file doesn't fit in memory; you could use mmap
, to access its content as a sequence of bytes without loading the whole file into memory:
from mmap import ACCESS_READ, mmap
with open("complete.txt") as f, mmap(f.fileno(), 0, access=ACCESS_READ) as s:
# use `s` here (assume that indices refer to the raw file in this case)
# e.g., `fragment = s[start:end]`
Upvotes: 1
Reputation: 8021
Beware of the leading \t
in IDs.txt
>>> print "\ta\tb\tc"
a b c
>>> "\ta\tb\tc".split("\t")
['', 'a', 'b', 'c']
i == 0
refers to an empty text rather than the gene ID.
Upvotes: 0
Reputation: 53535
Remove the line:
seqnew = seq.split("\t")
Just do:
retrieved = seqnew[int(start):int(end)]
will get the sub-string you want.
Then you'll be able to:
print retrieved
Upvotes: 0
Reputation: 174622
I don't know why you are splitting on \t
in your complete.txt
file, here is your code optimized:
ids = {}
with open('/Users/Zebrafish/Desktop/ASHISH/IDs.txt') as f:
for line in f:
if len(line.strip()):
# This makes sure you skip blank lines
id,start,end = line.split('\t')
ids[id] = (int(start),int(end))
# Here, I assume your `complete.txt` is a file with one long line.
with open('/Users/Zebrafish/Desktop/ASHISH/complete.txt') as f:
sequence = f.readline()
# For each id, fetch the sequence "chunk:
for id,value in ids.iteritems():
start, end = value
print('{} {}'.format(id,sequence[start-1:end]))
Upvotes: 1