Debian Codegraph @

CSplit2.h

raw
1 /* BaitFisher (version 1.2.7) a program for designing DNA target enrichment baits
2 * Copyright 2013-2016 by Christoph Mayer
3 *
4 * This source file is part of the BaitFisher-package.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with BaitFisher. If not, see <http://www.gnu.org/licenses/>.
18 *
19 *
20 * For any enquiries send an Email to Christoph Mayer
21 * [email protected]
22 *
23 * When publishing work that is based on the results please cite:
24 * Mayer et al. 2016: BaitFisher: A software package for multi-species target DNA enrichment probe design
25 *
26 */
27 #ifndef CSplitH
28 #define CSplitH
29
30 #include "fast-dynamic-bitset/fast-dynamic-bitset.h"
31 #include <iostream>
32 #include <vector>
33 #include <fstream>
34 //#include "number2cstring.h"
35
36 class CSplit
37 {
38 private:
39 fast_dynamic_bitset split;
40
41 public:
42 // Bitset with a specfied number of bits, here taxa
43 CSplit(unsigned long size):split(size) {
44 split.clear();
45 }
46
47 CSplit(const fast_dynamic_bitset& b):split(b){}
48
49 CSplit(const CSplit& b):split(b.split){}
50 // CSplit(unsigned long, const vector<unsigned long>&);
51 // CSplit(unsigned long, unsigned long*, unsigned long);
52
53 friend bool operator ==(const CSplit&, const CSplit&);
54 friend bool operator <(const CSplit&, const CSplit&);
55 friend bool lessThan_mod_flip_friend(const CSplit&, const CSplit&);
56
57 fast_dynamic_bitset& get_split()
58 {
59 return split;
60 }
61
62 void reset()
63 {
64 split.reset();
65 }
66
67 void set(unsigned i)
68 {
69 split.set(i);
70 }
71
72 void reset(unsigned i)
73 {
74 split.reset(i);
75 }
76
77 bool test(unsigned i) const
78 {
79 return split.test(i);
80 }
81
82 void set(std::vector<int> vec)
83 {
84 int i=0, n=vec.size();
85
86 for (; i<n; ++i)
87 {
88 split.set(vec[i]);
89 }
90 }
91
92 void flip()
93 {
94 split.flip();
95 }
96
97 void flip(unsigned i)
98 {
99 split.flip(i);
100 }
101
102
103 unsigned count_taxa_in_ingroup()
104 {
105 return split.count();
106 }
107
108 unsigned size()
109 {
110 return split.size();
111 }
112
113 bool compatible_with(const CSplit& s_b) const
114 {
115 CSplit s_a_flipped(split);
116 CSplit s_b_flipped(s_b.split);
117 s_a_flipped.split.flip();
118 s_b_flipped.split.flip();
119
120 if( ((split & s_b.split).none()) || ((split & s_b_flipped.split).none()) ||
121 ((s_a_flipped.split & s_b.split).none()) || ((s_a_flipped.split & s_b_flipped.split).none()) )
122 return true;
123 else
124 return false;
125 }
126
127 // Returns true if all 1s in (*this) are also 1s in s_b
128 bool is_this_a_subset_of_the_parameter(const CSplit& s_b) const
129 {
130 CSplit s_b_flipped(s_b.split);
131 s_b_flipped.split.flip();
132
133 // Some debug code:
134 // std::cout << "*this & parameter_flipped: " << (split & s_b_flipped.split) << std::endl;
135
136 return (split & s_b_flipped.split).none();
137 }
138
139 // Returns true if all 1s in s_b are also 1s in (*this)
140 bool is_parameter_a_subset_of_this(const CSplit& s_b) const
141 {
142 CSplit s_a_flipped(split);
143 s_a_flipped.split.flip();
144
145 // Some debug code:
146 // std::cout << "*this flipped & parameter: " << (s_a_flipped.split & s_b.split) << std::endl;
147
148 return (s_a_flipped.split & s_b.split).none();
149 }
150
151 void print(std::ostream& os) const
152 {
153 os << split;
154 }
155
156 const std::string as_string_lowBitsFirst() const
157 {
158 return split.as_string_lowBitsFirst();
159 }
160
161 const std::string as_string_highBitsFirst() const
162 {
163 return split.as_string_highBitsFirst();
164 }
165
166 };
167
168
169 /* // Neue Reihenfolge der Argumente */
170 /* CSplit::CSplit(unsigned long len, const vector<unsigned long>& vec):split(len) { */
171 /* vector<unsigned long>::const_iterator it; */
172 /* it = vec.begin(); */
173 /* while( it != vec.end() ) { */
174 /* split.set(*it); //indexerror */
175 /* ++it; */
176 /* } */
177 /* } */
178
179 /* // Neue Reihenfolge der Argumente */
180 /* CSplit::CSplit(unsigned long len, unsigned long *u, unsigned long array_len):split(len) { */
181 /* split.clear(); */
182 /* int i = 0; */
183 /* while(i < array_len) { */
184 /* split.set(u[i]); //indexerror */
185 /* i++; */
186 /* *u++; */
187 /* } */
188 /* } */
189
190
191
192
193
194 inline bool operator ==(const CSplit& split_a, const CSplit& split_b) {
195 if ( split_a.split == split_b.split ) {
196 return true;
197 } else {
198 CSplit split_b_flipped(split_b);
199 split_b_flipped.split.flip();
200 if( split_a.split == split_b_flipped.split ) {
201 return true;
202 } else {
203 return false;
204 }
205 }
206 }
207
208 inline bool operator <(const CSplit& split_a, const CSplit& split_b)
209 {
210 if ( split_a.split < split_b.split ) {
211 return true;
212 }
213 else {
214 return false;
215 }
216 }
217
218 /* inline bool lessThan_mod_flip_friend(const CSplit& split_a, const CSplit& split_b) */
219 /* { */
220 /* CSplit s_a_flipped(split_a); */
221 /* CSplit s_b_flipped(split_b); */
222
223 /* s_a_flipped.split.flip(); */
224 /* s_b_flipped.split.flip(); */
225
226 /* // return (split_a < split_b) || (s_a_flipped < s_b_flipped); */
227 /* return (s_a_flipped < s_b_flipped); */
228 /* } */
229
230 /* struct lessThan_mod_flip */
231 /* { */
232 /* bool operator()(const CSplit& split_a, const CSplit& split_b) const */
233 /* { */
234 /* return lessThan_mod_flip_friend(split_a, split_b); */
235 /* } */
236 /* }; */
237
238 //
239 // Split with branch length:
240 //
241 class CSplit_l : public CSplit
242 {
243 double b_length;
244
245 public:
246
247 CSplit_l(unsigned long u):CSplit(u), b_length(-1)
248 {}
249
250 CSplit_l(const fast_dynamic_bitset& b, double bl = -1):CSplit(b), b_length(bl) {}
251
252 CSplit_l(const CSplit& b, double bl = -1):CSplit(b), b_length(bl){}
253
254 CSplit_l(const CSplit_l& b):CSplit(b), b_length(b.b_length){}
255
256
257
258 void set_b_length(double b_l)
259 {
260 b_length = b_l;
261 }
262
263 double get_b_length() const
264 {
265 return b_length;
266 }
267
268
269 };
270
271 #endif