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;
22 #include "mussa_exceptions.hpp"
23 #include "alg/mussa.hpp"
24 #include "alg/flp.hpp"
30 : color_mapper(new AnnotationColors)
33 connect(&the_paths, SIGNAL(progress(const std::string&, int, int)),
34 this, SIGNAL(progress(const std::string&, int, int)));
37 Mussa::Mussa(const Mussa& m)
38 : analysis_name(m.analysis_name),
40 threshold(m.threshold),
41 soft_thres(m.soft_thres),
43 win_append(m.win_append),
44 thres_append(m.thres_append),
45 motif_sequences(m.motif_sequences),
46 color_mapper(m.color_mapper),
49 connect(&the_paths, SIGNAL(progress(const std::string&, int, int)),
50 this, SIGNAL(progress(const std::string&, int, int)));
53 // set all parameters to null state
58 ana_mode = TransitiveNway;
64 motif_sequences.clear();
65 if(color_mapper) color_mapper->clear();
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...
74 Mussa::set_name(string a_name)
76 analysis_name = a_name;
80 string Mussa::get_name()
88 if (the_seqs.size() > 0)
89 return the_seqs.size();
95 Mussa::set_window(int a_window)
101 int Mussa::get_window() const
107 Mussa::set_threshold(int a_threshold)
109 threshold = a_threshold;
111 if (a_threshold > soft_thres) {
112 soft_thres = a_threshold;
116 int Mussa::get_threshold() const
122 Mussa::set_soft_threshold(int new_threshold)
124 if (new_threshold < threshold) {
125 soft_thres = threshold;
126 } else if (new_threshold > window) {
129 soft_thres = new_threshold;
133 int Mussa::get_soft_threshold() const
139 Mussa::set_analysis_mode(enum analysis_modes new_ana_mode)
141 ana_mode = new_ana_mode;
145 enum Mussa::analysis_modes Mussa::get_analysis_mode() const
150 string Mussa::get_analysis_mode_name() const
155 return string("Transitive");
158 return string("Radial");
161 return string("Entropy");
164 return string("[deprecated] Recursive");
167 throw runtime_error("invalid analysis mode type");
172 const NwayPaths& Mussa::paths() const
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)
184 const vector_sequence_type& raw_seq = the_seqs;
185 ConservedPath::path_type aligned_path;
188 int window_length, win_i;
191 vector<bool> rc_list;
193 vector<bool> matched;
197 for(std::list<ConservedPath>::iterator pathz_i=begin; pathz_i != end; ++pathz_i)
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();
204 // loop over each bp in the conserved region for all sequences
205 for(win_i = 0; win_i < window_length; win_i++)
207 aligned_path.clear();
208 // determine which exact base pairs match between the sequences
210 for(i2 = 0; i2 < a_path.size()-1; i2++)
212 // assume not rc as most likely, adjust below
215 // no matter the case, any RC node needs adjustments
217 rc_1 = window_length-1;
218 if (a_path[i2+1] < 0)
219 rc_2 = window_length-1;
221 x_start = (abs(a_path[i2]-rc_1+win_i));
222 x_end = (abs(a_path[i2+1]-rc_2+win_i));
224 boost::shared_ptr<Sequence> cur(raw_seq[i2]) ;
225 boost::shared_ptr<Sequence> next(raw_seq[i2+1]);
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'))) )
238 aligned_path.push_back(x_start);
243 if (!( ((*cur)[x_start] == (*next)[x_end]) &&
244 ((*cur)[x_start] != 'N') && ((*next)[x_end] != 'N') ) ) {
247 aligned_path.push_back(x_start);
251 // grab the last part of our path, assuming we matched
253 aligned_path.push_back(x_end);
255 if (aligned_path.size() == a_path.size()) {
256 result.push_back(aligned_path);
257 reversed.push_back(rc_list);
265 void Mussa::append_sequence(const Sequence& a_seq)
267 boost::shared_ptr<Sequence> seq_copy(new Sequence(a_seq));
268 the_seqs.push_back(seq_copy);
272 void Mussa::append_sequence(boost::shared_ptr<Sequence> a_seq)
274 the_seqs.push_back(a_seq);
279 const vector<boost::shared_ptr<Sequence> >&
280 Mussa::sequences() const
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,
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);
294 if (name != 0 and name->size() > 0 ) {
295 aseq->set_species(*name);
297 the_seqs.push_back(aseq);
302 Mussa::load_mupa_file(fs::path para_file_path)
304 fs::ifstream para_file;
305 string file_data_line;
308 int split_index, fasta_index;
309 int sub_seq_start, sub_seq_end;
310 bool seq_params, did_seq;
313 string::size_type new_index, dir_index;
318 // if file was opened, read the parameter values
319 if (fs::exists(para_file_path))
321 para_file.open(para_file_path, ios::in);
323 // what directory is the mupa file in?
324 fs::path file_path_base = para_file_path.branch_path();
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);
332 while (!para_file.eof())
335 if (param == "ANA_NAME")
336 analysis_name = value;
337 else if (param == "APPEND_WIN")
339 else if (param == "APPEND_THRES")
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")
349 fs::path seq_file = file_path_base / value;
350 //cout << "seq_file_name " << seq_files.back() << endl;
357 while ((!para_file.eof()) && seq_params)
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);
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")
372 sub_seq_end = atoi(value.c_str());
374 //ignore empty lines or that start with '#'
375 else if ((param == "") || (param == "#")) {}
376 else seq_params = false;
378 load_sequence(seq_file, annot_file, fasta_index, sub_seq_start,
382 //ignore empty lines or that start with '#'
383 else if ((param == "") || (param == "#")) {}
386 clog << "Illegal/misplaced mussa parameter in file\n";
387 clog << param << "\n";
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);
402 soft_thres = threshold;
403 //cout << "nway mupa: analysis_name = " << analysis_name
404 // << " window = " << window
405 // << " threshold = " << threshold << endl;
407 // no file was loaded, signal error
410 throw mussa_load_error("Config File: " + para_file_path.string() + " not found");
419 if (the_seqs.size() < 2) {
420 throw mussa_analysis_error("you need to have at least 2 sequences to "
423 //cout << "nway ana: seq_num = " << the_seqs.size() << endl;
426 the_paths.setup(window, threshold);
428 // FIXME: once we implement a save feature we should remove this
429 if (not analysis_name.empty()) {
437 vector<int> seq_lens;
438 vector<FLPs> empty_FLP_vector;
440 string save_file_string;
442 empty_FLP_vector.clear();
443 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
445 all_comps.push_back(empty_FLP_vector);
446 for(vector<Sequence>::size_type i2 = 0; i2 < the_seqs.size(); i2++)
447 all_comps[i].push_back(dummy_comp);
449 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++) {
450 seq_lens.push_back(the_seqs[i]->size());
452 int seqcomps_done = 0;
453 int seqcomps_todo = (the_seqs.size() * (the_seqs.size()-1)) / 2;
454 emit progress("seqcomp", seqcomps_done, seqcomps_todo);
456 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
457 for(vector<Sequence>::size_type i2 = i+1; i2 < the_seqs.size(); i2++)
459 //cout << "seqcomping: " << i << " v. " << i2 << endl;
460 all_comps[i][i2].setup(window, threshold);
461 all_comps[i][i2].seqcomp(*the_seqs[i], *the_seqs[i2], false);
462 all_comps[i][i2].seqcomp(*the_seqs[i], the_seqs[i2]->rev_comp(),true);
464 emit progress("seqcomp", seqcomps_done, seqcomps_todo);
472 the_paths.set_soft_threshold(soft_thres);
474 if (ana_mode == TransitiveNway) {
475 the_paths.trans_path_search(all_comps);
477 else if (ana_mode == RadialNway) {
478 the_paths.radiate_path_search(all_comps);
480 else if (ana_mode == EntropyNway)
482 vector<string> some_Seqs;
483 //unlike other methods, entropy needs to look at the sequence at this stage
485 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
487 some_Seqs.push_back(*the_seqs[i]);
490 the_paths.setup_ent(ent_thres, some_Seqs); // ent analysis extra setup
491 the_paths.entropy_path_search(all_comps);
494 // old recursive transitive analysis function
495 else if (ana_mode == RecursiveNway)
496 the_paths.find_paths_r(all_comps);
498 the_paths.simple_refine();
502 Mussa::save(fs::path save_path)
504 fs::path flp_filepath;
505 fs::fstream save_file;
506 ostringstream append_info;
507 int dir_create_status;
509 if (save_path.empty()) {
510 if (not analysis_name.empty()) {
511 std::string save_name = analysis_name;
512 // gotta do bit with adding win & thres if to be appended
515 append_info << "_w" << window;
516 save_name += append_info.str();
521 append_info << "_t" << threshold;
522 save_name += append_info.str();
524 save_path = save_name;
526 throw mussa_save_error("Need filename or analysis name to save");
530 if (not fs::exists(save_path)) {
531 fs::create_directory(save_path);
533 // save sequence and annots to a special mussa file
534 save_file.open(save_path / (save_path.leaf()+".museq"), ios::out);
535 save_file << "<Mussa_Sequence>" << endl;
537 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
539 the_seqs[i]->save(save_file);
542 save_file << "</Mussa_Sequence>" << endl;
545 // save nway paths to its mussa save file
546 the_paths.save(save_path / (save_path.leaf()+ ".muway"));
548 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++) {
549 for(vector<Sequence>::size_type i2 = i+1; i2 < the_seqs.size(); i2++)
552 append_info << "_sp_" << i << "v" << i2;
553 all_comps[i][i2].save(save_path/(save_path.leaf()+append_info.str()+".flp"));
560 Mussa::save_muway(fs::path save_path)
562 the_paths.save(save_path);
566 Mussa::load(fs::path ana_file)
569 fs::path file_path_base;
570 fs::path a_file_path;
571 fs::path ana_path(ana_file);
574 ostringstream append_info;
575 vector<FLPs> empty_FLP_vector;
578 //clog << "ana_file name " << ana_file.string() << endl;
579 analysis_name = ana_path.leaf();
580 //clog << " ana_name " << analysis_name << endl;
581 file_path_base = ana_path.branch_path() / analysis_name;
582 a_file_path = file_path_base / (analysis_name + ".muway");
583 //clog << " loading museq: " << a_file_path.string() << endl;
584 the_paths.load(a_file_path);
585 // perhaps this could be more elegent, but at least this'll let
586 // us know what our threshold and window sizes were when we load a muway
587 window = the_paths.get_window();
588 threshold = the_paths.get_threshold();
589 soft_thres = threshold;
591 int seq_num = the_paths.sequence_count();
593 a_file_path = file_path_base / (analysis_name + ".museq");
595 // this is a bit of a hack due to C++ not acting like it should with files
596 for (i = 1; i <= seq_num; i++)
598 boost::shared_ptr<Sequence> tmp_seq(new Sequence);
599 //clog << "mussa_class: loading museq frag... " << a_file_path.string() << endl;
600 tmp_seq->load_museq(a_file_path, i);
601 the_seqs.push_back(tmp_seq);
604 empty_FLP_vector.clear();
605 for(i = 0; i < seq_num; i++)
607 all_comps.push_back(empty_FLP_vector);
608 for(i2 = 0; i2 < seq_num; i2++)
609 all_comps[i].push_back(dummy_comp);
612 for(i = 0; i < seq_num; i++)
614 for(i2 = i+1; i2 < seq_num; i2++)
617 append_info << analysis_name << "_sp_" << i << "v" << i2 << ".flp";
618 //clog << append_info.str() << endl;
619 a_file_path = file_path_base / append_info.str();
620 //clog << "path " << a_file_path.string() << endl;
621 all_comps[i][i2].load(a_file_path);
622 //clog << "real size = " << all_comps[i][i2].size() << endl;
631 fs::fstream save_file;
633 save_file.open(analysis_name, ios::out);
635 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
636 save_file << *(the_seqs[i]) << endl;
638 save_file << window << endl;
640 //note more complex eventually since analysis_name may need to have
641 //window size, threshold and other stuff to modify it...
642 the_paths.save_old(analysis_name);
647 Mussa::load_old(char * load_file_path, int s_num)
650 string file_data_line;
651 int i, space_split_i, comma_split_i;
652 vector<int> loaded_path;
653 string node_pair, node;
657 the_paths.setup(0, 0);
658 save_file.open(load_file_path, ios::in);
660 // currently loads old mussa format
663 for(i = 0; i < seq_num; i++)
665 getline(save_file, file_data_line);
666 boost::shared_ptr<Sequence> a_seq(new Sequence(file_data_line));
667 the_seqs.push_back(a_seq);
671 getline(save_file, file_data_line);
672 window = atoi(file_data_line.c_str());
675 while (!save_file.eof())
678 getline(save_file, file_data_line);
679 if (file_data_line != "")
680 for(i = 0; i < seq_num; i++)
682 space_split_i = file_data_line.find(" ");
683 node_pair = file_data_line.substr(0,space_split_i);
684 //cout << "np= " << node_pair;
685 comma_split_i = node_pair.find(",");
686 node = node_pair.substr(comma_split_i+1);
687 //cout << "n= " << node << " ";
688 loaded_path.push_back(atoi (node.c_str()));
689 file_data_line = file_data_line.substr(space_split_i+1);
692 // FIXME: do we have any information about what the threshold should be?
693 the_paths.add_path(0, loaded_path);
697 //the_paths.save("tmp.save");
700 void Mussa::add_motif(const Sequence& motif, const Color& color)
702 motif_sequences.insert(motif);
703 color_mapper->appendInstanceColor("motif", motif, color);
706 void Mussa::set_motifs(const vector<Sequence>& motifs,
707 const vector<Color>& colors)
709 if (motifs.size() != colors.size()) {
710 throw mussa_error("motif and color vectors must be the same size");
713 motif_sequences.clear();
714 for(size_t i = 0; i != motifs.size(); ++i)
716 add_motif(motifs[i], colors[i]);
718 update_sequences_motifs();
721 // I mostly split the ifstream out so I can use a stringstream to test it.
722 void Mussa::load_motifs(std::istream &in)
731 in >> seq >> red >> green >> blue;
732 // if we couldn't read this line 'cause we're like at the end of the file
733 // try to exit the loop
737 seq = Sequence::motif_normalize(seq);
738 } catch(motif_normalize_error e) {
739 clog << "unable to parse " << seq << " skipping" << endl;
740 clog << e.what() << endl;
743 if (red < 0.0 or red > 1.0) {
744 clog << "invalid red value " << red << ". must be in range [0..1]"
748 if (green < 0.0 or green > 1.0) {
749 clog << "invalid green value " << green << ". must be in range [0..1]"
753 if (blue < 0.0 or blue > 1.0) {
754 clog << "invalid blue value " << blue << ". must be in range [0..1]"
758 if (motif_sequences.find(seq) == motif_sequences.end()) {
759 // sequence wasn't found
760 motif_sequences.insert(seq);
761 Color c(red, green, blue);
762 color_mapper->appendInstanceColor("motif", seq, c);
764 clog << "sequence " << seq << " was already defined skipping"
769 update_sequences_motifs();
772 void Mussa::load_motifs(fs::path filename)
775 f.open(filename, ifstream::in);
779 void Mussa::update_sequences_motifs()
781 // once we've loaded all the motifs from the file,
782 // lets attach them to the sequences
783 for(vector<boost::shared_ptr<Sequence> >::iterator seq_i = the_seqs.begin();
784 seq_i != the_seqs.end();
787 // clear out old motifs
788 (*seq_i)->clear_motifs();
789 // for all the motifs in our set, attach them to the current sequence
790 for(set<Sequence>::iterator motif_i = motif_sequences.begin();
791 motif_i != motif_sequences.end();
794 (*seq_i)->add_motif(*motif_i);
799 const set<Sequence>& Mussa::motifs() const
801 return motif_sequences;
804 boost::shared_ptr<AnnotationColors> Mussa::colorMapper()