From 73291a940f65e9db8cf6cfdb996a79e43498ab84 Mon Sep 17 00:00:00 2001 From: Niklaus Johner <nij2003@med.cornell.edu> Date: Thu, 10 Nov 2011 10:13:51 -0500 Subject: [PATCH] Added a function to find the index of a sequence in an alignment from its name AlignmentHandle::FindSequenceIndex() --- modules/seq/base/pymod/export_sequence.cc | 1 + modules/seq/base/src/alignment_handle.cc | 5 +++++ modules/seq/base/src/alignment_handle.hh | 4 +++- modules/seq/base/src/impl/sequence_list_impl.cc | 12 ++++++++++++ modules/seq/base/src/impl/sequence_list_impl.hh | 2 ++ 5 files changed, 23 insertions(+), 1 deletion(-) diff --git a/modules/seq/base/pymod/export_sequence.cc b/modules/seq/base/pymod/export_sequence.cc index 407633775..7b7631041 100644 --- a/modules/seq/base/pymod/export_sequence.cc +++ b/modules/seq/base/pymod/export_sequence.cc @@ -337,6 +337,7 @@ void export_sequence() .def("GetResidue", &AlignmentHandle::GetResidue) .def("AddSequence", &AlignmentHandle::AddSequence) .def("FindSequence", &AlignmentHandle::FindSequence) + .def("FindSequenceIndex", &AlignmentHandle::FindSequenceIndex) .def("Copy", &AlignmentHandle::Copy) .def("ToString", &AlignmentHandle::ToString) .def("GetLength", &AlignmentHandle::GetLength) diff --git a/modules/seq/base/src/alignment_handle.cc b/modules/seq/base/src/alignment_handle.cc index 3e4ed4256..8432d3c90 100644 --- a/modules/seq/base/src/alignment_handle.cc +++ b/modules/seq/base/src/alignment_handle.cc @@ -166,6 +166,11 @@ ConstSequenceHandle AlignmentHandle::FindSequence(const String& name) const return ConstSequenceHandle(impl_->FindSequence(name)); } +int AlignmentHandle::FindSequenceIndex(const String& name) const +{ + this->CheckValidity(); + return impl_->FindSequenceIndex(name); +} void AlignmentHandle::Cut(int start, int end) { diff --git a/modules/seq/base/src/alignment_handle.hh b/modules/seq/base/src/alignment_handle.hh index 9a109215b..5a59cef72 100644 --- a/modules/seq/base/src/alignment_handle.hh +++ b/modules/seq/base/src/alignment_handle.hh @@ -97,7 +97,9 @@ public: /// If several sequences have the same name, the first matching sequence will /// be returned. ConstSequenceHandle FindSequence(const String& name) const; - + + int FindSequenceIndex(const String& name) const; + /// \brief attach view to given sequence /// \sa SequenceHandle::AttachView(const mol::EntityView&) void AttachView(int seq_index, const mol::EntityView& view); diff --git a/modules/seq/base/src/impl/sequence_list_impl.cc b/modules/seq/base/src/impl/sequence_list_impl.cc index f0ef01531..bd332872b 100644 --- a/modules/seq/base/src/impl/sequence_list_impl.cc +++ b/modules/seq/base/src/impl/sequence_list_impl.cc @@ -58,6 +58,18 @@ SequenceImplPtr SequenceListImpl::FindSequence(const String& name) const return SequenceImplPtr(); } + +int SequenceListImpl::FindSequenceIndex(const String& name) const +{ + for (size_t i=0; i<list_.size(); ++i) { + if (list_[i]->GetName()==name) { + return int(i); + } + } + return -1; +} + + String SequenceListImpl::ToString(int width) const { std::stringstream buffer; diff --git a/modules/seq/base/src/impl/sequence_list_impl.hh b/modules/seq/base/src/impl/sequence_list_impl.hh index 7be941f73..52443dcd6 100644 --- a/modules/seq/base/src/impl/sequence_list_impl.hh +++ b/modules/seq/base/src/impl/sequence_list_impl.hh @@ -65,6 +65,8 @@ public: SequenceImplPtr FindSequence(const String& name) const; + int FindSequenceIndex(const String& name) const; + String ToString(int width=80) const; SequenceListImplPtr Slice(int first, int n) const; -- GitLab