From 61acb788f682e8a26a22b94d65b49b07d7dc3498 Mon Sep 17 00:00:00 2001 From: Tobias Schmidt <tobias.schmidt@unibas.ch> Date: Wed, 13 Jul 2011 14:18:25 +0200 Subject: [PATCH] fixes weight matrix problem for non-alpha characters (BZDNG-280) --- modules/seq/alg/src/subst_weight_matrix.hh | 31 +++++++++++++++++----- modules/seq/alg/tests/CMakeLists.txt | 1 + 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/modules/seq/alg/src/subst_weight_matrix.hh b/modules/seq/alg/src/subst_weight_matrix.hh index cdf0fc83a..f310893ab 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 993352440..3a26d1d67 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}") -- GitLab