Example #1
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());
  }
}
Example #2
0
/**
 * 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