1 #ifndef ALGO_BLAST_GUMBEL_PARAMS__GENERAL_SCORE_MATRIX___HPP
2 #define ALGO_BLAST_GUMBEL_PARAMS__GENERAL_SCORE_MATRIX___HPP
3 
4 /* $Id: general_score_matrix.hpp 575325 2018-11-27 18:22:00Z ucko $
5 * ===========================================================================
6 *
7 *                            PUBLIC DOMAIN NOTICE
8 *               National Center for Biotechnology Information
9 *
10 *  This software/database is a "United States Government Work" under the
11 *  terms of the United States Copyright Act.  It was written as part of
12 *  the author's offical duties as a United States Government employee and
13 *  thus cannot be copyrighted.  This software/database is freely available
14 *  to the public for use. The National Library of Medicine and the U.S.
15 *  Government have not placed any restriction on its use or reproduction.
16 *
17 *  Although all reasonable efforts have been taken to ensure the accuracy
18 *  and reliability of the software and data, the NLM and the U.S.
19 *  Government do not and cannot warrant the performance or results that
20 *  may be obtained by using this software or data. The NLM and the U.S.
21 *  Government disclaim all warranties, express or implied, including
22 *  warranties of performance, merchantability or fitness for any particular
23 *  purpose.
24 *
25 *  Please cite the author in any work or product based on this material.
26 *
27 * ===========================================================================*/
28 
29 /*****************************************************************************
30 
31 File name: general_score_matrix.hpp
32 
33 Author: Greg Boratyn
34 
35 Contents: Generalized score matrix that can be set to any values.
36           Initialization to any of the standard score matrices is also
37           available.
38 
39 ******************************************************************************/
40 
41 
42 #include <corelib/ncbiobj.hpp>
43 #include <util/tables/raw_scoremat.h>
44 #include <vector>
45 
46 
47 BEGIN_NCBI_SCOPE
48 BEGIN_SCOPE(blast)
49 
50 /// Score matrix that can take any value
51 class CGeneralScoreMatrix : public CObject
52 {
53 public:
54     /// Names of standard scoring matrices
55     ///
56     enum EScoreMatrixName {
57         eBlosum45,
58         eBlosum62,
59         eBlosum80,
60         ePam30,
61         ePam70,
62         ePam250
63     };
64 
65 public:
66     /// Creates a standard score matrix
67     /// @param type Standard score matrix type [in]
68     /// @param sub_size Number of rows and columns to copy [in]
69     ///
70     CGeneralScoreMatrix(EScoreMatrixName type,
71                         unsigned int sub_size = 25);
72 
73     /// Creates user defined score matrix. Matrix and alphabet are copied.
74     /// @param matrix Matrix values [in]
75     /// @param size Number of alphabet elements [in]
76     /// @param alphabet Alphabel symbols [in]
77     ///
78     CGeneralScoreMatrix(const Int4** matrix, unsigned int size,
79                         const char* alphabet = NULL);
80 
81     /// Creates score matrix from values in a given vector
82     /// @param vals Values for the score matrix as consecutive rows [in]
83     /// @param alphabet Order of residues for the score matrix or NULL
84     ///
85     CGeneralScoreMatrix(const vector<Int4>& vals, const char* alphabet = NULL);
86 
87     /// Copy constructor
88     /// @param matrix Score matrix
89     CGeneralScoreMatrix(const CGeneralScoreMatrix& matrix);
90 
91     /// Destructor
92     ~CGeneralScoreMatrix();
93 
94     /// Access matrix values
95     /// @return Pointer to matrix values
96     ///
GetMatrix(void) const97     const Int4** GetMatrix(void) const {return (const Int4**)m_ScoreMatrix;}
98 
99     /// Get order of residues in the score matrix
100     /// @return Pointer to alphabet symbols
101     ///
GetResidueOrder(void) const102     const char* GetResidueOrder(void) const {return m_ResidueOrder;}
103 
104     /// Get number of residues
105     /// @return Number of residues
106     ///
GetNumResidues(void) const107     unsigned int GetNumResidues(void) const {return m_NumResidues;}
108 
109     /// Get score matrix entry for at specified position. Throws exception
110     /// if index out of bounds
111     /// @param i Matrix row number [in]
112     /// @param j Matrix column number [in]
113     /// @return Score matrix value at position i, j
114     ///
115     Int4 GetScore(Uint4 i, Uint4 j) const;
116 
117     /// Return score matrix entry for a given pair of residues. Throws
118     /// exception for illegal residues
119     /// @param a Residue 1 [in]
120     /// @param b Residue 2 [in]
121     /// @return Score matrix value for a given pair of residues
122     ///
123     Int4 GetScore(char a, char b) const;
124 
125 private:
126     /// Forbid assignment operator
127     CGeneralScoreMatrix& operator=(const CGeneralScoreMatrix&);
128 
129 protected:
130     Int4** m_ScoreMatrix;
131     char* m_ResidueOrder;
132     unsigned int m_NumResidues;
133 };
134 
135 
136 /// Exceptions for CGeneralScoreMatrix
137 class CGeneralScoreMatrixException : public CException
138 {
139 public:
140     enum EErrCode {
141         eInvalid,
142         eIndexOutOfBounds,
143         eInvalidResidue,
144         eNoResidueInfo
145     };
146 
GetErrCodeString(void) const147     virtual const char* GetErrCodeString(void) const override {
148         switch (GetErrCode()) {
149         case eInvalid : return "eInvalid";
150         case eIndexOutOfBounds : return "eIndexOutOfBounds";
151         case eInvalidResidue : return "eInvalidResidue";
152         case eNoResidueInfo : return "eNoResidueInfo";
153         }
154         return "UnknownError";
155     }
156 
157     NCBI_EXCEPTION_DEFAULT(CGeneralScoreMatrixException, CException);
158 };
159 
160 
161 END_SCOPE(blast)
162 END_NCBI_SCOPE
163 
164 #endif // ALGO_BLAST_GUMBEL_PARAMS__GENERAL_SCORE_MATRIX___HPP
165 
166 
167