Cigar.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 #include "Cigar.h"
00021 #include "STLUtilities.h"
00022
00023
00024 const int32_t Cigar::INDEX_NA = -1;
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 void Cigar::getCigarString(std::string& cigarString) const
00037 {
00038 using namespace STLUtilities;
00039
00040 std::vector<CigarOperator>::const_iterator i;
00041
00042 cigarString.clear();
00043
00044
00045
00046 for (i = cigarOperations.begin(); i != cigarOperations.end(); i++)
00047 {
00048 cigarString << (*i).count << (*i).getChar();
00049 }
00050 }
00051
00052 void Cigar::getCigarString(String& cigarString) const
00053 {
00054 std::string cigar;
00055
00056 getCigarString(cigar);
00057
00058 cigarString = cigar.c_str();
00059
00060 return;
00061 }
00062
00063 void Cigar::getExpandedString(std::string &s) const
00064 {
00065 s = "";
00066
00067 std::vector<CigarOperator>::const_iterator i;
00068
00069
00070
00071
00072 for (i = cigarOperations.begin(); i != cigarOperations.end(); i++)
00073 {
00074 for (uint32_t j = 0; j<(*i).count; j++) s += (*i).getChar();
00075 }
00076 return;
00077 }
00078
00079
00080 bool Cigar::operator == (Cigar &rhs) const
00081 {
00082
00083 if (this->size() != rhs.size()) return false;
00084
00085 for (int i = 0; i < this->size(); i++)
00086 {
00087 if (cigarOperations[i]!=rhs.cigarOperations[i]) return false;
00088 }
00089 return true;
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 int Cigar::getExpectedQueryBaseCount() const
00111 {
00112 int matchCount = 0;
00113 std::vector<CigarOperator>::const_iterator i;
00114 for (i = cigarOperations.begin(); i != cigarOperations.end(); i++)
00115 {
00116 switch (i->operation)
00117 {
00118 case match:
00119 case mismatch:
00120 case softClip:
00121 case insert:
00122 matchCount += i->count;
00123 break;
00124 default:
00125
00126 break;
00127 }
00128 }
00129 return matchCount;
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 int Cigar::getExpectedReferenceBaseCount() const
00150 {
00151 int matchCount = 0;
00152 std::vector<CigarOperator>::const_iterator i;
00153 for (i = cigarOperations.begin(); i != cigarOperations.end(); i++)
00154 {
00155 switch (i->operation)
00156 {
00157 case match:
00158 case mismatch:
00159 case del:
00160 case skip:
00161 matchCount += i->count;
00162 break;
00163 default:
00164
00165 break;
00166 }
00167 }
00168 return matchCount;
00169 }
00170
00171
00172
00173 int Cigar::getNumBeginClips() const
00174 {
00175 int numBeginClips = 0;
00176 for (unsigned int i = 0; i != cigarOperations.size(); i++)
00177 {
00178 if ((cigarOperations[i].operation == softClip) ||
00179 (cigarOperations[i].operation == hardClip))
00180 {
00181
00182 numBeginClips += cigarOperations[i].count;
00183 }
00184 else
00185 {
00186
00187 break;
00188 }
00189 }
00190 return(numBeginClips);
00191 }
00192
00193
00194
00195 int Cigar::getNumEndClips() const
00196 {
00197 int numEndClips = 0;
00198 for (int i = (cigarOperations.size() - 1); i >= 0; i--)
00199 {
00200 if ((cigarOperations[i].operation == softClip) ||
00201 (cigarOperations[i].operation == hardClip))
00202 {
00203
00204 numEndClips += cigarOperations[i].count;
00205 }
00206 else
00207 {
00208
00209 break;
00210 }
00211 }
00212 return(numEndClips);
00213 }
00214
00215
00216 int32_t Cigar::getRefOffset(int32_t queryIndex)
00217 {
00218
00219 if ((queryToRef.size() == 0) || (refToQuery.size() == 0))
00220 {
00221 setQueryAndReferenceIndexes();
00222 }
00223 if ((queryIndex < 0) || ((uint32_t)queryIndex >= queryToRef.size()))
00224 {
00225 return(INDEX_NA);
00226 }
00227 return(queryToRef[queryIndex]);
00228 }
00229
00230
00231 int32_t Cigar::getQueryIndex(int32_t refOffset)
00232 {
00233
00234 if ((queryToRef.size() == 0) || (refToQuery.size() == 0))
00235 {
00236 setQueryAndReferenceIndexes();
00237 }
00238 if ((refOffset < 0) || ((uint32_t)refOffset >= refToQuery.size()))
00239 {
00240 return(INDEX_NA);
00241 }
00242 return(refToQuery[refOffset]);
00243 }
00244
00245
00246 int32_t Cigar::getRefPosition(int32_t queryIndex, int32_t queryStartPos)
00247 {
00248
00249 if ((queryToRef.size() == 0) || (refToQuery.size() == 0))
00250 {
00251 setQueryAndReferenceIndexes();
00252 }
00253 if ((queryIndex < 0) || ((uint32_t)queryIndex >= queryToRef.size()))
00254 {
00255 return(INDEX_NA);
00256 }
00257
00258 if (queryToRef[queryIndex] != INDEX_NA)
00259 {
00260 return(queryToRef[queryIndex] + queryStartPos);
00261 }
00262 return(INDEX_NA);
00263 }
00264
00265
00266
00267
00268
00269 int32_t Cigar::getQueryIndex(int32_t refPosition, int32_t queryStartPos)
00270 {
00271
00272 if ((queryToRef.size() == 0) || (refToQuery.size() == 0))
00273 {
00274 setQueryAndReferenceIndexes();
00275 }
00276
00277 int32_t refOffset = refPosition - queryStartPos;
00278 if ((refOffset < 0) || ((uint32_t)refOffset >= refToQuery.size()))
00279 {
00280 return(INDEX_NA);
00281 }
00282
00283 return(refToQuery[refOffset]);
00284 }
00285
00286
00287
00288
00289 uint32_t Cigar::getNumOverlaps(int32_t start, int32_t end,
00290 int32_t queryStartPos)
00291 {
00292
00293 if ((queryToRef.size() == 0) || (refToQuery.size() == 0))
00294 {
00295 setQueryAndReferenceIndexes();
00296 }
00297
00298
00299 int32_t startRefOffset = 0;
00300
00301
00302
00303
00304 if (start > queryStartPos)
00305 {
00306 startRefOffset = start - queryStartPos;
00307 }
00308
00309 int32_t endRefOffset = end - queryStartPos;
00310 if (end == -1)
00311 {
00312
00313
00314
00315 endRefOffset = refToQuery.size();
00316 }
00317
00318
00319
00320
00321 if (endRefOffset < 0)
00322 {
00323 return(0);
00324 }
00325
00326
00327
00328
00329 int32_t refOffset = 0;
00330 int32_t numOverlaps = 0;
00331 for (unsigned int queryIndex = 0; queryIndex < queryToRef.size();
00332 queryIndex++)
00333 {
00334 refOffset = getRefOffset(queryIndex);
00335 if (refOffset > endRefOffset)
00336 {
00337
00338
00339 break;
00340 }
00341 else if ((refOffset >= startRefOffset) && (refOffset < endRefOffset))
00342 {
00343
00344 ++numOverlaps;
00345 }
00346 }
00347
00348 return(numOverlaps);
00349 }
00350
00351
00352
00353 void Cigar::clearQueryAndReferenceIndexes()
00354 {
00355 queryToRef.clear();
00356 refToQuery.clear();
00357 }
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376 void Cigar::setQueryAndReferenceIndexes()
00377 {
00378
00379 clearQueryAndReferenceIndexes();
00380
00381
00382 for (uint32_t cigarIndex = 0; cigarIndex < cigarOperations.size(); cigarIndex++)
00383 {
00384
00385 switch (cigarOperations[cigarIndex].operation)
00386 {
00387 case match:
00388 case mismatch:
00389
00390
00391 for (uint32_t i = 0; i < cigarOperations[cigarIndex].count; i++)
00392 {
00393
00394
00395 int32_t queryToRefLen = queryToRef.size();
00396 int32_t refToQueryLen = refToQuery.size();
00397 queryToRef.push_back(refToQueryLen);
00398 refToQuery.push_back(queryToRefLen);
00399 }
00400 break;
00401 case insert:
00402 case softClip:
00403
00404
00405 for (uint32_t i = 0; i < cigarOperations[cigarIndex].count; i++)
00406 {
00407 queryToRef.push_back(INDEX_NA);
00408 }
00409 break;
00410 case del:
00411 case skip:
00412
00413
00414 for (uint32_t i = 0; i < cigarOperations[cigarIndex].count; i++)
00415 {
00416 refToQuery.push_back(INDEX_NA);
00417 }
00418 break;
00419 case hardClip:
00420 case pad:
00421 case none:
00422 break;
00423 };
00424 }
00425 }
00426