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