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