make the path view independent of type of connection
[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 #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   public:
66     typedef std::string::iterator iterator;
67     typedef std::string::const_iterator const_iterator;
68     typedef std::string::size_type size_type;
69
70     Sequence();
71     Sequence(std::string seq);
72     //! assignment to constant sequences
73     Sequence &operator=(const Sequence&);
74     Sequence &operator=(const std::string &);
75     friend std::ostream& operator<<(std::ostream& out, const Sequence& seq);
76
77     //! set sequence to a (sub)string containing nothing but AGCTN
78     void set_filtered_sequence(const std::string& seq, 
79                                std::string::size_type start=0, 
80                                std::string::size_type count=0);
81
82     //! load sequence AGCT from fasta file
83     /*! \throws mussa_load_error 
84      */
85     void load_fasta(const std::string file_path, int seq_num=1, 
86                     int start_index=0, int end_index=0);
87     //! load sequence annotations
88     /*! \throws mussa_load_error 
89      */
90     void load_annot(const std::string file_path, int start_index, int end_index);
91     const std::list<annot>& annotations() const;
92     const std::list<motif>& motifs() const;
93
94     // simple access functions
95     void set_seq(const std::string& a_seq);
96     const std::string& get_seq() const;
97     const char *c_seq() const;
98     std::string subseq(int start, int end) const;
99     std::string rev_comp() const;
100
101     // string-like functions
102     iterator begin();
103     const_iterator begin() const;
104     iterator end();
105     const_iterator end() const;
106     bool empty() const;
107     //! alias for size, (included as this is much like a string)
108     std::string::size_type length() const;
109     //! the number of base pairs in this sequence
110     std::string::size_type size() const;
111     void clear();
112
113     const std::string& get_header() const;
114     //! add a motif to our list of motifs
115     /*! \throws motif_normalize_error if there's something wrong with a_motif
116      */
117     void add_motif(std::string a_motif);
118     //! clear our list of found motifs
119     void clear_motifs();
120     //! search a sequence for a_motif
121     /*! \throws motif_normalize_error if there's something wrong with a_motif
122      */
123     std::vector<int> find_motif(std::string a_motif);
124     //! convert IUPAC symbols to upperase
125     /*! \throws motif_normalize_error if there is an invalid symbol
126      */
127     static std::string motif_normalize(std::string a_motif);
128     void save(std::fstream &save_file);
129     void load_museq(std::string load_file_path, int seq_num); 
130 };
131 #endif