// Gompertz model void tp5(arma::mat& g3, int lk, double gam, double om) { while(om==1) { om = 0.999 + rand() * 0.002 / RAND_MAX; } for(int n2=0; n2<lk; n2++) { g3.at(0, n2) = Rf_dpois(n2, 0, false); } for(int n1=1; n1<lk; n1++) { for(int n2=0; n2<lk; n2++) { g3.at(n1, n2) = Rf_dpois(n2, n1*exp(gam * (1 - log(n1)/log(om))), false); } } }
// trend model void tp3(arma::mat& g3, int lk, double gam) { for(int n1=0; n1<lk; n1++) { for(int n2=0; n2<lk; n2++) { g3.at(n1, n2) = Rf_dpois(n2, gam*n1, false); } } }
// Ricker model void tp4(arma::mat& g3, int lk, double gam, double om) { for(int n1=0; n1<lk; n1++) { for(int n2=0; n2<lk; n2++) { g3.at(n1, n2) = Rf_dpois(n2, n1*exp(gam*(1-n1/om)), false); } } }
// Gompertz + immigration model void tp5(arma::mat& g3, int lk, double gam, double om, double imm) { for(int n1=0; n1<lk; n1++) { for(int n2=0; n2<lk; n2++) { g3.at(n1, n2) = Rf_dpois(n2, n1*exp(gam * (1 - log(double (n1) + 1)/log(om + 1))) + imm, false); } } }
/*! * \brief LAArmadillo::arma2OiMat * \param result * \param m */ void LAArmadillo::arma2OiMat(OiMat &result, const arma::mat &m){ for(int i = 0; i < m.n_rows; i++){ for(int j = 0; j < m.n_cols; j++){ result.setAt(i, j, m.at(i, j)); } } }
/*! * \brief LAArmadillo::oiMat2Arma * \param result * \param m */ void LAArmadillo::oiMat2Arma(arma::mat &result, const OiMat &m){ for(int row = 0; row < m.getRowCount(); row++){ for(int col = 0; col < m.getColCount(); col++){ result.at(row, col) = m.getAt(row, col); } } }
/******************************************************************* * 函数功能:几何校正需要调用的函数 * * */ arma::mat toAffinity(arma::mat &f){ arma::mat A; arma::mat v; v << 0 << 0 << 1 << arma::endr; int flag = f.n_rows; switch(flag){ case 6:{ // oriented ellipses arma::mat T = f.rows(0, 1); arma::mat tmp = join_horiz(f.rows(2, 3), f.rows(4, 5)); arma::mat tmp1 = join_horiz(tmp, T); A = join_vert(tmp1, v); break;} case 4:{ // oriented discs arma::mat T = f.rows(0, 1); double s = f.at(2,0); double th = f.at(3,0); arma::mat S = arma::randu<arma::mat>(2,2); /*S.at(0, 0) = s*cos(th); S.at(0, 1) = -s*sin(th); S.at(1, 0) = s*sin(th); S.at(1, 1) = s*cos(th);*/ S << s*cos(th) << -s*sin(th) << arma::endr << s*sin(th) << s*cos(th) << arma::endr; arma::mat tmp1 = join_horiz(S, T); A = join_vert(tmp1, v); //A.print("A ="); break;} /*case 3:{ // discs mat T = f.rows(0, 1); mat s = f.row(2); int th = 0 ; A = [s*[cos(th) -sin(th) ; sin(th) cos(th)], T ; 0 0 1] ; } case 5:{ // ellipses mat T = f.rows(0, 1); A = [mapFromS(f(3:5)), T ; 0 0 1] ; }*/ default: std::cout << "出错啦!" << std::endl; break; } return A; }
// constant model void tp1(arma::mat& g3, int lk, double gam, double om) { int Nmin=0; for(int n1=0; n1<lk; n1++) { for(int n2=0; n2<lk; n2++) { Nmin = std::min(n1, n2); for(int c=0; c<=Nmin; c++) { g3.at(n1, n2) += exp(Rf_dbinom(c, n1, om, true) + Rf_dpois(n2-c, gam, true)); } } } }
arma::mat compute_dct(arma::mat& inp) { arma::mat out(8,8); for(int v = 0; v < 8; v++) { for(int u = 0; u < 8; u++) { double Cu, Cv, z = 0.0; COEFFS(Cu,Cv,u,v); for(int y = 0; y < 8; y++) { for(int x = 0; x < 8; x++) { double s, q; s = inp.at(x,y); q = s * cos((double)(2*x+1) * (double)u * M_PI/16.0) * cos((double)(2*y+1) * (double)v * M_PI/16.0); z += q; }} out.at(u,v) = 0.25 * Cu * Cv * z; }} return out; }
Rcpp::NumericMatrix rcpp_segIBDandNVersion2(std::string pathThisBreed, int NFileC, int NC, const arma::ivec& ArmaIndexC, const arma::mat& ArmaNat, int minSNP, double minL, const arma::vec& ArmaPos, const arma::vec& Armakb, double a, std::string stdsymB, int skip, int cskip) { int m, m2, i, j, r, rK, endoffile, gleich; double L, w, lSEG ; char str1[100]; char symB = stdsymB.at(0); FILE *fC; Rcpp::NumericMatrix confROH(NC, NC); int K = (minSNP<=60)?(minSNP/2):(30); int M = Armakb.n_elem - 1; size_t bufsize = 2*NFileC; char* Line = (char*)malloc(bufsize*sizeof(char)); if(Line == NULL){error_return("Memory allocation failed.");}; int** Nat = (int**)calloc(NC,sizeof(int*)); double** fROH = (double**)calloc(NC,sizeof(double*)); int** thisROH = (int**)calloc(NC,sizeof(int*)); int* currAllelesC = (int*)calloc(NC,sizeof(int)); int* prevAllelesC = (int*)calloc(NC,sizeof(int)); int* indexC = (int*)calloc(NC,sizeof(int)); double* Pos = (double*)calloc(ArmaPos.n_elem, sizeof(double)); double* kb = (double*)calloc(Armakb.n_elem, sizeof(double)); if(Nat == NULL){error_return("Memory allocation failed.");}; if(fROH == NULL){error_return("Memory allocation failed.");}; if(thisROH == NULL){error_return("Memory allocation failed.");}; if(currAllelesC == NULL){error_return("Memory allocation failed.");}; if(prevAllelesC == NULL){error_return("Memory allocation failed.");}; if(indexC == NULL){error_return("Memory allocation failed.");}; if(Pos == NULL){error_return("Memory allocation failed.");}; if(kb == NULL){error_return("Memory allocation failed.");}; for(m=0;m<M+1;m++){ Pos[m] = ArmaPos.at(m); kb[m] = Armakb.at(m); } for(i=0; i<NC;i++){ indexC[i] = ArmaIndexC.at(i); fROH[i] = (double*)calloc(i+1, sizeof(double)); thisROH[i]= (int*)calloc(i+1, sizeof(int)); Nat[i] = (int*)calloc(M, sizeof(int)); if(fROH[i] == NULL){error_return("Memory allocation failed.");}; if(thisROH[i] == NULL){error_return("Memory allocation failed.");}; if(Nat[i] == NULL){error_return("Memory allocation failed.");}; for(m=0; m<M;m++){ Nat[i][m] = ArmaNat.at(m,i); } } fC = fopen(pathThisBreed.c_str(),"r"); if(fC == NULL){error_return("File opening failed.");}; for(i=0;i<skip+1;i++){ while(fgetc(fC)!='\n'){} } endoffile=0; m=0; while(!endoffile){ for(i=0; i<NC;i++){ prevAllelesC[i] = currAllelesC[i]; currAllelesC[i] = 0; } rK=0; while(rK<K){ for(i=0; i<cskip; i++){ endoffile = fscanf(fC, "%s ", str1)<1; if(endoffile){break;} } if(endoffile){break;} endoffile = fgets(Line,2*NFileC,fC)==NULL; if(endoffile){break;} for(i=0; i<NC;i++){ if(Line[2*indexC[i]]==symB){currAllelesC[i]= currAllelesC[i] | (1u<<rK);} } rK++; } if(endoffile){Rprintf("M=%d\n",m+rK);} if(rK==0){break;} for(i=0; i<NC;i++){ for(j=0; j<i+1; j++){ if(currAllelesC[i]==currAllelesC[j]){ if(prevAllelesC[i]==prevAllelesC[j] && m>0){ /* ROH verlängern */ thisROH[i][j] += rK; }else{ /* neuer ROH */ thisROH[i][j] = rK; if(m>0){ gleich = ~(prevAllelesC[i] ^ prevAllelesC[j]); r = K-1; while(r>=0 && ((gleich>>r)&1u)){ thisROH[i][j] += 1; r--; } } } }else{ if(prevAllelesC[i]==prevAllelesC[j] && m>0){ /* ROH beenden */ gleich = ~(currAllelesC[i] ^ currAllelesC[j]); r = 0; while(r<K && ((gleich>>r)&1u)){ thisROH[i][j] += 1; r++; } if(thisROH[i][j]>=minSNP){ L = Pos[m+r]-Pos[m+r-thisROH[i][j]]; if(L>=minL){ w = L*L/(a+L*L); lSEG = 0.0; for(m2=m+r-thisROH[i][j];m2<m+r;m2++){ if(Nat[i][m2]*Nat[j][m2]>0){lSEG += kb[m2+1]-kb[m2];} } fROH[i][j] += w*lSEG; } } thisROH[i][j] = 0; } } }