IntaRNA 3.4.1
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
242 Interaction( const RnaSequence & s1, const RnaSequence & s2 );
243
249 Interaction( const Interaction & toCopy );
250
259 Interaction( const InteractionRange & range );
260
264 virtual ~Interaction();
265
272 bool
273 isValid() const;
274
279 bool
280 isEmpty() const;
281
285 void
286 sort();
287
291 void
292 clear();
293
300 void
301 setSeedRange( const BasePair bp_left, const BasePair bp_right, const E_type energy );
302
310
318
331 operator= ( const Interaction & toCopy );
332
345 operator= ( const InteractionRange & range );
346
355 bool operator < ( const Interaction &i ) const;
356
362 bool operator == ( const Interaction &i ) const;
363
371 static
372 bool
373 compareEnergy( const E_type& energy, const Interaction & hasLargerE );
374
381 friend std::ostream& operator<<(std::ostream& out, const BasePair& bp);
382
389 friend std::ostream& operator<<(std::ostream& out, const Boundary& b);
390
397 friend std::ostream& operator<<(std::ostream& out, const Interaction& i);
398
418 static
419 std::string
420 dotBar( const Interaction & i, const bool fullLength = false );
421
442 static
443 std::string
444 dotBracket( const Interaction & i, const char symOpen = '(', const char symClose = ')', const bool fullLength = false);
445
446
447protected:
448
449 template < typename bpIterator >
450 static
451 std::string
452 dotSomething( bpIterator bpBegin, const bpIterator bpEnd, const bool handleSeq1, const char bpSymbol );
453
454
455};
456
457
458
462
463
464inline
466:
467 s1(&s1)
468 , s2(&s2)
469 , basePairs()
470 , energy( E_INF )
471 , seed( NULL )
472{
473}
474
476
477inline
479:
480 s1(toCopy.s1)
481 , s2(toCopy.s2)
482 , basePairs(toCopy.basePairs)
483 , energy( toCopy.energy )
484 , seed( toCopy.seed == NULL ? NULL : new SeedSet() )
485{
486 // copy seed if needed
487 if (seed != NULL) {
488 // copy all seeds
489 seed->insert(toCopy.seed->begin(), toCopy.seed->end());
490 }
491}
492
494
495inline
497:
498 s1(NULL)
499 , s2(NULL)
500 , basePairs()
501 , energy( E_INF )
502 , seed( NULL )
503{
504 // init data
505 this->operator =( range );
506}
507
509
510inline
515
517
518inline
519bool
521isEmpty() const
522{
523 return basePairs.size()==0;
524}
525
527
528inline
529void
531sort()
532{
533 // sort based on STL functionalities for vector and pair
534 std::sort(basePairs.begin(), basePairs.end());
535}
536
538
539inline
540void
542clear()
543{
544 // clear interaction base pairing information
545 basePairs.clear();
546 // clear energy
547 energy = E_INF;
548 // undo seed information
550
551}
552
554
555inline
556bool
558compareEnergy( const E_type & energy, const Interaction & hasLargerE )
559{
560 return energy < hasLargerE.energy && !(E_equal(energy,hasLargerE.energy));
561}
562
564
565inline
566bool
568operator < ( const Interaction &i ) const
569{
570#if INTARNA_IN_DEBUG_MODE
571 if (i.s1 != s1 || i.s2 != s2) throw std::runtime_error("Interaction::operator < () : comparing interactions for different sequences");
572 if (basePairs.empty() || i.basePairs.empty()) throw std::runtime_error("Interaction::operator < () : comparing empty interactions");
573#endif
574 // check if energy is larger
575 if (energy > i.energy && !(E_equal(energy, i.energy))) {
576 return false;
577 }
578 // check if energy is equal
579 if (E_equal(energy, i.energy)) {
580 return
581 // i1 is smaller
582 basePairs.begin()->first < i.basePairs.begin()->first // i1
583 // i1 is equal BUT i2 smaller
584 || (basePairs.begin()->first == i.basePairs.begin()->first // i1
585 && basePairs.begin()->second < i.basePairs.begin()->second) // i2
586 // i1 and i2 are equal BUT j1 smaller
587 || (basePairs.begin()->first == i.basePairs.begin()->first // i1
588 && basePairs.begin()->second == i.basePairs.begin()->second // i2
589 && basePairs.rbegin()->first < i.basePairs.rbegin()->first) // j1
590 // i1, i2 and j1 are equal BUT j2 smaller
591 || (basePairs.begin()->first == i.basePairs.begin()->first // i1
592 && basePairs.begin()->second == i.basePairs.begin()->second // i2
593 && basePairs.rbegin()->first == i.basePairs.rbegin()->first // j1
594 && basePairs.rbegin()->second < i.basePairs.rbegin()->second) // j2
595 // i1, i2, j1, j2 are equal BUT more base pairs
596 || (basePairs.begin()->first == i.basePairs.begin()->first // i1
597 && basePairs.begin()->second == i.basePairs.begin()->second // i2
598 && basePairs.rbegin()->first == i.basePairs.rbegin()->first // j1
599 && basePairs.rbegin()->second == i.basePairs.rbegin()->second // j2
600 && basePairs.size() > i.basePairs.size())
601 // i1, i2, j1, j2, #bps are equal BUT first seed energy smaller
602 || (basePairs.begin()->first == i.basePairs.begin()->first // i1
603 && basePairs.begin()->second == i.basePairs.begin()->second // i2
604 && basePairs.rbegin()->first == i.basePairs.rbegin()->first // j1
605 && basePairs.rbegin()->second == i.basePairs.rbegin()->second // j2
606 && basePairs.size() == i.basePairs.size()
607 && seed != NULL && seed->size() > 0 && i.seed != NULL && i.seed->size() > 0 && seed->begin()->energy < i.seed->begin()->energy)
608 ;
609 } else {
610 // has to have smaller energy
611 return true;
612 }
613}
614
616
617template < typename bpIterator >
618inline
619std::string
621dotSomething( bpIterator bp, const bpIterator bpEnd, const bool handleSeq1, const char bpSymbol )
622{
623 // stream to compile output
624 std::stringstream dotBracket;
625 // compile dotBracket1
626 for (bpIterator bpLast = bp; bp!=bpEnd; bp++) {
627 // fill unpaired up to current bp in seq1
628 for ( size_t u = handleSeq1 ? (bp->first - bpLast->first) : (bp->second - bpLast->second); u>1; u-- ) {
629 dotBracket <<'.';
630 }
631 // add base pair
632 dotBracket <<bpSymbol;
633 // update last bp for next round
634 bpLast = bp;
635 }
636 // return compiled string
637 return dotBracket.str();
638}
639
641
642} // namespace
643
644
645#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)
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:531
SeedSet * seed
optional: seed information
Definition Interaction.h:235
std::set< Seed > SeedSet
type of a vector containing seeds
Definition Interaction.h:218
Interaction(const RnaSequence &s1, const RnaSequence &s2)
Definition Interaction.h:465
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:621
bool operator<(const Interaction &i) const
Definition Interaction.h:568
void clear()
Definition Interaction.h:542
Interaction & operator=(const Interaction &toCopy)
virtual ~Interaction()
Definition Interaction.h:511
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:558
IndexRangeList getSeedRanges1() const
bool isEmpty() const
Definition Interaction.h:521
friend std::ostream & operator<<(std::ostream &out, const BasePair &bp)
bool isValid() const
IndexRangeList getSeedRanges2() const
Definition RnaSequence.h:29
static const size_t lastPos
Definition RnaSequence.h:57
#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