NCBI C++ ToolKit
feature_panel_ds.cpp
Go to the documentation of this file.
00001 /*  $Id: feature_panel_ds.cpp 25159 2012-01-27 17:17:16Z wuliangs $
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:  Liangshou Wu
00027  *
00028  */
00029 
00030 #include <ncbi_pch.hpp>
00031 #include <gui/widgets/seq_graphic/feature_panel_ds.hpp>
00032 #include <gui/widgets/seq_graphic/track_config_manager.hpp>
00033 
00034 #include <gui/utils/app_job.hpp>
00035 #include <gui/utils/app_job_impl.hpp>
00036 #include <gui/objutils/utils.hpp>
00037 #include <objtools/data_loaders/genbank/gbloader.hpp>
00038 #include <corelib/ncbitime.hpp>
00039 
00040 BEGIN_NCBI_SCOPE
00041 USING_SCOPE(objects);
00042 
00043 ///////////////////////////////////////////////////////////////////////////////
00044 /// CTrackInitializationJob
00045 ///////////////////////////////////////////////////////////////////////////////
00046 class NCBI_GUIWIDGETS_SEQGRAPHIC_EXPORT CTrackInitializationJob :
00047         public CJobCancelable
00048 {
00049 public:
00050     typedef CAnnotMetaData::TAnnotMetaDataList TAnnotMetaDataList;
00051 
00052     CTrackInitializationJob(const string& desc, const ILayoutTrackFactory* factory,
00053         CTrackContainer* parent, CTempTrackProxy* params,
00054         const CObject& obj, objects::CScope& scope,
00055         ISGDataSourceContext* ds_context, CRenderingContext* r_cntx,
00056         const ILayoutTrackFactory::SExtraParams& extra_param,
00057         const TAnnotMetaDataList& src_annots);
00058 
00059     virtual ~CTrackInitializationJob(){};
00060 
00061     /// @name IAppJob implementation
00062     /// @{
00063     virtual EJobState                   Run();
00064     virtual CConstIRef<IAppJobProgress> GetProgress();
00065     virtual CRef<CObject>               GetResult();
00066     virtual CConstIRef<IAppJobError>    GetError();
00067     virtual string                      GetDescr() const;
00068     /// @}
00069 
00070 private:
00071     /// @name Job results and status.
00072     /// @{
00073     CRef<CAppJobError>          m_Error;
00074     CRef<CTrackInitJobResult>   m_Result;
00075     string                      m_Desc;
00076     /// @}
00077 
00078     CConstIRef<ILayoutTrackFactory>  m_Factory;
00079     SConstScopedObject          m_ScopedObj;         
00080     CIRef<ISGDataSourceContext> m_DSContext;
00081     CRenderingContext*          m_RenderingCntx;
00082     ILayoutTrackFactory::SExtraParams m_ExtraParam;
00083     TAnnotMetaDataList          m_SrcAnnots;
00084 };
00085 
00086 
00087 ///////////////////////////////////////////////////////////////////////////////
00088 /// CAnnotMetaDataJob
00089 ///////////////////////////////////////////////////////////////////////////////
00090 class NCBI_GUIWIDGETS_SEQGRAPHIC_EXPORT CAnnotMetaDataJob : public CJobCancelable
00091 {
00092 public:
00093     CAnnotMetaDataJob(const string& desc, CBioseq_Handle handle,
00094                       const string& ctx);
00095 
00096     virtual ~CAnnotMetaDataJob(){};
00097 
00098     /// @name IAppJob implementation
00099     /// @{
00100     virtual EJobState                   Run();
00101     virtual void                        RequestCancel();
00102     virtual CConstIRef<IAppJobProgress> GetProgress();
00103     virtual CRef<CObject>               GetResult();
00104     virtual CConstIRef<IAppJobError>    GetError();
00105     virtual string                      GetDescr() const;
00106     /// @}
00107 
00108 private:
00109     /// @name Job results and status.
00110     /// @{
00111     CRef<CAppJobError>              m_Error;
00112     CRef<CAnnotMetaDataJobResult>   m_Result;
00113     string                          m_Desc;
00114     /// @}
00115 
00116     objects::CBioseq_Handle m_Handle;           ///< target sequence
00117     string                  m_ViewerCtx;
00118 };
00119 
00120 
00121 ///////////////////////////////////////////////////////////////////////////////
00122 /// CSeqGraphicJob implementation
00123 ///////////////////////////////////////////////////////////////////////////////
00124 CTrackInitializationJob::CTrackInitializationJob(const string& desc,
00125     const ILayoutTrackFactory* factory, CTrackContainer* parent,
00126     CTempTrackProxy* params, const CObject& obj, objects::CScope& scope,
00127     ISGDataSourceContext* ds_context, CRenderingContext* r_cntx,
00128     const ILayoutTrackFactory::SExtraParams& extra_param,
00129     const TAnnotMetaDataList& src_annots)
00130     : m_Desc(desc)
00131     , m_Factory(factory)
00132     , m_ScopedObj(obj, scope)
00133     , m_DSContext(ds_context)
00134     , m_RenderingCntx(r_cntx)
00135     , m_ExtraParam(extra_param)
00136 {
00137     m_SrcAnnots = src_annots;
00138     m_Result.Reset(new CTrackInitJobResult);
00139     m_Result->m_ParentTrack = parent;
00140     m_Result->m_TrackParams.Reset(params);
00141     m_Result->m_Desc = desc;
00142 }
00143 
00144 IAppJob::EJobState CTrackInitializationJob::Run()
00145 {
00146     m_StopRequested = false;
00147     m_Error.Reset();
00148     try {
00149         m_Result->m_Tracks =
00150             m_Factory->CreateTracks(m_ScopedObj, m_DSContext,
00151             m_RenderingCntx, m_ExtraParam, m_SrcAnnots);
00152     } catch (std::exception& e) {
00153         m_Error.Reset(new CAppJobError("failed on initializing " +
00154             m_Factory->GetThisTypeInfo().GetDescr()));
00155         LOG_POST( Info << "CTrackInitializationJob::Run() failed job"
00156             << e.what());
00157         return eFailed;
00158     }
00159 
00160     //LOG_POST( Info << "CTrackInitializationJob::Run() completed job" );
00161 
00162     return eCompleted;
00163 }
00164 
00165 CConstIRef<IAppJobProgress> CTrackInitializationJob::GetProgress()
00166 {
00167     return CConstIRef<IAppJobProgress>(NULL);
00168 }
00169 
00170 CRef<CObject> CTrackInitializationJob::GetResult()
00171 {
00172     return CRef<CObject>(m_Result.GetPointer());
00173 }
00174 
00175 CConstIRef<IAppJobError> CTrackInitializationJob::GetError()
00176 {
00177     return CConstIRef<IAppJobError>(m_Error.GetPointer());
00178 }
00179 
00180 string CTrackInitializationJob::GetDescr() const
00181 {
00182     return m_Desc;
00183 }
00184 
00185 
00186 
00187 ///////////////////////////////////////////////////////////////////////////////
00188 /// CAnnotMetaDataJob implementation
00189 ///////////////////////////////////////////////////////////////////////////////
00190 CAnnotMetaDataJob::CAnnotMetaDataJob(const string& desc,
00191                                      CBioseq_Handle handle,
00192                                      const string& ctx)
00193     : m_Desc(desc)
00194     , m_Handle(handle)
00195     , m_ViewerCtx(ctx)
00196 {
00197     m_Result.Reset(new CAnnotMetaDataJobResult);
00198 }
00199 
00200 
00201 void CAnnotMetaDataJob::RequestCancel()
00202 {
00203     m_StopRequested = true;
00204 }
00205 
00206 
00207 IAppJob::EJobState CAnnotMetaDataJob::Run()
00208 {
00209     m_StopRequested = false;
00210     m_Error.Reset();
00211     try {
00212         CStopWatch sw;
00213         sw.Start();
00214 
00215         /*
00216         // Get complete list of NAAs using the currently fastest way.
00217         // We may be able to avoid this step if there is a guarrantee 
00218         // that each NAA comes with meta-data
00219         set<string> na_all;
00220         CGBLoaderParams param;
00221         string loader_name =
00222             CGBDataLoader::GetLoaderNameFromArgs(param);
00223         CDataLoader* d_loader =
00224             CObjectManager::GetInstance()->FindDataLoader(loader_name);
00225         CGBDataLoader* loader = dynamic_cast<CGBDataLoader*>(d_loader);
00226         _ASSERT(loader);
00227         if (loader) {
00228             na_all = loader->GetNamedAnnotAccessions(m_Handle.GetSeq_id_Handle());
00229         }
00230         */
00231 
00232         // Get all NAAs having meta-data
00233         CSeqUtils::TNAA_MetaDataMap meta_data;
00234         CConstRef<CSeq_id> seq_id = m_Handle.GetSeqId(); 
00235         CSeqUtils::GetNAAMetaData(*seq_id, meta_data,
00236                                   m_Handle.GetScope(), m_ViewerCtx);
00237 
00238         // extract the required info
00239         bool stop_requested = false;
00240         CTrackConfigManager::ParseAnnotInfo(meta_data, m_Result->m_Annots, stop_requested);
00241 
00242         LOG_POST(Info << "Time for retrieving NA meta-data: "
00243             << sw.Elapsed() << " seconds");
00244 
00245     } catch (std::exception& e) {
00246         string id_str;
00247         m_Handle.GetSeqId()->GetLabel(&id_str);
00248         LOG_POST( Info << "CAnnotMetaDataJob::Run() failed to get meta data for "
00249             <<  id_str << ", Details: "
00250             << e.what());
00251 
00252         // maybe there is no such thing as named annotation for
00253         // the given sequence type, such as a local sequence.
00254         // So we won't consider it as failed
00255     }
00256 
00257     return eCompleted;
00258 }
00259 
00260 
00261 CConstIRef<IAppJobProgress> CAnnotMetaDataJob::GetProgress()
00262 {
00263     return CConstIRef<IAppJobProgress>(NULL);
00264 }
00265 
00266 
00267 CRef<CObject> CAnnotMetaDataJob::GetResult()
00268 {
00269     return CRef<CObject>(m_Result.GetPointer());
00270 }
00271 
00272 
00273 CConstIRef<IAppJobError> CAnnotMetaDataJob::GetError()
00274 {
00275     return CConstIRef<IAppJobError>(m_Error.GetPointer());
00276 }
00277 
00278 
00279 string CAnnotMetaDataJob::GetDescr() const
00280 {
00281     return m_Desc;
00282 }
00283 
00284 
00285 ///////////////////////////////////////////////////////////////////////////////
00286 /// CFeaturePanelDS
00287 ///////////////////////////////////////////////////////////////////////////////
00288 
00289 CFeaturePanelDS::CFeaturePanelDS(CScope& scope, const CSeq_id& id)
00290     : CSGGenBankDS(scope, id)
00291     , m_RenderingCntx(NULL)
00292     , m_MetaDataJob(-1)
00293 {}
00294 
00295 
00296 CFeaturePanelDS::~CFeaturePanelDS()
00297 {
00298     DeleteMetaDataJob();
00299 }
00300 
00301 
00302 void CFeaturePanelDS::DeleteMetaDataJob()
00303 {
00304     if (m_MetaDataJob > -1) {
00305         CAppJobDispatcher& disp = CAppJobDispatcher::GetInstance();
00306         try {
00307             disp.DeleteJob(m_MetaDataJob);
00308         } catch(CAppJobException& e)  {
00309             switch(e.GetErrCode())  {
00310                 case CAppJobException::eUnknownJob:
00311                 case CAppJobException::eEngine_UnknownJob:
00312                     /// this is fine - job probably already finished
00313                     break;
00314                 default:
00315                     // something wrong
00316                     LOG_POST(Error << "CSGGenBankDS::DeleteAllJobs() "
00317                         << "Error canceling job");
00318                     LOG_POST(e.ReportAll());
00319             }
00320         }
00321         m_MetaDataJob = -1;
00322     }
00323 }
00324 
00325 
00326 void CFeaturePanelDS::ClearMetaDataJobId()
00327 {
00328     m_MetaDataJob = -1;
00329 }
00330 
00331 
00332 void CFeaturePanelDS::CreateTrack(const string& desc,
00333                                   const ILayoutTrackFactory* factory,
00334                                   CTrackContainer* parent,
00335                                   CTempTrackProxy* params,
00336                                   const ILayoutTrackFactory::SExtraParams& extra_param,
00337                                   const TAnnotMetaDataList& src_annots)
00338 {
00339     CConstRef<CSeq_id> seq_id = m_Handle.GetSeqId();
00340 
00341     CRef<CTrackInitializationJob> job(
00342         new CTrackInitializationJob(desc, factory, parent, params,
00343         *seq_id, GetScope(), m_DSContext, m_RenderingCntx,
00344         extra_param, src_annots));
00345     x_LaunchJob(*job, -1);
00346 }
00347 
00348 
00349 void CFeaturePanelDS::GetAnnotMetaData(const string& desc, const string& context)
00350 {
00351     CRef<CAnnotMetaDataJob> job(new CAnnotMetaDataJob(desc, m_Handle, context));
00352     if (m_Background) {
00353         TJobID job_id = x_BackgroundJob(*job, -1, "ObjManagerEngine");
00354         if (job_id > -1) {
00355             m_MetaDataJob = job_id;
00356         }
00357     } else {
00358         x_ForegroundJob(*job);
00359     }
00360 }
00361 
00362 
00363 ///////////////////////////////////////////////////////////////////////////////
00364 /// CSGFeatureDSType
00365 ///////////////////////////////////////////////////////////////////////////////
00366 
00367 ISGDataSource*
00368 CFeaturePanelDSType::CreateDS(SConstScopedObject& object) const
00369 {
00370     const CSeq_id& id = dynamic_cast<const CSeq_id&>(object.object.GetObject());
00371     return new CFeaturePanelDS(object.scope.GetObject(), id);
00372 }
00373 
00374 string CFeaturePanelDSType::GetExtensionIdentifier() const
00375 {
00376     static string sid("seqgraphic_feature_panel_ds_type");
00377     return sid;
00378 }
00379 
00380 string CFeaturePanelDSType::GetExtensionLabel() const
00381 {
00382     static string slabel("Graphical View Feature Panel Data Source Type");
00383     return slabel;
00384 }
00385 
00386 bool CFeaturePanelDSType::IsSharable() const
00387 {
00388     return false;
00389 }
00390 
00391 
00392 END_NCBI_SCOPE
Modified on Wed May 23 12:55:17 2012 by modify_doxy.py rev. 337098