| 1 |
import sys |
| 2 |
import K3000_common as kc |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
zero_d_key = lambda x: x.split('_')[0]+'_0' |
| 26 |
|
| 27 |
def unique(s): |
| 28 |
"""Return a list of the elements in s, but without duplicates. |
| 29 |
|
| 30 |
For example, unique([1,2,3,1,2,3]) is some permutation of [1,2,3], |
| 31 |
unique("abcabc") some permutation of ["a", "b", "c"], and |
| 32 |
unique(([1, 2], [2, 3], [1, 2])) some permutation of |
| 33 |
[[2, 3], [1, 2]]. |
| 34 |
|
| 35 |
For best speed, all sequence elements should be hashable. Then |
| 36 |
unique() will usually work in linear time. |
| 37 |
|
| 38 |
If not possible, the sequence elements should enjoy a total |
| 39 |
ordering, and if list(s).sort() doesn't raise TypeError it's |
| 40 |
assumed that they do enjoy a total ordering. Then unique() will |
| 41 |
usually work in O(N*log2(N)) time. |
| 42 |
|
| 43 |
If that's not possible either, the sequence elements must support |
| 44 |
equality-testing. Then unique() will usually work in quadratic |
| 45 |
time. |
| 46 |
""" |
| 47 |
|
| 48 |
n = len(s) |
| 49 |
if n == 0: |
| 50 |
return [] |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
try: |
| 74 |
t = list(s) |
| 75 |
|
| 76 |
|
| 77 |
t.sort(key=kc.allele_values) |
| 78 |
|
| 79 |
|
| 80 |
except TypeError: |
| 81 |
|
| 82 |
del t |
| 83 |
else: |
| 84 |
|
| 85 |
last = t[0] |
| 86 |
lasti = i = 1 |
| 87 |
while i < n: |
| 88 |
if not kc.d_list_equal(t[i],last): |
| 89 |
|
| 90 |
t[lasti] = last = t[i] |
| 91 |
lasti += 1 |
| 92 |
i += 1 |
| 93 |
|
| 94 |
return t[:lasti] |
| 95 |
|
| 96 |
assert(0==1) |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
return u |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
def compare (tuple1,tuple2): |
| 112 |
''' |
| 113 |
if tuple1 starts with tuple2: return 0 |
| 114 |
if tuple1<tuple2: return -1 |
| 115 |
if tuple1>tuple2: return 1 |
| 116 |
''' |
| 117 |
|
| 118 |
if len(tuple1) > 0 and tuple1[-1][0] == 'i' : |
| 119 |
tuple1=tuple1[:-1] |
| 120 |
if len(tuple2) > 0 and tuple2[-1][0] == 'i' : |
| 121 |
tuple2=tuple2[:-1] |
| 122 |
tmp_tuple1=tuple1[0:len(tuple2)] |
| 123 |
return kc.d_list_order(tmp_tuple1,tuple2) |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
class sorted_list(object): |
| 129 |
"""Class sorted list |
| 130 |
Contains a sorted set of list of allele+distance "a_b" (a is positive or negative, not equal to 0), b is an integer >= 0 |
| 131 |
Divided into buckets. Each first value is a bucket. |
| 132 |
""" |
| 133 |
|
| 134 |
def __init__(self): |
| 135 |
self.main_dict={} |
| 136 |
self.size=0 |
| 137 |
|
| 138 |
|
| 139 |
def add(self, mylist): |
| 140 |
"""add a new list""" |
| 141 |
self.size+=1 |
| 142 |
zdk = zero_d_key(mylist[0]) |
| 143 |
if zdk not in self.main_dict: |
| 144 |
self.main_dict[zdk]=[] |
| 145 |
self.main_dict[zdk]+=[mylist[1:]] |
| 146 |
|
| 147 |
|
| 148 |
def sorted_add(self,mylist): |
| 149 |
"""add a new list""" |
| 150 |
self.add(mylist) |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
current_list = self.main_dict[zero_d_key(mylist[0])] |
| 158 |
pos_current = len(current_list)-1 |
| 159 |
while True: |
| 160 |
previous = pos_current-1 |
| 161 |
while current_list[previous]==None and previous>=0: |
| 162 |
previous-=1 |
| 163 |
|
| 164 |
if previous==-1: break |
| 165 |
|
| 166 |
if kc.d_list_order(current_list[previous],current_list[pos_current]) <=0: break |
| 167 |
|
| 168 |
else : |
| 169 |
current_list[previous], current_list[pos_current] = current_list[pos_current], current_list[previous] |
| 170 |
pos_current = previous; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
def sort(self): |
| 176 |
"""sort the whole structure - feasible only before removing elements""" |
| 177 |
for key, value in self.main_dict.items(): |
| 178 |
value.sort(key=kc.allele_values) |
| 179 |
|
| 180 |
def traverse(self): |
| 181 |
for key, value in self.main_dict.items(): |
| 182 |
|
| 183 |
for mylist in value: |
| 184 |
if mylist != None: |
| 185 |
yield [key]+mylist |
| 186 |
|
| 187 |
def index_nodes(self): |
| 188 |
''' For each element in the structure, we add its id as a last value, stored as i_14, for instance for node 14.''' |
| 189 |
index_id=1 |
| 190 |
for key, value in self.main_dict.items(): |
| 191 |
for mylist in value: |
| 192 |
if mylist != None: |
| 193 |
mylist+=['i_'+str(index_id)] |
| 194 |
index_id+=1 |
| 195 |
|
| 196 |
|
| 197 |
def remove(self,mylist): |
| 198 |
'''remove an element from the structure''' |
| 199 |
''' if the element is not in a structure a warning is raised''' |
| 200 |
zero_d_key = mylist[0].split('_')[0]+'_0' |
| 201 |
try: |
| 202 |
tormindex=self.main_dict[zero_d_key].index(mylist[1:]) |
| 203 |
del self.main_dict[zero_d_key][tormindex] |
| 204 |
except : |
| 205 |
|
| 206 |
|
| 207 |
return 0 |
| 208 |
self.size-=1 |
| 209 |
return 1 |
| 210 |
|
| 211 |
def get_node_id(self, search_sr): |
| 212 |
list_nodes = self.get_lists_starting_with_given_prefix(search_sr) |
| 213 |
|
| 214 |
for node in list_nodes: |
| 215 |
if node[:-1]==search_sr: |
| 216 |
return node[-1].split("_")[-1] |
| 217 |
return None |
| 218 |
|
| 219 |
|
| 220 |
def get_lists_starting_with_given_prefix(self, prefix): |
| 221 |
''' given a prefix of a list, return all lists in the set starting with this prefix |
| 222 |
Dichotomy. log(|self|/size(alphabet)) comparisons |
| 223 |
O(|prefix|*log(|self|/size(alphabet))) |
| 224 |
''' |
| 225 |
|
| 226 |
|
| 227 |
zkd = zero_d_key(prefix[0]) |
| 228 |
if zkd not in self.main_dict: return [] |
| 229 |
|
| 230 |
if len(prefix)==1: |
| 231 |
res = [] |
| 232 |
for l in self.main_dict[zkd]: |
| 233 |
res.append([prefix[0]]+l) |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
return res |
| 238 |
|
| 239 |
first=prefix[0] |
| 240 |
|
| 241 |
current_list = self.main_dict[zkd] |
| 242 |
|
| 243 |
prefix = prefix[1:] |
| 244 |
start=0 |
| 245 |
n=len(current_list)-1 |
| 246 |
stop=n |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
while start <=stop: |
| 251 |
|
| 252 |
middle = start+int((stop-start)/2) |
| 253 |
if middle>n: break |
| 254 |
|
| 255 |
while middle<=stop: |
| 256 |
tuple1 = current_list[middle] |
| 257 |
if tuple1 == None: middle+=1 |
| 258 |
else: break |
| 259 |
if middle == stop+1: |
| 260 |
middle = start+int((stop-start)/2)-1 |
| 261 |
while middle>=start: |
| 262 |
tuple1 = current_list[middle] |
| 263 |
if tuple1 == None: middle-=1 |
| 264 |
else: break |
| 265 |
if middle == start-1: return [] |
| 266 |
|
| 267 |
|
| 268 |
cmp_val = compare(tuple1,prefix) |
| 269 |
if cmp_val == -1: |
| 270 |
start = middle+1 |
| 271 |
continue |
| 272 |
if cmp_val == 1: |
| 273 |
stop = middle-1 |
| 274 |
continue |
| 275 |
|
| 276 |
res=[[first]+tuple1] |
| 277 |
|
| 278 |
i=middle-1 |
| 279 |
while i>-1: |
| 280 |
if current_list[i]!=None: |
| 281 |
if compare(current_list[i],prefix)==0: res.append([first]+current_list[i]) |
| 282 |
else: break |
| 283 |
i-=1 |
| 284 |
|
| 285 |
i=middle+1 |
| 286 |
while i<=n: |
| 287 |
if current_list[i]!=None: |
| 288 |
if compare(current_list[i],prefix)==0: res.append([first]+current_list[i]) |
| 289 |
else: break |
| 290 |
i+=1 |
| 291 |
|
| 292 |
|
| 293 |
return res |
| 294 |
|
| 295 |
return [] |
| 296 |
|
| 297 |
def contains (self,mylist): |
| 298 |
''' if sr is in SR: return True, else return False. |
| 299 |
faster (O(log(|SR|)) that the 'in' function from non ordered array (O(|SR|)) |
| 300 |
''' |
| 301 |
X=self.get_lists_starting_with_given_prefix(mylist) |
| 302 |
if X==mylist: return True |
| 303 |
if mylist in X: return True |
| 304 |
return False |
| 305 |
|
| 306 |
def __len__(self): |
| 307 |
return self.size |
| 308 |
|
| 309 |
|
| 310 |
def contains(self,query): |
| 311 |
"""only for debug purpose, no need to be optimized""" |
| 312 |
for key, value in self.main_dict.items(): |
| 313 |
if key==query: |
| 314 |
print("key", key, "query",query) |
| 315 |
return True |
| 316 |
for followup in value: |
| 317 |
for unitig_id in followup: |
| 318 |
if unitig_id==query: |
| 319 |
return True |
| 320 |
return False |
| 321 |
|
| 322 |
def unique(self): |
| 323 |
for key, value in self.main_dict.items(): |
| 324 |
size_before=len(value) |
| 325 |
value=unique(value) |
| 326 |
size_after=len(value) |
| 327 |
self.main_dict[key]=value |
| 328 |
self.size-=(size_before-size_after) |
| 329 |
|
| 330 |
def __str__(self): |
| 331 |
str_SR="" |
| 332 |
for key, value in self.main_dict.items(): |
| 333 |
for followup in value: |
| 334 |
str_SR+=str(key) |
| 335 |
for unitig_id in followup: |
| 336 |
str_SR+=","+str(unitig_id) |
| 337 |
str_SR+="\n" |
| 338 |
return str_SR |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|