double *xtract_init_window(const int N, const int type) { double *window; window = malloc(N * sizeof(double)); switch (type) { case XTRACT_GAUSS: gauss(window, N, 0.4); break; case XTRACT_HAMMING: hamming(window, N); break; case XTRACT_HANN: hann(window, N); break; case XTRACT_BARTLETT: bartlett(window, N); break; case XTRACT_TRIANGULAR: triangular(window, N); break; case XTRACT_BARTLETT_HANN: bartlett_hann(window, N); break; case XTRACT_BLACKMAN: blackman(window, N); break; case XTRACT_KAISER: kaiser(window, N, 3 * PI); break; case XTRACT_BLACKMAN_HARRIS: blackman_harris(window, N); break; default: hann(window, N); break; } return window; }
void CCA_caseonly(bool perm, vector<vector<int> > & blperm_case, Set & S, Plink & P) { /////////////// // Output file ofstream EPI; if (!perm) { string f = par::output_file_name+".genepi"; P.printLOG("\nWriting gene-based epistasis tests to [ " + f + " ]\n"); EPI.open(f.c_str(), ios::out); EPI.precision(4); EPI << setw(12) << "NIND" << " " << setw(12) << "GENE1" << " " << setw(12) << "GENE2" << " " << setw(12) << "NSNP1" << " " << setw(12) << "NSNP2" << " " << setw(12) << "CC1" << " " // << setw(12) << "PILLAI" << " " << setw(12) << "BART" << " " << "\n"; } ////////////////////////////////// // Canonical correlation analysis // Number of genes int ns = P.snpset.size(); // Consider each pair of genes for (int s1=0; s1 < ns-1; s1++) { for (int s2 = s1+1; s2 < ns; s2++) { //////////////////////////////////////////////////////// // Step 1. Construct covariance matrix (cases only) // And partition covariance matrix: // S_11 S_21 // S_12 S_22 int n1=0, n2=0; vector<vector<double> > sigma(0); vector<double> mean(0); vector<CSNP*> pSNP(0); ///////////////////////////// // List of SNPs for both loci for (int l=0; l<P.snpset[s1].size(); l++) { if ( S.cur[s1][l] ) { pSNP.push_back( P.SNP[ P.snpset[s1][l] ] ); n1++; } } for (int l=0; l<P.snpset[s2].size(); l++) { if ( S.cur[s2][l] ) { pSNP.push_back( P.SNP[ P.snpset[s2][l] ] ); n2++; } } // NOTE: we need to make sure that n1 < n2. Migth cause problems below if this is not the case.// ********* int n12 = n1 + n2; int ne = n1 < n2 ? n1 : n2;// ne = min(p,q) /////////////////////////////////////////////////// // Choose cases-only P.setFlags(false); vector<Individual*>::iterator person = P.sample.begin(); int ncase=0; while ( person != P.sample.end() ) { if ( (*person)->aff && !(*person)->missing ) { (*person)->flag = true; ncase++; } person++; } int nind = calcGENEPIMeanVariance(pSNP, n1,n2, false, &P, mean, sigma, P.sample , blperm_case[s1], blperm_case[s2] ); /////////////////////////// // Partition covariance matrix vector<vector<double> > I11; vector<vector<double> > I11b; vector<vector<double> > I12; vector<vector<double> > I21; vector<vector<double> > I22; vector<vector<double> > I22b; sizeMatrix( I11, n1, n1); sizeMatrix( I11b, n1, n1); sizeMatrix( I12, n1, n2); sizeMatrix( I21, n2, n1); sizeMatrix( I22, n2, n2); sizeMatrix( I22b, n2, n2); // For step 4b (eigenvectors for gene2) for (int i=0; i<n1; i++) for (int j=0; j<n1; j++) { I11[i][j] = sigma[i][j]; I11b[i][j] = sigma[i][j]; } for (int i=0; i<n1; i++) for (int j=0; j<n2; j++) I12[i][j] = sigma[i][n1+j]; for (int i=0; i<n2; i++) for (int j=0; j<n1; j++) I21[i][j] = sigma[n1+i][j]; for (int i=0; i<n2; i++) for (int j=0; j<n2; j++) { I22[i][j] = sigma[n1+i][n1+j]; I22b[i][j] = sigma[n1+i][n1+j]; } //////////////////////////////////////////////////////// // Step 2. Calculate the p x p matrix M1 = inv(sqrt(sig11)) %*% sig12 %*% inv(sig22) %*% sig21 %*% inv(sqrt(sig11)) bool flag = true; I11 = msqrt(I11); I11 = svd_inverse(I11,flag); I22 = svd_inverse(I22,flag); I22b = msqrt(I22b);// For Step 4b I22b = svd_inverse(I22b,flag); I11b = svd_inverse(I11b,flag); matrix_t tmp; matrix_t M1; multMatrix(I11, I12, tmp); multMatrix(tmp, I22, M1); multMatrix(M1, I21, tmp); multMatrix(tmp, I11, M1); //////////////////////////////////////////////////////// // Step 3. Determine the p eigenvalues of M1. The sqrt(eigen(M)) = // p canonical correlations Identify the largest can corr // Compute evalues and evectors vector_t eigen = eigenvalues(M1); // Sort eigenvalues vector<double> sorted_eigen = eigen; sort(sorted_eigen.begin(),sorted_eigen.end(),greater<double>()); // P-value // long double pillai_pvalue = pillai(ncase,n1,n2,sorted_eigen[0]); long double bartlett_pvalue = bartlett(ncase,n1,n2,sorted_eigen); ///////////////////////////////////////////////////////////////////// // OUTPUT EPI << setw(12) << ncase << " " << setw(12) << P.setname[s1] << " " << setw(12) << P.setname[s2] << " " << setw(12) << n1 << " " << setw(12) << n2 << " " << setw(12) << sqrt(sorted_eigen[0]) << " " // << setw(12) << pillai_pvalue << " " << setw(12) << bartlett_pvalue << " " << "\n"; } // End of loop over genes2 } // End of loop over genes1 EPI.close(); } // End of CCA_caseonly