1 /* $Id: gene_info_reader_app.cpp 631547 2021-05-19 13:51:35Z 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 * Author: Vahram Avagyan
27 *
28 */
29
30 /// @file gene_info_reader_app.cpp
31 /// Demo command-line application for reading Gene information from files.
32 ///
33 /// This source file contains a command-line application that uses the
34 /// Gene info reader library to convert Gene IDs to/from Gis and
35 /// to produce the Gene Info description lines given a Gi or a Gene ID.
36
37 #include <ncbi_pch.hpp>
38 #include <corelib/ncbiapp.hpp>
39
40 #include <objtools/blast/gene_info_reader/gene_info_reader.hpp>
41
42 #ifndef SKIP_DOXYGEN_PROCESSING
43 USING_NCBI_SCOPE;
44 #endif /* SKIP_DOXYGEN_PROCESSING */
45
46 //==========================================================================//
47
48 /// CReadFilesApp
49 ///
50 /// Class implementing the Gene Info reader application.
51 ///
52 /// CReadFilesApp is an NCBI command-line application that provides a
53 /// simple interface for converting Gene IDs to and from Gis and for
54 /// retrieving and formatting Gene Information for given Gene IDs and Gis.
55
56 class CReadFilesApp : public CNcbiApplication
57 {
58 private:
59 /// Initialize the Application.
60 virtual void Init(void);
61 /// Run the Application.
62 virtual int Run(void);
63 /// Exit the Application.
64 virtual void Exit(void);
65
66 /// Output a list of integers to stdout.
67 /// @param listVals
68 /// List of integer values to output.
69 void OutputIntList(const list<int>& listVals);
70 /// Output a list of TGi values to stdout.
71 /// @param listVals
72 /// List of TGi values to output.
73 void OutputGiList(const list<TGi>& listVals);
74 /// Output a list of Gene Information objects to stdout.
75 /// @param listInfos
76 /// List of Gene Information objects to output.
77 void OutputInfoList(IGeneInfoInput::TGeneInfoList& listInfos);
78 };
79
80 //==========================================================================//
81
82 void CReadFilesApp::
OutputIntList(const list<int> & listVals)83 OutputIntList(const list<int>& listVals)
84 {
85 list<int>::const_iterator it = listVals.begin();
86 for (; it != listVals.end(); it++)
87 {
88 cout << *it << " ";
89 }
90 cout << endl;
91 }
92
93 void CReadFilesApp::
OutputGiList(const list<TGi> & listVals)94 OutputGiList(const list<TGi>& listVals)
95 {
96 list<TGi>::const_iterator it = listVals.begin();
97 for (; it != listVals.end(); it++)
98 {
99 cout << *it << " ";
100 }
101 cout << endl;
102 }
103
104 void CReadFilesApp::
OutputInfoList(IGeneInfoInput::TGeneInfoList & listInfos)105 OutputInfoList(IGeneInfoInput::TGeneInfoList& listInfos)
106 {
107 IGeneInfoInput::TGeneInfoList::iterator it = listInfos.begin();
108 for (; it != listInfos.end(); it++)
109 {
110 string strInfo;
111 (*it)->ToString(strInfo, false);
112 cout << strInfo << endl;
113 }
114 }
115
Init(void)116 void CReadFilesApp::Init(void)
117 {
118 HideStdArgs(fHideLogfile | fHideConffile | fHideVersion);
119
120 // Create command-line argument descriptions class
121 auto_ptr<CArgDescriptions> arg_desc(new CArgDescriptions);
122
123 // Specify USAGE context
124 arg_desc->SetUsageContext(GetArguments().GetProgramBasename(),
125 "The program can be used to convert Gene IDs to/from Gis, "
126 "and print Gene Info lines given Gis or Gene IDs.");
127
128 // Gi to Gene ID
129 arg_desc->AddDefaultKey ("gi2id", "Gi",
130 "The Gi to convert to a Gene ID",
131 CArgDescriptions::eInteger,
132 "0");
133
134 // Gene ID to Gi
135 arg_desc->AddDefaultKey ("id2gi", "GeneID",
136 "The Gene ID to convert to a Gi",
137 CArgDescriptions::eInteger,
138 "0");
139
140 // Gi to Gene Info
141 arg_desc->AddDefaultKey ("gi2info", "Gi",
142 "The Gi to print the Gene Info line(s) for",
143 CArgDescriptions::eInteger,
144 "0");
145
146 // Gene ID to Gene Info
147 arg_desc->AddDefaultKey ("id2info", "GeneID",
148 "The Gene ID to print the Gene Info line for",
149 CArgDescriptions::eInteger,
150 "0");
151
152 // Setup arg.descriptions for this application
153 SetupArgDescriptions(arg_desc.release());
154 }
155
Run(void)156 int CReadFilesApp::Run(void)
157 {
158 int nRetval = 0;
159 try
160 {
161 // Create the reader object. This version of the constructor reads
162 // the path to the Gene Info files from the GENE_INFO_PATH
163 // environment variable.
164 CGeneInfoFileReader fileReader;
165
166 TGi gi2id = GI_FROM(TIntId, GetArgs()["gi2id"].AsIntId());
167 int id2gi = GetArgs()["id2gi"].AsInteger();
168 TGi gi2info = GI_FROM(TIntId, GetArgs()["gi2info"].AsIntId());
169 int id2info = GetArgs()["id2info"].AsInteger();
170
171 if (gi2id > ZERO_GI)
172 {
173 IGeneInfoInput::TGeneIdList idList;
174 if (fileReader.GetGeneIdsForGi(gi2id, idList))
175 {
176 cout << "Gene IDs for Gi=" << gi2id << ":" << endl;
177 OutputIntList(idList);
178 }
179 else
180 {
181 cout << "No Gene IDs found for Gi=" << gi2id << endl;
182 }
183 }
184
185 if (id2gi > 0)
186 {
187 IGeneInfoInput::TGiList giListRNA, giListProtein, giListGenomic;
188 bool bRNA, bProtein, bGenomic;
189 bRNA = fileReader.GetRNAGisForGeneId(id2gi, giListRNA);
190 bProtein = fileReader.GetProteinGisForGeneId(id2gi, giListProtein);
191 bGenomic = fileReader.GetGenomicGisForGeneId(id2gi, giListGenomic);
192
193 if (bRNA)
194 {
195 cout << "RNA Gis for Gene ID=" << id2gi << ":" << endl;
196 OutputGiList(giListRNA);
197 }
198 else
199 {
200 cout << "No RNA Gis for Gene ID=" << id2gi << endl;
201 }
202
203 if (bProtein)
204 {
205 cout << "Protein Gis for Gene ID=" << id2gi << ":" << endl;
206 OutputGiList(giListProtein);
207 }
208 else
209 {
210 cout << "No Protein Gis for Gene ID=" << id2gi << endl;
211 }
212
213 if (bGenomic)
214 {
215 cout << "Genomic Gis for Gene ID=" << id2gi << ":" << endl;
216 OutputGiList(giListGenomic);
217 }
218 else
219 {
220 cout << "No Genomic Gis found for Gene ID=" << id2gi << endl;
221 }
222 }
223
224 if (gi2info > ZERO_GI)
225 {
226 IGeneInfoInput::TGeneInfoList listInfos;
227 if (fileReader.GetGeneInfoForGi(gi2info, listInfos))
228 {
229 cout << "Gene Info for Gi=" << gi2info << ":" << endl;
230 OutputInfoList(listInfos);
231 }
232 else
233 {
234 cout << "No Gene Info found for Gi=" << gi2info << endl;
235 }
236 }
237
238 if (id2info > 0)
239 {
240 IGeneInfoInput::TGeneInfoList listInfos;
241 if (fileReader.GetGeneInfoForId(id2info, listInfos))
242 {
243 cout << "Gene Info for Gene ID=" << id2info << ":" << endl;
244 OutputInfoList(listInfos);
245 }
246 else
247 {
248 cout << "No Gene Info found for Gene ID=" << id2info << endl;
249 }
250 }
251 }
252 catch (CException& e)
253 {
254 cerr << endl << "Reading Gene Info failed: "
255 << e.what() << endl;
256 nRetval = 1;
257 }
258
259 return nRetval;
260 }
261
Exit(void)262 void CReadFilesApp::Exit(void)
263 {
264 SetDiagStream(0);
265 }
266
267 //==========================================================================//
268
main(int argc,const char * argv[])269 int main(int argc, const char* argv[])
270 {
271 // Execute main application function
272 return CReadFilesApp().AppMain(argc, argv);
273 }
274
275