Skip to content
This repository has been archived by the owner on Oct 17, 2022. It is now read-only.

Commit

Permalink
Add options to write out hypo/meth posterior probabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
jqujqu committed Mar 1, 2017
1 parent 44a55a5 commit 8daee54
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/analysis/hmr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <cmath>
#include <fstream>
#include <iomanip>
#include <string>

#include <unistd.h>

Expand Down Expand Up @@ -312,6 +313,8 @@ main(int argc, const char **argv) {
try {

string outfile;
string hypo_post_outfile;
string meth_post_outfile;

size_t desert_size = 1000;
size_t max_iterations = 10;
Expand Down Expand Up @@ -339,6 +342,12 @@ main(int argc, const char **argv) {
opt_parse.add_opt("verbose", 'v', "print more run info", false, VERBOSE);
opt_parse.add_opt("partial", '\0', "identify PMRs instead of HMRs",
false, PARTIAL_METH);
opt_parse.add_opt("post-hypo", '\0', "output file for single-CpG posteiror "
"hypomethylation probability (default: NULL)",
false, hypo_post_outfile);
opt_parse.add_opt("post-meth", '\0', "output file for single-CpG posteiror "
"methylation probability (default: NULL)",
false, meth_post_outfile);
opt_parse.add_opt("params-in", 'P', "HMM parameters file (no training)",
false, params_in_file);
opt_parse.add_opt("params-out", 'p', "write HMM parameters to this file",
Expand Down Expand Up @@ -472,6 +481,50 @@ main(int argc, const char **argv) {
domains[i].set_name("HYPO" + smithlab::toa(good_hmr_count++));
out << domains[i] << '\n';
}

/***********************************
* STEP 6: (OPTIONAL) WRITE POSTERIOR
*/

if (!hypo_post_outfile.empty() || !meth_post_outfile.empty()) {
bool fg_class = true;
vector<double> fg_posterior;
hmm.PosteriorScores(meth, reset_points, start_trans, trans,
end_trans, fg_alpha, fg_beta, bg_alpha,
bg_beta, fg_class, fg_posterior);

if (!hypo_post_outfile.empty()) {
if (VERBOSE)
cerr << "[WRITING " << hypo_post_outfile
<< " (4th field: CpG:<M_reads>:<U_reads>)]" << endl;
std::ofstream of;
of.open(hypo_post_outfile.c_str());
std::ostream out(of.rdbuf());
for (size_t i = 0; i < cpgs.size(); ++i) {
GenomicRegion cpg(cpgs[i]);
cpg.set_name("CpG:" + toa(static_cast<size_t>(meth[i].first)) +
":" + toa(static_cast<size_t>(meth[i].second)));
cpg.set_score(scores[i]);
out << cpg << '\n';
}
}

if (!meth_post_outfile.empty()) {
std::ofstream of;
of.open(meth_post_outfile.c_str());
std::ostream out(of.rdbuf());
if (VERBOSE)
cerr << "[WRITING " << meth_post_outfile
<< " (4th field: CpG:<M_reads>:<U_reads>)]" << endl;
for (size_t i = 0; i < cpgs.size(); ++i) {
GenomicRegion cpg(cpgs[i]);
cpg.set_name("CpG:" + toa(static_cast<size_t>(meth[i].first)) +
":" + toa(static_cast<size_t>(meth[i].second)));
cpg.set_score(1.0 - scores[i]);
out << cpg << '\n';
}
}
}
}
catch (SMITHLABException &e) {
cerr << "ERROR:\t" << e.what() << endl;
Expand Down

0 comments on commit 8daee54

Please sign in to comment.