# 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 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 slightly different from that in the ProDy Tutorial.
This example uses `Ensemble`

which has a method for performing
iterative superposition.

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='calpha')
```

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()
```

### PCA calculations¶

Performing `PCA`

is only three lines of code:

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

**Faster method**

Principal modes can be calculated faster using singular value decomposition:

```
In [14]: svd = PCA('Ubiquitin')
In [15]: svd.performSVD(ensemble)
```

For heterogeneous NMR datasets, both methods yields identical results:

```
In [16]: abs(svd.getEigvals()[:20] - pca.getEigvals()).max()
Out[16]: 6.6613381477509392e-15
In [17]: abs(calcOverlap(pca, svd).diagonal()[:20]).min()
Out[17]: 0.99999999999999956
```

### Write NMD file¶

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

function:

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

### Print data¶

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

```
In [19]: 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 to 79, which is the one that is closest
to the mean structure (note that indices start from 0 in Python).
Then, we perform ANM calculations using `calcANM()`

for the active
coordset:

```
In [20]: ubi.setACSIndex(78)
In [21]: anm, temp = calcANM(ubi)
In [22]: 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 [23]: 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
```