12 #include "my_assert.h"
16 #include "SingleModel.h"
17 #include "SingleQModel.h"
18 #include "PairedEndModel.h"
19 #include "PairedEndQModel.h"
22 #include "GroupInfo.h"
35 float lb, ub; // the interval is [lb, ub]
37 CIType() { lb = ub = 0.0; }
42 int start_gene_id, end_gene_id;
49 int nCV, nSpC, nSamples; // nCV: number of count vectors; nSpC: number of theta vectors sampled per count vector; nSamples: nCV * nSpC
53 char cvsF[STRLEN], tmpF[STRLEN], command[STRLEN];
55 CIType *iso_tau, *gene_tau;
60 char imdName[STRLEN], statName[STRLEN];
61 char modelF[STRLEN], groupF[STRLEN], refF[STRLEN];
63 vector<double> eel; //expected effective lengths
75 CIParams *ciParamsArray;
77 template<class ModelType>
78 void calcExpectedEffectiveLengths(ModelType& model) {
80 double *pdf = NULL, *cdf = NULL, *clen = NULL; // clen[i] = \sigma_{j=1}^{i}pdf[i]*(lb+i)
82 model.getGLD().copyTo(pdf, cdf, lb, ub, span);
83 clen = new double[span + 1];
85 for (int i = 1; i <= span; i++) {
86 clen[i] = clen[i - 1] + pdf[i] * (lb + i);
89 eel.assign(M + 1, 0.0);
90 for (int i = 1; i <= M; i++) {
91 int totLen = refs.getRef(i).getTotLen();
92 int fullLen = refs.getRef(i).getFullLen();
93 int pos1 = max(min(totLen - fullLen + 1, ub) - lb, 0);
94 int pos2 = max(min(totLen, ub) - lb, 0);
96 if (pos2 == 0) { eel[i] = 0.0; continue; }
98 eel[i] = fullLen * cdf[pos1] + ((cdf[pos2] - cdf[pos1]) * (totLen + 1) - (clen[pos2] - clen[pos1]));
100 if (eel[i] < MINEEL) { eel[i] = 0.0; }
108 void* sample_theta_from_c(void* arg) {
113 gamma_generator **rgs;
115 Params *params = (Params*)arg;
116 FILE *fi = params->fi;
117 double *mw = params->mw;
119 cvec = new int[cvlen];
120 theta = new double[cvlen];
121 gammas = new gamma_dist*[cvlen];
122 rgs = new gamma_generator*[cvlen];
124 float **vecs = new float*[nSpC];
125 for (int i = 0; i < nSpC; i++) vecs[i] = new float[cvlen];
128 while (fscanf(fi, "%d", &cvec[0]) == 1) {
129 for (int j = 1; j < cvlen; j++) assert(fscanf(fi, "%d", &cvec[j]) == 1);
133 for (int j = 0; j < cvlen; j++) {
134 gammas[j] = new gamma_dist(cvec[j]);
135 rgs[j] = new gamma_generator(*(params->engine), *gammas[j]);
138 for (int i = 0; i < nSpC; i++) {
140 for (int j = 0; j < cvlen; j++) {
141 theta[j] = ((j == 0 || eel[j] >= EPSILON) ? (*rgs[j])() : 0.0);
144 assert(sum >= EPSILON);
145 for (int j = 0; j < cvlen; j++) theta[j] /= sum;
148 for (int j = 0; j < cvlen; j++) {
149 theta[j] = (mw[j] < EPSILON ? 0.0 : theta[j] / mw[j]);
152 assert(sum >= EPSILON);
153 for (int j = 0; j < cvlen; j++) theta[j] /= sum;
157 vecs[i][0] = theta[0];
158 for (int j = 1; j < cvlen; j++)
159 if (eel[j] >= EPSILON) {
160 vecs[i][j] = theta[j] / eel[j];
163 else assert(theta[j] < EPSILON);
165 assert(sum >= EPSILON);
166 for (int j = 1; j < cvlen; j++) vecs[i][j] /= sum;
169 buffer->write(nSpC, vecs);
171 for (int j = 0; j < cvlen; j++) {
176 if (verbose && cnt % 100 == 0) { printf("Thread %d, %d count vectors are processed!\n", params->no, cnt); }
184 for (int i = 0; i < nSpC; i++) delete[] vecs[i];
190 template<class ModelType>
191 void sample_theta_vectors_from_count_vectors() {
194 calcExpectedEffectiveLengths<ModelType>(model);
197 int num_threads = min(nThreads, nCV);
199 buffer = new Buffer(nMB, nSamples, cvlen, tmpF);
201 paramsArray = new Params[num_threads];
202 threads = new pthread_t[num_threads];
205 for (int i = 0; i < num_threads; i++) {
206 paramsArray[i].no = i;
207 sprintf(inpF, "%s%d", cvsF, i);
208 paramsArray[i].fi = fopen(inpF, "r");
209 paramsArray[i].engine = engineFactory::new_engine();
210 paramsArray[i].mw = model.getMW();
213 /* set thread attribute to be joinable */
214 pthread_attr_init(&attr);
215 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
217 for (int i = 0; i < num_threads; i++) {
218 rc = pthread_create(&threads[i], &attr, &sample_theta_from_c, (void*)(¶msArray[i]));
219 pthread_assert(rc, "pthread_create", "Cannot create thread " + itos(i) + " (numbered from 0) in sample_theta_vectors_from_count_vectors!");
221 for (int i = 0; i < num_threads; i++) {
222 rc = pthread_join(threads[i], &status);
223 pthread_assert(rc, "pthread_join", "Cannot join thread " + itos(i) + " (numbered from 0) in sample_theta_vectors_from_count_vectors!");
226 /* destroy attribute */
227 pthread_attr_destroy(&attr);
230 for (int i = 0; i < num_threads; i++) {
231 fclose(paramsArray[i].fi);
232 delete paramsArray[i].engine;
234 delete[] paramsArray;
236 delete buffer; // Must delete here, force the content left in the buffer be written into the disk
238 if (verbose) { printf("Sampling is finished!\n"); }
241 void calcCI(int nSamples, float *samples, float &lb, float &ub) {
242 int p, q; // p pointer for lb, q pointer for ub;
244 int threshold = nSamples - (int(confidence * nSamples - 1e-8) + 1);
247 sort(samples, samples + nSamples);
249 p = 0; q = nSamples - 1;
253 while (newq > 0 && samples[newq - 1] == samples[newq]) newq--;
255 } while (newq >= 0 && nSamples - (newq + 1) <= threshold);
257 nOutside = nSamples - (q + 1);
259 lb = -1e30; ub = 1e30;
261 if (samples[q] - samples[p] < ub - lb) {
267 while (newp < nSamples - 1 && samples[newp] == samples[newp + 1]) newp++;
269 if (newp <= threshold) {
270 nOutside += newp - p;
272 while (nOutside > threshold && q < nSamples - 1) {
274 while (newq < nSamples - 1 && samples[newq] == samples[newq + 1]) newq++;
275 nOutside -= newq - q;
278 assert(nOutside <= threshold);
281 } while (p <= threshold);
284 void* calcCI_batch(void* arg) {
285 float *itsamples, *gtsamples;
287 CIParams *ciParams = (CIParams*)arg;
289 itsamples = new float[nSamples];
290 gtsamples = new float[nSamples];
292 fin.open(tmpF, ios::binary);
293 streampos pos = streampos(gi.spAt(ciParams->start_gene_id)) * nSamples * FLOATSIZE;
294 fin.seekg(pos, ios::beg);
297 for (int i = ciParams->start_gene_id; i < ciParams->end_gene_id; i++) {
298 int b = gi.spAt(i), e = gi.spAt(i + 1);
299 memset(gtsamples, 0, FLOATSIZE * nSamples);
300 for (int j = b; j < e; j++) {
301 for (int k = 0; k < nSamples; k++) {
302 fin.read((char*)(&itsamples[k]), FLOATSIZE);
303 gtsamples[k] += itsamples[k];
305 calcCI(nSamples, itsamples, iso_tau[j].lb, iso_tau[j].ub);
307 calcCI(nSamples, gtsamples, gene_tau[i].lb, gene_tau[i].ub);
310 if (verbose && cnt % 1000 == 0) { printf("In thread %d, %d genes are processed for CI calculation!\n", ciParams->no, cnt); }
321 void calculate_credibility_intervals(char* imdName) {
324 int num_threads = nThreads;
326 iso_tau = new CIType[M + 1];
327 gene_tau = new CIType[m];
330 int quotient = M / num_threads;
331 if (quotient < 1) { num_threads = M; quotient = 1; }
333 int num_isoforms = 0;
335 // A just so so strategy for paralleling
336 ciParamsArray = new CIParams[num_threads];
337 for (int i = 0; i < num_threads; i++) {
338 ciParamsArray[i].no = i;
339 ciParamsArray[i].start_gene_id = cur_gene_id;
342 while ((m - cur_gene_id > num_threads - i - 1) && (i == num_threads - 1 || num_isoforms < quotient)) {
343 num_isoforms += gi.spAt(cur_gene_id + 1) - gi.spAt(cur_gene_id);
347 ciParamsArray[i].end_gene_id = cur_gene_id;
350 threads = new pthread_t[num_threads];
352 /* set thread attribute to be joinable */
353 pthread_attr_init(&attr);
354 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
357 for (int i = 0; i < num_threads; i++) {
358 rc = pthread_create(&threads[i], &attr, &calcCI_batch, (void*)(&ciParamsArray[i]));
359 pthread_assert(rc, "pthread_create", "Cannot create thread " + itos(i) + " (numbered from 0) in calculate_credibility_intervals!");
361 for (int i = 0; i < num_threads; i++) {
362 rc = pthread_join(threads[i], &status);
363 pthread_assert(rc, "pthread_join", "Cannot join thread " + itos(i) + " (numbered from 0) in calculate_credibility_intervals!");
366 // releasing resources
368 /* destroy attribute */
369 pthread_attr_destroy(&attr);
372 delete[] ciParamsArray;
374 //isoform level results
375 sprintf(outF, "%s.iso_res", imdName);
376 fo = fopen(outF, "a");
377 for (int i = 1; i <= M; i++)
378 fprintf(fo, "%.6g%c", iso_tau[i].lb, (i < M ? '\t' : '\n'));
379 for (int i = 1; i <= M; i++)
380 fprintf(fo, "%.6g%c", iso_tau[i].ub, (i < M ? '\t' : '\n'));
384 sprintf(outF, "%s.gene_res", imdName);
385 fo = fopen(outF, "a");
386 for (int i = 0; i < m; i++)
387 fprintf(fo, "%.6g%c", gene_tau[i].lb, (i < m - 1 ? '\t' : '\n'));
388 for (int i = 0; i < m; i++)
389 fprintf(fo, "%.6g%c", gene_tau[i].ub, (i < m - 1 ? '\t' : '\n'));
395 if (verbose) { printf("All credibility intervals are calculated!\n"); }
398 int main(int argc, char* argv[]) {
400 printf("Usage: rsem-calculate-credibility-intervals reference_name imdName statName confidence nCV nSpC nMB [-p #Threads] [-q]\n");
404 strcpy(imdName, argv[2]);
405 strcpy(statName, argv[3]);
407 confidence = atof(argv[4]);
409 nSpC = atoi(argv[6]);
414 for (int i = 8; i < argc; i++) {
415 if (!strcmp(argv[i], "-p")) nThreads = atoi(argv[i + 1]);
416 if (!strcmp(argv[i], "-q")) quiet = true;
420 sprintf(refF, "%s.seq", argv[1]);
421 refs.loadRefs(refF, 1);
423 sprintf(groupF, "%s.grp", argv[1]);
427 nSamples = nCV * nSpC;
429 assert(nSamples > 0 && cvlen > 1); // for Buffter.h: (bufsize_type)nSamples
431 sprintf(tmpF, "%s.tmp", imdName);
432 sprintf(cvsF, "%s.countvectors", imdName);
434 sprintf(modelF, "%s.model", statName);
435 FILE *fi = fopen(modelF, "r");
436 general_assert(fi != NULL, "Cannot open " + cstrtos(modelF) + "!");
437 assert(fscanf(fi, "%d", &model_type) == 1);
442 case 0 : sample_theta_vectors_from_count_vectors<SingleModel>(); break;
443 case 1 : sample_theta_vectors_from_count_vectors<SingleQModel>(); break;
444 case 2 : sample_theta_vectors_from_count_vectors<PairedEndModel>(); break;
445 case 3 : sample_theta_vectors_from_count_vectors<PairedEndQModel>(); break;
449 calculate_credibility_intervals(imdName);
452 sprintf(command, "rm -f %s", tmpF);
453 int status = system(command);
455 fprintf(stderr, "Cannot delete %s!\n", tmpF);