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

Go to the SVN repository for this file.

00001 /*  $Id: table_data_seq_table.cpp 27200 2013-01-09 18:41:56Z katargir $
00002  * ===========================================================================
00003  *
00004  *                            PUBLIC DOMAIN NOTICE
00005  *               National Center for Biotechnology Information
00006  *
00007  *  This software/database is a "United States Government Work" under the
00008  *  terms of the United States Copyright Act.  It was written as part of
00009  *  the author's official duties as a United States Government employee and
00010  *  thus cannot be copyrighted.  This software/database is freely available
00011  *  to the public for use. The National Library of Medicine and the U.S.
00012  *  Government have not placed any restriction on its use or reproduction.
00013  *
00014  *  Although all reasonable efforts have been taken to ensure the accuracy
00015  *  and reliability of the software and data, the NLM and the U.S.
00016  *  Government do not and cannot warrant the performance or results that
00017  *  may be obtained by using this software or data. The NLM and the U.S.
00018  *  Government disclaim all warranties, express or implied, including
00019  *  warranties of performance, merchantability or fitness for any particular
00020  *  purpose.
00021  *
00022  *  Please cite the author in any work or product based on this material.
00023  *
00024  * ===========================================================================
00025  *
00026  * Authors: Roman Katargin
00027  *
00028  * File Description:
00029  *
00030  */
00031 
00032 #include <ncbi_pch.hpp>
00033 
00034 #include <gui/objutils/interface_registry.hpp>
00035 #include <gui/objutils/table_data.hpp>
00036 #include <gui/objutils/label.hpp>
00037 
00038 #include "table_data_seq_table.hpp"
00039 
00040 #include <objects/seqtable/Seq_table.hpp>
00041 #include <objects/seqtable/SeqTable_column.hpp>
00042 #include <objects/seqtable/SeqTable_column_info.hpp>
00043 #include <objects/seqtable/SeqTable_multi_data.hpp>
00044 #include <objects/seqtable/SeqTable_single_data.hpp>
00045 #include <objects/seqtable/SeqTable_sparse_index.hpp>
00046 #include <objects/seqtable/CommonString_table.hpp>
00047 
00048 
00049 BEGIN_NCBI_SCOPE
00050 USING_SCOPE(objects);
00051 
00052 void initCTableDataSeq_table()
00053 {
00054     CInterfaceRegistry::RegisterFactory(
00055             typeid(ITableData).name(),
00056             CSeq_table::GetTypeInfo(),
00057             new CObjectInterfaceFactory<CTableDataSeq_table>());
00058 }
00059 
00060 CTableDataSeq_table* CTableDataSeq_table::CreateObject(SConstScopedObject& object, ICreateParams*)
00061 {
00062     CTableDataSeq_table* table_data = new CTableDataSeq_table();
00063     table_data->m_Object = object.object;
00064     table_data->m_Scope  = object.scope;
00065     table_data->Init();
00066     return table_data;
00067 }
00068 
00069 void CTableDataSeq_table::Init()
00070 {
00071     try {
00072         const CSeq_table& seq_table = dynamic_cast<const CSeq_table&>(*m_Object);
00073         size_t colIndex = 0;
00074         ITERATE(CSeq_table::TColumns, it, seq_table.GetColumns()) {
00075             const CSeqTable_column_info& header = (*it)->GetHeader();
00076             if (header.IsSetField_name() && "disabled" == header.GetField_name()) {
00077                 if ((*it)->IsSetSparse()) {
00078                     const CSeqTable_sparse_index& sparse_index = (*it)->GetSparse();
00079                     if (sparse_index.IsIndexes()) {
00080                         ITERATE(CSeqTable_sparse_index::TIndexes, it2, sparse_index.GetIndexes())
00081                             m_InvalidRows.insert(*it2);
00082                     }
00083                 }
00084             }
00085             else
00086                 m_ColMap.push_back(colIndex);
00087             ++colIndex;
00088         }
00089     }
00090     catch (const exception& e) {
00091         LOG_POST(Error << "CTableDataSeq_table::Init()" << e.what());
00092     }
00093 }
00094 
00095 ITableData::ColumnType CTableDataSeq_table::GetColumnType(size_t col) const
00096 {
00097     try {
00098         const CSeqTable_column& column = x_GetColumn(col);
00099         if (!column.IsSetData()) {
00100             if (column.IsSetDefault()) {
00101                 const CSeqTable_single_data& defVal = column.GetDefault();
00102                 switch(defVal.Which()) {
00103                     case CSeqTable_single_data::e_String:
00104                         return kString;
00105                     case CSeqTable_single_data::e_Int:
00106                         return kInt;
00107                     case CSeqTable_single_data::e_Real:
00108                         return kReal;
00109                     case CSeqTable_single_data::e_Loc:
00110                     case CSeqTable_single_data::e_Id:
00111                     case CSeqTable_single_data::e_Interval:
00112                         return kObject;
00113                     default:
00114                         return kNone;
00115                 }
00116             }
00117             return kNone;
00118         }
00119 
00120         const CSeqTable_multi_data& data = column.GetData();
00121         switch(data.Which()) {
00122         case CSeqTable_multi_data_Base::e_not_set:
00123             return kNone;
00124         case CSeqTable_multi_data_Base::e_Int:
00125             {{
00126                 const CSeqTable_column_info& header = column.GetHeader();
00127                 if (header.IsSetField_id() &&
00128                     header.GetField_id() == CSeqTable_column_info::eField_id_location_strand) {
00129                     return kCommonString;
00130                 }
00131             }}
00132             return kInt;
00133         case CSeqTable_multi_data_Base::e_Real:
00134             return kReal;
00135         case CSeqTable_multi_data_Base::e_String:
00136             return kString;
00137         case CSeqTable_multi_data_Base::e_Bytes:
00138             return kBytes;
00139         case CSeqTable_multi_data_Base::e_Common_string:
00140             return kCommonString;
00141         case CSeqTable_multi_data_Base::e_Common_bytes:
00142             return kCommonBytes;
00143         case CSeqTable_multi_data_Base::e_Loc:
00144         case CSeqTable_multi_data_Base::e_Id:
00145         case CSeqTable_multi_data_Base::e_Interval:
00146             return kObject;
00147         default:
00148             return kNone;
00149         }
00150     }
00151     catch (const std::exception& e) {
00152         LOG_POST(Error << "CTableDataSeq_table: " << e.what());
00153         return kNone;
00154     }
00155 }
00156 
00157 string CTableDataSeq_table::GetColumnLabel(size_t col) const
00158 {
00159     string label;
00160 
00161     try {
00162         const CSeqTable_column_info& header = x_GetColumn(col).GetHeader();
00163         if (header.IsSetTitle()) {
00164             label = header.GetTitle();
00165         }
00166         else if (header.IsSetField_name()) {
00167             label = header.GetField_name();
00168         }
00169         else if (header.IsSetField_id()) {
00170             CSeqTable_column_info::TField_id field_id = header.GetField_id();
00171             label = CSeqTable_column_info::ENUM_METHOD_NAME(EField_id)()->FindName(field_id, true);
00172         }
00173     }
00174     catch (const std::exception& e) {
00175         LOG_POST(Error << "CTableDataSeq_table: " << e.what());
00176     }
00177 
00178     if (label.empty()) {
00179         label = "Column" + NStr::NumericToString(col);
00180     }
00181 
00182     return label;
00183 }
00184 
00185 string CTableDataSeq_table::GetRowLabel(size_t row) const
00186 {
00187     if (m_InvalidRows.find(row) != m_InvalidRows.end())
00188         return "(invalid)";
00189     return "";
00190 }
00191 
00192 static const char* s_StrandValues[] =
00193 {
00194     "Unknown",
00195     "Pos",
00196     "Neg",
00197     "Both",
00198     "Both Rev"
00199 };
00200 
00201 
00202 vector<string> CTableDataSeq_table::GetColumnCommonStrings(size_t col) const
00203 {
00204     vector<string> strings;
00205 
00206     try {
00207         const CSeqTable_column& column = x_GetColumn(col);
00208 
00209 
00210         if (column.IsSetData()) {
00211             const CSeqTable_multi_data& data = column.GetData();
00212             if (data.IsCommon_string()) {
00213                 const CCommonString_table& string_table = data.GetCommon_string();
00214                 ITERATE(CSeqTable_multi_data::TString, it, string_table.GetStrings()) {
00215                     strings.push_back(*it);
00216                 }
00217             }
00218             else if (data.IsInt()) {
00219                 const CSeqTable_column_info& header = column.GetHeader();
00220                 if (header.IsSetField_id() &&
00221                     header.GetField_id() == CSeqTable_column_info::eField_id_location_strand) {
00222                     for (int i = 0; (size_t)i < sizeof(s_StrandValues)/sizeof(s_StrandValues[0]); ++i)
00223                         strings.push_back(s_StrandValues[i]);
00224                 }
00225             }
00226         }
00227     }
00228     catch (const std::exception& e) {
00229         LOG_POST(Error << "CTableDataSeq_table: " << e.what());
00230     }
00231 
00232     return strings;
00233 }
00234 
00235 size_t CTableDataSeq_table::GetRowsCount() const
00236 {
00237     try {
00238         const CSeq_table& seq_table = dynamic_cast<const CSeq_table&>(*m_Object);
00239         return seq_table.GetNum_rows();
00240     }
00241     catch (const std::exception& e) {
00242         LOG_POST(Error << "CTableDataSeq_table: " << e.what());
00243         return 0;
00244     }
00245 }
00246 
00247 size_t CTableDataSeq_table::GetColsCount() const
00248 {
00249     return m_ColMap.size();
00250 }
00251 
00252 const CSeqTable_column& CTableDataSeq_table::x_GetColumn(size_t col) const
00253 {
00254     size_t colIndex = m_ColMap[col];
00255     const CSeq_table& seq_table = dynamic_cast<const CSeq_table&>(*m_Object);
00256     return *seq_table.GetColumns()[colIndex];
00257 }
00258 
00259 objects::CSeqTable_column& CTableDataSeq_table::x_GetColumn(size_t col)
00260 {
00261     size_t colIndex = m_ColMap[col];
00262     const CSeq_table& seq_table = dynamic_cast<const CSeq_table&>(*m_Object);
00263     return *const_cast<CSeq_table&>(seq_table).SetColumns()[colIndex];
00264 }
00265 
00266 
00267 string CTableDataSeq_table::GetStringValue(size_t row, size_t col) const
00268 {
00269     string value;
00270     try {
00271         const CSeqTable_column& column = x_GetColumn(col);
00272         if (column.IsSetData()) {
00273             const CSeqTable_multi_data& data = column.GetData();
00274 
00275             switch(data.Which()) {
00276                 case CSeqTable_multi_data::e_Int:
00277                     {{
00278                         const CSeqTable_column_info& header = column.GetHeader();
00279                         if (header.IsSetField_id() &&
00280                             header.GetField_id() == CSeqTable_column_info::eField_id_location_strand) {
00281                             long strand = GetIntValue(row, col);
00282                             if (strand >= 0 && (size_t)strand < sizeof(s_StrandValues)/sizeof(s_StrandValues[0]))
00283                                 value = s_StrandValues[strand];
00284                             else
00285                                 value = "Other";
00286                         }
00287                         else {
00288                             value = NStr::NumericToString(GetIntValue(row, col));
00289                         }
00290                     }}
00291                     break;
00292                 case CSeqTable_multi_data_Base::e_Real:
00293                     {{
00294                         const vector<double>& reals = data.GetReal();
00295                         if (row < reals.size())
00296                             value = NStr::DoubleToString(reals[row]);
00297                     }}
00298                     break;
00299                 case CSeqTable_multi_data::e_Loc:
00300                     {{
00301                         vector<SLocLabel>::iterator iter = std::find(
00302                             m_LocLabels.begin(), m_LocLabels.end(), SLocLabel((int)col));
00303 
00304                         if (iter == m_LocLabels.end()) {
00305                             m_LocLabels.push_back(SLocLabel((int)col));
00306                             const CSeq_table& seq_table = dynamic_cast<const CSeq_table&>(*m_Object); 
00307                             m_LocLabels[m_LocLabels.size()-1].m_Labels.resize(seq_table.GetNum_rows());
00308                             iter = m_LocLabels.begin() + m_LocLabels.size() - 1;
00309                         }
00310                             
00311                         if ((*iter).m_Labels[row].empty()) {
00312                             string label;
00313                             const CSeqTable_multi_data::TLoc& locs = data.GetLoc();
00314                             if (row < locs.size()) {
00315                                 const CSeq_loc& loc = *locs[row];
00316                                 CLabel::GetLabel(loc, &label, CLabel::eContent, m_Scope);
00317                             }
00318                             (*iter).m_Labels[row] = label;
00319                         }
00320                         value = (*iter).m_Labels[row];
00321                     }}
00322                     break;
00323                 case CSeqTable_multi_data::e_Id:
00324                     {{
00325                         const CSeqTable_multi_data::TId& ids = data.GetId();
00326                         if (row < ids.size()) {
00327                             const CSeq_id& seq_id = *ids[row];
00328                             CLabel::GetLabel(seq_id, &value, CLabel::eContent, m_Scope);
00329                         }
00330                     }}
00331                     break;
00332                 case CSeqTable_multi_data::e_String:
00333                     {{
00334                         const CSeqTable_multi_data::TString& strings = data.GetString();
00335                         if (row < strings.size())
00336                             value = strings[row];
00337                     }}
00338                     break;
00339                 case CSeqTable_multi_data::e_Common_string:
00340                     {{
00341                         const CCommonString_table& string_table = data.GetCommon_string();
00342                         const vector<int>& indexes = string_table.GetIndexes();
00343                         const CSeqTable_multi_data::TString& strings = string_table.GetStrings();
00344                         if (row < indexes.size()) {
00345                             int index = indexes[row];
00346                             if (index >= 0 && index < (int)strings.size())
00347                                 value = strings[index];
00348                         }
00349                     }}
00350                     break;
00351                 default:
00352                     break;
00353             }
00354         }
00355         else if (column.IsSetDefault()) {
00356             const CSeqTable_single_data& defVal = column.GetDefault();
00357             switch(defVal.Which()) {
00358                 case CSeqTable_single_data::e_String:
00359                     value = defVal.GetString();
00360                     break;
00361                 case CSeqTable_single_data::e_Int:
00362                     value = NStr::DoubleToString(defVal.GetInt());
00363                     break;
00364                 case CSeqTable_single_data::e_Real:
00365                     value = NStr::DoubleToString(defVal.GetReal());
00366                     break;
00367                 case CSeqTable_single_data::e_Loc:
00368                     CLabel::GetLabel(defVal.GetLoc(), &value, CLabel::eContent, m_Scope);
00369                     break;
00370                 case CSeqTable_single_data::e_Id:
00371                     CLabel::GetLabel(defVal.GetId(), &value, CLabel::eContent, m_Scope);
00372                     break;
00373                 case CSeqTable_single_data::e_Interval:
00374                     CLabel::GetLabel(defVal.GetInterval(), &value, CLabel::eContent, m_Scope);
00375                     break;
00376                 default:
00377                     break;
00378             }
00379         }
00380     }
00381     catch (const std::exception& e) {
00382         LOG_POST(Error << "CTableDataSeq_table: " << e.what());
00383     }
00384 
00385     return value;
00386 }
00387 
00388 long CTableDataSeq_table::GetIntValue(size_t row, size_t col) const
00389 {
00390     long value = 0;
00391     try {
00392         bool valueSet = false;
00393         const CSeqTable_column& column = x_GetColumn(col);
00394         if (column.IsSetData()) {
00395             const CSeqTable_multi_data& data = column.GetData();
00396             if (data.Which() == CSeqTable_multi_data::e_Int) {
00397                 const vector<int>& ints = data.GetInt();
00398                 if (row < ints.size()) {
00399                     const CSeqTable_column_info& header = column.GetHeader();
00400                     if (header.IsSetField_id()) {
00401                         switch (header.GetField_id()) {
00402                         case CSeqTable_column_info::eField_id_location_from :
00403                         case CSeqTable_column_info::eField_id_location_to :
00404                         case CSeqTable_column_info::eField_id_product_from :
00405                         case CSeqTable_column_info::eField_id_product_to :
00406                             value = ints[row] + 1;
00407                             break;
00408                         default:
00409                             value = ints[row];
00410                             break;
00411                         }
00412                     }
00413                     else {
00414                         value = ints[row];
00415                     }
00416                     valueSet = true;
00417                 }
00418             }
00419         }
00420         if (!valueSet && column.IsSetDefault()) {
00421             const CSeqTable_single_data& defVal = column.GetDefault();
00422             if (defVal.IsInt()) {
00423                 value = defVal.GetInt();
00424             }
00425         }
00426     }
00427     catch (const std::exception& e) {
00428         LOG_POST(Error << "CTableDataSeq_table: " << e.what());
00429     }
00430 
00431     return value;
00432 }
00433 
00434 double CTableDataSeq_table::GetRealValue(size_t row, size_t col) const
00435 {
00436     double value = 0;
00437     try {
00438         bool valueSet = false;
00439         const CSeqTable_column& column = x_GetColumn(col);
00440         if (column.IsSetData()) {
00441             const CSeqTable_multi_data& data = column.GetData();
00442             if (data.Which() == CSeqTable_multi_data::e_Real) {
00443                 const vector<double>& reals = data.GetReal();
00444                 if (row < reals.size()) {
00445                     value = reals[row];
00446                     valueSet = true;
00447                 }
00448             }
00449         }
00450         if (!valueSet && column.IsSetDefault()) {
00451             const CSeqTable_single_data& defVal = column.GetDefault();
00452             if (defVal.IsReal()) {
00453                 value = defVal.GetReal();
00454             }
00455         }
00456     }
00457     catch (const std::exception& e) {
00458         LOG_POST(Error << "CTableDataSeq_table: " << e.what());
00459     }
00460 
00461     return value;
00462 }
00463 
00464 SConstScopedObject CTableDataSeq_table::GetObjectValue(size_t row, size_t col) const
00465 {
00466     SConstScopedObject value;
00467 
00468     try {
00469         const CSeqTable_column& column = x_GetColumn(col);
00470         if (column.IsSetData()) {
00471             const CSeqTable_multi_data& data = column.GetData();
00472             if (data.Which() == CSeqTable_multi_data::e_Loc) {
00473                 const CSeqTable_multi_data::TLoc& locs = data.GetLoc();
00474                 if (row < locs.size()) {
00475                     value.object.Reset(locs[row].GetPointer());
00476                     value.scope.Reset(m_Scope.GetPointer());
00477                 }
00478             }
00479             else if (data.Which() == CSeqTable_multi_data::e_Id) {
00480                 const CSeqTable_multi_data::TId& ids = data.GetId();
00481                 if (row < ids.size()) {
00482                     value.object.Reset(ids[row].GetPointer());
00483                     value.scope.Reset(m_Scope.GetPointer());
00484                 }
00485             }
00486             else if (data.Which() == CSeqTable_multi_data::e_Interval) {
00487                 const CSeqTable_multi_data::TInterval& intervals = data.GetInterval();
00488                 if (row < intervals.size()) {
00489                     value.object.Reset(intervals[row].GetPointer());
00490                     value.scope.Reset(m_Scope.GetPointer());
00491                 }
00492             }
00493         }
00494         else if (column.IsSetDefault()) {
00495             const CSeqTable_single_data& defVal = column.GetDefault();
00496             if (defVal.IsLoc()) {
00497                 value.object.Reset(&defVal.GetLoc());
00498                 value.scope.Reset(m_Scope.GetPointer());
00499             }
00500             else if (defVal.IsId()) {
00501                 value.object.Reset(&defVal.GetId());
00502                 value.scope.Reset(m_Scope.GetPointer());
00503             }
00504             else if (defVal.IsInterval()) {
00505                 value.object.Reset(&defVal.GetInterval());
00506                 value.scope.Reset(m_Scope.GetPointer());
00507             }
00508         }
00509     }
00510     catch (const std::exception& e) {
00511         LOG_POST(Error << "CTableDataSeq_table: " << e.what());
00512     }
00513 
00514     return value;
00515 }
00516 
00517 bool CTableDataSeq_table::AllowEdit(size_t col)
00518 {
00519     try {
00520         const CSeqTable_column& column = x_GetColumn(col);
00521         if (!column.IsSetData())
00522             return false;
00523 
00524         const CSeqTable_multi_data& data = column.GetData();
00525         switch(data.Which()) {
00526         case CSeqTable_multi_data_Base::e_String:
00527             return true;
00528         case CSeqTable_multi_data_Base::e_Int:
00529             return true;
00530         case CSeqTable_multi_data_Base::e_Real:
00531             return true;
00532         case CSeqTable_multi_data_Base::e_Common_string:
00533             return true;
00534         default:
00535             return false;
00536         }
00537     }
00538     catch (const std::exception& e) {
00539         LOG_POST(Error << "CTableDataSeq_table: " << e.what());
00540         return false;
00541     }
00542 }
00543 
00544 void CTableDataSeq_table::SetStringValue(size_t row, size_t col, const string& value)
00545 {
00546     try {
00547         CSeqTable_column& column = x_GetColumn(col);
00548         if (!column.IsSetData())
00549             return;
00550 
00551         CSeqTable_multi_data& data = column.SetData();
00552         switch(data.Which()) {
00553         case CSeqTable_multi_data_Base::e_String:
00554             {{
00555                 CSeqTable_multi_data::TString& strings = data.SetString();
00556                 if (row < strings.size()) {
00557                     strings[row] = value;
00558                 }
00559             }}
00560             break;
00561         case CSeqTable_multi_data_Base::e_Common_string:
00562             {{
00563                 CCommonString_table& string_table = data.SetCommon_string();
00564                 vector<int>& indexes = string_table.SetIndexes();
00565                 const CSeqTable_multi_data::TString& strings = string_table.GetStrings();
00566                 CSeqTable_multi_data::TString::const_iterator it =
00567                     std::find (strings.begin(), strings.end(), value);
00568                 if (it != strings.end()) {
00569                     indexes[row] = (int)(it - strings.begin());
00570                 }
00571             }}
00572             break;
00573         case CSeqTable_multi_data_Base::e_Int:
00574             {{
00575                 const CSeqTable_column_info& header = column.GetHeader();
00576                 if (header.IsSetField_id() &&
00577                     header.GetField_id() == CSeqTable_column_info::eField_id_location_strand) {
00578                     for (int i = 0; (size_t)i < sizeof(s_StrandValues)/sizeof(s_StrandValues[0]); ++i) {
00579                         if (value == s_StrandValues[i]) {
00580                             vector<int>& ints = data.SetInt();
00581                             if (row < ints.size()) {
00582                                 ints[row] = i;
00583                             }
00584                             break;
00585                         }
00586                     }
00587                 }
00588             }}
00589         default:
00590             break;
00591         }
00592     }
00593     catch (const std::exception& e) {
00594         LOG_POST(Error << "CTableDataSeq_table: " << e.what());
00595     }
00596 }
00597 
00598 void CTableDataSeq_table::SetIntValue(size_t row, size_t col, long value)
00599 {
00600     try {
00601         CSeqTable_column& column = x_GetColumn(col);
00602         if (!column.IsSetData())
00603             return;
00604 
00605         CSeqTable_multi_data& data = column.SetData();
00606         switch(data.Which()) {
00607         case CSeqTable_multi_data_Base::e_Int:
00608             {{
00609                 vector<int>& ints = data.SetInt();
00610                 if (row < ints.size()) {
00611                     ints[row] = value;
00612                 }
00613             }}
00614             break;
00615         default:
00616             break;
00617         }
00618     }
00619     catch (const std::exception& e) {
00620         LOG_POST(Error << "CTableDataSeq_table: " << e.what());
00621     }
00622 }
00623 
00624 void CTableDataSeq_table::SetRealValue(size_t row, size_t col, double value)
00625 {
00626     try {
00627         CSeqTable_column& column = x_GetColumn(col);
00628         if (!column.IsSetData())
00629             return;
00630 
00631         CSeqTable_multi_data& data = column.SetData();
00632         switch(data.Which()) {
00633         case CSeqTable_multi_data_Base::e_Real:
00634             {{
00635                 vector<double>& doubles = data.SetReal();
00636                 if (row < doubles.size()) {
00637                     doubles[row] = value;
00638                 }
00639             }}
00640             break;
00641         default:
00642             break;
00643         }
00644     }
00645     catch (const std::exception& e) {
00646         LOG_POST(Error << "CTableDataSeq_table: " << e.what());
00647     }
00648 }
00649 
00650 END_NCBI_SCOPE
Modified on Fri Aug 22 18:17:35 2014 by modify_doxy.py rev. 426318