NCBI C++ Toolkit Cross Reference

  C++/src/algo/blast/unit_tests/api/aalookup_unit_test.cpp


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
/* $Id: aalookup_unit_test.cpp 52346 2011-12-19 16:45:43Z maning $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: George Coulouris * * File Description: * Protein lookup table unit tests * * =========================================================================== */ #include <ncbi_pch.hpp> #include <corelib/test_boost.hpp> #include <corelib/ncbitime.hpp> #include <objmgr/object_manager.hpp> #include <objmgr/scope.hpp> #include <objmgr/util/sequence.hpp> #include <objects/seqloc/Seq_loc.hpp> #include <objects/seqloc/Seq_interval.hpp> #include <objects/seqalign/Seq_align.hpp> #include <objects/seqalign/Seq_align_set.hpp> #include <serial/serial.hpp> #include <serial/iterator.hpp> #include <serial/objostr.hpp> #include <algo/blast/api/bl2seq.hpp> #include <blast_objmgr_priv.hpp> #include <algo/blast/core/blast_setup.h> #include <algo/blast/core/blast_encoding.h> #include <algo/blast/core/blast_aalookup.h> #include <algo/blast/core/lookup_util.h> #include "test_objmgr.hpp" using namespace std; using namespace ncbi; using namespace ncbi::objects; using namespace ncbi::blast; /// A general lookup table for the test struct AalookupTestFixture { BLAST_SequenceBlk *query_blk; BlastSeqLoc *lookup_segments; LookupTableOptions *lookup_options; BlastScoreBlk *sbp; LookupTableWrap *lookup_wrap_ptr; BlastAaLookupTable *lookup; // constructor // this constructor actually does nothing. // use GetSeqBlk() and FillLookupTable() to instantiate a // testing lookuptable instead. AalookupTestFixture(){ query_blk=NULL; lookup_segments=NULL; lookup_options=NULL; sbp=NULL; lookup_wrap_ptr=NULL; lookup=NULL; } // destructor ~AalookupTestFixture() { LookupTableWrapFree(lookup_wrap_ptr); LookupTableOptionsFree(lookup_options); BlastSequenceBlkFree(query_blk); BlastSeqLocFree(lookup_segments); BlastScoreBlkFree(sbp); } // to create a sequence with given gid void GetSeqBlk(string gid){ CSeq_id id(gid); auto_ptr<SSeqLoc> ssl(CTestObjMgr::Instance().CreateSSeqLoc(id, eNa_strand_unknown)); SBlastSequence sequence = GetSequence(*ssl->seqloc, eBlastEncodingProtein, ssl->scope, eNa_strand_unknown, // strand not applicable eNoSentinels); // nucl sentinel not applicable // Create the sequence block. // Note that GetSequence always includes sentinel bytes for // protein sequences. BlastSeqBlkNew(&query_blk); BlastSeqBlkSetSequence(query_blk, sequence.data.release(), sequence.length - 2); const Uint1 kNullByte = GetSentinelByte(eBlastEncodingProtein); BOOST_REQUIRE(query_blk != NULL); BOOST_REQUIRE(query_blk->sequence[0] != kNullByte); BOOST_REQUIRE(query_blk->sequence[query_blk->length - 1] != kNullByte); BOOST_REQUIRE(query_blk->sequence_start[0] == kNullByte); BOOST_REQUIRE(query_blk->sequence_start[query_blk->length + 1] == kNullByte); // Indicate which regions of the query to index // The interval is [0...length-1] but must ignore the two // sentinel bytes. This makes the interval [0...length-3] BlastSeqLocNew(&lookup_segments, 0, sequence.length - 3); } // to create debruijn sequence void GetSeqBlk(){ // generate sequence Int4 k=0,n=0,i=0, len=0; Uint1 *sequence=NULL; k=BLASTAA_SIZE; //alphabet size n=3; //word size len = iexp(k,n) + (n-1); // leave room for and pad with sentinels sequence = (Uint1*) malloc(len + 2); sequence[0] = 0; sequence[len+1] = 0; debruijn(n,k,sequence+1,NULL); for(i=0;i<n-1;i++) sequence[len-n+2+i] = sequence[i]; // create sequence block BlastSetUp_SeqBlkNew(sequence, len, &query_blk, TRUE); // indicate region of query to index lookup_segments=NULL; BlastSeqLocNew(&lookup_segments, 0, len-1); } // to create a trivial sequence (all '0') of length len void GetSeqBlk(Int4 len){ Uint1 *sequence=NULL; // leave room for and pad with sentinels sequence = (Uint1*) malloc(len + 2); for(Int4 i=0; i<len+2; i++) sequence[i] = 0; // create sequence block BlastSetUp_SeqBlkNew(sequence, len, &query_blk, TRUE); // indicate region of query to index lookup_segments=NULL; BlastSeqLocNew(&lookup_segments, 0, len-1); } // to fill up a lookup table // hasNeighbor specfies if neighboring words will be considered. void FillLookupTable(bool hasNeighbor=false){ // create lookup options LookupTableOptionsNew(eBlastTypeBlastp, &lookup_options); BLAST_FillLookupTableOptions(lookup_options, eBlastTypeBlastp, FALSE, // megablast (hasNeighbor)? 11: -1, // threshold 3); // word size // create score blocks sbp = BlastScoreBlkNew(BLASTAA_SEQ_CODE, 1); if(hasNeighbor){ // generate score options BlastScoringOptions *score_options; BlastScoringOptionsNew(eBlastTypeBlastp, &score_options); BLAST_FillScoringOptions(score_options, eBlastTypeBlastp, FALSE, 0, 0, NULL, BLAST_GAP_OPEN_PROT, BLAST_GAP_EXTN_PROT); Blast_ScoreBlkMatrixInit(eBlastTypeBlastp, score_options, sbp, &BlastFindMatrixPath); BlastScoringOptionsFree(score_options); } // create lookup table LookupTableWrapInit(query_blk, lookup_options, NULL, lookup_segments, sbp, &lookup_wrap_ptr, NULL /* RPS info */, NULL); lookup = (BlastAaLookupTable*) lookup_wrap_ptr->lut; } }; BOOST_FIXTURE_TEST_SUITE(aalookup, AalookupTestFixture) BOOST_AUTO_TEST_CASE(BackboneIntegrityTest) { // get a sequence block of gi|129295 GetSeqBlk("gi|129295"); FillLookupTable(); // In this case, we asked for no neighboring, so there should be no neighboring words BOOST_REQUIRE_EQUAL(0, lookup->threshold); BOOST_REQUIRE_EQUAL(0, lookup->neighbor_matches); // The selected sequence should use smallbone BOOST_REQUIRE_EQUAL( lookup->bone_type, eSmallbone ); // count the total number of words found Int4 num_hits_found = 0, i; for(i=0;i<lookup->backbone_size;i++) { num_hits_found += ((AaLookupSmallboneCell *)(lookup->thick_backbone))[i].num_used; } BOOST_REQUIRE_EQUAL(230, num_hits_found); } BOOST_AUTO_TEST_CASE(DebruijnSequenceTest) { // create a debruijn sequence GetSeqBlk(); FillLookupTable(); // The constructed sequence should use smallbone BOOST_REQUIRE_EQUAL( lookup->bone_type, eSmallbone ); Int4 i; // by definition, a de Bruijn sequence contains one occurrence of each word. for(i=0;i<lookup->backbone_size;i++) { Int4 num_used = ((AaLookupSmallboneCell *)(lookup->thick_backbone))[i].num_used; // some cells should be vacant if( ( ( i & 0x1F ) >= BLASTAA_SIZE ) || ( ( (i & 0x3E0) >> 5 ) >= BLASTAA_SIZE ) || ( ( ( i & 0x7C00 ) >> 10 ) >= BLASTAA_SIZE ) ){ BOOST_REQUIRE_EQUAL( num_used, 0 ); }else{ // otherwise, the cell should contain exactly one hit BOOST_REQUIRE_EQUAL( num_used, 1 ); } } } BOOST_AUTO_TEST_CASE(NeighboringWordsTest) { // create a deruijn sequence for neibhoring words test GetSeqBlk(); FillLookupTable(true); // The constructed sequence should use smallbone BOOST_REQUIRE_EQUAL( lookup->bone_type, eSmallbone ); // Now we have neighboring words, for each possible 3-mer, Int4 index; for(Int4 u=0;u<BLASTAA_SIZE;u++) for(Int4 v=0;v<BLASTAA_SIZE;v++) for(Int4 w=0;w<BLASTAA_SIZE;w++) { Int4 score; Int4 count = 0; // find its neighbors by brute force for(Int4 x=0;x<BLASTAA_SIZE;x++) for(Int4 y=0;y<BLASTAA_SIZE;y++) for(Int4 z=0;z<BLASTAA_SIZE;z++) { // compute the score of these two words score = sbp->matrix->data[u][x] + sbp->matrix->data[v][y] + sbp->matrix->data[w][z]; // if the score is above the threshold or the words match, record it if ( (score >= 11) || ( (u==x) && (v==y) && (w==z) ) ) count++; } // compute the index of the word index = (u << 10) | (v << 5) | (w); // ensure that the number of neighbors matches the lookup table //printf("count=%d, lut=%d\n",count, lookup->thick_backbone[index].num_used); Int4 num_used = ((AaLookupSmallboneCell *)(lookup->thick_backbone))[index].num_used; BOOST_REQUIRE_EQUAL(count, num_used); } } BOOST_AUTO_TEST_CASE(BackboneSequenceTest) { // create a trivial sequence Int4 len = 65534; // 65535 is the maximum possible unsigned short GetSeqBlk(len); FillLookupTable(); BOOST_REQUIRE_EQUAL( lookup->bone_type, eBackbone ); Int4 num_used = ((AaLookupBackboneCell *)(lookup->thick_backbone))[0].num_used; BOOST_REQUIRE_EQUAL(num_used, len-2); Int4 offset = ((Int4 *)(lookup->overflow))[num_used-1]; BOOST_REQUIRE_EQUAL(offset, len-3); } BOOST_AUTO_TEST_CASE(SmallboneSequenceTest) { // create a trivial sequence Int4 len = 65533; GetSeqBlk(len); FillLookupTable(); BOOST_REQUIRE_EQUAL( lookup->bone_type, eSmallbone ); Int4 num_used = ((AaLookupSmallboneCell *)(lookup->thick_backbone))[0].num_used; BOOST_REQUIRE_EQUAL(num_used, len-2); Int4 offset = ((Uint2 *)(lookup->overflow))[num_used-1]; BOOST_REQUIRE_EQUAL(offset, len-3); } #if 0 // Needs to be fixed to actually use a PSSM BOOST_AUTO_TEST_CASE(testDebruijnPSSM) { Int4 i=0,j=0; // loop indices Int4 k=BLASTAA_SIZE; // alphabet size Int4 n=3; // word size Int4 len = iexp(k,n) + (n-1); // leave room for and pad with sentinels Uint1 *sequence = (Uint1*) malloc(len + 2); sequence[0] = 0; sequence[len+1] = 0; // generate debruijn sequence debruijn(n,k,sequence+1,NULL); // unwrap it for(i=0;i<n-1;i++) sequence[len-n+2+i] = sequence[i]; // create sequence block BLAST_SequenceBlk *query_blk_debruijn=NULL; BlastSetUp_SeqBlkNew(sequence, len, &query_blk_debruijn, TRUE); // indicate region of query to index BlastSeqLoc *lookup_segments_debruijn=NULL; BlastSeqLocNew(&lookup_segments_debruijn, 0, len-1); // create the score block BlastScoringOptions *score_options = NULL; BlastScoringOptionsNew(eBlastTypeBlastp, &score_options); BLAST_FillScoringOptions(score_options, eBlastTypeBlastp, FALSE, 0, 0, NULL, BLAST_GAP_OPEN_PROT, BLAST_GAP_EXTN_PROT); BlastScoreBlk *sbp = BlastScoreBlkNew(BLASTAA_SEQ_CODE, 1); Blast_ScoreBlkMatrixInit(eBlastTypeBlastp, score_options, sbp, &BlastFindMatrixPath); // create the PSSM BlastScoreBlk *pssm_sbp = BlastScoreBlkNew(BLASTAA_SEQ_CODE, 1); pssm_sbp->posMatrix = (Int4 **) calloc(len, sizeof(Int4 *)); for(i=0;i<len;i++) { pssm_sbp->posMatrix[i] = (Int4 *) calloc(BLASTAA_SIZE, sizeof(Int4)); for(j=0;j<BLASTAA_SIZE;j++) pssm_sbp->posMatrix[i][j] = sbp->matrix[j][sequence[i+1]]; } // set PSSM lookup table options LookupTableOptions* pssm_lut_options=NULL; LookupTableOptionsNew(eBlastTypeBlastp, &pssm_lut_options); BLAST_FillLookupTableOptions(pssm_lut_options, eBlastTypeBlastp, FALSE, // megablast 11, // threshold 3); // word size // create the PSSM lookup table LookupTableWrap* pssm_lut_wrap_ptr=NULL; LookupTableWrapInit(NULL, //query_blk_debruijn, pssm_lut_options, NULL, lookup_segments_debruijn, pssm_sbp, &pssm_lut_wrap_ptr, NULL /* RPS Info */, NULL); BlastAaLookupTable* pssm_lut = (BlastAaLookupTable*) pssm_lut_wrap_ptr->lut; { /* for each possible 3-mer, */ Int4 index; for(i=0;i<BLASTAA_SIZE;i++) for(j=0;j<BLASTAA_SIZE;j++) for(k=0;k<BLASTAA_SIZE;k++) { Int4 score; Int4 count = 0; /* find its neighbors by brute force */ for(Int4 x=0;x<BLASTAA_SIZE;x++) for(Int4 y=0;y<BLASTAA_SIZE;y++) for(Int4 z=0;z<BLASTAA_SIZE;z++) { /* compute the score of these two words */ score = sbp->matrix[i][x] + sbp->matrix[j][y] + sbp->matrix[k][z]; /* if the score is above the threshold, record it */ if (score >= 11) count++; } /* compute the index of the word */ index = (i << 10) | (j << 5) | (k); /* ensure that the number of neighbors matches the lookup table */ //printf("count=%d, lut=%d\n",count, pssm_lut->thick_backbone[index].num_used); BOOST_REQUIRE_EQUAL(count, pssm_lut->thick_backbone[index].num_used); } } LookupTableWrapFree(pssm_lut_wrap_ptr); LookupTableOptionsFree(pssm_lut_options); BlastScoreBlkFree(pssm_sbp); BlastSequenceBlkFree(query_blk_debruijn); BlastSeqLocFree(lookup_segments_debruijn); BlastScoreBlkFree(sbp); BlastScoringOptionsFree(score_options); } // testDebruijnPSSM #endif // TestBlastAaLookupTable // Register this test suite with the default test factory registry BOOST_AUTO_TEST_SUITE_END()

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.