import os
import sys
sys.path.append(os.path.abspath("/users/amtseng/tfmodisco/src/"))
from tfmodisco.run_tfmodisco import import_shap_scores, import_tfmodisco_results
from motif.read_motifs import pfm_info_content, pfm_to_pwm, trim_motif_by_ic
from motif.match_motifs import match_motifs_to_database
from util import figure_to_vdom_image, import_peak_table, import_profiles
import plot.viz_sequence as viz_sequence
import numpy as np
import h5py
import pandas as pd
import sklearn.cluster
import scipy.cluster.hierarchy
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
import vdom.helpers as vdomh
from IPython.display import display
import tqdm
tqdm.tqdm_notebook()
/users/amtseng/miniconda3/envs/tfmodisco-mini/lib/python3.7/site-packages/ipykernel_launcher.py:19: TqdmDeprecationWarning: This function will be removed in tqdm==5.0.0 Please use `tqdm.notebook.tqdm` instead of `tqdm.tqdm_notebook`
<tqdm.notebook.tqdm_notebook at 0x7ff825fb89d0>
# Plotting defaults
font_manager.fontManager.ttflist.extend(
font_manager.createFontList(
font_manager.findSystemFonts(fontpaths="/users/amtseng/modules/fonts")
)
)
plot_params = {
"figure.titlesize": 22,
"axes.titlesize": 22,
"axes.labelsize": 20,
"legend.fontsize": 18,
"xtick.labelsize": 16,
"ytick.labelsize": 16,
"font.family": "Roboto",
"font.weight": "bold"
}
plt.rcParams.update(plot_params)
/users/amtseng/miniconda3/envs/tfmodisco-mini/lib/python3.7/site-packages/ipykernel_launcher.py:4: MatplotlibDeprecationWarning: The createFontList function was deprecated in Matplotlib 3.2 and will be removed two minor releases later. Use FontManager.addfont instead. after removing the cwd from sys.path.
# Define parameters/fetch arguments
tf_name = os.environ["TFM_TF_NAME"]
preds_path = os.environ["TFM_PRED_PATH"]
shap_scores_path = os.environ["TFM_SHAP_PATH"]
tfm_results_path = os.environ["TFM_TFM_PATH"]
if "TFM_TASK_INDEX" in os.environ:
task_index = int(os.environ["TFM_TASK_INDEX"])
else:
task_index = None
hyp_score_key = os.environ["TFM_HYP_SCORE_KEY"]
if "TFM_MOTIF_CACHE" in os.environ:
tfm_motifs_cache_dir = os.environ["TFM_MOTIF_CACHE"]
else:
tfm_motifs_cache_dir = None
print("TF name: %s" % tf_name)
print("Predictions/performance path: %s" % preds_path)
print("DeepSHAP scores path: %s" % shap_scores_path)
print("TF-MoDISco results path: %s" % tfm_results_path)
print("Task index: %s" % task_index)
print("Importance score key: %s" % hyp_score_key)
print("Saved TF-MoDISco-derived motifs cache: %s" % tfm_motifs_cache_dir)
TF name: MAFK Predictions/performance path: /users/amtseng/tfmodisco/results/peak_predictions/multitask_profile_finetune/MAFK_multitask_profile_finetune_fold7/MAFK_multitask_profile_finetune_fold7_pred_perf.h5 DeepSHAP scores path: /users/amtseng/tfmodisco/results/importance_scores/multitask_profile_finetune/MAFK_multitask_profile_finetune_fold7/MAFK_multitask_profile_finetune_task0_fold7_imp_scores.h5 TF-MoDISco results path: /users/amtseng/tfmodisco/results/tfmodisco/multitask_profile_finetune/MAFK_multitask_profile_finetune_fold7/MAFK_multitask_profile_finetune_task0_fold7_count_tfm.h5 Task index: 0 Importance score key: count_hyp_scores Saved TF-MoDISco-derived motifs cache: /users/amtseng/tfmodisco/results/reports/tfmodisco_results//cache/multitask_profile_finetune/MAFK_multitask_profile_finetune_fold7/MAFK_multitask_profile_finetune_task0_fold7_count
# Define constants
input_length, profile_length = 2114, 1000
shap_score_center_size = 400
profile_display_center_size = 400
base_path = "/users/amtseng/tfmodisco/"
data_path = os.path.join(base_path, "data/processed/ENCODE/")
labels_path = os.path.join(data_path, "labels/%s" % tf_name)
# Paths to original called peaks
all_peak_beds = sorted([item for item in os.listdir(labels_path) if item.endswith(".bed.gz")])
if task_index is None:
peak_bed_paths = [os.path.join(labels_path, item) for item in all_peak_beds]
else:
peak_bed_paths = [os.path.join(labels_path, all_peak_beds[task_index])]
if tfm_motifs_cache_dir:
os.makedirs(tfm_motifs_cache_dir, exist_ok=True)
def extract_profiles_and_coords(
seqlets_arr, one_hot_seqs, hyp_scores, true_profs, pred_profs, pred_coords,
input_length, profile_length, input_center_cut_size, profile_center_cut_size,
task_index=None
):
"""
From the seqlets object of a TF-MoDISco pattern's seqlets and alignments,
extracts the predicted and observed profiles of the model, as well as the
set of coordinates for the seqlets.
Arguments:
`seqlets_arr`: a TF-MoDISco pattern's seqlets object array (N-array)
`one_hot_seqs`: an N x R x 4 array of input sequences, where R is
the cut centered size
`hyp_scores`: an N x R x 4 array of hypothetical importance scores
`true_profs`: an N x T x O x 2 array of true profile counts
`pred_profs`: an N x T x O x 2 array of predicted profile probabilities
`pred_coords`: an N x 3 object array of coordinates for the input sequence
underlying the predictions
`input_length`: length of original input sequences, I
`profile_length`: length of profile predictions, O
`input_center_cut_size`: centered cut size of SHAP scores used
`profile_center_cut_size`: size to cut profiles to when returning them, P
`task_index`: index of task to focus on for profiles; if None, returns
profiles for all tasks
Returns an N x (T or 1) x P x 2 array of true profile counts, an
N x (T or 1) x P x 2 array of predicted profile probabilities, an N x Q x 4
array of one-hot seqlet sequences, an N x Q x 4 array of hypothetical seqlet
importance scores, and an N x 3 object array of seqlet coordinates, where P
is the profile cut size and Q is the seqlet length. Returned profiles are
centered at the same center as the seqlets.
Note that it is important that the seqlet indices match exactly with the indices
out of the N. This should be the exact sequences in the original SHAP scores.
"""
true_seqlet_profs, pred_seqlet_profs, seqlet_seqs, seqlet_hyps, seqlet_coords = [], [], [], [], []
def seqlet_coord_to_profile_coord(seqlet_coord):
return seqlet_coord + ((input_length - input_center_cut_size) // 2) - ((input_length - profile_length) // 2)
def seqlet_coord_to_input_coord(seqlet_coord):
return seqlet_coord + ((input_length - input_center_cut_size) // 2)
# For each seqlet, fetch the true/predicted profiles
for seqlet in seqlets_arr:
coord_index = seqlet.coor.example_idx
seqlet_start = seqlet.coor.start
seqlet_end = seqlet.coor.end
seqlet_rc = seqlet.coor.is_revcomp
# Get indices of profile to cut out
seqlet_center = (seqlet_start + seqlet_end) // 2
prof_center = seqlet_coord_to_profile_coord(seqlet_center)
prof_start = prof_center - (profile_center_cut_size // 2)
prof_end = prof_start + profile_center_cut_size
if task_index is None or true_profs.shape[1] == 1:
# Use all tasks if the predictions only have 1 task to begin with
task_start, task_end = None, None
else:
task_start, task_end = task_index, task_index + 1
true_prof = true_profs[coord_index, task_start:task_end, prof_start:prof_end] # (T or 1) x P x 2
pred_prof = pred_profs[coord_index, task_start:task_end, prof_start:prof_end] # (T or 1) x P x 2
true_seqlet_profs.append(true_prof)
pred_seqlet_profs.append(pred_prof)
# The one-hot-sequences and hypothetical scores are assumed to already by cut/centered,
# so the indices match the seqlet indices
if seqlet_rc:
seqlet_seqs.append(np.flip(one_hot_seqs[coord_index, seqlet_start:seqlet_end], axis=(0, 1)))
seqlet_hyps.append(np.flip(hyp_scores[coord_index, seqlet_start:seqlet_end], axis=(0, 1)))
else:
seqlet_seqs.append(one_hot_seqs[coord_index, seqlet_start:seqlet_end])
seqlet_hyps.append(hyp_scores[coord_index, seqlet_start:seqlet_end])
# Get the coordinates of the seqlet based on the input coordinates
inp_start = seqlet_coord_to_input_coord(seqlet_start)
inp_end = seqlet_coord_to_input_coord(seqlet_end)
chrom, start, _ = pred_coords[coord_index]
seqlet_coords.append([chrom, start + inp_start, start + inp_end])
return np.stack(true_seqlet_profs), np.stack(pred_seqlet_profs), np.stack(seqlet_seqs), np.stack(seqlet_hyps), np.array(seqlet_coords, dtype=object)
def plot_profiles(seqlet_true_profs, seqlet_pred_profs, kmeans_clusters=5, save_path=None):
"""
Plots the given profiles with a heatmap.
Arguments:
`seqlet_true_profs`: an N x O x 2 NumPy array of true profiles, either as raw
counts or probabilities (they will be normalized)
`seqlet_pred_profs`: an N x O x 2 NumPy array of predicted profiles, either as
raw counts or probabilities (they will be normalized)
`kmeans_cluster`: when displaying profile heatmaps, there will be this
many clusters
`save_path`: if provided, save the profile matrices here
Returns the figure.
"""
assert len(seqlet_true_profs.shape) == 3
assert seqlet_true_profs.shape == seqlet_pred_profs.shape
num_profs, width, _ = seqlet_true_profs.shape
# First, normalize the profiles along the output profile dimension
def normalize(arr, axis=0):
arr_sum = np.sum(arr, axis=axis, keepdims=True)
arr_sum[arr_sum == 0] = 1 # If 0, keep 0 as the quotient instead of dividing by 0
return arr / arr_sum
true_profs_norm = normalize(seqlet_true_profs, axis=1)
pred_profs_norm = normalize(seqlet_pred_profs, axis=1)
# Compute the mean profiles across all examples
true_profs_mean = np.mean(true_profs_norm, axis=0)
pred_profs_mean = np.mean(pred_profs_norm, axis=0)
# Perform k-means clustering on the predicted profiles, with the strands pooled
kmeans_clusters = max(5, num_profs // 50) # Set number of clusters based on number of profiles, with minimum
kmeans = sklearn.cluster.KMeans(n_clusters=kmeans_clusters)
cluster_assignments = kmeans.fit_predict(
np.reshape(pred_profs_norm, (pred_profs_norm.shape[0], -1))
)
# Perform hierarchical clustering on the cluster centers to determine optimal ordering
kmeans_centers = kmeans.cluster_centers_
cluster_order = scipy.cluster.hierarchy.leaves_list(
scipy.cluster.hierarchy.optimal_leaf_ordering(
scipy.cluster.hierarchy.linkage(kmeans_centers, method="centroid"), kmeans_centers
)
)
# Order the profiles so that the cluster assignments follow the optimal ordering
cluster_inds = []
for cluster_id in cluster_order:
cluster_inds.append(np.where(cluster_assignments == cluster_id)[0])
cluster_inds = np.concatenate(cluster_inds)
# Compute a matrix of profiles, normalized to the maximum height, ordered by clusters
def make_profile_matrix(flat_profs, order_inds):
matrix = flat_profs[order_inds]
maxes = np.max(matrix, axis=1, keepdims=True)
maxes[maxes == 0] = 1 # If 0, keep 0 as the quotient instead of dividing by 0
return matrix / maxes
true_matrix = make_profile_matrix(true_profs_norm, cluster_inds)
pred_matrix = make_profile_matrix(pred_profs_norm, cluster_inds)
if save_path:
np.savez_compressed(
true_profs_mean=true_profs_mean, pred_profs_mean=pred_profs_mean,
true_matrix=true_matrix, pred_matrix=pred_matrix
)
# Create a figure with the right dimensions
mean_height = 4
heatmap_height = min(num_profs * 0.004, 8)
fig_height = mean_height + (2 * heatmap_height)
fig, ax = plt.subplots(
3, 2, figsize=(16, fig_height), sharex=True,
gridspec_kw={
"width_ratios": [1, 1],
"height_ratios": [mean_height / fig_height, heatmap_height / fig_height, heatmap_height / fig_height]
}
)
# Plot the average predictions
ax[0, 0].plot(true_profs_mean[:, 0], color="darkslateblue")
ax[0, 0].plot(-true_profs_mean[:, 1], color="darkorange")
ax[0, 1].plot(pred_profs_mean[:, 0], color="darkslateblue")
ax[0, 1].plot(-pred_profs_mean[:, 1], color="darkorange")
# Set axes on average predictions
max_mean_val = max(np.max(true_profs_mean), np.max(pred_profs_mean))
mean_ylim = max_mean_val * 1.05 # Make 5% higher
ax[0, 0].set_title("True profiles")
ax[0, 0].set_ylabel("Average probability")
ax[0, 1].set_title("Predicted profiles")
for j in (0, 1):
ax[0, j].set_ylim(-mean_ylim, mean_ylim)
ax[0, j].label_outer()
# Plot the heatmaps
ax[1, 0].imshow(true_matrix[:, :, 0], interpolation="nearest", aspect="auto", cmap="Blues")
ax[1, 1].imshow(pred_matrix[:, :, 0], interpolation="nearest", aspect="auto", cmap="Blues")
ax[2, 0].imshow(true_matrix[:, :, 1], interpolation="nearest", aspect="auto", cmap="Oranges")
ax[2, 1].imshow(pred_matrix[:, :, 1], interpolation="nearest", aspect="auto", cmap="Oranges")
# Set axes on heatmaps
for i in (1, 2):
for j in (0, 1):
ax[i, j].set_yticks([])
ax[i, j].set_yticklabels([])
ax[i, j].label_outer()
width = true_matrix.shape[1]
delta = 100
num_deltas = (width // 2) // delta
labels = list(range(max(-width // 2, -num_deltas * delta), min(width // 2, num_deltas * delta) + 1, delta))
tick_locs = [label + max(width // 2, num_deltas * delta) for label in labels]
for j in (0, 1):
ax[2, j].set_xticks(tick_locs)
ax[2, j].set_xticklabels(labels)
ax[2, j].set_xlabel("Distance from seqlet center (bp)")
fig.tight_layout()
plt.show()
return fig
def get_summit_distances(coords, peak_table):
"""
Given a set of coordinates, computes the distance of the center of each
coordinate to the nearest summit.
Arguments:
`coords`: an N x 3 object array of coordinates
`peak_table`: a 10-column table of peak data, as imported by
`import_peak_table`
Returns and N-array of integers, which is the distance of each coordinate
midpoint to the nearest coordinate.
"""
chroms = coords[:, 0]
midpoints = (coords[:, 1] + coords[:, 2]) // 2
dists = []
for i in range(len(coords)):
chrom = chroms[i]
midpoint = midpoints[i]
rows = peak_table[peak_table["chrom"] == chrom]
dist_arr = (midpoint - rows["summit"]).values
min_dist = dist_arr[np.argmin(np.abs(dist_arr))]
dists.append(min_dist)
return np.array(dists)
def plot_summit_dists(summit_dists):
"""
Plots the distribution of seqlet distances to summits.
Arguments:
`summit_dists`: the array of distances as returned by
`get_summit_distances`
Returns the figure.
"""
fig = plt.figure(figsize=(8, 6))
num_bins = max(len(summit_dists) // 30, 20)
plt.hist(summit_dists, bins=num_bins, color="purple")
plt.title("Histogram of distance of seqlets to peak summits")
plt.xlabel("Signed distance from seqlet center to nearest peak summit (bp)")
plt.show()
return fig
# Import SHAP coordinates and one-hot sequences
hyp_scores, _, one_hot_seqs, shap_coords = import_shap_scores(shap_scores_path, hyp_score_key, center_cut_size=shap_score_center_size)
# This cuts the sequences/scores off just as how TF-MoDISco saw them, but the coordinates are uncut
Importing SHAP scores: 0%| | 0/7 [00:00<?, ?it/s] Importing SHAP scores: 14%|█▍ | 1/7 [00:03<00:23, 3.99s/it] Importing SHAP scores: 29%|██▊ | 2/7 [00:04<00:10, 2.04s/it] Importing SHAP scores: 43%|████▎ | 3/7 [00:05<00:05, 1.35s/it] Importing SHAP scores: 57%|█████▋ | 4/7 [00:05<00:03, 1.07s/it] Importing SHAP scores: 71%|███████▏ | 5/7 [00:09<00:03, 1.94s/it] Importing SHAP scores: 86%|████████▌ | 6/7 [00:10<00:01, 1.52s/it] Importing SHAP scores: 100%|██████████| 7/7 [00:10<00:00, 1.49s/it]
# Import the set of all profiles and their coordinates
true_profs, pred_profs, all_pred_coords = import_profiles(preds_path)
Importing predictions: 0%| | 0/311 [00:00<?, ?it/s] Importing predictions: 0%| | 1/311 [00:04<24:22, 4.72s/it] Importing predictions: 1%| | 2/311 [00:18<52:01, 10.10s/it] Importing predictions: 1%| | 3/311 [00:38<1:13:50, 14.38s/it] Importing predictions: 1%|▏ | 4/311 [00:58<1:24:56, 16.60s/it] Importing predictions: 2%|▏ | 5/311 [01:30<1:54:34, 22.46s/it] Importing predictions: 2%|▏ | 6/311 [01:50<1:48:47, 21.40s/it] Importing predictions: 2%|▏ | 7/311 [02:10<1:45:51, 20.89s/it] Importing predictions: 3%|▎ | 8/311 [02:34<1:51:37, 22.11s/it] Importing predictions: 3%|▎ | 9/311 [02:53<1:46:27, 21.15s/it] Importing predictions: 3%|▎ | 10/311 [03:20<1:54:01, 22.73s/it] Importing predictions: 4%|▎ | 11/311 [03:42<1:53:25, 22.68s/it] Importing predictions: 4%|▍ | 12/311 [04:03<1:50:22, 22.15s/it] Importing predictions: 4%|▍ | 13/311 [04:21<1:44:15, 20.99s/it] Importing predictions: 5%|▍ | 14/311 [04:43<1:44:56, 21.20s/it] Importing predictions: 5%|▍ | 15/311 [05:06<1:46:30, 21.59s/it] Importing predictions: 5%|▌ | 16/311 [05:29<1:49:16, 22.23s/it] Importing predictions: 5%|▌ | 17/311 [05:47<1:41:39, 20.75s/it] Importing predictions: 6%|▌ | 18/311 [06:06<1:39:13, 20.32s/it] Importing predictions: 6%|▌ | 19/311 [06:25<1:36:48, 19.89s/it] Importing predictions: 6%|▋ | 20/311 [06:50<1:44:36, 21.57s/it] Importing predictions: 7%|▋ | 21/311 [07:10<1:42:07, 21.13s/it] Importing predictions: 7%|▋ | 22/311 [07:37<1:49:50, 22.81s/it] Importing predictions: 7%|▋ | 23/311 [08:01<1:50:56, 23.11s/it] Importing predictions: 8%|▊ | 24/311 [08:26<1:53:36, 23.75s/it] Importing predictions: 8%|▊ | 25/311 [08:53<1:57:51, 24.73s/it] Importing predictions: 8%|▊ | 26/311 [09:18<1:57:34, 24.75s/it] Importing predictions: 9%|▊ | 27/311 [09:40<1:52:44, 23.82s/it] Importing predictions: 9%|▉ | 28/311 [10:03<1:51:06, 23.56s/it] Importing predictions: 9%|▉ | 29/311 [10:27<1:51:37, 23.75s/it] Importing predictions: 10%|▉ | 30/311 [10:55<1:57:51, 25.17s/it] Importing predictions: 10%|▉ | 31/311 [11:12<1:46:05, 22.73s/it] Importing predictions: 10%|█ | 32/311 [11:33<1:42:35, 22.06s/it] Importing predictions: 11%|█ | 33/311 [11:52<1:38:10, 21.19s/it] Importing predictions: 11%|█ | 34/311 [12:14<1:38:15, 21.28s/it] Importing predictions: 11%|█▏ | 35/311 [12:43<1:49:40, 23.84s/it] Importing predictions: 12%|█▏ | 36/311 [13:03<1:43:18, 22.54s/it] Importing predictions: 12%|█▏ | 37/311 [13:30<1:49:25, 23.96s/it] Importing predictions: 12%|█▏ | 38/311 [13:59<1:55:31, 25.39s/it] Importing predictions: 13%|█▎ | 39/311 [14:34<2:08:04, 28.25s/it] Importing predictions: 13%|█▎ | 40/311 [14:42<1:40:53, 22.34s/it] Importing predictions: 13%|█▎ | 41/311 [14:47<1:16:06, 16.91s/it] Importing predictions: 14%|█▎ | 42/311 [14:57<1:06:29, 14.83s/it] Importing predictions: 14%|█▍ | 43/311 [15:02<53:47, 12.04s/it] Importing predictions: 14%|█▍ | 44/311 [15:14<53:25, 12.00s/it] Importing predictions: 14%|█▍ | 45/311 [15:24<49:56, 11.26s/it] Importing predictions: 15%|█▍ | 46/311 [15:33<46:58, 10.64s/it] Importing predictions: 15%|█▌ | 47/311 [15:45<49:16, 11.20s/it] Importing predictions: 15%|█▌ | 48/311 [15:53<44:32, 10.16s/it] Importing predictions: 16%|█▌ | 49/311 [16:02<42:43, 9.79s/it] Importing predictions: 16%|█▌ | 50/311 [16:07<36:28, 8.38s/it] Importing predictions: 16%|█▋ | 51/311 [16:13<32:42, 7.55s/it] Importing predictions: 17%|█▋ | 52/311 [16:21<33:41, 7.81s/it] Importing predictions: 17%|█▋ | 53/311 [16:29<33:53, 7.88s/it] Importing predictions: 17%|█▋ | 54/311 [16:37<34:22, 8.03s/it] Importing predictions: 18%|█▊ | 55/311 [16:42<29:28, 6.91s/it] Importing predictions: 18%|█▊ | 56/311 [16:51<32:22, 7.62s/it] Importing predictions: 18%|█▊ | 57/311 [17:00<33:40, 7.96s/it] Importing predictions: 19%|█▊ | 58/311 [17:06<31:43, 7.52s/it] Importing predictions: 19%|█▉ | 59/311 [17:18<36:37, 8.72s/it] Importing predictions: 19%|█▉ | 60/311 [17:26<35:41, 8.53s/it] Importing predictions: 20%|█▉ | 61/311 [17:34<34:43, 8.34s/it] Importing predictions: 20%|█▉ | 62/311 [17:39<30:24, 7.33s/it] Importing predictions: 20%|██ | 63/311 [17:44<27:57, 6.76s/it] Importing predictions: 21%|██ | 64/311 [17:53<30:20, 7.37s/it] Importing predictions: 21%|██ | 65/311 [17:56<25:19, 6.18s/it] Importing predictions: 21%|██ | 66/311 [18:03<26:10, 6.41s/it] Importing predictions: 22%|██▏ | 67/311 [18:07<22:23, 5.51s/it] Importing predictions: 22%|██▏ | 68/311 [18:10<19:41, 4.86s/it] Importing predictions: 22%|██▏ | 69/311 [18:17<21:56, 5.44s/it] Importing predictions: 23%|██▎ | 70/311 [18:20<19:23, 4.83s/it] Importing predictions: 23%|██▎ | 71/311 [18:27<21:47, 5.45s/it] Importing predictions: 23%|██▎ | 72/311 [18:30<19:14, 4.83s/it] Importing predictions: 23%|██▎ | 73/311 [18:37<21:39, 5.46s/it] Importing predictions: 24%|██▍ | 74/311 [18:41<19:10, 4.85s/it] Importing predictions: 24%|██▍ | 75/311 [18:44<17:18, 4.40s/it] Importing predictions: 24%|██▍ | 76/311 [18:51<20:12, 5.16s/it] Importing predictions: 25%|██▍ | 77/311 [18:54<17:59, 4.62s/it] Importing predictions: 25%|██▌ | 78/311 [19:03<22:47, 5.87s/it] Importing predictions: 25%|██▌ | 79/311 [19:07<20:33, 5.32s/it] Importing predictions: 26%|██▌ | 80/311 [19:19<28:14, 7.34s/it] Importing predictions: 26%|██▌ | 81/311 [19:51<55:32, 14.49s/it] Importing predictions: 26%|██▋ | 82/311 [20:14<1:05:29, 17.16s/it] Importing predictions: 27%|██▋ | 83/311 [20:40<1:15:12, 19.79s/it] Importing predictions: 27%|██▋ | 84/311 [21:04<1:19:20, 20.97s/it] Importing predictions: 27%|██▋ | 85/311 [21:34<1:30:08, 23.93s/it] Importing predictions: 28%|██▊ | 86/311 [22:05<1:37:44, 26.06s/it] Importing predictions: 28%|██▊ | 87/311 [22:32<1:37:55, 26.23s/it] Importing predictions: 28%|██▊ | 88/311 [23:05<1:45:19, 28.34s/it] Importing predictions: 29%|██▊ | 89/311 [23:31<1:41:39, 27.47s/it] Importing predictions: 29%|██▉ | 90/311 [24:01<1:44:27, 28.36s/it] Importing predictions: 29%|██▉ | 91/311 [24:29<1:43:05, 28.12s/it] Importing predictions: 30%|██▉ | 92/311 [24:52<1:37:09, 26.62s/it] Importing predictions: 30%|██▉ | 93/311 [25:22<1:40:03, 27.54s/it] Importing predictions: 30%|███ | 94/311 [25:49<1:39:04, 27.39s/it] Importing predictions: 31%|███ | 95/311 [26:25<1:48:43, 30.20s/it] Importing predictions: 31%|███ | 96/311 [26:55<1:48:06, 30.17s/it] Importing predictions: 31%|███ | 97/311 [27:12<1:33:15, 26.15s/it] Importing predictions: 32%|███▏ | 98/311 [27:24<1:17:30, 21.83s/it] Importing predictions: 32%|███▏ | 99/311 [27:27<57:40, 16.32s/it] Importing predictions: 32%|███▏ | 100/311 [27:36<48:40, 13.84s/it] Importing predictions: 32%|███▏ | 101/311 [27:39<37:29, 10.71s/it] Importing predictions: 33%|███▎ | 102/311 [27:52<40:02, 11.50s/it] Importing predictions: 33%|███▎ | 103/311 [28:21<58:16, 16.81s/it] Importing predictions: 33%|███▎ | 104/311 [28:41<1:01:03, 17.70s/it] Importing predictions: 34%|███▍ | 105/311 [29:11<1:12:49, 21.21s/it] Importing predictions: 34%|███▍ | 106/311 [29:33<1:13:50, 21.61s/it] Importing predictions: 34%|███▍ | 107/311 [30:08<1:27:23, 25.70s/it] Importing predictions: 35%|███▍ | 108/311 [30:37<1:29:41, 26.51s/it] Importing predictions: 35%|███▌ | 109/311 [30:52<1:17:29, 23.02s/it] Importing predictions: 35%|███▌ | 110/311 [31:05<1:07:27, 20.14s/it] Importing predictions: 36%|███▌ | 111/311 [31:13<54:23, 16.32s/it] Importing predictions: 36%|███▌ | 112/311 [31:19<44:30, 13.42s/it] Importing predictions: 36%|███▋ | 113/311 [31:23<34:42, 10.52s/it] Importing predictions: 37%|███▋ | 114/311 [31:27<27:52, 8.49s/it] Importing predictions: 37%|███▋ | 115/311 [31:34<26:57, 8.25s/it] Importing predictions: 37%|███▋ | 116/311 [31:38<22:06, 6.80s/it] Importing predictions: 38%|███▊ | 117/311 [31:45<22:41, 7.02s/it] Importing predictions: 38%|███▊ | 118/311 [31:54<23:46, 7.39s/it] Importing predictions: 38%|███▊ | 119/311 [32:04<26:49, 8.38s/it] Importing predictions: 39%|███▊ | 120/311 [32:26<39:15, 12.33s/it] Importing predictions: 39%|███▉ | 121/311 [32:29<30:44, 9.71s/it] Importing predictions: 39%|███▉ | 122/311 [32:36<27:51, 8.84s/it] Importing predictions: 40%|███▉ | 123/311 [32:43<26:13, 8.37s/it] Importing predictions: 40%|███▉ | 124/311 [32:51<25:34, 8.21s/it] Importing predictions: 40%|████ | 125/311 [32:55<20:56, 6.76s/it] Importing predictions: 41%|████ | 126/311 [32:59<18:51, 6.12s/it] Importing predictions: 41%|████ | 127/311 [33:08<21:32, 7.03s/it] Importing predictions: 41%|████ | 128/311 [33:12<18:34, 6.09s/it] Importing predictions: 41%|████▏ | 129/311 [33:26<24:53, 8.21s/it] Importing predictions: 42%|████▏ | 130/311 [33:38<28:36, 9.48s/it] Importing predictions: 42%|████▏ | 131/311 [34:01<40:43, 13.57s/it] Importing predictions: 42%|████▏ | 132/311 [34:28<52:00, 17.43s/it] Importing predictions: 43%|████▎ | 133/311 [34:38<45:16, 15.26s/it] Importing predictions: 43%|████▎ | 134/311 [34:57<48:48, 16.54s/it] Importing predictions: 43%|████▎ | 135/311 [35:21<54:28, 18.57s/it] Importing predictions: 44%|████▎ | 136/311 [35:46<1:00:17, 20.67s/it] Importing predictions: 44%|████▍ | 137/311 [36:07<1:00:22, 20.82s/it] Importing predictions: 44%|████▍ | 138/311 [36:13<47:07, 16.35s/it] Importing predictions: 45%|████▍ | 139/311 [36:21<39:12, 13.68s/it] Importing predictions: 45%|████▌ | 140/311 [36:24<30:11, 10.60s/it] Importing predictions: 45%|████▌ | 141/311 [36:43<37:09, 13.12s/it] Importing predictions: 46%|████▌ | 142/311 [36:49<30:46, 10.93s/it] Importing predictions: 46%|████▌ | 143/311 [36:52<24:15, 8.67s/it] Importing predictions: 46%|████▋ | 144/311 [36:59<22:41, 8.15s/it] Importing predictions: 47%|████▋ | 145/311 [37:03<19:19, 6.98s/it] Importing predictions: 47%|████▋ | 146/311 [37:18<25:24, 9.24s/it] Importing predictions: 47%|████▋ | 147/311 [37:41<36:31, 13.36s/it] Importing predictions: 48%|████▊ | 148/311 [38:01<41:35, 15.31s/it] Importing predictions: 48%|████▊ | 149/311 [38:30<52:30, 19.45s/it] Importing predictions: 48%|████▊ | 150/311 [38:48<50:57, 18.99s/it] Importing predictions: 49%|████▊ | 151/311 [39:19<1:00:02, 22.51s/it] Importing predictions: 49%|████▉ | 152/311 [39:38<56:57, 21.49s/it] Importing predictions: 49%|████▉ | 153/311 [40:01<58:14, 22.12s/it] Importing predictions: 50%|████▉ | 154/311 [40:34<1:05:50, 25.16s/it] Importing predictions: 50%|████▉ | 155/311 [41:01<1:06:52, 25.72s/it] Importing predictions: 50%|█████ | 156/311 [41:30<1:09:30, 26.91s/it] Importing predictions: 50%|█████ | 157/311 [42:00<1:11:18, 27.78s/it] Importing predictions: 51%|█████ | 158/311 [42:38<1:18:46, 30.89s/it] Importing predictions: 51%|█████ | 159/311 [43:02<1:12:48, 28.74s/it] Importing predictions: 51%|█████▏ | 160/311 [43:06<53:28, 21.25s/it] Importing predictions: 52%|█████▏ | 161/311 [43:18<46:21, 18.54s/it] Importing predictions: 52%|█████▏ | 162/311 [43:29<40:08, 16.16s/it] Importing predictions: 52%|█████▏ | 163/311 [44:01<52:18, 21.20s/it] Importing predictions: 53%|█████▎ | 164/311 [44:30<57:37, 23.52s/it] Importing predictions: 53%|█████▎ | 165/311 [44:57<59:19, 24.38s/it] Importing predictions: 53%|█████▎ | 166/311 [45:27<1:03:06, 26.12s/it] Importing predictions: 54%|█████▎ | 167/311 [45:49<59:39, 24.86s/it] Importing predictions: 54%|█████▍ | 168/311 [46:13<58:46, 24.66s/it] Importing predictions: 54%|█████▍ | 169/311 [46:42<1:01:37, 26.04s/it] Importing predictions: 55%|█████▍ | 170/311 [46:59<54:54, 23.37s/it] Importing predictions: 55%|█████▍ | 171/311 [47:10<45:46, 19.62s/it] Importing predictions: 55%|█████▌ | 172/311 [47:14<34:41, 14.97s/it] Importing predictions: 56%|█████▌ | 173/311 [47:22<29:16, 12.73s/it] Importing predictions: 56%|█████▌ | 174/311 [47:25<22:41, 9.94s/it] Importing predictions: 56%|█████▋ | 175/311 [47:35<22:12, 9.80s/it] Importing predictions: 57%|█████▋ | 176/311 [47:38<17:43, 7.88s/it] Importing predictions: 57%|█████▋ | 177/311 [47:42<14:35, 6.53s/it] Importing predictions: 57%|█████▋ | 178/311 [47:49<14:52, 6.71s/it] Importing predictions: 58%|█████▊ | 179/311 [47:52<12:36, 5.73s/it] Importing predictions: 58%|█████▊ | 180/311 [48:00<13:48, 6.33s/it] Importing predictions: 58%|█████▊ | 181/311 [48:04<12:12, 5.64s/it] Importing predictions: 59%|█████▊ | 182/311 [48:08<10:56, 5.09s/it] Importing predictions: 59%|█████▉ | 183/311 [48:15<12:19, 5.77s/it] Importing predictions: 59%|█████▉ | 184/311 [48:19<11:01, 5.21s/it] Importing predictions: 59%|█████▉ | 185/311 [48:27<12:46, 6.09s/it] Importing predictions: 60%|█████▉ | 186/311 [48:31<11:27, 5.50s/it] Importing predictions: 60%|██████ | 187/311 [48:35<10:21, 5.01s/it] Importing predictions: 60%|██████ | 188/311 [48:50<16:18, 7.95s/it] Importing predictions: 61%|██████ | 189/311 [48:54<13:28, 6.63s/it] Importing predictions: 61%|██████ | 190/311 [49:08<18:16, 9.07s/it] Importing predictions: 61%|██████▏ | 191/311 [49:12<14:56, 7.47s/it] Importing predictions: 62%|██████▏ | 192/311 [49:20<14:57, 7.54s/it] Importing predictions: 62%|██████▏ | 193/311 [49:24<12:42, 6.46s/it] Importing predictions: 62%|██████▏ | 194/311 [49:28<11:04, 5.68s/it] Importing predictions: 63%|██████▎ | 195/311 [49:36<12:22, 6.40s/it] Importing predictions: 63%|██████▎ | 196/311 [49:39<10:47, 5.63s/it] Importing predictions: 63%|██████▎ | 197/311 [49:47<12:01, 6.33s/it] Importing predictions: 64%|██████▎ | 198/311 [49:51<10:22, 5.50s/it] Importing predictions: 64%|██████▍ | 199/311 [49:55<09:12, 4.93s/it] Importing predictions: 64%|██████▍ | 200/311 [50:02<10:35, 5.72s/it] Importing predictions: 65%|██████▍ | 201/311 [50:06<09:23, 5.13s/it] Importing predictions: 65%|██████▍ | 202/311 [50:15<11:17, 6.21s/it] Importing predictions: 65%|██████▌ | 203/311 [50:19<09:54, 5.51s/it] Importing predictions: 66%|██████▌ | 204/311 [50:23<09:11, 5.15s/it] Importing predictions: 66%|██████▌ | 205/311 [50:31<10:49, 6.13s/it] Importing predictions: 66%|██████▌ | 206/311 [50:36<10:02, 5.73s/it] Importing predictions: 67%|██████▋ | 207/311 [50:47<12:50, 7.41s/it] Importing predictions: 67%|██████▋ | 208/311 [50:55<12:35, 7.33s/it] Importing predictions: 67%|██████▋ | 209/311 [51:08<15:43, 9.25s/it] Importing predictions: 68%|██████▊ | 210/311 [51:14<13:50, 8.22s/it] Importing predictions: 68%|██████▊ | 211/311 [51:21<13:13, 7.93s/it] Importing predictions: 68%|██████▊ | 212/311 [51:38<17:32, 10.63s/it] Importing predictions: 68%|██████▊ | 213/311 [51:45<15:37, 9.57s/it] Importing predictions: 69%|██████▉ | 214/311 [51:58<16:52, 10.44s/it] Importing predictions: 69%|██████▉ | 215/311 [52:07<15:51, 9.92s/it] Importing predictions: 69%|██████▉ | 216/311 [52:20<17:24, 11.00s/it] Importing predictions: 70%|██████▉ | 217/311 [52:36<19:35, 12.50s/it] Importing predictions: 70%|███████ | 218/311 [52:45<17:43, 11.43s/it] Importing predictions: 70%|███████ | 219/311 [52:59<18:55, 12.34s/it] Importing predictions: 71%|███████ | 220/311 [53:08<17:00, 11.21s/it] Importing predictions: 71%|███████ | 221/311 [53:16<15:19, 10.22s/it] Importing predictions: 71%|███████▏ | 222/311 [53:31<17:24, 11.74s/it] Importing predictions: 72%|███████▏ | 223/311 [53:39<15:23, 10.49s/it] Importing predictions: 72%|███████▏ | 224/311 [53:55<17:47, 12.27s/it] Importing predictions: 72%|███████▏ | 225/311 [54:02<15:04, 10.52s/it] Importing predictions: 73%|███████▎ | 226/311 [54:16<16:25, 11.59s/it] Importing predictions: 73%|███████▎ | 227/311 [54:24<14:40, 10.48s/it] Importing predictions: 73%|███████▎ | 228/311 [54:31<13:11, 9.53s/it] Importing predictions: 74%|███████▎ | 229/311 [54:47<15:52, 11.62s/it] Importing predictions: 74%|███████▍ | 230/311 [54:55<14:05, 10.43s/it] Importing predictions: 74%|███████▍ | 231/311 [55:08<15:03, 11.30s/it] Importing predictions: 75%|███████▍ | 232/311 [55:15<13:05, 9.95s/it] Importing predictions: 75%|███████▍ | 233/311 [55:22<11:38, 8.95s/it] Importing predictions: 75%|███████▌ | 234/311 [55:37<13:43, 10.69s/it] Importing predictions: 76%|███████▌ | 235/311 [55:46<12:55, 10.20s/it] Importing predictions: 76%|███████▌ | 236/311 [56:03<15:34, 12.46s/it] Importing predictions: 76%|███████▌ | 237/311 [56:16<15:34, 12.62s/it] Importing predictions: 77%|███████▋ | 238/311 [56:24<13:21, 10.98s/it] Importing predictions: 77%|███████▋ | 239/311 [56:44<16:30, 13.76s/it] Importing predictions: 77%|███████▋ | 240/311 [56:53<14:34, 12.31s/it] Importing predictions: 77%|███████▋ | 241/311 [57:07<15:02, 12.89s/it] Importing predictions: 78%|███████▊ | 242/311 [57:14<12:55, 11.24s/it] Importing predictions: 78%|███████▊ | 243/311 [57:27<13:09, 11.61s/it] Importing predictions: 78%|███████▊ | 244/311 [57:35<11:42, 10.48s/it] Importing predictions: 79%|███████▉ | 245/311 [57:41<10:07, 9.20s/it] Importing predictions: 79%|███████▉ | 246/311 [57:56<11:45, 10.85s/it] Importing predictions: 79%|███████▉ | 247/311 [58:02<10:09, 9.53s/it] Importing predictions: 80%|███████▉ | 248/311 [58:12<10:13, 9.75s/it] Importing predictions: 80%|████████ | 249/311 [58:20<09:26, 9.14s/it] Importing predictions: 80%|████████ | 250/311 [58:29<09:06, 8.96s/it] Importing predictions: 81%|████████ | 251/311 [58:45<11:13, 11.22s/it] Importing predictions: 81%|████████ | 252/311 [58:52<09:41, 9.86s/it] Importing predictions: 81%|████████▏ | 253/311 [59:03<09:49, 10.16s/it] Importing predictions: 82%|████████▏ | 254/311 [59:07<08:07, 8.55s/it] Importing predictions: 82%|████████▏ | 255/311 [59:13<07:05, 7.60s/it] Importing predictions: 82%|████████▏ | 256/311 [59:25<08:06, 8.85s/it] Importing predictions: 83%|████████▎ | 257/311 [59:30<07:01, 7.80s/it] Importing predictions: 83%|████████▎ | 258/311 [59:42<07:54, 8.95s/it] Importing predictions: 83%|████████▎ | 259/311 [59:47<06:51, 7.91s/it] Importing predictions: 84%|████████▎ | 260/311 [59:57<07:20, 8.63s/it] Importing predictions: 84%|████████▍ | 261/311 [1:00:11<08:24, 10.09s/it] Importing predictions: 84%|████████▍ | 262/311 [1:00:27<09:41, 11.87s/it] Importing predictions: 85%|████████▍ | 263/311 [1:00:45<10:58, 13.72s/it] Importing predictions: 85%|████████▍ | 264/311 [1:00:52<09:05, 11.61s/it] Importing predictions: 85%|████████▌ | 265/311 [1:01:04<09:03, 11.82s/it] Importing predictions: 86%|████████▌ | 266/311 [1:01:09<07:18, 9.74s/it] Importing predictions: 86%|████████▌ | 267/311 [1:01:15<06:24, 8.75s/it] Importing predictions: 86%|████████▌ | 268/311 [1:01:26<06:49, 9.52s/it] Importing predictions: 86%|████████▋ | 269/311 [1:01:32<05:43, 8.18s/it] Importing predictions: 87%|████████▋ | 270/311 [1:01:46<06:47, 9.94s/it] Importing predictions: 87%|████████▋ | 271/311 [1:01:53<06:03, 9.08s/it] Importing predictions: 87%|████████▋ | 272/311 [1:02:01<05:40, 8.73s/it] Importing predictions: 88%|████████▊ | 273/311 [1:02:19<07:26, 11.75s/it] Importing predictions: 88%|████████▊ | 274/311 [1:02:25<06:09, 9.99s/it] Importing predictions: 88%|████████▊ | 275/311 [1:02:38<06:28, 10.80s/it] Importing predictions: 89%|████████▊ | 276/311 [1:02:44<05:28, 9.38s/it] Importing predictions: 89%|████████▉ | 277/311 [1:02:54<05:27, 9.62s/it] Importing predictions: 89%|████████▉ | 278/311 [1:03:00<04:41, 8.53s/it] Importing predictions: 90%|████████▉ | 279/311 [1:03:06<04:07, 7.72s/it] Importing predictions: 90%|█████████ | 280/311 [1:03:17<04:33, 8.82s/it] Importing predictions: 90%|█████████ | 281/311 [1:03:22<03:50, 7.67s/it] Importing predictions: 91%|█████████ | 282/311 [1:03:33<04:09, 8.61s/it] Importing predictions: 91%|█████████ | 283/311 [1:03:46<04:34, 9.79s/it] Importing predictions: 91%|█████████▏| 284/311 [1:03:57<04:33, 10.12s/it] Importing predictions: 92%|█████████▏| 285/311 [1:04:21<06:13, 14.35s/it] Importing predictions: 92%|█████████▏| 286/311 [1:04:31<05:30, 13.23s/it] Importing predictions: 92%|█████████▏| 287/311 [1:04:44<05:10, 12.93s/it] Importing predictions: 93%|█████████▎| 288/311 [1:04:51<04:16, 11.17s/it] Importing predictions: 93%|█████████▎| 289/311 [1:05:01<04:02, 11.01s/it] Importing predictions: 93%|█████████▎| 290/311 [1:05:21<04:46, 13.63s/it] Importing predictions: 94%|█████████▎| 291/311 [1:05:29<03:57, 11.89s/it] Importing predictions: 94%|█████████▍| 292/311 [1:05:42<03:50, 12.13s/it] Importing predictions: 94%|█████████▍| 293/311 [1:05:49<03:11, 10.62s/it] Importing predictions: 95%|█████████▍| 294/311 [1:06:02<03:15, 11.51s/it] Importing predictions: 95%|█████████▍| 295/311 [1:06:09<02:41, 10.09s/it] Importing predictions: 95%|█████████▌| 296/311 [1:06:15<02:12, 8.80s/it] Importing predictions: 95%|█████████▌| 297/311 [1:06:31<02:32, 10.92s/it] Importing predictions: 96%|█████████▌| 298/311 [1:06:38<02:05, 9.69s/it] Importing predictions: 96%|█████████▌| 299/311 [1:06:52<02:13, 11.15s/it] Importing predictions: 96%|█████████▋| 300/311 [1:07:00<01:51, 10.17s/it] Importing predictions: 97%|█████████▋| 301/311 [1:07:19<02:06, 12.70s/it] Importing predictions: 97%|█████████▋| 302/311 [1:07:32<01:55, 12.85s/it] Importing predictions: 97%|█████████▋| 303/311 [1:07:38<01:25, 10.72s/it] Importing predictions: 98%|█████████▊| 304/311 [1:07:49<01:16, 10.90s/it] Importing predictions: 98%|█████████▊| 305/311 [1:07:55<00:56, 9.48s/it] Importing predictions: 98%|█████████▊| 306/311 [1:08:01<00:41, 8.29s/it] Importing predictions: 99%|█████████▊| 307/311 [1:08:11<00:35, 8.93s/it] Importing predictions: 99%|█████████▉| 308/311 [1:08:16<00:23, 7.86s/it] Importing predictions: 99%|█████████▉| 309/311 [1:08:27<00:17, 8.62s/it] Importing predictions: 100%|█████████▉| 310/311 [1:08:36<00:08, 8.66s/it] Importing predictions: 100%|██████████| 311/311 [1:09:01<00:00, 13.32s/it]
# Import the set of peaks
peak_table = import_peak_table(peak_bed_paths)
# Subset the predicted profiles/coordinates to the task-specific SHAP coordinates/scores
shap_coords_table = pd.DataFrame(shap_coords, columns=["chrom", "start", "end"])
pred_coords_table = pd.DataFrame(all_pred_coords, columns=["chrom", "start", "end"])
subset_inds = pred_coords_table.reset_index().drop_duplicates(["chrom", "start", "end"]).merge(
shap_coords_table.reset_index(), on=["chrom", "start", "end"]
).sort_values("index_y")["index_x"].values
true_profs = true_profs[subset_inds]
pred_profs = pred_profs[subset_inds]
pred_coords = all_pred_coords[subset_inds]
# Make sure the coordinates all match
assert np.all(pred_coords == shap_coords)
# Import the TF-MoDISco results object
tfm_obj = import_tfmodisco_results(tfm_results_path, hyp_scores, one_hot_seqs, shap_score_center_size)
Plot the central region of some randomly selected actual importance scores
plot_slice = slice(int(shap_score_center_size / 4), int(3 * shap_score_center_size / 4))
for index in np.random.choice(hyp_scores.shape[0], size=5, replace=False):
viz_sequence.plot_weights((hyp_scores[index] * one_hot_seqs[index])[plot_slice], subticks_frequency=100)
motif_pfms, motif_hcwms, motif_cwms = [], [], [] # Save the trimmed PFMs, hCWMs, and CWMs
motif_pfms_short = [] # PFMs that are even more trimmed (for TOMTOM)
num_seqlets = [] # Number of seqlets for each motif
motif_seqlets = [] # Save seqlets of each motif
if tfm_motifs_cache_dir:
motif_hdf5 = h5py.File(os.path.join(tfm_motifs_cache_dir, "all_motifs.h5"), "w")
metaclusters = tfm_obj.metacluster_idx_to_submetacluster_results
num_metaclusters = len(metaclusters.keys())
for metacluster_i, metacluster_key in enumerate(metaclusters.keys()):
metacluster = metaclusters[metacluster_key]
display(vdomh.h3("Metacluster %d/%d" % (metacluster_i + 1, num_metaclusters)))
patterns = metacluster.seqlets_to_patterns_result.patterns
if not patterns:
break
motif_pfms.append([])
motif_hcwms.append([])
motif_cwms.append([])
motif_pfms_short.append([])
num_seqlets.append([])
motif_seqlets.append([])
num_patterns = len(patterns)
for pattern_i, pattern in enumerate(patterns):
seqlets = pattern.seqlets
display(vdomh.h4("Pattern %d/%d" % (pattern_i + 1, num_patterns)))
display(vdomh.p("%d seqlets" % len(seqlets)))
pfm = pattern["sequence"].fwd
hcwm = pattern["task0_hypothetical_contribs"].fwd
cwm = pattern["task0_contrib_scores"].fwd
pfm_fig = viz_sequence.plot_weights(pfm, subticks_frequency=10, return_fig=True)
hcwm_fig = viz_sequence.plot_weights(hcwm, subticks_frequency=10, return_fig=True)
cwm_fig = viz_sequence.plot_weights(cwm, subticks_frequency=10, return_fig=True)
pfm_fig.tight_layout()
hcwm_fig.tight_layout()
cwm_fig.tight_layout()
motif_table = vdomh.table(
vdomh.tr(
vdomh.td("Sequence (PFM)"),
vdomh.td(figure_to_vdom_image(pfm_fig))
),
vdomh.tr(
vdomh.td("Hypothetical contributions (hCWM)"),
vdomh.td(figure_to_vdom_image(hcwm_fig))
),
vdomh.tr(
vdomh.td("Actual contributions (CWM)"),
vdomh.td(figure_to_vdom_image(cwm_fig))
)
)
display(motif_table)
plt.close("all") # Remove all standing figures
# Trim motif based on information content
short_trimmed_pfm = trim_motif_by_ic(pfm, pfm)
motif_pfms_short[-1].append(short_trimmed_pfm)
# Expand trimming to +/- 4bp on either side
trimmed_pfm = trim_motif_by_ic(pfm, pfm, pad=4)
trimmed_hcwm = trim_motif_by_ic(pfm, hcwm, pad=4)
trimmed_cwm = trim_motif_by_ic(pfm, cwm, pad=4)
motif_pfms[-1].append(trimmed_pfm)
motif_hcwms[-1].append(trimmed_hcwm)
motif_cwms[-1].append(trimmed_cwm)
num_seqlets[-1].append(len(seqlets))
seqlet_true_profs, seqlet_pred_profs, seqlet_seqs, seqlet_hyps, seqlet_coords = extract_profiles_and_coords(
seqlets, one_hot_seqs, hyp_scores, true_profs, pred_profs, pred_coords,
input_length, profile_length, shap_score_center_size,
profile_display_center_size, task_index=task_index
)
motif_seqlets[-1].append((seqlet_seqs, seqlet_hyps))
assert np.allclose(np.sum(seqlet_seqs, axis=0) / len(seqlet_seqs), pattern["sequence"].fwd)
# ^Sanity check: PFM derived from seqlets match the PFM stored in the pattern
prof_fig = plot_profiles(
# Flatten to NT x O x 2
np.reshape(seqlet_true_profs, (-1, seqlet_true_profs.shape[2], seqlet_true_profs.shape[3])),
np.reshape(seqlet_pred_profs, (-1, seqlet_pred_profs.shape[2], seqlet_pred_profs.shape[3]))
)
summit_dists = get_summit_distances(seqlet_coords, peak_table)
dist_fig = plot_summit_dists(summit_dists)
if tfm_motifs_cache_dir:
# Save results and figures
motif_id = "%d_%d" % (metacluster_i, pattern_i)
np.savez_compressed(
os.path.join(tfm_motifs_cache_dir, motif_id + "_seqlets"),
seqlet_true_profs=seqlet_true_profs, seqlet_pred_profs=seqlet_pred_profs,
seqlet_seqs=seqlet_seqs, seqlet_hyps=seqlet_hyps, seqlet_coords=seqlet_coords,
summit_dists=summit_dists
)
prof_fig.savefig(os.path.join(tfm_motifs_cache_dir, motif_id + "_profiles.png"))
dist_fig.savefig(os.path.join(tfm_motifs_cache_dir, motif_id + "_summitdists.png"))
pfm_fig.savefig(os.path.join(tfm_motifs_cache_dir, motif_id + "_pfm_full.png"))
hcwm_fig.savefig(os.path.join(tfm_motifs_cache_dir, motif_id + "_hcwm_full.png"))
cwm_fig.savefig(os.path.join(tfm_motifs_cache_dir, motif_id + "_cwm_full.png"))
motif_dset = motif_hdf5.create_group(motif_id)
motif_dset.create_dataset("pfm_full", data=pfm, compression="gzip")
motif_dset.create_dataset("hcwm_full", data=hcwm, compression="gzip")
motif_dset.create_dataset("cwm_full", data=cwm, compression="gzip")
motif_dset.create_dataset("pfm_trimmed", data=trimmed_pfm, compression="gzip")
motif_dset.create_dataset("hcwm_trimmed", data=trimmed_hcwm, compression="gzip")
motif_dset.create_dataset("cwm_trimmed", data=trimmed_cwm, compression="gzip")
motif_dset.create_dataset("pfm_short_trimmed", data=short_trimmed_pfm, compression="gzip")
if tfm_motifs_cache_dir:
motif_hdf5.close()
5675 seqlets
Sequence (PFM) | |
Hypothetical contributions (hCWM) | |
Actual contributions (CWM) |
322 seqlets
Sequence (PFM) | |
Hypothetical contributions (hCWM) | |
Actual contributions (CWM) |
310 seqlets
Sequence (PFM) | |
Hypothetical contributions (hCWM) | |
Actual contributions (CWM) |
109 seqlets
Sequence (PFM) | |
Hypothetical contributions (hCWM) | |
Actual contributions (CWM) |
Motifs are trimmed based on information content, and presented in descending order by number of supporting seqlets. The motifs are separated by metacluster. The motifs are presented as hCWMs. The forward orientation is defined as the orientation that is richer in purines.
colgroup = vdomh.colgroup(
vdomh.col(style={"width": "5%"}),
vdomh.col(style={"width": "5%"}),
vdomh.col(style={"width": "45%"}),
vdomh.col(style={"width": "45%"})
)
header = vdomh.thead(
vdomh.tr(
vdomh.th("#", style={"text-align": "center"}),
vdomh.th("Seqlets", style={"text-align": "center"}),
vdomh.th("Forward", style={"text-align": "center"}),
vdomh.th("Reverse", style={"text-align": "center"})
)
)
for i in range(len(motif_hcwms)):
display(vdomh.h3("Metacluster %d/%d" % (i + 1, num_metaclusters)))
body = []
for j in range(len(motif_hcwms[i])):
motif = motif_hcwms[i][j]
if np.sum(motif[:, [0, 2]]) > 0.5 * np.sum(motif):
# Forward is purine-rich, reverse-complement is pyrimidine-rich
f, rc = motif, np.flip(motif, axis=(0, 1))
else:
f, rc = np.flip(motif, axis=(0, 1)), motif
f_fig = viz_sequence.plot_weights(f, figsize=(20, 4), return_fig=True)
f_fig.tight_layout()
rc_fig = viz_sequence.plot_weights(rc, figsize=(20, 4), return_fig=True)
rc_fig.tight_layout()
if tfm_motifs_cache_dir:
# Save results and figures
motif_id = "%d_%d" % (i, j)
f_fig.savefig(os.path.join(tfm_motifs_cache_dir, motif_id + "_hcwm_trimmed_fwd.png"))
rc_fig.savefig(os.path.join(tfm_motifs_cache_dir, motif_id + "_hcwm_trimmed_rev.png"))
body.append(
vdomh.tr(
vdomh.td(str(j + 1)),
vdomh.td(str(num_seqlets[i][j])),
vdomh.td(figure_to_vdom_image(f_fig)),
vdomh.td(figure_to_vdom_image(rc_fig))
)
)
display(vdomh.table(colgroup, header, vdomh.tbody(*body)))
plt.close("all")
# | Seqlets | Forward | Reverse |
---|---|---|---|
1 | 5675 | ||
2 | 322 | ||
3 | 310 | ||
4 | 109 |
Here, the TF-MoDISco motifs are plotted as hCWMs, but the TOMTOM matches are shown as PWMs.
num_matches_to_keep = 10
num_matches_to_show = 5
header = vdomh.thead(
vdomh.tr(
vdomh.th("Motif ID", style={"text-align": "center"}),
vdomh.th("q-val", style={"text-align": "center"}),
vdomh.th("PWM", style={"text-align": "center"})
)
)
for i in range(len(motif_pfms)):
display(vdomh.h3("Metacluster %d/%d" % (i + 1, num_metaclusters)))
# Compute TOMTOM matches for all motifs in the metacluster at once
out_dir = os.path.join(tfm_motifs_cache_dir, "tomtom", "metacluster_%d" % i) if tfm_motifs_cache_dir else None
tomtom_matches = match_motifs_to_database(motif_pfms_short[i], top_k=num_matches_to_keep, temp_dir=out_dir)
for j in range(len(motif_pfms[i])):
display(vdomh.h4("Motif %d/%d" % (j + 1, len(motif_pfms[i]))))
viz_sequence.plot_weights(motif_hcwms[i][j])
body = []
for k, (match_name, match_pfm, match_qval) in enumerate(tomtom_matches[j]):
fig = viz_sequence.plot_weights(pfm_to_pwm(match_pfm), return_fig=True)
fig.tight_layout()
if k < num_matches_to_show:
body.append(
vdomh.tr(
vdomh.td(match_name),
vdomh.td(str(match_qval)),
vdomh.td(figure_to_vdom_image(fig))
)
)
if tfm_motifs_cache_dir:
# Save results and figures
motif_id = "%d_%d" % (i, j)
fig.savefig(os.path.join(out_dir, motif_id + ("_hit-%d.png" % (k + 1))))
else:
body.append(
vdomh.tr(
vdomh.td(match_name),
vdomh.td(str(match_qval)),
vdomh.td("Not shown")
)
)
if not body:
display(vdomh.p("No TOMTOM matches passing threshold"))
else:
display(vdomh.table(header, vdomh.tbody(*body)))
plt.close("all")
Motif ID | q-val | PWM |
---|---|---|
MAFB_HUMAN.H11MO.0.B | 6.12268e-09 | |
MAFG_HUMAN.H11MO.0.A | 6.12268e-09 | |
MAFK_HUMAN.H11MO.0.A | 6.12268e-09 | |
MA0496.3_MAFK | 6.12268e-09 | |
MA1520.1_MAF | 9.77391e-09 | |
MAFK_HUMAN.H11MO.1.A | 1.0297000000000001e-08 | Not shown |
MAF_HUMAN.H11MO.0.A | 3.5516199999999996e-08 | Not shown |
MA1521.1_MAFA | 1.45365e-07 | Not shown |
MAFF_HUMAN.H11MO.0.B | 6.49507e-07 | Not shown |
MAF_HUMAN.H11MO.1.B | 1.77295e-05 | Not shown |
Motif ID | q-val | PWM |
---|---|---|
MA0139.1_CTCF | 4.457140000000001e-21 | |
CTCF_HUMAN.H11MO.0.A | 3.95028e-15 | |
CTCFL_HUMAN.H11MO.0.A | 3.66792e-08 | |
MA1102.2_CTCFL | 0.00011211200000000001 | |
MA1568.1_TCF21(var.2) | 0.134969 | |
MA1638.1_HAND2 | 0.154289 | Not shown |
SNAI1_HUMAN.H11MO.0.C | 0.180101 | Not shown |
Motif ID | q-val | PWM |
---|---|---|
MA1633.1_BACH1 | 7.491260000000001e-06 | |
NF2L2_HUMAN.H11MO.0.A | 7.491260000000001e-06 | |
MA0501.1_MAF::NFE2 | 7.491260000000001e-06 | |
MA0591.1_Bach1::Mafk | 7.491260000000001e-06 | |
BACH2_HUMAN.H11MO.0.A | 7.491260000000001e-06 | |
MA0089.2_NFE2L1 | 7.491260000000001e-06 | Not shown |
NFE2_HUMAN.H11MO.0.A | 7.491260000000001e-06 | Not shown |
MA0150.2_Nfe2l2 | 9.832280000000001e-06 | Not shown |
BACH1_HUMAN.H11MO.0.A | 3.57422e-05 | Not shown |
JUN_HUMAN.H11MO.0.A | 0.0005405809999999999 | Not shown |
Motif ID | q-val | PWM |
---|---|---|
SP2_HUMAN.H11MO.0.A | 5.29825e-05 | |
SP3_HUMAN.H11MO.0.B | 5.29825e-05 | |
SP1_HUMAN.H11MO.1.A | 5.29825e-05 | |
SP1_HUMAN.H11MO.0.A | 6.28415e-05 | |
MA1513.1_KLF15 | 0.00011779399999999999 | |
CTCFL_HUMAN.H11MO.0.A | 0.000616259 | Not shown |
MA1650.1_ZBTB14 | 0.00187334 | Not shown |
PATZ1_HUMAN.H11MO.0.C | 0.00258712 | Not shown |
KLF16_HUMAN.H11MO.0.D | 0.00258712 | Not shown |
KLF3_HUMAN.H11MO.0.B | 0.00269642 | Not shown |
Here, the motifs are presented as hCWMs, along with the actual importance scores of a random sample of seqlets that support the motif.
num_seqlets_to_show = 10
colgroup = vdomh.colgroup(
vdomh.col(style={"width": "50%"}),
vdomh.col(style={"width": "50%"})
)
header = vdomh.thead(
vdomh.tr(
vdomh.th("Motif hCWM", style={"text-align": "center"}),
vdomh.th("Seqlets", style={"text-align": "center"})
)
)
for i in range(len(motif_hcwms)):
display(vdomh.h3("Metacluster %d/%d" % (i + 1, num_metaclusters)))
for j in range(len(motif_hcwms[i])):
display(vdomh.h4("Motif %d/%d" % (j + 1, len(motif_hcwms[i]))))
motif_fig = viz_sequence.plot_weights(motif_hcwms[i][j], figsize=(20, 4), return_fig=True)
motif_fig.tight_layout()
seqlet_seqs, seqlet_hyps = motif_seqlets[i][j]
sample_size = min(num_seqlets_to_show, len(seqlet_seqs))
sample_inds = np.random.choice(len(seqlet_seqs), size=sample_size, replace=False)
sample = []
for k in sample_inds:
fig = viz_sequence.plot_weights(seqlet_hyps[k] * seqlet_seqs[k], subticks_frequency=10, return_fig=True)
fig.tight_layout()
sample.append(figure_to_vdom_image(fig))
body = vdomh.tbody(vdomh.tr(vdomh.td(figure_to_vdom_image(motif_fig)), vdomh.td(*sample)))
display(vdomh.table(colgroup, header, body))
plt.close("all")
Motif hCWM | Seqlets |
---|---|
Motif hCWM | Seqlets |
---|---|
Motif hCWM | Seqlets |
---|---|
Motif hCWM | Seqlets |
---|---|