X-Git-Url: http://woldlab.caltech.edu/gitweb/?a=blobdiff_plain;f=alg%2Fmussa.cpp;h=4b96d1b4d115a6f12915d1b3fd5a1d082d43212e;hb=f88eea68b95773eb5683dcca6cf3fa59b9f00036;hp=47a6b0fe464e72670a0e55b2726d8001ece47de6;hpb=57c7ed97408c77c15d341325f2a18d8530e74cf0;p=mussa.git diff --git a/alg/mussa.cpp b/alg/mussa.cpp index 47a6b0f..4b96d1b 100644 --- a/alg/mussa.cpp +++ b/alg/mussa.cpp @@ -12,22 +12,18 @@ // ---------- mussa_class.cc ----------- // ---------------------------------------- +#include #include #include namespace fs = boost::filesystem; -#include -#include -#include -#include -namespace spirit = boost::spirit; - #include #include #include "mussa_exceptions.hpp" -#include "alg/mussa.hpp" #include "alg/flp.hpp" +#include "alg/mussa.hpp" +#include "alg/motif_parser.hpp" using namespace std; @@ -36,8 +32,8 @@ Mussa::Mussa() : color_mapper(new AnnotationColors) { clear(); - connect(&the_paths, SIGNAL(progress(const std::string&, int, int)), - this, SIGNAL(progress(const std::string&, int, int))); + connect(&the_paths, SIGNAL(progress(const QString&, int, int)), + this, SIGNAL(progress(const QString&, int, int))); } Mussa::Mussa(const Mussa& m) @@ -53,8 +49,14 @@ Mussa::Mussa(const Mussa& m) analysis_path(m.analysis_path), dirty(m.dirty) { - connect(&the_paths, SIGNAL(progress(const std::string&, int, int)), - this, SIGNAL(progress(const std::string&, int, int))); + connect(&the_paths, SIGNAL(progress(const QString&, int, int)), + this, SIGNAL(progress(const QString&, int, int))); +} + +MussaRef Mussa::init() +{ + boost::shared_ptr m(new Mussa()); + return m; } boost::filesystem::path Mussa::get_analysis_path() const @@ -83,7 +85,15 @@ Mussa::clear() the_seqs.clear(); the_paths.clear(); analysis_path = fs::path(); - dirty = false; + set_dirty(false); +} + +void Mussa::set_dirty(bool new_state) +{ + if (dirty != new_state) { + dirty = new_state; + emit isModified(dirty); + } } bool Mussa::is_dirty() const @@ -104,7 +114,7 @@ void Mussa::set_name(string a_name) { analysis_name = a_name; - dirty = true; + set_dirty(true); } string Mussa::get_name() const @@ -137,7 +147,7 @@ void Mussa::set_window(int a_window) { window = a_window; - dirty = true; + set_dirty(true); } int Mussa::get_window() const @@ -149,7 +159,7 @@ void Mussa::set_threshold(int a_threshold) { threshold = a_threshold; - dirty = true; + set_dirty(true); if (a_threshold > soft_thres) { soft_thres = a_threshold; } @@ -181,7 +191,7 @@ void Mussa::set_analysis_mode(enum analysis_modes new_ana_mode) { ana_mode = new_ana_mode; - dirty = true; + set_dirty(true); } enum Mussa::analysis_modes Mussa::get_analysis_mode() const @@ -308,17 +318,17 @@ void Mussa::append_sequence(const Sequence& a_seq) { boost::shared_ptr seq_copy(new Sequence(a_seq)); the_seqs.push_back(seq_copy); - dirty = true; + set_dirty(true); } void Mussa::append_sequence(boost::shared_ptr a_seq) { the_seqs.push_back(a_seq); - dirty = true; + set_dirty(true); } -const vector >& +const vector& Mussa::sequences() const { return the_seqs; @@ -337,7 +347,7 @@ void Mussa::load_sequence(fs::path seq_file, fs::path annot_file, aseq->set_species(*name); } the_seqs.push_back(aseq); - dirty = true; + set_dirty(true); } void @@ -453,7 +463,7 @@ Mussa::load_mupa_file(fs::path para_file_path) // << " threshold = " << threshold << endl; } // no file was loaded, signal error - dirty = true; + set_dirty(true); } @@ -464,8 +474,7 @@ Mussa::analyze() throw mussa_analysis_error("you need to have at least 2 sequences to " "do an analysis."); } - //cout << "nway ana: seq_num = " << the_seqs.size() << endl; - + seqcomp(); the_paths.setup(window, threshold); nway(); @@ -541,7 +550,6 @@ Mussa::nway() void Mussa::save(fs::path save_path) { - fs::path flp_filepath; fs::fstream save_file; ostringstream append_info; int dir_create_status; @@ -572,8 +580,12 @@ Mussa::save(fs::path save_path) if (not fs::exists(save_path)) { fs::create_directory(save_path); } + + std::string basename = save_path.leaf(); + fs::path museq(basename + ".museq", fs::native); + // save sequence and annots to a special mussa file - save_file.open(save_path / (save_path.leaf()+".museq"), ios::out); + save_file.open(save_path / museq, ios::out); save_file << "" << endl; for(vector::size_type i = 0; i < the_seqs.size(); i++) @@ -584,18 +596,27 @@ Mussa::save(fs::path save_path) save_file << "" << endl; save_file.close(); + // if we have any motifs, save them. + if (motif_sequences.size()) { + fs::path mtl(basename + ".mtl", fs::native); + save_motifs(save_path / mtl); + } + // save nway paths to its mussa save file - the_paths.save(save_path / (save_path.leaf()+ ".muway")); + fs::path muway(basename + ".muway", fs::native); + the_paths.save(save_path / muway); for(vector::size_type i = 0; i < the_seqs.size(); i++) { for(vector::size_type i2 = i+1; i2 < the_seqs.size(); i2++) { append_info.str(""); append_info << "_sp_" << i << "v" << i2; - all_comps[i][i2].save(save_path/(save_path.leaf()+append_info.str()+".flp")); + fs::path flp(basename+append_info.str()+".flp", fs::native); + all_comps[i][i2].save(save_path / flp); } } - dirty = false; + + set_dirty(false); analysis_path = save_path; } @@ -619,12 +640,9 @@ Mussa::load(fs::path ana_file) FLPs dummy_comp; analysis_path = ana_file; - //clog << "ana_file name " << ana_file.string() << endl; analysis_name = ana_path.leaf(); - //clog << " ana_name " << analysis_name << endl; - file_path_base = ana_path.branch_path() / analysis_name; - a_file_path = file_path_base / (analysis_name + ".muway"); - //clog << " loading museq: " << a_file_path.string() << endl; + fs::path muway(analysis_name+".muway", fs::native); + a_file_path = analysis_path / muway; the_paths.load(a_file_path); // perhaps this could be more elegent, but at least this'll let // us know what our threshold and window sizes were when we load a muway @@ -634,17 +652,22 @@ Mussa::load(fs::path ana_file) int seq_num = the_paths.sequence_count(); - a_file_path = file_path_base / (analysis_name + ".museq"); + fs::path museq(analysis_name + ".museq", fs::native); + a_file_path = analysis_path / museq; // this is a bit of a hack due to C++ not acting like it should with files for (i = 1; i <= seq_num; i++) { boost::shared_ptr tmp_seq(new Sequence); - //clog << "mussa_class: loading museq frag... " << a_file_path.string() << endl; tmp_seq->load_museq(a_file_path, i); the_seqs.push_back(tmp_seq); } + fs::path mtl(analysis_name + ".mtl", fs::native); + fs::path motif_file = analysis_path / mtl; + if (fs::exists(motif_file)) { + load_motifs(motif_file); + } empty_FLP_vector.clear(); for(i = 0; i < seq_num; i++) { @@ -660,15 +683,13 @@ Mussa::load(fs::path ana_file) append_info.str(""); append_info << analysis_name << "_sp_" << i << "v" << i2 << ".flp"; //clog << append_info.str() << endl; - a_file_path = file_path_base / append_info.str(); - //clog << "path " << a_file_path.string() << endl; + fs::path flp(append_info.str(), fs::native); + a_file_path = analysis_path / flp; all_comps[i][i2].load(a_file_path); - //clog << "real size = " << all_comps[i][i2].size() << endl; } } } - void Mussa::save_old() { @@ -745,6 +766,7 @@ void Mussa::add_motif(const Sequence& motif, const Color& color) { motif_sequences.insert(motif); color_mapper->appendInstanceColor("motif", motif.get_sequence(), color); + set_dirty(true); } void Mussa::set_motifs(const vector& motifs, @@ -762,60 +784,18 @@ void Mussa::set_motifs(const vector& motifs, update_sequences_motifs(); } -// Helper functor to append created motifs to our Mussa analysis -struct push_back_motif { - std::set& motif_set; - boost::shared_ptr color_mapper; - std::string& seq_string; - std::string& name; - float& red; - float& green; - float& blue; - float& alpha; - - push_back_motif(std::set& motif_set_, - boost::shared_ptr color_mapper_, - std::string& seq_, - std::string& name_, - float &red_, float &green_, float &blue_, float &alpha_) - : motif_set(motif_set_), - color_mapper(color_mapper_), - seq_string(seq_), - name(name_), - red(red_), - green(green_), - blue(blue_), - alpha(alpha_) - { - } - - void operator()(std::string::const_iterator, - std::string::const_iterator) const - { - Sequence seq(seq_string); - // shouldn't we have a better field than "fasta header" and speices? - seq.set_fasta_header(name); - // we need to clear the name in case the next motif doesn't have one. - name.clear(); - // be nice if glsequence was a subclass of sequence so we could - // just attach colors directly to the motif. - Color c(red, green, blue); - color_mapper->appendInstanceColor("motif", seq.c_str(), c); - motif_set.insert(seq); - }; -}; - -// I mostly split the ifstream out so I can use a stringstream to test it. +void Mussa::load_motifs(fs::path filename) +{ + fs::ifstream f; + f.open(filename, ifstream::in); + load_motifs(f); +} + void Mussa::load_motifs(std::istream &in) { std::string data; - const char *alphabet = Sequence::nucleic_iupac_alphabet.c_str(); - string seq; - string name; - float red = 1.0; - float green = 0.0; - float blue = 0.0; - float alpha = 1.0; + const char *alphabet = Alphabet::dna_cstr; + motif_parser::ParsedMotifs parsed_motifs(motif_sequences, color_mapper); // slurp our data into a string std::streamsize bytes_read = 1; @@ -825,34 +805,30 @@ void Mussa::load_motifs(std::istream &in) bytes_read = in.readsome(buf, bufsiz); data.append(buf, buf+bytes_read); } - // parse our string - bool ok = spirit::parse(data.begin(), data.end(), - *( - ( - ( - (+spirit::chset<>(alphabet))[spirit::assign_a(seq)] >> - +spirit::space_p - ) >> - !( - (spirit::alpha_p >> *spirit::graph_p)[spirit::assign_a(name)] - >> +spirit::space_p - ) >> - spirit::real_p[spirit::assign_a(red)] >> +spirit::space_p >> - spirit::real_p[spirit::assign_a(green)] >> +spirit::space_p >> - spirit::real_p[spirit::assign_a(blue)] >> +spirit::space_p - )[push_back_motif(motif_sequences, color_mapper, seq, name, red, green, blue, alpha)] - )).full; - if (not ok) { - std::clog << "Error parsing motif stream " << std::endl; - } + parsed_motifs.parse(data); update_sequences_motifs(); } -void Mussa::load_motifs(fs::path filename) +void Mussa::save_motifs(fs::path filename) { - fs::ifstream f; - f.open(filename, ifstream::in); - load_motifs(f); + fs::ofstream out_stream; + out_stream.open(filename, ofstream::out); + save_motifs(out_stream); +} + +void Mussa::save_motifs(std::ostream& out) +{ + for(motif_set::iterator motif_i = motif_sequences.begin(); + motif_i != motif_sequences.end(); + ++motif_i) + { + out << motif_i->get_sequence() << " "; + if (motif_i->get_name().size() > 0) { + out << "\"" << motif_i->get_name() << "\" "; + } + out << color_mapper->lookup("motif", motif_i->get_sequence()); + out << std::endl; + } } void Mussa::update_sequences_motifs()