Evolution Analysis

This part follows from MSA Files. The aim of this part is to show how to calculate residue conservation and coevolution properties based on multiple sequence alignments (MSAs). MSA

First, we import everything from the ProDy package.

In [1]: from prody import *

In [2]: from pylab import *

In [3]: ion()  # turn interactive mode on

Get MSA data

Let’s download full MSA file for protein family RnaseA. We can do this by specifying the PDB ID of a protein in this family.

In [4]: searchPfam('1K2A').keys()
Out[4]: ['PF00074']
In [5]: fetchPfamMSA('PF00074')
Out[5]: 'PF00074_full.sth'

Let’s parse the downloaded file:

In [6]: msa = parseMSA('PF00074_full.sth')

Refine MSA

Here, we refine the MSA to decrease the number of gaps. We will remove any columns in the alignment for which there is a gap in the specified PDB file, and then remove any rows that have more than 20% gaps. refineMSA() does all of this and returns an MSA object.

In [7]: msa_refine = refineMSA(msa, label='RNAS2_HUMAN', rowocc=0.8, seqid=0.98)

In [8]: msa_refine
Out[8]: <MSA: PF00074_full refined (label=RNAS2_HUMAN, rowocc>=0.8, seqid>=0.98) (533 sequences, 128 residues)>

MSA is refined based on the sequence of RNAS2_HUMAN, corresponding to 1K2A.

Occupancy calculation

Evol plotting functions are prefixed with show. We can plot the occupancy for each column to see if there are any positions in the MSA that have a lot of gaps. We use the function showMSAOccupancy() that uses calcMSAOccupancy() to calculate occupancy for MSA.

In [9]: showMSAOccupancy(msa_refine, occ='res');
../../_images/msa_analysis_occ_res.png

Let’s find the minimum:

In [10]: calcMSAOccupancy(msa_refine, occ='res').min();

We can also specify indices based on the PDB.

In [11]: indices = list(range(4,130))

In [12]: showMSAOccupancy(msa_refine, occ='res', indices=indices);
../../_images/msa_analysis_occ_res_indices.png

Further refining the MSA to remove positions that have low occupancy will change the start and end positions of the labels in the MSA. This is not corrected automatically on refinement. We can also plot occupancy based on rows for the sequences in the MSA.

Entropy Calculation

Here, we show how to calculate and plot Shannon Entropy. Entropy for each position in the MSA is calculated using calcShannonEntropy(). It takes MSA object or a numpy 2D array containg MSA as input and returns a 1D numpy array.

In [13]: entropy = calcShannonEntropy(msa_refine)

In [14]: entropy
Out[14]: 
array([ 2.63800919,  2.45450938,  1.42041785,  2.15639571,  1.63907879,
        2.09393186,  1.82330678,  0.25908259,  2.00605129,  1.69565943,
        0.86225618,  0.37467677,  1.61399529,  2.152871  ,  2.37773811,
        2.03304806,  2.22500579,  2.47038886,  1.39355386,  0.04095142,
        0.96975454,  2.49565623,  1.40664469,  0.42308606,  2.1870084 ,
        2.1912902 ,  1.37821019,  1.78935302,  1.9773083 ,  1.66724354,
        2.38991496,  2.16435735,  2.23861691,  0.07666912,  0.53090604,
        2.17685992,  2.05601916,  0.39718599,  0.66921337,  0.22814177,
        1.13067592,  0.73408808,  1.7134237 ,  1.75080943,  1.99947939,
        2.42045936,  1.86751707,  1.03884166,  2.06000272,  1.84548923,
        1.30756389,  0.14081204,  2.40116039,  2.06824591,  2.20118327,
        1.86602952,  1.93722842,  2.31294099,  0.55243632,  1.40868441,
        1.77233537,  1.45889218,  1.25211336,  1.65401333,  2.32637287,
        2.50318713,  0.95322287,  0.6088434 ,  1.516984  ,  2.16086557,
        0.49502443,  2.2973572 ,  1.93483556,  2.31261882,  1.4581374 ,
        2.2167319 ,  1.51758558,  0.5770289 ,  2.04402151,  0.07524241,
        1.93959113,  1.44071683,  1.42274434,  1.9023148 ,  1.81370905,
        1.70753063,  1.89902445,  0.81650334,  1.71242241,  1.68083773,
        0.98479639,  1.8581422 ,  0.04095142,  2.13080516,  0.06205112,
        2.01730025,  1.96933716,  2.09058798,  2.47214553,  2.38254327,
        2.44521881,  1.69081665,  2.15967129,  1.40280104,  1.65347276,
        0.90701065,  0.96741081,  0.1092195 ,  1.61548777,  1.75714973,
        2.15960565,  1.78756768,  1.92852948,  2.08465504,  0.42115912,
        0.3488321 ,  1.25056073,  0.9596198 ,  2.35731046,  1.26015433,
        1.54531754,  1.3825499 ,  1.27762709,  0.01552932,  0.41832099,
        0.32073906,  1.12347468,  0.12739974])

entropy is a 1D Numpy array. Plotting is done using showShannonEntropy().

In [15]: showShannonEntropy(entropy, indices);
../../_images/msa_analysis_entropy.png

Mutual Information

We can calculate mutual information between the positions of the MSA using buildMutinfoMatrix() which also takes an MSA object or a numpy 2D array containing MSA as input.

In [16]: mutinfo = buildMutinfoMatrix(msa_refine)

In [17]: mutinfo
Out[17]: 
array([[ 0.        ,  0.80235226,  0.55114673, ...,  0.16578316,
         0.31217035,  0.10893036],
       [ 0.80235226,  0.        ,  0.59997967, ...,  0.13780672,
         0.40552489,  0.10436392],
       [ 0.55114673,  0.59997967,  0.        , ...,  0.16524   ,
         0.40110279,  0.13758264],
       ..., 
       [ 0.16578316,  0.13780672,  0.16524   , ...,  0.        ,
         0.57198283,  0.35523316],
       [ 0.31217035,  0.40552489,  0.40110279, ...,  0.57198283,
         0.        ,  0.39735979],
       [ 0.10893036,  0.10436392,  0.13758264, ...,  0.35523316,
         0.39735979,  0.        ]])

Result is a 2D Numpy array.

We can also apply normalization using applyMutinfoNorm() and correction using applyMutinfoCorr() to the mutual information matrix based on references [Martin05] and [Dunn08], respectively.

[Martin05]Martin LC, Gloor GB, Dunn SD, Wahl LM. Using information theory to search for co-evolving residues in proteins. Bioinformatics 2005 21(22):4116-4124.
[Dunn08]Dunn SD, Wahl LM, Gloor GB. Mutual information without the influence of phylogeny or entropy dramatically improves residue contact prediction. Bioinformatics 2008 24(3):333-340.
In [18]: mutinfo_norm = applyMutinfoNorm(mutinfo, entropy, norm='minent')

In [19]: mutinfo_corr = applyMutinfoCorr(mutinfo, corr='apc')

Note that by default norm="sument" normalization is applied in applyMutinfoNorm and corr="prod" is applied in applyMutinfoCorr.

Now we plot the mutual information matrices that we obtained above and see the effects of different corrections and normalizations.

In [20]: showMutinfoMatrix(mutinfo);
../../_images/msa_analysis_mutinfo.png
In [21]: showMutinfoMatrix(mutinfo_corr, clim=[0, mutinfo_corr.max()],
   ....:    xlabel='1K2A: 4-131');
   ....: 
../../_images/msa_analysis_mutinfo_corr.png

Output Results

Here we show how to write the mutual information and entropy arrays to file. We use the writeArray() to write Numpy array data.

In [22]: writeArray('1K2A_MI.txt', mutinfo)
Out[22]: '1K2A_MI.txt'

This can be later loaded using parseArray().

Rank-ordering

Further analysis can also be done by rank ordering the matrix and analyzing the pairs with highest mutual information or the most co-evolving residues. This is done using calcRankorder(). A z-score normalization can also be applied to select coevolving pairs based on a z score cutoff.

In [23]: rank_row, rank_col, zscore_sort = calcRankorder(mutinfo, zscore=True)

In [24]: asarray(indices)[rank_row[:5]]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-24-215b7120867d> in <module>()
----> 1 asarray(indices)[rank_row[:5]]

IndexError: index 126 is out of bounds for axis 1 with size 126

In [25]: asarray(indices)[rank_col[:5]]
Out[25]: array([127, 127, 129, 127, 128])

In [26]: zscore_sort[:5]
Out[26]: array([ 4.86370837,  4.35821895,  4.04974775,  4.03299096,  3.42713102])