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/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>
32 // Sequence data class
34 //! Attach annotation information to a sequence track
38 annot(int begin, int end, std::string type, std::string name);
46 friend bool operator==(const annot& left, const annot& right);
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);
58 BOOST_CLASS_EXPORT(annot);
61 /* The way that motifs are found currently doesn't really
62 * indicate that the match was a reverse compliment
64 struct motif : public annot
68 motif() : annot(), sequence("") {};
69 //! this constructor is for when we're adding motifs to our annotations
70 motif(int begin, std::string motif);
73 // boost::serialization support
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);
82 BOOST_CLASS_EXPORT(motif);
84 //! sequence track for mussa.
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;
96 // some standard dna alphabets
97 // Include nl (\012), and cr (\015) to make sequence parsing eol
98 // convention independent.
100 static const std::string dna_alphabet;
101 static const std::string rna_alphabet;
102 //! this is the general iupac alphabet for nucleotides
103 static const std::string nucleic_iupac_alphabet;
104 //! the protein alphabet
105 static const std::string protein_alphabet;
109 Sequence(const char* seq);
110 Sequence(const std::string& seq);
111 Sequence(const Sequence& seq);
112 //! assignment to constant sequences
113 Sequence &operator=(const Sequence&);
115 // dangerous since they create large copies
116 //operator std::string();
117 //operator std::string() const;
119 friend std::ostream& operator<<(std::ostream&, const Sequence&);
120 friend bool operator<(const Sequence&, const Sequence&);
121 friend bool operator==(const Sequence&, const Sequence&);
122 const_reference operator[](size_type) const;
124 //! set sequence to a (sub)string containing nothing but AGCTN
125 void set_filtered_sequence(const std::string& seq,
126 std::string::size_type start=0,
127 std::string::size_type count=0);
129 //! retrive element at specific position
130 const_reference at(size_type n) const;
131 //! clear the sequence and its annotations
135 const_iterator begin() const;
137 const_iterator end() const;
138 //! is our sequence empty?
140 //! how many base pairs are there in our sequence
141 size_type size() const;
142 //! alias for size (used by string)
143 size_type length() const;
145 //! return a subsequence, copying over any appropriate annotation
146 Sequence subseq(int start=0, int count = std::string::npos) const;
147 //! return a reverse compliment
148 std::string rev_comp() const;
150 //! set sequence (filtered)
151 void set_sequence(const std::string &);
153 std::string get_sequence() const;
155 void set_species(const std::string &);
157 std::string get_species() const;
158 //! set the fasta header
159 void set_fasta_header(std::string header);
160 //! get the fasta header
161 std::string get_fasta_header() const;
162 //! get name (will return the first non-empty, of fasta_header, species)
163 std::string get_name() const;
165 //! load sequence AGCT from fasta file
166 //! \throw mussa_load_error
167 //! \throw sequence_empty_error
168 //! \throw sequence_empty_file_error
169 void load_fasta(const boost::filesystem::path file_path, int seq_num=1,
170 int start_index=0, int end_index=0);
171 //! load sequence from stream
172 //! \throw mussa_load_error
173 //! \throw sequence_empty_error
174 //! \throw sequence_empty_file_error
175 void load_fasta(std::iostream& file, int seq_num=1,
176 int start_index=0, int end_index=0);
177 //! load sequence annotations
178 //! \throws mussa_load_error
179 void load_annot(const boost::filesystem::path file_path, int start_index, int end_index);
180 //! load sequence annotations
181 //! \throws mussa_load_error
182 void load_annot(std::fstream& data_stream, int start_index, int end_index);
183 bool parse_annot(std::string data, int start_index=0, int end_index=0);
184 //! add an annotation to our list of annotations
185 void add_annotation(const annot& a);
186 const std::list<annot>& annotations() const;
187 const std::list<motif>& motifs() const;
189 //! add a motif to our list of motifs
190 //! \throws motif_normalize_error if there's something wrong with a_motif
191 void add_motif(const Sequence& a_motif);
192 //! clear our list of found motifs
194 //! search a sequence for a_motif
195 //! \throws motif_normalize_error if there's something wrong with a_motif
196 std::vector<int> find_motif(const std::string& a_motif) const;
197 //! search a sequence for a_motif
198 //! \throws motif_normalize_error if there's something wrong with a_motif
199 std::vector<int> find_motif(const Sequence& a_motif) const;
200 //! convert IUPAC symbols to upperase
201 //! \throws motif_normalize_error if there is an invalid symbol
202 static std::string motif_normalize(const std::string& a_motif);
204 //! annotate the current sequence with other sequences
205 void find_sequences(std::list<Sequence>::iterator start,
206 std::list<Sequence>::iterator end);
208 void save(boost::filesystem::fstream &save_file);
209 void load_museq(boost::filesystem::path load_file_path, int seq_num);
216 std::list<annot> annots;
217 //! a seperate list for motifs since we're currently not saving them
218 std::list<motif> motif_list;
220 void motif_scan(std::string a_motif, std::vector<int> * motif_match_starts) const;
221 std::string rc_motif(std::string a_motif) const;
222 //! look for a string sequence type and and it to an annotation list
223 void add_string_annotation(std::string a_seq, std::string name);
225 // boost::serialization support
226 friend class boost::serialization::access;
227 template<class Archive>
228 void serialize(Archive& ar, const unsigned int /*version*/) {
229 ar & BOOST_SERIALIZATION_NVP(seq);
230 ar & BOOST_SERIALIZATION_NVP(header);
231 ar & BOOST_SERIALIZATION_NVP(species);
232 ar & BOOST_SERIALIZATION_NVP(annots);
233 ar & BOOST_SERIALIZATION_NVP(motif_list);
236 BOOST_CLASS_EXPORT(Sequence);