1 // This file is part of the Mussa source distribution.
2 // http://mussa.caltech.edu/
3 // Contact author: Tristan De Buysscher, tristan@caltech.edu
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.
11 // ----------------------------------------
12 // ---------- mussa_class.cc -----------
13 // ----------------------------------------
15 #include <boost/filesystem/operations.hpp>
16 #include <boost/filesystem/fstream.hpp>
17 namespace fs = boost::filesystem;
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;
28 #include "mussa_exceptions.hpp"
29 #include "alg/mussa.hpp"
30 #include "alg/flp.hpp"
36 : color_mapper(new AnnotationColors)
39 connect(&the_paths, SIGNAL(progress(const std::string&, int, int)),
40 this, SIGNAL(progress(const std::string&, int, int)));
43 Mussa::Mussa(const Mussa& m)
44 : analysis_name(m.analysis_name),
46 threshold(m.threshold),
47 soft_thres(m.soft_thres),
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),
56 connect(&the_paths, SIGNAL(progress(const std::string&, int, int)),
57 this, SIGNAL(progress(const std::string&, int, int)));
60 boost::filesystem::path Mussa::get_analysis_path() const
65 void Mussa::set_analysis_path(boost::filesystem::path pathname)
67 analysis_path = pathname;
70 // set all parameters to null state
75 ana_mode = TransitiveNway;
81 motif_sequences.clear();
82 if(color_mapper) color_mapper->clear();
85 analysis_path = fs::path();
89 bool Mussa::is_dirty() const
94 bool Mussa::empty() const
96 return the_seqs.empty();
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...
104 Mussa::set_name(string a_name)
106 analysis_name = a_name;
110 string Mussa::get_name() const
112 return analysis_name;
115 string Mussa::get_title() const
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) {
123 return std::string("Unnamed");
130 if (the_seqs.size() > 0)
131 return the_seqs.size();
137 Mussa::set_window(int a_window)
143 int Mussa::get_window() const
149 Mussa::set_threshold(int a_threshold)
151 threshold = a_threshold;
153 if (a_threshold > soft_thres) {
154 soft_thres = a_threshold;
158 int Mussa::get_threshold() const
164 Mussa::set_soft_threshold(int new_threshold)
166 if (new_threshold < threshold) {
167 soft_thres = threshold;
168 } else if (new_threshold > window) {
171 soft_thres = new_threshold;
175 int Mussa::get_soft_threshold() const
181 Mussa::set_analysis_mode(enum analysis_modes new_ana_mode)
183 ana_mode = new_ana_mode;
187 enum Mussa::analysis_modes Mussa::get_analysis_mode() const
192 string Mussa::get_analysis_mode_name() const
197 return string("Transitive");
200 return string("Radial");
203 return string("Entropy");
206 return string("[deprecated] Recursive");
209 throw runtime_error("invalid analysis mode type");
214 const NwayPaths& Mussa::paths() const
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)
226 const vector_sequence_type& raw_seq = the_seqs;
227 ConservedPath::path_type aligned_path;
230 int window_length, win_i;
233 vector<bool> rc_list;
235 vector<bool> matched;
239 for(std::list<ConservedPath>::iterator pathz_i=begin; pathz_i != end; ++pathz_i)
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();
246 // loop over each bp in the conserved region for all sequences
247 for(win_i = 0; win_i < window_length; win_i++)
249 aligned_path.clear();
250 // determine which exact base pairs match between the sequences
252 for(i2 = 0; i2 < a_path.size()-1; i2++)
254 // assume not rc as most likely, adjust below
257 // no matter the case, any RC node needs adjustments
259 rc_1 = window_length-1;
260 if (a_path[i2+1] < 0)
261 rc_2 = window_length-1;
263 x_start = (abs(a_path[i2]-rc_1+win_i));
264 x_end = (abs(a_path[i2+1]-rc_2+win_i));
266 boost::shared_ptr<Sequence> cur(raw_seq[i2]) ;
267 boost::shared_ptr<Sequence> next(raw_seq[i2+1]);
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'))) )
280 aligned_path.push_back(x_start);
285 if (!( ((*cur)[x_start] == (*next)[x_end]) &&
286 ((*cur)[x_start] != 'N') && ((*next)[x_end] != 'N') ) ) {
289 aligned_path.push_back(x_start);
293 // grab the last part of our path, assuming we matched
295 aligned_path.push_back(x_end);
297 if (aligned_path.size() == a_path.size()) {
298 result.push_back(aligned_path);
299 reversed.push_back(rc_list);
307 void Mussa::append_sequence(const Sequence& a_seq)
309 boost::shared_ptr<Sequence> seq_copy(new Sequence(a_seq));
310 the_seqs.push_back(seq_copy);
314 void Mussa::append_sequence(boost::shared_ptr<Sequence> a_seq)
316 the_seqs.push_back(a_seq);
321 const vector<boost::shared_ptr<Sequence> >&
322 Mussa::sequences() const
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,
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);
336 if (name != 0 and name->size() > 0 ) {
337 aseq->set_species(*name);
339 the_seqs.push_back(aseq);
344 Mussa::load_mupa_file(fs::path para_file_path)
346 fs::ifstream para_file;
347 string file_data_line;
350 int split_index, fasta_index;
351 int sub_seq_start, sub_seq_end;
352 bool seq_params, did_seq;
355 string::size_type new_index, dir_index;
360 // if file was opened, read the parameter values
361 if (not fs::exists(para_file_path))
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");
369 para_file.open(para_file_path, ios::in);
371 // what directory is the mupa file in?
372 fs::path file_path_base = para_file_path.branch_path();
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);
383 if (param == "ANA_NAME")
384 analysis_name = value;
385 else if (param == "APPEND_WIN")
387 else if (param == "APPEND_THRES")
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")
397 fs::path seq_file = file_path_base / value;
398 //cout << "seq_file_name " << seq_files.back() << endl;
405 while (para_file && seq_params)
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);
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")
420 sub_seq_end = atoi(value.c_str());
422 //ignore empty lines or that start with '#'
423 else if ((param == "") || (param == "#")) {}
424 else seq_params = false;
426 load_sequence(seq_file, annot_file, fasta_index, sub_seq_start,
430 //ignore empty lines or that start with '#'
431 else if ((param == "") || (param == "#")) {}
434 clog << "Illegal/misplaced mussa parameter in file\n";
435 clog << param << "\n";
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);
450 soft_thres = threshold;
451 //cout << "nway mupa: analysis_name = " << analysis_name
452 // << " window = " << window
453 // << " threshold = " << threshold << endl;
455 // no file was loaded, signal error
463 if (the_seqs.size() < 2) {
464 throw mussa_analysis_error("you need to have at least 2 sequences to "
467 //cout << "nway ana: seq_num = " << the_seqs.size() << endl;
470 the_paths.setup(window, threshold);
477 vector<int> seq_lens;
478 vector<FLPs> empty_FLP_vector;
480 string save_file_string;
482 empty_FLP_vector.clear();
483 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
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);
489 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++) {
490 seq_lens.push_back(the_seqs[i]->size());
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);
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++)
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);
504 emit progress("seqcomp", seqcomps_done, seqcomps_todo);
512 the_paths.set_soft_threshold(soft_thres);
514 if (ana_mode == TransitiveNway) {
515 the_paths.trans_path_search(all_comps);
517 else if (ana_mode == RadialNway) {
518 the_paths.radiate_path_search(all_comps);
520 else if (ana_mode == EntropyNway)
522 vector<Sequence> some_Seqs;
523 //unlike other methods, entropy needs to look at the sequence at this stage
525 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
527 some_Seqs.push_back(*the_seqs[i]);
530 the_paths.setup_ent(ent_thres, some_Seqs); // ent analysis extra setup
531 the_paths.entropy_path_search(all_comps);
534 // old recursive transitive analysis function
535 else if (ana_mode == RecursiveNway)
536 the_paths.find_paths_r(all_comps);
538 the_paths.simple_refine();
542 Mussa::save(fs::path save_path)
544 fs::path flp_filepath;
545 fs::fstream save_file;
546 ostringstream append_info;
547 int dir_create_status;
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
557 append_info << "_w" << window;
558 save_name += append_info.str();
563 append_info << "_t" << threshold;
564 save_name += append_info.str();
566 save_path = save_name;
568 throw mussa_save_error("Need filename or analysis name to save");
572 if (not fs::exists(save_path)) {
573 fs::create_directory(save_path);
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;
579 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
581 the_seqs[i]->save(save_file);
584 save_file << "</Mussa_Sequence>" << endl;
587 // save nway paths to its mussa save file
588 the_paths.save(save_path / (save_path.leaf()+ ".muway"));
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++)
594 append_info << "_sp_" << i << "v" << i2;
595 all_comps[i][i2].save(save_path/(save_path.leaf()+append_info.str()+".flp"));
599 analysis_path = save_path;
603 Mussa::save_muway(fs::path save_path)
605 the_paths.save(save_path);
609 Mussa::load(fs::path ana_file)
612 fs::path file_path_base;
613 fs::path a_file_path;
614 fs::path ana_path(ana_file);
617 ostringstream append_info;
618 vector<FLPs> empty_FLP_vector;
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;
635 int seq_num = the_paths.sequence_count();
637 a_file_path = file_path_base / (analysis_name + ".museq");
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++)
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);
648 empty_FLP_vector.clear();
649 for(i = 0; i < seq_num; i++)
651 all_comps.push_back(empty_FLP_vector);
652 for(i2 = 0; i2 < seq_num; i2++)
653 all_comps[i].push_back(dummy_comp);
656 for(i = 0; i < seq_num; i++)
658 for(i2 = i+1; i2 < seq_num; i2++)
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;
675 fs::fstream save_file;
677 save_file.open(analysis_name, ios::out);
679 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
680 save_file << *(the_seqs[i]) << endl;
682 save_file << window << endl;
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);
691 Mussa::load_old(char * load_file_path, int s_num)
694 string file_data_line;
695 int i, space_split_i, comma_split_i;
696 vector<int> loaded_path;
697 string node_pair, node;
701 the_paths.setup(0, 0);
702 save_file.open(load_file_path, ios::in);
704 // currently loads old mussa format
707 for(i = 0; i < seq_num; i++)
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);
715 getline(save_file, file_data_line);
716 window = atoi(file_data_line.c_str());
719 while (!save_file.eof())
722 getline(save_file, file_data_line);
723 if (file_data_line != "")
724 for(i = 0; i < seq_num; i++)
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);
736 // FIXME: do we have any information about what the threshold should be?
737 the_paths.add_path(0, loaded_path);
741 //the_paths.save("tmp.save");
744 void Mussa::add_motif(const Sequence& motif, const Color& color)
746 motif_sequences.insert(motif);
747 color_mapper->appendInstanceColor("motif", motif.get_sequence(), color);
750 void Mussa::set_motifs(const vector<Sequence>& motifs,
751 const vector<Color>& colors)
753 if (motifs.size() != colors.size()) {
754 throw mussa_error("motif and color vectors must be the same size");
757 motif_sequences.clear();
758 for(size_t i = 0; i != motifs.size(); ++i)
760 add_motif(motifs[i], colors[i]);
762 update_sequences_motifs();
765 // Helper functor to append created motifs to our Mussa analysis
766 struct push_back_motif {
767 std::set<Sequence>& motif_set;
768 boost::shared_ptr<AnnotationColors> color_mapper;
769 std::string& seq_string;
776 push_back_motif(std::set<Sequence>& motif_set_,
777 boost::shared_ptr<AnnotationColors> color_mapper_,
780 float &red_, float &green_, float &blue_, float &alpha_)
781 : motif_set(motif_set_),
782 color_mapper(color_mapper_),
792 void operator()(std::string::const_iterator,
793 std::string::const_iterator) const
795 Sequence seq(seq_string, Sequence::nucleic_alphabet);
796 // shouldn't we have a better field than "fasta header" and speices?
797 seq.set_fasta_header(name);
798 // we need to clear the name in case the next motif doesn't have one.
800 // be nice if glsequence was a subclass of sequence so we could
801 // just attach colors directly to the motif.
802 Color c(red, green, blue);
803 color_mapper->appendInstanceColor("motif", seq.c_str(), c);
804 motif_set.insert(seq);
808 // I mostly split the ifstream out so I can use a stringstream to test it.
809 void Mussa::load_motifs(std::istream &in)
812 const char *alphabet = Alphabet::nucleic_alphabet.c_str();
820 // slurp our data into a string
821 std::streamsize bytes_read = 1;
822 while (in.good() and bytes_read) {
823 const std::streamsize bufsiz=512;
825 bytes_read = in.readsome(buf, bufsiz);
826 data.append(buf, buf+bytes_read);
829 bool ok = spirit::parse(data.begin(), data.end(),
833 (+spirit::chset<>(alphabet))[spirit::assign_a(seq)] >>
837 (spirit::alpha_p >> *spirit::graph_p)[spirit::assign_a(name)]
840 spirit::real_p[spirit::assign_a(red)] >> +spirit::space_p >>
841 spirit::real_p[spirit::assign_a(green)] >> +spirit::space_p >>
842 spirit::real_p[spirit::assign_a(blue)] >> +spirit::space_p
843 )[push_back_motif(motif_sequences, color_mapper, seq, name, red, green, blue, alpha)]
846 std::clog << "Error parsing motif stream " << std::endl;
848 update_sequences_motifs();
851 void Mussa::load_motifs(fs::path filename)
854 f.open(filename, ifstream::in);
858 void Mussa::update_sequences_motifs()
860 // once we've loaded all the motifs from the file,
861 // lets attach them to the sequences
862 for(vector<boost::shared_ptr<Sequence> >::iterator seq_i = the_seqs.begin();
863 seq_i != the_seqs.end();
866 // clear out old motifs
867 (*seq_i)->clear_motifs();
868 // for all the motifs in our set, attach them to the current sequence
869 for(set<Sequence>::iterator motif_i = motif_sequences.begin();
870 motif_i != motif_sequences.end();
873 (*seq_i)->add_motif(*motif_i);
878 const set<Sequence>& Mussa::motifs() const
880 return motif_sequences;
883 boost::shared_ptr<AnnotationColors> Mussa::colorMapper()