NCBI C++ ToolKit
general_score_matrix.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: general_score_matrix.cpp 45495 2010-04-21 18:28:18Z boratyng $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's offical duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================*/
25 
26 /*****************************************************************************
27 
28 File name: general_score_matrix.cpp
29 
30 Author: Greg Boratyn
31 
32 Contents: Implementation of generalized score matrix
33 
34 ******************************************************************************/
35 
36 
37 #include <ncbi_pch.hpp>
38 
39 #include <math.h>
41 
42 
44 USING_SCOPE(blast);
45 
46 //CGeneralScoreMatrix
47 
50  unsigned int sub_size)
51 {
53  SNCBIFullScoreMatrix full_matrix;
54 
55  switch (type) {
56 
57  case eBlosum45:
58  matrix = NCBISM_Blosum45;
59  break;
60  case eBlosum62:
61  matrix = NCBISM_Blosum62;
62  break;
63  case eBlosum80:
64  matrix = NCBISM_Blosum80;
65  break;
66  case ePam30:
67  matrix = NCBISM_Pam30;
68  break;
69  case ePam70:
70  matrix = NCBISM_Pam70;
71  break;
72  case ePam250:
73  matrix = NCBISM_Pam250;
74  break;
75 
76  default:
78  "Unrecognised standard scoring matrix name");
79  }
80 
81  NCBISM_Unpack(&matrix, &full_matrix);
82 
83  m_NumResidues = min(strlen(matrix.symbols), (size_t)sub_size);
84  m_ScoreMatrix = new int*[m_NumResidues];
86  for (unsigned i=1;i < m_NumResidues;i++) {
88  }
89 
90  m_ResidueOrder = new char[m_NumResidues];
91  strncpy(m_ResidueOrder, matrix.symbols, m_NumResidues);
92 
93  for (unsigned i=0;i < m_NumResidues;i++) {
94  for (unsigned j=0;j < m_NumResidues;j++) {
95  m_ScoreMatrix[i][j] = full_matrix.s[
96  (unsigned)m_ResidueOrder[i]][(unsigned)m_ResidueOrder[j]];
97  }
98  }
99 }
100 
101 
103  unsigned int num_res,
104  const char* order)
105  : m_NumResidues(num_res)
106 {
107  if (order) {
108  m_ResidueOrder=new char[m_NumResidues];
109  m_ResidueOrder = strncpy(m_ResidueOrder, order, num_res);
110  }
111  else {
113  }
116  for (unsigned i=1;i < m_NumResidues;i++) {
118  }
119  for (unsigned i=0;i < m_NumResidues;i++) {
120  for (unsigned j=0;j < m_NumResidues;j++)
121  m_ScoreMatrix[i][j] = matrix[i][j];
122  }
123 }
124 
126  const char* order)
127  : m_NumResidues((unsigned int)sqrt((double)vals.size()))
128 {
129  _ASSERT(vals.size() > 0);
130  _ASSERT(m_NumResidues * m_NumResidues == vals.size());
131 
132  if (order) {
133  m_ResidueOrder=new char[m_NumResidues];
134  m_ResidueOrder = strncpy(m_ResidueOrder, order, m_NumResidues);
135  }
136  else {
138  }
141  for (unsigned i=1;i < m_NumResidues;i++) {
143  }
144  for (unsigned i=0;i < vals.size();i++) {
145  m_ScoreMatrix[0][i] = vals[i];
146  }
147 }
148 
150 {
151  m_NumResidues = matrix.m_NumResidues;
152  if (matrix.m_ResidueOrder) {
153  m_ResidueOrder = new char[m_NumResidues];
154  memcpy(m_ResidueOrder, matrix.GetResidueOrder(), m_NumResidues
155  * sizeof(char));
156  }
157  else {
159  }
160 
163  for (unsigned i=1;i < m_NumResidues;i++) {
165  }
166 
167  memcpy(*m_ScoreMatrix, *matrix.m_ScoreMatrix, m_NumResidues * m_NumResidues * sizeof(Int4));
168 }
169 
170 
172 {
173  if (m_ResidueOrder)
174  delete [] m_ResidueOrder;
175 
176  if (m_ScoreMatrix != NULL && *m_ScoreMatrix != NULL) {
177  delete [] *m_ScoreMatrix;
178  delete [] m_ScoreMatrix;
179  }
180 }
181 
182 
184 {
185  if (i >= m_NumResidues || j >= m_NumResidues) {
186  NCBI_THROW(CGeneralScoreMatrixException, eIndexOutOfBounds,
187  "Score matrix index out of bounds");
188  }
189 
190  return m_ScoreMatrix[i][j];
191 }
192 
194 {
195  if (!m_ResidueOrder) {
197  "Score matrix does not contain residue order information");
198  }
199 
200  unsigned i = 0, j = 0;
201  while (i < m_NumResidues && m_ResidueOrder[i] != a) {
202  i++;
203  }
204  while (j < m_NumResidues && m_ResidueOrder[j] != b) {
205  j++;
206  }
207 
208  if (i >= m_NumResidues) {
209  char residue[2];
210  residue[0] = a;
211  residue[1] = 0;
212  NCBI_THROW(CGeneralScoreMatrixException, eInvalidResidue,
213  (string)"Residue " + residue
214  + " not in score matrix alphabet");
215  }
216 
217  if (j >= m_NumResidues) {
218  char residue[2];
219  residue[0] = b;
220  residue[1] = 0;
221  NCBI_THROW(CGeneralScoreMatrixException, eInvalidResidue,
222  (string)"Residue " + residue
223  + " not in score matrix alphabet");
224  }
225 
226  return m_ScoreMatrix[i][j];
227 }
228 
Exceptions for CGeneralScoreMatrix.
Score matrix that can take any value.
const char * GetResidueOrder(void) const
Get order of residues in the score matrix.
Int4 GetScore(Uint4 i, Uint4 j) const
Get score matrix entry for at specified position.
EScoreMatrixName
Names of standard scoring matrices.
CGeneralScoreMatrix(EScoreMatrixName type, unsigned int sub_size=25)
Creates a standard score matrix.
USING_SCOPE(blast)
#define NULL
Definition: ncbistd.hpp:225
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
int32_t Int4
4-byte (32-bit) signed integer
Definition: ncbitype.h:102
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
int i
const struct ncbi::grid::netcache::search::fields::SIZE size
unsigned int a
Definition: ncbi_localip.c:102
T min(T x_, T y_)
const SNCBIPackedScoreMatrix NCBISM_Pam30
Definition: sm_pam30.c:92
const SNCBIPackedScoreMatrix NCBISM_Blosum62
Definition: sm_blosum62.c:92
const SNCBIPackedScoreMatrix NCBISM_Pam250
Definition: sm_pam250.c:92
const SNCBIPackedScoreMatrix NCBISM_Blosum80
Definition: sm_blosum80.c:92
void NCBISM_Unpack(const SNCBIPackedScoreMatrix *psm, SNCBIFullScoreMatrix *fsm)
Expand a packed score matrix into an unpacked one, which callers can proceed to index directly by sta...
Definition: raw_scoremat.c:81
const SNCBIPackedScoreMatrix NCBISM_Pam70
Definition: sm_pam70.c:92
const SNCBIPackedScoreMatrix NCBISM_Blosum45
The standard matrices.
Definition: sm_blosum45.c:92
TNCBIScore s[128][128]
Definition: raw_scoremat.h:87
const char * symbols
order of residues
Definition: raw_scoremat.h:47
Definition: type.c:6
#define _ASSERT
Modified on Mon Jul 22 05:02:20 2024 by modify_doxy.py rev. 669887