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
|
/*
* Copyright (C) 2010-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 <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "CigarRoller.h"
////////////////////////////////////////////////////////////////////////
//
// Cigar Roller Class
//
CigarRoller & CigarRoller::operator += (CigarRoller &rhs)
{
std::vector<CigarOperator>::iterator i;
for (i = rhs.cigarOperations.begin(); i != rhs.cigarOperations.end(); i++)
{
(*this) += *i;
}
return *this;
}
//
// Append a new operator at the end of the sequence.
//
CigarRoller & CigarRoller::operator += (const CigarOperator &rhs)
{
// Adding to the cigar, so the query & reference indexes would be
// incomplete, so just clear them.
clearQueryAndReferenceIndexes();
if (rhs.count==0)
{
// nothing to do
}
else if (cigarOperations.empty() || cigarOperations.back() != rhs)
{
cigarOperations.push_back(rhs);
}
else
{
// last stored operation is the same as the new one, so just add it in
cigarOperations.back().count += rhs.count;
}
return *this;
}
CigarRoller & CigarRoller::operator = (CigarRoller &rhs)
{
clear();
(*this) += rhs;
return *this;
}
//
void CigarRoller::Add(Operation operation, int count)
{
CigarOperator rhs(operation, count);
(*this) += rhs;
}
void CigarRoller::Add(char operation, int count)
{
switch (operation)
{
case 0:
case 'M':
Add(match, count);
break;
case 1:
case 'I':
Add(insert, count);
break;
case 2:
case 'D':
Add(del, count);
break;
case 3:
case 'N':
Add(skip, count);
break;
case 4:
case 'S':
Add(softClip, count);
break;
case 5:
case 'H':
Add(hardClip, count);
break;
case 6:
case 'P':
Add(pad, count);
break;
case 7:
case '=':
Add(match, count);
break;
case 8:
case 'X':
Add(match, count);
break;
default:
// Hmmm... what to do?
std::cerr << "ERROR "
<< "(" << __FILE__ << ":" << __LINE__ <<"): "
<< "Parsing CIGAR - invalid character found "
<< "with parameter " << operation << " and " << count
<< std::endl;
break;
}
}
void CigarRoller::Add(const char *cigarString)
{
int operationCount = 0;
while (*cigarString)
{
if (isdigit(*cigarString))
{
char *endPtr;
operationCount = strtol((char *) cigarString, &endPtr, 10);
cigarString = endPtr;
}
else
{
Add(*cigarString, operationCount);
cigarString++;
}
}
}
bool CigarRoller::Remove(int index)
{
if((index < 0) || ((unsigned int)index >= cigarOperations.size()))
{
// can't remove, out of range, return false.
return(false);
}
cigarOperations.erase(cigarOperations.begin() + index);
// Modifying the cigar, so the query & reference indexes are out of date,
// so clear them.
clearQueryAndReferenceIndexes();
return(true);
}
bool CigarRoller::IncrementCount(int index, int increment)
{
if((index < 0) || ((unsigned int)index >= cigarOperations.size()))
{
// can't update, out of range, return false.
return(false);
}
cigarOperations[index].count += increment;
// Modifying the cigar, so the query & reference indexes are out of date,
// so clear them.
clearQueryAndReferenceIndexes();
return(true);
}
bool CigarRoller::Update(int index, Operation op, int count)
{
if((index < 0) || ((unsigned int)index >= cigarOperations.size()))
{
// can't update, out of range, return false.
return(false);
}
cigarOperations[index].operation = op;
cigarOperations[index].count = count;
// Modifying the cigar, so the query & reference indexes are out of date,
// so clear them.
clearQueryAndReferenceIndexes();
return(true);
}
void CigarRoller::Set(const char *cigarString)
{
clear();
Add(cigarString);
}
void CigarRoller::Set(const uint32_t* cigarBuffer, uint16_t bufferLen)
{
clear();
// Parse the buffer.
for (int i = 0; i < bufferLen; i++)
{
int opLen = cigarBuffer[i] >> 4;
Add(cigarBuffer[i] & 0xF, opLen);
}
}
//
// when we examine CIGAR strings, we need to know how
// many cumulative insert and delete positions there are
// so that we can adjust the read location appropriately.
//
// Here, we iterate over the vector of CIGAR operations,
// summaring the count for each insert or delete (insert
// increases the offset, delete decreases it).
//
// The use case for this is when we have a genome match
// position based on an index word other than the first one,
// and there is also a insert or delete between the beginning
// of the read and the index word. We can't simply report
// the match position without taking into account the indels,
// otherwise we'll be off by N where N is the sum of this
// indel count.
//
// DEPRECATED - do not use. There are better ways to accomplish that by using
// read lengths, reference lengths, span of the read, etc.
int CigarRoller::getMatchPositionOffset()
{
int offset = 0;
std::vector<CigarOperator>::iterator i;
for (i = cigarOperations.begin(); i != cigarOperations.end(); i++)
{
switch (i->operation)
{
case insert:
offset += i->count;
break;
case del:
offset -= i->count;
break;
// TODO anything for case skip:????
default:
break;
}
}
return offset;
}
//
// Get the string reprentation of the Cigar operations in this object.
// Caller must delete the returned value.
//
const char * CigarRoller::getString()
{
// NB: the exact size of the string is not important, it just needs to be guaranteed
// larger than the largest number of characters we could put into it.
// we do not explicitly manage memory usage, and we expect when program exits, the memory used here will be freed
static char *ret = NULL;
static unsigned int retSize = 0;
if (ret == NULL)
{
retSize = cigarOperations.size() * 12 + 1; // 12 == a magic number -> > 1 + log base 10 of MAXINT
ret = (char*) malloc(sizeof(char) * retSize);
assert(ret != NULL);
}
else
{
// currently, ret pointer has enough memory to use
if (retSize > cigarOperations.size() * 12 + 1)
{
}
else
{
retSize = cigarOperations.size() * 12 + 1;
free(ret);
ret = (char*) malloc(sizeof(char) * retSize);
}
assert(ret != NULL);
}
char *ptr = ret;
char buf[12]; // > 1 + log base 10 of MAXINT
std::vector<CigarOperator>::iterator i;
// Progressively append the character representations of the operations to
// the cigar string we allocated above.
*ptr = '\0'; // clear result string
for (i = cigarOperations.begin(); i != cigarOperations.end(); i++)
{
sprintf(buf, "%d%c", (*i).count, (*i).getChar());
strcat(ptr, buf);
while (*ptr)
{
ptr++; // limit the cost of strcat above
}
}
return ret;
}
void CigarRoller::clear()
{
// Clearing the cigar, so the query & reference indexes are out of
// date, so clear them.
clearQueryAndReferenceIndexes();
cigarOperations.clear();
}
|