attach motifs to a sequence object
[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
18 #include <fstream>
19 #include <list>
20 #include <string>
21 #include <vector>
22
23 // Sequence data class
24
25 //! Attach annotation information to a sequence track
26 struct annot
27 {
28   annot();
29   annot(int start, int end, std::string type, std::string name);
30   
31   int start, end;
32   std::string name, type;
33 };
34
35 /* The way that motifs are found currently doesn't really 
36  * indicate that the match was a reverse compliment
37  */
38 struct motif : public annot
39 {
40   std::string sequence;
41
42   //! this constructor is for when we're adding motifs to our annotations
43   motif(int start, std::string motif);
44 };
45
46 //! sequence track for mussa.
47 class Sequence
48 {
49   friend class ConnView;
50   friend class SeqView;
51   private:
52     std::string sequence;
53     std::string header;
54     std::string species;
55
56     std::list<annot> annots;
57     //! a seperate list for motifs since we're currently not saving them
58     std::list<motif> motif_list;
59
60     void motif_scan(std::string a_motif, std::vector<int> * motif_match_starts);
61     std::string rc_motif(std::string a_motif);
62     std::string motif_validate(std::string a_motif);
63   public:
64     typedef std::string::iterator iterator;
65     typedef std::string::const_iterator const_iterator;
66     typedef std::string::size_type size_type;
67
68     Sequence();
69     Sequence(std::string seq);
70     //! assignment to constant sequences
71     Sequence &operator=(const Sequence&);
72     Sequence &operator=(const std::string &);
73
74     //! set sequence to a (sub)string containing nothing but AGCTN
75     void set_filtered_sequence(const std::string& seq, 
76                                std::string::size_type start=0, 
77                                std::string::size_type count=0);
78
79     //! load sequence AGCT from fasta file
80     /*! \throws mussa_load_error 
81      */
82     void load_fasta(const std::string file_path, int seq_num=1, 
83                     int start_index=0, int end_index=0);
84     //! load sequence annotations
85     /*! \throws mussa_load_error 
86      */
87     void load_annot(const std::string file_path, int start_index, int end_index);
88     const std::list<annot>& annotations() const;
89     const std::list<motif>& motifs() const;
90
91     // simple access functions
92     void set_seq(const std::string& a_seq);
93     const std::string& get_seq() const;
94     const char *c_seq() const;
95     std::string subseq(int start, int end) const;
96     std::string rev_comp() const;
97
98     // string-like functions
99     iterator begin();
100     const_iterator begin() const;
101     iterator end();
102     const_iterator end() const;
103     bool empty() const;
104     //! alias for size, (included as this is much like a string)
105     std::string::size_type length() const;
106     //! the number of base pairs in this sequence
107     std::string::size_type size() const;
108     void clear();
109
110     const std::string& get_header() const;
111     //! add a motif to our list of motifs
112     void add_motif(std::string a_motif);
113     std::vector<int> find_motif(std::string a_motif);
114     void save(std::fstream &save_file);
115     void load_museq(std::string load_file_path, int seq_num); 
116 };
117 #endif