| 440 | } |
| 441 | |
| 442 | void GenomeCopyNumber::recalculateRatioUsingCG (int degree, bool intercept, float minExpectedGC, float maxExpectedGC) { |
| 443 | |
| 444 | if (degree > MAXDEGREE) { |
| 445 | cerr << "polynomial degree should be < 10\n"; |
| 446 | exit (-1); |
| 447 | } |
| 448 | int maximalNumberOfCopies = ploidy_*2; |
| 449 | float interval = float (0.01) ; |
| 450 | |
| 451 | //first guess about parameters |
| 452 | const int npoints = degree+2; |
| 453 | |
| 454 | |
| 455 | double around [MAXDEGREE+2]; |
| 456 | for (int i = 0; i<npoints; i++) { |
| 457 | around[i] = minExpectedGC + (maxExpectedGC-minExpectedGC)/(npoints-1)*i; //0.55-0.35 |
| 458 | } // for degree = 3 one will get : { 0.35, 0.40, 0.45, 0.5, 0.55 }; for 0.35 and 0.55 |
| 459 | |
| 460 | |
| 461 | double yValues [MAXDEGREE+2]; |
| 462 | for (int i = 0; i <npoints; i++) |
| 463 | yValues[i] = calculateMedianRatioAround(interval, float(around[i])); |
| 464 | int nvars = degree; //fit by cubic polynomial |
| 465 | ap::real_2d_array xy; |
| 466 | xy.setlength(npoints,nvars+1); |
| 467 | for (int i = 0; i <npoints; i++) { |
| 468 | xy(i,degree) = yValues[i]; |
| 469 | xy(i,degree-1) = around[i]; |
| 470 | for (int j = degree-2; j>=0; j--) { |
| 471 | xy(i,j)=xy(i,j+1)*around[i]; |
| 472 | } |
| 473 | /* this is equal to |
| 474 | xy(i,0) = around[i]*around[i]*around[i]; |
| 475 | xy(i,1) = around[i]*around[i]; |
| 476 | xy(i,2) = around[i]; |
| 477 | xy(i,3) = yValues[i]; */ |
| 478 | } |
| 479 | linearmodel lm; |
| 480 | int info; |
| 481 | lrreport ar; |
| 482 | |
| 483 | if (intercept) |
| 484 | lrbuild(xy,npoints,nvars,info,lm,ar); |
| 485 | else |
| 486 | lrbuildz(xy,npoints,nvars,info,lm,ar); |
| 487 | if (info != 1) { |
| 488 | cerr << "Error in the first linear regression (the first guess about parameters), code: " << info <<"\n"; |
| 489 | } |
| 490 | ap::real_1d_array v; |
| 491 | v.setlength(nvars+int(intercept)); |
| 492 | lrunpack(lm,v,nvars); |
| 493 | //cout << v(0) << "\t" << v(1)<< "\t" <<v(2)<< "\t" <<v(3)<< "\n"; |
| 494 | double a[MAXDEGREE+1]; |
| 495 | for (int i = 0; i <degree; i++) { |
| 496 | a[i] = v(i); |
| 497 | } /* this is equal to |
| 498 | double a0 = v(0); |
| 499 | double a1 = v(1); |
no test coverage detected