/usr/include/blasr/bwt/Occ.hpp is in libblasr-dev 0~20151014+gitbe5d1bf-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #ifndef _BLASR_OCC_HPP_
#define _BLASR_OCC_HPP_
#include <fstream>
#include <vector>
#include <algorithm>
#include "DNASequence.hpp"
#include "NucConversion.hpp"
#include "utils.hpp"
#include "matrix/Matrix.hpp"
#include "matrix/FlatMatrix.hpp"
template<typename T_BWTSequence, typename T_Major, typename T_Minor>
class Occ {
public:
int majorBinSize;
int minorBinSize;
int hasDebugInformation;
FlatMatrix2D<T_Major> major;
FlatMatrix2D<T_Minor> minor;
FlatMatrix2D<T_Major> full;
static const unsigned int AlphabetSize = 5;
T_BWTSequence *bwtSeqRef;
DNALength numMajorBins, numMinorBins;
void PrintBins(std::ostream &out) {
out << "numMajor: " << numMajorBins << " numMinor: " << numMinorBins << std::endl;
DNALength ma, mi, mii;
mi = 0;
int i;
for (ma = 0; ma < numMajorBins; ma++) {
out << "MAJOR: ";
for (i = 0; i < 5; i++ ){ out << major[ma][i] << " "; } out << std::endl;
for (mii = 0; mii < majorBinSize / minorBinSize && mi < numMinorBins; mii++, mi++) {
out << " ";
for (i = 0;i <5; i++ ){
out << minor[mi][i] << " ";
}
out << std::endl;
}
}
}
void InitializeBWT(T_BWTSequence &bwtSeq) {
bwtSeqRef = &bwtSeq;
}
void Initialize(T_BWTSequence &bwtSeq,
int _majorBinSize=4096,
int _minorBinSize=64,
int _hasDebugInformation=0) {
//
// This reference is used when counting nucleotides. It assumes
// the sequence does not change between initialization and
// subsequent calls to count.
//
bwtSeqRef = &bwtSeq;
majorBinSize = _majorBinSize;
minorBinSize = _minorBinSize;
hasDebugInformation = _hasDebugInformation;
InitializeMajorBins(bwtSeq);
InitializeMinorBins(bwtSeq);
if (hasDebugInformation) {
InitializeTestBins(bwtSeq);
}
}
void InitializeMajorBins(T_BWTSequence &bwtSeq) {
numMajorBins = CeilOfFraction(bwtSeq.length, (DNALength) majorBinSize);
major.Allocate(numMajorBins, AlphabetSize);
std::vector<DNALength> runningTotal;
runningTotal.resize(AlphabetSize);
fill(runningTotal.begin(), runningTotal.end(), 0);
fill(&major.matrix[0], &major.matrix[numMajorBins*AlphabetSize], 0);
DNALength p;
DNALength binIndex = 0;
for (p = 0; p < bwtSeq.length; p++) {
Nucleotide nuc = ThreeBit[bwtSeq[p]];
// only handle ACTGN, $==6, so skip counting that.
if (nuc > AlphabetSize) continue;
if (p % majorBinSize == 0) { //majorBinSize-1) {
// cout << "storing at " << p<< " " << binIndex << std::endl;
int n;
for (n = 0; n < AlphabetSize; n++ ) {
major[binIndex][n] = runningTotal[n];
}
binIndex++;
}
runningTotal[nuc]++;
}
}
void InitializeTestBins(T_BWTSequence &bwtSeq) {
full.Allocate(bwtSeq.length, AlphabetSize);
fill(full.matrix, &full.matrix[bwtSeq.length * AlphabetSize],0);
DNALength p;
int n;
for (p = 0; p < bwtSeq.length; p++) {
Nucleotide nuc = ThreeBit[bwtSeq[p]];
if (nuc > AlphabetSize) {
for (n = 0; n < AlphabetSize; n++ ) {
full[p][n] = full[p-1][n];
}
}
else {
full[p][nuc]++;
if (p > 0) {
for (n = 0; n < AlphabetSize; n++ ) {
full[p][n] = full[p-1][n] + full[p][n];
}
}
}
}
}
void InitializeMinorBins(T_BWTSequence &bwtSeq) {
numMinorBins = CeilOfFraction(bwtSeq.length, (DNALength) minorBinSize);
minor.Allocate(numMinorBins, AlphabetSize);
std::vector<DNALength> majorRunningTotal;
majorRunningTotal.resize(AlphabetSize);
std::fill(majorRunningTotal.begin(), majorRunningTotal.end(), 0);
std::fill(&minor.matrix[0], &minor.matrix[numMinorBins*AlphabetSize], 0);
DNALength p;
DNALength minorBinIndex = 0;
for (p = 0; p < bwtSeq.length; p++ ){
Nucleotide nuc = ThreeBit[bwtSeq[p]];
if (nuc > AlphabetSize) continue;
//
// The minor bins are running totals inside each major
// bin. When the count hits a bin offset, reset the bin
// counter.
//
if (p % majorBinSize == 0) {
fill(majorRunningTotal.begin(), majorRunningTotal.end(), 0);
}
if (p % minorBinSize == 0) {
int n;
for (n = 0; n < AlphabetSize; n++ ) {
minor[minorBinIndex][n] = majorRunningTotal[n];
}
minorBinIndex++;
}
majorRunningTotal[nuc]++;
}
}
int Count(Nucleotide nuc, DNALength p ) {
DNALength majorIndex = p / majorBinSize;
DNALength minorIndex = p / minorBinSize;
DNALength lastBinnedIndex = minorBinSize * (p / minorBinSize);
//
// This should be sort of O(1), since the last expression should
// be made of bit operations that are fast.
//
Nucleotide smallNuc = ThreeBit[nuc];
//assert(smallNuc < 5);
DNALength nocc = major[majorIndex][smallNuc] + minor[minorIndex][smallNuc] + bwtSeqRef->CountNuc(lastBinnedIndex, p+1, nuc);
// assert(full.matrix == NULL or full[p][smallNuc] == nocc);
return nocc;
}
void Write(std::ostream &out) {
out.write((char*) &majorBinSize, sizeof(majorBinSize));
out.write((char*) &minorBinSize, sizeof(minorBinSize));
out.write((char*) &numMajorBins, sizeof(numMajorBins));
if (numMajorBins > 0) {
out.write((char*) major[0], sizeof(T_Major) * numMajorBins*AlphabetSize);
}
out.write((char*) &numMinorBins, sizeof(numMinorBins));
if (numMinorBins > 0) {
out.write((char*) minor[0], sizeof(T_Minor) * numMinorBins*AlphabetSize);
}
if (hasDebugInformation) {
DNALength bwtSeqLength = bwtSeqRef->length;
out.write((char*)&bwtSeqLength, sizeof(bwtSeqLength));
out.write((char*)&full.matrix[0], sizeof(DNALength)* bwtSeqLength * AlphabetSize);
}
}
int Read(std::istream &in, int _hasDebugInformation) {
hasDebugInformation = _hasDebugInformation;
in.read((char*) &majorBinSize, sizeof(majorBinSize));
in.read((char*) &minorBinSize, sizeof(minorBinSize));
in.read((char*) &numMajorBins, sizeof(numMajorBins));
if (numMajorBins > 0) {
major.Resize(numMajorBins * AlphabetSize);
in.read((char*) major[0], sizeof(T_Major) * numMajorBins * AlphabetSize);
major.nRows = numMajorBins;
major.nCols = AlphabetSize;
}
in.read((char*) &numMinorBins, sizeof(numMinorBins));
if (numMinorBins > 0) {
minor.Resize(numMinorBins * AlphabetSize);
in.read((char*) minor[0], sizeof(T_Minor) * numMinorBins*AlphabetSize);
minor.nRows = numMinorBins;
minor.nCols = AlphabetSize;
}
if (hasDebugInformation) {
DNALength bwtSeqLength;
in.read((char*)&bwtSeqLength, sizeof(bwtSeqLength));
if (full.matrix) {delete [] full.matrix;}
full.matrix = ProtectedNew<DNALength>(bwtSeqLength *AlphabetSize);
full.nRows = bwtSeqLength;
full.nCols = AlphabetSize;
in.read((char*)&full.matrix[0], sizeof(DNALength)* bwtSeqLength * AlphabetSize);
}
return 1;
}
};
#endif // _BLASR_OCC_HPP_
|