IntaRNA 3.4.1
RNA-RNA interaction prediction | C++ API
Loading...
Searching...
No Matches
RnaSequence.h
Go to the documentation of this file.
1
2#ifndef INTARNA_RNASEQUENCE_H_
3#define INTARNA_RNASEQUENCE_H_
4
5#include <locale>
6#include <string>
7#include <vector>
8
9#include "IntaRNA/general.h"
10
11
12#ifndef VIENNA_RNA_PAIR_MAT_H
13#define VIENNA_RNA_PAIR_MAT_H
14extern "C" {
15 #include <ViennaRNA/pair_mat.h>
16}
17#endif
18
19namespace IntaRNA {
20
30
31public:
32
34 typedef std::string String_type;
35
38 typedef short Code_type;
39
41 typedef std::vector<Code_type> CodeSeq_type;
42
43
48 const static std::string SequenceAlphabetIUPAC;
49
53 const static std::string SequenceAlphabet;
54
57 static const size_t lastPos;
58
59public:
60
69 RnaSequence(const std::string& id
70 , const std::string & seqString
71 , const long idxPos0 = 1
72 , const size_t seqNumber = 1 );
73
77 virtual ~RnaSequence();
78
79
81
86 const std::string&
87 getId() const;
88
93 const size_t
94 getSeqNumber() const;
95
100 size_t
101 size() const;
102
108 long
109 getInOutIndex( const size_t i ) const;
110
117 size_t
118 getIndex( const long i ) const;
119
125 size_t
126 getReversedIndex( const size_t i ) const;
127
132 const String_type&
133 asString() const;
134
140 const CodeSeq_type&
141 asCodes() const;
142
148 bool
149 isAmbiguous() const;
150
159 bool
160 isAmbiguous( const size_t i ) const;
161
167 const bool operator == ( const RnaSequence &rna2 ) const;
168
175 friend std::ostream& operator<<(std::ostream& out, const RnaSequence& rna);
176
177
178
179public:
180
182
189 static
191 getUpperCase( const std::string & seqString );
192
193
205 static
208
220 static
222 getCodeForChar( const char nucleotide );
223
224
229 static
230 bool
231 isValidSequence( const std::string& sequence );
232
233
238 static
239 bool
240 isValidSequenceIUPAC( const std::string& sequence );
241
242
252 static
253 bool
254 areComplementary( const RnaSequence & s1, const RnaSequence & s2,
255 const size_t p1, const size_t p2 );
256
266 static
267 bool
268 isGU( const RnaSequence & s1, const RnaSequence & s2,
269 const size_t p1, const size_t p2 );
270
271protected:
272
274
275
277 static std::locale codeLocale;
278
280 static int bpGUcodes[];
281
283 std::string id;
284
287
290
293
296
299 size_t seqNumber;
300
301};
302
303
307
308
309inline
311 const std::string & id
312 , const std::string & seqString
313 , const long idxPos0
314 , const size_t seqNumber )
315 :
316 id(id)
317 , seqString(getUpperCase(seqString))
318 , seqCode(getCodeForString(this->seqString))
319 , ambiguous(this->seqString.contains('N'))
320 , idxPos0(idxPos0)
321 , seqNumber(seqNumber)
322{
323#if INTARNA_IN_DEBUG_MODE
324 if (id.size() == 0) {
325 throw std::runtime_error("RnaSequence::RnaSequence : id empty");
326 }
327 if (seqString.size() == 0) {
328 throw std::runtime_error("RnaSequence::RnaSequence : seqString empty");
329 }
330#endif
331
332}
333
335
336inline
340
342
343inline
344const std::string&
346getId() const
347{
348 return id;
349}
350
352
353inline
354const size_t
356getSeqNumber() const
357{
358 return seqNumber;
359}
360
362
363inline
364size_t
366size() const
367{
368 return seqString.size();
369}
370
372
373inline
374long
376getInOutIndex( const size_t i ) const
377{
378#if INTARNA_IN_DEBUG_MODE
379 if (i >= size()) {
380 throw std::runtime_error("RnaSequence::getInOutIndex : index "+toString(i)+" >= length "+toString(size()));
381 }
382#endif
383 // get in/out index
384 long p = idxPos0 + (long)i;
385 // check for -+1 index transition
386 if (idxPos0<0 && p>=0) {
387 p++;
388 }
389 // final in/out index
390 return p;
391}
392
394
395inline
396size_t
398getIndex( const long i ) const
399{
400 // check lower bounds
401 if (i < idxPos0) {
402 throw std::runtime_error("RnaSequence::getIndex : index "+toString(i)+" < idxPos0 "+toString(idxPos0));
403 }
404 // shift to internal indexing
405 size_t p = (size_t)(i - idxPos0);
406 // check for -+1 index transition
407 if (idxPos0<0 && i>=0) {
408 assert(i>0);
409 p--;
410 }
411 // check upper bound
412 if (p >= size()) {
413 throw std::runtime_error("RnaSequence::getIndex : index "+toString(i)+" relates to "+toString(p)+" >= length "+toString(size()));
414 }
415 // return internal index
416 return p;
417}
418
420
421inline
422size_t
424getReversedIndex( const size_t i ) const
425{
426#if INTARNA_IN_DEBUG_MODE
427 if (i >= size()) {
428 throw std::runtime_error("RnaSequence::getReversedIndex : index "+toString(i)+" >= seq.length "+toString(size()));
429 }
430#endif
431
432 return this->size() -i -1;
433}
434
436
437inline
438const
442asString() const
443{
444 return seqString;
445}
446
448
449inline
450const
454asCodes() const
455{
456 return seqCode;
457}
458
460
461inline
462bool
464isAmbiguous() const
465{
466 return ambiguous;
467}
468
470
471inline
472bool
474isAmbiguous( const size_t i ) const
475{
476 // check for ambiguous nucleotide encoding
477 return this->seqString.at(i) == 'N';
478}
479
481
482inline
486getUpperCase( const std::string & seqString )
487{
488#if INTARNA_IN_DEBUG_MODE
489 if (!isValidSequenceIUPAC(seqString)) throw std::runtime_error("RnaSequence::getUpperCase() : the given sequence contains non-IUPAC codes : '"+seqString+"'");
490#endif
491
492 // create container to fill
493 String_type seqRet(seqString.size(),'_');
494
495 for (size_t i=0; i<seqString.size(); ++i)
496 {
497 // get upper case characters
498 seqRet[i] = std::toupper(seqString.at(i),codeLocale);
499 if (seqRet[i]=='T') {
500 seqRet[i] = 'U';
501 }
502 // overwrite non-ACGU characters with N = ambiguous (no distinction needed)
503 if (!SequenceAlphabet.contains(seqRet[i])) {
504 seqRet[i] = 'N';
505 }
506 }
507
508 return seqRet;
509}
510
512
513inline
517getCodeForString( const String_type& seqString )
518{
519 // create container to fill and init with 'X'
521
522 for (size_t i=0; i<seqString.size(); ++i)
523 {
524 seqCode[i] = getCodeForChar( seqString.at(i) );
525 }
526
527 return seqCode;
528}
529
531
532inline
533bool
535isValidSequence( const std::string & sequence )
536{
537 // check whether or not the string contains unsupported characters
538 return sequence.find_first_not_of(SequenceAlphabet) == std::string::npos;
539}
540
542
543inline
544bool
546isValidSequenceIUPAC( const std::string & sequence )
547{
548 // check whether or not the string contains unsupported characters
549 return sequence.find_first_not_of(SequenceAlphabetIUPAC) == std::string::npos;
550}
551
553
554inline
555std::ostream&
556operator<<(std::ostream& out, const RnaSequence& rna)
557{
558 // add ID(SEQUENCE) to stream
559 out <<rna.id <<'(' <<rna.asString() <<')';
560 return out;
561}
562
564
565inline
569getCodeForChar( const char nucleotide )
570{
571#if INTARNA_IN_DEBUG_MODE
572 // check if nucleotide character is NOT supported
573 if (!SequenceAlphabet.contains(nucleotide))
574 throw std::runtime_error("RnaSequence::getCodeForChar() : unsupported nucleotide character '"+toString(nucleotide)+"' in sequence");
575#endif
576
577 // otherwise get encoding:
578 // use nucleotide character encoding from Vienna RNA package
579 return (Code_type)encode_char(nucleotide);
580}
581
583
584inline
585bool
587areComplementary( const RnaSequence & s1, const RnaSequence & s2,
588 const size_t p1, const size_t p2 )
589{
590#if INTARNA_IN_DEBUG_MODE
591 // check if valid positions
592 if (p1>=s1.size() || p2>=s2.size())
593 throw std::runtime_error("RnaSequence::areComplementary : index positions p1/p2 ("
594 + toString(p1)+"/"+toString(p2)
595 + ") are out of bounds s1/s2 ("
596 + toString(s1.size())+"/"+toString(s2.size())
597 +")"
598 );
599#endif
600
601 // check via VRNA util
602 return BP_pair[s1.seqCode.at(p1)][s2.seqCode.at(p2)] > 0;
603}
604
606
607inline
608bool
610isGU( const RnaSequence & s1, const RnaSequence & s2,
611 const size_t p1, const size_t p2 )
612{
613#if INTARNA_IN_DEBUG_MODE
614 // check if valid positions
615 if (p1>=s1.size() || p2>=s2.size())
616 throw std::runtime_error("RnaSequence::areComplementary : index positions p1/p2 ("
617 + toString(p1)+"/"+toString(p2)
618 + ") are out of bounds s1/s2 ("
619 + toString(s1.size())+"/"+toString(s2.size())
620 +")"
621 );
622#endif
623
624 // get bp code
625 int bpCode = BP_pair[s1.seqCode.at(p1)][s2.seqCode.at(p2)];
626
627 // check if GU pair
628 return bpCode == bpGUcodes[0] || bpCode == bpGUcodes[1] ;
629}
630
632
633inline
634const bool
636operator == ( const RnaSequence &rna2 ) const
637{
638 return // check if same object (pointer)
639 this == &rna2
640 || (
641 // ensure same lengths
642 this->size() == rna2.size()
643 // ids are identical (most likely shorter, i.e. faster check)
644 && this->id == rna2.id
645 // sequences identical
646 && this->seqString == rna2.seqString
647 );
648}
649
651
652} // namespace
653
654
655#endif /* RNASEQUENCE_H_ */
Definition RnaSequence.h:29
virtual ~RnaSequence()
Definition RnaSequence.h:337
long getInOutIndex(const size_t i) const
Definition RnaSequence.h:376
static bool isValidSequenceIUPAC(const std::string &sequence)
Definition RnaSequence.h:546
static int bpGUcodes[]
codes for G and U to check for GU base pairs
Definition RnaSequence.h:280
const CodeSeq_type & asCodes() const
Definition RnaSequence.h:454
const bool operator==(const RnaSequence &rna2) const
Definition RnaSequence.h:636
long idxPos0
Input/output index of the first sequence position.
Definition RnaSequence.h:295
static String_type getUpperCase(const std::string &seqString)
Definition RnaSequence.h:486
static const std::string SequenceAlphabetIUPAC
Definition RnaSequence.h:48
CodeSeq_type seqCode
Integer encoding of the sequence.
Definition RnaSequence.h:289
size_t getReversedIndex(const size_t i) const
Definition RnaSequence.h:424
const std::string & getId() const
Definition RnaSequence.h:346
std::vector< Code_type > CodeSeq_type
type for sequence integer encoded representation
Definition RnaSequence.h:41
const size_t getSeqNumber() const
Definition RnaSequence.h:356
RnaSequence(const std::string &id, const std::string &seqString, const long idxPos0=1, const size_t seqNumber=1)
Definition RnaSequence.h:310
bool ambiguous
Whether or not the sequence contains ambiguous nucleotide encodings.
Definition RnaSequence.h:292
static std::locale codeLocale
the locale to use for integer encoding
Definition RnaSequence.h:277
friend std::ostream & operator<<(std::ostream &out, const RnaSequence &rna)
Definition RnaSequence.h:556
static bool isGU(const RnaSequence &s1, const RnaSequence &s2, const size_t p1, const size_t p2)
Definition RnaSequence.h:610
static const size_t lastPos
Definition RnaSequence.h:57
const String_type & asString() const
Definition RnaSequence.h:442
bool isAmbiguous() const
Definition RnaSequence.h:464
String_type seqString
The sequence's string representation.
Definition RnaSequence.h:286
size_t getIndex(const long i) const
Definition RnaSequence.h:398
static bool isValidSequence(const std::string &sequence)
Definition RnaSequence.h:535
static const std::string SequenceAlphabet
Definition RnaSequence.h:53
short Code_type
Definition RnaSequence.h:38
static bool areComplementary(const RnaSequence &s1, const RnaSequence &s2, const size_t p1, const size_t p2)
Definition RnaSequence.h:587
size_t size() const
Definition RnaSequence.h:366
size_t seqNumber
Definition RnaSequence.h:299
static Code_type getCodeForChar(const char nucleotide)
Definition RnaSequence.h:569
std::string String_type
type for sequence string representation
Definition RnaSequence.h:34
static CodeSeq_type getCodeForString(const String_type &seqString)
Definition RnaSequence.h:517
std::string id
ID of this sequence.
Definition RnaSequence.h:283
#define toString(x)
Definition general.h:60
Definition Accessibility.h:13
std::ostream & operator<<(std::ostream &out, const AccessibilityConstraint &c)
Definition AccessibilityConstraint.h:481