| 1 | #include <limits.h> |
|---|
| 2 | #include <math.h> |
|---|
| 3 | #include "muscle.h" |
|---|
| 4 | #include "msa.h" |
|---|
| 5 | #include "distfunc.h" |
|---|
| 6 | #include "msa.h" |
|---|
| 7 | #include "seqvect.h" |
|---|
| 8 | #include "pwpath.h" |
|---|
| 9 | |
|---|
| 10 | // ScoreDist |
|---|
| 11 | // E. Sonnhammer & V. Hollich, Scoredist: A simple and robust protein sequence |
|---|
| 12 | // distance estimator, BMC Bioinformatics 2005, 6:108. |
|---|
| 13 | |
|---|
| 14 | extern int BLOSUM62[20][20]; |
|---|
| 15 | extern double BLOSUM62_Expected; |
|---|
| 16 | |
|---|
| 17 | static const double Dayhoff_CalibrationFactor = 1.3370; |
|---|
| 18 | static const double JTT_CalibrationFactor = 1.2873; |
|---|
| 19 | static const double MV_CalibrationFactor = 1.1775; |
|---|
| 20 | static const double LARGE_D = 3.0; |
|---|
| 21 | |
|---|
| 22 | static double CalibrationFactor = JTT_CalibrationFactor; |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | // Similarity score |
|---|
| 26 | static double Sigma(const MSA &msa, unsigned SeqIndex1, unsigned SeqIndex2, |
|---|
| 27 | unsigned *ptrLength) |
|---|
| 28 | { |
|---|
| 29 | unsigned Length = 0; |
|---|
| 30 | double Score = 0; |
|---|
| 31 | const unsigned ColCount = msa.GetColCount(); |
|---|
| 32 | for (unsigned ColIndex = 0; ColIndex < ColCount; ++ColIndex) |
|---|
| 33 | { |
|---|
| 34 | unsigned Letter1 = msa.GetLetterEx(SeqIndex1, ColIndex); |
|---|
| 35 | unsigned Letter2 = msa.GetLetterEx(SeqIndex2, ColIndex); |
|---|
| 36 | if (Letter1 >= 20 || Letter2 >= 20) |
|---|
| 37 | continue; |
|---|
| 38 | ++Length; |
|---|
| 39 | Score += BLOSUM62[Letter1][Letter2]; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | *ptrLength = Length; |
|---|
| 43 | return Score; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | // Normalized score |
|---|
| 47 | static double Sigma_N(const MSA &msa, unsigned SeqIndex1, unsigned SeqIndex2) |
|---|
| 48 | { |
|---|
| 49 | unsigned Length = UINT_MAX; |
|---|
| 50 | double Score = Sigma(msa, SeqIndex1, SeqIndex2, &Length); |
|---|
| 51 | double RandomScore = Length*BLOSUM62_Expected; |
|---|
| 52 | return Score - RandomScore; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | // Upper limit |
|---|
| 56 | static double Sigma_U(const MSA &msa, unsigned SeqIndex1, unsigned SeqIndex2, |
|---|
| 57 | unsigned *ptrLength) |
|---|
| 58 | { |
|---|
| 59 | double Score11 = Sigma(msa, SeqIndex1, SeqIndex1, ptrLength); |
|---|
| 60 | double Score22 = Sigma(msa, SeqIndex2, SeqIndex2, ptrLength); |
|---|
| 61 | return (Score11 + Score22)/2; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | // Normalized upper limit |
|---|
| 65 | static double Sigma_UN(const MSA &msa, unsigned SeqIndex1, unsigned SeqIndex2) |
|---|
| 66 | { |
|---|
| 67 | unsigned Length = UINT_MAX; |
|---|
| 68 | double Score = Sigma_U(msa, SeqIndex1, SeqIndex2, &Length); |
|---|
| 69 | double RandomScore = Length*BLOSUM62_Expected; |
|---|
| 70 | return Score - RandomScore; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | double GetScoreDist(const MSA &msa, unsigned SeqIndex1, unsigned SeqIndex2) |
|---|
| 74 | { |
|---|
| 75 | if (g_Alpha != ALPHA_Amino) |
|---|
| 76 | Quit("Scoredist is only for amino acid sequences"); |
|---|
| 77 | |
|---|
| 78 | double s_N = Sigma_N(msa, SeqIndex1, SeqIndex2); |
|---|
| 79 | double s_UN = Sigma_UN(msa, SeqIndex1, SeqIndex2); |
|---|
| 80 | double d = 0.0; |
|---|
| 81 | if (s_UN != 0) |
|---|
| 82 | { |
|---|
| 83 | double Ratio = s_N/s_UN; |
|---|
| 84 | if (Ratio < 0.001) |
|---|
| 85 | d = LARGE_D; |
|---|
| 86 | else |
|---|
| 87 | d = -log(Ratio); |
|---|
| 88 | } |
|---|
| 89 | return d*CalibrationFactor; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | void DistPWScoreDist(const SeqVect &v, DistFunc &DF) |
|---|
| 93 | { |
|---|
| 94 | SEQWEIGHT SeqWeightSave = GetSeqWeightMethod(); |
|---|
| 95 | SetSeqWeightMethod(SEQWEIGHT_Henikoff); |
|---|
| 96 | |
|---|
| 97 | const unsigned uSeqCount = v.Length(); |
|---|
| 98 | DF.SetCount(uSeqCount); |
|---|
| 99 | |
|---|
| 100 | const unsigned uPairCount = (uSeqCount*(uSeqCount + 1))/2; |
|---|
| 101 | unsigned uCount = 0; |
|---|
| 102 | SetProgressDesc("PW ScoreDist"); |
|---|
| 103 | for (unsigned uSeqIndex1 = 0; uSeqIndex1 < uSeqCount; ++uSeqIndex1) |
|---|
| 104 | { |
|---|
| 105 | const Seq &s1 = v.GetSeq(uSeqIndex1); |
|---|
| 106 | MSA msa1; |
|---|
| 107 | msa1.FromSeq(s1); |
|---|
| 108 | for (unsigned uSeqIndex2 = 0; uSeqIndex2 < uSeqIndex1; ++uSeqIndex2) |
|---|
| 109 | { |
|---|
| 110 | if (0 == uCount%20) |
|---|
| 111 | Progress(uCount, uPairCount); |
|---|
| 112 | ++uCount; |
|---|
| 113 | const Seq &s2 = v.GetSeq(uSeqIndex2); |
|---|
| 114 | MSA msa2; |
|---|
| 115 | msa2.FromSeq(s2); |
|---|
| 116 | |
|---|
| 117 | PWPath Path; |
|---|
| 118 | MSA msaOut; |
|---|
| 119 | AlignTwoMSAs(msa1, msa2, msaOut, Path, false, false); |
|---|
| 120 | |
|---|
| 121 | float d = (float) GetScoreDist(msaOut, 0, 1); |
|---|
| 122 | DF.SetDist(uSeqIndex1, uSeqIndex2, d); |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | ProgressStepsDone(); |
|---|
| 126 | |
|---|
| 127 | SetSeqWeightMethod(SeqWeightSave); |
|---|
| 128 | } |
|---|