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