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