make Sequence a subclass of std::string
[mussa.git] / alg / sequence.cpp
index 1c4d2a62b0c3e878cbd6c730261d660576b144a5..077908e1c3e2ad414f32ee24e74b566cd4774228 100644 (file)
@@ -62,14 +62,30 @@ annot::annot(int start, int end, std::string type, std::string name)
 {
 }
 
+annot::~annot()
+{
+}
+
+bool operator==(const annot& left, const annot& right)
+{
+  return ((left.start == right.start) and
+          (left.end == right.end) and
+          (left.type == right.type) and
+          (left.name == right.name));
+}
+
 motif::motif(int start, std::string motif)
  : annot(start, start+motif.size(), "motif", motif),
    sequence(motif)
 {
 }
 
+motif::~motif()
+{
+}
+
 Sequence::Sequence()
- :  sequence(""),
+ :  std::string(),
     header(""),
     species("")
 {
@@ -77,17 +93,32 @@ Sequence::Sequence()
   motif_list.clear();
 }
 
-Sequence::Sequence(std::string seq) 
- :  header(""),
+Sequence::~Sequence()
+{
+}
+
+Sequence::Sequence(const std::string& seq) 
+ :  std::string(),
+    header(""),
     species("")
 {
   set_filtered_sequence(seq);
 }
 
+Sequence::Sequence(const Sequence& o)
+  : std::string(o),
+    header(o.header),
+    species(o.species),
+    annots(o.annots),
+    motif_list(o.motif_list)
+{
+}
+
 Sequence &Sequence::operator=(const Sequence& s)
 {
   if (this != &s) {
-    sequence = s.sequence;
+    //sequence = s.sequence;
+    assign(s);
     header = s.header;
     species = s.species;
     annots = s.annots;
@@ -101,17 +132,6 @@ Sequence &Sequence::operator=(const std::string& s)
   return *this;
 }
 
-char Sequence::operator[](int index) const
-{
-  return sequence[index];
-}
-
-std::ostream& operator<<(std::ostream& out, const Sequence& seq)
-{
-  out << "Sequence(" << seq.get_seq() << ")";
-  return out;
-}
-
 static void multiplatform_getline(std::istream& in, std::string& line)
 {
   line.clear();
@@ -126,7 +146,6 @@ static void multiplatform_getline(std::istream& in, std::string& line)
   if (c=='\012' or c == '\015') {
     in.get();
   }
 }
 
 //! load a fasta file into a sequence
@@ -227,8 +246,8 @@ void Sequence::set_filtered_sequence(const std::string &old_seq,
 
   if ( count == 0)
     count = old_seq.size() - start;
-  sequence.clear();
-  sequence.reserve(count);
+  std::string::clear();
+  reserve(count);
 
   // Make a conversion table
 
@@ -254,7 +273,7 @@ void Sequence::set_filtered_sequence(const std::string &old_seq,
   // finally, the actual conversion loop
   for(std::string::size_type seq_index = 0; seq_index < count; seq_index++)
   {
-    sequence += conversionTable[ (int)old_seq[seq_index+start]];
+    append(1, conversionTable[ (int)old_seq[seq_index+start]]);
   }
 }
 
@@ -409,94 +428,14 @@ Sequence::parse_annot(std::string data, int start_index, int end_index)
   find_sequences(query_seqs.begin(), query_seqs.end());
 }
 
-/*
-void
-Sequence::load_annot(std::istream& data_stream, int start_index, int end_index)
-{
-  std::string file_data_line;
-  annot an_annot;
-  std::string::size_type space_split_i;
-  std::string annot_value;
-  std::list<annot>::iterator list_i;
-  std::string err_msg;
-
-  annots.clear();
-
-  getline(data_stream,file_data_line);
-  species = file_data_line;
-
-  // end_index = 0 means no end was specified, so cut to the end 
-  if (end_index == 0)
-    end_index = sequence.length();
-
-  //std::cout << "START: " << start_index << " END: " << end_index << std::endl;
-
-  while ( !data_stream.eof() )
-  {
-    getline(data_stream,file_data_line);
-    if (file_data_line != "")
-    {
-      // need to get 4 values...almost same code 4 times...
-      // get annot start index
-      space_split_i = file_data_line.find(" ");
-      annot_value = file_data_line.substr(0,space_split_i);
-      an_annot.start = atoi (annot_value.c_str());
-      file_data_line = file_data_line.substr(space_split_i+1);
-      // get annot end index
-      space_split_i = file_data_line.find(" ");
-      annot_value = file_data_line.substr(0,space_split_i);
-      an_annot.end = atoi (annot_value.c_str());
-      file_data_line = file_data_line.substr(space_split_i+1);
-
-      //std::cout << "seq, annots: " << an_annot.start << ", " << an_annot.end
-      //     << std::endl;
-
-      // get annot name
-      space_split_i = file_data_line.find(" ");
-      if (space_split_i == std::string::npos)  // no entries for name & type
-      {
-        std::cout << "seq, annots - no name or type\n";
-        an_annot.name = "";
-        an_annot.type = "";
-      }
-      else
-      {
-        annot_value = file_data_line.substr(0,space_split_i);
-        an_annot.name = annot_value;
-        file_data_line = file_data_line.substr(space_split_i+1);
-        // get annot type
-        space_split_i = file_data_line.find(" ");
-        if (space_split_i == std::string::npos)  // no entry for type
-          an_annot.type = "";
-        else
-        {
-          annot_value = file_data_line.substr(0,space_split_i);
-          an_annot.type = annot_value;
-        }
-      }
-
-
-      // add annot to list if it falls within the range of sequence specified
-      if ((start_index <= an_annot.start) && (end_index >= an_annot.end)) 
-      {
-        an_annot.start -= start_index;
-        an_annot.end -= start_index;
-        annots.push_back(an_annot);
-      }
-      // else no (or bogus) annotations
-    }
-  }
-}
-*/
-
 const std::string& Sequence::get_species() const
 {
   return species;
 }
 
-bool Sequence::empty() const
+void Sequence::add_annotation(const annot& a)
 {
-  return (size() == 0);
+  annots.push_back(a);
 }
 
 const std::list<annot>& Sequence::annotations() const
@@ -504,55 +443,47 @@ const std::list<annot>& Sequence::annotations() const
   return annots;
 }
 
-std::string::size_type Sequence::length() const
-{
-  return size();
-}
-
-std::string::size_type Sequence::size() const
-{
-  return sequence.size();
-}
-
-Sequence::iterator Sequence::begin()
-{
-  return sequence.begin();
-}
-
-Sequence::const_iterator Sequence::begin() const
-{
-  return sequence.begin();
-}
-
-Sequence::iterator Sequence::end()
-{
-  return sequence.end();
-}
-
-Sequence::const_iterator Sequence::end() const
+Sequence
+Sequence::subseq(int start, int count) const
 {
-  return sequence.end();
-}
-
+  // there might be an off by one error with start+count > size()
+  if ( count == npos || start+count > size()) {
+    count = size()-start;
+  }
+  Sequence new_seq(std::string::substr(start, count));
+  new_seq.set_header(get_header());
+  //new_seq.set_species(get_species());
 
-const std::string&
-Sequence::get_seq() const
-{
-  return sequence;
-}
+  new_seq.motif_list = motif_list;
+  // attempt to copy & reannotate position based annotations 
+  int end = start+count;
 
+  for(std::list<annot>::const_iterator annot_i = annots.begin();
+      annot_i != annots.end();
+      ++annot_i)
+  {
+    int annot_start = annot_i->start;
+    int annot_end = annot_i->end;
+
+    if (annot_start < end) {
+      if (annot_start >=start) {
+        annot_start -= start;
+      } else {
+        annot_start = 0;
+      }
 
-std::string
-Sequence::subseq(int start, int count) const
-{
-  return sequence.substr(start, count);
-}
+      if (annot_end < end) {
+        annot_end -= start;
+      } else {
+        annot_end = count;
+      }
 
+      annot new_annot(annot_start, annot_end, annot_i->type, annot_i->name);
+      new_seq.annots.push_back(new_annot);
+    }
+  }
 
-const char *
-Sequence::c_seq() const
-{
-  return sequence.c_str();
+  return new_seq;
 }
 
 std::string
@@ -562,7 +493,7 @@ Sequence::rev_comp() const
   char conversionTable[257];
   int seq_i, table_i, len;
 
-  len = sequence.length();
+  len = length();
   rev_comp.reserve(len);
   // make a conversion table
   // init all parts of conversion table to '~' character
@@ -588,7 +519,7 @@ Sequence::rev_comp() const
   // finally, the actual conversion loop
   for(seq_i = len - 1; seq_i >= 0; seq_i--)
   {
-    table_i = (int) sequence[seq_i];
+    table_i = (int) at(seq_i);
     rev_comp += conversionTable[table_i];
   }
 
@@ -614,13 +545,6 @@ Sequence::sp_name() const
 }
 */
 
-void
-Sequence::set_seq(const std::string& a_seq)
-{
-  set_filtered_sequence(a_seq);
-}
-
-
 /*
 std::string 
 Sequence::species()
@@ -632,7 +556,7 @@ Sequence::species()
 void
 Sequence::clear()
 {
-  sequence = "";
+  std::string::clear();
   header = "";
   species = "";
   annots.clear();
@@ -651,7 +575,7 @@ Sequence::save(fs::fstream &save_file)
   //save_file.open(save_file_path.c_str(), std::ios::app);
 
   save_file << "<Sequence>" << std::endl;
-  save_file << sequence << std::endl;
+  save_file << *this << std::endl;
   save_file << "</Sequence>" << std::endl;
 
   save_file << "<Annotations>" << std::endl;
@@ -687,7 +611,7 @@ Sequence::load_museq(fs::path load_file_path, int seq_num)
       seq_counter++;
   }
   getline(load_file, file_data_line);
-  sequence = file_data_line;
+  assign(file_data_line);
   getline(load_file, file_data_line);
   getline(load_file, file_data_line);
   if (file_data_line == "<Annotations>")
@@ -897,12 +821,12 @@ Sequence::find_motif(std::string a_motif)
 void
 Sequence::motif_scan(std::string a_motif, std::vector<int> * motif_match_starts)
 {
-  char * seq_c;
+   const char * seq_c;
   std::string::size_type seq_i;
   int motif_i, motif_len;
 
   // faster to loop thru the sequence as a old c std::string (ie char array)
-  seq_c = (char*)sequence.c_str();
+  seq_c = c_str();
   //std::cout << "Sequence: motif, seq len = " << sequence.length() << std::endl; 
   motif_len = a_motif.length();
 
@@ -915,7 +839,7 @@ Sequence::motif_scan(std::string a_motif, std::vector<int> * motif_match_starts)
 
   //std::cout << "Sequence: motif, length= " << length << std::endl;
   seq_i = 0;
-  while (seq_i < sequence.length())
+  while (seq_i < length())
   {
     //std::cout << seq_c[seq_i];
     //std::cout << seq_c[seq_i] << "?" << a_motif[motif_i] << ":" << motif_i << " ";
@@ -1002,7 +926,7 @@ void Sequence::find_sequences(std::list<Sequence>::iterator start,
                               std::list<Sequence>::iterator end)
 {
   while (start != end) {
-    add_string_annotation(start->get_seq(), start->get_header());
+    add_string_annotation(*start, start->get_header());
     ++start;
   }
 }