From f3b6e8fa194217e3c386a556430d496cd2e09ee7 Mon Sep 17 00:00:00 2001 From: Diane Trout Date: Wed, 9 Aug 2006 01:19:24 +0000 Subject: [PATCH] Implement serialize for sequence 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 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 | 1 + alg/sequence.cpp | 2 +- alg/sequence.hpp | 43 ++++++++++++++++++++++++++++++++++++++ alg/test/CMakeLists.txt | 3 ++- alg/test/test_sequence.cpp | 25 ++++++++++++++++++++++ py/CMakeLists.txt | 1 + py/test/CMakeLists.txt | 1 + qui/test/CMakeLists.txt | 1 + 8 files changed, 75 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d94df48..854ba6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,7 @@ TARGET_LINK_LIBRARIES(mussagl ${OPENGL_gl_LIBRARY} ${BOOST_PROGRAM_OPTIONS_LIBRARY} ${BOOST_FILESYSTEM_LIBRARY} + ${BOOST_SERIALIZATION_LIBRARY} ) IF(USE_PYTHON) diff --git a/alg/sequence.cpp b/alg/sequence.cpp index b2f76c7..e2961c2 100644 --- a/alg/sequence.cpp +++ b/alg/sequence.cpp @@ -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::iterator start, } } + diff --git a/alg/sequence.hpp b/alg/sequence.hpp index c576c3e..9cb3c85 100644 --- a/alg/sequence.hpp +++ b/alg/sequence.hpp @@ -16,6 +16,13 @@ #include #include + +#include +#include +#include +#include +#include + #include #include #include @@ -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 + 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 + void motif::serialize(Archive& ar, const unsigned int /*version*/) { + ar & boost::serialization::base_object(*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 + void Sequence::serialize(Archive& ar, const unsigned int /*version*/) { + ar & boost::serialization::base_object(*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 diff --git a/alg/test/CMakeLists.txt b/alg/test/CMakeLists.txt index 8a34c1c..681d4e2 100644 --- a/alg/test/CMakeLists.txt +++ b/alg/test/CMakeLists.txt @@ -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( diff --git a/alg/test/test_sequence.cpp b/alg/test/test_sequence.cpp index 990e193..f7ea70d 100644 --- a/alg/test/test_sequence.cpp +++ b/alg/test/test_sequence.cpp @@ -7,6 +7,9 @@ namespace fs=boost::filesystem; #include #include +#include +#include + #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; + } +} diff --git a/py/CMakeLists.txt b/py/CMakeLists.txt index 151f7ac..5750a52 100644 --- a/py/CMakeLists.txt +++ b/py/CMakeLists.txt @@ -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} diff --git a/py/test/CMakeLists.txt b/py/test/CMakeLists.txt index 8957b7c..bf03795 100644 --- a/py/test/CMakeLists.txt +++ b/py/test/CMakeLists.txt @@ -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} diff --git a/qui/test/CMakeLists.txt b/qui/test/CMakeLists.txt index 3765a45..feff58e 100644 --- a/qui/test/CMakeLists.txt +++ b/qui/test/CMakeLists.txt @@ -20,6 +20,7 @@ SET(libs ${OPENGL_gl_LIBRARY} ${BOOST_PROGRAM_OPTIONS_LIBRARY} ${BOOST_FILESYSTEM_LIBRARY} + ${BOOST_SERIALIZATION_LIBRARY} ${QT_QTTEST_LIBRARY} ) -- 2.30.2