add dirty flag
[mussa.git] / alg / mussa.cpp
1 //  This file is part of the Mussa source distribution.
2 //  http://mussa.caltech.edu/
3 //  Contact author: Tristan  De Buysscher, tristan@caltech.edu
4
5 // This program and all associated source code files are Copyright (C) 2005
6 // the California Institute of Technology, Pasadena, CA, 91125 USA.  It is
7 // under the GNU Public License; please see the included LICENSE.txt
8 // file for more information, or contact Tristan directly.
9
10
11 //                        ----------------------------------------
12 //                          ---------- mussa_class.cc -----------
13 //                        ----------------------------------------
14
15 #include <boost/filesystem/operations.hpp>
16 #include <boost/filesystem/fstream.hpp>
17 namespace fs = boost::filesystem;
18
19 #include <iostream>
20 #include <sstream>
21
22 #include "mussa_exceptions.hpp"
23 #include "alg/mussa.hpp"
24 #include "alg/flp.hpp"
25
26 using namespace std;
27
28
29 Mussa::Mussa()
30   : color_mapper(new AnnotationColors)
31 {
32   clear();
33   connect(&the_paths, SIGNAL(progress(const std::string&, int, int)), 
34           this, SIGNAL(progress(const std::string&, int, int)));
35 }
36
37 Mussa::Mussa(const Mussa& m)
38   : analysis_name(m.analysis_name),
39     window(m.window),
40     threshold(m.threshold),
41     soft_thres(m.soft_thres),
42     ana_mode(m.ana_mode),
43     win_append(m.win_append),
44     thres_append(m.thres_append),
45     motif_sequences(m.motif_sequences),
46     color_mapper(m.color_mapper),
47     dirty(m.dirty)
48 {
49   connect(&the_paths, SIGNAL(progress(const std::string&, int, int)), 
50           this, SIGNAL(progress(const std::string&, int, int)));
51 }
52
53 // set all parameters to null state
54 void
55 Mussa::clear()
56 {
57   analysis_name = "";
58   ana_mode = TransitiveNway;
59   window = 0;
60   threshold = 0;
61   soft_thres = 0;
62   win_append = false;
63   thres_append = false;
64   motif_sequences.clear();
65   if(color_mapper) color_mapper->clear();
66   the_paths.clear();
67   dirty = false;
68 }
69
70 // these 5 simple methods manually set the parameters for doing an analysis
71 // used so that the gui can take input from user and setup the analysis
72 // note - still need a set_append(bool, bool) method...
73 void
74 Mussa::set_name(string a_name)
75 {
76   analysis_name = a_name;
77   dirty = true;
78 }
79
80 string Mussa::get_name()
81 {
82   return analysis_name;
83 }
84
85 int 
86 Mussa::size() const
87 {
88   if (the_seqs.size() > 0)
89     return the_seqs.size();
90   else
91     return 0;
92 }
93
94 void
95 Mussa::set_window(int a_window)
96 {
97   window = a_window;
98   dirty = true;
99 }
100
101 int Mussa::get_window() const
102 {
103   return window;
104 }
105
106 void
107 Mussa::set_threshold(int a_threshold)
108 {
109   threshold = a_threshold;
110   dirty = true;
111   if (a_threshold > soft_thres) {
112     soft_thres = a_threshold;
113   }
114 }
115
116 int Mussa::get_threshold() const
117 {
118   return threshold;
119 }
120
121 void
122 Mussa::set_soft_threshold(int new_threshold)
123 {
124   if (new_threshold < threshold) {
125     soft_thres = threshold;
126   } else if (new_threshold > window) {
127     soft_thres = window; 
128   } else {
129     soft_thres = new_threshold;
130   }
131 }
132
133 int Mussa::get_soft_threshold() const
134 {
135   return soft_thres;
136 }
137
138 void
139 Mussa::set_analysis_mode(enum analysis_modes new_ana_mode)
140 {
141   ana_mode = new_ana_mode;
142   dirty = true;
143 }
144
145 enum Mussa::analysis_modes Mussa::get_analysis_mode() const
146 {
147   return ana_mode;
148 }
149
150 string Mussa::get_analysis_mode_name() const
151 {
152   switch (ana_mode)
153   {
154   case TransitiveNway:
155     return string("Transitive");
156     break;
157   case RadialNway:
158     return string("Radial");
159     break;
160   case EntropyNway:
161     return string("Entropy");
162     break;
163   case RecursiveNway:
164     return string("[deprecated] Recursive");
165     break;
166   default:
167     throw runtime_error("invalid analysis mode type");
168     break;
169   }
170 }
171
172 const NwayPaths& Mussa::paths() const
173 {
174   return the_paths;
175 }
176
177 //template <class IteratorT>
178 //void Mussa::createLocalAlignment(IteratorT begin, IteratorT end)
179 void Mussa::createLocalAlignment(std::list<ConservedPath>::iterator begin, 
180                                  std::list<ConservedPath>::iterator end, 
181                                  std::list<ConservedPath::path_type>& result,
182                                  std::list<std::vector<bool> >& reversed)
183 {
184   const vector_sequence_type& raw_seq = the_seqs;
185   ConservedPath::path_type aligned_path;
186   size_t i2, i3;
187   int x_start, x_end;
188   int window_length, win_i;
189   int rc_1 = 0; 
190   int rc_2 = 0;
191   vector<bool> rc_list;
192   bool full_match;
193   vector<bool> matched;
194   int align_counter;
195
196   align_counter = 0;
197   for(std::list<ConservedPath>::iterator pathz_i=begin; pathz_i != end; ++pathz_i)
198   {
199     ConservedPath& a_path = *pathz_i;
200     window_length = a_path.window_size;
201     // determine which parts of the path are RC relative to first species
202     rc_list = a_path.reverseComplimented();
203     
204     // loop over each bp in the conserved region for all sequences
205     for(win_i = 0; win_i < window_length; win_i++)
206     {
207       aligned_path.clear();
208       // determine which exact base pairs match between the sequences
209       full_match = true;
210       for(i2 = 0; i2 < a_path.size()-1; i2++)
211       {
212         // assume not rc as most likely, adjust below
213         rc_1 = 0;
214         rc_2 = 0;
215         // no matter the case, any RC node needs adjustments
216         if (a_path[i2] < 0)
217           rc_1 = window_length-1;
218         if (a_path[i2+1] < 0)
219           rc_2 = window_length-1;        
220          
221         x_start = (abs(a_path[i2]-rc_1+win_i));
222         x_end =   (abs(a_path[i2+1]-rc_2+win_i));
223         
224         boost::shared_ptr<Sequence> cur(raw_seq[i2]) ;
225         boost::shared_ptr<Sequence> next(raw_seq[i2+1]);
226         // RC case handling
227         // ugh, and xor...only want rc coloring if just one of the nodes is rc
228         // if both nodes are rc, then they are 'normal' relative to each other
229         if((rc_list[i2] || rc_list[i2+1] )&&!(rc_list[i2] && rc_list[i2+1]))
230         { //the hideous rc matching logic - not complex, but annoying
231           if(!(( ((*cur)[x_start]=='A')&&((*next)[x_end]=='T')) ||
232                 (((*cur)[x_start]=='T')&&((*next)[x_end]=='A')) ||
233                 (((*cur)[x_start]=='G')&&((*next)[x_end]=='C')) ||
234                 (((*cur)[x_start]=='C')&&((*next)[x_end]=='G'))) ) 
235           {
236             full_match = false;
237           } else {
238             aligned_path.push_back(x_start);
239           }
240         }
241         else
242         { // forward match
243           if (!( ((*cur)[x_start] == (*next)[x_end]) &&
244                 ((*cur)[x_start] != 'N') && ((*next)[x_end] != 'N') ) ) {
245             full_match = false;
246           } else {
247             aligned_path.push_back(x_start);
248           }
249         }
250       }
251       // grab the last part of our path, assuming we matched
252       if (full_match)
253         aligned_path.push_back(x_end);
254
255       if (aligned_path.size() == a_path.size()) {
256         result.push_back(aligned_path);
257         reversed.push_back(rc_list);
258       }
259     }
260     align_counter++;
261   }
262 }
263
264
265 void Mussa::append_sequence(const Sequence& a_seq)
266 {
267   boost::shared_ptr<Sequence> seq_copy(new Sequence(a_seq));
268   the_seqs.push_back(seq_copy);
269   dirty = true;
270 }
271
272 void Mussa::append_sequence(boost::shared_ptr<Sequence> a_seq)
273 {
274   the_seqs.push_back(a_seq);
275   dirty = true;
276 }
277
278
279 const vector<boost::shared_ptr<Sequence> >& 
280 Mussa::sequences() const
281 {
282   return the_seqs;
283 }
284
285 void Mussa::load_sequence(fs::path seq_file, fs::path annot_file, 
286                           int fasta_index, int sub_seq_start, int sub_seq_end,
287                           std::string *name)
288 {
289   boost::shared_ptr<Sequence> aseq(new Sequence);
290   aseq->load_fasta(seq_file, fasta_index, sub_seq_start, sub_seq_end);
291   if ( not annot_file.empty() ) {
292     aseq->load_annot(annot_file, sub_seq_start, sub_seq_end);
293   }
294   if (name != 0 and name->size() > 0 ) {
295     aseq->set_species(*name);
296   }
297   the_seqs.push_back(aseq);
298   dirty = true;
299 }
300
301 void
302 Mussa::load_mupa_file(fs::path para_file_path)
303 {
304   fs::ifstream para_file;
305   string file_data_line;
306   string param, value; 
307   fs::path annot_file;
308   int split_index, fasta_index;
309   int sub_seq_start, sub_seq_end;
310   bool seq_params, did_seq;
311   string err_msg;
312   bool parsing_path;
313   string::size_type new_index, dir_index;
314
315   // initialize values
316   clear();
317
318   // if file was opened, read the parameter values
319   if (fs::exists(para_file_path))
320   {
321     para_file.open(para_file_path, ios::in);
322
323     // what directory is the mupa file in?
324     fs::path file_path_base = para_file_path.branch_path();
325
326     // setup loop by getting file's first line
327     getline(para_file,file_data_line);
328     split_index = file_data_line.find(" ");
329     param = file_data_line.substr(0,split_index);
330     value = file_data_line.substr(split_index+1);
331     
332     while (!para_file.eof())
333     {
334       did_seq = false;
335       if (param == "ANA_NAME")
336         analysis_name = value;
337       else if (param == "APPEND_WIN")
338         win_append = true;
339       else if (param == "APPEND_THRES")
340         thres_append = true;
341       else if (param == "SEQUENCE_NUM")
342         ; // ignore sequence_num now
343       else if (param == "WINDOW")
344         window = atoi(value.c_str());
345       else if (param == "THRESHOLD")
346         threshold = atoi(value.c_str());
347       else if (param == "SEQUENCE")
348       {
349         fs::path seq_file = file_path_base / value;
350         //cout << "seq_file_name " << seq_files.back() << endl;
351         fasta_index = 1;
352         annot_file = "";
353         sub_seq_start = 0;
354         sub_seq_end = 0;
355         seq_params = true;
356
357         while ((!para_file.eof()) && seq_params)
358         {
359           getline(para_file,file_data_line);
360           split_index = file_data_line.find(" ");
361           param = file_data_line.substr(0,split_index);
362           value = file_data_line.substr(split_index+1);
363
364           if (param == "FASTA_INDEX")
365             fasta_index = atoi(value.c_str());
366           else if (param == "ANNOTATION")
367             annot_file = file_path_base / value;
368           else if (param == "SEQ_START")
369             sub_seq_start = atoi(value.c_str());
370           else if (param == "SEQ_END")
371           {
372             sub_seq_end = atoi(value.c_str());
373           }
374           //ignore empty lines or that start with '#'
375           else if ((param == "") || (param == "#")) {}
376           else seq_params = false;
377         }
378         load_sequence(seq_file, annot_file, fasta_index, sub_seq_start, 
379                       sub_seq_end);
380         did_seq = true;
381       }
382       //ignore empty lines or that start with '#'
383       else if ((param == "") || (param == "#")) {}
384       else
385       {
386         clog << "Illegal/misplaced mussa parameter in file\n";
387         clog << param << "\n";
388       }
389
390       if (!did_seq)
391       {
392         getline(para_file,file_data_line);
393         split_index = file_data_line.find(" ");
394         param = file_data_line.substr(0,split_index);
395         value = file_data_line.substr(split_index+1);
396         did_seq = false;
397       }
398     }
399
400     para_file.close();
401
402     soft_thres = threshold;
403     //cout << "nway mupa: analysis_name = " << analysis_name 
404     //     << " window = " << window 
405     //     << " threshold = " << threshold << endl;
406   }
407   // no file was loaded, signal error
408   else
409   {
410     throw mussa_load_error("Config File: " + para_file_path.string() + " not found");
411   }
412   dirty = true;
413 }
414
415
416 void
417 Mussa::analyze()
418 {
419   time_t t1, t2, begin, end;
420   double seqloadtime, seqcomptime, nwaytime, savetime, totaltime;
421
422   begin = time(NULL);
423
424   t1 = time(NULL);
425         
426   if (the_seqs.size() < 2) {
427     throw mussa_analysis_error("you need to have at least 2 sequences to "
428                                "do an analysis.");
429   }
430   //cout << "nway ana: seq_num = " << the_seqs.size() << endl;
431
432   t2 = time(NULL);
433   seqloadtime = difftime(t2, t1);
434
435   t1 = time(NULL);
436   seqcomp();
437   t2 = time(NULL);
438   seqcomptime = difftime(t2, t1);
439
440   t1 = time(NULL);
441   the_paths.setup(window, threshold);
442   nway();
443   t2 = time(NULL);
444   nwaytime = difftime(t2, t1);
445
446   t1 = time(NULL);
447   save();
448   t2 = time(NULL);
449   savetime = difftime(t2, t1);
450
451   end = time(NULL);
452   totaltime = difftime(end, begin);
453
454   //cout << "seqload\tseqcomp\tnway\tsave\ttotal\n";
455   //cout << seqloadtime << "\t";
456   //cout << seqcomptime << "\t";
457   //cout << nwaytime << "\t";
458   //cout << savetime << "\t";
459   //cout << totaltime << "\n";
460 }
461
462 void
463 Mussa::seqcomp()
464 {
465   vector<int> seq_lens;
466   vector<FLPs> empty_FLP_vector;
467   FLPs dummy_comp;
468   string save_file_string;
469
470   empty_FLP_vector.clear();
471   for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
472   {
473     all_comps.push_back(empty_FLP_vector); 
474     for(vector<Sequence>::size_type i2 = 0; i2 < the_seqs.size(); i2++)
475       all_comps[i].push_back(dummy_comp);
476   }
477   for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++) {
478     seq_lens.push_back(the_seqs[i]->size());
479   }
480   int seqcomps_done = 0;
481   int seqcomps_todo = (the_seqs.size() * (the_seqs.size()-1)) / 2;
482   emit progress("seqcomp", seqcomps_done, seqcomps_todo);
483
484   for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
485     for(vector<Sequence>::size_type i2 = i+1; i2 < the_seqs.size(); i2++)
486     {
487       //cout << "seqcomping: " << i << " v. " << i2 << endl;
488       all_comps[i][i2].setup(window, threshold);
489       all_comps[i][i2].seqcomp(*the_seqs[i], *the_seqs[i2], false);
490       all_comps[i][i2].seqcomp(*the_seqs[i], the_seqs[i2]->rev_comp(),true);
491       ++seqcomps_done;
492       emit progress("seqcomp", seqcomps_done, seqcomps_todo);
493     }
494 }
495
496 void
497 Mussa::nway()
498 {
499
500   the_paths.set_soft_threshold(soft_thres);
501
502   if (ana_mode == TransitiveNway) {
503     the_paths.trans_path_search(all_comps);
504   }
505   else if (ana_mode == RadialNway) {
506     the_paths.radiate_path_search(all_comps);
507   }
508   else if (ana_mode == EntropyNway)
509   {
510     vector<string> some_Seqs;
511     //unlike other methods, entropy needs to look at the sequence at this stage
512     some_Seqs.clear();
513     for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
514     {
515       some_Seqs.push_back(*the_seqs[i]);
516     }
517
518     the_paths.setup_ent(ent_thres, some_Seqs); // ent analysis extra setup
519     the_paths.entropy_path_search(all_comps);
520   }
521
522   // old recursive transitive analysis function
523   else if (ana_mode == RecursiveNway)
524     the_paths.find_paths_r(all_comps);
525
526   the_paths.simple_refine();
527 }
528
529 void
530 Mussa::save()
531 {
532   string save_name;
533   fs::path flp_filepath;
534   fs::fstream save_file;
535   ostringstream append_info;
536   int dir_create_status;
537
538   if (not analysis_name.empty()) {
539     // not sure why, but gotta close file each time since can't pass 
540     // file streams
541     save_name = analysis_name;
542
543     // gotta do bit with adding win & thres if to be appended
544     if (win_append)
545     {
546       append_info.str("");
547       append_info <<  "_w" << window;
548       save_name += append_info.str();
549     }
550
551     if (thres_append)
552     {
553       append_info.str("");
554       append_info <<  "_t" << threshold;
555       save_name += append_info.str();
556     }
557     fs::path save_path( save_name);
558
559     if (not fs::exists(save_path)) {
560       fs::create_directory(save_path);
561     }
562     // save sequence and annots to a special mussa file
563     save_file.open(save_path / (save_name+".museq"), ios::out);
564     save_file << "<Mussa_Sequence>" << endl;
565
566     for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
567     {
568       the_seqs[i]->save(save_file);
569     }
570
571     save_file << "</Mussa_Sequence>" << endl;
572     save_file.close();
573
574     // save nway paths to its mussa save file
575     the_paths.save(save_path / (save_name + ".muway"));
576
577     for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
578       for(vector<Sequence>::size_type i2 = i+1; i2 < the_seqs.size(); i2++)
579       {
580         append_info.str("");
581         append_info <<  "_sp_" << i << "v" << i2;
582         all_comps[i][i2].save(save_path/(save_name+append_info.str()+".flp"));
583       }
584   } else {
585     throw mussa_error("Can't save analysis without an analysis name");
586   }
587   dirty = false;
588 }
589
590 void
591 Mussa::save_muway(fs::path save_path)
592 {
593   the_paths.save(save_path);
594 }
595
596 void
597 Mussa::load(fs::path ana_file)
598 {
599   int i, i2;
600   fs::path file_path_base;
601   fs::path a_file_path; 
602   fs::path ana_path(ana_file);
603   bool parsing_path;
604   string err_msg;
605   ostringstream append_info;
606   vector<FLPs> empty_FLP_vector;
607   FLPs dummy_comp;
608
609   //clog << "ana_file name " << ana_file.string() << endl;
610   analysis_name = ana_path.leaf();
611   //clog << " ana_name " << analysis_name << endl;
612   file_path_base =  ana_path.branch_path() / analysis_name;
613   a_file_path = file_path_base / (analysis_name + ".muway");
614   //clog << " loading museq: " << a_file_path.string() << endl;
615   the_paths.load(a_file_path);
616   // perhaps this could be more elegent, but at least this'll let
617   // us know what our threshold and window sizes were when we load a muway
618   window = the_paths.get_window();
619   threshold = the_paths.get_threshold();
620   soft_thres = threshold;
621
622   int seq_num = the_paths.sequence_count();
623
624   a_file_path = file_path_base / (analysis_name + ".museq");
625
626   // this is a bit of a hack due to C++ not acting like it should with files
627   for (i = 1; i <= seq_num; i++)
628   {
629     boost::shared_ptr<Sequence> tmp_seq(new Sequence);
630     //clog << "mussa_class: loading museq frag... " << a_file_path.string() << endl;
631     tmp_seq->load_museq(a_file_path, i);
632     the_seqs.push_back(tmp_seq);
633   }
634   
635   empty_FLP_vector.clear();
636   for(i = 0; i < seq_num; i++)
637   {
638     all_comps.push_back(empty_FLP_vector); 
639     for(i2 = 0; i2 < seq_num; i2++)
640       all_comps[i].push_back(dummy_comp);
641   }
642   
643   for(i = 0; i < seq_num; i++)
644   {
645     for(i2 = i+1; i2 < seq_num; i2++)
646     {
647       append_info.str("");
648       append_info << analysis_name <<  "_sp_" << i << "v" << i2 << ".flp";
649       //clog << append_info.str() << endl;
650       a_file_path = file_path_base / append_info.str();
651       //clog << "path " << a_file_path.string() << endl;
652       all_comps[i][i2].load(a_file_path);
653       //clog << "real size = " << all_comps[i][i2].size() << endl;
654     }
655   }
656 }
657
658
659 void
660 Mussa::save_old()
661 {
662   fs::fstream save_file;
663
664   save_file.open(analysis_name, ios::out);
665
666   for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
667     save_file << *(the_seqs[i]) << endl;
668
669   save_file << window << endl;
670   save_file.close();
671   //note more complex eventually since analysis_name may need to have
672   //window size, threshold and other stuff to modify it...
673   the_paths.save_old(analysis_name);
674 }
675
676
677 void
678 Mussa::load_old(char * load_file_path, int s_num)
679 {
680   fstream save_file;
681   string file_data_line; 
682   int i, space_split_i, comma_split_i;
683   vector<int> loaded_path;
684   string node_pair, node;
685   Sequence a_seq;
686
687   int seq_num = s_num;
688   the_paths.setup(0, 0);
689   save_file.open(load_file_path, ios::in);
690
691   // currently loads old mussa format
692
693   // get sequences
694   for(i = 0; i < seq_num; i++)
695   {
696     getline(save_file, file_data_line);
697     boost::shared_ptr<Sequence> a_seq(new Sequence(file_data_line));
698     the_seqs.push_back(a_seq);
699   }
700
701   // get window size
702   getline(save_file, file_data_line);
703   window = atoi(file_data_line.c_str());
704   // get paths
705
706   while (!save_file.eof())
707   {
708     loaded_path.clear();
709     getline(save_file, file_data_line);
710     if (file_data_line != "")
711     for(i = 0; i < seq_num; i++)
712     {
713       space_split_i = file_data_line.find(" ");
714       node_pair = file_data_line.substr(0,space_split_i);
715       //cout << "np= " << node_pair;
716       comma_split_i = node_pair.find(",");
717       node = node_pair.substr(comma_split_i+1);
718       //cout << "n= " << node << " ";
719       loaded_path.push_back(atoi (node.c_str()));
720       file_data_line = file_data_line.substr(space_split_i+1);
721     }
722     //cout << endl;
723     // FIXME: do we have any information about what the threshold should be?
724     the_paths.add_path(0, loaded_path);
725   }
726   save_file.close();
727
728   //the_paths.save("tmp.save");
729 }
730
731 void Mussa::add_motif(const Sequence& motif, const Color& color)
732 {
733   motif_sequences.insert(motif);
734   color_mapper->appendInstanceColor("motif", motif, color);
735 }
736
737 void Mussa::set_motifs(const vector<Sequence>& motifs, 
738                        const vector<Color>& colors)
739 {
740   if (motifs.size() != colors.size()) {
741     throw mussa_error("motif and color vectors must be the same size");
742   }
743
744   motif_sequences.clear();
745   for(size_t i = 0; i != motifs.size(); ++i)
746   {
747     add_motif(motifs[i], colors[i]);
748   }
749   update_sequences_motifs();
750 }
751
752 // I mostly split the ifstream out so I can use a stringstream to test it.
753 void Mussa::load_motifs(std::istream &in)
754 {
755   string seq;
756   float red;
757   float green;
758   float blue;
759
760   while(in.good())
761   {
762     in >> seq >> red >> green >> blue;
763     // if we couldn't read this line 'cause we're like at the end of the file
764     // try to exit the loop
765     if (!in.good())
766       break;
767     try {
768       seq = Sequence::motif_normalize(seq);
769     } catch(motif_normalize_error e) {
770       clog << "unable to parse " << seq << " skipping" << endl;
771       clog << e.what() << endl;
772       continue;
773     }
774     if (red < 0.0 or red > 1.0) {
775       clog << "invalid red value " << red << ". must be in range [0..1]" 
776            << endl;
777       continue;
778     }
779     if (green < 0.0 or green > 1.0) {
780       clog << "invalid green value " << green << ". must be in range [0..1]" 
781            << endl;
782       continue;
783     }
784     if (blue < 0.0 or blue > 1.0) {
785       clog << "invalid blue value " << blue << ". must be in range [0..1]" 
786            << endl;
787       continue;
788     }
789     if (motif_sequences.find(seq) == motif_sequences.end()) {
790       // sequence wasn't found
791       motif_sequences.insert(seq);
792       Color c(red, green, blue);
793       color_mapper->appendInstanceColor("motif", seq, c);
794     } else {
795       clog << "sequence " << seq << " was already defined skipping" 
796            << endl;
797       continue;
798     }
799   }
800   update_sequences_motifs();
801 }
802
803 void Mussa::load_motifs(fs::path filename)
804 {
805   fs::ifstream f;
806   f.open(filename, ifstream::in);
807   load_motifs(f);
808 }
809
810 void Mussa::update_sequences_motifs()
811 {
812   // once we've loaded all the motifs from the file, 
813   // lets attach them to the sequences
814   for(vector<boost::shared_ptr<Sequence> >::iterator seq_i = the_seqs.begin();
815       seq_i != the_seqs.end();
816       ++seq_i)
817   {
818     // clear out old motifs
819     (*seq_i)->clear_motifs();
820     // for all the motifs in our set, attach them to the current sequence
821     for(set<Sequence>::iterator motif_i = motif_sequences.begin();
822         motif_i != motif_sequences.end();
823         ++motif_i)
824     {
825       (*seq_i)->add_motif(*motif_i);
826     }
827   }
828 }
829
830 const set<Sequence>& Mussa::motifs() const
831 {
832   return motif_sequences;
833 }
834
835 boost::shared_ptr<AnnotationColors> Mussa::colorMapper()
836 {
837   return color_mapper;
838 }