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 // This file is part of the Mussa source distribution.
12 // http://mussa.caltech.edu/
13 // Contact author: Tristan De Buysscher, tristan@caltech.edu
15 // This program and all associated source code files are Copyright (C) 2005
16 // the California Institute of Technology, Pasadena, CA, 91125 USA. It is
17 // under the GNU Public License; please see the included LICENSE.txt
18 // file for more information, or contact Tristan directly.
21 // ----------------------------------------
22 // ---------- sequence.cc -----------
23 // ----------------------------------------
24 #include <boost/filesystem/fstream.hpp>
25 #include <boost/filesystem/operations.hpp>
26 namespace fs = boost::filesystem;
28 #include <boost/spirit/core.hpp>
29 #include <boost/spirit/actor/push_back_actor.hpp>
30 #include <boost/spirit/iterator/file_iterator.hpp>
31 #include <boost/spirit/utility/chset.hpp>
32 namespace spirit = boost::spirit;
34 #include "alg/sequence.hpp"
36 #include "mussa_exceptions.hpp"
44 bool operator==(const motif& left, const motif& right)
46 return ((left.begin== right.begin) and
47 (left.end == right.end) and
48 (left.type == right.type) and
49 (left.name == right.name));
61 motif::motif(int begin_, std::string motif)
63 end(begin_+motif.size()),
74 Sequence::Sequence(AlphabetRef alphabet)
75 : seq(new SeqSpan("", alphabet, SeqSpan::PlusStrand)),
76 annotation_list(new SeqSpanRefList),
77 motif_list(new MotifList)
85 Sequence::Sequence(const char *seq, AlphabetRef alphabet_, SeqSpan::strand_type strand_)
88 annotation_list(new SeqSpanRefList),
89 motif_list(new MotifList)
91 set_filtered_sequence(seq, alphabet_, 0, npos, strand_);
94 Sequence::Sequence(const std::string& seq,
95 AlphabetRef alphabet_,
96 SeqSpan::strand_type strand_)
99 annotation_list(new SeqSpanRefList),
100 motif_list(new MotifList)
102 set_filtered_sequence(seq, alphabet_, 0, seq.size(), strand_);
105 Sequence::Sequence(const Sequence& o)
109 annotation_list(o.annotation_list),
110 motif_list(o.motif_list)
114 Sequence::Sequence(const Sequence* o)
118 annotation_list(o->annotation_list),
119 motif_list(o->motif_list)
123 Sequence::Sequence(const SequenceRef o)
124 : seq(new SeqSpan(o->seq)),
127 annotation_list(new SeqSpanRefList),
128 motif_list(o->motif_list)
130 // copy over the annotations in the other sequence ref,
131 // attaching them to our current sequence ref
132 for(SeqSpanRefList::const_iterator annot_i = o->annotation_list->begin();
133 annot_i != o->annotation_list->end();
136 size_type annot_begin= (*annot_i)->start();
137 size_type annot_count = (*annot_i)->size();
139 SeqSpanRef new_annot(seq->subseq(annot_begin, annot_count));
140 new_annot->setAnnotations((*annot_i)->annotations());
141 annotation_list->push_back(new_annot);
145 Sequence::Sequence(const SeqSpanRef& seq_ref)
149 annotation_list(new SeqSpanRefList),
150 motif_list(new MotifList)
154 Sequence &Sequence::operator=(const Sequence& s)
160 annotation_list = s.annotation_list;
161 motif_list = s.motif_list;
166 void Sequence::load_fasta(fs::path file_path, int seq_num, int start_index, int end_index)
168 load_fasta(file_path, reduced_nucleic_alphabet, seq_num, start_index, end_index);
171 //! load a fasta file into a sequence
172 void Sequence::load_fasta(fs::path file_path, AlphabetRef a,
173 int seq_num, int start_index, int end_index)
175 fs::fstream data_file;
176 data_file.open(file_path, std::ios::in);
178 if (!data_file.good())
180 throw mussa_load_error("Sequence File: "+file_path.string()+" not found");
183 load_fasta(data_file, a, seq_num, start_index, end_index);
184 } catch(sequence_empty_error e) {
185 // there doesn't appear to be any sequence
186 // catch and rethrow to include the filename
187 std::stringstream msg;
188 msg << "The selected sequence in "
189 << file_path.native_file_string()
190 << " appears to be empty";
191 throw sequence_empty_error(msg.str());
192 } catch(sequence_empty_file_error e) {
193 std::stringstream errormsg;
194 errormsg << file_path.native_file_string()
195 << " did not have any fasta sequences" << std::endl;
196 throw sequence_empty_file_error(errormsg.str());
197 } catch(sequence_invalid_load_error e) {
198 std::ostringstream msg;
199 msg << file_path.native_file_string();
200 msg << " " << e.what();
201 throw sequence_invalid_load_error(msg.str());
206 void Sequence::load_fasta(std::istream& file,
207 int seq_num, int start_index, int end_index)
209 load_fasta(file, reduced_nucleic_alphabet, seq_num, start_index, end_index);
213 Sequence::load_fasta(std::istream& data_file, AlphabetRef a,
215 int start_index, int end_index)
217 std::string file_data_line;
218 int header_counter = 0;
219 size_t line_counter = 0;
220 bool read_seq = true;
221 std::string rev_comp;
222 std::string sequence_raw;
223 std::string seq_tmp; // holds sequence during basic filtering
224 const Alphabet &alpha = Alphabet::get_alphabet(a);
227 throw mussa_load_error("fasta sequence number is 1 based (can't be 0)");
230 // search for the header of the fasta sequence we want
231 while ( (!data_file.eof()) && (header_counter < seq_num) )
233 multiplatform_getline(data_file, file_data_line);
235 if (file_data_line.substr(0,1) == ">")
239 if (header_counter > 0) {
240 header = file_data_line.substr(1);
244 while ( !data_file.eof() && read_seq ) {
245 multiplatform_getline(data_file,file_data_line);
247 if (file_data_line.substr(0,1) == ">")
250 for (std::string::const_iterator line_i = file_data_line.begin();
251 line_i != file_data_line.end();
254 if(alpha.exists(*line_i)) {
255 sequence_raw += *line_i;
257 std::ostringstream msg;
258 msg << "Unrecognized characters in fasta sequence at line ";
260 throw sequence_invalid_load_error(msg.str());
266 // Lastly, if subselection of the sequence was specified we keep cut out
267 // and only keep that part
268 // end_index = 0 means no end was specified, so cut to the end
270 end_index = sequence_raw.size();
272 // sequence filtering for upcasing agctn and convert non AGCTN to N
273 if (end_index-start_index <= 0) {
274 std::string msg("The selected sequence appears to be empty");
275 throw sequence_empty_error(msg);
277 set_filtered_sequence(sequence_raw, a, start_index, end_index-start_index, SeqSpan::PlusStrand);
279 std::string errormsg("There were no fasta sequences");
280 throw sequence_empty_file_error(errormsg);
284 void Sequence::set_filtered_sequence(const std::string &in_seq,
285 AlphabetRef alphabet_,
288 SeqSpan::strand_type strand_)
291 count = in_seq.size() - start;
293 new_seq.reserve(count);
295 // finally, the actual conversion loop
296 const Alphabet& alpha_impl = Alphabet::get_alphabet(alphabet_); // go get one of our actual alphabets
297 std::string::const_iterator seq_i = in_seq.begin()+start;
298 for(size_type i = 0; i != count; ++i, ++seq_i)
300 if (alpha_impl.exists(*seq_i)) {
301 new_seq.append(1, toupper(*seq_i));
303 new_seq.append(1, 'N');
306 SeqSpanRef new_seq_ref(new SeqSpan(new_seq, alphabet_, strand_));
311 Sequence::load_annot(fs::path file_path, int start_index, int end_index)
313 if (not fs::exists(file_path)) {
314 throw mussa_load_error("Annotation File " + file_path.string() + " was not found");
316 if (fs::is_directory(file_path)) {
317 throw mussa_load_error(file_path.string() +
318 " is a directory, please provide a file for annotations."
321 fs::fstream data_stream(file_path, std::ios::in);
324 throw mussa_load_error("Error loading annotation file " + file_path.string());
328 load_annot(data_stream, start_index, end_index);
329 } catch(annotation_load_error e) {
330 std::ostringstream msg;
331 msg << file_path.native_file_string()
334 throw annotation_load_error(msg.str());
340 Sequence::load_annot(std::istream& data_stream, int start_index, int end_index)
342 // so i should probably be passing the parse function some iterators
343 // but the annotations files are (currently) small, so i think i can
344 // get away with loading the whole file into memory
347 while(data_stream.good()) {
352 parse_annot(data, start_index, end_index);
355 /* If this works, yikes, this is some brain hurting code.
357 * what's going on is that when pb_annot is instantiated it stores references
358 * to begin, end, name, type, declared in the parse function, then
359 * when operator() is called it grabs values from those references
360 * and uses that to instantiate an annot object and append that to our
363 * This weirdness is because the spirit library requires that actions
364 * conform to a specific prototype operator()(IteratorT, IteratorT)
365 * which doesn't provide any useful opportunity for me to actually
366 * grab the results of our parsing.
368 * so I instantiate this structure in order to have a place to grab
372 struct push_back_annot {
374 SeqSpanRefListRef children;
381 push_back_annot(Sequence* parent_seq,
382 SeqSpanRefListRef children_list,
388 : parent(parent_seq),
389 children(children_list),
398 void operator()(std::string::const_iterator,
399 std::string::const_iterator) const
401 children->push_back(parent->make_annotation(name, type, begin, end));
406 struct push_back_seq {
407 std::list<Sequence>& seq_list;
412 push_back_seq(std::list<Sequence>& seq_list_,
416 : seq_list(seq_list_),
423 void operator()(std::string::const_iterator,
424 std::string::const_iterator) const
426 std::string::iterator seq_i = seq.begin();
427 std::string::iterator seq_end = seq.end();
429 // this if block is a hack, for some reason spirit was
430 // duplicating the last character if the file didn't end
432 // this checks for the trailing newline, and if it is missing
433 // removes the last character ( which should be the duplicated character.
434 // check test_sequence.cpp:sequence_no_trailing_newline for test case
435 // also see ticket:265 for more information
436 if (seq.size() > 0) {
437 std::string::value_type c = seq[seq.size()-1];
438 if (not (c == '\015' or c == '\012')) {
439 // doesn't end with a new line character
445 // filter out newlines from our sequence
447 for(; seq_i != seq_end; ++seq_i)
449 if (*seq_i != '\015' && *seq_i != '\012') new_seq += *seq_i;
451 //std::cout << "adding seq: " << name << " " << new_seq << std::endl;
453 s.set_fasta_header(name);
454 seq_list.push_back(s);
460 Sequence::parse_annot(std::string data, int start_index, int end_index)
467 SeqSpanRefListRef parsed_annots(new SeqSpanRefList);
468 std::list<Sequence> query_seqs;
471 bool ok = spirit::parse(data.begin(), data.end(),
478 )[spirit::assign_a(species)] >>
482 ( // ignore html tags
483 *(spirit::space_p) >>
485 +(~spirit::ch_p('>')) >>
490 ( // parse an absolute location name
491 (spirit::uint_p[spirit::assign_a(start)] >>
493 spirit::uint_p[spirit::assign_a(end)] >>
498 )[spirit::assign_a(name)] >>
505 )[spirit::assign_a(type)]
507 // to understand how this group gets set
508 // read the comment above struct push_back_annot
509 )[push_back_annot(this, parsed_annots, start, end, name, type, parsed)]
511 ((spirit::ch_p('>')|spirit::str_p(">")) >>
512 (*(spirit::print_p))[spirit::assign_a(name)] >>
514 (+(spirit::chset<>(Alphabet::nucleic_cstr)))[spirit::assign_a(seqstr)]
515 )[push_back_seq(query_seqs, name, seqstr, parsed)]
522 std::stringstream msg;
523 msg << "Error parsing annotation #" << parsed;
524 throw annotation_load_error(msg.str());
526 // If everything loaded correctly add the sequences to our annotation list
527 // add newly parsed annotations to our sequence
528 std::copy(parsed_annots->begin(), parsed_annots->end(), std::back_inserter(*annotation_list));
529 // go search for query sequences
530 find_sequences(query_seqs.begin(), query_seqs.end());
533 void Sequence::add_annotation(const SeqSpanRef a)
535 annotation_list->push_back(a);
538 void Sequence::add_annotation(std::string name, std::string type, size_type start, size_type stop)
540 add_annotation(make_annotation(name, type, start, stop));
544 Sequence::make_annotation(std::string name, std::string type, size_type start, size_type stop) const
546 // we want things to be in the positive direction
548 size_type tmp = start;
552 size_type count = stop - start;
553 SeqSpanRef new_annot(seq->subseq(start, count, SeqSpan::UnknownStrand));
554 AnnotationsRef metadata(new Annotations(name));
555 metadata->set("type", type);
556 new_annot->setAnnotations(metadata);
560 const SeqSpanRefList& Sequence::annotations() const
562 return *annotation_list;
565 void Sequence::copy_children(Sequence &new_seq, size_type start, size_type count) const
567 new_seq.motif_list = motif_list;
568 new_seq.annotation_list.reset(new SeqSpanRefList);
570 for(SeqSpanRefList::const_iterator annot_i = annotation_list->begin();
571 annot_i != annotation_list->end();
574 size_type annot_begin= (*annot_i)->start();
575 size_type annot_end = (*annot_i)->stop();
577 if (annot_begin < start+count) {
578 if (annot_begin >= start) {
579 annot_begin -= start;
584 if (annot_end < start+count) {
589 SeqSpanRef new_annot(new_seq.seq->subseq(annot_begin, annot_end));
590 new_annot->setAnnotations((*annot_i)->annotations());
591 new_seq.annotation_list->push_back(new_annot);
597 Sequence::subseq(size_type start, size_type count, SeqSpan::strand_type strand) const
599 // FIXME: should i really allow a subsequence of an empty sequence?
605 Sequence new_seq(*this);
606 new_seq.seq = seq->subseq(start, count, strand);
607 if (seq->annotations()) {
608 AnnotationsRef a(new Annotations(*(seq->annotations())));
609 new_seq.seq->setAnnotations(a);
611 copy_children(new_seq, start, count);
617 // FIXME: This needs to be moved into SeqSpan
618 Sequence Sequence::rev_comp() const
620 // a reverse complement is the whole opposite strand
621 return subseq(0, npos, SeqSpan::OppositeStrand);
624 const Alphabet& Sequence::get_alphabet() const
626 return (seq) ? seq->get_alphabet() : Alphabet::empty_alphabet();
629 void Sequence::set_fasta_header(std::string header_)
634 void Sequence::set_species(const std::string& name)
639 std::string Sequence::get_species() const
646 Sequence::get_fasta_header() const
652 Sequence::get_name() const
654 if (header.size() > 0)
656 else if (species.size() > 0)
662 void Sequence::set_sequence(const std::string& s, AlphabetRef a)
664 set_filtered_sequence(s, a, 0, s.size(), SeqSpan::PlusStrand);
667 std::string Sequence::get_sequence() const
669 return seq->sequence();
672 Sequence::const_reference Sequence::operator[](Sequence::size_type i) const
683 annotation_list.reset(new SeqSpanRefList);
684 motif_list.reset(new MotifList);
688 Sequence::save(fs::fstream &save_file)
690 std::string type("type");
691 std::string empty_str("");
693 SeqSpanRefList::iterator annots_i;
694 AnnotationsRef metadata;
696 save_file << "<Sequence>" << std::endl;
697 save_file << *this << std::endl;
698 save_file << "</Sequence>" << std::endl;
700 save_file << "<Annotations>" << std::endl;
701 save_file << species << std::endl;
702 for (annots_i = annotation_list->begin();
703 annots_i != annotation_list->end();
706 metadata = (*annots_i)->annotations();
707 save_file << (*annots_i)->parentStart() << " " << (*annots_i)->parentStop() << " " ;
708 save_file << metadata->name() << " "
709 << metadata->getdefault(type, empty_str) << std::endl;
711 save_file << "</Annotations>" << std::endl;
716 //Sequence::load_museq(fs::path load_file_path, int seq_num)
718 // fs::fstream load_file;
719 // std::string file_data_line;
724 // std::string annot_name;
725 // std::string annot_type;
727 // std::string::size_type space_split_i;
728 // std::string annot_value;
730 // annotation_list.reset(new SeqSpanRefList);
732 // load_file.open(load_file_path, std::ios::in);
735 // // search for the seq_num-th sequence
736 // while ( (!load_file.eof()) && (seq_counter < seq_num) )
738 // getline(load_file,file_data_line);
739 // if (file_data_line == "<Sequence>")
742 // getline(load_file, file_data_line);
743 // // looks like the sequence is written as a single line
744 // set_filtered_sequence(file_data_line, reduced_dna_alphabet, 0, file_data_line.size(), SeqSpan::PlusStrand);
745 // getline(load_file, file_data_line);
746 // getline(load_file, file_data_line);
747 // if (file_data_line == "<Annotations>")
749 // getline(load_file, file_data_line);
750 // species = file_data_line;
751 // while ( (!load_file.eof()) && (file_data_line != "</Annotations>") )
753 // getline(load_file,file_data_line);
754 // if ((file_data_line != "") && (file_data_line != "</Annotations>"))
756 // // need to get 4 values...almost same code 4 times...
757 // // get annot start index
758 // space_split_i = file_data_line.find(" ");
759 // annot_value = file_data_line.substr(0,space_split_i);
760 // annot_begin = atoi (annot_value.c_str());
761 // file_data_line = file_data_line.substr(space_split_i+1);
762 // // get annot end index
763 // space_split_i = file_data_line.find(" ");
764 // annot_value = file_data_line.substr(0,space_split_i);
765 // annot_end = atoi (annot_value.c_str());
767 // if (space_split_i == std::string::npos) // no entry for type or name
769 // std::cout << "seq, annots - no type or name\n";
773 // else // else get annot type
775 // file_data_line = file_data_line.substr(space_split_i+1);
776 // space_split_i = file_data_line.find(" ");
777 // annot_value = file_data_line.substr(0,space_split_i);
778 // //an_annot.type = annot_value;
779 // annot_type = annot_value;
780 // if (space_split_i == std::string::npos) // no entry for name
782 // std::cout << "seq, annots - no name\n";
785 // else // get annot name
787 // file_data_line = file_data_line.substr(space_split_i+1);
788 // space_split_i = file_data_line.find(" ");
789 // annot_value = file_data_line.substr(0,space_split_i);
790 // // this seems like its wrong?
791 // annot_type = annot_value;
794 // add_annotation(annot_name, annot_type, annot_begin, annot_end);
796 // //std::cout << "seq, annots: " << an_annot.start << ", " << an_annot.end
797 // // << "-->" << an_annot.type << "::" << an_annot.name << std::endl;
800 // load_file.close();
803 SequenceRef Sequence::load_museq(boost::filesystem::fstream& load_file)
805 boost::shared_ptr<Sequence> seq(new Sequence);
806 std::string file_data_line;
811 std::string annot_name;
812 std::string annot_type;
814 std::string::size_type space_split_i;
815 std::string annot_value;
817 //seq->annotation_list.reset(new SeqSpanRefList);
820 // search for the next sequence
822 while ( (!load_file.eof()) && (seq_counter < seq_num) )
824 getline(load_file,file_data_line);
825 if (file_data_line == "<Sequence>")
829 // Could not find next sequence
836 getline(load_file, file_data_line);
837 // looks like the sequence is written as a single line
838 seq->set_filtered_sequence(file_data_line, reduced_dna_alphabet, 0, file_data_line.size(), SeqSpan::PlusStrand);
839 getline(load_file, file_data_line);
840 getline(load_file, file_data_line);
841 if (file_data_line == "<Annotations>")
843 getline(load_file, file_data_line);
844 seq->set_species(file_data_line);
845 while ( (!load_file.eof()) && (file_data_line != "</Annotations>") )
847 getline(load_file,file_data_line);
848 if ((file_data_line != "") && (file_data_line != "</Annotations>"))
850 // need to get 4 values...almost same code 4 times...
851 // get annot start index
852 space_split_i = file_data_line.find(" ");
853 annot_value = file_data_line.substr(0,space_split_i);
854 annot_begin = atoi (annot_value.c_str());
855 file_data_line = file_data_line.substr(space_split_i+1);
856 // get annot end index
857 space_split_i = file_data_line.find(" ");
858 annot_value = file_data_line.substr(0,space_split_i);
859 annot_end = atoi (annot_value.c_str());
861 if (space_split_i == std::string::npos) // no entry for type or name
863 std::cout << "seq, annots - no type or name\n";
867 else // else get annot type
869 file_data_line = file_data_line.substr(space_split_i+1);
870 space_split_i = file_data_line.find(" ");
871 annot_value = file_data_line.substr(0,space_split_i);
872 //an_annot.type = annot_value;
873 annot_type = annot_value;
874 if (space_split_i == std::string::npos) // no entry for name
876 std::cout << "seq, annots - no name\n";
879 else // get annot name
881 file_data_line = file_data_line.substr(space_split_i+1);
882 space_split_i = file_data_line.find(" ");
883 annot_value = file_data_line.substr(0,space_split_i);
884 // this seems like its wrong?
885 annot_type = annot_value;
888 seq->add_annotation(annot_name, annot_type, annot_begin, annot_end);
890 //std::cout << "seq, annots: " << an_annot.start << ", " << an_annot.end
891 // << "-->" << an_annot.type << "::" << an_annot.name << std::endl;
899 void Sequence::add_motif(const Sequence& a_motif)
901 std::vector<int> motif_starts = find_motif(a_motif);
903 for(std::vector<int>::iterator motif_start_i = motif_starts.begin();
904 motif_start_i != motif_starts.end();
907 motif_list->push_back(motif(*motif_start_i, a_motif.get_sequence()));
911 void Sequence::clear_motifs()
916 motif_list.reset(new MotifList);
919 const Sequence::MotifList& Sequence::motifs() const
925 Sequence::find_motif(const Sequence& a_motif) const
927 std::vector<int> motif_match_starts;
928 Sequence norm_motif_rc;
930 motif_match_starts.clear();
931 // std::cout << "motif is: " << norm_motif << std::endl;
933 if (a_motif.size() > 0)
935 //std::cout << "Sequence: none blank motif\n";
936 motif_scan(a_motif, &motif_match_starts);
938 norm_motif_rc = a_motif.rev_comp();;
939 // make sure not to do search again if it is a palindrome
940 if (norm_motif_rc != a_motif) {
941 motif_scan(norm_motif_rc, &motif_match_starts);
944 return motif_match_starts;
948 Sequence::motif_scan(const Sequence& a_motif, std::vector<int> * motif_match_starts) const
950 // if there's no sequence we can't scan for it?
951 // should this throw an exception?
954 std::string::size_type seq_i = 0;
955 Sequence::size_type motif_i = 0;
956 Sequence::size_type motif_len = a_motif.length();
957 Sequence::value_type motif_char;
958 Sequence::value_type seq_char;
960 while (seq_i < size())
962 // this is pretty much a straight translation of Nora's python code
963 // to match iupac letter codes
964 motif_char = toupper(a_motif[motif_i]);
965 seq_char = toupper(seq->at(seq_i));
966 if (motif_char =='N')
968 else if (motif_char == seq_char)
970 else if ((motif_char =='M') && ((seq_char=='A') || (seq_char=='C')))
972 else if ((motif_char =='R') && ((seq_char=='A') || (seq_char=='G')))
974 else if ((motif_char =='W') && ((seq_char=='A') || (seq_char=='T')))
976 else if ((motif_char =='S') && ((seq_char=='C') || (seq_char=='G')))
978 else if ((motif_char =='Y') && ((seq_char=='C') || (seq_char=='T')))
980 else if ((motif_char =='K') && ((seq_char=='G') || (seq_char=='T')))
982 else if ((motif_char =='V') &&
983 ((seq_char=='A') || (seq_char=='C') || (seq_char=='G')))
985 else if ((motif_char =='H') &&
986 ((seq_char=='A') || (seq_char=='C') || (seq_char=='T')))
988 else if ((motif_char =='D') &&
989 ((seq_char=='A') || (seq_char=='G') || (seq_char=='T')))
991 else if ((motif_char =='B') &&
992 ((seq_char=='C') || (seq_char=='G') || (seq_char=='T')))
996 // if a motif doesn't match, erase our current trial and try again
1001 // end Nora stuff, now we see if a match is found this pass
1002 if (motif_i == motif_len)
1004 motif_match_starts->push_back(seq_i - motif_len + 1);
1010 //std::cout << std::endl;
1013 void Sequence::add_string_annotation(std::string a_seq,
1016 std::vector<int> seq_starts = find_motif(a_seq);
1018 for(std::vector<int>::iterator seq_start_i = seq_starts.begin();
1019 seq_start_i != seq_starts.end();
1022 add_annotation(name, "", *seq_start_i, *seq_start_i+a_seq.size());
1026 void Sequence::find_sequences(std::list<Sequence>::iterator start,
1027 std::list<Sequence>::iterator end)
1029 while (start != end) {
1030 add_string_annotation(start->get_sequence(), start->get_fasta_header());
1036 std::ostream& operator<<(std::ostream& out, const Sequence& s)
1039 for(Sequence::const_iterator s_i = s.begin(); s_i != s.end(); ++s_i) {
1046 bool operator<(const Sequence& x, const Sequence& y)
1048 Sequence::const_iterator x_i = x.begin();
1049 Sequence::const_iterator y_i = y.begin();
1050 // for sequences there's some computation associated with computing .end
1051 // so lets cache it.
1052 Sequence::const_iterator xend = x.end();
1053 Sequence::const_iterator yend = y.end();
1055 if( x_i == xend and y_i == yend ) {
1057 } else if ( x_i == xend ) {
1059 } else if ( y_i == yend ) {
1061 } else if ( (*x_i) < (*y_i)) {
1063 } else if ( (*x_i) > (*y_i) ) {
1072 template <typename Iter1, typename Iter2>
1074 bool sequence_insensitive_equality(Iter1 abegin, Iter1 aend, Iter2 bbegin, Iter2 bend)
1076 Iter1 aseq_i = abegin;
1077 Iter2 bseq_i = bbegin;
1078 if (aend-abegin == bend-bbegin) {
1079 // since the length of the two sequences is equal, we only need to
1081 for(; aseq_i != aend; ++aseq_i, ++bseq_i) {
1082 if (toupper(*aseq_i) != toupper(*bseq_i)) {
1092 bool operator==(const Sequence& x, const Sequence& y)
1094 if (x.seq and y.seq) {
1095 // both x and y are defined
1096 if (SeqSpan::isFamily(x.seq, y.seq)) {
1097 // both are part of the same SeqSpan tree
1098 return *(x.seq) == *(y.seq);
1100 // we'll have to do a real comparison
1101 return sequence_insensitive_equality<SeqSpan::const_iterator, SeqSpan::const_iterator>(
1107 // true if they're both empty (with either a null SeqSpanRef or
1108 // a zero length string
1109 return (x.size() == y.size());
1113 bool operator!=(const Sequence& x, const Sequence& y)
1115 return not operator==(x, y);