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