load and display a motif list
[mussa.git] / alg / mussa.cpp
index 224daa7de006227689c2d886cb0166ed04f86c3b..fa54eb2f83eed17b58960d4a6cfd3c3f9ccb195e 100644 (file)
@@ -27,6 +27,19 @@ Mussa::Mussa()
   clear();
 }
 
+Mussa::Mussa(const Mussa& m)
+  : analysis_name(m.analysis_name),
+    window(m.window),
+    threshold(m.threshold),
+    soft_thres(m.soft_thres),
+    ana_mode(m.ana_mode),
+    win_append(m.win_append),
+    thres_append(m.thres_append),
+    motif_sequences(m.motif_sequences),
+    color_mapper(m.color_mapper)
+{
+}
+
 // set all parameters to null state
 void
 Mussa::clear()
@@ -38,6 +51,8 @@ Mussa::clear()
   soft_thres = 0;
   win_append = false;
   thres_append = false;
+  motif_sequences.clear();
+  color_mapper.clear();
 }
 
 // these 5 simple methods manually set the parameters for doing an analysis
@@ -628,3 +643,81 @@ Mussa::load_old(char * load_file_path, int s_num)
 
   //the_paths.save("tmp.save");
 }
+
+// I mostly split the ifstream out so I can use a stringstream to test it.
+void Mussa::load_motifs(std::istream &in)
+{
+  string seq;
+  float red;
+  float green;
+  float blue;
+
+  while(in.good())
+  {
+    in >> seq >> red >> green >> blue;
+    // if we couldn't read this line 'cause we're like at the end of the file
+    // try to exit the loop
+    if (!in.good())
+      break;
+    try {
+      seq = Sequence::motif_normalize(seq);
+    } catch(motif_normalize_error e) {
+      clog << "unable to parse " << seq << " skipping" << endl;
+      clog << e.what() << endl;
+      continue;
+    }
+    if (red < 0.0 or red > 1.0) {
+      clog << "invalid red value " << red << ". must be in range [0..1]" 
+           << endl;
+      continue;
+    }
+    if (green < 0.0 or green > 1.0) {
+      clog << "invalid green value " << green << ". must be in range [0..1]" 
+           << endl;
+      continue;
+    }
+    if (blue < 0.0 or blue > 1.0) {
+      clog << "invalid blue value " << blue << ". must be in range [0..1]" 
+           << endl;
+      continue;
+    }
+    if (motif_sequences.find(seq) == motif_sequences.end()) {
+      // sequence wasn't found
+      motif_sequences.insert(seq);
+      Color c(red, green, blue);
+      color_mapper.appendInstanceColor("motif", seq, c);
+    } else {
+      clog << "sequence " << seq << " was already defined skipping" 
+           << endl;
+      continue;
+    }
+  }
+  // once we've loaded all the motifs from the file, 
+  // lets attach them to the sequences
+  for(vector<Sequence>::iterator seq_i = the_seqs.begin();
+      seq_i != the_seqs.end();
+      ++seq_i)
+  {
+    // clear out old motifs
+    seq_i->clear_motifs();
+    // for all the motifs in our set, attach them to the current sequence
+    for(set<string>::iterator motif_i = motif_sequences.begin();
+        motif_i != motif_sequences.end();
+        ++motif_i)
+    {
+      seq_i->add_motif(*motif_i);
+    }
+  }
+}
+
+void Mussa::load_motifs(string filename)
+{
+  ifstream f;
+  f.open(filename.c_str(), ifstream::in);
+  load_motifs(f);
+}
+
+AnnotationColors& Mussa::colorMapper()
+{
+  return color_mapper;
+}