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/path.hpp>
16 #include <boost/filesystem/operations.hpp>
17 #include <boost/filesystem/fstream.hpp>
18 namespace fs = boost::filesystem;
20 #include <boost/algorithm/string.hpp>
25 #include "mussa_exceptions.hpp"
30 #include "motif_parser.hpp"
36 : color_mapper(new AnnotationColors)
39 connect(&the_paths, SIGNAL(progress(const QString&, int, int)),
40 this, SIGNAL(progress(const QString&, 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 QString&, int, int)),
57 this, SIGNAL(progress(const QString&, int, int)));
60 MussaRef Mussa::init()
62 boost::shared_ptr<Mussa> m(new Mussa());
66 boost::filesystem::path Mussa::get_analysis_path() const
71 void Mussa::set_analysis_path(boost::filesystem::path pathname)
73 analysis_path = pathname;
76 // set all parameters to null state
81 ana_mode = TransitiveNway;
87 motif_sequences.clear();
88 if(color_mapper) color_mapper->clear();
91 analysis_path = fs::path();
95 void Mussa::set_append_window(bool v)
100 bool Mussa::get_append_window()
105 void Mussa::set_append_threshold(bool v)
110 bool Mussa::get_append_threshold()
115 void Mussa::set_dirty(bool new_state)
117 if (dirty != new_state) {
119 emit isModified(dirty);
123 bool Mussa::is_dirty() const
128 bool Mussa::empty() const
130 return the_seqs.empty();
134 // these 5 simple methods manually set the parameters for doing an analysis
135 // used so that the gui can take input from user and setup the analysis
136 // note - still need a set_append(bool, bool) method...
138 Mussa::set_name(string a_name)
140 analysis_name = a_name;
144 string Mussa::get_name() const
146 return analysis_name;
149 string Mussa::get_title() const
151 fs::path analysis_path = get_analysis_path();
152 if (not analysis_path.empty()) {
153 return analysis_path.native_file_string();
154 } else if (get_name().size() > 0) {
157 return std::string("Unnamed");
164 if (the_seqs.size() > 0)
165 return the_seqs.size();
171 Mussa::set_window(int a_window)
177 int Mussa::get_window() const
183 Mussa::set_threshold(int a_threshold)
185 threshold = a_threshold;
187 if (a_threshold > soft_thres) {
188 soft_thres = a_threshold;
192 int Mussa::get_threshold() const
198 Mussa::set_soft_threshold(int new_threshold)
200 if (new_threshold < threshold) {
201 new_threshold = threshold;
202 } else if (new_threshold > window) {
203 new_threshold = window;
205 if (soft_thres != new_threshold) {
206 soft_thres = new_threshold;
211 int Mussa::get_soft_threshold() const
218 Mussa::set_analysis_mode(enum analysis_modes new_ana_mode)
220 ana_mode = new_ana_mode;
224 enum Mussa::analysis_modes Mussa::get_analysis_mode() const
229 string Mussa::get_analysis_mode_name() const
234 return string("Transitive");
237 return string("Radial");
240 return string("Entropy");
243 return string("[deprecated] Recursive");
246 throw runtime_error("invalid analysis mode type");
251 const NwayPaths& Mussa::paths() const
256 //template <class IteratorT>
257 //void Mussa::createLocalAlignment(IteratorT begin, IteratorT end)
258 void Mussa::createLocalAlignment(std::list<ConservedPath>::iterator begin,
259 std::list<ConservedPath>::iterator end,
260 std::list<ConservedPath::path_type>& result,
261 std::list<std::vector<bool> >& reversed)
263 const vector_sequence_type& raw_seq = the_seqs;
264 ConservedPath::path_type aligned_path;
267 int window_length, win_i;
270 vector<bool> rc_list;
272 vector<bool> matched;
276 for(std::list<ConservedPath>::iterator pathz_i=begin; pathz_i != end; ++pathz_i)
278 ConservedPath& a_path = *pathz_i;
279 window_length = a_path.window_size;
280 // determine which parts of the path are RC relative to first species
281 rc_list = a_path.reverseComplimented();
283 // loop over each bp in the conserved region for all sequences
284 for(win_i = 0; win_i < window_length; win_i++)
286 aligned_path.clear();
287 // determine which exact base pairs match between the sequences
289 for(i2 = 0; i2 < a_path.size()-1; i2++)
291 // assume not rc as most likely, adjust below
294 // no matter the case, any RC node needs adjustments
296 rc_1 = window_length-1;
297 if (a_path[i2+1] < 0)
298 rc_2 = window_length-1;
300 x_start = (abs(a_path[i2]-rc_1+win_i));
301 x_end = (abs(a_path[i2+1]-rc_2+win_i));
303 boost::shared_ptr<Sequence> cur(raw_seq[i2]) ;
304 boost::shared_ptr<Sequence> next(raw_seq[i2+1]);
306 // ugh, and xor...only want rc coloring if just one of the nodes is rc
307 // if both nodes are rc, then they are 'normal' relative to each other
308 if((rc_list[i2] || rc_list[i2+1] )&&!(rc_list[i2] && rc_list[i2+1]))
309 { //the hideous rc matching logic - not complex, but annoying
310 if(!(( ((*cur)[x_start]=='A')&&((*next)[x_end]=='T')) ||
311 (((*cur)[x_start]=='T')&&((*next)[x_end]=='A')) ||
312 (((*cur)[x_start]=='G')&&((*next)[x_end]=='C')) ||
313 (((*cur)[x_start]=='C')&&((*next)[x_end]=='G'))) )
317 aligned_path.push_back(x_start);
322 if (!( ((*cur)[x_start] == (*next)[x_end]) &&
323 ((*cur)[x_start] != 'N') && ((*next)[x_end] != 'N') ) ) {
326 aligned_path.push_back(x_start);
330 // grab the last part of our path, assuming we matched
332 aligned_path.push_back(x_end);
334 if (aligned_path.size() == a_path.size()) {
335 result.push_back(aligned_path);
336 reversed.push_back(rc_list);
344 void Mussa::append_sequence(const Sequence& a_seq)
346 boost::shared_ptr<Sequence> seq_copy(new Sequence(a_seq));
347 the_seqs.push_back(seq_copy);
351 void Mussa::append_sequence(boost::shared_ptr<Sequence> a_seq)
353 the_seqs.push_back(a_seq);
358 const vector<SequenceRef>&
359 Mussa::sequences() const
364 void Mussa::load_sequence(fs::path seq_file, fs::path annot_file,
365 int fasta_index, int sub_seq_start, int sub_seq_end,
368 boost::shared_ptr<Sequence> aseq(new Sequence);
369 aseq->load_fasta(seq_file, fasta_index, sub_seq_start, sub_seq_end);
370 if ( not annot_file.empty() ) {
371 aseq->load_annot(annot_file, sub_seq_start, sub_seq_end);
373 if (name != 0 and name->size() > 0 ) {
374 aseq->set_species(*name);
376 the_seqs.push_back(aseq);
381 Mussa::load_mupa_file(fs::path para_file_path)
383 if (not fs::exists(para_file_path))
385 throw mussa_load_error("Config File: " + para_file_path.string() + " not found");
386 } else if (fs::is_directory(para_file_path)) {
387 throw mussa_load_error("Config File: " + para_file_path.string() + " is a directory.");
388 } else if (fs::is_empty(para_file_path)) {
389 throw mussa_load_error("Config File: " + para_file_path.string() + " is empty");
391 // what directory is the mupa file in?
392 fs::path file_path_base( para_file_path.branch_path()) ;
394 fs::ifstream para_file;
395 para_file.open(para_file_path, ios::in);
397 load_mupa_stream(para_file, file_path_base);
403 Mussa::load_mupa_stream(std::istream& para_file, fs::path& file_path_base)
406 std::vector< std::string > tokens;
409 enum parsing_state_enum { START, INSEQUENCE };
410 parsing_state_enum parsing_state = START;
412 // sequence file parameters
415 int split_index, fasta_index;
416 int sub_seq_start, sub_seq_end;
417 bool seq_params, did_seq;
424 while (para_file.good())
427 // setup loop by getting file's first line
428 multiplatform_getline(para_file, line);
429 // strip leading/trailing whitespace
431 // ignore commented out or blank lines
432 if ( line.size() == 0 or line[0] == '#' ) {
436 // split the line on white spance
437 boost::split(tokens, line, boost::is_space());
438 // do we have a name/value pair?
439 if (tokens.size() != 2) {
440 std::stringstream errmsg;
441 errmsg << "Error parsing MUPA file line: "
442 << line_count << std::endl
444 throw mussa_load_error(errmsg.str());
447 boost::to_upper(tokens[0]);
448 // Parameters only useful after a sequence block
449 if (parsing_state == INSEQUENCE) {
450 // in the following if blocks, if we do
451 // successfully match a token we should continue
452 // on to the next token
453 // but if we don't match a token we want to
454 // fall through to the top level parsing
456 if (tokens[0] == "FASTA_INDEX") {
457 fasta_index = atoi(tokens[1].c_str());
459 } else if (tokens[0] == "ANNOTATION") {
460 annot_file = file_path_base / tokens[1];
462 } else if (tokens[0] == "SEQ_START") {
463 sub_seq_start = atoi(tokens[1].c_str());
465 } else if (tokens[0] == "SEQ_END") {
466 sub_seq_end = atoi(tokens[1].c_str());
469 // any other token means we're done with this
470 // sequence so we should load it
471 // (and let the "unknown" token fall through into the
472 // top level token parser)
473 load_sequence(seq_file, annot_file, fasta_index, sub_seq_start,
475 parsing_state = START;
478 // if we didn't consume a token from the previous if block
480 // top level token parsing
481 if (tokens[0] == "ANA_NAME") {
482 analysis_name = tokens[1];
483 } else if (tokens[0] == "APPEND_WIN") {
485 } else if (tokens[0] == "APPEND_THRES") {
487 } else if (tokens[0] == "SEQUENCE_NUM") {
488 ; // ignore sequence_num now
489 } else if (tokens[0] == "WINDOW") {
490 window = atoi(tokens[1].c_str());
491 } else if (tokens[0] == "THRESHOLD") {
492 threshold = atoi(tokens[1].c_str());
493 } else if (tokens[0] == "SEQUENCE") {
494 if (parsing_state == INSEQUENCE) {
495 load_sequence(seq_file, annot_file, fasta_index, sub_seq_start,
497 parsing_state = START;
499 // reset sequence parameters
500 seq_file = file_path_base / tokens[1];
506 parsing_state = INSEQUENCE;
508 clog << "Illegal/misplaced mussa parameter in file\n";
509 clog << tokens[0] << "\n";
510 std::stringstream errmsg;
511 errmsg << "Invalid mussa paaramater '"
514 << line_count << std::endl
516 throw mussa_load_error(errmsg.str());
517 throw mussa_load_error("Error parsing MUPA file");
521 // if we hit the end of the file and there's a sequence
522 // pending, go ahead and load it
523 if (parsing_state == INSEQUENCE) {
524 load_sequence(seq_file, annot_file, fasta_index, sub_seq_start,
528 soft_thres = threshold;
536 if (the_seqs.size() < 2) {
537 throw mussa_analysis_error("you need to have at least 2 sequences to "
542 the_paths.setup(window, threshold);
549 vector<int> seq_lens;
550 vector<FLPs> empty_FLP_vector;
552 string save_file_string;
554 empty_FLP_vector.clear();
555 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
557 all_comps.push_back(empty_FLP_vector);
558 for(vector<Sequence>::size_type i2 = 0; i2 < the_seqs.size(); i2++)
559 all_comps[i].push_back(dummy_comp);
561 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++) {
562 seq_lens.push_back(the_seqs[i]->size());
564 int seqcomps_done = 0;
565 int seqcomps_todo = (the_seqs.size() * (the_seqs.size()-1)) / 2;
566 emit progress("seqcomp", seqcomps_done, seqcomps_todo);
568 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
569 for(vector<Sequence>::size_type i2 = i+1; i2 < the_seqs.size(); i2++)
571 //cout << "seqcomping: " << i << " v. " << i2 << endl;
572 all_comps[i][i2].setup(window, threshold);
573 all_comps[i][i2].seqcomp(*the_seqs[i], *the_seqs[i2], false);
574 all_comps[i][i2].seqcomp(*the_seqs[i], the_seqs[i2]->rev_comp(),true);
576 emit progress("seqcomp", seqcomps_done, seqcomps_todo);
584 the_paths.set_soft_threshold(soft_thres);
586 if (ana_mode == TransitiveNway) {
587 the_paths.trans_path_search(all_comps);
589 else if (ana_mode == RadialNway) {
590 the_paths.radiate_path_search(all_comps);
592 else if (ana_mode == EntropyNway)
594 vector<Sequence> some_Seqs;
595 //unlike other methods, entropy needs to look at the sequence at this stage
597 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
599 some_Seqs.push_back(*the_seqs[i]);
602 the_paths.setup_ent(ent_thres, some_Seqs); // ent analysis extra setup
603 the_paths.entropy_path_search(all_comps);
606 // old recursive transitive analysis function
607 else if (ana_mode == RecursiveNway)
608 the_paths.find_paths_r(all_comps);
610 the_paths.simple_refine();
614 Mussa::save(fs::path save_path)
616 fs::fstream save_file;
617 ostringstream append_info;
618 int dir_create_status;
620 if (save_path.empty()) {
621 if (not analysis_path.empty()) {
622 save_path = analysis_path;
623 } else if (not analysis_name.empty()) {
624 std::string save_name = analysis_name;
625 // gotta do bit with adding win & thres if to be appended
628 append_info << "_w" << window;
629 save_name += append_info.str();
634 append_info << "_t" << threshold;
635 save_name += append_info.str();
637 save_path = save_name;
639 throw mussa_save_error("Need filename or analysis name to save");
643 if (not fs::exists(save_path)) {
644 fs::create_directory(save_path);
647 std::string basename = save_path.leaf();
648 fs::path museq(basename + ".museq", fs::native);
650 // save sequence and annots to a special mussa file
651 save_file.open(save_path / museq, ios::out);
652 save_file << "<Mussa_Sequence>" << endl;
654 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
656 the_seqs[i]->save(save_file);
659 save_file << "</Mussa_Sequence>" << endl;
662 // if we have any motifs, save them.
663 if (motif_sequences.size()) {
664 fs::path mtl(basename + ".mtl", fs::native);
665 save_motifs(save_path / mtl);
668 // save nway paths to its mussa save file
669 fs::path muway(basename + ".muway", fs::native);
670 the_paths.save(save_path / muway);
672 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++) {
673 for(vector<Sequence>::size_type i2 = i+1; i2 < the_seqs.size(); i2++)
676 append_info << "_sp_" << i << "v" << i2;
677 fs::path flp(basename+append_info.str()+".flp", fs::native);
678 all_comps[i][i2].save(save_path / flp);
683 analysis_path = save_path;
687 Mussa::save_muway(fs::path save_path)
689 the_paths.save(save_path);
693 Mussa::load(fs::path ana_file)
696 fs::path file_path_base;
697 fs::path a_file_path;
698 fs::path ana_path(ana_file);
701 ostringstream append_info;
702 vector<FLPs> empty_FLP_vector;
706 //--------------------------------------------------------
708 //--------------------------------------------------------
709 analysis_path = ana_file;
710 analysis_name = ana_path.leaf();
711 fs::path muway(analysis_name+".muway", fs::native);
712 a_file_path = analysis_path / muway;
713 the_paths.load(a_file_path);
714 // perhaps this could be more elegent, but at least this'll let
715 // us know what our threshold and window sizes were when we load a muway
716 window = the_paths.get_window();
717 threshold = the_paths.get_threshold();
718 soft_thres = the_paths.get_soft_threshold();
721 //--------------------------------------------------------
723 //--------------------------------------------------------
724 //int seq_num = the_paths.sequence_count();
726 fs::path museq(analysis_name + ".museq", fs::native);
727 a_file_path = analysis_path / museq;
729 // this is a bit of a hack due to C++ not acting like it should with files
730 /*for (i = 1; i <= seq_num; i++)
732 boost::shared_ptr<Sequence> tmp_seq(new Sequence);
733 tmp_seq->load_museq(a_file_path, i);
734 the_seqs.push_back(tmp_seq);
739 boost::filesystem::fstream load_museq_fs;
740 load_museq_fs.open(a_file_path, std::ios::in);
741 boost::shared_ptr<Sequence> tmp_seq;
744 tmp_seq = Sequence::load_museq(load_museq_fs);
748 the_seqs.push_back(tmp_seq);
756 //safe guard in case of an infinate loop.
757 //FIXME: If mussa can handle a comparison of 10000 sequences
758 // in the future, then this code should be fixed.
761 throw mussa_load_error(" Run away sequence load!");
765 load_museq_fs.close();
767 //--------------------------------------------------------
769 //--------------------------------------------------------
770 fs::path mtl(analysis_name + ".mtl", fs::native);
771 fs::path motif_file = analysis_path / mtl;
772 if (fs::exists(motif_file)) {
773 load_motifs(motif_file);
776 vector<Sequence>::size_type seq_num = the_seqs.size();
777 empty_FLP_vector.clear();
778 for(i = 0; i < seq_num; i++)
780 all_comps.push_back(empty_FLP_vector);
781 for(i2 = 0; i2 < seq_num; i2++)
782 all_comps[i].push_back(dummy_comp);
786 for(i = 0; i < seq_num; i++)
788 for(i2 = i+1; i2 < seq_num; i2++)
791 append_info << analysis_name << "_sp_" << i << "v" << i2 << ".flp";
792 //clog << append_info.str() << endl;
793 fs::path flp(append_info.str(), fs::native);
794 a_file_path = analysis_path / flp;
795 all_comps[i][i2].load(a_file_path);
803 fs::fstream save_file;
805 save_file.open(analysis_name, ios::out);
807 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
808 save_file << *(the_seqs[i]) << endl;
810 save_file << window << endl;
812 //note more complex eventually since analysis_name may need to have
813 //window size, threshold and other stuff to modify it...
814 the_paths.save_old(analysis_name);
819 Mussa::load_old(char * load_file_path, int s_num)
822 string file_data_line;
823 int i, space_split_i, comma_split_i;
824 vector<int> loaded_path;
825 string node_pair, node;
829 the_paths.setup(0, 0);
830 save_file.open(load_file_path, ios::in);
832 // currently loads old mussa format
835 for(i = 0; i < seq_num; i++)
837 getline(save_file, file_data_line);
838 boost::shared_ptr<Sequence> a_seq(new Sequence(file_data_line));
839 the_seqs.push_back(a_seq);
843 getline(save_file, file_data_line);
844 window = atoi(file_data_line.c_str());
847 while (!save_file.eof())
850 getline(save_file, file_data_line);
851 if (file_data_line != "")
852 for(i = 0; i < seq_num; i++)
854 space_split_i = file_data_line.find(" ");
855 node_pair = file_data_line.substr(0,space_split_i);
856 //cout << "np= " << node_pair;
857 comma_split_i = node_pair.find(",");
858 node = node_pair.substr(comma_split_i+1);
859 //cout << "n= " << node << " ";
860 loaded_path.push_back(atoi (node.c_str()));
861 file_data_line = file_data_line.substr(space_split_i+1);
864 // FIXME: do we have any information about what the threshold should be?
865 the_paths.add_path(0, loaded_path);
869 //the_paths.save("tmp.save");
872 void Mussa::add_motif(const Sequence& motif, const Color& color)
874 motif_sequences.insert(motif);
875 color_mapper->appendInstanceColor("motif", motif.get_sequence(), color);
879 void Mussa::set_motifs(const vector<Sequence>& motifs,
880 const vector<Color>& colors)
882 if (motifs.size() != colors.size()) {
883 throw mussa_error("motif and color vectors must be the same size");
886 motif_sequences.clear();
887 for(size_t i = 0; i != motifs.size(); ++i)
889 add_motif(motifs[i], colors[i]);
891 update_sequences_motifs();
894 void Mussa::load_motifs(fs::path filename)
897 f.open(filename, ifstream::in);
901 void Mussa::load_motifs(std::istream &in)
904 const char *alphabet = Alphabet::dna_cstr;
905 motif_parser::ParsedMotifs parsed_motifs(motif_sequences, color_mapper);
907 // slurp our data into a string
908 std::streamsize bytes_read = 1;
909 while (in.good() and bytes_read) {
910 const std::streamsize bufsiz=512;
912 bytes_read = in.readsome(buf, bufsiz);
913 data.append(buf, buf+bytes_read);
915 parsed_motifs.parse(data);
916 update_sequences_motifs();
919 void Mussa::save_motifs(fs::path filename)
921 fs::ofstream out_stream;
922 out_stream.open(filename, ofstream::out);
923 save_motifs(out_stream);
926 void Mussa::save_motifs(std::ostream& out)
928 for(motif_set::iterator motif_i = motif_sequences.begin();
929 motif_i != motif_sequences.end();
932 out << motif_i->get_sequence() << " ";
933 if (motif_i->get_name().size() > 0) {
934 out << "\"" << motif_i->get_name() << "\" ";
936 out << color_mapper->lookup("motif", motif_i->get_sequence());
941 void Mussa::update_sequences_motifs()
943 // once we've loaded all the motifs from the file,
944 // lets attach them to the sequences
945 for(vector<SequenceRef >::iterator seq_i = the_seqs.begin();
946 seq_i != the_seqs.end();
949 // clear out old motifs
950 (*seq_i)->clear_motifs();
951 // for all the motifs in our set, attach them to the current sequence
952 for(set<Sequence>::iterator motif_i = motif_sequences.begin();
953 motif_i != motif_sequences.end();
956 (*seq_i)->add_motif(*motif_i);
961 const set<Sequence>& Mussa::motifs() const
963 return motif_sequences;
966 boost::shared_ptr<AnnotationColors> Mussa::colorMapper()