BPNet tf-Modisco report

In [1]:
modisco_dir = "/users/avsec/workspace/basepair/data/processed/chipnexus/exp/models/oct-sox-nanog-klf/models/n_dil_layers=9/modisco/valid/new-hparams"
In [2]:
# Parameters
modisco_dir = "Nanog"
In [3]:
from basepair.modisco.results import ModiscoResult
from basepair.config import get_data_dir
from basepair.utils import read_json
from basepair.plot.vdom import vdom_modisco
from kipoi.readers import HDF5Reader
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
from plotnine import *
Using TensorFlow backend.
In [4]:
mr = ModiscoResult(f"{modisco_dir}/modisco.h5")
mr.open()
In [5]:
# load the data
modisco_kwargs = read_json(os.path.join(modisco_dir, "kwargs.json"))
d = HDF5Reader(modisco_kwargs['imp_scores'])
d.open()
In [6]:
strand_dist_file = f"{modisco_dir}/strand_distances.h5"
if modisco_kwargs.get("ignore_strand_dist", False) and os.path.exists(strand_dist_file):
    included_samples = HDF5Reader.load(strand_dist_file)['included_samples']
else:
    included_samples = np.ones(d.f['inputs'].shape[:1], dtype=bool)
    
if modisco_kwargs.get("filter_npy", None) is not None:
    included_samples = np.load(modisco_kwargs['filter_npy']) * included_samples
    
    
id_hash = pd.DataFrame({"peak_id": d.f['/metadata/interval_from_task'][:][included_samples], 
                        "example_idx": np.arange(d.f['/metadata/interval_from_task'][included_samples].shape[0])})
tasks = list(d.f["targets"]["profile"].keys())
In [7]:
# get all seqlet instances
dfp = mr.seqlet_df_instances().rename(columns=dict(seqname="example_idx"))
dfp = pd.merge(dfp, id_hash, on="example_idx")
TF-MoDISco is using the TensorFlow backend.
In [8]:
# row = example_idx
total_counts = pd.DataFrame({task: d.f[f"/targets/profile/{task}"][:][included_samples].sum(axis=-1).sum(axis=-1)
          for task in tasks
         })
In [9]:
len(mr.patterns())
Out[9]:
30
In [10]:
# total number of seqlets
len(dfp)
Out[10]:
21749
In [11]:
# Number of metaclusters
len(mr.metaclusters())
Out[11]:
1

Number of seqlets per pattern

In [12]:
mc_stat = mr.metacluster_stats()
In [13]:
ggplot(aes(x="pattern", y='n'), mc_stat) + geom_bar(stat='identity') + \
    facet_wrap("~metacluster", ncol=4, labeller='label_both') + \
    ylab("Number of seqlets") + theme_classic()
Out[13]:
<ggplot: (-9223363270911688255)>

Zoom-into the 500 seqlet range

In [14]:
ggplot(aes(x="pattern", y='n'), mc_stat) + geom_bar(stat='identity') + \
    facet_wrap("~metacluster", ncol=4, labeller='label_both') + \
    ylab("Number of seqlets") + theme_classic() + coord_cartesian(ylim=[0, 500])
Out[14]:
<ggplot: (-9223363270914149081)>

Important tasks per metacluster

In [15]:
mcs_grouped = mc_stat.groupby("metacluster").n.agg(["count", "sum"]).reset_index()
fig, ax = plt.subplots(2, 1, sharex=False, figsize=(18,6), 
                       gridspec_kw={'height_ratios': [2,1]})
mcs_grouped.plot("metacluster", "count", 
                 label="# patterns per metacluster", style="o--", 
                 ax=ax[0], 
                 yticks=range(mcs_grouped['count'].max()+1),
                 xticks=range(38),
                 fontsize='large',
                 xlim=(-.5, len(mr.metaclusters()) - .5 ))
mcs_grouped.plot("metacluster", "sum", 
                 label="# seqlets per metacluster", 
                 style="o--", ax=ax[0], secondary_y=True)
ax[0].grid(linewidth=0.2)
mr.plot_metacluster_activity(ax[1], cbar=False)
ax[1].set_title("Importance score activity: Red = positive, Blue = negative");
/users/avsec/bin/anaconda3/envs/chipnexus/lib/python3.6/site-packages/pandas/plotting/_core.py:998: UserWarning: Attempting to set identical left==right results
in singular transformations; automatically expanding.
left=0.0, right=0.0
  ax.set_xlim(left, right)
/users/avsec/bin/anaconda3/envs/chipnexus/lib/python3.6/site-packages/pandas/plotting/_core.py:998: UserWarning: Attempting to set identical left==right results
in singular transformations; automatically expanding.
left=0.0, right=0.0
  ax.set_xlim(left, right)
In [16]:
vdom_modisco(mr, "plots", total_counts, dfp, is_open=True, trim_frac=0.08, letter_width=0.15, height=0.5)
Out[16]:
metacluster_0, # patterns: 30, # seqlets: 21749, important for: Nanog
  • pattern_0: # seqlets: 6568
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 6282 / 133644 regions (4.7%)

  • pattern_1: # seqlets: 4645
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 4441 / 133644 regions (3.3%)

  • pattern_2: # seqlets: 1925
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 1893 / 133644 regions (1.4%)

  • pattern_3: # seqlets: 1392
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 1341 / 133644 regions (1.0%)

  • pattern_4: # seqlets: 1130
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 1117 / 133644 regions (0.8%)

  • pattern_5: # seqlets: 968
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 962 / 133644 regions (0.7%)

  • pattern_6: # seqlets: 752
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 752 / 133644 regions (0.6%)

  • pattern_7: # seqlets: 503
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 495 / 133644 regions (0.4%)

  • pattern_8: # seqlets: 469
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 460 / 133644 regions (0.3%)

  • pattern_9: # seqlets: 295
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 295 / 133644 regions (0.2%)

  • pattern_10: # seqlets: 261
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 261 / 133644 regions (0.2%)

  • pattern_11: # seqlets: 265
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 263 / 133644 regions (0.2%)

  • pattern_12: # seqlets: 247
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 247 / 133644 regions (0.2%)

  • pattern_13: # seqlets: 232
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 232 / 133644 regions (0.2%)

  • pattern_14: # seqlets: 215
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 215 / 133644 regions (0.2%)

  • pattern_15: # seqlets: 218
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 210 / 133644 regions (0.2%)

  • pattern_16: # seqlets: 176
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 176 / 133644 regions (0.1%)

  • pattern_17: # seqlets: 146
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 146 / 133644 regions (0.1%)

  • pattern_18: # seqlets: 140
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 138 / 133644 regions (0.1%)

  • pattern_19: # seqlets: 110
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 99 / 133644 regions (0.1%)

  • pattern_20: # seqlets: 140
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 139 / 133644 regions (0.1%)

  • pattern_21: # seqlets: 124
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 124 / 133644 regions (0.1%)

  • pattern_22: # seqlets: 139
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 139 / 133644 regions (0.1%)

  • pattern_23: # seqlets: 92
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 89 / 133644 regions (0.1%)

  • pattern_24: # seqlets: 114
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 114 / 133644 regions (0.1%)

  • pattern_25: # seqlets: 143
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 134 / 133644 regions (0.1%)

  • pattern_26: # seqlets: 93
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 93 / 133644 regions (0.1%)

  • pattern_27: # seqlets: 87
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 87 / 133644 regions (0.1%)

  • pattern_28: # seqlets: 63
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 63 / 133644 regions (0.0%)

  • pattern_29: # seqlets: 97
    Aggregated profiles and contribution scores)
    Aggregated hypothetical contribution scores)
    Sequence

    ChIP-nexus counts
    Importance scores (profile)
    Importance scores (counts)
    Positional distribution
    Total count distribution

    Pattern occurs in 97 / 133644 regions (0.1%)

In [17]:
print("Metaclusters heatmap")
import seaborn as sns
activity_patterns = np.array(mr.f.f['metaclustering_results']['attribute_vectors'])[
                    np.array(
        [x[0] for x in sorted(
                enumerate(mr.f.f['metaclustering_results']['metacluster_indices']),
               key=lambda x: x[1])])]
sns.heatmap(activity_patterns, center=0);
Metaclusters heatmap