File: SamTags.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 (165 lines) | stat: -rw-r--r-- 5,615 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
/*
 *  Copyright (C) 2011  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 "SamTags.h"
#include "BaseUtilities.h"

const char* SamTags::BQ_TAG = "BQ";
const char SamTags::BQ_TAG_TYPE = 'Z';
const char* SamTags::MD_TAG = "MD";
const char SamTags::MD_TAG_TYPE = 'Z';
const char* SamTags::ORIG_POS_TAG = "OP";
const char SamTags::ORIG_POS_TAG_TYPE = 'i';
const char* SamTags::ORIG_CIGAR_TAG = "OC";
const char SamTags::ORIG_CIGAR_TAG_TYPE = 'Z';
const char* SamTags::ORIG_QUAL_TAG = "OQ";
const char SamTags::ORIG_QUAL_TAG_TYPE = 'Z';


// Create the MD tag for the specified input record and the genome.
bool SamTags::createMDTag(String& outputMDtag, SamRecord& inputRec, 
                          GenomeSequence& genome)
{
    outputMDtag.Clear();
    // Get the cigar to use for determing alignment.
    Cigar* cigarInfo = inputRec.getCigarInfo();
    if(cigarInfo == NULL)
    {
        throw(std::runtime_error("Cannot createMDTag - failed to get the cigar"));
        return(false);
    }
    int32_t queryIndex = Cigar::INDEX_NA;

    // get where this read starts on the reference.
    uint32_t startOfReadOnRefIndex = 
        genome.getGenomePosition(inputRec.getReferenceName());
    if(startOfReadOnRefIndex == (uint32_t)INVALID_CHROMOSOME_INDEX)
    {
        // Failed to find the reference for this chromosome, so return false.
        return(false);
    }
    startOfReadOnRefIndex += inputRec.get0BasedPosition();

    // Track the number of consecutive matches.
    int32_t matchCount = 0;
    // Track if it is currently in a deletion so it knows when not to add
    // a '^'.
    bool currentDeletion = false;

    // Loop through the Reference indices (ignores insertions/pads/clips).
    for(int refOffset = 0; 
        refOffset < cigarInfo->getExpectedReferenceBaseCount();
        ++refOffset)
    {
        // Get the query index for this reference position..
        queryIndex = cigarInfo->getQueryIndex(refOffset);

        char refBase = genome[startOfReadOnRefIndex + refOffset];

        if(queryIndex != Cigar::INDEX_NA)
        {
            // Both the reference and the read have a base, so get the bases.
            char readBase = inputRec.getSequence(queryIndex);
            currentDeletion = false;

            // 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, so update counter.
                ++matchCount;
            }
            else
            {
                // Mismatch, so output the number of matches if any.
                if(matchCount != 0)
                {
                    outputMDtag += matchCount;
                    matchCount = 0;
                }
                outputMDtag += refBase;
            }
        }
        else
        {
            // This reference position is not in the query, so it is a deletion.
            // Deletion, so output the number of matches if any.
            if(matchCount != 0)
            {
                outputMDtag += matchCount;
                matchCount = 0;
            }

            if(!currentDeletion)
            {
                // Not currently in a deletion, so add the ^
                outputMDtag += '^';
            }
            // Add the deleted base.
            outputMDtag += refBase;
            currentDeletion = true;
        }
    }

    // output the match count at the end.
    outputMDtag += matchCount;
    return(true);
}

// Check to see if the MD tag in the record is accurate.
bool SamTags::isMDTagCorrect(SamRecord& inputRec, GenomeSequence& genome)
{
    String calcMDtag;
    if(!createMDTag(calcMDtag, inputRec, genome))
    {
        // Could not generate the MD tag, so just return that it is incorrect.
        return(false);
    }
    
    const String* origMDtag = inputRec.getStringTag(MD_TAG);
    
    if(origMDtag == NULL)
    {
        // There was no tag.
        // if there is not a new tag, then they are the same and true
        // should be returned.  If there is a new tag, then the old one was
        // wrong so false should be returned.  So just return the result of
        // IsEmpty.
        return(calcMDtag.IsEmpty());
    }
    else
    {
        // origMDtag is not NULL, so just compare the two tags.
        return(calcMDtag == *origMDtag);
    }
}


// Update/Add the MD tag in the inputRec.
bool SamTags::updateMDTag(SamRecord& inputRec, GenomeSequence& genome)
{
    // Calculate the new MD tag.
    String calcMDtag;
    createMDTag(calcMDtag, inputRec, genome);
    
    // Add the MD tag.  If it is already there and is different it will
    // replace it.  If it is already there and it is the same, it won't
    // do anything.
    return(inputRec.addTag(MD_TAG, MD_TAG_TYPE, calcMDtag.c_str()));
}