IntaRNA 3.5.0-pre
RNA-RNA interaction prediction | C++ API
Loading...
Searching...
No Matches
Interaction.h
Go to the documentation of this file.
1
2#ifndef INTARNA_INTERACTION_H_
3#define INTARNA_INTERACTION_H_
4
5#include <algorithm>
6#include <sstream>
7#include <set>
8#include <utility>
9#include <vector>
10
11#include <boost/functional/hash.hpp>
12
13#include "IntaRNA/general.h"
15#include "IntaRNA/RnaSequence.h"
17
18
19namespace IntaRNA {
20
21// dummy declaration to avoid inclusion loop
22class InteractionRange;
23
24
29
30public:
31
33 class BasePair {
34
35 public:
36
37 size_t first;
38 size_t second;
39
46 const size_t second=RnaSequence::lastPos )
48 {}
49
51 struct Hash {
57 size_t operator()(const BasePair &i ) const
58 {
59 size_t key = 0;
60 boost::hash_combine(key, i.first);
61 boost::hash_combine(key, i.second);
62 return key;
63 }
64 };
65
70 const bool
71 operator < ( const BasePair &bp ) const {
72 return (first < bp.first)
73 || (first == bp.first && second < bp.second);
74 }
75
81 const bool
82 operator == ( const BasePair &bp ) const {
83 return first == bp.first
84 && second == bp.second;
85 }
86
87 };
88
90 typedef std::vector<BasePair> PairingVec;
91
95 class Boundary {
96
97 public:
98
99 size_t i1;
100 size_t j1;
101 size_t i2;
102 size_t j2;
103
112 , const size_t i2=RnaSequence::lastPos, const size_t j2=RnaSequence::lastPos )
113 : i1(i1), j1(j1), i2(i2), j2(j2)
114 {}
115
117 struct Hash {
123 size_t operator()(const Boundary &i ) const
124 {
125 size_t key = 0;
126 boost::hash_combine(key, i.i1);
127 boost::hash_combine(key, i.j1);
128 boost::hash_combine(key, i.i2);
129 boost::hash_combine(key, i.j2);
130 return key;
131 }
132 };
133
139 const bool
140 operator == ( const Boundary & rhs ) const
141 {
142 return i1 == rhs.i1
143 && i2 == rhs.i2
144 && j1 == rhs.j1
145 && j2 == rhs.j2 ;
146 }
147
148 };
149
150
154 class Seed {
155 public:
156
161
169 , const BasePair & bp_j
170 , const E_type energy );
171
178
185 const bool
186 operator < ( const Seed &s ) const {
187 if (! E_equal(energy, s.energy )) {
188 return energy < s.energy;
189 }
190 if (bp_i.first != s.bp_i.first) {
191 return bp_i.first < s.bp_i.first;
192 }
193 if (bp_i.second != s.bp_i.second) {
194 return bp_i.second < s.bp_i.second;
195 }
196 if (bp_j.first != s.bp_j.first) {
197 return bp_j.first < s.bp_j.first;
198 }
199 return bp_j.second < s.bp_j.second;
200 }
201
207 const bool
208 operator == ( const Seed &s ) const {
209 return E_equal(energy, s.energy )
210 && bp_i == s.bp_i
211 && bp_j == s.bp_j
212 ;
213 }
214
215 };
216
218 typedef std::set<Seed> SeedSet;
219
220public:
221
224
227
230
233
236
239
245 Interaction( const RnaSequence & s1, const RnaSequence & s2 );
246
252 Interaction( const Interaction & toCopy );
253
262 Interaction( const InteractionRange & range );
263
267 virtual ~Interaction();
268
275 bool
276 isValid() const;
277
282 bool
283 isEmpty() const;
284
288 void
289 sort();
290
294 void
295 clear();
296
303 void
304 setSeedRange( const BasePair bp_left, const BasePair bp_right, const E_type energy );
305
313
321
334 operator= ( const Interaction & toCopy );
335
348 operator= ( const InteractionRange & range );
349
357 bool operator < ( const Interaction &i ) const;
358
364 bool operator == ( const Interaction &i ) const;
365
373 static
374 bool
375 compareEnergy( const E_type& energy, const Interaction & hasLargerE );
376
381 bool
382 hasEnsembleEnergy() const;
383
390 E_type
391 getRankingEnergy() const;
392
399 friend std::ostream& operator<<(std::ostream& out, const BasePair& bp);
400
407 friend std::ostream& operator<<(std::ostream& out, const Boundary& b);
408
415 friend std::ostream& operator<<(std::ostream& out, const Interaction& i);
416
436 static
437 std::string
438 dotBar( const Interaction & i, const bool fullLength = false );
439
460 static
461 std::string
462 dotBracket( const Interaction & i, const char symOpen = '(', const char symClose = ')', const bool fullLength = false);
463
464
465protected:
466
467 template < typename bpIterator >
468 static
469 std::string
470 dotSomething( bpIterator bpBegin, const bpIterator bpEnd, const bool handleSeq1, const char bpSymbol );
471
472
473};
474
475
476
480
481
482inline
484:
485 s1(&s1)
486 , s2(&s2)
487 , basePairs()
488 , energy( E_INF )
489 , ensembleEnergy( E_INF )
490 , seed( NULL )
491{
492}
493
495
496inline
498:
499 s1(toCopy.s1)
500 , s2(toCopy.s2)
501 , basePairs(toCopy.basePairs)
502 , energy( toCopy.energy )
503 , ensembleEnergy( toCopy.ensembleEnergy )
504 , seed( toCopy.seed == NULL ? NULL : new SeedSet() )
505{
506 // copy seed if needed
507 if (seed != NULL) {
508 // copy all seeds
509 seed->insert(toCopy.seed->begin(), toCopy.seed->end());
510 }
511}
512
514
515inline
517:
518 s1(NULL)
519 , s2(NULL)
520 , basePairs()
521 , energy( E_INF )
522 , ensembleEnergy( E_INF )
523 , seed( NULL )
524{
525 // init data
526 this->operator =( range );
527}
528
530
531inline
536
538
539inline
540bool
542isEmpty() const
543{
544 return basePairs.size()==0;
545}
546
548
549inline
550void
552sort()
553{
554 // sort based on STL functionalities for vector and pair
555 std::sort(basePairs.begin(), basePairs.end());
556}
557
559
560inline
561void
563clear()
564{
565 // clear interaction base pairing information
566 basePairs.clear();
567 // clear energy
568 energy = E_INF;
569 // clear optional ensemble ranking energy
571 // undo seed information
573
574}
575
577
578inline
579bool
581compareEnergy( const E_type & energy, const Interaction & hasLargerE )
582{
583 return energy < hasLargerE.getRankingEnergy()
584 && !(E_equal(energy,hasLargerE.getRankingEnergy()));
585}
586
588
589inline
590bool
596
598
599inline
600E_type
606
608
609inline
610bool
612operator < ( const Interaction &i ) const
613{
614#if INTARNA_IN_DEBUG_MODE
615 if (i.s1 != s1 || i.s2 != s2) throw std::runtime_error("Interaction::operator < () : comparing interactions for different sequences");
616 if (basePairs.empty() || i.basePairs.empty()) throw std::runtime_error("Interaction::operator < () : comparing empty interactions");
617#endif
618 // check if energy is larger
619 const E_type rankEnergy = getRankingEnergy();
620 const E_type otherRankEnergy = i.getRankingEnergy();
621 if (rankEnergy > otherRankEnergy && !(E_equal(rankEnergy, otherRankEnergy))) {
622 return false;
623 }
624 // check if energy is equal
625 if (E_equal(rankEnergy, otherRankEnergy)) {
626 return
627 // i1 is smaller
628 basePairs.begin()->first < i.basePairs.begin()->first // i1
629 // i1 is equal BUT i2 smaller
630 || (basePairs.begin()->first == i.basePairs.begin()->first // i1
631 && basePairs.begin()->second < i.basePairs.begin()->second) // i2
632 // i1 and i2 are equal BUT j1 smaller
633 || (basePairs.begin()->first == i.basePairs.begin()->first // i1
634 && basePairs.begin()->second == i.basePairs.begin()->second // i2
635 && basePairs.rbegin()->first < i.basePairs.rbegin()->first) // j1
636 // i1, i2 and j1 are equal BUT j2 smaller
637 || (basePairs.begin()->first == i.basePairs.begin()->first // i1
638 && basePairs.begin()->second == i.basePairs.begin()->second // i2
639 && basePairs.rbegin()->first == i.basePairs.rbegin()->first // j1
640 && basePairs.rbegin()->second < i.basePairs.rbegin()->second) // j2
641 // i1, i2, j1, j2 are equal BUT more base pairs
642 || (basePairs.begin()->first == i.basePairs.begin()->first // i1
643 && basePairs.begin()->second == i.basePairs.begin()->second // i2
644 && basePairs.rbegin()->first == i.basePairs.rbegin()->first // j1
645 && basePairs.rbegin()->second == i.basePairs.rbegin()->second // j2
646 && basePairs.size() > i.basePairs.size())
647 // i1, i2, j1, j2, #bps are equal BUT first seed energy smaller
648 || (basePairs.begin()->first == i.basePairs.begin()->first // i1
649 && basePairs.begin()->second == i.basePairs.begin()->second // i2
650 && basePairs.rbegin()->first == i.basePairs.rbegin()->first // j1
651 && basePairs.rbegin()->second == i.basePairs.rbegin()->second // j2
652 && basePairs.size() == i.basePairs.size()
653 && seed != NULL && seed->size() > 0 && i.seed != NULL && i.seed->size() > 0 && seed->begin()->energy < i.seed->begin()->energy)
654 ;
655 } else {
656 // has to have smaller energy
657 return true;
658 }
659}
660
662
663template < typename bpIterator >
664inline
665std::string
667dotSomething( bpIterator bp, const bpIterator bpEnd, const bool handleSeq1, const char bpSymbol )
668{
669 // stream to compile output
670 std::stringstream dotBracket;
671 // compile dotBracket1
672 for (bpIterator bpLast = bp; bp!=bpEnd; bp++) {
673 // fill unpaired up to current bp in seq1
674 for ( size_t u = handleSeq1 ? (bp->first - bpLast->first) : (bp->second - bpLast->second); u>1; u-- ) {
675 dotBracket <<'.';
676 }
677 // add base pair
678 dotBracket <<bpSymbol;
679 // update last bp for next round
680 bpLast = bp;
681 }
682 // return compiled string
683 return dotBracket.str();
684}
685
687
688} // namespace
689
690
691#endif /* INTERACTION_H_ */
Definition IndexRangeList.h:20
Definition InteractionRange.h:17
type of a base pair index encoding
Definition Interaction.h:33
BasePair(const size_t first=RnaSequence::lastPos, const size_t second=RnaSequence::lastPos)
Definition Interaction.h:45
const bool operator==(const BasePair &bp) const
Definition Interaction.h:82
const bool operator<(const BasePair &bp) const
Definition Interaction.h:71
size_t first
index in first sequence
Definition Interaction.h:37
size_t second
index in second sequence
Definition Interaction.h:38
Definition Interaction.h:95
size_t i2
1st index in second sequence
Definition Interaction.h:101
const bool operator==(const Boundary &rhs) const
Definition Interaction.h:140
size_t i1
1st index in first sequence
Definition Interaction.h:99
size_t j1
last index in first sequence
Definition Interaction.h:100
Boundary(const size_t i1=RnaSequence::lastPos, const size_t j1=RnaSequence::lastPos, const size_t i2=RnaSequence::lastPos, const size_t j2=RnaSequence::lastPos)
Definition Interaction.h:111
size_t j2
last index in second sequence
Definition Interaction.h:102
Definition Interaction.h:154
BasePair bp_j
right-most base pair of seed
Definition Interaction.h:175
const bool operator<(const Seed &s) const
Definition Interaction.h:186
const bool operator==(const Seed &s) const
Definition Interaction.h:208
BasePair bp_i
left-most base pair of seed
Definition Interaction.h:173
Seed(const BasePair &bp_i, const BasePair &bp_j, const E_type energy)
E_type energy
overall energy of seed
Definition Interaction.h:177
Definition Interaction.h:28
static std::string dotBracket(const Interaction &i, const char symOpen='(', const char symClose=')', const bool fullLength=false)
void setSeedRange(const BasePair bp_left, const BasePair bp_right, const E_type energy)
E_type ensembleEnergy
optional ensemble energy used to rank an ensemble-selected interaction
Definition Interaction.h:235
std::vector< BasePair > PairingVec
type of a vector encoding base pair indices that are interacting
Definition Interaction.h:90
static std::string dotBar(const Interaction &i, const bool fullLength=false)
E_type energy
energy of the interaction
Definition Interaction.h:232
void sort()
Definition Interaction.h:552
SeedSet * seed
optional: seed information
Definition Interaction.h:238
std::set< Seed > SeedSet
type of a vector containing seeds
Definition Interaction.h:218
Interaction(const RnaSequence &s1, const RnaSequence &s2)
Definition Interaction.h:483
const RnaSequence * s1
the first interaction partner
Definition Interaction.h:223
friend std::ostream & operator<<(std::ostream &out, const Boundary &b)
friend std::ostream & operator<<(std::ostream &out, const Interaction &i)
static std::string dotSomething(bpIterator bpBegin, const bpIterator bpEnd, const bool handleSeq1, const char bpSymbol)
Definition Interaction.h:667
bool operator<(const Interaction &i) const
Definition Interaction.h:612
void clear()
Definition Interaction.h:563
Interaction & operator=(const Interaction &toCopy)
virtual ~Interaction()
Definition Interaction.h:532
const RnaSequence * s2
the second interaction partner
Definition Interaction.h:226
PairingVec basePairs
interacting indices
Definition Interaction.h:229
bool operator==(const Interaction &i) const
static bool compareEnergy(const E_type &energy, const Interaction &hasLargerE)
Definition Interaction.h:581
IndexRangeList getSeedRanges1() const
bool isEmpty() const
Definition Interaction.h:542
E_type getRankingEnergy() const
Definition Interaction.h:602
friend std::ostream & operator<<(std::ostream &out, const BasePair &bp)
bool isValid() const
IndexRangeList getSeedRanges2() const
bool hasEnsembleEnergy() const
Definition Interaction.h:592
Definition RnaSequence.h:29
static const size_t lastPos
Definition RnaSequence.h:57
#define E_isNotINF(e)
check if a given energy is NOT set to E_INF
Definition general.h:137
#define E_equal(e1, e2)
check if two energies are equal
Definition general.h:131
void INTARNA_CLEANUP(T *&pointer)
Definition general.h:30
Definition Accessibility.h:13
int E_type
type for energy values (energy + accessibility [ED]) (internally)
Definition general.h:78
const E_type E_INF
Definition general.h:79
hash value computation
Definition Interaction.h:51
size_t operator()(const BasePair &i) const
Definition Interaction.h:57
hash value computation
Definition Interaction.h:117
size_t operator()(const Boundary &i) const
Definition Interaction.h:123