#include "includes.h" #include "grid.h" #include "vector.h" #include extern ofstream errorfs; #include "declare.h" void makejf3(dgrid &jointfreq3mkv2, const dgrid &jointfreq2, const dgrid &freqfreq, const ivectors &allele_list, const int &debug) { int i,j,k,l; const int numloc=allele_list.dim(); int dum; double value; //initialize three-locus-haplotype frequencies object //find number of rows needed int max=0; for (l=0;lmax) max=dum; } //cout << "max " << max << endl; jointfreq3mkv2.init(max,numloc-2); //cout << jointfreq3mkv2.dimx() << " " << jointfreq3mkv2.dimy() << endl; jointfreq3mkv2.clear(0); //fill in for (l=0;l0) value= jointfreq2(i*allele_list[l+1].dim()+j,l)* jointfreq2(j*allele_list[l+2].dim()+k,l+1)/freqfreq(j,l+1); else value=0.0; jointfreq3mkv2(i*allele_list[l+1].dim()*allele_list[l+2].dim()+ j*allele_list[l+2].dim()+ k,l)=value; } } } } //check that all columns sum to 1 for (l=0;lmax) max=dum; } //cout << "max " << max << endl; jointfreq3fmkv2.init(max,numloc-2); // cout << jointfreq3mkv2.dimx() << " " << jointfreq3mkv2.dimy() << endl; jointfreq3fmkv2.clear(0); //fill in for (l=0;l1) jointfreq4mkv2(i,0)=jointfreq3mkv2(i,C-2); if (C1) jointfreq4fmkv2(i,0)=jointfreq3fmkv2(i,C-2); if (C jointfreq4fmkv2(ipos*allelecounts(ilocus+3)+jpos,pos4) ) ) errorfs <<"WARNING: freq. retrieved from jointfreq4fmkv2 is <=0" << endl; return jointfreq4fmkv2(ipos*allelecounts(ilocus+3)+jpos,pos4); } double getjointfreq4mkv2(const igrid &freqallele, const ivector &allelecounts, const dgrid &jointfreq4mkv2, const int &ilocus, const int &iallele, const int &jallele, const int &kallele, const int &C) { //this function retrieves a conditional allele frequency int l; int ipos=-1, jpos=-1, kpos=-1; int pos4=1; if (C-ilocus==2) pos4=0; //find positions for iallele and jallele and kallele for (l=0;ljointfreq4mkv2(ipos* allelecounts(ilocus+1+pos4)* allelecounts(ilocus+3)+ jpos* allelecounts(ilocus+3)+ kpos ,pos4) ) ) errorfs <<"WARNING: freq. retrieved from jointfreq4mkv2 is <=0" << endl; return jointfreq4mkv2(ipos* allelecounts(ilocus+1+pos4)* allelecounts(ilocus+3)+ jpos* allelecounts(ilocus+3)+ kpos ,pos4); } void augmentdatamkv2(dgrid &augjointfreq3mkv2, const dgrid &jointfreq3mkv2, dgrid &augjointfreq3fmkv2, const dgrid &jointfreq3fmkv2, const dgrid &jointfreq2, const dgrid &freqfreq, const ivector &allelecounts, const int &C, const int &debug) { int i,j,k,l; double value=0.0; //MAKE augjointfreq3mkv2 augjointfreq3mkv2.clear(0.0); //make augjointfreq3mkv2 //make columns 0,...,C-3 for (j = 0; j < C-2; j++) { for (i=0; i< jointfreq3mkv2.dimx(); i++) { augjointfreq3mkv2(i,j)=jointfreq3mkv2(i,j); } } //make column C-2 if (C>1) { for (i=0; i1) { for (i=0; i0.0) condfreq3lmkv2(i*allelecounts(t+1)*allelecounts(t+2)+ j*allelecounts(t+2)+ k,t) =jointfreq3mkv2(i*allelecounts(t+1)*allelecounts(t+2)+ j*allelecounts(t+2)+ k,t)/ jointfreq2(i*allelecounts(t+1)+j,t); } } } } if (debug==1) { cout << "condfreq3lmkv2" << endl; for (i=0;i0.0) ) condfreq3rmkv2(i*allelecounts(t+1)*allelecounts(t+2)+ j*allelecounts(t+2)+ k,t) =jointfreq3mkv2(i*allelecounts(t+1)*allelecounts(t+2)+ j*allelecounts(t+2)+ k,t)/ jointfreq2(j*allelecounts(t+2)+k,t+1); } } } } if (debug==1) { cout << "condfreq3rmkv2" << endl; for (i=0;iC) { for (k=0;k=0) { //make column of allele frequencies if (data(h,LHSle)==0) obsprobs(2*h+1,LHSle)=1; else obsprobs(2*h+1,LHSle)= getfreq(freqallele, freqfreq, data(h,LHSle), LHSle); //make column of conditional allele frequencies if (LHSre-LHSle>0) { if (data(h,LHSle+1)==0) obsprobs(2*h+1,LHSle+1)=1; else { if (data(h,LHSle)==0) obsprobs(2*h+1,LHSle+1)= getfreq(freqallele, freqfreq, data(h,LHSle+1), LHSle+1); else obsprobs(2*h+1,LHSle+1)=getcondfreq2l(freqallele, allelecounts, condfreq2l, LHSle, data(h,LHSle), data(h,LHSle+1)); } if (LHSre-LHSle>1) { for (l=LHSle+2;l<=LHSre;l++) { if (data(h,l)==0) obsprobs(2*h+1,l)=1; else { if (data(h,l-1)==0) { if (data(h,l-2)==0) { obsprobs(2*h+1,l)= getfreq(freqallele, freqfreq, data(h,l), l); } else {//data(h,l-2)!=0) obsprobs(2*h+1,l)= getcondfreq3fmkv2(freqallele, allelecounts, condfreq3fmkv2, l-2, data(h,l-2), data(h,l) ); } } else {//data(h,l-1)!=0 if (data(h,l-2)==0) { obsprobs(2*h+1,l)= getcondfreq2l(freqallele, allelecounts, condfreq2l, l-1, data(h,l-1), data(h,l)); } else {//data(h,l-2)!=0) obsprobs(2*h+1,l)= getcondfreq3mkv2(freqallele, allelecounts, condfreq3lmkv2, l-2, data(h,l-2), data(h,l-1), data(h,l)); } } } } } } } //make RHS if (RHSre>=0) { //make column of allele frequencies if (data(h,RHSre)==0) obsprobs(2*h+1,RHSre)=1; else obsprobs(2*h+1,RHSre)= getfreq(freqallele, freqfreq, data(h,RHSre), RHSre); //make column of conditional allele frequencies if (RHSre-RHSle>0) { if (data(h,RHSre-1)==0) obsprobs(2*h+1,RHSre-1)=1; else { if (data(h,RHSre)==0) obsprobs(2*h+1,RHSre-1)= getfreq(freqallele, freqfreq, data(h,RHSre-1), RHSre-1); else obsprobs(2*h+1,RHSre-1)=getcondfreq2r(freqallele, allelecounts, condfreq2r, RHSre-1, data(h,RHSre-1), data(h,RHSre), C); } if (RHSre-RHSle>1) { for (l=RHSre-2;l>=RHSle;l--) { if (data(h,l)==0) obsprobs(2*h+1,l)=1; else { if (data(h,l+1)==0) { if (data(h,l+2)==0) { obsprobs(2*h+1,l)= getfreq(freqallele, freqfreq, data(h,l), l); } else {//data(h,l+2)!=0) //delete me obsprobs(2*h+1,l)= getcondfreq3fmkv2(freqallele, allelecounts, condfreq3fmkv2, l, data(h,l), data(h,l+2) ); } } else {//data(h,l+1)!=0 if (data(h,l+2)==0) { obsprobs(2*h+1,l)= getcondfreq2r(freqallele, allelecounts, condfreq2r, l, data(h,l), data(h,l+1), C); } else {//data(h,l+2)!=0) obsprobs(2*h+1,l)= getcondfreq3mkv2(freqallele, allelecounts, condfreq3rmkv2, l, data(h,l), data(h,l+1), data(h,l+2)); } } } } } } } } if (debug) { cout << "obsprobs (N: haplotype frequencies)" << endl; for (l=0;lcondfreq3fmkv2(ipos*allelecounts(ilocus+2)+jpos,ilocus) ) ) errorfs <<"WARNING: freq. retrieved from condfreq3fmkv2 is <=0" << endl; return condfreq3fmkv2(ipos*allelecounts(ilocus+2)+jpos,ilocus); } double getcondfreq3mkv2(const igrid &freqallele, const ivector &allelecounts, const dgrid &condfreq3mkv2, const int &ilocus, const int &iallele, const int &jallele, const int &kallele) { //this function retrieves a conditional allele frequency int l; int ipos=-1, jpos=-1, kpos=-1; //find positions for iallele and jallele and kallele for (l=0;l0); if ( !(-0.0001condfreq3mkv2(ipos* allelecounts(ilocus+1)* allelecounts(ilocus+2)+ jpos* allelecounts(ilocus+2)+ kpos ,ilocus) ) ) { errorfs << ilocus << " " << ipos << " " << jpos << " " << kpos << " " << iallele << " " << jallele << " " << kallele << " " << condfreq3mkv2(ipos*allelecounts(ilocus+1)* allelecounts(ilocus+2)+ jpos*allelecounts(ilocus+2)+kpos,ilocus) << endl; errorfs <<"WARNING: freq. retrieved from condfreq3mkv2 is " << endl << "not in unit interval" << endl; exit(1);} return condfreq3mkv2(ipos* allelecounts(ilocus+1)* allelecounts(ilocus+2)+ jpos* allelecounts(ilocus+2)+ kpos ,ilocus); } void makealphamkv2(dgrid &alpha, const dgrid &initstore, const dgrid &transstore, const dgrid &obsprobs, const dvector &obsprobsAstore, const int &C, const int &LE, const int &RE) { //assembles alpha object int i,j,h,t; const int numhap=alpha.dimx()/2; int LHSle, LHSre, RHSle, RHSre; double sum; //these are endpoints for t, rather than t+1 if (LE=RHSle;t--) { for (i=0;i<2;i++) { sum=0; for (j=0;j<2;j++) { sum+= beta(2*h+j,t+1)*transstore(2*i+j,t)*obsprobs(2*h+j,t+1); } beta(2*h+i,t)=sum; } } if (LE=LHSle;t--) { for (i=0;i<2;i++) { sum=0; for (j=0;j<2;j++) { sum+= beta(2*h+j,t+1)*transstore(2*i+j,t)*obsprobs(2*h+j,t+1); } beta(2*h+i,t)=sum; } } if (CC+1) { /* //delete me if (C==16 && h==11) { cout << data(h,C-2) << " " << data(h,C-1) << " " << data(h,C) << " " << data(h,C+1) << " " << data(h,C+2) << " " << "obsA " << getcondfreq4lSMART(freqallele, freqfreq, allelecounts, jointfreq2, jointfreq3mkv2, jointfreq4mkv2, jointfreq4fmkv2, C-1, data(h,C-1), data(h,C), data(h,C+1), data(h,C+2), C) << " " << getcondfreq4lSMART(freqallele, freqfreq, allelecounts, jointfreq2, jointfreq3mkv2, jointfreq4mkv2, jointfreq4fmkv2, C-2, data(h,C-2), data(h,C-1), data(h,C), data(h,C+1), C) << " " << getfreqSMART(freqallele,freqfreq,data(h,C+1),C+1) << " " << getcondfreq2lSMART(freqallele,freqfreq, allelecounts,jointfreq2, C+1,data(h,C+1),data(h,C+2)) << endl; exit(1);} */ obsprobsAstore(2*h+1)= getcondfreq4lSMART(freqallele, freqfreq, allelecounts, jointfreq2, jointfreq3mkv2, jointfreq4mkv2, jointfreq4fmkv2, C-2, data(h,C-2), data(h,C-1), data(h,C), data(h,C+1), C)* getcondfreq4lSMART(freqallele, freqfreq, allelecounts, jointfreq2, jointfreq3mkv2, jointfreq4mkv2, jointfreq4fmkv2, C-1, data(h,C-1), data(h,C), data(h,C+1), data(h,C+2), C)/ ( getfreqSMART(freqallele, freqfreq, data(h,C+1), C+1)* getcondfreq2lSMART(freqallele, freqfreq, allelecounts, jointfreq2, C+1, data(h,C+1), data(h,C+2)) ); } else { if (LEC+1) { obsprobsAstore(2*h+1)= getcondfreq3lSMART(freqallele,freqfreq,allelecounts, jointfreq2, jointfreq3mkv2,jointfreq3fmkv2, C-1, data(h,C-1),data(h,C),data(h,C+1))* getcondfreq4lSMART(freqallele, freqfreq, allelecounts, jointfreq2, jointfreq3mkv2, jointfreq4mkv2, jointfreq4fmkv2, C-1, data(h,C-1), data(h,C), data(h,C+1), data(h,C+2),C)/ ( getfreqSMART(freqallele, freqfreq, data(h,C+1), C+1)* getcondfreq2lSMART(freqallele, freqfreq, allelecounts, jointfreq2, C+1, data(h,C+1), data(h,C+2)) ); } else { obsprobsAstore(2*h+1)= getcondfreq3lSMART(freqallele, freqfreq, allelecounts, jointfreq2, jointfreq3mkv2, jointfreq3fmkv2, C-1, data(h,C-1),data(h,C),data(h,C+1))/ getfreqSMART(freqallele, freqfreq, data(h,C+1), C+1); } } } } if (debug) { cout << "ObsprobsAstore" << endl; for (h=0;hmax) max=dum; } //cout << "max " << max << endl; jointfreq3mkv2.init(max,numloc-2); // cout << jointfreq3mkv2.dimx() << " " << jointfreq3mkv2.dimy() << endl; jointfreq3mkv2.clear(0); //fill in for (l=0;lmax) max=dum; } //cout << "max " << max << endl; jointfreq2.init(max,numloc-1); // cout << jointfreq3mkv2.dimx() << " " << jointfreq3mkv2.dimy() << endl; jointfreq2.clear(0); for (l=0;lrw) rw=rwtemp; } //initialize jointfreq2; jointfreq3.init(rw,num_markers-2); //fill in jointfreq3 for (t=0;t