X-Git-Url: https://git.donarmstrong.com/?p=rsem.git;a=blobdiff_plain;f=EM.cpp;h=dfdd0e4256f43debf0243bb61301ded8acf2749e;hp=318b7eba94cfd9a31cb3009f2b6cf0d627b59df1;hb=237bbdf363c9e42ee24e2fd63106dccf20d9bf2f;hpb=a42d21510e502fe5cdd21a5d7ad023b33d2e6b99 diff --git a/EM.cpp b/EM.cpp index 318b7eb..dfdd0e4 100644 --- a/EM.cpp +++ b/EM.cpp @@ -10,6 +10,8 @@ #include #include "utils.h" +#include "my_assert.h" +#include "sampling.h" #include "Read.h" #include "SingleRead.h" @@ -58,10 +60,12 @@ int nThreads; bool genBamF; // If user wants to generate bam file, true; otherwise, false. +bool bamSampling; // true if sampling from read posterior distribution when bam file is generated bool updateModel, calcExpectedWeights; bool genGibbsOut; // generate file for Gibbs sampler -char refName[STRLEN], imdName[STRLEN], outName[STRLEN]; +char refName[STRLEN], outName[STRLEN]; +char imdName[STRLEN], statName[STRLEN]; char refF[STRLEN], groupF[STRLEN], cntF[STRLEN], tiF[STRLEN]; char mparamsF[STRLEN], bmparamsF[STRLEN]; char modelF[STRLEN], thetaF[STRLEN]; @@ -99,7 +103,7 @@ void init(ReadReader **&readers, HitContainer **&hitvs, doubl indices[i] = new ReadIndex(readFs[i]); } for (int i = 0; i < nThreads; i++) { - readers[i] = new ReadReader(s, readFs); + readers[i] = new ReadReader(s, readFs, refs.hasPolyA(), mparams.seedLen); // allow calculation of calc_lq() function readers[i]->setIndices(indices); } @@ -110,12 +114,11 @@ void init(ReadReader **&readers, HitContainer **&hitvs, doubl sprintf(datF, "%s.dat", imdName); fin.open(datF); - if (!fin.is_open()) { fprintf(stderr, "Cannot open %s! It may not exist.\n", datF); exit(-1); } + general_assert(fin.is_open(), "Cannot open " + cstrtos(datF) + "! It may not exist."); fin>>nReads>>nHits>>rt; - if (nReads != N1) { fprintf(stderr, "Number of alignable reads does not match!\n"); exit(-1); } - //assert(nReads == N1); - if (rt != read_type) { fprintf(stderr, "Data file (.dat) does not have the right read type!\n"); exit(-1); } - //assert(rt == read_type); + general_assert(nReads == N1, "Number of alignable reads does not match!"); + general_assert(rt == read_type, "Data file (.dat) does not have the right read type!"); + //A just so so strategy for paralleling nhT = nHits / nThreads; @@ -125,12 +128,12 @@ void init(ReadReader **&readers, HitContainer **&hitvs, doubl ncpvs = new double*[nThreads]; for (int i = 0; i < nThreads; i++) { int ntLeft = nThreads - i - 1; // # of threads left - if (!readers[i]->locate(curnr)) { fprintf(stderr, "Read indices files do not match!\n"); exit(-1); } - //assert(readers[i]->locate(curnr)); - while (nrLeft > ntLeft && hitvs[i]->getNHits() < nhT) { - if (!hitvs[i]->read(fin)) { fprintf(stderr, "Cannot read alignments from .dat file!\n"); exit(-1); } - //assert(hitvs[i]->read(fin)); + general_assert(readers[i]->locate(curnr), "Read indices files do not match!"); + + while (nrLeft > ntLeft && (i == nThreads - 1 || hitvs[i]->getNHits() < nhT)) { + general_assert(hitvs[i]->read(fin), "Cannot read alignments from .dat file!"); + --nrLeft; if (verbose && nrLeft % 1000000 == 0) { printf("DAT %d reads left!\n", nrLeft); } } @@ -183,12 +186,9 @@ void* E_STEP(void* arg) { memset(countv, 0, sizeof(double) * (M + 1)); for (int i = 0; i < N; i++) { if (needCalcConPrb || updateModel) { - if (!reader->next(read)) { - fprintf(stderr, "Can not load a read!\n"); - exit(-1); - } - //assert(reader->next(read)); + general_assert(reader->next(read), "Can not load a read!"); } + fr = hitv->getSAt(i); to = hitv->getSAt(i + 1); fracs.resize(to - fr + 1); @@ -250,10 +250,8 @@ void* calcConProbs(void* arg) { reader->reset(); for (int i = 0; i < N; i++) { - if (!reader->next(read)) { - fprintf(stderr, "Can not load a read!\n"); - exit(-1); - } + general_assert(reader->next(read), "Can not load a read!"); + fr = hitv->getSAt(i); to = hitv->getSAt(i + 1); @@ -305,7 +303,7 @@ void writeResults(ModelType& model, double* counts) { char outF[STRLEN]; FILE *fo; - sprintf(modelF, "%s.model", outName); + sprintf(modelF, "%s.model", statName); model.write(modelF); //calculate tau values @@ -318,8 +316,9 @@ void writeResults(ModelType& model, double* counts) { tau[i] = theta[i] / eel[i]; denom += tau[i]; } - if (denom <= 0) { fprintf(stderr, "No alignable reads?!\n"); exit(-1); } - //assert(denom > 0); + + general_assert(denom > 0, "No alignable reads?!"); + for (int i = 1; i <= M; i++) { tau[i] /= denom; } @@ -337,7 +336,7 @@ void writeResults(ModelType& model, double* counts) { 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.getLeft().c_str(), (i < M ? '\t' : '\n')); + fprintf(fo, "%s%c", transcript.getGeneID().c_str(), (i < M ? '\t' : '\n')); } fclose(fo); @@ -394,8 +393,8 @@ void release(ReadReader **readers, HitContainer **hitvs, doub } inline bool doesUpdateModel(int ROUND) { - //return false; // never update, for debugging only - return ROUND <= 20 || ROUND % 100 == 0; + // return ROUND <= 20 || ROUND % 100 == 0; + return ROUND <= 10; } //Including initialize, algorithm and results saving @@ -462,14 +461,12 @@ void EM() { //E step for (int i = 0; i < nThreads; i++) { rc = pthread_create(&threads[i], &attr, E_STEP, (void*)(&fparams[i])); - if (rc != 0) { fprintf(stderr, "Cannot create thread %d at ROUND %d! (numbered from 0)\n", i, ROUND); exit(-1); } - //assert(rc == 0); + pthread_assert(rc, "pthread_create", "Cannot create thread " + itos(i) + " (numbered from 0) at ROUND " + itos(ROUND) + "!"); } for (int i = 0; i < nThreads; i++) { rc = pthread_join(threads[i], &status); - if (rc != 0) { fprintf(stderr, "Cannot join thread %d at ROUND %d! (numbered from 0)\n", i, ROUND); exit(-1); } - //assert(rc == 0); + pthread_assert(rc, "pthread_join", "Cannot join thread " + itos(i) + " (numbered from 0) at ROUND " + itos(ROUND) + "!"); } model.setNeedCalcConPrb(false); @@ -505,7 +502,7 @@ void EM() { } if (verbose) printf("ROUND = %d, SUM = %.15g, bChange = %f, totNum = %d\n", ROUND, sum, bChange, totNum); - } while (ROUND < MIN_ROUND || totNum > 0 && ROUND < MAX_ROUND); + } while (ROUND < MIN_ROUND || (totNum > 0 && ROUND < MAX_ROUND)); //while (ROUND < MAX_ROUND); if (totNum > 0) fprintf(stderr, "Warning: RSEM reaches %d iterations before meeting the convergence criteria.\n", MAX_ROUND); @@ -515,11 +512,11 @@ void EM() { if (model.getNeedCalcConPrb()) { for (int i = 0; i < nThreads; i++) { rc = pthread_create(&threads[i], &attr, calcConProbs, (void*)(&fparams[i])); - if (rc != 0) { fprintf(stderr, "Cannot create thread %d when generate files for Gibbs sampler! (numbered from 0)\n", i); exit(-1); } + 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); - if (rc != 0) { fprintf(stderr, "Cannot join thread %d when generate files for Gibbs sampler! (numbered from 0)\n", i); exit(-1); } + pthread_assert(rc, "pthread_join", "Cannot join thread " + itos(i) + " (numbered from 0) when generating files for Gibbs sampler!"); } } model.setNeedCalcConPrb(false); @@ -547,15 +544,9 @@ void EM() { } } fclose(fo); - - char scoreF[STRLEN]; - sprintf(scoreF, "%s.ns", imdName); - fo = fopen(scoreF, "w"); - fprintf(fo, "%.15g\n", model.getLogP()); - fclose(fo); } - sprintf(thetaF, "%s.theta", outName); + sprintf(thetaF, "%s.theta", statName); fo = fopen(thetaF, "w"); fprintf(fo, "%d\n", M + 1); @@ -571,20 +562,21 @@ void EM() { for (int i = 1; i <= M; i++) if (eel[i] < EPSILON) { theta[i] = 0.0; } else sum += theta[i]; - if (sum < EPSILON) { fprintf(stderr, "No Expected Effective Length is no less than %.6g?!\n", MINEEL); exit(-1); } + + 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 updateModel = false; calcExpectedWeights = true; + for (int i = 0; i <= M; i++) probv[i] = theta[i]; for (int i = 0; i < nThreads; i++) { rc = pthread_create(&threads[i], &attr, E_STEP, (void*)(&fparams[i])); - if (rc != 0) { fprintf(stderr, "Cannot create thread %d when calculate expected weights! (numbered from 0)\n", i); exit(-1); } - //assert(rc == 0); + 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); - if (rc != 0) { fprintf(stderr, "Cannot join thread %d! (numbered from 0) when calculate expected weights!\n", i); exit(-1); } - //assert(rc == 0); + pthread_assert(rc, "pthread_join", "Cannot join thread " + itos(i) + " (numbered from 0) when calculating expected weights!"); } model.setNeedCalcConPrb(false); for (int i = 1; i < nThreads; i++) { @@ -598,7 +590,7 @@ void EM() { pthread_attr_destroy(&attr); //convert theta' to theta - double *mw = model.getMW(); + 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]); @@ -616,15 +608,35 @@ void EM() { writeResults(model, countvs[0]); if (genBamF) { - sprintf(outBamF, "%s.bam", outName); - if (transcripts.getType() == 0) { - sprintf(chr_list, "%s.chrlist", refName); - pt_chr_list = (char*)(&chr_list); + sprintf(outBamF, "%s.transcript.bam", outName); + + if (bamSampling) { + int local_N; + int fr, to, len, id; + vector arr; + uniform01 rg(engine_type(time(NULL))); + + if (verbose) printf("Begin to sample reads from their posteriors.\n"); + for (int i = 0; i < nThreads; i++) { + local_N = hitvs[i]->getN(); + for (int j = 0; j < local_N; j++) { + fr = hitvs[i]->getSAt(j); + to = hitvs[i]->getSAt(j + 1); + len = to - fr + 1; + arr.assign(len, 0); + arr[0] = ncpvs[i][j]; + for (int k = fr; k < to; k++) arr[k - fr + 1] = arr[k - fr] + hitvs[i]->getHitAt(k).getConPrb(); + id = (arr[len - 1] < EPSILON ? -1 : sample(rg, arr, len)); // if all entries in arr are 0, let id be -1 + for (int k = fr; k < to; k++) hitvs[i]->getHitAt(k).setConPrb(k - fr + 1 == id ? 1.0 : 0.0); + } + } + + if (verbose) printf("Sampling is finished.\n"); } - BamWriter writer(inpSamType, inpSamF, pt_fn_list, outBamF, pt_chr_list); + BamWriter writer(inpSamType, inpSamF, pt_fn_list, outBamF, transcripts); HitWrapper wrapper(nThreads, hitvs); - writer.work(wrapper, transcripts); + writer.work(wrapper); } release(readers, hitvs, ncpvs, mhps); @@ -635,15 +647,16 @@ int main(int argc, char* argv[]) { bool quiet = false; if (argc < 5) { - printf("Usage : rsem-run-em refName read_type imdName outName [-p #Threads] [-b samInpType samInpF has_fn_list_? [fn_list]] [-q] [--gibbs-out]\n\n"); + 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"); 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(" imdName: name for all upstream/downstream user-unseen files. (different files have different suffices)\n"); - printf(" outName: name for all output files. (different files have different suffices)\n"); + printf(" sampleName: sample's name, including the path\n"); + printf(" sampleToken: sampleName excludes the path\n"); printf(" -p: number of threads which user wants to use. (default: 1)\n"); printf(" -b: produce bam format output file. (default: off)\n"); printf(" -q: set it quiet\n"); - printf(" --gibbs-out: generate output file use by Gibbs sampler. (default: off)\n"); + printf(" --gibbs-out: generate output file used by Gibbs sampler. (default: off)\n"); + printf(" --sampling: sample each read from its posterior distribution when bam file is generated. (default: off)\n"); printf("// model parameters should be in imdName.mparams.\n"); exit(-1); } @@ -652,12 +665,14 @@ int main(int argc, char* argv[]) { strcpy(refName, argv[1]); read_type = atoi(argv[2]); - strcpy(imdName, argv[3]); - strcpy(outName, argv[4]); + strcpy(outName, argv[3]); + sprintf(imdName, "%s.temp/%s", argv[3], argv[4]); + sprintf(statName, "%s.stat/%s", argv[3], argv[4]); nThreads = 1; genBamF = false; + bamSampling = false; genGibbsOut = false; pt_fn_list = pt_chr_list = NULL; @@ -674,9 +689,10 @@ int main(int argc, char* argv[]) { } if (!strcmp(argv[i], "-q")) { quiet = true; } if (!strcmp(argv[i], "--gibbs-out")) { genGibbsOut = true; } + if (!strcmp(argv[i], "--sampling")) { bamSampling = true; } } - if (nThreads <= 0) { fprintf(stderr, "Number of threads should be bigger than 0!\n"); exit(-1); } - //assert(nThreads > 0); + + general_assert(nThreads > 0, "Number of threads should be bigger than 0!"); verbose = !quiet; @@ -691,13 +707,15 @@ int main(int argc, char* argv[]) { sprintf(tiF, "%s.ti", refName); transcripts.readFrom(tiF); - sprintf(cntF, "%s.cnt", imdName); + sprintf(cntF, "%s.cnt", statName); fin.open(cntF); - if (!fin.is_open()) { fprintf(stderr, "Cannot open %s! It may not exist.\n", cntF); exit(-1); } + + general_assert(fin.is_open(), "Cannot open " + cstrtos(cntF) + "! It may not exist."); + fin>>N0>>N1>>N2>>N_tot; fin.close(); - if (N1 <= 0) { fprintf(stderr, "There are no alignable reads!\n"); exit(-1); } + general_assert(N1 > 0, "There are no alignable reads!"); if (nThreads > N1) nThreads = N1; @@ -708,7 +726,9 @@ int main(int argc, char* argv[]) { sprintf(mparamsF, "%s.mparams", imdName); fin.open(mparamsF); - if (!fin.is_open()) { fprintf(stderr, "Cannot open %s! It may not exist.\n", mparamsF); exit(-1); } + + general_assert(fin.is_open(), "Cannot open " + cstrtos(mparamsF) + "It may not exist."); + fin>> mparams.minL>> mparams.maxL>> mparams.probF; int val; // 0 or 1 , for estRSPD fin>>val; @@ -728,7 +748,7 @@ int main(int argc, char* argv[]) { time_t b = time(NULL); - printTimeUsed(a, b); + printTimeUsed(a, b, "EM.cpp"); return 0; }