In [2]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

Loading MDAnalysis


In [5]:
import MDAnalysis as mda
mda.__version__


Out[5]:
'0.14.0-dev0'

The Universe

The basic object to load structures and trajectories into MDAnalysis are Universes. Universe take a topology and a trajectory (optional) as arguments


In [13]:
TOPOLOGY = 'data/adk.psf'

u = mda.Universe(TOPOLOGY)

Universe extract all the information in the topology and make them available to you. For example you can see how many atoms are in the topology


In [14]:
u.atoms.n_atoms


Out[14]:
3341

Or the number of individual residues


In [15]:
u.residues.n_residues


Out[15]:
214

We can also leverage numpy to see how many unique residue types there are. Residue names are stored in resnames.


In [22]:
np.unique(u.residues.resnames)


Out[22]:
array(['ALA', 'ARG', 'ASN', 'ASP', 'CYS', 'GLN', 'GLU', 'GLY', 'HSD',
       'ILE', 'LEU', 'LYS', 'MET', 'PHE', 'PRO', 'SER', 'THR', 'TYR', 'VAL'], 
      dtype='|S3')

Select parts of a Topology

mostly we don't want to work on a complete topology but only analyze a specific part (selection) of it. For this we can create selections. If we want to select all backbone atoms for example


In [23]:
bb = u.atoms.select_atoms('backbone')

To check that only backbone atoms are selected we can look if we only have C, CA, N and O atoms


In [29]:
print('number of backbone atoms : {}'.format(bb.n_atoms))
print('unique atoms types : {}'.format(np.unique(bb.names)))


number of backbone atoms : 855
unique atoms types : ['C' 'CA' 'N' 'O']

Or we want to only select the C$_{\alpha}$-atoms.


In [30]:
ca = u.atoms.select_atoms('protein and name CA')

In [31]:
print('number of Ca atoms : {}'.format(ca.n_atoms))
print('unique atoms types : {}'.format(np.unique(ca.names)))


number of Ca atoms : 214
unique atoms types : ['CA']

The complete syntax and possible selection can be look up here

NOTE

It is very important to select C$_{\alpha}$-atoms with the string protein and name CA because Calcium ions also have the name CA.

Load a trajectory

To load a simulation we can give the Universe object also a trajectory file as an argument


In [32]:
TRAJECTORY = 'data/adk_dims.dcd'

u = mda.Universe(TOPOLOGY, TRAJECTORY)

now we can look how many frames the trajectory has


In [33]:
u.trajectory.n_frames


Out[33]:
98

Iterating over a trajectory work pythonically.


In [34]:
for time_step in u.trajectory:
    print(time_step.time)


0.0
0.99999991192
1.99999982384
2.99999973576
3.99999964768
4.9999995596
5.99999947152
6.99999938344
7.99999929536
8.99999920728
9.9999991192
10.9999990311
11.999998943
12.999998855
13.9999987669
14.9999986788
15.9999985907
16.9999985026
17.9999984146
18.9999983265
19.9999982384
20.9999981503
21.9999980622
22.9999979742
23.9999978861
24.999997798
25.9999977099
26.9999976218
27.9999975338
28.9999974457
29.9999973576
30.9999972695
31.9999971814
32.9999970934
33.9999970053
34.9999969172
35.9999968291
36.999996741
37.999996653
38.9999965649
39.9999964768
40.9999963887
41.9999963006
42.9999962126
43.9999961245
44.9999960364
45.9999959483
46.9999958602
47.9999957722
48.9999956841
49.999995596
50.9999955079
51.9999954198
52.9999953318
53.9999952437
54.9999951556
55.9999950675
56.9999949794
57.9999948914
58.9999948033
59.9999947152
60.9999946271
61.999994539
62.999994451
63.9999943629
64.9999942748
65.9999941867
66.9999940986
67.9999940106
68.9999939225
69.9999938344
70.9999937463
71.9999936582
72.9999935702
73.9999934821
74.999993394
75.9999933059
76.9999932178
77.9999931298
78.9999930417
79.9999929536
80.9999928655
81.9999927774
82.9999926894
83.9999926013
84.9999925132
85.9999924251
86.999992337
87.999992249
88.9999921609
89.9999920728
90.9999919847
91.9999918966
92.9999918086
93.9999917205
94.9999916324
95.9999915443
96.9999914562