-
Notifications
You must be signed in to change notification settings - Fork 7
/
CSequence_Mol2_1.h
2855 lines (2417 loc) · 69.2 KB
/
CSequence_Mol2_1.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 CSEQUENCE_MOL_H
#define CSEQUENCE_MOL_H
// Changes:
// 22.08.2009: According to the fasta format specifications, the sequence name is only the string between the ">" and
// the first space. The rest of the line contains a sequence description or annotation information.
// This is now reflected in the CSequence_Mol class. The variables full_name, name, and description
// contain this information.
// The sequence name variables are now of type faststring (not string as before).
// 07.01.2011: Migrated to fastring2.h and CFile2_1.h
// 30.04.2011: Sliding window CG-AT analysis
// TODO: Data type handling is/was not optimal.
// In function set_taxon_and_sequence the auto_detect_type always superset the
// value the user specified when creating the CSequence_Mol object.
// This caused unwanted changes of the data type.
#include <vector>
#include <iostream>
#include "faststring2.h"
//#include "CFile/CFile3b.h"
#include "CFile/CFile2_1.h"
#include <cstdio>
#include "basic-DNA-RNA-AA-routines.h"
#include "fast-realloc-vector.h"
#include <cstring>
#include <cstdio>
class CSequence_Mol
{
public:
enum DataTypesEnum {
standard = 1, // characters with arbitrarily-assigned,
// discrete states, such as discrete
// morphological data
dna, /* DNA sequences (states A, C, G, T) */
rna, /* RNA sequences (states A, C, G, U) */
// nucleotide, /* nucleotide sequences */
protein, /* amino acid sequences */
molecular, /* AA, dna, or rna */
unknown,
auto_detect_type,
mixed
};
enum processing_flag {
convert_toupper = 1,
convert_ambig2N = 2,
autodetecttype = 4,
removegaps = 8,
removestars = 16,
compute_numbers_of_residues_flag = 32,
gaps_to_ambig = 64
};
private:
faststring full_name; // with description after first space
faststring short_name; // without description after first space
faststring description; // description only
faststring data;
DataTypesEnum type;
char general_ambig;
bool numbers_of_residues_computed;
// General rule: If we convert symbols, we count the new symbols we have in memory,
// not the symbols in the file. This is more consistent.
//
unsigned number_of_AAs; // Does not include Xs and ?
unsigned number_of_DNARNA_bases; // Includes Us but not ambigs
unsigned number_of_DNARNA_amgibs; // Includes Ns and '?'
unsigned number_of_Us;
unsigned number_of_gaps;
unsigned number_of_stars;
unsigned number_of_questionmarks;
unsigned number_of_Xs;
unsigned number_of_Ns;
unsigned number_raw_original_length; //no spaces, but gaps and stars
unsigned number_CG; // CG content in case of DNA/RNA
fastvector<unsigned> gap_and_star_koords;
fastvector<unsigned> gap_koords;
fastvector<unsigned> star_koords;
bool bitcode_recoded;
private:
void find_shortname_and_description()
{
full_name.removeSpacesBack();
full_name.removeSpacesFront();
faststring::size_t pos_description = full_name.find(' ');
faststring::size_t full_name_len = full_name.size();
if (pos_description == faststring::npos) // no space -> no sequence description
{
pos_description = full_name.size();
short_name.assign(full_name.begin(), full_name.begin() + pos_description);
}
else
{
short_name.assign(full_name.begin(), full_name.begin() + pos_description);
// Skip all spaces
while (pos_description < full_name_len && full_name.get_unckecked(pos_description) == ' ')
{
// std::cerr << "-------------DEBUG: ++pos_description" << std::endl;
++pos_description;
}
// std::cerr << "-------------DEBUG: " << full_name.end() - (full_name.begin() + pos_description) << std::endl;
// if (full_name.end() - (full_name.begin() + pos_description) > 0) // Should not occur any more!!!!
// {
description.assign(full_name.begin() + pos_description, full_name.end() );
// }
}
}
void toupper_this_sequence()
{
data.toupper();
}
// TODO: Keep case of ambig.
void change_ambigs()
{
if (type == dna || type == rna)
{
char * it = data.begin();
char * it_end = data.end();
number_of_questionmarks = 0;
number_of_Ns = 0;
while (it != it_end)
{
char c = *it;
if ( c == 'N' || c == 'n' || c == '?' )
{
// Convert all ambiguity codes to N's
*it = general_ambig;
if (general_ambig == '?')
++number_of_questionmarks;
else
++number_of_Ns;
// ++number_of_DNARNA_amgibs;
}
++it;
}
}
else if (type == protein)
{
char * it = data.begin();
char * it_end = data.end();
number_of_questionmarks = 0;
number_of_Xs = 0;
while (it != it_end)
{
char c = *it;
if ( c == 'X' || c == 'x' || c == '?' )
{
// Convert all ambiguity codes to N's
++number_of_DNARNA_amgibs;
*it = general_ambig;
if (general_ambig == '?' )
++number_of_questionmarks;
else
++number_of_Xs;
}
++it;
}
}
// TODO: Adjust the numbers
}
void remove_gaps_stars_this_sequence(bool rg, bool rs)
{
// char *it = data.begin();
// char *it_end = data.end();
// TODO
if (rg)
{}
if (rs)
{}
}
void gaps_to_ambig_this_sequence()
{
char *it = data.begin();
char *it_end = data.end();
unsigned count=0;
while (it != it_end)
{
if (*it =='-')
{
*it = general_ambig;
++count;
}
++it;
}
// If residues are computed, we have to change the numbers counts for gaps and ambigs:
if (numbers_of_residues_computed)
{
number_of_gaps -= count; // Should indeed be 0 now.
if (general_ambig == 'N' || general_ambig == 'n')
{
number_of_Ns += count;
}
else if (general_ambig == 'X' || general_ambig == 'x')
{
number_of_Xs += count;
}
else if (general_ambig == '?')
{
number_of_questionmarks += count;
}
if (type == dna || type == rna)
{
++number_of_DNARNA_amgibs += count;
}
}
}
void addCharToData(char c)
{
data.push_back(c);
}
// Reads line of data and removes only white spaces
void getRawLine(CFile& infile)
{
register char c;
c = infile.getchar();
while (!infile.fail() && c != '\n')
{
if ( isspace(c) ) // We remove white spaces.
{
c = infile.getchar();
continue;
}
addCharToData(c);
c = infile.getchar();
}
}
void getRawLine(CFile& infile, unsigned &num)
{
register char c;
unsigned local_num = 0;
c = infile.getchar();
while (!infile.fail() && c != '\n')
{
if ( isspace(c) ) // We remove white spaces.
{
c = infile.getchar();
continue;
}
addCharToData(c);
c = infile.getchar();
++local_num;
}
num = local_num;
}
// Reads line of data and removes only white spaces
void getRawLine_toupper(CFile& infile)
{
register char c;
c = infile.getchar();
while (!infile.fail() && c != '\n')
{
if ( isspace(c) ) // We remove white spaces.
{
c = infile.getchar();
continue;
}
addCharToData(toupper(c));
c = infile.getchar();
}
}
// Called e.g. from Phobos
void getLineOfDNA_toupper_ambig2N_removegaps (CFile& infile, unsigned &pos_in_seq)
{
register char c;
unsigned pos = pos_in_seq;
register char local_general_ambig = toupper(general_ambig);
// ++pos;
c = infile.getchar();
while (!infile.fail() && c != '\n')
{
// if the char is a space or something we do not count, we subtract this further below
++number_raw_original_length;
c = toupper(c);
if ( c == 'A' || c == 'T' || c == 'G' || c == 'C')
{
addCharToData(c);
++number_of_DNARNA_bases;
}
else if ( c == 'U')
{
addCharToData('U');
++number_of_DNARNA_bases;
++number_of_Us;
}
else if ( c == 'R' || c == 'Y' || c == 'K' || c == 'M' ||
c == 'S' || c == 'W' || c == 'B' || c == 'D' ||
c == 'H' || c == 'V' )
{
// Convert all ambiguity codes to N's
addCharToData(local_general_ambig);
++number_of_DNARNA_amgibs; // includes N and '?'
if (local_general_ambig == 'N')
++number_of_Ns;
else
++number_of_questionmarks;
}
else if (c == '?' || c == 'N')
{
addCharToData(local_general_ambig);
if (local_general_ambig == 'N')
++number_of_Ns;
else
++number_of_questionmarks;
++number_of_DNARNA_amgibs;
}
else if ( isspace(c) )
{
--number_raw_original_length; // we do not count spaces
} // Spaces are removed
else if ( c=='-' || c=='*') // What do we do with gaps and *s - Here they are removed,
// but their positions are stored in order to able to reconstruct the original coordinates.
{ // I don't really know why we tolerate '*' ? They occur in protein sequences as stop codons.
gap_and_star_koords.push_back(pos); // If we want to reconstruct the true position in the sequence,
// we need to keep track of the positions we cut out.
if (c=='-')
{
gap_koords.push_back(pos);
++number_of_gaps;
}
else
{
star_koords.push_back(pos);
++number_of_stars;
}
// c = infile.getchar();
// ++pos;
}
else
{
// we do not count this -just to be consistent before we bail out
--number_raw_original_length;
infile.clear(CFile::__fail_reason1 | CFile::__fail_flag);
break;
}
c = infile.getchar();
++pos;
}
pos_in_seq = pos;
}
void getLineOfDNA_generic (CFile& infile, unsigned &pos_in_seq,
processing_flag pflag)
{
register char c;
register char c_upper;
register char local_general_ambig = general_ambig;
unsigned pos = pos_in_seq;
// register signed char delta;
register bool convert_toupper_bool = (pflag & convert_toupper); // handled
register bool convert_ambig2N_bool = (pflag & convert_ambig2N); // handled
register bool removegaps_bool = (pflag & removegaps); // handled
register bool removestars_bool = (pflag & removestars); // handled
register bool gaps_to_ambig_bool = (pflag & gaps_to_ambig); // handled
// In the routine calling this function it should be checked, that the general_ambig
// character is upper or lower case, as specified.
if (convert_toupper_bool)
local_general_ambig = toupper(general_ambig);
// ++pos;
c = infile.getchar();
c_upper = toupper(c);
while (!infile.fail() && c != '\n')
{
// if the char is a space or something we do not count, we subtract this further below
++number_raw_original_length;
if (c >= 'a' && c <= 'z' && convert_toupper_bool)
c = c_upper;
if ( c_upper == 'A' || c_upper == 'T' || c_upper == 'G' || c_upper == 'C')
{
addCharToData(c);
++number_of_DNARNA_bases;
}
else if ( c_upper == 'U')
{
addCharToData(c);
++number_of_DNARNA_bases;
++number_of_Us;
}
else if (c_upper == 'R' || c_upper == 'Y' || c_upper == 'K' || c_upper == 'M' ||
c_upper == 'S' || c_upper == 'W' || c_upper == 'B' || c_upper == 'D' ||
c_upper == 'H' || c_upper == 'V' )
{
if (convert_ambig2N_bool)
{
// Convert all ambiguity codes to N's, but we preserve the case. 'w' -> 'n' and 'W' -> 'N' etc.
if (c <='Z') // i.e. lower case.
addCharToData( tolower(local_general_ambig) );
else
addCharToData( toupper(local_general_ambig) );
++number_of_DNARNA_amgibs; // includes N and '?'
if (local_general_ambig == '?' )
++number_of_questionmarks;
else
++number_of_Ns;
}
else
{
addCharToData(c);
}
}
else if (c == '?' || c_upper == 'N') // c is either: 'N', 'n', '?'
{
++number_of_DNARNA_amgibs;
if (local_general_ambig == '?')
{
addCharToData(local_general_ambig);
++number_of_questionmarks;
}
else // c is '?' or 'N' or 'n' and general_ambig is 'N' or 'n' --- add 'N' or 'n'
{
++number_of_Ns;
if (c == '?') // convert to general ambig with proper case:
{
addCharToData(local_general_ambig); // Append general_ambig with appropriate case.
}
else // c is 'N' or 'n', general_ambig is 'N' or 'n'
{
addCharToData(c); // Append N or n and preserve case !!! to be consistent with the flags.
}
// Note: In case of: convert to upper, we have already converted c to upper.
}
}
else if ( isspace(c) )
{
--number_raw_original_length; // we do not count spaces
} // Spaces are removed
else if ( c=='-' || c=='*') // What do we do with gaps and *s - Here they are removed,
// but their positions are stored in order to able to reconstruct the original coordinates.
{ // I don't really know why we tolerate '*' ? They occur in protein sequences as stop codons.
if (c=='-')
{
if ( gaps_to_ambig_bool)
{
// std::cerr << "Gaps to ambig " << std::endl;
addCharToData(local_general_ambig);
if (general_ambig == '?' )
++number_of_questionmarks;
else
++number_of_Ns;
++number_of_DNARNA_amgibs;
}
else if ( removegaps_bool )
{
// std::cerr << "Remove gaps " << std::endl;
gap_and_star_koords.push_back(pos); // If we want to reconstruct the true position in the sequence,
// we need to keep track of the positions we cut out.
gap_koords.push_back(pos);
++number_of_gaps;
}
else
{
addCharToData('-');
++number_of_gaps;
}
}
else if (removestars_bool)
{
gap_and_star_koords.push_back(pos); // If we want to reconstruct the true position in the sequence,
// we need to keep track of the positions we cut out.
star_koords.push_back(pos);
++number_of_stars;
}
// c = infile.getchar();
// ++pos;
}
else
{
// we do not count this -just to be consistent before we bail out
--number_raw_original_length;
infile.clear(CFile::__fail_reason1 | CFile::__fail_flag);
break;
}
c = infile.getchar();
c_upper = toupper(c);
++pos;
}
pos_in_seq = pos;
}
void getLineOfProtein_generic (CFile& infile, unsigned &pos_in_seq,
processing_flag pflag)
{
register char c;
register char c_upper;
register char local_general_ambig = general_ambig;
unsigned pos = pos_in_seq;
// register signed char delta;
register bool convert_toupper_bool = (pflag & convert_toupper); // handled
// register bool convert_ambig2N_bool = (pflag & convert_ambig2N); // handled
register bool removegaps_bool = (pflag & removegaps); // handled
// register bool removestars_bool = (pflag & removestars); // handled
register bool gaps_to_ambig_bool = (pflag & gaps_to_ambig);// handled
// In the routine calling this function it should be checked, that the general_ambig
// character is upper or lower case, as specified.
if (convert_toupper_bool)
local_general_ambig = toupper(general_ambig);
// ++pos;
c = infile.getchar();
c_upper = toupper(c);
while (!infile.fail() && c != '\n')
{
// if the char is a space or something we do not count, we subtract this further below
++number_raw_original_length;
if (c >= 'a' && c <= 'z' && convert_toupper_bool)
c = c_upper;
if (c == '?' || c_upper == 'X' )
{
if (local_general_ambig == '?')
{
addCharToData(local_general_ambig);
++number_of_questionmarks;
}
else // c is '?' or 'X' or 'x' and general_ambig is 'X' or 'x'
{
++number_of_Xs;
if (c == '?')
{
addCharToData(local_general_ambig);
}
else // c is 'X' 'x', general_ambig is 'X' 'x'
addCharToData(c); // Append X or x and preserve case !!! to be consistent with the flags.
// Note: In case of convert to upper, we have already converted c to upper.
}
}
else // The following code assumes we do not have X or ?. Otherwise Xs are duplicated in the data.
if ( is_aa_or_aa_ambig_extended(c) ) // includes other ambigs (!'X', !'x' ,!'?')
{
addCharToData(c);
++number_of_AAs; // Counts ambigs but not X or ?
}
else if ( isspace(c) )
{
--number_raw_original_length; // we do not count spaces
} // Spaces are removed
else if ( c=='-' || c=='*') // What do we do with gaps and *s - Here they are removed,
// but their positions are stored in order to able to reconstruct the original coordinates.
{ // I don't really know why we tolerate '*' ? They occur in protein sequences as stop codons.
if (c=='-')
{
if ( gaps_to_ambig_bool)
{
addCharToData(local_general_ambig);
if (local_general_ambig == '?' )
++number_of_questionmarks;
else
++number_of_Xs;
}
else if ( removegaps_bool )
{
gap_and_star_koords.push_back(pos); // If we want to reconstruct the true position in the sequence,
// we need to keep track of the positions we cut out.
gap_koords.push_back(pos);
++number_of_gaps;
}
else
{
addCharToData('-');
++number_of_gaps;
}
}
/* else if (removestars_bool) */
/* { */
/* gap_and_star_koords.push_back(pos); // If we want to reconstruct the true position in the sequence, */
/* // we need to keep track of the positions we cut out. */
/* star_koords.push_back(pos); */
/* ++number_of_stars; */
/* } */
// c = infile.getchar();
// ++pos;
}
else // Unknown symbol
{
// we do not count this -just to be consistent before we bail out
--number_raw_original_length;
infile.clear(CFile::__fail_reason2 | CFile::__fail_flag);
break;
}
c = infile.getchar();
c_upper = toupper(c);
++pos;
}
pos_in_seq = pos;
}
void getLineOfDNA_toupper_removegaps (CFile& infile, unsigned &pos_in_seq)
{
register char c;
unsigned pos = pos_in_seq;
register char local_general_ambig = toupper(general_ambig);
// ++pos;
c = infile.getchar();
while (!infile.fail() && c != '\n')
{
// if the char is a space or something we do not count, we subtract this further below
++number_raw_original_length;
c = toupper(c);
if ( c == 'A' || c == 'T' || c == 'G' || c == 'C')
{
addCharToData(c);
++number_of_DNARNA_bases;
}
else if ( c == 'N' || c == '?')
{
addCharToData(local_general_ambig);
if (general_ambig == 'N')
++number_of_Ns;
else
++number_of_questionmarks;
++number_of_DNARNA_amgibs; // includes N and '?'
}
else if ( c == 'U')
{
addCharToData('U');
++number_of_DNARNA_bases;
++number_of_Us;
}
else if ( c == 'R' || c == 'Y' || c == 'K' || c == 'M' ||
c == 'S' || c == 'W' || c == 'B' || c == 'D' ||
c == 'H' || c == 'V' )
{
addCharToData(c);
++number_of_DNARNA_amgibs;
}
else if ( isspace(c) ) // Spaces are removed
{
--number_raw_original_length; // we do not count spaces
}
else if ( c=='-' || c=='*') // What do we do with gaps and *s - Here they are removed, but their positions are stored in order to reconstruct the original coordinates.
{ // I don't really know why we tolerate '*' ?
gap_and_star_koords.push_back(pos); // If we want to reconstruct the true position in the sequence,
// we need to keep track of the positions we cut out.
if (c=='-')
{
gap_koords.push_back(pos);
++number_of_gaps;
}
else
{
star_koords.push_back(pos);
++number_of_stars;
}
// c = infile.getchar();
// ++pos;
}
else
{
// Something went wrong -> the last reading event from the file failed
// -> we set the fail flag, this informs the user that something went wrong
// We do not count this - just to be consistent before we bail out
--number_raw_original_length;
infile.clear(CFile::__fail_reason1 | CFile::__fail_flag);
break;
}
c = infile.getchar();
++pos;
}
pos_in_seq = pos;
}
void getLineOfProtein_toupper_removegaps (CFile& infile, unsigned &pos_in_seq)
{
register char c;
unsigned pos = pos_in_seq;
// ++pos;
c = infile.getchar();
char local_general_ambig = toupper(general_ambig);
while (!infile.fail() && c != '\n')
{
// if the char is a space or something we do not count, we subtract this further below
++number_raw_original_length;
c = toupper(c);
if (c == 'X' || c=='?')
{
addCharToData(local_general_ambig);
if (local_general_ambig == 'X' )
++number_of_Xs;
else
++number_of_questionmarks;
}
else if (is_aa_or_aa_ambig_extended(c) )
{
addCharToData(c);
++number_of_AAs;
}
else if ( isspace(c) )// Spaces are removed
{
--number_raw_original_length; // we do not count spaces
}
else if ( c=='-' || c=='*') // What do we do with gaps and *s - Here they are removed, but their positions are stored in order to reconstruct the original coordinates.
{ // I don't really know why we tolerate '*' ?
gap_and_star_koords.push_back(pos); // If we want to reconstruct the true position in the sequence,
// we need to keep track of the positions we cut out.
if (c=='-')
{
gap_koords.push_back(pos);
++number_of_gaps;
}
else
{
star_koords.push_back(pos);
++number_of_stars;
}
}
else
{
// we do not count this -just to be consistent before we bail out
--number_raw_original_length;
infile.clear(CFile::__fail_reason2 | CFile::__fail_flag);
break;
}
c = infile.getchar();
++pos;
}
pos_in_seq = pos;
}
void getLineOfDNA_toupper_ambig2N_gaps2ambig (CFile& infile)
{
register char c;
c = infile.getchar();
while (!infile.fail() && c != '\n')
{
// if the char is a space or something we do not count, we subtract this further below
++number_raw_original_length;
c = toupper(c);
if ( c == 'A' || c == 'T' || c == 'G' || c == 'C')
{
addCharToData(c);
++number_of_DNARNA_bases;
}
else if ( c == 'N' || c == '?')
{
addCharToData(general_ambig);
if (general_ambig == 'N')
++number_of_Ns;
else
++number_of_questionmarks;
++number_of_DNARNA_amgibs;
}
else if ( c == 'U')
{
addCharToData('U');
++number_of_DNARNA_bases;
++number_of_Us;
}
else if ( c == 'R' || c == 'Y' || c == 'K' || c == 'M' ||
c == 'S' || c == 'W' || c == 'B' || c == 'D' ||
c == 'H' || c == 'V' )
{
// Convert all ambiguity codes to N's
addCharToData(general_ambig);
if (general_ambig == 'N')
++number_of_Ns;
else
++number_of_questionmarks;
++number_of_DNARNA_amgibs;
}
else if (c=='-')
{
addCharToData(general_ambig);
if (general_ambig == 'N')
++number_of_Ns;
else
++number_of_questionmarks;
++number_of_DNARNA_amgibs;
}
else if ( isspace(c) ) // We remove white spaces.
{ // I don't really know why we tolerate '*' ?
--number_raw_original_length; // we do not count spaces
}
else
{
// we do not count this -just to be consistent before we bail out
--number_raw_original_length;
infile.clear(CFile::__fail_reason1 | CFile::__fail_flag);
return;
}
c = infile.getchar();
}
}
public:
CSequence_Mol(DataTypesEnum dt, char p_general_ambig = '\0'):
type(dt), general_ambig(p_general_ambig),
numbers_of_residues_computed(false), bitcode_recoded(false)
{
data.reserve(10000);
if (p_general_ambig == '\0') // Use the typical ambig character
{
if (dt == dna) general_ambig = 'N';
else if (dt == protein) general_ambig = 'X';
else general_ambig = '?';
}
reset_numbers_of_residues();
}
CSequence_Mol(DataTypesEnum dt, faststring name, faststring::size_t len, char p_general_ambig = '\0'):
full_name(name),type(dt), general_ambig(p_general_ambig),
numbers_of_residues_computed(false), bitcode_recoded(false)
{
if (len < faststring::npos)
++len;
data.reserve(len);
find_shortname_and_description();
if (p_general_ambig == '\0') // Use the typical ambig character
{
if (dt == dna) general_ambig = 'N';
else if (dt == protein) general_ambig = 'X';
else general_ambig = '?';
}
reset_numbers_of_residues();
}
//**************************************************************************
// pos1 must be the first column starting with 0.
// pos2 must be the index after the last column.
// pos2-pos1 must be the number of bases that are copied to this sequence.
//**************************************************************************
CSequence_Mol(const CSequence_Mol& s, faststring::size_t pos1=0, faststring::size_t pos2 = faststring::npos):
full_name(s.full_name),
short_name(s.short_name),
description(s.description),
type(s.type),
general_ambig(s.general_ambig),
numbers_of_residues_computed(false),
bitcode_recoded(s.bitcode_recoded)
{
if (pos1 == 0 && pos2 >= s.length() )
data = s.data;
else
{
if (pos2 > s.length() )
pos2 = s.length();
if (pos1 >= pos2)
{
return;
}
data.assign(s.data, pos1, pos2-pos1);
}
}
// Should only be applied to dna sequences
void convert_ambig2N_this_sequence()
{
if ( !(type == dna || type == rna) )
return;
// This is a DNA sequence:
char * it = data.begin();
char * it_end = data.end();
while (it != it_end)
{
char c = *it;
char c_upper = toupper(c);
if ( c_upper == 'R' || c_upper == 'Y' || c_upper == 'K' || c_upper == 'M' ||
c_upper == 'S' || c_upper == 'W' || c_upper == 'B' || c_upper == 'D' ||
c_upper == 'H' || c_upper == 'V' )
{
// Convert all ambiguity codes to N's
*it = general_ambig;
if (general_ambig == 'N' || general_ambig == 'n')
++number_of_Ns;
else
++number_of_questionmarks;
++number_of_DNARNA_amgibs;
}
++it;
}
}
bool has_only_gaps()
{
char * it = data.begin();
char * it_end = data.end();
while (it != it_end)
{