Implement serialize for sequence
authorDiane Trout <diane@caltech.edu>
Wed, 9 Aug 2006 01:19:24 +0000 (01:19 +0000)
committerDiane Trout <diane@caltech.edu>
Wed, 9 Aug 2006 01:19:24 +0000 (01:19 +0000)
This manages to save Sequence, motif, and annot using boost::serialization.

I added a serialize_simple to test_sequence.cpp

On the down side with this patch mussa now depends on boost::serialize
libraries.

(And it was harder to get working than I'd expected. I missed the fact
that there seems to be limits about sending non-const objects to an
archive class. I also found it useful to put the
template<class Archive> serialize() function into the header with
and to mark the class with BOOST_CLASS_EXPORT(). If one includes
the boost/archive/*archive classes before including sequence.hpp,
the BOOST_CLASS_EXPORT will force the appropriate template to be
instantiated.)

CMakeLists.txt
alg/sequence.cpp
alg/sequence.hpp
alg/test/CMakeLists.txt
alg/test/test_sequence.cpp
py/CMakeLists.txt
py/test/CMakeLists.txt
qui/test/CMakeLists.txt

index d94df4864d7351d402799656165dcee5554769c6..854ba6e213ce126cc1c9ff6b45269c33b8512a1f 100644 (file)
@@ -45,6 +45,7 @@ TARGET_LINK_LIBRARIES(mussagl
                         ${OPENGL_gl_LIBRARY}
                         ${BOOST_PROGRAM_OPTIONS_LIBRARY}
                         ${BOOST_FILESYSTEM_LIBRARY}
+                        ${BOOST_SERIALIZATION_LIBRARY}
                       )
 
 IF(USE_PYTHON)
index b2f76c7c4f6d9107c59215e6293dd12ae1a0afae..e2961c2c7bf60de6d7339070bd4ab9990365c4eb 100644 (file)
@@ -679,7 +679,6 @@ Sequence::load_museq(fs::path load_file_path, int seq_num)
   load_file.close();
 }
 
-
 std::string
 Sequence::rc_motif(std::string a_motif)
 {
@@ -943,3 +942,4 @@ void Sequence::find_sequences(std::list<Sequence>::iterator start,
   }
 }
 
+
index c576c3eb45de72f600b2e0d12abaff5361f9f7cf..9cb3c85fa433839ced63995fd4168f5d0cb24f14 100644 (file)
 
 #include <boost/filesystem/path.hpp>
 #include <boost/filesystem/fstream.hpp>
+
+#include <boost/serialization/base_object.hpp>
+#include <boost/serialization/list.hpp>
+#include <boost/serialization/string.hpp>
+#include <boost/serialization/utility.hpp>
+#include <boost/serialization/export.hpp>
+
 #include <list>
 #include <string>
 #include <vector>
@@ -36,7 +43,19 @@ struct annot
   std::string name;
 
   friend bool operator==(const annot& left, const annot& right);
+private:
+  // boost::serialization support
+  friend class boost::serialization::access;
+  template<class Archive>
+  void serialize(Archive& ar, const unsigned int /*version*/) {
+    ar & begin;
+    ar & end;
+    ar & type;
+    ar & name;
+  }
 };
+BOOST_CLASS_EXPORT(annot);
+
 
 /* The way that motifs are found currently doesn't really 
  * indicate that the match was a reverse compliment
@@ -45,10 +64,21 @@ struct motif : public annot
 {
   std::string sequence;
 
+  motif() :  annot(), sequence("") {};
   //! this constructor is for when we're adding motifs to our annotations
   motif(int begin, std::string motif);
   ~motif();
+
+  // boost::serialization support
+private:
+  friend class boost::serialization::access;
+  template<class Archive>
+  void motif::serialize(Archive& ar, const unsigned int /*version*/) {
+    ar & boost::serialization::base_object<annot>(*this);
+    ar & sequence;
+  }
 };
+BOOST_CLASS_EXPORT(motif);
 
 //! sequence track for mussa.
 class Sequence : public std::string
@@ -66,6 +96,17 @@ class Sequence : public std::string
     //! look for a string sequence type and and it to an annotation list
     void add_string_annotation(std::string a_seq, std::string name);
 
+    // boost::serialization support
+    friend class boost::serialization::access;
+    template<class Archive>
+    void Sequence::serialize(Archive& ar, const unsigned int /*version*/) {
+      ar & boost::serialization::base_object<std::string>(*this);
+      ar & header;
+      ar & species;
+      ar & annots;
+      ar & motif_list;
+    }
+
   public:
     Sequence();
     ~Sequence();
@@ -141,6 +182,8 @@ class Sequence : public std::string
 
     void save(boost::filesystem::fstream &save_file);
     void load_museq(boost::filesystem::path load_file_path, int seq_num); 
+
 };
+BOOST_CLASS_EXPORT(Sequence);
 
 #endif
index 8a34c1c91b6b8e030f056641a401385914d25262..681d4e2df9766dbd0061460cd84f864b5043a638 100644 (file)
@@ -21,9 +21,10 @@ ADD_EXECUTABLE(unittest ${SOURCES})
 ADD_TEST(core_test ${CMAKE_BINARY_DIR}/alg/test/unittest)
 LINK_DIRECTORIES(${CMAKE_BINARY_DIR}/alg})
 TARGET_LINK_LIBRARIES(unittest 
+                        mussa_core 
                         ${BOOST_UNIT_TEST_LIBRARY} 
                         ${BOOST_FILESYSTEM_LIBRARY}
-                        mussa_core 
+                        ${BOOST_SERIALIZATION_LIBRARY}
                         ${OPENGL_gl_LIBRARY})
 
 SET_TARGET_PROPERTIES(
index 990e193fda17ac1b7b1a97fb29d16b375f1c7c75..f7ea70d6a6b1f1fe8ce2d2d4662a75e88b248068 100644 (file)
@@ -7,6 +7,9 @@ namespace fs=boost::filesystem;
 #include <iostream>
 #include <sstream>
 
+#include <boost/archive/text_oarchive.hpp>
+#include <boost/archive/text_iarchive.hpp>
+
 #include "alg/sequence.hpp"
 #include "mussa_exceptions.hpp"
 
@@ -418,3 +421,25 @@ BOOST_AUTO_TEST_CASE( get_name )
   seq.set_fasta_header("fasta human");
   BOOST_CHECK_EQUAL( seq.get_name(), "fasta human");
 }
+
+BOOST_AUTO_TEST_CASE( serialize_simple )
+{
+  std::string seq_string = "AAGGCCTT";
+  Sequence seq(seq_string);
+
+  std::ostringstream oss;
+  // allocate/deallocate serialization components
+  {
+    boost::archive::text_oarchive oarchive(oss);
+    const Sequence& const_seq(seq);
+    BOOST_CHECK_EQUAL(seq, const_seq);
+    oarchive << const_seq;
+  }
+
+  Sequence seq_loaded;
+  {
+    std::istringstream iss(oss.str());
+    boost::archive::text_iarchive iarchive(iss);
+    iarchive >> seq_loaded;
+  }
+}  
index 151f7acea4d0352e220e1b17a0dc363a54ac24c1..5750a5263644f1d21b2e03c356108cd01eb7d179 100644 (file)
@@ -34,6 +34,7 @@ IF(BOOST_PYTHON_LIBRARY)
                           mussa_qui
                           ${BOOST_PYTHON_LIBRARY}
                           ${BOOST_FILESYSTEM_LIBRARY}
+                          ${BOOST_SERIALIZATION_LIBRARY}
                           ${OPENGL_gl_LIBRARY}
                           ${PYTHON_LIBRARIES}
                           ${QT_LIBRARIES} 
index 8957b7c601a5aa58953cbdb1972126fbb286ce2a..bf03795f88203d4aa8b7290492c3c4bd954d77eb 100644 (file)
@@ -24,6 +24,7 @@ TARGET_LINK_LIBRARIES(mussa_python_test
                         ${OPENGL_gl_LIBRARY}
                         ${BOOST_PROGRAM_OPTIONS_LIBRARY}
                         ${BOOST_FILESYSTEM_LIBRARY}
+                        ${BOOST_SERIALIZATION_LIBRARY}
                         ${BOOST_PYTHON_LIBRARY}
                         ${PYTHON_LIBRARIES}
                         ${BOOST_UNIT_TEST_LIBRARY} 
index 3765a45e7e34cc1fc2fa5687de6f4e3fce91e9f5..feff58e226b947251291e1a194c1a4cb30cb5032 100644 (file)
@@ -20,6 +20,7 @@ SET(libs
         ${OPENGL_gl_LIBRARY}
         ${BOOST_PROGRAM_OPTIONS_LIBRARY}
         ${BOOST_FILESYSTEM_LIBRARY}
+        ${BOOST_SERIALIZATION_LIBRARY}
         ${QT_QTTEST_LIBRARY}
       )