# NMR Models¶

This example shows how to perform principal component analysis (PCA) of an ensemble of NMR models. The protein of interest is ubiquitin, and for illustration puposes, we will repeat the calculations for the ensemble of ubiquitin models that were analyzed in [AB09].

A `PCA`

object that stores the covariance matrix and principal modes that
describe the dominant changes in the dataset will be obtained. `PCA`

and principal modes (`Mode`

) can be used as input to functions in
`dynamics`

module for further analysis.

## Notes¶

Note that this example is only slightly different from that in the ProDy Tutorial. Also, note that this example applies to any PDB file that contains multiple models.

## Prepare ensemble¶

We start by importing everything from the ProDy package:

```
In [1]: from prody import *
In [2]: from pylab import *
In [3]: ion()
```

We parse only Cα atoms using `parsePDB()`

(note that it is possible to
repeat this calculation for all atoms):

```
In [4]: ubi = parsePDB('2k39', subset='ca')
```

We use residues 1 to 70, as residues 71 to 76 are very mobile and including them skews the results.

```
In [5]: ubi = ubi.select('resnum < 71').copy()
```

```
In [6]: ensemble = Ensemble('Ubiquitin NMR ensemble')
In [7]: ensemble.setCoords( ubi.getCoords() )
```

Then, we add all of the coordinate sets to the ensemble, and perform an iterative superposition:

```
In [8]: ensemble.addCoordset( ubi.getCoordsets() )
In [9]: ensemble.iterpose()
```

We can then make a new Atomgroup object where we replace the coordinates with the ones from the ensemble as follows:

```
In [10]: ubi_copy = ubi.copy()
In [11]: ubi_copy.delCoordset(range(ubi_copy.numCoordsets()))
In [12]: ubi_copy.addCoordset(ensemble.getCoordsets())
```

## PCA calculations¶

Performing `PCA`

is only three lines of code:

```
In [13]: pca = PCA('Ubiquitin')
In [14]: pca.buildCovariance(ensemble)
In [15]: pca.calcModes()
In [16]: repr(pca)
Out[16]: '<PCA: Ubiquitin (20 modes; 70 atoms)>'
```

**Faster method**

Principal modes can be calculated faster using singular value decomposition:

```
In [17]: svd = PCA('Ubiquitin')
In [18]: svd.performSVD(ensemble)
```

For heterogeneous NMR datasets, both methods yields identical results:

```
In [19]: abs(svd.getEigvals()[:20] - pca.getEigvals()).max()
Out[19]: 1.2434497875801753e-14
In [20]: abs(calcOverlap(pca, svd).diagonal()[:20]).min()
Out[20]: 0.9999999999999993
```

## Write NMD file¶

Write principal modes into an NMD Format file for NMWiz using
`writeNMD()`

function:

```
In [21]: writeNMD('ubi_pca.nmd', pca[:3], ubi)
Out[21]: 'ubi_pca.nmd'
```

## Print data¶

Let’s print fraction of variance for top ranking 4 PCs (listed in Table S3):

```
In [22]: for mode in pca[:4]:
....: print(calcFractVariance(mode).round(3))
....:
0.134
0.094
0.083
0.065
```

## Compare with ANM results¶

We set the active coordinate set of ubi_copy to model 79, which is the one
that is closest to the mean structure (note that indices start from 0 in Python
so we give it 78).
Then, we perform ANM calculations using `calcANM()`

for the active
coordset:

```
In [23]: ubi_copy.setACSIndex(78)
In [24]: anm, temp = calcANM(ubi_copy)
In [25]: anm.setTitle('Ubiquitin')
```

We calculate overlaps between ANM and PCA modes (presented in Table 1).
`printOverlapTable()`

function is handy to print a formatted overlap
table:

```
In [26]: printOverlapTable(pca[:4], anm[:4])
Overlap Table
ANM Ubiquitin
#1 #2 #3 #4
PCA Ubiquitin #1 -0.19 -0.30 +0.22 -0.62
PCA Ubiquitin #2 +0.09 -0.72 -0.16 +0.16
PCA Ubiquitin #3 +0.31 -0.06 -0.23 0.00
PCA Ubiquitin #4 +0.11 +0.02 +0.16 -0.31
```