fixes for Mussa::load_motif
authorDiane Trout <diane@caltech.edu>
Wed, 4 Oct 2006 01:02:21 +0000 (01:02 +0000)
committerDiane Trout <diane@caltech.edu>
Wed, 4 Oct 2006 01:02:21 +0000 (01:02 +0000)
If you're using references to pass variables from the boost::spirit parser
via a functor class, it might be useful to actually make sure the
class constructors parameters are references too.

This does improve how a motif file is parsed.

alg/mussa.cpp
alg/test/test_glsequence.cpp
alg/test/test_mussa.cpp

index c93990b72e81ef371cdcb615d234231c5ac3a466..47a6b0fe464e72670a0e55b2726d8001ece47de6 100644 (file)
@@ -762,7 +762,6 @@ void Mussa::set_motifs(const vector<Sequence>& motifs,
   update_sequences_motifs();
 }
 
-
 // Helper functor to append created motifs to our Mussa analysis
 struct push_back_motif {
   std::set<Sequence>& motif_set;
@@ -772,27 +771,27 @@ struct push_back_motif {
   float& red;
   float& green;
   float& blue;
+  float& alpha;
 
   push_back_motif(std::set<Sequence>& motif_set_,
                   boost::shared_ptr<AnnotationColors> color_mapper_,
                   std::string& seq_, 
                   std::string& name_,
-                  float red_, float green_, float blue_)
+                  float &red_, float &green_, float &blue_, float &alpha_)
     : motif_set(motif_set_),
       color_mapper(color_mapper_),
       seq_string(seq_),
       name(name_),
       red(red_),
       green(green_),
-      blue(blue_)
+      blue(blue_),
+      alpha(alpha_)
   {
   }
 
   void operator()(std::string::const_iterator, 
                   std::string::const_iterator) const 
   {
-    //std::cout << "motif: " << seq_string << "/" << name << endl;
-
     Sequence seq(seq_string);
     // shouldn't we have a better field than "fasta header" and speices?
     seq.set_fasta_header(name);
@@ -813,9 +812,10 @@ void Mussa::load_motifs(std::istream &in)
   const char *alphabet = Sequence::nucleic_iupac_alphabet.c_str();
   string seq;
   string name;
-  float red;
-  float green;
-  float blue;
+  float red = 1.0;
+  float green = 0.0;
+  float blue = 0.0;
+  float alpha = 1.0;
 
   // slurp our data into a string
   std::streamsize bytes_read = 1;
@@ -826,7 +826,7 @@ void Mussa::load_motifs(std::istream &in)
     data.append(buf, buf+bytes_read);
   }
   // parse our string
-  bool status = spirit::parse(data.begin(), data.end(),
+  bool ok = spirit::parse(data.begin(), data.end(),
      *( 
        ( 
         (
@@ -834,14 +834,17 @@ void Mussa::load_motifs(std::istream &in)
          +spirit::space_p
         ) >>
         !(
-          (spirit::alpha_p >> *spirit::alnum_p)[spirit::assign_a(name)]
+          (spirit::alpha_p >> *spirit::graph_p)[spirit::assign_a(name)]
           >> +spirit::space_p
         ) >>
         spirit::real_p[spirit::assign_a(red)] >> +spirit::space_p >>
         spirit::real_p[spirit::assign_a(green)] >> +spirit::space_p >>
         spirit::real_p[spirit::assign_a(blue)] >> +spirit::space_p
-       )[push_back_motif(motif_sequences, color_mapper, seq, name, red, green, blue)]
+       )[push_back_motif(motif_sequences, color_mapper, seq, name, red, green, blue, alpha)]
      )).full;
+  if (not ok) {
+    std::clog << "Error parsing motif stream " << std::endl;
+  }
   update_sequences_motifs();
 }
 
index f0bf3018bad74d536476cf4f6386c01538d1be59..179f6329fe00eb58ad86ec14afb7432758561581 100644 (file)
@@ -34,7 +34,7 @@ BOOST_AUTO_TEST_CASE ( glsequence_operator_assignment )
 BOOST_AUTO_TEST_CASE( glsequence_color )
 {
   boost::shared_ptr<AnnotationColors> cm(new AnnotationColors);
-  Color black(0.0, 0.0, 0.0, 0.0);
+  Color black(0.0, 0.0, 0.0, 1.0);
   Color c(0.1, 0.2, 0.3, 0.4);
   boost::shared_ptr<Sequence> seq(new Sequence("AAGGCCTT"));
   GlSequence s(seq, cm);
index 068d44b5015dabc7d35d5add142eb9b82909028e..ae16ee81fffcdb5dbd1317733a5a62dc76a4b72e 100644 (file)
@@ -213,6 +213,20 @@ BOOST_AUTO_TEST_CASE( mussa_named_motif )
   BOOST_CHECK_EQUAL(motifs.begin()->get_name(), "cat");
 }
 
+BOOST_AUTO_TEST_CASE( mussa_weirdly_spaced_named_motif )
+{
+  string data = "CCAATT       cat_meow123     0.1    0.2 0.3\n";
+  istringstream test_istream(data);
+
+  Mussa m1;
+  m1.append_sequence("AAAAGGGGTTTT");
+  m1.append_sequence("GGGCCCCTTCCAATT");
+  m1.load_motifs(test_istream);
+
+  std::set<Sequence> motifs = m1.motifs();
+  BOOST_REQUIRE_EQUAL(motifs.size(), 1);
+  BOOST_CHECK_EQUAL(motifs.begin()->get_name(), "cat_meow123");
+}
 BOOST_AUTO_TEST_CASE( mussa_add_motif )
 {
   vector<Sequence> motifs;