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"
32 connect(&the_paths, SIGNAL(progress(const std::string&, int, int)),
33 this, SIGNAL(progress(const std::string&, int, int)));
36 Mussa::Mussa(const Mussa& m)
37 : analysis_name(m.analysis_name),
39 threshold(m.threshold),
40 soft_thres(m.soft_thres),
42 win_append(m.win_append),
43 thres_append(m.thres_append),
44 motif_sequences(m.motif_sequences),
45 color_mapper(m.color_mapper)
47 connect(&the_paths, SIGNAL(progress(const std::string&, int, int)),
48 this, SIGNAL(progress(const std::string&, int, int)));
51 // set all parameters to null state
56 ana_mode = TransitiveNway;
62 motif_sequences.clear();
67 // these 5 simple methods manually set the parameters for doing an analysis
68 // used so that the gui can take input from user and setup the analysis
69 // note - still need a set_append(bool, bool) method...
71 Mussa::set_name(string a_name)
73 analysis_name = a_name;
76 string Mussa::get_name()
84 if (the_seqs.size() > 0)
85 return the_seqs.size();
91 Mussa::set_window(int a_window)
96 int Mussa::get_window() const
102 Mussa::set_threshold(int a_threshold)
104 threshold = a_threshold;
105 if (a_threshold > soft_thres)
106 soft_thres = a_threshold;
109 int Mussa::get_threshold() const
115 Mussa::set_soft_threshold(int new_threshold)
117 if (new_threshold < threshold) {
118 soft_thres = threshold;
119 } else if (new_threshold > window) {
122 soft_thres = new_threshold;
126 int Mussa::get_soft_threshold() const
132 Mussa::set_analysis_mode(enum analysis_modes new_ana_mode)
134 ana_mode = new_ana_mode;
137 enum Mussa::analysis_modes Mussa::get_analysis_mode() const
142 string Mussa::get_analysis_mode_name() const
147 return string("Transitive");
150 return string("Radial");
153 return string("Entropy");
156 return string("[deprecated] Recursive");
159 throw runtime_error("invalid analysis mode type");
164 const NwayPaths& Mussa::paths() const
169 //template <class IteratorT>
170 //void Mussa::createLocalAlignment(IteratorT begin, IteratorT end)
171 void Mussa::createLocalAlignment(std::list<ConservedPath>::iterator begin,
172 std::list<ConservedPath>::iterator end,
173 std::list<ConservedPath::path_type>& result,
174 std::list<std::vector<bool> >& reversed)
176 const vector<Sequence>& raw_seq = the_seqs;
177 ConservedPath::path_type aligned_path;
180 int window_length, win_i;
183 vector<bool> rc_list;
185 vector<bool> matched;
189 for(std::list<ConservedPath>::iterator pathz_i=begin; pathz_i != end; ++pathz_i)
191 ConservedPath& a_path = *pathz_i;
192 window_length = a_path.window_size;
193 // determine which parts of the path are RC relative to first species
194 rc_list = a_path.reverseComplimented();
196 // loop over each bp in the conserved region for all sequences
197 for(win_i = 0; win_i < window_length; win_i++)
199 aligned_path.clear();
200 // determine which exact base pairs match between the sequences
202 for(i2 = 0; i2 < a_path.size()-1; i2++)
204 // assume not rc as most likely, adjust below
207 // no matter the case, any RC node needs adjustments
209 rc_1 = window_length-1;
210 if (a_path[i2+1] < 0)
211 rc_2 = window_length-1;
213 x_start = (abs(a_path[i2]-rc_1+win_i));
214 x_end = (abs(a_path[i2+1]-rc_2+win_i));
217 // ugh, and xor...only want rc coloring if just one of the nodes is rc
218 // if both nodes are rc, then they are 'normal' relative to each other
219 if((rc_list[i2] || rc_list[i2+1] )&&!(rc_list[i2] && rc_list[i2+1]))
220 { //the hideous rc matching logic - not complex, but annoying
221 if(!(( (raw_seq[i2][x_start]=='A')&&(raw_seq[i2+1][x_end]=='T')) ||
222 ((raw_seq[i2][x_start]=='T')&&(raw_seq[i2+1][x_end]=='A')) ||
223 ((raw_seq[i2][x_start]=='G')&&(raw_seq[i2+1][x_end]=='C')) ||
224 ((raw_seq[i2][x_start]=='C')&&(raw_seq[i2+1][x_end]=='G'))) )
228 aligned_path.push_back(x_start);
233 if (!( (raw_seq[i2][x_start] == raw_seq[i2+1][x_end]) &&
234 (raw_seq[i2][x_start] != 'N') &&
235 (raw_seq[i2+1][x_end] != 'N') ) ) {
238 aligned_path.push_back(x_start);
242 // grab the last part of our path, assuming we matched
244 aligned_path.push_back(x_end);
246 if (aligned_path.size() == a_path.size()) {
247 result.push_back(aligned_path);
248 reversed.push_back(rc_list);
256 // takes a string and sets it as the next seq
258 Mussa::add_a_seq(string a_seq)
260 Sequence aSeq(a_seq);
261 the_seqs.push_back(aSeq);
264 const vector<Sequence>&
265 Mussa::sequences() const
270 void Mussa::load_sequence(fs::path seq_file, fs::path annot_file,
271 int fasta_index, int sub_seq_start, int sub_seq_end)
274 aseq.load_fasta(seq_file, fasta_index, sub_seq_start, sub_seq_end);
275 if ( not annot_file.empty() ) {
276 aseq.load_annot(annot_file, sub_seq_start, sub_seq_end);
278 the_seqs.push_back(aseq);
282 Mussa::load_mupa_file(fs::path para_file_path)
284 fs::ifstream para_file;
285 string file_data_line;
288 int split_index, fasta_index;
289 int sub_seq_start, sub_seq_end;
290 bool seq_params, did_seq;
293 string::size_type new_index, dir_index;
298 // if file was opened, read the parameter values
299 if (fs::exists(para_file_path))
301 para_file.open(para_file_path, ios::in);
303 // what directory is the mupa file in?
304 fs::path file_path_base = para_file_path.branch_path();
306 // setup loop by getting file's first line
307 getline(para_file,file_data_line);
308 split_index = file_data_line.find(" ");
309 param = file_data_line.substr(0,split_index);
310 value = file_data_line.substr(split_index+1);
312 while (!para_file.eof())
315 if (param == "ANA_NAME")
316 analysis_name = value;
317 else if (param == "APPEND_WIN")
319 else if (param == "APPEND_THRES")
321 else if (param == "SEQUENCE_NUM")
322 ; // ignore sequence_num now
323 else if (param == "WINDOW")
324 window = atoi(value.c_str());
325 else if (param == "THRESHOLD")
326 threshold = atoi(value.c_str());
327 else if (param == "SEQUENCE")
329 fs::path seq_file = file_path_base / value;
330 //cout << "seq_file_name " << seq_files.back() << endl;
337 while ((!para_file.eof()) && seq_params)
339 getline(para_file,file_data_line);
340 split_index = file_data_line.find(" ");
341 param = file_data_line.substr(0,split_index);
342 value = file_data_line.substr(split_index+1);
344 if (param == "FASTA_INDEX")
345 fasta_index = atoi(value.c_str());
346 else if (param == "ANNOTATION")
347 annot_file = file_path_base / value;
348 else if (param == "SEQ_START")
349 sub_seq_start = atoi(value.c_str());
350 else if (param == "SEQ_END")
352 sub_seq_end = atoi(value.c_str());
354 //ignore empty lines or that start with '#'
355 else if ((param == "") || (param == "#")) {}
356 else seq_params = false;
358 load_sequence(seq_file, annot_file, fasta_index, sub_seq_start,
362 //ignore empty lines or that start with '#'
363 else if ((param == "") || (param == "#")) {}
366 clog << "Illegal/misplaced mussa parameter in file\n";
367 clog << param << "\n";
372 getline(para_file,file_data_line);
373 split_index = file_data_line.find(" ");
374 param = file_data_line.substr(0,split_index);
375 value = file_data_line.substr(split_index+1);
382 soft_thres = threshold;
383 //cout << "nway mupa: analysis_name = " << analysis_name
384 // << " window = " << window
385 // << " threshold = " << threshold << endl;
387 // no file was loaded, signal error
390 throw mussa_load_error("Config File: " + para_file_path.string() + " not found");
398 time_t t1, t2, begin, end;
399 double seqloadtime, seqcomptime, nwaytime, savetime, totaltime;
405 if (the_seqs.size() < 2) {
406 throw mussa_analysis_error("you need to have at least 2 sequences to "
409 //cout << "nway ana: seq_num = " << the_seqs.size() << endl;
412 seqloadtime = difftime(t2, t1);
417 seqcomptime = difftime(t2, t1);
420 the_paths.setup(window, threshold);
423 nwaytime = difftime(t2, t1);
428 savetime = difftime(t2, t1);
431 totaltime = difftime(end, begin);
433 //cout << "seqload\tseqcomp\tnway\tsave\ttotal\n";
434 //cout << seqloadtime << "\t";
435 //cout << seqcomptime << "\t";
436 //cout << nwaytime << "\t";
437 //cout << savetime << "\t";
438 //cout << totaltime << "\n";
444 vector<int> seq_lens;
445 vector<FLPs> empty_FLP_vector;
447 string save_file_string;
449 empty_FLP_vector.clear();
450 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
452 all_comps.push_back(empty_FLP_vector);
453 for(vector<Sequence>::size_type i2 = 0; i2 < the_seqs.size(); i2++)
454 all_comps[i].push_back(dummy_comp);
456 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++) {
457 seq_lens.push_back(the_seqs[i].size());
459 int seqcomps_done = 0;
460 int seqcomps_todo = (the_seqs.size() * (the_seqs.size()-1)) / 2;
461 emit progress("seqcomp", seqcomps_done, seqcomps_todo);
463 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
464 for(vector<Sequence>::size_type i2 = i+1; i2 < the_seqs.size(); i2++)
466 //cout << "seqcomping: " << i << " v. " << i2 << endl;
467 all_comps[i][i2].setup(window, threshold);
468 all_comps[i][i2].seqcomp(the_seqs[i], the_seqs[i2], false);
469 all_comps[i][i2].seqcomp(the_seqs[i], the_seqs[i2].rev_comp(),true);
471 emit progress("seqcomp", seqcomps_done, seqcomps_todo);
478 vector<string> some_Seqs;
480 the_paths.set_soft_threshold(soft_thres);
482 if (ana_mode == TransitiveNway) {
483 the_paths.trans_path_search(all_comps);
485 else if (ana_mode == RadialNway) {
486 the_paths.radiate_path_search(all_comps);
488 else if (ana_mode == EntropyNway)
490 //unlike other methods, entropy needs to look at the sequence at this stage
492 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
494 some_Seqs.push_back(the_seqs[i]);
497 the_paths.setup_ent(ent_thres, some_Seqs); // ent analysis extra setup
498 the_paths.entropy_path_search(all_comps);
501 // old recursive transitive analysis function
502 else if (ana_mode == RecursiveNway)
503 the_paths.find_paths_r(all_comps);
505 the_paths.simple_refine();
512 fs::path flp_filepath;
513 fs::fstream save_file;
514 ostringstream append_info;
515 int dir_create_status;
517 if (not analysis_name.empty()) {
518 // not sure why, but gotta close file each time since can't pass
520 save_name = analysis_name;
522 // gotta do bit with adding win & thres if to be appended
526 append_info << "_w" << window;
527 save_name += append_info.str();
533 append_info << "_t" << threshold;
534 save_name += append_info.str();
536 fs::path save_path( save_name);
538 if (not fs::exists(save_path)) {
539 fs::create_directory(save_path);
541 // save sequence and annots to a special mussa file
542 save_file.open(save_path / (save_name+".museq"), ios::out);
543 save_file << "<Mussa_Sequence>" << endl;
545 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
547 the_seqs[i].save(save_file);
550 save_file << "</Mussa_Sequence>" << endl;
553 // save nway paths to its mussa save file
554 the_paths.save(save_path / (save_name + ".muway"));
556 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
557 for(vector<Sequence>::size_type i2 = i+1; i2 < the_seqs.size(); i2++)
560 append_info << "_sp_" << i << "v" << i2;
561 all_comps[i][i2].save(save_path/(save_name+append_info.str()+".flp"));
567 Mussa::save_muway(fs::path save_path)
569 the_paths.save(save_path);
573 Mussa::load(fs::path ana_file)
576 fs::path file_path_base;
577 fs::path a_file_path;
578 fs::path ana_path(ana_file);
582 ostringstream append_info;
583 vector<FLPs> empty_FLP_vector;
586 //cout << "ana_file name " << ana_file.string() << endl;
587 analysis_name = ana_path.leaf();
588 //cout << " ana_name " << analysis_name << endl;
589 file_path_base = ana_path.branch_path() / analysis_name;
590 a_file_path = file_path_base / (analysis_name + ".muway");
591 //cout << " loading museq: " << a_file_path.string() << endl;
592 the_paths.load(a_file_path);
593 // perhaps this could be more elegent, but at least this'll let
594 // us know what our threshold and window sizes were when we load a muway
595 window = the_paths.get_window();
596 threshold = the_paths.get_threshold();
597 soft_thres = threshold;
599 int seq_num = the_paths.sequence_count();
601 a_file_path = file_path_base / (analysis_name + ".museq");
603 // this is a bit of a hack due to C++ not acting like it should with files
604 for (i = 1; i <= seq_num; i++)
607 //cout << "mussa_class: loading museq frag... " << a_file_path.string() << endl;
608 tmp_seq.load_museq(a_file_path, i);
609 the_seqs.push_back(tmp_seq);
612 empty_FLP_vector.clear();
613 for(i = 0; i < seq_num; i++)
615 all_comps.push_back(empty_FLP_vector);
616 for(i2 = 0; i2 < seq_num; i2++)
617 all_comps[i].push_back(dummy_comp);
620 for(i = 0; i < seq_num; i++)
622 for(i2 = i+1; i2 < seq_num; i2++)
625 append_info << analysis_name << "_sp_" << i << "v" << i2 << ".flp";
626 //cout << append_info.str() << endl;
627 a_file_path = file_path_base / append_info.str();
628 //cout << "path " << a_file_path.string() << endl;
629 all_comps[i][i2].load(a_file_path);
630 //cout << "real size = " << all_comps[i][i2].size() << endl;
639 fs::fstream save_file;
641 save_file.open(analysis_name, ios::out);
643 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
644 save_file << the_seqs[i] << endl;
646 save_file << window << endl;
648 //note more complex eventually since analysis_name may need to have
649 //window size, threshold and other stuff to modify it...
650 the_paths.save_old(analysis_name);
655 Mussa::load_old(char * load_file_path, int s_num)
658 string file_data_line;
659 int i, space_split_i, comma_split_i;
660 vector<int> loaded_path;
661 string node_pair, node;
665 the_paths.setup(0, 0);
666 save_file.open(load_file_path, ios::in);
668 // currently loads old mussa format
671 for(i = 0; i < seq_num; i++)
673 getline(save_file, file_data_line);
674 a_seq = file_data_line;
675 the_seqs.push_back(a_seq);
679 getline(save_file, file_data_line);
680 window = atoi(file_data_line.c_str());
683 while (!save_file.eof())
686 getline(save_file, file_data_line);
687 if (file_data_line != "")
688 for(i = 0; i < seq_num; i++)
690 space_split_i = file_data_line.find(" ");
691 node_pair = file_data_line.substr(0,space_split_i);
692 //cout << "np= " << node_pair;
693 comma_split_i = node_pair.find(",");
694 node = node_pair.substr(comma_split_i+1);
695 //cout << "n= " << node << " ";
696 loaded_path.push_back(atoi (node.c_str()));
697 file_data_line = file_data_line.substr(space_split_i+1);
700 // FIXME: do we have any information about what the threshold should be?
701 the_paths.add_path(0, loaded_path);
705 //the_paths.save("tmp.save");
708 void Mussa::add_motifs(const vector<string>& motifs,
709 const vector<Color>& colors)
711 if (motifs.size() != colors.size()) {
712 throw mussa_error("motif and color vectors must be the same size");
715 for(size_t i = 0; i != motifs.size(); ++i)
717 motif_sequences.insert(motifs[i]);
718 color_mapper.appendInstanceColor("motif", motifs[i], colors[i]);
720 update_sequences_motifs();
723 // I mostly split the ifstream out so I can use a stringstream to test it.
724 void Mussa::load_motifs(std::istream &in)
733 in >> seq >> red >> green >> blue;
734 // if we couldn't read this line 'cause we're like at the end of the file
735 // try to exit the loop
739 seq = Sequence::motif_normalize(seq);
740 } catch(motif_normalize_error e) {
741 clog << "unable to parse " << seq << " skipping" << endl;
742 clog << e.what() << endl;
745 if (red < 0.0 or red > 1.0) {
746 clog << "invalid red value " << red << ". must be in range [0..1]"
750 if (green < 0.0 or green > 1.0) {
751 clog << "invalid green value " << green << ". must be in range [0..1]"
755 if (blue < 0.0 or blue > 1.0) {
756 clog << "invalid blue value " << blue << ". must be in range [0..1]"
760 if (motif_sequences.find(seq) == motif_sequences.end()) {
761 // sequence wasn't found
762 motif_sequences.insert(seq);
763 Color c(red, green, blue);
764 color_mapper.appendInstanceColor("motif", seq, c);
766 clog << "sequence " << seq << " was already defined skipping"
771 update_sequences_motifs();
774 void Mussa::load_motifs(fs::path filename)
777 f.open(filename, ifstream::in);
781 void Mussa::update_sequences_motifs()
783 // once we've loaded all the motifs from the file,
784 // lets attach them to the sequences
785 for(vector<Sequence>::iterator seq_i = the_seqs.begin();
786 seq_i != the_seqs.end();
789 // clear out old motifs
790 seq_i->clear_motifs();
791 // for all the motifs in our set, attach them to the current sequence
792 for(set<string>::iterator motif_i = motif_sequences.begin();
793 motif_i != motif_sequences.end();
796 seq_i->add_motif(*motif_i);
801 const set<string>& Mussa::motifs() const
803 return motif_sequences;
806 AnnotationColors& Mussa::colorMapper()