NCBI C++ Toolkit Cross Reference

  C++/src/gui/widgets/seq_graphic/histogram_glyph.cpp


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
/* $Id: histogram_glyph.cpp 28903 2013-09-12 18:54:31Z falkrb $ * =========================================================================== * * 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: Vlad Lebedev, Liangshou Wu * * File Description: * CHistogramGlyph -- utility class for having * feature density histograms his in graphical layouts. * */ #include <ncbi_pch.hpp> #include <gui/widgets/seq_graphic/histogram_glyph.hpp> #include <gui/widgets/seq_graphic/rendering_ctx.hpp> #include <gui/objutils/obj_fingerprint.hpp> #include <gui/objutils/utils.hpp> #include <gui/opengl/glrender.hpp> #include <gui/opengl/glutils.hpp> #include <gui/opengl/gltexturefont.hpp> #include "wx_histogram_config_dlg.hpp" #include <gui/widgets/wx/message_box.hpp> BEGIN_NCBI_SCOPE USING_SCOPE(objects); // // CHistogramGlyph::CHistogramGlyph() // CHistogramGlyph::CHistogramGlyph(const TMap& map, CSeqFeatData::ESubtype subtype, const string& title) : m_DlgHost(NULL) , m_AnnotName(CSeqUtils::GetUnnamedAnnot()) , m_Subtype(subtype) , m_ShowTitle(false) { string desc = title; if (desc.empty()) { const CFeatList& feat_list = *CSeqFeatData::GetFeatList(); desc = feat_list.GetDescription( CSeqFeatData::GetTypeFromSubtype(subtype), subtype); } m_Maps.insert(TMaps::value_type(desc, map)); m_AxisMax = ceil(GetMax()); m_AxisMin = floor(GetMin()); } CHistogramGlyph::CHistogramGlyph(const TMap& map, const string& title) : m_DlgHost(NULL) , m_AnnotName(CSeqUtils::GetUnnamedAnnot()) , m_Subtype(CSeqFeatData::eSubtype_any) , m_ShowTitle(false) { m_Maps.insert(TMaps::value_type(title, map)); m_AxisMax = ceil(GetMax()); m_AxisMin = floor(GetMin()); } CHistogramGlyph::CHistogramGlyph(const TMaps& maps, CSeqFeatData::ESubtype subtype) : m_DlgHost(NULL) , m_AnnotName(CSeqUtils::GetUnnamedAnnot()) , m_Subtype(subtype) , m_ShowTitle(false) { m_Maps = maps; m_AxisMax = ceil(GetMax()); m_AxisMin = floor(GetMin()); } CHistogramGlyph::CHistogramGlyph(const TMaps& maps, const string& name) : m_DlgHost(NULL) , m_AnnotName(name) , m_Subtype(CSeqFeatData::eSubtype_any) , m_ShowTitle(false) { m_Maps = maps; m_AxisMax = ceil(GetMax()); m_AxisMin = floor(GetMin()); } bool CHistogramGlyph::OnLeftDblClick(const TModelPoint& /*p*/) { string name; if (GetFeatSubtype() != CSeqFeatData::eSubtype_any) { const CFeatList& feats(*CSeqFeatData::GetFeatList()); name = feats.GetStoragekey(GetFeatSubtype()); } else { name = GetAnnotName(); } if ( !m_ConfigMgr->IsTempSettings(name) ) { CHistConfigDlg dlg; dlg.SetConfig(x_GetConfig()); dlg.SetConfigName(name); dlg.CreateX(NULL); if (m_DlgHost) { m_DlgHost->PreDialogShow(); } if(dlg.ShowModal() == wxID_OK) { m_ConfigMgr->AddSettings(name, dlg.GetConfig()); x_OnLayoutChanged(); } if (m_DlgHost) { m_DlgHost->PostDialogShow(); } } return true; } bool CHistogramGlyph::NeedTooltip(const TModelPoint& p, string& tt, string& t_title) const { GetTooltip(p, tt, t_title); return true; } void CHistogramGlyph::GetTooltip(const TModelPoint& /*p*/, string& tt, string& /*t_title*/) const { if (GetFeatSubtype() != CSeqFeatData::eSubtype_any) { tt = CSeqFeatData::GetFeatList()->GetDescription(GetFeatType(), GetFeatSubtype()); tt += " distribution histogram"; if (!CSeqUtils::IsUnnamed(GetTitle())) { tt += " (" + GetTitle() + ")"; } } else { // it is graph if ( !GetDesc().empty() ) { tt = GetDesc(); NStr::ReplaceInPlace(tt, "&#xA;", "\n"); NStr::ReplaceInPlace(tt, "&#xa;", "\n"); } else { tt = "Graph: " + GetTitle(); } } tt += "\n\nDouble click on the histogram to change settings"; } void CHistogramGlyph::GetHTMLActiveAreas(TAreaVector* p_areas) const { CHTMLActiveArea area; CSeqGlyph::x_InitHTMLActiveArea(area); area.m_PositiveStrand = true; CSeq_loc loc; CSeq_interval& ival = loc.SetInt(); CRef<CSGSequenceDS> seq_ds = m_Context->GetSeqDS(); CConstRef<CSeq_id> seq_id = seq_ds->GetBioseqHandle().GetSeqId(); ival.SetId().Assign(*seq_id); TMaps::const_iterator first_map = m_Maps.begin(); int subtype = 0; CObjFingerprint::EObjectType type = CObjFingerprint::eUnknown; if (GetFeatSubtype() != CSeqFeatData::eSubtype_any) { area.m_Flags = CHTMLActiveArea::fNoSelection | CHTMLActiveArea::fTooltipEmbedded | CHTMLActiveArea::fNoPin; area.m_Descr = CSeqFeatData::GetFeatList()->GetDescription(GetFeatType(), GetFeatSubtype()); area.m_Descr += " distribution histogram"; if (CSeqUtils::IsUnnamed(GetTitle())) { area.m_Descr += " (" + GetTitle() + ")"; } type = CObjFingerprint::eFeature; subtype = GetFeatSubtype(); ival.SetFrom(first_map->second.GetStart()); ival.SetTo(first_map->second.GetStop()); } else { area.m_Flags = CHTMLActiveArea::fNoSelection; type = CObjFingerprint::eGraph; ival.SetFrom(0); ival.SetTo(seq_ds->GetSequenceLength() - 1); } area.m_Signature = CObjFingerprint::GetHistogramSignature(loc, type, &m_Context->GetScope(), GetTitle(), GetAnnotName(), subtype); if (type == CObjFingerprint::eFeature) { area.m_Signature = "fake|" + area.m_Signature; } p_areas->push_back(area); } bool CHistogramGlyph::IsClickable() const { string name; if (GetFeatSubtype() != CSeqFeatData::eSubtype_any) { const CFeatList& feats(*CSeqFeatData::GetFeatList()); name = feats.GetStoragekey(GetFeatSubtype()); } else { name = GetAnnotName(); } if (m_ConfigMgr->IsTempSettings(name)) { return false; } return true; } objects::CSeqFeatData::E_Choice CHistogramGlyph::GetFeatType() const { return objects::CSeqFeatData::GetTypeFromSubtype(m_Subtype); } void CHistogramGlyph::SetAnnotName(const string& name) { m_AnnotName = name; if (NStr::StartsWith(m_AnnotName, "NA")) { // In case it is a high level coverage graph, the // annotation name has level number tagged at the // end of the base annotation name, such as // NA000008860.1@100 for NA000008860.1 // What we need here is the base annotation name int level; string base_name; if (ExtractZoomLevel(name, &base_name, &level) ) { m_AnnotName = base_name; } } } void CHistogramGlyph::x_Draw() const { CGlRender& gl = GetGl(); if (m_Maps.empty()) { return; } _ASSERT(m_Context); TModelRect rcm = GetModelRect(); TModelUnit top = GetTop(); TModelUnit bottom = top + GetHeight(); CRef<CHistParams> config = x_GetConfig(); if (config->m_DrawBg) { gl.ColorC(config->m_bgColor); m_Context->DrawQuad(rcm); } TSeqRange range = GetRange(); TModelUnit two_pix_size = m_Context->ScreenToSeq(2.0); if (range.GetLength() < two_pix_size) { // bar is less then 2 pixel. Just draw a background gl.ColorC(config->m_fgColor); m_Context->DrawQuad(range.GetFrom(), top, range.GetFrom() + two_pix_size, bottom); return; } CGlAttrGuard AttrGuard(GL_LINE_BIT); gl.Disable(GL_LINE_SMOOTH); if (config->m_NeedRuler && (config->m_Type == CHistParams::eHistogram || config->m_Type == CHistParams::eLineGraph) ) { int tick_n = 2; TModelUnit dist_y = GetHeight() / tick_n; const CRgbaColor& r_c = config->m_RulerColor; gl.Color4f(r_c.GetRed(), r_c.GetGreen(), r_c.GetBlue(), 0.4f); m_Context->DrawLine(GetLeft(), top + dist_y, GetRight(), top + dist_y); gl.Color4f(r_c.GetRed(), r_c.GetGreen(), r_c.GetBlue(), 0.4f); m_Context->DrawLine(GetLeft(), top, GetRight(), top); int ruler_n = 4; TModelUnit dist_x = GetWidth() / ruler_n; TModelUnit min_dist = m_Context->ScreenToSeq(300.0); if (dist_x < min_dist) { dist_x = min_dist; ruler_n = (int)(GetWidth() / dist_x); } TModelUnit label_off = m_Context->ScreenToSeq(1.0); //CGlBitmapFont font(CGlBitmapFont::eHelvetica8); CGlTextureFont font(CGlTextureFont::eFontFace_Helvetica, 8); TModelUnit x = GetLeft() + dist_x * 0.1; for (; x < GetRight(); x += dist_x) { gl.Color4f(r_c.GetRed(), r_c.GetGreen(), r_c.GetBlue(), 0.5f); m_Context->DrawLine(x, top, x, bottom); gl.ColorC(config->m_LabelColor); m_Context->TextOut(&font, NStr::DoubleToString(GetAxisMax()).c_str(), x + label_off, top + gl.TextHeight(&font) + 1, false); m_Context->TextOut(&font, NStr::DoubleToString(GetAxisMin()).c_str(), x + label_off, bottom - 1, false); } } float scale = GetAxisMax() - GetAxisMin(); if (scale > 0.0) { // do pre-division scale = 1.0f / scale; } bool multi_map = m_Maps.size() > 1; ITERATE (TMaps, map_iter, m_Maps) { const TMap& densityMap = map_iter->second; if (config->m_Type == CHistParams::eSmearBar || config->m_Type == CHistParams::eMergedBar) { // draw smear bar CRgbaColor color_max = config->m_SmearColorMax; CRgbaColor color_min = config->m_SmearColorMin; float l_scale = densityMap.GetMax() - densityMap.GetMin(); if (l_scale > 0.0) { // do pre-division l_scale = 1.0f / l_scale; } const CHistParams::TColorSet::const_iterator c_iter = config->m_Colors.find(map_iter->first); if (c_iter != config->m_Colors.end()) { color_max = c_iter->second; color_max.SetAlpha(0.9f); color_min = color_max; color_min.Lighten(0.5f); color_min.SetAlpha(0.9f); } TMap::runlen_iterator seg_it = densityMap.RunLenBegin(); for (; seg_it; seg_it.Advance()) { TModelUnit value = seg_it.GetValue(); if (value == 0) continue; TSeqPos f = seg_it.GetSeqPosition(); TSeqPos t = seg_it.GetSeqRunEndPosition(); float score = (value - GetAxisMin()) * l_scale; score = max(0.0f, score); score = min(1.0f, score); CRgbaColor color(CRgbaColor::Interpolate(color_max, color_min, score)); gl.ColorC(color); // draw our segment as quad m_Context->DrawQuad(f, top, t, bottom); } } else if (config->m_Type == CHistParams::eHistogram) { // draw histogram if (multi_map && config->m_Colors.count(map_iter->first) > 0) { CHistParams::TColorSet::const_iterator c_iter = config->m_Colors.find(map_iter->first); gl.ColorC(c_iter->second); } else { gl.ColorC(config->m_fgColor); } gl.PushMatrix(); gl.Translatef(0.0f, bottom, 0.0f); m_Context->DrawLine(rcm.Left(), 0.0, rcm.Right(), 0.0); gl.Scalef(1.0f, scale * GetHeight(), 1.0f); TMap::runlen_iterator seg_it = densityMap.RunLenBegin(); for (; seg_it; seg_it.Advance()) { TModelUnit value = seg_it.GetValue(); if (value == 0) continue; TSeqPos f = seg_it.GetSeqPosition(); TSeqPos t = seg_it.GetSeqRunEndPosition(); TModelUnit yy1 = value - GetAxisMin(); // draw our segment as quad m_Context->DrawQuad(f, -yy1, t, 0.0); } gl.PopMatrix(); } else { // draw line graph if (multi_map && config->m_Colors.count(map_iter->first) > 0) { CHistParams::TColorSet::const_iterator c_iter = config->m_Colors.find(map_iter->first); gl.ColorC(c_iter->second); } else { gl.ColorC(config->m_fgColor); } gl.PushMatrix(); gl.Translatef(0.0f, bottom, 0.0f); m_Context->DrawLine(rcm.Left(), 0.0, rcm.Right(), 0.0); gl.Scalef(1.0f, - scale * GetHeight(), 1.0f); gl.Begin(GL_LINE_STRIP); TMap::runlen_iterator seg_it = densityMap.RunLenBegin(); for (; seg_it; seg_it.Advance()) { TSeqPos f = seg_it.GetSeqPosition(); TSeqPos t = seg_it.GetSeqRunEndPosition();; TModelUnit y_curr = seg_it.GetValue() - GetAxisMin(); gl.Vertex2d(f - m_Context->GetOffset() + 0.5, y_curr); if (t != f) { gl.Vertex2d(t - m_Context->GetOffset() + 0.5, y_curr); } } gl.End(); gl.PopMatrix(); } } //// draw the graph title //x_Color(CSeqGraphicColorConfig::eLabel_SeqGraph); //if (x_LabelWillFit(inrc, horz, flip_strand)) { // TModelUnit widthP = horz ? fabs(inrc.Width()) / m_ScaleX : fabs(inrc.Height()) / m_ScaleY; // string out = m_Font_Helv10.Truncate(title.c_str(), widthP); // TModelUnit fs = gl.TextHeight(&m_Font_Helv10); // TModelUnit xM = horz ? inrc.CenterPoint().X() : rcm.Right() + fs * (map_idx + 1); // TModelUnit yM = horz ? rcm.Top() + fs * (map_idx + 1) : inrc.CenterPoint().Y(); // x_TextOut(&m_Font_Helv10, out.c_str(), horz, true, xM, yM); //} // Draw selection if (IsSelected()) { m_Context->DrawSelection(rcm); } gl.Enable(GL_LINE_SMOOTH); } void CHistogramGlyph::x_UpdateBoundingBox() { CRef<CHistParams> config = x_GetConfig(); m_Height = config->m_Height; TSeqRange range = GetRange(); m_Width = range.GetLength(); m_Pos.m_X = range.GetFrom(); } END_NCBI_SCOPE

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.