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