void* calcCI_batch(void* arg) { float *itsamples, *gtsamples; ifstream fin; CIParams *ciParams = (CIParams*)arg; itsamples = new float[nSamples]; gtsamples = new float[nSamples]; fin.open(tmpF, ios::binary); streampos pos = streampos(gi.spAt(ciParams->start_gene_id)) * nSamples * FLOATSIZE; fin.seekg(pos, ios::beg); int cnt = 0; for (int i = ciParams->start_gene_id; i < ciParams->end_gene_id; i++) { int b = gi.spAt(i), e = gi.spAt(i + 1); memset(gtsamples, 0, FLOATSIZE * nSamples); for (int j = b; j < e; j++) { for (int k = 0; k < nSamples; k++) { fin.read((char*)(&itsamples[k]), FLOATSIZE); gtsamples[k] += itsamples[k]; } calcCI(nSamples, itsamples, iso_tau[j].lb, iso_tau[j].ub); } calcCI(nSamples, gtsamples, gene_tau[i].lb, gene_tau[i].ub); ++cnt; if (verbose && cnt % 1000 == 0) { printf("In thread %d, %d genes are processed for CI calculation!\n", ciParams->no, cnt); } } fin.close(); delete[] itsamples; delete[] gtsamples; return NULL; }
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"); } }
void calculate_credibility_intervals(char* imdName) { FILE *fo; char outF[STRLEN]; int num_threads = nThreads; iso_tau = new CIType[M + 1]; gene_tau = new CIType[m]; assert(M > 0); int quotient = M / num_threads; if (quotient < 1) { num_threads = M; quotient = 1; } int cur_gene_id = 0; int num_isoforms = 0; // A just so so strategy for paralleling ciParamsArray = new CIParams[num_threads]; for (int i = 0; i < num_threads; i++) { ciParamsArray[i].no = i; ciParamsArray[i].start_gene_id = cur_gene_id; num_isoforms = 0; while ((m - cur_gene_id > num_threads - i - 1) && (i == num_threads - 1 || num_isoforms < quotient)) { num_isoforms += gi.spAt(cur_gene_id + 1) - gi.spAt(cur_gene_id); ++cur_gene_id; } ciParamsArray[i].end_gene_id = cur_gene_id; } threads = new pthread_t[num_threads]; /* set thread attribute to be joinable */ pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); // paralleling for (int i = 0; i < num_threads; i++) { rc = pthread_create(&threads[i], &attr, &calcCI_batch, (void*)(&ciParamsArray[i])); pthread_assert(rc, "pthread_create", "Cannot create thread " + itos(i) + " (numbered from 0) in calculate_credibility_intervals!"); } for (int i = 0; i < num_threads; i++) { rc = pthread_join(threads[i], &status); pthread_assert(rc, "pthread_join", "Cannot join thread " + itos(i) + " (numbered from 0) in calculate_credibility_intervals!"); } // releasing resources /* destroy attribute */ pthread_attr_destroy(&attr); delete[] threads; delete[] ciParamsArray; //isoform level results sprintf(outF, "%s.iso_res", imdName); fo = fopen(outF, "a"); for (int i = 1; i <= M; i++) fprintf(fo, "%.6g%c", iso_tau[i].lb, (i < M ? '\t' : '\n')); for (int i = 1; i <= M; i++) fprintf(fo, "%.6g%c", iso_tau[i].ub, (i < M ? '\t' : '\n')); fclose(fo); //gene level results sprintf(outF, "%s.gene_res", imdName); fo = fopen(outF, "a"); for (int i = 0; i < m; i++) fprintf(fo, "%.6g%c", gene_tau[i].lb, (i < m - 1 ? '\t' : '\n')); for (int i = 0; i < m; i++) fprintf(fo, "%.6g%c", gene_tau[i].ub, (i < m - 1 ? '\t' : '\n')); fclose(fo); delete[] iso_tau; delete[] gene_tau; if (verbose) { printf("All credibility intervals are calculated!\n"); } }