set_motifs should erase the previous motifs
authorDiane Trout <diane@caltech.edu>
Thu, 29 Jun 2006 21:40:53 +0000 (21:40 +0000)
committerDiane Trout <diane@caltech.edu>
Thu, 29 Jun 2006 21:40:53 +0000 (21:40 +0000)
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
alg/mussa.hpp
alg/sequence.cpp
alg/test/test_mussa.cpp
alg/test/test_sequence.cpp
qui/motif_editor/MotifEditor.cpp

index 5acd58e6bdfeaf1a9081ee3d93c5351be79054ba..af704e8e696d64250b7b12fe9a954dda4596b089 100644 (file)
@@ -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<string>& motifs, 
+void Mussa::set_motifs(const vector<string>& motifs, 
                        const vector<Color>& 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]);
index e53e727cf170b04be7ff642364e5b75e73ffade2..2d497f7c4bd0c9b9d7d31b232e6f7ae9c8719485 100644 (file)
@@ -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<std::string>& motifs, 
+    void set_motifs(const std::vector<std::string>& motifs, 
                     const std::vector<Color>& colors);
     //! load motifs from an ifstream
     /*! The file should look something like
index 7d8b12a05b45c6763f40ec6ff3eccb20473afec1..b9163c0c6b17dc103fae39e9487850c08328de49 100644 (file)
@@ -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;
 }
index 2ab594a10107eb0bcd2c3bc82193ccad966722b7..2cf19c0c4226bfb0a3f5f5747dc535a3764f205f 100644 (file)
@@ -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 
index 1f3256ee3997bec9e32d767dfb4614aac61c9cf5..cf543fd00c603bce8b0286ed5ffafb08ef8a91c2 100644 (file)
@@ -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");
index b74826e43017299b0858a30c1f2234b013a52fe3..28e1afe46ce512a0c3411688919d39ff99c78563 100644 (file)
@@ -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();
 }