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