Antkeeper  0.0.1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
protein.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_GENETICS_PROTEIN_HPP
21 #define ANTKEEPER_GENETICS_PROTEIN_HPP
22 
24 #include <type_traits>
25 
26 namespace genetics {
27 
29 namespace protein {
30 
38 template <class T, class ForwardIt1, class ForwardIt2>
39 T identity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2);
40 
49 template <class ForwardIt1, class ForwardIt2, class Matrix>
50 typename std::remove_all_extents<Matrix>::type score(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, const Matrix& matrix);
51 
59 template <class T, class ForwardIt1, class ForwardIt2, class Matrix>
60 typename T similarity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, const Matrix& matrix);
61 
62 template <class T, class ForwardIt1, class ForwardIt2>
63 T identity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2)
64 {
65  auto length = std::distance(first1, last1);
66 
67  T sum = 0;
68  for (; first1 != last1; ++first1, ++first2)
69  if (*first1 == *first2)
70  ++sum;
71 
72  return sum / static_cast<T>(length);
73 }
74 
75 template <class ForwardIt1, class ForwardIt2, class Matrix>
76 typename std::remove_all_extents<Matrix>::type score(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, const Matrix& matrix)
77 {
78  typename std::remove_all_extents<Matrix>::type result = 0;
79  for (; first1 != last1; ++first1, ++first2)
80  result += amino_acid::score(*first1, *first2, matrix);
81  return result;
82 }
83 
84 template <class T, class ForwardIt1, class ForwardIt2, class Matrix>
85 typename T similarity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, const Matrix& matrix)
86 {
87  T length = static_cast<T>(std::distance(first1, last1));
88  T positive_count = T(0);
89 
90  for (; first1 != last1; ++first1, ++first2)
91  if (amino_acid::score(*first1, *first2, matrix) > 0)
92  ++positive_count;
93 
94  return positive_count / length;
95 }
96 
97 } // namespace protein
98 } // namespace genetics
99 
100 #endif // ANTKEEPER_GENETICS_PROTEIN_HPP
std::remove_all_extents< Matrix >::type score(char a, char b, const Matrix &matrix)
Scores two amino acids using a substitution matrix.
Definition: amino-acid.hpp:42
T similarity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, const Matrix &matrix)
Returns the percent similarity between two proteins.
Definition: protein.hpp:85
std::remove_all_extents< Matrix >::type score(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, const Matrix &matrix)
Scores two proteins using a substitution matrix.
Definition: protein.hpp:76
T identity(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2)
Returns the percent identity between two proteins.
Definition: protein.hpp:63
Genetic algorithms.
Definition: amino-acid.hpp:25
T length(const quaternion< T > &q)
Calculates the length of a quaternion.
Definition: quaternion.hpp:602
T distance(const vector< T, N > &p0, const vector< T, N > &p1)
Calculates the distance between two points.
Definition: vector.hpp:1106
constexpr T sum(const vector< T, N > &x) noexcept
Calculates the sum of all elements in a vector.
Definition: vector.hpp:1585