11 #include "my_assert.h"
15 #include "SingleModel.h"
16 #include "SingleQModel.h"
17 #include "PairedEndModel.h"
18 #include "PairedEndQModel.h"
22 #include "GroupInfo.h"
23 #include "WriteResults.h"
31 double *pme_c, *pve_c; //posterior mean and variance vectors on counts
32 double *pme_tpm, *pme_fpkm;
40 Item(int sid, double conprb) {
42 this->conprb = conprb;
53 int BURNIN, NSAMPLES, GAP;
54 char refName[STRLEN], imdName[STRLEN], statName[STRLEN];
55 char thetaF[STRLEN], ofgF[STRLEN], refF[STRLEN], modelF[STRLEN];
60 vector<HIT_INT_TYPE> s;
66 vector<double> pme_c, pve_c; //global posterior mean and variance vectors on counts
67 vector<double> pme_tpm, pme_fpkm;
77 void load_data(char* refName, char* statName, char* imdName) {
83 sprintf(refF, "%s.seq", refName);
84 refs.loadRefs(refF, 1);
88 sprintf(ofgF, "%s.ofg", imdName);
90 general_assert(fin.is_open(), "Cannot open " + cstrtos(ofgF) + "!");
92 general_assert(tmpVal == M, "M in " + cstrtos(ofgF) + " is not consistent with " + cstrtos(refF) + "!");
95 s.clear(); hits.clear();
97 while (getline(fin, line)) {
98 istringstream strin(line);
102 while (strin>>sid>>conprb) {
103 hits.push_back(Item(sid, conprb));
105 s.push_back(hits.size());
112 totc = N0 + N1 + (M + 1);
114 if (verbose) { printf("Loading Data is finished!\n"); }
117 template<class ModelType>
118 void init_model_related(char* modelF) {
122 calcExpectedEffectiveLengths<ModelType>(M, refs, model, eel);
123 memcpy(mw, model.getMW(), sizeof(double) * (M + 1)); // otherwise, after exiting this procedure, mw becomes undefined
131 quotient = NSAMPLES / nThreads;
132 left = NSAMPLES % nThreads;
134 sprintf(cvsF, "%s.countvectors", imdName);
135 paramsArray = new Params[nThreads];
136 threads = new pthread_t[nThreads];
138 for (int i = 0; i < nThreads; i++) {
139 paramsArray[i].no = i;
141 paramsArray[i].nsamples = quotient;
142 if (i < left) paramsArray[i].nsamples++;
144 sprintf(outF, "%s%d", cvsF, i);
145 paramsArray[i].fo = fopen(outF, "w");
147 paramsArray[i].engine = engineFactory::new_engine();
148 paramsArray[i].pme_c = new double[M + 1];
149 memset(paramsArray[i].pme_c, 0, sizeof(double) * (M + 1));
150 paramsArray[i].pve_c = new double[M + 1];
151 memset(paramsArray[i].pve_c, 0, sizeof(double) * (M + 1));
152 paramsArray[i].pme_tpm = new double[M + 1];
153 memset(paramsArray[i].pme_tpm, 0, sizeof(double) * (M + 1));
154 paramsArray[i].pme_fpkm = new double[M + 1];
155 memset(paramsArray[i].pme_fpkm, 0, sizeof(double) * (M + 1));
158 /* set thread attribute to be joinable */
159 pthread_attr_init(&attr);
160 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
162 if (verbose) { printf("Initialization finished!\n"); }
165 //sample theta from Dir(1)
166 void sampleTheta(engine_type& engine, vector<double>& theta) {
168 gamma_generator gmg(engine, gm);
171 theta.assign(M + 1, 0);
173 for (int i = 0; i <= M; i++) {
177 assert(denom > EPSILON);
178 for (int i = 0; i <= M; i++) theta[i] /= denom;
181 void writeCountVector(FILE* fo, vector<int>& counts) {
182 for (int i = 0; i < M; i++) {
183 fprintf(fo, "%d ", counts[i]);
185 fprintf(fo, "%d\n", counts[M]);
188 void* Gibbs(void* arg) {
190 HIT_INT_TYPE len, fr, to;
191 Params *params = (Params*)arg;
193 vector<double> theta, tpm, fpkm;
194 vector<int> z, counts;
197 uniform01 rg(*params->engine);
199 // generate initial state
200 sampleTheta(*params->engine, theta);
204 counts.assign(M + 1, 1); // 1 pseudo count
207 for (READ_INT_TYPE i = 0; i < N1; i++) {
208 fr = s[i]; to = s[i + 1];
211 for (HIT_INT_TYPE j = fr; j < to; j++) {
212 arr[j - fr] = theta[hits[j].sid] * hits[j].conprb;
213 if (j > fr) arr[j - fr] += arr[j - fr - 1]; // cumulative
215 z[i] = hits[fr + sample(rg, arr, len)].sid;
220 CHAINLEN = 1 + (params->nsamples - 1) * GAP;
221 for (int ROUND = 1; ROUND <= BURNIN + CHAINLEN; ROUND++) {
223 for (READ_INT_TYPE i = 0; i < N1; i++) {
225 fr = s[i]; to = s[i + 1]; len = to - fr;
227 for (HIT_INT_TYPE j = fr; j < to; j++) {
228 arr[j - fr] = counts[hits[j].sid] * hits[j].conprb;
229 if (j > fr) arr[j - fr] += arr[j - fr - 1]; //cumulative
231 z[i] = hits[fr + sample(rg, arr, len)].sid;
235 if (ROUND > BURNIN) {
236 if ((ROUND - BURNIN - 1) % GAP == 0) {
237 writeCountVector(params->fo, counts);
238 for (int i = 0; i <= M; i++) theta[i] = counts[i] / totc;
239 polishTheta(M, theta, eel, mw);
240 calcExpressionValues(M, theta, eel, tpm, fpkm);
241 for (int i = 0; i <= M; i++) {
242 params->pme_c[i] += counts[i] - 1;
243 params->pve_c[i] += (counts[i] - 1) * (counts[i] - 1);
244 params->pme_tpm[i] += tpm[i];
245 params->pme_fpkm[i] += fpkm[i];
250 if (verbose && ROUND % 100 == 0) { printf("Thread %d, ROUND %d is finished!\n", params->no, ROUND); }
257 // char inpF[STRLEN], command[STRLEN];
260 /* destroy attribute */
261 pthread_attr_destroy(&attr);
264 pme_c.assign(M + 1, 0);
265 pve_c.assign(M + 1, 0);
266 pme_tpm.assign(M + 1, 0);
267 pme_fpkm.assign(M + 1, 0);
268 for (int i = 0; i < nThreads; i++) {
269 fclose(paramsArray[i].fo);
270 delete paramsArray[i].engine;
271 for (int j = 0; j <= M; j++) {
272 pme_c[j] += paramsArray[i].pme_c[j];
273 pve_c[j] += paramsArray[i].pve_c[j];
274 pme_tpm[j] += paramsArray[i].pme_tpm[j];
275 pme_fpkm[j] += paramsArray[i].pme_fpkm[j];
277 delete[] paramsArray[i].pme_c;
278 delete[] paramsArray[i].pve_c;
279 delete[] paramsArray[i].pme_tpm;
280 delete[] paramsArray[i].pme_fpkm;
282 delete[] paramsArray;
285 for (int i = 0; i <= M; i++) {
286 pme_c[i] /= NSAMPLES;
287 pve_c[i] = (pve_c[i] - NSAMPLES * pme_c[i] * pme_c[i]) / (NSAMPLES - 1);
288 pme_tpm[i] /= NSAMPLES;
289 pme_fpkm[i] /= NSAMPLES;
293 int main(int argc, char* argv[]) {
295 printf("Usage: rsem-run-gibbs reference_name imdName statName BURNIN NSAMPLES GAP [-p #Threads] [--var] [-q]\n");
299 strcpy(refName, argv[1]);
300 strcpy(imdName, argv[2]);
301 strcpy(statName, argv[3]);
303 BURNIN = atoi(argv[4]);
304 NSAMPLES = atoi(argv[5]);
311 for (int i = 7; i < argc; i++) {
312 if (!strcmp(argv[i], "-p")) nThreads = atoi(argv[i + 1]);
313 if (!strcmp(argv[i], "--var")) var_opt = true;
314 if (!strcmp(argv[i], "-q")) quiet = true;
318 assert(NSAMPLES > 1); // Otherwise, we cannot calculate posterior variance
320 if (nThreads > NSAMPLES) {
322 printf("Warning: Number of samples is less than number of threads! Change the number of threads to %d!\n", nThreads);
325 load_data(refName, statName, imdName);
327 sprintf(modelF, "%s.model", statName);
328 FILE *fi = fopen(modelF, "r");
329 general_assert(fi != NULL, "Cannot open " + cstrtos(modelF) + "!");
330 assert(fscanf(fi, "%d", &model_type) == 1);
333 mw = new double[M + 1]; // make an extra copy
336 case 0 : init_model_related<SingleModel>(modelF); break;
337 case 1 : init_model_related<SingleQModel>(modelF); break;
338 case 2 : init_model_related<PairedEndModel>(modelF); break;
339 case 3 : init_model_related<PairedEndQModel>(modelF); break;
342 if (verbose) printf("Gibbs started!\n");
345 for (int i = 0; i < nThreads; i++) {
346 rc = pthread_create(&threads[i], &attr, Gibbs, (void*)(¶msArray[i]));
347 pthread_assert(rc, "pthread_create", "Cannot create thread " + itos(i) + " (numbered from 0)!");
349 for (int i = 0; i < nThreads; i++) {
350 rc = pthread_join(threads[i], NULL);
351 pthread_assert(rc, "pthread_join", "Cannot join thread " + itos(i) + " (numbered from 0)!");
355 if (verbose) printf("Gibbs finished!\n");
357 writeResultsGibbs(M, refName, imdName, pme_c, pme_fpkm, pme_tpm);
366 sprintf(groupF, "%s.grp", refName);
370 sprintf(varF, "%s.var", statName);
371 FILE *fo = fopen(varF, "w");
372 general_assert(fo != NULL, "Cannot open " + cstrtos(varF) + "!");
373 for (int i = 0; i < m; i++) {
374 int b = gi.spAt(i), e = gi.spAt(i + 1), number_of_isoforms = e - b;
375 for (int j = b; j < e; j++) {
376 fprintf(fo, "%s\t%d\t%.15g\t%.15g\n", refs.getRef(j).getName().c_str(), number_of_isoforms, pme_c[j], pve_c[j]);
382 delete mw; // delete the copy