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 plot.viz_sequence as viz_sequence
import numpy as np
import h5py
import sklearn.cluster
import scipy.cluster.hierarchy
import matplotlib.pyplot as plt
import vdom.helpers as vdomh
from IPython.display import display
# Define parameters/fetch arguments
tf_name = os.environ["TFM_TF_NAME"]
tfm_results_path = os.environ["TFM_TFM_PATH"]
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("TF-MoDISco results path: %s" % tfm_results_path)
print("Saved TF-MoDISco-derived motifs cache: %s" % tfm_motifs_cache_dir)
TF name: Sox2 TF-MoDISco results path: /users/amtseng/tfmodisco/results/tfmodisco/BPNet/BPNet_Sox2_ChIPseq/BPNet_Sox2_ChIPseq_count_tfm.h5 Saved TF-MoDISco-derived motifs cache: /users/amtseng/tfmodisco/results/reports/tfmodisco_results//cache/BPNet/BPNet_Sox2_ChIPseq/BPNet_Sox2_ChIPseq_count
# Define paths and constants
min_ic = 0.3
if tfm_motifs_cache_dir:
os.makedirs(tfm_motifs_cache_dir, exist_ok=True)
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")
with h5py.File(tfm_results_path, "r") as f:
metaclusters = f["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)))
if "patterns" not in metacluster["seqlets_to_patterns_result"].keys():
continue
patterns = metacluster["seqlets_to_patterns_result"]["patterns"]
num_patterns = len(patterns["all_pattern_names"][:])
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_name in enumerate(patterns["all_pattern_names"][:]):
pattern_name = pattern_name.decode()
pattern = patterns[pattern_name]
seqlets = pattern["seqlets_and_alnmts"]["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, min_ic=min_ic)
motif_pfms_short[-1].append(short_trimmed_pfm)
# Expand trimming to +/- 4bp on either side
trimmed_pfm = trim_motif_by_ic(pfm, pfm, min_ic=min_ic, pad=4)
trimmed_hcwm = trim_motif_by_ic(pfm, hcwm, min_ic=min_ic, pad=4)
trimmed_cwm = trim_motif_by_ic(pfm, cwm, min_ic=min_ic, 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))
if tfm_motifs_cache_dir:
# Save results and figures
motif_id = "%d_%d" % (metacluster_i, pattern_i)
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()
4058 seqlets
Sequence (PFM) | |
Hypothetical contributions (hCWM) | |
Actual contributions (CWM) |
1123 seqlets
Sequence (PFM) | |
Hypothetical contributions (hCWM) | |
Actual contributions (CWM) |
157 seqlets
Sequence (PFM) | |
Hypothetical contributions (hCWM) | |
Actual contributions (CWM) |
145 seqlets
Sequence (PFM) | |
Hypothetical contributions (hCWM) | |
Actual contributions (CWM) |
137 seqlets
Sequence (PFM) | |
Hypothetical contributions (hCWM) | |
Actual contributions (CWM) |
78 seqlets
Sequence (PFM) | |
Hypothetical contributions (hCWM) | |
Actual contributions (CWM) |
56 seqlets
Sequence (PFM) | |
Hypothetical contributions (hCWM) | |
Actual contributions (CWM) |
55 seqlets
Sequence (PFM) | |
Hypothetical contributions (hCWM) | |
Actual contributions (CWM) |
44 seqlets
Sequence (PFM) | |
Hypothetical contributions (hCWM) | |
Actual contributions (CWM) |
43 seqlets
Sequence (PFM) | |
Hypothetical contributions (hCWM) | |
Actual contributions (CWM) |
38 seqlets
Sequence (PFM) | |
Hypothetical contributions (hCWM) | |
Actual contributions (CWM) |
33 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")
/users/amtseng/tfmodisco/src/plot/viz_sequence.py:152: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`). fig = plt.figure(figsize=figsize)
# | Seqlets | Forward | Reverse |
---|---|---|---|
1 | 4058 | ||
2 | 1123 | ||
3 | 157 | ||
4 | 145 | ||
5 | 137 | ||
6 | 78 | ||
7 | 56 | ||
8 | 55 | ||
9 | 44 | ||
10 | 43 | ||
11 | 38 | ||
12 | 33 |
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 |
---|---|---|
MA0142.1_Pou5f1::Sox2 | 1.37979e-12 | |
PO5F1_HUMAN.H11MO.0.A | 1.37979e-12 | |
NANOG_HUMAN.H11MO.0.A | 1.30354e-11 | |
SOX2_HUMAN.H11MO.1.A | 7.10607e-06 | |
MA0792.1_POU5F1B | 1.10509e-05 | |
MA0789.1_POU3F4 | 5.14575e-05 | Not shown |
PO3F1_HUMAN.H11MO.0.C | 0.00118721 | Not shown |
PO5F1_HUMAN.H11MO.1.A | 0.00281348 | Not shown |
MA1115.1_POU5F1 | 0.00281348 | Not shown |
MA0786.1_POU3F1 | 0.0100764 | Not shown |
Motif ID | q-val | PWM |
---|---|---|
MA0077.1_SOX9 | 0.0012806 | |
SOX9_HUMAN.H11MO.0.B | 0.00365426 | |
SOX13_HUMAN.H11MO.0.D | 0.00365426 | |
MA0868.2_SOX8 | 0.00557091 | |
MA0867.2_SOX4 | 0.00706894 | |
MA1152.1_SOX15 | 0.047286699999999994 | Not shown |
MA0515.1_Sox6 | 0.0608477 | Not shown |
SOX9_HUMAN.H11MO.1.B | 0.0608477 | Not shown |
MA1120.1_SOX13 | 0.0625398 | Not shown |
SOX3_HUMAN.H11MO.0.B | 0.0663003 | Not shown |
Motif ID | q-val | PWM |
---|---|---|
MA0068.2_PAX4 | 0.0942451 | |
IRF1_HUMAN.H11MO.0.A | 0.0942451 | |
IRF8_HUMAN.H11MO.0.B | 0.0942451 | |
MA0612.2_EMX1 | 0.0942451 | |
LHX2_HUMAN.H11MO.0.A | 0.0942451 | |
MA0050.2_IRF1 | 0.0942451 | Not shown |
MA0705.1_Lhx8 | 0.0942451 | Not shown |
IRF2_HUMAN.H11MO.0.A | 0.0942451 | Not shown |
MA0621.1_mix-a | 0.0942451 | Not shown |
LHX4_HUMAN.H11MO.0.D | 0.0942451 | Not shown |
Motif ID | q-val | PWM |
---|---|---|
MA0505.1_Nr5a2 | 1.72561e-05 | |
NR6A1_HUMAN.H11MO.0.B | 4.65291e-05 | |
NR5A2_HUMAN.H11MO.0.B | 8.50768e-05 | |
MA0592.3_ESRRA | 0.00010101 | |
STF1_HUMAN.H11MO.0.B | 0.00010101 | |
ERR2_HUMAN.H11MO.0.A | 0.00010643799999999999 | Not shown |
ERR3_HUMAN.H11MO.0.B | 0.000141609 | Not shown |
MA1541.1_NR6A1 | 0.000158741 | Not shown |
MA0643.1_Esrrg | 0.000985578 | Not shown |
MA1540.1_NR5A1 | 0.00143646 | Not shown |
Motif ID | q-val | PWM |
---|---|---|
ZIC3_HUMAN.H11MO.0.B | 6.781390000000001e-09 | |
ZIC2_HUMAN.H11MO.0.D | 9.23644e-08 | |
MA1629.1_Zic2 | 4.5050100000000004e-05 | |
MA0696.1_ZIC1 | 0.0007702039999999999 | |
MA0751.1_ZIC4 | 0.00089169 | |
MA1628.1_Zic1::Zic2 | 0.00354616 | Not shown |
MA0697.1_ZIC3 | 0.00583859 | Not shown |
ZIC4_HUMAN.H11MO.0.D | 0.00783916 | Not shown |
MA1584.1_ZIC5 | 0.010620600000000001 | Not shown |
MA1102.2_CTCFL | 0.0577553 | Not shown |
Motif ID | q-val | PWM |
---|---|---|
SOX9_HUMAN.H11MO.0.B | 0.00326489 | |
SOX10_HUMAN.H11MO.0.B | 0.18441300000000002 | |
MA1120.1_SOX13 | 0.229746 | |
SOX3_HUMAN.H11MO.0.B | 0.229746 | |
MA0867.2_SOX4 | 0.229746 | |
SOX2_HUMAN.H11MO.0.A | 0.382232 | Not shown |
MA0077.1_SOX9 | 0.382232 | Not shown |
SOX15_HUMAN.H11MO.0.D | 0.382232 | Not shown |
SOX17_HUMAN.H11MO.0.C | 0.427228 | Not shown |
MA0514.1_Sox3 | 0.430162 | Not shown |
Motif ID | q-val | PWM |
---|---|---|
NR6A1_HUMAN.H11MO.0.B | 0.05563969999999999 | |
MA0505.1_Nr5a2 | 0.115199 | |
ERR3_HUMAN.H11MO.0.B | 0.12621500000000002 | |
ERR2_HUMAN.H11MO.0.A | 0.12621500000000002 | |
MA0071.1_RORA | 0.12621500000000002 | |
MA1541.1_NR6A1 | 0.152917 | Not shown |
RORA_HUMAN.H11MO.0.C | 0.228095 | Not shown |
MA0643.1_Esrrg | 0.228095 | Not shown |
RARA_HUMAN.H11MO.1.A | 0.228095 | Not shown |
MA0160.1_NR4A2 | 0.228095 | Not shown |
Motif ID | q-val | PWM |
---|---|---|
MA1120.1_SOX13 | 0.13388699999999998 | |
FOXQ1_HUMAN.H11MO.0.C | 0.13388699999999998 | |
FOXO4_HUMAN.H11MO.0.C | 0.27662600000000004 | |
SOX9_HUMAN.H11MO.1.B | 0.27662600000000004 | |
MA0077.1_SOX9 | 0.27662600000000004 | |
MA0040.1_Foxq1 | 0.27662600000000004 | Not shown |
SOX17_HUMAN.H11MO.0.C | 0.313659 | Not shown |
MA1563.1_SOX18 | 0.313659 | Not shown |
SOX3_HUMAN.H11MO.0.B | 0.352191 | Not shown |
SOX13_HUMAN.H11MO.0.D | 0.402751 | Not shown |
Motif ID | q-val | PWM |
---|---|---|
MA0505.1_Nr5a2 | 0.211504 | |
ERR3_HUMAN.H11MO.0.B | 0.290117 | |
MA0071.1_RORA | 0.290117 | |
MA0141.3_ESRRB | 0.290117 | |
ERR2_HUMAN.H11MO.0.A | 0.290117 | |
NR6A1_HUMAN.H11MO.0.B | 0.305467 | Not shown |
Motif ID | q-val | PWM |
---|---|---|
NANOG_HUMAN.H11MO.1.B | 0.14838099999999999 | |
MA0515.1_Sox6 | 0.14838099999999999 | |
SOX15_HUMAN.H11MO.0.D | 0.14838099999999999 | |
SOX2_HUMAN.H11MO.0.A | 0.14838099999999999 | |
SOX3_HUMAN.H11MO.0.B | 0.14838099999999999 | |
SOX10_HUMAN.H11MO.1.A | 0.14838099999999999 | Not shown |
MA0867.2_SOX4 | 0.14838099999999999 | Not shown |
MA0514.1_Sox3 | 0.14838099999999999 | Not shown |
HMX1_HUMAN.H11MO.0.D | 0.14838099999999999 | Not shown |
SOX4_HUMAN.H11MO.0.B | 0.14838099999999999 | Not shown |
Motif ID | q-val | PWM |
---|---|---|
NR6A1_HUMAN.H11MO.0.B | 0.053526599999999994 | |
MA0505.1_Nr5a2 | 0.16406700000000002 | |
ZN554_HUMAN.H11MO.1.D | 0.270444 | |
ERR3_HUMAN.H11MO.0.B | 0.270444 | |
ERR2_HUMAN.H11MO.0.A | 0.270444 | |
MA0071.1_RORA | 0.280111 | Not shown |
MA1541.1_NR6A1 | 0.280111 | Not shown |
Motif ID | q-val | PWM |
---|---|---|
SOX5_HUMAN.H11MO.0.C | 0.237506 | |
MA0077.1_SOX9 | 0.237506 | |
SOX2_HUMAN.H11MO.1.A | 0.237506 | |
MA0087.1_Sox5 | 0.291261 | |
MA1629.1_Zic2 | 0.291261 |