diff --git a/src/game/genetics/protein.hpp b/src/game/genetics/protein.hpp index 3786f2b..c95f6b5 100644 --- a/src/game/genetics/protein.hpp +++ b/src/game/genetics/protein.hpp @@ -40,21 +40,22 @@ T identity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2); * Scores two proteins using a substitution matrix. * * @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. * @return Score of the two proteins. */ template -typename std::remove_all_extents::type score(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2, const Matrix& matrix); +typename std::remove_all_extents::type score(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, const Matrix& matrix); -template -typename std::remove_all_extents::type score(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2, const Matrix& matrix) -{ - typename std::remove_all_extents::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 +typename T similarity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, const Matrix& matrix); template T identity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2) @@ -69,6 +70,28 @@ T identity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2) return sum / static_cast(length); } +template +typename std::remove_all_extents::type score(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, const Matrix& matrix) +{ + typename std::remove_all_extents::type result = 0; + for (; first1 != last1; ++first1, ++first2) + result += amino_acid::score(*first1, *first2, matrix); + return result; +} + +template +typename T similarity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, const Matrix& matrix) +{ + T length = static_cast(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 genetics