Figured out how to serialize a shared_ptr<string>
[mussa.git] / alg / sequence.hpp
1 #ifndef _MUSSA_SEQUENCE_H_
2 #define _MUSSA_SEQUENCE_H_
3 //  This file is part of the Mussa source distribution.
4 //  http://mussa.caltech.edu/
5 //  Contact author: Tristan  De Buysscher, tristan@caltech.edu
6
7 // This program and all associated source code files are Copyright (C) 2005
8 // the California Institute of Technology, Pasadena, CA, 91125 USA.  It is
9 // under the GNU Public License; please see the included LICENSE.txt
10 // file for more information, or contact Tristan directly.
11
12
13 //                        ----------------------------------------
14 //                           ---------- sequence.hh -----------
15 //                        ----------------------------------------
16
17 #include <boost/filesystem/path.hpp>
18 #include <boost/filesystem/fstream.hpp>
19
20 #include <boost/serialization/base_object.hpp>
21 #include <boost/serialization/export.hpp>
22 #include <boost/serialization/list.hpp>
23 #include <boost/serialization/nvp.hpp>
24 #include <boost/serialization/string.hpp>
25 #include <boost/serialization/shared_ptr.hpp>
26 #include <boost/serialization/utility.hpp>
27 #include <boost/serialization/version.hpp>
28 #include <boost/serialization/vector.hpp>
29
30 #include <boost/shared_ptr.hpp>
31
32 #include <iostream>
33
34 // Sequence data class
35
36 //! Attach annotation information to a sequence track
37 struct annot
38 {
39   annot();
40   annot(int begin, int end, std::string type, std::string name);
41   ~annot();
42   
43   int begin;
44   int end;
45   std::string type;
46   std::string name;
47
48   friend bool operator==(const annot& left, const annot& right);
49 private:
50   // boost::serialization support
51   friend class boost::serialization::access;
52   template<class Archive>
53   void serialize(Archive& ar, const unsigned int /*version*/) {
54     ar & BOOST_SERIALIZATION_NVP(begin);
55     ar & BOOST_SERIALIZATION_NVP(end);
56     ar & BOOST_SERIALIZATION_NVP(type);
57     ar & BOOST_SERIALIZATION_NVP(name);
58   }
59 };
60 BOOST_CLASS_EXPORT(annot);
61
62
63 /* The way that motifs are found currently doesn't really 
64  * indicate that the match was a reverse compliment
65  */
66 struct motif : public annot
67 {
68   std::string sequence;
69
70   motif() :  annot(), sequence("") {};
71   //! this constructor is for when we're adding motifs to our annotations
72   motif(int begin, std::string motif);
73   ~motif();
74
75   // boost::serialization support
76 private:
77   friend class boost::serialization::access;
78   template<class Archive>
79   void serialize(Archive& ar, const unsigned int /*version*/) {
80     ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(annot);
81     ar & BOOST_SERIALIZATION_NVP(sequence);
82   }
83 };
84 BOOST_CLASS_EXPORT(motif);
85
86 //! the only purpose of this class is that the shared_ptr template 
87 //! functions need the serialization support to be in-class.
88 class seq_string : public std::string
89 {
90 private:
91   friend class boost::serialization::access;
92   template<class Archive>
93   void serialize(Archive& ar, const unsigned int /*version*/) {
94     //ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(std::string);
95     ar & boost::serialization::make_nvp("bases",
96            boost::serialization::base_object<std::string>(*this)
97          );
98   }
99 };
100
101 //! sequence track for mussa.
102 class Sequence 
103 {
104 public:
105   typedef std::string::difference_type difference_type;
106   typedef std::string::iterator iterator;
107   typedef std::string::const_iterator const_iterator;
108   typedef std::string::reference reference;
109   typedef std::string::const_reference const_reference;
110   typedef std::string::size_type size_type;
111   static const size_type npos = std::string::npos;
112
113   // some standard dna alphabets 
114   // Include nl (\012), and cr (\015) to make sequence parsing eol 
115   // convention independent.
116
117   static const std::string dna_alphabet;
118   static const std::string rna_alphabet;
119   //! this is the general iupac alphabet for nucleotides
120   static const std::string nucleic_iupac_alphabet;
121   //! the protein alphabet
122   static const std::string protein_alphabet;
123
124   Sequence();
125   ~Sequence();
126   Sequence(const char* seq);
127   Sequence(const std::string& seq);
128   Sequence(const Sequence& seq);
129   //! assignment to constant sequences
130   Sequence &operator=(const Sequence&);
131
132   // dangerous since they create large copies
133   //operator std::string();
134   //operator std::string() const;
135
136   friend std::ostream& operator<<(std::ostream&, const Sequence&);
137   friend bool operator<(const Sequence&, const Sequence&);
138   friend bool operator==(const Sequence&, const Sequence&);
139   const_reference operator[](size_type) const;
140
141   //! set sequence to a (sub)string containing nothing but AGCTN
142   void set_filtered_sequence(const std::string& seq, 
143                              std::string::size_type start=0, 
144                              std::string::size_type count=0);
145
146   //! retrive element at specific position
147   const_reference at(size_type n) const;
148   //! clear the sequence and its annotations
149   void clear();
150   //! return c pointer to the sequence data
151   const char *c_str() const;
152   //! forward iterator
153   const_iterator begin() const;
154   //! last iterator
155   const_iterator end() const;
156   //! is our sequence empty?
157   bool empty() const;
158   //! how many base pairs are there in our sequence
159   size_type size() const;
160   //! alias for size (used by string)
161   size_type length() const;
162
163   //! return a subsequence, copying over any appropriate annotation
164   Sequence subseq(int start=0, int count = std::string::npos) const;
165   //! return a reverse compliment
166   std::string rev_comp() const;
167
168   //! set sequence (filtered)
169   void set_sequence(const std::string &);
170   //! get sequence
171   std::string get_sequence() const;
172   //! set species name
173   void set_species(const std::string &);
174   //! get species name
175   std::string get_species() const;
176   //! set the fasta header
177   void set_fasta_header(std::string header);
178   //! get the fasta header
179   std::string get_fasta_header() const;
180   //! get name (will return the first non-empty, of fasta_header, species)
181   std::string get_name() const;
182   
183   //! load sequence AGCT from fasta file
184   //! \throw mussa_load_error
185   //! \throw sequence_empty_error
186   //! \throw sequence_empty_file_error
187   void load_fasta(const boost::filesystem::path file_path, int seq_num=1, 
188                   int start_index=0, int end_index=0);
189   //! load sequence from stream
190   //! \throw mussa_load_error
191   //! \throw sequence_empty_error
192   //! \throw sequence_empty_file_error
193   void load_fasta(std::iostream& file, int seq_num=1, 
194                   int start_index=0, int end_index=0);
195   //! load sequence annotations
196   //! \throws mussa_load_error 
197   void load_annot(const boost::filesystem::path file_path, int start_index, int end_index);
198   //! load sequence annotations
199   //! \throws mussa_load_error 
200   void load_annot(std::fstream& data_stream, int start_index, int end_index);
201   bool parse_annot(std::string data, int start_index=0, int end_index=0);
202   //! add an annotation to our list of annotations
203   void add_annotation(const annot& a);
204   const std::list<annot>& annotations() const;
205   const std::list<motif>& motifs() const;
206
207   //! add a motif to our list of motifs
208   //! \throws motif_normalize_error if there's something wrong with a_motif
209   void add_motif(const Sequence& a_motif);
210   //! clear our list of found motifs
211   void clear_motifs();
212   //! search a sequence for a_motif
213   //! \throws motif_normalize_error if there's something wrong with a_motif
214   std::vector<int> find_motif(const std::string& a_motif) const;
215   //! search a sequence for a_motif
216   //! \throws motif_normalize_error if there's something wrong with a_motif
217   std::vector<int> find_motif(const Sequence& a_motif) const;
218   //! convert IUPAC symbols to upperase
219   //! \throws motif_normalize_error if there is an invalid symbol
220   static std::string motif_normalize(const std::string& a_motif);
221   
222   //! annotate the current sequence with other sequences
223   void find_sequences(std::list<Sequence>::iterator start, 
224                       std::list<Sequence>::iterator end);
225   
226   void save(boost::filesystem::fstream &save_file);
227   void load_museq(boost::filesystem::path load_file_path, int seq_num); 
228   
229 private:
230   boost::shared_ptr<seq_string> seq;
231   std::string header;
232   std::string species;
233
234   std::list<annot> annots;
235   //! a seperate list for motifs since we're currently not saving them
236   std::list<motif> motif_list;
237
238   void motif_scan(std::string a_motif, std::vector<int> * motif_match_starts) const;
239   std::string rc_motif(std::string a_motif) const;
240   //! look for a string sequence type and and it to an annotation list
241   void add_string_annotation(std::string a_seq, std::string name);
242   
243   // boost::serialization support
244   friend class boost::serialization::access;
245   template<class Archive>
246   void serialize(Archive& ar, const unsigned int /*version*/) {
247     ar & BOOST_SERIALIZATION_NVP(seq);
248     ar & BOOST_SERIALIZATION_NVP(header);
249     ar & BOOST_SERIALIZATION_NVP(species);
250     ar & BOOST_SERIALIZATION_NVP(annots);
251     ar & BOOST_SERIALIZATION_NVP(motif_list);
252   }
253 };
254 BOOST_CLASS_EXPORT(Sequence);
255
256 #endif