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());
327 // so i should probably be passing the parse function some iterators
328 // but the annotations files are (currently) small, so i think i can
329 // get away with loading the whole file into memory
332 while(data_stream.good()) {
339 parse_annot(data, start_index, end_index);
340 } catch(annotation_load_error e) {
341 std::ostringstream msg;
342 msg << file_path.native_file_string()
345 throw annotation_load_error(msg.str());
349 /* If this works, yikes, this is some brain hurting code.
351 * what's going on is that when pb_annot is instantiated it stores references
352 * to begin, end, name, type, declared in the parse function, then
353 * when operator() is called it grabs values from those references
354 * and uses that to instantiate an annot object and append that to our
357 * This weirdness is because the spirit library requires that actions
358 * conform to a specific prototype operator()(IteratorT, IteratorT)
359 * which doesn't provide any useful opportunity for me to actually
360 * grab the results of our parsing.
362 * so I instantiate this structure in order to have a place to grab
366 struct push_back_annot {
368 SeqSpanRefListRef children;
375 push_back_annot(Sequence* parent_seq,
376 SeqSpanRefListRef children_list,
382 : parent(parent_seq),
383 children(children_list),
392 void operator()(std::string::const_iterator,
393 std::string::const_iterator) const
395 children->push_back(parent->make_annotation(name, type, begin, end));
400 struct push_back_seq {
401 std::list<Sequence>& seq_list;
406 push_back_seq(std::list<Sequence>& seq_list_,
410 : seq_list(seq_list_),
417 void operator()(std::string::const_iterator,
418 std::string::const_iterator) const
420 // filter out newlines from our sequence
422 for(std::string::const_iterator seq_i = seq.begin();
426 if (*seq_i != '\015' && *seq_i != '\012') new_seq += *seq_i;
428 //std::cout << "adding seq: " << name << " " << new_seq << std::endl;
431 s.set_fasta_header(name);
432 seq_list.push_back(s);
438 Sequence::parse_annot(std::string data, int start_index, int end_index)
445 SeqSpanRefListRef parsed_annots(new SeqSpanRefList);
446 std::list<Sequence> query_seqs;
449 bool ok = spirit::parse(data.begin(), data.end(),
456 )[spirit::assign_a(species)] >>
460 ( // ignore html tags
461 *(spirit::space_p) >>
463 +(~spirit::ch_p('>')) >>
468 ( // parse an absolute location name
469 (spirit::uint_p[spirit::assign_a(start)] >>
471 spirit::uint_p[spirit::assign_a(end)] >>
476 )[spirit::assign_a(name)] >>
483 )[spirit::assign_a(type)]
485 // to understand how this group gets set
486 // read the comment above struct push_back_annot
487 )[push_back_annot(this, parsed_annots, start, end, name, type, parsed)]
489 ((spirit::ch_p('>')|spirit::str_p(">")) >>
490 (*(spirit::print_p))[spirit::assign_a(name)] >>
492 (+(spirit::chset<>(Alphabet::nucleic_cstr)))[spirit::assign_a(seqstr)]
493 )[push_back_seq(query_seqs, name, seqstr, parsed)]
500 std::stringstream msg;
501 msg << "Error parsing annotation #" << parsed;
502 throw annotation_load_error(msg.str());
504 // If everything loaded correctly add the sequences to our annotation list
505 // add newly parsed annotations to our sequence
506 std::copy(parsed_annots->begin(), parsed_annots->end(), std::back_inserter(*annotation_list));
507 // go search for query sequences
508 find_sequences(query_seqs.begin(), query_seqs.end());
511 void Sequence::add_annotation(const SeqSpanRef a)
513 annotation_list->push_back(a);
516 void Sequence::add_annotation(std::string name, std::string type, size_type start, size_type stop)
518 add_annotation(make_annotation(name, type, start, stop));
522 Sequence::make_annotation(std::string name, std::string type, size_type start, size_type stop) const
524 // we want things to be in the positive direction
526 size_type tmp = start;
530 size_type count = stop - start;
531 SeqSpanRef new_annot(seq->subseq(start, count, SeqSpan::UnknownStrand));
532 AnnotationsRef metadata(new Annotations(name));
533 metadata->set("type", type);
534 new_annot->setAnnotations(metadata);
538 const SeqSpanRefList& Sequence::annotations() const
540 return *annotation_list;
543 void Sequence::copy_children(Sequence &new_seq, size_type start, size_type count) const
545 new_seq.motif_list = motif_list;
546 new_seq.annotation_list.reset(new SeqSpanRefList);
548 for(SeqSpanRefList::const_iterator annot_i = annotation_list->begin();
549 annot_i != annotation_list->end();
552 size_type annot_begin= (*annot_i)->start();
553 size_type annot_end = (*annot_i)->stop();
555 if (annot_begin < start+count) {
556 if (annot_begin >= start) {
557 annot_begin -= start;
562 if (annot_end < start+count) {
567 SeqSpanRef new_annot(new_seq.seq->subseq(annot_begin, annot_end));
568 new_annot->setAnnotations((*annot_i)->annotations());
569 new_seq.annotation_list->push_back(new_annot);
575 Sequence::subseq(size_type start, size_type count, SeqSpan::strand_type strand) const
577 // FIXME: should i really allow a subsequence of an empty sequence?
583 Sequence new_seq(*this);
584 new_seq.seq = seq->subseq(start, count, strand);
585 if (seq->annotations()) {
586 AnnotationsRef a(new Annotations(*(seq->annotations())));
587 new_seq.seq->setAnnotations(a);
589 copy_children(new_seq, start, count);
595 // FIXME: This needs to be moved into SeqSpan
596 Sequence Sequence::rev_comp() const
598 // a reverse complement is the whole opposite strand
599 return subseq(0, npos, SeqSpan::OppositeStrand);
602 const Alphabet& Sequence::get_alphabet() const
604 return (seq) ? seq->get_alphabet() : Alphabet::empty_alphabet();
607 void Sequence::set_fasta_header(std::string header_)
612 void Sequence::set_species(const std::string& name)
617 std::string Sequence::get_species() const
624 Sequence::get_fasta_header() const
630 Sequence::get_name() const
632 if (header.size() > 0)
634 else if (species.size() > 0)
640 void Sequence::set_sequence(const std::string& s, AlphabetRef a)
642 set_filtered_sequence(s, a, 0, s.size(), SeqSpan::PlusStrand);
645 std::string Sequence::get_sequence() const
647 return seq->sequence();
650 Sequence::const_reference Sequence::operator[](Sequence::size_type i) const
661 annotation_list.reset(new SeqSpanRefList);
662 motif_list.reset(new MotifList);
666 Sequence::save(fs::fstream &save_file)
668 std::string type("type");
669 std::string empty_str("");
671 SeqSpanRefList::iterator annots_i;
672 AnnotationsRef metadata;
674 // not sure why, or if i'm doing something wrong, but can't seem to pass
675 // file pointers down to this method from the mussa control class
676 // so each call to save a sequence appends to the file started by mussa_class
677 //save_file.open(save_file_path.c_str(), std::ios::app);
679 save_file << "<Sequence>" << std::endl;
680 save_file << *this << std::endl;
681 save_file << "</Sequence>" << std::endl;
683 save_file << "<Annotations>" << std::endl;
684 save_file << species << std::endl;
685 for (annots_i = annotation_list->begin();
686 annots_i != annotation_list->end();
689 metadata = (*annots_i)->annotations();
690 save_file << (*annots_i)->parentStart() << " " << (*annots_i)->parentStop() << " " ;
691 save_file << metadata->name() << " "
692 << metadata->getdefault(type, empty_str) << std::endl;
694 save_file << "</Annotations>" << std::endl;
699 //Sequence::load_museq(fs::path load_file_path, int seq_num)
701 // fs::fstream load_file;
702 // std::string file_data_line;
707 // std::string annot_name;
708 // std::string annot_type;
710 // std::string::size_type space_split_i;
711 // std::string annot_value;
713 // annotation_list.reset(new SeqSpanRefList);
715 // load_file.open(load_file_path, std::ios::in);
718 // // search for the seq_num-th sequence
719 // while ( (!load_file.eof()) && (seq_counter < seq_num) )
721 // getline(load_file,file_data_line);
722 // if (file_data_line == "<Sequence>")
725 // getline(load_file, file_data_line);
726 // // looks like the sequence is written as a single line
727 // set_filtered_sequence(file_data_line, reduced_dna_alphabet, 0, file_data_line.size(), SeqSpan::PlusStrand);
728 // getline(load_file, file_data_line);
729 // getline(load_file, file_data_line);
730 // if (file_data_line == "<Annotations>")
732 // getline(load_file, file_data_line);
733 // species = file_data_line;
734 // while ( (!load_file.eof()) && (file_data_line != "</Annotations>") )
736 // getline(load_file,file_data_line);
737 // if ((file_data_line != "") && (file_data_line != "</Annotations>"))
739 // // need to get 4 values...almost same code 4 times...
740 // // get annot start index
741 // space_split_i = file_data_line.find(" ");
742 // annot_value = file_data_line.substr(0,space_split_i);
743 // annot_begin = atoi (annot_value.c_str());
744 // file_data_line = file_data_line.substr(space_split_i+1);
745 // // get annot end index
746 // space_split_i = file_data_line.find(" ");
747 // annot_value = file_data_line.substr(0,space_split_i);
748 // annot_end = atoi (annot_value.c_str());
750 // if (space_split_i == std::string::npos) // no entry for type or name
752 // std::cout << "seq, annots - no type or name\n";
756 // else // else get annot type
758 // file_data_line = file_data_line.substr(space_split_i+1);
759 // space_split_i = file_data_line.find(" ");
760 // annot_value = file_data_line.substr(0,space_split_i);
761 // //an_annot.type = annot_value;
762 // annot_type = annot_value;
763 // if (space_split_i == std::string::npos) // no entry for name
765 // std::cout << "seq, annots - no name\n";
768 // else // get annot name
770 // file_data_line = file_data_line.substr(space_split_i+1);
771 // space_split_i = file_data_line.find(" ");
772 // annot_value = file_data_line.substr(0,space_split_i);
773 // // this seems like its wrong?
774 // annot_type = annot_value;
777 // add_annotation(annot_name, annot_type, annot_begin, annot_end);
779 // //std::cout << "seq, annots: " << an_annot.start << ", " << an_annot.end
780 // // << "-->" << an_annot.type << "::" << an_annot.name << std::endl;
783 // load_file.close();
786 SequenceRef Sequence::load_museq(boost::filesystem::fstream& load_file)
788 boost::shared_ptr<Sequence> seq(new Sequence);
789 std::string file_data_line;
794 std::string annot_name;
795 std::string annot_type;
797 std::string::size_type space_split_i;
798 std::string annot_value;
800 //seq->annotation_list.reset(new SeqSpanRefList);
803 // search for the next sequence
805 while ( (!load_file.eof()) && (seq_counter < seq_num) )
807 getline(load_file,file_data_line);
808 if (file_data_line == "<Sequence>")
812 // Could not find next sequence
819 getline(load_file, file_data_line);
820 // looks like the sequence is written as a single line
821 seq->set_filtered_sequence(file_data_line, reduced_dna_alphabet, 0, file_data_line.size(), SeqSpan::PlusStrand);
822 getline(load_file, file_data_line);
823 getline(load_file, file_data_line);
824 if (file_data_line == "<Annotations>")
826 getline(load_file, file_data_line);
827 seq->set_species(file_data_line);
828 while ( (!load_file.eof()) && (file_data_line != "</Annotations>") )
830 getline(load_file,file_data_line);
831 if ((file_data_line != "") && (file_data_line != "</Annotations>"))
833 // need to get 4 values...almost same code 4 times...
834 // get annot start index
835 space_split_i = file_data_line.find(" ");
836 annot_value = file_data_line.substr(0,space_split_i);
837 annot_begin = atoi (annot_value.c_str());
838 file_data_line = file_data_line.substr(space_split_i+1);
839 // get annot end index
840 space_split_i = file_data_line.find(" ");
841 annot_value = file_data_line.substr(0,space_split_i);
842 annot_end = atoi (annot_value.c_str());
844 if (space_split_i == std::string::npos) // no entry for type or name
846 std::cout << "seq, annots - no type or name\n";
850 else // else get annot type
852 file_data_line = file_data_line.substr(space_split_i+1);
853 space_split_i = file_data_line.find(" ");
854 annot_value = file_data_line.substr(0,space_split_i);
855 //an_annot.type = annot_value;
856 annot_type = annot_value;
857 if (space_split_i == std::string::npos) // no entry for name
859 std::cout << "seq, annots - no name\n";
862 else // get annot name
864 file_data_line = file_data_line.substr(space_split_i+1);
865 space_split_i = file_data_line.find(" ");
866 annot_value = file_data_line.substr(0,space_split_i);
867 // this seems like its wrong?
868 annot_type = annot_value;
871 seq->add_annotation(annot_name, annot_type, annot_begin, annot_end);
873 //std::cout << "seq, annots: " << an_annot.start << ", " << an_annot.end
874 // << "-->" << an_annot.type << "::" << an_annot.name << std::endl;
882 void Sequence::add_motif(const Sequence& a_motif)
884 std::vector<int> motif_starts = find_motif(a_motif);
886 for(std::vector<int>::iterator motif_start_i = motif_starts.begin();
887 motif_start_i != motif_starts.end();
890 motif_list->push_back(motif(*motif_start_i, a_motif.get_sequence()));
894 void Sequence::clear_motifs()
899 motif_list.reset(new MotifList);
902 const Sequence::MotifList& Sequence::motifs() const
908 Sequence::find_motif(const Sequence& a_motif) const
910 std::vector<int> motif_match_starts;
911 Sequence norm_motif_rc;
913 motif_match_starts.clear();
914 // std::cout << "motif is: " << norm_motif << std::endl;
916 if (a_motif.size() > 0)
918 //std::cout << "Sequence: none blank motif\n";
919 motif_scan(a_motif, &motif_match_starts);
921 norm_motif_rc = a_motif.rev_comp();;
922 // make sure not to do search again if it is a palindrome
923 if (norm_motif_rc != a_motif) {
924 motif_scan(norm_motif_rc, &motif_match_starts);
927 return motif_match_starts;
931 Sequence::motif_scan(const Sequence& a_motif, std::vector<int> * motif_match_starts) const
933 // if there's no sequence we can't scan for it?
934 // should this throw an exception?
937 std::string::size_type seq_i = 0;
938 Sequence::size_type motif_i = 0;
939 Sequence::size_type motif_len = a_motif.length();
940 Sequence::value_type motif_char;
941 Sequence::value_type seq_char;
943 while (seq_i < size())
945 // this is pretty much a straight translation of Nora's python code
946 // to match iupac letter codes
947 motif_char = toupper(a_motif[motif_i]);
948 seq_char = toupper(seq->at(seq_i));
949 if (motif_char =='N')
951 else if (motif_char == seq_char)
953 else if ((motif_char =='M') && ((seq_char=='A') || (seq_char=='C')))
955 else if ((motif_char =='R') && ((seq_char=='A') || (seq_char=='G')))
957 else if ((motif_char =='W') && ((seq_char=='A') || (seq_char=='T')))
959 else if ((motif_char =='S') && ((seq_char=='C') || (seq_char=='G')))
961 else if ((motif_char =='Y') && ((seq_char=='C') || (seq_char=='T')))
963 else if ((motif_char =='K') && ((seq_char=='G') || (seq_char=='T')))
965 else if ((motif_char =='V') &&
966 ((seq_char=='A') || (seq_char=='C') || (seq_char=='G')))
968 else if ((motif_char =='H') &&
969 ((seq_char=='A') || (seq_char=='C') || (seq_char=='T')))
971 else if ((motif_char =='D') &&
972 ((seq_char=='A') || (seq_char=='G') || (seq_char=='T')))
974 else if ((motif_char =='B') &&
975 ((seq_char=='C') || (seq_char=='G') || (seq_char=='T')))
979 // if a motif doesn't match, erase our current trial and try again
984 // end Nora stuff, now we see if a match is found this pass
985 if (motif_i == motif_len)
987 motif_match_starts->push_back(seq_i - motif_len + 1);
993 //std::cout << std::endl;
996 void Sequence::add_string_annotation(std::string a_seq,
999 std::vector<int> seq_starts = find_motif(a_seq);
1001 for(std::vector<int>::iterator seq_start_i = seq_starts.begin();
1002 seq_start_i != seq_starts.end();
1005 add_annotation(name, "", *seq_start_i, *seq_start_i+a_seq.size());
1009 void Sequence::find_sequences(std::list<Sequence>::iterator start,
1010 std::list<Sequence>::iterator end)
1012 while (start != end) {
1013 add_string_annotation(start->get_sequence(), start->get_fasta_header());
1019 std::ostream& operator<<(std::ostream& out, const Sequence& s)
1022 for(Sequence::const_iterator s_i = s.begin(); s_i != s.end(); ++s_i) {
1029 bool operator<(const Sequence& x, const Sequence& y)
1031 Sequence::const_iterator x_i = x.begin();
1032 Sequence::const_iterator y_i = y.begin();
1033 // for sequences there's some computation associated with computing .end
1034 // so lets cache it.
1035 Sequence::const_iterator xend = x.end();
1036 Sequence::const_iterator yend = y.end();
1038 if( x_i == xend and y_i == yend ) {
1040 } else if ( x_i == xend ) {
1042 } else if ( y_i == yend ) {
1044 } else if ( (*x_i) < (*y_i)) {
1046 } else if ( (*x_i) > (*y_i) ) {
1055 template <typename Iter1, typename Iter2>
1057 bool sequence_insensitive_equality(Iter1 abegin, Iter1 aend, Iter2 bbegin, Iter2 bend)
1059 Iter1 aseq_i = abegin;
1060 Iter2 bseq_i = bbegin;
1061 if (aend-abegin == bend-bbegin) {
1062 // since the length of the two sequences is equal, we only need to
1064 for(; aseq_i != aend; ++aseq_i, ++bseq_i) {
1065 if (toupper(*aseq_i) != toupper(*bseq_i)) {
1075 bool operator==(const Sequence& x, const Sequence& y)
1077 if (x.seq and y.seq) {
1078 // both x and y are defined
1079 if (SeqSpan::isFamily(x.seq, y.seq)) {
1080 // both are part of the same SeqSpan tree
1081 return *(x.seq) == *(y.seq);
1083 // we'll have to do a real comparison
1084 return sequence_insensitive_equality<SeqSpan::const_iterator, SeqSpan::const_iterator>(
1090 // true if they're both empty (with either a null SeqSpanRef or
1091 // a zero length string
1092 return (x.size() == y.size());
1096 bool operator!=(const Sequence& x, const Sequence& y)
1098 return not operator==(x, y);