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

Go to the SVN repository for this file.

1 /* $Id: histogram_conf.cpp 46398 2021-04-13 19:16:35Z shkeda $
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  */
29 
30 /// @file
31 ///
32 
33 #include <ncbi_pch.hpp>
36 #include <gui/objutils/utils.hpp>
39 #include <util/static_map.hpp>
40 
43 
44 static const string kHistParamsKey("GBPlugins.SeqGraphicHistogram");
45 static const string kDefHistKey("Default");
46 
47 ///////////////////////////////////////////////////////////////////////////////
48 /// CHistParams
49 ///////////////////////////////////////////////////////////////////////////////
50 
52 static const TTypeStr s_TypeStrs[] = {
53  { "histogram", CHistParams::eHistogram },
54  { "line graph", CHistParams::eLineGraph },
55  { "merged bar", CHistParams::eMergedBar },
56  { "smear bar", CHistParams::eSmearBar },
57 };
58 
61 
63 {
64  TTypeMap::const_iterator iter = sm_TypeMap.find(type);
65  if (iter != sm_TypeMap.end()) {
66  return iter->second;
67  }
68  NCBI_THROW(CException, eInvalid, "Invalid type string: " + type);
69 }
70 
72 {
74  for (iter = sm_TypeMap.begin(); iter != sm_TypeMap.end(); ++iter) {
75  if (iter->second == type) {
76  return iter->first;
77  }
78  }
79  return kEmptyStr;
80 }
81 
83 static const TScaleStr s_ScaleStrs[] = {
84  { "linear", CHistParams::eLinear },
85  { "log10", CHistParams::eLog10 },
86  { "log2", CHistParams::eLog2 },
87  { "loge", CHistParams::eLoge }
88 };
89 
91  { CHistParams::eLinear, "linear" },
92  { CHistParams::eLog10, "log 10"},
93  { CHistParams::eLoge, "ln"},
94  { CHistParams::eLog2, "log 2" }
95 };
96 
99 
101 {
102  TScaleMap::const_iterator iter = sm_ScaleMap.find(scale);
103  if (iter != sm_ScaleMap.end()) {
104  return iter->second;
105  }
106  // in case it was blank
107  else {
108  return CHistParams::eLinear;
109  }
110 }
111 
113 {
115  for (iter = sm_ScaleMap.begin(); iter != sm_ScaleMap.end(); ++iter) {
116  if (iter->second == scale) {
117  return iter->first;
118  }
119  }
120  return kEmptyStr;
121 }
123 {
124  for (auto iter = sm_ScaleNames.begin(); iter != sm_ScaleNames.end(); ++iter) {
125  if (iter->first == scale) {
126  return iter->second;
127  }
128  }
129  return kEmptyStr;
130 }
131 
132 string CHistParams::ScaleTMSToStr(const string& sScaleTMS)
133 {
134  // TMS does not currently support anything but log2 scaling, and the absence of setting is
135  // considered to be "linear" (SV-1516)
136  // other cases added (SV-2703)
137  if(sScaleTMS == "log2 scaled") {
138  return "log2";
139  } else if(sScaleTMS == "linear scaled") {
140  return "linear";
141  } else if(sScaleTMS == "loge scaled") {
142  return "loge";
143  } else if(sScaleTMS == "log10 scaled") {
144  return "log10";
145  }
146  return "linear";
147 }
148 
149 
150 
151 ///////////////////////////////////////////////////////////////////////////////
152 /// CHistParamsManager implementation
153 ///////////////////////////////////////////////////////////////////////////////
154 
155 void CHistParamsManager::LoadSettings(const string& curr_color,
156  const string& curr_size)
157 {
160 
161  CRegistryReadView view = registry.GetReadView(kHistParamsKey);
163  view.GetTopKeys(keys);
164  ITERATE(CRegistryReadView::TKeys, iter, keys) {
165  string base_key = kHistParamsKey + "." + iter->key;
166  CRegistryReadView sub_view =
168 
169  CRef<CHistParams> params(new CHistParams);
171  sub_view.GetString("Type"));
173  sub_view.GetString("Scale"));
174  params->m_DrawBg = sub_view.GetBool("DrawBg", false);
175  params->m_NeedRuler = sub_view.GetBool("NeedRuler", true);
176  params->m_ClipOutliers = sub_view.GetBool("clip", false);
177  params->m_SDeviationThreshold = sub_view.GetInt("SDeviationThreshold", 5);
178 
179  // loading color settings
181  registry, kHistParamsKey, iter->key, curr_color, kDefHistKey);
182  CSGConfigUtils::GetColor(sub_view, "FG", params->m_fgColor);
183  params->m_fgNegColor = params->m_fgColor.RotateColor(params->m_fgColor, -90); // Set default
184  CSGConfigUtils::GetColor(sub_view, "FG_neg", params->m_fgNegColor);
185  CSGConfigUtils::GetColor(sub_view, "BG", params->m_bgColor);
186  CSGConfigUtils::GetColor(sub_view, "SmearColorMin", params->m_SmearColorMin);
187  CSGConfigUtils::GetColor(sub_view, "SmearColorMax", params->m_SmearColorMax);
188  CSGConfigUtils::GetColor(sub_view, "Label", params->m_LabelColor);
189  CSGConfigUtils::GetColor(sub_view, "RulerColor", params->m_RulerColor);
190  CSGConfigUtils::GetColor(sub_view, "ocolor", params->m_OutlierColor);
191 
192  // loading color set
193  string sub_key = base_key + "." + CSGConfigUtils::ColorKey() +
194  "." + curr_color + ".ColorSet";
195  sub_view = registry.GetReadView(sub_key);
196  CRegistryReadView::TKeys color_keys;
197  sub_view.GetKeys(color_keys);
198  ITERATE(CRegistryReadView::TKeys, citer, color_keys) {
200  CSGConfigUtils::GetColor(sub_view, citer->key, color);
201  params->m_Colors[citer->key] = color;
202  }
203 
204  // loading size settings
206  registry, kHistParamsKey, iter->key, curr_size, kDefHistKey);
207  params->m_Height = sub_view.GetReal("Height", 10.0);
208 
209  m_HistSettings[iter->key] = params;
210  }
211 }
212 
213 
214 void CHistParamsManager::SaveSettings(const string& curr_color,
215  const string& curr_size) const
216 {
218 
220  CRef<CHistParams> params = iter->second;
221  if ( !params->GetDirty() ) continue;
222 
223  string base_key = kHistParamsKey + "." + iter->first;
224  CRegistryWriteView view =
226  view.Set("Type", CHistParams::TypeValueToStr(params->m_Type));
227  view.Set("Scale", CHistParams::ScaleValueToStr(params->m_Scale));
228  view.Set("NeedRuler", params->m_NeedRuler);
229  view.Set("DrawBg", params->m_DrawBg);
230  view.Set("clip", params->m_ClipOutliers);
231  view.Set("SDeviationThreshold", params->m_SDeviationThreshold);
232 
233  // save color settings
235  registry, kHistParamsKey, iter->first, curr_color, kDefHistKey);
236  CSGConfigUtils::SetColor(view, "BG", params->m_bgColor);
237  CSGConfigUtils::SetColor(view, "FG", params->m_fgColor);
238  CSGConfigUtils::SetColor(view, "FG_neg", params->m_fgNegColor);
239  CSGConfigUtils::SetColor(view, "SmearColorMin", params->m_SmearColorMin);
240  CSGConfigUtils::SetColor(view, "SmearColorMax", params->m_SmearColorMax);
241  CSGConfigUtils::SetColor(view, "Label", params->m_LabelColor);
242  CSGConfigUtils::SetColor(view, "RulerColor", params->m_RulerColor);
243  CSGConfigUtils::SetColor(view, "ocolor", params->m_OutlierColor);
244 
245  string key = base_key + "." + CSGConfigUtils::ColorKey() + "." +
246  curr_color + "." + "ColorSet";
247  view = registry.GetWriteView(key);
248  ITERATE (CHistParams::TColorSet, citer, params->m_Colors) {
249  CSGConfigUtils::SetColor(view, citer->first, citer->second);
250  }
251 
252  // save size settings
254  registry, kHistParamsKey, iter->first, curr_size, kDefHistKey);
255  view.Set("Height", params->m_Height);
256  }
257 }
258 
259 
262 {
263  return m_HistSettings.find(kDefHistKey)->second;
264 }
265 
266 
269 {
270  if (subtype != CSeqFeatData::eSubtype_any) {
271  const CFeatList& feats(*CSeqFeatData::GetFeatList());
272  vector<string> feat_hierarchy = feats.GetStoragekeys(subtype);
273 
274  // try them in reverse order so the more specific keys get tried first.
275  vector<string>::reverse_iterator riter(feat_hierarchy.end());
276  vector<string>::reverse_iterator rend(feat_hierarchy.begin());
277 
278  for ( ; riter != rend; ++riter) {
280  if ( iter != m_HistSettings.end()) {
281  return iter->second;
282  }
283  }
284  // shouldn't get here.
285  // means that there wasn't a Master feature item in the global settings.
286  _ASSERT(false);
287 
288  }
289  return CRef<CHistParams>(NULL);
290 }
291 
292 
294 CHistParamsManager::GetHistParams(const string& name) const
295 {
296  string new_name = name.empty() ? CSeqUtils::GetUnnamedAnnot() : NStr::Replace(name, ".", "_");
298  if (iter != m_HistSettings.end()) {
299  return iter->second;
300  } else {
301  iter = m_TempHistSettings.find(new_name);
302  if (iter != m_TempHistSettings.end()) {
303  return iter->second;
304  }
305  }
306 
308  return iter->second;
309 }
310 
311 
312 bool CHistParamsManager::IsTempSettings(const string& name) const
313 {
314  string new_name = name.empty() ? CSeqUtils::GetUnnamedAnnot() : NStr::Replace(name, ".", "_");
316  if (iter != m_TempHistSettings.end()) {
317  return true;
318  }
319 
320  return false;
321 }
322 
323 
324 bool CHistParamsManager::HasSettings(const string& name) const
325 {
326  string new_name = name.empty() ? CSeqUtils::GetUnnamedAnnot() : NStr::Replace(name, ".", "_");
328  if (iter != m_HistSettings.end()) {
329  return true;
330  } else {
331  iter = m_TempHistSettings.find(new_name);
332  if (iter != m_TempHistSettings.end()) {
333  return true;
334  }
335  }
336  return false;
337 }
338 
339 
341 {
342  const CFeatList& feats(*CSeqFeatData::GetFeatList());
343  vector<string> feat_hierarchy = feats.GetStoragekeys(subtype);
344  if ( !feat_hierarchy.empty() &&
345  m_HistSettings.find(feat_hierarchy.back()) != m_HistSettings.end()) {
346  return true;
347  }
348 
349  return false;
350 }
351 
352 
353 void CHistParamsManager::x_AddSettings(const string& name,
354  CRef<CHistParams> hist_params,
355  THistParams& settings)
356 {
357  // The reason we need to replace '.' with '_' is because when saving
358  // the setting to a .ini file, '.' conflicts with delimeter '.'
359  // Hence any methods access the histogram setting will also need to do
360  // the similar replacement before retrieving the settings when using a name.
361  string new_name = name.empty() ? CSeqUtils::GetUnnamedAnnot() : NStr::Replace(name, ".", "_");
362  THistParams::iterator iter = settings.find(new_name);
363  if (iter != settings.end()) {
364  *iter->second = *hist_params;
365  } else {
366  settings[new_name] = hist_params;
367  }
368 }
369 
370 CRef<CChoice> CHistParams::CreateScaleOptions(const string& option_name, CHistParams::EScale option_value)
371 {
372  auto choice = CTrackConfigUtils::CreateChoice
373  (option_name, "Linear/Log Scale",
374  CHistParams::ScaleValueToStr(option_value),
375  "Scale for graph data");
376  choice->SetValues()
380  "Shown at linear scale",
381  "Graph data is shown at linear scale"));
382  choice->SetValues()
386  "Shown at log base 10 scale",
387  "Graph data is shown at logarithmic (base 10) scale"));
388  choice->SetValues()
392  "Shown at natural logarithm (base e) scale",
393  "Graph data is shown at natural logrithm (base e) scale"));
394  choice->SetValues()
398  "Shown at log base 2 scale",
399  "Graph data is shown at logarithmic (base 2) scale"));
400  return choice;
401 }
402 
CConfigurableItems - a static list of items that can be configured.
vector< string > GetStoragekeys(int subtype) const
Get hierarchy of keys above this subtype, starting with "Master" example, eSubtype_gene,...
static CGuiRegistry & GetInstance()
access the application-wide singleton
Definition: registry.cpp:400
void LoadSettings(const string &curr_color, const string &curr_size)
CHistParamsManager implementation.
THistParams m_HistSettings
cached histogram rendering parameters.
bool HasSettings(const string &name) const
Check if there is settings for a given name.
void SaveSettings(const string &curr_color, const string &curr_size) const
bool IsTempSettings(const string &name) const
Check if there is settings for a given name.
THistParams m_TempHistSettings
temporary histogram settings.
CRef< CHistParams > GetDefHistParams() const
CRef< CHistParams > GetHistParams(TFeatSubtype subtype) const
Get histogram settings using feature subtype.
void x_AddSettings(const string &name, CRef< CHistParams > hist_params, THistParams &settings)
File Description:
static CRef< objects::CChoice > CreateScaleOptions(const string &option_name, CHistParams::EScale option_value)
CRgbaColor m_fgNegColor
CRgbaColor m_SmearColorMin
static EType TypeStrToValue(const string &type)
CRgbaColor m_LabelColor
CRgbaColor m_OutlierColor
static EScale ScaleStrToValue(const string &scale)
static const string & TypeValueToStr(CHistParams::EType type)
static const string & ScaleValueToStr(CHistParams::EScale scale)
static string ScaleTMSToStr(const string &sScaleTMS)
converts values returned in TMS "stored_scale" attribute (e.g.
CRgbaColor m_RulerColor
bool GetDirty() const
TColorSet m_Colors
EScale m_Scale
requested scale
TModelUnit m_Height
CRgbaColor m_bgColor
CRgbaColor m_fgColor
static const string & ScaleValueToName(CHistParams::EScale scale)
CRgbaColor m_SmearColorMax
class CRegistryReadView provides a nested hierarchical view at a particular key.
Definition: reg_view.hpp:58
int GetInt(const string &key, int default_val=0) const
access a named key at this level, with no recursion
Definition: reg_view.cpp:230
double GetReal(const string &key, double default_val=0) const
Definition: reg_view.cpp:235
bool GetBool(const string &key, bool default_val=false) const
Definition: reg_view.cpp:241
list< SKeyInfo > TKeys
retrieve information about all keys in the registry
Definition: reg_view.hpp:68
void GetTopKeys(TKeys &keys) const
Retrieve information about the top level keys in this view.
Definition: reg_view.cpp:278
string GetString(const string &key, const string &default_val=kEmptyStr) const
Definition: reg_view.cpp:246
void GetKeys(TKeys &keys) const
Retrieve information about all keys in this view.
Definition: reg_view.cpp:284
void Set(const string &key, int val)
access a named key at this level, with no recursion
Definition: reg_view.cpp:533
class CRgbaColor provides a simple abstraction for managing colors.
Definition: rgba_color.hpp:58
static CRegistryReadView GetSizeReadView(const CGuiRegistry &reg, const string &base_key, const string &sect, const string &key, const string &def_sect="")
static CRegistryReadView GetColorReadView(const CGuiRegistry &reg, const string &base_key, const string &sect, const string &key, const string &def_sect="")
Create a read view specifically for 'Color' section.
static CRegistryReadView GetReadView(const CGuiRegistry &reg, const string &base_key, const string &curr_key, const string &def_key1="", const string &def_key2="", const string &def_key3="")
read/readwrite view creation helper methods.
static CRegistryWriteView GetWriteView(CGuiRegistry &reg, const string &base_key, const string &curr_key, const string &def_key1="", const string &def_key2="", const string &def_key3="")
static const string & ColorKey()
static CRegistryWriteView GetColorRWView(CGuiRegistry &reg, const string &base_key, const string &sect, const string &key, const string &def_sect="")
static CRegistryWriteView GetSizeRWView(CGuiRegistry &reg, const string &base_key, const string &sect, const string &key, const string &def_sect="")
static void SetColor(CRegistryWriteView &view, const string &key, const CRgbaColor &color)
static void GetColor(const CRegistryReadView &view, const string &key, CRgbaColor &color)
static const CFeatList * GetFeatList()
class CStaticArrayMap<> provides access to a static array in much the same way as CStaticArraySet<>,...
Definition: static_map.hpp:175
TBase::const_iterator const_iterator
Definition: static_map.hpp:179
static CRef< objects::CChoice > CreateChoice(const string &name, const string &disp_name, const string &curr_val, const string &help, bool optional=false)
Definition: layout_conf.hpp:79
static CRef< objects::CChoiceItem > CreateChoiceItem(const string &name, const string &disp_name, const string &help, const string &legend_txt, bool optional=false)
Definition: layout_conf.hpp:61
const_iterator end() const
Definition: map.hpp:152
void clear()
Definition: map.hpp:169
const_iterator find(const key_type &key) const
Definition: map.hpp:153
Definition: map.hpp:338
static CMemoryRegistry registry
Definition: cn3d_tools.cpp:81
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NULL
Definition: ncbistd.hpp:225
#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
static const string & GetUnnamedAnnot()
Get the commonly used symbol representing a unnnamed annotation.
Definition: utils.hpp:531
static CRgbaColor RotateColor(const CRgbaColor &c, float degrees)
Rotate the hue of the color by degrees.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define kEmptyStr
Definition: ncbistr.hpp:123
static string & Replace(const string &src, const string &search, const string &replace, string &dst, SIZE_TYPE start_pos=0, SIZE_TYPE max_replace=0, SIZE_TYPE *num_replace=0)
Replace occurrences of a substring within a string.
Definition: ncbistr.cpp:3314
static const string kHistParamsKey("GBPlugins.SeqGraphicHistogram")
USING_SCOPE(objects)
static const TScaleStr s_ScaleStrs[]
DEFINE_STATIC_ARRAY_MAP(TTypeMap, sm_TypeMap, s_TypeStrs)
static const string kDefHistKey("Default")
static const TTypeStr s_TypeStrs[]
SStaticPair< const char *, CHistParams::EType > TTypeStr
CHistParams.
static const map< CHistParams::EScale, string > sm_ScaleNames
SStaticPair< const char *, CHistParams::EScale > TScaleStr
CStaticArrayMap< string, CHistParams::EType > TTypeMap
CStaticArrayMap< string, CHistParams::EScale > TScaleMap
n background color
const struct ncbi::grid::netcache::search::fields::KEY key
Template structure SStaticPair is simlified replacement of STL pair<> Main reason of introducing this...
Definition: static_set.hpp:60
Definition: type.c:6
#define _ASSERT
Modified on Tue Apr 23 07:38:31 2024 by modify_doxy.py rev. 669887