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 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
#include "SequenceFuns.h"
#include <assert.h>
void complementSeqNumbers(char* ReadsIn, char* ReadsOut, uint Lread) {//complement the numeric sequences
for (uint jj=0;jj<Lread;jj++) {
switch (int(ReadsIn[jj])){
case (3): ReadsOut[jj]=char(0);break;
case (2): ReadsOut[jj]=char(1);break;
case (1): ReadsOut[jj]=char(2);break;
case (0): ReadsOut[jj]=char(3);break;
default: ReadsOut[jj]=ReadsIn[jj];
};
};
};
void revComplementNucleotides(char* ReadsIn, char* ReadsOut, uint Lread) {//complement the numeric sequences
for (uint jj=0;jj<Lread;jj++) {
switch (ReadsIn[Lread-1-jj]){
case ('A'): ReadsOut[jj]='T';break;
case ('C'): ReadsOut[jj]='G';break;
case ('G'): ReadsOut[jj]='C';break;
case ('T'): ReadsOut[jj]='A';break;
case ('N'): ReadsOut[jj]='N';break;
case ('R'): ReadsOut[jj]='Y';break;
case ('Y'): ReadsOut[jj]='R';break;
case ('K'): ReadsOut[jj]='M';break;
case ('M'): ReadsOut[jj]='K';break;
case ('S'): ReadsOut[jj]='S';break;
case ('W'): ReadsOut[jj]='W';break;
case ('B'): ReadsOut[jj]='V';break;
case ('D'): ReadsOut[jj]='H';break;
case ('V'): ReadsOut[jj]='B';break;
case ('H'): ReadsOut[jj]='D';break;
case ('a'): ReadsOut[jj]='t';break;
case ('c'): ReadsOut[jj]='g';break;
case ('g'): ReadsOut[jj]='c';break;
case ('t'): ReadsOut[jj]='a';break;
case ('n'): ReadsOut[jj]='n';break;
case ('r'): ReadsOut[jj]='y';break;
case ('y'): ReadsOut[jj]='r';break;
case ('k'): ReadsOut[jj]='m';break;
case ('m'): ReadsOut[jj]='k';break;
case ('s'): ReadsOut[jj]='s';break;
case ('w'): ReadsOut[jj]='w';break;
case ('b'): ReadsOut[jj]='v';break;
case ('d'): ReadsOut[jj]='h';break;
case ('v'): ReadsOut[jj]='b';break;
case ('h'): ReadsOut[jj]='d';break;
default: ReadsOut[jj]=ReadsIn[Lread-1-jj];
};
};
};
void revComplementNucleotides(string &seq) {//complement the numeric sequences
string seq1(seq);
for (uint jj=0;jj<seq.size();jj++) {
switch (seq1[seq.size()-1-jj]){
case ('A'): seq[jj]='T';break;
case ('C'): seq[jj]='G';break;
case ('G'): seq[jj]='C';break;
case ('T'): seq[jj]='A';break;
case ('N'): seq[jj]='N';break;
case ('R'): seq[jj]='Y';break;
case ('Y'): seq[jj]='R';break;
case ('K'): seq[jj]='M';break;
case ('M'): seq[jj]='K';break;
case ('S'): seq[jj]='S';break;
case ('W'): seq[jj]='W';break;
case ('B'): seq[jj]='V';break;
case ('D'): seq[jj]='H';break;
case ('V'): seq[jj]='B';break;
case ('H'): seq[jj]='D';break;
case ('a'): seq[jj]='t';break;
case ('c'): seq[jj]='g';break;
case ('g'): seq[jj]='c';break;
case ('t'): seq[jj]='a';break;
case ('n'): seq[jj]='n';break;
case ('r'): seq[jj]='y';break;
case ('y'): seq[jj]='r';break;
case ('k'): seq[jj]='m';break;
case ('m'): seq[jj]='k';break;
case ('s'): seq[jj]='s';break;
case ('w'): seq[jj]='w';break;
case ('b'): seq[jj]='v';break;
case ('d'): seq[jj]='h';break;
case ('v'): seq[jj]='b';break;
case ('h'): seq[jj]='d';break;
default: seq[jj]=seq1[seq.size()-1-jj];
};
};
};
char nuclToNumBAM(char cc){
switch (cc) {//=ACMGRSVTWYHKDBN
case ('='): cc=0;break;
case ('A'): case ('a'): cc=1;break;
case ('C'): case ('c'): cc=2;break;
case ('M'): case ('m'): cc=3;break;
case ('G'): case ('g'): cc=4;break;
case ('R'): case ('r'): cc=5;break;
case ('S'): case ('s'): cc=6;break;
case ('V'): case ('v'): cc=7;break;
case ('T'): case ('t'): cc=8;break;
case ('W'): case ('w'): cc=9;break;
case ('Y'): case ('y'): cc=10;break;
case ('H'): case ('h'): cc=11;break;
case ('K'): case ('k'): cc=12;break;
case ('D'): case ('d'): cc=13;break;
case ('B'): case ('b'): cc=14;break;
case ('N'): case ('n'): cc=15;break;
default: cc=15;
};
return cc;
};
void nuclPackBAM(char* ReadsIn, char* ReadsOut, uint Lread) {//pack nucleotides for BAM
for (uint jj=0;jj<Lread/2;jj++) {
ReadsOut[jj]=nuclToNumBAM(ReadsIn[2*jj])<<4 | nuclToNumBAM(ReadsIn[2*jj+1]);
};
if (Lread%2==1) {
ReadsOut[Lread/2]=nuclToNumBAM(ReadsIn[Lread-1])<<4;
};
};
void convertNucleotidesToNumbers(const char* R0, char* R1, const uint Lread) {//transform sequence from ACGT into 0-1-2-3 code
for (uint jj=0;jj<Lread;jj++) {
switch (int(R0[jj])){
case (65): case(97):
R1[jj]=char(0);break;//A
case (67): case(99):
R1[jj]=char(1);break;//C
case (71): case(103):
R1[jj]=char(2);break;//G
case (84): case(116):
R1[jj]=char(3);break;//T
default:
R1[jj]=char(4);//anything else is converted to N
};
};
};
void convertCapitalBasesToNum(uint8_t *rS, uint64_t N)
{//only capital bases are allowed
for (uint64_t ib=0; ib<N; ib++) {
switch (rS[ib]) {
case 'A':
rS[ib]=0;
break;
case 'C':
rS[ib]=1;
break;
case 'G':
rS[ib]=2;
break;
case 'T':
rS[ib]=3;
break;
default:
rS[ib]=4;
};
};
};
uint convertNucleotidesToNumbersRemoveControls(const char* R0, char* R1, const uint Lread) {//transform sequence from ACGT into 0-1-2-3 code
uint iR1=0;
for (uint jj=0;jj<Lread;jj++) {
switch (int(R0[jj])){
case (65): case(97):
R1[jj]=char(0);break;//A
case (67): case(99):
R1[jj]=char(1);break;//C
case (71): case(103):
R1[jj]=char(2);break;//G
case (84): case(116):
R1[jj]=char(3);break;//T
default:
if (int(R0[jj]) < 32) {//control characters are skipped
continue;
} else {//all non-control non-ACGT characters are convreted to N
R1[jj]=char(4);//anything else
};
};
++iR1;
};
return iR1;
};
char convertNt01234(const char R0) {//transform sequence from ACGT into 0-1-2-3 code
switch(R0)
{
case('a'):
case('A'):
return 0;
break;
case('c'):
case('C'):
return 1;
break;
case('g'):
case('G'):
return 2;
break;
case('t'):
case('T'):
return 3;
break;
default:
return 4;
};
};
int32 convertNuclStrToInt32(const string S, uint32 &intOut) {
intOut=0;
int32 posN=-1;
for (uint32 ii=0; ii<S.size(); ii++) {
uint32 nt = (uint32) convertNt01234(S.at(ii));
if (nt>3) {//N
if (posN>=0)
return -2; //two Ns
posN=ii;
nt=0;
};
intOut = intOut << 2;
intOut +=nt;
//intOut += nt<<(2*ii);
};
return posN;
};
string convertNuclInt32toString(uint32 nuclNum, const uint32 L) {
string nuclOut(L,'N');
string nuclChar="ACGT";
for (uint32 ii=1; ii<=L; ii++) {
nuclOut[L-ii] = nuclChar[nuclNum & 3];
nuclNum = nuclNum >> 2;
};
return nuclOut;
};
int64 convertNuclStrToInt64(const string S, uint64 &intOut) {
intOut=0;
int64 posN=-1;
for (uint64 ii=0; ii<S.size(); ii++) {
uint64 nt = (uint64) convertNt01234(S.at(ii));
if (nt>3) {//N
if (posN>=0)
return -2; //two Ns
posN=ii;
nt=0;
};
intOut = intOut << 2;
intOut +=nt;
//intOut += nt<<(2*ii);
};
return posN;
};
string convertNuclInt64toString(uint64 nuclNum, const uint32 L) {
string nuclOut(L,'N');
string nuclChar="ACGT";
for (uint64 ii=1; ii<=L; ii++) {
nuclOut[L-ii] = nuclChar[nuclNum & 3];
nuclNum = nuclNum >> 2;
};
return nuclOut;
};
uint chrFind(uint Start, uint i2, uint* chrStart) {// find chromosome from global locus
uint i1=0, i3;
while (i1+1<i2) {
i3=(i1+i2)/2;
if ( chrStart[i3] > Start ) {
i2=i3;
} else {
i1=i3;
};
};
return i1;
};
uint localSearch(const char *x, uint nx, const char *y, uint ny, double pMM){
//find the best alignment of two short sequences x and y
//pMM is the maximum percentage of mismatches
uint nMatch=0, nMM=0, nMatchBest=0, nMMbest=0, ixBest=nx;
for (uint ix=0;ix<nx;ix++) {
nMatch=0; nMM=0;
for (uint iy=0;iy<min(ny,nx-ix);iy++) {
if (x[ix+iy]>3) continue;
if (x[ix+iy]==y[iy]) {
nMatch++;
} else {
nMM++;
};
};
if ( ( nMatch>nMatchBest || (nMatch==nMatchBest && nMM<nMMbest) ) && double(nMM)/double(nMatch)<=pMM) {
ixBest=ix;
nMatchBest=nMatch;
nMMbest=nMM;
};
};
return ixBest;
};
uint localSearchNisMM(const char *x, uint nx, const char *y, uint ny, double pMM){
//find the best alignment of two short sequences x and y
//pMM is the maximum percentage of mismatches
//Ns in x OR y are considered mismatches
uint nMatch=0, nMM=0, nMatchBest=0, nMMbest=0, ixBest=nx;
for (uint ix=0;ix<nx;ix++) {
nMatch=0; nMM=0;
for (uint iy=0;iy<min(ny,nx-ix);iy++) {
if (x[ix+iy]==y[iy] && y[iy]<4) {
nMatch++;
} else {
nMM++;
};
};
if ( ( nMatch>nMatchBest || (nMatch==nMatchBest && nMM<nMMbest) ) && double(nMM)/double(nMatch)<=pMM) {
ixBest=ix;
nMatchBest=nMatch;
nMMbest=nMM;
};
};
return ixBest;
};
uint32 localAlignHammingDist(const string &text, const string &query, uint32 &pos)
{
uint32 distBest=query.size();
if (text.size()<query.size()) {//query is longer than text, no match
return text.size()+1;
};
for (uint32 ii=0; ii<text.size()-query.size()+1; ii++) {
uint32 dist1=0;
for (uint32 jj=0; jj<query.size(); jj++) {
if (query[jj]!='N' && text[jj+ii]!=query[jj]) {//N in query does not count as mismatch
++dist1;
};
};
if (dist1<distBest) {
distBest=dist1;
pos=ii;
};
};
return distBest;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
uint32 localSearchGeneral(const char *text, const uint32 textLen, const vector<char> &query, const int32 textStart, const int32 textEnd, double pMM, vector <uint32> vecMM, uint32 &nMM)
{
assert(textEnd <= (int32)textLen);
assert(textStart + (int32)query.size() >= 0);
nMM=0;
uint32 nMatchBest=0;
int32 posBest=textLen;
uint32 clippedL = 0;
int32 dirSearch = (textStart <= textEnd ? 1 : -1); //search direction
for (int32 pos=textStart; pos!=textEnd; pos+=dirSearch) {
int32 qs = max(0, -pos);
int32 qe = min((uint32)query.size(), (uint32)(textLen-pos) );
uint32 nMatch1=0, nMM1=0;
for (uint32 iq=qs; iq<qe; iq++) {
if (text[pos+iq]>3)
continue; //Ns in the text are not counted as matches or mismatches
if (text[pos+iq]==query[iq]) {
nMatch1++;
} else {
nMM1++;
if ( nMM1 >= vecMM.size() ) {
nMatch1=0;
break; //too many mismatches
};
};
};
//if ( (nMatch1>nMatchBest || (nMatch1==nMatchBest && nMM1<nMM)) && double(nMM1)<=double(nMatch1)*pMM ) {
if ( (nMatch1>nMatchBest || (nMatch1==nMatchBest && nMM1<nMM)) && nMM1<vecMM.size() && (qe-qs)>=vecMM[nMM1]) {
posBest=pos;
nMatchBest=nMatch1;
nMM=nMM1;
clippedL = (uint32)(textStart <= textEnd ? posBest+(int32)query.size(): -posBest+(int32)textLen );
};
};
return clippedL;
};
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
uint qualitySplit(char* r,uint L, uint maxNsplit, uint minLsplit, uint** splitR) {
//splits the read r[L] by quality scores q[L], outputs in splitR - split coordinate/length - per base
//returns number of good split regions
uint iR=0,iS=0,iR1,LgoodMin=0, iFrag=0;
while ( (iR<L) & (iS<maxNsplit) ) { //main cycle
//find next good base
while ( iR<L && r[iR]>3 ) {
if (r[iR]==MARK_FRAG_SPACER_BASE)
iFrag++; //count read fragments
iR++;
};
if (iR==L) break; //exit when reached end of read
iR1=iR;
//find the next bad base
while ( iR<L && r[iR]<=3 ) {
iR++;
};
if ( (iR-iR1)>LgoodMin ) LgoodMin=iR-iR1;
if ( (iR-iR1)<minLsplit ) continue; //too short for a good region
splitR[0][iS]=iR1; //good region start
splitR[1][iS]=iR-iR1; //good region length
splitR[2][iS]=iFrag; //good region fragment
iS++;
};
if (iS==0) splitR[1][0]=LgoodMin; //output min good piece length
return iS;
};
|