1 #ifndef FOLDED_MATRIX_H 2 #define FOLDED_MATRIX_H 8 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 10 #pragma warning(disable:4267) 12 #pragma warning(disable:4244) 18 #include <boost/noncopyable.hpp> 19 #include <boost/scoped_ptr.hpp> 20 #include <boost/bind.hpp> 21 #include <boost/ref.hpp> 25 #include "SecStruct.h" 33 Matrix(
unsigned int length) : len_(length) {
34 unsigned int size_ = len_*len_;
36 std::fill(m_, m_ + size_, 0);
39 ~
Matrix() {
delete [] m_; }
40 const EnergyValue&
get(
int x,
int y)
const{
return m_[x*len_+y]; }
41 void set(
int x,
int y,
EnergyValue val) { m_[x*len_ + y] = val; }
42 unsigned int getLength()
const {
return len_; }
49 typedef boost::scoped_ptr<SecStructProxy> SecStructProxyPtr;
61 virtual int getSplitIndex()
const = 0;
64 virtual int getLength()
const = 0;
68 return energy_.getEnergy( *getNucleotide(index_A), *getNucleotide(index_B) );
86 EnergyValue getSecStructEnergy() {
return structure_energy_; }
91 findDepthRecur(structure, 0, strategy_.getLength() - 1);
95 std::ostream& printMatrix(std::ostream& os,
int print_width)
const ;
97 std::ostream& printStructures(std::ostream& os,
int print_width)
const;
100 unsigned int max_foldings_;
106 SecStructProxyPtr proxy_;
111 SecStruct& findDepthRecur(
SecStruct& structure,
int first_idx,
int second_idx )
const;
125 class SecStructCount {
127 explicit SecStructCount(
const SecStruct& sec_struct)
128 : counter_(1), sec_struct_(sec_struct) {}
129 SecStructCount(
const SecStructCount& c)
130 : counter_(c.counter_), sec_struct_(c.sec_struct_) {}
134 bool operator==(
const SecStructCount& s)
const {
return sec_struct_ == s.sec_struct_; }
136 bool operator<(
const SecStructCount& s)
const {
return sec_struct_ < s.sec_struct_; }
139 const SecStruct&
get()
const {
return sec_struct_; }
141 void addPair(
const ConnectPair& pair) { sec_struct_.addPair(pair); }
144 bool dec()
const { --counter_;
return counter_ > 0; }
146 void inc()
const { ++counter_; }
148 int getCounter()
const {
return counter_; }
150 SecStructCount& operator=(
const SecStructCount&);
151 mutable int counter_;
157 inline std::ostream&
operator<<(std::ostream& os,
const SecStructCount& sec_struct_count) {
158 os << sec_struct_count.get() <<
"[" << sec_struct_count.getCounter() <<
"]";
162 typedef std::set<SecStructCount> SecStructParts;
163 typedef SecStructParts::iterator StructPartIter;
167 class StructPartManager : boost::noncopyable {
169 StructPartManager(
unsigned int max_foldings) : max_foldings_(max_foldings) { }
171 StructPartIter begin() {
return structures_.begin(); }
172 StructPartIter end() {
return structures_.end(); }
174 unsigned int size()
const {
return structures_.size(); }
177 StructPartIter addPair(StructPartIter iter,
const ConnectPair& pair ) {
178 SecStructCount new_struct(iter->get());
179 new_struct.addPair(pair);
181 return insert(new_struct);
185 StructPartIter insert(
const SecStructCount& sec_struct_count) {
186 StructPartIter new_iter = structures_.find(sec_struct_count);
187 if(new_iter != structures_.end() )
189 else if(structures_.size() < max_foldings_)
190 new_iter = structures_.insert(sec_struct_count).first;
194 void remove(StructPartIter iter) {
196 structures_.erase(iter);
199 SecStructParts structures_;
200 unsigned int max_foldings_;
204 struct SecStructIterLessFunctor {
205 bool operator()(
const StructPartIter& itA,
const StructPartIter& itB)
const {
206 return itA->get() < itB->get();
214 struct StructPartCollection :
public std::set<StructPartIter,SecStructIterLessFunctor> {
217 void incPartsCount() {
218 std::for_each( begin(), end(), boost::bind( &SecStructCount::inc, boost::bind(&StructPartIter::operator*,_1) ) );
222 void decPartsCount() {
223 std::for_each( begin(), end(), boost::bind( &SecStructCount::dec, boost::bind(&StructPartIter::operator*,_1) ) );
229 void joinOne(
const StructPartIter& sec_struct) {
230 StructPartCollection::iterator found = find(sec_struct);
238 void join(
const StructPartCollection& collection) {
239 std::for_each(collection.begin(), collection.end(), boost::bind(&StructPartCollection::joinOne,
this, _1) );
245 bool addNewSecStruct(
const StructPartIter& sec_struct,
const ConnectPair& pair,
246 StructPartManager& manager) {
247 StructPartIter new_iter = manager.addPair(sec_struct, pair);
248 if(new_iter == manager.end())
258 StructPartManager& manager, StructPartCollection& new_collection)
const {
260 for(StructPartCollection::const_iterator it = begin(); it != end(); ++it )
261 if(!new_collection.addNewSecStruct(*it, pair, manager) )
269 inline std::ostream&
operator<<(std::ostream& os,
const StructPartCollection& collection) {
270 std::transform(collection.begin(), collection.end(),
271 std::ostream_iterator<SecStructCount>(os,
""),
272 boost::bind(&StructPartIter::operator*,_1) );
277 struct ActivePoint :
public std::pair<int,int> {
279 explicit ActivePoint(
int x,
int y) : std::pair<int,int>(x,y) {}
282 bool operator<(
const ActivePoint& p) {
284 return first < p.first;
285 return p.second < second;
290 std::ostream&
operator<<(std::ostream& os,
const ActivePoint& p) {
291 os <<
'(' << p.first <<
',' << p.second <<
')';
298 class FoldActivePoints {
301 explicit FoldActivePoints(
const ActivePoint& start,
const StructPartIter start_struct_iter) {
302 addActivePoint(start);
303 folded_parts_.insert(start_struct_iter);
306 FoldActivePoints(
const FoldActivePoints& bef)
307 : active_points_(bef.active_points_), folded_parts_(bef.folded_parts_) {}
309 ~FoldActivePoints() { }
310 bool operator==(
const FoldActivePoints& p)
const {
return active_points_ == p.active_points_; }
311 bool operator<(
const FoldActivePoints& p)
const {
return active_points_ < p.active_points_; }
314 bool hasActivePoint()
const {
return !active_points_.empty(); }
317 ActivePoint getFirstActivePoint() {
318 assert( !active_points_.empty() );
319 ActivePoint out = *(active_points_.begin());
320 active_points_.erase( active_points_.begin() );
325 void addActivePoint(
const ActivePoint& active_point) { active_points_.insert(active_point); }
328 void join(
const FoldActivePoints& f)
const { folded_parts_.join( f.folded_parts_ ); }
332 bool addPair(
const ConnectPair& pair, StructPartManager& manager) {
333 StructPartCollection new_folded_parts_;
334 bool out = folded_parts_.addPair(pair, manager, new_folded_parts_ );
335 swap( folded_parts_, new_folded_parts_ );
340 StructPartCollection& getFoldedParts() {
return folded_parts_; }
343 const StructPartCollection& getFoldedParts()
const {
return folded_parts_; }
346 friend std::ostream&
operator<<(std::ostream& os,
const FoldActivePoints& state);
349 std::set<ActivePoint> active_points_;
351 mutable StructPartCollection folded_parts_;
355 inline std::ostream&
operator<<(std::ostream& os,
const FoldActivePoints& state) {
357 std::copy(state.active_points_.begin(), state.active_points_.end(), std::ostream_iterator<ActivePoint>(os,
"") );
358 os << std::endl <<
"structs: " << state.getFoldedParts() << std::endl;
365 typedef std::set<FoldActivePoints> StatesColection;
367 bool empty()
const {
return states_.empty(); }
369 unsigned int size()
const {
return states_.size(); }
373 FoldActivePoints getFirstState() {
374 assert( !states_.empty() );
375 FoldActivePoints out = *(states_.begin());
376 states_.erase( states_.begin() );
381 void addState(
const FoldActivePoints& new_state) {
382 addOrJoin(new_state);
386 void addStateMove(
const FoldActivePoints& state,
387 const ActivePoint& new_point) {
388 FoldActivePoints new_state(state);
389 new_state.addActivePoint(new_point);
390 addOrJoin(new_state);
392 void addStateJump(
const FoldActivePoints& state,
393 const ActivePoint& point_jump1,
394 const ActivePoint& point_jump2) {
395 FoldActivePoints new_state(state);
396 new_state.addActivePoint(point_jump1);
397 new_state.addActivePoint(point_jump2);
398 addOrJoin(new_state);
401 StatesColection states_;
404 void addOrJoin(
const FoldActivePoints& new_state) {
405 StatesColection::iterator found = states_.find(new_state);
406 if(found != states_.end() )
407 found->join(new_state);
409 states_.insert(new_state);
419 : matrix_(matrix), strategy_(strategy)
421 StructPartManager sec_struct_manager(max_foldings);
423 SecStructCount start_sec_struct(sec_struct_empty);
424 StructPartIter start_struct_iter = sec_struct_manager.insert(start_sec_struct);
425 ActivePoint start_point(0, strategy_.getLength() - 1);
427 states.addState( FoldActivePoints(start_point, start_struct_iter) );
428 find( states, sec_struct_manager );
431 std::transform( sec_struct_manager.begin(), sec_struct_manager.end(),
432 std::inserter( possible_structures_, possible_structures_.begin() ),
433 bind(&SecStructCount::get, _1) );
436 const SecStructures& getStructures()
const {
return possible_structures_; }
438 std::ostream& printStructures(std::ostream& os,
int print_width)
const;
446 void find(States& states, StructPartManager& sec_struct_manager)
const;
449 inline FoldedMatrix::FoldedMatrix(
const FoldedMatrixStrategy& strategy,
unsigned int max_foldings )
450 : strategy_(strategy), max_foldings_(max_foldings), matrix_(strategy_.getLength() ), proxy_(0L)
453 structure_energy_ = matrix_.get(0,matrix_.getLength()-1 );
457 if( proxy_.get() == 0L )
458 proxy_.reset(
new SecStructProxy(matrix_, strategy_, max_foldings_) );
459 return proxy_->getStructures();
462 inline std::ostream& FoldedMatrix::printStructures(std::ostream& os,
int print_width)
const {
463 if(proxy_.get() != 0L )
464 proxy_->printStructures(os,print_width);
466 os <<
"sec struct not calculated" << std::endl;
472 inline int findMaxVal(
const Matrix& matrix,
475 int left=matrix.get(i,j-1);
476 int down=matrix.get(i+1,j);
477 int downleft= matrix.get(i+1,j-1) + strategy.
getEnergy(i,j);
478 return (std::max)( (std::max)(left, down), downleft );
484 inline void FoldedMatrix::makeMatrix() {
486 int length = matrix_.getLength();
487 int split_point = strategy_.getSplitIndex();
491 for(n = 1; n < 4 && n < length; ++n) {
492 int i = (std::max)( split_point - n, 0);
494 for(;(i < split_point) && (i < length-1) && (j < length); ++i,++j) {
495 int max_v = findMaxVal( matrix_, strategy_, i, j);
496 matrix_.set(i,j,max_v);
501 for(n=4; n<length; ++n){
504 for( ;(i < length-1) && ( j<length );++i, ++j) {
505 int max_val = findMaxVal( matrix_, strategy_, i, j);
507 for(
int k=0; k<j-i; ++k) {
508 int loopval = matrix_.get(i,i+k)+matrix_.get(k+i+1,j);
509 if( loopval > max_val)
512 matrix_.set(i,j,max_val);
518 inline std::ostream& FoldedMatrix::printMatrix(std::ostream& os,
int print_width)
const {
519 for(
int i=0;i < strategy_.getLength();++i) {
520 os.width(print_width);
524 os <<
"j/"<< std::endl;
525 os.width(print_width);
526 for(
int i=0;i < strategy_.getLength();++i){
527 os.width(print_width);
528 os <<*strategy_.getNucleotide(i);
531 os <<
"i"<< std::endl;
534 for(;i < strategy_.getSplitIndex();++i){
535 os.width(print_width);
540 for(;i < strategy_.getLength(); ++i) {
541 os.width(print_width);
548 for(
int i=0;i<strategy_.getLength();++i) {
549 for(
int j=0;j<strategy_.getLength();++j){
550 os.width(print_width);
552 os << matrix_.get(i,j);
558 os<<*strategy_.getNucleotide(i);
567 inline std::ostream& SecStructProxy::printStructures(std::ostream& os,
int print_width)
const {
568 os <<
"sequence length: "<< strategy_.getLength() << std::endl;
569 os <<
"found: " << possible_structures_.size() <<
" structures" << std::endl;
571 for(
int i = 0; i < strategy_.getLength(); ++i ) {
572 os.width(print_width);
573 os << *strategy_.getNucleotide(i);
578 for(; i < strategy_.getSplitIndex(); ++i){
579 os.width(print_width);
582 for(; i < strategy_.getLength();++i) {
583 os.width(print_width);
584 os<<i - strategy_.getSplitIndex();
589 std::copy( possible_structures_.begin(), possible_structures_.end(), std::ostream_iterator<SecStruct>(os,
"\n") );
594 inline SecStruct& FoldedMatrix::findDepthRecur(
SecStruct& structure,
int x,
int y)
const {
599 if(current_energy == 0)
602 if(current_energy == matrix_.get(x,y-1) )
603 return findDepthRecur( structure, x, y-1 );
604 else if(current_energy == matrix_.get(x+1,y) )
605 return findDepthRecur( structure, x+1, y);
606 else if(current_energy == matrix_.get(x+1,y-1) + strategy_.getEnergy(x,y) ) {
608 structure.
addPair(
ConnectPair( strategy_.getNucleotide(x), strategy_.getNucleotide(y) ) );
609 return findDepthRecur( structure, x+1, y-1);
612 for(
int k=x; k < y; ++k)
613 if(current_energy == (matrix_.get(x,k)+matrix_.get(k+1,y) ) ) {
615 findDepthRecur(struct_one, x, k);
617 findDepthRecur(struct_two, k+1, y);
618 structure.
append(struct_one);
619 structure.
append(struct_two);
628 inline void SecStructProxy::find(States& states, StructPartManager& sec_struct_manager)
const 630 while(!states.empty() ) {
633 FoldActivePoints state = states.getFirstState();
637 ActivePoint curr_point = state.getFirstActivePoint();
639 int x = curr_point.first;
640 int y = curr_point.second;
645 if(current_energy == 0) {
647 if( state.hasActivePoint() ) {
649 states.addState(state);
657 state.getFoldedParts().decPartsCount();
660 bool left = current_energy == matrix_.get(x,y-1);
661 bool down = current_energy == matrix_.get(x+1,y);
662 bool diag = current_energy == (matrix_.get(x+1,y-1) + strategy_.getEnergy(x,y) );
666 state.getFoldedParts().incPartsCount();
667 states.addStateMove(state, ActivePoint(x, y-1) );
671 state.getFoldedParts().incPartsCount();
672 states.addStateMove(state, ActivePoint(x+1, y) );
676 state.getFoldedParts().incPartsCount();
679 ConnectPair current_pair( strategy_.getNucleotide(x), strategy_.getNucleotide(y) );
680 if( state.addPair(current_pair, sec_struct_manager) )
681 states.addStateMove(state, ActivePoint(x+1, y-1) );
684 if(!left && !down && !diag) {
686 for(
int k=x; k < y; ++k)
687 if(current_energy == (matrix_.get(x,k)+matrix_.get(k+1,y) ) ) {
689 state.getFoldedParts().incPartsCount();
690 states.addStateJump(state, ActivePoint(x, k), ActivePoint(k+1, y) );
702 #endif //FOLDED_MATRIX_H
The pair of nucleotides which are join by Watson-Crick interaction.
Definition: SecStruct.h:30
int getEnergy(int index_A, int index_B) const
Definition: FoldedMatrix.h:67
int EnergyValue
Definition: EnergyNucleo.h:15
matrix - 2D array with indexing
Definition: FoldedMatrix.h:31
std::set< SecStruct > SecStructures
Definition: SecStruct.h:104
Definition: FoldedMatrix.h:52
const SecStructures & getStructures()
Definition: FoldedMatrix.h:456
the maps between pair of nucleotides and its energy
Definition: EnergyNucleo.h:37
void append(const SecStruct &s)
Definition: SecStruct.h:79
void addPair(const ConnectPair &p)
Definition: SecStruct.h:76
std::ostream & operator<<(std::ostream &os, const Chain &chain)
Definition: Chain.h:162
SecStruct findInDepth() const
Definition: FoldedMatrix.h:89
Definition: FoldedMatrix.h:416
Definition: FoldedMatrix.h:76
the secondary structure
Definition: SecStruct.h:55