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