From af8b516e7a49523d765900b0cb362fb78ee3ee0f Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Sat, 26 Dec 2020 20:39:11 +0800 Subject: [PATCH] Document genetics::protein::identity function --- src/game/genetics/protein.hpp | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/game/genetics/protein.hpp b/src/game/genetics/protein.hpp index 4e3ab38..3786f2b 100644 --- a/src/game/genetics/protein.hpp +++ b/src/game/genetics/protein.hpp @@ -26,11 +26,21 @@ namespace genetics { namespace protein { +/** + * Returns the percent identity 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 identity between the two proteins. + */ +template +T identity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2); + /** * Scores two proteins using a substitution matrix. * - * @param first1,last1 Sequence of IUPAC amino acid codes which constitute the first protein. - * @param first2,last2 Sequence of IUPAC amino acid codes which constitute the second 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 matrix Substitution matrix. * @return Score of the two proteins. */ @@ -46,6 +56,19 @@ typename std::remove_all_extents::type score(ForwardIt1 first1, ForwardI return result; } +template +T identity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2) +{ + auto length = std::distance(first1, last1); + + T sum = 0; + for (; first1 != last1; ++first1, ++first2) + if (*first1 == *first2) + ++sum; + + return sum / static_cast(length); +} + } // namespace protein } // namespace genetics