CycloBranch
cPubChemSearchWidget.h
Go to the documentation of this file.
1
7#ifndef _CPUBCHEMSEARCHWIDGET_H
8#define _CPUBCHEMSEARCHWIDGET_H
9
10#include <QWidget>
11#include <QDesktopServices>
12#include <QUrl>
13#include <QFileInfo>
14#include <QNetworkAccessManager>
15#include <QNetworkRequest>
16#include <QNetworkReply>
17#include <QtConcurrent>
18#include <QApplication>
19#include <QThreadPool>
20
21#include "core/utilities.h"
22
23
24// forward declaration
25class QTextBrowser;
26class QVBoxLayout;
27
28
32class cPubChemSearchWidget : public QWidget
33{
34 Q_OBJECT
35
36public:
37
38
43
44
49
50
55 void setHTML(string html);
56
57
62 string getHTML();
63
64
69 void closeEvent(QCloseEvent *event);
70
71
76 void store(ofstream& os);
77
78
83 void load(ifstream& is);
84
85
86private:
87
88 QVBoxLayout* layout;
89 QTextBrowser* textbrowser;
90 string htmlstring;
91
92
93protected:
94
95
100 void keyPressEvent(QKeyEvent *event);
101
102
103};
104
105
110
114 typedef string result_type;
115
116
122 string getCIDList(const string& compound) {
123 string url = "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/fastformula/";
124 url += compound;
125 url += "/cids/txt";
126
127 QNetworkAccessManager networkmanager;
128 QNetworkRequest request(QUrl(url.c_str()));
129 QNetworkReply *response = networkmanager.get(request);
130
131 QEventLoop loop;
132 QObject::connect(response, SIGNAL(finished()), &loop, SLOT(quit()));
133 loop.exec();
134
135 string cids = response->readAll().toStdString();
136
137 stringstream errorstream;
138 if (cids.empty()) {
139 errorstream << "@ERROR" << endl;
140 errorstream << "URL: " << url << endl;
141 errorstream << "ERROR: " << response->errorString().toStdString() << endl;
142 errorstream << endl;
143 }
144
145 response->deleteLater();
146
147 if (cids.empty()) {
148 return errorstream.str();
149 }
150
151 return cids;
152 }
153
154
160 void getCommaSeparatedCIDs(string& cids, string& reducedcids) {
161 reducedcids.clear();
162 if (cids.find("Status:") == string::npos) {
163 istringstream inputstream(cids);
164 string line;
165 while (getline(inputstream, line)) {
166 if (!reducedcids.empty()) {
167 reducedcids += ",";
168 }
169 reducedcids += line;
170 }
171 }
172 }
173
174
180 void attachVectorOfCIDs(string& cids, vector<string>& cidsvector) {
181 if (cids.find("Status:") == string::npos) {
182 istringstream inputstream(cids);
183 string line;
184 while (getline(inputstream, line)) {
185 cidsvector.push_back(line);
186 }
187 }
188 }
189
190
196 int getNumberOfCIDs(string& cids) {
197 int count = 0;
198 if (cids.find("Status:") == string::npos) {
199 istringstream inputstream(cids);
200 string line;
201 while (getline(inputstream, line)) {
202 count++;
203 }
204 }
205 else {
206 if (cids.find("Status: 404") == string::npos) {
207 // report error codes 500 (server error), 503 (server busy), etc.
208 cout << cids << endl << endl;
209 }
210 }
211
212 return count;
213 }
214
215
221 string operator()(const string& compound) {
222 return getCIDList(compound);
223 }
224};
225
226
231
235 typedef string result_type;
236
237
243 string getNameList(const string& cid) {
244 bool usepost = true;
245
246 string url = "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/";
247 if (!usepost) {
248 url += cid + "/";
249 }
250 url += "synonyms/txt";
251
252 if (!usepost) {
253 // URL too long
254 if (url.size() > 4000) {
255 return "Status: 414";
256 }
257 }
258
259 QNetworkAccessManager networkmanager;
260 QNetworkRequest request(QUrl(url.c_str()));
261 QNetworkReply *response;
262
263 if (usepost) {
264 QString body = "cid=";
265 body += cid.c_str();
266 request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
267 response = networkmanager.post(request, body.toUtf8());
268 }
269 else {
270 response = networkmanager.get(request);
271 }
272
273 QEventLoop loop;
274 QObject::connect(response, SIGNAL(finished()), &loop, SLOT(quit()));
275 loop.exec();
276
277 string names = response->readAll().toStdString();
278
279 stringstream errorstream;
280 if (names.empty()) {
281 errorstream << "@ERROR" << endl;
282 errorstream << "URL: " << url << endl;
283 errorstream << "ERROR: " << response->errorString().toStdString() << endl;
284 errorstream << endl;
285 }
286
287 response->deleteLater();
288
289 if (names.empty()) {
290 return errorstream.str();
291 }
292
293 return names;
294 }
295
296
302 void getReducedNames(string& names, set<string>& reducednames) {
303 if (names.find("Status:") == string::npos) {
304 istringstream inputstream(names);
305 string line;
306 while (getline(inputstream, line)) {
307 if (line.find("SCHEMBL") == string::npos) {
308 reducednames.insert(line);
309 }
310 }
311 if (reducednames.empty()) {
312 reducednames.insert("This item has only SCHEMBL name(s).");
313 }
314 }
315 else {
316 if (names.find("Status: 404") == string::npos) {
317 // report error codes 500 (server error), 503 (server busy), etc.
318 cout << names << endl << endl;
319 }
320 }
321 }
322
323
329 string operator()(const string& cid) {
330 if (cid.empty()) {
331 return "";
332 }
333 return getNameList(cid);
334 }
335};
336
337
342
346 int id;
347
348
353
354
359
360
364 string iontype;
365
366};
367
368#endif
369
The widget representing the PubChem search widget.
Definition: cPubChemSearchWidget.h:33
void closeEvent(QCloseEvent *event)
Handle the window close event.
Definition: cPubChemSearchWidget.cpp:50
cPubChemSearchWidget()
The constructor.
Definition: cPubChemSearchWidget.cpp:9
void setHTML(string html)
Set the HTML text into the widget.
Definition: cPubChemSearchWidget.cpp:39
void store(ofstream &os)
Store the content into an output stream.
Definition: cPubChemSearchWidget.cpp:56
void keyPressEvent(QKeyEvent *event)
Handle a key press event.
Definition: cPubChemSearchWidget.cpp:67
string getHTML()
Get the HTML text from the widget.
Definition: cPubChemSearchWidget.cpp:45
void load(ifstream &is)
Load the content from an input stream.
Definition: cPubChemSearchWidget.cpp:61
~cPubChemSearchWidget()
The destructor.
Definition: cPubChemSearchWidget.cpp:32
The functor used to get the CIDs of compounds with the same formula.
Definition: cPubChemSearchWidget.h:109
int getNumberOfCIDs(string &cids)
Get the number of compounds with the same formula.
Definition: cPubChemSearchWidget.h:196
void attachVectorOfCIDs(string &cids, vector< string > &cidsvector)
Attach a vector of CIDs.
Definition: cPubChemSearchWidget.h:180
void getCommaSeparatedCIDs(string &cids, string &reducedcids)
Get a list of CIDs separated by comma.
Definition: cPubChemSearchWidget.h:160
string getCIDList(const string &compound)
Get the list of CIDs.
Definition: cPubChemSearchWidget.h:122
string result_type
result_type
Definition: cPubChemSearchWidget.h:114
string operator()(const string &compound)
Overloaded operator ().
Definition: cPubChemSearchWidget.h:221
The functor used to get the names of compounds from CID.
Definition: cPubChemSearchWidget.h:230
string getNameList(const string &cid)
Get the list of names.
Definition: cPubChemSearchWidget.h:243
void getReducedNames(string &names, set< string > &reducednames)
Get the reduded list of names.
Definition: cPubChemSearchWidget.h:302
string result_type
result_type
Definition: cPubChemSearchWidget.h:235
string operator()(const string &cid)
Overloaded operator ().
Definition: cPubChemSearchWidget.h:329
An auxiliary row info structure.
Definition: cPubChemSearchWidget.h:341
double theoreticalmz
Theoretical m/z.
Definition: cPubChemSearchWidget.h:352
double experimentalint
Experimental intensity.
Definition: cPubChemSearchWidget.h:358
int id
Identification ID.
Definition: cPubChemSearchWidget.h:346
string iontype
Ion type.
Definition: cPubChemSearchWidget.h:364
Auxiliary funtions and structures.