NCBI C++ ToolKit
blast_hitmatrix.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: blast_hitmatrix.cpp 46176 2021-01-28 17:07:49Z grichenk $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Irena Zaretskaya
27  *
28  * File Description:
29  */
30 
31 #include <ncbi_pch.hpp>
32 
33 #include <corelib/ncbistl.hpp>
35 
36 #include <util/image/image.hpp>
38 #include <util/image/image_io.hpp>
39 
40 #include <objmgr/util/sequence.hpp>
41 
45 #include <gui/opengl/glrender.hpp>
46 #include <gui/opengl/glresmgr.hpp>
47 #include "blast_hitmatrix.hpp"
48 
50 
52 {
55 
56  // create our object manager
59 }
60 
61 
63 {
65 
66  TModelRect rc_m(0, 0, 1000, 1000);
67  CBioseq_Handle s_handle = m_DataSource->GetSubjectHandle();
68  if(s_handle) {
69  rc_m.SetHorz(0, s_handle.GetBioseqLength());
70  } else {
71  TSeqRange s_r = m_DataSource->GetSubjectHitsRange();
72  rc_m.SetHorz(s_r.GetFrom(), s_r.GetToOpen());
73  }
74 
75  CBioseq_Handle q_handle = m_DataSource->GetQueryHandle();
76  if(q_handle) {
77  rc_m.SetVert(0, q_handle.GetBioseqLength());
78  } else {
79  TSeqRange q_r = m_DataSource->GetQueryHitsRange();
80  rc_m.SetVert(q_r.GetFrom(), q_r.GetToOpen());
81  }
82 
84 }
85 
86 
88  int height,
89  int width,
91  const string& font_path)
92 {
93  ITERATE (CSeq_annot::TData::TAlign, iter, seqAligns) {
94  CRef< CSeq_align > seq_align = *iter;
95  m_Aligns.push_back(CConstRef<CSeq_align>(seq_align));
96  }
97  m_Height = height;
98  m_Width = width;
99  m_Format = format;
100  m_FileOut = false;
101  m_Thumbnail = false;
102 
103  x_InitGraphics(font_path);
104  m_Renderer.reset(new CHitMatrixRenderer);
105 }
106 
107 
109 {
110  for(size_t i=0; i < m_Aligns.size();i++) {
111  const CSeq_id& queryID = m_Aligns[i]->GetSeq_id(0);
112  /* Somehow code for IsLocal stopped working
113  if(queryID.IsLocal()) {
114  (*sequence::GetId(queryID,*m_Scope,sequence:: eGetId_Best).GetSeqId()).GetLabel(&m_QueryID);
115  }
116  else {
117  CLabel::GetLabel(queryID,&m_QueryID,CLabel::eDefault,m_Scope.GetPointer());
118  }
119  */
121  if(queryID.IsLocal()) {
122  m_QueryID = "lcl|" + m_QueryID;
123  }
124 
125  const CSeq_id& subjectID = m_Aligns[i]->GetSeq_id(1);
126  /*
127  if(subjectID.IsLocal()) {
128  (*sequence::GetId(subjectID,*m_Scope,sequence:: eGetId_Best).GetSeqId()).GetLabel(&m_SubjectID);
129  }
130  else {
131  CLabel::GetLabel(subjectID,&m_SubjectID,CLabel::eDefault,m_Scope.GetPointer());
132  }
133  */
135  if(subjectID.IsLocal()) {
136  m_SubjectID = "lcl|" + m_SubjectID;
137  }
138  //m_SubjectID = "Subj: " + m_SubjectID;
139  break;
140  }
141 }
142 
143 
144 //extracting arguments, verifying and loading data
146 {
147 
149  x_GetLabels();
150 
151  // create a Data Source
152  CHitMatrixDSBuilder builder;
153  //builder.Init(*m_Scope, *m_Annot);
154  builder.Init(*m_Scope, m_Aligns);
155  m_DataSource = builder.CreateDataSource();
156  m_DataSource->SelectDefaultIds();
157 
158  x_InitPort();
159 
160  m_Renderer->ShowRulers(!m_Thumbnail);
161  m_Renderer->ShowGrid(!m_Thumbnail);
162  CRgbaColor cl("236 255 243");
163  m_Renderer->SetBackgroundColor(cl);
165 }
166 
167 
168 
169 
171 {
172  if(m_Aligns.size() > 0) {
173  m_Renderer->Resize(m_Width, m_Height, m_Port);
174  if(m_Thumbnail) {
175  m_Renderer->GetBottomRuler().SetDisplayOptions(CRuler::fHideLabels);
176  m_Renderer->GetLeftRuler().SetDisplayOptions(CRuler::fHideLabels);
177  //m_Renderer.SetBackGroundColor(CRgbaColor("211 223 245"));
178  //m_Renderer.SetBackGroundColor(CRgbaColor("238 238 238"));
179  }
180  else {
181  m_Renderer->GetBottomRuler().SetDisplayOptions(CRuler::fShowTextLabel);
182  m_Renderer->GetLeftRuler().SetDisplayOptions(CRuler::fShowTextLabel);
183  m_Renderer->GetBottomRuler().SetTextLabel(m_QueryID);
184  m_Renderer->GetLeftRuler().SetTextLabel(m_SubjectID);
185  }
186 
187 
188  //m_Renderer.GetBottomRuler().SetColor(CRuler::eText,CRgbaColor(24,0,0));
189  //m_Renderer.GetLeftRuler().SetColor(CRuler::eBackground,CRgbaColor(175,238,238));
190  //m_Renderer.GetBottomRuler().SetColor(CRuler::eText,CRgbaColor(0,0,0));
191  //m_Renderer.GetLeftRuler().SetColor(CRuler::eBackground,CRgbaColor("173 255 47"));
192 
193  // adjust visible space
194  m_Port.SetViewport(TVPRect(10, 10, m_Width, m_Height)); ///### this have to be eliminated
195  m_Port.ZoomAll();
196 
197  m_Renderer->Render(m_Port);
198  }
199 }
200 
202 {
203  bool success = x_RenderImage();
204  if(success) {
206  }
207  return success;
208 }
209 
210 
212 {
213  bool success = x_RenderImage();
214  if(success) {
215  if(IsFileOut()) {
217  }
218  else {//netcache
219  CNetCacheAPI nc_client("NC_HitMatrix", "blast_hitmatrix");
221  }
222 
223  }
224  return success;
225 }
226 
227 
229 {
230  bool success = false;
231  try {
232  // Clear any previously used OpenGL states
233  IRender& gl = GetGl();
234  gl.Clear();
235 
236  x_PreProcess();
237 
240 
241  x_Render();
242 
243  glFinish();
246  success = true;
247  }
248  catch (CException& e) {
249  m_ErrorMessage = "Error rendering image:" + e.GetMsg();
250  }
251  catch (exception& e) {
252  m_ErrorMessage = "Error rendering image:" + (string)e.what();
253  }
254  catch (...) {
255  m_ErrorMessage = "Error rendering image: unknown error";
256  }
257  return success;
258 }
259 
260 void CBlastHitMatrix::x_InitGraphics(const string& font_path)
261 {
262  try {
265 #ifdef GLEW_MX
266  GLEWContext glew_context;
267  GLenum err = glewContextInit(&glew_context);
268  if (GLEW_OK != err) {
269  // Problem: Error creating glew context
270  _TRACE("Error creating glew context");
271  } else {
272  CGLGlewContext::GetInstance().SetGlewContext(&glew_context);
273  err = glewInit();
274  if (GLEW_OK != err) {
275  // Problem: glewInit failed, something is seriously wrong.
276  // No extenstions will be available.
277  _TRACE("Error loading opengl extensions");
278  }
279  }
280 #else
281  GLenum err = glewInit();
282  if (GLEW_OK != err)
283  {
284  _TRACE("Error initializing glew: " << glewGetErrorString(err));
285  }
286 #endif
287  // initialize OpenGL fonts
289  // path to find font files (they can be copied from svn
290  // gbench/trunk/src/gui/res/share/gbench/fonts
291  CFtglFontManager::Instance().SetFontPath(font_path.empty() ? "./"
292  : font_path);
294 
296  GetRenderer( CGlResMgr::Instance().GetApiLevel());
297  if (mgr.IsNull()) {
298  ERR_POST(Error << "CGlRender object not available.");
300  string("no CGlRender object not available"));
301 
302  }
303  }
304  catch (CException& e) {
305  m_ErrorMessage = "Error during initialization: " + e.GetMsg();
306  }
307  catch (...) {
308  m_ErrorMessage = "Error during rendering initialization: unknown error";
309  }
310 }
311 
312 
314 
315 
316 
Declares class to display hitmatrix image view for blast 2 seq.
CBioseq_Handle –.
static TRegisterLoaderInfo RegisterInObjectManager(CObjectManager &om, CReader *reader=0, CObjectManager::EIsDefault is_default=CObjectManager::eDefault, CObjectManager::TPriority priority=CObjectManager::kPriority_NotSet)
Definition: gbloader.cpp:366
CImage & SetBuffer(void)
bool MakeCurrent(void)
Definition: gloscontext.cpp:84
const CImage & GetBuffer(void) const
CHitMatrixDSBuilder.
CIRef< IHitMatrixDataSource > CreateDataSource()
void Init(objects::CScope &scope, const objects::CSeq_align &align)
initial data set from which an alignment will be build
CHitMatrixRenderer - renders Hit Matrix Graph, Rulers, axes, grid, sequence and features graphs.
static bool WriteImage(const CImage &image, CNcbiOstream &ostr, EType type, ECompress compress=eCompress_Default)
Definition: image_io.cpp:287
static void FlipY(CImage &image)
Definition: image_util.cpp:191
const unsigned char * GetData(void) const
Definition: image.cpp:85
void SetDepth(size_t depth, bool update_image=true)
Definition: image.cpp:132
Client API for NetCache server.
class CRgbaColor provides a simple abstraction for managing colors.
Definition: rgba_color.hpp:58
@ fShowTextLabel
Definition: ruler.hpp:112
@ fHideLabels
Definition: ruler.hpp:109
CScope –.
Definition: scope.hpp:92
std::ofstream out("events_result.xml")
main entry point for tests
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
CRef< CScope > m_Scope
Current scope.
CRef< CGlOsContext > m_Context
CGlOsContext context.
void x_InitPort()
Initializes CGlPane.
int m_Height
Image height.
int m_Width
Image width.
bool IsFileOut(void)
Checks if image is to be written to the file.
bool Display(CNcbiOstream &out)
Outputs the image into CNcbiOstream.
bool WriteToFile(void)
Outputs the image into the file (m_FileOut=true) or netcache.
bool x_RenderImage(void)
Renders a pairwise alignments between the first two Seq-id in the alignment.
string m_SubjectID
Subject label id.
vector< CConstRef< CSeq_align > > m_Aligns
Vector of seqaligns.
CImageIO::EType m_Format
Image format (png,bmp etc)
CRef< CObjectManager > m_ObjMgr
Object manager.
bool m_FileOut
true if output to the file
void x_InitGraphics(const string &font_path="")
Initialize rendering environment.
unique_ptr< CHitMatrixRenderer > m_Renderer
Renderer setup Parameter.
CIRef< IHitMatrixDataSource > m_DataSource
Renderer setup Parameter.
string m_ImageKey
netcacheID
CBlastHitMatrix(const list< CRef< CSeq_align > > &seqAligns, int height=600, int width=800, CImageIO::EType format=CImageIO::ePng, const string &font_path="")
Constructor.
string m_QueryID
Query label id.
string m_File
File name.
CGlPane m_Port
Renderer setup Parameter.
string m_ErrorMessage
Error message.
void x_InitObjectManager()
Initializes Object Manager.
void x_PreProcess(void)
Performs pre-processing for image rendering.
void x_GetLabels(void)
Creates Query and subject labels info.
void x_Render(void)
Inits renderer display options and text labels.
string
Definition: cgiapp.hpp:687
#define _TRACE(message)
Definition: ncbidbg.hpp:122
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
const string & GetMsg(void) const
Get message string.
Definition: ncbiexpt.cpp:461
virtual const char * what(void) const noexcept
Standard report (includes full backlog).
Definition: ncbiexpt.cpp:342
virtual void Clear()=0
Clear/delete any graphics/OpenGL memory.
void SetModelLimitsRect(const TModelRect &R)
Definition: glpane.hpp:342
static CGlResMgr & Instance()
Definition: glresmgr.cpp:59
void SetViewport(const TVPRect &R)
Definition: glpane.cpp:96
void SetDeviceResolution(unsigned int res)
Get/set device resolution in pixels per inch (defaults to 72) You can get this value from wxWidgets v...
IRender & GetGl()
convenience function for getting current render manager
void SetFontPath(const string &p)
This path will be prepended to filename when calling GetFont It should be set to "ToStdString(CSysPat...
CGlRect< TVPUnit > TVPRect
Definition: gltypes.hpp:53
void ZoomAll(int options=fZoomXY)
Definition: glpane.cpp:289
static CFtglFontManager & Instance()
Get an instance of the manager so you can retrieve fonts.
void SetVert(T bottom, T top)
Definition: glrect.hpp:123
void Clear()
Delete all current fonts.
void SetHorz(T left, T right)
Definition: glrect.hpp:117
static void GetLabel(const CObject &obj, string *label, ELabelType type=eDefault)
Definition: label.cpp:140
@ eDefault
Definition: label.hpp:73
string PutData(const void *buf, size_t size, const CNamedParameterList *optional=NULL)
Put BLOB to server.
static CRef< CObjectManager > GetInstance(void)
Return the existing object manager or create one.
void AddDefaults(TPriority pri=kPriority_Default)
Add default data loaders from object manager.
Definition: scope.cpp:504
TSeqPos GetBioseqLength(void) const
TObjectType * GetPointer(void) THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:998
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
bool IsNull(void) const THROWS_NONE
Check if pointer is null – same effect as Empty().
Definition: ncbiobj.hpp:735
position_type GetToOpen(void) const
Definition: range.hpp:138
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
TFrom GetFrom(void) const
Get the From member data.
Definition: Range_.hpp:222
bool IsLocal(void) const
Check if variant Local is selected.
Definition: Seq_id_.hpp:775
list< CRef< CSeq_align > > TAlign
Definition: Seq_annot_.hpp:194
int i
The NCBI C++/STL use hints.
NetCache client specs.
static Format format
Definition: njn_ioutil.cpp:53
Modified on Wed Apr 17 13:09:54 2024 by modify_doxy.py rev. 669887