X-Git-Url: http://woldlab.caltech.edu/gitweb/?a=blobdiff_plain;f=alg%2Fsequence.cpp;h=521496d3497b2b96d4786819438d170f6ac50ce4;hb=f3c0553a22b0e4ddc39ee45f51725352d92e97f1;hp=608ddd3b2059ba808c283e7726b181bfb27e0ce2;hpb=f3e6762bedaf3775619a36f4103c8cf35d2ca887;p=mussa.git diff --git a/alg/sequence.cpp b/alg/sequence.cpp index 608ddd3..521496d 100644 --- a/alg/sequence.cpp +++ b/alg/sequence.cpp @@ -32,6 +32,7 @@ namespace fs = boost::filesystem; namespace spirit = boost::spirit; #include "alg/sequence.hpp" +#include "io.hpp" #include "mussa_exceptions.hpp" #include @@ -40,27 +41,7 @@ namespace spirit = boost::spirit; #include #include -annot::annot() - : begin(0), - end(0), - type(""), - name("") -{ -} - -annot::annot(int begin, int end, std::string type, std::string name) - : begin(begin), - end(end), - type(type), - name(name) -{ -} - -annot::~annot() -{ -} - -bool operator==(const annot& left, const annot& right) +bool operator==(const motif& left, const motif& right) { return ((left.begin== right.begin) and (left.end == right.end) and @@ -68,8 +49,20 @@ bool operator==(const annot& left, const annot& right) (left.name == right.name)); } -motif::motif(int begin, std::string motif) - : annot(begin, begin+motif.size(), "motif", motif), +motif::motif() + : begin(0), + end(0), + type("motif"), + name(""), + sequence("") +{ +} + +motif::motif(int begin_, std::string motif) + : begin(begin_), + end(begin_+motif.size()), + type("motif"), + name(motif), sequence(motif) { } @@ -78,9 +71,10 @@ motif::~motif() { } - Sequence::Sequence(AlphabetRef alphabet) - : seq(new SeqSpan("", alphabet, SeqSpan::PlusStrand)) + : seq(new SeqSpan("", alphabet, SeqSpan::PlusStrand)), + annotation_list(new SeqSpanRefList), + motif_list(new MotifList) { } @@ -90,7 +84,9 @@ Sequence::~Sequence() Sequence::Sequence(const char *seq, AlphabetRef alphabet_, SeqSpan::strand_type strand_) : header(""), - species("") + species(""), + annotation_list(new SeqSpanRefList), + motif_list(new MotifList) { set_filtered_sequence(seq, alphabet_, 0, npos, strand_); } @@ -99,7 +95,9 @@ Sequence::Sequence(const std::string& seq, AlphabetRef alphabet_, SeqSpan::strand_type strand_) : header(""), - species("") + species(""), + annotation_list(new SeqSpanRefList), + motif_list(new MotifList) { set_filtered_sequence(seq, alphabet_, 0, seq.size(), strand_); } @@ -108,7 +106,7 @@ Sequence::Sequence(const Sequence& o) : seq(o.seq), header(o.header), species(o.species), - annots(o.annots), + annotation_list(o.annotation_list), motif_list(o.motif_list) { } @@ -117,7 +115,7 @@ Sequence::Sequence(const Sequence* o) : seq(o->seq), header(o->header), species(o->species), - annots(o->annots), + annotation_list(o->annotation_list), motif_list(o->motif_list) { } @@ -126,15 +124,30 @@ Sequence::Sequence(const SequenceRef o) : seq(new SeqSpan(o->seq)), header(o->header), species(o->species), - annots(o->annots), + annotation_list(new SeqSpanRefList), motif_list(o->motif_list) { + // copy over the annotations in the other sequence ref, + // attaching them to our current sequence ref + for(SeqSpanRefList::const_iterator annot_i = o->annotation_list->begin(); + annot_i != o->annotation_list->end(); + ++annot_i) + { + size_type annot_begin= (*annot_i)->start(); + size_type annot_count = (*annot_i)->size(); + + SeqSpanRef new_annot(seq->subseq(annot_begin, annot_count)); + new_annot->setAnnotations((*annot_i)->annotations()); + annotation_list->push_back(new_annot); + } } Sequence::Sequence(const SeqSpanRef& seq_ref) : seq(seq_ref), header(""), - species("") + species(""), + annotation_list(new SeqSpanRefList), + motif_list(new MotifList) { } @@ -144,28 +157,12 @@ Sequence &Sequence::operator=(const Sequence& s) seq = s.seq; header = s.header; species = s.species; - annots = s.annots; + annotation_list = s.annotation_list; motif_list = s.motif_list; } return *this; } -static void multiplatform_getline(std::istream& in, std::string& line) -{ - line.clear(); - char c; - in.get(c); - while(in.good() and !(c == '\012' or c == '\015') ) { - line.push_back(c); - in.get(c); - } - // if we have cr-lf eat it - c = in.peek(); - if (c=='\012' or c == '\015') { - in.get(); - } -} - void Sequence::load_fasta(fs::path file_path, int seq_num, int start_index, int end_index) { load_fasta(file_path, reduced_nucleic_alphabet, seq_num, start_index, end_index); @@ -327,6 +324,21 @@ Sequence::load_annot(fs::path file_path, int start_index, int end_index) throw mussa_load_error("Error loading annotation file " + file_path.string()); } + try { + load_annot(data_stream, start_index, end_index); + } catch(annotation_load_error e) { + std::ostringstream msg; + msg << file_path.native_file_string() + << " " + << e.what(); + throw annotation_load_error(msg.str()); + } + data_stream.close(); +} + +void +Sequence::load_annot(std::istream& data_stream, int start_index, int end_index) +{ // so i should probably be passing the parse function some iterators // but the annotations files are (currently) small, so i think i can // get away with loading the whole file into memory @@ -336,17 +348,8 @@ Sequence::load_annot(fs::path file_path, int start_index, int end_index) data_stream.get(c); data.push_back(c); } - data_stream.close(); - try { - parse_annot(data, start_index, end_index); - } catch(annotation_load_error e) { - std::ostringstream msg; - msg << file_path.native_file_string() - << " " - << e.what(); - throw annotation_load_error(msg.str()); - } + parse_annot(data, start_index, end_index); } /* If this works, yikes, this is some brain hurting code. @@ -367,20 +370,23 @@ Sequence::load_annot(fs::path file_path, int start_index, int end_index) */ struct push_back_annot { - std::list& annot_list; + Sequence* parent; + SeqSpanRefListRef children; int& begin; int& end; std::string& name; std::string& type; int &parsed; - push_back_annot(std::list& annot_list_, + push_back_annot(Sequence* parent_seq, + SeqSpanRefListRef children_list, int& begin_, int& end_, std::string& name_, std::string& type_, int &parsed_) - : annot_list(annot_list_), + : parent(parent_seq), + children(children_list), begin(begin_), end(end_), name(name_), @@ -392,8 +398,7 @@ struct push_back_annot { void operator()(std::string::const_iterator, std::string::const_iterator) const { - //std::cout << "adding annot: " << begin << "|" << end << "|" << name << "|" << type << std::endl; - annot_list.push_back(annot(begin, end, name, type)); + children->push_back(parent->make_annotation(name, type, begin, end)); ++parsed; }; }; @@ -442,8 +447,8 @@ Sequence::parse_annot(std::string data, int start_index, int end_index) int end=0; std::string name; std::string type; - std::string seq; - std::list parsed_annots; + std::string seqstr; + SeqSpanRefListRef parsed_annots(new SeqSpanRefList); std::list query_seqs; int parsed=0; @@ -485,13 +490,13 @@ Sequence::parse_annot(std::string data, int start_index, int end_index) ) // to understand how this group gets set // read the comment above struct push_back_annot - )[push_back_annot(parsed_annots, start, end, type, name, parsed)] + )[push_back_annot(this, parsed_annots, start, end, name, type, parsed)] | ((spirit::ch_p('>')|spirit::str_p(">")) >> (*(spirit::print_p))[spirit::assign_a(name)] >> spirit::eol_p >> - (+(spirit::chset<>(Alphabet::nucleic_cstr)))[spirit::assign_a(seq)] - )[push_back_seq(query_seqs, name, seq, parsed)] + (+(spirit::chset<>(Alphabet::nucleic_cstr)))[spirit::assign_a(seqstr)] + )[push_back_seq(query_seqs, name, seqstr, parsed)] ) >> *spirit::space_p ) @@ -502,33 +507,56 @@ Sequence::parse_annot(std::string data, int start_index, int end_index) msg << "Error parsing annotation #" << parsed; throw annotation_load_error(msg.str()); } + // If everything loaded correctly add the sequences to our annotation list // add newly parsed annotations to our sequence - std::copy(parsed_annots.begin(), parsed_annots.end(), std::back_inserter(annots)); - // go seearch for query sequences + std::copy(parsed_annots->begin(), parsed_annots->end(), std::back_inserter(*annotation_list)); + // go search for query sequences find_sequences(query_seqs.begin(), query_seqs.end()); } -void Sequence::add_annotation(const annot& a) +void Sequence::add_annotation(const SeqSpanRef a) { - annots.push_back(a); + annotation_list->push_back(a); } -const std::list& Sequence::annotations() const +void Sequence::add_annotation(std::string name, std::string type, size_type start, size_type stop) { - return annots; + add_annotation(make_annotation(name, type, start, stop)); +} + +SeqSpanRef +Sequence::make_annotation(std::string name, std::string type, size_type start, size_type stop) const +{ + // we want things to be in the positive direction + if (stop < start) { + size_type tmp = start; + start = stop; + stop = tmp; + } + size_type count = stop - start; + SeqSpanRef new_annot(seq->subseq(start, count, SeqSpan::UnknownStrand)); + AnnotationsRef metadata(new Annotations(name)); + metadata->set("type", type); + new_annot->setAnnotations(metadata); + return new_annot; +} + +const SeqSpanRefList& Sequence::annotations() const +{ + return *annotation_list; } void Sequence::copy_children(Sequence &new_seq, size_type start, size_type count) const { new_seq.motif_list = motif_list; - new_seq.annots.clear(); + new_seq.annotation_list.reset(new SeqSpanRefList); - for(std::list::const_iterator annot_i = annots.begin(); - annot_i != annots.end(); + for(SeqSpanRefList::const_iterator annot_i = annotation_list->begin(); + annot_i != annotation_list->end(); ++annot_i) { - size_type annot_begin= annot_i->begin; - size_type annot_end = annot_i->end; + size_type annot_begin= (*annot_i)->start(); + size_type annot_end = (*annot_i)->stop(); if (annot_begin < start+count) { if (annot_begin >= start) { @@ -542,9 +570,9 @@ void Sequence::copy_children(Sequence &new_seq, size_type start, size_type count } else { annot_end = count; } - - annot new_annot(annot_begin, annot_end, annot_i->type, annot_i->name); - new_seq.annots.push_back(new_annot); + SeqSpanRef new_annot(new_seq.seq->subseq(annot_begin, annot_end)); + new_annot->setAnnotations((*annot_i)->annotations()); + new_seq.annotation_list->push_back(new_annot); } } } @@ -558,7 +586,7 @@ Sequence::subseq(size_type start, size_type count, SeqSpan::strand_type strand) return new_seq; } - Sequence new_seq = *this; + Sequence new_seq(*this); new_seq.seq = seq->subseq(start, count, strand); if (seq->annotations()) { AnnotationsRef a(new Annotations(*(seq->annotations()))); @@ -636,15 +664,18 @@ Sequence::clear() seq.reset(); header.clear(); species.clear(); - annots.clear(); - motif_list.clear(); + annotation_list.reset(new SeqSpanRefList); + motif_list.reset(new MotifList); } void Sequence::save(fs::fstream &save_file) { + std::string type("type"); + std::string empty_str(""); //fstream save_file; - std::list::iterator annots_i; + SeqSpanRefList::iterator annots_i; + AnnotationsRef metadata; // not sure why, or if i'm doing something wrong, but can't seem to pass // file pointers down to this method from the mussa control class @@ -657,45 +688,149 @@ Sequence::save(fs::fstream &save_file) save_file << "" << std::endl; save_file << species << std::endl; - for (annots_i = annots.begin(); annots_i != annots.end(); ++annots_i) + for (annots_i = annotation_list->begin(); + annots_i != annotation_list->end(); + ++annots_i) { - save_file << annots_i->begin << " " << annots_i->end << " " ; - save_file << annots_i->name << " " << annots_i->type << std::endl; + metadata = (*annots_i)->annotations(); + save_file << (*annots_i)->parentStart() << " " << (*annots_i)->parentStop() << " " ; + save_file << metadata->name() << " " + << metadata->getdefault(type, empty_str) << std::endl; } save_file << "" << std::endl; //save_file.close(); } -void -Sequence::load_museq(fs::path load_file_path, int seq_num) +//void +//Sequence::load_museq(fs::path load_file_path, int seq_num) +//{ +// fs::fstream load_file; +// std::string file_data_line; +// int seq_counter; +// //annot an_annot; +// int annot_begin; +// int annot_end; +// std::string annot_name; +// std::string annot_type; +// +// std::string::size_type space_split_i; +// std::string annot_value; +// +// annotation_list.reset(new SeqSpanRefList); +// +// load_file.open(load_file_path, std::ios::in); +// +// seq_counter = 0; +// // search for the seq_num-th sequence +// while ( (!load_file.eof()) && (seq_counter < seq_num) ) +// { +// getline(load_file,file_data_line); +// if (file_data_line == "") +// seq_counter++; +// } +// getline(load_file, file_data_line); +// // looks like the sequence is written as a single line +// set_filtered_sequence(file_data_line, reduced_dna_alphabet, 0, file_data_line.size(), SeqSpan::PlusStrand); +// getline(load_file, file_data_line); +// getline(load_file, file_data_line); +// if (file_data_line == "") +// { +// getline(load_file, file_data_line); +// species = file_data_line; +// while ( (!load_file.eof()) && (file_data_line != "") ) +// { +// getline(load_file,file_data_line); +// if ((file_data_line != "") && (file_data_line != "")) +// { +// // need to get 4 values...almost same code 4 times... +// // get annot start index +// space_split_i = file_data_line.find(" "); +// annot_value = file_data_line.substr(0,space_split_i); +// annot_begin = atoi (annot_value.c_str()); +// file_data_line = file_data_line.substr(space_split_i+1); +// // get annot end index +// space_split_i = file_data_line.find(" "); +// annot_value = file_data_line.substr(0,space_split_i); +// annot_end = atoi (annot_value.c_str()); +// +// if (space_split_i == std::string::npos) // no entry for type or name +// { +// std::cout << "seq, annots - no type or name\n"; +// annot_name = ""; +// annot_type = ""; +// } +// else // else get annot type +// { +// file_data_line = file_data_line.substr(space_split_i+1); +// space_split_i = file_data_line.find(" "); +// annot_value = file_data_line.substr(0,space_split_i); +// //an_annot.type = annot_value; +// annot_type = annot_value; +// if (space_split_i == std::string::npos) // no entry for name +// { +// std::cout << "seq, annots - no name\n"; +// annot_name = ""; +// } +// else // get annot name +// { +// file_data_line = file_data_line.substr(space_split_i+1); +// space_split_i = file_data_line.find(" "); +// annot_value = file_data_line.substr(0,space_split_i); +// // this seems like its wrong? +// annot_type = annot_value; +// } +// } +// add_annotation(annot_name, annot_type, annot_begin, annot_end); +// } +// //std::cout << "seq, annots: " << an_annot.start << ", " << an_annot.end +// // << "-->" << an_annot.type << "::" << an_annot.name << std::endl; +// } +// } +// load_file.close(); +//} + +SequenceRef Sequence::load_museq(boost::filesystem::fstream& load_file) { - fs::fstream load_file; + boost::shared_ptr seq(new Sequence); std::string file_data_line; int seq_counter; - annot an_annot; + //annot an_annot; + int annot_begin; + int annot_end; + std::string annot_name; + std::string annot_type; + std::string::size_type space_split_i; std::string annot_value; - annots.clear(); - load_file.open(load_file_path, std::ios::in); + //seq->annotation_list.reset(new SeqSpanRefList); seq_counter = 0; - // search for the seq_num-th sequence + // search for the next sequence + int seq_num = 1; while ( (!load_file.eof()) && (seq_counter < seq_num) ) { getline(load_file,file_data_line); if (file_data_line == "") seq_counter++; } + + // Could not find next sequence + if (load_file.eof()) + { + seq.reset(); + return seq; + } + getline(load_file, file_data_line); // looks like the sequence is written as a single line - set_filtered_sequence(file_data_line, reduced_dna_alphabet, 0, file_data_line.size(), SeqSpan::PlusStrand); + seq->set_filtered_sequence(file_data_line, reduced_dna_alphabet, 0, file_data_line.size(), SeqSpan::PlusStrand); getline(load_file, file_data_line); getline(load_file, file_data_line); if (file_data_line == "") { getline(load_file, file_data_line); - species = file_data_line; + seq->set_species(file_data_line); while ( (!load_file.eof()) && (file_data_line != "") ) { getline(load_file,file_data_line); @@ -705,45 +840,48 @@ Sequence::load_museq(fs::path load_file_path, int seq_num) // get annot start index space_split_i = file_data_line.find(" "); annot_value = file_data_line.substr(0,space_split_i); - an_annot.begin = atoi (annot_value.c_str()); + annot_begin = atoi (annot_value.c_str()); file_data_line = file_data_line.substr(space_split_i+1); // get annot end index space_split_i = file_data_line.find(" "); annot_value = file_data_line.substr(0,space_split_i); - an_annot.end = atoi (annot_value.c_str()); + annot_end = atoi (annot_value.c_str()); if (space_split_i == std::string::npos) // no entry for type or name { std::cout << "seq, annots - no type or name\n"; - an_annot.type = ""; - an_annot.name = ""; + annot_name = ""; + annot_type = ""; } else // else get annot type { file_data_line = file_data_line.substr(space_split_i+1); space_split_i = file_data_line.find(" "); annot_value = file_data_line.substr(0,space_split_i); - an_annot.type = annot_value; + //an_annot.type = annot_value; + annot_type = annot_value; if (space_split_i == std::string::npos) // no entry for name { std::cout << "seq, annots - no name\n"; - an_annot.name = ""; + annot_name = ""; } else // get annot name { file_data_line = file_data_line.substr(space_split_i+1); space_split_i = file_data_line.find(" "); annot_value = file_data_line.substr(0,space_split_i); - an_annot.type = annot_value; + // this seems like its wrong? + annot_type = annot_value; } } - annots.push_back(an_annot); // don't forget to actually add the annot + seq->add_annotation(annot_name, annot_type, annot_begin, annot_end); } //std::cout << "seq, annots: " << an_annot.start << ", " << an_annot.end // << "-->" << an_annot.type << "::" << an_annot.name << std::endl; } } - load_file.close(); + //load_file.close(); + return seq; } @@ -755,18 +893,21 @@ void Sequence::add_motif(const Sequence& a_motif) motif_start_i != motif_starts.end(); ++motif_start_i) { - motif_list.push_back(motif(*motif_start_i, a_motif.get_sequence())); + motif_list->push_back(motif(*motif_start_i, a_motif.get_sequence())); } } void Sequence::clear_motifs() { - motif_list.clear(); + if (motif_list) + motif_list->clear(); + else + motif_list.reset(new MotifList); } -const std::list& Sequence::motifs() const +const Sequence::MotifList& Sequence::motifs() const { - return motif_list; + return *motif_list; } std::vector @@ -849,7 +990,6 @@ Sequence::motif_scan(const Sequence& a_motif, std::vector * motif_match_sta // end Nora stuff, now we see if a match is found this pass if (motif_i == motif_len) { - annot new_motif; motif_match_starts->push_back(seq_i - motif_len + 1); motif_i = 0; } @@ -864,16 +1004,11 @@ void Sequence::add_string_annotation(std::string a_seq, { std::vector seq_starts = find_motif(a_seq); - //std::cout << "searching for " << a_seq << " found " << seq_starts.size() << std::endl; - for(std::vector::iterator seq_start_i = seq_starts.begin(); seq_start_i != seq_starts.end(); ++seq_start_i) { - annots.push_back(annot(*seq_start_i, - *seq_start_i+a_seq.size(), - "", - name)); + add_annotation(name, "", *seq_start_i, *seq_start_i+a_seq.size()); } }