| 1 |
#ifndef SEQLIB_HISTOGRAM_H__ |
| 2 |
#define SEQLIB_HISTOGRAM_H__ |
| 3 |
|
| 4 |
#include <iostream> |
| 5 |
#include <cassert> |
| 6 |
#include <string> |
| 7 |
#include <utility> |
| 8 |
#include <vector> |
| 9 |
#include <fstream> |
| 10 |
|
| 11 |
#include "IntervalTree.h" |
| 12 |
|
| 13 |
class Bin; |
| 14 |
|
| 15 |
typedef TInterval<Bin> BinInterval; |
| 16 |
typedef TIntervalTree<Bin> BinIntervalTree; |
| 17 |
typedef std::vector<BinInterval> BinIntervalVector; |
| 18 |
|
| 19 |
#define INTERCHR 250000000 |
| 20 |
|
| 21 |
namespace SeqLib { |
| 22 |
|
| 23 |
class Histogram; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
class Bin { |
| 28 |
|
| 29 |
friend class Histogram; |
| 30 |
|
| 31 |
public: |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
Bin() : m_count(0) |
| 36 |
{ |
| 37 |
bounds = {0,1}; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
friend std::ostream& operator<<(std::ostream &out, const Bin &b) { |
| 43 |
out << b.bounds.first << "," << b.bounds.second << "," << b.m_count; |
| 44 |
return out; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
int32_t getCount() const { return m_count; } |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
bool contains(const int32_t& elem) const; |
| 61 |
|
| 62 |
|
| 63 |
bool operator < (const Bin& b) const; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
Bin& operator--(); |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
Bin& operator++(); |
| 74 |
|
| 75 |
private: |
| 76 |
int32_t m_count; |
| 77 |
std::pair<int32_t, int32_t> bounds; |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
class Histogram { |
| 88 |
|
| 89 |
private: |
| 90 |
|
| 91 |
std::vector<int32_t> m_ind; |
| 92 |
|
| 93 |
public: |
| 94 |
|
| 95 |
std::vector<Bin> m_bins; |
| 96 |
|
| 97 |
|
| 98 |
Histogram() {} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
Histogram(const int32_t& start, const int32_t& end, const uint32_t& width); |
| 107 |
|
| 108 |
std::string toFileString() const; |
| 109 |
|
| 110 |
friend std::ostream& operator<<(std::ostream &out, const Histogram &h) { |
| 111 |
for (auto& i : h.m_bins) |
| 112 |
out << i << std::endl; |
| 113 |
return out; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
std::vector<int32_t>::iterator begin() { return m_ind.begin(); } |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
std::vector<int32_t>::iterator end() { return m_ind.end(); } |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
void Initialize(size_t num_bins, std::vector<int32_t>* pspanv, size_t min_bin_width = 0); |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
void addElem(const int32_t &elem); |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
void removeElem(const int32_t &elem); |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
void toCSV(std::ofstream &fs); |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
int totalCount() const { |
| 145 |
int tot = 0; |
| 146 |
for (auto& i : m_bins) |
| 147 |
tot += i.getCount(); |
| 148 |
return tot; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
int32_t binCount(size_t i) { return m_bins[i].getCount(); } |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
size_t numBins() { return m_bins.size(); } |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
size_t retrieveBinID(const int32_t& elem) const; |
| 167 |
|
| 168 |
}; |
| 169 |
|
| 170 |
} |
| 171 |
#endif |