add annot and motif classes to python interface
[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 #include <list>
20 #include <string>
21 #include <vector>
22 #include <iostream>
23
24 // Sequence data class
25
26 //! Attach annotation information to a sequence track
27 struct annot
28 {
29   annot();
30   annot(int begin, int end, std::string type, std::string name);
31   ~annot();
32   
33   int begin;
34   int end;
35   std::string type;
36   std::string name;
37
38   friend bool operator==(const annot& left, const annot& right);
39 };
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 : public annot
45 {
46   std::string sequence;
47
48   //! this constructor is for when we're adding motifs to our annotations
49   motif(int begin, std::string motif);
50   ~motif();
51 };
52
53 //! sequence track for mussa.
54 class Sequence : public std::string
55 {
56   private:
57     std::string header;
58     std::string species;
59
60     std::list<annot> annots;
61     //! a seperate list for motifs since we're currently not saving them
62     std::list<motif> motif_list;
63
64     void motif_scan(std::string a_motif, std::vector<int> * motif_match_starts);
65     std::string rc_motif(std::string a_motif);
66     //! look for a string sequence type and and it to an annotation list
67     void add_string_annotation(std::string a_seq, std::string name);
68
69   public:
70     Sequence();
71     ~Sequence();
72     Sequence(const char* seq);
73     Sequence(const std::string& seq);
74     Sequence(const Sequence& seq);
75     //! assignment to constant sequences
76     Sequence &operator=(const Sequence&);
77
78     //! set sequence to a (sub)string containing nothing but AGCTN
79     void set_filtered_sequence(const std::string& seq, 
80                                std::string::size_type start=0, 
81                                std::string::size_type count=0);
82
83     //! load sequence AGCT from fasta file
84     //! \throw mussa_load_error
85     //! \throw sequence_empty_error
86     //! \throw sequence_empty_file_error
87     void load_fasta(const boost::filesystem::path file_path, int seq_num=1, 
88                     int start_index=0, int end_index=0);
89     //! load sequence from stream
90     //! \throw mussa_load_error
91     //! \throw sequence_empty_error
92     //! \throw sequence_empty_file_error
93     void load_fasta(std::iostream& file, int seq_num=1, 
94                     int start_index=0, int end_index=0);
95     //! load sequence annotations
96     //! \throws mussa_load_error 
97     void load_annot(const boost::filesystem::path file_path, int start_index, int end_index);
98     //! load sequence annotations
99     //! \throws mussa_load_error 
100     void load_annot(std::fstream& data_stream, int start_index, int end_index);
101     void parse_annot(std::string data, int start_index, int end_index);
102     //! add an annotation to our list of annotations
103     void add_annotation(const annot& a);
104     const std::list<annot>& annotations() const;
105     const std::list<motif>& motifs() const;
106
107     //! return a subsequence, copying over any appropriate annotation
108     Sequence subseq(int start=0, int count = std::string::npos) const;
109     //! return a reverse compliment
110     std::string rev_comp() const;
111
112     //! clear the sequence and its annotations
113     void clear();
114
115     //! set species name
116     void set_species(const std::string &);
117     //! get species name
118     std::string get_species() const;
119     //! set the fasta header
120     void set_fasta_header(std::string header);
121     //! get the fasta header
122     std::string get_fasta_header() const;
123     //! get name (will return the first non-empty, of fasta_header, species)
124     std::string get_name() const;
125
126     //! add a motif to our list of motifs
127     //! \throws motif_normalize_error if there's something wrong with a_motif
128     void add_motif(const Sequence& a_motif);
129     //! clear our list of found motifs
130     void clear_motifs();
131     //! search a sequence for a_motif
132     //! \throws motif_normalize_error if there's something wrong with a_motif
133     std::vector<int> find_motif(std::string a_motif);
134     //! convert IUPAC symbols to upperase
135     //! \throws motif_normalize_error if there is an invalid symbol
136     static std::string motif_normalize(std::string a_motif);
137
138     //! annotate the current sequence with other sequences
139     void find_sequences(std::list<Sequence>::iterator start, 
140                         std::list<Sequence>::iterator end);
141
142     void save(boost::filesystem::fstream &save_file);
143     void load_museq(boost::filesystem::path load_file_path, int seq_num); 
144 };
145
146 #endif