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
|
#ifndef __GC_FRAG_MODEL__
#define __GC_FRAG_MODEL__
#include "DistributionUtils.hpp"
#include "Eigen/Dense"
#include "SalmonMath.hpp"
#include <boost/iostreams/filtering_stream.hpp>
#include <iostream>
#include <vector>
struct GCDesc {
int32_t fragFrac;
int32_t contextFrac;
// assumes 101 bins
int32_t fragBin() { return fragFrac; }
int32_t contextBin() { return contextFrac; }
int32_t fragBin(int32_t n) {
double w = (100.0 / n);
return std::min(n - 1, static_cast<int32_t>(fragFrac / w));
}
int32_t contextBin(int32_t n) {
double w = (100.0 / n);
return std::min(n - 1, static_cast<int32_t>(contextFrac / w));
}
bool operator==(const GCDesc& other) const {
return fragFrac == other.fragFrac and contextFrac == other.contextFrac;
}
bool operator==(const GCDesc&& other) const {
return fragFrac == other.fragFrac and contextFrac == other.contextFrac;
}
friend std::ostream& operator<<(std::ostream& os, const GCDesc& c);
};
inline std::ostream& operator<<(std::ostream& os, const GCDesc& c) {
os << "{ fragFrac : " << c.fragFrac << ", contextFrac : " << c.contextFrac << "}\n";
return os;
}
class GCFragModel {
public:
GCFragModel(size_t condBins = 3, size_t numGCBins = 101,
distribution_utils::DistributionSpace dspace =
distribution_utils::DistributionSpace::LOG)
: condBins_(condBins), numGCBins_(numGCBins), dspace_(dspace),
normalized_(false) {
counts_ = Eigen::MatrixXd(condBins_, numGCBins_);
if (dspace_ == distribution_utils::DistributionSpace::LOG) {
counts_.setOnes();
counts_ *= salmon::math::LOG_0;
} else {
counts_.setZero();
}
// set the total vector to be the right size and full of 0's.
modelTotals_.resize(condBins_, 0.0);
}
bool writeBinary(boost::iostreams::filtering_ostream& out) const {
auto* mutThis = const_cast<GCFragModel*>(this);
int32_t dtype =
(dspace_ == distribution_utils::DistributionSpace::LINEAR) ? 0 : 1;
out.write(reinterpret_cast<char*>(&dtype), sizeof(dtype));
typename Eigen::MatrixXd::Index rows = counts_.rows(),
cols = counts_.cols();
out.write(reinterpret_cast<char*>(&rows),
sizeof(typename Eigen::MatrixXd::Index));
out.write(reinterpret_cast<char*>(&cols),
sizeof(typename Eigen::MatrixXd::Index));
out.write(reinterpret_cast<char*>(const_cast<double*>(modelTotals_.data())),
sizeof(double) * rows);
out.write(reinterpret_cast<char*>(mutThis->counts_.data()),
rows * cols * sizeof(typename Eigen::MatrixXd::Scalar));
return true;
}
GCFragModel(const GCFragModel&) = default;
GCFragModel(GCFragModel&&) = default;
GCFragModel& operator=(const GCFragModel&) = default;
GCFragModel& operator=(GCFragModel&&) = default;
/*
double likelihood_(uint32_t numBins) {
}
uint32_t optNumBins() {
if (numGCBins_ != 101) {
std::cerr << "Selecting the optimal number of bins is currently "
<< "only supported when the initial histograms are generated "
<< "using 101 bins.\n";
std::exit(1);
}
std::vector<uint32_t> nbins{5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 101};
// Using the "BR" penalty
// see:
// Davies, Laurie, et al. "A comparison of automatic histogram
constructions."
// ESAIM: Probability and Statistics 13 (2009): 181-196.
std::vector<double> scores;
for (auto nb : nbins) {
likelihood_(nb)
}
}
*/
void reset(distribution_utils::DistributionSpace dspace =
distribution_utils::DistributionSpace::LOG) {
normalized_ = false;
dspace_ = dspace;
if (dspace_ == distribution_utils::DistributionSpace::LOG) {
counts_.setOnes();
counts_ *= salmon::math::LOG_0;
} else {
counts_.setZero();
}
}
GCFragModel ratio(GCFragModel& other, double maxRatio) {
if (!normalized_) {
normalize();
}
if (!other.normalized_) {
other.normalize();
}
double minRatio = 1.0 / maxRatio;
GCFragModel ratioModel(condBins_, numGCBins_, dspace_);
for (size_t r = 0; r < condBins_; ++r) {
for (size_t c = 0; c < numGCBins_; ++c) {
double rat = (counts_(r, c) / other.counts_(r, c));
if (rat > maxRatio) {
rat = maxRatio;
}
if (rat < minRatio) {
rat = minRatio;
}
ratioModel.counts_(r, c) = rat;
}
}
return ratioModel;
}
void inc(GCDesc desc,
double fragWeight //< the weight associated with this fragment
) {
auto ctx = (condBins_ > 1) ? desc.contextBin(condBins_) : 0;
auto frag = (numGCBins_ != 101) ? desc.fragBin(numGCBins_) : desc.fragBin();
if (dspace_ == distribution_utils::DistributionSpace::LOG) {
counts_(ctx, frag) = salmon::math::logAdd(counts_(ctx, frag), fragWeight);
} else {
counts_(ctx, frag) += fragWeight;
}
}
double get(GCDesc desc) {
auto ctx = (condBins_ > 1) ? desc.contextBin(condBins_) : 0;
auto frag = (numGCBins_ != 101) ? desc.fragBin(numGCBins_) : desc.fragBin();
return counts_(ctx, frag);
}
distribution_utils::DistributionSpace distributionSpace() const {
return dspace_;
}
void combineCounts(const GCFragModel& other) {
if (dspace_ != other.dspace_) {
std::cerr
<< "Cannot combine distributions that live in a different space!\n";
std::exit(1);
}
if (dspace_ == distribution_utils::DistributionSpace::LOG) {
for (size_t r = 0; r < condBins_; ++r) {
for (size_t c = 0; c < numGCBins_; ++c) {
counts_(r, c) =
salmon::math::logAdd(counts_(r, c), other.counts_(r, c));
}
}
} else {
for (size_t r = 0; r < condBins_; ++r) {
for (size_t c = 0; c < numGCBins_; ++c) {
counts_(r, c) += other.counts_(r, c);
}
}
}
}
/**
* NOTE: Improve interface --- also converts out of log space
*/
void normalize(double prior = 0.1) {
if (!normalized_) {
if (dspace_ == distribution_utils::DistributionSpace::LOG) {
prior = std::log(prior);
for (size_t r = 0; r < condBins_; ++r) {
double rowMass{salmon::math::LOG_0};
for (size_t c = 0; c < numGCBins_; ++c) {
rowMass = salmon::math::logAdd(
prior, salmon::math::logAdd(rowMass, counts_(r, c)));
}
if (!salmon::math::isLog0(rowMass)) {
for (size_t c = 0; c < numGCBins_; ++c) {
counts_(r, c) = std::exp(
salmon::math::logAdd(prior, counts_(r, c)) - rowMass);
}
modelTotals_[r] = std::exp(rowMass);
}
// if rowMass is LOG_0, then leave modelTotals_[r] as 0.0
}
} else {
for (size_t r = 0; r < condBins_; ++r) {
double rowMass = 0.0;
for (size_t c = 0; c < numGCBins_; ++c) {
rowMass += (prior + counts_(r, c));
}
if (rowMass > 0.0) {
double norm = 1.0 / rowMass;
for (size_t c = 0; c < numGCBins_; ++c) {
counts_(r, c) = (prior + counts_(r, c)) * norm;
}
modelTotals_[r] = rowMass;
}
// if rowMass is 0.0, just leave modelTotals_[r] as 0.0;
}
}
normalized_ = true;
dspace_ = distribution_utils::DistributionSpace::LINEAR;
}
}
private:
size_t condBins_;
size_t numGCBins_;
distribution_utils::DistributionSpace dspace_;
bool normalized_;
Eigen::MatrixXd counts_;
std::vector<double> modelTotals_;
};
#endif //__GC_FRAG_MODEL__
|