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

Go to the SVN repository for this file.

1 /* $Id: table_data_seq_table.cpp 47479 2023-05-02 13:24:02Z 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: Roman Katargin
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
36 #include <gui/objutils/label.hpp>
37 
38 #include "table_data_seq_table.hpp"
39 
47 
48 
51 
53 {
55  typeid(ITableData).name(),
56  CSeq_table::GetTypeInfo(),
58 }
59 
61 {
62  CTableDataSeq_table* table_data = new CTableDataSeq_table();
63  table_data->m_Object = object.object;
64  table_data->m_Scope = object.scope;
65  table_data->Init();
66  return table_data;
67 }
68 
70 {
71  try {
72  const CSeq_table& seq_table = dynamic_cast<const CSeq_table&>(*m_Object);
73  size_t colIndex = 0;
74  ITERATE(CSeq_table::TColumns, it, seq_table.GetColumns()) {
75  const CSeqTable_column_info& header = (*it)->GetHeader();
76  if (header.IsSetField_name() && "disabled" == header.GetField_name()) {
77  if ((*it)->IsSetSparse()) {
78  const CSeqTable_sparse_index& sparse_index = (*it)->GetSparse();
79  if (sparse_index.IsIndexes()) {
81  m_InvalidRows.insert(*it2);
82  }
83  }
84  }
85  else
86  m_ColMap.push_back(colIndex);
87  ++colIndex;
88  }
89  }
90  catch (const exception& e) {
91  LOG_POST(Error << "CTableDataSeq_table::Init()" << e.what());
92  }
93 }
94 
96 {
97  try {
98  const CSeqTable_column& column = x_GetColumn(col);
99  if (!column.IsSetData()) {
100  if (column.IsSetDefault()) {
101  const CSeqTable_single_data& defVal = column.GetDefault();
102  switch(defVal.Which()) {
104  return kString;
106  return kInt;
108  return kReal;
112  return kObject;
113  default:
114  return kNone;
115  }
116  }
117  return kNone;
118  }
119 
120  const CSeqTable_multi_data& data = column.GetData();
121  switch(data.Which()) {
123  return kNone;
125  {{
126  const CSeqTable_column_info& header = column.GetHeader();
127  if (header.IsSetField_id() &&
129  return kCommonString;
130  }
131  }}
132  return kInt;
134  return kReal;
136  return kString;
138  return kBytes;
140  return kCommonString;
142  return kCommonBytes;
146  return kObject;
147  default:
148  return kNone;
149  }
150  }
151  catch (const std::exception& e) {
152  LOG_POST(Error << "CTableDataSeq_table: " << e.what());
153  return kNone;
154  }
155 }
156 
157 string CTableDataSeq_table::GetColumnLabel(size_t col) const
158 {
159  string label;
160 
161  try {
162  const CSeqTable_column_info& header = x_GetColumn(col).GetHeader();
163  if (header.IsSetTitle()) {
164  label = header.GetTitle();
165  }
166  else if (header.IsSetField_name()) {
167  label = header.GetField_name();
168  }
169  else if (header.IsSetField_id()) {
170  CSeqTable_column_info::TField_id field_id = header.GetField_id();
171  label = CSeqTable_column_info::ENUM_METHOD_NAME(EField_id)()->FindName(field_id, true);
172  }
173  }
174  catch (const std::exception& e) {
175  LOG_POST(Error << "CTableDataSeq_table: " << e.what());
176  }
177 
178  if (label.empty()) {
179  label = "Column" + NStr::NumericToString(col);
180  }
181 
182  return label;
183 }
184 
186 {
188  return "(invalid)";
189  return "";
190 }
191 
192 static const char* s_StrandValues[] =
193 {
194  "Unknown",
195  "Pos",
196  "Neg",
197  "Both",
198  "Both Rev"
199 };
200 
201 
202 vector<string> CTableDataSeq_table::GetColumnCommonStrings(size_t col) const
203 {
204  vector<string> strings;
205 
206  try {
207  const CSeqTable_column& column = x_GetColumn(col);
208 
209 
210  if (column.IsSetData()) {
211  const CSeqTable_multi_data& data = column.GetData();
212  if (data.IsCommon_string()) {
213  const CCommonString_table& string_table = data.GetCommon_string();
214  ITERATE(CSeqTable_multi_data::TString, it, string_table.GetStrings()) {
215  strings.push_back(*it);
216  }
217  }
218  else if (data.IsInt()) {
219  const CSeqTable_column_info& header = column.GetHeader();
220  if (header.IsSetField_id() &&
222  for (int i = 0; (size_t)i < sizeof(s_StrandValues)/sizeof(s_StrandValues[0]); ++i)
223  strings.push_back(s_StrandValues[i]);
224  }
225  }
226  }
227  }
228  catch (const std::exception& e) {
229  LOG_POST(Error << "CTableDataSeq_table: " << e.what());
230  }
231 
232  return strings;
233 }
234 
236 {
237  try {
238  const CSeq_table& seq_table = dynamic_cast<const CSeq_table&>(*m_Object);
239  return seq_table.GetNum_rows();
240  }
241  catch (const std::exception& e) {
242  LOG_POST(Error << "CTableDataSeq_table: " << e.what());
243  return 0;
244  }
245 }
246 
248 {
249  return m_ColMap.size();
250 }
251 
253 {
254  size_t colIndex = m_ColMap[col];
255  const CSeq_table& seq_table = dynamic_cast<const CSeq_table&>(*m_Object);
256  return *seq_table.GetColumns()[colIndex];
257 }
258 
259 objects::CSeqTable_column& CTableDataSeq_table::x_GetColumn(size_t col)
260 {
261  size_t colIndex = m_ColMap[col];
262  const CSeq_table& seq_table = dynamic_cast<const CSeq_table&>(*m_Object);
263  return *const_cast<CSeq_table&>(seq_table).SetColumns()[colIndex];
264 }
265 
266 void CTableDataSeq_table::GetStringValue(size_t row, size_t col, string& value) const
267 {
268  value.resize(0);
269  try {
270  const CSeqTable_column& column = x_GetColumn(col);
271  if (column.IsSetData()) {
272  const CSeqTable_multi_data& data = column.GetData();
273 
274  switch(data.Which()) {
276  {{
277  const CSeqTable_column_info& header = column.GetHeader();
278  if (header.IsSetField_id() &&
280  long strand = GetIntValue(row, col);
281  if (strand >= 0 && (size_t)strand < sizeof(s_StrandValues)/sizeof(s_StrandValues[0]))
282  value = s_StrandValues[strand];
283  else
284  value = "Other";
285  }
286  else {
288  }
289  }}
290  break;
292  {{
293  const vector<double>& reals = data.GetReal();
294  if (row < reals.size())
295  value = NStr::DoubleToString(reals[row]);
296  }}
297  break;
299  {{
300  vector<SLocLabel>::iterator iter = std::find(
301  m_LocLabels.begin(), m_LocLabels.end(), SLocLabel((int)col));
302 
303  if (iter == m_LocLabels.end()) {
304  m_LocLabels.push_back(SLocLabel((int)col));
305  const CSeq_table& seq_table = dynamic_cast<const CSeq_table&>(*m_Object);
306  m_LocLabels[m_LocLabels.size()-1].m_Labels.resize(seq_table.GetNum_rows());
307  iter = m_LocLabels.begin() + m_LocLabels.size() - 1;
308  }
309 
310  if ((*iter).m_Labels[row].empty()) {
311  string label;
312  const CSeqTable_multi_data::TLoc& locs = data.GetLoc();
313  if (row < locs.size()) {
314  const CSeq_loc& loc = *locs[row];
316  }
317  (*iter).m_Labels[row] = label;
318  }
319  value = (*iter).m_Labels[row];
320  }}
321  break;
323  {{
324  const CSeqTable_multi_data::TId& ids = data.GetId();
325  if (row < ids.size()) {
326  const CSeq_id& seq_id = *ids[row];
328  }
329  }}
330  break;
332  {{
333  const CSeqTable_multi_data::TString& strings = data.GetString();
334  if (row < strings.size())
335  value = strings[row];
336  }}
337  break;
339  {{
340  const CCommonString_table& string_table = data.GetCommon_string();
341  const vector<int>& indexes = string_table.GetIndexes();
342  const CSeqTable_multi_data::TString& strings = string_table.GetStrings();
343  if (row < indexes.size()) {
344  int index = indexes[row];
345  if (index >= 0 && index < (int)strings.size())
346  value = strings[index];
347  }
348  }}
349  break;
350  default:
351  break;
352  }
353  }
354  else if (column.IsSetDefault()) {
355  const CSeqTable_single_data& defVal = column.GetDefault();
356  switch(defVal.Which()) {
358  value = defVal.GetString();
359  break;
361  value = NStr::DoubleToString(defVal.GetInt());
362  break;
364  value = NStr::DoubleToString(defVal.GetReal());
365  break;
368  break;
371  break;
374  break;
375  default:
376  break;
377  }
378  }
379  }
380  catch (const std::exception& e) {
381  LOG_POST(Error << "CTableDataSeq_table: " << e.what());
382  }
383 }
384 
385 
386 long CTableDataSeq_table::GetIntValue(size_t row, size_t col) const
387 {
388  long value = 0;
389  try {
390  bool valueSet = false;
391  const CSeqTable_column& column = x_GetColumn(col);
392  if (column.IsSetData()) {
393  const CSeqTable_multi_data& data = column.GetData();
394  if (data.Which() == CSeqTable_multi_data::e_Int) {
395  const vector<int>& ints = data.GetInt();
396  if (row < ints.size()) {
397  const CSeqTable_column_info& header = column.GetHeader();
398  if (header.IsSetField_id()) {
399  switch (header.GetField_id()) {
404  value = ints[row] + 1;
405  break;
406  default:
407  value = ints[row];
408  break;
409  }
410  }
411  else {
412  value = ints[row];
413  }
414  valueSet = true;
415  }
416  }
417  }
418  if (!valueSet && column.IsSetDefault()) {
419  const CSeqTable_single_data& defVal = column.GetDefault();
420  if (defVal.IsInt()) {
421  value = defVal.GetInt();
422  }
423  }
424  }
425  catch (const std::exception& e) {
426  LOG_POST(Error << "CTableDataSeq_table: " << e.what());
427  }
428 
429  return value;
430 }
431 
432 double CTableDataSeq_table::GetRealValue(size_t row, size_t col) const
433 {
434  double value = 0;
435  try {
436  bool valueSet = false;
437  const CSeqTable_column& column = x_GetColumn(col);
438  if (column.IsSetData()) {
439  const CSeqTable_multi_data& data = column.GetData();
440  if (data.Which() == CSeqTable_multi_data::e_Real) {
441  const vector<double>& reals = data.GetReal();
442  if (row < reals.size()) {
443  value = reals[row];
444  valueSet = true;
445  }
446  }
447  }
448  if (!valueSet && column.IsSetDefault()) {
449  const CSeqTable_single_data& defVal = column.GetDefault();
450  if (defVal.IsReal()) {
451  value = defVal.GetReal();
452  }
453  }
454  }
455  catch (const std::exception& e) {
456  LOG_POST(Error << "CTableDataSeq_table: " << e.what());
457  }
458 
459  return value;
460 }
461 
463 {
465 
466  try {
467  const CSeqTable_column& column = x_GetColumn(col);
468  if (column.IsSetData()) {
469  const CSeqTable_multi_data& data = column.GetData();
470  if (data.Which() == CSeqTable_multi_data::e_Loc) {
471  const CSeqTable_multi_data::TLoc& locs = data.GetLoc();
472  if (row < locs.size()) {
473  value.object.Reset(locs[row].GetPointer());
474  value.scope.Reset(m_Scope.GetPointer());
475  }
476  }
477  else if (data.Which() == CSeqTable_multi_data::e_Id) {
478  const CSeqTable_multi_data::TId& ids = data.GetId();
479  if (row < ids.size()) {
480  value.object.Reset(ids[row].GetPointer());
481  value.scope.Reset(m_Scope.GetPointer());
482  }
483  }
484  else if (data.Which() == CSeqTable_multi_data::e_Interval) {
485  const CSeqTable_multi_data::TInterval& intervals = data.GetInterval();
486  if (row < intervals.size()) {
487  value.object.Reset(intervals[row].GetPointer());
488  value.scope.Reset(m_Scope.GetPointer());
489  }
490  }
491  }
492  else if (column.IsSetDefault()) {
493  const CSeqTable_single_data& defVal = column.GetDefault();
494  if (defVal.IsLoc()) {
495  value.object.Reset(&defVal.GetLoc());
496  value.scope.Reset(m_Scope.GetPointer());
497  }
498  else if (defVal.IsId()) {
499  value.object.Reset(&defVal.GetId());
500  value.scope.Reset(m_Scope.GetPointer());
501  }
502  else if (defVal.IsInterval()) {
503  value.object.Reset(&defVal.GetInterval());
504  value.scope.Reset(m_Scope.GetPointer());
505  }
506  }
507  }
508  catch (const std::exception& e) {
509  LOG_POST(Error << "CTableDataSeq_table: " << e.what());
510  }
511 
512  return value;
513 }
514 
516 {
517  try {
518  const CSeqTable_column& column = x_GetColumn(col);
519  if (!column.IsSetData())
520  return false;
521 
522  const CSeqTable_multi_data& data = column.GetData();
523  switch(data.Which()) {
525  return true;
527  return true;
529  return true;
531  return true;
532  default:
533  return false;
534  }
535  }
536  catch (const std::exception& e) {
537  LOG_POST(Error << "CTableDataSeq_table: " << e.what());
538  return false;
539  }
540 }
541 
542 void CTableDataSeq_table::SetStringValue(size_t row, size_t col, const string& value)
543 {
544  try {
546  if (!column.IsSetData())
547  return;
548 
549  CSeqTable_multi_data& data = column.SetData();
550  switch(data.Which()) {
552  {{
554  if (row < strings.size()) {
555  strings[row] = value;
556  }
557  }}
558  break;
560  {{
561  CCommonString_table& string_table = data.SetCommon_string();
562  vector<int>& indexes = string_table.SetIndexes();
563  const CSeqTable_multi_data::TString& strings = string_table.GetStrings();
564  CSeqTable_multi_data::TString::const_iterator it =
565  std::find (strings.begin(), strings.end(), value);
566  if (it != strings.end()) {
567  indexes[row] = (int)(it - strings.begin());
568  }
569  }}
570  break;
572  {{
573  const CSeqTable_column_info& header = column.GetHeader();
574  if (header.IsSetField_id() &&
576  for (int i = 0; (size_t)i < sizeof(s_StrandValues)/sizeof(s_StrandValues[0]); ++i) {
577  if (value == s_StrandValues[i]) {
578  vector<int>& ints = data.SetInt();
579  if (row < ints.size()) {
580  ints[row] = i;
581  }
582  break;
583  }
584  }
585  }
586  }}
587  default:
588  break;
589  }
590  }
591  catch (const std::exception& e) {
592  LOG_POST(Error << "CTableDataSeq_table: " << e.what());
593  }
594 }
595 
596 void CTableDataSeq_table::SetIntValue(size_t row, size_t col, long value)
597 {
598  try {
600  if (!column.IsSetData())
601  return;
602 
603  CSeqTable_multi_data& data = column.SetData();
604  switch(data.Which()) {
606  {{
607  vector<int>& ints = data.SetInt();
608  if (row < ints.size()) {
609  ints[row] = static_cast<int>(value);
610  }
611  }}
612  break;
613  default:
614  break;
615  }
616  }
617  catch (const std::exception& e) {
618  LOG_POST(Error << "CTableDataSeq_table: " << e.what());
619  }
620 }
621 
622 void CTableDataSeq_table::SetRealValue(size_t row, size_t col, double value)
623 {
624  try {
626  if (!column.IsSetData())
627  return;
628 
629  CSeqTable_multi_data& data = column.SetData();
630  switch(data.Which()) {
632  {{
633  vector<double>& doubles = data.SetReal();
634  if (row < doubles.size()) {
635  doubles[row] = value;
636  }
637  }}
638  break;
639  default:
640  break;
641  }
642  }
643  catch (const std::exception& e) {
644  LOG_POST(Error << "CTableDataSeq_table: " << e.what());
645  }
646 }
647 
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.
User-defined methods of the data storage class.
User-defined methods of the data storage class.
static const signed char indexes[2][4][20]
Definition: camellia.c:187
CCommonString_table –.
static void RegisterFactory(const string &interface_name, IInterfaceFactory *factory)
iterator_bool insert(const value_type &val)
Definition: set.hpp:149
const_iterator find(const key_type &key) const
Definition: set.hpp:137
const_iterator end() const
Definition: set.hpp:136
static const char * column
Definition: stats.c:23
static const char *const strings[]
Definition: utf8.c:21
char data[12]
Definition: iconv.c:80
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define LOG_POST(message)
This macro is deprecated and it's strongly recomended to move in all projects (except tests) to macro...
Definition: ncbidiag.hpp:226
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
virtual void SetStringValue(size_t row, size_t col, const string &value)
static void GetLabel(const CObject &obj, string *label, ELabelType type=eDefault)
Definition: label.cpp:140
virtual vector< string > GetColumnCommonStrings(size_t col) const
void GetStringValue(size_t row, size_t col, string &value) const
const objects::CSeqTable_column & x_GetColumn(size_t col) const
virtual string GetColumnLabel(size_t col) const
virtual long GetIntValue(size_t row, size_t col) const
CConstRef< CObject > m_Object
virtual void SetIntValue(size_t row, size_t col, long value)
virtual void SetRealValue(size_t row, size_t col, double value)
virtual string GetRowLabel(size_t row) const
static CTableDataSeq_table * CreateObject(SConstScopedObject &object, ICreateParams *params)
virtual SConstScopedObject GetObjectValue(size_t row, size_t col) const
vector< SLocLabel > m_LocLabels
virtual size_t GetRowsCount() const
virtual ColumnType GetColumnType(size_t col) const
virtual bool AllowEdit(size_t col)
virtual size_t GetColsCount() const
CRef< objects::CScope > m_Scope
virtual double GetRealValue(size_t row, size_t col) const
@ eContent
Definition: label.hpp:62
#define ENUM_METHOD_NAME(EnumName)
Definition: serialbase.hpp:994
TObjectType * GetPointer(void) THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:998
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static string DoubleToString(double value, int precision=-1, TNumToStringFlags flags=0)
Convert double to string.
Definition: ncbistr.hpp:5187
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 const char label[]
TField_id GetField_id(void) const
Get the Field_id member data.
bool IsId(void) const
Check if variant Id is selected.
const TColumns & GetColumns(void) const
Get the Columns member data.
Definition: Seq_table_.hpp:433
E_Choice Which(void) const
Which variant is currently selected.
bool IsSetTitle(void) const
user friendly column name, can be skipped Check if a value has been assigned to Title data member.
const TId & GetId(void) const
Get the variant data.
const TString & GetString(void) const
Get the variant data.
const TTitle & GetTitle(void) const
Get the Title member data.
const TIndexes & GetIndexes(void) const
Get the Indexes member data.
const TIndexes & GetIndexes(void) const
Get the variant data.
const TInterval & GetInterval(void) const
Get the variant data.
TIndexes & SetIndexes(void)
Assign a value to Indexes data member.
bool IsSetField_name(void) const
any column can be identified by ASN.1 text locator string with omitted object type.
const TLoc & GetLoc(void) const
Get the variant data.
vector< CRef< CSeqTable_column > > TColumns
Definition: Seq_table_.hpp:92
vector< CRef< CSeq_loc > > TLoc
bool IsSetField_id(void) const
Check if a value has been assigned to Field_id data member.
const TField_name & GetField_name(void) const
Get the Field_name member data.
bool IsInt(void) const
Check if variant Int is selected.
bool IsReal(void) const
Check if variant Real is selected.
vector< CRef< CSeq_id > > TId
bool IsLoc(void) const
Check if variant Loc is selected.
TNum_rows GetNum_rows(void) const
Get the Num_rows member data.
Definition: Seq_table_.hpp:393
TReal GetReal(void) const
Get the variant data.
vector< CRef< CSeq_interval > > TInterval
bool IsInterval(void) const
Check if variant Interval is selected.
const TStrings & GetStrings(void) const
Get the Strings member data.
bool IsIndexes(void) const
Check if variant Indexes is selected.
TInt GetInt(void) const
Get the variant data.
@ eField_id_location_strand
location strand
@ eField_id_product_to
product interval to
@ eField_id_product_from
product interval from
@ e_Real
a set of reals, one per row
@ e_Loc
a set of locations, one per row
@ e_not_set
No variant selected.
@ e_Common_bytes
a set of byte arrays with small set of possible values
@ e_Bytes
a set of byte arrays, one per row
@ e_String
a set of strings, one per row
@ e_Int
a set of 4-byte integers, one per row
@ e_Common_string
a set of string with small set of possible values
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
int i
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
#define row(bind, expected)
Definition: string_bind.c:73
USING_SCOPE(objects)
void initCTableDataSeq_table()
static const char * s_StrandValues[]
Modified on Fri Apr 12 17:17:11 2024 by modify_doxy.py rev. 669887