double LocalCorrTrack(int pos1, int pos2, bool cmpl1, bool cmpl2, bool rnd){ if(!outLC) return 0; if(smoothProf1==0) getMem(smoothProf1,profWithFlanksLength+10, "storeCorrTrack"); if(lcTmp==0) getMem(lcTmp,profWithFlanksLength+10, "storeCorrTrack"); calcSmoothProfile(0, cmpl1); // calculate smooth ptrofile c=\int f*\rho for the second profile (y) memcpy(smoothProf1,LCorrelation.re,profWithFlanksLength*sizeof(double)); calcSmoothProfile(1, cmpl2); // calculate smooth profile for the first profile (x) smoothProf2=LCorrelation.re; double av=0; double sd=track1->sd0*track2->sd0; for(int i=LFlankProfSize; i<profWithFlanksLength-RFlankProfSize; i++){ double x=smoothProf1[i] /profWithFlanksLength; //the smoothed profile for x double y=smoothProf2[i] /profWithFlanksLength; //the smoothed profile for y double lc=0; //local correlation // int ax=bTrack1.getBVal(pos1+i-LFlankProfSize,cmpl1); // int ay=bTrack2.getBVal(pos2+i-LFlankProfSize,cmpl2); // if(ax==NA || ay==NA) {lcTmp[i]=NA; continue;} // double x0=(ax==NA)?0: bTrack1.getVal(ax), y0=(ay==NA)?0: bTrack2.getVal(ay); // lc=0.5*(x0*y+y0*x); lc=x*y/sd; lc=normLC(lc); lcTmp[i]=lc; av+=lc; // We use wCorrelation.im as a tmp buffer dHist.add(lc,rnd ? 1:0); } av/=profileLength; if(!rnd) addLCProf(lcTmp,pos1); return av; }
//==================== Make correlation track double storeCorrTrack(int pos, bool cmpl1, bool cmpl2){ if(outWIG==NONE) return 0; getMem0(corr2,profWithFlanksLength+10, "storeCorrTrack"); kern->fx.re[0]=kern->fx.re0; kern->fy.re[0]=kern->fy.re0; kern->ft.re[0]=kern->ft.re0; calcSmoothProfile(1, cmpl2); memcpy(corr2,wCorrelation.re,profWithFlanksLength*sizeof(double)); calcSmoothProfile(0, cmpl1); double av=0; double sd=bTrack1.sd0*bTrack2.sd0; for(int i=LFlankProfSize; i<profWithFlanksLength-RFlankProfSize; i++){ double x=wCorrelation.re[i]/profWithFlanksLength; double y=corr2[i] /profWithFlanksLength; if((outWIG & WIG_CENTER) == WIG_CENTER) {x-=bTrack1.av0; y-=bTrack2.av0;} if((outWIG & WIG_MULT) == WIG_MULT) wCorrelation.re[i]=x*y/sd; if((outWIG & WIG_SUM ) == WIG_SUM ){ double xx=kern->fx.datRe[i], yy=kern->fy.datRe[i]; if((outWIG & WIG_CENTER) == WIG_CENTER) {xx-=bTrack1.av0; yy-=bTrack2.av0;} wCorrelation.re[i]=0.5*(xx*x+yy*y)/sd; av+=wCorrelation.re[i]; } } av/=profileLength; wigCorr.addArray(wCorrelation.re+LFlankProfSize,pos); return av; }
void smooth(const char *fname){ outLC=1; initOutLC(); int l=profileLength; bTrack *tr=new bTrack(fname); for(int i=0,k=0; i<l; i+=wProfStep,k++){ double d; d=100.*k/(l/wProfStep); if(k%10000 ==0) verb("\nSmooother: %4.1f%% (%6i/%i) ",d,k,l/wProfStep); else if(k%1000 ==0) verb("."); double *pr1=tr->getProfile(i,0); // decode the first profile. Decoder uses hasCompl and complFg flags and combines profiles if(smoothProf==0) getMem(smoothProf,profWithFlanksLength+10, "storeCorrTrack"); if(smTmp==0) getMem(smTmp,profWithFlanksLength+10, "storeCorrTrack"); kern->fftx(pr1,0); calcSmoothProfile(&(kern->fx),0, 0); // calculate smooth ptrofile c=\int f*\rho addLCProf(LCorrelation.re,i); } char pfil[4096],wfil[4096]; makeFileName(pfil,trackPath,fname); char *s=strrchr(pfil,'/'); if(s==0) s=wfil; s=strrchr(s,'.'); if(s) *s=0; sprintf(wfil,"%s_sm.bgr",pfil); //================ normalize double tt=0,ee=0,dd=0,nn=0; for(int i=0; i<l; i++) { double x=lcProfile->get(i); ee+=x; nn++; dd+=x*x; } tt=ee; ee/=nn; dd=dd/nn-ee*ee; dd=sqrt(dd); for(int i=0; i<l; i++) { double x=lcProfile->get(i); if(smoothZ){if((x-ee)/dd < smoothZ) x=0;} else {x=x*tr->total/tt/binSize;} lcProfile->set(i,x); } FILE *f=gopen(wfil,"w"); char b[4096]; strcpy(b,fname); s=strrchr(b,'.'); if(s) *s=0; s=strchr(b,'/'); if(s==0) s=b; fprintf(f,"track type=bedGraph name=\"%s_Sm\" description=\"Smoothed track. Width=%.0f\"\n",s, kernelSigma); verb("\n Write Smooth profile...\n"); writeBedGr(f,lcProfile, NA, NA); verb(" Done\n"); }
//============= calculate smoothed profile c=\int f*\rho void calcSmoothProfile(int k, bool cmpl){ Fourier *fx= (k==1) ? &kern->fy: &kern->fx; calcSmoothProfile(fx,k,cmpl); }