update sequence serialization to support the xml archive
[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 : public std::string
86 {
87   private:
88     std::string header;
89     std::string species;
90
91     std::list<annot> annots;
92     //! a seperate list for motifs since we're currently not saving them
93     std::list<motif> motif_list;
94
95     void motif_scan(std::string a_motif, std::vector<int> * motif_match_starts);
96     std::string rc_motif(std::string a_motif);
97     //! look for a string sequence type and and it to an annotation list
98     void add_string_annotation(std::string a_seq, std::string name);
99
100     // boost::serialization support
101     friend class boost::serialization::access;
102     template<class Archive>
103     void serialize(Archive& ar, const unsigned int /*version*/) {
104       ar & boost::serialization::make_nvp(
105              "seq_text", 
106              boost::serialization::base_object<std::string >(*this)
107            );
108       ar & BOOST_SERIALIZATION_NVP(header);
109       ar & BOOST_SERIALIZATION_NVP(species);
110       ar & BOOST_SERIALIZATION_NVP(annots);
111       ar & BOOST_SERIALIZATION_NVP(motif_list);
112     }
113
114   public:
115     Sequence();
116     ~Sequence();
117     Sequence(const char* seq);
118     Sequence(const std::string& seq);
119     Sequence(const Sequence& seq);
120     //! assignment to constant sequences
121     Sequence &operator=(const Sequence&);
122
123     //! set sequence to a (sub)string containing nothing but AGCTN
124     void set_filtered_sequence(const std::string& seq, 
125                                std::string::size_type start=0, 
126                                std::string::size_type count=0);
127
128     //! load sequence AGCT from fasta file
129     //! \throw mussa_load_error
130     //! \throw sequence_empty_error
131     //! \throw sequence_empty_file_error
132     void load_fasta(const boost::filesystem::path file_path, int seq_num=1, 
133                     int start_index=0, int end_index=0);
134     //! load sequence from stream
135     //! \throw mussa_load_error
136     //! \throw sequence_empty_error
137     //! \throw sequence_empty_file_error
138     void load_fasta(std::iostream& file, int seq_num=1, 
139                     int start_index=0, int end_index=0);
140     //! load sequence annotations
141     //! \throws mussa_load_error 
142     void load_annot(const boost::filesystem::path file_path, int start_index, int end_index);
143     //! load sequence annotations
144     //! \throws mussa_load_error 
145     void load_annot(std::fstream& data_stream, int start_index, int end_index);
146     bool parse_annot(std::string data, int start_index=0, int end_index=0);
147     //! add an annotation to our list of annotations
148     void add_annotation(const annot& a);
149     const std::list<annot>& annotations() const;
150     const std::list<motif>& motifs() const;
151
152     //! return a subsequence, copying over any appropriate annotation
153     Sequence subseq(int start=0, int count = std::string::npos) const;
154     //! return a reverse compliment
155     std::string rev_comp() const;
156
157     //! clear the sequence and its annotations
158     void clear();
159
160     //! set species name
161     void set_species(const std::string &);
162     //! get species name
163     std::string get_species() const;
164     //! set the fasta header
165     void set_fasta_header(std::string header);
166     //! get the fasta header
167     std::string get_fasta_header() const;
168     //! get name (will return the first non-empty, of fasta_header, species)
169     std::string get_name() const;
170
171     //! add a motif to our list of motifs
172     //! \throws motif_normalize_error if there's something wrong with a_motif
173     void add_motif(const Sequence& a_motif);
174     //! clear our list of found motifs
175     void clear_motifs();
176     //! search a sequence for a_motif
177     //! \throws motif_normalize_error if there's something wrong with a_motif
178     std::vector<int> find_motif(std::string a_motif);
179     //! convert IUPAC symbols to upperase
180     //! \throws motif_normalize_error if there is an invalid symbol
181     static std::string motif_normalize(std::string a_motif);
182
183     //! annotate the current sequence with other sequences
184     void find_sequences(std::list<Sequence>::iterator start, 
185                         std::list<Sequence>::iterator end);
186
187     void save(boost::filesystem::fstream &save_file);
188     void load_museq(boost::filesystem::path load_file_path, int seq_num); 
189
190 };
191 BOOST_CLASS_EXPORT(Sequence);
192
193 #endif