#include #include #include #include #include "/data/xiaoxiak/thesis/header/nrutil.c" #include "/data/xiaoxiak/thesis/header/matrix.c" #include "/data/xiaoxiak/thesis/header/random.c" #include "/data/xiaoxiak/thesis/header/sort.c" #include "/data/xiaoxiak/thesis/header/math.c" #include "/data/xiaoxiak/thesis/header/minimiz.c" #define FREE_ARG char* #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d); /* function declarations */ void intrpCalc(float *xloc,float **y,float ***vals,int n,int nseq); void intrpCalc1D(float *y,float **vals,int n); double getCost(int i,int j, int k, float *xlocInt, float *xloc,float *seq,float *mnInt); double getVarCost(int i, int j, int k, float *xlocInt, float *xloc, float *seq, float *mnInt, float *varInt); void kernelSmooth(float width,float *diffxSm, float *xSm,float *ySm,int n,float *x, float *y,int NN); int morf1DIntM(float *xlocInt, float *xloc,float *seq, int *peakIndex,float *mnInt, float *varInt, int n, int nInt, float lambda, int *optPath, double *minCost); int morf1DInt(float *xlocInt, float *xloc, float *seq, int *peakIndex,float *mnInt,int n,int nInt,float lambda, int *optPath); void seqCalEM(int **initCal,float **y,float **yInt,float *xlocInt, float *xloc,float lambda,float tol, int nseq,int n,float *yError,int **yyPeakIndex); void invertMaps(int nseq,int n,int **initCal,float **calInv); void invertMapsN(int nseq,int gg,int N,int **initCal,float **calInvN); float getSmyInt(int i,int j,float **calInv,float **y); int getSmyIntIndex(int i,int j,float **calInv); float getSmyIntN(int gg,int i,int j,float **calInv,float **yy); void getInitVal(int seqInd,float *xlocInt, float *xloc,float **y,int **initCal,int n, int nInt,int nseq,float lambda,int **yyPeakIndex); int max(int a, int b); int min(int a, int b); /* a function for debugging */ float getSmyInt1D(int n,int i,int j,float **calInv,float **y,int nInt); /* PENALTY controls how we penalize 1 for |f'-1|^2 2 for |log(f')|^2 and 3 for |f(x)-x|^2*/ int ff=1,PENALTY=3; /* maximum change;*/ int MC, MChit=1; float BASELINE; int main() { ifstream filer1("/data/xiaoxiak/thesis/data/yan/79ymass_15_con_norm_log.dat"); if(!filer1){ cerr << "Failure to open filer1\n"; getchar(); exit(EXIT_FAILURE); } ifstream filer2("/data/xiaoxiak/thesis/data/yan/79xmass_even.dat"); if(!filer2){ cerr << "Failure to open filer2\n"; getchar(); exit(EXIT_FAILURE); } ifstream filer3("/data/xiaoxiak/thesis/data/yan/79yy.peakindex.norm.80.window_400.dat"); if(!filer3){ cerr << "Failure to open filer3\n"; getchar(); exit(EXIT_FAILURE); } ofstream filew1("./m1.dat"); if(!filew1){ cerr << "Failure to open filew1\n"; getchar(); exit(EXIT_FAILURE); } ofstream filew2("./m2.dat"); if(!filew2){ cerr << "Failure to open filew2\n"; getchar(); exit(EXIT_FAILURE); } ofstream filew3("./m3.dat"); if(!filew3){ cerr << "Failure to open filew3\n"; getchar(); exit(EXIT_FAILURE); } ofstream filew4; filew4.open ("./yy.dat"); /* filew4("./yy.dat"); if(!filew4){ cerr << "Failure to open filew4\n"; getchar(); exit(EXIT_FAILURE); } */ ofstream costlog("./cost.log"); if(!costlog){ cerr << "Failure to open costlog\n"; getchar(); exit(EXIT_FAILURE); } ofstream filewparameter("./parameter.log"); if(!filewparameter){ cerr << "Failure to open filewparameter\n"; getchar(); exit(EXIT_FAILURE); } int NN=49999,nseq=79,n,nInt,gg=7,i,j,k,II=1,JJ=1,N=NN,**initCal; float width=5.0,tol=1.0e-3,lambda,*xloc,*diffloc,*xlocInt,*yLoc,*yyLoc,*xxLoc,*yError,**y,**yy, **xx,**yCal,**yInt,**calInv,**calInvN,***coefCal; int **yyPeakIndex; float xstep=0; long setSeed=-8989; time_t t1=time(NULL); yy=matrix(1,nseq,1,NN); xx=matrix(1,nseq,1,NN); yyLoc=vector(1,NN); xxLoc=vector(1,NN); yyPeakIndex=imatrix(1,nseq,1,n); /*this gives data values*/ filer1.seekg(0); for(i=1;i<=nseq;i++){ for(j=1;j<=NN;j++){ filer1 >> yy[i][j]; //if(yy[i][j]<0.0) nrerror("negative intensity"); } } /* this gives x co-ordinate-here we assume that xx is in increasing order in the file */ filer2.seekg(0); for(i=1;i<=nseq;i++){ for(j=1;j<=NN;j++){ filer2 >> xx[i][j]; } } /* this gives the base line value*/ filer3.seekg(0); for(i=1;i<=nseq;i++){ for(j=1;j<=n;j++){ filer3 >> yyPeakIndex[i][j]; } } /* II =: argmax(i) xx[i][1], JJ =: argmin(j) xx[j][NN], N =: max{xx[II][N] <= xx[JJ][NN] */ for(i=2;i<=nseq;i++){ if(xx[i][1] > xx[II][1]) II=i; if(xx[i][NN] < xx[JJ][NN]) JJ=i; } while(xx[II][N]>xx[JJ][NN]){ N--; } /*common spacing xx[II][1],xx[II][2],...xx[II][N]*/ // if(1.0*(N-n)/(n-1.0)!=int((N-n)/(n-1))) // nrerror("nonallowed choices for N,n, and gg"); // gg=(N-n)/(n-1); n=(N-1)/(gg+1)+1; nInt=ff*(n-1)+n; N=(gg+1)*(n-1)+1; cout << "n = " << n << "\n"; cout << "II = " << II << "\n"; cout << "JJ = " << JJ << "\n"; cout << "N = " << N << "\n"; filewparameter << "II = " << II << "\n"; filewparameter << "JJ = " << JJ << "\n"; filewparameter << "N = " << N << "\n"; y=matrix(1,nseq,1,n); yCal=matrix(1,nseq,1,n); yInt=matrix(1,nseq,1,nInt); xloc=vector(1,n); diffloc=vector(1,n); xlocInt=vector(1,nInt); yLoc=vector(1,n); yError=vector(1,nseq); calInv=matrix(1,nseq,1,n); calInvN=matrix(1,nseq,1,N); initCal=imatrix(1,nseq,1,n); coefCal=f3tensor(1,nseq,1,n-1,1,2); /*for(i=1;i<=n;i++) xloc[i]=xx[1]+(i-1.0)*(xx[N]-xx[1])/(n-1.0);*/ /*gg=(N-n)/(n-1); gg+1=(N-1)/(n-1)*/ /*modification #1 */ for(i=1;i<=n;i++) xloc[i]=xx[II][1+(i-1)*(gg+1)]; diffloc[1]=xx[II][2]-xx[II][1]; diffloc[n]=xx[II][N]-xx[II][N-1]; for(i=2;i=max((i-1),(1+(ff+1)*(i-1-MC-1)));k--){ if(minCostLoc > costMat[i-1][k] ){ if(PENALTY==1) temp=costMat[i-1][k]+getVarCost(i,j,k,xlocInt,xloc,seq,mnInt,varInt)+ lambda*SQR((j-k)/(ff+1.0)-1.0); else if(PENALTY==2) temp=costMat[i-1][k]+getVarCost(i,j,k,xlocInt,xloc,seq,mnInt,varInt)+ lambda*SQR(log((j-k)/(ff+1.0))); /*PENALTY==3*/ else{ if((peakIndex[i-1]==1) && (peakIndex[i] == 1) && ((xloc[i]-xloc[i-1]) != (xlocInt[j]-xlocInt[k]))) temp = 1.0e10; else{ cc=xloc[i]-xloc[i-1]+0.0; bb=(xlocInt[j]-xlocInt[k])/cc+0.0; aa=xlocInt[k]-xloc[i-1]*bb; if(fabs(bb-1)<=1e-6) temp=costMat[i-1][k]+getVarCost(i,j,k,xlocInt,xloc,seq,mnInt,varInt)+ lambda*SQR(aa); else temp=costMat[i-1][k]+getVarCost(i,j,k,xlocInt,xloc,seq,mnInt,varInt)+ lambda*(pow((aa+(bb-1)*xloc[i]),3)- pow((aa+(bb-1)*xloc[i-1]),3))/3.0/(bb-1)/cc; if(i==n) temp+=lambdaN*SQR(1.0*j-nInt); } } if(temp < minCostLoc){ /* assuming no ties */ minCostLoc=temp; costMat[i][j]=temp; pathMat[i-1][j]=k; } if(minCostLoc < 0){ cout<<"******"; cout<< "i-1=" << i-1 <<" j=" <1;i--) { optPath[i]=pathMat[i][optPath[i+1]]; if(optPath[i] ==(1+(ff+1)*(i+MC-1)) || optPath[i] == (1+(ff+1)*(i-MC-1))){ cout<<"hit the boundary at " << i << " step" << endl; sethit=1; } if(sethit==1) break; } optPath[1]=1; free_imatrix(pathMat,1,n-1,1,nInt); free_dmatrix(costMat,1,n,1,nInt); return sethit; } void kernelSmooth(float width,float *diffxSm,float *xSm,float *ySm,int n,float *x, float *y,int NN) { /* gets f(x)=ySm of length n for a set of x's from data vector y (which are both of length N-get average over region of total width 2*width-rectangular kernel here*/ /* cntNew is the guess for the index of the first x that corresponds to the first xSm and wndwMx controls size of window on index scale to search for relevant x */ int i,j,k,kOld; float m,width_x; k=kOld=1; for(i=1;i<=n;i++){ /* cnt is the guess of the index for the x val corresponding to xSm-set up a window in the index of width called wndwMx through which look for relevant y points */ ySm[i]=0.0; m=0.0; width_x=width*diffxSm[i]; for(j=k;j<=NN;j++){ // cout << "i=" << i << " j=" << j << " xSm[i]= " << xSm[i] << " x[j]= " << x[j] << " dif=" << fabs(xSm[i]-x[j]) << endl; if(fabs(xSm[i]-x[j]) <= 0.5*width_x){ //cout << "get here" << endl; ySm[i]+=y[j]; m+=1.0; } if(x[j]-0.5*width_x > xSm[i]){ if(iwidth_x){ k=j; kOld=j; } if(xSm[i+1]-xSm[i]