Skip to main content
TSR Research Group

Hierarchical Clustering

Clusters structures by TSR key similarity, going from key frequencies through a Jaccard similarity matrix to an average-linkage tree and clustered heat map.

Overview

Clustering groups structures by how much of their TSR key content they share, in three steps:

  1. Key frequencies: each structure becomes a vector of how often each key occurs in it.
  2. Similarity: every pair of structures is compared, with a Jaccard similarity over those vectors, giving a square matrix.
  3. Hierarchical clustering: average linkage on the corresponding distances (one minus the similarity) builds a tree, drawn over the matrix as a clustered heat map.

The tree describes how the structures group at every distance. Reading off a fixed number of clusters is a separate step, a cut of the tree by a rule that has to be chosen.

Run it with BioTSR

BioTSR 1.0.0 runs all three steps from Python, in biotsr.analysis; they are not on the command line. Drawing the heat map needs the plotting extra:

bash
python -m pip install "biotsr[plot]==1.0.0"

The example uses the three chains of the Common Keys example. If you have not run it, compute them first, from the same samples.csv:

csv
sample_id,pdb_id,method,chain,label
ubiquitin,1UBQ,protein,A,ubiquitin
barnase,1BRS,protein,A,barnase
barstar,1BRS,protein,D,barstar
bash
biotsr fetch 1UBQ 1BRS --pdb-dir structures
biotsr batch --manifest samples.csv --pdb-dir structures --output batch --no-triplets

That writes batch/ubiquitin.keys, batch/barnase.keys and batch/barstar.keys. Then, in Python from the same folder:

python
import biotsr
from biotsr.analysis import (AnalysisSample, feature_matrix, similarity_matrix,
                             hierarchy, clustermap_spec)
from biotsr.analysis.render import render_clustermap

samples = [AnalysisSample.from_result(biotsr.load_result(path), sample_id=name)
           for name, path in [("ubiquitin", "batch/ubiquitin.keys"),
                              ("barnase", "batch/barnase.keys"),
                              ("barstar", "batch/barstar.keys")]]

fm = feature_matrix(samples)                    # step 1: one column per key
distance = similarity_matrix(fm, "generalized_jaccard").to_distance()  # step 2
tree = hierarchy(distance)                      # step 3: average linkage
render_clustermap(clustermap_spec(distance), "clustermap.png")

This writes the clustered heat map to clustermap.png.

DADADADADGDGDGDGDGDGDGDGDTDTDTDTDCDCDCDCDCDC0generalized Jaccard distance1
Keys, distances and the tree, for the 22 nucleotides of 1BNA. Computed with BioTSR 1.0.0, following its tutorial 08: one sample per residue (2,431 distinct keys between them), generalized Jaccard distances, average linkage. Darker cells are more similar residues; rows and columns follow the tree. The base of each residue is printed beside its row afterwards, for comparison; the tree was built from the distances alone. In this one structure the first 18 of the 21 merges, up to a distance of 0.63, each join residues of the same base. No number of clusters was chosen: the result is the tree itself.
  • Similarity measures: generalized Jaccard, which weighs each key by how often it occurs, is the default; binary Jaccard, which looks only at which keys are present, and three other measures are available. Every matrix exports as CSV with its sample ids.
  • Linkage: average linkage (UPGMA).
  • No number of clusters by default. The result is the tree and the heat map ordered by it. A flat partition is made only when you ask for one, with cut_hierarchy(tree, height=…) or n_clusters=…; BioTSR never chooses the rule. A number of clusters chosen to match known classes makes agreement with those classes partly a consequence of the choice.
  • What is compared: samples are compared only when their keys share a key space, and samples computed with different settings only when you say so.

Tutorial 08 runs the whole workflow on 1BNA, including the optional flat clusters, and the analysis method record, §5 defines it.

The published workflow

The papers ran these steps with scripts from two repositories, listed under Source code below:

StepScriptRepository
1. Key frequencieskey_frequency_drug.pyKinases-and-Phosphatases-Clustering
2. Jaccard similaritythe Jaccard codehsp70_actin
3. Clustermapclustermap_n.pyKinases-and-Phosphatases-Clustering

Two of their options are not part of BioTSR 1.0: the feature selection and adjacent-key summing of the hsp70_actin comparison, and the fixed number of clusters clustermap_n.py cuts the tree into with its -n option. The earlier pipeline also clustered a distance matrix rounded to three decimals.

Source code

  • BioTSR (opens on GitHub)

    Current software

    Used by the examples on this page.

    Language: Python

    Licence Apache License 2.0

  • Kinases-and-Phosphatases-Clustering (opens on GitHub)

    Research code

    Research code for steps 1 and 3 of the published workflow. It is kept unchanged for provenance and for work that depends on it; its README has its own installation instructions.

    Language: Python

    Licence No licence stated

  • hsp70_actin (opens on GitHub)

    Research code

    Research code for step 2 of the published workflow, with the feature-selection options of the hsp70/actin comparison. It is kept unchanged for provenance and for work that depends on it; its README has its own installation instructions.

    Language: Python

    Licence GNU General Public License v3.0