A tour of react-bio-viz in Jupyter¶
Load an alignment, compute distances and a tree with nj.rs, and look at all of it, plus a gene model. Every widget is interactive: pan, zoom, select and drag.
To link widgets together and respond to what you do in them from Python, continue with Linked views.
Install
pip install react-bio-viz nj_py
or, from a checkout of the repository: pnpm install && pnpm build, then
pip install -e packages/python nj_py. The widgets work in JupyterLab, Notebook 7, VS Code and Colab.
import json
from nj_py import distance_matrix, nj
from react_bio_viz import MSA, DistanceMatrix, GeneModel, PhyloTree, parse_newick, read_fasta, to_newick
1. An alignment¶
read_fasta returns the plain structure the widgets take: a list of betula {"identifier", "sequence"}
records. This is a 24-sequence alignment of AUX/IAA family proteins.
records = read_fasta("data/aux_iaa.fasta")
print(f"{len(records)} sequences × {len(records[0]['sequence'])} columns")
24 sequences × 941 columns
Scroll to pan, Ctrl/⌘-scroll (or pinch) to zoom, drag the minimap. Shift-drag across the alignment to select columns, click a label to select a row, drag a label to reorder.
MSA(msa=records, width=820, height=480, options={"tracks": ["conservation", "logo"]})
2. Distances¶
Any analysis works — the widgets only visualize. nj_py, the
Python package of nj.rs, takes the same records as MSA. Its distance_matrix estimates the
distance between every pair of sequences, here as substitutions per site under the LG model for
proteins (other models).
distances = distance_matrix(records, substitution_model="Lg")
DistanceMatrix(labels=distances["names"], matrix=distances["matrix"], width=820, height=520,
options={"showNumbers": False, "cellWidth": 20, "cellHeight": 20})
3. A tree¶
nj builds a neighbour-joining tree (Saitou & Nei, 1987) from the same records and model, with
bootstrap support. It returns Newick, which parse_newick turns into the nested
{"name", "length", "children"} structure PhyloTree takes.
nj.rs reports support as a count of bootstrap replicates, and PhyloTree shades branches by a 0–1
value, so the counts are scaled first.
SAMPLES = 100
result = nj(records, substitution_model="Lg", n_bootstrap_samples=SAMPLES)
tree = parse_newick(result["newick"])
def scale_support(node):
if node["children"] and node["name"]:
node["name"] = f"{int(node['name']) / SAMPLES:.2f}"
for child in node["children"]:
scale_support(child)
scale_support(tree)
Internal nodes are labelled with their support, and branches below weakly supported nodes are faded. Drag a node to another row to rotate the tree, or past the top or bottom to reroot on it. Click an internal node's marker to collapse it.
PhyloTree(tree=tree, width=820, height=480, leaf_spacing=18, interactive=True)
to_newick turns the tree back into Newick, to save it or use it elsewhere:
newick = to_newick(tree)
print(newick[:120], "…")
(AAG12546.1:0,(((((((((((XP_013641924.1:0.012,(VDD42994.1:0,XP_013711935.1:0)1.00:0.018)1.00:0.023,((VDC98083.1:0.013,(X …
4. A gene¶
A gene model, with its transcripts, exons and CDSs. Click an exon for its details:
with open("data/genemodel.json") as handle:
gene = json.load(handle)
GeneModel(gene=gene, width=820)
5. Share it¶
An exported notebook keeps its widgets interactive, with no Python running behind them:
jupyter nbconvert --to html --execute tour.ipynb
More¶
Linked views links these widgets and handles their events in Python.
GenomeBrowser stacks feature, coverage and gene tracks; BlastHitDistribution shows BLAST hits
along a query. See the documentation for every widget, trait and option.