go back to sequence being its own class
[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/list.hpp>
22 #include <boost/serialization/nvp.hpp>
23 #include <boost/serialization/string.hpp>
24 #include <boost/serialization/utility.hpp>
25 #include <boost/serialization/export.hpp>
26
27 #include <list>
28 #include <string>
29 #include <vector>
30 #include <iostream>
31
32 // Sequence data class
33
34 //! Attach annotation information to a sequence track
35 struct annot
36 {
37   annot();
38   annot(int begin, int end, std::string type, std::string name);
39   ~annot();
40   
41   int begin;
42   int end;
43   std::string type;
44   std::string name;
45
46   friend bool operator==(const annot& left, const annot& right);
47 private:
48   // boost::serialization support
49   friend class boost::serialization::access;
50   template<class Archive>
51   void serialize(Archive& ar, const unsigned int /*version*/) {
52     ar & BOOST_SERIALIZATION_NVP(begin);
53     ar & BOOST_SERIALIZATION_NVP(end);
54     ar & BOOST_SERIALIZATION_NVP(type);
55     ar & BOOST_SERIALIZATION_NVP(name);
56   }
57 };
58 BOOST_CLASS_EXPORT(annot);
59
60
61 /* The way that motifs are found currently doesn't really 
62  * indicate that the match was a reverse compliment
63  */
64 struct motif : public annot
65 {
66   std::string sequence;
67
68   motif() :  annot(), sequence("") {};
69   //! this constructor is for when we're adding motifs to our annotations
70   motif(int begin, std::string motif);
71   ~motif();
72
73   // boost::serialization support
74 private:
75   friend class boost::serialization::access;
76   template<class Archive>
77   void serialize(Archive& ar, const unsigned int /*version*/) {
78     ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(annot);
79     ar & BOOST_SERIALIZATION_NVP(sequence);
80   }
81 };
82 BOOST_CLASS_EXPORT(motif);
83
84 //! sequence track for mussa.
85 class Sequence 
86 {
87 public:
88   typedef std::string::difference_type difference_type;
89   typedef std::string::iterator iterator;
90   typedef std::string::const_iterator const_iterator;
91   typedef std::string::reference reference;
92   typedef std::string::const_reference const_reference;
93   typedef std::string::size_type size_type;
94   static const size_type npos = std::string::npos;
95
96   Sequence();
97   ~Sequence();
98   Sequence(const char* seq);
99   Sequence(const std::string& seq);
100   Sequence(const Sequence& seq);
101   //! assignment to constant sequences
102   Sequence &operator=(const Sequence&);
103
104   // dangerous since they create large copies
105   //operator std::string();
106   //operator std::string() const;
107
108   friend std::ostream& operator<<(std::ostream&, const Sequence&);
109   friend bool operator<(const Sequence&, const Sequence&);
110   friend bool operator==(const Sequence&, const Sequence&);
111   const_reference operator[](size_type) const;
112
113   //! set sequence to a (sub)string containing nothing but AGCTN
114   void set_filtered_sequence(const std::string& seq, 
115                              std::string::size_type start=0, 
116                              std::string::size_type count=0);
117
118   //! retrive element at specific position
119   const_reference at(size_type n) const;
120   //! clear the sequence and its annotations
121   void clear();
122   //! forward iterator
123   iterator begin();
124   const_iterator begin() const;
125   iterator end();
126   const_iterator end() const;
127   //! is our sequence empty?
128   bool empty() const;
129   //! how many base pairs are there in our sequence
130   size_type size() const;
131   //! alias for size (used by string)
132   size_type length() const;
133
134   //! return a subsequence, copying over any appropriate annotation
135   Sequence subseq(int start=0, int count = std::string::npos) const;
136   //! return a reverse compliment
137   std::string rev_comp() const;
138
139   //! set sequence (filtered)
140   void set_sequence(const std::string &);
141   //! get sequence
142   std::string get_sequence() const;
143   //! set species name
144   void set_species(const std::string &);
145   //! get species name
146   std::string get_species() const;
147   //! set the fasta header
148   void set_fasta_header(std::string header);
149   //! get the fasta header
150   std::string get_fasta_header() const;
151   //! get name (will return the first non-empty, of fasta_header, species)
152   std::string get_name() const;
153   
154   //! load sequence AGCT from fasta file
155   //! \throw mussa_load_error
156   //! \throw sequence_empty_error
157   //! \throw sequence_empty_file_error
158   void load_fasta(const boost::filesystem::path file_path, int seq_num=1, 
159                   int start_index=0, int end_index=0);
160   //! load sequence from stream
161   //! \throw mussa_load_error
162   //! \throw sequence_empty_error
163   //! \throw sequence_empty_file_error
164   void load_fasta(std::iostream& file, int seq_num=1, 
165                   int start_index=0, int end_index=0);
166   //! load sequence annotations
167   //! \throws mussa_load_error 
168   void load_annot(const boost::filesystem::path file_path, int start_index, int end_index);
169   //! load sequence annotations
170   //! \throws mussa_load_error 
171   void load_annot(std::fstream& data_stream, int start_index, int end_index);
172   bool parse_annot(std::string data, int start_index=0, int end_index=0);
173   //! add an annotation to our list of annotations
174   void add_annotation(const annot& a);
175   const std::list<annot>& annotations() const;
176   const std::list<motif>& motifs() const;
177
178   //! add a motif to our list of motifs
179   //! \throws motif_normalize_error if there's something wrong with a_motif
180   void add_motif(const Sequence& a_motif);
181   //! clear our list of found motifs
182   void clear_motifs();
183   //! search a sequence for a_motif
184   //! \throws motif_normalize_error if there's something wrong with a_motif
185   std::vector<int> find_motif(const std::string& a_motif) const;
186   //! search a sequence for a_motif
187   //! \throws motif_normalize_error if there's something wrong with a_motif
188   std::vector<int> find_motif(const Sequence& a_motif) const;
189   //! convert IUPAC symbols to upperase
190   //! \throws motif_normalize_error if there is an invalid symbol
191   static std::string motif_normalize(const std::string& a_motif);
192   
193   //! annotate the current sequence with other sequences
194   void find_sequences(std::list<Sequence>::iterator start, 
195                       std::list<Sequence>::iterator end);
196   
197   void save(boost::filesystem::fstream &save_file);
198   void load_museq(boost::filesystem::path load_file_path, int seq_num); 
199   
200 private:
201   std::string seq;
202   std::string header;
203   std::string species;
204
205   std::list<annot> annots;
206   //! a seperate list for motifs since we're currently not saving them
207   std::list<motif> motif_list;
208
209   void motif_scan(std::string a_motif, std::vector<int> * motif_match_starts) const;
210   std::string rc_motif(std::string a_motif) const;
211   //! look for a string sequence type and and it to an annotation list
212   void add_string_annotation(std::string a_seq, std::string name);
213   
214   // boost::serialization support
215   friend class boost::serialization::access;
216   template<class Archive>
217   void serialize(Archive& ar, const unsigned int /*version*/) {
218     ar & BOOST_SERIALIZATION_NVP(seq);
219     ar & BOOST_SERIALIZATION_NVP(header);
220     ar & BOOST_SERIALIZATION_NVP(species);
221     ar & BOOST_SERIALIZATION_NVP(annots);
222     ar & BOOST_SERIALIZATION_NVP(motif_list);
223   }
224 };
225 BOOST_CLASS_EXPORT(Sequence);
226
227 #endif