| 1 | #include "SeqLib/Histogram.h" |
| 2 | #include "SeqLib/SeqLibUtils.h" |
| 3 | #include <fstream> |
| 4 | #include <cmath> |
| 5 | #include <algorithm> |
| 6 | #include <sstream> |
| 7 | |
| 8 | #define BINARY_SEARCH 1 |
| 9 | |
| 10 | #define DEBUG_HISTOGRAM |
| 11 | |
| 12 | namespace SeqLib { |
| 13 | |
| 14 | Histogram::Histogram(const int32_t& start, const int32_t& end, const uint32_t& width) |
| 15 | { |
| 16 | |
| 17 | if (end >= start) |
| 18 | throw std::invalid_argument("Histogram end must be > start"); |
| 19 | |
| 20 | Bin bin; |
| 21 | bin.bounds.first = start; |
| 22 | |
| 23 | int32_t next_end = start + width - 1; // -1 because width=1 is bound.first = bounds.second |
| 24 | |
| 25 | while (next_end < end) |
| 26 | { |
| 27 | // finish this bin |
| 28 | bin.bounds.second = next_end; |
| 29 | m_bins.push_back(bin); |
| 30 | m_ind.push_back(bin.bounds.first); // make the index for the lower bound |
| 31 | |
| 32 | // start a new one |
| 33 | bin.bounds.first = next_end+1; |
| 34 | next_end += width; |
| 35 | } |
| 36 | |
| 37 | // finish the last bin |
| 38 | bin.bounds.second = end; |
| 39 | m_bins.push_back(bin); |
| 40 | m_ind.push_back(bin.bounds.first); |
| 41 | |
| 42 | // add a final bin |
| 43 | //bin.bounds.first = end+1; |
| 44 | //bin.bounds.second = INT_MAX; |
| 45 | //m_bins.push_back(bin); |
| 46 | //m_ind.push_back(bin.bounds.first); |
| 47 | |
| 48 | } |
| 49 | |
| 50 | void Histogram::toCSV(std::ofstream &fs) { |
| 51 | |
| 52 | for (auto& i : m_bins) |
| 53 | fs << i << std::endl; |
| 54 | |
| 55 | } |
| 56 | void Histogram::removeElem(const int32_t& elem) { |
| 57 | --m_bins[retrieveBinID(elem)]; |
| 58 | } |
| 59 | |
| 60 | void Histogram::addElem(const int32_t& elem) { |
| 61 | ++m_bins[retrieveBinID(elem)]; |
| 62 | } |
| 63 | |
| 64 | std::string Histogram::toFileString() const { |
| 65 | std::stringstream ss; |
| 66 | for (auto& i : m_bins) |
| 67 | if (i.m_count) |
| 68 | ss << i.bounds.first << "_" << i.bounds.second << "_" << i.m_count << ","; |
| 69 | std::string out = ss.str(); |
| 70 | out.pop_back(); // trim off last comma |
| 71 | return(out); |
| 72 | |
| 73 | } |
| 74 | |
| 75 | size_t Histogram::retrieveBinID(const int32_t& elem) const { |
| 76 | |
| 77 | if (elem < m_bins[0].bounds.first) |
| 78 | { |
| 79 | #ifdef DEBUG_HISTOGRAM |
| 80 | std::cerr << "removeElem: elem of value " << elem << " is below min bin " << m_bins[0] << std::endl; |
| 81 | exit(1); |
| 82 | #endif |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | if (elem > m_bins.back().bounds.second) |
| 87 | { |
| 88 | #ifdef DEBUG_HISTOGRAM |
| 89 | std::cerr << "removeElem: elem of value " << elem << " is above max bin " << m_bins.back() << std::endl; |
| 90 | exit(1); |
| 91 | #endif |
| 92 | return m_bins.size(); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | if (m_bins[0].contains(elem)) |
| 97 | return 0; |
| 98 | if (m_bins.back().contains(elem)) |
| 99 | return m_bins.size(); |
| 100 | |
| 101 | #ifdef BINARY_SEARCH |
| 102 | // binary search |
| 103 | std::vector<int32_t>::const_iterator it = std::upper_bound(m_ind.begin(), m_ind.end(), elem); |
| 104 | size_t i = it - m_ind.begin()-1; |
| 105 | assert(i < m_ind.size()); |
| 106 | return i; |
| 107 | #else |
| 108 | for (size_t i = 0; i < m_bins.size(); i++) { |
| 109 | if (m_bins[i].contains(elem)) { |
| 110 | return i; |
| 111 | } |
| 112 | } |
| 113 | #endif |
| 114 | std::cerr << "bin not found for element " << elem << std::endl; |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | void Histogram::initialize(size_t num_bins, std::vector<int32_t>* pspanv, size_t min_bin_width) { |
| 119 | |
| 120 | // ensure that they spans are sorted |
| 121 | std::sort(pspanv->begin(), pspanv->end()); |
| 122 | |
| 123 | // fill the histogram bins with matrix pairs (pre-sorted by distance) |
| 124 | Bin bin; |
| 125 | |
| 126 | // get number of inter-chr events |
| 127 | size_t intra = 0; |
| 128 | for (auto& i : *pspanv) |
| 129 | if (i != INTERCHR) |
| 130 | intra++; |
| 131 | |
| 132 | int bin_cut = 0; |
| 133 | try { |
| 134 | bin_cut = floor((double)intra / (double)num_bins); |
| 135 | if (bin_cut == 0) |
| 136 | throw 1; |
| 137 | } catch(...) { |
| 138 | std::cerr << "Error in determining bin cut. Not enought events or too many bins?" << std::endl; |
| 139 | std::cerr << "Events: " << pspanv->size() << " Num Bins " << num_bins << " quantile count (hist height) " << bin_cut << std::endl; |
| 140 | } |
| 141 | |
| 142 | std::cout << "...Events per bin: " << bin_cut << " num bins " << num_bins << std::endl; |
| 143 | |
| 144 | S last_span = 0; |
| 145 | size_t tcount = 0; // count events put into bins |
| 146 | |
| 147 | // iterate over spans |
| 148 | for (auto& span : *pspanv) { |
| 149 | if (span != INTERCHR) { |
| 150 | |
| 151 | ++tcount; |
| 152 | |
| 153 | // moved into a new bin? (or done?) |
| 154 | if (bin.getCount() > bin_cut && span != last_span && (last_span - bin.bounds.first) >= min_bin_width) { |
| 155 | |
| 156 | // finalize, save old bin |
| 157 | bin.bounds.second = last_span; |
| 158 | m_bins.push_back(bin); |
| 159 | |
| 160 | // new bin |
| 161 | bin.bounds.first = last_span+1; |
| 162 | bin.m_count = 0; |
| 163 | |
| 164 | } |
| 165 | ++bin; |
| 166 | if (bin.getCount() >= bin_cut) { |
| 167 | last_span = span; |
| 168 | } |
| 169 | |
| 170 | //update the size of current bin |
| 171 | bin.bounds.second = span; |
| 172 | } |
| 173 | } |
| 174 | // add the last bin |
| 175 | bin.bounds.second = INTERCHR-1; |
| 176 | m_bins.push_back(bin); |
| 177 | |
| 178 | // add a bin for interchr events |
| 179 | bin.bounds = {INTERCHR, INTERCHR}; |
| 180 | bin.m_count = pspanv->size() - intra; |
| 181 | m_bins.push_back(bin); |
| 182 | |
| 183 | // make the indices of lower bound |
| 184 | for (auto& i : m_bins) |
| 185 | m_ind.push_back(i.bounds.first); |
| 186 | |
| 187 | if (m_bins.size() != (num_bins+1)) { |
| 188 | //std::cout << " bin cut " << bin_cut << std::endl; |
| 189 | //std::cout << " num bins " << num_bins << " bins.size() " << m_bins.size() << std::endl; |
| 190 | //assert(bins.size() == (num_bins+1)); |
| 191 | } |
| 192 | |
| 193 | } |
| 194 | |
| 195 | bool Bin::operator < (const Bin& b) const { |
| 196 | return (bounds.first < b.bounds.first || (bounds.first==b.bounds.first && bounds.second < b.bounds.second)); |
| 197 | |
| 198 | } |
| 199 | |
| 200 | bool Bin::contains(const int32_t& elem) const { |
| 201 | |
| 202 | return (elem >= bounds.first && elem <= bounds.second); |
| 203 | |
| 204 | |
| 205 | } |
| 206 | |
| 207 | Bin& Bin::operator++() |
| 208 | { |
| 209 | ++m_count; |
| 210 | return *this; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | Bin& Bin::operator--() { |
| 215 | assert(m_count > 0); |
| 216 | --m_count; |
| 217 | return *this; |
| 218 | } |
| 219 | |
| 220 | } |