-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdensity_functions_crosslink_reg.h
267 lines (217 loc) · 10.1 KB
/
density_functions_crosslink_reg.h
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
// ======================================================================
// PureCLIP: capturing target-specific protein-RNA interaction footprints
// ======================================================================
// Copyright (C) 2017 Sabrina Krakau, Max Planck Institute for Molecular
// Genetics
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// =======================================================================
// Author: Sabrina Krakau <[email protected]>
// =======================================================================
#ifndef APPS_HMMS_DENSITY_FUNCTIONS_CROSSLINK_REG_H_
#define APPS_HMMS_DENSITY_FUNCTIONS_CROSSLINK_REG_H_
#include <iostream>
#include <fstream>
#include <math.h> // lgamma
#include <boost/math/tools/minima.hpp> // BRENT's algorithm
#include <boost/math/distributions/negative_binomial.hpp>
#include <boost/math/special_functions/gamma.hpp> // normalized lower incomplete gamma function: gamma_p()
#include <boost/math/distributions/binomial.hpp>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_roots.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_multiroots.h>
using namespace seqan;
////////
// ZTBIN_REG
////////
// P = (k-1)/(n-1) ?
class ZTBIN_REG
{
public:
ZTBIN_REG(long double b0_): b0(b0_) {}
ZTBIN_REG() {}
long double getDensity(unsigned const &k, unsigned const &n, long double const &pred, AppOptions const& options);
long double getDensity(unsigned const &k, unsigned const &n, AppOptions const& options);
void updateP(String<String<String<double> > > &statePosteriors, String<String<Observations> > &setObs, AppOptions const& options);
void updateRegCoeffs(String<String<String<double> > > &statePosteriors, String<String<Observations> > &setObs, AppOptions const&options);
long double b0; // intercept
String<long double> regCoeffs;
};
// Functor for Brent's algorithm: find regression coefficients
// for given motif m; optimize b_m
struct FctLL_ZTBIN_REG
{
FctLL_ZTBIN_REG(long double const& b0_, char const& m_, String<String<String<double> > > const& statePosteriors_, String<String<Observations> > &setObs_, AppOptions const&options_) : b0(b0_), m(m_), statePosteriors(statePosteriors_), setObs(setObs_), options(options_)
{
}
double operator()(double const& b)
{
long double ll = 0.0;
for (unsigned s = 0; s < 2; ++s)
{
String<long double> lls;
resize(lls, length(setObs[s]), 0.0, Exact());
#if HMM_PARALLEL
SEQAN_OMP_PRAGMA(parallel for schedule(dynamic, 1) num_threads(options.numThreads))
#endif
for (unsigned i = 0; i < length(setObs[s]); ++i)
{
for (unsigned t = 0; t < setObs[s][i].length(); ++t)
{
// only optimize for sites with fimo score > 0.0 and corresponding to current id m
if (setObs[s][i].nEstimates[t] >= options.nThresholdForP && setObs[s][i].truncCounts[t] > 0 && setObs[s][i].motifIds[t] == m && setObs[s][i].fimoScores[t] > 0.0 && setObs[s][i].nEstimates[t] <= options.maxBinN)
{
unsigned k = setObs[s][i].truncCounts[t];
unsigned n = (setObs[s][i].nEstimates[t] > setObs[s][i].truncCounts[t]) ? (setObs[s][i].nEstimates[t]) : (setObs[s][i].truncCounts[t]);
long double x = setObs[s][i].fimoScores[t];
if (((long double)(k) / (long double)(n)) <= options.maxkNratio)
{
long double p = 1.0/(1.0+exp(-b0 - b*x));
// l = log(1.0) -log(1.0 - pow((1.0-p), n)) + log (n over k) + k*log(p) + (n-k)*log(1.0-p);
// ignore parts not meaning for optimization!
long double l = -log(1.0 - pow((1.0-p), n)) + k*log(p) + (n-k)*log(1.0-p);
lls[i] += l * statePosteriors[s][i][t];
}
}
}
}
// combine results from threads
for (unsigned i = 0; i < length(setObs[s]); ++i)
ll += lls[i];
}
return (-ll);
}
private:
long double b0;
char m; // motif ID
String<String<String<double> > > statePosteriors;
String<String<Observations> > &setObs;
AppOptions options;
};
void ZTBIN_REG::updateRegCoeffs(String<String<String<double> > > &statePosteriors,
String<String<Observations> > &setObs,
AppOptions const&options)
{
int bits = 60;
boost::uintmax_t maxIter = options.maxIter_brent;
long double bMin = 0.0;
long double bMax = 1.0;
// for each input motif learn independent regCoeff (each position only one motif match with score assigned)
for (unsigned char m = 0; m < options.nInputMotifs; ++m)
{
FctLL_ZTBIN_REG fct_ZTBIN_REG(this->b0, m, statePosteriors, setObs, options);
std::pair<long double, long double> res = boost::math::tools::brent_find_minima(fct_ZTBIN_REG, bMin, bMax, bits, maxIter);
this->regCoeffs[m] = res.first;
}
}
// use truncCounts
void ZTBIN_REG::updateP(String<String<String<double> > > &statePosteriors,
String<String<Observations> > &setObs, AppOptions const& options)
{
long double sum1 = 0.0;
long double sum2 = 0.0;
for (unsigned s = 0; s < 2; ++s)
{
for (unsigned i = 0; i < length(setObs[s]); ++i)
{
for (unsigned t = 0; t < setObs[s][i].length(); ++t)
{
if (setObs[s][i].nEstimates[t] >= options.nThresholdForP && setObs[s][i].truncCounts[t] > 0 && setObs[s][i].fimoScores[t] == 0.0 && setObs[s][i].nEstimates[t] <= options.maxBinN) // avoid deviding by 0 (NOTE !), zero-truncated
{
// p^ = (k-1)/(n-1); 'Truncated Binomial and Negative Binomial Distributions' Rider, 1955
unsigned k = setObs[s][i].truncCounts[t];
unsigned n = (setObs[s][i].nEstimates[t] > setObs[s][i].truncCounts[t]) ? (setObs[s][i].nEstimates[t]) : (setObs[s][i].truncCounts[t]);
if (((long double)(k) / (long double)(n)) <= options.maxkNratio)
{
sum1 += statePosteriors[s][i][t] * ((double)(k - 1)/(double)(n - 1));
sum2 += statePosteriors[s][i][t];
}
}
}
}
}
//std::cout << "updateP: sum1" << sum1 << " sum2: " << sum2 << " p: " << (sum1/sum2) << std::endl;
long double p = sum1/sum2;
this->b0 = log(p/(1.0-p));
updateRegCoeffs(statePosteriors, setObs, options);
}
// k: diagnostic events (de); n: read counts (c)
long double ZTBIN_REG::getDensity(unsigned const &k, unsigned const &n, long double const &pred, AppOptions const& options)
{
if (k == 0) return 0.0; // zero-truncated
unsigned n2 = n;
unsigned k2 = k;
n2 = (n2 > k2) ? n2 : k2; // make sure n >= k (or limit k?)
// use boost implementation, maybe avoids overflow
boost::math::binomial_distribution<long double> boostBin;
boostBin = boost::math::binomial_distribution<long double> ((int)n2, (long double)pred);
long double res = boost::math::pdf(boostBin, k2);
if (std::isnan(res)) // or any other error?
{
std::cerr << "ERROR: binomial pdf is : " << res << std::endl;
return 0.0;
}
return res * (long double)(1.0/(1.0 - pow((1.0 - (long double)pred), n2))); // zero-truncated TODO ???
}
long double ZTBIN_REG::getDensity(unsigned const &k, unsigned const &n, AppOptions const&options)
{
if (k == 0) return 0.0; // zero-truncated
unsigned n2 = n;
unsigned k2 = k;
n2 = (n2 > k2) ? n2 : k2; // make sure n >= k (or limit k?)
long double pred = 1.0/(1.0+exp(- this->b0));
// use boost implementation, maybe avoids overflow
boost::math::binomial_distribution<long double> boostBin;
boostBin = boost::math::binomial_distribution<long double> ((int)n2, pred);
long double res = boost::math::pdf(boostBin, k2);
if (std::isnan(res) || std::isinf(res)) // or any other error?
{
std::cerr << "ERROR: binomial pdf is : " << res << std::endl;
return 0.0;
}
return res * (long double)(1.0/(1.0 - pow((1.0 - pred), n2))); // zero-truncated TODO ???
}
void myPrint(ZTBIN_REG &bin)
{
std::cout << "*** ZTBIN_REG ***" << std::endl;
std::cout << " p (0):"<< (1.0/(1.0+exp(-bin.b0))) << std::endl;
std::cout << " b0:"<< bin.b0 << std::endl;
for (unsigned m = 0; m < length(bin.regCoeffs); ++m)
std::cout << " b" << m << ": " << bin.regCoeffs[m] << std::endl;
std::cout << std::endl;
}
template<typename TOut>
void printParams(TOut &out, ZTBIN_REG &bin, int i)
{
out << "bin" << i << ".b0" << '\t' << bin.b0 << std::endl;
for (unsigned m = 0; m < length(bin.regCoeffs); ++m)
out << "bin" << i << ".b" << (m+1) << '\t' << bin.regCoeffs[m] << std::endl;
out << std::endl;
}
bool checkConvergence(ZTBIN_REG &bin1, ZTBIN_REG &bin2, AppOptions &options)
{
if (std::fabs(bin1.b0 - bin2.b0) > options.bin_p_conv) return false;
for (unsigned m = 0; m < length(bin1.regCoeffs); ++m)
if (std::fabs(bin1.regCoeffs[m] - bin2.regCoeffs[m]) > options.bin_b_conv) return false;
return true;
}
void checkOrderBin1Bin2(ZTBIN_REG &bin1, ZTBIN_REG &bin2)
{
if (bin1.b0 > bin2.b0)
std::swap(bin1.b0, bin2.b0);
}
#endif