Debian Codegraph @

src/pbmer/SparseHaplotype.cpp

raw
1 #include <pbcopper/pbmer/SparseHaplotype.h>
2
3 #include <iomanip>
4 #include <iostream>
5 #include <random>
6 #include <sstream>
7
8 namespace PacBio {
9 namespace Pbmer {
10
11 SparseHaplotype::SparseHaplotype(std::string name, uint32_t id, int32_t nvars)
12 : name_{std::move(name)}, haplotypeId_{id}, nVariants_{nvars}
13 {
14 variants_.resize(nvars);
15 }
16
17 SparseHaplotype::SparseHaplotype(std::string name, uint32_t id, const std::string& seq)
18 : name_{std::move(name)}, haplotypeId_{id}, nVariants_{static_cast<int32_t>(seq.size())}
19 {
20 variants_.resize(seq.size());
21
22 fractionalMembership_.push_back(0.0);
23 cluster_ = 0;
24
25 int idx = 0;
26 for (const auto i : seq) {
27 if (i != '1' && i != '0') {
28 throw std::runtime_error("Invalid binary sequence. String must only contain [0,1]");
29 }
30
31 if (i == '1') {
32 variants_[idx] = 1;
33 }
34 ++idx;
35 }
36 }
37
38 void SparseHaplotype::InitializeMembership(int32_t nClusters)
39 {
40 cluster_ = 0;
41 fractionalMembership_.resize(0);
42 fractionalMembership_.clear();
43
44 std::random_device rd;
45 std::mt19937 gen(rd());
46 std::uniform_real_distribution<> dis(0.001, 0.999);
47
48 // stick breaking model to generate random variables that sum to (or nearly) one.
49 double stick = 1.0;
50 for (auto i = 0; i < nClusters; ++i) {
51
52 auto temp = stick * dis(gen);
53 fractionalMembership_.push_back(temp);
54 stick = temp;
55 }
56 }
57
58 void SparseHaplotype::InitializeMembership(int32_t nClusters, int prior)
59 {
60 cluster_ = 0;
61 fractionalMembership_.resize(0);
62 fractionalMembership_.clear();
63
64 std::random_device rd;
65 std::mt19937 gen(rd());
66 std::uniform_real_distribution<> dis(0.0, 1.0);
67
68 // stick breaking model to generate random variables that sum to (or nearly) one.
69 double stick = 0.5;
70 for (auto i = 0; i < nClusters; ++i) {
71 if (i != prior) {
72 auto temp = stick * dis(gen);
73 fractionalMembership_.push_back(temp);
74 stick = temp;
75 } else {
76 fractionalMembership_.push_back(0.5);
77 }
78 }
79 }
80
81 void SparseHaplotype::LoadVariants(const Bubbles& bubs)
82 {
83 uint32_t bubIdx = 0;
84 for (const auto& b : bubs) {
85 for (const auto& l : b.LData) {
86 auto id = std::get<0>(l);
87 if (id == haplotypeId_) {
88 variants_[bubIdx] = 1;
89 }
90 }
91 ++bubIdx;
92 }
93 }
94
95 std::string SparseHaplotype::HapStr() const
96 {
97 std::ostringstream result;
98 for (boost::dynamic_bitset<>::size_type i = 0; i < variants_.size(); ++i) {
99 result << variants_[i];
100 }
101 return result.str();
102 }
103
104 std::string SparseHaplotype::ToStr() const
105 {
106 std::ostringstream result;
107
108 result << name_ << " " << haplotypeId_ << " ";
109
110 for (boost::dynamic_bitset<>::size_type i = 0; i < variants_.size(); ++i) {
111 result << variants_[i];
112 }
113
114 if (!fractionalMembership_.empty()) {
115 result << " nclusters:" << fractionalMembership_.size() << " ";
116
117 for (auto m : fractionalMembership_) {
118 result << m << " ";
119 }
120
121 result << "clusterAssignment: " << cluster_ << " ";
122 result << "clusterProb:" << fractionalMembership_[cluster_];
123 }
124
125 return result.str();
126 }
127
128 int32_t SparseHaplotype::ClusterId() const { return cluster_; }
129
130 const std::string& SparseHaplotype::Name() const { return name_; }
131
132 int32_t SparseHaplotype::NVariants() const { return nVariants_; }
133
134 bool SparseHaplotype::HasVar(int index) const
135 {
136 assert(index < static_cast<int>(variants_.size()) && index >= 0);
137 return (variants_[index] == 1);
138 }
139
140 double SparseHaplotype::MembershipFraction(int index) const
141 {
142 assert(index < static_cast<int>(fractionalMembership_.size()) && index >= 0);
143 return fractionalMembership_[index];
144 }
145
146 void SparseHaplotype::SetMembershipProb(int index, double val)
147 {
148 assert(index < static_cast<int>(fractionalMembership_.size()) && index >= 0);
149 fractionalMembership_[index] = val;
150 }
151
152 void SparseHaplotype::SetCluster(int32_t index) { cluster_ = index; }
153
154 } // namespace Pbmer
155 } // namespace PacBio