| 1 | // Copyright (c) 2005 J. Craig Venter Institutecxx . . $ `<file>/libbio/kmer.H`/ |
| 2 | // Author: Brian Walenz |
| 3 | // |
| 4 | // This program is free software; you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation; either version 2 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received (LICENSE.txt) a copy of the GNU General Public |
| 15 | // License along with this program; if not, write to the Free Software |
| 16 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | |
| 18 | // A 'simple' kMer datastructure. |
| 19 | |
| 20 | #ifndef BIO_KMER_Hcxx . . $ `libbio/kmer.H:21:9`! |
| 21 | #define BIO_KMER_Hcxx . . $ `libbio/kmer.H:21:9`! |
| 22 | |
| 23 | // The maximum size of a mer. You get 32 bases per word, so |
| 24 | // KMER_WORDS=4 will get you up to a 128-mer. |
| 25 | // |
| 26 | #define KMER_WORDScxx . . $ `libbio/kmer.H:26:9`! 1 |
| 27 | |
| 28 | #include "util.h"cxx . . $ `<file>/libutil/util.h`/ |
| 29 | #include "util++.H"cxx . . $ `<file>/libutil/util++.H`/ |
| 30 | #include "bio.h"cxx . . $ `<file>/libbio/bio.h`/ |
| 31 | #include "bio++.H"cxx . . $ `<file>/libbio/bio++.H`/ |
| 32 | #include "kmeriface.H"cxx . . $ `<file>/libbio/kmeriface.H`/ |
| 33 | |
| 34 | #if KMER_WORDScxx . . $ `libbio/kmer.H:26:9`! == 1 |
| 35 | #include "kmertiny.H"cxx . . $ `<file>/libbio/kmertiny.H`/ |
| 36 | typedef kMerTinycxx . . $ kMerTiny# kMercxx . . $ kMer#; |
| 37 | #else |
| 38 | #include "kmerhuge.H" |
| 39 | typedef kMerHuge kMer; |
| 40 | #endif |
| 41 | |
| 42 | |
| 43 | #undef DEBUGADDBASE |
| 44 | #undef DEBUGCOMP |
| 45 | #undef DEBUGSPACE |
| 46 | |
| 47 | |
| 48 | class kMerBuildercxx . . $ kMerBuilder# { |
| 49 | public: |
| 50 | kMerBuildercxx . . $ kMerBuilder#kMerBuilder(3068bc546887ddbd).(uint32cxx . . $ uint32# ms=0, uint32cxx . . $ uint32# cm=0, char *tm=0L); |
| 51 | ~cxx . . $ kMerBuilder#`~kMerBuilder`(49f6e7a06ebc5aa8).kMerBuildercxx . . $ kMerBuilder#(); |
| 52 | |
| 53 | // Clear all mer data, reset state to as just after construction. |
| 54 | void clearcxx . . $ kMerBuilder#clear(b45ddcbc8423892c).(bool clearMer=true); |
| 55 | |
| 56 | // Returns true if we need another base to finish the mer. This |
| 57 | // only occurs for compressed mers, if we are in a homopolymer run. |
| 58 | // |
| 59 | private: |
| 60 | bool addBaseContiguouscxx . . $ kMerBuilder#addBaseContiguous(7dc03904b9dbe7ff).(uint64cxx . . $ uint64# cf, uint64cxx . . $ uint64# cr); |
| 61 | bool addBaseCompressedcxx . . $ kMerBuilder#addBaseCompressed(7dc03904b9dbe7ff).(uint64cxx . . $ uint64# cf, uint64cxx . . $ uint64# cr); |
| 62 | bool addBaseSpacedcxx . . $ kMerBuilder#addBaseSpaced(7dc03904b9dbe7ff).(uint64cxx . . $ uint64# cf, uint64cxx . . $ uint64# cr); |
| 63 | bool addBaseCompressedSpacedcxx . . $ kMerBuilder#addBaseCompressedSpaced(7dc03904b9dbe7ff).(uint64cxx . . $ uint64# cf, uint64cxx . . $ uint64# cr); |
| 64 | |
| 65 | public: |
| 66 | bool addBasecxx . . $ kMerBuilder#addBase(b5fd09639b7a0261).(char ch) { |
| 67 | uint64cxx . . $ uint64# cf = letterToBits[ch]; |
| 68 | uint64cxx . . $ uint64# cr = letterToBits[complementSymbol[ch]]; |
| 69 | |
| 70 | #ifdef DEBUGADDBASE |
| 71 | fprintf(stderr, "addBase() %c\n", ch); |
| 72 | #endif |
| 73 | |
| 74 | if (_stylecxx . . $ kMerBuilder#_style. == 0) |
| 75 | return(addBaseContiguouscxx . . $ kMerBuilder#addBaseContiguous(7dc03904b9dbe7ff).(cf, cr)); |
| 76 | |
| 77 | if (_stylecxx . . $ kMerBuilder#_style. == 1) |
| 78 | return(addBaseCompressedcxx . . $ kMerBuilder#addBaseCompressed(7dc03904b9dbe7ff).(cf, cr)); |
| 79 | |
| 80 | if (_stylecxx . . $ kMerBuilder#_style. == 2) |
| 81 | return(addBaseSpacedcxx . . $ kMerBuilder#addBaseSpaced(7dc03904b9dbe7ff).(cf, cr)); |
| 82 | |
| 83 | if (_stylecxx . . $ kMerBuilder#_style. == 3) |
| 84 | return(addBaseCompressedSpacedcxx . . $ kMerBuilder#addBaseCompressedSpaced(7dc03904b9dbe7ff).(cf, cr)); |
| 85 | |
| 86 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "kMerBuilder::addBase()-- Invalid mer type %d.\n", _stylecxx . . $ kMerBuilder#_style.); |
| 87 | exitcxx . . $ exit(44ed87f96534fad7).(1); |
| 88 | |
| 89 | return(false); |
| 90 | } |
| 91 | |
| 92 | void maskcxx . . $ kMerBuilder#mask(49f6e7a06ebc5aa8).(void) { |
| 93 | _fMercxx . . $ kMerBuilder#_fMer.->maskcxx . . $ kMerTiny#mask(b45ddcbc8423892c).(true); |
| 94 | _rMercxx . . $ kMerBuilder#_rMer.->maskcxx . . $ kMerTiny#mask(b45ddcbc8423892c).(false); |
| 95 | }; |
| 96 | |
| 97 | kMercxx . . $ kMer# const &theFMercxx . . $ kMerBuilder#theFMer(52e25c29fea41909).(void) { return(*_fMercxx . . $ kMerBuilder#_fMer.); }; |
| 98 | kMercxx . . $ kMer# const &theRMercxx . . $ kMerBuilder#theRMer(52e25c29fea41909).(void) { return(*_rMercxx . . $ kMerBuilder#_rMer.); }; |
| 99 | kMercxx . . $ kMer# const &theCMercxx . . $ kMerBuilder#theCMer(52e25c29fea41909).(void) { return((theFMercxx . . $ kMerBuilder#theFMer(52e25c29fea41909).() <cxx . . $ kMerTiny#`operator<`(3a2dde3078dcd13f). theRMercxx . . $ kMerBuilder#theRMer(52e25c29fea41909).()) ? theFMercxx . . $ kMerBuilder#theFMer(52e25c29fea41909).() : theRMercxx . . $ kMerBuilder#theRMer(52e25c29fea41909).()); }; |
| 100 | |
| 101 | uint32cxx . . $ uint32# merSizecxx . . $ kMerBuilder#merSize(db52760e79bf9054).(void) { return(_merSizecxx . . $ kMerBuilder#_merSize.); }; |
| 102 | uint32cxx . . $ uint32# templateSpancxx . . $ kMerBuilder#templateSpan(db52760e79bf9054).(void) { return(_templateSpancxx . . $ kMerBuilder#_templateSpan.); }; |
| 103 | |
| 104 | uint32cxx . . $ uint32# baseSpancxx . . $ kMerBuilder#baseSpan(2141b656ce2cc063).(uint32cxx . . $ uint32# b) { |
| 105 | return(_compressionLengthcxx . . $ kMerBuilder#_compressionLength.[(_compressionIndexcxx . . $ kMerBuilder#_compressionIndex. + 1 + b) % _merSizecxx . . $ kMerBuilder#_merSize.]);; |
| 106 | }; |
| 107 | |
| 108 | private: |
| 109 | |
| 110 | // Style of builder we are |
| 111 | uint32cxx . . $ uint32# _stylecxx . . $ kMerBuilder#_style.; |
| 112 | |
| 113 | // Amount of the mer that has valid sequence. Sigh. I really needed a signed value here -- |
| 114 | // where negative values mean that we first have to get to the end of the template that was |
| 115 | // invalid, then we need to build a new mer. |
| 116 | // |
| 117 | // And, yes, just simply making it signed leads to all sortes of compiler warnings about |
| 118 | // comparing signed and unsigned. And I've been here before, and those warnings just propate |
| 119 | // endlessly. Just go away, Mr. Smartypants. |
| 120 | // |
| 121 | // Details: when building spaced seeds, if we hit an N in the middle of the template, we need to |
| 122 | // invalidate the mer, but not start building a new mer until we exhaust the current template. |
| 123 | // The example is template=1101. Suppose we hit an N at the second 1. We set the merSizeValid |
| 124 | // to 0, and proceed. When we push on the base for the last 1 in the template, we'd increment |
| 125 | // the merSizeValid. The first two 1's in the template would now create a mer big enough to be |
| 126 | // valid, and we'd return it -- but now the template we're using is 0111. |
| 127 | // |
| 128 | // _merSizeValid is offset by _merSize (e.g., the true valid size is _merSizeValid - _merSize). |
| 129 | // _merSizeValidIs is the size _merSizeValid needs to be in order for it to be valid. |
| 130 | // Similarily, _merSizeValidZero is the value of zero (currently this is equal to _merSize). |
| 131 | // |
| 132 | uint32cxx . . $ uint32# _merSizecxx . . $ kMerBuilder#_merSize.; // Desired number of bases in the mer |
| 133 | uint32cxx . . $ uint32# *_merSizeValidcxx . . $ kMerBuilder#_merSizeValid.; // Actual number of bases in the mer |
| 134 | uint32cxx . . $ uint32# _merSizeValidZerocxx . . $ kMerBuilder#_merSizeValidZero.; // Definition of 'zero' bases in the mer |
| 135 | uint32cxx . . $ uint32# _merSizeValidIscxx . . $ kMerBuilder#_merSizeValidIs.; // Definition of 'full' bases in the mer |
| 136 | |
| 137 | // An array of mers, we allocate all mers in one block |
| 138 | kMercxx . . $ kMer# *_merStoragecxx . . $ kMerBuilder#_merStorage.; |
| 139 | |
| 140 | // Pointer to the currently active mer |
| 141 | kMercxx . . $ kMer# *_fMercxx . . $ kMerBuilder#_fMer.; |
| 142 | kMercxx . . $ kMer# *_rMercxx . . $ kMerBuilder#_rMer.; |
| 143 | |
| 144 | // For compression |
| 145 | uint32cxx . . $ uint32# _compressioncxx . . $ kMerBuilder#_compression.; |
| 146 | uint32cxx . . $ uint32# _compressionIndexcxx . . $ kMerBuilder#_compressionIndex.; // index into cL[] that is the last base in the mer |
| 147 | uint32cxx . . $ uint32# _compressionFirstIndexcxx . . $ kMerBuilder#_compressionFirstIndex.; // index into cL[] that is the first base in a run |
| 148 | uint32cxx . . $ uint32# *_compressionLengthcxx . . $ kMerBuilder#_compressionLength.; // one per base |
| 149 | uint32cxx . . $ uint32# _compressionCurrentLengthcxx . . $ kMerBuilder#_compressionCurrentLength.; |
| 150 | |
| 151 | // For templates |
| 152 | uint32cxx . . $ uint32# _templateSpancxx . . $ kMerBuilder#_templateSpan.; // # of 0's and 1's in the template |
| 153 | uint32cxx . . $ uint32# _templateLengthcxx . . $ kMerBuilder#_templateLength.; // length of the pattern in the template |
| 154 | char *_templatecxx . . $ kMerBuilder#_template.; // character string template |
| 155 | uint32cxx . . $ uint32# _templatePoscxx . . $ kMerBuilder#_templatePos.; // position we are building in the template |
| 156 | uint32cxx . . $ uint32# _templateMercxx . . $ kMerBuilder#_templateMer.; // the mer we should output next |
| 157 | uint32cxx . . $ uint32# _templateFirstcxx . . $ kMerBuilder#_templateFirst.; // if true, we're still building the initial mer |
| 158 | }; |
| 159 | |
| 160 | #endif // BIO_KMER_H |