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 "mussa_exceptions.hh"
16 #include "mussa_class.hh"
29 // set all parameters to null state
34 ana_mode = TransitiveNway;
41 fasta_indices.clear();
43 sub_seq_starts.clear();
47 // these 5 simple methods manually set the parameters for doing an analysis
48 // used so that the gui can take input from user and setup the analysis
49 // note - still need a set_append(bool, bool) method...
51 Mussa::set_name(string a_name)
53 analysis_name = a_name;
56 string Mussa::get_name()
64 if (the_seqs.size() > 0)
65 return the_seqs.size();
66 else if (seq_files.size() > 0)
67 return seq_files.size();
73 Mussa::set_window(int a_window)
78 int Mussa::get_window() const
84 Mussa::set_threshold(int a_threshold)
86 threshold = a_threshold;
87 //soft_thres = a_threshold;
90 int Mussa::get_threshold() const
96 Mussa::set_soft_thres(int sft_thres)
98 soft_thres = sft_thres;
102 Mussa::set_analysis_mode(enum analysis_modes new_ana_mode)
104 ana_mode = new_ana_mode;
107 enum Mussa::analysis_modes Mussa::get_analysis_mode() const
112 string Mussa::get_analysis_mode_name() const
117 return string("Transitive");
120 return string("Radial");
123 return string("Entropy");
126 return string("[deprecated] Recursive");
129 throw runtime_error("invalid analysis mode type");
134 const NwayPaths& Mussa::paths() const
139 // takes a string and sets it as the next seq
141 Mussa::add_a_seq(string a_seq)
146 the_seqs.push_back(aSeq);
150 // sets info for just 1 seq at a time
152 Mussa::set_seq_info(string seq_file, string annot_file, int fa_i, int a_start, int the_end)
154 seq_files.push_back(seq_file);
155 fasta_indices.push_back(fa_i);
156 annot_files.push_back(annot_file);
157 sub_seq_starts.push_back(a_start);
158 sub_seq_ends.push_back(the_end);
161 const vector<Sequence>&
162 Mussa::sequences() const
168 Mussa::load_mupa_file(string para_file_path)
171 string file_data_line;
172 string param, value, annot_file;
173 int split_index, fasta_index;
174 int sub_seq_start, sub_seq_end;
175 bool seq_params, did_seq;
178 string::size_type new_index, dir_index;
183 para_file.open(para_file_path.c_str(), ios::in);
185 // if file was opened, read the parameter values
188 // need to find the path to the .mupa file
193 new_index = (para_file_path.substr(dir_index)).find("/");
194 if (new_index != string::npos)
195 dir_index += new_index + 1;
197 parsing_path = false;
200 file_path_base = para_file_path.substr(0,dir_index);
202 // setup loop by getting file's first line
203 getline(para_file,file_data_line);
204 split_index = file_data_line.find(" ");
205 param = file_data_line.substr(0,split_index);
206 value = file_data_line.substr(split_index+1);
208 while (!para_file.eof())
211 if (param == "ANA_NAME")
212 analysis_name = value;
213 else if (param == "APPEND_WIN")
215 else if (param == "APPEND_THRES")
217 else if (param == "SEQUENCE_NUM")
218 ; // ignore sequence_num now
219 else if (param == "WINDOW")
220 window = atoi(value.c_str());
221 else if (param == "THRESHOLD")
222 threshold = atoi(value.c_str());
223 else if (param == "SEQUENCE")
225 seq_files.push_back(file_path_base + value);
226 //cout << "seq_file_name " << seq_files.back() << endl;
233 while ((!para_file.eof()) && seq_params)
235 getline(para_file,file_data_line);
236 split_index = file_data_line.find(" ");
237 param = file_data_line.substr(0,split_index);
238 value = file_data_line.substr(split_index+1);
240 if (param == "FASTA_INDEX")
241 fasta_index = atoi(value.c_str());
242 else if (param == "ANNOTATION")
243 annot_file = file_path_base + value;
244 else if (param == "SEQ_START")
245 sub_seq_start = atoi(value.c_str());
246 else if (param == "SEQ_END")
248 sub_seq_end = atoi(value.c_str());
250 //ignore empty lines or that start with '#'
251 else if ((param == "") || (param == "#")) {}
252 else seq_params = false;
255 fasta_indices.push_back(fasta_index);
256 annot_files.push_back(annot_file);
257 sub_seq_starts.push_back(sub_seq_start);
258 sub_seq_ends.push_back(sub_seq_end);
261 //ignore empty lines or that start with '#'
262 else if ((param == "") || (param == "#")) {}
265 clog << "Illegal/misplaced mussa parameter in file\n";
266 clog << param << "\n";
271 getline(para_file,file_data_line);
272 split_index = file_data_line.find(" ");
273 param = file_data_line.substr(0,split_index);
274 value = file_data_line.substr(split_index+1);
281 soft_thres = threshold;
282 //cout << "nway mupa: analysis_name = " << analysis_name
283 // << " window = " << window
284 // << " threshold = " << threshold << endl;
286 // no file was loaded, signal error
289 throw mussa_load_error("Config File: " + para_file_path + " not found");
295 Mussa::analyze(int w, int t, enum Mussa::analysis_modes the_ana_mode, double new_ent_thres)
297 time_t t1, t2, begin, end;
298 double seqloadtime, seqcomptime, nwaytime, savetime, totaltime;
302 ana_mode = the_ana_mode;
303 ent_thres = new_ent_thres;
313 load_sequence_data();
315 if (the_seqs.size() < 2) {
316 throw mussa_analysis_error("you need to have at least 2 sequences to "
319 //cout << "nway ana: seq_num = " << the_seqs.size() << endl;
322 seqloadtime = difftime(t2, t1);
327 seqcomptime = difftime(t2, t1);
331 the_paths.setup(window, threshold);
334 nwaytime = difftime(t2, t1);
339 savetime = difftime(t2, t1);
342 totaltime = difftime(end, begin);
345 //cout << "seqload\tseqcomp\tnway\tsave\ttotal\n";
346 //cout << seqloadtime << "\t";
347 //cout << seqcomptime << "\t";
348 //cout << nwaytime << "\t";
349 //cout << savetime << "\t";
350 //cout << totaltime << "\n";
355 Mussa::load_sequence_data()
357 list<string>::iterator seq_files_i, annot_files_i;
358 list<int>::iterator fasta_indices_i, seq_starts_i, seq_ends_i;
363 seq_files_i = seq_files.begin();
364 fasta_indices_i = fasta_indices.begin();
365 annot_files_i = annot_files.begin();
366 seq_starts_i = sub_seq_starts.begin();
367 seq_ends_i = sub_seq_ends.begin();
369 while ( (seq_files_i != seq_files.end()) && (err_msg == "") )
370 /* it should be guarenteed that each of the following exist
371 should I bother checking, and how to deal with if not true...
373 (fasta_indices_i != fasta_indices.end()) &&
374 (annot_files_i != annot_files.end()) &&
375 (seq_starts_i != sub_seq_starts.end()) &&
376 (seq_ends_i != sub_seq_ends.end()) )
379 aSeq.load_fasta(*seq_files_i, *fasta_indices_i,*seq_starts_i, *seq_ends_i);
380 if (annot_files_i->size() > 0)
381 aSeq.load_annot(*annot_files_i, *seq_starts_i, *seq_ends_i);
383 the_seqs.push_back(aSeq);
384 //cout << aSeq.get_header() << endl;
385 //cout << aSeq.get_seq() << endl;
387 ++seq_files_i; // advance all the iterators
399 vector<int> seq_lens;
400 vector<FLPs> empty_FLP_vector;
402 string save_file_string;
404 empty_FLP_vector.clear();
405 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
407 all_comps.push_back(empty_FLP_vector);
408 for(vector<Sequence>::size_type i2 = 0; i2 < the_seqs.size(); i2++)
409 all_comps[i].push_back(dummy_comp);
411 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
412 seq_lens.push_back(the_seqs[i].size());
414 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
415 for(vector<Sequence>::size_type i2 = i+1; i2 < the_seqs.size(); i2++)
417 //cout << "seqcomping: " << i << " v. " << i2 << endl;
418 all_comps[i][i2].setup(window, threshold);
419 all_comps[i][i2].seqcomp(the_seqs[i].get_seq(), the_seqs[i2].get_seq(), false);
420 all_comps[i][i2].seqcomp(the_seqs[i].get_seq(),the_seqs[i2].rev_comp(),true);
427 vector<string> some_Seqs;
429 the_paths.set_soft_thres(soft_thres);
431 if (ana_mode == TransitiveNway) {
432 the_paths.trans_path_search(all_comps);
434 else if (ana_mode == RadialNway) {
435 the_paths.radiate_path_search(all_comps);
437 else if (ana_mode == EntropyNway)
439 //unlike other methods, entropy needs to look at the sequence at this stage
441 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
443 some_Seqs.push_back(the_seqs[i].get_seq());
446 the_paths.setup_ent(ent_thres, some_Seqs); // ent analysis extra setup
447 the_paths.entropy_path_search(all_comps);
450 // old recursive transitive analysis function
451 else if (ana_mode == RecursiveNway)
452 the_paths.find_paths_r(all_comps);
454 the_paths.simple_refine();
460 string save_name, save_path, create_dir_cmd, flp_filepath;
462 ostringstream append_info;
463 int dir_create_status;
466 // not sure why, but gotta close file each time since can't pass file streams
468 save_name = analysis_name;
470 // gotta do bit with adding win & thres if to be appended
474 append_info << "_w" << window;
475 save_name += append_info.str();
481 append_info << "_t" << threshold;
482 save_name += append_info.str();
485 //#include <stdlib.h>
486 // ******* use appropriate for os ------- 1 of 4
487 // the additions for osX make it more sane where it saves the analysis
488 // will come up with a cleaner sol'n later...
489 create_dir_cmd = "mkdir " + save_name; //linux
490 //create_dir_cmd = "mkdir " + file_path_base + save_name; //osX
492 dir_create_status = system( (const char*) create_dir_cmd.c_str());
493 //cout << "action: " << dir_create_status << endl;
495 // save sequence and annots to a special mussa file
497 // ******** use appropriate for OS ---------- 2 of 4
498 save_path = save_name + "/" + save_name + ".museq"; //linux
499 //save_path = file_path_base + save_name + "/" + save_name + ".museq"; //osX
501 save_file.open(save_path.c_str(), ios::out);
502 save_file << "<Mussa_Sequence>" << endl;
505 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
507 the_seqs[i].save(save_file);
510 //save_file.open(save_path.c_str(), ios::app);
511 save_file << "</Mussa_Sequence>" << endl;
514 // save nway paths to its mussa save file
516 // ******** use appropriate for OS -------- 3 of 4
517 save_path = save_name + "/" + save_name + ".muway"; //linux
518 //save_path = file_path_base + save_name + "/" + save_name + ".muway"; //os X
519 the_paths.save(save_path);
521 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
522 for(vector<Sequence>::size_type i2 = i+1; i2 < the_seqs.size(); i2++)
525 append_info << "_sp_" << i << "v" << i2;
526 // ******** use appropriate for OS --------- 4 of 4
528 save_path = save_name + "/" + save_name + append_info.str() + ".flp";
530 //save_path = file_path_base + save_name + "/" + save_name + append_info.str() + ".flp";
531 all_comps[i][i2].save(save_path);
536 Mussa::save_muway(string save_path)
538 the_paths.save(save_path);
542 Mussa::load(string ana_file)
545 string::size_type start_index, end_index;
546 string file_path_base, a_file_path, ana_path;
550 ostringstream append_info;
551 vector<FLPs> empty_FLP_vector;
554 //cout << "ana_file name " << ana_file << endl;
557 end_index = ana_path.size()-1;
558 if (ana_path[end_index] == '/') {
561 start_index = ana_path.rfind('/', end_index);
562 if (start_index == string::npos) {
566 // skip the / we found
569 analysis_name = ana_path.substr(start_index, end_index-start_index+1);
570 //cout << " ana_name " << analysis_name << endl;
571 file_path_base = ana_path.substr(0, start_index) + analysis_name
572 + "/" + analysis_name;
573 a_file_path = file_path_base + ".muway";
574 //cout << " loading museq: " << a_file_path << endl;
575 the_paths.load(a_file_path);
577 int seq_num = the_paths.sequence_count();
579 a_file_path = file_path_base + ".museq";
581 // this is a bit of a hack due to C++ not acting like it should with files
582 for (i = 1; i <= seq_num; i++)
585 //cout << "mussa_class: loading museq frag... " << a_file_path << endl;
586 tmp_seq.load_museq(a_file_path, i);
587 the_seqs.push_back(tmp_seq);
590 empty_FLP_vector.clear();
591 for(i = 0; i < seq_num; i++)
593 all_comps.push_back(empty_FLP_vector);
594 for(i2 = 0; i2 < seq_num; i2++)
595 all_comps[i].push_back(dummy_comp);
598 for(i = 0; i < seq_num; i++)
600 for(i2 = i+1; i2 < seq_num; i2++)
603 append_info << "_sp_" << i << "v" << i2;
604 //cout << append_info.str() << endl;
605 a_file_path = file_path_base + append_info.str() + ".flp";
606 all_comps[i][i2].load(a_file_path);
607 //cout << "real size = " << all_comps[i][i2].size() << endl;
618 save_file.open(analysis_name.c_str(), ios::out);
620 for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
621 save_file << the_seqs[i].get_seq() << endl;
623 save_file << window << endl;
625 //note more complex eventually since analysis_name may need to have
626 //window size, threshold and other stuff to modify it...
627 the_paths.save_old(analysis_name);
632 Mussa::load_old(char * load_file_path, int s_num)
635 string file_data_line;
636 int i, space_split_i, comma_split_i;
637 vector<int> loaded_path;
638 string node_pair, node;
642 the_paths.setup(0, 0);
643 save_file.open(load_file_path, ios::in);
645 // currently loads old mussa format
648 for(i = 0; i < seq_num; i++)
650 getline(save_file, file_data_line);
651 a_seq.set_seq(file_data_line);
652 the_seqs.push_back(a_seq);
656 getline(save_file, file_data_line);
657 window = atoi(file_data_line.c_str());
660 while (!save_file.eof())
663 getline(save_file, file_data_line);
664 if (file_data_line != "")
665 for(i = 0; i < seq_num; i++)
667 space_split_i = file_data_line.find(" ");
668 node_pair = file_data_line.substr(0,space_split_i);
669 //cout << "np= " << node_pair;
670 comma_split_i = node_pair.find(",");
671 node = node_pair.substr(comma_split_i+1);
672 //cout << "n= " << node << " ";
673 loaded_path.push_back(atoi (node.c_str()));
674 file_data_line = file_data_line.substr(space_split_i+1);
677 // FIXME: do we have any information about what the threshold should be?
678 the_paths.add_path(0, loaded_path);
682 //the_paths.save("tmp.save");