| 1 |
/* BaitFisher (version 1.2.8) a program for designing DNA target enrichment baits |
| 2 |
* BaitFilter (version 1.0.6) a program for selecting optimal bait regions |
| 3 |
* Copyright 2013-2017 by Christoph Mayer |
| 4 |
* |
| 5 |
* This source file is part of the BaitFisher-package. |
| 6 |
* |
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation, either version 3 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with BaitFisher. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
* |
| 20 |
* |
| 21 |
* For any enquiries send an Email to Christoph Mayer |
| 22 |
* [email protected] |
| 23 |
* |
| 24 |
* When publishing work that is based on the results please cite: |
| 25 |
* Mayer et al. 2016: BaitFisher: A software package for multi-species target DNA enrichment probe design |
| 26 |
* |
| 27 |
*/ |
| 28 |
#ifndef CSEQNAMELIST_H |
| 29 |
#define CSEQNAMELIST_H |
| 30 |
|
| 31 |
|
| 32 |
#include <iostream> |
| 33 |
#include <fstream> |
| 34 |
#include "faststring2.h" |
| 35 |
#include <map> |
| 36 |
#include <set> |
| 37 |
#include "CFile/CFile2_1.h" |
| 38 |
#include "CSequence_Mol2_1.h" |
| 39 |
#include <iterator> |
| 40 |
#include <algorithm> |
| 41 |
#include <climits> |
| 42 |
|
| 43 |
inline void set_to_short_name(faststring &str) |
| 44 |
{ |
| 45 |
faststring::size_t pos = str.find(' '); |
| 46 |
if (pos != faststring::npos) |
| 47 |
str.shorten(pos); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
class CSeqNameList |
| 52 |
{ |
| 53 |
// A note on memory management. |
| 54 |
// This class collects names and information about sequences. |
| 55 |
// Sequence names are stored in faststrings. The class keeps pointers to these faststrings. |
| 56 |
// The class keeps a copy of the original string it obtained. Therefore, it allocates memory |
| 57 |
// for the strings and it has to release the memory when they are not used any more. |
| 58 |
|
| 59 |
// When removing entries there vector position is set to "empty" values. |
| 60 |
// They are not removed entirely, since this would be too inefficient. |
| 61 |
// Removed entries are removed from the map. |
| 62 |
// In particular: m.size() provides the number of valid entries. |
| 63 |
// For every change in this file: Check that this is handled consistent. |
| 64 |
|
| 65 |
public: |
| 66 |
typedef std::map<faststring*, unsigned, less_than_pointer_to_faststring_struct> myMap; |
| 67 |
typedef std::set<faststring> mySet; |
| 68 |
typedef myMap::iterator CSeqNameListIterator; |
| 69 |
typedef mySet::iterator mySetIterator; |
| 70 |
|
| 71 |
private: |
| 72 |
faststring infile_name; |
| 73 |
bool only_names; // Formerly, all vectors had been initialised to ensure that all fields are accessible. |
| 74 |
// This is not the case any more. If only_names is true, the vectors defined below can be empty. |
| 75 |
// TODO: Check that this is handled consistently. |
| 76 |
|
| 77 |
myMap m; // maps sequence names to id's. id's are indices used to refer to the other sequence data. |
| 78 |
std::vector<faststring*> full_names; |
| 79 |
std::vector<unsigned> seq_len_vec; |
| 80 |
std::vector<double> CG_content; |
| 81 |
std::vector<unsigned> corrected_len_vec; |
| 82 |
std::vector<unsigned> repeat_density_bp_mbp; |
| 83 |
|
| 84 |
void add_unchecked(const faststring &full_name, unsigned sl, double CG_c, unsigned cl, unsigned rdens) |
| 85 |
{ |
| 86 |
// The basic add routine creates a copy of the sequence name that shall be added. |
| 87 |
faststring *tmp = new faststring(full_name); |
| 88 |
|
| 89 |
full_names.push_back(tmp); |
| 90 |
seq_len_vec.push_back(sl); |
| 91 |
CG_content.push_back(CG_c); |
| 92 |
corrected_len_vec.push_back(cl); |
| 93 |
repeat_density_bp_mbp.push_back(rdens); |
| 94 |
|
| 95 |
faststring::size_t pos = full_names.size()-1; |
| 96 |
|
| 97 |
// std::cerr << "Adding new seq: Name " << *tmp << " pos " << pos << " len " << sl << " CG " << CG_c << " cl " << cl<< std::endl; |
| 98 |
|
| 99 |
m[tmp] = pos; |
| 100 |
} |
| 101 |
|
| 102 |
void add_only_name_unchecked(const char *full_name) |
| 103 |
{ |
| 104 |
faststring *tmp = new faststring(full_name); |
| 105 |
full_names.push_back(tmp); |
| 106 |
|
| 107 |
faststring::size_t pos = full_names.size()-1; |
| 108 |
m[tmp] = pos; |
| 109 |
} |
| 110 |
|
| 111 |
public: |
| 112 |
|
| 113 |
bool add_only_name_non_redundant(const char *full_name) |
| 114 |
{ |
| 115 |
if (get_id_short_name(*full_name) == UINT_MAX ) |
| 116 |
{ |
| 117 |
add_only_name_unchecked( full_name ); |
| 118 |
return true; |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
std::cerr << "Detected previous entry of " << full_name << std::endl; |
| 123 |
return false; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/* void add(const CSeqNameList &snl, unsigned id) */ |
| 128 |
/* { */ |
| 129 |
/* if (snl.only_names) */ |
| 130 |
/* add_only_name( snl.full_names[id]->c_str() ); */ |
| 131 |
/* else */ |
| 132 |
/* add(*(snl.full_names[id]), snl.seq_len_vec[id], snl.CG_content[id], snl.corrected_len_vec[id], snl.repeat_density_bp_mbp[id]); */ |
| 133 |
/* } */ |
| 134 |
|
| 135 |
bool add_non_redundant(const faststring &full_name, unsigned sl, double CG_c, unsigned cl, unsigned rdens) |
| 136 |
{ |
| 137 |
if (get_id_short_name(full_name) == UINT_MAX) |
| 138 |
{ |
| 139 |
add_unchecked(full_name, sl, CG_c, cl, rdens); |
| 140 |
return true; |
| 141 |
} |
| 142 |
else |
| 143 |
{ |
| 144 |
std::cerr << "Detected previous entry of " << full_name << ". Name has not been added again." << std::endl; |
| 145 |
return false; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
bool add_non_redundant(const CSeqNameList &snl, unsigned id) |
| 151 |
{ |
| 152 |
if (!snl.only_names) |
| 153 |
{ |
| 154 |
if (get_id_short_name(*(snl.full_names[id])) == UINT_MAX) |
| 155 |
{ |
| 156 |
add_unchecked(*(snl.full_names[id]), snl.seq_len_vec[id], snl.CG_content[id], snl.corrected_len_vec[id], snl.repeat_density_bp_mbp[id]); |
| 157 |
return true; |
| 158 |
} |
| 159 |
else |
| 160 |
{ |
| 161 |
std::cerr << "Detected previous entry of " << *snl.full_names[id] << std::endl; |
| 162 |
return false; |
| 163 |
} |
| 164 |
} |
| 165 |
else // only_names |
| 166 |
{ |
| 167 |
if (get_id_short_name(*(snl.full_names[id])) == UINT_MAX) |
| 168 |
{ |
| 169 |
add_only_name_unchecked( snl.full_names[id]->c_str() ); |
| 170 |
return true; |
| 171 |
} |
| 172 |
else |
| 173 |
{ |
| 174 |
std::cerr << "Detected previous entry of " << *snl.full_names[id] << std::endl; |
| 175 |
return false; |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
void reset() |
| 182 |
{ |
| 183 |
// Release memory. Otherwise we have a memory leak; |
| 184 |
unsigned i=0, n=full_names.size(); |
| 185 |
while (i<n) |
| 186 |
{ |
| 187 |
delete full_names[i]; |
| 188 |
++i; |
| 189 |
} |
| 190 |
|
| 191 |
infile_name.clear(); |
| 192 |
only_names = false; |
| 193 |
m.clear(); |
| 194 |
full_names.clear(); |
| 195 |
seq_len_vec.clear(); |
| 196 |
CG_content.clear(); |
| 197 |
repeat_density_bp_mbp.clear(); |
| 198 |
corrected_len_vec.clear(); |
| 199 |
} |
| 200 |
|
| 201 |
// This version is the most efficient and should be used if alternative versions can be used. |
| 202 |
// User has to check that it is an iterator into the map m. |
| 203 |
void remove(CSeqNameListIterator it) |
| 204 |
{ |
| 205 |
unsigned index = it->second; |
| 206 |
// We do not entirely remove these entries, since |
| 207 |
// this is too inefficient. |
| 208 |
// The vectors still contain the element, but it is empty. |
| 209 |
// The element is removed from the map. |
| 210 |
if (!only_names) |
| 211 |
{ |
| 212 |
seq_len_vec[index] = 0; |
| 213 |
CG_content[index] = 0; |
| 214 |
corrected_len_vec[index] = 0; |
| 215 |
repeat_density_bp_mbp[index] = 0; |
| 216 |
} |
| 217 |
|
| 218 |
*(full_names[index]) = ""; |
| 219 |
m.erase(it); // Erase this element out of the map. |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
// remove entry from the "list". |
| 224 |
void remove(unsigned index) |
| 225 |
{ |
| 226 |
CSeqNameListIterator it; |
| 227 |
|
| 228 |
if (index >= full_names.size()) |
| 229 |
{ |
| 230 |
std::cerr << "Internal error: Call to CSeqNameList::remove(unsigned index) but the index is out-of range." << std::endl; |
| 231 |
exit(-33); |
| 232 |
} |
| 233 |
|
| 234 |
it = m.find(full_names[index]); // We pass a pointer here. Remember that we use less_than_pointer_to_faststring_struct to compare the elements, so in the end we search for the string and not for the pointer. |
| 235 |
|
| 236 |
if (it == m.end()) |
| 237 |
{ |
| 238 |
std::cerr << "Internal error: Call to CSeqNameList::remove(unsigned index) but the index the iterator found with m.find does not exist." << std::endl; |
| 239 |
exit(-33); |
| 240 |
} |
| 241 |
else |
| 242 |
{ |
| 243 |
remove(it); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
void remove(faststring &full_name) |
| 249 |
{ |
| 250 |
CSeqNameListIterator it; |
| 251 |
it = m.find(&full_name); // We have to take the address operator here. Remember that we use less_than_pointer_to_faststring_struct to compare the elements, so in the end we search for the string and not for the pointer. |
| 252 |
|
| 253 |
if (it == m.end()) |
| 254 |
{ |
| 255 |
std::cerr << "Internal error: Call to CSeqNameList::remove(faststring &full_name) but full_name does not exist in map m." << std::endl; |
| 256 |
exit(-33); |
| 257 |
} |
| 258 |
|
| 259 |
remove(it); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
// Import sequence names from a fasta file |
| 264 |
CSeqNameList(const char *fasta_file_name, bool read_only_names=false):infile_name(fasta_file_name), only_names(read_only_names) |
| 265 |
{ |
| 266 |
CFile infile; |
| 267 |
|
| 268 |
infile.ffopen(fasta_file_name); |
| 269 |
|
| 270 |
if ( !infile.exists() ) |
| 271 |
{ |
| 272 |
std::cerr << "Sorry! The input file \n \""; |
| 273 |
std::cerr << fasta_file_name; |
| 274 |
std::cerr << "\"\ncould not be opened. I guess it does not exist.\n"; |
| 275 |
exit (-101); |
| 276 |
} |
| 277 |
|
| 278 |
CSequence_Mol seq(CSequence_Mol::unknown); |
| 279 |
|
| 280 |
if (!only_names) |
| 281 |
{ |
| 282 |
while (!infile.eof()) |
| 283 |
{ |
| 284 |
seq.readRawFastaSequence(infile); |
| 285 |
if ( infile.fail() && !infile.eof() ) |
| 286 |
{ |
| 287 |
std::cerr << "\n\n"; |
| 288 |
std::cerr << "ERROR: A problem was detected while reading the input file \""<< fasta_file_name << "\". The file might be empty or it might not be a valid fasta file.\n"; |
| 289 |
std::cerr << "File position: line "; |
| 290 |
std::cerr << faststring(infile.line()) << std::endl; |
| 291 |
std::cerr << std::flush; |
| 292 |
exit(-7); |
| 293 |
} |
| 294 |
else |
| 295 |
{ |
| 296 |
faststring name_to_add = seq.getFullName(); |
| 297 |
|
| 298 |
// std::cerr << "Processing sequence: " << name_to_add << std::endl; |
| 299 |
|
| 300 |
seq.compute_numbers_of_residues(); |
| 301 |
|
| 302 |
// unsigned Ambigs = seq.get_number_of_DNARNA_ambigs(); |
| 303 |
unsigned bases = seq.get_number_of_DNARNA_bases(); |
| 304 |
double CGc = seq.get_CG_content(); |
| 305 |
unsigned rdens = 0; |
| 306 |
|
| 307 |
// std::cerr << "Adding 2: " << name_to_add << " slen: " << seq.length() << " CG: " << CGc << " bases: " << bases << std::endl; |
| 308 |
|
| 309 |
bool non_redundant = add_non_redundant(name_to_add, seq.length(), CGc, bases, rdens); |
| 310 |
|
| 311 |
if (!non_redundant) |
| 312 |
{ |
| 313 |
std::cerr << "Exiting due to non unique sequence names." << std::endl; |
| 314 |
exit(-1); |
| 315 |
} |
| 316 |
} |
| 317 |
} // End while |
| 318 |
} |
| 319 |
else // Read only names: |
| 320 |
{ |
| 321 |
while (!infile.eof()) |
| 322 |
{ |
| 323 |
seq.readSeqName_ignore_Sequence_data(infile); |
| 324 |
if ( infile.fail() && !infile.eof() ) |
| 325 |
{ |
| 326 |
std::cerr << "\n\n"; |
| 327 |
std::cerr << "ERROR: A problem was detected while reading the input file \""<< fasta_file_name << "\". The file might be empty or it might not be a valid fasta file.\n"; |
| 328 |
std::cerr << "File position: line "; |
| 329 |
std::cerr << faststring(infile.line()) << std::endl; |
| 330 |
std::cerr << std::flush; |
| 331 |
exit(-7); |
| 332 |
// break; // Stop reading this file. |
| 333 |
} |
| 334 |
else |
| 335 |
{ |
| 336 |
const char * name_to_add = seq.getFullName(); |
| 337 |
// std::cerr << "Processing sequence: " << name_to_add << std::endl; |
| 338 |
bool non_redundant = add_only_name_non_redundant(name_to_add); |
| 339 |
|
| 340 |
if (!non_redundant) |
| 341 |
{ |
| 342 |
std::cerr << "Exiting due to non unique sequence names." << std::endl; |
| 343 |
exit(-1); |
| 344 |
} |
| 345 |
} |
| 346 |
} // End while |
| 347 |
} // End else only_names |
| 348 |
|
| 349 |
infile.ffclose(); |
| 350 |
} // END constructor |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
// Import sequence names from a text file: |
| 355 |
CSeqNameList(const char *text_file_name, int dummy):infile_name(text_file_name), only_names(true) |
| 356 |
{ |
| 357 |
CFile infile; |
| 358 |
faststring name_to_add; |
| 359 |
(void) dummy; |
| 360 |
|
| 361 |
infile.ffopen(text_file_name); |
| 362 |
|
| 363 |
if ( !infile.exists() ) |
| 364 |
{ |
| 365 |
std::cerr << "Sorry! The input file \n \""; |
| 366 |
std::cerr << text_file_name; |
| 367 |
std::cerr << "\"\ncould not be opened. I guess it does not exist.\n"; |
| 368 |
exit (-101); |
| 369 |
} |
| 370 |
|
| 371 |
infile.getline(name_to_add); |
| 372 |
while (!infile.eof()) |
| 373 |
{ |
| 374 |
// std::cerr << "Processing sequence: " << name_to_add << std::endl; |
| 375 |
bool non_redundant = add_only_name_non_redundant(name_to_add.c_str()); |
| 376 |
|
| 377 |
if (!non_redundant) |
| 378 |
{ |
| 379 |
std::cerr << "Exiting due to non unique sequence names." << std::endl; |
| 380 |
exit(-1); |
| 381 |
} |
| 382 |
|
| 383 |
infile.getline(name_to_add); |
| 384 |
} // End while |
| 385 |
|
| 386 |
infile.ffclose(); |
| 387 |
} // END constructor |
| 388 |
|
| 389 |
|
| 390 |
// Default constructor |
| 391 |
CSeqNameList(bool read_only_names=false):infile_name(""), only_names(read_only_names) |
| 392 |
{} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
// File must be of the form: |
| 398 |
// sequence name\tdenstiy in bp/Mbp |
| 399 |
void add_repeat_densities(const char * filename) |
| 400 |
{ |
| 401 |
std::ifstream is(filename); |
| 402 |
faststring line; |
| 403 |
std::vector<faststring> vf; |
| 404 |
faststring sn; |
| 405 |
unsigned rdens; |
| 406 |
unsigned id; |
| 407 |
|
| 408 |
if (only_names) |
| 409 |
{ |
| 410 |
std::cerr << "Internal error: Reading repeat densities for names only sequence list.\n"; |
| 411 |
exit(-22); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
getline(is, line); |
| 416 |
while (is) |
| 417 |
{ |
| 418 |
split(vf, line, "\t"); |
| 419 |
if (vf.size()!=2) |
| 420 |
{ |
| 421 |
std::cerr << "Critical error when adding repeat densities. Line " << line << " not a valid input.\n"; |
| 422 |
exit(-21); |
| 423 |
} |
| 424 |
sn = vf[0]; |
| 425 |
rdens = vf[1].ToUnsigned(); |
| 426 |
|
| 427 |
id = get_id_short_name(sn); |
| 428 |
|
| 429 |
repeat_density_bp_mbp[id] = rdens; |
| 430 |
|
| 431 |
getline(is, line); |
| 432 |
} |
| 433 |
is.close(); |
| 434 |
} |
| 435 |
|
| 436 |
~CSeqNameList() |
| 437 |
{ |
| 438 |
/* unsigned i=0, n=full_names.size(); */ |
| 439 |
|
| 440 |
/* while (i<n) */ |
| 441 |
/* { */ |
| 442 |
/* delete full_names[i]; */ |
| 443 |
/* ++i; */ |
| 444 |
/* } */ |
| 445 |
reset(); |
| 446 |
} // END destructor |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
void use_list_as_filter(const char *fasta_file_name, |
| 453 |
const char *out_in_list, |
| 454 |
const char *out_not_in_list, |
| 455 |
unsigned char_per_line=50) |
| 456 |
{ |
| 457 |
CFile infile; |
| 458 |
|
| 459 |
infile.ffopen(fasta_file_name); |
| 460 |
|
| 461 |
if ( !infile.exists() ) |
| 462 |
{ |
| 463 |
std::cerr << "Sorry! The input file \n \""; |
| 464 |
std::cerr << fasta_file_name; |
| 465 |
std::cerr << "\"\ncould not be opened. I guess it does not exist.\n"; |
| 466 |
exit (-101); |
| 467 |
} |
| 468 |
|
| 469 |
FILE *os_in_list; // (out_in_list); |
| 470 |
FILE *os_not_in_list; // (out_not_in_list); |
| 471 |
|
| 472 |
os_in_list = fopen(out_in_list, "w"); |
| 473 |
os_not_in_list = fopen(out_not_in_list, "w"); |
| 474 |
|
| 475 |
CSequence_Mol seq(CSequence_Mol::unknown); |
| 476 |
while (!infile.eof()) |
| 477 |
{ |
| 478 |
seq.readRawFastaSequence(infile); |
| 479 |
if ( infile.fail() && !infile.eof() ) |
| 480 |
{ |
| 481 |
std::cerr << "\n\n"; |
| 482 |
std::cerr << "An error occurred while reading the input file. It might not be a valid fasta file.\n"; |
| 483 |
std::cerr << "File position: line "; |
| 484 |
std::cerr << faststring(infile.line()) << std::endl; |
| 485 |
std::cerr << std::flush; |
| 486 |
} |
| 487 |
|
| 488 |
faststring fname = seq.getFullName(); |
| 489 |
|
| 490 |
CSeqNameListIterator it; |
| 491 |
it = m.find(&fname); // We have to take the address operator here. Remember that we use less_than_pointer_to_faststring_struct to compare the elements, so we do no search for the pointer but for the string indeed. |
| 492 |
|
| 493 |
if (it != m.end() ) |
| 494 |
{ |
| 495 |
seq.writeSequence_fasta(os_in_list, char_per_line); |
| 496 |
} |
| 497 |
else |
| 498 |
{ |
| 499 |
seq.writeSequence_fasta(os_not_in_list, char_per_line); |
| 500 |
} |
| 501 |
|
| 502 |
} // END while |
| 503 |
|
| 504 |
infile.ffclose(); |
| 505 |
fclose(os_in_list); |
| 506 |
fclose(os_not_in_list); |
| 507 |
|
| 508 |
} // END use_list_as_filter |
| 509 |
|
| 510 |
|
| 511 |
// Reads the specified fasta file. |
| 512 |
// If the sequence name is found in this name list write it to output file. |
| 513 |
// XXXXXXXXXXXX TODO: return value: number of sequences with not only sequence name but also sequence info. |
| 514 |
void write_sequences_in_list(const char *fasta_file_name, |
| 515 |
const char *out_in_list, |
| 516 |
const char *write_mode) |
| 517 |
{ |
| 518 |
CFile infile; |
| 519 |
|
| 520 |
infile.ffopen(fasta_file_name); |
| 521 |
|
| 522 |
if ( !infile.exists() ) |
| 523 |
{ |
| 524 |
std::cerr << "Sorry! The input file \n \""; |
| 525 |
std::cerr << fasta_file_name; |
| 526 |
std::cerr << "\"\ncould not be opened. I guess it does not exist.\n"; |
| 527 |
exit (-101); |
| 528 |
} |
| 529 |
|
| 530 |
FILE *os_in_list; // (out_in_list); |
| 531 |
unsigned line_length; |
| 532 |
|
| 533 |
os_in_list = fopen(out_in_list, write_mode); |
| 534 |
|
| 535 |
CSequence_Mol seq(CSequence_Mol::unknown); |
| 536 |
while (!infile.eof()) |
| 537 |
{ |
| 538 |
seq.readRawFastaSequence(infile, line_length); |
| 539 |
if ( infile.fail() && !infile.eof() ) |
| 540 |
{ |
| 541 |
std::cerr << "\n\n"; |
| 542 |
std::cerr << "An error occurred while reading the input file. It might not be a valid fasta file.\n"; |
| 543 |
std::cerr << "File position: line "; |
| 544 |
std::cerr << faststring(infile.line()) << std::endl; |
| 545 |
std::cerr << std::flush; |
| 546 |
} |
| 547 |
|
| 548 |
// Is the sequence we just read in the list of sequence names?? |
| 549 |
// This uses the full name |
| 550 |
|
| 551 |
faststring fname = seq.getFullName(); |
| 552 |
|
| 553 |
CSeqNameListIterator it; |
| 554 |
it = m.find(&fname); // We have to take the address operator here. Remember that we use less_than_pointer_to_faststring_struct to compare the elements, so we do no search for the pointer but for the string indeed. |
| 555 |
|
| 556 |
if (it != m.end() ) |
| 557 |
{ |
| 558 |
seq.writeSequence_fasta(os_in_list, line_length); |
| 559 |
} |
| 560 |
} // END while |
| 561 |
|
| 562 |
infile.ffclose(); |
| 563 |
fclose(os_in_list); |
| 564 |
|
| 565 |
} // END use_list_as_filter |
| 566 |
|
| 567 |
|
| 568 |
void print(std::ostream &os, short flag=0) |
| 569 |
{ |
| 570 |
// With flag == 0 we only print the size of the list. |
| 571 |
|
| 572 |
os << "This CSeqNameList has " << full_names.size() << " entries.\n"; |
| 573 |
|
| 574 |
if (flag > 0) |
| 575 |
os << "flag: " << flag << std::endl; |
| 576 |
|
| 577 |
if (flag == 1) |
| 578 |
{ |
| 579 |
unsigned i=0, n=full_names.size(); |
| 580 |
|
| 581 |
os << "n: " << n << std::endl; |
| 582 |
os << "only_names: " << only_names << std::endl; |
| 583 |
|
| 584 |
if (only_names) |
| 585 |
{ |
| 586 |
while (i<n) |
| 587 |
{ |
| 588 |
os << "Name: " << *(full_names[i]) |
| 589 |
<< std::endl; |
| 590 |
++i; |
| 591 |
} |
| 592 |
} |
| 593 |
else // !only_names |
| 594 |
{ |
| 595 |
while (i<n) |
| 596 |
{ |
| 597 |
os << "Name: " << *(full_names[i]) |
| 598 |
<< " " |
| 599 |
<< seq_len_vec[i] |
| 600 |
<< " " |
| 601 |
<< CG_content[i] |
| 602 |
<< " " |
| 603 |
<< corrected_len_vec[i] |
| 604 |
<< " " |
| 605 |
<< repeat_density_bp_mbp[i] |
| 606 |
<< std::endl; |
| 607 |
++i; |
| 608 |
} |
| 609 |
} |
| 610 |
} |
| 611 |
else if (flag ==2) // fasta format: |
| 612 |
{ |
| 613 |
unsigned i=0, n=full_names.size(); |
| 614 |
|
| 615 |
while (i<n) |
| 616 |
{ |
| 617 |
os << ">" << *(full_names[i]) |
| 618 |
<< std::endl; |
| 619 |
++i; |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
} |
| 625 |
|
| 626 |
unsigned size() |
| 627 |
{ |
| 628 |
return m.size(); |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
double get_CG_content(unsigned id) |
| 633 |
{ |
| 634 |
if (only_names) |
| 635 |
return 0; |
| 636 |
else |
| 637 |
return CG_content[id]; |
| 638 |
} |
| 639 |
|
| 640 |
const char *get_name(unsigned id) |
| 641 |
{ |
| 642 |
return full_names[id]->c_str(); |
| 643 |
} |
| 644 |
|
| 645 |
unsigned get_seq_length(unsigned id) |
| 646 |
{ |
| 647 |
if (only_names) |
| 648 |
return 0; |
| 649 |
else |
| 650 |
return seq_len_vec[id]; |
| 651 |
} |
| 652 |
|
| 653 |
unsigned get_corrected_len(unsigned id) |
| 654 |
{ |
| 655 |
if (only_names) |
| 656 |
return 0; |
| 657 |
else |
| 658 |
return corrected_len_vec[id]; |
| 659 |
} |
| 660 |
|
| 661 |
unsigned get_repeat_content(unsigned id) |
| 662 |
{ |
| 663 |
if (only_names) |
| 664 |
return 0; |
| 665 |
else |
| 666 |
return repeat_density_bp_mbp[id]; |
| 667 |
|
| 668 |
} |
| 669 |
|
| 670 |
//XXXXXXXXXXXXXXXXXXXX |
| 671 |
unsigned get_id(faststring str) |
| 672 |
{ |
| 673 |
CSeqNameListIterator it_find; |
| 674 |
it_find = m.find(&str); // We have to take the address operator here. Remember that we use less_than_pointer_to_faststring_struct to compare the elements, so we do no search for the pointer but for the string indeed. |
| 675 |
if (it_find != m.end()) |
| 676 |
return it_find->second; |
| 677 |
else |
| 678 |
return UINT_MAX; |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
// Search for str_in in the list of sequences. str_in can be the full name or the short name. |
| 683 |
// The target list can contain full or short names. |
| 684 |
unsigned get_id_short_name(faststring str) // No reference since we change the string |
| 685 |
{ |
| 686 |
// We shorten str to the short name. |
| 687 |
// If it is the full name and the target only contains |
| 688 |
// the short name this is important. |
| 689 |
set_to_short_name(str); |
| 690 |
|
| 691 |
CSeqNameListIterator it_find; |
| 692 |
it_find = m.lower_bound(&str); // it_find points to an element that is either equal to str or the first element |
| 693 |
// in the map that is greater than str. In case, str contains the short name of the |
| 694 |
// target, we are looking for this element. |
| 695 |
// The question whether the short name is unique is not addressed here. The id of the |
| 696 |
// first suitable short name is returned. |
| 697 |
if (it_find == m.end()) |
| 698 |
return UINT_MAX; |
| 699 |
|
| 700 |
// if (*(it_find->first) == str) // str is equal to the string that has been found. |
| 701 |
// return it_find->second; |
| 702 |
|
| 703 |
// str is not equal to the string that has been found, but it might be equal to its short name. |
| 704 |
faststring::size_t pos1 = it_find->first->find(' '); |
| 705 |
|
| 706 |
if (pos1 == faststring::npos) |
| 707 |
pos1 = it_find->first->size(); |
| 708 |
faststring::size_t pos2 = str.size(); |
| 709 |
|
| 710 |
// The short names differ or the complete string is the short name. Since the short name should have been found above, |
| 711 |
// we have not found the string. |
| 712 |
if (pos1 != pos2) |
| 713 |
return UINT_MAX; |
| 714 |
|
| 715 |
// If str is longer than the target, it cannot be the short name. |
| 716 |
// If the space is found at a position with index larger than the size of str, it cannot be the short name of str. |
| 717 |
// It could be that this case is not possible. |
| 718 |
// if (str.size() > it_find->first->size() || pos > str.size() ) |
| 719 |
// return UINT_MAX; |
| 720 |
|
| 721 |
if (strncmp(str.c_str(), it_find->first->c_str(), pos1) != 0) |
| 722 |
return UINT_MAX; |
| 723 |
|
| 724 |
return it_find->second; |
| 725 |
} |
| 726 |
|
| 727 |
// Note: removed names have an empty string as its name. |
| 728 |
std::vector<faststring*>& get_vec_of_seq_names() |
| 729 |
{ |
| 730 |
return full_names; |
| 731 |
} |
| 732 |
|
| 733 |
void copy_to_this_if_Full_name_has_a_match(CSeqNameList &a, faststring str) |
| 734 |
{ |
| 735 |
CSeqNameListIterator it_beg, it_end; |
| 736 |
it_beg = a.m.begin(); |
| 737 |
it_end = a.m.end(); |
| 738 |
|
| 739 |
faststring::size_t find_pos; |
| 740 |
unsigned id; |
| 741 |
|
| 742 |
// Copy a to this: |
| 743 |
while (it_beg != it_end) |
| 744 |
{ |
| 745 |
find_pos = it_beg->first->find(str); |
| 746 |
|
| 747 |
if (find_pos != faststring::npos) // we have a match |
| 748 |
{ |
| 749 |
id = it_beg->second; |
| 750 |
add_non_redundant(a, id); |
| 751 |
} |
| 752 |
++it_beg; |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
bool remove_if_Full_name_has_a_match(faststring str, bool report_failure_to_find_match = false) |
| 757 |
{ |
| 758 |
CSeqNameListIterator it_beg, it_end, it_next; |
| 759 |
it_beg = m.begin(); |
| 760 |
it_end = m.end(); |
| 761 |
|
| 762 |
faststring::size_t find_pos; |
| 763 |
bool one_removed = false; |
| 764 |
|
| 765 |
while (it_beg != it_end) |
| 766 |
{ |
| 767 |
// The sequence name is: it_beg->first. In this we search for str: |
| 768 |
find_pos = it_beg->first->find(str); |
| 769 |
|
| 770 |
it_next = it_beg; |
| 771 |
++it_next; |
| 772 |
|
| 773 |
if (find_pos != faststring::npos) // we have a match |
| 774 |
{ |
| 775 |
remove(it_beg); |
| 776 |
one_removed = true; |
| 777 |
} |
| 778 |
it_beg = it_next; |
| 779 |
} |
| 780 |
if (!one_removed && report_failure_to_find_match) |
| 781 |
{ |
| 782 |
std::cout << "NOTE: From CSeqNameList::remove_if_Full_name_has_a_matchNote: No match found for " << str << std::endl; |
| 783 |
} |
| 784 |
return one_removed; |
| 785 |
} |
| 786 |
|
| 787 |
int remove_if_Full_name_has_a_match(std::vector<faststring> v, bool report_failure_to_find_match = false) |
| 788 |
{ |
| 789 |
unsigned i,N=v.size(); |
| 790 |
unsigned count=0; |
| 791 |
|
| 792 |
for (i=0; i<N; ++i) |
| 793 |
{ |
| 794 |
if (remove_if_Full_name_has_a_match(v[i]), report_failure_to_find_match) |
| 795 |
++count; |
| 796 |
} |
| 797 |
return count; |
| 798 |
} |
| 799 |
|
| 800 |
|
| 801 |
void set_to_union_of(CSeqNameList &a, CSeqNameList &b) |
| 802 |
{ |
| 803 |
// The use of set_union does not seem to be better than this solution: |
| 804 |
|
| 805 |
set_to(a, "set union"); |
| 806 |
|
| 807 |
/* CSeqNameListIterator it_beg, it_end; */ |
| 808 |
/* unsigned id; */ |
| 809 |
|
| 810 |
/* infile_name = "set union"; */ |
| 811 |
|
| 812 |
/* it_beg = a.m.begin(); */ |
| 813 |
/* it_end = a.m.end(); */ |
| 814 |
|
| 815 |
/* // Copy a to this: */ |
| 816 |
/* while (it_beg != it_end) */ |
| 817 |
/* { */ |
| 818 |
/* id = it_beg->second; */ |
| 819 |
/* add(a, id); */ |
| 820 |
/* ++it_beg; */ |
| 821 |
/* } */ |
| 822 |
|
| 823 |
// Copy b to this, for all elements that have not been added yet: |
| 824 |
|
| 825 |
add_List_non_redundant(b); |
| 826 |
} |
| 827 |
|
| 828 |
void set_to(CSeqNameList &a, faststring name) |
| 829 |
{ |
| 830 |
reset(); |
| 831 |
infile_name = name; |
| 832 |
only_names = a.only_names; |
| 833 |
|
| 834 |
CSeqNameListIterator it_beg, it_end; |
| 835 |
unsigned id; |
| 836 |
|
| 837 |
it_beg = a.m.begin(); |
| 838 |
it_end = a.m.end(); |
| 839 |
|
| 840 |
// Copy a to this: |
| 841 |
while (it_beg != it_end) |
| 842 |
{ |
| 843 |
id = it_beg->second; |
| 844 |
add_non_redundant(a, id); |
| 845 |
++it_beg; |
| 846 |
} |
| 847 |
|
| 848 |
} |
| 849 |
|
| 850 |
void add_List_non_redundant(CSeqNameList &b) |
| 851 |
{ |
| 852 |
CSeqNameListIterator it_beg, it_end; |
| 853 |
unsigned id; |
| 854 |
|
| 855 |
if (only_names != b.only_names) |
| 856 |
{ |
| 857 |
std::cerr << "CSeqNameList objects can only be added to another list of the only_names field match." << std::endl; |
| 858 |
} |
| 859 |
|
| 860 |
it_beg = b.m.begin(); |
| 861 |
it_end = b.m.end(); |
| 862 |
|
| 863 |
while (it_beg != it_end) |
| 864 |
{ |
| 865 |
id = get_id_short_name(*(it_beg->first)); // Search for sequence in this. |
| 866 |
if (id == UINT_MAX) // Sequence is unknown in this, so we add it |
| 867 |
{ |
| 868 |
add_non_redundant(b, it_beg->second); |
| 869 |
} |
| 870 |
++it_beg; |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
bool is_only_names() |
| 875 |
{ |
| 876 |
return only_names; |
| 877 |
} |
| 878 |
|
| 879 |
|
| 880 |
void set_to_intersection_of(CSeqNameList &a, CSeqNameList &b) |
| 881 |
{ |
| 882 |
infile_name = "set intersection"; |
| 883 |
|
| 884 |
// use set_intersection |
| 885 |
// typedef std::set<faststring> faststringSet; |
| 886 |
|
| 887 |
// faststringSet out_fs; |
| 888 |
|
| 889 |
mySet out_fs; |
| 890 |
|
| 891 |
mySetIterator out_itr( out_fs.begin() ); |
| 892 |
|
| 893 |
mySet a_short; |
| 894 |
mySet b_short; |
| 895 |
|
| 896 |
CSeqNameListIterator it1, it2; |
| 897 |
|
| 898 |
faststring tmp; |
| 899 |
|
| 900 |
it1 = a.m.begin(); |
| 901 |
it2 = a.m.end(); |
| 902 |
while (it1 != it2) |
| 903 |
{ |
| 904 |
tmp = *(it1->first); |
| 905 |
set_to_short_name(tmp); |
| 906 |
a_short.insert(tmp); |
| 907 |
++it1; |
| 908 |
} |
| 909 |
|
| 910 |
it1 = b.m.begin(); |
| 911 |
it2 = b.m.end(); |
| 912 |
while (it1 != it2) |
| 913 |
{ |
| 914 |
tmp = *(it1->first); |
| 915 |
set_to_short_name(tmp); |
| 916 |
b_short.insert(tmp); |
| 917 |
++it1; |
| 918 |
} |
| 919 |
|
| 920 |
set_intersection(a_short.begin(), a_short.end(), |
| 921 |
b_short.begin(), b_short.end(), |
| 922 |
std::inserter( out_fs, out_itr ) ); |
| 923 |
|
| 924 |
mySetIterator it_beg, it_end; |
| 925 |
unsigned id; |
| 926 |
|
| 927 |
it_beg = out_fs.begin(); |
| 928 |
it_end = out_fs.end(); |
| 929 |
|
| 930 |
while (it_beg != it_end) |
| 931 |
{ |
| 932 |
id = a.get_id_short_name(*it_beg); |
| 933 |
add_non_redundant(a, id); |
| 934 |
|
| 935 |
++it_beg; |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
void set_to_diff_of(CSeqNameList &a,CSeqNameList &b) |
| 940 |
{ |
| 941 |
infile_name = "set diff"; |
| 942 |
|
| 943 |
// typedef std::map<faststring*, unsigned> myMap; |
| 944 |
// typedef std::set<faststring> mySet; |
| 945 |
mySet out_fs; |
| 946 |
|
| 947 |
mySetIterator out_itr( out_fs.begin() ); |
| 948 |
|
| 949 |
mySet a_short; |
| 950 |
mySet b_short; |
| 951 |
|
| 952 |
CSeqNameListIterator it1, it2; |
| 953 |
|
| 954 |
faststring tmp; |
| 955 |
|
| 956 |
it1 = a.m.begin(); |
| 957 |
it2 = a.m.end(); |
| 958 |
while (it1 != it2) |
| 959 |
{ |
| 960 |
tmp = *(it1->first); |
| 961 |
set_to_short_name(tmp); |
| 962 |
a_short.insert(tmp); |
| 963 |
++it1; |
| 964 |
} |
| 965 |
|
| 966 |
it1 = b.m.begin(); |
| 967 |
it2 = b.m.end(); |
| 968 |
while (it1 != it2) |
| 969 |
{ |
| 970 |
tmp = *(it1->first); |
| 971 |
set_to_short_name(tmp); |
| 972 |
b_short.insert(tmp); |
| 973 |
++it1; |
| 974 |
} |
| 975 |
|
| 976 |
set_difference(a_short.begin(), a_short.end(), |
| 977 |
b_short.begin(), b_short.end(), |
| 978 |
std::inserter( out_fs, out_itr ) ); |
| 979 |
|
| 980 |
mySetIterator it_beg, it_end; |
| 981 |
unsigned id; |
| 982 |
|
| 983 |
it_beg = out_fs.begin(); |
| 984 |
it_end = out_fs.end(); |
| 985 |
|
| 986 |
// std::cerr << "out_fs" << out_fs.size() << std::endl; |
| 987 |
|
| 988 |
while (it_beg != it_end) |
| 989 |
{ |
| 990 |
id = a.get_id_short_name(*it_beg); |
| 991 |
add_non_redundant(a, id); |
| 992 |
|
| 993 |
++it_beg; |
| 994 |
} |
| 995 |
} |
| 996 |
|
| 997 |
void set_to_sym_diff_of(CSeqNameList &a,CSeqNameList &b) |
| 998 |
{ |
| 999 |
infile_name = "set sym diff"; |
| 1000 |
|
| 1001 |
// typedef std::map<faststring*, unsigned> myMap; |
| 1002 |
// typedef std::set<faststring> mySet; |
| 1003 |
mySet out_fs; |
| 1004 |
|
| 1005 |
mySetIterator out_itr( out_fs.begin() ); |
| 1006 |
|
| 1007 |
mySet a_short; |
| 1008 |
mySet b_short; |
| 1009 |
|
| 1010 |
CSeqNameListIterator it1, it2; |
| 1011 |
|
| 1012 |
faststring tmp; |
| 1013 |
|
| 1014 |
it1 = a.m.begin(); |
| 1015 |
it2 = a.m.end(); |
| 1016 |
while (it1 != it2) |
| 1017 |
{ |
| 1018 |
tmp = *(it1->first); |
| 1019 |
set_to_short_name(tmp); |
| 1020 |
a_short.insert(tmp); |
| 1021 |
++it1; |
| 1022 |
} |
| 1023 |
|
| 1024 |
it1 = b.m.begin(); |
| 1025 |
it2 = b.m.end(); |
| 1026 |
while (it1 != it2) |
| 1027 |
{ |
| 1028 |
tmp = *(it1->first); |
| 1029 |
set_to_short_name(tmp); |
| 1030 |
b_short.insert(tmp); |
| 1031 |
++it1; |
| 1032 |
} |
| 1033 |
|
| 1034 |
set_symmetric_difference(a_short.begin(), a_short.end(), |
| 1035 |
b_short.begin(), b_short.end(), |
| 1036 |
std::inserter( out_fs, out_itr ) ); |
| 1037 |
|
| 1038 |
mySetIterator it_beg, it_end; |
| 1039 |
unsigned id; |
| 1040 |
|
| 1041 |
it_beg = out_fs.begin(); |
| 1042 |
it_end = out_fs.end(); |
| 1043 |
|
| 1044 |
while (it_beg != it_end) |
| 1045 |
{ |
| 1046 |
// The seq can be found either in a or in b. |
| 1047 |
id = a.get_id_short_name(*it_beg); |
| 1048 |
if (id != UINT_MAX) |
| 1049 |
add_non_redundant(a, id); |
| 1050 |
else |
| 1051 |
{ |
| 1052 |
id = b.get_id_short_name(*it_beg); |
| 1053 |
add_non_redundant(b, id); |
| 1054 |
} |
| 1055 |
|
| 1056 |
++it_beg; |
| 1057 |
} |
| 1058 |
|
| 1059 |
} |
| 1060 |
|
| 1061 |
|
| 1062 |
// Searches for sequence names found in this in list a. If present in list a add it to list c. |
| 1063 |
// If not, search for it in list b. If found add it to d. Oherwise produce error. |
| 1064 |
void divide_this_uppon_existence_in_first_two_sets(CSeqNameList &a, CSeqNameList &b, |
| 1065 |
CSeqNameList &c, CSeqNameList &d) |
| 1066 |
{ |
| 1067 |
c.reset(); |
| 1068 |
d.reset(); |
| 1069 |
|
| 1070 |
CSeqNameListIterator it_beg, it_end; |
| 1071 |
|
| 1072 |
it_beg = m.begin(); |
| 1073 |
it_end = m.end(); |
| 1074 |
|
| 1075 |
unsigned id; |
| 1076 |
|
| 1077 |
while (it_beg != it_end) |
| 1078 |
{ |
| 1079 |
id = a.get_id_short_name(*(it_beg->first)); |
| 1080 |
|
| 1081 |
if (id != UINT_MAX) |
| 1082 |
{ |
| 1083 |
c.add_non_redundant(a, id); |
| 1084 |
} |
| 1085 |
else |
| 1086 |
{ |
| 1087 |
id = b.get_id_short_name(*(it_beg->first)); |
| 1088 |
if (id != UINT_MAX) |
| 1089 |
{ |
| 1090 |
d.add_non_redundant(b, id); |
| 1091 |
} |
| 1092 |
else |
| 1093 |
{ |
| 1094 |
std::cerr << "Internal problem: Sequence found in neither file, even though I think it should be in one of the files: " << it_beg->first << std::endl; |
| 1095 |
} |
| 1096 |
} |
| 1097 |
++it_beg; |
| 1098 |
} |
| 1099 |
} |
| 1100 |
|
| 1101 |
// All sequence names are split according to delim. The field with 1-based number field is extracted and stored in the field_set. |
| 1102 |
bool get_set_of_name_field(unsigned field, char delim, std::set<faststring> &field_set) |
| 1103 |
{ |
| 1104 |
unsigned i, n=full_names.size(); |
| 1105 |
std::vector<faststring> splitter; |
| 1106 |
bool error=false; |
| 1107 |
|
| 1108 |
for (i=0; i<n; ++i) // foreach full name |
| 1109 |
{ |
| 1110 |
// Skip removed sequence names: |
| 1111 |
if ( !full_names[i]->empty() ) |
| 1112 |
{ |
| 1113 |
split(splitter, *full_names[i], "|"); |
| 1114 |
if (splitter.size() < field ) |
| 1115 |
{ |
| 1116 |
std::cerr << "Warning: In get_set_of_name_field you tried to extract field # " |
| 1117 |
<< field << " after splitting " << full_names[i] << " with delimiter " << delim |
| 1118 |
<< " but this field does not exist." << std::endl; |
| 1119 |
error = true; |
| 1120 |
} |
| 1121 |
else |
| 1122 |
{ |
| 1123 |
field_set.insert(splitter[field-1]); // Convert 1-based field number to 0-based index. |
| 1124 |
} |
| 1125 |
} |
| 1126 |
} |
| 1127 |
return error; |
| 1128 |
} |
| 1129 |
|
| 1130 |
|
| 1131 |
}; // END CSeqNameList |
| 1132 |
|
| 1133 |
|
| 1134 |
#endif |