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

Go to the SVN repository for this file.

1 /* $Id: seqgraphic_job.cpp 46398 2021-04-13 19:16:35Z shkeda $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Informationion
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: Vlad Lebedev, Liangshou Wu, Victor Joukov
27  *
28  * File Description:
29  *
30  */
31 
32 
33 #include <ncbi_pch.hpp>
36 #include <objmgr/graph_ci.hpp>
40 
47 
48 
51 
52 ///////////////////////////////////////////////////////////////////////////////
53 /// CSeqGraphicJob
54 ///////////////////////////////////////////////////////////////////////////////
55 
56 CSeqGraphicJob::CSeqGraphicJob(const string& desc)
57  : m_Desc(desc)
58  , m_Token(kInvalidJobToken)
59  , m_TaskTotal(0)
60  , m_TaskCompleted(0)
61 {
62  m_Error.Reset(new CAppJobError("Job failed"));
63  SetTaskName("Loading...");
64 }
65 
66 
67 
69 {
70  m_Result.Reset();
71  return x_Execute();
72 }
73 
74 
76 {
78  float progress = 0.0;
79  if (m_TaskTotal != 0) {
80  progress = (float)m_TaskCompleted/m_TaskTotal;
81  }
83  (new CAppJobProgress(progress, m_TaskName));
84 }
85 
86 
88 {
89  return m_Result;
90 }
91 
92 
94 {
96 }
97 
98 
100 {
101  return m_Desc;
102 }
103 
104 
105 void CSeqGraphicJob::SetTaskName(const string& name)
106 {
107  CFastMutexGuard lock(m_Mutex);
108  m_TaskName = name;
109 }
110 
111 
113 {
114  CFastMutexGuard lock(m_Mutex);
115  m_TaskCompleted = completed;
116 }
117 
118 
120 {
121  CFastMutexGuard lock(m_Mutex);
123 }
124 
125 
127 {
128  CFastMutexGuard lock(m_Mutex);
129  m_TaskTotal = total;
130 }
131 
132 
134 {
135  return IsCanceled();
136 }
137 
138 
139 
140 ///////////////////////////////////////////////////////////////////////////////
141 /// CSGAnnotJob
142 ///////////////////////////////////////////////////////////////////////////////
143 
144 CSGAnnotJob::CSGAnnotJob(const string& desc, CBioseq_Handle handle,
145  const SAnnotSelector& sel, const TSeqRange& range)
146  : CSeqGraphicJob(desc)
147  , m_Handle(handle)
148  , m_Sel(sel)
149  , m_Range(range)
150  , m_GraphLevel(0)
151 {}
152 
153 
156  int level, TModelUnit window,
157  const string& title, bool fixed_scale, const TAxisLimits& y_limits)
158 {
160  SetTaskName("Loading coverage graph...");
161  SAnnotSelector sel = m_Sel;
163  sel.ResetAnnotsNames();
166  sel.IncludeNamedAnnotAccession(iter->first, level);
167  sel.AddNamedAnnots(CombineWithZoomLevel(iter->first, level));
168  }
169  }
170 
171  state = x_CreateHistFromGraph(glyphs, sel, window, fixed_scale, y_limits);
172  return state;
173 }
174 
175 
177  const CMappedGraph& gr,
178  TModelUnit window,
179  bool fixed_scale)
180 {
181 
182  // If we are showing the coverage, we need to pick the right
183  // graph to show in case there are multiple graphs, such as
184  // percentil graphs generated and stored in VDB graph
185  // referred tickets include SV-1646, CXX-4124, ID-1100, SADB-80
186  // and SV-596
187 
188  // skip the grpah that says it is a percentile coverage graph,
189  // but not 100 percentile.
190 
191  if (x_PercentileButNotMax(gr))
192  return;
193 
194  string g_title = x_GetGraphName(gr);
195  string g_name = CSeqUtils::GetUnnamedAnnot();
196  if (gr.GetAnnot().IsNamed()) {
197  g_name = gr.GetAnnot().GetName();
198  }
199  auto iter = d_maps.find(g_title);
200  if (iter == d_maps.end()) {
201  string g_desc = CSeqUtils::GetAnnotComment(gr.GetAnnot());
202  iter = d_maps.insert(
203  pair<string, SGraphObject>(
204  g_title,
205  SGraphObject(
206  m_Range.GetFrom(),
207  m_Range.GetTo(),
208  (float)window,
210  g_desc, g_name))).first;
211  }
212  SGraphObject& g_obj = iter->second;
213  switch (gr.GetGraph().Which()) {
215  {{
216  const CByte_graph& graph = gr.GetGraph().GetByte();
217  x_AddGraphToMap(g_obj.m_Map, gr, graph,
218  g_obj.m_Max, g_obj.m_Min, fixed_scale);
219 
220  //x_AddGraphToMap(g_obj.m_Map, gr, graph.GetValues(),
221  // graph.GetMax(), graph.GetMin(), g_obj.m_Max,
222  // g_obj.m_Min, fixed_scale);
223  break;
224  }}
226  {{
227  const CInt_graph& graph = gr.GetGraph().GetInt();
228  x_AddGraphToMap(g_obj.m_Map, gr, graph,
229  g_obj.m_Max, g_obj.m_Min, fixed_scale);
230  break;
231  }}
233  {{
234  const CReal_graph& graph = gr.GetGraph().GetReal();
235  x_AddGraphToMap(g_obj.m_Map, gr, graph,
236  g_obj.m_Max, g_obj.m_Min, fixed_scale);
237  break;
238  }}
239  default:
240  break;
241  }
242 
243 }
244 
245 
248  const SAnnotSelector& sel,
249  TModelUnit window,
250  bool fixed_scale,
251  const TAxisLimits& y_limits)
252 {
254  m_Handle.GetRangeSeq_loc(m_Range.GetFrom(), m_Range.GetTo()) );
255  CGraph_CI graph_iter(m_Handle.GetScope(), *loc, sel);
256  // We want to merge graphs with the same graph name.
257  // Graphs will have the same name if they have the same annotation
258  // name and graph title .
259  typedef map<string, SGraphObject> TDMaps;
260  TDMaps d_maps;
261 
262  for (; graph_iter; ++graph_iter) {
263  if (IsCanceled())
264  return eCanceled;
265  x_AddGraphObject(d_maps, *graph_iter, window, fixed_scale);
266  }
267 
268  NON_CONST_ITERATE (TDMaps, iter, d_maps) {
269  SGraphObject& g_obj = iter->second;
270  CRef<CHistogramGlyph> hist = Ref(new CHistogramGlyph(g_obj.m_Map, iter->first));
271  //CRef<CSeqGlyph> fref(hist);
272  hist->SetDesc(g_obj.m_Desc);
273  hist->SetAnnotName(g_obj.m_AnnotName);
274  hist->SetAxisMaxFixed(max(y_limits.max, g_obj.m_Max));
275  hist->SetAxisMinFixed(min(y_limits.min, g_obj.m_Min));
276  hist->SetFixedScale(fixed_scale);
277  glyphs.emplace_back(hist.GetPointer());
278  }
279 
280  return eCompleted;
281 }
282 
283 /*
284 template <typename ValType>
285 void CSGAnnotJob::x_AddGraphToMap(CDensityMap<float>& the_map,
286  const CMappedGraph& gr,
287  const vector<ValType>& vec,
288  double l_max, double l_min,
289  double& g_max, double& g_min,
290  bool fixed_scale)
291 {
292  if (vec.empty()) return;
293 
294  typedef CHistogramGlyph::TDataType TDataType;
295  TSeqRange r = gr.GetLoc().GetTotalRange();
296  if (r.GetLength() == 0)
297  return;
298  TSeqRange map_r = r.IntersectionWith(m_Range);
299  TSeqPos pos = r.GetFrom();
300  double a = gr.IsSetA() ? gr.GetA() : 1.0;
301  double b = gr.IsSetB() ? gr.GetB() : 0.0;
302 
303  TSeqPos comp = gr.IsSetComp() ? gr.GetComp() : 1;
304  if (comp == 0) {
305  // Ignore this graph.
306  // The 'comp' value is set incorrectly. Do not try to reinterprete it
307  // in any other way. See GP-4721 (comment-2638485)
308  return;
309  }
310 
311  // skip the uninterested range
312  typename vector<ValType>::const_iterator iter_gr = vec.begin();
313  int offset = (map_r.GetFrom() - pos) / comp;
314  advance(iter_gr, min(offset, (int)vec.size()));
315  pos += offset * comp;
316 
317  // if the data type is char, we need to convert them into unsigned char
318  bool unsigned_char = false;
319  if (sizeof(ValType) == 1) unsigned_char = true;
320  TDataType val;
321  while (iter_gr != vec.end() && pos < map_r.GetToOpen()) {
322  TSeqRange range = TSeqRange(pos, pos + comp - 1);
323  //if (unsigned_char) {
324  //val = (TDataType)((unsigned char)(*iter_gr) * a + b);
325  //} else {
326  //val = (TDataType)((*iter_gr) * a + b);
327  //}
328  if (unsigned_char) {
329  val = (TDataType)(unsigned char)(*iter_gr);
330  } else {
331  val = (TDataType)(*iter_gr);
332  }
333  if (val < l_min)
334  val = l_min;
335  else if (val > l_max)
336  val = l_max;
337 
338  val = val * a + b;
339  the_map.AddRange(range, val);
340  pos += comp;
341  ++iter_gr;
342  }
343 
344  //if (fixed_scale) {
345  g_max = max(l_max * a + b, g_max);
346  g_min = min(l_min * a + b, g_min);
347  //}
348 }
349 */
350 
351 template <typename TGraphType>
353  const CMappedGraph& gr,
354  const TGraphType& graph,
355  double& g_max, double& g_min,
356  bool fixed_scale)
357 {
358  const auto& vec = graph.GetValues();
359  if (vec.empty()) return;
360  auto l_max = graph.GetMax();
361  auto l_min = graph.GetMin();
362 
363  typedef CHistogramGlyph::TDataType TDataType;
364  TSeqRange r = gr.GetLoc().GetTotalRange();
365  if (r.GetLength() == 0)
366  return;
367  TSeqRange map_r = r.IntersectionWith(m_Range);
368  TSeqPos pos = r.GetFrom();
369  double a = gr.IsSetA() ? gr.GetA() : 1.0;
370  double b = gr.IsSetB() ? gr.GetB() : 0.0;
371 
372  TSeqPos comp = gr.IsSetComp() ? gr.GetComp() : 1;
373  if (comp == 0) {
374  // Ignore this graph.
375  // The 'comp' value is set incorrectly. Do not try to reinterprete it
376  // in any other way. See GP-4721 (comment-2638485)
377  return;
378  }
379 
380  // skip the uninterested range
381  //typename vector<ValType>::const_iterator
382  auto iter_gr = vec.begin();
383  int offset = (map_r.GetFrom() - pos) / comp;
384  advance(iter_gr, min(offset, (int)vec.size()));
385  pos += offset * comp;
386 
387  // if the data type is char, we need to convert them into unsigned char
388  bool unsigned_char = false;
389  typedef typename TGraphType::TValues TGraphValues;
390  typedef typename TGraphValues::value_type TGraphValueType;
391  if (sizeof(TGraphValueType) == 1) unsigned_char = true;
392  TDataType val;
393  while (iter_gr != vec.end() && pos < map_r.GetToOpen()) {
394  TSeqRange range = TSeqRange(pos, pos + comp - 1);
395  //if (unsigned_char) {
396  //val = (TDataType)((unsigned char)(*iter_gr) * a + b);
397  //} else {
398  //val = (TDataType)((*iter_gr) * a + b);
399  //}
400  if (unsigned_char) {
401  val = (TDataType)(unsigned char)(*iter_gr);
402  } else {
403  val = (TDataType)(*iter_gr);
404  }
405  if (val < l_min)
406  val = l_min;
407  else if (val > l_max)
408  val = l_max;
409 
410  val = val * a + b;
411  the_map.AddRange(range, val);
412  pos += comp;
413  ++iter_gr;
414  }
415 
416  //if (fixed_scale) {
417  g_max = max(l_max * a + b, g_max);
418  g_min = min(l_min * a + b, g_min);
419  //}
420 }
421 
422 
424 {
425  string name = kEmptyStr;
426 
427  /// We should favor title over annotation name.
428  if (gr.IsSetTitle()) {
429  name = gr.GetTitle();
430  } else {
431  name = CSeqUtils::GetAnnotName(gr.GetAnnot());
432  }
433 
434  return name;
435 }
436 
437 
439 {
440  CSeq_annot_Handle annot_handle = gr.GetAnnot();
441  CConstRef<CSeq_annot> annot = annot_handle.GetCompleteSeq_annot();
442  if (annot->CanGetDesc()) {
443  ITERATE (CSeq_annot::TDesc::Tdata, iter, annot->GetDesc().Get()) {
444  if ((*iter)->IsUser() && (*iter)->GetUser().GetType().IsStr() &&
445  (*iter)->GetUser().GetType().GetStr() == "AnnotationTrack") {
446  CConstRef<CUser_field> field = (*iter)->GetUser().GetFieldRef("StatisticsType");
447  if (field && field->GetData().IsStr() &&
448  field->GetData().GetStr() == "Percentiles" &&
449  gr.IsSetComment() && gr.GetComment() != "100%") {
450  return true;
451  }
452  }
453  }
454  }
455 
456  return false;
457 }
458 
459 
User-defined methods of the data storage class.
User-defined methods of the data storage class.
User-defined methods of the data storage class.
User-defined methods of the data storage class.
User-defined methods of the data storage class.
CAppJobError Default implementation for IAppJobError - encapsulates a text error message.
CAppJobProgress Default implementation for IAppJobProgress - the class encapsulates a text message an...
CBioseq_Handle –.
CByte_graph –.
Definition: Byte_graph.hpp:66
CGraph_CI –.
Definition: graph_ci.hpp:234
void SetAnnotName(const string &name)
void SetAxisMaxFixed(TDataType max)
void SetFixedScale(bool value)
void SetDesc(const string &desc)
void SetAxisMinFixed(TDataType min)
CInt_graph –.
Definition: Int_graph.hpp:66
CMappedGraph –.
Definition: graph_ci.hpp:61
CReal_graph –.
Definition: Real_graph.hpp:66
objects::SAnnotSelector m_Sel
our annotation selector
string x_GetGraphName(const objects::CMappedGraph &gr) const
Get a name for a graph.
void x_AddGraphToMap(CDensityMap< float > &the_map, const objects::CMappedGraph &gr, const TGraphType &graph, double &g_max, double &g_min, bool fixed_scale)
bool x_PercentileButNotMax(const objects::CMappedGraph &gr) const
Check the graph type.
CSGAnnotJob(const string &desc, objects::CBioseq_Handle handle, const objects::SAnnotSelector &sel, const TSeqRange &range)
CSGAnnotJob.
TSeqRange m_Range
target range
IAppJob::EJobState x_CreateHistFromGraph(CSeqGlyph::TObjects &glyphs, const objects::SAnnotSelector &sel, TModelUnit window, bool fixed_scale, const TAxisLimits &y_limits)
objects::CBioseq_Handle m_Handle
target sequence
void x_AddGraphObject(map< string, SGraphObject > &d_maps, const objects::CMappedGraph &gr, TModelUnit window, bool fixed_scale)
IAppJob::EJobState x_LoadCoverageGraph(CSeqGlyph::TObjects &glyphs, int level, TModelUnit window, const string &title, bool fixed_scale, const TAxisLimits &y_limits)
list< CRef< CSeqGlyph > > TObjects
Definition: seq_glyph.hpp:85
CSeqGraphicJob – the base class of seqgraphic job for handling the job status such as reporting the p...
CRef< CObject > m_Result
string m_Desc
Job description or name.
CFastMutex m_Mutex
mutex for guarding state access
virtual void SetTaskTotal(int total)
virtual CConstIRef< IAppJobError > GetError()
Returns IAppJobError object describing internal error that caused the Job to fail.
int m_TaskTotal
Total amount of task for a job.
CSeqGraphicJob(const string &desc=kEmptyStr)
CSeqGraphicJob.
virtual CConstIRef< IAppJobProgress > GetProgress()
return progress object, the function shall be synchronized internally.
virtual void SetTaskName(const string &name)
int m_TaskCompleted
The amount of task finished.
virtual string GetDescr() const
Returns a human readable description of the Job (optional)
virtual void SetTaskCompleted(int completed)
set total finished task number.
virtual CRef< CObject > GetResult()
Returns the Job Result.
virtual void AddTaskCompleted(int delta)
set to add newly finished task number.
CRef< CAppJobError > m_Error
virtual EJobState Run()
Function that does all the useful work, called by the Engine.
virtual EJobState x_Execute()=0
method truly doing the job.
virtual bool StopRequested() const
CSeq_annot_Handle –.
CConstRef< CUser_field > GetFieldRef(const string &str, const string &delim=".", NStr::ECase use_case=NStr::eCase) const
Return a field reference representing the tokenized key, or a NULL reference if the key doesn't exist...
Definition: User_field.cpp:226
const_iterator end() const
Definition: map.hpp:152
iterator_bool insert(const value_type &val)
Definition: map.hpp:165
const_iterator find(const key_type &key) const
Definition: map.hpp:153
int offset
Definition: replacements.h:160
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 NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
static string GetAnnotName(const objects::CSeq_annot_Handle &annot_handle)
static const string & GetUnnamedAnnot()
Get the commonly used symbol representing a unnnamed annotation.
Definition: utils.hpp:531
static string GetAnnotComment(const objects::CSeq_annot_Handle &annot_handle)
GLdouble TModelUnit
Definition: gltypes.hpp:48
virtual bool IsCanceled() const override
virtual void AddRange(TSeqRange range, CntType score=1, bool expand=false)
EJobState
Job states (describe FSM)
Definition: app_job.hpp:86
@ eCanceled
Definition: app_job.hpp:91
@ eCompleted
Definition: app_job.hpp:89
@ eFailed
Definition: app_job.hpp:90
TRange GetTotalRange(void) const
Definition: Seq_loc.hpp:913
bool IsNamed(void) const
CConstRef< CSeq_annot > GetCompleteSeq_annot(void) const
Complete and return const reference to the current seq-annot.
const string & GetName(void) const
TSeqPos GetComp(void) const
Definition: graph_ci.hpp:156
const string & GetTitle(void) const
Definition: graph_ci.hpp:112
bool IsSetTitle(void) const
Definition: graph_ci.hpp:108
CSeq_annot_Handle GetAnnot(void) const
Get containing annot handle.
Definition: graph_ci.cpp:99
double GetB(void) const
Definition: graph_ci.hpp:174
bool IsIncludedAnyNamedAnnotAccession(void) const
check if any named annot accession is included in the search
SAnnotSelector & ResetAnnotsNames(void)
Select annotations from all Seq-annots.
bool IsSetComp(void) const
Definition: graph_ci.hpp:152
double GetA(void) const
Definition: graph_ci.hpp:165
string CombineWithZoomLevel(const string &acc, int zoom_level)
Combine accession string and zoom level into a string with separator.
SAnnotSelector & IncludeNamedAnnotAccession(const string &acc, int zoom_level=0)
bool IsSetB(void) const
Definition: graph_ci.hpp:170
SAnnotSelector & SetAnnotType(TAnnotType type)
Set annotation type (feat, align, graph)
bool IsSetA(void) const
Definition: graph_ci.hpp:161
SAnnotSelector & AddNamedAnnots(const CAnnotName &name)
Add named annot to set of annots names to look for.
const TNamedAnnotAccessions & GetNamedAnnotAccessions(void) const
const CSeq_loc & GetLoc(void) const
Definition: graph_ci.hpp:126
bool IsSetComment(void) const
Definition: graph_ci.hpp:117
const string & GetComment(void) const
Definition: graph_ci.hpp:121
const CSeq_graph::C_Graph & GetGraph(void) const
Definition: graph_ci.cpp:192
CRef< C > Ref(C *object)
Helper functions to get CRef<> and CConstRef<> objects.
Definition: ncbiobj.hpp:2015
TObjectType * GetPointer(void) THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:998
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
position_type GetToOpen(void) const
Definition: range.hpp:138
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
#define kEmptyStr
Definition: ncbistr.hpp:123
TTo GetTo(void) const
Get the To member data.
Definition: Range_.hpp:269
TFrom GetFrom(void) const
Get the From member data.
Definition: Range_.hpp:222
const TStr & GetStr(void) const
Get the variant data.
const TData & GetData(void) const
Get the Data member data.
bool IsStr(void) const
Check if variant Str is selected.
const TInt & GetInt(void) const
Get the variant data.
Definition: Seq_graph_.cpp:131
const TByte & GetByte(void) const
Get the variant data.
Definition: Seq_graph_.cpp:153
const TReal & GetReal(void) const
Get the variant data.
Definition: Seq_graph_.cpp:109
E_Choice Which(void) const
Which variant is currently selected.
Definition: Seq_graph_.hpp:716
const Tdata & Get(void) const
Get the member data.
const TDesc & GetDesc(void) const
Get the Desc member data.
Definition: Seq_annot_.hpp:852
bool CanGetDesc(void) const
Check if it is safe to call GetDesc method.
Definition: Seq_annot_.hpp:846
list< CRef< CAnnotdesc > > Tdata
range(_Ty, _Ty) -> range< _Ty >
double value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:228
unsigned int a
Definition: ncbi_localip.c:102
map< string, map< string, string > > TValues
T max(T x_, T y_)
T min(T x_, T y_)
Int4 delta(size_t dimension_, const Int4 *score_)
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
USING_SCOPE(objects)
static const int kInvalidJobToken
CHistogramGlyph::TMap m_Map
SAnnotSelector –.
Modified on Fri Apr 12 17:17:21 2024 by modify_doxy.py rev. 669887