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
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.
13 // ----------------------------------------
14 // ---------- sequence.hh -----------
15 // ----------------------------------------
17 #include <boost/filesystem/path.hpp>
18 #include <boost/filesystem/fstream.hpp>
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>
30 #include <boost/shared_ptr.hpp>
34 #include "alg/alphabet.hpp"
36 // Sequence data class
38 //! Attach annotation information to a sequence track
42 annot(int begin, int end, std::string type, std::string name);
50 friend bool operator==(const annot& left, const annot& right);
52 // boost::serialization support
53 friend class boost::serialization::access;
54 template<class Archive>
55 void serialize(Archive& ar, const unsigned int /*version*/) {
56 ar & BOOST_SERIALIZATION_NVP(begin);
57 ar & BOOST_SERIALIZATION_NVP(end);
58 ar & BOOST_SERIALIZATION_NVP(type);
59 ar & BOOST_SERIALIZATION_NVP(name);
62 BOOST_CLASS_EXPORT(annot);
65 /* The way that motifs are found currently doesn't really
66 * indicate that the match was a reverse compliment
68 struct motif : public annot
72 motif() : annot(), sequence("") {};
73 //! this constructor is for when we're adding motifs to our annotations
74 motif(int begin, std::string motif);
77 // boost::serialization support
79 friend class boost::serialization::access;
80 template<class Archive>
81 void serialize(Archive& ar, const unsigned int /*version*/) {
82 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(annot);
83 ar & BOOST_SERIALIZATION_NVP(sequence);
86 BOOST_CLASS_EXPORT(motif);
88 //! the only purpose of this class is that the shared_ptr template
89 //! functions need the serialization support to be in-class.
90 class seq_string : public std::string
93 typedef std::string::iterator iterator;
94 typedef std::string::reverse_iterator reverse_iterator;
95 typedef std::string::const_iterator const_iterator;
96 typedef std::string::const_reverse_iterator const_reverse_iterator;
98 friend class boost::serialization::access;
99 template<class Archive>
100 void serialize(Archive& ar, const unsigned int /*version*/) {
101 //ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(std::string);
102 ar & boost::serialization::make_nvp("bases",
103 boost::serialization::base_object<std::string>(*this)
108 //! sequence track for mussa.
112 typedef std::string::value_type value_type;
113 typedef std::string::difference_type difference_type;
114 typedef std::string::iterator iterator;
115 typedef std::string::reverse_iterator reverse_iterator;
116 typedef std::string::const_iterator const_iterator;
117 typedef std::string::const_reverse_iterator const_reverse_iterator;
118 typedef std::string::reference reference;
119 typedef std::string::const_reference const_reference;
120 typedef std::string::size_type size_type;
121 static const size_type npos = std::string::npos;
122 enum strand_type { UnknownStrand, PlusStrand, MinusStrand, BothStrand };
123 enum alphabet_ref { reduced_dna_alphabet, reduced_rna_alphabet, reduced_nucleic_alphabet,
124 nucleic_alphabet, protein_alphabet };
126 Sequence(alphabet_ref a = reduced_nucleic_alphabet);
127 Sequence(const char* seq, alphabet_ref a = reduced_nucleic_alphabet);
128 Sequence(const std::string& seq, alphabet_ref a = reduced_nucleic_alphabet);
129 Sequence(const Sequence& seq);
131 //! assignment to constant sequences
132 Sequence &operator=(const Sequence&);
134 friend std::ostream& operator<<(std::ostream&, const Sequence&);
135 friend bool operator<(const Sequence&, const Sequence&);
136 friend bool operator==(const Sequence&, const Sequence&);
137 friend bool operator!=(const Sequence&, const Sequence&);
138 const_reference operator[](size_type) const;
140 //! set sequence to a (sub)string containing nothing but AGCTN
141 void set_filtered_sequence(const std::string& seq,
144 size_type count=npos,
145 strand_type strand=UnknownStrand);
147 //! retrive element at specific position
148 const_reference at(size_type n) const;
149 //! clear the sequence and its annotations
151 //! return c pointer to the sequence data
152 const char *c_str() const;
154 const_iterator begin() const;
156 const_iterator end() const;
157 //! is our sequence empty?
159 //! how many base pairs are there in our sequence
160 size_type size() const;
161 //! alias for size (used by string)
162 size_type length() const;
164 const_reverse_iterator rbegin() const;
165 //! reverse end iterator
166 const_reverse_iterator rend() const;
167 //! is our sequence empty?
168 //! start position relative to "base" sequence
169 size_type start() const;
170 //! one past the last position relative to "base" sequence
171 size_type stop() const;
173 //! return a subsequence, copying over any appropriate annotation
174 Sequence subseq(int start=0, int count = std::string::npos);
175 //! reverse a character
176 std::string create_reverse_map() const;
177 //! return a reverse compliment (this needs to be improved?)
178 Sequence rev_comp() const;
180 //! set sequence (filtered)
181 void set_sequence(const std::string &, alphabet_ref);
183 std::string get_sequence() const;
185 void set_species(const std::string &);
187 std::string get_species() const;
188 //! set the fasta header
189 void set_fasta_header(std::string header);
190 //! get the fasta header
191 std::string get_fasta_header() const;
192 //! get name (will return the first non-empty, of fasta_header, species)
193 std::string get_name() const;
194 //! return a reference to whichever alphabet we're currently representing
195 const Alphabet& get_alphabet() const;
196 //! return a reference to whichever alphabet we're currently representing
197 const Alphabet& get_alphabet(alphabet_ref) const;
199 //! load sequence from fasta file using the sequences current alphabet
200 void load_fasta(const boost::filesystem::path file_path, int seq_num=1,
201 int start_index=0, int end_index=0);
202 //! load sequence AGCT from fasta file
203 //! \throw mussa_load_error
204 //! \throw sequence_empty_error
205 //! \throw sequence_empty_file_error
206 void load_fasta(const boost::filesystem::path file_path,
209 int start_index=0, int end_index=0);
210 void load_fasta(std::istream& file,
211 int seq_num=1, int start_index=0, int end_index=0);
212 //! load sequence from stream
213 //! \throw mussa_load_error
214 //! \throw sequence_empty_error
215 //! \throw sequence_empty_file_error
216 void load_fasta(std::istream& file,
219 int start_index=0, int end_index=0);
220 //! load sequence annotations
221 //! \throws mussa_load_error
222 void load_annot(const boost::filesystem::path file_path, int start_index, int end_index);
223 //! load sequence annotations
224 //! \throws mussa_load_error
225 void load_annot(std::fstream& data_stream, int start_index, int end_index);
226 //! parse annotation file
227 /*! \throws annotation_load_error
229 void parse_annot(std::string data, int start_index=0, int end_index=0);
230 //! add an annotation to our list of annotations
231 void add_annotation(const annot& a);
232 const std::list<annot>& annotations() const;
233 const std::list<motif>& motifs() const;
235 //! add a motif to our list of motifs
236 void add_motif(const Sequence& a_motif);
237 //! clear our list of found motifs
239 //! search a sequence for a_motif
240 //! \throws motif_normalize_error if there's something wrong with a_motif
241 std::vector<int> find_motif(const Sequence& a_motif) const;
242 //! annotate the current sequence with other sequences
243 void find_sequences(std::list<Sequence>::iterator start,
244 std::list<Sequence>::iterator end);
246 void save(boost::filesystem::fstream &save_file);
247 void load_museq(boost::filesystem::path load_file_path, int seq_num);
252 //! hold a shared pointer to our sequence string
253 boost::shared_ptr<seq_string> seq;
254 //! which alphabet we're using
255 alphabet_ref alphabet;
256 //! start offset into the sequence
258 //! number of basepairs of the shared sequence we represent
260 //! strand orientation
267 //! store our oldstyle annotations
268 std::list<annot> annots;
269 //! a seperate list for motifs since we're currently not saving them
270 std::list<motif> motif_list;
272 void motif_scan(const Sequence& a_motif, std::vector<int> * motif_match_starts) const;
273 std::string rc_motif(std::string a_motif) const;
274 //! look for a string sequence type and and it to an annotation list
275 void add_string_annotation(std::string a_seq, std::string name);
277 // boost::serialization support
278 friend class boost::serialization::access;
279 template<class Archive>
280 void serialize(Archive& ar, const unsigned int /*version*/) {
281 ar & BOOST_SERIALIZATION_NVP(parent);
282 ar & BOOST_SERIALIZATION_NVP(seq);
283 ar & BOOST_SERIALIZATION_NVP(alphabet);
284 ar & BOOST_SERIALIZATION_NVP(seq_start);
285 ar & BOOST_SERIALIZATION_NVP(seq_count);
286 ar & BOOST_SERIALIZATION_NVP(strand);
287 ar & BOOST_SERIALIZATION_NVP(header);
288 ar & BOOST_SERIALIZATION_NVP(species);
289 ar & BOOST_SERIALIZATION_NVP(annots);
290 ar & BOOST_SERIALIZATION_NVP(motif_list);
293 BOOST_CLASS_EXPORT(Sequence);