File: SamQuerySeqWithRefHelper.cpp

package info (click to toggle)
libstatgen 1.0.15-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,588 kB
  • sloc: cpp: 49,624; ansic: 1,408; makefile: 320; sh: 60
file content (347 lines) | stat: -rw-r--r-- 10,610 bytes parent folder | download | duplicates (4)
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
/*
 *  Copyright (C) 2010  Regents of the University of Michigan
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdexcept>

#include "SamQuerySeqWithRefHelper.h"
#include "BaseUtilities.h"
#include "SamFlag.h"

SamQuerySeqWithRefIter::SamQuerySeqWithRefIter(SamRecord& record,
                                               GenomeSequence& refSequence,
                                               bool forward)
    : myRecord(record),
      myRefSequence(refSequence),
      myCigar(NULL),
      myStartOfReadOnRefIndex(INVALID_GENOME_INDEX),
      myQueryIndex(0),
      myForward(forward)
{
    myCigar = myRecord.getCigarInfo();
    myStartOfReadOnRefIndex = 
        refSequence.getGenomePosition(myRecord.getReferenceName());
    
    if(myStartOfReadOnRefIndex != INVALID_GENOME_INDEX)
    {
        // This reference name was found in the reference file, so 
        // add the start position.
        myStartOfReadOnRefIndex += myRecord.get0BasedPosition();
    }

    if(!forward)
    {
        myQueryIndex = myRecord.getReadLength() - 1;
    }
}


SamQuerySeqWithRefIter::~SamQuerySeqWithRefIter()
{
}



bool SamQuerySeqWithRefIter::reset(bool forward)
{
    myCigar = myRecord.getCigarInfo();
    if(myCigar == NULL)
    {
        // Failed to get Cigar.
        return(false);
    }

    // Get where the position of where this read starts as mapped to the 
    // reference.
    myStartOfReadOnRefIndex = 
        myRefSequence.getGenomePosition(myRecord.getReferenceName());
    
    if(myStartOfReadOnRefIndex != INVALID_GENOME_INDEX)
    {
        // This reference name was found in the reference file, so 
        // add the start position.
        myStartOfReadOnRefIndex += myRecord.get0BasedPosition();
    }

    myForward = forward;
    
    if(myForward)
    {
        myQueryIndex = 0;
    }
    else
    {
        // reverse, so start at the last entry.
        myQueryIndex = myRecord.getReadLength() - 1;
    }
    return(true);
}


// Returns information for the next position where the query and the 
// reference match or mismatch.  To be a match or mismatch, both the query
// and reference must have a base that is not 'N'.
// This means:
//    insertions and deletions are not mismatches or matches.
//    'N' bases are not matches or mismatches
// Returns true if an entry was found, false if there are no more matches or
// mismatches.
bool SamQuerySeqWithRefIter::getNextMatchMismatch(SamSingleBaseMatchInfo& matchMismatchInfo)
{
    // Check whether or not this read is mapped. 
    // If the read is not mapped, return no matches.
    if(!SamFlag::isMapped(myRecord.getFlag()))
    {
        // Not mapped.
        return(false);
    }

    // Check that the Cigar is set.
    if(myCigar == NULL)
    {
        // Error.
        throw(std::runtime_error("Cannot determine matches/mismatches since failed to retrieve the cigar"));
        return(false);
    }

    // If myStartOfReadOnRefIndex is the default (unset) value, then
    // the reference was not found, so return false, no matches/mismatches.
    if(myStartOfReadOnRefIndex == INVALID_GENOME_INDEX)
    {
        // This reference name was not found in the reference file, so just
        // return no matches/mismatches.
        return(false);
    }


    // Repull the read length from the record to check just in case the
    // record has changed length.
    // Loop until a match or mismatch is found as long as query index
    // is still within the read  (Loop is broken by a return).
    while((myQueryIndex < myRecord.getReadLength()) &&
          (myQueryIndex >= 0))
    {
        // Still more bases, look for a match/mismatch.

        // Get the reference offset for this read position.
        int32_t refOffset = myCigar->getRefOffset(myQueryIndex);
        if(refOffset == Cigar::INDEX_NA)
        {
            // This is either a softclip or an insertion
            // which do not count as a match or a mismatch, so
            // go to the next index.
            nextIndex();
            continue;
        }
        
        // Both the reference and the read have a base, so get the bases.
        char readBase = myRecord.getSequence(myQueryIndex, SamRecord::NONE);
        char refBase = myRefSequence[myStartOfReadOnRefIndex + refOffset];
       
        // If either the read or the reference bases are unknown, then
        // it does not count as a match or a mismatch.
        if(BaseUtilities::isAmbiguous(readBase) || 
           BaseUtilities::isAmbiguous(refBase))
        {
            // Either the reference base or the read base are unknown,
            // so skip this position.
            nextIndex();
            continue;
        }

        // Both the read & the reference have a known base, so it is either
        // a match or a mismatch.
        matchMismatchInfo.setQueryIndex(myQueryIndex);

        // Check if they are equal.
        if(BaseUtilities::areEqual(readBase, refBase))
        {
            // Match.
            matchMismatchInfo.setType(SamSingleBaseMatchInfo::MATCH);
            // Increment the query index to the next position.
            nextIndex();
            return(true);
        }
        else
        {
            // Mismatch
            matchMismatchInfo.setType(SamSingleBaseMatchInfo::MISMATCH);
            // Increment the query index to the next position.
            nextIndex();
            return(true);
        }
    }

    // No matches or mismatches were found, so return false.
    return(false);
}


void SamQuerySeqWithRefIter::nextIndex()
{
    if(myForward)
    {
        ++myQueryIndex;
    }
    else
    {
        --myQueryIndex;
    }
}


SamSingleBaseMatchInfo::SamSingleBaseMatchInfo()
    : myType(UNKNOWN),
      myQueryIndex(0)
{
}


SamSingleBaseMatchInfo::~SamSingleBaseMatchInfo()
{
}


SamSingleBaseMatchInfo::Type SamSingleBaseMatchInfo::getType()
{
    return(myType);
}


int32_t SamSingleBaseMatchInfo::getQueryIndex()
{
    return(myQueryIndex);
}


void SamSingleBaseMatchInfo::setType(Type newType)
{
    myType = newType;
}


void SamSingleBaseMatchInfo::setQueryIndex(int32_t queryIndex)
{
    myQueryIndex = queryIndex;
}


///////////////////////////////////////////////////////////////////////////
void SamQuerySeqWithRef::seqWithEquals(const char* currentSeq,
                                       int32_t seq0BasedPos,
                                       Cigar& cigar, 
                                       const char* referenceName,
                                       const GenomeSequence& refSequence,
                                       std::string& updatedSeq)
{
    updatedSeq = currentSeq;

    int32_t seqLength = updatedSeq.length();
    int32_t queryIndex = 0;

    uint32_t startOfReadOnRefIndex = 
        refSequence.getGenomePosition(referenceName);
    
    if(startOfReadOnRefIndex == INVALID_GENOME_INDEX)
    {
        // This reference name was not found in the reference file, so just
        // return.
        return;
    }
    startOfReadOnRefIndex += seq0BasedPos;
    
    // Loop until the entire sequence has been updated.
    while(queryIndex < seqLength)
    {
        // Still more bases, look for matches.

        // Get the reference offset for this read position.
        int32_t refOffset = cigar.getRefOffset(queryIndex);
        if(refOffset != Cigar::INDEX_NA)
        {
            // Both the reference and the read have a base, so get the bases.
            char readBase = currentSeq[queryIndex];
            char refBase = refSequence[startOfReadOnRefIndex + refOffset];

            // If neither base is unknown and they are the same, count it
            // as a match.
            if(!BaseUtilities::isAmbiguous(readBase) && 
               !BaseUtilities::isAmbiguous(refBase) && 
               (BaseUtilities::areEqual(readBase, refBase)))
            {
                // Match.
                updatedSeq[queryIndex] = '=';
            }
        }
        // Increment the query index to the next position.
        ++queryIndex;
        continue;
    }
}


void SamQuerySeqWithRef::seqWithoutEquals(const char* currentSeq,
                                          int32_t seq0BasedPos,
                                          Cigar& cigar, 
                                          const char* referenceName,
                                          const GenomeSequence& refSequence,
                                          std::string& updatedSeq)
{
    updatedSeq = currentSeq;

    int32_t seqLength = updatedSeq.length();
    int32_t queryIndex = 0;

    uint32_t startOfReadOnRefIndex = 
        refSequence.getGenomePosition(referenceName);
    
    if(startOfReadOnRefIndex == INVALID_GENOME_INDEX)
    {
        // This reference name was not found in the reference file, so just
        // return.
        return;
    }
    startOfReadOnRefIndex += seq0BasedPos;
    
    // Loop until the entire sequence has been updated.
    while(queryIndex < seqLength)
    {
        // Still more bases, look for matches.

        // Get the reference offset for this read position.
        int32_t refOffset = cigar.getRefOffset(queryIndex);
        if(refOffset != Cigar::INDEX_NA)
        {
            // Both the reference and the read have a base, so get the bases.
            char readBase = currentSeq[queryIndex];
            char refBase = refSequence[startOfReadOnRefIndex + refOffset];
            
            // If the bases are equal, set the sequence to the reference
            // base. (Skips the check for ambiguous to catch a case where
            // ambiguous had been converted to a '=', and if both are ambiguous,
            // it will still be set to ambiguous.)
            if(BaseUtilities::areEqual(readBase, refBase))
            {
                // Match.
                updatedSeq[queryIndex] = refBase;
            }
        }

        // Increment the query index to the next position.
        ++queryIndex;
        continue;
    }
}