80aa49f75e17bcf9ca572de2d9b8c53658ea31af
[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 #include <boost/enable_shared_from_this.hpp>
32
33 #include <iostream>
34
35 #include "alphabet.hpp"
36 #include "seq.hpp"
37 #include "seq_span.hpp"
38
39 // Sequence data class
40
41 //! Attach annotation information to a sequence track
42 struct annot
43 {
44   annot();
45   annot(int begin, int end, std::string type, std::string name);
46   ~annot();
47   
48   int begin;
49   int end;
50   std::string type;
51   std::string name;
52
53   friend bool operator==(const annot& left, const annot& right);
54 private:
55   // boost::serialization support
56   friend class boost::serialization::access;
57   template<class Archive>
58   void serialize(Archive& ar, const unsigned int /*version*/) {
59     ar & BOOST_SERIALIZATION_NVP(begin);
60     ar & BOOST_SERIALIZATION_NVP(end);
61     ar & BOOST_SERIALIZATION_NVP(type);
62     ar & BOOST_SERIALIZATION_NVP(name);
63   }
64 };
65 BOOST_CLASS_EXPORT(annot);
66
67
68 /* The way that motifs are found currently doesn't really 
69  * indicate that the match was a reverse compliment
70  */
71 struct motif : public annot
72 {
73   std::string sequence;
74
75   motif() :  annot(), sequence("") {};
76   //! this constructor is for when we're adding motifs to our annotations
77   motif(int begin, std::string motif);
78   ~motif();
79
80   // boost::serialization support
81 private:
82   friend class boost::serialization::access;
83   template<class Archive>
84   void serialize(Archive& ar, const unsigned int /*version*/) {
85     ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(annot);
86     ar & BOOST_SERIALIZATION_NVP(sequence);
87   }
88 };
89 BOOST_CLASS_EXPORT(motif);
90
91
92 //! sequence track for mussa.
93 class Sequence 
94 {
95 public:
96   typedef SeqString::value_type value_type;
97   typedef SeqString::difference_type difference_type;
98   typedef SeqString::iterator iterator;
99   typedef SeqString::reverse_iterator reverse_iterator;
100   typedef SeqString::const_iterator const_iterator;
101   typedef SeqString::const_reverse_iterator const_reverse_iterator;
102   typedef SeqString::reference reference;
103   typedef SeqString::const_reference const_reference;
104   typedef SeqString::size_type size_type;
105   static const size_type npos = SeqString::npos;
106                       
107   Sequence(AlphabetRef a = reduced_dna_alphabet);
108   Sequence(const char* seq, 
109            AlphabetRef a = reduced_dna_alphabet,
110            SeqSpan::strand_type strand = SeqSpan::PlusStrand);
111   Sequence(const std::string& seq,
112            AlphabetRef a = reduced_dna_alphabet,
113            SeqSpan::strand_type strand = SeqSpan::PlusStrand);
114   Sequence(const Sequence& seq);
115   Sequence(const Sequence *);
116   Sequence(const SeqSpanRef&); 
117   ~Sequence();
118   //! assignment to constant sequences
119   Sequence &operator=(const Sequence&);
120
121   friend std::ostream& operator<<(std::ostream&, const Sequence&);
122   friend bool operator<(const Sequence&, const Sequence&);
123   friend bool operator==(const Sequence&, const Sequence&);
124   friend bool operator!=(const Sequence&, const Sequence&);
125   const_reference operator[](size_type) const;
126
127   //! set sequence to a (sub)string containing nothing but AGCTN
128   void set_filtered_sequence(const std::string& seq,
129                              AlphabetRef a=reduced_dna_alphabet, 
130                              size_type start=0,
131                              size_type count=npos,
132                              SeqSpan::strand_type strand=SeqSpan::PlusStrand);
133
134   //! retrive element at specific position
135   const_reference at(size_type i) const { return seq->at(i); }
136   //! clear the sequence and its annotations
137   void clear();
138   //! return a non-null terminated c pointer to the sequence data
139   const char *data() const { return seq->data(); }
140   //! forward iterator
141   const_iterator begin() const { return seq->begin(); }
142   //! last iterator
143   const_iterator end() const { return seq->end(); }
144   //! is our sequence empty?
145   bool empty() const { return (seq) ? seq->empty() : true ; }
146   //! find first 
147   size_type find_first_not_of(const std::string& q, size_type index=0) { return seq->find_first_not_of(q, index); } 
148   //! how many base pairs are there in our sequence
149   size_type size() const { return (seq) ? seq->size() : 0; }
150   //! alias for size (used by string)
151   size_type length() const { return size(); }
152   //! reverse iterator
153   const_reverse_iterator rbegin() const { return seq->rbegin(); }
154   //! reverse end iterator
155   const_reverse_iterator rend() const { return seq->rend(); }
156   //! is our sequence empty?
157   //! start position relative to "base" sequence
158   size_type start() const { return seq->parentStart(); }
159   //! one past the last position relative to "base" sequence
160   size_type stop() const { return seq->parentStop(); }
161
162   //! return a subsequence, copying over any appropriate annotation
163   Sequence subseq(size_type start=0, 
164                   size_type count = npos,
165                   SeqSpan::strand_type strand = SeqSpan::SameStrand) const;
166   //! reverse a character
167   std::string create_reverse_map() const;
168   //! return a reverse compliment (this needs to be improved?)
169   Sequence rev_comp() const;
170
171   //! set sequence (filtered)
172   void set_sequence(const std::string &, AlphabetRef);
173   //! get sequence
174   std::string get_sequence() const;
175   //! set species name
176   void set_species(const std::string &);
177   //! get species name
178   std::string get_species() const;
179   //! set the fasta header
180   void set_fasta_header(std::string header);
181   //! get the fasta header
182   std::string get_fasta_header() const;
183   //! get name (will return the first non-empty, of fasta_header, species)
184   std::string get_name() const;
185   //! return a reference to whichever alphabet we're currently representing
186   const Alphabet& get_alphabet() const; 
187   
188   //! load sequence from fasta file using the sequences current alphabet
189   void load_fasta(const boost::filesystem::path file_path, int seq_num=1,
190                   int start_index=0, int end_index=0);
191   //! load sequence AGCT from fasta file
192   //! \throw mussa_load_error
193   //! \throw sequence_empty_error
194   //! \throw sequence_empty_file_error
195   void load_fasta(const boost::filesystem::path file_path, 
196                   AlphabetRef a, 
197                   int seq_num=1, 
198                               int start_index=0, int end_index=0);
199   void load_fasta(std::istream& file, 
200                   int seq_num=1, int start_index=0, int end_index=0);
201   //! load sequence from stream
202   //! \throw mussa_load_error
203   //! \throw sequence_empty_error
204   //! \throw sequence_empty_file_error
205   void load_fasta(std::istream& file, 
206                   AlphabetRef a,
207                   int seq_num=1, 
208                               int start_index=0, int end_index=0);
209   //! load sequence annotations
210   //! \throws mussa_load_error 
211   void load_annot(const boost::filesystem::path file_path, int start_index, int end_index);
212   //! load sequence annotations
213   //! \throws mussa_load_error 
214   void load_annot(std::fstream& data_stream, int start_index, int end_index);
215   //! parse annotation file
216   /*! \throws annotation_load_error 
217    */
218   void parse_annot(std::string data, int start_index=0, int end_index=0);
219   //! add an annotation to our list of annotations
220   void add_annotation(const annot& a);
221   const std::list<annot>& annotations() const;
222   const std::list<motif>& motifs() const;
223
224   //! add a motif to our list of motifs
225   void add_motif(const Sequence& a_motif);
226   //! clear our list of found motifs
227   void clear_motifs();
228   //! search a sequence for a_motif
229   //! \throws motif_normalize_error if there's something wrong with a_motif
230   std::vector<int> find_motif(const Sequence& a_motif) const;  
231   //! annotate the current sequence with other sequences
232   void find_sequences(std::list<Sequence>::iterator start, 
233                       std::list<Sequence>::iterator end);
234   
235   void save(boost::filesystem::fstream &save_file);
236   void load_museq(boost::filesystem::path load_file_path, int seq_num);
237   
238 protected:  
239   SeqSpanRef seq;
240   //! fasta header
241   std::string header;
242   //! species name
243   std::string species;
244
245   //! store our oldstyle annotations
246   std::list<annot> annots;
247   //! a seperate list for motifs since we're currently not saving them
248   std::list<motif> motif_list;
249
250   //! copy over all our annotation children
251   void copy_children(Sequence &, size_type start, size_type count) const;
252
253   void motif_scan(const Sequence& a_motif, std::vector<int> * motif_match_starts) const;
254   std::string rc_motif(std::string a_motif) const;
255   //! look for a string sequence type and and it to an annotation list
256   void add_string_annotation(std::string a_seq, std::string name);
257   
258   // boost::serialization support
259   friend class boost::serialization::access;
260   template<class Archive>
261   void serialize(Archive& ar, const unsigned int /*version*/) {
262     ar & BOOST_SERIALIZATION_NVP(seq);
263     ar & BOOST_SERIALIZATION_NVP(header);
264     ar & BOOST_SERIALIZATION_NVP(species);
265     ar & BOOST_SERIALIZATION_NVP(annots);
266     ar & BOOST_SERIALIZATION_NVP(motif_list);
267   }
268 };
269 BOOST_CLASS_EXPORT(Sequence);
270 #endif