hit_matrix_pane.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:17k
- /*
- * ===========================================================================
- * PRODUCTION $Log: hit_matrix_pane.cpp,v $
- * PRODUCTION Revision 1000.3 2004/06/01 21:10:58 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.9
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: hit_matrix_pane.cpp,v 1000.3 2004/06/01 21:10:58 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Andrey Yazhuk
- *
- * File Description:
- *
- */
- #include <ncbi_pch.hpp>
- #include <corelib/ncbistl.hpp>
- #include <corelib/ncbitime.hpp>
- #include <gui/widgets/hit_matrix/hit_matrix_pane.hpp>
- #include <gui/widgets/hit_matrix/hit_matrix_widget.hpp>
- #include <FL/Fl.H>
- BEGIN_NCBI_SCOPE
- #define AXIS_AREA_W 50
- #define AXIS_AREA_H 30
- CHitMatrixPane::CHitMatrixPane(int PosX, int PosY, int Width, int Height, const char* Label)
- : CGlPaneWidgetChild(PosX, PosY, Width, Height, Label),
- m_BackColor(0.95f, 1.0f, 0.95f),
- m_pTextFont(NULL),
- m_pSeqFont(NULL),
- m_VertSelHandler(eVert),
- m_BottomRuler(true),
- m_TopRuler(true),
- m_LeftRuler(false),
- m_RightRuler(false),
- m_QueryGraph(false),
- m_SubjectGraph(true)
- {
- m_MatrixPane.EnableOffset(true);
- m_SubjectPane.EnableOffset(true);
- m_QueryPane.EnableOffset(true);
- m_Gen.SetIntegerMode(true, true);
- m_Gen.EnableOneBased(true, true);
- m_Grid.EnableIntegerCentering(true);
- m_pTextFont = new CGlBitmapFont(CGlBitmapFont::eHelvetica12);
- m_pSeqFont = new CGlBitmapFont(CGlBitmapFont::eCourier14);
- m_QueryGraph.SetFonts(m_pTextFont, m_pSeqFont);
- m_SubjectGraph.SetFonts(m_pTextFont, m_pSeqFont);
- //setup Event Handlers
- m_HorzSelHandler.SetHost(static_cast<ISelHandlerHost*>(this));
- x_RegisterHandler(dynamic_cast<IEventHandler*>(&m_HorzSelHandler), fSubjectArea, &m_SubjectPane);
- m_VertSelHandler.SetHost(static_cast<ISelHandlerHost*>(this));
- x_RegisterHandler(dynamic_cast<IEventHandler*>(&m_VertSelHandler), fQueryArea, &m_QueryPane);
- m_MouseZoomHandler.SetHost(static_cast<IMouseZoomHandlerHost*>(this));
- x_RegisterHandler(dynamic_cast<IEventHandler*>(&m_MouseZoomHandler), fMatrixArea, &m_MatrixPane);
- m_Graph.SetHost(static_cast<IHitMatrixGraphHost*>(this));
- x_RegisterHandler(dynamic_cast<IEventHandler*>(&m_Graph), fMatrixArea, &m_MatrixPane);
- /* x_AddHandler(dynamic_cast<IEventHandler*>(this), fDescrArea);
- */
- m_Event.StandardConfig();
-
- x_Layout();
- x_SetupAxes();
- }
- void CHitMatrixPane::x_SetupAxes()
- {
- m_TopRuler.SetHorizontal(true, CRuler::eTop);
- m_RightRuler.SetHorizontal(false, CRuler::eRight);
- }
- TVPPoint CHitMatrixPane::GetPortSize(void)
- {
- return TVPPoint(m_rcMatrix.Width(), m_rcMatrix.Height());
- }
- void CHitMatrixPane::SetWidget(CHitMatrixWidget* pParent)
- {
- m_pParent = pParent;
-
- if(m_pParent) {
- m_MatrixPane = x_GetParent()->GetPort();
- }
- }
- void CHitMatrixPane::resize(int x, int y, int w, int h)
- {
- CGlPaneWidgetChild::resize(x, y, w, h);
- x_Layout();
- m_MatrixPane.SetViewport(m_rcMatrix);
-
- m_SubjectPane.SetViewport(m_rcBottomSeq);
- x_AdjsutToMasterPane(m_SubjectPane, true, false);
-
- m_QueryPane.SetViewport(m_rcLeftSeq);
- x_AdjsutToMasterPane(m_QueryPane, false, true);
- }
- void CHitMatrixPane::x_Render(void)
- {
- glClearColor(m_BackColor.GetRed(), m_BackColor.GetGreen(), m_BackColor.GetBlue(), m_BackColor.GetAlpha());
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- // optional render background images
- x_RenderContent();
- }
- void CHitMatrixPane::Update()
- {
- _ASSERT(x_GetParent());
- m_Graph.DeleteGlyphs();
- CHitMatrixDataSource* pDS = x_GetParent()->GetDS();
- if(pDS) {
- CBioseq_Handle q_handle = pDS->GetQueryHandle();
- CBioseq_Handle s_handle = pDS->GetSubjectHandle();
- const CHitMatrixDataSource::THitAdapterCont& hits = pDS->GetHits();
- for( CHitMatrixDataSource::THitAdapterCont::const_iterator it =
- hits.begin(); it != hits.end(); it++ ) {
- //const CHitDataAdapter* hit = *it;
- //cout << "Q Start " << hit.GetQueryStart() << ", Subj Start " << hit.GetSubjectStart() << endl;
- m_Graph.CreateGlyph(&(*it));
- }
-
- if(m_ColorScoreId) {
- m_Graph.AssignColorsByScore(m_ColorScoreId);
- m_ColorScoreId.Reset();
- }
- m_SubjectGraph.SetBioseqHandle(s_handle);
- m_QueryGraph.SetBioseqHandle(q_handle);
- } else {
- CBioseq_Handle h_null;
- m_SubjectGraph.SetBioseqHandle(h_null);
- m_QueryGraph.SetBioseqHandle(h_null);
- }
- }
- void CHitMatrixPane::ColorByScore(CConstRef<CObject_id> score_id)
- {
- m_ColorScoreId = score_id;
- CHitMatrixDataSource* pDS = x_GetParent()->GetDS();
- if(pDS) {
- m_Graph.AssignColorsByScore(score_id);
- }
- }
- typedef multimap<const CHitDataAdapter*, const CHitElemGlyph*> THitToElemGlyphMap;
- bool SubjectStartLess(const CHitElemDataAdapter* el1, const CHitElemDataAdapter* el2)
- {
- return el1->GetSubjectStart() < el2->GetSubjectStart();
- }
- CConstRef<CSeq_align_set> CHitMatrixPane::GetSelectedHits() const
- {
- const TElemGlyphSet& glyphs = m_Graph.GetSelectedGlyphs();
-
- // build a temporary map
- THitToElemGlyphMap hit_to_glyph;
- ITERATE( CHitMatrixPane::TElemGlyphSet, it_G, glyphs) {
- const CHitElemDataAdapter& elem = (*it_G)->GetHitElem();
- const CHitDataAdapter& hit = elem.GetHit();
- hit_to_glyph.insert(THitToElemGlyphMap::value_type(&hit, *it_G));
- }
- // create a new seq-align-set
- CRef<CSeq_align_set> set(new CSeq_align_set);
- CSeq_align_set::Tdata ls_rf_aligns = set->Set();
- const CHitDataAdapter* p_curr_hit = NULL;
- CSeq_align* p_align = NULL;
- CDense_seg* p_denseg = NULL;
- vector<const CHitElemDataAdapter*> elems;
-
- // iterate by Glyphs
- for( THitToElemGlyphMap::const_iterator it = hit_to_glyph.begin();
- it != hit_to_glyph.end(); ) {
- // for a new Hit create a CSeq_align
- p_curr_hit = it->first;
- p_align = new CSeq_align();
- p_align->SetType(CSeq_align::eType_partial);
- ls_rf_aligns.push_back(CRef<CSeq_align>(p_align)); // add to set
- CSeq_align::C_Segs& segs = p_align->SetSegs();
- segs.Select(CSeq_align::C_Segs::e_Denseg);
- p_denseg = & segs.SetDenseg();
-
- // for all elements of the current hit - add to list
- for( ; it != hit_to_glyph.end() && it->first == p_curr_hit; it++ ) {
- const CHitElemDataAdapter& elem = it->second->GetHitElem();
- elems.push_back(&elem);
- }
- int size = elems.size();
- if(size)
- sort(elems.begin(), elems.end(), SubjectStartLess);
- p_denseg->SetDim(2);
- p_denseg->SetNumseg(size);
- p_denseg->SetStarts().resize(2*size);
- p_denseg->SetLens().resize(size);
- p_denseg->SetStrands().resize(size);
- // add elems to denseg
- int i_start = 0, i_len = 0;
- ITERATE(vector<const CHitElemDataAdapter*>, it_e, elems) {
- const CHitElemDataAdapter* elem = *it_e;
- p_denseg->SetStarts()[i_start++] = elem->GetSubjectStart();
- p_denseg->SetStarts()[i_start++] = elem->GetQueryStart();
- p_denseg->SetLens()[i_len++] = elem->GetLength();
- }
- elems.clear();
- }
- return ConstRef<CSeq_align_set>(set);
- }
- TModelRect CHitMatrixPane::GetSelectedHitElemsRect() const
- {
- const TElemGlyphSet& glyphs = m_Graph.GetSelectedGlyphs();
- bool b_first = true;
-
- int left, right, top, bottom;
- ITERATE( CHitMatrixPane::TElemGlyphSet, it_G, glyphs) {
- const CHitElemDataAdapter& elem = (*it_G)->GetHitElem();
-
- int s_from = elem.GetSubjectStart();
- int q_from = elem.GetQueryStart();
- int len = elem.GetLength();
- int s_to = s_from + len;
- int q_to = q_from + len;
-
- if(q_from > q_to)
- swap(q_from, q_to);
- if(s_from > s_to)
- swap(s_from, s_to);
- if(b_first) {
- left = s_from; right = s_to;
- bottom = q_from; top = q_to;
- b_first = false;
- } else {
- left = min(left, s_from);
- right = max(right, s_to);
- bottom = min(bottom, q_from);
- top = max(top, q_to);
- }
- }
- return b_first ? TModelRect(0, 0) : TModelRect(left, bottom, right + 1, top + 1);
- }
- const CHitMatrixPane::TRangeColl& CHitMatrixPane::GetSubjectSelection() const
- {
- return m_HorzSelHandler.GetSelection();
- }
- const CHitMatrixPane::TRangeColl& CHitMatrixPane::GetQuerySelection() const
- {
- return m_VertSelHandler.GetSelection();
- }
- void CHitMatrixPane::x_Layout(void)
- {
- TVPPoint subj_gr_size = m_SubjectGraph.PreferredSize();
- TVPPoint query_gr_size = m_QueryGraph.PreferredSize();
- int mx_left = query_gr_size.X() + AXIS_AREA_W;
- int mx_bottom = subj_gr_size.Y() + AXIS_AREA_H;
- int mx_right = w() - AXIS_AREA_W;
- int mx_top = h() - AXIS_AREA_H;
- m_rcMatrix.Init(mx_left, mx_bottom, mx_right, mx_top);
- m_rcAxes.Init(query_gr_size.X(), subj_gr_size.Y(), w(), h());
- m_rcLeftSeq.Init(0, mx_bottom, query_gr_size.X(), mx_top);
- m_rcBottomSeq.Init(mx_left, 0, mx_right, subj_gr_size.Y());
- }
- void CHitMatrixPane::x_RenderContent(void)
- {
- if(x_GetParent()) {
- const CGlPane& VP = x_GetParent()->GetPort();
-
- // prepare CGlPanes
- TModelRect rc_M = VP.GetModelLimitsRect();
- m_MatrixPane.SetModelLimitsRect(rc_M);
- TModelRect rc_V = VP.GetVisibleRect();
- m_MatrixPane.SetVisibleRect(rc_V);
- x_AdjsutToMasterPane(m_SubjectPane, true, false);
- x_AdjsutToMasterPane(m_QueryPane, false, true);
- // now rendering
- x_RenderAxisAndGrid(m_MatrixPane);
- x_RenderSeqPanes();
- m_Graph.Render(m_MatrixPane);
- x_RenderSelHandler(true, m_MatrixPane, CLinearSelHandler::ePassiveState);
- x_RenderSelHandler(false, m_MatrixPane, CLinearSelHandler::ePassiveState);
- x_RenderMouseZoomHandler(m_MatrixPane);
- }
- }
- void CHitMatrixPane::x_RenderAxisAndGrid(CGlPane& gr_pane)
- {
- CGlPane pane(CGlPane::eNeverUpdate);
- pane.SetViewport(m_rcAxes);
-
- m_Grid.Render(&pane, &gr_pane, &m_Gen);
- // draw horizonatl rulers
- // draw bottom ruler
- TVPRect rc_VP(m_rcMatrix.Left(), m_rcAxes.Bottom(), m_rcMatrix.Right(), m_rcMatrix.Bottom() - 1);
- pane.SetViewport(rc_VP);
- x_AdjsutToMasterPane(pane, true, false);
- m_BottomRuler.Render(pane);
- // draw top ruler
- rc_VP.Init(m_rcMatrix.Left(), m_rcMatrix.Top() + 1, m_rcMatrix.Right(), m_rcAxes.Top());
- pane.SetViewport(rc_VP);
- x_AdjsutToMasterPane(pane, true, false);
-
- m_TopRuler.Render(pane);
- // draw vertical rulers
- // draw left ruler
- rc_VP.Init(m_rcAxes.Left(), m_rcMatrix.Bottom(), m_rcMatrix.Left() - 1, m_rcMatrix.Top());
- pane.SetViewport(rc_VP);
- x_AdjsutToMasterPane(pane, false, true);
- m_LeftRuler.Render(pane);
- // draw right ruler
- rc_VP.Init(m_rcMatrix.Right() + 1, m_rcMatrix.Bottom(), m_rcAxes.Right(), m_rcMatrix.Top());
- pane.SetViewport(rc_VP);
- x_AdjsutToMasterPane(pane, false, true);
- m_RightRuler.Render(pane);
- }
- void CHitMatrixPane::x_RenderSeqPanes()
- {
- m_SubjectGraph.Render(m_SubjectPane);
- x_RenderSelHandler(true, m_SubjectPane, CLinearSelHandler::eActiveState);
-
- m_QueryGraph.Render(m_QueryPane);
- x_RenderSelHandler(false, m_QueryPane, CLinearSelHandler::eActiveState);
- }
- void CHitMatrixPane::x_AdjsutToMasterPane(CGlPane& pane, bool b_model_x, bool b_model_y)
- {
- const CGlPane& VP = x_GetParent()->GetPort();
-
- TModelRect rc_vis = VP.GetVisibleRect();
- TModelRect rc_lim = VP.GetModelLimitsRect();
- // assuming that Viewport in the pane has been set properly
- if(! b_model_x) { // adjust horz range to represent pixels
- int max_x = pane.GetViewport().Width() - 1;
- rc_lim.SetHorz(0, max_x);
- rc_vis.SetHorz(0, max_x);
- }
-
- if(! b_model_y) { // adjust vert range to represent pixels
- int max_y = pane.GetViewport().Height() - 1;
- rc_lim.SetVert(0, max_y);
- rc_vis.SetVert(0, max_y);
- }
- pane.SetModelLimitsRect(rc_lim);
- pane.SetVisibleRect(rc_vis);
- }
- int CHitMatrixPane::x_GetAreaByMousePos()
- {
- int vp_x = Fl::event_x();
- int vp_y = h() - Fl::event_y();
- if(m_rcMatrix.PtInRect(vp_x, vp_y)) {
- return fMatrixArea;
- } else if(m_rcBottomSeq.PtInRect(vp_x, vp_y)) {
- return fSubjectArea;
- } else if(m_rcLeftSeq.PtInRect(vp_x, vp_y)) {
- return fQueryArea;
- }
- return fOther;
- }
- void CHitMatrixPane::x_RenderMouseZoomHandler(CGlPane& pane)
- {
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- m_MouseZoomHandler.Render(pane);
- glDisable(GL_BLEND);
- }
- void CHitMatrixPane::x_RenderSelHandler(bool b_horz, CGlPane& pane, CLinearSelHandler::ERenderingOption option)
- {
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- CLinearSelHandler& handler = b_horz ? m_HorzSelHandler : m_VertSelHandler;
- handler.Render(pane, option);
- glDisable(GL_BLEND);
- }
- ////////////////////////////////////////////////////////////////////////////////
- /// IAlnMarkHandlerHost implementation
- TModelUnit CHitMatrixPane::MZHH_GetScale(EScaleType type)
- {
- const CGlPane& VP = x_GetParent()->GetPort();
- switch(type) {
- case eCurrent: return VP.GetScaleX();
- case eMin: return VP.GetMinScaleX();
- case eMax: return VP.GetZoomAllScaleX();
- default: _ASSERT(false); return -1;
- }
- }
- void CHitMatrixPane::MZHH_SetScale(TModelUnit scale, const TModelPoint& point)
- {
- x_GetParent()->OnSetScaleX(scale, point);
- }
- void CHitMatrixPane::MZHH_ZoomRect(const TModelRect& rc)
- {
- x_GetParent()->ZoomRect(rc);
- }
- void CHitMatrixPane::MZHH_Scroll(TModelUnit d_x, TModelUnit d_y)
- {
- x_GetParent()->Scroll(d_x, d_y);
- }
- TVPUnit CHitMatrixPane::MZHH_GetVPPosByY(int y) const
- {
- return h() - 1 - y;
- }
- void CHitMatrixPane::MZHH_Redraw(void)
- {
- redraw();
- }
- TVPUnit CHitMatrixPane::HMGH_GetVPPosByY(int y) const
- {
- return h() - 1 - y;
- }
- void CHitMatrixPane::HMGH_Redraw(void)
- {
- redraw();
- }
- ////////////////////////////////////////////////////////////////////////////////
- /// ISelHandlerHost implementation
- void CHitMatrixPane::SHH_Redraw()
- {
- redraw();
- }
- TModelUnit CHitMatrixPane::SHH_GetModelByWindow(int z, EOrientation orient)
- {
- switch(orient) {
- case eHorz: return m_MatrixPane.UnProjectX(z);
- case eVert: return m_MatrixPane.UnProjectY(h() - z);
- default: _ASSERT(false); return -1;
- }
- }
- TVPUnit CHitMatrixPane::SHH_GetWindowByModel(TModelUnit z, EOrientation orient)
- {
- switch(orient) {
- case eHorz: return m_MatrixPane.ProjectX(z);
- case eVert: return h() - m_MatrixPane.ProjectY(z);
- default: _ASSERT(false); return -1;
- }
- }
- void CHitMatrixPane::x_OnShowPopup()
- {
- x_GetParent()->OnShowPopup();
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: hit_matrix_pane.cpp,v $
- * Revision 1000.3 2004/06/01 21:10:58 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.9
- *
- * Revision 1.9 2004/05/21 22:27:54 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.8 2004/02/12 21:05:21 yazhuk
- * Added GetSelectedHitElemsRect() const; implemented x_OnShowPopup();
- * Enabled "offset" mode in CGlPanes
- *
- * Revision 1.7 2004/01/08 19:48:05 yazhuk
- * Minor clean-up
- *
- * Revision 1.6 2003/12/10 16:57:18 yazhuk
- * Updated implementation of IMouseZoomHandlerHost interface.
- *
- * Revision 1.5 2003/12/06 04:19:13 ucko
- * Avoid make_pair<>, which is broken on WorkShop.
- *
- * Revision 1.4 2003/12/05 17:35:23 yazhuk
- * Functions for retrieving selection, support for coloration by score
- *
- * Revision 1.3 2003/12/01 17:07:13 yazhuk
- * Eliminated IHitMatrixParent; refactored event handling; added support for linear selection.
- *
- * Revision 1.2 2003/11/18 17:57:23 yazhuk
- * Fixed GCC warnings
- *
- * Revision 1.1 2003/11/17 20:41:19 yazhuk
- * Initial revision
- *
- * ===========================================================================
- */