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