| |
| |
| """ |
| 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 |
|
|
|
|
| |
| |
| |
| |
| 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 |
| """ |
| |
| |
| |
| |
| |
| |
| 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 |
| """ |
| |
| |
| all_inserts = batch[:, :, 1] * ins_pos_mask |
| |
| |
| |
| 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 |
| """ |
| |
| |
| all_deletes = batch[:, :, 0] * del_pos_mask |
| |
| |
| |
| 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 |
| """ |
| |
| |
| 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) |
| |
| |
| |
| out = vmapped_subpairs(start_idxes) |
| transition_counts = out[:, 1:, 1:] |
| |
| |
| |
| transition_counts = jnp.sum(transition_counts, axis=0) |
| return transition_counts |
|
|
|
|
|
|
|
|
|
|
| |
| |
| |
| 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 |
| |
| """ |
| |
| |
| |
| |
| non_gaps = jnp.where((batch != gap_tok) & (batch != 0), 1, 0) |
| gaps = jnp.where((batch == gap_tok), batch, 0) |
| |
| |
| |
| match_pos = jnp.where(jnp.sum(non_gaps, axis=2) == 2, 1, 0) |
| |
| |
| ins_pos = jnp.where(gaps[:,:,0] == gap_tok, 1, 0) |
| |
| |
| del_pos = jnp.where(gaps[:,:,1] == gap_tok, 1, 0) |
| |
| |
| |
| paths_compressed = (match_pos + (ins_pos*2) + (del_pos*3)) |
| |
| |
| |
| |
| |
| 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) |
| |
| |
| 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) |
| |
| |
| |
| del non_gaps, gaps, paths_compressed |
| |
| |
| |
| |
| |
| |
| |
| match_pos = match_pos.astype(bool) |
| countsubs_vmapped = jax.vmap(count_substitutions, |
| in_axes = (0,0)) |
| subCounts_persamp = countsubs_vmapped(batch, |
| match_pos) |
| |
| |
| |
| |
| |
| insCounts_persamp = count_insertions(batch = batch, |
| ins_pos_mask = ins_pos) |
| del ins_pos |
| |
| |
| |
| |
| delCounts_persamp = count_deletions(batch = batch, |
| del_pos_mask = del_pos) |
| del del_pos |
| |
| |
| |
| |
| |
| |
| |
| 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) |
|
|
|
|
| |
| |
| |
| def get_aa_counts(seq_mat): |
| valid_aas = jnp.arange(3, 23) |
| |
| |
| 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') |
|
|
|
|
| |
| |
| |
| def get_equilib_counts(in_dir, split, gap_tok=43): |
| print(f'Working on split: {split}') |
| |
| |
| with open(f'{in_dir}/{split}_pair_alignments.npy', 'rb') as f: |
| |
| hmm_paths = np.load(f) |
| assert hmm_paths.shape[1] == 2324 |
| del f |
| |
| |
| 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 |
| |
| |
| |
| |
| non_gaps = np.where((hmm_paths != gap_tok) & (hmm_paths != 0), 1, 0) |
| |
| |
| |
| |
| 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 |
| |
| |
| |
| |
| 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}') |
| |
| dset = pairAlign_dloader(in_dir = in_dir, |
| split = split) |
| |
| |
| 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): |
| |
| |
| lens = (batch != 0).sum(axis=1)[:,0] |
| if i == 0: |
| jitted_summarize_alignment = jax.jit(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 |
| |
| |
| 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) |
| |
| |
| subCounts_lst.append(batch_subCounts_persamp) |
| insCounts_lst.append(batch_insCounts_persamp) |
| delCounts_lst.append(batch_delCounts_persamp) |
| transCounts_lst.append(batch_transCounts_persamp) |
| |
| |
| |
| 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 |
| |
| |
| |
| 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 |
| |
| |
| |
| |
| |
| |
| 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) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |