-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathutils.py
555 lines (481 loc) · 15.2 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import re
import numpy as np
import torch
from torch.nn.utils.rnn import pad_packed_sequence, pack_sequence
from scipy.sparse import coo_matrix
from scipy.spatial import cKDTree
from deepblast.constants import x, m, y
from itertools import islice
from functools import reduce
from numba import jit
def state_f(z):
if z[0] == '-':
return x
if z[1] == '-':
return y
else:
return m
def tmstate_f(z):
""" Parsing TM-specific state string. """
if z == '1':
return x
if z == '2':
return y
else:
return m
def revstate_f(z):
if z == x:
return '1'
if z == y:
return '2'
if z == m:
return ':'
def clip_boundaries(X, Y, A, st):
""" Remove xs and ys from ends. """
if A[0] == m:
first = 0
else:
first = A.index(m)
if A[-1] == m:
last = len(A)
else:
last = len(A) - A[::-1].index(m)
X, Y = states2alignment(np.array(A), X, Y)
X_ = X[first:last].replace('-', '')
Y_ = Y[first:last].replace('-', '')
A_ = A[first:last]
st_ = st[first:last]
return X_, Y_, A_, st_
def state_diff_f(X):
""" Constructs a state transition element.
Notes
-----
There is a bit of a paradox regarding beginning / ending gaps.
To see this, try to derive an alignment matrix for the
following alignments
XXXMMMXXX
MMYYXXMM
It turns out it isn't possible to derive traversal rules
that are consistent between these two alignments
without explicitly handling start / end states as separate
end states. The current workaround is to force the start / end
states to be match states (similar to the needleman-wunsch algorithm).
"""
a, b = X
if a == x and b == x:
# Transition XX, increase tape on X
return (1, 0)
if a == x and b == m:
# Transition XM, increase tape on both X and Y
return (1, 1)
if a == m and b == m:
# Transition MM, increase tape on both X and Y
return (1, 1)
if a == m and b == x:
# Transition MX, increase tape on X
return (1, 0)
if a == m and b == y:
# Transition MY, increase tape on y
return (0, 1)
if a == y and b == y:
# Transition YY, increase tape on y
return (0, 1)
if a == y and b == m:
# Transition YM, increase tape on both X and Y
return (1, 1)
if a == x and b == y:
# Transition XY increase tape on y
return (0, 1)
if a == y and b == x:
# Transition YX increase tape on x
return (1, 0)
else:
raise ValueError(f'`Transition` ({a}, {b}) is not allowed.')
def states2edges(states):
""" Converts state string to bipartite matching. """
prev_s, next_s = states[:-1], states[1:]
transitions = list(zip(prev_s, next_s))
state_diffs = np.array(list(map(state_diff_f, transitions)))
coords = np.cumsum(state_diffs, axis=0).tolist()
coords = [(0, 0)] + list(map(tuple, coords))
return coords
def states2matrix(states, sparse=False):
""" Converts state string to alignment matrix.
Parameters
----------
states : list
The state string
"""
coords = states2edges(states)
data = np.ones(len(coords))
row, col = list(zip(*coords))
row, col = np.array(row), np.array(col)
N, M = max(row) + 1, max(col) + 1
mat = coo_matrix((data, (row, col)), shape=(N, M))
if sparse:
return mat
else:
return mat.toarray()
def states2alignment(states: np.array, X: str, Y: str):
""" Converts state string to gapped alignments """
# Convert states to array if it is a string
if isinstance(states, str):
states = np.array(list(map(tmstate_f, list(states))))
sx = np.sum(states == x) + np.sum(states == m)
sy = np.sum(states == y) + np.sum(states == m)
if sx != len(X):
raise ValueError(
f'The state string length {sx} does not match '
f'the length of sequence {len(X)}.\n'
f'SequenceX: {X}\nSequenceY: {Y}\nStates: {states}\n'
)
if sy != len(Y):
raise ValueError(
f'The state string length {sy} does not match '
f'the length of sequence {len(X)}.\n'
f'SequenceX: {X}\nSequenceY: {Y}\nStates: {states}\n'
)
i, j = 0, 0
res = []
for k in range(len(states)):
if states[k] == x:
cx = X[i]
cy = '-'
i += 1
elif states[k] == y:
cx = '-'
cy = Y[j]
j += 1
elif states[k] == m:
cx = X[i]
cy = Y[j]
i += 1
j += 1
else:
raise ValueError(f'{states[k]} is not recognized')
res.append((cx, cy))
aligned_x, aligned_y = zip(*res)
return ''.join(aligned_x), ''.join(aligned_y)
def reverse_dict(x):
return dict(list(zip(list(x.values()), list(x.keys()))))
def decode(codes, alphabet):
""" Converts one-hot encodings to string
Parameters
----------
code : torch.Tensor
One-hot encodings.
alphabet : dict-like
Matches letters to one-hot encodings.
Returns
-------
genes : list of Tensor
List of proteins
others : list of Tensor
List of proteins
states : list of Tensor
List of alignment state strings
dm : torch.Tensor
B x N x M dimension matrix with padding.
"""
alphabet = reverse_dict(alphabet)
s = list(map(lambda x: alphabet[int(x)], codes))
return ''.join(s).replace('▁', '')
def pack_sequences(genes, others):
x = genes + others
lens = list(map(len, x))
order = np.argsort(lens)[::-1].copy()
y = [x[i] for i in order]
packed = pack_sequence(y)
return packed, order
def unpack_sequences(x, order):
""" Unpack object into two sequences.
Parameters
----------
x : PackedSequence
Packed sequence object containing 2 sequences.
order : np.array
The original order of the sequences.
Returns
-------
x : torch.Tensor
Tensor representation for first protein sequences.
xlen : torch.Tensor
Lengths of the first protein sequences.
y : torch.Tensor
Tensor representation for second protein sequences.
ylen : torch.Tensor
Lengths of the second protein sequences.
"""
lookup = {order[i]: i for i in range(len(order))}
seq, seqlen = pad_packed_sequence(x, batch_first=True)
seq = [seq[lookup[i]] for i in range(len(order))]
seqlen = [seqlen[lookup[i]] for i in range(len(order))]
b = len(seqlen) // 2
x, xlen = torch.stack(seq[:b]), torch.stack(seqlen[:b]).long()
y, ylen = torch.stack(seq[b:]), torch.stack(seqlen[b:]).long()
return x, xlen, y, ylen
def collate_f(batch):
genes = [x[0] for x in batch]
others = [x[1] for x in batch]
states = [x[2] for x in batch]
alignments = [x[3] for x in batch]
paths = [x[4] for x in batch]
masks = [x[5] for x in batch]
g_masks = [x[6] for x in batch]
o_masks = [x[7] for x in batch]
max_x = max(map(len, genes))
max_y = max(map(len, others))
B = len(genes)
dm = torch.zeros((B, max_x, max_y))
p = torch.zeros((B, max_x, max_y))
gM = torch.zeros((B, max_x))
oM = torch.zeros((B, max_y))
G = torch.zeros((B, max_x, max_y)).bool()
G.requires_grad = False
for b in range(B):
n, m = len(genes[b]), len(others[b])
dm[b, :n, :m] = alignments[b]
p[b, :n, :m] = paths[b]
G[b, :n, :m] = masks[b].bool()
gM[b, :n] = g_masks[b]
oM[b, :m] = o_masks[b]
return genes, others, states, dm, p, G, gM, oM
def test_collate_f(batch):
genes = [x[0] for x in batch]
others = [x[1] for x in batch]
states = [x[2] for x in batch]
alignments = [x[3] for x in batch]
paths = [x[4] for x in batch]
masks = [x[5] for x in batch]
gene_names = [x[6] for x in batch]
other_names = [x[7] for x in batch]
max_x = max(map(len, genes))
max_y = max(map(len, others))
B = len(genes)
dm = torch.zeros((B, max_x, max_y))
p = torch.zeros((B, max_x, max_y))
G = torch.zeros((B, max_x, max_y)).bool()
G.requires_grad = False
for b in range(B):
n, m = len(genes[b]), len(others[b])
dm[b, :n, :m] = alignments[b]
p[b, :n, :m] = paths[b]
G[b, :n, :m] = masks[b].bool()
return genes, others, states, dm, p, G, gene_names, other_names
def collate_fasta_f(batch):
gene_ids = [x[0] for x in batch]
other_ids = [x[1] for x in batch]
genes = [x[2] for x in batch]
others = [x[3] for x in batch]
seqs, order = pack_sequences(genes, others)
return gene_ids, other_ids, seqs, order
def path_distance_matrix(pi):
""" Builds a min path distance matrix.
This will be passed into the SoftPathLoss function.
For each cell, it will compute the distance between
coordinates in the cell and the nearest cell located in the path.
Parameters
----------
pi : list of tuple
Coordinates of the ground truth alignment
Returns
-------
Pdist : np.array
Matrix of distances to path.
"""
pi = np.array(pi)
model = cKDTree(pi)
xs = np.arange(pi[:, 0].max() + 1)
ys = np.arange(pi[:, 1].max() + 1)
coords = np.dstack(np.meshgrid(xs, ys)).reshape(-1, 2)
d, i = model.query(coords)
Pdist = np.array(coo_matrix((d, (coords[:, 0], coords[:, 1]))).todense())
return Pdist
# Preprocessing functions
# def gap_mask(states: np.array):
# """ Builds a mask for all gaps (0s are gaps, 1s are matches)
#
# Parameters
# ----------
# states : np.array
# List of alignment states
#
# Returns
# -------
# mask : np.array
# Masked array.
#
# Notes
# -----
# Gaps and mismatches (denoted by `.`) are all masked here.
# """x
# i, j = 0, 0
# res = []
# coords = []
# data = []
# for k in range(len(states)):
# # print(i, j, k, states[k])
# if states[k] == '1':
# coords.append((i, j))
# data.append(0)
# i += 1
# elif states[k] == '2':
# coords.append((i, j))
# data.append(0)
# j += 1
# elif states[k] == ':':
# coords.append((i, j))
# data.append(1)
# i += 1
# j += 1
# elif states[k] == '.':
# coords.append((i, j))
# data.append(0)
# i += 1
# j += 1
# else:
# raise ValueError(f'{states[k]} is not recognized')
# rows, cols = zip(*coords)
# rows = np.array(rows)
# cols = np.array(cols)
# data = np.array(data)
# mask = coo_matrix((data, (rows, cols))).todense()
# return mask
def gap_mask(states: str, sparse=False):
st = np.array(list(map(tmstate_f, list(states))))
coords = states2edges(st)
data = np.ones(len(coords))
row, col = list(zip(*coords))
row, col = np.array(row), np.array(col)
N, M = max(row) + 1, max(col) + 1
idx = np.array(list(states)) == ':'
idx[0] = 1
data = data[idx]
row = row[idx]
col = col[idx]
mat = coo_matrix((data, (row, col)), shape=(N, M))
if sparse:
return mat
else:
return mat.toarray().astype(bool)
def window(seq, n=2):
"Returns a sliding window (of width n) over data from the iterable"
" s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... "
it = iter(seq)
result = tuple(islice(it, n))
if len(result) == n:
yield result
for elem in it:
result = result[1:] + (elem,)
yield result
def replace_orphan(w, s=5):
i = len(w) // 2
# identify orphans and replace with gaps
sw = ''.join(w)
if ((w[i] == ':') and ((('1' * s) in sw[:i] and ('1' * s) in sw[i:])
or (('2' * s) in sw[:i] and ('2' * s) in sw[i:]))):
return ['1', '2']
else:
return [w[i]]
def remove_orphans(states, threshold: int = 11):
""" Removes singletons and doubletons that are orphaned.
A match is considered orphaned if it exceeds the `threshold` gap.
Parameters
----------
states : np.array
List of alignment states
threshold : int
Number of consecutive gaps surrounding a matched required for it
to be considered an orphan.
Returns
-------
new_states : np.array
States string with orphans removed.
Notes
-----
The threshold *must* be an odd number. This determines the window size.
"""
wins = list(window(states, threshold))
rwins = list(map(lambda x: replace_orphan(x, threshold // 2), list(wins)))
new_states = list(reduce(lambda x, y: x + y, rwins))
new_states += list(states[:threshold // 2])
new_states += list(states[-threshold // 2 + 1:])
return ''.join(new_states)
def reshape(x, N, M):
# Motherfucker ...
if x.shape != (N, M) and x.shape != (M, N):
raise ValueError(f'The shape of `x` {x.shape} '
f'does not agree with ({N}, {M})')
if tuple(x.shape) != (N, M):
return x.t()
else:
return x
def get_sequence(x, tokenizer):
x = [re.sub(r"[UZOB]", "X", ' '.join(list(x)))]
id_ = tokenizer.batch_encode_plus(
x, add_special_tokens=False, padding=True)
seq = torch.Tensor(id_['input_ids']).long().squeeze()
mask = torch.Tensor(id_['attention_mask']).squeeze()
return seq, mask
@jit(nopython=True)
def is_subset(x, y):
# test if x is a subset of y
if x.shape[0] > y.shape[0]:
return False
k = x.shape[0]
for i in range(y.shape[0] - k + 1):
if all([y[i:i+k][j] == x[j] for j in range(len(x))]):
return True
return False
@jit(nopython=True)
def __trim_gap(x, k=10):
"""
Parameters
----------
x : np.array of binary values
Returns
-------
i, j : ints
index of longest subsequence without k consecutive gaps
y : np.array
Mask indicating longest subsequence without k consecutive gaps
"""
gaps = np.zeros(k).astype(np.int64)
# unfortunately, we are going to have to brute force this and evaluate
# all O(n^2) possible substrings
ans = np.zeros((x.shape[0], x.shape[0]))
for i in range(x.shape[0]):
for j in range(i):
if is_subset(gaps, x[j:i]):
continue
else:
ans[i, j] = i - j
return ans
def _trim_gap(x, k=10):
ans = __trim_gap(x, k)
ind = np.unravel_index(np.argmax(ans, axis=None), ans.shape)
i, j = ind
return j, i
def trim_gap(dfX, k=10):
""" Gets longest subsequence without k consecutive gaps
Parameters
----------
dfX : pd.DataFrame
Contains columns named `chain1`, `chain2` and `alignment`
"""
dfY = dfX.copy()
# 1 if match, 0 if gap
bin_aln = (
~(np.array(list(dfX['alignment'])) != ':').astype(np.bool)
).astype(np.int)
str_aln = ''.join(map(str, list(bin_aln)))
if '0'*k in str_aln:
i, j = _trim_gap(bin_aln, k)
states = np.array(list(map(tmstate_f, list(dfX['alignment']))))
Ax, Ay = states2alignment(states, dfX['chain1'], dfX['chain2'])
dfY['chain1'] = Ax[i:j].replace('-', '')
dfY['chain2'] = Ay[i:j].replace('-', '')
dfY['alignment'] = dfX['alignment'][i:j]
return dfY
else:
return dfX