diff --git a/modules/seq/alg/src/subst_weight_matrix.hh b/modules/seq/alg/src/subst_weight_matrix.hh index cdf0fc83a634651561044f30a40cc0df1613536a..f310893ab6a002a830d432a0b07d0547fbb3943d 100644 --- a/modules/seq/alg/src/subst_weight_matrix.hh +++ b/modules/seq/alg/src/subst_weight_matrix.hh @@ -47,24 +47,43 @@ public: ::memset(weights_, 0, sizeof(WeightType)*ALPHABET_SIZE*ALPHABET_SIZE); } + /// \brief Get the substitution weight between two amino acids + /// + /// If the amino acid single letter code is unknown (e.g. '?') + /// a weight of 0 is returned WeightType GetWeight(char aa_one, char aa_two) const { + if (!(IsAlpha(aa_one) && IsAlpha(aa_two))) { + return 0; + } int i=Index(aa_one, aa_two); - assert(i>=0 && i<ALPHABET_SIZE*ALPHABET_SIZE); - return weights_[i]; + return (i>=0 && i<ALPHABET_SIZE*ALPHABET_SIZE) ? weights_[i] : 0; } + /// \brief Set the substitution weight between two amino acids + /// + /// The weight is only set if the amino acid single letter code + /// is known (e.g. no weight is set for '?') void SetWeight(char aa_one, char aa_two, WeightType weight) { - int i=Index(aa_one, aa_two); - assert(i>=0 && i<ALPHABET_SIZE*ALPHABET_SIZE); - weights_[i]=weight; + if ((IsAlpha(aa_one) && IsAlpha(aa_two))) { + int i=Index(aa_one, aa_two); + if (i>=0 && i<ALPHABET_SIZE*ALPHABET_SIZE) { + weights_[i]=weight; + } + } } + private: int Index(char aa_one, char aa_two) const { return (toupper(aa_one)-'A')*ALPHABET_SIZE+(toupper(aa_two)-'A'); } - WeightType weights_[ALPHABET_SIZE*ALPHABET_SIZE]; + + /// \brief Check if uppercase character is one of [A-Z] + bool IsAlpha(char aa) const { + return (toupper(aa)>='A' && toupper(aa)<='Z'); + } + WeightType weights_[ALPHABET_SIZE*ALPHABET_SIZE]; }; SubstWeightMatrixPtr DLLEXPORT_OST_SEQ_ALG diff --git a/modules/seq/alg/tests/CMakeLists.txt b/modules/seq/alg/tests/CMakeLists.txt index 9933524408a7fd8bcf4107c72217fe6632b7f3d3..3a26d1d67c0f00c64b810a752eb38f7cfd174723 100644 --- a/modules/seq/alg/tests/CMakeLists.txt +++ b/modules/seq/alg/tests/CMakeLists.txt @@ -5,6 +5,7 @@ set(OST_SEQ_ALG_UNIT_TESTS test_renumber.py test_local_align.py test_global_align.py + test_weight_matrix.py ) ost_unittest(MODULE seq_alg SOURCES "${OST_SEQ_ALG_UNIT_TESTS}")