1 /* $Id: genbank_id_resolve.cpp 632624 2021-06-03 17:38:23Z ivanov $
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: Frank Ludwig
27 *
28 * File Description: Write gff file
29 *
30 */
31
32 #include <ncbi_pch.hpp>
33
34 #include <objmgr/util/sequence.hpp>
35
36 #include <objtools/writers/writer_exception.hpp>
37 #include <objtools/writers/genbank_id_resolve.hpp>
38
39 BEGIN_NCBI_SCOPE
40 USING_SCOPE(objects);
41
42 // ----------------------------------------------------------------------------
CGenbankIdResolve()43 CGenbankIdResolve::CGenbankIdResolve():
44 mThrowOnUnresolvedGi(false),
45 mLabelType(CSeq_id::eContent)
46 // ----------------------------------------------------------------------------
47 {}
48
49 // ----------------------------------------------------------------------------
~CGenbankIdResolve()50 CGenbankIdResolve::~CGenbankIdResolve()
51 // ----------------------------------------------------------------------------
52 {}
53
54 // ----------------------------------------------------------------------------
55 CGenbankIdResolve&
Get()56 CGenbankIdResolve::Get()
57 // ----------------------------------------------------------------------------
58 {
59 static CGenbankIdResolve resolver;
60 return resolver;
61 }
62
63 // ----------------------------------------------------------------------------
GetBestId(CSeq_id_Handle idh,CScope & scope,string & best_id)64 bool CGenbankIdResolve::GetBestId(
65 CSeq_id_Handle idh,
66 CScope& scope,
67 string& best_id)
68 // ----------------------------------------------------------------------------
69 {
70 if (!idh) {
71 return false;
72 }
73 CSeq_id_Handle best_idh = sequence::GetId(idh, scope, sequence::eGetId_Best);
74 if (!best_idh) {
75 best_idh = idh;
76 }
77 if (best_idh.IsGi() && mThrowOnUnresolvedGi) {
78 // we are not supposed to let any GI numbers see the light of day:
79 string msg("Unable to resolve GI number ");
80 msg += NStr::NumericToString(best_idh.GetGi());
81 NCBI_THROW(CObjWriterException, eBadInput, msg);
82 }
83 string backup = best_id;
84 try {
85 best_idh.GetSeqId()->GetLabel(&best_id, mLabelType);
86 }
87 catch (...) {
88 best_id = backup;
89 return false;
90 }
91 return true;
92 }
93
94 // ----------------------------------------------------------------------------
GetBestId(const CMappedFeat & mf,string & best_id)95 bool CGenbankIdResolve::GetBestId(
96 const CMappedFeat& mf,
97 string& best_id)
98 // ----------------------------------------------------------------------------
99 {
100 CSeq_id_Handle idh = mf.GetLocationId();
101 if (idh) {
102 return GetBestId(idh, mf.GetScope(), best_id);
103 }
104 const CSeq_loc& loc = mf.GetLocation();
105 idh = sequence::GetIdHandle(loc, &mf.GetScope());
106 return GetBestId(idh, mf.GetScope(), best_id);
107 }
108
109 // -----------------------------------------------------------------------------
GetBestId(const CSeq_loc & loc,string & best_id)110 bool CGenbankIdResolve::GetBestId(
111 const CSeq_loc& loc,
112 string& best_id)
113 // -----------------------------------------------------------------------------
114 {
115 const CSeq_id* pId = loc.GetId();
116 if (!pId) {
117 NCBI_THROW(CObjWriterException, eBadInput,
118 "CGenbankIdResolve: Location without good ID");
119 }
120 return GetBestId(
121 CSeq_id_Handle::GetHandle(*pId),
122 xGetDefaultScope(),
123 best_id);
124 }
125
126 // -----------------------------------------------------------------------------
xGetDefaultScope()127 CScope& CGenbankIdResolve::xGetDefaultScope()
128 // -----------------------------------------------------------------------------
129 {
130 if (!mpDefaultScope) {
131 mpDefaultScope.Reset(new CScope(*CObjectManager::GetInstance()));
132 mpDefaultScope->AddDefaults();
133 }
134 return *mpDefaultScope;
135 }
136
137 END_NCBI_SCOPE
138