keep track of a subsequences parent
authorDiane Trout <diane@caltech.edu>
Fri, 15 Sep 2006 18:08:09 +0000 (18:08 +0000)
committerDiane Trout <diane@caltech.edu>
Fri, 15 Sep 2006 18:08:09 +0000 (18:08 +0000)
fixes ticket:129
I needed to know the parent offset inorder to get the start/stop sizes
to be relative to its parent in a "sequence" tree.

alg/sequence.cpp
alg/sequence.hpp
alg/test/test_sequence.cpp

index 0be66593c303e6191a7eea87ddd6272472a11445..92a7edca08fd7d45ed36905b86def2121b330f23 100644 (file)
@@ -83,7 +83,8 @@ const std::string Sequence::nucleic_iupac_alphabet("AaCcGgTtUuRrYyMmKkSsWwBbDdHh
 const std::string Sequence::protein_alphabet("AaCcDdEeFfGgHhIiKkLlMmNnPpQqRrSsTtVvWwYy\012\015");
 
 Sequence::Sequence()
-  : seq_start(0),
+  : parent(0),
+    seq_start(0),
     seq_count(0),
     strand(UnknownStrand)
 {
@@ -94,7 +95,8 @@ Sequence::~Sequence()
 }
 
 Sequence::Sequence(const char *seq)
-  : seq_start(0),
+  : parent(0),
+    seq_start(0),
     seq_count(0),
     strand(UnknownStrand),
     header(""),
@@ -104,7 +106,8 @@ Sequence::Sequence(const char *seq)
 }
 
 Sequence::Sequence(const std::string& seq) 
-  : seq_start(0),
+  : parent(0),
+    seq_start(0),
     seq_count(0),
     strand(UnknownStrand),
     header(""),
@@ -114,7 +117,8 @@ Sequence::Sequence(const std::string& seq)
 }
 
 Sequence::Sequence(const Sequence& o)
-  : seq(o.seq),
+  : parent(o.parent),
+    seq(o.seq),
     seq_start(o.seq_start),
     seq_count(o.seq_count),
     strand(o.strand),
@@ -128,6 +132,7 @@ Sequence::Sequence(const Sequence& o)
 Sequence &Sequence::operator=(const Sequence& s)
 {
   if (this != &s) {
+    parent = s.parent;
     seq = s.seq;
     seq_start = s.seq_start;
     seq_count = s.seq_count;
@@ -284,6 +289,7 @@ void Sequence::set_filtered_sequence(const std::string &old_seq,
   {
     new_seq->append(1, conversionTable[ (int)old_seq[seq_index+start]]);
   }
+  parent = 0;
   seq = new_seq;
   seq_start = 0;
   seq_count = count;
@@ -468,7 +474,7 @@ const std::list<annot>& Sequence::annotations() const
 }
 
 Sequence
-Sequence::subseq(int start, int count) const
+Sequence::subseq(int start, int count)
 {
   if (!seq) {
     Sequence new_seq;
@@ -480,7 +486,8 @@ Sequence::subseq(int start, int count) const
     count = size()-start;
   }
   Sequence new_seq(*this);
-  new_seq.seq_start = start;
+  new_seq.parent = this;
+  new_seq.seq_start = seq_start+start;
   new_seq.seq_count = count;
 
   new_seq.motif_list = motif_list;
@@ -613,6 +620,7 @@ Sequence::const_reference Sequence::at(Sequence::size_type i) const
 void
 Sequence::clear()
 {
+  parent = 0;
   seq.reset();
   seq_start = 0;
   seq_count = 0;
@@ -655,12 +663,15 @@ bool Sequence::empty() const
 
 Sequence::size_type Sequence::start() const
 {
-  return seq_start;
+  if (parent)
+    return seq_start - parent->start();
+  else
+    return seq_start;
 }
 
 Sequence::size_type Sequence::stop() const
 {
-  return seq_start + seq_count;
+  return start() + seq_count;
 }
 
 Sequence::size_type Sequence::size() const
index 4a99e708be9e0767ef76e0af2e05f8aed4be44db..5010729243fc8b5caae76424419789db81c0b866 100644 (file)
@@ -163,7 +163,7 @@ public:
   size_type stop() const;
 
   //! return a subsequence, copying over any appropriate annotation
-  Sequence subseq(int start=0, int count = std::string::npos) const;
+  Sequence subseq(int start=0, int count = std::string::npos);
   //! return a reverse compliment (this needs to be improved?)
   std::string rev_comp() const;
 
@@ -229,6 +229,8 @@ public:
   void load_museq(boost::filesystem::path load_file_path, int seq_num); 
   
 private:
+  //! parent sequence
+  Sequence *parent;
   //! hold a shared pointer to our sequence string
   boost::shared_ptr<seq_string> seq;
   //! start offset into the sequence
@@ -256,6 +258,7 @@ private:
   friend class boost::serialization::access;
   template<class Archive>
   void serialize(Archive& ar, const unsigned int /*version*/) {
+    ar & BOOST_SERIALIZATION_NVP(parent);
     ar & BOOST_SERIALIZATION_NVP(seq);
     ar & BOOST_SERIALIZATION_NVP(seq_start);
     ar & BOOST_SERIALIZATION_NVP(seq_count);
index e8b35ad823d4e3ca96542db6ff0f1a2cce65bcd2..3a2b9db428b8419b809b46c6315a3cd2c10e89cf 100644 (file)
@@ -111,12 +111,8 @@ BOOST_AUTO_TEST_CASE( sequence_start_stop )
   BOOST_CHECK_EQUAL( s3.size(), 3);
   BOOST_CHECK_EQUAL( s3, s3seq_string);
   
-  // FIXME: updated for ticket:129
-  // once I have a solution for ticket:129 the commented
-  // out version should work.
-  //std::string s4seq_string = s3seq_string.substr(1,1);
-  std::string s4seq_string = seq_string.substr(1,1);
-  Sequence s4 = s2.subseq(1,1);
+  std::string s4seq_string = s3seq_string.substr(1,1);
+  Sequence s4 = s3.subseq(1,1);
   BOOST_CHECK_EQUAL( s4.start(), 1 );
   BOOST_CHECK_EQUAL( s4.stop(), 1+1);
   BOOST_CHECK_EQUAL( s4.size(), 1);