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 // ----------------------------------------
19 #include "mussa_exceptions.hpp"
20 #include "alg/mussa.hpp"
21 #include "alg/flp.hpp"
30 Mussa::Mussa(const Mussa& m)
31 : analysis_name(m.analysis_name),
33 threshold(m.threshold),
34 soft_thres(m.soft_thres),
36 win_append(m.win_append),
37 thres_append(m.thres_append),
38 motif_sequences(m.motif_sequences),
39 color_mapper(m.color_mapper)
43 // set all parameters to null state
48 ana_mode = TransitiveNway;
54 motif_sequences.clear();
58 // these 5 simple methods manually set the parameters for doing an analysis
59 // used so that the gui can take input from user and setup the analysis
60 // note - still need a set_append(bool, bool) method...
62 Mussa::set_name(string a_name)
64 analysis_name = a_name;
67 string Mussa::get_name()
75 if (the_seqs.size() > 0)
76 return the_seqs.size();
82 Mussa::set_window(int a_window)
87 int Mussa::get_window() const
93 Mussa::set_threshold(int a_threshold)
95 threshold = a_threshold;
96 //soft_thres = a_threshold;
99 int Mussa::get_threshold() const
105 Mussa::set_soft_thres(int sft_thres)
107 soft_thres = sft_thres;
111 Mussa::set_analysis_mode(enum analysis_modes new_ana_mode)
113 ana_mode = new_ana_mode;
116 enum Mussa::analysis_modes Mussa::get_analysis_mode() const
121 string Mussa::get_analysis_mode_name() const
126 return string("Transitive");
129 return string("Radial");
132 return string("Entropy");
135 return string("[deprecated] Recursive");
138 throw runtime_error("invalid analysis mode type");
143 const NwayPaths& Mussa::paths() const
148 // takes a string and sets it as the next seq
150 Mussa::add_a_seq(string a_seq)
155 the_seqs.push_back(aSeq);
158 const vector<Sequence>&
159 Mussa::sequences() const
164 void Mussa::load_sequence(string seq_file, string annot_file, int fasta_index,
165 int sub_seq_start, int sub_seq_end)
168 aseq.load_fasta(seq_file, fasta_index, sub_seq_start, sub_seq_end);
169 if (annot_file.size() > 0) {
170 aseq.load_annot(annot_file, sub_seq_start, sub_seq_end);
172 the_seqs.push_back(aseq);
176 Mussa::load_mupa_file(string para_file_path)
178 string file_path_base;
180 string file_data_line;
181 string param, value, annot_file;
182 int split_index, fasta_index;
183 int sub_seq_start, sub_seq_end;
184 bool seq_params, did_seq;
187 string::size_type new_index, dir_index;
192 para_file.open(para_file_path.c_str(), ios::in);
194 // if file was opened, read the parameter values
197 // need to find the path to the .mupa file
202 new_index = (para_file_path.substr(dir_index)).find("/");
203 if (new_index != string::npos)
204 dir_index += new_index + 1;
206 parsing_path = false;
209 file_path_base = para_file_path.substr(0,dir_index);
211 // setup loop by getting file's first line
212 getline(para_file,file_data_line);
213 split_index = file_data_line.find(" ");
214 param = file_data_line.substr(0,split_index);
215 value = file_data_line.substr(split_index+1);
217 while (!para_file.eof())
220 if (param == "ANA_NAME")
221 analysis_name = value;
222 else if (param == "APPEND_WIN")
224 else if (param == "APPEND_THRES")
226 else if (param == "SEQUENCE_NUM")
227 ; // ignore sequence_num now
228 else if (param == "WINDOW")
229 window = atoi(value.c_str());
230 else if (param == "THRESHOLD")
231 threshold = atoi(value.c_str());
232 else if (param == "SEQUENCE")
234 string seq_file = file_path_base + value;
235 //cout << "seq_file_name " << seq_files.back() << endl;
242 while ((!para_file.eof()) && seq_params)
244 getline(para_file,file_data_line);
245 split_index = file_data_line.find(" ");
246 param = file_data_line.substr(0,split_index);
247 value = file_data_line.substr(split_index+1);
249 if (param == "FASTA_INDEX")
250 fasta_index = atoi(value.c_str());
251 else if (param == "ANNOTATION")
252 annot_file = file_path_base + value;
253 else if (param == "SEQ_START")
254 sub_seq_start = atoi(value.c_str());
255 else if (param == "SEQ_END")
257 sub_seq_end = atoi(value.c_str());
259 //ignore empty lines or that start with '#'
260 else if ((param == "") || (param == "#")) {}
261 else seq_params = false;
263 load_sequence(seq_file, annot_file, fasta_index, sub_seq_start,
267 //ignore empty lines or that start with '#'
268 else if ((param == "") || (param == "#")) {}
271 clog << "Illegal/misplaced mussa parameter in file\n";
272 clog << param << "\n";
277 getline(para_file,file_data_line);
278 split_index = file_data_line.find(" ");
279 param = file_data_line.substr(0,split_index);
280 value = file_data_line.substr(split_index+1);
287 soft_thres = threshold;
288 //cout << "nway mupa: analysis_name = " << analysis_name
289 // << " window = " << window
290 // << " threshold = " << threshold << endl;
292 // no file was loaded, signal error
295 throw mussa_load_error("Config File: " + para_file_path + " not found");
301 Mussa::analyze(int w, int t, enum Mussa::analysis_modes the_ana_mode, double new_ent_thres)
303 time_t t1, t2, begin, end;
304 double seqloadtime, seqcomptime, nwaytime, savetime, totaltime;
308 ana_mode = the_ana_mode;
309 ent_thres = new_ent_thres;
320 if (the_seqs.size() < 2) {
321 throw mussa_analysis_error("you need to have at least 2 sequences to "
324 //cout << "nway ana: seq_num = " << the_seqs.size() << endl;
327 seqloadtime = difftime(t2, t1);
332 seqcomptime = difftime(t2, t1);
336 the_paths.setup(window, threshold);
339 nwaytime = difftime(t2, t1);
344 savetime = difftime(t2, t1);
347 totaltime = difftime(end, begin);
350 //cout << "seqload\tseqcomp\tnway\tsave\ttotal\n";
351 //cout << seqloadtime << "\t";
352 //cout << seqcomptime << "\t";
353 //cout << nwaytime << "\t";
354 //cout << savetime << "\t";
355 //cout << totaltime << "\n";
361 vector<int> seq_lens;
362 vector<FLPs> empty_FLP_vector;
364 string save_file_string;
366 empty_FLP_vector.clear();
367 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
369 all_comps.push_back(empty_FLP_vector);
370 for(vector<Sequence>::size_type i2 = 0; i2 < the_seqs.size(); i2++)
371 all_comps[i].push_back(dummy_comp);
373 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
374 seq_lens.push_back(the_seqs[i].size());
376 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
377 for(vector<Sequence>::size_type i2 = i+1; i2 < the_seqs.size(); i2++)
379 //cout << "seqcomping: " << i << " v. " << i2 << endl;
380 all_comps[i][i2].setup(window, threshold);
381 all_comps[i][i2].seqcomp(the_seqs[i].get_seq(), the_seqs[i2].get_seq(), false);
382 all_comps[i][i2].seqcomp(the_seqs[i].get_seq(),the_seqs[i2].rev_comp(),true);
389 vector<string> some_Seqs;
391 the_paths.set_soft_thres(soft_thres);
393 if (ana_mode == TransitiveNway) {
394 the_paths.trans_path_search(all_comps);
396 else if (ana_mode == RadialNway) {
397 the_paths.radiate_path_search(all_comps);
399 else if (ana_mode == EntropyNway)
401 //unlike other methods, entropy needs to look at the sequence at this stage
403 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
405 some_Seqs.push_back(the_seqs[i].get_seq());
408 the_paths.setup_ent(ent_thres, some_Seqs); // ent analysis extra setup
409 the_paths.entropy_path_search(all_comps);
412 // old recursive transitive analysis function
413 else if (ana_mode == RecursiveNway)
414 the_paths.find_paths_r(all_comps);
416 the_paths.simple_refine();
422 string save_name, save_path, create_dir_cmd, flp_filepath;
424 ostringstream append_info;
425 int dir_create_status;
428 // not sure why, but gotta close file each time since can't pass file streams
430 save_name = analysis_name;
432 // gotta do bit with adding win & thres if to be appended
436 append_info << "_w" << window;
437 save_name += append_info.str();
443 append_info << "_t" << threshold;
444 save_name += append_info.str();
447 //#include <stdlib.h>
448 // ******* use appropriate for os ------- 1 of 4
449 // the additions for osX make it more sane where it saves the analysis
450 // will come up with a cleaner sol'n later...
451 create_dir_cmd = "mkdir " + save_name; //linux
452 //create_dir_cmd = "mkdir " + file_path_base + save_name; //osX
454 dir_create_status = system( (const char*) create_dir_cmd.c_str());
455 //cout << "action: " << dir_create_status << endl;
457 // save sequence and annots to a special mussa file
459 // ******** use appropriate for OS ---------- 2 of 4
460 save_path = save_name + "/" + save_name + ".museq"; //linux
461 //save_path = file_path_base + save_name + "/" + save_name + ".museq"; //osX
463 save_file.open(save_path.c_str(), ios::out);
464 save_file << "<Mussa_Sequence>" << endl;
467 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
469 the_seqs[i].save(save_file);
472 //save_file.open(save_path.c_str(), ios::app);
473 save_file << "</Mussa_Sequence>" << endl;
476 // save nway paths to its mussa save file
478 // ******** use appropriate for OS -------- 3 of 4
479 save_path = save_name + "/" + save_name + ".muway"; //linux
480 //save_path = file_path_base + save_name + "/" + save_name + ".muway"; //os X
481 the_paths.save(save_path);
483 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
484 for(vector<Sequence>::size_type i2 = i+1; i2 < the_seqs.size(); i2++)
487 append_info << "_sp_" << i << "v" << i2;
488 // ******** use appropriate for OS --------- 4 of 4
490 save_path = save_name + "/" + save_name + append_info.str() + ".flp";
492 //save_path = file_path_base + save_name + "/" + save_name + append_info.str() + ".flp";
493 all_comps[i][i2].save(save_path);
498 Mussa::save_muway(string save_path)
500 the_paths.save(save_path);
504 Mussa::load(string ana_file)
507 string::size_type start_index, end_index;
508 string file_path_base, a_file_path, ana_path;
512 ostringstream append_info;
513 vector<FLPs> empty_FLP_vector;
516 //cout << "ana_file name " << ana_file << endl;
519 end_index = ana_path.size()-1;
520 if (ana_path[end_index] == '/') {
523 start_index = ana_path.rfind('/', end_index);
524 if (start_index == string::npos) {
528 // skip the / we found
531 analysis_name = ana_path.substr(start_index, end_index-start_index+1);
532 //cout << " ana_name " << analysis_name << endl;
533 file_path_base = ana_path.substr(0, start_index) + analysis_name
534 + "/" + analysis_name;
535 a_file_path = file_path_base + ".muway";
536 //cout << " loading museq: " << a_file_path << endl;
537 the_paths.load(a_file_path);
539 int seq_num = the_paths.sequence_count();
541 a_file_path = file_path_base + ".museq";
543 // this is a bit of a hack due to C++ not acting like it should with files
544 for (i = 1; i <= seq_num; i++)
547 //cout << "mussa_class: loading museq frag... " << a_file_path << endl;
548 tmp_seq.load_museq(a_file_path, i);
549 the_seqs.push_back(tmp_seq);
552 empty_FLP_vector.clear();
553 for(i = 0; i < seq_num; i++)
555 all_comps.push_back(empty_FLP_vector);
556 for(i2 = 0; i2 < seq_num; i2++)
557 all_comps[i].push_back(dummy_comp);
560 for(i = 0; i < seq_num; i++)
562 for(i2 = i+1; i2 < seq_num; i2++)
565 append_info << "_sp_" << i << "v" << i2;
566 //cout << append_info.str() << endl;
567 a_file_path = file_path_base + append_info.str() + ".flp";
568 all_comps[i][i2].load(a_file_path);
569 //cout << "real size = " << all_comps[i][i2].size() << endl;
580 save_file.open(analysis_name.c_str(), ios::out);
582 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
583 save_file << the_seqs[i].get_seq() << endl;
585 save_file << window << endl;
587 //note more complex eventually since analysis_name may need to have
588 //window size, threshold and other stuff to modify it...
589 the_paths.save_old(analysis_name);
594 Mussa::load_old(char * load_file_path, int s_num)
597 string file_data_line;
598 int i, space_split_i, comma_split_i;
599 vector<int> loaded_path;
600 string node_pair, node;
604 the_paths.setup(0, 0);
605 save_file.open(load_file_path, ios::in);
607 // currently loads old mussa format
610 for(i = 0; i < seq_num; i++)
612 getline(save_file, file_data_line);
613 a_seq.set_seq(file_data_line);
614 the_seqs.push_back(a_seq);
618 getline(save_file, file_data_line);
619 window = atoi(file_data_line.c_str());
622 while (!save_file.eof())
625 getline(save_file, file_data_line);
626 if (file_data_line != "")
627 for(i = 0; i < seq_num; i++)
629 space_split_i = file_data_line.find(" ");
630 node_pair = file_data_line.substr(0,space_split_i);
631 //cout << "np= " << node_pair;
632 comma_split_i = node_pair.find(",");
633 node = node_pair.substr(comma_split_i+1);
634 //cout << "n= " << node << " ";
635 loaded_path.push_back(atoi (node.c_str()));
636 file_data_line = file_data_line.substr(space_split_i+1);
639 // FIXME: do we have any information about what the threshold should be?
640 the_paths.add_path(0, loaded_path);
644 //the_paths.save("tmp.save");
647 // I mostly split the ifstream out so I can use a stringstream to test it.
648 void Mussa::load_motifs(std::istream &in)
657 in >> seq >> red >> green >> blue;
658 // if we couldn't read this line 'cause we're like at the end of the file
659 // try to exit the loop
663 seq = Sequence::motif_normalize(seq);
664 } catch(motif_normalize_error e) {
665 clog << "unable to parse " << seq << " skipping" << endl;
666 clog << e.what() << endl;
669 if (red < 0.0 or red > 1.0) {
670 clog << "invalid red value " << red << ". must be in range [0..1]"
674 if (green < 0.0 or green > 1.0) {
675 clog << "invalid green value " << green << ". must be in range [0..1]"
679 if (blue < 0.0 or blue > 1.0) {
680 clog << "invalid blue value " << blue << ". must be in range [0..1]"
684 if (motif_sequences.find(seq) == motif_sequences.end()) {
685 // sequence wasn't found
686 motif_sequences.insert(seq);
687 Color c(red, green, blue);
688 color_mapper.appendInstanceColor("motif", seq, c);
690 clog << "sequence " << seq << " was already defined skipping"
695 // once we've loaded all the motifs from the file,
696 // lets attach them to the sequences
697 for(vector<Sequence>::iterator seq_i = the_seqs.begin();
698 seq_i != the_seqs.end();
701 // clear out old motifs
702 seq_i->clear_motifs();
703 // for all the motifs in our set, attach them to the current sequence
704 for(set<string>::iterator motif_i = motif_sequences.begin();
705 motif_i != motif_sequences.end();
708 seq_i->add_motif(*motif_i);
713 void Mussa::load_motifs(string filename)
716 f.open(filename.c_str(), ifstream::in);
720 AnnotationColors& Mussa::colorMapper()