This file is indexed.

/usr/include/pbseq/alignment/algorithms/alignment/ExtendAlign.hpp is in libblasr-dev 0~20161219-1.

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
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
#ifndef _BLASR_EXTEND_ALIGN_HPP_
#define _BLASR_EXTEND_ALIGN_HPP_
#include <iosfwd>
#include <vector>
#include <algorithm>
// pbdata
#include "../../../pbdata/defs.h"
#include "../../../pbdata/NucConversion.hpp"
#include "../../../pbdata/matrix/FlatMatrix.hpp"

#include "../../datastructures/alignment/Alignment.hpp"
#include "KBandAlign.hpp"

//FIXME: change data type of target pos from int to GenomeLength 
//       in order to support > 4G genome.
//       No need to change data type of query pos to DNALength, 
//       since it's unlikely to have > 2G bases per zmw.

//FIXME: change data type of target pos from int to GenomeLength 
//       in order to support > 4G genome.
//       No need to change data type of query pos to DNALength, 
//       since it's unlikely to have > 2G bases per zmw.

class RCToIndex {
public:
    int qStart, tStart;
    int middleCol;
    int band;
    int nCols;

    RCToIndex(); 

    int operator()(int r, int c, int &index); 
};

class BaseIndex {
public:
    int queryPos, refPos;
    int queryAlignLength, refAlignLength;

    int QNotAtSeqBoundary(int q); 

    int TNotAtSeqBoundary(int t); 

    int QAlignLength(); 

    int TAlignLength(); 
};

class ForwardIndex : public BaseIndex {
public:

    int QuerySeqPos(int q); 

    int RefSeqPos(int t); 

    int GetQueryStartPos(int startQ, int endQ); 

    int GetRefStartPos(int startT, int endT); 

    void OrderArrowVector(std::vector<Arrow> &mat); 
};


class ReverseIndex : public BaseIndex {
public:

    int QuerySeqPos(int q); 

    int RefSeqPos(int t); 

    int GetQueryStartPos(int startQ, int endQ); 

    int GetRefStartPos(int startT, int endT); 

    void OrderArrowVector(std::vector<Arrow> &mat); 
};

template<typename T_Alignment, 
    typename T_ScoreFn, 
    typename T_QuerySeq, 
    typename T_RefSeq, 
    typename T_Index>
    int ExtendAlignment(T_QuerySeq &querySeq, int queryPos, 
            T_RefSeq   &refSeq,   int refPos,
            int k,
            std::vector<int>   &scoreMat,
            std::vector<Arrow> &pathMat,
            T_Alignment   &alignment,
            T_ScoreFn     &scoreFn,
            T_Index  &index,
            int minExtendNBases=1, // Require that there
            // are more than one
            // base to align.
            int maxNDrops=2 // A drop is a row where
            // the alignment is
            // extended without
            // increasing the alignment
            // score.  maxnDrops is the
            // maximum number of times
            // that one may have before
            // terminating the alignment
            // 
            ) {
        PB_UNUSED(queryPos);
        PB_UNUSED(refPos);
        //
        // Try extending an alignment in the forward direction as long the
        // maximum score that is extended is above a threshold above the
        // initial score.  This dynamically grows the alignment matrices as
        // the alignment is extended (or the limits of the alignment
        // matrices since reusable buffers are used).   
        // 

        DNALength nCols = 2 * k + 1 + 1;  // 2*k is for search space, +1 is for the
        // middle band, and the last +1 is for the
        // boundary conditions at the beginning of
        // the array.

        RCToIndex rcToIndex;
        rcToIndex.band      = k;
        rcToIndex.nCols     = nCols;
        rcToIndex.middleCol = k+2-1;

        if (index.queryAlignLength  < minExtendNBases or
                index.refAlignLength < minExtendNBases) {
            //
            // One of the sequences isn't long enough to even try to extend,
            // just bail with an empty alignment.
            //
            return 0;
        }

        //
        // Preallocate arrays to be at least k long.  The full matrix may
        // not be loaded.
        //
        int matSize = nCols * (k+1);
        if (scoreMat.size() < nCols * (k+1)) {
            scoreMat.resize(nCols * (k+1));
            pathMat.resize(nCols * (k+1));
        }

        //
        // Initialize boundary conditions.
        //

        int q;
        int t;
        // Initialize first column for insertions.
        int firstIndex;
        fill(scoreMat.begin(), scoreMat.begin() + matSize, 0);
        fill(pathMat.begin(), pathMat.begin() + matSize, NoArrow);	
        rcToIndex(0, 0, firstIndex);
        scoreMat[firstIndex] = 0;
        pathMat[firstIndex]  = NoArrow;

        // Initialize insertion penalties.
        t = 0;
        int i;
        int pi;
        for (q = 1; q <= k and index.QNotAtSeqBoundary(q-1); q++) {
            bool res = rcToIndex(q, t, i);
            assert(res);
            res = rcToIndex(q-1, t, pi);
            int qSeqPos = index.QuerySeqPos(q-1);
            scoreMat[i] = scoreMat[pi] + scoreFn.Insertion(querySeq, qSeqPos);
            pathMat[i]  = Up;
            //		cout << "initializing insertion gap penalty for " << q << " " << refPos-1 << " "  << i << " " << scoreMat[i] << endl;
        }


        // Initialize the first row for deletions.
        q = 0;

        for (t = 1; t <= k and index.TNotAtSeqBoundary(t-1); t++) {
            bool res = rcToIndex(q, t, i);
            assert(res);
            int previ;
            res = rcToIndex(q,t-1,previ);

            int qSeqPos = index.QuerySeqPos(0);
            scoreMat[i] = scoreMat[previ] + scoreFn.Deletion(querySeq, qSeqPos);
            pathMat[i]  = Left;
            //		cout << "initializing deletion gap penalty for " << ((int)queryPos)-1 << " " << t << " " << i << " " << scoreMat[i] << endl;
        }
        /*	PrintFlatMatrix(&scoreMat[0], k , nCols, cout);
            cout << endl;
            PrintFlatMatrix(&pathMat[0],  k, nCols, cout);
            cout << endl;
            */
        int nDrops = 0;
        int prevRowMinScore = INF_INT;
        int globalMinScore = INF_INT;
        int globalMinScoreQPos = 0;
        int globalMinScoreTPos = 0;

        int curIndex = -1;

        int maxAlignLength = std::min(index.QAlignLength(), index.TAlignLength()) + maxNDrops;

        for (q = 1; (index.QNotAtSeqBoundary(q-1) and 
                    nDrops < maxNDrops and
                    q < maxAlignLength);
                q++ ) {

            //
            // Grow the path and score matrices by another row if this has
            // extended beyond their current capacity.
            //
            if ((q+1) * nCols > scoreMat.size()) {
                scoreMat.resize((q+1)*nCols);
                pathMat.resize((q+1)*nCols);
            }

            //
            // Now score the latest row.
            //
            int curRowMinScore = INF_INT;	
            int diagLength = q;

            int tStart = std::max((int) 1, ((int)diagLength) - k);
            int tEnd   = std::min((int) (diagLength + k +1), index.TAlignLength() + 1 );
            int qSeqPos, tSeqPos;
            for (t = tStart; t < std::min(tEnd, maxAlignLength); t++) {
                int insIndex, delIndex, matchIndex;

                bool hasInsIndex = false, hasDelIndex = false, hasMatchIndex = false, hasCurIndex = false;

                hasCurIndex   = rcToIndex(q, t, curIndex);
                assert(hasCurIndex);

                hasDelIndex   = rcToIndex(q, t - 1, delIndex);
                hasInsIndex   = rcToIndex(q - 1, t, insIndex);
                hasMatchIndex = rcToIndex(q-1, t-1, matchIndex);

                int insScore, delScore, matchScore;
                delScore   = INF_INT;
                insScore   = INF_INT;
                matchScore = INF_INT;
                //			cout << "ins index: " << insIndex << " del: " << delIndex << " match index " << matchIndex << endl;
                qSeqPos = index.QuerySeqPos(q-1); // The offset is to allow for the boundary buffer.
                tSeqPos = index.RefSeqPos(t-1); // ditto.
                /*			if (scoreMat[insIndex] == -1) {
                            cout << "bleh" << endl;
                            }
                            if (scoreMat[matchIndex] == -1) {
                            cout << "bleh" << endl;
                            }
                            if (scoreMat[delIndex] == -1) {
                            cout << "bleh" << endl;
                            }

                            if (scoreFn.Insertion(refSeq, (DNALength) tSeqPos, querySeq, (DNALength) qSeqPos) == -1) {
                            cout << "bleh" << endl;
                            }
                            if (scoreFn.Deletion(refSeq, (DNALength) tSeqPos, querySeq, (DNALength) qSeqPos) == -1) {
                            cout << "ugh" << endl;
                            }
                            if ( scoreFn.Match(refSeq, (DNALength) tSeqPos, querySeq, (DNALength) qSeqPos) == -1) {
                            cout <<" gah" << endl;
                            }*/

                if (hasInsIndex) {
                    insScore   = scoreMat[insIndex] + scoreFn.Insertion(refSeq, (DNALength) tSeqPos, querySeq, (DNALength) qSeqPos);
                }
                if (hasDelIndex) {
                    delScore   = scoreMat[delIndex] + scoreFn.Deletion(refSeq, (DNALength) tSeqPos, querySeq, (DNALength) qSeqPos);
                }
                if (hasMatchIndex) {
                    matchScore = scoreMat[matchIndex] + scoreFn.Match(refSeq, (DNALength) tSeqPos, querySeq, (DNALength) qSeqPos);
                }
                /*			cout << "ins score: " << insScore << "[" << scoreMat[insIndex] << "] del score " << delScore 
                            << " [" << scoreMat[delIndex] << "] match score " << matchScore 
                            << " [" << scoreMat[matchIndex] << "] qchar " << (int) querySeq.seq[qSeqPos] << " tchar " << (int) refSeq.seq[tSeqPos] << endl;*/
                int minScore = std::min(matchScore, delScore);
                minScore = std::min(minScore, insScore);
                scoreMat[curIndex] = minScore;
                //			cout << "extend: " << qSeqPos << " " << tSeqPos << " " << minScore << endl;
                if (minScore != INF_INT) {
                    if (minScore == insScore)   { pathMat[curIndex] = Up; }
                    if (minScore == delScore)   { pathMat[curIndex] = Left; }
                    if (minScore == matchScore) { pathMat[curIndex] = Diagonal; }
                }
                else {
                    pathMat[curIndex] = NoArrow;
                }

                assert(pathMat[curIndex] != NoArrow);
                if (minScore < curRowMinScore) {
                    curRowMinScore = minScore;
                }
                if (minScore < globalMinScore) {
                    globalMinScore = minScore;
                    globalMinScoreQPos  = q;
                    globalMinScoreTPos  = t;
                }

            }

            if (curRowMinScore > prevRowMinScore) {
                nDrops++;
            }
            prevRowMinScore = curRowMinScore;
        }

        q = globalMinScoreQPos;
        t = globalMinScoreTPos;
        std::vector<Arrow>  optAlignment;

        rcToIndex(q,t,i);
        //
        // When the optimal score is on a cell with NoArrow, there is no
        // good alignment.  Only try and trace an alignment out if the path
        // starts on a good alignment.
        //
        if (pathMat[i] != NoArrow) {
            while(q > 0 or t > 0) {
                int res;
                res = rcToIndex(q, t, i);
                assert(res != 0);
                Arrow arrow = pathMat[i];

                optAlignment.push_back(pathMat[i]);
                if (pathMat[i] == NoArrow) {
                    assert(pathMat[i] != NoArrow);
                }
                if (arrow == Diagonal) {
                    q--;
                    t--;
                }
                else if (arrow == Left) {
                    t--;
                }
                else if (arrow == Up) {
                    q--;
                }
            }
        }

        index.OrderArrowVector(optAlignment);
        alignment.ArrowPathToAlignment(optAlignment);
        alignment.qPos = index.GetQueryStartPos(q, globalMinScoreQPos);
        alignment.tPos = index.GetRefStartPos(t, globalMinScoreTPos);

        return globalMinScore;
    }

template<typename T_Alignment, typename T_ScoreFn, typename T_QuerySeq, typename T_RefSeq>
int ExtendAlignmentForward(T_QuerySeq &querySeq, int queryPos, 
        T_RefSeq   &refSeq,   int refPos,
        int k,
        std::vector<int>   &scoreMat,
        std::vector<Arrow> &pathMat,
        T_Alignment   &alignment,
        T_ScoreFn     &scoreFn,
        int minExtendNBases=1, // Require that there
        // are more than one
        // base to align.
        int maxNDrops=2 // A drop is a row where
        // the alignment is
        // extended without
        // increasing the alignment
        // score.  maxnDrops is the
        // maximum number of times
        // that one may have before
        // terminating the alignment
        // 
        ) {

    ForwardIndex forwardIndex;
    forwardIndex.queryPos = queryPos;
    forwardIndex.refPos   = refPos;
    //
    // The alignment does not include queryPos nor refPos.
    //
    forwardIndex.queryAlignLength = querySeq.length - queryPos;
    forwardIndex.refAlignLength   = refSeq.length - refPos;
    int alignScore;
    alignScore= ExtendAlignment(querySeq, queryPos, 
            refSeq, refPos, 
            k, 
            scoreMat, pathMat, 
            alignment, scoreFn, forwardIndex, minExtendNBases, maxNDrops);
    alignment.qPos = queryPos;
    alignment.tPos = refPos;
    return alignScore;
}

template<typename T_Alignment, typename T_ScoreFn, typename T_QuerySeq, typename T_RefSeq>
int ExtendAlignmentReverse(T_QuerySeq &querySeq, int queryPos, 
        T_RefSeq   &refSeq,   int refPos,
        int k,
        std::vector<int>   &scoreMat,
        std::vector<Arrow> &pathMat,
        T_Alignment   &alignment,
        T_ScoreFn     &scoreFn,
        int minExtendNBases=1, // Require that there
        // are more than one
        // base to align.
        int maxNDrops=2 // A drop is a row where
        // the alignment is
        // extended without
        // increasing the alignment
        // score.  maxnDrops is the
        // maximum number of times
        // that one may have before
        // terminating the alignment
        // 
        ) {

    ReverseIndex reverseIndex;
    reverseIndex.queryPos = queryPos-1;
    reverseIndex.refPos   = refPos-1;
    reverseIndex.queryAlignLength = queryPos;
    reverseIndex.refAlignLength   = refPos;
    int alignScore;
    alignScore = ExtendAlignment(querySeq, queryPos, 
            refSeq, refPos, 
            k, 
            scoreMat, pathMat, 
            alignment, scoreFn, reverseIndex, minExtendNBases, maxNDrops);

    return alignScore;

}

#endif // _BLASR_EXTEND_ALIGN_HPP_