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 namespace fs = boost::filesystem;
27 #include <boost/spirit/core.hpp>
28 #include <boost/spirit/actor/push_back_actor.hpp>
29 #include <boost/spirit/iterator/file_iterator.hpp>
30 #include <boost/spirit/utility/chset.hpp>
31 namespace spirit = boost::spirit;
33 #include "alg/sequence.hpp"
34 #include "mussa_exceptions.hpp"
48 annot::annot(int begin, int end, std::string type, std::string name)
60 bool operator==(const annot& left, const annot& right)
62 return ((left.begin== right.begin) and
63 (left.end == right.end) and
64 (left.type == right.type) and
65 (left.name == right.name));
68 motif::motif(int begin, std::string motif)
69 : annot(begin, begin+motif.size(), "motif", motif),
78 const std::string Sequence::dna_alphabet("AaCcGgTtNn\012\015");
79 const std::string Sequence::rna_alphabet("AaCcGgNnUu\012\015");
80 //! this is the general iupac alphabet for nucleotides
81 const std::string Sequence::nucleic_iupac_alphabet("AaCcGgTtUuRrYyMmKkSsWwBbDdHhVvNn\012\015");
82 //! the protein alphabet
83 const std::string Sequence::protein_alphabet("AaCcDdEeFfGgHhIiKkLlMmNnPpQqRrSsTtVvWwYy\012\015");
97 Sequence::Sequence(const char *seq)
101 strand(UnknownStrand),
105 set_filtered_sequence(seq);
108 Sequence::Sequence(const std::string& seq)
112 strand(UnknownStrand),
116 set_filtered_sequence(seq);
119 Sequence::Sequence(const Sequence& o)
122 seq_start(o.seq_start),
123 seq_count(o.seq_count),
128 motif_list(o.motif_list)
132 Sequence &Sequence::operator=(const Sequence& s)
137 seq_start = s.seq_start;
138 seq_count = s.seq_count;
143 motif_list = s.motif_list;
148 static void multiplatform_getline(std::istream& in, std::string& line)
153 while(in.good() and !(c == '\012' or c == '\015') ) {
157 // if we have cr-lf eat it
159 if (c=='\012' or c == '\015') {
164 //! load a fasta file into a sequence
166 * \param file_path the location of the fasta file in the filesystem
167 * \param seq_num which sequence in the file to load
168 * \param start_index starting position in the fasta sequence, 0 for beginning
169 * \param end_index ending position in the fasta sequence, 0 for end
170 * \return error message, empty string if no error. (gag!)
172 void Sequence::load_fasta(fs::path file_path, int seq_num,
173 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, 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());
202 Sequence::load_fasta(std::iostream& data_file, int seq_num,
203 int start_index, int end_index)
205 std::string file_data_line;
206 int header_counter = 0;
207 bool read_seq = true;
208 std::string rev_comp;
209 std::string sequence_raw;
210 std::string seq_tmp; // holds sequence during basic filtering
213 throw mussa_load_error("fasta sequence number is 1 based (can't be 0)");
216 // search for the header of the fasta sequence we want
217 while ( (!data_file.eof()) && (header_counter < seq_num) )
219 multiplatform_getline(data_file, file_data_line);
220 if (file_data_line.substr(0,1) == ">")
224 if (header_counter > 0) {
225 header = file_data_line.substr(1);
229 while ( !data_file.eof() && read_seq ) {
230 multiplatform_getline(data_file,file_data_line);
231 if (file_data_line.substr(0,1) == ">")
233 else sequence_raw += file_data_line;
236 // Lastly, if subselection of the sequence was specified we keep cut out
237 // and only keep that part
238 // end_index = 0 means no end was specified, so cut to the end
240 end_index = sequence_raw.size();
242 // sequence filtering for upcasing agctn and convert non AGCTN to N
243 if (end_index-start_index <= 0) {
244 std::string msg("The selected sequence appears to be empty");
245 throw sequence_empty_error(msg);
247 set_filtered_sequence(sequence_raw, start_index, end_index-start_index);
249 std::string errormsg("There were no fasta sequences");
250 throw sequence_empty_file_error(errormsg);
254 void Sequence::set_filtered_sequence(const std::string &old_seq,
259 char conversionTable[257];
262 count = old_seq.size() - start;
263 boost::shared_ptr<seq_string> new_seq(new seq_string);
264 new_seq->reserve(count);
266 // Make a conversion table
268 // everything we don't specify below will become 'N'
269 for(int table_i=0; table_i < 256; table_i++)
271 conversionTable[table_i] = 'N';
273 // add end of string character for printing out table for testing purposes
274 conversionTable[256] = '\0';
276 // we want these to map to themselves - ie not to change
277 conversionTable[(int)'A'] = 'A';
278 conversionTable[(int)'T'] = 'T';
279 conversionTable[(int)'G'] = 'G';
280 conversionTable[(int)'C'] = 'C';
282 conversionTable[(int)'a'] = 'A';
283 conversionTable[(int)'t'] = 'T';
284 conversionTable[(int)'g'] = 'G';
285 conversionTable[(int)'c'] = 'C';
287 // finally, the actual conversion loop
288 for(std::string::size_type seq_index = 0; seq_index < count; seq_index++)
290 new_seq->append(1, conversionTable[ (int)old_seq[seq_index+start]]);
300 Sequence::load_annot(fs::path file_path, int start_index, int end_index)
302 fs::fstream data_stream(file_path, std::ios::in);
305 throw mussa_load_error("Sequence File: " + file_path.string() + " not found");
308 // so i should probably be passing the parse function some iterators
309 // but the annotations files are (currently) small, so i think i can
310 // get away with loading the whole file into memory
313 while(data_stream.good()) {
319 parse_annot(data, start_index, end_index);
322 /* If this works, yikes, this is some brain hurting code.
324 * what's going on is that when pb_annot is instantiated it stores references
325 * to begin, end, name, type, declared in the parse function, then
326 * when operator() is called it grabs values from those references
327 * and uses that to instantiate an annot object and append that to our
330 * This weirdness is because the spirit library requires that actions
331 * conform to a specific prototype operator()(IteratorT, IteratorT)
332 * which doesn't provide any useful opportunity for me to actually
333 * grab the results of our parsing.
335 * so I instantiate this structure in order to have a place to grab
339 struct push_back_annot {
340 std::list<annot>& annot_list;
346 push_back_annot(std::list<annot>& annot_list_,
351 : annot_list(annot_list_),
359 void operator()(std::string::const_iterator,
360 std::string::const_iterator) const
362 //std::cout << "adding annot: " << begin << "|" << end << "|" << name << "|" << type << std::endl;
363 annot_list.push_back(annot(begin, end, name, type));
367 struct push_back_seq {
368 std::list<Sequence>& seq_list;
372 push_back_seq(std::list<Sequence>& seq_list_,
375 : seq_list(seq_list_),
381 void operator()(std::string::const_iterator,
382 std::string::const_iterator) const
384 // filter out newlines from our sequence
386 for(std::string::const_iterator seq_i = seq.begin();
390 if (*seq_i != '\015' && *seq_i != '\012') new_seq += *seq_i;
392 //std::cout << "adding seq: " << name << " " << new_seq << std::endl;
395 s.set_fasta_header(name);
396 seq_list.push_back(s);
401 Sequence::parse_annot(std::string data, int start_index, int end_index)
408 std::list<Sequence> query_seqs;
410 bool status = spirit::parse(data.begin(), data.end(),
417 )[spirit::assign_a(species)] >>
421 ( // ignore html tags
422 *(spirit::space_p) >>
424 +(~spirit::ch_p('>')) >>
429 ( // parse an absolute location name
430 (spirit::uint_p[spirit::assign_a(start)] >>
432 spirit::uint_p[spirit::assign_a(end)] >>
437 )[spirit::assign_a(name)] >>
444 )[spirit::assign_a(type)]
446 // to understand how this group gets set
447 // read the comment above struct push_back_annot
448 )[push_back_annot(annots, start, end, type, name)]
450 ((spirit::ch_p('>')|spirit::str_p(">")) >>
451 (*(spirit::print_p))[spirit::assign_a(name)] >>
453 (+(spirit::chset<>(nucleic_iupac_alphabet.c_str())))[spirit::assign_a(seq)]
454 )[push_back_seq(query_seqs, name, seq)]
461 // go seearch for query sequences
462 find_sequences(query_seqs.begin(), query_seqs.end());
466 void Sequence::add_annotation(const annot& a)
471 const std::list<annot>& Sequence::annotations() const
477 Sequence::subseq(int start, int count)
484 // there might be an off by one error with start+count > size()
485 if ( count == npos || start+count > size()) {
486 count = size()-start;
488 Sequence new_seq(*this);
489 new_seq.parent = this;
490 new_seq.seq_start = seq_start+start;
491 new_seq.seq_count = count;
493 new_seq.motif_list = motif_list;
494 new_seq.annots.clear();
495 // attempt to copy & reannotate position based annotations
496 int end = start+count;
498 for(std::list<annot>::const_iterator annot_i = annots.begin();
499 annot_i != annots.end();
502 int annot_begin= annot_i->begin;
503 int annot_end = annot_i->end;
505 if (annot_begin < end) {
506 if (annot_begin >= start) {
507 annot_begin -= start;
512 if (annot_end < end) {
518 annot new_annot(annot_begin, annot_end, annot_i->type, annot_i->name);
519 new_seq.annots.push_back(new_annot);
527 Sequence::rev_comp() const
529 std::string rev_comp;
530 char conversionTable[257];
531 int seq_i, table_i, len;
534 rev_comp.reserve(len);
535 // make a conversion table
536 // init all parts of conversion table to '~' character
537 // '~' I doubt will ever appear in a sequence file (jeez, I hope)
538 // and may the fleas of 1000 camels infest the genitals of any biologist (and
539 // seven generations of their progeny) who decides to make it mean
540 // something special!!!
541 // PS - double the curse for any smartass non-biologist who tries it as well
542 for(table_i=0; table_i < 256; table_i++)
544 conversionTable[table_i] = '~';
546 // add end of string character for printing out table for testing purposes
547 conversionTable[256] = '\0';
549 // add in the characters for the bases we want to convert
550 conversionTable[(int)'A'] = 'T';
551 conversionTable[(int)'T'] = 'A';
552 conversionTable[(int)'G'] = 'C';
553 conversionTable[(int)'C'] = 'G';
554 conversionTable[(int)'N'] = 'N';
556 // finally, the actual conversion loop
557 for(seq_i = len - 1; seq_i >= 0; seq_i--)
559 table_i = (int) at(seq_i);
560 rev_comp += conversionTable[table_i];
566 void Sequence::set_fasta_header(std::string header_)
571 void Sequence::set_species(const std::string& name)
576 std::string Sequence::get_species() const
583 Sequence::get_fasta_header() const
589 Sequence::get_name() const
591 if (header.size() > 0)
593 else if (species.size() > 0)
599 void Sequence::set_sequence(const std::string& s)
601 set_filtered_sequence(s);
604 std::string Sequence::get_sequence() const
609 return std::string();
612 Sequence::const_reference Sequence::operator[](Sequence::size_type i) const
617 Sequence::const_reference Sequence::at(Sequence::size_type i) const
619 if (!seq) throw std::out_of_range("empty sequence");
620 return seq->at(i+seq_start);
630 strand = UnknownStrand;
637 const char *Sequence::c_str() const
640 return seq->c_str()+seq_start;
645 Sequence::const_iterator Sequence::begin() const
647 if (seq and seq_count != 0)
648 return seq->begin()+seq_start;
650 return Sequence::const_iterator(0);
653 Sequence::const_iterator Sequence::end() const
655 if (seq and seq_count != 0) {
656 return seq->begin() + seq_start + seq_count;
658 return Sequence::const_iterator(0);
662 bool Sequence::empty() const
664 return (seq_count == 0) ? true : false;
667 Sequence::size_type Sequence::start() const
670 return seq_start - parent->start();
675 Sequence::size_type Sequence::stop() const
677 return start() + seq_count;
680 Sequence::size_type Sequence::size() const
685 Sequence::size_type Sequence::length() const
691 Sequence::save(fs::fstream &save_file)
694 std::list<annot>::iterator annots_i;
696 // not sure why, or if i'm doing something wrong, but can't seem to pass
697 // file pointers down to this method from the mussa control class
698 // so each call to save a sequence appends to the file started by mussa_class
699 //save_file.open(save_file_path.c_str(), std::ios::app);
701 save_file << "<Sequence>" << std::endl;
702 save_file << *this << std::endl;
703 save_file << "</Sequence>" << std::endl;
705 save_file << "<Annotations>" << std::endl;
706 save_file << species << std::endl;
707 for (annots_i = annots.begin(); annots_i != annots.end(); ++annots_i)
709 save_file << annots_i->begin << " " << annots_i->end << " " ;
710 save_file << annots_i->name << " " << annots_i->type << std::endl;
712 save_file << "</Annotations>" << std::endl;
717 Sequence::load_museq(fs::path load_file_path, int seq_num)
719 fs::fstream load_file;
720 std::string file_data_line;
723 std::string::size_type space_split_i;
724 std::string annot_value;
727 load_file.open(load_file_path, std::ios::in);
730 // search for the seq_num-th sequence
731 while ( (!load_file.eof()) && (seq_counter < seq_num) )
733 getline(load_file,file_data_line);
734 if (file_data_line == "<Sequence>")
737 getline(load_file, file_data_line);
738 // looks like the sequence is written as a single line
739 set_filtered_sequence(file_data_line);
740 getline(load_file, file_data_line);
741 getline(load_file, file_data_line);
742 if (file_data_line == "<Annotations>")
744 getline(load_file, file_data_line);
745 species = file_data_line;
746 while ( (!load_file.eof()) && (file_data_line != "</Annotations>") )
748 getline(load_file,file_data_line);
749 if ((file_data_line != "") && (file_data_line != "</Annotations>"))
751 // need to get 4 values...almost same code 4 times...
752 // get annot start index
753 space_split_i = file_data_line.find(" ");
754 annot_value = file_data_line.substr(0,space_split_i);
755 an_annot.begin = atoi (annot_value.c_str());
756 file_data_line = file_data_line.substr(space_split_i+1);
757 // get annot end index
758 space_split_i = file_data_line.find(" ");
759 annot_value = file_data_line.substr(0,space_split_i);
760 an_annot.end = atoi (annot_value.c_str());
762 if (space_split_i == std::string::npos) // no entry for type or name
764 std::cout << "seq, annots - no type or name\n";
768 else // else get annot type
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 an_annot.type = annot_value;
774 if (space_split_i == std::string::npos) // no entry for name
776 std::cout << "seq, annots - no name\n";
779 else // get annot name
781 file_data_line = file_data_line.substr(space_split_i+1);
782 space_split_i = file_data_line.find(" ");
783 annot_value = file_data_line.substr(0,space_split_i);
784 an_annot.type = annot_value;
787 annots.push_back(an_annot); // don't forget to actually add the annot
789 //std::cout << "seq, annots: " << an_annot.start << ", " << an_annot.end
790 // << "-->" << an_annot.type << "::" << an_annot.name << std::endl;
797 Sequence::rc_motif(std::string a_motif) const
799 std::string rev_comp;
800 char conversionTable[257];
801 int seq_i, table_i, len;
803 len = a_motif.length();
804 rev_comp.reserve(len);
806 for(table_i=0; table_i < 256; table_i++)
808 conversionTable[table_i] = '~';
810 // add end of std::string character for printing out table for testing purposes
811 conversionTable[256] = '\0';
813 // add in the characters for the bases we want to convert (IUPAC)
814 conversionTable[(int)'A'] = 'T';
815 conversionTable[(int)'T'] = 'A';
816 conversionTable[(int)'G'] = 'C';
817 conversionTable[(int)'C'] = 'G';
818 conversionTable[(int)'N'] = 'N';
819 conversionTable[(int)'M'] = 'K';
820 conversionTable[(int)'R'] = 'Y';
821 conversionTable[(int)'W'] = 'W';
822 conversionTable[(int)'S'] = 'S';
823 conversionTable[(int)'Y'] = 'R';
824 conversionTable[(int)'K'] = 'M';
825 conversionTable[(int)'V'] = 'B';
826 conversionTable[(int)'H'] = 'D';
827 conversionTable[(int)'D'] = 'H';
828 conversionTable[(int)'B'] = 'V';
830 // finally, the actual conversion loop
831 for(seq_i = len - 1; seq_i >= 0; seq_i--)
833 //std::cout << "** i = " << seq_i << " bp = " <<
834 table_i = (int) a_motif[seq_i];
835 rev_comp += conversionTable[table_i];
838 //std::cout << "seq: " << a_motif << std::endl;
839 //std::cout << "rc: " << rev_comp << std::endl;
845 Sequence::motif_normalize(const std::string& a_motif)
847 std::string valid_motif;
850 len = a_motif.length();
851 valid_motif.reserve(len);
853 // this just upcases IUPAC symbols. Eventually should return an error if non IUPAC is present.
854 // current nonIUPAC symbols are omitted, which is not reported atm
855 for(seq_i = 0; seq_i < len; seq_i++)
857 if ((a_motif[seq_i] == 'a') || (a_motif[seq_i] == 'A'))
859 else if ((a_motif[seq_i] == 't') || (a_motif[seq_i] == 'T'))
861 else if ((a_motif[seq_i] == 'g') || (a_motif[seq_i] == 'G'))
863 else if ((a_motif[seq_i] == 'c') || (a_motif[seq_i] == 'C'))
865 else if ((a_motif[seq_i] == 'n') || (a_motif[seq_i] == 'N'))
867 else if ((a_motif[seq_i] == 'm') || (a_motif[seq_i] == 'M'))
869 else if ((a_motif[seq_i] == 'r') || (a_motif[seq_i] == 'R'))
871 else if ((a_motif[seq_i] == 'w') || (a_motif[seq_i] == 'W'))
873 else if ((a_motif[seq_i] == 's') || (a_motif[seq_i] == 'S'))
875 else if ((a_motif[seq_i] == 'y') || (a_motif[seq_i] == 'Y'))
877 else if ((a_motif[seq_i] == 'k') || (a_motif[seq_i] == 'K'))
879 else if ((a_motif[seq_i] == 'v') || (a_motif[seq_i] == 'V'))
881 else if ((a_motif[seq_i] == 'h') || (a_motif[seq_i] == 'H'))
883 else if ((a_motif[seq_i] == 'd') || (a_motif[seq_i] == 'D'))
885 else if ((a_motif[seq_i] == 'b') || (a_motif[seq_i] == 'B'))
888 std::string msg = "Letter ";
889 msg += a_motif[seq_i];
890 msg += " is not a valid IUPAC symbol";
891 throw motif_normalize_error(msg);
894 //std::cout << "valid_motif is: " << valid_motif << std::endl;
898 void Sequence::add_motif(const Sequence& a_motif)
900 std::vector<int> motif_starts = find_motif(a_motif);
902 for(std::vector<int>::iterator motif_start_i = motif_starts.begin();
903 motif_start_i != motif_starts.end();
906 motif_list.push_back(motif(*motif_start_i, a_motif.get_sequence()));
910 void Sequence::clear_motifs()
915 const std::list<motif>& Sequence::motifs() const
921 Sequence::find_motif(const std::string& a_motif) const
923 std::vector<int> motif_match_starts;
924 std::string norm_motif_rc;
926 motif_match_starts.clear();
928 //std::cout << "motif is: " << a_motif << std::endl;
929 std::string norm_motif = motif_normalize(a_motif);
930 //std::cout << "motif is: " << a_motif << std::endl;
932 if (norm_motif.size() > 0)
934 //std::cout << "Sequence: none blank motif\n";
935 motif_scan(norm_motif, &motif_match_starts);
937 norm_motif_rc = rc_motif(a_motif);
938 // make sure not to do search again if it is a palindrome
939 if (norm_motif_rc != norm_motif) {
940 motif_scan(norm_motif_rc, &motif_match_starts);
943 return motif_match_starts;
947 Sequence::find_motif(const Sequence& a_motif) const
949 return find_motif(a_motif.get_sequence());
953 Sequence::motif_scan(std::string a_motif, std::vector<int> * motif_match_starts) const
955 // if there's no sequence we can't scan for it?
956 // should this throw an exception?
959 std::string::const_iterator seq_c = seq->begin();
960 std::string::size_type seq_i;
961 int motif_i, motif_len;
963 //std::cout << "Sequence: motif, seq len = " << sequence.length() << std::endl;
964 motif_len = a_motif.length();
966 //std::cout << "motif_length: " << motif_len << std::endl;
967 //std::cout << "RAAARRRRR\n";
971 //std::cout << "motif: " << a_motif << std::endl;
973 //std::cout << "Sequence: motif, length= " << length << std::endl;
975 while (seq_i < length())
977 //std::cout << seq_c[seq_i];
978 //std::cout << seq_c[seq_i] << "?" << a_motif[motif_i] << ":" << motif_i << " ";
979 // this is pretty much a straight translation of Nora's python code
980 // to match iupac letter codes
981 if (a_motif[motif_i] =='N')
983 else if (a_motif[motif_i] == seq_c[seq_i])
985 else if ((a_motif[motif_i] =='M') &&
986 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='C')))
988 else if ((a_motif[motif_i] =='R') &&
989 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='G')))
991 else if ((a_motif[motif_i] =='W') &&
992 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='T')))
994 else if ((a_motif[motif_i] =='S') &&
995 ((seq_c[seq_i]=='C') || (seq_c[seq_i]=='G')))
997 else if ((a_motif[motif_i] =='Y') &&
998 ((seq_c[seq_i]=='C') || (seq_c[seq_i]=='T')))
1000 else if ((a_motif[motif_i] =='K') &&
1001 ((seq_c[seq_i]=='G') || (seq_c[seq_i]=='T')))
1003 else if ((a_motif[motif_i] =='V') &&
1004 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='C') ||
1005 (seq_c[seq_i]=='G')))
1007 else if ((a_motif[seq_i] =='H') &&
1008 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='C') ||
1009 (seq_c[seq_i]=='T')))
1011 else if ((a_motif[motif_i] =='D') &&
1012 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='G') ||
1013 (seq_c[seq_i]=='T')))
1015 else if ((a_motif[motif_i] =='B') &&
1016 ((seq_c[seq_i]=='C') || (seq_c[seq_i]=='G') ||
1017 (seq_c[seq_i]=='T')))
1026 // end Nora stuff, now we see if a match is found this pass
1027 if (motif_i == motif_len)
1029 //std::cout << "!!";
1031 motif_match_starts->push_back(seq_i - motif_len + 1);
1037 //std::cout << std::endl;
1040 void Sequence::add_string_annotation(std::string a_seq,
1043 std::vector<int> seq_starts = find_motif(a_seq);
1045 //std::cout << "searching for " << a_seq << " found " << seq_starts.size() << std::endl;
1047 for(std::vector<int>::iterator seq_start_i = seq_starts.begin();
1048 seq_start_i != seq_starts.end();
1051 annots.push_back(annot(*seq_start_i,
1052 *seq_start_i+a_seq.size(),
1058 void Sequence::find_sequences(std::list<Sequence>::iterator start,
1059 std::list<Sequence>::iterator end)
1061 while (start != end) {
1062 add_string_annotation(start->get_sequence(), start->get_fasta_header());
1068 std::ostream& operator<<(std::ostream& out, const Sequence& s)
1070 for(Sequence::const_iterator s_i = s.begin(); s_i != s.end(); ++s_i) {
1076 bool operator<(const Sequence& x, const Sequence& y)
1078 Sequence::const_iterator x_i = x.begin();
1079 Sequence::const_iterator y_i = y.begin();
1080 // for sequences there's some computation associated with computing .end
1081 // so lets cache it.
1082 Sequence::const_iterator xend = x.end();
1083 Sequence::const_iterator yend = y.end();
1085 if( x_i == xend and y_i == yend ) {
1087 } else if ( x_i == xend ) {
1089 } else if ( y_i == yend ) {
1091 } else if ( (*x_i) < (*y_i)) {
1093 } else if ( (*x_i) > (*y_i) ) {
1102 bool operator==(const Sequence& x, const Sequence& y)
1104 if (x.empty() and y.empty()) {
1105 // if there's no sequence in either sequence structure, they're equal
1107 } else if (x.empty() or y.empty()) {
1108 // if we fail the first test, and we discover one is empty,
1109 // we know they can't be equal. (and we need to do this
1110 // to prevent dereferencing an empty pointer)
1112 } else if (x.seq_count != y.seq_count) {
1113 // if they're of different lenghts, they're not equal
1116 Sequence::const_iterator xseq_i = x.begin();
1117 Sequence::const_iterator yseq_i = y.begin();
1118 // since the length of the two sequences is equal, we only need to
1120 for(; xseq_i != x.end(); ++xseq_i, ++yseq_i) {
1121 if (*xseq_i != *yseq_i) {