From 71e280b4ec6faba353988637dafc2ebdfc514ff1 Mon Sep 17 00:00:00 2001
From: Gabriel Studer <gabriel.studer@unibas.ch>
Date: Sun, 30 Aug 2015 15:26:37 +0200
Subject: [PATCH] Function to extract subpart of HMM

---
 modules/seq/base/pymod/export_hmm.cc |  1 +
 modules/seq/base/src/hmm.cc          | 21 +++++++++++++++++++++
 modules/seq/base/src/hmm.hh          |  2 ++
 3 files changed, 24 insertions(+)

diff --git a/modules/seq/base/pymod/export_hmm.cc b/modules/seq/base/pymod/export_hmm.cc
index 8ca851939..72a072215 100644
--- a/modules/seq/base/pymod/export_hmm.cc
+++ b/modules/seq/base/pymod/export_hmm.cc
@@ -52,6 +52,7 @@ void export_hmm()
   class_<HMM, HMMPtr>("HMM", init<>())
     .def("Load", &HMM::Load).staticmethod("Load")
     .def("AddColumn", &HMM::push_back)
+    .def("Extract", &HMM::Extract)
     .add_property("null_model", make_function(&HMM::GetNullModel,
                   return_value_policy<copy_const_reference>()))
     .add_property("columns", 
diff --git a/modules/seq/base/src/hmm.cc b/modules/seq/base/src/hmm.cc
index 390a55b82..fd953d5f5 100644
--- a/modules/seq/base/src/hmm.cc
+++ b/modules/seq/base/src/hmm.cc
@@ -159,6 +159,27 @@ String HMM::GetSequence() const{
   return ss.str();
 }
 
+HMMPtr HMM::Extract(uint from, uint to){
+
+  if(to <= from){
+    throw std::runtime_error("Second index must be bigger than first one!");
+  }
+
+  if(to >= this->size()){
+    throw std::runtime_error("Invalid index!");
+  }
+
+  HMMPtr return_hmm(new HMM);
+  return_hmm->SetNullModel(null_model_);
+  for(uint i = from; i < to; ++i){
+    return_hmm->push_back(columns_[i]);
+  }
+
+  return return_hmm;
+}
+
+
+
 
 Real HMM::GetAverageEntropy() const {
   Real n_eff=0.0;
diff --git a/modules/seq/base/src/hmm.hh b/modules/seq/base/src/hmm.hh
index 82826948e..68b83db1e 100644
--- a/modules/seq/base/src/hmm.hh
+++ b/modules/seq/base/src/hmm.hh
@@ -194,6 +194,8 @@ class HMM {
 
   String GetSequence() const;
 
+  HMMPtr Extract(uint from, uint to);
+
   //some functions to make it behave like a vector
 
   size_t size() const { return columns_.size(); }
-- 
GitLab