1 /*  $Id: id_handler.cpp 452861 2014-11-25 16:39:11Z whlavina $
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:  Vyacheslav Chetvernin
27  * File Description:
28  *
29  */
30 
31 #include <ncbi_pch.hpp>
32 #include <corelib/ncbistr.hpp>
33 
34 #include <algo/gnomon/id_handler.hpp>
35 
36 #include <objects/general/Object_id.hpp>
37 #include <objects/general/Dbtag.hpp>
38 #include <objects/seqloc/Seq_id.hpp>
39 #include <objmgr/object_manager.hpp>
40 #include <objmgr/scope.hpp>
41 #include <objmgr/util/sequence.hpp>
42 #include <objmgr/seq_vector.hpp>
43 
44 BEGIN_NCBI_SCOPE
45 BEGIN_SCOPE(gnomon)
46 
47 USING_SCOPE(objects);
48 
CIdHandler(CScope & scope)49 CIdHandler::CIdHandler(CScope& scope)
50     : m_Scope(scope)
51 {
52 }
53 
ToCanonical(const CSeq_id & id) const54 CConstRef<CSeq_id> CIdHandler::ToCanonical(const CSeq_id& id) const
55 {
56     CConstRef<CSeq_id> canonical_id;
57     try {
58         CSeq_id_Handle idh = sequence::GetId(id, m_Scope, sequence::eGetId_Canonical | sequence::eGetId_ThrowOnError);
59         canonical_id = idh.GetSeqId();
60     } catch (sequence::CSeqIdFromHandleException& e) {
61         if (e.GetErrCode() != sequence::CSeqIdFromHandleException::eRequestedIdNotFound)
62             throw;
63         canonical_id.Reset(&id);
64     }
65     return canonical_id;
66 }
67 
ToString(const CSeq_id & id)68 string CIdHandler::ToString(const CSeq_id& id)
69 {
70     return id.AsFastaString();
71 }
72 
ToSeq_id(const string & str)73 CRef<CSeq_id> CIdHandler::ToSeq_id(const string& str)
74 {
75     return CRef<CSeq_id>(new CSeq_id(str));
76 }
77 
GnomonMRNA(Int8 id)78 CRef<CSeq_id> CIdHandler::GnomonMRNA(Int8 id)
79 {
80     CRef<CSeq_id> result(new CSeq_id);
81     CSeq_id::TGeneral& gnl = result->SetGeneral();
82     gnl.SetDb("GNOMON");
83     gnl.SetTag().SetStr(NStr::NumericToString(id) + ".m");
84     return result;
85 }
86 
GnomonProtein(Int8 id)87 CRef<CSeq_id> CIdHandler::GnomonProtein(Int8 id)
88 {
89     CRef<CSeq_id> result(new CSeq_id);
90     CSeq_id::TGeneral& gnl = result->SetGeneral();
91     gnl.SetDb("GNOMON");
92     gnl.SetTag().SetStr(NStr::NumericToString(id) + ".p");
93     return result;
94 }
95 
IsId(const CObject_id & obj)96 bool CIdHandler::IsId(const CObject_id& obj)
97 {
98     Int8 id;
99     switch (obj.GetIdType(id)) {
100     case CObject_id::e_not_set:
101         return false;
102     default:
103         return true;
104     }
105 }
106 
GetId(const CObject_id & obj)107 Int8 CIdHandler::GetId(const CObject_id& obj)
108 {
109     Int8 id;
110     switch (obj.GetIdType(id)) {
111     case CObject_id::e_not_set:
112         NCBI_THROW(CException, eUnknown, "No integral ID for object ID");
113     default:
114         ;
115     }
116     return id;
117 }
118 
SetId(CObject_id & obj,Int8 value)119 void CIdHandler::SetId(CObject_id& obj, Int8 value)
120 {
121     if (value >= numeric_limits<CObject_id::TId>::min()  &&
122         value <= numeric_limits<CObject_id::TId>::max()) {
123         obj.SetId(static_cast<CObject_id::TId>(value));
124     } else {
125         obj.SetStr(NStr::NumericToString(value));
126     }
127 }
128 
129 
GetDNASequence(CConstRef<objects::CSeq_id> id,CScope & scope)130 string GetDNASequence(CConstRef<objects::CSeq_id> id, CScope& scope)
131 {
132     CBioseq_Handle bh (scope.GetBioseqHandle(*id));
133     if (!bh) {
134         NCBI_THROW(CException, eUnknown, "Sequence '"+CIdHandler::ToString(*id)+"' retrieval failed");
135     }
136     CSeqVector sv (bh.GetSeqVector(CBioseq_Handle::eCoding_Iupac));
137     string seq;
138     sv.GetSeqData(0, sv.size(), seq);
139 
140     return seq;
141 }
142 
143 END_SCOPE(gnomon)
144 END_NCBI_SCOPE
145