| 1 |
import os, sys, DNA |
| 2 |
|
| 3 |
####################################################### |
| 4 |
# Begin class methods |
| 5 |
####################################################### |
| 6 |
|
| 7 |
# bpw, 20050312 - .seqStore is exactly a compressed fasta file. Stop |
| 8 |
# building it and assume the input is compressed. |
| 9 |
|
| 10 |
def createIndexedFasta( prefix, nickname): |
| 11 |
|
| 12 |
# This is a class method (as opposed to an object method). |
| 13 |
# This method creates an indexed FASTA file on disk. |
| 14 |
|
| 15 |
print >>sys.stderr, "Creating %s.idxStore" % (prefix) |
| 16 |
|
| 17 |
the_uid = None |
| 18 |
defline = None |
| 19 |
seqline = None |
| 20 |
linenumber = 0 |
| 21 |
cur_offset = 0 |
| 22 |
|
| 23 |
FASTA = file( prefix, "r") |
| 24 |
IDXSTORE = file( prefix + ".idxStore", "w") |
| 25 |
for line in FASTA: |
| 26 |
linenumber += 1 |
| 27 |
line = line.strip() |
| 28 |
if(line[0:1] == ">"): |
| 29 |
# Clear current data to make space for new data. |
| 30 |
if( the_uid != None ): |
| 31 |
# The first time thru the_uid is equal to None. |
| 32 |
assert(defline != None) |
| 33 |
assert(seqline != None) |
| 34 |
|
| 35 |
# If we are using a database, then this might be the place to register the data. |
| 36 |
# uid2defline[the_uid] = defline |
| 37 |
# uid2seqline[the_uid] = seqline |
| 38 |
|
| 39 |
def_offset = cur_offset |
| 40 |
def_length = len(defline) |
| 41 |
cur_offset += def_length + 1 |
| 42 |
# remember the UNIX newline inserted by print. |
| 43 |
seq_offset = cur_offset |
| 44 |
seq_length = len(seqline) |
| 45 |
cur_offset += seq_length + 1; |
| 46 |
# remember the UNIX newline inserted by print. |
| 47 |
|
| 48 |
print >>IDXSTORE, the_uid, def_length, seq_length, def_offset, seq_offset |
| 49 |
# end if |
| 50 |
|
| 51 |
# Now process the new data. |
| 52 |
defline = line |
| 53 |
the_uid = line.split()[0][1:] |
| 54 |
seqline = "" # Clear any accumulated sequence. |
| 55 |
else: |
| 56 |
seqline += line # Accumulate more DNA sequence |
| 57 |
# end if |
| 58 |
# end for |
| 59 |
|
| 60 |
if(the_uid != None): |
| 61 |
# Now make sure that accumulated data makes it to disk. |
| 62 |
|
| 63 |
# If we are using a database, then this might be the place to register the data. |
| 64 |
# uid2defline[the_uid] = defline |
| 65 |
# uid2seqline[the_uid] = seqline |
| 66 |
|
| 67 |
def_offset = cur_offset |
| 68 |
def_length = len(defline) |
| 69 |
cur_offset += def_length + 1; |
| 70 |
# remember the UNIX newline inserted by print. |
| 71 |
seq_offset = cur_offset |
| 72 |
seq_length = len(seqline) |
| 73 |
cur_offset += seq_length + 1; |
| 74 |
# remember the UNIX newline inserted by print. |
| 75 |
|
| 76 |
print >>IDXSTORE, the_uid, def_length, seq_length, def_offset, seq_offset |
| 77 |
# end if |
| 78 |
|
| 79 |
FASTA.close() |
| 80 |
IDXSTORE.close() |
| 81 |
# end if |
| 82 |
# end def |
| 83 |
|
| 84 |
####################################################### |
| 85 |
# End class methods |
| 86 |
####################################################### |
| 87 |
|
| 88 |
class IdxStore: |
| 89 |
__doc__ = "Class for fast access to multiFASTA files." |
| 90 |
|
| 91 |
####################################################### |
| 92 |
# Begin instance methods |
| 93 |
####################################################### |
| 94 |
|
| 95 |
def __init__(self,prefix,*optargs): |
| 96 |
__doc__ = "Create an instance of the class" |
| 97 |
if(optargs): |
| 98 |
self.nickname = optargs[0] |
| 99 |
else: |
| 100 |
self.nickname = None |
| 101 |
# end if |
| 102 |
self.uid2iid = {} # declare an empty mapping |
| 103 |
self.iid2uid = [] # empty |
| 104 |
self.iid2def_length = [] |
| 105 |
self.iid2seq_length = [] |
| 106 |
self.iid2def_offset = [] |
| 107 |
self.iid2seq_offset = [] |
| 108 |
|
| 109 |
filename = prefix + ".idxStore" |
| 110 |
idxstore = file(filename, "r"); |
| 111 |
|
| 112 |
the_iid=0; |
| 113 |
while 1: |
| 114 |
line = idxstore.readline(); |
| 115 |
if not line: break |
| 116 |
# sys.stderr.write("idxline %s\n" % line) |
| 117 |
cols = line.split() |
| 118 |
the_uid=cols[0]; |
| 119 |
def_length=int(cols[1]); seq_length=int(cols[2]); |
| 120 |
def_offset=eval(cols[3]); seq_offset=eval(cols[4]) |
| 121 |
if(self.nickname): |
| 122 |
self.uid2iid[self.nickname + ':' + str(the_iid)] = the_iid; # hashed |
| 123 |
self.uid2iid[the_uid] = the_iid; # hashed |
| 124 |
self.iid2uid.append(the_uid); # vector |
| 125 |
self.iid2def_length.append(def_length) # vector |
| 126 |
self.iid2seq_length.append(seq_length) |
| 127 |
self.iid2def_offset.append(def_offset) |
| 128 |
self.iid2seq_offset.append(seq_offset) |
| 129 |
the_iid += 1; |
| 130 |
|
| 131 |
filename = prefix |
| 132 |
self.seqstore = file(filename, "r"); |
| 133 |
return |
| 134 |
|
| 135 |
|
| 136 |
def getStringFromFasta(self, forward, scaf_uid, start, length ): |
| 137 |
try: |
| 138 |
scaf_iid = self.uid2iid[scaf_uid] |
| 139 |
except KeyError: |
| 140 |
scaf_iid = eval(scaf_uid) |
| 141 |
if(scaf_iid < 1000000000): |
| 142 |
# sys.stderr.write("Using scaf_uid as the index\n") |
| 143 |
pass |
| 144 |
else: |
| 145 |
sys.stderr.write("scaf_uid=<%s> is invalid.\n" % scaf_uid) |
| 146 |
return "" |
| 147 |
|
| 148 |
seq_length = self.iid2seq_length[scaf_iid]; |
| 149 |
seq_offset = self.iid2seq_offset[scaf_iid]; |
| 150 |
# print >>sys.stderr, "seq_length, seq_offset, start =", seq_length, seq_offset, start |
| 151 |
# print >>sys.stderr, "seek to offset =", seq_offset+start |
| 152 |
self.seqstore.seek(seq_offset+start, 0); # from the beginning of file |
| 153 |
substring = self.seqstore.read(length) |
| 154 |
|
| 155 |
if(not forward): |
| 156 |
# sys.stderr.write("Taking reversecomplement\n") |
| 157 |
try: |
| 158 |
substring = DNA.DNA(substring).reversecomplement() |
| 159 |
except KeyError: |
| 160 |
sys.stderr.write("KeyError in DNA.DNA.reversecomplement()\n") |
| 161 |
sys.stderr.write("The query %d %s %d %d\n" % (forward,scaf_uid,start,length)) |
| 162 |
sys.stderr.write("%s\n" % substring) |
| 163 |
#else: |
| 164 |
#sys.stderr.write("Leave as is\n") |
| 165 |
return substring |
| 166 |
# end class |
| 167 |
|
| 168 |
def convertIndexToUID ( x_prefix, y_prefix, inpfile, outname, assemblyId1, assemblyId2 ): |
| 169 |
outfile = myfile() |
| 170 |
|
| 171 |
DefLines = file(x_prefix, 'r') |
| 172 |
the_x_uid = {} # Declare an empty dictionary |
| 173 |
ii = 0 |
| 174 |
for line in DefLines: |
| 175 |
# A valid idxStore format |
| 176 |
(ga_uid, sln, cln, sst, cst) = line.split() |
| 177 |
the_x_uid[assemblyId1+":"+str(ii)] = ga_uid |
| 178 |
ii += 1 |
| 179 |
# end for |
| 180 |
DefLines.close() |
| 181 |
|
| 182 |
DefLines = file(y_prefix, 'r') |
| 183 |
the_y_uid = {} # Declare an empty dictionary |
| 184 |
ii = 0 |
| 185 |
for line in DefLine: |
| 186 |
# A valid idxStore format |
| 187 |
(ga_uid, sln, cln, sst, cst) = line.split() |
| 188 |
the_y_uid[assemblyId2+":"+str(ii)] = ga_uid |
| 189 |
ii += 1 |
| 190 |
# end for |
| 191 |
DefLines.close() |
| 192 |
|
| 193 |
inpfile.seek(0) |
| 194 |
for line in inpfile: |
| 195 |
if(line[0] == 'M'): |
| 196 |
FM = MatchRecord.MatchRecord(line) |
| 197 |
FM.x_scaf_uid = the_x_uid[FM.x_scaf_uid] |
| 198 |
FM.y_scaf_uid = the_y_uid[FM.y_scaf_uid] |
| 199 |
print >>outfile, FM |
| 200 |
# end if |
| 201 |
# end for |
| 202 |
outfile.finished() |
| 203 |
return outfile |
| 204 |
# end def |
| 205 |
|
| 206 |
#if __name__ == '__main__': |
| 207 |
# main(sys.argv[1],sys.argv[2]) |
| 208 |
#main() |