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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ABOUT:
======
precompute counts for HMM inputs

updated on Sept 26 2024 for (B, L, 2) input sizes

"""
import jax
from jax import numpy as jnp


######################
### HELPER FUNCTIONS #
######################
# these get called in summarize_alignment
def count_substitutions(one_sample, one_sample_bool):
    """
    vectorized way to count types of substitutions i.e. emissions at
    match states
    yields a (20, 20) matrix per site in max_len
     
    this will get vmapped over the batch dimension
    """
    # identify what the pair is, using an indicator matrix: 
    #    (rows = anc, cols = desc)
    # subtract index by 3, because there's three special tokens at beginning 
    # of alphabet: pad, bos, and eos
    # finally, multiply by sample boolean i.e. mask the whole matrix if 
    # the position is NOT a match position
    one_sample_bool = jnp.expand_dims(one_sample_bool, 1)
    
    def encode_pairs(vec_at_pos, bool_at_pos):
        indc_mat = jnp.zeros((20,20),dtype='uint8')
        anc_tok, desc_tok = vec_at_pos
        indc_mat = indc_mat.at[anc_tok-3, desc_tok-3].add(1)
        indc_mat = indc_mat * bool_at_pos
        return indc_mat
    vmapped_encode_pairs = jax.vmap(encode_pairs, in_axes=0)
    
    subCounts_persite_persamp = vmapped_encode_pairs(one_sample, one_sample_bool)
    subCounts = subCounts_persite_persamp.sum(axis=0)
    
    return subCounts


def count_insertions(batch, ins_pos_mask):
    """
    count different types of insertions i.e. emissions at insert states
    yields a (20,) vector for the whole batch
    
    unlike other counting methods, this can operate on the whole batch at once
    """
    ### use insertion positions as a mask on the batch
    # look at DESCENDANT SEQ to see what types of insertions happen
    all_inserts = batch[:, :, 1] * ins_pos_mask
    
    ### count the number of valid tokens
    # this gets vmapped over the valid alphabet
    valid_toks = jnp.arange(3, 23)
    def count_insertions(tok):
        return (all_inserts == tok).sum(axis=1)
    vmapped_count_insertions = jax.vmap(count_insertions, in_axes=0)
    
    insCounts = vmapped_count_insertions(valid_toks)
    return insCounts.T


def count_deletions(batch, del_pos_mask):
    """
    count different types of deletions 
    yields a (20,) vector for the whole batch
    
    unlike other counting methods, this can operate on the whole batch at once
    """
    ### use deletion positions as a mask on the batch
    # look at ANCESTOR SEQ to see what aas got deleted
    all_deletes = batch[:, :, 0] * del_pos_mask
    
    ### count the number of valid tokens
    # this gets vmapped over the valid alphabet
    valid_toks = jnp.arange(3, 23)
    def count_deletions(tok):
        return (all_deletes == tok).sum(axis=1)
    vmapped_count_deletions = jax.vmap(count_deletions, in_axes=0)
    
    delCounts = vmapped_count_deletions(valid_toks)
    return delCounts.T


def count_transitions(one_alignment_path, start_idxes):
    """
    vectorized way to count types of transitions (M, I, D)
    yields a (3, 3) matrix for the sample
     
    this will get vmapped over the batch dimension
    """
    # this is vmapped over the length of start_idxes, to get a sliding
    # window effect on one_alignment_path
    def identify_pair_type(start_idx):
        indicator_mat = jnp.zeros((4,4))
        from_tok, to_tok = one_alignment_path[(start_idx, start_idx+1),]
        indicator_mat = indicator_mat.at[from_tok, to_tok].add(1)
        return indicator_mat
    vmapped_subpairs = jax.vmap(identify_pair_type, in_axes=0)
    
    # indicator matrix is (4,4), but first row and column are transitions
    # to padding characters and don't really count; cut them off
    out = vmapped_subpairs(start_idxes)
    transition_counts = out[:, 1:, 1:]
    
    # sum over whole sequence length to get all transitions for this 
    # alignment path
    transition_counts = jnp.sum(transition_counts, axis=0)
    return transition_counts





###################
### MAIN FUNCTION #
###################
def summarize_alignment(batch, align_len, gap_tok=43):
    """
    batch should be a tensor of aligned sequences that are categorically 
    encoded, with the following non-alphabet tokens:
         0: <pad>
         1: <bos> (not included in pairHMM data, but still reserved token)
         2: <eos> (not included in pairHMM data, but still reserved token)
        43: default gap char (but can be changed)
    
    batch is of size (batch_size, max_seq_len, 2), where-
        dim2=0: ancestor sequence, aligned (i.e. with gaps)
        dim2=1: descendant sequence, aligned (i.e. with gaps)
    
    align_len is a vector of size (batch_size,) that has the length of the 
        alignments
    
    """
    #######################################
    ### COMPRESS ALIGNMENT REPRESENTATION #
    #######################################
    ### split into gaps vs not gaps
    non_gaps = jnp.where((batch != gap_tok) & (batch != 0), 1, 0)
    gaps = jnp.where((batch == gap_tok), batch, 0)
    
    ### find matches, inserts, and deletions
    # matches found using non_gaps vector
    match_pos = jnp.where(jnp.sum(non_gaps, axis=2) == 2, 1, 0)
    
    # inserts mean ancestor char == gap_tok
    ins_pos = jnp.where(gaps[:,:,0] == gap_tok, 1, 0)
    
    # deletions means descendant char == gap_tok
    del_pos = jnp.where(gaps[:,:,1] == gap_tok, 1, 0)
    
    # combine all into one vec for later
    # M = 1, I = 2, D = 3; padding is 0
    paths_compressed = (match_pos + (ins_pos*2) + (del_pos*3))
    
    
    ### ADD ADDITIONAL MATCH STATES AT BEGINNING AND END OF ALIGNMENT PATHS
    ### this is part of the LG05, RS07, and H20 assumptions
    # add match at the end of the paths
    extra_end_col = jnp.zeros((batch.shape[0], 1))
    to_adjust = jnp.concatenate([paths_compressed, extra_end_col], axis=1)
    x_idxes = (jnp.arange(0, batch.shape[0]))
    with_extra_end_match = to_adjust.at[x_idxes, align_len].add(1)
    
    # add extra start at the beginning of the paths
    extra_start_col = jnp.ones((batch.shape[0], 1))
    paths_with_extra_start_end_matches = jnp.concatenate([extra_start_col, 
                                                          with_extra_end_match], 
                                                          axis=1).astype(int)
    
    
    ### clean up variables
    del non_gaps, gaps, paths_compressed
    
    
    ######################################
    ### COUNT EMISSIONS FROM MATCH STATE #
    ######################################
    ### use count_substitutions function, defined above
    ### vmap it along the batch dimension (dim0)
    match_pos = match_pos.astype(bool)
    countsubs_vmapped = jax.vmap(count_substitutions, 
                                 in_axes = (0,0))
    subCounts_persamp = countsubs_vmapped(batch, 
                                          match_pos)
    
    
    #######################################
    ### COUNT EMISSIONS FROM INSERT STATE #
    #######################################
    insCounts_persamp = count_insertions(batch = batch, 
                                         ins_pos_mask = ins_pos)
    del ins_pos
    
    ################################################
    ### COUNT TYPES OF DELETIONS FROM DELETE STATE #
    ################################################
    delCounts_persamp = count_deletions(batch = batch, 
                                        del_pos_mask = del_pos)
    del del_pos
    
    
    #######################
    ### COUNT TRANSITIONS #
    #######################
    ### use count_transitions function, defined above
    ### vmap it along the batch dimension (dim0)
    counttrans_vmapped = jax.vmap(count_transitions, 
                                  in_axes=(0, None))
    start_idxes = jnp.arange(start = 0, 
                             stop = paths_with_extra_start_end_matches.shape[1] - 1).astype(int)
    transCounts_persamp = counttrans_vmapped(paths_with_extra_start_end_matches, 
                                             start_idxes)

    return (subCounts_persamp, insCounts_persamp,
            delCounts_persamp, transCounts_persamp)


######################################
### overal equilibrium distributions #
######################################
def get_aa_counts(seq_mat):
    valid_aas = jnp.arange(3, 23)
    
    # vmap this over valid_aas
    def count_aas(aa, dset):
        return (dset == aa).sum()
    
    vmapped_count_aas = jax.vmap(count_aas, in_axes=(0, None))
    counts = vmapped_count_aas(valid_aas, seq_mat)
    return counts

def safe_convert(mat):
    assert mat.max() <= 65535
    assert mat.max() >= 0
    return mat.astype('uint16')


##############################################
### functional wrappers that also save files #
##############################################
def get_equilib_counts(in_dir, split, gap_tok=43):
    print(f'Working on split: {split}')
    
    ### read file
    with open(f'{in_dir}/{split}_pair_alignments.npy', 'rb') as f:
        # B, L, 2
        hmm_paths = np.load(f)
        assert hmm_paths.shape[1] == 2324
        del f
    
    ### run get_aa_counts to get equilibrium distribution across all positions
    AA_counts = get_aa_counts(hmm_paths)
    
    out_file = f'{split}_AAcounts.npy'
    with open(out_file, 'wb') as g:
        np.save(out_file, AA_counts)
    del AA_counts, out_file
    
    
    ### mask to only select match positions
    # split into gaps vs not gaps
    non_gaps = np.where((hmm_paths != gap_tok) & (hmm_paths != 0), 1, 0)
        
    
    # find matches positions, use it to create a mask to only keep values at 
    # match sites
    match_pos = np.where(np.sum(non_gaps, axis=-1) == 2, True, False)[:,:,None]
    del non_gaps
    
    match_mask = np.repeat(match_pos, 2, axis=-1)
    del match_pos
    
    masked_hmm_paths = hmm_paths * match_mask
    del match_mask
    
    
    ### run get_aa_counts as normal on this masked matrix to get equilibrium
    ### distribution ONLY at match positions
    AA_counts_subsOnly = get_aa_counts(masked_hmm_paths)
    
    out_file = f'{split}_AAcounts_subsOnly.npy'
    with open(out_file, 'wb') as g:
        np.save(out_file, AA_counts_subsOnly)
        
        
def summarize_trans_emiss_counts_wrapper(in_dir, split):
    print(f'Processing {split}')
    ### initialize the pytorch dataset object
    dset = pairAlign_dloader(in_dir = in_dir,
                             split = split)
    
    ### create a dataloader that returns jax arrays
    dload = DataLoader(dset, 
                       batch_size = batch_size, 
                       shuffle = False,
                       collate_fn = jax_collator)
    
    subCounts_lst = []
    insCounts_lst = []
    delCounts_lst = []
    transCounts_lst = []
    for i, batch in enumerate(dload):
        # batch is (batchsize, max_len, 2)
        # lens will be (batchsize, max_len)
        lens = (batch != 0).sum(axis=1)[:,0]
        if i == 0:
            jitted_summarize_alignment = jax.jit(summarize_alignment)
            
            
        ### use jit-compiled summarize alignment
        out = jitted_summarize_alignment(batch, lens)
        batch_subCounts_persamp = out[0]
        batch_insCounts_persamp = out[1]
        batch_delCounts_persamp = out[2]
        batch_transCounts_persamp = out[3]
        del out
        
        # adjust types
        batch_subCounts_persamp = safe_convert(batch_subCounts_persamp)
        batch_insCounts_persamp = safe_convert(batch_insCounts_persamp)
        batch_delCounts_persamp = safe_convert(batch_delCounts_persamp)
        batch_transCounts_persamp = safe_convert(batch_transCounts_persamp)
        
        # append
        subCounts_lst.append(batch_subCounts_persamp)
        insCounts_lst.append(batch_insCounts_persamp)
        delCounts_lst.append(batch_delCounts_persamp)
        transCounts_lst.append(batch_transCounts_persamp)
    
    
    # concatenate
    subCounts_persamp = jnp.concatenate(subCounts_lst, axis=0)
    assert subCounts_persamp.shape == (len(dset), 20, 20), f'subCounts shape is wrong: {subCounts_persamp.shape}'
    del subCounts_lst
    
    insCounts_persamp = jnp.concatenate(insCounts_lst, axis=0)
    assert insCounts_persamp.shape == (len(dset), 20), f'insCounts shape is wrong: {insCounts_persamp.shape}'
    del insCounts_lst
    
    delCounts_persamp = jnp.concatenate(delCounts_lst, axis=0)
    assert delCounts_persamp.shape == (len(dset), 20), f'delCounts shape is wrong: {delCounts_persamp.shape}'
    del delCounts_lst
    
    transCounts_persamp = jnp.concatenate(transCounts_lst, axis=0)
    assert transCounts_persamp.shape == (len(dset), 3, 3), f'transCounts shape is wrong: {transCounts_persamp.shape}'
    del transCounts_lst
    
    
    # output
    with open(f'./precalculated_counts/{split}_subCounts.npy', 'wb') as g:
        jnp.save(g, subCounts_persamp)
    
    with open(f'./precalculated_counts/{split}_insCounts.npy', 'wb') as g:
        jnp.save(g, insCounts_persamp)
    
    with open(f'./precalculated_counts/{split}_delCounts.npy', 'wb') as g:
        jnp.save(g, delCounts_persamp)
    
    with open(f'./precalculated_counts/{split}_transCounts.npy', 'wb') as g:
        jnp.save(g, transCounts_persamp)


if __name__ == '__main__':
    import os
    import numpy as np
    from pairAlign_dloader import pairAlign_dloader, jax_collator
    from tqdm import tqdm
    
    ### make sure this still works as expected
    # (M->M) (added START)
    # (M->D)
    # (D->I)
    # (I->M) (added END)
    fake_align = jnp.array([[3,  4, 43, 0],
                            [3, 43,  5, 0]]).T[None,:,:]
    
    true_trans_mat = jnp.array([[1, 0, 1],
                                [1, 0, 0],
                                [0, 1, 0]])
    
    true_match_mat = jnp.zeros( (20, 20) )
    true_match_mat = true_match_mat.at[0,0].add(1)
    
    true_del_vec = jnp.zeros( (20,) )
    true_del_vec = true_del_vec.at[1].add(1)
    
    true_ins_vec = jnp.zeros( (20,) )
    true_ins_vec = true_ins_vec.at[2].add(1)
    
    out = summarize_alignment(fake_align, 
                              3, 
                              gap_tok=43)
    
    pred_match_mat = out[0][0]
    pred_ins_vec = out[1][0]
    pred_del_vec = out[2][0]
    pred_trans_mat = out[3][0]
    del out
    
    assert jnp.allclose(true_match_mat, pred_match_mat)
    assert jnp.allclose(true_del_vec, pred_del_vec)
    assert jnp.allclose(true_ins_vec, pred_ins_vec)
    assert jnp.allclose(true_trans_mat, pred_trans_mat)
    
    
    
    # ### do this for all regular spilts
    # splitnames = [f'TENPERC_split{i}' for i in range(5)]
    # batch_size = 1500
    
    # for split in splitnames:
    #     get_equilib_counts(in_dir = 'tenperc_data_pairAlignments',  
    #                        split = split, 
    #                        gap_tok = 43)
        
    #     summarize_trans_emiss_counts_wrapper(in_dir = 'tenperc_data_pairAlignments',  
    #                                          split = split)