-
Notifications
You must be signed in to change notification settings - Fork 3
/
CSequences2.h
4812 lines (4047 loc) · 147 KB
/
CSequences2.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
// TODO: Check all new whether they have a corresponding delete
// IMPORTANT:
// Some functions only work if the short names are unique. These are the sequence
// names before the first space or the full name if the name contains no spaces.
// Reading and writing work even if the short names are not unique. Accessing
// sequences by the short name does not work.
// Double check results when using this for library for sequences of different
// length. Only very basic tasks are allowed!
//////////////////////////////////////////////////////////////////////
// CSequences.h: interface for the CSequences class.
//
//////////////////////////////////////////////////////////////////////
// Next general goal: Use size_t instead of unsigned int for array sizes.
#ifndef CSEQUENCES2_H
#define CSEQUENCES2_H
#include <vector>
#include <map>
#include "faststring2.h"
#include "fast-realloc-vector.h"
#include "CSplit2.h"
#include "CSequence_Mol2_1.h"
#include <cassert>
#include <cmath>
#include <utility>
#include "basic-DNA-RNA-AA-routines.h"
#include <climits>
#include <set>
#include <algorithm>
//#include "basic-DNA-RNA-AA-routines.h"
#define PrintMessage_cerr(s1) fputs(s1, stderr)
#define ErrorMessage(s1) fputs(s1, stderr)
#define flush_cerr() fflush(stderr)
////////////////////////////////////////////////////////////////////
// Head file for a DNA or PROTEIN sequence collection.
//
// Characteristics:
// - most member functions assume that sequences have identical lengths, though this is not true for all.
// If sequences are not of equal length, it is recommended to revise the code in this head file first.
//
////////////////////////////////////////////////////////////////////
// Global Functions
//////////////////////////////////
// Criteria for reference taxa:
// - Original reference taxa have 2 pipes (3 sections), other sequences have 3 pipes (4 sections) in sequence names.
// - Hamstered reference taxa are those for which section 2 and 3 are identical in the first 4 characters.
/* inline bool is_1kite_hamstered_core_seq_id(faststring seq_id) */
/* { */
/* #ifdef DEBUG */
/* std::cerr << "Called: CSequences2.h:is_1kite_hamstered_core_seq_id with parameter " << seq_id << '\n'; */
/* #endif */
/* // std::cerr << "Calling outdated function: is_1kite_hamstered_core_seq_id\n"; */
/* // exit(-1); */
/* std::vector<faststring> l; */
/* split(l, seq_id, "|"); */
/* unsigned num_sections = l.size(); */
/* if (num_sections > 4 || num_sections < 3) */
/* { */
/* std::cerr << "Error: Non standard 1kite seq_id encountered: " << seq_id << '\n'; */
/* exit(0); */
/* } */
/* #ifdef DEBUG */
/* std::cerr << "num_sections: " << num_sections << '\n'; */
/* #endif */
/* if (num_sections == 3) */
/* return true; */
/* else */
/* return false; */
/* } */
inline bool is_1kite_original_reference_taxon_seq_id(faststring seq_id)
{
#ifdef DEBUG
std::cerr << "Called: CSequences2.h:is_1kite_original_reference_taxon_seq_id with parameter " << seq_id << '\n';
#endif
std::vector<faststring> l;
split(l, seq_id, "|");
std::vector<faststring>::size_type num_sections = l.size();
if (num_sections > 4 || num_sections < 3)
{
std::cerr << "Error: Non standard 1kite seq_id encountered: " << seq_id << '\n';
exit(0);
}
#ifdef DEBUG
std::cerr << "num_sections: " << num_sections << '\n';
#endif
if (num_sections == 3)
return true;
else
return false;
}
inline bool is_1kite_hamstered_reference_seq_id(faststring seq_id)
{
std::vector<faststring> l;
split(l, seq_id, "|");
#ifdef DEBUG
std::cerr << "Called: CPfamScanParser.h:is_1kite_hamstered_reference_seq_id with parameter " << seq_id << '\n';
#endif
if (l.size() != 4)
{
std::cerr << "Error: Non standard 1kite seq_id encountered: " << seq_id << '\n';
exit(0);
}
faststring part2 = l[1];
part2.shorten_to_first_occurrence_of('_');
faststring part3 = l[2];
part2.shorten(4);
part3.shorten(4);
#ifdef DEBUG
std::cerr << "num_sections: " << l.size() << " " << part2 << " " << part3 << '\n';
#endif
return (part2 == part3);
}
//template<class T>
inline void add_or_count(std::map<faststring, unsigned> &m, const faststring &x)
{
std::map<faststring,unsigned>::iterator it;
it = m.find(x);
if (it == m.end() )
{
m[x] = 1;
}
else
{
++it->second;
}
}
inline unsigned add_or_count_return(std::map<faststring, unsigned> &m, const faststring &x)
{
std::map<faststring,unsigned>::iterator it;
it = m.find(x);
if (it == m.end() )
{
return m[x] = 1;
}
else
{
return (++it->second);
}
}
inline void vector_of_faststring_shorten_all(std::vector<faststring> &v, unsigned len)
{
size_t N = v.size();
for (size_t i=0; i< N; ++i)
{
v[i].shorten(len);
}
}
// Shortens strings if they are longer then len, appends copies of fill if they are to short.
inline void vector_of_faststring_resize_all(std::vector<faststring> &v, unsigned len, char fill)
{
unsigned N = v.size();
for (unsigned i=0; i< N; ++i)
{
v[i].resize(len, fill);
}
}
inline void vector_of_faststring_replace_characters(std::vector<faststring> &v, faststring &string_of_replacement_symbols, char skip_char)
{
unsigned char all_symbols_lookup[256];
faststring::size_t i;
unsigned j;
for (j=0; j<256; ++j)
{
all_symbols_lookup[j] = (unsigned char)j;
}
faststring::size_t n = string_of_replacement_symbols.size();
for (i=0; i<n; i+=2)
{
all_symbols_lookup[(unsigned char)string_of_replacement_symbols[i]] = string_of_replacement_symbols[i+1];
}
for (i=0; i < v.size(); ++i)
{
char *pos = v[i].begin();
char *pos_end = v[i].end();
char *pos_write;
char c;
unsigned removed = 0;
faststring::size_t N = v[i].length();
pos_write = pos;
while (pos < pos_end)
{
c = all_symbols_lookup[(unsigned char)*pos];
if (c != skip_char)
{
*pos_write = c;
++pos_write;
}
else
{
++removed;
}
++pos;
}
v[i].shorten(N-removed);
}
}
inline void vector_of_faststring_unique(std::vector<faststring> &v, faststring::size_t len)
{
faststring::size_t digits = (unsigned)log10(v.size()) + 1;
faststring::size_t mlen;
if (digits+1 >= len)
mlen = 0;
else
mlen = len-digits-1;
std::map<faststring, unsigned> m_names, m_names_short;
std::map<faststring, unsigned>::iterator f_it, f_it2;
faststring tmp;
for (unsigned i=0; i < v.size(); ++i)
{
add_or_count(m_names, v[i]);
}
for (unsigned i=0; i < v.size(); ++i)
{
if (m_names[v[i]] > 1)
{
v[i].shorten(mlen);
}
add_or_count(m_names_short, v[i]);
}
m_names.clear();
char s = '_';
if (mlen == 0)
s = 'S';
unsigned n;
for (unsigned i=0; i < v.size(); ++i)
{
if (m_names_short[v[i]] > 1)
{
n = add_or_count_return(m_names, v[i]);
v[i] += s + faststring(n, '0', digits);
}
}
}
inline void vector_of_faststring_trimSpaces_front_back(std::vector<faststring> &v)
{
unsigned N = v.size();
for (unsigned i=0; i< N; ++i)
{
v[i].removeSpacesFront();
v[i].removeSpacesBack();
}
}
//////////////////////////////////
// Data types
//////////////////////////////////
//////////////////////////////////
// Class CSequences
//////////////////////////////////
// Class for aligned sequences
class CSequences2
{
public:
typedef char chartype;
private:
CSequence_Mol::DataTypesEnum datatype; // DNA, Protein, or other
unsigned taxaNum;
char ambig_char;
bool originalPosNumbers_supplied;
unsigned posNum; // Stores 0 if sequences have unequal lengths.
// Checked for fasta. Phylip input guarantees
// equal lengths.
std::vector<unsigned> originalPosNumbers; // In case we excluded positions from the alignment, we want to remember the original positions.
std::vector<CSequence_Mol*> seqData; // The vector of pointers to sequences
// Map of short names. Only makes sense if the short names are unique!
std::map<faststring, CSequence_Mol*> sn_map; // Obtain pointer to the sequence by sequence name. Is map of short names.
// Short names are normal names
bool short_names_unique; // The short_name is the name before the first space or the full name if the name has no spaces.
void add_seq(CSequence_Mol* seq)
{
// std::cerr << "Adding: " << seq->getName() << '\n';
if (sn_map.find(seq->getName()) != sn_map.end() )
short_names_unique = false;
seqData.push_back(seq);
sn_map[seq->getName()] = seq;
++taxaNum;
}
void determine_map_of_sequence_names()
{
short_names_unique = true;
// Creates/recreates the map of short names.
if (sn_map.size() == 0)
{
int i, N=seqData.size();
for(i=0; i<N; ++i)
{
if (sn_map.find(seqData[i]->getName()) != sn_map.end() )
short_names_unique = false;
sn_map[seqData[i]->getName()] = seqData[i];
}
}
}
void recompute_are_shortnames_unique()
{
sn_map.clear();
determine_map_of_sequence_names();
}
public:
// Minimal constructor: Empty sequences object
CSequences2(CSequence_Mol::DataTypesEnum Dt, char set_ambig_char='?'):
datatype(Dt), taxaNum(0), ambig_char(set_ambig_char), originalPosNumbers_supplied(false),posNum(0), short_names_unique(true)
{}
// Constructor for a set of empty sequences with names and length.
CSequences2(CSequence_Mol::DataTypesEnum Dt, std::vector<faststring> names, unsigned len):
datatype(Dt), taxaNum(0), ambig_char('?'), originalPosNumbers_supplied(false),posNum(0), short_names_unique(true)
{
unsigned N = names.size();
seqData.reserve(N);
unsigned i;
CSequence_Mol *seq;
for (i=0; i<N; ++i)
{
seq = new CSequence_Mol (CSequence_Mol::dna, names[i], len, ambig_char);
add_seq(seq);
}
}
~CSequences2()
{
int i, n=taxaNum;
for (i=0; i<n; ++i)
{
delete seqData[i];
}
}
// This constructor can be used as/to
// - general copy constructor
// - extract a range of sites
// Coordinates:
// pos1 must be the first column. Is 0 based index.
// pos2 must be the index after the last column. Is 0 based index.
// pos2-pos1 must be the number of bases that are copied to this sequence.
CSequences2(const CSequences2 &s, faststring::size_t pos1 = 0, faststring::size_t pos2 = faststring::npos):
datatype(s.datatype), taxaNum(0), ambig_char(s.ambig_char),
originalPosNumbers_supplied(false), posNum(0), short_names_unique(true)
{
unsigned i, N=s.taxaNum;
CSequence_Mol *seq;
if (N>0)
{
// This is taken out of the loop to silence the warning that seq is used uninitialised below.
seq = new CSequence_Mol ( *(s.seqData[0]), pos1, pos2);
add_seq(seq);
for (i=1; i<N; ++i)
{
seq = new CSequence_Mol ( *(s.seqData[i]), pos1, pos2);
add_seq(seq);
}
// The following code does not check anything - remove later.
// if (i != n)
// {
// std::cerr << "Critical error in CSequences2 constructor: taxaNum and number of sequences found disagree.\n";
// }
posNum = seq->length();
}
}
// Add more constructors: (a) from range, (b) from range_list
bool are_short_names_unique()
{
return short_names_unique;
}
bool equal_length_of_all_sequences()
{
size_t len;
unsigned i;
if (taxaNum==0)
return true;
len = seqData[0]->length();
for (i=1; i<taxaNum; ++i)
{
if (len != seqData[i]->length())
return false;
}
return true;
}
void trim_seq_names(unsigned max_len)
{
for (unsigned i=0; i<taxaNum; ++i)
{
seqData[i]->trim_seq_name(max_len);
}
recompute_are_shortnames_unique();
}
void trim_seq_names(const char *trim_at_these_symbols)
{
for (unsigned i=0; i<taxaNum; ++i)
{
seqData[i]->trim_seq_name(trim_at_these_symbols);
}
recompute_are_shortnames_unique();
}
unsigned long memory_usage(unsigned long &value1, unsigned long &value2, unsigned long &value3, float &mean2)
{
unsigned long tmp1=sizeof(*this);
unsigned long tmp2=0, tmp3=0;
unsigned long i,N;
for (i=0, N=seqData.size(); i<N; ++i)
tmp2 += seqData[i]->memory_usage();
std::map<faststring, CSequence_Mol*>::iterator it;
it = sn_map.begin();
while (it != sn_map.end() )
{
tmp3 += it->first.memory_usage();
tmp3 += sizeof(it->first);
tmp3 += sizeof(it->second);
++it;
}
value1 = tmp1;
value2 = tmp2;
value3 = tmp3;
mean2 = (float)tmp2/N;
return tmp1 + tmp2 + tmp3;
}
CSequence_Mol* get_seq_by_name(faststring name) // Must be short name.
{
// Test code:
{
// print_DEBUG(cerr, 0);
}
std::map<faststring, CSequence_Mol*>::iterator find_it = sn_map.find(name);
if (find_it != sn_map.end() )
return find_it->second;
else
return NULL;
}
CSequence_Mol* get_seq_by_index(unsigned id)
{
if (id >= seqData.size())
{
return NULL;
}
else
{
return seqData[id];
}
}
// Depreciated:
CSequence_Mol* get_seq(unsigned id)
{
return get_seq_by_index(id);
}
const char* get_Seq_Data(unsigned id)
{
if (id >= seqData.size())
{
return NULL;
}
else
{
return seqData[id]->getSeqStr();
}
}
const char* get_Seq_Name(unsigned id)
{
if (id >= seqData.size())
{
return NULL;
}
else
{
return seqData[id]->getName();
}
}
const char* get_Seq_FullName(unsigned id)
{
if (id >= seqData.size())
{
return NULL;
}
else
{
return seqData[id]->getFullName();
}
}
unsigned GetTaxaNum() { return taxaNum;}
unsigned GetPosNum() { return posNum;}
CSequence_Mol::DataTypesEnum get_datatype()
{
return datatype;
}
chartype GetChar(unsigned TaxaIndex,
unsigned PosIndex) const
{
assert(TaxaIndex < taxaNum);
assert(PosIndex < posNum);
return seqData[TaxaIndex]->get_pos(PosIndex);
}
void print_DEBUG(std::ostream &os, unsigned flag=1)
{
if (flag & 1) // scalars:
{
os << "Debug output CSequences2 object, flag==0\n";
os << "Data type: " << (int)datatype<< '\n';
os << "taxaNum: " << taxaNum << '\n';
os << "posNum: " << posNum << '\n';
os << "ambig_char: " << ambig_char << '\n';
os << "originalPosNumbers_supplied " << (int)originalPosNumbers_supplied << '\n';
os << "size of originalPosNumbers vector: " << originalPosNumbers.size() << '\n';
os << "size of seqData : " << seqData.size() << '\n';
os << "size of sn_map : " << sn_map.size() << '\n';
}
if (flag & 2)
{
size_t n=seqData.size();
os << "Data types of sequences:\n";
for (size_t i=0; i<n; ++i)
{
os << i << ": " << seqData[i]->get_datatype() << " " << seqData[i]->type_as_string() << '\n';
}
}
}
/* void SetChar(unsigned TaxaIndex, unsigned PosIndex, chartype mychar) */
/* { */
/* assert(TaxaIndex < taxaNum); */
/* assert(PosIndex < posNum); */
/* seqData[TaxaIndex]->set_pos(PosIndex, mychar); */
/* } */
void SetOriginalPosNumber(unsigned index, unsigned theOriginalPosNumber)
{
originalPosNumbers_supplied = true;
assert(index < posNum);
originalPosNumbers[index] = theOriginalPosNumber;
}
unsigned GetOriginalPosNumber(unsigned index) const
{
assert(index < posNum);
if (originalPosNumbers_supplied)
return originalPosNumbers[index];
else
return UINT_MAX;
}
// Moves taxon with index index to top, preserving the order of all other taxa.
// Of course this changes all indices with the only exception that this taxon is
// already at the top of the vector.
// The index is of course 0 based.
void reorder_move_seq_to_top(unsigned index)
{
if (index < taxaNum && index > 0)
{
seqData.insert(seqData.begin(), seqData[index]);
seqData.erase(seqData.begin()+index+1); // We have to add 1 since we have one additional entry at the beginning
}
}
// TODO: Not very efficient!!
// Return true if taxon_name has been found, false otherwise
bool reorder_move_seq_to_top(faststring &taxon_name)
{
unsigned i;
for (i=0; i< taxaNum; ++i)
{
faststring iname = seqData[i]->getFullName();
if (seqData[i]->getFullName() == taxon_name)
{
std::cerr << "Move to top: " << i << '\n';
reorder_move_seq_to_top(i);
return true;
}
}
return false;
}
// Reorder the sequences such the those in the given vector are at the top
// in the order given by the vector.
// TODO: Not very efficient! - Very simple and thus secure implementation.
bool reorder_sort_by(std::vector<faststring> &names)
{
size_t n=names.size();
bool success = true;
for (int i=n-1; i>=0; --i)
{
success = success & reorder_move_seq_to_top(names[i]);
}
return success;
}
bool get_Originalposnumbers_Supplied()
{
return originalPosNumbers_supplied;
}
char get_ambiguity_character()
{
return ambig_char;
}
void trimSeqNamesAtSymbolsInString(const char *symbol_list)
{
for (unsigned i=0; i < taxaNum; ++i)
{
seqData[i]->trim_seq_name(symbol_list);
}
}
void get_sequence_names(std::vector<faststring> &snames)
{
snames.clear();
snames.reserve(taxaNum);
unsigned i;
// unsigned max=0;
for (i=0; i < taxaNum; ++i)
{
snames.push_back(seqData[i]->getFullName());
/* if (snames[i].size() > max) */
/* { */
/* max = snames[i].size(); */
/* } */
}
// return max;
}
void get_short_sequence_names(std::vector<faststring> &snames)
{
snames.clear();
snames.reserve(taxaNum);
unsigned i;
// unsigned max=0;
for (i=0; i < taxaNum; ++i)
{
snames.push_back(seqData[i]->getName_faststring());
}
}
// Not yet implemented
/* void unique_full_names(std::vector<faststring> &snames) */
/* { */
/* } */
// Not yet implemented
/* void unique_short_names(std::vector<faststring> &snames) */
/* { */
/* } */
/*
void unique_maxlen_names(std::vector<faststring> &snames, int ulen=10)
{
snames.clear();
std::map<faststring, unsigned> m_snames;
std::map<faststring, unsigned> m_shortend_names;
std::map<faststring, unsigned>::iterator f_it, f_it2;
unsigned digits = (unsigned)log10(taxaNum) + 1;
unsigned i;
faststring tmp;
faststring tmp_short;
if (ulen - 1 < digits)
{
std::cerr << "Internal library error: Attempt to shorten sequence names to negative length. Usage of the function: unique_maxlen_names should be revised.\n";
exit(-1);
}
for (i=0; i < taxaNum; ++i)
{
tmp = seqData[i]->getPhylipName(ulen);
f_it = m_snames.find(tmp);
// If we find it it is not unique
if (f_it != m_snames.end() ) // This name is not unique
{
++f_it->second; // We count this occurrence
}
}
for (i=0; i < taxaNum; ++i)
{
tmp = seqData[i]->getPhylipName(ulen);
// snames.push_back(seqData[i]->getPhylipName());
f_it = m_snames.find(tmp);
if (f_it->second > 0 ) // This name is not unique
{
tmp_short = tmp;
tmp_short.resize(ulen-digits-1, ' ');
// We know that we will have multiple entries in the end.
f_it2 = m_shortend_names.find(tmp_short);
// It could be that this will the first entry
if (f_it2 != m_shortend_names.end() )
{}
}
else // Otherwise we will have only one entry.
{
snames.push_back(tmp);
}
}
///
for (i=0; i < taxaNum; ++i)
{
// push_back first sequence phylip sequence name. This is already trimmed to 10 characters.
snames.push_back(seqData[i]->getPhylipName(ulen));
// Search for this name in m_snames map.
f_it = m_snames.find(snames[i]);
// If we find it it is not unique
if (f_it != m_snames.end() ) // This name is not unique
{
++f_it->second; // We count this occurrence
}
else // If we do not find it, we insert it and set the counter to 1
{
m_snames.insert(std::make_pair(snames[i], 1));
}
}
// We have build the map that count the number of occurrences - now we need to create unique names:
// We will shorten the names to add a number.
// When shortening names, previously unique names might become non-unique so we have to take care of this first:
f_it = m_snames.begin();
while (f_it != m_snames.end() )
{
// std::cout << f_it->first << " " << f_it->second << '\n';
if (f_it->second > 1) // If the 10 character sequence name occurs more than one, we have to number them:
{
faststring temp = f_it->first;
temp.shorten(9-digits);
f_it2 = m_shortend_names.find(temp);
if (f_it2 == m_shortend_names.end() ) // We already have this name in the map:
{
m_shortend_names[temp] += f_it->second;
}
else // Create this entry:
{
m_shortend_names[temp] = f_it->second;
}
}
++f_it;
}
// Now we have two maps:
// The first has entries for all names
// The second has entries for all names that need to be shortened.
// For all names that need to be shortened:
f_it = m_shortend_names.begin();
while (f_it != m_shortend_names.end() )
{
unsigned internal_counter = 1;
unsigned j;
faststring temp;
for (j=0; j<taxaNum; ++j)
{
temp = snames[j];
if ( m_snames[temp] != 1 )
{
temp.shorten(10-digits);
}
if (temp == f_it->first)
{
faststring nn = faststring(internal_counter, '0', digits);
// nn.replace_char(' ', '0');
snames[j] = temp;
snames[j].append('_');
snames[j].append(nn);
++internal_counter;
}
}
++f_it;
}
}
*/
unsigned PairwiseSequenceSymbolMatches(unsigned taxon1, unsigned taxon2,
char numDistCharacters,
const signed char* pos_vec,
signed char* match_vec) const
{
unsigned count = 0;
unsigned pos;
for (pos = 0; pos < posNum; ++pos)
{
if (pos_vec[pos] &&
seqData[taxon1]->get_pos(pos) == seqData[taxon2]->get_pos(pos) &&
seqData[taxon1]->get_pos(pos) < numDistCharacters)
{
++count;
match_vec[pos] = 1;
}
else
match_vec[pos] = 0;
}
return count;
}
unsigned PairwiseSequenceSymbolMatches(
unsigned taxon1,
const faststring & ref_seq,
char numDistCharacters,
const signed char* pos_vec,
signed char* match_vec) const
// Ist hier alles OK?????????????????????????????????????????
{
unsigned count = 0;
unsigned pos;
for (pos = 0; pos < posNum; ++pos)
{
if (pos_vec[pos] && seqData[taxon1]->get_pos(pos) == ref_seq[pos]
&& ref_seq[pos] < numDistCharacters)
{
++count;
match_vec[pos] = 1;
}
else
match_vec[pos] = 0;
}
return count;
}
// Only works for recoded sequences!!!!
void ConsensusSequence(faststring& conSeq,
const CSplit& setOfTaxa,
char numDistCharacters,
unsigned numSymbols,
double consensusThreshold )
{
unsigned pos;
unsigned taxon, maxindex, equalindex;
unsigned taxaCount = 0;
unsigned consensusSymMinimum;
char i;
std::vector<unsigned> counterSymbolsVec(numSymbols,0);
for (pos=0; pos < posNum; ++pos)
{
// Initialise variable
taxaCount = 0;
for (i=0; i < numDistCharacters; ++i)
counterSymbolsVec[i] = 0;
// Count number of occuring symbols for this position over all taxa
for (taxon = 0; taxon < taxaNum; ++taxon)
{
if (setOfTaxa.test(taxon))
{
++taxaCount;
++counterSymbolsVec[seqData[taxon]->get_pos(pos)];
}
}
consensusSymMinimum = (unsigned) std::ceil(consensusThreshold * taxaCount);
maxindex = 0;
equalindex = numDistCharacters;
for (i = 1; i < numDistCharacters; ++i)
{
if (counterSymbolsVec[i] >= counterSymbolsVec[maxindex])
{
if (counterSymbolsVec[i] == counterSymbolsVec[maxindex])
equalindex = i;
maxindex = i;