HMM and MSM Timescales for Ala2ΒΆ

hmm-and-msm_evaluated

This example builds HMM and MSMs on the alanine_dipeptide dataset using varing lag times and numbers of states, and compares the relaxation timescales

In [1]:
from __future__ import print_function
import os
%matplotlib inline
from matplotlib.pyplot import *
from msmbuilder.featurizer import SuperposeFeaturizer
from msmbuilder.example_datasets import AlanineDipeptide
from msmbuilder.hmm import GaussianHMM
from msmbuilder.cluster import KCenters
from msmbuilder.msm import MarkovStateModel

First: load and "featurize"

Featurization refers to the process of converting the conformational snapshots from your MD trajectories into vectors in some space $\mathbb{R}^N$ that can be manipulated and modeled by subsequent analyses. The Gaussian HMM, for instance, uses Gaussian emission distributions, so it models the trajectory as a time-dependent mixture of multivariate Gaussians.

In general, the featurization is somewhat of an art. For this example, we're using Mixtape's SuperposeFeaturizer, which superposes each snapshot onto a reference frame (trajectories[0][0] in this example), and then measure the distance from each atom to its position in the reference conformation as the 'feature'

In [2]:
print(AlanineDipeptide.description())

dataset = AlanineDipeptide().get()
trajectories = dataset.trajectories
topology = trajectories[0].topology

indices = [atom.index for atom in topology.atoms if atom.element.symbol in ['C', 'O', 'N']]
featurizer = SuperposeFeaturizer(indices, trajectories[0][0])
sequences = featurizer.transform(trajectories)
The dataset consists of ten 10ns trajectories of of alanine dipeptide,
simulated using OpenMM 6.0.1 (CUDA platform, NVIDIA GTX660) with the
AMBER99SB-ILDN force field at 300K (langevin dynamics, friction coefficient
of 91/ps, timestep of 2fs) with GBSA implicit solvent. The coordinates are
saved every 1ps. Each trajectory contains 9,999 snapshots.

The dataset, including the script used to generate the dataset
is available on figshare at

http://dx.doi.org/10.6084/m9.figshare.1026131

Now sequences is our featurized data.

In [3]:
lag_times = [1, 10, 20, 30, 40]
hmm_ts0 = {}
hmm_ts1 = {}
n_states = [3, 5]

for n in n_states:
    hmm_ts0[n] = []
    hmm_ts1[n] = []
    for lag_time in lag_times:
        strided_data = [s[i::lag_time] for s in sequences for i in range(lag_time)]
        hmm = GaussianHMM(n_states=n, n_init=1).fit(strided_data)
        timescales = hmm.timescales_ * lag_time
        hmm_ts0[n].append(timescales[0])
        hmm_ts1[n].append(timescales[1])
        print('n_states=%d\tlag_time=%d\ttimescales=%s' % (n, lag_time, timescales))
    print()
n_states=3	lag_time=1	timescales=[ 127.4874661     3.99015125]
n_states=3	lag_time=10	timescales=[ 208.90710932    6.04015815]
n_states=3	lag_time=20	timescales=[ 222.64990208    6.23338075]
n_states=3	lag_time=30	timescales=[ 228.06846726    6.1386462 ]
n_states=3	lag_time=40	timescales=[ 230.57404963    7.48969401]

n_states=5	lag_time=1	timescales=[ 133.25164039    4.31735137    1.68092002    0.94390323]
n_states=5	lag_time=10	timescales=[ 207.85914733    6.32892925    2.54288815    1.95523612]
n_states=5	lag_time=20	timescales=[ 221.74233454    6.2312583     4.33380425]
n_states=5	lag_time=30	timescales=[ 228.47614015    7.84501364    6.80742261]
n_states=5	lag_time=40	timescales=[ 230.65893183    9.03919961    7.74149687]

In [4]:
figure(figsize=(14,3))

for i, n in enumerate(n_states):
    subplot(1,len(n_states),1+i)
    plot(lag_times, hmm_ts0[n])
    plot(lag_times, hmm_ts1[n])
    if i == 0:
        ylabel('Relaxation Timescale')
    xlabel('Lag Time')
    title('%d states' % n)

show()
In [5]:
msmts0, msmts1 = {}, {}
lag_times = [1, 10, 20, 30, 40]
n_states = [4, 8, 16, 32, 64]

for n in n_states:
    msmts0[n] = []
    msmts1[n] = []
    for lag_time in lag_times:
        assignments = KCenters(n_clusters=n).fit_predict(sequences)
        msm = MarkovStateModel(lag_time=lag_time, verbose=False).fit(assignments)
        timescales = msm.timescales_
        msmts0[n].append(timescales[0])
        msmts1[n].append(timescales[1])
        print('n_states=%d\tlag_time=%d\ttimescales=%s' % (n, lag_time, timescales[0:2]))
    print()
n_states=4	lag_time=1	timescales=[ 56.05045681          nan]
n_states=4	lag_time=10	timescales=[ 74.68045608          nan]
n_states=4	lag_time=20	timescales=[ 98.57968305          nan]
n_states=4	lag_time=30	timescales=[ 194.41696132           nan]
n_states=4	lag_time=40	timescales=[ 156.62098769           nan]

n_states=8	lag_time=1	timescales=[ 36.16503828   3.43813875]
n_states=8	lag_time=10	timescales=[ 128.28227085    4.38764974]
n_states=8	lag_time=20	timescales=[ 196.71685273    6.30630612]
n_states=8	lag_time=30	timescales=[ 222.58626795    6.27674017]
n_states=8	lag_time=40	timescales=[ 214.52087525    9.17410815]

n_states=16	lag_time=1	timescales=[ 104.13745165    3.56771839]
n_states=16	lag_time=10	timescales=[ 211.72398397    6.13696567]
n_states=16	lag_time=20	timescales=[ 215.84113098    5.77616761]
n_states=16	lag_time=30	timescales=[ 220.62276021    7.72321291]
n_states=16	lag_time=40	timescales=[ 231.71368666   10.32642675]

n_states=32	lag_time=1	timescales=[ 126.90552031    4.31607204]
n_states=32	lag_time=10	timescales=[ 219.09782761    6.30147665]
n_states=32	lag_time=20	timescales=[ 229.28581876    6.46755887]
n_states=32	lag_time=30	timescales=[ 232.31203506    8.08615089]
n_states=32	lag_time=40	timescales=[ 235.57151296   11.1373825 ]

n_states=64	lag_time=1	timescales=[ 158.82014145    5.11431161]
n_states=64	lag_time=10	timescales=[ 230.61590783    6.56993299]
n_states=64	lag_time=20	timescales=[ 233.39409051    6.64298217]
n_states=64	lag_time=30	timescales=[ 235.50606004    9.16533876]
n_states=64	lag_time=40	timescales=[ 237.63962116   12.0611693 ]

In [6]:
figure(figsize=(14,3))

for i, n in enumerate(n_states):
    subplot(1,len(n_states),1+i)
    plot(lag_times, msmts0[n])
    plot(lag_times, msmts1[n])
    if i == 0:
        ylabel('Relaxation Timescale')
    xlabel('Lag Time')
    title('%d states' % n)

show()
In [7]:
 

(hmm-and-msm.ipynb; hmm-and-msm_evaluated.ipynb; hmm-and-msm.py)

Versions