fix boost::python sequence interface
authorDiane Trout <diane@caltech.edu>
Fri, 30 Jun 2006 20:37:58 +0000 (20:37 +0000)
committerDiane Trout <diane@caltech.edu>
Fri, 30 Jun 2006 20:37:58 +0000 (20:37 +0000)
Making sequence a subclass of string caused some problems for
the boost::python sequence class. This updates the interface so it
works again.

alg/sequence.cpp
alg/sequence.hpp
py/CMakeLists.txt
py/module.cpp
py/sequence.cpp
py/test/TestSequence.py

index b8488dcbc70042551362f90655949189897eca2e..ee0f9f7118d315be3c015a92646762d512f1b772 100644 (file)
@@ -443,7 +443,7 @@ void Sequence::set_species(const std::string& name)
   species = name;
 }
 
-const std::string& Sequence::get_species() const
+std::string Sequence::get_species() const
 {
   return species;
 }
index 407168ebc0d5e298dccc86fbd6f29771e07f95f9..47813657f7f976174948bef1c4ba53d0c2320df6 100644 (file)
@@ -104,7 +104,7 @@ class Sequence : public std::string
     const std::list<annot>& annotations() const;
     const std::list<motif>& motifs() const;
     void set_species(const std::string &);
-    const std::string& get_species() const;
+    std::string get_species() const;
 
     //! return a subsequence, copying over any appropriate annotation
     Sequence subseq(int start=0, int count = std::string::npos) const;
index a717e5dd4c953afe32da0eff076b866447e81913..8e0a6ff767bd7a3f951989af0e724a8754227a1a 100644 (file)
@@ -23,13 +23,13 @@ INCLUDE( ${QT_USE_FILE} )
   
   #SET_SOURCE_FILES_PROPERTIES(${SOURCES} COM
 
-  ADD_LIBRARY(_mussa MODULE ${SOURCES})
+  ADD_LIBRARY(mussa MODULE ${SOURCES})
   ADD_LIBRARY(mussa_py STATIC ${SOURCES})
-  ADD_CUSTOM_TARGET(_mussa.so ALL
-                     COMMAND cp lib_mussa.so _mussa.so
-                     DEPENDS _mussa)
+  ADD_CUSTOM_TARGET(mussa.so ALL
+                     COMMAND cp libmussa.so mussa.so
+                     DEPENDS mussa)
   LINK_DIRECTORIES(${MUSSA_BINARY_DIR}/alg)
-  TARGET_LINK_LIBRARIES(_mussa 
+  TARGET_LINK_LIBRARIES(mussa 
                           mussa_core
                           mussa_qui
                           ${BOOST_PYTHON_LIBRARY}
@@ -41,7 +41,8 @@ INCLUDE( ${QT_USE_FILE} )
 
   IF(PYTHON_EXECUTABLE)
     SET(PYTHON_TEST_DIR ${CMAKE_SOURCE_DIR}/py/test/)
-    ADD_TEST(sequence ${PYTHON_EXECUTABLE} ${PYTHON_TEST_DIR}/TestSequence.py)
+    ADD_TEST(sequence_py 
+             ${PYTHON_EXECUTABLE} ${PYTHON_TEST_DIR}/TestSequence.py)
   ENDIF(PYTHON_EXECUTABLE)
 #ELSE(BOOST_PYTHON_LIB)
 #ENDIF(BOOST_PYTHON_LIB)
index 9f3a1452866bbb7fdf038494f298952f613e4284..448b523b89db0f516480495c20c2d9e97c37fa42 100644 (file)
@@ -8,7 +8,7 @@ void export_nway_paths();
 void export_sequence();
 void export_mussa_window();
 
-BOOST_PYTHON_MODULE(_mussa)
+BOOST_PYTHON_MODULE(mussa)
 {
   export_conserved_path();
   export_glsequence();
index b64565a99e15444eba28338288e9ad4d61d0a49a..3bdca90781e3bd978a4a1704f54e2a9ccbcebd2f 100644 (file)
@@ -1,4 +1,5 @@
 #include <boost/python.hpp>
+#include <boost/iterator.hpp>
 #include <boost/python/return_internal_reference.hpp>
 #include <boost/python/return_by_value.hpp>
 #include <boost/python/return_value_policy.hpp>
@@ -7,15 +8,19 @@ using namespace boost::python;
 #include <string>
 #include "alg/glsequence.hpp"
 
+PyObject* seq_to_string(const Sequence& s) {
+  ::PyString_FromStringAndSize(s.data(), s.size());
+}
+
 void export_sequence()
 {
   class_<Sequence>("Sequence")
     .def(init<std::string>())
-    //.def("__str__", &Sequence::get_seq, return_value_policy<return_by_value>())
+    .def("__str__", &seq_to_string)
     .def("size", &Sequence::size)
     .def("__len__", &Sequence::size)
     .add_property("header", &Sequence::get_header, &Sequence::set_header)
-    //.add_property("seq", &Sequence::get_seq, &Sequence::set_seq)
+    .add_property("species", &Sequence::get_species, &Sequence::set_species)
     .def("rcseq", &Sequence::rev_comp, return_value_policy<return_by_value>())
   ;
 }
index 23cdc93e22b3528616ae9ce2498e90845826d344..274fbdba4cd751da41dec9221b9cc79a888eff3a 100644 (file)
@@ -14,7 +14,20 @@ class TestSequence(unittest.TestCase):
     self.failUnless(len(seq_text) == len(s))
     self.failUnless(len(s) == s.size())
     self.failUnless(str(s) == seq_text)
+    self.failUnless(str(s) == seq_text)
+
+  def testNames(self):
+    seq_text = "AAGGCCTT"
+    header_text = "hello"
+    species = "Goo Gooerific"
+
+    s = mussa.Sequence(seq_text)
+
+    s.header = header_text
+    s.species = species
+    self.failUnless(s.header == header_text)
+    self.failUnless(s.species == species)
+     
 def suite():
   return unittest.makeSuite(TestSequence, 'test')