Browse Source

Add function for calculating the percent similarity between two proteins

master
C. J. Howard 4 years ago
parent
commit
64131116ff
1 changed files with 33 additions and 10 deletions
  1. +33
    -10
      src/game/genetics/protein.hpp

+ 33
- 10
src/game/genetics/protein.hpp View File

@ -40,21 +40,22 @@ T identity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2);
* Scores two proteins using a substitution matrix. * Scores two proteins using a substitution matrix.
* *
* @param first1,last1 Range of IUPAC amino acid codes which constitute the first protein. * @param first1,last1 Range of IUPAC amino acid codes which constitute the first protein.
* @param first2,last2 Range of IUPAC amino acid codes which constitute the second protein.
* @param first2 Beginning of the range of IUPAC amino acid codes which constitute the second protein.
* @param matrix Substitution matrix. * @param matrix Substitution matrix.
* @return Score of the two proteins. * @return Score of the two proteins.
*/ */
template <class ForwardIt1, class ForwardIt2, class Matrix> template <class ForwardIt1, class ForwardIt2, class Matrix>
typename std::remove_all_extents<Matrix>::type score(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2, const Matrix& matrix);
typename std::remove_all_extents<Matrix>::type score(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, const Matrix& matrix);
template <class ForwardIt1, class ForwardIt2, class Matrix>
typename std::remove_all_extents<Matrix>::type score(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2, const Matrix& matrix)
{
typename std::remove_all_extents<Matrix>::type result = 0;
for (; first1 != last1 && first2 != last2; ++first1, ++first2)
result += amino_acid::score(*first1, *first2, matrix);
return result;
}
/**
* Returns the percent similarity between two proteins.
*
* @param first1,last1 Range of IUPAC amino acids which constitute the first protein.
* @param first2 Beginning of the range of IUPAC amino acids which constitute the second protein.
* @return Percent similarity between the two proteins.
*/
template <class T, class ForwardIt1, class ForwardIt2, class Matrix>
typename T similarity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, const Matrix& matrix);
template <class T, class ForwardIt1, class ForwardIt2> template <class T, class ForwardIt1, class ForwardIt2>
T identity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2) T identity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2)
@ -69,6 +70,28 @@ T identity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2)
return sum / static_cast<T>(length); return sum / static_cast<T>(length);
} }
template <class ForwardIt1, class ForwardIt2, class Matrix>
typename std::remove_all_extents<Matrix>::type score(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, const Matrix& matrix)
{
typename std::remove_all_extents<Matrix>::type result = 0;
for (; first1 != last1; ++first1, ++first2)
result += amino_acid::score(*first1, *first2, matrix);
return result;
}
template <class T, class ForwardIt1, class ForwardIt2, class Matrix>
typename T similarity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, const Matrix& matrix)
{
T length = static_cast<T>(std::distance(first1, last1));
T positive_count = T(0);
for (; first1 != last1; ++first1, ++first2)
if (amino_acid::score(*first1, *first2, matrix) > 0)
++positive_count;
return positive_count / length;
}
} // namespace protein } // namespace protein
} // namespace genetics } // namespace genetics

Loading…
Cancel
Save