diff --git a/modules/seq/base/src/alignment_handle.cc b/modules/seq/base/src/alignment_handle.cc index ee21b144c3d3674b6dd3dc17bb8fb20e2b1ad252..3ad612da9c7a61b17d3294d7afcfc5733ff7ad08 100644 --- a/modules/seq/base/src/alignment_handle.cc +++ b/modules/seq/base/src/alignment_handle.cc @@ -62,9 +62,9 @@ int AlignmentHandle::GetResidueIndex(int seq_index, int pos) const void AlignmentHandle::AddSequence(const ConstSequenceHandle& sequence) { this->CheckValidity(); - if (impl_->GetCount()>0 && - impl_->GetSequence(0)->GetLength()!=sequence.GetLength()) { - throw InvalidAlignment(); + if (!sequence.IsValid() || (impl_->GetCount()>0 && + impl_->GetSequence(0)->GetLength()!=sequence.GetLength())) { + throw InvalidSequence(); } return impl_->AddSequence(sequence.Impl()); } @@ -124,7 +124,7 @@ int AlignmentHandle::GetCount() const AlignmentHandle AlignmentFromSequenceList(const SequenceList& seq_list) { - if (seq_list.SequencesHaveEqualLength()) { + if (seq_list.IsValid() && seq_list.SequencesHaveEqualLength()) { return AlignmentHandle(seq_list.Impl()); } throw InvalidAlignment(); @@ -188,10 +188,14 @@ void AlignmentHandle::ShiftRegion(int start, int end, int amount, { this->CheckValidity(); int cnt=0; - for (impl::SequenceListImpl::Iterator i=impl_->Begin(), - e=impl_->End(); i!=e; ++i, ++cnt) { - if (master==-1 || cnt==master) { - (*i)->ShiftRegion(start, end, amount); + if(master!=-1){ + impl::SequenceImplPtr handle = this->GetSequence(master).Impl(); + handle->ShiftRegion(start, end, amount); + } + else{ + for (impl::SequenceListImpl::Iterator i=impl_->Begin(), + e=impl_->End(); i!=e; ++i, ++cnt) { + (*i)->ShiftRegion(start, end, amount); } } } @@ -199,6 +203,9 @@ void AlignmentHandle::ShiftRegion(int start, int end, int amount, AlignedRegion AlignmentHandle::MakeRegion(int start, int n, int master) const { this->CheckValidity(); + if(start<0 || n < 0 || start >= n || start + n >= this->GetLength()){ + throw std::out_of_range("Region not valid"); + } return AlignedRegion(*this, start, start+n, master); } diff --git a/modules/seq/base/src/alignment_handle.hh b/modules/seq/base/src/alignment_handle.hh index e007b4fb3797cabc72c0da1316c79ffd11f6a96f..998618b520ed6e90fa88b81f45ddadf4f4e5dcaa 100644 --- a/modules/seq/base/src/alignment_handle.hh +++ b/modules/seq/base/src/alignment_handle.hh @@ -121,8 +121,7 @@ public: /// If set to -1, no master sequence is defined and the operations will /// affect all sequences /// - /// This method does not throw any exceptions, even if the aligned region is - /// out of bounds. + /// If the aligned region is out of bounds, a std::out_of_bounds exeception will be thrown. AlignedRegion MakeRegion(int start, int n, int master=-1) const; /// \brief get number of sequences in alignment diff --git a/modules/seq/base/src/impl/sequence_impl.cc b/modules/seq/base/src/impl/sequence_impl.cc index 070211924e0c121bdd35abb61cc368090d8a7762..5ce66685e0d90e229a93c0a9c02f9cee4fd10a20 100644 --- a/modules/seq/base/src/impl/sequence_impl.cc +++ b/modules/seq/base/src/impl/sequence_impl.cc @@ -280,6 +280,9 @@ void SequenceImpl::Replace(const String& str,int start, int end) void SequenceImpl::ShiftRegion(int start, int end, int amount) { + if(start <= end && end + amount > this->GetLength() || start + amount <= 0){ + throw std::out_of_range("Region not valid"); + } String str1=seq_string_.substr(start, end-start); if (amount<0) { String str2=seq_string_.substr(start+amount, -amount); diff --git a/modules/seq/base/src/impl/sequence_list_impl.hh b/modules/seq/base/src/impl/sequence_list_impl.hh index 0911aa9b8b136a1caa000e62eb6b88978493f4b2..93bc070353780e6c90ddc24add126b5a10560d12 100644 --- a/modules/seq/base/src/impl/sequence_list_impl.hh +++ b/modules/seq/base/src/impl/sequence_list_impl.hh @@ -41,12 +41,21 @@ public: /// \brief get number of sequences in list int GetCount() const { return list_.size(); } - SequenceImplPtr& GetSequence(int i) - { - return list_[i]; + SequenceImplPtr& GetSequence(int i) { + unsigned int index = static_cast<unsigned int>(i); + if (index<list_.size()) { + return list_[index]; + } + throw Error("Index not covered SequenceList"); } - const SequenceImplPtr& GetSequence(int i) const { return list_[i]; } + const SequenceImplPtr& GetSequence(unsigned int i) const { + unsigned int index = static_cast<unsigned int>(i); + if (index<list_.size()) { + return list_[index]; + } + throw Error("Index not covered SequenceList"); + } int GetPos(int seq_index, int residue_index) const;