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 // filter out newlines from our sequence
428 for(std::string::const_iterator seq_i = seq.begin();
432 if (*seq_i != '\015' && *seq_i != '\012') new_seq += *seq_i;
434 //std::cout << "adding seq: " << name << " " << new_seq << std::endl;
437 s.set_fasta_header(name);
438 seq_list.push_back(s);
444 Sequence::parse_annot(std::string data, int start_index, int end_index)
451 SeqSpanRefListRef parsed_annots(new SeqSpanRefList);
452 std::list<Sequence> query_seqs;
455 bool ok = spirit::parse(data.begin(), data.end(),
462 )[spirit::assign_a(species)] >>
466 ( // ignore html tags
467 *(spirit::space_p) >>
469 +(~spirit::ch_p('>')) >>
474 ( // parse an absolute location name
475 (spirit::uint_p[spirit::assign_a(start)] >>
477 spirit::uint_p[spirit::assign_a(end)] >>
482 )[spirit::assign_a(name)] >>
489 )[spirit::assign_a(type)]
491 // to understand how this group gets set
492 // read the comment above struct push_back_annot
493 )[push_back_annot(this, parsed_annots, start, end, name, type, parsed)]
495 ((spirit::ch_p('>')|spirit::str_p(">")) >>
496 (*(spirit::print_p))[spirit::assign_a(name)] >>
498 (+(spirit::chset<>(Alphabet::nucleic_cstr)))[spirit::assign_a(seqstr)]
499 )[push_back_seq(query_seqs, name, seqstr, parsed)]
506 std::stringstream msg;
507 msg << "Error parsing annotation #" << parsed;
508 throw annotation_load_error(msg.str());
510 // If everything loaded correctly add the sequences to our annotation list
511 // add newly parsed annotations to our sequence
512 std::copy(parsed_annots->begin(), parsed_annots->end(), std::back_inserter(*annotation_list));
513 // go search for query sequences
514 find_sequences(query_seqs.begin(), query_seqs.end());
517 void Sequence::add_annotation(const SeqSpanRef a)
519 annotation_list->push_back(a);
522 void Sequence::add_annotation(std::string name, std::string type, size_type start, size_type stop)
524 add_annotation(make_annotation(name, type, start, stop));
528 Sequence::make_annotation(std::string name, std::string type, size_type start, size_type stop) const
530 // we want things to be in the positive direction
532 size_type tmp = start;
536 size_type count = stop - start;
537 SeqSpanRef new_annot(seq->subseq(start, count, SeqSpan::UnknownStrand));
538 AnnotationsRef metadata(new Annotations(name));
539 metadata->set("type", type);
540 new_annot->setAnnotations(metadata);
544 const SeqSpanRefList& Sequence::annotations() const
546 return *annotation_list;
549 void Sequence::copy_children(Sequence &new_seq, size_type start, size_type count) const
551 new_seq.motif_list = motif_list;
552 new_seq.annotation_list.reset(new SeqSpanRefList);
554 for(SeqSpanRefList::const_iterator annot_i = annotation_list->begin();
555 annot_i != annotation_list->end();
558 size_type annot_begin= (*annot_i)->start();
559 size_type annot_end = (*annot_i)->stop();
561 if (annot_begin < start+count) {
562 if (annot_begin >= start) {
563 annot_begin -= start;
568 if (annot_end < start+count) {
573 SeqSpanRef new_annot(new_seq.seq->subseq(annot_begin, annot_end));
574 new_annot->setAnnotations((*annot_i)->annotations());
575 new_seq.annotation_list->push_back(new_annot);
581 Sequence::subseq(size_type start, size_type count, SeqSpan::strand_type strand) const
583 // FIXME: should i really allow a subsequence of an empty sequence?
589 Sequence new_seq(*this);
590 new_seq.seq = seq->subseq(start, count, strand);
591 if (seq->annotations()) {
592 AnnotationsRef a(new Annotations(*(seq->annotations())));
593 new_seq.seq->setAnnotations(a);
595 copy_children(new_seq, start, count);
601 // FIXME: This needs to be moved into SeqSpan
602 Sequence Sequence::rev_comp() const
604 // a reverse complement is the whole opposite strand
605 return subseq(0, npos, SeqSpan::OppositeStrand);
608 const Alphabet& Sequence::get_alphabet() const
610 return (seq) ? seq->get_alphabet() : Alphabet::empty_alphabet();
613 void Sequence::set_fasta_header(std::string header_)
618 void Sequence::set_species(const std::string& name)
623 std::string Sequence::get_species() const
630 Sequence::get_fasta_header() const
636 Sequence::get_name() const
638 if (header.size() > 0)
640 else if (species.size() > 0)
646 void Sequence::set_sequence(const std::string& s, AlphabetRef a)
648 set_filtered_sequence(s, a, 0, s.size(), SeqSpan::PlusStrand);
651 std::string Sequence::get_sequence() const
653 return seq->sequence();
656 Sequence::const_reference Sequence::operator[](Sequence::size_type i) const
667 annotation_list.reset(new SeqSpanRefList);
668 motif_list.reset(new MotifList);
672 Sequence::save(fs::fstream &save_file)
674 std::string type("type");
675 std::string empty_str("");
677 SeqSpanRefList::iterator annots_i;
678 AnnotationsRef metadata;
680 // not sure why, or if i'm doing something wrong, but can't seem to pass
681 // file pointers down to this method from the mussa control class
682 // so each call to save a sequence appends to the file started by mussa_class
683 //save_file.open(save_file_path.c_str(), std::ios::app);
685 save_file << "<Sequence>" << std::endl;
686 save_file << *this << std::endl;
687 save_file << "</Sequence>" << std::endl;
689 save_file << "<Annotations>" << std::endl;
690 save_file << species << std::endl;
691 for (annots_i = annotation_list->begin();
692 annots_i != annotation_list->end();
695 metadata = (*annots_i)->annotations();
696 save_file << (*annots_i)->parentStart() << " " << (*annots_i)->parentStop() << " " ;
697 save_file << metadata->name() << " "
698 << metadata->getdefault(type, empty_str) << std::endl;
700 save_file << "</Annotations>" << std::endl;
705 //Sequence::load_museq(fs::path load_file_path, int seq_num)
707 // fs::fstream load_file;
708 // std::string file_data_line;
713 // std::string annot_name;
714 // std::string annot_type;
716 // std::string::size_type space_split_i;
717 // std::string annot_value;
719 // annotation_list.reset(new SeqSpanRefList);
721 // load_file.open(load_file_path, std::ios::in);
724 // // search for the seq_num-th sequence
725 // while ( (!load_file.eof()) && (seq_counter < seq_num) )
727 // getline(load_file,file_data_line);
728 // if (file_data_line == "<Sequence>")
731 // getline(load_file, file_data_line);
732 // // looks like the sequence is written as a single line
733 // set_filtered_sequence(file_data_line, reduced_dna_alphabet, 0, file_data_line.size(), SeqSpan::PlusStrand);
734 // getline(load_file, file_data_line);
735 // getline(load_file, file_data_line);
736 // if (file_data_line == "<Annotations>")
738 // getline(load_file, file_data_line);
739 // species = file_data_line;
740 // while ( (!load_file.eof()) && (file_data_line != "</Annotations>") )
742 // getline(load_file,file_data_line);
743 // if ((file_data_line != "") && (file_data_line != "</Annotations>"))
745 // // need to get 4 values...almost same code 4 times...
746 // // get annot start index
747 // space_split_i = file_data_line.find(" ");
748 // annot_value = file_data_line.substr(0,space_split_i);
749 // annot_begin = atoi (annot_value.c_str());
750 // file_data_line = file_data_line.substr(space_split_i+1);
751 // // get annot end index
752 // space_split_i = file_data_line.find(" ");
753 // annot_value = file_data_line.substr(0,space_split_i);
754 // annot_end = atoi (annot_value.c_str());
756 // if (space_split_i == std::string::npos) // no entry for type or name
758 // std::cout << "seq, annots - no type or name\n";
762 // else // else get annot type
764 // file_data_line = file_data_line.substr(space_split_i+1);
765 // space_split_i = file_data_line.find(" ");
766 // annot_value = file_data_line.substr(0,space_split_i);
767 // //an_annot.type = annot_value;
768 // annot_type = annot_value;
769 // if (space_split_i == std::string::npos) // no entry for name
771 // std::cout << "seq, annots - no name\n";
774 // else // get annot name
776 // file_data_line = file_data_line.substr(space_split_i+1);
777 // space_split_i = file_data_line.find(" ");
778 // annot_value = file_data_line.substr(0,space_split_i);
779 // // this seems like its wrong?
780 // annot_type = annot_value;
783 // add_annotation(annot_name, annot_type, annot_begin, annot_end);
785 // //std::cout << "seq, annots: " << an_annot.start << ", " << an_annot.end
786 // // << "-->" << an_annot.type << "::" << an_annot.name << std::endl;
789 // load_file.close();
792 SequenceRef Sequence::load_museq(boost::filesystem::fstream& load_file)
794 boost::shared_ptr<Sequence> seq(new Sequence);
795 std::string file_data_line;
800 std::string annot_name;
801 std::string annot_type;
803 std::string::size_type space_split_i;
804 std::string annot_value;
806 //seq->annotation_list.reset(new SeqSpanRefList);
809 // search for the next sequence
811 while ( (!load_file.eof()) && (seq_counter < seq_num) )
813 getline(load_file,file_data_line);
814 if (file_data_line == "<Sequence>")
818 // Could not find next sequence
825 getline(load_file, file_data_line);
826 // looks like the sequence is written as a single line
827 seq->set_filtered_sequence(file_data_line, reduced_dna_alphabet, 0, file_data_line.size(), SeqSpan::PlusStrand);
828 getline(load_file, file_data_line);
829 getline(load_file, file_data_line);
830 if (file_data_line == "<Annotations>")
832 getline(load_file, file_data_line);
833 seq->set_species(file_data_line);
834 while ( (!load_file.eof()) && (file_data_line != "</Annotations>") )
836 getline(load_file,file_data_line);
837 if ((file_data_line != "") && (file_data_line != "</Annotations>"))
839 // need to get 4 values...almost same code 4 times...
840 // get annot start index
841 space_split_i = file_data_line.find(" ");
842 annot_value = file_data_line.substr(0,space_split_i);
843 annot_begin = atoi (annot_value.c_str());
844 file_data_line = file_data_line.substr(space_split_i+1);
845 // get annot end index
846 space_split_i = file_data_line.find(" ");
847 annot_value = file_data_line.substr(0,space_split_i);
848 annot_end = atoi (annot_value.c_str());
850 if (space_split_i == std::string::npos) // no entry for type or name
852 std::cout << "seq, annots - no type or name\n";
856 else // else get annot type
858 file_data_line = file_data_line.substr(space_split_i+1);
859 space_split_i = file_data_line.find(" ");
860 annot_value = file_data_line.substr(0,space_split_i);
861 //an_annot.type = annot_value;
862 annot_type = annot_value;
863 if (space_split_i == std::string::npos) // no entry for name
865 std::cout << "seq, annots - no name\n";
868 else // get annot name
870 file_data_line = file_data_line.substr(space_split_i+1);
871 space_split_i = file_data_line.find(" ");
872 annot_value = file_data_line.substr(0,space_split_i);
873 // this seems like its wrong?
874 annot_type = annot_value;
877 seq->add_annotation(annot_name, annot_type, annot_begin, annot_end);
879 //std::cout << "seq, annots: " << an_annot.start << ", " << an_annot.end
880 // << "-->" << an_annot.type << "::" << an_annot.name << std::endl;
888 void Sequence::add_motif(const Sequence& a_motif)
890 std::vector<int> motif_starts = find_motif(a_motif);
892 for(std::vector<int>::iterator motif_start_i = motif_starts.begin();
893 motif_start_i != motif_starts.end();
896 motif_list->push_back(motif(*motif_start_i, a_motif.get_sequence()));
900 void Sequence::clear_motifs()
905 motif_list.reset(new MotifList);
908 const Sequence::MotifList& Sequence::motifs() const
914 Sequence::find_motif(const Sequence& a_motif) const
916 std::vector<int> motif_match_starts;
917 Sequence norm_motif_rc;
919 motif_match_starts.clear();
920 // std::cout << "motif is: " << norm_motif << std::endl;
922 if (a_motif.size() > 0)
924 //std::cout << "Sequence: none blank motif\n";
925 motif_scan(a_motif, &motif_match_starts);
927 norm_motif_rc = a_motif.rev_comp();;
928 // make sure not to do search again if it is a palindrome
929 if (norm_motif_rc != a_motif) {
930 motif_scan(norm_motif_rc, &motif_match_starts);
933 return motif_match_starts;
937 Sequence::motif_scan(const Sequence& a_motif, std::vector<int> * motif_match_starts) const
939 // if there's no sequence we can't scan for it?
940 // should this throw an exception?
943 std::string::size_type seq_i = 0;
944 Sequence::size_type motif_i = 0;
945 Sequence::size_type motif_len = a_motif.length();
946 Sequence::value_type motif_char;
947 Sequence::value_type seq_char;
949 while (seq_i < size())
951 // this is pretty much a straight translation of Nora's python code
952 // to match iupac letter codes
953 motif_char = toupper(a_motif[motif_i]);
954 seq_char = toupper(seq->at(seq_i));
955 if (motif_char =='N')
957 else if (motif_char == seq_char)
959 else if ((motif_char =='M') && ((seq_char=='A') || (seq_char=='C')))
961 else if ((motif_char =='R') && ((seq_char=='A') || (seq_char=='G')))
963 else if ((motif_char =='W') && ((seq_char=='A') || (seq_char=='T')))
965 else if ((motif_char =='S') && ((seq_char=='C') || (seq_char=='G')))
967 else if ((motif_char =='Y') && ((seq_char=='C') || (seq_char=='T')))
969 else if ((motif_char =='K') && ((seq_char=='G') || (seq_char=='T')))
971 else if ((motif_char =='V') &&
972 ((seq_char=='A') || (seq_char=='C') || (seq_char=='G')))
974 else if ((motif_char =='H') &&
975 ((seq_char=='A') || (seq_char=='C') || (seq_char=='T')))
977 else if ((motif_char =='D') &&
978 ((seq_char=='A') || (seq_char=='G') || (seq_char=='T')))
980 else if ((motif_char =='B') &&
981 ((seq_char=='C') || (seq_char=='G') || (seq_char=='T')))
985 // if a motif doesn't match, erase our current trial and try again
990 // end Nora stuff, now we see if a match is found this pass
991 if (motif_i == motif_len)
993 motif_match_starts->push_back(seq_i - motif_len + 1);
999 //std::cout << std::endl;
1002 void Sequence::add_string_annotation(std::string a_seq,
1005 std::vector<int> seq_starts = find_motif(a_seq);
1007 for(std::vector<int>::iterator seq_start_i = seq_starts.begin();
1008 seq_start_i != seq_starts.end();
1011 add_annotation(name, "", *seq_start_i, *seq_start_i+a_seq.size());
1015 void Sequence::find_sequences(std::list<Sequence>::iterator start,
1016 std::list<Sequence>::iterator end)
1018 while (start != end) {
1019 add_string_annotation(start->get_sequence(), start->get_fasta_header());
1025 std::ostream& operator<<(std::ostream& out, const Sequence& s)
1028 for(Sequence::const_iterator s_i = s.begin(); s_i != s.end(); ++s_i) {
1035 bool operator<(const Sequence& x, const Sequence& y)
1037 Sequence::const_iterator x_i = x.begin();
1038 Sequence::const_iterator y_i = y.begin();
1039 // for sequences there's some computation associated with computing .end
1040 // so lets cache it.
1041 Sequence::const_iterator xend = x.end();
1042 Sequence::const_iterator yend = y.end();
1044 if( x_i == xend and y_i == yend ) {
1046 } else if ( x_i == xend ) {
1048 } else if ( y_i == yend ) {
1050 } else if ( (*x_i) < (*y_i)) {
1052 } else if ( (*x_i) > (*y_i) ) {
1061 template <typename Iter1, typename Iter2>
1063 bool sequence_insensitive_equality(Iter1 abegin, Iter1 aend, Iter2 bbegin, Iter2 bend)
1065 Iter1 aseq_i = abegin;
1066 Iter2 bseq_i = bbegin;
1067 if (aend-abegin == bend-bbegin) {
1068 // since the length of the two sequences is equal, we only need to
1070 for(; aseq_i != aend; ++aseq_i, ++bseq_i) {
1071 if (toupper(*aseq_i) != toupper(*bseq_i)) {
1081 bool operator==(const Sequence& x, const Sequence& y)
1083 if (x.seq and y.seq) {
1084 // both x and y are defined
1085 if (SeqSpan::isFamily(x.seq, y.seq)) {
1086 // both are part of the same SeqSpan tree
1087 return *(x.seq) == *(y.seq);
1089 // we'll have to do a real comparison
1090 return sequence_insensitive_equality<SeqSpan::const_iterator, SeqSpan::const_iterator>(
1096 // true if they're both empty (with either a null SeqSpanRef or
1097 // a zero length string
1098 return (x.size() == y.size());
1102 bool operator!=(const Sequence& x, const Sequence& y)
1104 return not operator==(x, y);