From 67c1aa39a44e6967c8b912dfa78b720dbfea037b Mon Sep 17 00:00:00 2001 From: Diane Trout Date: Thu, 29 Jun 2006 21:40:53 +0000 Subject: [PATCH] set_motifs should erase the previous motifs I renamed add_motifs to set_motifs as I was expecting that the function would clear out the previous motifs that were stored in the analysis. this should fix ticket:95 --- alg/mussa.cpp | 3 ++- alg/mussa.hpp | 15 +++++++------- alg/sequence.cpp | 5 +++-- alg/test/test_mussa.cpp | 35 ++++++++++++++++++++++++++++++-- alg/test/test_sequence.cpp | 32 ++++++++++++++++++++++++++++- qui/motif_editor/MotifEditor.cpp | 2 +- 6 files changed, 78 insertions(+), 14 deletions(-) diff --git a/alg/mussa.cpp b/alg/mussa.cpp index 5acd58e..af704e8 100644 --- a/alg/mussa.cpp +++ b/alg/mussa.cpp @@ -720,13 +720,14 @@ void Mussa::add_motif(const string& motif, const Color& color) color_mapper->appendInstanceColor("motif", motif, color); } -void Mussa::add_motifs(const vector& motifs, +void Mussa::set_motifs(const vector& motifs, const vector& colors) { if (motifs.size() != colors.size()) { throw mussa_error("motif and color vectors must be the same size"); } + motif_sequences.clear(); for(size_t i = 0; i != motifs.size(); ++i) { add_motif(motifs[i], colors[i]); diff --git a/alg/mussa.hpp b/alg/mussa.hpp index e53e727..2d497f7 100644 --- a/alg/mussa.hpp +++ b/alg/mussa.hpp @@ -144,15 +144,16 @@ public: void save_old(); void load_old(char * load_file_path, int s_num); - // manage motif lists - void add_motif(const std::string& motifs, - const Color& colors); - //! add vector of motifs and colors to our motif collection - /*! this depends on sets and color maps being unique + // manage motif lists + //! add a motif it wont be applied until update_sequences_motif is called + void add_motif(const std::string& motifs, const Color& colors); + //! add vector of motifs and colors to our motif collection + /*! this will automatically call update_sequences_motif + * this depends on sets and color maps being unique * (aka if you add the same item more than once it doesn't - * increase the size of the data structure + * increase the size of the data structure) */ - void add_motifs(const std::vector& motifs, + void set_motifs(const std::vector& motifs, const std::vector& colors); //! load motifs from an ifstream /*! The file should look something like diff --git a/alg/sequence.cpp b/alg/sequence.cpp index 7d8b12a..b9163c0 100644 --- a/alg/sequence.cpp +++ b/alg/sequence.cpp @@ -812,15 +812,16 @@ Sequence::find_motif(std::string a_motif) a_motif = motif_normalize(a_motif); //std::cout << "motif is: " << a_motif << std::endl; - if (a_motif != "") + if (a_motif.size() > 0) { //std::cout << "Sequence: none blank motif\n"; motif_scan(a_motif, &motif_match_starts); a_motif_rc = rc_motif(a_motif); // make sure not to do search again if it is a palindrome - if (a_motif_rc != a_motif) + if (a_motif_rc != a_motif) { motif_scan(a_motif_rc, &motif_match_starts); + } } return motif_match_starts; } diff --git a/alg/test/test_mussa.cpp b/alg/test/test_mussa.cpp index 2ab594a..2cf19c0 100644 --- a/alg/test/test_mussa.cpp +++ b/alg/test/test_mussa.cpp @@ -165,11 +165,42 @@ BOOST_AUTO_TEST_CASE( mussa_add_motif ) Mussa m1; m1.append_sequence("AAAAGGGGTTTT"); m1.append_sequence("GGGCCCCTTGGTT"); - m1.add_motifs(motifs, colors); + m1.set_motifs(motifs, colors); int first_size = m1.motifs().size(); BOOST_CHECK_EQUAL( first_size, 1 ); - m1.add_motifs(motifs, colors); + BOOST_REQUIRE(first_size > 0); + BOOST_CHECK_EQUAL(*(m1.motifs().begin()), motifs.front()); + // make sure that our sequences have the right number of motifs + BOOST_CHECK_EQUAL(m1.sequences()[0]->motifs().size(), 1); + BOOST_CHECK_EQUAL(m1.sequences()[1]->motifs().size(), 1); // because of rc + + // verify that setting the motif clears the arrays + m1.set_motifs(motifs, colors); BOOST_CHECK_EQUAL( first_size, m1.motifs().size() ); + // make sure that our sequences have the right number of motifs + BOOST_CHECK_EQUAL(m1.sequences()[0]->motifs().size(), 1); + BOOST_CHECK_EQUAL(m1.sequences()[1]->motifs().size(), 1); + + // add a different motif + motifs.clear(); + motifs.push_back("CCTTGG"); + BOOST_CHECK_EQUAL(motifs.size(), 1); + m1.set_motifs(motifs, colors); + BOOST_CHECK_EQUAL(m1.motifs().size(), 1); + BOOST_REQUIRE(m1.motifs().size() > 0); + BOOST_CHECK_EQUAL(*(m1.motifs().begin()), motifs.front()); + BOOST_CHECK_EQUAL(m1.sequences()[0]->motifs().size(), 0); + BOOST_CHECK_EQUAL(m1.sequences()[1]->motifs().size(), 1); + + // try a motif that doesn't exist + motifs.clear(); + motifs.push_back("CCTTGG"); + BOOST_CHECK_EQUAL(motifs.size(), 1); + m1.set_motifs(motifs, colors); + BOOST_CHECK_EQUAL(m1.motifs().size(), 1); + BOOST_CHECK_EQUAL(m1.sequences()[0]->motifs().size(), 0); + BOOST_CHECK_EQUAL(m1.sequences()[1]->motifs().size(), 1); + } static void diff --git a/alg/test/test_sequence.cpp b/alg/test/test_sequence.cpp index 1f3256e..cf543fd 100644 --- a/alg/test/test_sequence.cpp +++ b/alg/test/test_sequence.cpp @@ -226,9 +226,11 @@ BOOST_AUTO_TEST_CASE( sequence_motifs ) BOOST_CHECK( motif_end == s1.motifs().end() ); BOOST_CHECK( motif_i == motif_end ); - + // this shouldn't show up s1.add_motif(bogus); BOOST_CHECK( s1.motifs().begin() == s1.motifs().end() ); + BOOST_CHECK_EQUAL( s1.motifs().size(), 0 ); + s1.add_motif(m); BOOST_CHECK( s1.motifs().begin() != s1.motifs().end() ); BOOST_CHECK_EQUAL( s1.motifs().size(), 2 ); @@ -340,6 +342,34 @@ BOOST_AUTO_TEST_CASE( subseq_annotation_test ) } } +BOOST_AUTO_TEST_CASE( motif_annotation_update ) +{ + string s("CCGTCCCCCATCATCGCGGCTCTCCGAGAGTCCCGCGCCCCACTCCCGGC" + "ACCCACCTGACCGCGGGCGGCTCCGGCCCCGCTTCGCCCCACTGCGATCA" + "GTCGCGTCCCGCAGGCCAGGCACGCCCCGCCGCTCCCGCTGCGCCGGGCG" + "TCTGGGACCTCGGGCGGCTCCTCCGAGGGGCGGGGCAGCCGGGAGCCACG" + "CCCCCGCAGGTGAGCCGGCCACGCCCACCGCCCGTGGGAAGTTCAGCCTC" + "GGGGCTCCAGCCCCGCGGGAATGGCAGAACTTCGCACGCGGAACTGGTAA" + "CCTCCAGGACACCTCGAATCAGGGTGATTGTAGCGCAGGGGCCTTGGCCA" + "AGCTAAAACTTTGGAAACTTTAGATCCCAGACAGGTGGCTTTCTTGCAGT"); + Sequence seq(s); + + // starting conditions + BOOST_CHECK_EQUAL(seq.annotations().size(), 0); + BOOST_CHECK_EQUAL(seq.motifs().size(), 0); + seq.add_annotation(annot(0, 10, "0-10", "0-10")); + seq.add_annotation(annot(10, 20, "10-20", "10-20")); + seq.add_annotation(annot(0, 20, "0-20", "0-20")); + BOOST_CHECK_EQUAL(seq.annotations().size(), 3); + BOOST_CHECK_EQUAL(seq.motifs().size(), 0); + seq.add_motif("CCGTCCC"); + BOOST_CHECK_EQUAL(seq.annotations().size(), 3); + BOOST_CHECK_EQUAL(seq.motifs().size(), 1); + seq.clear_motifs(); + BOOST_CHECK_EQUAL(seq.annotations().size(), 3); + BOOST_CHECK_EQUAL(seq.motifs().size(), 0); +} + BOOST_AUTO_TEST_CASE( out_operator ) { string s("AAGGCCTT"); diff --git a/qui/motif_editor/MotifEditor.cpp b/qui/motif_editor/MotifEditor.cpp index b74826e..28e1afe 100644 --- a/qui/motif_editor/MotifEditor.cpp +++ b/qui/motif_editor/MotifEditor.cpp @@ -59,7 +59,7 @@ void MotifEditor::updateMotifs() colors.push_back((*md_i)->color()); } } - analysis->add_motifs(motifs, colors); + analysis->set_motifs(motifs, colors); emit changedMotifs(); } -- 2.30.2