Finding attractors

Find the stable states (phenotypes) of the fitted Boolean network, filter them, and map observed cells onto them.

Starter notebook. The cells below are a scaffold: real function calls with placeholder paths/arguments and TODO markers. Fill in your own data and run top-to-bottom. Anything marked TODO is a choice you need to make for your dataset.

Setup and imports

[ ]:
import os
import os.path as op
import time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import bobaT as bb

sns.set_style('whitegrid')
plt.rcParams['figure.dpi'] = 100
import warnings
warnings.filterwarnings('ignore')

Configure paths

[ ]:
# Input data
DATA_DIR = './test_data'

# Output directories
OUTPUT_DIR = './output'
VAL_DIR = './output/validation'
ATTRACTOR_DIR = './output/attractors'
PERTURB_DIR = './output/perturbations'
os.makedirs(OUTPUT_DIR, exist_ok=True)
os.makedirs(ATTRACTOR_DIR, exist_ok=True)

Load network, rules, and binarized data

These come from the inference step.

[ ]:
# Load the base network as a graph-tool graph
network_path = 'tf-lit-network.csv'  # TODO: your base network CSV
graph, vertex_dict = bb.load.load_network(
    f'{DATA_DIR}/{network_path}',
    remove_sinks=False, remove_selfloops=True, remove_sources=False,
)
v_names, nodes = bb.utils.get_nodes(vertex_dict, graph)

rules, regulators_dict = bb.load.load_rules(fname=f'{OUTPUT_DIR}/rules.txt')

# TODO: load the binarized data and clusters saved during inference
binarized_data = None  # e.g. reload from f'{OUTPUT_DIR}/binarized_data_train.csv'
clusters = None

Search for attractors

tf_basin controls the Hamming radius of the basin search.

[ ]:
tf_basin = 2
attractor_dict = bb.tl.find_attractors(
    binarized_data, rules, nodes, regulators_dict, tf_basin=tf_basin,
    save_dir=ATTRACTOR_DIR, on_nodes=[], off_nodes=[],
)

Filter attractors to the relevant ones

[ ]:
attractor_dict_filtered = bb.tl.filter_attractors(ATTRACTOR_DIR, nodes, clusters)

Visualize attractors

[ ]:
# On/off state of each gene per attractor
bb.plot.plot_attractors(f'{ATTRACTOR_DIR}/attractors_filtered.txt')

# Clustered heatmap — helpful when there are many attractors
bb.plot.plot_attractors_clustermap(fname=f'{ATTRACTOR_DIR}/attractors_filtered.txt')

Load the attractor dictionary for downstream use

[ ]:
attractor_dict = bb.utils.get_attractor_dict(ATTRACTOR_DIR, filtered=True)

Next step

Run in silico perturbations against these attractors.