NCBI C++ ToolKit
struct_dp_demo.cpp

Search Toolkit Book for _dp_demo_8cpp_source

Go to the documentation of this file.
1 /* $Id: struct_dp_demo.cpp 65388 2014-11-25 17:41:40Z vasilche $
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 official 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 * Authors: Paul Thiessen
27 *
28 * File Description:
29 * Test/demo for dynamic programming-based alignment algorithms
30 *
31 * ===========================================================================
32 */
33 
34 #include <ncbi_pch.hpp>
35 #include <corelib/ncbistd.hpp>
36 #include <corelib/ncbi_limits.h>
38 
39 #include <map>
40 #include <string>
41 
43 
44 #include "struct_dp_demo.hpp"
45 
47 
48 
49 BEGIN_SCOPE(struct_dp)
50 
51 #define ERROR_MESSAGE(s) ERR_POST(Error << "struct_dp_demo: " << s << '!')
52 #define INFO_MESSAGE(s) ERR_POST(Info << "struct_dp_demo: " << s)
53 
54 void DPApp::Init(void)
55 {
56 }
57 
58 static inline unsigned int ScreenResidueCharacter(char original)
59 {
60  int ch = toupper((unsigned char) original);
61  switch (ch) {
62  case 'A': case 'R': case 'N': case 'D': case 'C':
63  case 'Q': case 'E': case 'G': case 'H': case 'I':
64  case 'L': case 'K': case 'M': case 'F': case 'P':
65  case 'S': case 'T': case 'W': case 'Y': case 'V':
66  case 'B': case 'Z':
67  break;
68  default:
69  ch = 'X'; // make all but natural aa's just 'X'
70  }
71  return ch;
72 }
73 
74 static int GetBLOSUM62Score(char a, char b)
75 {
76  static SNCBIFullScoreMatrix Blosum62Matrix;
77  static bool unpacked = false;
78 
79  if (!unpacked) {
80  NCBISM_Unpack(&NCBISM_Blosum62, &Blosum62Matrix);
81  unpacked = true;
82  }
83 
84  return Blosum62Matrix.s[ScreenResidueCharacter(a)][ScreenResidueCharacter(b)];
85 }
86 
88 static string subject, query;
89 
90 extern "C" {
91 int ScoreByBlosum62(unsigned int block, unsigned int queryPos)
92 {
93  if (!blocks || block >= blocks->nBlocks ||
94  queryPos + blocks->blockSizes[block] > query.size())
95  {
96  ERROR_MESSAGE("ScoreByBlosum62() - invalid parameters");
97  return DP_NEGATIVE_INFINITY;
98  }
99 
100  int score = 0;
101  for (unsigned int i=0; i<blocks->blockSizes[block]; i++)
102  score += GetBLOSUM62Score(
103  subject[blocks->blockPositions[block] + i],
104  query[queryPos + i]);
105 
106 // INFO_MESSAGE("score for block " << block << " pos " << queryPos << ": " << score);
107  return score;
108 }
109 } // extern "C"
110 
111 int DPApp::Run(void)
112 {
113  // this demo tries to reproduce the VAST alignment of 1DOI and 1FRD
114  subject = "PTVEYLNYEVVDDNGWDMYDDDVFGEASDMDLDDEDYGSLEVNEGEYILEAAEAQGYDWPFSC"
115  "RAGACANCAAIVLEGDIDMDMQQILSDEEVEDKNVRLTCIGSPDADEVKIVYNAKHLDYLQNRVI";
116 
118 
119  blocks->blockPositions[0] = 0;
120  blocks->blockSizes[0] = 7;
121  blocks->maxLoops[0] = 100;
122 
123  blocks->blockPositions[1] = 35;
124  blocks->blockSizes[1] = 49;
125  blocks->maxLoops[1] = 100;
126 
127  blocks->blockPositions[2] = 86;
128  blocks->blockSizes[2] = 8;
129  blocks->maxLoops[2] = 100;
130 
131  blocks->blockPositions[3] = 97;
132  blocks->blockSizes[3] = 10;
133  blocks->maxLoops[3] = 100;
134 // blocks->maxLoops[3] = 0;
135 // blocks->freezeBlocks[3] = 74;
136 
137  blocks->blockPositions[4] = 109;
138  blocks->blockSizes[4] = 11;
139 // blocks->freezeBlocks[4] = 85;
140 
141 // for (int b=0; b<blocks->nBlocks; b++)
142 // INFO_MESSAGE("Block " << (b+1) << ": "
143 // << subject.substr(blocks->blockPositions[b], blocks->blockSizes[b]));
144 
145  query = "ASYQVRLINKKQDIDTTIEIDEETTILDGAEENGIELPFSCHSGSCSSCVGKVVEGEVDQSDQ"
146  "IFLDDEQMGKGFALLCVTYPRSNCTIKTHQEPYLA";
147 
148  DP_AlignmentResult *alignment;
149  int result = DP_LocalBlockAlign(blocks, ScoreByBlosum62, 0, query.size() - 1, &alignment);
150 
151  // crudely print out alignment result
153  INFO_MESSAGE("Found alignment, total score: " << alignment->score
154  << " for " << alignment->nBlocks << " blocks");
155  unsigned int block;
156  for (block=0; block<alignment->nBlocks; block++) {
157  unsigned int
158  subjectStart = blocks->blockPositions[block + alignment->firstBlock],
159  queryStart = alignment->blockPositions[block],
160  blockSize = blocks->blockSizes[block + alignment->firstBlock];
161  INFO_MESSAGE(
162  "Block " << (block + alignment->firstBlock + 1) << ", score "
163  << ScoreByBlosum62(block + alignment->firstBlock, queryStart) << ":\n"
164  << "S: " << subject.substr(subjectStart, blockSize)
165  << ' ' << (subjectStart + 1) << '-' << (subjectStart + blockSize) << '\n'
166  << "Q: " << query.substr(queryStart, blockSize)
167  << ' ' << (queryStart + 1) << '-' << (queryStart + blockSize)
168  );
169 
170  }
171  DP_DestroyAlignmentResult(alignment);
172  }
173 
174  else if (result == STRUCT_DP_NO_ALIGNMENT) {
175  INFO_MESSAGE("Found no significant alignment");
176  }
177 
179  ERROR_MESSAGE("DP_GlobalBlockAlign() failed");
180  }
181 
183  return 0;
184 }
185 
186 END_SCOPE(struct_dp)
187 
188 
190 USING_SCOPE(struct_dp);
191 
192 int main(int argc, const char* argv[])
193 {
194  SetDiagStream(&NcbiCerr); // send all diagnostic messages to cerr
195  SetDiagPostLevel(eDiag_Info); // show all messages
196 
197  DPApp app;
198  return app.AppMain(argc, argv, NULL, eDS_Default, NULL); // don't use config file
199 }
int Run(void)
void Init(void)
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define NULL
Definition: ncbistd.hpp:225
EDiagSev SetDiagPostLevel(EDiagSev post_sev=eDiag_Error)
Set the threshold severity for posting the messages.
Definition: ncbidiag.cpp:6129
void SetDiagStream(CNcbiOstream *os, bool quick_flush=true, FDiagCleanup cleanup=0, void *cleanup_data=0, const string &stream_name="")
Set diagnostic stream.
Definition: ncbidiag.cpp:8083
@ eDS_Default
Try standard log file (app.name + ".log") in /log/, use stderr on failure.
Definition: ncbidiag.hpp:1790
@ eDiag_Info
Informational message.
Definition: ncbidiag.hpp:651
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
#define NcbiCerr
Definition: ncbistre.hpp:544
int i
unsigned int a
Definition: ncbi_localip.c:102
int toupper(Uchar c)
Definition: ncbictype.hpp:73
const SNCBIPackedScoreMatrix NCBISM_Blosum62
Definition: sm_blosum62.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
unsigned int firstBlock
Definition: struct_dp.h:104
unsigned int nBlocks
Definition: struct_dp.h:103
unsigned int * blockPositions
Definition: struct_dp.h:105
unsigned int * maxLoops
Definition: struct_dp.h:68
unsigned int * blockPositions
Definition: struct_dp.h:66
unsigned int nBlocks
Definition: struct_dp.h:65
unsigned int * blockSizes
Definition: struct_dp.h:67
TNCBIScore s[128][128]
Definition: raw_scoremat.h:87
int DP_LocalBlockAlign(const DP_BlockInfo *blocks, DP_BlockScoreFunction BlockScore, unsigned int queryFrom, unsigned int queryTo, DP_AlignmentResult **alignment)
void DP_DestroyBlockInfo(DP_BlockInfo *blocks)
#define STRUCT_DP_FOUND_ALIGNMENT
Definition: struct_dp.h:44
#define STRUCT_DP_PARAMETER_ERROR
Definition: struct_dp.h:46
#define STRUCT_DP_ALGORITHM_ERROR
Definition: struct_dp.h:47
static const int DP_NEGATIVE_INFINITY
Definition: struct_dp.h:51
#define STRUCT_DP_NO_ALIGNMENT
Definition: struct_dp.h:45
void DP_DestroyAlignmentResult(DP_AlignmentResult *alignment)
DP_BlockInfo * DP_CreateBlockInfo(unsigned int nBlocks)
static unsigned int ScreenResidueCharacter(char original)
USING_SCOPE(struct_dp)
static DP_BlockInfo * blocks
static int GetBLOSUM62Score(char a, char b)
#define INFO_MESSAGE(s)
int ScoreByBlosum62(unsigned int block, unsigned int queryPos)
static string subject
#define ERROR_MESSAGE(s)
static string query
int main(int argc, const char *argv[])
USING_NCBI_SCOPE
else result
Definition: token2.c:20
Modified on Sun Apr 14 05:25:20 2024 by modify_doxy.py rev. 669887