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