ticket:104 fix some pointer problems with MussaAlignedWindow
[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 start, int end, std::string type, std::string name);
31   
32   int start;
33   int end;
34   std::string type;
35   std::string name;
36 };
37
38 /* The way that motifs are found currently doesn't really 
39  * indicate that the match was a reverse compliment
40  */
41 struct motif : public annot
42 {
43   std::string sequence;
44
45   //! this constructor is for when we're adding motifs to our annotations
46   motif(int start, std::string motif);
47 };
48
49 //! sequence track for mussa.
50 class Sequence
51 {
52   private:
53     std::string sequence;
54     std::string header;
55     std::string species;
56
57     std::list<annot> annots;
58     //! a seperate list for motifs since we're currently not saving them
59     std::list<motif> motif_list;
60
61     void motif_scan(std::string a_motif, std::vector<int> * motif_match_starts);
62     std::string rc_motif(std::string a_motif);
63     //! look for a string sequence type and and it to an annotation list
64     void add_string_annotation(std::string a_seq, std::string name);
65
66   public:
67     typedef std::string::iterator iterator;
68     typedef std::string::const_iterator const_iterator;
69     typedef std::string::size_type size_type;
70
71     Sequence();
72     Sequence(std::string seq);
73     //! assignment to constant sequences
74     Sequence &operator=(const Sequence&);
75     Sequence &operator=(const std::string &);
76     char operator[](int) const;
77     friend std::ostream& operator<<(std::ostream& out, const Sequence& seq);
78
79     //! set sequence to a (sub)string containing nothing but AGCTN
80     void set_filtered_sequence(const std::string& seq, 
81                                std::string::size_type start=0, 
82                                std::string::size_type count=0);
83
84     //! load sequence AGCT from fasta file
85     //! \throw mussa_load_error
86     //! \throw sequence_empty_error
87     //! \throw sequence_empty_file_error
88     void load_fasta(const boost::filesystem::path file_path, int seq_num=1, 
89                     int start_index=0, int end_index=0);
90     //! load sequence from stream
91     //! \throw mussa_load_error
92     //! \throw sequence_empty_error
93     //! \throw sequence_empty_file_error
94     void load_fasta(std::iostream& file, int seq_num=1, 
95                     int start_index=0, int end_index=0);
96     //! load sequence annotations
97     //! \throws mussa_load_error 
98     void load_annot(const boost::filesystem::path file_path, int start_index, int end_index);
99     //! load sequence annotations
100     //! \throws mussa_load_error 
101     void load_annot(std::fstream& data_stream, int start_index, int end_index);
102     void parse_annot(std::string data, int start_index, int end_index);
103     const std::list<annot>& annotations() const;
104     const std::list<motif>& motifs() const;
105     const std::string& get_species() const;
106
107     // simple access functions
108     void set_seq(const std::string& a_seq);
109     const std::string& get_seq() const;
110     const char *c_seq() const;
111     std::string subseq(int start, int count) const;
112     std::string rev_comp() const;
113
114     // string-like functions
115     iterator begin();
116     const_iterator begin() const;
117     iterator end();
118     const_iterator end() const;
119     bool empty() const;
120     //! alias for size, (included as this is much like a string)
121     std::string::size_type length() const;
122     //! the number of base pairs in this sequence
123     std::string::size_type size() const;
124     void clear();
125
126     void set_header(std::string header);
127     std::string get_header() const;
128     //! add a motif to our list of motifs
129     //! \throws motif_normalize_error if there's something wrong with a_motif
130     void add_motif(std::string a_motif);
131     //! clear our list of found motifs
132     void clear_motifs();
133     //! search a sequence for a_motif
134     //! \throws motif_normalize_error if there's something wrong with a_motif
135     std::vector<int> find_motif(std::string a_motif);
136     //! convert IUPAC symbols to upperase
137     //! \throws motif_normalize_error if there is an invalid symbol
138     static std::string motif_normalize(std::string a_motif);
139
140     //! annotate the current sequence with other sequences
141     void find_sequences(std::list<Sequence>::iterator start, 
142                         std::list<Sequence>::iterator end);
143
144     void save(boost::filesystem::fstream &save_file);
145     void load_museq(boost::filesystem::path load_file_path, int seq_num); 
146 };
147
148 #endif