Let the annotation parser skip html tags
[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/string.hpp>
23 #include <boost/serialization/utility.hpp>
24 #include <boost/serialization/export.hpp>
25
26 #include <list>
27 #include <string>
28 #include <vector>
29 #include <iostream>
30
31 // Sequence data class
32
33 //! Attach annotation information to a sequence track
34 struct annot
35 {
36   annot();
37   annot(int begin, int end, std::string type, std::string name);
38   ~annot();
39   
40   int begin;
41   int end;
42   std::string type;
43   std::string name;
44
45   friend bool operator==(const annot& left, const annot& right);
46 private:
47   // boost::serialization support
48   friend class boost::serialization::access;
49   template<class Archive>
50   void serialize(Archive& ar, const unsigned int /*version*/) {
51     ar & begin;
52     ar & end;
53     ar & type;
54     ar & name;
55   }
56 };
57 BOOST_CLASS_EXPORT(annot);
58
59
60 /* The way that motifs are found currently doesn't really 
61  * indicate that the match was a reverse compliment
62  */
63 struct motif : public annot
64 {
65   std::string sequence;
66
67   motif() :  annot(), sequence("") {};
68   //! this constructor is for when we're adding motifs to our annotations
69   motif(int begin, std::string motif);
70   ~motif();
71
72   // boost::serialization support
73 private:
74   friend class boost::serialization::access;
75   template<class Archive>
76   void motif::serialize(Archive& ar, const unsigned int /*version*/) {
77     ar & boost::serialization::base_object<annot>(*this);
78     ar & sequence;
79   }
80 };
81 BOOST_CLASS_EXPORT(motif);
82
83 //! sequence track for mussa.
84 class Sequence : public std::string
85 {
86   private:
87     std::string header;
88     std::string species;
89
90     std::list<annot> annots;
91     //! a seperate list for motifs since we're currently not saving them
92     std::list<motif> motif_list;
93
94     void motif_scan(std::string a_motif, std::vector<int> * motif_match_starts);
95     std::string rc_motif(std::string a_motif);
96     //! look for a string sequence type and and it to an annotation list
97     void add_string_annotation(std::string a_seq, std::string name);
98
99     // boost::serialization support
100     friend class boost::serialization::access;
101     template<class Archive>
102     void Sequence::serialize(Archive& ar, const unsigned int /*version*/) {
103       ar & boost::serialization::base_object<std::string>(*this);
104       ar & header;
105       ar & species;
106       ar & annots;
107       ar & motif_list;
108     }
109
110   public:
111     Sequence();
112     ~Sequence();
113     Sequence(const char* seq);
114     Sequence(const std::string& seq);
115     Sequence(const Sequence& seq);
116     //! assignment to constant sequences
117     Sequence &operator=(const Sequence&);
118
119     //! set sequence to a (sub)string containing nothing but AGCTN
120     void set_filtered_sequence(const std::string& seq, 
121                                std::string::size_type start=0, 
122                                std::string::size_type count=0);
123
124     //! load sequence AGCT from fasta file
125     //! \throw mussa_load_error
126     //! \throw sequence_empty_error
127     //! \throw sequence_empty_file_error
128     void load_fasta(const boost::filesystem::path file_path, int seq_num=1, 
129                     int start_index=0, int end_index=0);
130     //! load sequence from stream
131     //! \throw mussa_load_error
132     //! \throw sequence_empty_error
133     //! \throw sequence_empty_file_error
134     void load_fasta(std::iostream& file, int seq_num=1, 
135                     int start_index=0, int end_index=0);
136     //! load sequence annotations
137     //! \throws mussa_load_error 
138     void load_annot(const boost::filesystem::path file_path, int start_index, int end_index);
139     //! load sequence annotations
140     //! \throws mussa_load_error 
141     void load_annot(std::fstream& data_stream, int start_index, int end_index);
142     bool parse_annot(std::string data, int start_index=0, int end_index=0);
143     //! add an annotation to our list of annotations
144     void add_annotation(const annot& a);
145     const std::list<annot>& annotations() const;
146     const std::list<motif>& motifs() const;
147
148     //! return a subsequence, copying over any appropriate annotation
149     Sequence subseq(int start=0, int count = std::string::npos) const;
150     //! return a reverse compliment
151     std::string rev_comp() const;
152
153     //! clear the sequence and its annotations
154     void clear();
155
156     //! set species name
157     void set_species(const std::string &);
158     //! get species name
159     std::string get_species() const;
160     //! set the fasta header
161     void set_fasta_header(std::string header);
162     //! get the fasta header
163     std::string get_fasta_header() const;
164     //! get name (will return the first non-empty, of fasta_header, species)
165     std::string get_name() const;
166
167     //! add a motif to our list of motifs
168     //! \throws motif_normalize_error if there's something wrong with a_motif
169     void add_motif(const Sequence& a_motif);
170     //! clear our list of found motifs
171     void clear_motifs();
172     //! search a sequence for a_motif
173     //! \throws motif_normalize_error if there's something wrong with a_motif
174     std::vector<int> find_motif(std::string a_motif);
175     //! convert IUPAC symbols to upperase
176     //! \throws motif_normalize_error if there is an invalid symbol
177     static std::string motif_normalize(std::string a_motif);
178
179     //! annotate the current sequence with other sequences
180     void find_sequences(std::list<Sequence>::iterator start, 
181                         std::list<Sequence>::iterator end);
182
183     void save(boost::filesystem::fstream &save_file);
184     void load_museq(boost::filesystem::path load_file_path, int seq_num); 
185
186 };
187 BOOST_CLASS_EXPORT(Sequence);
188
189 #endif