X-Git-Url: https://git.donarmstrong.com/?p=rsem.git;a=blobdiff_plain;f=EM.cpp;h=bf15c2b4b6493ca3674804428bc456540f4aa345;hp=2ce946fd6e97b3e7deb0b33b7ab42b143efe3046;hb=636b82d9f60ebcbec7ef1b73ba23bbbacfd8b36a;hpb=543f20967e693ef9150eddc94e7869ed04bad962 diff --git a/EM.cpp b/EM.cpp index 2ce946f..bf15c2b 100644 --- a/EM.cpp +++ b/EM.cpp @@ -44,6 +44,8 @@ #include "HitWrapper.h" #include "BamWriter.h" +#include "WriteResults.h" + using namespace std; const double STOP_CRITERIA = 0.001; @@ -68,7 +70,7 @@ bool genGibbsOut; // generate file for Gibbs sampler char refName[STRLEN], outName[STRLEN]; char imdName[STRLEN], statName[STRLEN]; -char refF[STRLEN], groupF[STRLEN], cntF[STRLEN], tiF[STRLEN]; +char refF[STRLEN], cntF[STRLEN], tiF[STRLEN]; char mparamsF[STRLEN]; char modelF[STRLEN], thetaF[STRLEN]; @@ -83,7 +85,6 @@ vector theta, eel; // eel : expected effective length double *probv, **countvs; Refs refs; -GroupInfo gi; Transcripts transcripts; ModelParams mparams; @@ -271,111 +272,11 @@ void* calcConProbs(void* arg) { return NULL; } -template -void calcExpectedEffectiveLengths(ModelType& model) { - int lb, ub, span; - double *pdf = NULL, *cdf = NULL, *clen = NULL; // clen[i] = sigma_{j=1}^{i}pdf[i]*(lb+i) - - model.getGLD().copyTo(pdf, cdf, lb, ub, span); - clen = new double[span + 1]; - clen[0] = 0.0; - for (int i = 1; i <= span; i++) { - clen[i] = clen[i - 1] + pdf[i] * (lb + i); - } - - eel.clear(); - eel.resize(M + 1, 0.0); - for (int i = 1; i <= M; i++) { - int totLen = refs.getRef(i).getTotLen(); - int fullLen = refs.getRef(i).getFullLen(); - int pos1 = max(min(totLen - fullLen + 1, ub) - lb, 0); - int pos2 = max(min(totLen, ub) - lb, 0); - - if (pos2 == 0) { eel[i] = 0.0; continue; } - - eel[i] = fullLen * cdf[pos1] + ((cdf[pos2] - cdf[pos1]) * (totLen + 1) - (clen[pos2] - clen[pos1])); - assert(eel[i] >= 0); - if (eel[i] < MINEEL) { eel[i] = 0.0; } - } - - delete[] pdf; - delete[] cdf; - delete[] clen; -} - template void writeResults(ModelType& model, double* counts) { - double denom; - char outF[STRLEN]; - FILE *fo; - - sprintf(modelF, "%s.model", statName); - model.write(modelF); - - //calculate tau values - double *tau = new double[M + 1]; - memset(tau, 0, sizeof(double) * (M + 1)); - - denom = 0.0; - for (int i = 1; i <= M; i++) - if (eel[i] >= EPSILON) { - tau[i] = theta[i] / eel[i]; - denom += tau[i]; - } - - general_assert(denom > 0, "No alignable reads?!"); - - for (int i = 1; i <= M; i++) { - tau[i] /= denom; - } - - //isoform level results - sprintf(outF, "%s.iso_res", imdName); - fo = fopen(outF, "w"); - for (int i = 1; i <= M; i++) { - const Transcript& transcript = transcripts.getTranscriptAt(i); - fprintf(fo, "%s%c", transcript.getTranscriptID().c_str(), (i < M ? '\t' : '\n')); - } - for (int i = 1; i <= M; i++) - fprintf(fo, "%.2f%c", counts[i], (i < M ? '\t' : '\n')); - for (int i = 1; i <= M; i++) - fprintf(fo, "%.15g%c", tau[i], (i < M ? '\t' : '\n')); - for (int i = 1; i <= M; i++) { - const Transcript& transcript = transcripts.getTranscriptAt(i); - fprintf(fo, "%s%c", transcript.getGeneID().c_str(), (i < M ? '\t' : '\n')); - } - fclose(fo); - - //gene level results - sprintf(outF, "%s.gene_res", imdName); - fo = fopen(outF, "w"); - for (int i = 0; i < m; i++) { - const string& gene_id = transcripts.getTranscriptAt(gi.spAt(i)).getGeneID(); - fprintf(fo, "%s%c", gene_id.c_str(), (i < m - 1 ? '\t' : '\n')); - } - for (int i = 0; i < m; i++) { - double sumC = 0.0; // sum of counts - int b = gi.spAt(i), e = gi.spAt(i + 1); - for (int j = b; j < e; j++) sumC += counts[j]; - fprintf(fo, "%.2f%c", sumC, (i < m - 1 ? '\t' : '\n')); - } - for (int i = 0; i < m; i++) { - double sumT = 0.0; // sum of tau values - int b = gi.spAt(i), e = gi.spAt(i + 1); - for (int j = b; j < e; j++) sumT += tau[j]; - fprintf(fo, "%.15g%c", sumT, (i < m - 1 ? '\t' : '\n')); - } - for (int i = 0; i < m; i++) { - int b = gi.spAt(i), e = gi.spAt(i + 1); - for (int j = b; j < e; j++) { - fprintf(fo, "%s%c", transcripts.getTranscriptAt(j).getTranscriptID().c_str(), (j < e - 1 ? ',' : (i < m - 1 ? '\t' :'\n'))); - } - } - fclose(fo); - - delete[] tau; - - if (verbose) { printf("Expression Results are written!\n"); } + sprintf(modelF, "%s.model", statName); + model.write(modelF); + writeResultsEM(M, refName, imdName, transcripts, theta, eel, countvs[0]); } template @@ -423,7 +324,6 @@ void EM() { Params fparams[nThreads]; pthread_t threads[nThreads]; pthread_attr_t attr; - void *status; int rc; @@ -471,7 +371,7 @@ void EM() { } for (int i = 0; i < nThreads; i++) { - rc = pthread_join(threads[i], &status); + rc = pthread_join(threads[i], NULL); pthread_assert(rc, "pthread_join", "Cannot join thread " + itos(i) + " (numbered from 0) at ROUND " + itos(ROUND) + "!"); } @@ -521,7 +421,7 @@ void EM() { pthread_assert(rc, "pthread_create", "Cannot create thread " + itos(i) + " (numbered from 0) when generating files for Gibbs sampler!"); } for (int i = 0; i < nThreads; i++) { - rc = pthread_join(threads[i], &status); + rc = pthread_join(threads[i], NULL); pthread_assert(rc, "pthread_join", "Cannot join thread " + itos(i) + " (numbered from 0) when generating files for Gibbs sampler!"); } } @@ -552,28 +452,8 @@ void EM() { fout.close(); } - sprintf(thetaF, "%s.theta", statName); - fo = fopen(thetaF, "w"); - fprintf(fo, "%d\n", M + 1); - - // output theta' - for (int i = 0; i < M; i++) fprintf(fo, "%.15g ", theta[i]); - fprintf(fo, "%.15g\n", theta[M]); - - //calculate expected effective lengths for each isoform - calcExpectedEffectiveLengths(model); - - //correct theta vector - sum = theta[0]; - for (int i = 1; i <= M; i++) - if (eel[i] < EPSILON) { theta[i] = 0.0; } - else sum += theta[i]; - - general_assert(sum >= EPSILON, "No Expected Effective Length is no less than" + ftos(MINEEL, 6) + "?!"); - - for (int i = 0; i <= M; i++) theta[i] /= sum; - //calculate expected weights and counts using learned parameters + //just use the raw theta learned from the data, do not correct for eel or mw updateModel = false; calcExpectedWeights = true; for (int i = 0; i <= M; i++) probv[i] = theta[i]; for (int i = 0; i < nThreads; i++) { @@ -581,7 +461,7 @@ void EM() { pthread_assert(rc, "pthread_create", "Cannot create thread " + itos(i) + " (numbered from 0) when calculating expected weights!"); } for (int i = 0; i < nThreads; i++) { - rc = pthread_join(threads[i], &status); + rc = pthread_join(threads[i], NULL); pthread_assert(rc, "pthread_join", "Cannot join thread " + itos(i) + " (numbered from 0) when calculating expected weights!"); } model.setNeedCalcConPrb(false); @@ -595,15 +475,18 @@ void EM() { /* destroy attribute */ pthread_attr_destroy(&attr); - //convert theta' to theta - double *mw = model.getMW(); - sum = 0.0; - for (int i = 0; i <= M; i++) { - theta[i] = (mw[i] < EPSILON ? 0.0 : theta[i] / mw[i]); - sum += theta[i]; - } - assert(sum >= EPSILON); - for (int i = 0; i <= M; i++) theta[i] /= sum; + + sprintf(thetaF, "%s.theta", statName); + fo = fopen(thetaF, "w"); + fprintf(fo, "%d\n", M + 1); + + // output theta' + for (int i = 0; i < M; i++) fprintf(fo, "%.15g ", theta[i]); + fprintf(fo, "%.15g\n", theta[M]); + + //calculate expected effective lengths for each isoform + calcExpectedEffectiveLengths(M, refs, model, eel); + polishTheta(M, theta, eel, model.getMW()); // output theta for (int i = 0; i < M; i++) fprintf(fo, "%.15g ", theta[i]); @@ -652,8 +535,8 @@ int main(int argc, char* argv[]) { ifstream fin; bool quiet = false; - if (argc < 5) { - printf("Usage : rsem-run-em refName read_type sampleName sampleToken [-p #Threads] [-b samInpType samInpF has_fn_list_? [fn_list]] [-q] [--gibbs-out] [--sampling]\n\n"); + if (argc < 6) { + printf("Usage : rsem-run-em refName read_type sampleName imdName statName [-p #Threads] [-b samInpType samInpF has_fn_list_? [fn_list]] [-q] [--gibbs-out] [--sampling]\n\n"); printf(" refName: reference name\n"); printf(" read_type: 0 single read without quality score; 1 single read with quality score; 2 paired-end read without quality score; 3 paired-end read with quality score.\n"); printf(" sampleName: sample's name, including the path\n"); @@ -672,8 +555,8 @@ int main(int argc, char* argv[]) { strcpy(refName, argv[1]); read_type = atoi(argv[2]); strcpy(outName, argv[3]); - sprintf(imdName, "%s.temp/%s", argv[3], argv[4]); - sprintf(statName, "%s.stat/%s", argv[3], argv[4]); + strcpy(imdName, argv[4]); + strcpy(statName, argv[5]); nThreads = 1; @@ -682,7 +565,7 @@ int main(int argc, char* argv[]) { genGibbsOut = false; pt_fn_list = pt_chr_list = NULL; - for (int i = 5; i < argc; i++) { + for (int i = 6; i < argc; i++) { if (!strcmp(argv[i], "-p")) { nThreads = atoi(argv[i + 1]); } if (!strcmp(argv[i], "-b")) { genBamF = true; @@ -706,9 +589,6 @@ int main(int argc, char* argv[]) { sprintf(refF, "%s.seq", refName); refs.loadRefs(refF); M = refs.getM(); - sprintf(groupF, "%s.grp", refName); - gi.load(groupF); - m = gi.getm(); sprintf(tiF, "%s.ti", refName); transcripts.readFrom(tiF);