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

Go to the SVN repository for this file.

1 /* $Id: graphread.cpp 100332 2023-07-20 14:35:54Z vasilche $
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: Eugene Vasilchenko
27  *
28  * File Description:
29  * Access to WGS files
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
36 #include <corelib/ncbistr.hpp>
37 #include <corelib/ncbi_param.hpp>
39 #include <objects/seq/seq__.hpp>
45 #include <serial/objistrasnb.hpp>
46 #include <serial/serial.hpp>
47 #include <sra/error_codes.hpp>
48 
50 
52 
53 #define NCBI_USE_ERRCODE_X VDBGraphReader
55 
57 
58 
59 NCBI_PARAM_DECL(bool, VDBGRAPH, USE_VDB_INDEX);
60 NCBI_PARAM_DEF_EX(bool, VDBGRAPH, USE_VDB_INDEX, true,
61  eParam_NoThread, VDBGRAPH_USE_VDB_INDEX);
62 
63 
65 {
66  if ( lookup_type == eLookupDefault ) {
67  static bool use_vdb_index = NCBI_PARAM_TYPE(VDBGRAPH, USE_VDB_INDEX)::GetDefault();
68  return !use_vdb_index;
69  }
70  else {
71  return lookup_type == eLookupInMemory;
72  }
73 }
74 
75 
76 /////////////////////////////////////////////////////////////////////////////
77 // CVDBGraphDb_Impl
78 /////////////////////////////////////////////////////////////////////////////
79 
80 
82  : m_Cursor(table),
83  INIT_VDB_COLUMN(SID),
84  INIT_VDB_COLUMN(START),
86  INIT_VDB_COLUMN(GR_Q0),
87  INIT_VDB_COLUMN(GR_Q10),
88  INIT_VDB_COLUMN(GR_Q50),
89  INIT_VDB_COLUMN(GR_Q90),
90  INIT_VDB_COLUMN(GR_Q100),
91  INIT_OPTIONAL_VDB_COLUMN(GR_ZOOM_Q0),
92  INIT_OPTIONAL_VDB_COLUMN(GR_ZOOM_Q10),
93  INIT_OPTIONAL_VDB_COLUMN(GR_ZOOM_Q50),
94  INIT_OPTIONAL_VDB_COLUMN(GR_ZOOM_Q90),
95  INIT_OPTIONAL_VDB_COLUMN(GR_ZOOM_Q100),
96  INIT_VDB_COLUMN(GRAPH),
97  INIT_VDB_COLUMN(SCALE),
98  INIT_OPTIONAL_VDB_COLUMN(NUM_SWITCHES)
99 {
100 }
101 
102 
104  : m_Mgr(mgr),
105  m_Path(path)
106 {
108  // VDB graph are plain VDB table objects.
109  // However, there could be other VDBs in the same namespace (NA*)
110  // so we have to check this situation and return normal eNotFoundDb error.
111  try {
112  m_GraphTable = CVDBTable(mgr, path);
113  }
114  catch ( CSraException& exc ) {
115  bool another_vdb = false;
116  if ( exc.GetErrCode() != exc.eNotFoundTable ) {
117  // check if the accession refers some other VDB object
118  try {
119  CVDB db(mgr, path);
120  another_vdb = true;
121  }
122  catch ( CSraException& /*exc2*/ ) {
123  }
124  }
125  if ( another_vdb || exc.GetErrCode() == exc.eNotFoundTable ) {
126  // It's either some other VDB object, or not an VDB object at all
127  // report eNotFoundDb with original rc
128  NCBI_THROW2_FMT(CSraException, eNotFoundDb,
129  "Cannot open VDB graph table: "<<path,
130  exc.GetRC());
131  }
132  else {
133  // neither vdbgraph table nor another VDB
134  // report original exception
135  throw;
136  }
137  }
139 
140  TVDBRowId last_row = curs->m_Cursor.GetMaxRowId();
141  SSeqInfo info;
143  if ( !idx ) {
144  ERR_POST(Warning<<"CVDBGraphDb: sid index not found. Scanning sequentially.");
145  for ( TVDBRowId row = 1; row <= last_row; ++row ) {
146  // read range and names
147  TSeqPos start = TSeqPos(*curs->START(row));
148  TSeqPos len = *curs->LEN(row);
149  CVDBStringValue seq_id = curs->SID(row);
150  if ( *seq_id == info.m_SeqId ) {
151  // continuation of graph
152  info.m_RowLast = row;
153  info.m_SeqLength = start + len;
154  continue;
155  }
156  if ( !info.m_SeqId.empty() ) {
157  m_SeqList.push_back(info);
158  }
159  info.m_SeqId = *seq_id;
160  info.m_Seq_id_Handle = CSeq_id_Handle::GetHandle(info.m_SeqId);
161  info.m_RowFirst = row;
162  info.m_RowSize = len;
163  info.m_SeqLength = len;
164  }
165  if ( !info.m_SeqId.empty() ) {
166  info.m_RowLast = last_row;
167  m_SeqList.push_back(info);
168  }
169  }
170  else if ( LookupIsInMemory(lookup_type) ) {
171  for ( TVDBRowId row = 1; row <= last_row; ++row ) {
172  CVDBStringValue seq_id = curs->SID(row);
173  info.m_SeqId = *seq_id;
174  info.m_Seq_id_Handle = CSeq_id_Handle::GetHandle(info.m_SeqId);
175  info.m_RowSize = *curs->LEN(row);
176  info.m_SeqLength = TSeqPos(*curs->START(row)) + info.m_RowSize;
177  info.m_RowLast = info.m_RowFirst = row;
178  TVDBRowIdRange range = idx.Find(info.m_SeqId);
179  _ASSERT(row == range.first);
180  _ASSERT(range.second);
181  if ( range.second > 1 ) {
182  row += range.second-1;
183  info.m_RowLast = row;
184  info.m_SeqLength = TSeqPos(*curs->START(row))+*curs->LEN(row);
185  }
186  m_SeqList.push_back(info);
187  }
188  }
189  else {
190  m_LookupIndex = idx;
191  }
192  Put(curs);
193 
196  (TSeqInfoMapByFirstRow::value_type(it->m_RowFirst, it));
198  (TSeqInfoMapBySeq_id::value_type(it->m_Seq_id_Handle, it));
199  }
200 }
201 
202 
204 {
205 }
206 
207 
209 {
210  CRef<SGraphTableCursor> curs = m_Graph.Get();
211  if ( !curs ) {
213  curs = new SGraphTableCursor(GraphTable());
214  }
215  return curs;
216 }
217 
218 
220 {
223  bool ret = curs->m_GR_ZOOM_Q100;
224  Put(curs);
225  return ret;
226 }
227 
228 
230 {
232  auto iter = m_SeqMapByFirstRow.find(first_row);
233  if ( iter != m_SeqMapByFirstRow.end() ) {
234  return *iter->second;
235  }
236  if ( m_LookupIndex ) {
238  auto curs = Graph();
239  CVDBStringValue id = curs->SID(first_row, CVDBValue::eMissing_Allow);
240  if ( id.empty() ) {
241  Put(curs);
242  return SSeqInfo();
243  }
244  SSeqInfo info;
245  info.m_SeqId = *id;
246  info.m_Seq_id_Handle = CSeq_id_Handle::GetHandle(info.m_SeqId);
247  info.m_RowFirst = first_row;
248  TSeqPos first_len = *curs->LEN(first_row);
249  info.m_RowSize = first_len;
251  _ASSERT(first_row == range.first);
252  _ASSERT(range.second);
253  if ( range.second > 1 ) {
254  // multi page
255  TVDBRowId last_row = first_row + range.second - 1;
256  info.m_RowLast = last_row;
257  TSeqPos last_start = TSeqPos(*curs->START(last_row));
258  TSeqPos last_len = *curs->LEN(last_row);
259  info.m_SeqLength = last_start + last_len;
260  }
261  else {
262  // single page
263  info.m_RowLast = first_row;
264  TSeqPos first_start = TSeqPos(*curs->START(first_row));
265  info.m_SeqLength = first_start + first_len;
266  }
267  Put(curs);
268  return info;
269  }
270  return SSeqInfo();
271 }
272 
273 
275 {
277  auto iter = m_SeqMapBySeq_id.find(idh);
278  if ( iter != m_SeqMapBySeq_id.end() ) {
279  return *iter->second;
280  }
281  if ( m_LookupIndex ) {
283  auto seq_id = idh.GetSeqId();
284  const CTextseq_id* text_id = seq_id->GetTextseq_Id();
285  if ( !text_id ||
286  text_id->IsSetName() || text_id->IsSetRelease() ||
287  !text_id->IsSetAccession() || !text_id->IsSetVersion() ) {
288  return SSeqInfo();
289  }
290  string id = text_id->GetAccession()+'.'+NStr::NumericToString(text_id->GetVersion());
291  NStr::ToUpper(id);
292  auto curs = Graph();
293  auto range = m_LookupIndex.Find(id);
294  if ( !range.second ) {
295  Put(curs);
296  return SSeqInfo();
297  }
298  auto first_row = range.first;
299  SSeqInfo info;
300  info.m_SeqId = id;
301  info.m_Seq_id_Handle = idh;
302  info.m_RowFirst = first_row;
303  TSeqPos first_len = *curs->LEN(first_row);
304  info.m_RowSize = first_len;
305  if ( range.second > 1 ) {
306  // multi page
307  TVDBRowId last_row = first_row + range.second - 1;
308  info.m_RowLast = last_row;
309  TSeqPos last_start = TSeqPos(*curs->START(last_row));
310  TSeqPos last_len = *curs->LEN(last_row);
311  info.m_SeqLength = last_start + last_len;
312  }
313  else {
314  // single page
315  info.m_RowLast = first_row;
316  TSeqPos first_start = TSeqPos(*curs->START(first_row));
317  info.m_SeqLength = first_start + first_len;
318  }
319  Put(curs);
320  return info;
321  }
322  return SSeqInfo();
323 }
324 
325 
326 /////////////////////////////////////////////////////////////////////////////
327 // CVDBGraphSeqIterator
328 /////////////////////////////////////////////////////////////////////////////
329 
331  : m_Db(db)
332 {
334 }
335 
336 
338  const CSeq_id_Handle& seq_id)
339  : m_Db(db)
340 {
341  m_Info = db.GetNCObject().GetSeqInfo(seq_id);
342 }
343 
344 
346 {
347  m_Info = m_Db->GetSeqInfoAtRow(GetInfo().m_RowLast+1);
348  return *this;
349 }
350 
351 
353 {
354  if ( !*this ) {
355  NCBI_THROW(CSraException, eInvalidState,
356  "CVDBGraphSeqIterator is invalid");
357  }
358  return m_Info;
359 }
360 
361 
362 template<class DstVector, class SrcVector>
363 static void sx_Assign(DstVector& dst, const SrcVector& src)
364 {
365  dst.clear();
366  dst.reserve(src.size());
367  ITERATE ( typename SrcVector, it, src ) {
368  dst.push_back(*it);
369  }
370 }
371 
372 
374 CVDBGraphSeqIterator::x_MakeGraph(const string& annot_name,
375  CSeq_loc& loc,
376  const SSeqInfo& info,
377  const COpenRange<TSeqPos>& range,
378  TSeqPos step,
379  SGraphTableCursor& cursor,
381  int level) const
382 {
383  if ( !column ) {
384  return null;
385  }
386 
387  CRef<CSeq_graph> graph(new CSeq_graph);
388  if ( !annot_name.empty() ) {
389  graph->SetTitle(annot_name);
390  }
391  graph->SetLoc(loc);
392  if ( level >= 0 ) {
393  graph->SetComment(NStr::IntToString(level)+"%");
394  }
395 
396  typedef SGraphTableCursor::TGraphQ TValue;
397  const TValue kMinIntValue = kMin_I4;
398  const TValue kMaxIntValue = kMax_I4;
399  const TValue kMinByteValue = 0;
400  const TValue kMaxByteValue = kMax_UI1;
401 
402  TValue max_v = numeric_limits<TValue>::min();
403  TValue min_v = numeric_limits<TValue>::max();
404  CInt_graph* int_graph = &graph->SetGraph().SetInt();
405  CReal_graph* real_graph = 0;
406  CInt_graph::TValues* int_vv = &int_graph->SetValues();
407  CReal_graph::TValues* real_vv = 0;
408  TSeqPos row_size = info.m_RowSize;
409  TSeqPos pos = range.GetFrom();
410  for ( TVDBRowId row = info.m_RowFirst + pos/row_size;
411  pos < range.GetToOpen();
412  ++row ) {
413  CVDBValueFor<TValue> values(cursor.m_Cursor, row, column);
414  for ( size_t index = pos%row_size/step;
415  index < values.size() && pos < range.GetToOpen();
416  ++index, pos += step ) {
417  TValue v = values[index];
418  bool switch_to_real = false;
419  if ( v < min_v ) {
420  min_v = v;
421  switch_to_real = v < kMinIntValue;
422  }
423  if ( v > max_v ) {
424  max_v = v;
425  switch_to_real = v > kMaxIntValue;
426  }
427  if ( switch_to_real && int_vv ) {
428  // switch to real graph
429  CRef<CInt_graph> save_int_graph(int_graph);
430  real_graph = &graph->SetGraph().SetReal();
431  int_graph = 0;
432  real_vv = &real_graph->SetValues();
433  sx_Assign(*real_vv, *int_vv);
434  int_vv = 0;
435  }
436  if ( int_vv ) {
437  int_vv->push_back(int(v));
438  }
439  else {
440  real_vv->push_back(double(v));
441  }
442  }
443  if ( pos < range.GetToOpen() &&
444  pos < (row-info.m_RowFirst+1)*row_size ) {
445  NCBI_THROW(CSraException, eNotFoundValue,
446  "CVDBGraphSeqIterator: graph data array is too short");
447  }
448  }
449  size_t numval = 0;
450  if ( min_v >= kMinByteValue && max_v <= kMaxByteValue ) {
451  // use smaller byte representation
452  numval = int_vv->size();
453  CRef<CByte_graph> byte_graph(new CByte_graph);
454  byte_graph->SetAxis(0);
455  byte_graph->SetMin(int(min_v));
456  byte_graph->SetMax(int(max_v));
457  sx_Assign(byte_graph->SetValues(), *int_vv);
458  graph->SetGraph().SetByte(*byte_graph);
459  int_graph = 0;
460  int_vv = 0;
461  }
462  else if ( real_graph ) {
463  // need bigger double representation
464  numval = real_vv->size();
465  real_graph->SetAxis(0);
466  real_graph->SetMin(double(min_v));
467  real_graph->SetMax(double(max_v));
468  }
469  else {
470  // int graph
471  numval = int_vv->size();
472  int_graph->SetAxis(0);
473  int_graph->SetMin(int(min_v));
474  int_graph->SetMax(int(max_v));
475  }
476  if ( step > 1 ) {
477  graph->SetComp(step);
478  }
479  uint32_t scale = cursor.SCALE(info.m_RowFirst);
480  if ( scale != 1 ) {
481  graph->SetA(1./scale);
482  }
483  if ( numval > size_t(kMax_Int) ) {
484  NCBI_THROW(CSraException, eDataError,
485  "CVDBGraphSeqIterator::x_MakeGraph: graph too big");
486  }
487  graph->SetNumval(int(numval));
488  return graph;
489 }
490 
491 
493 CVDBGraphSeqIterator::x_MakeTable(const string& annot_name,
494  CSeq_loc& loc,
495  const SSeqInfo& info,
496  const COpenRange<TSeqPos>& range,
497  SGraphTableCursor& cursor) const
498 {
499  TSeqPos size = range.GetLength();
500  TSeqPos row_size = info.m_RowSize;
501  typedef SGraphTableCursor::TGraphQ TValue;
502  const TValue kMinIntValue = kMin_I4;
503  const TValue kMaxIntValue = kMax_I4;
504 
505  TValue min_v = 0, max_v = 0;
506  vector<TValue> vv;
507  vv.reserve(size);
508  TSeqPos pos = range.GetFrom();
509  TVDBRowId row = pos/row_size;
510  for ( ; pos < range.GetToOpen(); ++row, pos += row_size ) {
511  CVDBValueFor<TValue> vv_arr(cursor.GRAPH(info.m_RowFirst+row));
512  TSeqPos off = TSeqPos(pos - row*row_size);
513  TSeqPos cnt = min(row_size-off, range.GetToOpen()-pos);
514  for ( TSeqPos i = 0; i < cnt; ++i ) {
515  TValue v = vv_arr[off+i];
516  vv.push_back(v);
517  if ( v > max_v ) {
518  max_v = v;
519  }
520  if ( v < min_v ) {
521  min_v = v;
522  }
523  }
524  }
525 
527  table->SetFeat_type(0);
528 
529  { // Seq-table location
531  table->SetColumns().push_back(col_id);
532  col_id->SetHeader().SetField_name("Seq-table location");
533  col_id->SetDefault().SetLoc(loc);
534  }
535  { // Seq-id
537  table->SetColumns().push_back(col_id);
539  col_id->SetDefault().SetId(loc.SetInt().SetId());
540  }
541 
542  // position
544  table->SetColumns().push_back(col_pos);
546  CSeqTable_multi_data::TInt& arr_pos = col_pos->SetData().SetInt();
547 
548  // span
550  table->SetColumns().push_back(col_span);
551  col_span->SetHeader().SetField_name("span");
552  CSeqTable_multi_data::TInt& arr_span = col_span->SetData().SetInt();
553 
555  table->SetColumns().push_back(col_val);
556  col_val->SetHeader().SetField_name("values");
557 
558  TSeqPos cur_i = 0;
559  TValue cur_v = vv[0];
560  if ( min_v < kMinIntValue || max_v > kMaxIntValue ) {
561  CSeqTable_multi_data::TReal& arr_vv = col_val->SetData().SetReal();
562  for ( TSeqPos i = 0; i < size; ++i ) {
563  TValue v = vv[i];
564  if ( v != cur_v ) {
565  arr_pos.push_back(cur_i+range.GetFrom());
566  arr_span.push_back(i-cur_i);
567  arr_vv.push_back(double(cur_v));
568  cur_i = i;
569  cur_v = v;
570  }
571  }
572  arr_pos.push_back(cur_i+range.GetFrom());
573  arr_span.push_back(size-cur_i);
574  arr_vv.push_back(double(cur_v));
575  }
576  else {
577  CSeqTable_multi_data::TInt& arr_vv = col_val->SetData().SetInt();
578  for ( TSeqPos i = 0; i < size; ++i ) {
579  TValue v = vv[i];
580  if ( v != cur_v ) {
581  arr_pos.push_back(cur_i+range.GetFrom());
582  arr_span.push_back(i-cur_i);
583  arr_vv.push_back(int(cur_v));
584  cur_i = i;
585  cur_v = v;
586  }
587  }
588  arr_pos.push_back(cur_i+range.GetFrom());
589  arr_span.push_back(size-cur_i);
590  arr_vv.push_back(int(cur_v));
591  }
592 
593  uint32_t scale = cursor.SCALE(info.m_RowFirst);
594  if ( scale != 1 ) {
596  table->SetColumns().push_back(col_step);
597  col_step->SetHeader().SetField_name("value_step");
598  col_step->SetDefault().SetReal(1./scale);
599  }
600  if ( arr_pos.size() > size_t(kMax_Int) ) {
601  NCBI_THROW(CSraException, eDataError,
602  "CVDBGraphSeqIterator::x_MakeTable: graph too big");
603  }
604  table->SetNum_rows(int(arr_pos.size()));
605  return table;
606 }
607 
608 
610  SGraphTableCursor& cursor) const
611 {
612  if ( !cursor.m_NUM_SWITCHES ) {
613  // no info about value changes
614  return false;
615  }
616  typedef SGraphTableCursor::TGraphV TValue;
617  const TValue kMinIntValue = kMin_I4;
618  const TValue kMaxIntValue = kMax_I4;
619  const TValue kMinByteValue = 0;
620  const TValue kMaxByteValue = kMax_UI1;
621  TValue min_v = 0, max_v = 0;
622  size_t values = 0;
623  uint64_t switches = 0;
624  TSeqPos pos = range.GetFrom();
625  const SSeqInfo& info = GetInfo();
626  TSeqPos row_size = info.m_RowSize;
627  TVDBRowId row = pos/row_size;
628  for ( ; pos < range.GetToOpen(); ++row, pos += row_size ) {
629  values += row_size;
630  switches += *cursor.NUM_SWITCHES(info.m_RowFirst+row);
631  TValue v = cursor.GR_Q100(info.m_RowFirst+row);
632  if ( v > max_v ) {
633  max_v = v;
634  }
635  if ( v < min_v ) {
636  min_v = v;
637  }
638  }
639  size_t table_value_size =
640  min_v < kMinIntValue || max_v > kMaxIntValue? sizeof(double): sizeof(int);
641  size_t graph_value_size =
642  min_v < kMinByteValue || max_v > kMaxByteValue? table_value_size: 1;
643  uint64_t table_size =
644  (table_value_size+2*sizeof(int))*switches; //+pos+span
645  size_t graph_size =
646  graph_value_size*values;
647  return table_size < graph_size;
648 }
649 
650 
652 {
653  const SSeqInfo& info = GetInfo();
654  if ( range.GetToOpen() > info.m_SeqLength ) {
655  range.SetToOpen(info.m_SeqLength);
656  }
657  if ( range.Empty() ) {
658  return false;
659  }
661  CRef<SGraphTableCursor> curs(GetDb().Graph());
662  bool seq_table_is_smaller = x_SeqTableIsSmaller(range, *curs);
663  GetDb().Put(curs);
664  return seq_table_is_smaller;
665 }
666 
667 
670  const string& annot_name,
671  TContentFlags content) const
672 {
673  const SSeqInfo& info = GetInfo();
674  if ( range0.GetToOpen() > info.m_SeqLength ) {
675  range0.SetToOpen(info.m_SeqLength);
676  }
677  if ( range0.Empty() ) {
678  return null;
679  }
680 
681  CRef<CSeq_annot> annot(new CSeq_annot);
682 
683  if ( !annot_name.empty() ) {
684  CRef<CAnnotdesc> desc(new CAnnotdesc);
685  desc->SetName(annot_name);
686  annot->SetDesc().Set().push_back(desc);
687  }
688 
690  CRef<SGraphTableCursor> curs(GetDb().Graph());
691 
692  if ( content & fGraphQAll ) {
693  if ( content & (fGraphZoomQAll|fGraphMain) ) {
694  NCBI_THROW(CSraException, eInvalidArg, "CVDBGraphSeqIterator: "
695  "several zoom tracks are requested");
696  }
697 
698  TSeqPos step = info.m_RowSize;
699 
700  COpenRange<TSeqPos> range = range0;
701  if ( TSeqPos adjust = range.GetFrom() % step ) {
702  range.SetFrom(range.GetFrom() - adjust);
703  }
704  if ( TSeqPos adjust = range.GetToOpen() % step ) {
705  range.SetToOpen(min(info.m_SeqLength,
706  range.GetToOpen() + (step - adjust)));
707  }
708 
709  CRef<CSeq_loc> loc(new CSeq_loc);
710  loc->SetInt().SetId(*SerialClone(*info.m_Seq_id_Handle.GetSeqId()));
711  loc->SetInt().SetFrom(range.GetFrom());
712  loc->SetInt().SetTo(range.GetTo());
713 
714  // describe statistics
715  {
716  CRef<CAnnotdesc> desc(new CAnnotdesc);
717  CUser_object& obj = desc->SetUser();
718  obj.SetType().SetStr("AnnotationTrack");
719  obj.AddField("ZoomLevel", int(step));
720  obj.AddField("StatisticsType", "Percentiles");
721  annot->SetDesc().Set().push_back(desc);
722  }
723 
724  for ( TContentFlags f = fGraphQ0; f & fGraphQAll; f <<= 1 ) {
725  if ( !(content & f) ) {
726  continue;
727  }
728  CRef<CSeq_graph> graph;
729  switch ( f ) {
730  case fGraphQ0 :
731  graph = x_MakeGraph(annot_name, *loc, info, range,
732  step, *curs, curs->m_GR_Q0 , 0);
733  break;
734  case fGraphQ10 :
735  graph = x_MakeGraph(annot_name, *loc, info, range,
736  step, *curs, curs->m_GR_Q10 , 10);
737  break;
738  case fGraphQ50 :
739  graph = x_MakeGraph(annot_name, *loc, info, range,
740  step, *curs, curs->m_GR_Q50 , 50);
741  break;
742  case fGraphQ90 :
743  graph = x_MakeGraph(annot_name, *loc, info, range,
744  step, *curs, curs->m_GR_Q90 , 90);
745  break;
746  case fGraphQ100:
747  graph = x_MakeGraph(annot_name, *loc, info, range,
748  step, *curs, curs->m_GR_Q100, 100);
749  break;
750  default:
751  break;
752  }
753  if ( graph ) {
754  annot->SetData().SetGraph().push_back(graph);
755  }
756  }
757  }
758 
759  if ( content & fGraphZoomQAll ) {
760  if ( content & (fGraphQAll|fGraphMain) ) {
761  NCBI_THROW(CSraException, eInvalidArg, "CVDBGraphSeqIterator: "
762  "several zoom tracks are requested");
763  }
764 
765  TSeqPos step = 100;
766 
767  COpenRange<TSeqPos> range = range0;
768  if ( TSeqPos adjust = range.GetFrom() % step ) {
769  range.SetFrom(range.GetFrom() - adjust);
770  }
771  if ( TSeqPos adjust = range.GetToOpen() % step ) {
772  range.SetToOpen(min(info.m_SeqLength,
773  range.GetToOpen() + (step - adjust)));
774  }
775 
776  CRef<CSeq_loc> loc(new CSeq_loc);
777  loc->SetInt().SetId(*SerialClone(*info.m_Seq_id_Handle.GetSeqId()));
778  loc->SetInt().SetFrom(range.GetFrom());
779  loc->SetInt().SetTo(range.GetTo());
780 
781  // describe statistics
782  {
783  CRef<CAnnotdesc> desc(new CAnnotdesc);
784  CUser_object& obj = desc->SetUser();
785  obj.SetType().SetStr("AnnotationTrack");
786  obj.AddField("ZoomLevel", int(step));
787  obj.AddField("StatisticsType", "Percentiles");
788  annot->SetDesc().Set().push_back(desc);
789  }
790 
791  for ( TContentFlags f = fGraphZoomQ0; f & fGraphZoomQAll; f <<= 1 ) {
792  if ( !(content & f) ) {
793  continue;
794  }
795  CRef<CSeq_graph> graph;
796  switch ( f ) {
797  case fGraphZoomQ0 :
798  graph = x_MakeGraph(annot_name, *loc, info, range,
799  step, *curs, curs->m_GR_ZOOM_Q0 , 0);
800  break;
801  case fGraphZoomQ10 :
802  graph = x_MakeGraph(annot_name, *loc, info, range,
803  step, *curs, curs->m_GR_ZOOM_Q10 , 10);
804  break;
805  case fGraphZoomQ50 :
806  graph = x_MakeGraph(annot_name, *loc, info, range,
807  step, *curs, curs->m_GR_ZOOM_Q50 , 50);
808  break;
809  case fGraphZoomQ90 :
810  graph = x_MakeGraph(annot_name, *loc, info, range,
811  step, *curs, curs->m_GR_ZOOM_Q90 , 90);
812  break;
813  case fGraphZoomQ100:
814  graph = x_MakeGraph(annot_name, *loc, info, range,
815  step, *curs, curs->m_GR_ZOOM_Q100, 100);
816  break;
817  default:
818  break;
819  }
820  if ( graph ) {
821  annot->SetData().SetGraph().push_back(graph);
822  }
823  }
824  }
825 
826  if ( content & fGraphMain ) {
827  COpenRange<TSeqPos> range = range0;
828 
829  CRef<CSeq_loc> loc(new CSeq_loc);
830  loc->SetInt().SetId(*SerialClone(*info.m_Seq_id_Handle.GetSeqId()));
831  loc->SetInt().SetFrom(range.GetFrom());
832  loc->SetInt().SetTo(range.GetTo());
833 
834  bool as_table = false;
835  if ( content & fGraphMainAsTable ) {
836  if ( content & fGraphMainAsGraph ) {
837  // select best
838  as_table = x_SeqTableIsSmaller(range, *curs);
839  }
840  else {
841  as_table = true;
842  }
843  }
844  if ( as_table ) {
845  {
846  CRef<CAnnotdesc> desc(new CAnnotdesc);
847  CUser_object& obj = desc->SetUser();
848  obj.SetType().SetStr("Track Data");
849  obj.AddField("track type", "graph");
850  annot->SetDesc().Set().push_back(desc);
851  }
852 
853  CRef<CSeq_table> table = x_MakeTable(annot_name, *loc, info, range,
854  *curs);
855  annot->SetData().SetSeq_table(*table);
856  }
857  else {
858  CRef<CSeq_graph> graph = x_MakeGraph(annot_name, *loc, info, range,
859  1, *curs, curs->m_GRAPH, -1);
860  annot->SetData().SetGraph().push_back(graph);
861  }
862  }
863 
864  GetDb().Put(curs);
865 
866  if ( !annot->IsSetData() ) {
867  return null;
868  }
869 
870  return annot;
871 }
872 
873 
CAnnotdesc –.
Definition: Annotdesc.hpp:66
CByte_graph –.
Definition: Byte_graph.hpp:66
CInt_graph –.
Definition: Int_graph.hpp:66
CReal_graph –.
Definition: Real_graph.hpp:66
CRef –.
Definition: ncbiobj.hpp:618
@ eNotFoundTable
DB table not found.
Definition: exception.hpp:93
virtual TErrCode GetErrCode(void) const
Definition: sraread.cpp:164
rc_t GetRC(void) const
Definition: exception.hpp:144
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
CUser_object & AddField(const string &label, const string &value, EParseField parse=eParse_String)
add a data field to the user object that holds a given value
CVDBTableIndex m_LookupIndex
Definition: graphread.hpp:161
CRef< SGraphTableCursor > Graph(void)
Definition: graphread.cpp:208
bool HasMidZoomGraphs(void)
Definition: graphread.cpp:219
TSeqInfoList m_SeqList
Definition: graphread.hpp:166
CVDBObjectCache< SGraphTableCursor > m_Graph
Definition: graphread.hpp:163
TSeqInfoMapBySeq_id m_SeqMapBySeq_id
Definition: graphread.hpp:167
CVDBGraphDb_Impl(CVDBMgr &mgr, CTempString path, ELookupType lookup_type=eLookupDefault)
Definition: graphread.cpp:103
SSeqInfo GetSeqInfoAtRow(TVDBRowId row)
Definition: graphread.cpp:229
void Put(CRef< SGraphTableCursor > &curs)
Definition: graphread.hpp:149
bool LookupIsInMemory() const
Definition: graphread.hpp:75
CVDBTable m_GraphTable
Definition: graphread.hpp:160
SSeqInfo GetSeqInfo(const CSeq_id_Handle &seq_id)
Definition: graphread.cpp:274
TSeqInfoMapByFirstRow m_SeqMapByFirstRow
Definition: graphread.hpp:168
CMutex m_SeqInfoMutex
Definition: graphread.hpp:165
const CVDBTable & GraphTable(void)
Definition: graphread.hpp:143
virtual ~CVDBGraphDb_Impl(void)
Definition: graphread.cpp:203
list< SSeqInfo > TSeqInfoList
Definition: graphread.hpp:131
CVDBGraphDb_Impl::SSeqInfo m_Info
Definition: graphread.hpp:297
bool x_SeqTableIsSmaller(COpenRange< TSeqPos > range, SGraphTableCursor &cursor) const
Definition: graphread.cpp:609
CVDBGraphDb_Impl & GetDb(void) const
Definition: graphread.hpp:274
bool SeqTableIsSmaller(COpenRange< TSeqPos > range) const
Definition: graphread.cpp:651
CRef< CSeq_graph > x_MakeGraph(const string &annot_name, CSeq_loc &loc, const SSeqInfo &info, const COpenRange< TSeqPos > &range, TSeqPos step, SGraphTableCursor &cursor, CVDBColumn &column, int level) const
Definition: graphread.cpp:374
CVDBGraphSeqIterator & operator++(void)
Definition: graphread.cpp:345
CRef< CSeq_table > x_MakeTable(const string &annot_name, CSeq_loc &loc, const SSeqInfo &info, const COpenRange< TSeqPos > &range, SGraphTableCursor &cursor) const
Definition: graphread.cpp:493
CRef< CSeq_annot > GetAnnot(COpenRange< TSeqPos > range, const string &annot_name=kEmptyStr, TContentFlags content=fDefaultContent) const
Definition: graphread.cpp:669
const SSeqInfo & GetInfo(void) const
Definition: graphread.cpp:352
TVDBRowIdRange Find(const string &value) const
Definition: vdbread.cpp:1276
size_t size(void) const
Definition: vdbread.hpp:694
@ eMissing_Allow
Definition: vdbread.hpp:620
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
The NCBI C++ standard methods for dealing with std::string.
#define LEN(ptr)
static const char * column
Definition: stats.c:23
Uint8 uint64_t
Uint4 uint32_t
NCBI_DEFINE_ERR_SUBCODE_X(1)
NCBI_PARAM_DECL(bool, VDBGRAPH, USE_VDB_INDEX)
BEGIN_NAMESPACE(objects)
static void sx_Assign(DstVector &dst, const SrcVector &src)
Definition: graphread.cpp:363
NCBI_PARAM_DEF_EX(bool, VDBGRAPH, USE_VDB_INDEX, true, eParam_NoThread, VDBGRAPH_USE_VDB_INDEX)
END_NCBI_NAMESPACE
Definition: graphread.cpp:875
END_NAMESPACE(objects)
BEGIN_NCBI_NAMESPACE
Definition: graphread.cpp:51
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
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
#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
void Warning(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1191
C * SerialClone(const C &src)
Create on heap a clone of the source object.
Definition: serialbase.hpp:512
CConstRef< CSeq_id > GetSeqId(void) const
static CSeq_id_Handle GetHandle(const CSeq_id &id)
Normal way of getting a handle, works for any seq-id.
void SetInt(TInt &v)
Definition: Seq_loc.hpp:983
TObjectType & GetNCObject(void) const
Get object.
Definition: ncbiobj.hpp:1187
#define NCBI_PARAM_TYPE(section, name)
Generate typename for a parameter from its {section, name} attributes.
Definition: ncbi_param.hpp:149
@ eParam_NoThread
Do not use per-thread values.
Definition: ncbi_param.hpp:418
#define kMin_I4
Definition: ncbi_limits.h:217
#define kMax_Int
Definition: ncbi_limits.h:184
#define kMax_UI1
Definition: ncbi_limits.h:213
#define kMax_I4
Definition: ncbi_limits.h:218
TThisType & SetToOpen(position_type toOpen)
Definition: range.hpp:175
position_type GetToOpen(void) const
Definition: range.hpp:138
bool Empty(void) const
Definition: range.hpp:148
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5084
static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
Definition: ncbistr.hpp:673
static string & ToUpper(string &str)
Convert string to upper case – string& version.
Definition: ncbistr.cpp:424
void SetType(TType &value)
Assign a value to Type data member.
void SetHeader(THeader &value)
Assign a value to Header data member.
void SetData(TData &value)
Assign a value to Data data member.
void SetDefault(TDefault &value)
Assign a value to Default data member.
bool IsSetAccession(void) const
Check if a value has been assigned to Accession data member.
TVersion GetVersion(void) const
Get the Version member data.
bool IsSetRelease(void) const
Check if a value has been assigned to Release data member.
bool IsSetVersion(void) const
Check if a value has been assigned to Version data member.
bool IsSetName(void) const
Check if a value has been assigned to Name data member.
const TAccession & GetAccession(void) const
Get the Accession member data.
void SetA(TA value)
Assign a value to A data member.
void SetAxis(TAxis value)
Assign a value to Axis data member.
void SetTitle(const TTitle &value)
Assign a value to Title data member.
Definition: Seq_graph_.hpp:784
TValues & SetValues(void)
Assign a value to Values data member.
void SetAxis(TAxis value)
Assign a value to Axis data member.
Definition: Int_graph_.hpp:394
void SetNumval(TNumval value)
Assign a value to Numval data member.
void SetComp(TComp value)
Assign a value to Comp data member.
void SetGraph(TGraph &value)
Assign a value to Graph data member.
Definition: Seq_graph_.cpp:250
vector< double > TValues
Definition: Real_graph_.hpp:88
void SetMax(TMax value)
Assign a value to Max data member.
void SetMax(TMax value)
Assign a value to Max data member.
Definition: Int_graph_.hpp:300
vector< int > TValues
Definition: Int_graph_.hpp:88
void SetComment(const TComment &value)
Assign a value to Comment data member.
Definition: Seq_graph_.hpp:831
void SetLoc(TLoc &value)
Assign a value to Loc data member.
Definition: Seq_graph_.cpp:224
void SetMin(TMin value)
Assign a value to Min data member.
void SetMin(TMin value)
Assign a value to Min data member.
Definition: Int_graph_.hpp:347
TValues & SetValues(void)
Assign a value to Values data member.
Definition: Int_graph_.hpp:431
void SetData(TData &value)
Assign a value to Data data member.
Definition: Seq_annot_.cpp:244
void SetDesc(TDesc &value)
Assign a value to Desc data member.
Definition: Seq_annot_.cpp:223
TName & SetName(void)
Select the variant.
Definition: Annotdesc_.hpp:508
bool IsSetData(void) const
Check if a value has been assigned to Data data member.
Definition: Seq_annot_.hpp:861
TUser & SetUser(void)
Select the variant.
Definition: Annotdesc_.cpp:190
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
Definition of all error codes used in SRA C++ support libraries.
<!DOCTYPE HTML >< html > n< header > n< title > PubSeq Gateway Help Page</title > n< style > n table
int i
int len
static MDB_envinfo info
Definition: mdb_load.c:37
range(_Ty, _Ty) -> range< _Ty >
constexpr bool empty(list< Ts... >) noexcept
const struct ncbi::grid::netcache::search::fields::SIZE size
T max(T x_, T y_)
T min(T x_, T y_)
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
static unsigned cnt[256]
#define NCBI_THROW2_FMT(exception_class, err_code, message, extra)
Definition: exception.hpp:176
#define row(bind, expected)
Definition: string_bind.c:73
SGraphTableCursor(const CVDBTable &table)
Definition: graphread.cpp:81
#define _ASSERT
#define INIT_VDB_COLUMN(name)
Definition: vdbread.hpp:597
pair< TVDBRowId, TVDBRowCount > TVDBRowIdRange
Definition: vdbread.hpp:84
#define INIT_OPTIONAL_VDB_COLUMN(name)
Definition: vdbread.hpp:603
int64_t TVDBRowId
Definition: vdbread.hpp:80
Modified on Tue May 14 16:24:21 2024 by modify_doxy.py rev. 669887