-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.cpp
More file actions
118 lines (105 loc) · 4.01 KB
/
api.cpp
File metadata and controls
118 lines (105 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <QJsonDocument>
#include <QJsonObject>
#include <curl/curl.h>
#include <QByteArray>
#include <QUrl>
#include <QUrlQuery>
#include <iostream>
#include <chrono>
#include <ostream>
#include <fstream>
#include <QCoreApplication>
#include <QJsonArray>
#include <list>
#include "api.h"
// Function to be called by libcurl when there is data to be written
size_t write_data(void *ptr, size_t size, size_t nmemb, QByteArray *data) {
data->append((char*) ptr, size * nmemb);
return size * nmemb;
}
QByteArray downloadUrl(const std::string &url, CURL* curl) {
qDebug() << "Downloading from" << url.c_str();
auto start = std::chrono::high_resolution_clock::now();
QByteArray data;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
curl_easy_perform(curl);
}
auto stop = std::chrono::high_resolution_clock::now();
qDebug() << "Downloaded" << data.size() << "bytes in" << std::chrono::duration_cast<std::chrono::seconds>(stop - start).count() << "seconds";
return data;
}
QByteArray DownloadSingle(const std::string &url) {
CURL *curl = curl_easy_init();
QByteArray data = downloadUrl(url, curl);
curl_easy_cleanup(curl);
return data;
}
std::vector<std::string> DownloadMany(const std::vector<std::string> &urls) {
std::vector<std::string> datas = {};
CURL *curl = curl_easy_init();
for (std::vector<std::string>::const_iterator url = urls.begin(); url != urls.end(); ++url) {
datas.push_back(downloadUrl(url->data(), curl).toStdString());
}
curl_easy_cleanup(curl);
return datas;
}
std::string searchHGNC(std::string terms, CURL* curl) {
QUrl url("https://clinicaltables.nlm.nih.gov/api/genes/v4/search");
QMap<QString, QString> params;
params["terms"] = QString::fromStdString(terms);
params["sf"] = "symbol,alias_symbol";
params["df"] = "symbol,alias_symbol";
QUrlQuery query;
for (auto it = params.begin(); it != params.end(); ++it) {
query.addQueryItem(it.key(), it.value());
}
url.setQuery(query);
qDebug() << "url:" << url.toString();
QByteArray data = downloadUrl(url.toString().toStdString(), curl);
return data.toStdString();
}
QJsonDocument searchPanther(QString geneInputList) {
QUrl url("http://pantherdb.org/services/oai/pantherdb/enrich/overrep");
QMap<QString, QString> params;
params["annotDataSet"] = "GO:0008150";
params["organism"] = "9606";
params["geneInputList"] = geneInputList;
QUrlQuery query;
for (auto it = params.begin(); it != params.end(); ++it) {
query.addQueryItem(it.key(), it.value());
}
url.setQuery(query);
QByteArray data = DownloadSingle(url.toString().toStdString());
// QString string = QString::fromUtf8(data.constData(), data.size());
// qDebug() << string;
QJsonDocument doc = QJsonDocument::fromJson(data);
return doc;
}
void saveTxt(QByteArray data, QString filename) {
std::ofstream outfile(filename.toStdString(), std::ios::out);
// qDebug() << "data is" << data.data();
outfile << data.data() << std::endl;
outfile.close();
}
std::string getGeneontology() {
QUrl url("http://current.geneontology.org/ontology/go.obo");
QByteArray data = DownloadSingle(url.toString().toStdString());
return data.toStdString();
}
std::vector<std::string> getOverrep(std::vector<std::string> geneLists) {
std::vector<std::string> labelList = {};
for (std::vector<std::string>::iterator geneList = geneLists.begin(); geneList != geneLists.end(); ++geneList) {
if (*geneList == "empty") {
labelList.push_back(std::string(""));
} else {
QJsonDocument doc = searchPanther(QString::fromStdString(*geneList));
labelList.push_back(doc.object()["results"].toObject()["result"].toArray().first().toObject()["term"].toObject()["label"].toString().toStdString());
}
} return labelList;
}
void printExample() {
}
void test() {}