Move alphabet type into SeqString
[mussa.git] / alg / seq_span.hpp
1 #ifndef SEQ_SPAN_HPP_
2 #define SEQ_SPAN_HPP_
3
4 #include <string>
5
6 #include <boost/serialization/base_object.hpp>
7 #include <boost/serialization/export.hpp>
8 #include <boost/serialization/list.hpp>
9 #include <boost/serialization/nvp.hpp>
10 #include <boost/serialization/string.hpp>
11 #include <boost/serialization/shared_ptr.hpp>
12 #include <boost/serialization/utility.hpp>
13 #include <boost/serialization/version.hpp>
14 #include <boost/serialization/vector.hpp>
15
16 #include <boost/shared_ptr.hpp>
17 #include <boost/enable_shared_from_this.hpp>
18
19 //! These classes provide for the internal implementation for the Sequence class
20 #include "seq.hpp"
21
22 class SeqSpan;
23 typedef boost::shared_ptr<SeqSpan> SeqSpanRef;
24
25 //! Track what segment of a sequence we're looking at
26 class SeqSpan : public boost::enable_shared_from_this<SeqSpan> {
27 public:
28   typedef SeqString::difference_type difference_type;
29   typedef SeqString::iterator iterator;
30   typedef SeqString::reverse_iterator reverse_iterator;
31   typedef SeqString::const_iterator const_iterator;
32   typedef SeqString::const_reverse_iterator const_reverse_iterator;
33   typedef SeqString::reference reference;
34   typedef SeqString::const_reference const_reference;
35   typedef SeqString::size_type size_type;
36   typedef SeqString::value_type value_type;
37   static const size_type npos = SeqString::npos;
38
39 public:
40   SeqSpan(const SeqSpan &);
41   SeqSpan(const SeqSpan *);
42   explicit SeqSpan(const std::string &, 
43                AlphabetRef = reduced_nucleic_alphabet
44            );
45   SeqSpan(const SeqSpanRef, size_type start=0, size_type count=npos);
46
47   //! assignment
48   SeqSpan& operator=(const SeqSpan&);
49   //! output
50   friend std::ostream& operator<<(std::ostream&, const SeqSpan&);
51   //! equality 
52   friend bool operator==(const SeqSpan&, const SeqSpan&);
53   friend bool operator!=(const SeqSpan&, const SeqSpan&);
54     
55   const Alphabet& get_alphabet() const { return seq->get_alphabet(); }
56   //! \defgroup string_operators
57   //! @{
58   //! retrive element at specific position
59   const_reference at(size_type n) const;
60   //! retrieve element at specific location
61   const_reference operator[](SeqSpan::size_type i) const;
62   //! return c pointer to the sequence data
63   const char *data() const;
64   //! forward iterator
65   const_iterator begin() const;
66   //! last iterator
67   const_iterator end() const;
68   //! is our sequence empty?
69   bool empty() const;
70   //! find first 
71   size_type find_first_not_of(const std::string&, size_type index=0) const; 
72   //! how many base pairs are there in our sequence
73   size_type size() const { return seq_count; }
74   //! alias for size (used by string)
75   size_type length() const { return size(); }
76   //! reverse iterator
77   const_reverse_iterator rbegin() const;
78   //! reverse end iterator
79   const_reverse_iterator rend() const;
80   //! @}
81   
82   //! start position relative to root sequence
83   size_type start() const { return seq_start; }
84   //! set position relative to root sequence
85   void setStart(size_type);
86   //! one past the last position relative to the root sequence
87   size_type stop() const { return seq_start + seq_count; }
88   //! set one past the last position relative to the root sequence.
89   void setStop(size_type);
90   
91   //! get start position relative to the parent sequence
92   size_type parentStart() const;
93   //! set start position relative to parent sequence
94   void setParentStart(size_type);
95   //! get stop position relative to the parent sequence
96   size_type parentStop() const;
97   //! set stop position relative to parent sequence
98   void setParentStop(size_type);
99   size_type parentSize() const { return (parent) ? parent->size() : size(); }
100   
101
102   //! return a subsequence, copying over any appropriate annotation
103   SeqSpanRef subseq(size_type start=0, size_type count = std::string::npos);
104   //! get sequence
105   std::string sequence() const; 
106   //! are both sequences derived from the same sequence tree?
107   static bool isFamily(const SeqSpan& a, const SeqSpan& b);
108   
109   friend class Sequence;
110 private:
111   //! do not statically initialize, only create with new
112   SeqSpan() {};
113 protected:
114   //! keep a reference to the sequence container 
115   SeqStringRef seq;
116   //! where our start location is in the full sequence
117   size_type seq_start;
118   //! how big we ware
119   size_type seq_count;  
120   // Do I need to track the strand here?
121   
122   //! keep a reference to who our parent span is 
123   SeqSpanRef parent;
124
125   // boost::serialization support
126   friend class boost::serialization::access;
127   template<class Archive>
128   void serialize(Archive& ar, const unsigned int /*version*/) {
129     ar & BOOST_SERIALIZATION_NVP(seq);
130     ar & BOOST_SERIALIZATION_NVP(seq_start);
131     ar & BOOST_SERIALIZATION_NVP(seq_count);
132     ar & BOOST_SERIALIZATION_NVP(parent);
133   }
134 };
135 #endif /*SEQ_SPAN_HPP_*/