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