File: Sampler.cpp

package info (click to toggle)
bitseq 0.7.5+dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 676 kB
  • sloc: cpp: 7,043; python: 562; makefile: 150; sh: 52
file content (220 lines) | stat: -rw-r--r-- 6,305 bytes parent folder | download | duplicates (4)
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
#ifdef DoSTATS
#include<sys/time.h>
#endif

#include "Sampler.h"
#include "common.h"

Sampler::Sampler(){ //{{{
   m=samplesN=samplesLogged=samplesTotal=samplesOut=Nmap=Nunmap=0;
   isoformLengths = NULL;
#ifdef DoSTATS
   tT=tTa=tZ=0;
   nT=nTa=nZ=0;
#endif
}//}}}
Sampler::~Sampler(){ //{{{
#ifdef DoSTATS
   message("---------------------------\nSTATISTICS:\n");
   message("Theta: %lg   %lgm   av:%lgs\n",nT,tT/60000.0,tT/1000.0/nT);
   message("Z: %lg   %lgm   av:%lgs\n",nZ,tZ/60000.0,tZ/1000.0/nZ);
   if(nTa>0)message("Theta Act: %ld   %lgm   av:%lgs\n",nTa,tTa/60000.0,tTa/1000.0/nTa);
   message("Total time: %lgm\n",(tT+tZ)/60000.0);
#endif
}//}}}
void Sampler::init(long m, long samplesTotal, long samplesOut, long Nunmap,const TagAlignments *alignments, const distributionParameters &betaPar, const distributionParameters &dirPar, long &seed){//{{{
//   this->n=n;
   this->m=m;
   this->samplesOut=samplesOut;
   this->Nmap=alignments->getNreads();
   this->Nunmap=Nunmap;
   this->alignments=alignments;
   beta=&betaPar;
   dir=&dirPar;
   //dir=new distributionParameters;
   //dir->alpha=1.0/m;
   //dir->beta=dirPar.beta;
   rng_mt.seed(seed);
   seed = (long) (1717171717.17*uniformDistribution(rng_mt));

   resetSampler(samplesTotal);

   theta.assign(m,0);
   C.assign(m,0);
}//}}}
void Sampler::resetSampler(long samplesTotal){//{{{
   this->samplesTotal=samplesTotal;
   samplesN = 0;
   samplesLogged = 0;
   logRate=(double)samplesOut/samplesTotal;
   sumC0 = 0;
   sumNorm.first = sumNorm.second = 0;
   thetaSum.assign(m,pairD(0,0));
   thetaSqSum.assign(m,pairD(0,0));
}//}}}
long Sampler::getAverageC0(){//{{{
   return (long) (sumC0 / sumNorm.first);
}//}}}
void Sampler::getAverage(vector<pairD> &av){//{{{
   long i;
   if((long)av.size()<m)
      av.assign(m,pairD(0,0));
   for(i=0;i<m;i++){
      if(sumNorm.first != 0)
         av[i].first=thetaSum[i].first/sumNorm.first;
      if(sumNorm.second != 0)
         av[i].second=thetaSum[i].second/sumNorm.second;
   }
}//}}}
pairD Sampler::getAverage(long i){//{{{
   double av1,av2;
   av1=(sumNorm.first==0)?0:thetaSum[i].first/sumNorm.first;
   av2=(sumNorm.second==0)?0:thetaSum[i].second/sumNorm.second;
   return pairD(av1,av2);
}//}}}
pairD Sampler::getWithinVariance(long i){//{{{
   double va1,va2;
   if(sumNorm.first==0)
      va1=0;
   else
      va1=thetaSqSum[i].first/(sumNorm.first-1.0) - 
           (thetaSum[i].first/(sumNorm.first-1.0))*
           (thetaSum[i].first/sumNorm.first);
   if(sumNorm.second==0)
      va2=0;
   else 
      va2=thetaSqSum[i].second/(sumNorm.second-1.0) - 
           (thetaSum[i].second/(sumNorm.second-1.0))*
           (thetaSum[i].second/sumNorm.second);
   if(va1<0)message("minus %lg %lg %lg\n",thetaSqSum[i].first,thetaSum[i].first,sumNorm.first);
   return pairD(va1,va2);
}//}}}
void Sampler::getThetaSums(long i, double *thSqSum, double *thSum, double *sumN){//{{{
   if(i >= m){
      (*thSqSum) = (*thSum) = (*sumN) = 0;
      return;
   }
   *thSqSum = thetaSqSum[i].first;
   *thSum = thetaSum[i].first;
   *sumN = sumNorm.first;
}//}}}
void Sampler::getTau(vector<double> &tau, double norm){//{{{
   double tauSum=0;

   if ((theta.size() > tau.size()) || (isoformLengths->size() != tau.size()))
     error("Sampler failed");

   tau.assign(tau.size(),0);

   tau[0]=theta[0]; // set thetaAct
   // divide by length:
   for(size_t i=1;i<theta.size();i++){
      tau[ i ] = theta[i] / (*isoformLengths)[ i ] * norm;
      tauSum += tau[i];
   }
   // DO normalize:
   for(size_t i=1;i<tau.size();i++)
      if(tau[i]>0) tau[i] /= tauSum;
}//}}}
void Sampler::appendFile(){//{{{
   long i;
   double norm=saveNorm;
   if((!save) || (outFile == NULL))return;
   thetaActLog.push_back(theta[0]);
   outFile->precision(9);
   (*outFile)<<scientific;
   if(saveType == "counts"){
      if(norm == 0)norm = Nmap;
      for(i=1;i<m;i++)
         (*outFile)<<theta[i]*norm<<" ";
   }else if(saveType == "rpkm"){
      if(norm == 0)norm = 1000000000.0;
      for(i=1;i<m;i++)
         if((*isoformLengths)[i]>0)
            (*outFile)<<theta[i]*norm/(*isoformLengths)[i]<<" ";
         else
            (*outFile)<<theta[i]*norm<<" ";
   }else if(saveType == "theta"){
      if(norm == 0)norm=1.0;
      for(i=1;i<m;i++)
         (*outFile)<<theta[i]*norm<<" ";
   }else if(saveType == "tau"){
      if(norm == 0)norm=1.0;
      vector<double> tau(m);
      getTau(tau,norm);
      for(i=1;i<m;i++)
         (*outFile)<<tau[i]<<" ";
   }
   (*outFile)<<endl;
}//}}}
void Sampler::updateSums(){//{{{
   long i;
   double s;
   for(i=0;i<m;i++){
      thetaSum[i].first+=theta[i];
      thetaSqSum[i].first+=theta[i]*theta[i];
   }
   sumC0+=C[0];
   sumNorm.first++;
   //if(doLog){
   for(i=0;i<m;i++){
      s = log(theta[i]) - log(1-theta[i]);//LOGIT
      thetaSum[i].second += s;
      thetaSqSum[i].second += s * s;
   }
   sumNorm.second++;
   //}
}//}}}
void Sampler::saveSamples(ofstream *outFile, const vector<double> *isoformLengths, const string &saveType, double norm){//{{{
   this->outFile = outFile;
   this->isoformLengths = isoformLengths;
   this->saveType = saveType;
   saveNorm = norm;
   save = true;
   thetaActLog.clear();
}//}}}
void Sampler::noSave(){//{{{
   save = false;
   outFile = NULL;
   if(isoformLengths){
      delete isoformLengths;
      isoformLengths = NULL;
   }
}//}}}

void Sampler::sampleTheta(){//{{{
#ifdef DoSTATS
   nT++;
   struct timeval start, end;
   gettimeofday(&start, NULL);
#endif
   vector<double> gamma(m,0);
   double gammaSum=0;
   long i;
   for(i=1;i<m;i++){
      gammaDistribution.param(gDP(dir->alpha + C[i], dir->beta));
      gamma[i]=gammaDistribution(rng_mt);
      gammaSum+=gamma[i];
   }
   if (gammaSum<=0) // at least something should be more than zero
     error("Sampler failed");

   for(i=1;i<m;i++){
      theta[i]=gamma[i]/gammaSum;
   }
#ifdef DoSTATS
   gettimeofday(&end, NULL);
   tT += (end.tv_sec-start.tv_sec)*1000*1000+(end.tv_usec-start.tv_usec);
#endif
}//}}}
void Sampler::sample(){//{{{
   samplesN++;
}//}}}
void Sampler::update(){//{{{
   doLog = false;
   if(samplesOut-samplesLogged>0){
      if(samplesTotal-samplesN<=samplesOut-samplesLogged)doLog=true;
      else if((long)(logRate * samplesN) > samplesLogged)doLog=true;
   }
   if(doLog) samplesLogged ++;
}//}}}