forked from deweylab/RSEM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gibbs.cpp
530 lines (433 loc) · 13.3 KB
/
Gibbs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cassert>
#include<fstream>
#include<sstream>
#include<vector>
#include<pthread.h>
#include "utils.h"
#include "my_assert.h"
#include "sampling.h"
#include "Model.h"
#include "SingleModel.h"
#include "SingleQModel.h"
#include "PairedEndModel.h"
#include "PairedEndQModel.h"
#include "Refs.h"
#include "GroupInfo.h"
#include "WriteResults.h"
using namespace std;
bool verbose = true;
struct Params {
int no, nsamples;
FILE *fo;
engine_type *engine;
double *pme_c, *pve_c; //posterior mean and variance vectors on counts
double *pme_tpm, *pme_fpkm;
double *pve_c_genes, *pve_c_trans;
};
struct Item {
int sid;
double conprb;
Item(int sid, double conprb) {
this->sid = sid;
this->conprb = conprb;
}
};
int nThreads;
int model_type;
int M;
READ_INT_TYPE N0, N1;
HIT_INT_TYPE nHits;
double totc;
int BURNIN, NSAMPLES, GAP;
char refName[STRLEN], imdName[STRLEN], statName[STRLEN];
char thetaF[STRLEN], ofgF[STRLEN], refF[STRLEN], modelF[STRLEN];
char cvsF[STRLEN];
Refs refs;
vector<HIT_INT_TYPE> s;
vector<Item> hits;
vector<double> eel;
double *mw;
vector<int> init_counts;
double pseudoC;
vector<double> pme_c, pve_c; //global posterior mean and variance vectors on counts
vector<double> pme_tpm, pme_fpkm;
bool quiet;
Params *paramsArray;
pthread_t *threads;
pthread_attr_t attr;
int rc;
bool hasSeed;
seedType seed;
int m;
char groupF[STRLEN];
GroupInfo gi;
bool alleleS;
int m_trans;
GroupInfo gt, ta;
vector<double> pve_c_genes, pve_c_trans;
// pliu
// if has prior file; file's name; and a vector to save prior parameters
bool has_prior;
char fprior[STRLEN];
vector<double> pseudo_counts;
//////
void load_data(char* refName, char* statName, char* imdName) {
ifstream fin;
string line;
int tmpVal;
//load reference file
sprintf(refF, "%s.seq", refName);
refs.loadRefs(refF, 1);
M = refs.getM();
//load ofgF;
sprintf(ofgF, "%s.ofg", imdName);
fin.open(ofgF);
general_assert(fin.is_open(), "Cannot open " + cstrtos(ofgF) + "!");
fin>>tmpVal>>N0;
general_assert(tmpVal == M, "M in " + cstrtos(ofgF) + " is not consistent with " + cstrtos(refF) + "!");
getline(fin, line);
s.clear(); hits.clear();
s.push_back(0);
while (getline(fin, line)) {
istringstream strin(line);
int sid;
double conprb;
while (strin>>sid>>conprb) {
hits.push_back(Item(sid, conprb));
}
s.push_back(hits.size());
}
fin.close();
N1 = s.size() - 1;
nHits = hits.size();
if (verbose) { printf("Loading data is finished!\n"); }
}
void load_group_info(char* refName) {
// Load group info
sprintf(groupF, "%s.grp", refName);
gi.load(groupF);
m = gi.getm();
alleleS = isAlleleSpecific(refName, >, &ta); // if allele-specific
m_trans = (alleleS ? ta.getm() : 0);
if (verbose) { printf("Loading group information is finished!\n"); }
}
// Load imdName.omit and initialize the init count vector.
void load_omit_info(const char* imdName) {
char omitF[STRLEN];
FILE *fi = NULL;
int tid;
sprintf(omitF, "%s.omit", imdName);
fi = fopen(omitF, "r");
init_counts.assign(M + 1, 0);
totc = M + 1;
while (fscanf(fi, "%d", &tid) == 1) {
init_counts[tid] = -1;
--totc;
}
fclose(fi);
totc = totc * pseudoC + N0 + N1;
}
// pliu
// load isoform's prior information and re-calculate totc
void load_prior_info(const char* fprior){
pseudo_counts.assign(M+1, 0.0);
ifstream fin;
string line;
fin.open(fprior);
for(int i=1; i<=M; ++i){
double prior;
getline(fin, line);
sscanf(line.c_str(), "%lf%*s", &prior);
if ( init_counts[i] == 0 ){ // not to-be-omitted
pseudo_counts[i] = prior;
}
}
fin.close();
// re-calculate 'totc' by considering prior parameters
totc = 1;
for ( int i=1; i<=M; ++i ) {
if ( init_counts[i] == 0 ) { // not to-be-omitted
totc += pseudo_counts[i];
}
}
totc += N0 + N1;
}
//////
template<class ModelType>
void init_model_related(char* modelF) {
ModelType model;
model.read(modelF);
calcExpectedEffectiveLengths<ModelType>(M, refs, model, eel);
memcpy(mw, model.getMW(), sizeof(double) * (M + 1)); // otherwise, after exiting this procedure, mw becomes undefined
}
// assign threads
void init() {
int quotient, left;
char outF[STRLEN];
quotient = NSAMPLES / nThreads;
left = NSAMPLES % nThreads;
sprintf(cvsF, "%s.countvectors", imdName);
paramsArray = new Params[nThreads];
threads = new pthread_t[nThreads];
hasSeed ? engineFactory::init(seed) : engineFactory::init();
for (int i = 0; i < nThreads; i++) {
paramsArray[i].no = i;
paramsArray[i].nsamples = quotient;
if (i < left) paramsArray[i].nsamples++;
sprintf(outF, "%s%d", cvsF, i);
paramsArray[i].fo = fopen(outF, "w");
paramsArray[i].engine = engineFactory::new_engine();
paramsArray[i].pme_c = new double[M + 1];
memset(paramsArray[i].pme_c, 0, sizeof(double) * (M + 1));
paramsArray[i].pve_c = new double[M + 1];
memset(paramsArray[i].pve_c, 0, sizeof(double) * (M + 1));
paramsArray[i].pme_tpm = new double[M + 1];
memset(paramsArray[i].pme_tpm, 0, sizeof(double) * (M + 1));
paramsArray[i].pme_fpkm = new double[M + 1];
memset(paramsArray[i].pme_fpkm, 0, sizeof(double) * (M + 1));
paramsArray[i].pve_c_genes = new double[m];
memset(paramsArray[i].pve_c_genes, 0, sizeof(double) * m);
paramsArray[i].pve_c_trans = NULL;
if (alleleS) {
paramsArray[i].pve_c_trans = new double[m_trans];
memset(paramsArray[i].pve_c_trans, 0, sizeof(double) * m_trans);
}
}
engineFactory::finish();
/* set thread attribute to be joinable */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
if (verbose) { printf("Initialization finished!\n"); }
}
void writeCountVector(FILE* fo, vector<int>& counts) {
for (int i = 0; i < M; i++) {
fprintf(fo, "%d ", counts[i]);
}
fprintf(fo, "%d\n", counts[M]);
}
void* Gibbs(void* arg) {
int CHAINLEN;
HIT_INT_TYPE len, fr, to;
Params *params = (Params*)arg;
vector<double> theta, tpm, fpkm;
vector<int> z, counts(init_counts);
vector<double> arr;
uniform_01_generator rg(*params->engine, uniform_01_dist());
// generate initial state
theta.assign(M + 1, 0.0);
z.assign(N1, 0);
counts[0] += N0;
for (READ_INT_TYPE i = 0; i < N1; i++) {
fr = s[i]; to = s[i + 1];
len = to - fr;
arr.assign(len, 0);
for (HIT_INT_TYPE j = fr; j < to; j++) {
arr[j - fr] = hits[j].conprb;
if (j > fr) arr[j - fr] += arr[j - fr - 1]; // cumulative
}
z[i] = hits[fr + sample(rg, arr, len)].sid;
++counts[z[i]];
}
// Gibbs sampling
CHAINLEN = 1 + (params->nsamples - 1) * GAP;
for (int ROUND = 1; ROUND <= BURNIN + CHAINLEN; ROUND++) {
for (READ_INT_TYPE i = 0; i < N1; i++) {
--counts[z[i]];
fr = s[i]; to = s[i + 1]; len = to - fr;
arr.assign(len, 0);
for (HIT_INT_TYPE j = fr; j < to; j++) {
if ( has_prior ) {
arr[j - fr] = (counts[hits[j].sid] + pseudo_counts[hits[j].sid]) * hits[j].conprb;
} else {
arr[j - fr] = (counts[hits[j].sid] + pseudoC) * hits[j].conprb;
}
if (j > fr) arr[j - fr] += arr[j - fr - 1]; //cumulative
}
z[i] = hits[fr + sample(rg, arr, len)].sid;
++counts[z[i]];
}
if (ROUND > BURNIN) {
if ((ROUND - BURNIN - 1) % GAP == 0) {
writeCountVector(params->fo, counts);
for (int i = 0; i <= M; i++) {
if ( has_prior ) {
theta[i] = (counts[i] < 0 ? 0.0 : (counts[i] + pseudo_counts[i]) / totc);
} else {
theta[i] = (counts[i] < 0 ? 0.0 : (counts[i] + pseudoC) / totc);
}
}
polishTheta(M, theta, eel, mw);
calcExpressionValues(M, theta, eel, tpm, fpkm);
for (int i = 0; i <= M; i++) {
params->pme_c[i] += counts[i];
params->pve_c[i] += double(counts[i]) * counts[i];
params->pme_tpm[i] += tpm[i];
params->pme_fpkm[i] += fpkm[i];
}
for (int i = 0; i < m; i++) {
int b = gi.spAt(i), e = gi.spAt(i + 1);
double count = 0.0;
for (int j = b; j < e; j++) count += counts[j];
params->pve_c_genes[i] += count * count;
}
if (alleleS)
for (int i = 0; i < m_trans; i++) {
int b = ta.spAt(i), e = ta.spAt(i + 1);
double count = 0.0;
for (int j = b; j < e; j++) count += counts[j];
params->pve_c_trans[i] += count * count;
}
}
}
if (verbose && ROUND % 100 == 0) { printf("Thread %d, ROUND %d is finished!\n", params->no, ROUND); }
}
return NULL;
}
void release() {
// char inpF[STRLEN], command[STRLEN];
string line;
/* destroy attribute */
pthread_attr_destroy(&attr);
delete[] threads;
pme_c.assign(M + 1, 0);
pve_c.assign(M + 1, 0);
pme_tpm.assign(M + 1, 0);
pme_fpkm.assign(M + 1, 0);
pve_c_genes.assign(m, 0);
pve_c_trans.clear();
if (alleleS) pve_c_trans.assign(m_trans, 0);
for (int i = 0; i < nThreads; i++) {
fclose(paramsArray[i].fo);
delete paramsArray[i].engine;
for (int j = 0; j <= M; j++) {
pme_c[j] += paramsArray[i].pme_c[j];
pve_c[j] += paramsArray[i].pve_c[j];
pme_tpm[j] += paramsArray[i].pme_tpm[j];
pme_fpkm[j] += paramsArray[i].pme_fpkm[j];
}
for (int j = 0; j < m; j++)
pve_c_genes[j] += paramsArray[i].pve_c_genes[j];
if (alleleS)
for (int j = 0; j < m_trans; j++)
pve_c_trans[j] += paramsArray[i].pve_c_trans[j];
delete[] paramsArray[i].pme_c;
delete[] paramsArray[i].pve_c;
delete[] paramsArray[i].pme_tpm;
delete[] paramsArray[i].pme_fpkm;
delete[] paramsArray[i].pve_c_genes;
if (alleleS) delete[] paramsArray[i].pve_c_trans;
}
delete[] paramsArray;
for (int i = 0; i <= M; i++) {
pme_c[i] /= NSAMPLES;
pve_c[i] = (pve_c[i] - double(NSAMPLES) * pme_c[i] * pme_c[i]) / double(NSAMPLES - 1);
if (pve_c[i] < 0.0) pve_c[i] = 0.0;
pme_tpm[i] /= NSAMPLES;
pme_fpkm[i] /= NSAMPLES;
}
for (int i = 0; i < m; i++) {
int b = gi.spAt(i), e = gi.spAt(i + 1);
double pme_c_gene = 0.0;
for (int j = b; j < e; j++) pme_c_gene += pme_c[j];
pve_c_genes[i] = (pve_c_genes[i] - double(NSAMPLES) * pme_c_gene * pme_c_gene) / double(NSAMPLES - 1);
if (pve_c_genes[i] < 0.0) pve_c_genes[i] = 0.0;
}
if (alleleS)
for (int i = 0; i < m_trans; i++) {
int b = ta.spAt(i), e = ta.spAt(i + 1);
double pme_c_tran = 0.0;
for (int j = b; j < e; j++) pme_c_tran += pme_c[j];
pve_c_trans[i] = (pve_c_trans[i] - double(NSAMPLES) * pme_c_tran * pme_c_tran) / double(NSAMPLES - 1);
if (pve_c_trans[i] < 0.0) pve_c_trans[i] = 0.0;
}
}
int main(int argc, char* argv[]) {
if (argc < 7) {
// pliu
// add an option --prior to take priors
printf("Usage: rsem-run-gibbs reference_name imdName statName BURNIN NSAMPLES GAP [-p #Threads] [--seed seed] [--pseudo-count pseudo_count] [--prior file] [-q]\n");
printf("\n");
printf("Format of the prior file:\n");
printf("- One isoform's prior per line\n");
printf("- Priors must be in the same order as in the .ti file\n");
printf("- Priors for those to-be-omitted isoforms must be included as well\n");
printf("- Comments can be added after prior separated by space(s)\n");
exit(-1);
}
strcpy(refName, argv[1]);
strcpy(imdName, argv[2]);
strcpy(statName, argv[3]);
BURNIN = atoi(argv[4]);
NSAMPLES = atoi(argv[5]);
GAP = atoi(argv[6]);
nThreads = 1;
hasSeed = false;
pseudoC = 1.0;
quiet = false;
// pliu
has_prior = false;
//////
for (int i = 7; i < argc; i++) {
if (!strcmp(argv[i], "-p")) nThreads = atoi(argv[i + 1]);
if (!strcmp(argv[i], "--seed")) {
hasSeed = true;
int len = strlen(argv[i + 1]);
seed = 0;
for (int k = 0; k < len; k++) seed = seed * 10 + (argv[i + 1][k] - '0');
}
if (!strcmp(argv[i], "--pseudo-count")) pseudoC = atof(argv[i + 1]);
if (!strcmp(argv[i], "-q")) quiet = true;
// pliu
if ( ! strcmp(argv[i], "--prior") ) {
has_prior = true;
strcpy(fprior, argv[i+1]);
}
//////
}
verbose = !quiet;
assert(NSAMPLES > 1); // Otherwise, we cannot calculate posterior variance
if (nThreads > NSAMPLES) {
nThreads = NSAMPLES;
fprintf(stderr, "Warning: Number of samples is less than number of threads! Change the number of threads to %d!\n", nThreads);
}
load_data(refName, statName, imdName);
load_group_info(refName);
load_omit_info(imdName);
// pliu
// have to do it after load_data() in order to use 'M'
// the variable 'totc' will be re-calculated by including the prior info
if ( has_prior ) {
load_prior_info(fprior);
}
//////
sprintf(modelF, "%s.model", statName);
FILE *fi = fopen(modelF, "r");
general_assert(fi != NULL, "Cannot open " + cstrtos(modelF) + "!");
assert(fscanf(fi, "%d", &model_type) == 1);
fclose(fi);
mw = new double[M + 1]; // make an extra copy
switch(model_type) {
case 0 : init_model_related<SingleModel>(modelF); break;
case 1 : init_model_related<SingleQModel>(modelF); break;
case 2 : init_model_related<PairedEndModel>(modelF); break;
case 3 : init_model_related<PairedEndQModel>(modelF); break;
}
if (verbose) printf("Gibbs started!\n");
init();
for (int i = 0; i < nThreads; i++) {
rc = pthread_create(&threads[i], &attr, Gibbs, (void*)(¶msArray[i]));
pthread_assert(rc, "pthread_create", "Cannot create thread " + itos(i) + " (numbered from 0)!");
}
for (int i = 0; i < nThreads; i++) {
rc = pthread_join(threads[i], NULL);
pthread_assert(rc, "pthread_join", "Cannot join thread " + itos(i) + " (numbered from 0)!");
}
release();
if (verbose) printf("Gibbs finished!\n");
writeResultsGibbs(M, m, m_trans, gi, gt, ta, alleleS, imdName, pme_c, pme_fpkm, pme_tpm, pve_c, pve_c_genes, pve_c_trans);
delete mw; // delete the copy
return 0;
}