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

Go to the SVN repository for this file.

1 /* $Id: segment_smear_glyph.cpp 47485 2023-05-02 14:46:59Z ucko $
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  * Authors: Liangshou Wu
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <corelib/rwstream.hpp>
37 #include <gui/opengl/irender.hpp>
40 
43 
44 const size_t resizeStep = 500;
45 
47  : m_SegMap(map)
48 {}
49 
50 
51 bool CSegmentSmearGlyph::NeedTooltip(const TModelPoint& /*p*/, ITooltipFormatter& /*tt*/, string& /*t_title*/) const
52 {
53  return false;
54 }
55 
56 void CSegmentSmearGlyph::GetTooltip(const TModelPoint& /*p*/, ITooltipFormatter& /*tt*/, string& /*t_title*/) const
57 {
58 }
59 
60 
62 {
63  return true;
64 }
65 
66 
68 {
69  if ( !m_SegMap.get() ) return;
70 
71  IRender& gl = GetGl();
72 
73  TModelUnit top = GetTop();
74  TModelUnit bottom = top + (GetHeight() - 1);
75 // top += 1;
76 // bottom -= 1;
77  CSegMap::runlen_iterator seg_it = m_SegMap->RunLenBegin();
78  for (; seg_it; seg_it.Advance()) {
79  TValueType value = seg_it.GetValue();
80  _ASSERT(value < m_Colors.size());
81  TSeqPos f = seg_it.GetSeqPosition();
82  TSeqPos t = seg_it.GetSeqRunEndPosition();
84  gl.ColorC(m_Colors[value]);
85  m_Context->DrawQuad(f, top, t, bottom);
86  } else {
87  m_Context->Draw3DQuad(f, top, t, bottom, m_Colors[value], false);
88  }
89  }
90  if (m_Context->GetScale() > 1.)
91  x_DrawGaps();
92 }
93 
95 {
96 
97  IRender& gl = GetGl();
98 
99  vector<TSeqRange> gaps;
100  m_SegMap->GetGaps(m_Context->GetVisibleRange(), gaps);
101  if (gaps.empty())
102  return;
103 
104  CGlPane* pane = m_Context->GetGlPane();
105  TVPRect rc_vp = pane->GetViewport();
106 
107  gl.PushAttrib(GL_ALL_ATTRIB_BITS);
108  gl.PushMatrix();
109  gl.Scalef(m_Context->GetScale(), 1.0f, 1.0f);
110  TModelUnit seq_from = m_Context->GetVisibleFrom();
111  TModelUnit seq_to = m_Context->GetVisibleTo();
112 
113  TVPUnit screen_from = rc_vp.Left();
114  TVPUnit screen_to = rc_vp.Right();
115  double shift_x = m_Context->SeqToScreenX(seq_from);
116 
117  CRasterizer<float> rasterizer(screen_from, screen_to, seq_from, seq_to);
118  for (auto&& gap : gaps) {
119  rasterizer.AddInterval(gap.GetFrom(), gap.GetToOpen(), 1., [](float& dst, const float& src, float fraction, bool) {
120  dst = min<float>(1., dst + fraction);
121  });
122  }
123  gl.Enable(GL_BLEND);
124  gl.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
125  gl.PolygonMode(GL_FRONT_AND_BACK, GL_FILL);
126 
127  auto& raster = rasterizer.GetRaster();
128 
129  static const float kGaps_Threshold = 0.025f;
130  TModelUnit top = GetTop();
131  TModelUnit bottom = top + (GetHeight() - 1);
132 
133  for (size_t x1 = 0; x1 < raster.size();) {
134  if (raster[x1] < kGaps_Threshold) {
135  ++x1;
136  continue;
137  }
138  size_t x2 = x1;
139  float val = max<float>(0.25, raster[x1]);
140  while (++x2 < raster.size()) {
141  const float& new_val = raster[x2];
142  if (new_val < kGaps_Threshold || max<float>(0.25, new_val) != val)
143  break;
144  }
145  size_t next_pos = x2;
146  x2 -= 1;
148  if (val < 0.5)
149  gap_color.SetAlpha(val);
150  gl.ColorC(gap_color);
151 
152  double start = x1 + shift_x;
153  double stop = x2 + shift_x;
154  if (start == stop) {
155  gl.Begin(GL_LINES);
156  gl.Vertex2d(start, top);
157  gl.Vertex2d(stop, bottom);
158  gl.End();
159  } else {
160  gl.Rectd(start, top, stop, bottom);
161  }
162 
163  x1 = next_pos;
164  }
165  gl.PopMatrix();
166  gl.PopAttrib();
167 
168 }
169 
171 {
173  SetWidth(seq_ds->GetSequenceLength());
174  SetLeft(0.0);
175 }
176 
178 {
179  if (m_RangesCache.capacity() <= m_ReservedCount)
181  if (m_RangesCache.capacity() == m_RangesCache.size()) {
184  }
185  SRangeItem item;
186  item.from = range.GetFrom();
187  item.to = range.GetTo();
188  item.score = score;
189  m_RangesCache.push_back(item);
191 }
192 
194 {
195  if (m_RangesCache.capacity() <= m_ReservedCount)
196  m_RangesCache.reserve(m_ReservedCount);
197  if (m_RangesCache.capacity() == m_RangesCache.size()) {
198  m_ReservedCount += resizeStep;
199  m_RangesCache.reserve(m_ReservedCount);
200  }
201  SRangeItem item;
202  ITERATE(CRangeCollection<TSeqPos>, range_it, ranges) {
203  item.from = range_it->GetFrom();
204  item.to = range_it->GetTo();
205  item.score = score;
206  m_RangesCache.push_back(item);
207  }
208  CDensityMap<TValueType>::AddRanges(ranges, score, expand);
209 }
210 
212 {
213  size_t sz = m_RangesCache.size();
214  writer.Write(reinterpret_cast<const char*>(&sz), sizeof(sz));
215  writer.Write(reinterpret_cast<const char*>(&m_RangesCache[0]), m_RangesCache.size() * sizeof(m_RangesCache[0]));
216  writer.Flush();
217 }
218 
220 {
221  size_t sz(0);
222  reader.Read(reinterpret_cast<char*>(&sz), sizeof(sz));
223  m_RangesCache.resize(sz);
224  size_t bytes_read;
225  size_t buf_size = sz * sizeof(m_RangesCache[0]);
226  char* buf_ptr = reinterpret_cast<char*>(&m_RangesCache[0]);
227  while (buf_size > 0) {
228  ERW_Result rw_res = reader.Read(buf_ptr, buf_size, &bytes_read);
229  if (rw_res == eRW_Success) {
230  buf_ptr += bytes_read;
231  buf_size -= bytes_read;
232  } else if (rw_res == eRW_Eof) {
233  break;
234  } else {
235  NCBI_THROW(CNetServiceException, eCommunicationError, "Error while reading BLOB");
236  }
237  }
238 
239  for (size_t i = 0; i < m_RangesCache.size(); ++i) {
240  const SRangeItem &item = m_RangesCache[i];
241  CDensityMap<TValueType>::AddRange(TSeqRange(item.from,item.to), item.score, false);
242  }
243 }
244 
246 {
247  m_Bins.clear();
248  m_Max = m_DefVal;
249  m_Min = m_DefVal;
250  m_Bins.resize(x_CalcNbins(), m_DefVal);
251  m_Range = range;
252  for (size_t i = 0; i < m_RangesCache.size(); ++i) {
253  const SRangeItem &item = m_RangesCache[i];
254  CDensityMap<TValueType>::AddRange(TSeqRange(item.from, item.to), item.score, false);
255  }
256 }
257 
258 void CSegmentSmearGlyph::CSegMap::GetGaps(const TModelRange& range, vector<TSeqRange>& gaps)
259 {
260  gaps.clear();
261  for (size_t i = 0; i < m_RangesCache.size(); ++i) {
262  const SRangeItem &item = m_RangesCache[i];
263  if (item.to < range.GetFrom())
264  continue;
265  if (item.from > range.GetTo())
266  break;
267  if (item.score == CSegmentConfig::eGap) {
268  gaps.push_back(TSeqRange(item.from, item.to));
269  }
270  }
271 }
272 
Run iterator. iterate through runs of equal values in the bins.
class CGlPane
Definition: glpane.hpp:62
Net Service exception.
TModelUnit GetVisibleFrom() const
CRef< CSGSequenceDS > GetSeqDS() const
CGlPane * GetGlPane()
inline method implementations
const TModelRange & GetVisibleRange() const
TVPUnit SeqToScreenX(const TModelUnit &size) const
TModelUnit GetVisibleTo() const
void DrawQuad(const TModelRect &rc, bool border=false) const
const TModelUnit & GetScale() const
void Draw3DQuad(TModelUnit x1, TModelUnit y1, TModelUnit x2, TModelUnit y2, const CRgbaColor &color, bool border=false) const
class CRgbaColor provides a simple abstraction for managing colors.
Definition: rgba_color.hpp:58
TSeqPos GetSequenceLength() const
void AddRanges(CRangeCollection< TSeqPos > ranges, TValueType score=1, bool expand=false)
void AddRange(TSeqRange range, TValueType score=1, bool expand=false)
std::vector< SRangeItem > m_RangesCache
void Deserialize(IReader &reader)
void GetGaps(const TModelRange &range, vector< TSeqRange > &gaps)
void SetRange(const TSeqRange &range)
void Serialize(IWriter &writer) const
virtual void x_Draw() const
The default renderer for this layout object.
unique_ptr< CSegMap > m_SegMap
CSegmentSmearGlyph(CSegMap *map)
virtual bool IsClickable() const
Query if this glyph is clickable.
virtual void x_UpdateBoundingBox()
Update the bounding box assuming children's sizes are fixed if any.
virtual bool NeedTooltip(const TModelPoint &p, ITooltipFormatter &tt, string &t_title) const
Check if need to show tooltip.
TColorCode m_Colors
color code for segment map.
virtual void GetTooltip(const TModelPoint &p, ITooltipFormatter &tt, string &t_title) const
Get the tooltip if available.
CRenderingContext * m_Context
the rendering context
Definition: seq_glyph.hpp:346
virtual void SetWidth(TModelUnit w)
Definition: seq_glyph.hpp:646
virtual void SetLeft(TModelUnit l)
Definition: seq_glyph.hpp:654
virtual TModelUnit GetTop() const
Definition: seq_glyph.hpp:599
virtual TModelUnit GetHeight() const
Definition: seq_glyph.hpp:587
A very basic data-read interface.
primitive interface to arrange tabular data in the tooltips
Definition: tooltip.hpp:55
A very basic data-write interface.
Definition: map.hpp:338
unsigned int TSeqPos
Type for sequence locations and lengths.
Definition: ncbimisc.hpp:875
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#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
GLdouble TModelUnit
Definition: gltypes.hpp:48
virtual void Scalef(GLfloat x, GLfloat y, GLfloat z)=0
const TRaster & GetRaster() const
Definition: rasterizer.hpp:120
virtual void Enable(GLenum glstate)=0
virtual void Begin(GLenum mode)=0
Start rendering.
virtual void BlendFunc(GLenum sfactor, GLenum dfactor)=0
Options to be used when GL_BLEND is enabled.
IRender & GetGl()
convenience function for getting current render manager
void Vertex2d(GLdouble x, GLdouble y)
Definition: irender.hpp:185
T Right() const
Definition: glrect.hpp:83
TVPRect & GetViewport(void)
Definition: glpane.hpp:332
virtual void PopAttrib()=0
virtual void PushAttrib(GLbitfield mask)=0
virtual void PopMatrix()=0
T Left() const
Definition: glrect.hpp:81
virtual void End()=0
Finish rendering (create buffer and send to renderer)
void AddInterval(TModelUnit min, TModelUnit max, const A &attr, F f_add)
Definition: rasterizer.hpp:68
virtual void PolygonMode(GLenum face, GLenum mode)=0
Set the polygon rasterization mode.
int TVPUnit
Definition: gltypes.hpp:47
virtual void ColorC(const CRgbaColor &c)=0
Set current color (glColor{3,4}{f,d}{v,})
void Rectd(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2)
Definition: irender.hpp:193
virtual void PushMatrix()=0
void SetAlpha(float r)
Definition: rgba_color.cpp:287
virtual void AddRanges(CRangeCollection< TSeqPos > ranges, CntType score=1, bool expand=false)
virtual void AddRange(TSeqRange range, CntType score=1, bool expand=false)
CRange< TSeqPos > TSeqRange
typedefs for sequence ranges
Definition: range.hpp:419
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
ERW_Result
Result codes for I/O operations.
virtual ERW_Result Flush(void)=0
Flush pending data (if any) down to the output device.
virtual ERW_Result Write(const void *buf, size_t count, size_t *bytes_written=0)=0
Write up to "count" bytes from the buffer pointed to by the "buf" argument onto the output device.
virtual ERW_Result Read(void *buf, size_t count, size_t *bytes_read=0)=0
Read as many as "count" bytes into a buffer pointed to by the "buf" argument.
@ eRW_Eof
End of data, should be considered permanent.
@ eRW_Success
Everything is okay, I/O completed.
int i
range(_Ty, _Ty) -> range< _Ty >
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
EIPRangeType t
Definition: ncbi_localip.c:101
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
Reader-writer based streams.
USING_SCOPE(objects)
const size_t resizeStep
#define _ASSERT
Modified on Sat May 25 14:21:42 2024 by modify_doxy.py rev. 669887