-
Notifications
You must be signed in to change notification settings - Fork 7
/
CSeqNameList.h
1134 lines (915 loc) · 29.1 KB
/
CSeqNameList.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* BaitFisher (version 1.2.8) a program for designing DNA target enrichment baits
* BaitFilter (version 1.0.6) a program for selecting optimal bait regions
* Copyright 2013-2017 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 CSEQNAMELIST_H
#define CSEQNAMELIST_H
#include <iostream>
#include <fstream>
#include "faststring2.h"
#include <map>
#include <set>
#include "CFile/CFile2_1.h"
#include "CSequence_Mol2_1.h"
#include <iterator>
#include <algorithm>
#include <climits>
inline void set_to_short_name(faststring &str)
{
faststring::size_t pos = str.find(' ');
if (pos != faststring::npos)
str.shorten(pos);
}
class CSeqNameList
{
// A note on memory management.
// This class collects names and information about sequences.
// Sequence names are stored in faststrings. The class keeps pointers to these faststrings.
// The class keeps a copy of the original string it obtained. Therefore, it allocates memory
// for the strings and it has to release the memory when they are not used any more.
// When removing entries there vector position is set to "empty" values.
// They are not removed entirely, since this would be too inefficient.
// Removed entries are removed from the map.
// In particular: m.size() provides the number of valid entries.
// For every change in this file: Check that this is handled consistent.
public:
typedef std::map<faststring*, unsigned, less_than_pointer_to_faststring_struct> myMap;
typedef std::set<faststring> mySet;
typedef myMap::iterator CSeqNameListIterator;
typedef mySet::iterator mySetIterator;
private:
faststring infile_name;
bool only_names; // Formerly, all vectors had been initialised to ensure that all fields are accessible.
// This is not the case any more. If only_names is true, the vectors defined below can be empty.
// TODO: Check that this is handled consistently.
myMap m; // maps sequence names to id's. id's are indices used to refer to the other sequence data.
std::vector<faststring*> full_names;
std::vector<unsigned> seq_len_vec;
std::vector<double> CG_content;
std::vector<unsigned> corrected_len_vec;
std::vector<unsigned> repeat_density_bp_mbp;
void add_unchecked(const faststring &full_name, unsigned sl, double CG_c, unsigned cl, unsigned rdens)
{
// The basic add routine creates a copy of the sequence name that shall be added.
faststring *tmp = new faststring(full_name);
full_names.push_back(tmp);
seq_len_vec.push_back(sl);
CG_content.push_back(CG_c);
corrected_len_vec.push_back(cl);
repeat_density_bp_mbp.push_back(rdens);
faststring::size_t pos = full_names.size()-1;
// std::cerr << "Adding new seq: Name " << *tmp << " pos " << pos << " len " << sl << " CG " << CG_c << " cl " << cl<< std::endl;
m[tmp] = pos;
}
void add_only_name_unchecked(const char *full_name)
{
faststring *tmp = new faststring(full_name);
full_names.push_back(tmp);
faststring::size_t pos = full_names.size()-1;
m[tmp] = pos;
}
public:
bool add_only_name_non_redundant(const char *full_name)
{
if (get_id_short_name(*full_name) == UINT_MAX )
{
add_only_name_unchecked( full_name );
return true;
}
else
{
std::cerr << "Detected previous entry of " << full_name << std::endl;
return false;
}
}
/* void add(const CSeqNameList &snl, unsigned id) */
/* { */
/* if (snl.only_names) */
/* add_only_name( snl.full_names[id]->c_str() ); */
/* else */
/* 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]); */
/* } */
bool add_non_redundant(const faststring &full_name, unsigned sl, double CG_c, unsigned cl, unsigned rdens)
{
if (get_id_short_name(full_name) == UINT_MAX)
{
add_unchecked(full_name, sl, CG_c, cl, rdens);
return true;
}
else
{
std::cerr << "Detected previous entry of " << full_name << ". Name has not been added again." << std::endl;
return false;
}
}
bool add_non_redundant(const CSeqNameList &snl, unsigned id)
{
if (!snl.only_names)
{
if (get_id_short_name(*(snl.full_names[id])) == UINT_MAX)
{
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]);
return true;
}
else
{
std::cerr << "Detected previous entry of " << *snl.full_names[id] << std::endl;
return false;
}
}
else // only_names
{
if (get_id_short_name(*(snl.full_names[id])) == UINT_MAX)
{
add_only_name_unchecked( snl.full_names[id]->c_str() );
return true;
}
else
{
std::cerr << "Detected previous entry of " << *snl.full_names[id] << std::endl;
return false;
}
}
}
void reset()
{
// Release memory. Otherwise we have a memory leak;
unsigned i=0, n=full_names.size();
while (i<n)
{
delete full_names[i];
++i;
}
infile_name.clear();
only_names = false;
m.clear();
full_names.clear();
seq_len_vec.clear();
CG_content.clear();
repeat_density_bp_mbp.clear();
corrected_len_vec.clear();
}
// This version is the most efficient and should be used if alternative versions can be used.
// User has to check that it is an iterator into the map m.
void remove(CSeqNameListIterator it)
{
unsigned index = it->second;
// We do not entirely remove these entries, since
// this is too inefficient.
// The vectors still contain the element, but it is empty.
// The element is removed from the map.
if (!only_names)
{
seq_len_vec[index] = 0;
CG_content[index] = 0;
corrected_len_vec[index] = 0;
repeat_density_bp_mbp[index] = 0;
}
*(full_names[index]) = "";
m.erase(it); // Erase this element out of the map.
}
// remove entry from the "list".
void remove(unsigned index)
{
CSeqNameListIterator it;
if (index >= full_names.size())
{
std::cerr << "Internal error: Call to CSeqNameList::remove(unsigned index) but the index is out-of range." << std::endl;
exit(-33);
}
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.
if (it == m.end())
{
std::cerr << "Internal error: Call to CSeqNameList::remove(unsigned index) but the index the iterator found with m.find does not exist." << std::endl;
exit(-33);
}
else
{
remove(it);
}
}
void remove(faststring &full_name)
{
CSeqNameListIterator it;
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.
if (it == m.end())
{
std::cerr << "Internal error: Call to CSeqNameList::remove(faststring &full_name) but full_name does not exist in map m." << std::endl;
exit(-33);
}
remove(it);
}
// Import sequence names from a fasta file
CSeqNameList(const char *fasta_file_name, bool read_only_names=false):infile_name(fasta_file_name), only_names(read_only_names)
{
CFile infile;
infile.ffopen(fasta_file_name);
if ( !infile.exists() )
{
std::cerr << "Sorry! The input file \n \"";
std::cerr << fasta_file_name;
std::cerr << "\"\ncould not be opened. I guess it does not exist.\n";
exit (-101);
}
CSequence_Mol seq(CSequence_Mol::unknown);
if (!only_names)
{
while (!infile.eof())
{
seq.readRawFastaSequence(infile);
if ( infile.fail() && !infile.eof() )
{
std::cerr << "\n\n";
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";
std::cerr << "File position: line ";
std::cerr << faststring(infile.line()) << std::endl;
std::cerr << std::flush;
exit(-7);
}
else
{
faststring name_to_add = seq.getFullName();
// std::cerr << "Processing sequence: " << name_to_add << std::endl;
seq.compute_numbers_of_residues();
// unsigned Ambigs = seq.get_number_of_DNARNA_ambigs();
unsigned bases = seq.get_number_of_DNARNA_bases();
double CGc = seq.get_CG_content();
unsigned rdens = 0;
// std::cerr << "Adding 2: " << name_to_add << " slen: " << seq.length() << " CG: " << CGc << " bases: " << bases << std::endl;
bool non_redundant = add_non_redundant(name_to_add, seq.length(), CGc, bases, rdens);
if (!non_redundant)
{
std::cerr << "Exiting due to non unique sequence names." << std::endl;
exit(-1);
}
}
} // End while
}
else // Read only names:
{
while (!infile.eof())
{
seq.readSeqName_ignore_Sequence_data(infile);
if ( infile.fail() && !infile.eof() )
{
std::cerr << "\n\n";
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";
std::cerr << "File position: line ";
std::cerr << faststring(infile.line()) << std::endl;
std::cerr << std::flush;
exit(-7);
// break; // Stop reading this file.
}
else
{
const char * name_to_add = seq.getFullName();
// std::cerr << "Processing sequence: " << name_to_add << std::endl;
bool non_redundant = add_only_name_non_redundant(name_to_add);
if (!non_redundant)
{
std::cerr << "Exiting due to non unique sequence names." << std::endl;
exit(-1);
}
}
} // End while
} // End else only_names
infile.ffclose();
} // END constructor
// Import sequence names from a text file:
CSeqNameList(const char *text_file_name, int dummy):infile_name(text_file_name), only_names(true)
{
CFile infile;
faststring name_to_add;
(void) dummy;
infile.ffopen(text_file_name);
if ( !infile.exists() )
{
std::cerr << "Sorry! The input file \n \"";
std::cerr << text_file_name;
std::cerr << "\"\ncould not be opened. I guess it does not exist.\n";
exit (-101);
}
infile.getline(name_to_add);
while (!infile.eof())
{
// std::cerr << "Processing sequence: " << name_to_add << std::endl;
bool non_redundant = add_only_name_non_redundant(name_to_add.c_str());
if (!non_redundant)
{
std::cerr << "Exiting due to non unique sequence names." << std::endl;
exit(-1);
}
infile.getline(name_to_add);
} // End while
infile.ffclose();
} // END constructor
// Default constructor
CSeqNameList(bool read_only_names=false):infile_name(""), only_names(read_only_names)
{}
// File must be of the form:
// sequence name\tdenstiy in bp/Mbp
void add_repeat_densities(const char * filename)
{
std::ifstream is(filename);
faststring line;
std::vector<faststring> vf;
faststring sn;
unsigned rdens;
unsigned id;
if (only_names)
{
std::cerr << "Internal error: Reading repeat densities for names only sequence list.\n";
exit(-22);
}
getline(is, line);
while (is)
{
split(vf, line, "\t");
if (vf.size()!=2)
{
std::cerr << "Critical error when adding repeat densities. Line " << line << " not a valid input.\n";
exit(-21);
}
sn = vf[0];
rdens = vf[1].ToUnsigned();
id = get_id_short_name(sn);
repeat_density_bp_mbp[id] = rdens;
getline(is, line);
}
is.close();
}
~CSeqNameList()
{
/* unsigned i=0, n=full_names.size(); */
/* while (i<n) */
/* { */
/* delete full_names[i]; */
/* ++i; */
/* } */
reset();
} // END destructor
void use_list_as_filter(const char *fasta_file_name,
const char *out_in_list,
const char *out_not_in_list,
unsigned char_per_line=50)
{
CFile infile;
infile.ffopen(fasta_file_name);
if ( !infile.exists() )
{
std::cerr << "Sorry! The input file \n \"";
std::cerr << fasta_file_name;
std::cerr << "\"\ncould not be opened. I guess it does not exist.\n";
exit (-101);
}
FILE *os_in_list; // (out_in_list);
FILE *os_not_in_list; // (out_not_in_list);
os_in_list = fopen(out_in_list, "w");
os_not_in_list = fopen(out_not_in_list, "w");
CSequence_Mol seq(CSequence_Mol::unknown);
while (!infile.eof())
{
seq.readRawFastaSequence(infile);
if ( infile.fail() && !infile.eof() )
{
std::cerr << "\n\n";
std::cerr << "An error occurred while reading the input file. It might not be a valid fasta file.\n";
std::cerr << "File position: line ";
std::cerr << faststring(infile.line()) << std::endl;
std::cerr << std::flush;
}
faststring fname = seq.getFullName();
CSeqNameListIterator it;
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.
if (it != m.end() )
{
seq.writeSequence_fasta(os_in_list, char_per_line);
}
else
{
seq.writeSequence_fasta(os_not_in_list, char_per_line);
}
} // END while
infile.ffclose();
fclose(os_in_list);
fclose(os_not_in_list);
} // END use_list_as_filter
// Reads the specified fasta file.
// If the sequence name is found in this name list write it to output file.
// XXXXXXXXXXXX TODO: return value: number of sequences with not only sequence name but also sequence info.
void write_sequences_in_list(const char *fasta_file_name,
const char *out_in_list,
const char *write_mode)
{
CFile infile;
infile.ffopen(fasta_file_name);
if ( !infile.exists() )
{
std::cerr << "Sorry! The input file \n \"";
std::cerr << fasta_file_name;
std::cerr << "\"\ncould not be opened. I guess it does not exist.\n";
exit (-101);
}
FILE *os_in_list; // (out_in_list);
unsigned line_length;
os_in_list = fopen(out_in_list, write_mode);
CSequence_Mol seq(CSequence_Mol::unknown);
while (!infile.eof())
{
seq.readRawFastaSequence(infile, line_length);
if ( infile.fail() && !infile.eof() )
{
std::cerr << "\n\n";
std::cerr << "An error occurred while reading the input file. It might not be a valid fasta file.\n";
std::cerr << "File position: line ";
std::cerr << faststring(infile.line()) << std::endl;
std::cerr << std::flush;
}
// Is the sequence we just read in the list of sequence names??
// This uses the full name
faststring fname = seq.getFullName();
CSeqNameListIterator it;
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.
if (it != m.end() )
{
seq.writeSequence_fasta(os_in_list, line_length);
}
} // END while
infile.ffclose();
fclose(os_in_list);
} // END use_list_as_filter
void print(std::ostream &os, short flag=0)
{
// With flag == 0 we only print the size of the list.
os << "This CSeqNameList has " << full_names.size() << " entries.\n";
if (flag > 0)
os << "flag: " << flag << std::endl;
if (flag == 1)
{
unsigned i=0, n=full_names.size();
os << "n: " << n << std::endl;
os << "only_names: " << only_names << std::endl;
if (only_names)
{
while (i<n)
{
os << "Name: " << *(full_names[i])
<< std::endl;
++i;
}
}
else // !only_names
{
while (i<n)
{
os << "Name: " << *(full_names[i])
<< " "
<< seq_len_vec[i]
<< " "
<< CG_content[i]
<< " "
<< corrected_len_vec[i]
<< " "
<< repeat_density_bp_mbp[i]
<< std::endl;
++i;
}
}
}
else if (flag ==2) // fasta format:
{
unsigned i=0, n=full_names.size();
while (i<n)
{
os << ">" << *(full_names[i])
<< std::endl;
++i;
}
}
}
unsigned size()
{
return m.size();
}
double get_CG_content(unsigned id)
{
if (only_names)
return 0;
else
return CG_content[id];
}
const char *get_name(unsigned id)
{
return full_names[id]->c_str();
}
unsigned get_seq_length(unsigned id)
{
if (only_names)
return 0;
else
return seq_len_vec[id];
}
unsigned get_corrected_len(unsigned id)
{
if (only_names)
return 0;
else
return corrected_len_vec[id];
}
unsigned get_repeat_content(unsigned id)
{
if (only_names)
return 0;
else
return repeat_density_bp_mbp[id];
}
//XXXXXXXXXXXXXXXXXXXX
unsigned get_id(faststring str)
{
CSeqNameListIterator it_find;
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.
if (it_find != m.end())
return it_find->second;
else
return UINT_MAX;
}
// Search for str_in in the list of sequences. str_in can be the full name or the short name.
// The target list can contain full or short names.
unsigned get_id_short_name(faststring str) // No reference since we change the string
{
// We shorten str to the short name.
// If it is the full name and the target only contains
// the short name this is important.
set_to_short_name(str);
CSeqNameListIterator it_find;
it_find = m.lower_bound(&str); // it_find points to an element that is either equal to str or the first element
// in the map that is greater than str. In case, str contains the short name of the
// target, we are looking for this element.
// The question whether the short name is unique is not addressed here. The id of the
// first suitable short name is returned.
if (it_find == m.end())
return UINT_MAX;
// if (*(it_find->first) == str) // str is equal to the string that has been found.
// return it_find->second;
// str is not equal to the string that has been found, but it might be equal to its short name.
faststring::size_t pos1 = it_find->first->find(' ');
if (pos1 == faststring::npos)
pos1 = it_find->first->size();
faststring::size_t pos2 = str.size();
// The short names differ or the complete string is the short name. Since the short name should have been found above,
// we have not found the string.
if (pos1 != pos2)
return UINT_MAX;
// If str is longer than the target, it cannot be the short name.
// If the space is found at a position with index larger than the size of str, it cannot be the short name of str.
// It could be that this case is not possible.
// if (str.size() > it_find->first->size() || pos > str.size() )
// return UINT_MAX;
if (strncmp(str.c_str(), it_find->first->c_str(), pos1) != 0)
return UINT_MAX;
return it_find->second;
}
// Note: removed names have an empty string as its name.
std::vector<faststring*>& get_vec_of_seq_names()
{
return full_names;
}
void copy_to_this_if_Full_name_has_a_match(CSeqNameList &a, faststring str)
{
CSeqNameListIterator it_beg, it_end;
it_beg = a.m.begin();
it_end = a.m.end();
faststring::size_t find_pos;
unsigned id;
// Copy a to this:
while (it_beg != it_end)
{
find_pos = it_beg->first->find(str);
if (find_pos != faststring::npos) // we have a match
{
id = it_beg->second;
add_non_redundant(a, id);
}
++it_beg;
}
}
bool remove_if_Full_name_has_a_match(faststring str, bool report_failure_to_find_match = false)
{
CSeqNameListIterator it_beg, it_end, it_next;
it_beg = m.begin();
it_end = m.end();
faststring::size_t find_pos;
bool one_removed = false;
while (it_beg != it_end)
{
// The sequence name is: it_beg->first. In this we search for str:
find_pos = it_beg->first->find(str);
it_next = it_beg;
++it_next;
if (find_pos != faststring::npos) // we have a match
{
remove(it_beg);
one_removed = true;
}
it_beg = it_next;
}
if (!one_removed && report_failure_to_find_match)
{
std::cout << "NOTE: From CSeqNameList::remove_if_Full_name_has_a_matchNote: No match found for " << str << std::endl;
}
return one_removed;
}
int remove_if_Full_name_has_a_match(std::vector<faststring> v, bool report_failure_to_find_match = false)
{
unsigned i,N=v.size();
unsigned count=0;
for (i=0; i<N; ++i)
{
if (remove_if_Full_name_has_a_match(v[i]), report_failure_to_find_match)
++count;
}
return count;
}
void set_to_union_of(CSeqNameList &a, CSeqNameList &b)
{
// The use of set_union does not seem to be better than this solution:
set_to(a, "set union");
/* CSeqNameListIterator it_beg, it_end; */
/* unsigned id; */
/* infile_name = "set union"; */
/* it_beg = a.m.begin(); */
/* it_end = a.m.end(); */
/* // Copy a to this: */
/* while (it_beg != it_end) */
/* { */
/* id = it_beg->second; */
/* add(a, id); */
/* ++it_beg; */
/* } */
// Copy b to this, for all elements that have not been added yet:
add_List_non_redundant(b);
}
void set_to(CSeqNameList &a, faststring name)
{
reset();
infile_name = name;
only_names = a.only_names;
CSeqNameListIterator it_beg, it_end;
unsigned id;
it_beg = a.m.begin();
it_end = a.m.end();
// Copy a to this:
while (it_beg != it_end)
{
id = it_beg->second;
add_non_redundant(a, id);
++it_beg;
}
}
void add_List_non_redundant(CSeqNameList &b)
{
CSeqNameListIterator it_beg, it_end;
unsigned id;
if (only_names != b.only_names)
{
std::cerr << "CSeqNameList objects can only be added to another list of the only_names field match." << std::endl;
}
it_beg = b.m.begin();
it_end = b.m.end();
while (it_beg != it_end)
{
id = get_id_short_name(*(it_beg->first)); // Search for sequence in this.
if (id == UINT_MAX) // Sequence is unknown in this, so we add it
{
add_non_redundant(b, it_beg->second);
}
++it_beg;
}
}
bool is_only_names()
{
return only_names;
}
void set_to_intersection_of(CSeqNameList &a, CSeqNameList &b)
{
infile_name = "set intersection";
// use set_intersection
// typedef std::set<faststring> faststringSet;
// faststringSet out_fs;
mySet out_fs;
mySetIterator out_itr( out_fs.begin() );
mySet a_short;
mySet b_short;
CSeqNameListIterator it1, it2;
faststring tmp;
it1 = a.m.begin();
it2 = a.m.end();
while (it1 != it2)
{
tmp = *(it1->first);
set_to_short_name(tmp);
a_short.insert(tmp);
++it1;
}
it1 = b.m.begin();
it2 = b.m.end();
while (it1 != it2)
{
tmp = *(it1->first);
set_to_short_name(tmp);
b_short.insert(tmp);
++it1;
}
set_intersection(a_short.begin(), a_short.end(),
b_short.begin(), b_short.end(),
std::inserter( out_fs, out_itr ) );
mySetIterator it_beg, it_end;
unsigned id;
it_beg = out_fs.begin();
it_end = out_fs.end();
while (it_beg != it_end)
{
id = a.get_id_short_name(*it_beg);
add_non_redundant(a, id);
++it_beg;
}
}
void set_to_diff_of(CSeqNameList &a,CSeqNameList &b)
{
infile_name = "set diff";
// typedef std::map<faststring*, unsigned> myMap;
// typedef std::set<faststring> mySet;
mySet out_fs;
mySetIterator out_itr( out_fs.begin() );
mySet a_short;
mySet b_short;
CSeqNameListIterator it1, it2;
faststring tmp;
it1 = a.m.begin();
it2 = a.m.end();
while (it1 != it2)
{
tmp = *(it1->first);
set_to_short_name(tmp);
a_short.insert(tmp);
++it1;
}
it1 = b.m.begin();
it2 = b.m.end();
while (it1 != it2)
{
tmp = *(it1->first);
set_to_short_name(tmp);
b_short.insert(tmp);
++it1;
}
set_difference(a_short.begin(), a_short.end(),
b_short.begin(), b_short.end(),
std::inserter( out_fs, out_itr ) );
mySetIterator it_beg, it_end;
unsigned id;
it_beg = out_fs.begin();
it_end = out_fs.end();
// std::cerr << "out_fs" << out_fs.size() << std::endl;
while (it_beg != it_end)
{
id = a.get_id_short_name(*it_beg);
add_non_redundant(a, id);
++it_beg;
}
}
void set_to_sym_diff_of(CSeqNameList &a,CSeqNameList &b)
{
infile_name = "set sym diff";