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"); } }
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()); } }