-
Notifications
You must be signed in to change notification settings - Fork 39
/
seq.hpp
1445 lines (1206 loc) · 39.9 KB
/
seq.hpp
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
// seq.hpp
/*===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
* Author: Vyacheslav Brover
*
* File Description:
* Sequence utilities
*
*/
#ifndef SEQ_HPP
#define SEQ_HPP
#include "common.hpp"
using namespace Common_sp;
namespace Seq_sp
{
// Frame
typedef char Frame;
// -3, -2, -1, 1, 2, 3
// If > 0 then strand = 1 else strand = -1
inline bool isFrame (char frame)
{ return
betweenEqual (frame, char (-3), char (-1)) ||
betweenEqual (frame, char ( 1), char ( 3));
}
// Strand
typedef char Strand;
// 1 - top
// -1 - bottom
// 0 - unknown
inline bool isStrand (char strand)
{ return strand == -1
|| strand == 1;
}
// Alphabets
// DNA
// Lower case: gap is impossible
// Upper case: gap is possible
extern const char* dnaAlphabet;
// Nucleotides:
// 'a' - adenine
// 'c' - cytosine
// 'g' - guanine
// 't' - thymine (RNA: 'u' - uracil)
extern const char* dnaWildcards;
/*
IUPAC:
m = ac
r = ag (purins)
w = at
s = cg
y = ct (pyramidins)
k = gt (ketons)
v = acg
h = act
d = agt
b = cgt
n = acgt
*/
inline bool isAmbigNucl (char c)
{ return strchr (dnaWildcards, c); }
extern const char* extDnaAlphabet;
// = DnaAlphabet + DnaWildcards
extern const char* extSparseDnaAlphabet;
// extDnaAlphabet + gap
// '-': gap
// ' ': nothing
// Peptides
// Lower case
extern const char* peptideAlphabet;
// Amino acides
extern const char* peptideWildcards;
inline bool isAmbigAa (char c)
{ return strchr (peptideWildcards, c); }
extern const char* extPeptideAlphabet;
// = PeptideAlphabet + PeptideWildcards
/*
X = any
B = DN
Z = EQ
J = IL
U = selenocysteine ("tga")
O = Pyrrolysine ("tag")
*/
extern const char* terminator;
// strlen() = 1
extern const char* extTermPeptideAlphabet;
// = ExtPeptideAlphabet + terminator
extern const char* extSparseTermPeptideAlphabet;
// = extPeptideAlphabet + gap
size_t alphabet2Pos (const char* alphabet,
char c);
// Return: position of c in alphabet[]
// Requires: c in alphabet[]
inline bool isAmbig (char c,
bool aa)
{ return aa ? isAmbigAa (c) : isAmbigNucl (c); }
typedef unsigned char Gencode;
// NCBI genetic code
/////////////////////////// Seq //////////////////////////////////
// "Sparse sequence" is a sequence with '-'
inline size_t countInsertions (const string &seq)
{ return strCountSet (seq, "-"); }
inline size_t sparseSeqLen (const string &seq)
{ return seq. size () - countInsertions (seq); }
#if 0
int SparseSeq2SeqPos (const char* SparseSeq,
int SparseSeqPos);
int Seq2SparseSeqPos (const char* SparseSeq,
int SeqPos);
// Return: SparseSeq [Result] != '-'
#endif
extern const size_t fastaLineLen;
typedef char (*GetIintersectChar) (const char* CharSet);
// Input: CharSet[]
// Return: Intersection character, ' ' if no intersection
struct Dna;
struct Peptide;
struct Seq : Named
// name: name of sequence
// Positions: 0-based
// stop: position of the last character + 1
// start != pos
// size = stop - start
// Reverse strand <=> start > stop
{
string seq;
// in getSeqAlphabet()
// May be empty()
bool sparse {false};
// true <=> '-' is allowed
protected:
Seq () = default;
Seq (const string &name_arg,
size_t seqLen,
bool sparse_arg)
: Named (name_arg)
, seq (seqLen, '\0')
, sparse (sparse_arg)
{ qcName (); }
Seq (const string &name_arg,
const string &seq_arg,
bool sparse_arg)
: Named (name_arg)
, seq (seq_arg)
, sparse (sparse_arg)
{ qcName ();
if (! sparse)
unSparse ();
}
Seq (LineInput &fasta,
size_t reserveLen,
bool sparse_arg,
bool makeUpper);
// Reads 1 sequence and skips blank lines
// Requires: fasta.line is the first line of the sequence
// If !makeUpper then make lowercase
void qcName () const;
void qcAlphabet () const;
// Invokes: getSeqAlphabet()
static LineInput* getLineInput (const string &fName)
{ LineInput* in = new LineInput (fName);
in->nextLine ();
return in;
}
public:
virtual Seq* copy () const override = 0;
void qc () const override
{ if (qc_on)
return;
qcName ();
qcAlphabet ();
}
virtual const Dna* asDna () const
{ return nullptr; }
virtual const Peptide* asPeptide () const
{ return nullptr; }
virtual const char* getSeqAlphabet () const = 0;
virtual bool isAmbiguous (char c) const = 0;
void printCase (ostream &os,
bool makeUpper) const;
// FASTA format
// Length of lines = fastaLineLen
size_t getIdSize () const
{ const size_t idSize = name. find (' ');
return idSize == string::npos ? name. size () : idSize;
}
static size_t getTaxonStart (const string &s);
// Return: != string::npos => s[Return] == '['
size_t getTaxonStart () const
{ return getTaxonStart (name); }
string getId () const
{ return name. substr (0, getIdSize ()); }
long /*CSeq_id::TGi*/ getGi () const;
// If there is no gi then throw
string getDescription (bool trimTaxon) const;
// Return: between Id and taxon
size_t getXs () const;
// Invokes: isAmbiguous()
size_t getContiguousXs () const;
// Return: max. length of contiguous ambiguous characters
virtual double getComplexityInt (size_t start,
size_t end) const = 0;
// Input: Subsequence form start to end
// Return: Entropy of diletters divided by log_2 e; >= 0
// If > stdMinComplexity then no repeats
double getComplexity () const
{ return getComplexityInt (0, seq. size ()); }
#if 0
size_t* GetAlphabetCount () const;
// Return: length = strlen (SeqAlphabet)
// Invokes: NewUintArray ()
#endif
void unSparse ()
{ strDeleteSet (seq, "-");
sparse = false;
}
map<char,size_t> getCharCount () const;
};
struct Multifasta : Root
/* Usage:
{ Multifasta fa (...);
while (fa. next ())
{Peptide|Dna} ... (fa);
} // To close LineInput and Progress
*/
{
LineInput in;
const bool aa;
Progress prog;
Multifasta (const string &fName,
bool aa_arg,
size_t displayPeriod = 1000)
: in (fName)
, aa (aa_arg)
, prog (0, displayPeriod)
{ in. nextLine ();
qcNewSeq ();
}
private:
void qcNewSeq () const;
public:
bool next () const
{ qcNewSeq ();
return ! in. eof;
}
};
////////////////////////////// Dna /////////////////////////////////
// acgtb: 4 nucleotides and blank
// Blank is encoded by '-'
// One of them must be true
size_t nuc2num (char wildNucleotide);
// Return: if wildNucleotide is in dnaAlphabet then position in dnaAlphabet
// else if wildNucleotide is in dnaWildcards then 4
// else error
// Case-insensitive
#if 0
typedef PROBABILITY NT_PROBABILITY [5];
void PrintNTProb (const NT_PROBABILITY Prob);
void CompressDna (const char* seq,
char* CompressedSeq,
size_t &CompressedSeqLen);
// Input: seq in extDnaAlphabet + '-' + uppercase
// Output: CompressedSeq []
// CompressedSeqLen
// Requires: CompressedSeq [] must have enough space (<= strlen (seq) + 1)
void UncompressDna (size_t SeqLen,
const char* CompressedSeq,
char* &seq,
size_t &CompressedSeqLen);
// Output: *seq, malloc (), length = SeqLen + 1
// CompressedSeqLen
#endif
uchar wild2nucleotides (char wildNucleotide,
bool acgtb [5]);
// Output: acgtb []
// Return: Number of 1's in acgtb []; 0..5
// |WildNucleotide|, "Degree of ambiguity"
// Requires: WildNucleotide in extDnaAlphabet + '-' + ' '
char nucleotides2wild (const bool acgtb [5]);
// Return: in extDnaAlphabet + '-'; if empty set then ' '
#if 0
void Wild2NucleotideFreq (char WildNucleotide,
NT_PROBABILITY acgtb);
// Output: acgtb []
// Sum_i acgtb [i] = 1
#endif
char complementaryNucleotide (char wildNucleotide);
// Return: in extDnaAlphabet
// Possible blank is preserved
// Requires: wildNucleotide in extDnaAlphabet
char getUnionNucleotide (const string& charSet);
// Return: in extDnaAlphabet + '-'; if empty set then ' '
// Requires: CharSet is in extDnaAlphabet + '-'
char getIntersectNucleotide (const char* charSet);
// GetIintersectChar
// Return: in extDnaAlphabet + '-'; if empty set then ' '
// Requires: CharSet is in extDnaAlphabet + '-'
bool nucleotideMatch (char wildNucleotide1,
char wildNucleotide2);
// Return: true if the nucleotides of wildNucleotide1 and wildNucleotide2 intersect
#if 0
bool NucleotideSeqMatch (const char* Seq1,
const char* Seq2);
// Requires: Seq1|2 [] - in extDnaAlphabet
bool MoreGeneralNucleotide (char WildNucleotide1,
char WildNucleotide2);
// Return: true if the nucleotides of WildNucleotide1 are a superset
// of those of WildNucleotide2 (i.e., WildNucleotide1 is
// more general than or equal to WildNucleotide2)
void SelectSpecificNucleotides (char* CharSet);
// Delete more general nucleotides
// Update: CharSet []
// Requires: CharSet is in extDnaAlphabet + '-'
size_t CountAmbiguousNucleotides (const char* seq);
// Input: seq - sparse sequence
char* getReverseDna (const char* Source);
// Return: malloc ()
// Requires: source is in extDnaAlphabet + '-'
#endif
string& reverseDna (string &seq);
// Requires: seq is in extDnaAlphabet + '-'
char codon2aa (const char codon [3],
Gencode gencode,
bool lowercasePossibleStartCodon);
// Return: in extTermPeptideAlphabet; lowercasePossibleStartcodon => lower-case
// Requires: codon[] is in extDnaAlphabet
inline size_t dna2codons_len (size_t dna_len)
{ return dna_len ? (dna_len - 1) / 3 + 1 : 0; }
#if 0
inline bool CodonMatch (const char Codon1 [3],
const char Codon2 [3])
{ return NucleotideSeqMatch (Codon1, Codon2); }
// Requires: Codon1|2 [] - in extDnaAlphabet
inline bool TerminatorCodon (const char Codon [3])
{ return Codon2AA (Codon) == '*'; }
bool MayBeTerminatorCodon (const char Codon [3]);
size_t Dna2PeptidePos (size_t DnaPos,
Frame frame,
size_t DnaLen);
size_t Peptide2DnaPos (size_t PeptidePos,
Frame frame,
size_t DnaLen);
#endif
struct Dna : Seq
{
static constexpr double stdMinComplexity {2.0}; // PAR
Dna () = default;
Dna (const string &name_arg,
size_t seqLen,
bool sparse_arg)
: Seq (name_arg, seqLen, sparse_arg)
{}
Dna (const string &name_arg,
const string &seq_arg,
bool sparse_arg)
: Seq (name_arg, seq_arg, sparse_arg)
{}
Dna (LineInput &fasta,
size_t reserveLen,
bool sparse_arg)
: Seq (fasta, reserveLen, sparse_arg, false)
{}
Dna (Multifasta &fasta,
size_t reserveLen,
bool sparse_arg);
Dna* copy () const final
{ return new Dna (*this); }
void saveText (ostream& os) const override
{ printCase (os, false); }
const Dna* asDna () const final
{ return this; }
const char* getSeqAlphabet () const final
{ return sparse ? extSparseDnaAlphabet : extDnaAlphabet; }
bool isAmbiguous (char c) const final
{ return ! strchr (dnaAlphabet, c); }
double getComplexityInt (size_t start,
size_t end) const final;
#if 0
void PrintHTML (bool UpperCase,
PHRED_SCORE MinGoodQual) const;
SEQ_ANNOT_LIST* GetAnnotList (int GoodStart = 0) const;
// List: "seq", "Score 10", "Score 1"
// Qual
void CreateQual ();
// Requires: Qual == nullptr
// Postcondition: Qual != nullptr
bool GoodQual () const;
// Return: false if there are wrong values in Qual []
void CopyQual (const PHRED_SCORE* SourceQual);
// Invokes: CreateQual (), GoodQual ()
void ReadQual (const char* FName);
// Input: FName: FASTA-file with phred quality scores
// Output: Qual
// Invokes: CreateQual (), GoodQual ()
// Requires: Qual != nullptr
void SetQual (PHRED_SCORE DefaultScore);
PHRED_SCORE GetMinQual () const;
PHRED_SCORE GetMaxQual () const;
void Qual2MeanVar (size_t Start,
size_t End,
double &Mean,
double &Var) const;
// Output: Mean, Var (unbiased)
void Qual2N (PHRED_SCORE MinQual,
bool GapCoded);
// If Qual [i] < MinQual then seq [i] := 'N'|'n'
// Delete Qual
void MinScore2Pos (PHRED_SCORE MinScore,
size_t &Start,
size_t &End) const;
// Output: Start, End; valid if Start < End
void QualSaveFile (FILE* F,
PHRED_SCORE DefaultScore) const;
char* Qual2String () const;
// Return: new [], May be nullptr
void DeleteStart (size_t NewStart);
#endif
Dna* makeComplementary () const;
// Return: Reverse and complementary Dna; !nullptr
void reverse ();
// Invokes: makeComplementary ()
Peptide makePeptide (Frame frame,
Gencode gencode,
bool lowercaseStartcodon,
bool firstStartCodon2M,
size_t &translationStart) const;
// Output: translationStart
// Requires: firstStartCodon2M => lowercaseStartcodon
Peptide cds2prot (Gencode gencode,
bool trunc5,
bool trunc3,
bool hasStopCodon,
bool allowExtraStopCodon) const;
// Return: if !trunc3 && hasStopCodon then no '*'
// Invokes: makePeptide(1,gencode,true,true)
Vector<Peptide> getOrfs (Frame frame,
Gencode gencode,
size_t len_min) const;
// Input: len_min: min. Peptide length without 'X'
// Return: Peptide: from '*' to '*'
#if 0
bool ExistsPeptide () const;
// Return: true if there is peptide w/o an internal stop codon in some frame
bool LongestCompleteCDS (size_t MinProteinLen,
size_t &Start,
size_t &End) const;
// Output: Start, End - position after last nt;
// (End - Start) % 3 = 0
// End <= strlen (seq)
// Valid if true
// Return: true if found
void RefineSparse (char* Target,
byte* TargetQual) const;
// Target[]='n' or seq[] is ambiguous
// Update: Target []: sparse sequence
// Qual []; if Target [i] = '-' then TargetQual [i] = 0
bool ContainsAmbiguity () const;
size_t GetAmbiguousPrefixEnd () const;
// = length of ambiguous nt prefix
size_t GetAmbiguousSuffixStart () const;
bool DeleteAmbiguousPrefix ();
// Return: True if non-empty prefix is deleted
bool DeleteAmbiguousSuffix ();
// Return: True if non-empty suffix is trimmed
void TrimN (bool GapCoded);
size_t TrimBadStart (size_t WindowLen,
PROBABILITY MaxAmbigFraction) const;
// Return: Good sequence start
size_t TrimBadEnd (size_t WindowLen,
PROBABILITY MaxAmbigFraction) const;
// Return: Good sequence end
size_t TrimLowComplexityStart () const;
// Return: Good sequence start
size_t TrimLowComplexityEnd () const;
// Return: Good sequence end
#endif
// Poly-nucleotide segment at the end of seq[]
bool polyNucWindow (size_t start,
size_t windowLen,
char nucleotide) const;
// Return: true if most of the nucleotides in the window are nucleotide
size_t findPolyNucEnd (size_t windowLen,
char nucleotide) const;
// Find poly-nucleotide segment at the end of seq
// Return: start of the poly-nucleotide segment; seq.size() if not found
// Invokes: polyNucWindow ()
// PolyA
bool polyAWindow (size_t start,
size_t windowLen) const
{ return polyNucWindow (start, windowLen, 'a'); }
size_t findPolyA (size_t windowLen) const
{ return findPolyNucEnd (windowLen, 'a'); }
size_t removePolyA (size_t windowLen)
{ const size_t polyA_start = findPolyA (windowLen);
const size_t polyA_len = seq. size () - polyA_start;
seq. erase (polyA_start);
return polyA_len;
}
// Non-idempotent
#if 0
// PolyA inside seq[]
void FindPolyAWindow (int &BestStart,
int &BestEnd,
double &MaxWeight) const;
// Output: BestStart = -1 iff MaxWeight < 0
bool DiNucLowComplexity (int &BestStart,
int &BestEnd,
double &MaxWeight,
int &MaxMonoNucLen) const;
// Find a low-complexity segement by di-nucleotide statistics
// Output: Valid if true
// MaxWeight: >= 20: significant
// MaxMonoNucLen: inside [BestStart, BestEnd]
// Return: true if found, MaxWeight >= 0
size_t NucleotideFreq (NT_PROBABILITY acgtb,
size_t Start,
size_t End,
uint Step) const;
// Return: number of nucleotides couned
// Output: acgtb []; sum = 1
size_t NucleotidePairFreq (PROBABILITY acgtb [5] [5]) const;
// Input: Start, End -??
// Return: strlen (seq)
// Output: acgtb [] []; sum = 1
double GetMeltTemp (double DnaConc = 300.0,
double SaltConc = 50.0,
double MgConc = 1.5) const;
// Thermodynamic (base-stacking calculated) T_m (melting temperature)
// Input: DnaConc is the primer concentration, nM
// SaltConc is monovalent cation (K+ and Na+) concentration, mM
// MgConc, mM
// Return: The melting temperature, in C^0
// Requires: strlen (seq) is between 7 and 35
// From: http://www-genome.wi.mit.edu/ftp/distribution/software/primer.0.5/primer.c,
// now Primer3 (http://frodo.wi.mit.edu/primer3/primer3_code.html)
// VectorNTI makes the same results
PRIMER* MakePrimer (size_t PrimerLen,
size_t SearchStart,
size_t SearchEnd,
PHRED_SCORE MinQuality,
uint MaxMaxRepeat,
PROBABILITY MaxGCShare,
double DnaConcentration,
double SaltConcentration,
double MgConcentration,
double MinMeltTemp,
double MaxMeltTemp,
double TargetMeltTemp) const;
// From 5' to 3'
// Return: Best primer, May be nullptr
// Primer inside the segment [SearchStart, SearchEnd]
// Requires: TargetMeltTemp between MinMeltTemp and MaxMeltTemp
// Invokes: GetMeltTemp ()
PROBABILITY NucleotideFreqFrame (size_t Start,
Frame frame,
size_t Count,
const char* Alphabet) const;
void GetOpenFrameProb (size_t Start,
size_t Count,
PROBABILITY FrameProb [3]) const;
// Output: FrameProb []
// Author: Max Troukhan
bool GetOrf (int &CDS_Start,
bool &FL5,
int &CDS_End,
bool &FL3,
const CodStatistics* StatTable,
double Stress) const;
// Output: CDS_Start|End - may be outside seq
// CDS_End - next position after the last known codon of the CDS
// FL5|3
// Valid if result is true
// CDS_Start < CDS_End
// Return: true if Orf is found
double CompareOrfs (const CodStatistics* StatTable1,
const CodStatistics* StatTable2) const;
// Return: Positive iff seq is more probable to be of organism of StatTable1
// CompareOrfs (a,b) = - CompareOrfs (b,a)
double Orf2Strand (const CodStatistics* StatTable) const;
// Return: Positive iff the positive strand of seq is more probable than the negative strand
// Orf2Strand (a) = - Orf2Strand (Reverse(a))
// Input: Neightborhood: to search start/stop codon, in nts
// Output: if true then CDS_Start < CDS_End and (CDS_End - CDS_Start) % 3 = 0
// Return: true iff FL
bool CDS_Start_nr2Orf (int CDS_Start_nr,
int Neighborhood,
int &CDS_Start,
int &CDS_End) const;
bool CDS_End_nr2Orf (int CDS_End_nr,
int Neighborhood,
int &CDS_Start,
int &CDS_End) const;
// Output: CDS_Start: minimum possible
#endif
};
struct FastaDna : Dna
{
FastaDna (const string &fName,
size_t reserveLen,
bool sparse_arg)
: Dna (* unique_ptr<LineInput> (getLineInput (fName)), reserveLen, sparse_arg)
{}
};
#if 0
class DNA_COLLECTION: public _SEQ_COLLECTION
// of Dna*
{
typedef _SEQ_COLLECTION inherited;
public:
DNA_COLLECTION (): inherited () {}
DNA_COLLECTION (const char* FName,
bool GapCoded);
Dna* GetDna (size_t i) const
{ return (Dna*) GetSeq (i); }
void SaveQual (const char* FName,
uint DefaultQualScore) const;
};
#endif
/////////////////////////// Peptide ///////////////////////////////
size_t aa2num (char wildAminoacid);
// Return: if wildAminoacid is in peptideAlphabet then position in peptideAlphabet
// else if wildAminoacid is terminator then 20
// else if wildAminoacid is in peptideWildcards then 21
// else error
// Case-insensitive
bool moreGeneralAminoacid (char wildAminoacid1,
char wildAminoacid2);
// Return: true if the aminoacids of wildAminoacid1 is a superset of those of wildAminoacid2
// (i.e., wildAminoacid1 is more general than or equal to wildAminoacid2)
// Requires: wildAminoacid1|2 in extTermPeptideAlphabet
inline bool aaMatch (char aa1,
char aa2)
{ return moreGeneralAminoacid (aa1, aa2)
|| moreGeneralAminoacid (aa2, aa1);
}
typedef double AlignScore;
struct SubstMat : Root
// Substitution matrix for protein alignment
// NCBI: e.g., /am/ftp-blast/matrices/BLOSUM62
/* ------ default -----
matrix gap_open gap_extent
-------- -------- ----------
BLOSUM90 10 1
BLOSUM80 10 1
BLOSUM62 11 1
BLOSUM50 13 2
BLOSUM45 14 2
PAM250 14 2
PAM70 10 1
PAM30 9 1
IDENTITY 15 2
*/
{
static constexpr const char* chars {"ARNDCQEGHILKMFPSTWYVBZX*"};
static constexpr size_t sim_size = 128;
AlignScore sim [sim_size] [sim_size];
explicit SubstMat (const string &fName);
void qc () const override;
// Print anomalies
void saveText (ostream& os) const override;
bool goodChar (size_t i) const
{ return sim [i] [i] == sim [i] [i]; } // Not a NaN
AlignScore getSubstitutionDist (size_t row,
size_t col) const
{ return sim [row] [row] + sim [col] [col] - 2 * sim [row] [col]; }
AlignScore getDeletionDist (size_t row,
AlignScore gap_sim) const
{ return sim [row] [row] - 2 * gap_sim; }
};
struct PeptideOrf : Root
// DNA -> translation of a segment -> ORF
{
size_t translationStart {no_index};
// Start of the peptide translation
// !strand => !translationStart
Strand strand {0};
// In the translation which starts from translationStart
size_t start {no_index};
// May be: no_index
bool startM {false};
// Peptide::isStartAa(peptide->seq[start])
size_t stop {no_index};
// >= start
bool stopTerminator {false};
// peptide->seq[stop] == *terminator
PeptideOrf (size_t translationStart_arg,
Strand strand_arg,
const Peptide* peptide,
size_t start_arg);
// Input: peptide: DNA segment translation
PeptideOrf (size_t translationStart_arg,
Strand strand_arg,
size_t start_arg,
bool startM_arg,
size_t stop_arg,
bool stopTerminator_arg)
: translationStart (translationStart_arg)
, strand (strand_arg)
, start (start_arg)
, startM (startM_arg)
, stop (stop_arg)
, stopTerminator (stopTerminator_arg)
{}
// In peptide translated from the DNA
explicit PeptideOrf (istream &is)
{ int iStrand;
is >> translationStart >> iStrand >> start >> stop >> startM >> stopTerminator;
strand = (char) iStrand;
}
PeptideOrf () = default;
void qc () const override;
void saveText (ostream &os) const override
{ os << translationStart
<< '\t' << (int) strand
<< '\t' << start
<< '\t' << stop
<< '\t' << startM
<< '\t' << stopTerminator;
}
// Requires: !empty()
bool empty () const override
{ return start == no_index; }
bool good (size_t size_min) const
{ return ! empty ()
&& startM
&& stopTerminator
&& size () >= size_min;
}
// Requires; !empty()
size_t size () const
{ return stop - start; }
// Without the terminator
size_t dnaPos (size_t pos) const
{ const size_t dnaLen = 3 * pos;
return strand == 1 ? (translationStart + dnaLen) : (translationStart - dnaLen);
}
// Requires: (bool)frame
size_t cdsStart () const
{ return dnaPos (start); }
size_t cdsStop () const
{ return dnaPos (stop + stopTerminator); }
Peptide* toPeptide (const Peptide* peptide) const;
// Input: peptide: used in Peptide::getOrfs()
};
struct Peptide : Seq
{
static constexpr const size_t stdAveLen {400}; // PAR
static constexpr double stdMinComplexity {2.5}; // PAR
bool pseudo {false};
Peptide () = default;
Peptide (const string &name_arg,
size_t seqLen,
bool sparse_arg)
: Seq (name_arg, seqLen, sparse_arg)
{}
Peptide (const string &name_arg,
const string &seq_arg,
bool sparse_arg)
: Seq (name_arg, seq_arg, sparse_arg)
{}
Peptide (LineInput &fasta,
size_t reserveLen,
bool sparse_arg)
: Seq (fasta, reserveLen, sparse_arg, true)
{}
Peptide (Multifasta &fasta,
size_t reserveLen,
bool sparse_arg);
Peptide* copy () const final
{ return new Peptide (*this); }
void qc () const override;
void saveText (ostream& os) const override
{ printCase (os, true); }
const Peptide* asPeptide () const final
{ return this; }
const char* getSeqAlphabet () const final
{ return sparse ? extSparseTermPeptideAlphabet : extTermPeptideAlphabet; }
bool isAmbiguous (char c) const final
{ return c == 'X'; }
bool hasInsideStop () const
{ const size_t pos = seq. find ('*');
return pos != string::npos && pos != seq. size () - 1;
}
bool trimStop ()
{ return trimSuffix (seq, "*"); }
bool isDescriptionPartial () const;
static bool isStartAa (char aa)
{ return aa != '*' && isLower (aa); }
// Requires: after codon2aa(,,true)
Vector<PeptideOrf> getPeptideOrfs (size_t translationStart,
Strand strand,
bool includeInitial,
bool longestOnly,
size_t len_min) const;
double getComplexityInt (size_t start,
size_t end) const final;
// Return: >= 0; -1 <=> seq has an amino acid not in *peptideAlphabet
double getSelfSimilarity (const SubstMat &mat,
size_t start = 0,
size_t stop = 0) const;
// Input: start, stop = 0 => whole sequence
double getSimilarity (const Peptide &other,
const SubstMat &mat,
double gapOpenCost,
double gapCost) const;