Try to call the REST APIs provided by Enrichr from Matlab, but webwrite does not work

5 次查看(过去 30 天)
Trying to translate a piece of Python code below to Matlab. The Python code is provided by Enrichr (see maayanlab.cloud/Enrichr/help#api) as an example for calling its REST APIs.
%{
% - this is the Python code
import json
import requests
ENRICHR_URL = 'https://maayanlab.cloud/Enrichr/addList'
genes_str = '\n'.join([
'PHF14', 'RBM3', 'MSL1', 'PHF21A', 'ARL10', 'INSR', 'JADE2', 'P2RX7',
'LINC00662', 'CCDC101', 'PPM1B', 'KANSL1L', 'CRYZL1', 'ANAPC16', 'TMCC1',
'CDH8', 'RBM11', 'CNPY2', 'HSPA1L', 'CUL2', 'PLBD2', 'LARP7', 'TECPR2',
'ZNF302', 'CUX1', 'MOB2', 'CYTH2', 'SEC22C', 'EIF4E3', 'ROBO2',
'ADAMTS9-AS2', 'CXXC1', 'LINC01314', 'ATF7', 'ATP5F1'
])
description = 'Example gene list'
payload = {
'list': (None, genes_str),
'description': (None, description)
}
response = requests.post(ENRICHR_URL, files=payload)
%}
% - this is the Matlab code
ENRICHR_URL = 'https://maayanlab.cloud/Enrichr/addList';
genes = {'PHF14', 'RBM3', 'MSL1', 'PHF21A', 'ARL10', 'INSR', 'JADE2', 'P2RX7', ...
'LINC00662', 'CCDC101', 'PPM1B', 'KANSL1L', 'CRYZL1', 'ANAPC16', 'TMCC1', ...
'CDH8', 'RBM11', 'CNPY2', 'HSPA1L', 'CUL2', 'PLBD2', 'LARP7', 'TECPR2', ...
'ZNF302', 'CUX1', 'MOB2', 'CYTH2', 'SEC22C', 'EIF4E3', 'ROBO2', ...
'ADAMTS9-AS2', 'CXXC1', 'LINC01314', 'ATF7', 'ATP5F1'};
genes_str = strjoin(genes, newline);
description = 'Example gene list';
options = weboptions('MediaType', 'application/json');
payload(1).('list') = [];
payload(2).('list') = genes_str;
payload(1).('description') = [];
payload(2).('description') = description;
%payload = struct('list', {string(missing), genes_str}, ...
% 'description', {string(missing), description});
response = webwrite(ENRICHR_URL, payload, options)
But, the translated Matlab code does not work. Any suggestions would be greatly apprecaited.

采纳的回答

Piyush Kumar
Piyush Kumar 2024-8-28
It looks like the issue might be related to how the payload is structured and sent in the MATLAB code. The webwrite function in MATLAB does not directly support the multipart/form-data content type, which is required for the Enrichr API. Instead, you can use the matlab.net.http package to create a more complex HTTP request.
The Python code provided use the files parameter in the requests.post method, which is typically used for multipart form-data.
In the context of the Enrichr API, the files parameter is used to send the gene list and description as separate parts of the request, even though they are not files. This is a common pattern for APIs that expect multipart form-data.
I tried the following code and it worked -
import matlab.net.URI
import matlab.net.http.RequestMessage
import matlab.net.http.io.MultipartProvider
import matlab.net.http.io.MultipartFormProvider
ENRICHR_URL = 'https://maayanlab.cloud/Enrichr/addList';
genes = {'PHF14', 'RBM3', 'MSL1', 'PHF21A', 'ARL10', 'INSR', 'JADE2', 'P2RX7', ...
'LINC00662', 'CCDC101', 'PPM1B', 'KANSL1L', 'CRYZL1', 'ANAPC16', 'TMCC1', ...
'CDH8', 'RBM11', 'CNPY2', 'HSPA1L', 'CUL2', 'PLBD2', 'LARP7', 'TECPR2', ...
'ZNF302', 'CUX1', 'MOB2', 'CYTH2', 'SEC22C', 'EIF4E3', 'ROBO2', ...
'ADAMTS9-AS2', 'CXXC1', 'LINC01314', 'ATF7', 'ATP5F1'};
genes_str = strjoin(genes, newline);
description = 'Example gene list';
disp(genes_str)
PHF14 RBM3 MSL1 PHF21A ARL10 INSR JADE2 P2RX7 LINC00662 CCDC101 PPM1B KANSL1L CRYZL1 ANAPC16 TMCC1 CDH8 RBM11 CNPY2 HSPA1L CUL2 PLBD2 LARP7 TECPR2 ZNF302 CUX1 MOB2 CYTH2 SEC22C EIF4E3 ROBO2 ADAMTS9-AS2 CXXC1 LINC01314 ATF7 ATP5F1
disp(description)
Example gene list
% Create the payload
formData = MultipartFormProvider( 'list', genes_str, 'description', description);
% Create the request
request = RequestMessage('post', [], formData);
% Send the request
uri = URI(ENRICHR_URL);
response = send(request, uri);
disp(response)
ResponseMessage with properties: StatusLine: 'HTTP/1.1 200 ' StatusCode: OK Header: [1x11 matlab.net.http.HeaderField] Body: [1x1 matlab.net.http.MessageBody] Completed: 0

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call Web Services from MATLAB Using HTTP 的更多信息

产品


版本

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by