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

Go to the SVN repository for this file.

1 /* $Id: cgi_hit_matrix.cpp 46176 2021-01-28 17:07:49Z 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  * Author: Irena Zaretskaya
27  *
28  * File Description:
29  */
30 
31 #include <ncbi_pch.hpp>
32 #include <syslog.h>
33 #include <corelib/ncbistl.hpp>
34 #include <cgi/cgictx.hpp>
35 #include <serial/serial.hpp>
36 #include <serial/objistr.hpp>
37 #include <serial/objostr.hpp>
38 
39 #include <connect/ncbi_socket.hpp>
41 
42 #include "blast_hitmatrix.hpp"
43 
44 
46 
47 
48 
50 {
51 public:
52  enum EErrCode {
56  };
57 
58  virtual const char* GetErrCodeString(void) const
59  {
60  switch(GetErrCode()) {
61  case eInvalidSeqAnnot: return "eInvalidSeqAnnot";
62  case eImageRenderError: return "eImageRenderError";
63  default: return CException::GetErrCodeString();
64  };
65  }
66 
68 };
69 
70 /////////////////////////////////////////////////////////////////////////////
71 /// CBlastHitMatrixCGIApplication
72 /// This CGI loads an accession from GenBank, extract a Seq-annot from the
73 /// sequence and renders a pairwise alignment between the first two Seq-id
74 /// in the alignment
75 
77 {
78 public:
80 
81 
82  virtual int ProcessRequest(CCgiContext &ctx);
83 
84 protected:
88 
89 private:
90 
92 
93 
94 
95  string m_File;
96  string m_RID;
97  string m_Thumbnail; //Indicated that thumbnail image should be used
98 
100  int m_Width;
101  int m_Height;
102 
103 };
104 
105 
106 
107 
109 {
110 }
111 
112 string s_GetRequestParam(const TCgiEntries &entries,const string &paramName)
113 {
114  string param = "";
115 
116  TCgiEntries::const_iterator iter = entries.find(paramName);
117 
118  if (iter != entries.end()){
119  param = iter->second;
120  }
121  return param;
122 }
123 
125 {
126  const CCgiRequest& request = ctx.GetRequest();
127  const TCgiEntries& entries = request.GetEntries();
128 
130 
131  string paramVal = s_GetRequestParam(entries,"width");
132 
133  m_Width = !paramVal.empty() ? NStr::StringToInt(paramVal) : 800;
134 
135  paramVal = s_GetRequestParam(entries,"height");
136 
137  m_Height = !paramVal.empty() ? NStr::StringToInt(paramVal) : 600;
138 
139  //Indicates that image should output in the file
140  m_File = s_GetRequestParam(entries,"file");
141 
142  m_Thumbnail = s_GetRequestParam(entries,"thumbnail");
143 }
144 
145 
147 {
148  const CNcbiRegistry & reg = ctx.GetConfig();
149  string blastURL = reg.Get("NetParams", "BlastURL");
150  string url = (string)blastURL + "?CMD=Get&RID=" + m_RID + "&FORMAT_TYPE=ASN.1&FORMAT_OBJECT=Alignment";
151 
152  SConnNetInfo* net_info = ConnNetInfo_Create(NULL);
153  // create HTTP connection
154  CConn_HttpStream inHttp(url,net_info);
155 
156  try {
157  m_Annot.Reset(new CSeq_annot);
158  unique_ptr<CObjectIStream> is
160  *is >> *m_Annot;
161  }
162  catch (CException& e) {
163  m_Annot.Reset();
164  NCBI_THROW(CBlastHitMatrixCGIException, eInvalidSeqAnnot, "Exception reading SeqAnnot via url " + url + ", exception message: " + e.GetMsg());
165  }
166 }
167 
168 //Use it for testing
170 {
171  CRef <CSeq_annot> m_Annot(new CSeq_annot);
172 
174  string path_base = CDirEntry(path_cgi).GetDir();
175  string ASN1FilePath = CDirEntry::MakePath(path_base, "testSeqAnnot.txt");
176 
177  LOG_POST(path_cgi);
178  LOG_POST(path_base);
179  LOG_POST(ASN1FilePath);
180 
181  try {
182  unique_ptr<CObjectIStream> in
183  (CObjectIStream::Open(eSerial_AsnText,ASN1FilePath));
184  *in >> *m_Annot;
185  }
186  catch (CException& e) {
187  cerr << "Exception reading SeqAnnot exception message: " + e.GetMsg() << endl;
188  }
189  cerr << MSerial_AsnText << *m_Annot;
190  return m_Annot;
191 }
192 
193 
195 {
196  if(!m_RID.empty()) {
197  x_GetSeqAnnot(ctx);
198  m_BlastHitMatrix = new CBlastHitMatrix((*m_Annot).GetData().GetAlign(),m_Height, m_Width,CImageIO::ePng);
199  if(!m_File.empty()) {
201  }
202  if(!m_Thumbnail.empty()) {
204  }
205  }
206 }
207 
209 {
210  // retrieve our CGI rendering params
212  x_InitAppData(ctx);
213 
214  bool success = true;
215  if(m_BlastHitMatrix->IsFileOut()) {
216  success = m_BlastHitMatrix->WriteToFile();
217  }
218  else {
219  string encoding("image/png");
220  CCgiResponse& response = ctx.GetResponse();
221  response.SetContentType(encoding);
222  response.WriteHeader();
223  success = m_BlastHitMatrix->Display(response.out());
224  }
225  if(!success) {
226  NCBI_THROW(CBlastHitMatrixCGIException, eImageRenderError, "Exception occured, exception message: " + m_BlastHitMatrix->GetErrorMessage());
227  }
228  return 0;
229 }
230 
231 /////////////////////////////////////////////////////////////////////////////
232 // MAIN
233 
234 int main(int argc, const char* argv[])
235 {
236  //Applog
237  SetSplitLogFile(true);
239 
240  openlog("hitmtrix", LOG_PID, LOG_LOCAL7);
241 
242  int result = CBlastHitMatrixCGIApplication().AppMain(argc, argv);
243  _TRACE("back to normal diags");
244  return result;
245 }
Declares class to display hitmatrix image view for blast 2 seq.
string s_GetRequestParam(const TCgiEntries &entries, const string &paramName)
int main(int argc, const char *argv[])
USING_NCBI_SCOPE
CRef< CSeq_annot > createAnnot(string rid)
CBlastHitMatrixCGIApplication This CGI loads an accession from GenBank, extract a Seq-annot from the ...
void x_GetCGIContextParams(CCgiContext &ctx)
virtual int ProcessRequest(CCgiContext &ctx)
This is the method you should override.
void x_GetSeqAnnot(CCgiContext &ctx)
CBlastHitMatrix * m_BlastHitMatrix
void x_InitAppData(CCgiContext &ctx)
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
NCBI_EXCEPTION_DEFAULT(CBlastHitMatrixCGIException, CException)
This class displays the image of the hitmatrix view for blast 2 seq results.
CCgiRequest::
Definition: ncbicgi.hpp:685
This stream exchanges data with an HTTP server located at the URL: http[s]://host[:port]/path[?...
CDirEntry –.
Definition: ncbifile.hpp:262
static CNcbiApplication * Instance(void)
Singleton method.
Definition: ncbiapp.cpp:244
CNcbiRegistry –.
Definition: ncbireg.hpp:913
CS_CONTEXT * ctx
Definition: t0006.c:12
const string & GetProgramExecutablePath(EFollowLinks follow_links=eIgnoreLinks) const
Get the application's executable path.
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:799
bool IsFileOut(void)
Checks if image is to be written to the file.
bool Display(CNcbiOstream &out)
Outputs the image into CNcbiOstream.
bool WriteToFile(void)
Outputs the image into the file (m_FileOut=true) or netcache.
void SetThumbnail(bool set)
Indicates that thumbmail should be shown.
void SetFileName(string fileName)
Inits file name if image is written to the file.
string GetErrorMessage(void)
Get error message.
string
Definition: cgiapp.hpp:687
const TCgiEntries & GetEntries(void) const
Get a set of entries(decoded) received from the client.
Definition: ncbicgi.hpp:1181
CNcbiOstream & out(void) const
Get output stream. Throw exception if GetOutput() is NULL.
Definition: ncbicgir.cpp:257
void SetContentType(const string &type)
Set content type (text/html by default if not provided)
Definition: ncbicgir.hpp:337
CNcbiOstream & WriteHeader(void) const
Write HTTP response header to the output stream.
Definition: ncbicgir.hpp:396
#define NULL
Definition: ncbistd.hpp:225
#define _TRACE(message)
Definition: ncbidbg.hpp:122
CDiagContext & GetDiagContext(void)
Get diag context instance.
Definition: logging.cpp:818
void SetSplitLogFile(bool value=true)
Split log files flag.
Definition: ncbidiag.cpp:6705
static void SetOldPostFormat(bool value)
Set old/new format flag.
Definition: ncbidiag.cpp:3352
#define LOG_POST(message)
This macro is deprecated and it's strongly recomended to move in all projects (except tests) to macro...
Definition: ncbidiag.hpp:226
TErrCode GetErrCode(void) const
Get error code.
Definition: ncbiexpt.cpp:453
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
const string & GetMsg(void) const
Get message string.
Definition: ncbiexpt.cpp:461
EErrCode
Error types that an application can generate.
Definition: ncbiexpt.hpp:884
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
Definition: ncbiexpt.cpp:444
string GetDir(EIfEmptyPath mode=eIfEmptyPath_Current) const
Get the directory component for this directory entry.
Definition: ncbifile.cpp:475
static string MakePath(const string &dir=kEmptyStr, const string &base=kEmptyStr, const string &ext=kEmptyStr)
Assemble a path from basic components.
Definition: ncbifile.cpp:413
#define MSerial_AsnText
I/O stream manipulators –.
Definition: serialbase.hpp:696
@ eSerial_AsnText
ASN.1 text.
Definition: serialdef.hpp:73
static CObjectIStream * Open(ESerialDataFormat format, CNcbiIstream &inStream, bool deleteInStream)
Create serial object reader and attach it to an input stream.
Definition: objistr.cpp:195
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
virtual const string & Get(const string &section, const string &name, TFlags flags=0) const
Get the parameter value.
Definition: ncbireg.cpp:262
static int StringToInt(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to int.
Definition: ncbistr.cpp:630
SConnNetInfo * ConnNetInfo_Create(const char *service)
The NCBI C++/STL use hints.
std::istream & in(std::istream &in_, double &x_)
else result
Definition: token2.c:20
static wxAcceleratorEntry entries[3]
Modified on Sat Dec 02 09:20:58 2023 by modify_doxy.py rev. 669887