throw errors when spirit parsing fails
[mussa.git] / alg / mussa.hpp
1 #ifndef _MUSSA_CLASS_H_
2 #define _MUSSA_CLASS_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 //                          ---------- mussa_class.hh -----------
15 //                        ----------------------------------------
16 #include <QObject> 
17
18 #include <boost/filesystem/path.hpp>
19 #include <boost/shared_ptr.hpp>
20
21 #include <list>
22 #include <string>
23 #include <vector>
24 #include <set>
25 #include <istream>
26
27 #include "alg/annotation_colors.hpp"
28 #include "alg/mussa_callback.hpp"
29 #include "alg/nway_paths.hpp"
30 #include "alg/sequence.hpp"
31
32 std::string int_to_str(int an_int);
33
34 class Mussa : public QObject
35 {
36     Q_OBJECT 
37
38 signals:
39     //! call whatever signaling system we want
40     void progress(const std::string& description, int cur, int max);
41
42 public:
43     typedef std::set<Sequence> motif_set;
44     enum analysis_modes { TransitiveNway, RadialNway, EntropyNway, 
45                           RecursiveNway };
46
47     Mussa();
48     Mussa(const Mussa &);
49
50     //! save all of mussa
51     void save(boost::filesystem::path save_path="");
52     //! save the nway comparison
53     void save_muway(boost::filesystem::path save_path);
54     //! load a saved analysis directory
55     void load(boost::filesystem::path ana_path);
56     // ! return path to the where the analysis is stored
57     boost::filesystem::path get_analysis_path() const;
58     //! set analysis path
59     void set_analysis_path(boost::filesystem::path);
60
61     //! clear parameters and initialize data lists
62     void clear();
63
64     //! set parameters from a file - 'mupa' ~ mussa parameters
65     void load_mupa_file(std::string para_file_path) { load_mupa_file(boost::filesystem::path(para_file_path));}
66     void load_mupa_file(boost::filesystem::path para_file_path);
67
68     // set parameters individually (eg from user input into gui classes)
69     //! set analysis name
70     void set_name(std::string a_name);
71     //! return name for this analysis
72     std::string get_name() const;
73     //! return a reasonable window title for this analysis
74     /*! this returns the "variable" portion for a title
75      */
76     std::string get_title() const;
77
78     //! return number of sequences in this analyzis
79     /*! this returns either the_seqs.size() or seq_files.size()
80      *  depending on which has data loaded in
81      *  (silly delayed loading of sequence data)
82      */
83     int size() const;
84
85     //! set number of bases for this window size
86     void set_window(int a_window);
87     //! get number of bases for the sliding window
88     int get_window() const;
89     //! set number of bases that must match for a window to be saved 
90     //! if threshold > soft_threshold this also sets soft_threshold
91     void set_threshold(int a_threshold);
92     //! get number of bases that must match for a window to be saved
93     int get_threshold() const;
94     //! sets the threshold used for computing the nway paths 
95     //! must be in range [threshold..window size]
96     void set_soft_threshold(int sft_thres);
97     int get_soft_threshold() const;
98  
99     void set_analysis_mode(enum analysis_modes new_ana_mode);
100     enum analysis_modes get_analysis_mode() const;
101     //! return a string name for an analysis mode
102     std::string get_analysis_mode_name() const;
103     //! return if we have unsaved changes
104     bool is_dirty() const;
105     //! is there anything loaded into this analysis?
106     bool empty() const;
107
108     //! return the refined paths found by the nway analysis.
109     const NwayPaths& paths() const;
110
111     //! given selected_paths, and view_paths, compute per base pair matches
112     //template <class IteratorT>
113     void createLocalAlignment(std::list<ConservedPath>::iterator begin, 
114                               std::list<ConservedPath>::iterator end,
115                               std::list<ConservedPath::path_type>& result,
116                               std::list<std::vector<bool> >& reversed);
117
118     //! run seqcomp and the nway filtering algorithm.
119     /*!analyze will run seqcomp and then the nway algorithm
120      * on whatever sequences have been loaded into this mussa instance.
121      * \throws mussa_analysis_error 
122      */
123     void analyze();
124     /*! Run the nway filtering algorithm, 
125      *  this might be used when changing the soft threshhold?
126      */
127     void nway();
128
129     //! appends a string sequence to the list of the_seqs
130     // void append_sequence(std::string a_seq);
131     //! appends a sequence to the list of the_seqs (makes copy)
132     void append_sequence(const Sequence& a_seq);
133     //! append a sequence to the list of seqs (shared)
134     void append_sequence(boost::shared_ptr<Sequence> a_seq);
135
136     //! Load a sequence from a fasta file and any annotations
137     /*! \param[in] seq_file the full path to the fasta file
138      *  \param[in] annot_file the full path to an annotation file,
139      *             if is an empty string, we won't bother loading anything
140      *  \param[in] fasta_index specify which sequence in a multisequence fasta
141      *             file
142      *  \param[in] sub_seq_start starting slice index to select a subsequence
143      *             use 0 start from the beginning.
144      *  \param[in] sub_seq_end ending slice index to select a subsequence
145      *             use 0 to go to the end.
146      *  \param[in] name sequence name, only used if not null
147      */
148     void load_sequence(boost::filesystem::path seq_file, 
149                        boost::filesystem::path annot_file, 
150                        int fasta_index, int sub_seq_start=0, int sub_seq_end=0,
151                        std::string *name=0);
152     //! allow examining the sequences we have loaded
153     typedef std::vector<boost::shared_ptr<Sequence> > vector_sequence_type;
154     const vector_sequence_type& sequences() const;
155
156     // deprecated - support bridge for python version of mussa
157     // these save & load from the old file format
158     void save_old();
159     void load_old(char * load_file_path, int s_num);
160
161     // manage motif lists 
162     //! add a motif it wont be applied until update_sequences_motif is called
163     void add_motif(const Sequence& motifs, const Color& colors);
164     //! add vector of motifs and colors to our motif collection 
165     /*! this will automatically call update_sequences_motif
166      *  this depends on sets and color maps being unique
167      *  (aka if you add the same item more than once it doesn't
168      *  increase the size of the data structure)
169      */
170     void set_motifs(const std::vector<Sequence>& motifs, 
171                     const std::vector<Color>& colors);
172     /*! The file should look something like
173      *  <sequence> <red> <green> <blue>
174      *  where sequence is a string of IUPAC symbols
175      *  and red,green,blue are a white space separated list of floats
176      *  in the range [0.0, 1.0]
177      */
178     //! load a list of motifs from a file named filename
179     void load_motifs(boost::filesystem::path filename);
180     //! load motifs from an ifstream
181     /*! \sa Mussa::load_motifs(boost::filesystem::path)
182      */
183     void load_motifs(std::istream &);
184     //! return our motifs;
185     const motif_set& motifs() const;
186
187     //! return color mapper
188     boost::shared_ptr<AnnotationColors> colorMapper();
189
190   private:
191     //! push motifs to our attached sequences
192     void update_sequences_motifs();
193
194     // Private variables
195     // parameters needed for a mussa analysis
196     //! name of this analysis. (will also be used when saving an analysis)
197     std::string analysis_name;
198     //! how many base pairs to include in a sliding window
199     int window;
200     //! how many base pairs need to match order to record a window as conserved
201     int threshold;
202     //! stores current filter used by GUI to change the connections shown
203     int soft_thres;
204     //! which nway comparison algorithm to use.
205     enum analysis_modes ana_mode;
206     double ent_thres;
207     //! should we append _w<window_size> to the saved analysis
208     bool win_append; 
209     //! should we append _t<threshold> to the saved analysis
210     bool thres_append;
211
212     //! sequence data
213     vector_sequence_type the_seqs;
214     //! the seqcomp data
215     std::vector<std::vector<FLPs> > all_comps;
216     //! N-way data, ie the mussa results  
217     NwayPaths the_paths;
218
219     //! motif list
220     motif_set motif_sequences;
221     //! color manager
222     boost::shared_ptr<AnnotationColors> color_mapper;
223     //! path to our analysis
224     boost::filesystem::path analysis_path;
225     //! flag indicating if we have unsaved changes
226     bool dirty;
227
228     // Private methods
229     //! runs all the seqcomps needed to support the nway comparison
230     void seqcomp();
231
232 };
233 //! provide a simple name to point to our Mussa shared_ptr
234 typedef boost::shared_ptr<Mussa> MussaRef;
235 #endif