src/gui/objutils/objtype_converters.cpp

Go to the documentation of this file.
00001 /*  $Id: objtype_converters.cpp 14892 2007-08-29 14:14:42Z dicuccio $
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 #include <serial/iterator.hpp>
00034 #include <serial/objectiter.hpp>
00035 #include <util/resize_iter.hpp>
00036 #include <gui/utils/init_registrar.hpp>
00037 #include <gui/objutils/object_factory.hpp>
00038 #include <gui/objutils/objects_interface.hpp>
00039 
00040 #include <objects/general/Dbtag.hpp>
00041 #include <objects/general/Date_std.hpp>
00042 #include <objects/general/Dbtag.hpp>
00043 #include <objects/seqfeat/Genetic_code.hpp>
00044 #include <objects/seqfeat/Genetic_code_table.hpp>
00045 #include <objects/seqfeat/Seq_feat.hpp>
00046 #include <objects/seqfeat/SeqFeatXref.hpp>
00047 #include <objmgr/util/seq_loc_util.hpp>
00048 #include <objmgr/feat_ci.hpp>
00049 
00050 #include "conv_seq_loc.cpp"
00051 
00052 
00053 BEGIN_NCBI_SCOPE
00054 USING_SCOPE(objects);
00055 
00056 // CDefaultTypeConverter
00057 
00058 class CDefaultTypeConverter : public CObject, public IObjTypeConverter
00059 {
00060 public:
00061     virtual CObjectInfo ConvertFromString (IObjTypeConverterContext* context,
00062                                            const string& value);
00063 
00064     virtual string ConvertToString (IObjTypeConverterContext* context,
00065                                     CConstObjectInfo& value);
00066 private:
00067     string ClassToString    (IObjTypeConverterContext* context,
00068                              CConstObjectInfo& info);
00069     string PrimitiveToString(IObjTypeConverterContext* context,
00070                              CConstObjectInfo& info);
00071     string ContainerToString(IObjTypeConverterContext* context,
00072                              CConstObjectInfo& info);
00073 };
00074 
00075 CObjectInfo CDefaultTypeConverter::ConvertFromString (
00076     IObjTypeConverterContext* context, const string& value)
00077 {
00078     CObjectInfo info;
00079     return info;
00080 }
00081 
00082 string CDefaultTypeConverter::ConvertToString (
00083     IObjTypeConverterContext* context, CConstObjectInfo& value)
00084 {
00085     if (!value)
00086         return "N/A";
00087 
00088     switch(value.GetTypeFamily()) {
00089     case eTypeFamilyClass:
00090         return "[" + value.GetTypeInfo()->GetName() + "]";
00091     case eTypeFamilyPrimitive :
00092         return PrimitiveToString(context, value);
00093     case eTypeFamilyChoice :
00094         {
00095             CConstObjectInfo var = *value.GetCurrentChoiceVariant();
00096             if (var.GetTypeFamily() == eTypeFamilyClass)
00097                 return ClassToString(context, var);
00098             return ConvertToString(context, var);
00099         }
00100     case eTypeFamilyContainer:
00101         return ContainerToString(context, value);
00102     case eTypeFamilyPointer:
00103         {
00104             CConstObjectInfo pointed = value.GetPointedObject();
00105             if (pointed.GetTypeFamily() == eTypeFamilyClass)
00106                 return ClassToString(context, pointed);
00107             return ConvertToString(context, pointed);
00108         }
00109     }
00110     return "Invalid object";
00111 }
00112 
00113 string CDefaultTypeConverter::PrimitiveToString
00114     (IObjTypeConverterContext* context, CConstObjectInfo& info)
00115 {
00116     string value = "N/A";
00117     switch (info.GetPrimitiveValueType()) {
00118     case ePrimitiveValueSpecial:        // null, void
00119         value = "NULL";
00120         break;
00121     case ePrimitiveValueBool:           // bool
00122         value = NStr::BoolToString(info.GetPrimitiveValueBool());
00123         break;
00124     case ePrimitiveValueChar:           // char
00125         value = info.GetPrimitiveValueChar();
00126         break;
00127     case ePrimitiveValueInteger:        // (signed|unsigned) (char|short|int|long)
00128         if ( info.IsPrimitiveValueSigned() ) {
00129             NStr::Int8ToString(value, info.GetPrimitiveValueInt8());
00130         } else {
00131             NStr::UInt8ToString(value, info.GetPrimitiveValueUint8());
00132         }
00133         break;
00134     case ePrimitiveValueReal:           // float|double
00135         NStr::DoubleToString(value, info.GetPrimitiveValueDouble());
00136         break;
00137     case ePrimitiveValueString:         // string|char*|const char*
00138         info.GetPrimitiveValueString(value);
00139         break;
00140     case ePrimitiveValueEnum:           // enum
00141         try {
00142             info.GetPrimitiveValueString(value);
00143         } catch (CSerialException&) {
00144             NStr::IntToString(value, info.GetPrimitiveValueInt());
00145         }
00146         break;
00147     case ePrimitiveValueOctetString:    // vector<(signed|unsigned)? char>
00148         {{
00149             vector<char> octet;
00150             info.GetPrimitiveValueOctetString(octet);
00151             CNcbiOstrstream os;
00152             for (CConstResizingIterator<vector<char> > it(octet.begin(), octet.end(), 4); !it.AtEnd(); ++it) {
00153                 os << hex << *it;
00154             }
00155             value = CNcbiOstrstreamToString(os);
00156         }}
00157         break;
00158     default:
00159         break;
00160     }
00161     return value;
00162 }
00163 
00164 string CDefaultTypeConverter::ClassToString
00165     (IObjTypeConverterContext* context, CConstObjectInfo& info)
00166 {
00167     CIRef<objects::IObjTypeConverter>
00168         conv(CreateInterface<IObjTypeConverter>(info));
00169     return conv->ConvertToString(context, info);
00170 }
00171 
00172 string CDefaultTypeConverter::ContainerToString
00173     (IObjTypeConverterContext* context, CConstObjectInfo& info)
00174 {
00175     vector<string> values;
00176     for (CConstObjectInfo::CElementIterator it = info.BeginElements(); it; ++it)
00177     {
00178         CConstObjectInfo elem_info = *it;
00179         values.push_back(ConvertToString(context, elem_info));
00180     }
00181     return NStr::Join(values, ",");
00182 }
00183 
00184 // CDbtagTypeConverter
00185 
00186 class CDbtagTypeConverter : public CObject, public IObjTypeConverter
00187 {
00188 public:
00189     virtual CObjectInfo ConvertFromString (IObjTypeConverterContext* context,
00190                                            const string& value);
00191 
00192     virtual string ConvertToString (IObjTypeConverterContext* context,
00193                                     CConstObjectInfo& value);
00194 };
00195 
00196 CObjectInfo CDbtagTypeConverter::ConvertFromString (
00197     IObjTypeConverterContext* context, const string& value)
00198 {
00199     CObjectInfo info;
00200     return info;
00201 }
00202 
00203 string CDbtagTypeConverter::ConvertToString (
00204     IObjTypeConverterContext* context, CConstObjectInfo& value)
00205 {
00206     string str;
00207     const CDbtag* dbTag = reinterpret_cast<const CDbtag*>(value.GetObjectPtr());
00208     dbTag->GetLabel(&str);
00209     return str;
00210 }
00211 
00212 // CDateTypeConverter
00213 
00214 class CDateTypeConverter : public CObject, public IObjTypeConverter
00215 {
00216 public:
00217     virtual CObjectInfo ConvertFromString (IObjTypeConverterContext* context,
00218                                            const string& value);
00219 
00220     virtual string ConvertToString (IObjTypeConverterContext* context,
00221                                     CConstObjectInfo& value);
00222 };
00223 
00224 CObjectInfo CDateTypeConverter::ConvertFromString (
00225     IObjTypeConverterContext* context, const string& value)
00226 {
00227     CObjectInfo info;
00228     return info;
00229 }
00230 
00231 string CDateTypeConverter::ConvertToString (
00232     IObjTypeConverterContext* context, CConstObjectInfo& value)
00233 {
00234     string str;
00235     const CDate_std* date = reinterpret_cast<const CDate_std*>(value.GetObjectPtr());
00236     date->GetDate(&str, "%M/%D/%Y");
00237     return str;
00238 }
00239 
00240 // CSeq_idTypeConverter
00241 
00242 class CSeq_idTypeConverter : public CObject, public IObjTypeConverter
00243 {
00244 public:
00245     virtual CObjectInfo ConvertFromString (IObjTypeConverterContext* context,
00246                                            const string& value);
00247 
00248     virtual string ConvertToString (IObjTypeConverterContext* context,
00249                                     CConstObjectInfo& value);
00250 };
00251 
00252 CObjectInfo CSeq_idTypeConverter::ConvertFromString (
00253     IObjTypeConverterContext* context, const string& value)
00254 {
00255     CObjectInfo info;
00256     return info;
00257 }
00258 
00259 string CSeq_idTypeConverter::ConvertToString (
00260     IObjTypeConverterContext* context, CConstObjectInfo& value)
00261 {
00262     string str;
00263     const CSeq_id* id = reinterpret_cast<const CSeq_id*>(value.GetObjectPtr());
00264     return id->AsFastaString();
00265 }
00266 
00267 // CGenetic_code
00268 
00269 class CGenetic_codeTypeConverter : public CObject, public IObjTypeConverter
00270 {
00271 public:
00272     virtual CObjectInfo ConvertFromString (IObjTypeConverterContext* context,
00273                                            const string& value);
00274 
00275     virtual string ConvertToString (IObjTypeConverterContext* context,
00276                                     CConstObjectInfo& value);
00277 };
00278 
00279 CObjectInfo CGenetic_codeTypeConverter::ConvertFromString (
00280     IObjTypeConverterContext* context, const string& value)
00281 {
00282     CObjectInfo info;
00283     return info;
00284 }
00285 
00286 string CGenetic_codeTypeConverter::ConvertToString (
00287     IObjTypeConverterContext* context, CConstObjectInfo& value)
00288 {
00289     string str;
00290 
00291     const CGenetic_code* code = reinterpret_cast<const CGenetic_code*>(value.GetObjectPtr());
00292     str = code->GetName();
00293     int id = code->GetId();
00294 
00295     const CGenetic_code_table& code_table = CGen_code_table::GetCodeTable();
00296     const CGenetic_code_table::Tdata& codes = code_table.Get();
00297 
00298     bool idFound = false;
00299     int idByName = 0;
00300     ITERATE (CGenetic_code_table::Tdata, it, codes) {
00301         if ((*it)->GetId() == id)
00302             idFound = true;
00303 
00304         if (!str.empty() && (*it)->GetName() == str)
00305             idByName = (*it)->GetId();
00306     }
00307 
00308     if (!idFound)
00309         id = (idByName != 0) ? idByName : 1;
00310 
00311     ITERATE (CGenetic_code_table::Tdata, it, codes) {
00312         if ((*it)->GetId() == id) {
00313             str = (*it)->GetName();
00314             break;
00315         }
00316     }
00317 
00318     return str;
00319 }
00320 
00321 // CSeq_feat IExtProperty
00322 
00323 class CSeq_feat_ExtProperty : public CObject, public IExtProperty
00324 {
00325 public:
00326     virtual CConstObjectInfo GetProperty (
00327         const char* propName, const CObject* obj, CScope& scope);
00328 };
00329 
00330 CConstObjectInfo CSeq_feat_ExtProperty::GetProperty (
00331             const char* propName, const CObject* obj, CScope& scope)
00332 {
00333     CConstObjectInfo info;
00334     const CSeq_feat* feat = dynamic_cast<const CSeq_feat*>(obj);
00335     if (!feat)
00336         return info;
00337 
00338     if (strcmp(propName, "@protein") == 0) {
00339         const CProt_ref* protRef = feat->GetProtXref();
00340 
00341         if (feat->IsSetProduct()) {
00342             const CSeq_loc& product = feat->GetProduct();
00343             int dist = numeric_limits<int>::max();
00344             CFeat_CI it(scope, product, SAnnotSelector(CSeqFeatData::e_Prot));
00345             for (; it; ++it) {
00346                 const CSeq_loc& loc = it->GetLocation();
00347                 sequence::ECompare comp = sequence::Compare(product, loc, &scope);
00348                 if (comp == sequence::eSame) {
00349                     protRef = &it->GetOriginalFeature().GetData().GetProt();
00350                     break;
00351                 }
00352                 else if (comp == sequence::eContains) {
00353                     int tmp = sequence::GetLength(product, &scope) -
00354                         sequence::GetLength(loc, &scope);
00355                     if (tmp < dist) {
00356                         dist = tmp;
00357                         protRef = &it->GetOriginalFeature().GetData().GetProt();
00358                     }
00359                 }
00360 
00361             }
00362         }
00363 
00364         info = ConstObjectInfo(*protRef);
00365     }
00366 
00367     return info;
00368 }
00369 
00370 // CSeq_loc IExtProperty
00371 
00372 class CSeq_loc_ExtProperty : public CObject, public IExtProperty
00373 {
00374 public:
00375     virtual CConstObjectInfo GetProperty (
00376         const char* propName, const CObject* obj, CScope& scope);
00377 };
00378 
00379 CConstObjectInfo CSeq_loc_ExtProperty::GetProperty (
00380             const char* propName, const CObject* obj, CScope& scope)
00381 {
00382     CConstObjectInfo info;
00383     const CSeq_loc* loc = dynamic_cast<const CSeq_loc*>(obj);
00384     if (!loc)
00385         return info;
00386 
00387     if (strcmp(propName, "@id") == 0) {
00388         const CSeq_id& id = sequence::GetId(*loc, &scope);
00389         CBioseq_Handle hnd = scope.GetBioseqHandle(id);
00390         if (hnd)  return ConstObjectInfo(id);
00391     }
00392 
00393     return info;
00394 }
00395 
00396 static void init()
00397 {
00398     CObjectFactory::RegisterDefaultFactory(
00399             typeid(IObjTypeConverter).name(),
00400             new CFactory<CDefaultTypeConverter>());
00401 
00402     CObjectFactory::RegisterMultiFactory(
00403             CDbtag::GetTypeInfo(),
00404             typeid(IObjTypeConverter).name(),
00405             new CFactory<CDbtagTypeConverter>());
00406 
00407     CObjectFactory::RegisterMultiFactory(
00408             CDate_std::GetTypeInfo(),
00409             typeid(IObjTypeConverter).name(),
00410             new CFactory<CDateTypeConverter>());
00411 
00412     CObjectFactory::RegisterMultiFactory(
00413             CSeq_id::GetTypeInfo(),
00414             typeid(IObjTypeConverter).name(),
00415             new CFactory<CSeq_idTypeConverter>());
00416 
00417     CObjectFactory::RegisterMultiFactory(
00418             CGenetic_code::GetTypeInfo(),
00419             typeid(IObjTypeConverter).name(),
00420             new CFactory<CGenetic_codeTypeConverter>());
00421 
00422     CObjectFactory::RegisterMultiFactory(
00423             CSeq_feat::GetTypeInfo(),
00424             typeid(IExtProperty).name(),
00425             new CFactory<CSeq_feat_ExtProperty>());
00426 
00427     CObjectFactory::RegisterMultiFactory(
00428             CSeq_loc::GetTypeInfo(),
00429             typeid(IExtProperty).name(),
00430             new CFactory<CSeq_loc_ExtProperty>());
00431 }
00432 
00433 ADD_INIT_FUNC(init)
00434 
00435 END_NCBI_SCOPE
00436 
00437 

Generated on Wed Dec 9 04:27:12 2009 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Wed Dec 09 08:18:00 2009 by modify_doxy.py rev. 173732