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