-
Notifications
You must be signed in to change notification settings - Fork 7
/
CRequiredTaxon.h
315 lines (262 loc) · 8.67 KB
/
CRequiredTaxon.h
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* BaitFisher (version 1.2.8) a program for designing DNA target enrichment baits
* Copyright 2013-2016 by Christoph Mayer
*
* This source file is part of the BaitFisher-package.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BaitFisher. If not, see <http://www.gnu.org/licenses/>.
*
*
* For any enquiries send an Email to Christoph Mayer
*
* When publishing work that is based on the results please cite:
* Mayer et al. 2016: BaitFisher: A software package for multi-species target DNA enrichment probe design
*
*/
#ifndef CREQUIREDTAXA_H
#define CREQUIREDTAXA_H
#include "faststring2.h"
#include <iostream>
#include <fstream>
#include <set>
#include <vector>
#include "CTaxonNamesDictionary.h"
#include "print_container.h"
class CRequiredTaxa
{
std::vector<std::set<faststring> > required_groups;
std::vector<std::set<unsigned> > required_groups_u;
CTaxonNamesDictionary &dictionary;
bool empty;
bool sets_intersect(const set<unsigned> &s1, const set<unsigned> &s2) const
{
// cout << "Set1" << endl;
// print_set(cout, s1);
// cout << endl;
// cout << "Set2" << endl;
// print_set(cout, s2);
// cout << endl;
unsigned size1 = s1.size();
unsigned size2 = s2.size();
unsigned size3 = macromin(size1, size2);
// We allocate memory for the result set that will be stored in a vector.
// Using allocated memory is more efficient than using an insert-iterator
// that has more overhead.
// Important: Later we have to shorten the vector to its actual size.
vector<unsigned> res(size3);
vector<unsigned>::iterator it;
it=set_intersection (s1.begin(), s1.end(), s2.begin(), s2.end(), res.begin());
/* cout << "SET1: "; */
/* print_container(cout, s1, "(", ",", ")\n"); */
/* cout << "SET2: "; */
/* print_container(cout, s2, "(", ",", ")\n"); */
/* cout << "SET-RES1: "; */
/* print_container(cout, res, "(", ",", ")\n"); */
/* cout << "SIZE of intersection (A): " << res.size() << endl; */
// Shorten the vector to the actual size of the intersection.
// Otherwise we have trailing 0s from the constructor of the vector
// and the size of res would always be size3.
res.resize(it-res.begin());
/* cout << "SIZE of intersection (B): " << res.size() << endl; */
/* cout << "SET-RES2: "; */
/* print_container(cout, res, "(", ",", ")\n"); */
return (res.size()>0);
}
// Do not allow gaps:
void parse_required_groups(faststring line, int &error)
{
if (line.empty())
{
cout << "LOG: Required taxa string is empty. No required taxa specified." << endl;
empty = true;
}
// dictionary.print(cout);
set<faststring> empty_set;
set<unsigned> empty_set_u;
unsigned i1, i2, N1, N2;
vector<faststring> splitter1;
vector<faststring> splitter2;
split_respect(splitter1, line, ", ");
N1 = splitter1.size();
for (i1=0; i1<N1; ++i1)
{
required_groups.push_back(empty_set);
required_groups_u.push_back(empty_set_u);
// cout << splitter1[i] << endl;
split(splitter2, splitter1[i1], "(, )");
N2 = splitter2.size();
for (i2=0; i2<N2; ++i2)
{
splitter2[i2].removeSpacesBack();
splitter2[i2].removeSpacesFront();
required_groups[i1].insert(splitter2[i2]);
unsigned index = dictionary.dictionary_get_index_of_taxonname(splitter2[i2]);
if (index == -1u)
{
std::cerr << "ERROR: Could not find taxon name " << splitter2[i2]
<< " in the list of all taxon names found in all given sequence files." << std::endl;
std::cerr << "Please check that the taxon name is spelled properly or remove the taxon name from the list of required"
<< " taxa to proceed. Exiting." << std::endl;
error = 3;
}
required_groups_u[i1].insert(index);
}
}
}
public:
// Error codes:
// 0: No error,
// 1: File does not contain a single line as required. We might have more or less than one line in the file.
// 3: Sequence names not found in dictionary
// The dummy parameter is only used to distinguish this and the following constructor, since both have the same parameter list
// except of this dummy parameter.
/* Deprecates. Required taxa have to be specified in the parameter file.
CRequiredTaxa(faststring infilename, CTaxonNamesDictionary &dict, int &error, int dummy):dictionary(dict),empty(false)
{
std::ifstream is(infilename.c_str());
error = 0;
std::vector<faststring> file_as_vec;
appendFileToVector(is, file_as_vec);
unsigned i;
i=0;
while (i< file_as_vec.size() )
{
file_as_vec[i].removeSpacesFront();
file_as_vec[i].removeSpacesBack();
if (file_as_vec[i].empty() )
{
file_as_vec.erase(file_as_vec.begin()+i);
}
else
{
++i;
}
}
if (file_as_vec.size() != 1)
{
error = 1;
return;
}
parse_required_groups(file_as_vec[0], error);
}
*/
// Error codes:
// 0: No error,
// 1: -, unused
// 3: Sequence names not found in dictionary
CRequiredTaxa(faststring required_string, CTaxonNamesDictionary &dict, int &error):dictionary(dict),empty(false)
{
error = 0;
parse_required_groups(required_string, error);
}
bool is_empty() const
{
return empty;
}
bool check_precense_of_all_groups_of_taxa(std::vector<faststring> taxon_vec) const
{
if (empty)
return true;
std::set<unsigned> taxon_set;
unsigned i, N, index;
for (i=0, N=taxon_vec.size(); i < N; ++i)
{
index = dictionary.dictionary_get_index_of_taxonname(taxon_vec[i]);
taxon_set.insert(index);
}
// v_set must have a non vanishing intersection with all groups of taxa that are required.
unsigned iset, Nset;
bool all_taxa_present = true;
for (iset=0, Nset=required_groups_u.size();iset < Nset; ++iset)
{
if ( !sets_intersect(taxon_set, required_groups_u[iset] ) )
{
// cout << " Sets do not intersect: " << endl;
// print_set(cout, taxon_set); cout << endl;
// print_set(cout, required_groups_u[iset]); cout << endl;
all_taxa_present = false;
break;
}
}
return all_taxa_present;
}
bool check_precense_of_all_groups_of_taxa(std::set<unsigned> taxon_ids) const
{
if (empty)
return true;
// taxon_ids must have a non vanishing intersection with all groups of taxa that are required.
unsigned iset, Nset;
bool all_taxa_present = true;
for (iset=0, Nset=required_groups_u.size();iset < Nset; ++iset)
{
if ( !sets_intersect(taxon_ids, required_groups_u[iset] ) )
{
// cout << " Sets do not intersect: " << endl;
// print_set(cout, taxon_set); cout << endl;
// print_set(cout, required_groups_u[iset]); cout << endl;
all_taxa_present = false;
break;
}
}
return all_taxa_present;
}
void print(std::ostream &os, int mode = 0)
{
std::vector<std::set<faststring> >::iterator it_vec_faststrings;
std::vector<std::set<unsigned> >::iterator it_vec_unsigned;
std::set<faststring>::iterator it_set_faststrings;
std::set<unsigned>::iterator it_set_unsigned;
it_vec_faststrings = required_groups.begin();
if (empty)
{
os << "CRequiredTaxa: empty" << endl;
return;
}
while (it_vec_faststrings != required_groups.end())
{
os << "(";
it_set_faststrings = it_vec_faststrings->begin();
while (it_set_faststrings != it_vec_faststrings->end() )
{
os << *it_set_faststrings;
++it_set_faststrings;
if (it_set_faststrings != it_vec_faststrings->end())
os << ",";
}
++it_vec_faststrings;
os << "),";
if (mode == 1)
os << endl;
}
it_vec_unsigned = required_groups_u.begin();
os << "*********" << endl;
while (it_vec_unsigned != required_groups_u.end())
{
os << "(";
it_set_unsigned = it_vec_unsigned->begin();
while (it_set_unsigned != it_vec_unsigned->end() )
{
os << *it_set_unsigned;
++it_set_unsigned;
if (it_set_unsigned != it_vec_unsigned->end())
os << ",";
}
++it_vec_unsigned;
os << "),";
if (mode == 1)
os << endl;
}
}
};
#endif