NCBI C++ ToolKit
nowin_main.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: nowin_main.cpp 92495 2021-01-26 18:35:12Z grichenk $
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: Paul Thiessen
27 *
28 * File Description:
29 * main and code for non-windowed (scripted) Cn3D
30 *
31 * ===========================================================================
32 */
33 
34 #include <ncbi_pch.hpp>
35 #include <corelib/ncbistd.hpp>
36 #include <corelib/ncbitime.hpp> // avoids some 'CurrentTime' conflict later on...
37 #include <corelib/ncbiapp.hpp>
38 #include <serial/objostr.hpp>
39 
43 #include <objects/seq/Bioseq.hpp>
47 
48 #include <algorithm>
49 #include <vector>
50 
52 
53 #ifdef __WXMSW__
54 #include <windows.h>
55 #include <wx/msw/winundef.h>
56 #endif
57 #include <wx/wx.h>
58 #include <wx/filesys.h>
59 #include <wx/fs_zip.h>
60 
61 #include "asn_reader.hpp"
62 #include "cn3d_tools.hpp"
63 #include "structure_set.hpp"
64 #include "opengl_renderer.hpp"
65 #include "cn3d_cache.hpp"
66 #include "data_manager.hpp"
67 #include "structure_window.hpp"
68 #include "cn3d_png.hpp"
69 #include "chemical_graph.hpp"
70 
73 
74 
75 BEGIN_SCOPE(Cn3D)
76 
77 #define ERRORTHROW(stream) do { \
78  CNcbiOstrstream oss; \
79  oss << stream; \
80  NCBI_THROW(CException, eUnknown, ((string) CNcbiOstrstreamToString(oss))); \
81 } while (0)
82 
83 void DisplayDiagnostic(const SDiagMessage& diagMsg)
84 {
85  string errMsg;
86  diagMsg.Write(errMsg);
87 
88  // severe errors go to cout, too
89  if (diagMsg.m_Severity >= eDiag_Error && diagMsg.m_Severity != eDiag_Trace)
90  NcbiCout << errMsg;
91 
92  // info messages and less severe errors go to cout
93  else
94  NcbiCout << errMsg;
95 }
96 
97 // other placeholders
98 bool IsWindowedMode(void) { return false; }
99 wxFrame * GlobalTopWindow(void) { ERRORMSG("Shouldn't call GlobalTopWindow() in no-window mode"); return NULL; }
100 void SetDialogSevereErrors(bool) { }
101 void RaiseLogWindow(void) { ERRORMSG("Shouldn't call RaiseLogWindow() in no-window mode"); }
102 
103 // no-window application object
105 {
106 private:
107 
108 public:
109  void Init(void);
110  int Run(void);
111  void Exit(void);
112 };
113 
114 void Cn3DNoWin::Init(void)
115 {
116  // turn off some Mesa-related env vars, irrelevant to off-screen rendering and not platform-indepdendent
117  setenv("MESA_NO_3DNOW", "true", 1);
118  setenv("MESA_NO_SSE", "true", 1);
119 
120  // setup the diagnostic stream
122  SetDiagPostLevel(eDiag_Info); // report all messages
123  SetDiagTrace(eDT_Default); // trace messages only when DIAG_TRACE env. var. is set
125 #ifdef _DEBUG
128 #else
131 #endif
132 
133  // C++ object verification
137 
138  // set up argument processing
139  CArgDescriptions *argDescr = new CArgDescriptions(false);
140  argDescr->SetUsageContext(GetArguments().GetProgramName(), "No-Window Cn3D");
141 
142  // get data from file or network
143  argDescr->AddOptionalKey("f", "file", "Ncbi-mime-asn1, Biostruc, or Cdd ASN.1 data file", argDescr->eString);
144  argDescr->AddOptionalKey("d", "id", "MMDB or PDB ID", argDescr->eString);
145 
146  // model, for network or Biostruc load
147  argDescr->AddOptionalKey("o", "model", "Model type to use for coordinates", argDescr->eString);
148  argDescr->SetConstraint("o", (new CArgAllow_Strings)->Allow("alpha")->Allow("single")->Allow("PDB"));
149 
150  // controls for output
151  argDescr->AddKey("p", "pngfile", "Output PNG file name", argDescr->eString);
152  argDescr->AddKey("w", "width", "Output width in pixels", argDescr->eInteger);
153  argDescr->AddKey("h", "height", "Output height in pixels", argDescr->eInteger);
154  argDescr->AddFlag("i", "Create interlaced PNG");
155 
156  // use favorite style
157  argDescr->AddOptionalKey("s", "style", "Favorite style", argDescr->eString);
158 
159  SetupArgDescriptions(argDescr);
160 }
161 
162 void Cn3DNoWin::Exit(void)
163 {
165  SetDiagStream(&cout);
166 
167  // remove dictionary
169 }
170 
171 int Cn3DNoWin::Run(void)
172 {
173  TRACEMSG("Hello! Running build from " << __DATE__);
174  int status = 1;
175 
176  try {
177 
178  // validate arguments
179  const CArgs& args = GetArgs();
180  if (!(args["f"].HasValue() || args["d"].HasValue()) || (args["f"].HasValue() && args["d"].HasValue()))
181  ERRORTHROW("Command line: Must supply one (and only one) of -f or -d");
183  if (args["o"].HasValue()) {
184  if (args["o"].AsString() == "alpha")
186  else if (args["o"].AsString() == "PDB")
187  model = eModel_type_pdb_model;
188  }
189  string favorite(args["s"].HasValue() ? args["s"].AsString() : kEmptyStr);
190 
191  // setup dirs
193 
194  // read dictionary
195  wxString dictFile = wxString(GetDataDir().c_str()) + "bstdt.val";
196  LoadStandardDictionary(dictFile.c_str());
197 
198  // set up registry
199  LoadRegistry();
200 
201  // local structure set and renderer
202  StructureSet *sset = NULL;
203  OpenGLRenderer renderer(NULL);
204 
205  // load data from file
206  if (args["f"].HasValue()) {
207  // if -o is present, assume this is a Biostruc file
208  if (args["o"].HasValue()) {
209  CNcbi_mime_asn1 *mime = CreateMimeFromBiostruc(args["f"].AsString(), model);
210  if (!mime || !LoadDataOnly(&sset, &renderer, NULL, mime, favorite))
211  ERRORTHROW("Failed to load Biostruc file " << args["f"].AsString());
212  } else {
213  if (!LoadDataOnly(&sset, &renderer, args["f"].AsString().c_str(), NULL, favorite, model))
214  ERRORTHROW("Failed to load file " << args["f"].AsString());
215  }
216  }
217 
218  // else network fetch
219  else {
220  CNcbi_mime_asn1 *mime = LoadStructureViaCache(args["d"].AsString(), model, 0);
221  if (!mime || !LoadDataOnly(&sset, &renderer, NULL, mime, favorite))
222  ERRORTHROW("Failed to load from network with id " << args["d"].AsString());
223  }
224 
225  if (!sset)
226  ERRORTHROW("Somehow ended up with NULL sset");
227  unique_ptr < StructureSet > sset_ap(sset); // so we can be sure it's deleted
228 
229  // export PNG image
230  if (!ExportPNG(NULL, &renderer, args["p"].AsString(), args["w"].AsInteger(), args["h"].AsInteger(), args["i"].HasValue()))
231  ERRORTHROW("PNG export failed");
232 
233  TRACEMSG("Done!");
234  status = 0;
235 
236  } catch (ncbi::CException& ce) {
237  ERRORMSG("Caught CException: " << ce.GetMsg());
238  } catch (std::exception& e) {
239  ERRORMSG("Caught exception: " << e.what());
240  } catch (...) {
241  ERRORMSG("Caught unknown exception");
242  }
243 
244  return status;
245 }
246 
247 END_SCOPE(Cn3D)
248 
250 
251 int main(int argc, char *argv[])
252 {
253  return Cn3DNoWin().AppMain(argc, argv, 0, eDS_Default, NULL);
254 }
User-defined methods of the data storage class.
User-defined methods of the data storage class.
User-defined methods of the data storage class.
void LoadStandardDictionary(const char *filename)
void DeleteStandardDictionary(void)
CArgAllow_Strings –.
Definition: ncbiargs.hpp:1641
CArgDescriptions –.
Definition: ncbiargs.hpp:541
CArgs –.
Definition: ncbiargs.hpp:379
void Exit(void)
Cleanup on application exit.
Definition: nowin_main.cpp:162
void Init(void)
Initialize the application.
Definition: nowin_main.cpp:114
int Run(void)
Run the application.
Definition: nowin_main.cpp:171
CNcbi_mime_asn1 * LoadStructureViaCache(const std::string &uid, ncbi::objects::EModel_type modelType, int assemblyId)
Definition: cn3d_cache.cpp:255
bool ExportPNG(Cn3DGLCanvas *glCanvas, OpenGLRenderer *renderer, const string &outputFilename, int outputWidth, int outputHeight, bool interlaced)
Definition: cn3d_png.cpp:392
void SetUpWorkingDirectories(const char *argv0)
Definition: cn3d_tools.cpp:333
void LoadRegistry(void)
Definition: cn3d_tools.cpp:171
const string & GetDataDir(void)
Definition: cn3d_tools.cpp:330
#define TRACEMSG(stream)
Definition: cn3d_tools.hpp:83
#define ERRORMSG(stream)
Definition: cn3d_tools.hpp:86
Include a standard set of the NCBI C++ Toolkit most basic headers.
CNcbi_mime_asn1 * CreateMimeFromBiostruc(const string &filename, EModel_type model)
#define setenv(n, v, o)
Definition: replacements.h:185
virtual const CArgs & GetArgs(void) const
Get parsed command line arguments.
Definition: ncbiapp.cpp:305
int AppMain(int argc, const char *const *argv, const char *const *envp=0, EAppDiagStream diag=eDS_Default, const char *conf=NcbiEmptyCStr, const string &name=NcbiEmptyString)
Main function (entry point) for the NCBI application.
Definition: ncbiapp.cpp:832
virtual void SetupArgDescriptions(CArgDescriptions *arg_desc)
Setup the command line argument descriptions.
Definition: ncbiapp.cpp:1208
const CNcbiArguments & GetArguments(void) const
Get the application's cached unprocessed command-line arguments.
void AddFlag(const string &name, const string &comment, CBoolEnum< EFlagValue > set_value=eFlagHasValueIfSet, TFlags flags=0)
Add description for flag argument.
Definition: ncbiargs.cpp:2459
void SetConstraint(const string &name, const CArgAllow *constraint, EConstraintNegate negate=eConstraint)
Set additional user defined constraint on argument value.
Definition: ncbiargs.cpp:2591
void AddKey(const string &name, const string &synopsis, const string &comment, EType type, TFlags flags=0)
Add description for mandatory key.
Definition: ncbiargs.cpp:2412
void SetUsageContext(const string &usage_name, const string &usage_description, bool usage_sort_args=false, SIZE_TYPE usage_width=78)
Set extra info to be used by PrintUsage().
Definition: ncbiargs.cpp:3304
void AddOptionalKey(const string &name, const string &synopsis, const string &comment, EType type, TFlags flags=0)
Add description for optional key without default value.
Definition: ncbiargs.cpp:2427
@ eString
An arbitrary string.
Definition: ncbiargs.hpp:589
@ eInteger
Convertible into an integer number (int or Int8)
Definition: ncbiargs.hpp:592
#define NULL
Definition: ncbistd.hpp:225
void Write(string &str, TDiagWriteFlags flags=fNone) const
Binary OR of "EDiagWriteFlags".
Definition: ncbidiag.cpp:5358
void SetDiagPostFlag(EDiagPostFlag flag)
Set the specified flag (globally).
Definition: ncbidiag.cpp:6073
EDiagSev m_Severity
Severity level.
Definition: ncbidiag.hpp:1651
void UnsetDiagTraceFlag(EDiagPostFlag flag)
Definition: ncbidiag.cpp:6094
EDiagSev SetDiagPostLevel(EDiagSev post_sev=eDiag_Error)
Set the threshold severity for posting the messages.
Definition: ncbidiag.cpp:6132
void SetDiagHandler(CDiagHandler *handler, bool can_delete=true)
Set the diagnostic handler using the specified diagnostic handler class.
Definition: ncbidiag.cpp:6291
void SetDiagTrace(EDiagTrace how, EDiagTrace dflt=eDT_Default)
Set the diagnostic trace settings.
Definition: ncbidiag.cpp:6229
void SetDiagStream(CNcbiOstream *os, bool quick_flush=true, FDiagCleanup cleanup=0, void *cleanup_data=0, const string &stream_name="")
Set diagnostic stream.
Definition: ncbidiag.cpp:8086
@ eDPF_Location
Include class and function if any.
Definition: ncbidiag.hpp:704
@ eDPF_Line
Source line.
Definition: ncbidiag.hpp:695
@ eDPF_File
File name (not full path)
Definition: ncbidiag.hpp:693
@ eDS_Default
Try standard log file (app.name + ".log") in /log/, use stderr on failure.
Definition: ncbidiag.hpp:1790
@ eDT_Default
Restores the default tracing context.
Definition: ncbidiag.hpp:1548
@ eDiag_Trace
Trace message.
Definition: ncbidiag.hpp:657
@ eDiag_Info
Informational message.
Definition: ncbidiag.hpp:651
@ eDiag_Error
Error message.
Definition: ncbidiag.hpp:653
static void SetVerifyDataGlobal(ESerialVerifyData verify)
@ eSerialVerifyData_Always
always verify (even if set not to later on)
Definition: serialdef.hpp:112
static void SetVerifyDataGlobal(ESerialVerifyData verify)
Set up default output data verification for streams created by the current process.
Definition: objostr.cpp:214
static void SetVerifyDataGlobal(ESerialVerifyData verify)
Set up default input data verification for streams created by the current process.
Definition: objistr.cpp:243
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
#define NcbiCout
Definition: ncbistre.hpp:543
#define kEmptyStr
Definition: ncbistr.hpp:123
EModel_type
Access to EModel_type's attributes (values, names) as defined in spec.
Definition: Model_type_.hpp:63
@ eModel_type_ncbi_all_atom
Definition: Model_type_.hpp:66
@ eModel_type_ncbi_backbone
Definition: Model_type_.hpp:65
@ eModel_type_pdb_model
Definition: Model_type_.hpp:67
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
#define GetProgramName
Avoid name clash with the NCBI C Toolkit.
Definition: ncbienv.hpp:49
Defines: CTimeFormat - storage class for time format.
#define ERRORTHROW(stream)
Definition: nowin_main.cpp:77
USING_SCOPE(objects)
int main(int argc, char *argv[])
Definition: nowin_main.cpp:251
void DisplayDiagnostic(const SDiagMessage &diagMsg)
Definition: nowin_main.cpp:83
void RaiseLogWindow(void)
Definition: nowin_main.cpp:101
void SetDialogSevereErrors(bool)
Definition: nowin_main.cpp:100
wxFrame * GlobalTopWindow(void)
Definition: nowin_main.cpp:99
USING_NCBI_SCOPE
Definition: nowin_main.cpp:71
bool IsWindowedMode(void)
Definition: nowin_main.cpp:98
SDiagMessage –.
Definition: ncbidiag.hpp:1599
bool LoadDataOnly(StructureSet **sset, OpenGLRenderer *renderer, const char *filename, CNcbi_mime_asn1 *mimeData, const string &favoriteStyle, EModel_type model, StructureWindow *window)
Modified on Wed Jul 24 17:16:02 2024 by modify_doxy.py rev. 669887