int main(int argc, char *argv[]) { SimpleMatrix m; assert (m.nrow() == 0); assert (m.ncol() == 0); m.resize(3, 2); assert (m.nrow() == 3); assert (m.ncol() == 2); m.zero(); assert (m[0][0] == 0.0); assert (m[2][1] == 0.0); std::vector<double> col(3, 1.0); m.appendCol(col); assert (m[0][0] == 0.0); assert (m[2][2] == 1.0); m.resize(2,3); std::vector<double> row(3, 2.0); m.appendRow(row); assert (m[0][0] == 0.0); assert (m[2][2] == 2.0); return 0; }
void printMatrix(SimpleMatrix& m, PlinkInputFile& f1) { for (int i = 0; i < m.nrow(); i++ ) { for (int j = 0; j < m.ncol(); j++) { int geno = (int) m[i][j]; char ref = f1.ref[j]; char alt = f1.alt[j]; switch(geno) { case 0: printf("%c %c\t", ref, ref); break; case 1: printf("%c %c\t", ref, alt); break; case 2: printf("%c %c\t", alt, alt); break; default: if ( geno < 0) printf(". .\t"); break; printf("ERROR reading!\n"); } } printf("\n"); } }
/** * Load covariate from @param fn, using specified @param covNameToUse, for * given * @param includedSample * covariate will be stored in @param covariate, and column names will be * stored * in @colNames * if covariate file missed some samples, those sample names will be stored in * @sampleToDrop * NOTE: for missing values in a covariate, it will drop this covariate out of * the following anaylysis * @return number of samples have covariates. * Example: * includedSample = [A, B, C] and in covaraite file we have [B, C, C, D] * then output covariate have 3 rows corresponding to [A, B, C] * row C filled by the last C in covariate file * sample D will be in sampleToDrop */ int _loadCovariate(const std::string& fn, const std::vector<std::string>& includedSample, const std::vector<std::string>& covNameToUse, DataLoader::HandleMissingCov handleMissingCov, SimpleMatrix* covariate, std::vector<std::string>* colNames, std::set<std::string>* sampleToDrop) { // load covariate SimpleMatrix mat; int ret = extractCovariate(fn, includedSample, covNameToUse, handleMissingCov, &mat, sampleToDrop); if (ret < 0) { return -1; } // create covariate sample index // const int nr = mat.nrow(); const int nc = mat.ncol(); std::map<std::string, int> covIndex; makeMap(mat.getRowName(), &covIndex); int idx = 0; for (size_t i = 0; i < includedSample.size(); ++i) { if (covIndex.find(includedSample[i]) == covIndex.end()) { sampleToDrop->insert(includedSample[i]); continue; } const int match = covIndex[includedSample[i]]; covariate->resize(idx + 1, nc); for (int j = 0; j < mat.ncol(); ++j) { (*covariate)[idx][j] = mat[match][j]; // skip row label, as MathMatrix class does not have row label } ++idx; } // set col label for (int i = 0; i < mat.ncol(); ++i) { // (*covariate).SetColumnLabel(i, mat.getColName()[i].c_str()); (*covariate).setColName(i, mat.getColName()[i]); } return 0; } // end _loadCovariate
int main(int argc, char** argv) { double genotype[] = {0, 2, -9, 2, 2, 2, 2, -9, 1, 2, 2, 2, 2, 1, 1, -9, -9, 0}; SimpleMatrix m; // marker by sample matrix m.resize(3, 6); int offset = 0; for (int i = 0; i < m.nrow(); ++i) { for (int j = 0; j < m.ncol(); ++j) { m[i][j] = genotype[offset]; ++offset; } } std::vector<std::string> iid; char buf[128]; for (int i = 0; i < m.ncol(); ++i) { sprintf(buf, "%d", i + 1); iid.push_back(buf); } std::vector<double> pheno; pheno.push_back(-9); pheno.push_back(-9); pheno.push_back(2); pheno.push_back(-9); pheno.push_back(2); pheno.push_back(2); PlinkOutputFile pout("testPlinkOutputFile.output"); pout.writeFAM(iid, iid, pheno); pout.writeBIM("1", "snp1", 0, 1, "G", "A"); pout.writeBIM("1", "snp2", 0, 2, "1", "2"); pout.writeBIM("1", "snp3", 0, 3, "A", "C"); pout.writeBED(&m, m.ncol(), m.nrow()); fprintf(stderr, "Done\n"); return 0; }
int main(int argc, char *argv[]) { // check loading all genotypes to memory PlinkInputFile f1 ("testPlinkInputFile"); SimpleMatrix m; f1.readIntoMatrix(&m); for (int i = 0; i < m.nrow(); i++ ) { for (int j = 0; j < m.ncol(); j++) { int geno = (int) m[i][j]; printf("%d ", geno); } printf("\n"); } printMatrix(m, f1); // loading some people and marker printf("----------------------------------------------------------------------\n"); m.clear(); f1.readIntoMatrix(&m, NULL, NULL); std::vector<std::string> personName, markerName; f1.readIntoMatrix(&m, &personName, &markerName); printMatrix(m, f1); // loading some people and marker printf("----------------------------------------------------------------------\n"); m.clear(); std::vector<std::string> people; std::vector<std::string> marker; people.push_back("1"); f1.readIntoMatrix(&m, &people, &marker); printMatrix(m, f1); // loading some people and marker printf("----------------------------------------------------------------------\n"); m.clear(); people.push_back("6"); marker.push_back("snp1"); f1.readIntoMatrix(&m, &people, &marker); printMatrix(m, f1); // loading some people and marker printf("----------------------------------------------------------------------\n"); m.clear(); people.push_back("2"); marker.push_back("snp2"); f1.readIntoMatrix(&m, &people, &marker); printMatrix(m, f1); return 0; }
void toMatrix(const SimpleMatrix& from, Matrix* to) { const int nr = from.nrow(); const int nc = from.ncol(); to->Dimension(nr, nc); // copy value for (int i = 0; i < nr; ++i) { for (int j = 0; j < nc; ++j) { (*to)(i, j) = from[i][j]; } } // copy col labels for (int i = 0; i < nc; ++i) { (*to).SetColumnLabel(i, from.getColName()[i].c_str()); } }