| 1 |
/* BaitFisher (version 1.2.8) 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 CREQUIREDTAXA_H |
| 28 |
#define CREQUIREDTAXA_H |
| 29 |
|
| 30 |
#include "faststring2.h" |
| 31 |
#include <iostream> |
| 32 |
#include <fstream> |
| 33 |
#include <set> |
| 34 |
#include <vector> |
| 35 |
#include "CTaxonNamesDictionary.h" |
| 36 |
#include "print_container.h" |
| 37 |
|
| 38 |
class CRequiredTaxa |
| 39 |
{ |
| 40 |
std::vector<std::set<faststring> > required_groups; |
| 41 |
std::vector<std::set<unsigned> > required_groups_u; |
| 42 |
CTaxonNamesDictionary &dictionary; |
| 43 |
bool empty; |
| 44 |
|
| 45 |
bool sets_intersect(const set<unsigned> &s1, const set<unsigned> &s2) const |
| 46 |
{ |
| 47 |
// cout << "Set1" << endl; |
| 48 |
// print_set(cout, s1); |
| 49 |
// cout << endl; |
| 50 |
// cout << "Set2" << endl; |
| 51 |
// print_set(cout, s2); |
| 52 |
// cout << endl; |
| 53 |
|
| 54 |
unsigned size1 = s1.size(); |
| 55 |
unsigned size2 = s2.size(); |
| 56 |
|
| 57 |
unsigned size3 = macromin(size1, size2); |
| 58 |
|
| 59 |
// We allocate memory for the result set that will be stored in a vector. |
| 60 |
// Using allocated memory is more efficient than using an insert-iterator |
| 61 |
// that has more overhead. |
| 62 |
// Important: Later we have to shorten the vector to its actual size. |
| 63 |
vector<unsigned> res(size3); |
| 64 |
vector<unsigned>::iterator it; |
| 65 |
it=set_intersection (s1.begin(), s1.end(), s2.begin(), s2.end(), res.begin()); |
| 66 |
|
| 67 |
/* cout << "SET1: "; */ |
| 68 |
/* print_container(cout, s1, "(", ",", ")\n"); */ |
| 69 |
/* cout << "SET2: "; */ |
| 70 |
/* print_container(cout, s2, "(", ",", ")\n"); */ |
| 71 |
/* cout << "SET-RES1: "; */ |
| 72 |
/* print_container(cout, res, "(", ",", ")\n"); */ |
| 73 |
/* cout << "SIZE of intersection (A): " << res.size() << endl; */ |
| 74 |
|
| 75 |
// Shorten the vector to the actual size of the intersection. |
| 76 |
// Otherwise we have trailing 0s from the constructor of the vector |
| 77 |
// and the size of res would always be size3. |
| 78 |
res.resize(it-res.begin()); |
| 79 |
/* cout << "SIZE of intersection (B): " << res.size() << endl; */ |
| 80 |
|
| 81 |
/* cout << "SET-RES2: "; */ |
| 82 |
/* print_container(cout, res, "(", ",", ")\n"); */ |
| 83 |
|
| 84 |
return (res.size()>0); |
| 85 |
} |
| 86 |
|
| 87 |
// Do not allow gaps: |
| 88 |
void parse_required_groups(faststring line, int &error) |
| 89 |
{ |
| 90 |
if (line.empty()) |
| 91 |
{ |
| 92 |
cout << "LOG: Required taxa string is empty. No required taxa specified." << endl; |
| 93 |
empty = true; |
| 94 |
} |
| 95 |
|
| 96 |
// dictionary.print(cout); |
| 97 |
|
| 98 |
set<faststring> empty_set; |
| 99 |
set<unsigned> empty_set_u; |
| 100 |
|
| 101 |
unsigned i1, i2, N1, N2; |
| 102 |
|
| 103 |
vector<faststring> splitter1; |
| 104 |
vector<faststring> splitter2; |
| 105 |
|
| 106 |
split_respect(splitter1, line, ", "); |
| 107 |
|
| 108 |
N1 = splitter1.size(); |
| 109 |
|
| 110 |
for (i1=0; i1<N1; ++i1) |
| 111 |
{ |
| 112 |
required_groups.push_back(empty_set); |
| 113 |
required_groups_u.push_back(empty_set_u); |
| 114 |
|
| 115 |
// cout << splitter1[i] << endl; |
| 116 |
split(splitter2, splitter1[i1], "(, )"); |
| 117 |
N2 = splitter2.size(); |
| 118 |
for (i2=0; i2<N2; ++i2) |
| 119 |
{ |
| 120 |
splitter2[i2].removeSpacesBack(); |
| 121 |
splitter2[i2].removeSpacesFront(); |
| 122 |
required_groups[i1].insert(splitter2[i2]); |
| 123 |
unsigned index = dictionary.dictionary_get_index_of_taxonname(splitter2[i2]); |
| 124 |
if (index == -1u) |
| 125 |
{ |
| 126 |
std::cerr << "ERROR: Could not find taxon name " << splitter2[i2] |
| 127 |
<< " in the list of all taxon names found in all given sequence files." << std::endl; |
| 128 |
std::cerr << "Please check that the taxon name is spelled properly or remove the taxon name from the list of required" |
| 129 |
<< " taxa to proceed. Exiting." << std::endl; |
| 130 |
error = 3; |
| 131 |
} |
| 132 |
required_groups_u[i1].insert(index); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
public: |
| 139 |
// Error codes: |
| 140 |
// 0: No error, |
| 141 |
// 1: File does not contain a single line as required. We might have more or less than one line in the file. |
| 142 |
// 3: Sequence names not found in dictionary |
| 143 |
|
| 144 |
// The dummy parameter is only used to distinguish this and the following constructor, since both have the same parameter list |
| 145 |
// except of this dummy parameter. |
| 146 |
/* Deprecates. Required taxa have to be specified in the parameter file. |
| 147 |
CRequiredTaxa(faststring infilename, CTaxonNamesDictionary &dict, int &error, int dummy):dictionary(dict),empty(false) |
| 148 |
{ |
| 149 |
std::ifstream is(infilename.c_str()); |
| 150 |
|
| 151 |
error = 0; |
| 152 |
std::vector<faststring> file_as_vec; |
| 153 |
|
| 154 |
appendFileToVector(is, file_as_vec); |
| 155 |
|
| 156 |
unsigned i; |
| 157 |
i=0; |
| 158 |
while (i< file_as_vec.size() ) |
| 159 |
{ |
| 160 |
file_as_vec[i].removeSpacesFront(); |
| 161 |
file_as_vec[i].removeSpacesBack(); |
| 162 |
if (file_as_vec[i].empty() ) |
| 163 |
{ |
| 164 |
file_as_vec.erase(file_as_vec.begin()+i); |
| 165 |
} |
| 166 |
else |
| 167 |
{ |
| 168 |
++i; |
| 169 |
} |
| 170 |
} |
| 171 |
if (file_as_vec.size() != 1) |
| 172 |
{ |
| 173 |
error = 1; |
| 174 |
return; |
| 175 |
} |
| 176 |
parse_required_groups(file_as_vec[0], error); |
| 177 |
} |
| 178 |
*/ |
| 179 |
|
| 180 |
// Error codes: |
| 181 |
// 0: No error, |
| 182 |
// 1: -, unused |
| 183 |
// 3: Sequence names not found in dictionary |
| 184 |
CRequiredTaxa(faststring required_string, CTaxonNamesDictionary &dict, int &error):dictionary(dict),empty(false) |
| 185 |
{ |
| 186 |
error = 0; |
| 187 |
parse_required_groups(required_string, error); |
| 188 |
} |
| 189 |
|
| 190 |
bool is_empty() const |
| 191 |
{ |
| 192 |
return empty; |
| 193 |
} |
| 194 |
|
| 195 |
bool check_precense_of_all_groups_of_taxa(std::vector<faststring> taxon_vec) const |
| 196 |
{ |
| 197 |
if (empty) |
| 198 |
return true; |
| 199 |
|
| 200 |
std::set<unsigned> taxon_set; |
| 201 |
|
| 202 |
unsigned i, N, index; |
| 203 |
for (i=0, N=taxon_vec.size(); i < N; ++i) |
| 204 |
{ |
| 205 |
index = dictionary.dictionary_get_index_of_taxonname(taxon_vec[i]); |
| 206 |
taxon_set.insert(index); |
| 207 |
} |
| 208 |
|
| 209 |
// v_set must have a non vanishing intersection with all groups of taxa that are required. |
| 210 |
|
| 211 |
unsigned iset, Nset; |
| 212 |
bool all_taxa_present = true; |
| 213 |
|
| 214 |
for (iset=0, Nset=required_groups_u.size();iset < Nset; ++iset) |
| 215 |
{ |
| 216 |
if ( !sets_intersect(taxon_set, required_groups_u[iset] ) ) |
| 217 |
{ |
| 218 |
// cout << " Sets do not intersect: " << endl; |
| 219 |
// print_set(cout, taxon_set); cout << endl; |
| 220 |
// print_set(cout, required_groups_u[iset]); cout << endl; |
| 221 |
all_taxa_present = false; |
| 222 |
break; |
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
return all_taxa_present; |
| 227 |
} |
| 228 |
|
| 229 |
bool check_precense_of_all_groups_of_taxa(std::set<unsigned> taxon_ids) const |
| 230 |
{ |
| 231 |
if (empty) |
| 232 |
return true; |
| 233 |
|
| 234 |
// taxon_ids must have a non vanishing intersection with all groups of taxa that are required. |
| 235 |
|
| 236 |
unsigned iset, Nset; |
| 237 |
bool all_taxa_present = true; |
| 238 |
|
| 239 |
for (iset=0, Nset=required_groups_u.size();iset < Nset; ++iset) |
| 240 |
{ |
| 241 |
if ( !sets_intersect(taxon_ids, required_groups_u[iset] ) ) |
| 242 |
{ |
| 243 |
// cout << " Sets do not intersect: " << endl; |
| 244 |
// print_set(cout, taxon_set); cout << endl; |
| 245 |
// print_set(cout, required_groups_u[iset]); cout << endl; |
| 246 |
all_taxa_present = false; |
| 247 |
break; |
| 248 |
} |
| 249 |
} |
| 250 |
return all_taxa_present; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
void print(std::ostream &os, int mode = 0) |
| 257 |
{ |
| 258 |
std::vector<std::set<faststring> >::iterator it_vec_faststrings; |
| 259 |
std::vector<std::set<unsigned> >::iterator it_vec_unsigned; |
| 260 |
|
| 261 |
std::set<faststring>::iterator it_set_faststrings; |
| 262 |
std::set<unsigned>::iterator it_set_unsigned; |
| 263 |
|
| 264 |
it_vec_faststrings = required_groups.begin(); |
| 265 |
|
| 266 |
if (empty) |
| 267 |
{ |
| 268 |
os << "CRequiredTaxa: empty" << endl; |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
while (it_vec_faststrings != required_groups.end()) |
| 273 |
{ |
| 274 |
os << "("; |
| 275 |
it_set_faststrings = it_vec_faststrings->begin(); |
| 276 |
while (it_set_faststrings != it_vec_faststrings->end() ) |
| 277 |
{ |
| 278 |
os << *it_set_faststrings; |
| 279 |
++it_set_faststrings; |
| 280 |
if (it_set_faststrings != it_vec_faststrings->end()) |
| 281 |
os << ","; |
| 282 |
} |
| 283 |
++it_vec_faststrings; |
| 284 |
os << "),"; |
| 285 |
if (mode == 1) |
| 286 |
os << endl; |
| 287 |
} |
| 288 |
|
| 289 |
it_vec_unsigned = required_groups_u.begin(); |
| 290 |
|
| 291 |
os << "*********" << endl; |
| 292 |
|
| 293 |
while (it_vec_unsigned != required_groups_u.end()) |
| 294 |
{ |
| 295 |
os << "("; |
| 296 |
it_set_unsigned = it_vec_unsigned->begin(); |
| 297 |
while (it_set_unsigned != it_vec_unsigned->end() ) |
| 298 |
{ |
| 299 |
os << *it_set_unsigned; |
| 300 |
++it_set_unsigned; |
| 301 |
if (it_set_unsigned != it_vec_unsigned->end()) |
| 302 |
os << ","; |
| 303 |
} |
| 304 |
++it_vec_unsigned; |
| 305 |
os << "),"; |
| 306 |
if (mode == 1) |
| 307 |
os << endl; |
| 308 |
} |
| 309 |
|
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
}; |
| 314 |
|
| 315 |
#endif |