Store Sequence sequence location in a shared_ptr class
[mussa.git] / alg / glseqbrowser.hpp
1 #ifndef _GLTRACKS_H_
2 #define _GLTRACKS_H_
3 #include <boost/shared_ptr.hpp>
4
5 #include <map>
6 #include <set>
7 #include <vector>
8
9 #include "alg/annotation_colors.hpp"
10 #include "alg/sequence.hpp"
11 #include "alg/glsequence.hpp"
12 #include "alg/sequence_location.hpp"
13 #include "alg/track_region.hpp"
14
15 //! Manage rendering a collection of glSequences
16 class GlSeqBrowser
17 {
18 public:
19   GlSeqBrowser();
20   GlSeqBrowser(const GlSeqBrowser&);
21
22   //! setup the opengl canvas
23   void initializeGL();
24   //! called when our screen canvas is resized
25   void resizeGL(int width, int height);
26   //! render our scene
27   void paintGL() const;
28
29   //! select a region (using canvas coordinates)
30   void selectRegion(int top, int left, int bottom, int right);
31
32   //! border size
33   float border() const;
34
35   //! max world left coordinate
36   float left() const;
37   //! max world right coordinate
38   float right() const;
39   // return how wide is a pixel in world coordinates?
40   float get_pixel_width() const;
41
42   void setViewportCenter(float x);
43   //! return world coordinate of the left side of the viewport
44   float viewportLeft() const;
45   //! return world coordinate of the center of the viewport
46   float viewportCenter() const;
47   //! return world coordinate of the right side of the viewport
48   float viewportRight() const;
49   //! return height of the viewport in world coordinates
50   float viewportHeight() const;
51   //! return width of the viewport in world coordinates
52   float viewportWidth() const;
53   
54   //! return viewport height in pixels
55   int viewportPixelHeight() const;
56   //! return viewport width in pixels
57   int viewportPixelWidth() const;
58
59   //! zoom out far enough to show the full size of the sequence
60   double zoomOut();
61   //! zoom in to a reasonable sequence view
62   double zoomToSequence();
63   //! set the current zoom level in (base pairs / pix )
64   //! its a double as zoom levels of [0.01, 1.0] make nice sequence views
65   void setZoom(double zoom_level);
66   //! returns the current zoom level as basepairs per pixel
67   double zoom() const;
68   //! center the provided path in the current viewport
69   void centerOnPath(const std::vector<int>&);
70
71   void setColorMapper(boost::shared_ptr<AnnotationColors> cm);
72   const AnnotationColors& colorMapper();
73
74   //! clear our tracks and connections
75   void clear();
76   //! clear everything related to a selection
77   void clear_selection();
78
79   //! add a sequence to the back of our track container (makes copy of s)
80   void push_sequence(const Sequence& s);
81   //! add a sequence to the back of our track container
82   void push_sequence(boost::shared_ptr<Sequence> s);
83   //! add a glsequence to the back of our track container
84   void push_sequence(GlSequence s);
85   //! add a glsequence to the back of our track container
86   void push_sequence(boost::shared_ptr<GlSequence> gs);
87   //! return our track container
88   const std::vector<boost::shared_ptr<GlSequence> >& sequences() const;
89
90   //! clear all the line segments between all the sequences
91   void clear_links();
92   //! define a path
93   void link(const std::vector<int>& path, const std::vector<bool>& isRC, int length);
94   //! set selected paths (it'd be nice if this could be a templated function)
95   void setSelectedPaths(std::vector<int> c);
96   //! returns the index of pathids based on order added by link
97   const std::set<int>& selectedPaths() const;
98
99   //! set selected tracks (it'd be nice if this could be a templated function)
100   void appendSelectedTrack(GLuint track_index, int left, int right);
101
102   //! return list of selected tracks
103   std::list<TrackRegion> selectedTracks() const;
104
105   //! copy sequence from selected track using formating function
106   template<class Item>
107   size_t copySelectedTracks(std::list<Item>& result, 
108              Item (*format_track)(const Sequence& s, int left, int right));
109   //! copy sequence from selected tracks as FASTA sequences
110   /*! \return number of base pairs copied
111    */
112   size_t copySelectedTracksAsFasta(std::string& copy_buffer);
113   //! copy sequence from selected tracks as a list of sequences
114   /*! \return number of base pairs copied
115    */
116   size_t copySelectedTracksAsSequences(std::list<Sequence>& result);
117   //! copy sequence from selected tracks as plain sequences
118   /*! \return number of base pairs copied
119    */
120   size_t copySelectedTracksAsString(std::string& copy_buffer);
121
122   //! copy tracks as a sequence and its coordinates
123   size_t copySelectedTracksAsSeqLocation(std::list<SequenceLocation>& result);
124   
125   
126   //! Provide a logical name for a type discriminator for our glName stack
127   enum FeatureType { MussaTrack, MussaSegment };
128
129   //! a useful point class
130   template<class T> struct point {
131     T x;
132     T y;
133
134     point(T x_, T y_):x(x_), y(y_) {}
135   };
136
137   //! a useful rectangle, where 0,0 is in the lower left
138   template<class T> struct rect {
139     T top;
140     T left;
141     T bottom;
142     T right;
143
144     rect() {}
145     rect(T t, T l, T b, T r) : top(t), left(l), bottom(b), right(r) {}
146     T width() { return right - left; }
147     T height() { return top - bottom; }
148     void clear() { top=0; left=0; bottom=0; right=0; }
149   };
150
151   struct Segment
152   {
153     point<float> start;
154     point<float> end;
155     bool reversed;
156     int length;
157     // each integer represents an index into our list of paths
158     std::set<int> path_ids;
159
160     Segment() : start(0.0, 0.0), end(0.0, 0.0), reversed(false), length(0) {}
161     Segment(float x1, float y1, float x2, float y2, bool isRC, int length_) 
162       : start(x1, y1), end(x2, y2), reversed(isRC), length(length_) {}
163   };
164
165   //! data structure holding our line segments
166   /*! the vector is of size track_container.size()-1
167    *  it's indexed by the pair x1, x2 (the two x coordinates between
168    *  the two tracks
169    */
170   typedef std::pair<int, int> segment_key;  
171   typedef std::map<segment_key, Segment> pair_segment_map;
172   typedef std::vector<pair_segment_map> path_segment_map_vector;
173   path_segment_map_vector path_segments;
174
175 private:
176   //! recalculate the viewable world
177   /*! depending on the size of our canvas, our zoom level and
178    *  how far we've been offset
179    */
180   void update_viewport(float center, double new_zoom);
181
182   //! determine where all the tracks should be placed
183   void update_layout();
184
185   //! convert opengl selections into the list of paths we should highlight
186   void processSelection(GLuint hits, 
187                         GLuint buffer[], 
188                         GLuint bufsize, 
189                         const rect<float>& r);
190
191   //! master scene drawing function
192   /*! draw is broken out for the opengl selection code
193    */
194   void draw() const;
195   //! draw glsequence tracks
196   void draw_tracks() const;
197   //! draw line segments between tracks
198   void draw_segments() const;
199   //! draw selection box
200   void draw_selection() const;
201
202   //! number of pixels to reserve around the edges of our canvas
203   const int border_width;
204   //! the current viewable region (world coord)
205   rect<float> cur_ortho;
206   //! how many pixels our viewport is (screen coord)
207   point<int> viewport_size;
208   //! the center of our current viewport (world coord) (used for scrollbar)
209   float viewport_center;
210   double zoom_level;
211   boost::shared_ptr<AnnotationColors> color_mapper;
212   //! counter for each path added to us via connect
213   int pathid;
214
215 protected:
216   //! container of all the GlSequences loaded into our scene
217   std::vector<boost::shared_ptr<GlSequence> > track_container;
218   //! where to draw our box (world coordinates)
219   rect<float> selectedRegion;
220   //! true if we have a selection
221   bool selectedMode;
222   //! indicate which paths are selected
223   std::set<int> selected_paths;
224   //! which track is selected (it only makes sense to have one track selected).
225   std::list<TrackRegion> selected_tracks;
226   typedef std::list<TrackRegion>::iterator selected_track_iterator;
227 };
228 #endif