test annotating a sequence with fasta records in a stream
[mussa.git] / alg / sequence.hpp
index 4b93ef132d81b93e924e153965407b32585c7851..fc8a0cfe7b2eea9d2690f5adfdc9af5b0b175544 100644 (file)
 
 // Sequence data class
 
-//! Attach annotation information to a sequence track
-struct annot
+/* The way that motifs are found currently doesn't really 
+ * indicate that the match was a reverse compliment
+ */
+struct motif
 {
-  annot();
-  annot(int begin, int end, std::string type, std::string name);
-  ~annot();
-  
+  motif();
+  //motif(int begin, int end, std::string type, std::string name);
+  //! this constructor is for when we're adding motifs to our annotations
+  motif(int begin, std::string motif);
+  ~motif();
+
   int begin;
   int end;
   std::string type;
   std::string name;
+  std::string sequence;
+
+  friend bool operator==(const motif& left, const motif& right);
 
-  friend bool operator==(const annot& left, const annot& right);
-private:
   // boost::serialization support
+private:
   friend class boost::serialization::access;
   template<class Archive>
   void serialize(Archive& ar, const unsigned int /*version*/) {
@@ -60,34 +66,13 @@ private:
     ar & BOOST_SERIALIZATION_NVP(end);
     ar & BOOST_SERIALIZATION_NVP(type);
     ar & BOOST_SERIALIZATION_NVP(name);
-  }
-};
-BOOST_CLASS_EXPORT(annot);
-
-
-/* The way that motifs are found currently doesn't really 
- * indicate that the match was a reverse compliment
- */
-struct motif : public annot
-{
-  std::string sequence;
-
-  motif() :  annot(), sequence("") {};
-  //! this constructor is for when we're adding motifs to our annotations
-  motif(int begin, std::string motif);
-  ~motif();
-
-  // boost::serialization support
-private:
-  friend class boost::serialization::access;
-  template<class Archive>
-  void serialize(Archive& ar, const unsigned int /*version*/) {
-    ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(annot);
     ar & BOOST_SERIALIZATION_NVP(sequence);
   }
 };
 BOOST_CLASS_EXPORT(motif);
 
+class Sequence;
+typedef boost::shared_ptr<Sequence> SequenceRef;
 
 //! sequence track for mussa.
 class Sequence 
@@ -103,16 +88,24 @@ public:
   typedef SeqString::const_reference const_reference;
   typedef SeqString::size_type size_type;
   static const size_type npos = SeqString::npos;
-  enum strand_type { UnknownStrand, PlusStrand, MinusStrand, BothStrand };
+  
+  typedef std::list<motif> MotifList;
+  typedef boost::shared_ptr<MotifList> MotifListRef;
                       
-  Sequence(AlphabetRef a = reduced_nucleic_alphabet);
+  Sequence(AlphabetRef a = reduced_dna_alphabet);
   Sequence(const char* seq, 
-           AlphabetRef a = reduced_nucleic_alphabet);
+           AlphabetRef a = reduced_dna_alphabet,
+           SeqSpan::strand_type strand = SeqSpan::PlusStrand);
   Sequence(const std::string& seq,
-           AlphabetRef a = reduced_nucleic_alphabet);
+           AlphabetRef a = reduced_dna_alphabet,
+           SeqSpan::strand_type strand = SeqSpan::PlusStrand);
+  //! make a new sequence, with the same SeqSpan
   Sequence(const Sequence& seq);
+  //! make a new sequence, with the same SeqSpan
   Sequence(const Sequence *);
-  Sequence(const SeqSpanRef&); 
+  //! Make a new sequence using a copy of SeqSpan
+  Sequence(const SequenceRef); 
+  Sequence(const SeqSpanRef&);
   ~Sequence();
   //! assignment to constant sequences
   Sequence &operator=(const Sequence&);
@@ -125,10 +118,10 @@ public:
 
   //! set sequence to a (sub)string containing nothing but AGCTN
   void set_filtered_sequence(const std::string& seq,
-                             AlphabetRef a, 
+                             AlphabetRef a=reduced_dna_alphabet
                              size_type start=0,
                              size_type count=npos,
-                             strand_type strand=UnknownStrand);
+                             SeqSpan::strand_type strand=SeqSpan::PlusStrand);
 
   //! retrive element at specific position
   const_reference at(size_type i) const { return seq->at(i); }
@@ -159,7 +152,9 @@ public:
   size_type stop() const { return seq->parentStop(); }
 
   //! return a subsequence, copying over any appropriate annotation
-  Sequence subseq(size_type start=0, size_type count = npos) const;
+  Sequence subseq(size_type start=0, 
+                  size_type count = npos,
+                  SeqSpan::strand_type strand = SeqSpan::SameStrand) const;
   //! reverse a character
   std::string create_reverse_map() const;
   //! return a reverse compliment (this needs to be improved?)
@@ -208,15 +203,20 @@ public:
   void load_annot(const boost::filesystem::path file_path, int start_index, int end_index);
   //! load sequence annotations
   //! \throws mussa_load_error 
-  void load_annot(std::fstream& data_stream, int start_index, int end_index);
+  void load_annot(std::istream& data_stream, int start_index, int end_index);
   //! parse annotation file
   /*! \throws annotation_load_error 
    */
   void parse_annot(std::string data, int start_index=0, int end_index=0);
   //! add an annotation to our list of annotations
-  void add_annotation(const annot& a);
-  const std::list<annot>& annotations() const;
-  const std::list<motif>& motifs() const;
+  void add_annotation(const SeqSpanRef a);
+  //! add an annotation using tristan's mussa file paramenters
+  void add_annotation(std::string name, std::string type, size_type start, size_type stop);
+  //! create an initialized annotation with the "standard" types.
+  SeqSpanRef make_annotation(std::string name, std::string type, size_type start, size_type stop) const;
+  const SeqSpanRefList& annotations() const;
+  
+  const MotifList& motifs() const;
 
   //! add a motif to our list of motifs
   void add_motif(const Sequence& a_motif);
@@ -228,23 +228,23 @@ public:
   //! annotate the current sequence with other sequences
   void find_sequences(std::list<Sequence>::iterator start, 
                      std::list<Sequence>::iterator end);
+  SeqSpanRef seqspan() { return seq; }
   
   void save(boost::filesystem::fstream &save_file);
-  void load_museq(boost::filesystem::path load_file_path, int seq_num);
+  //void load_museq(boost::filesystem::path load_file_path, int seq_num);
+  static SequenceRef load_museq(boost::filesystem::fstream& load_file);
   
 protected:  
   SeqSpanRef seq;
-  //! strand orientation
-  strand_type strand;
   //! fasta header
   std::string header;
   //! species name
   std::string species;
 
-  //! store our oldstyle annotations
-  std::list<annot> annots;
+  //! store annotation regions
+  SeqSpanRefListRef annotation_list;
   //! a seperate list for motifs since we're currently not saving them
-  std::list<motif> motif_list;
+  MotifListRef motif_list;
 
   //! copy over all our annotation children
   void copy_children(Sequence &, size_type start, size_type count) const;
@@ -259,10 +259,9 @@ protected:
   template<class Archive>
   void serialize(Archive& ar, const unsigned int /*version*/) {
     ar & BOOST_SERIALIZATION_NVP(seq);
-    ar & BOOST_SERIALIZATION_NVP(strand);
     ar & BOOST_SERIALIZATION_NVP(header);
     ar & BOOST_SERIALIZATION_NVP(species);
-    ar & BOOST_SERIALIZATION_NVP(annots);
+    ar & BOOST_SERIALIZATION_NVP(annotation_list);
     ar & BOOST_SERIALIZATION_NVP(motif_list);
   }
 };