Skip to content
Snippets Groups Projects
Commit 6932f0df authored by UMEC Mathieu's avatar UMEC Mathieu
Browse files

changing shape

parent 5dfe64f7
No related branches found
No related tags found
No related merge requests found
......@@ -3,15 +3,11 @@ import json
from urllib import request, parse
def send_request_to_Mapping_API(URL, metabolites_list, head,method='POST'):
def send_request_to_Mapping_API(URL, data_json, head,method='POST'):
"""
This function gives us the response of the api for the metabolites list
"""
if len(metabolites_list)==0 :
return("Stop")
data_for_request={"analytes": metabolites_list}
json_data = json.dumps(data_for_request).encode('utf-8')
req = request.Request(URL, data=json_data, headers=head, method='POST')
req = request.Request(URL, data=data_json, headers=head, method='POST')
with request.urlopen(req) as response:
result = response.read()
out_data=result.decode('utf-8')
......@@ -39,49 +35,65 @@ The out-file is compose of five columns pathwayName, pathwaySource, pathwayId, i
A line is associated with a metabolite and a metabolic pathway
"""
API_Datas=send_request_to_Mapping_API('https://rampdb.nih.gov/api/pathways-from-analytes',metabolites_list,{'Accept': '*/*','Content-Type': 'application/json'})
if API_Datas=="Stop":
print (" Your metabolite list is empty. Please restart mapping with a non-empty metabolite list. Here's an example ['KEGG:C01157','hmdb:HMDB0000064','hmdb:HMDB0000148','chebi:16015']")
if len(metabolites_list)==0 :
return (" Your metabolite list is empty. Please restart mapping with a non-empty metabolite list. Here's an example ['KEGG:C01157','hmdb:HMDB0000064','hmdb:HMDB0000148','chebi:16015']")
else:
len_out_api=len(API_Datas)
print(API_Datas[-3]+" metabolites were found")
index_begin_interest=API_Datas.find("[")
index_end_interest=API_Datas.find("]")
datas_to_treat=API_Datas[index_begin_interest:index_end_interest+1]
name_to_search=["pathwayName","pathwaySource","pathwayId","inputId"]
index = datas_to_treat.find("{")
index_begin_lines=[index]
while index != -1:
index+=1
index = datas_to_treat.find("{",index)
index_begin_lines.append(index)
index_begin_lines[-1]=len(datas_to_treat)
pathwayName=[]
inputId=[]
data_for_request={"analytes": metabolites_list}
json_data = json.dumps(data_for_request).encode('utf-8')
API_Datas=send_request_to_Mapping_API('https://rampdb.nih.gov/api/pathways-from-analytes',json_data,{'Accept': '*/*','Content-Type': 'application/json'})
len_out_api=len(API_Datas)
print(API_Datas[-3]+" metabolites were found")
index_begin_interest=API_Datas.find("[")
index_end_interest=API_Datas.find("]")
datas_to_treat=API_Datas[index_begin_interest:index_end_interest+1]
name_to_search=["pathwayName","pathwaySource","pathwayId","inputId"]
index = datas_to_treat.find("{")
index_begin_lines=[index]
while index != -1:
index+=1
index = datas_to_treat.find("{",index)
index_begin_lines.append(index)
index_begin_lines[-1]=len(datas_to_treat)
pathwayName=[]
inputId=[]
if infos=="all" :
pathwaySource=[]
pathwayId=[]
commonName=[]
for index_pos in range (len(index_begin_lines)-1):
one_line=datas_to_treat[index_begin_lines[index_pos]:index_begin_lines[index_pos+1]]
pathwayName.append(one_line[16:one_line.find("pathwaySource")-3])
inputId.append(one_line[one_line.find("inputId")+10:one_line.find("commonName")-3])
if infos=="all" :
pathwaySource=[]
pathwayId=[]
commonName=[]
for index_pos in range (len(index_begin_lines)-1):
one_line=datas_to_treat[index_begin_lines[index_pos]:index_begin_lines[index_pos+1]]
pathwayName.append(one_line[16:one_line.find("pathwaySource")-3])
inputId.append(one_line[one_line.find("inputId")+10:one_line.find("commonName")-3])
if infos=="all" :
pathwaySource.append(one_line[one_line.find("pathwaySource")+16:one_line.find("pathwayId")-3])
pathwayId.append(one_line[one_line.find("pathwayId")+12:one_line.find("inputId")-3])
commonName.append(one_line[one_line.find("commonName")+13:len(one_line)-3])
pathwaySource.append(one_line[one_line.find("pathwaySource")+16:one_line.find("pathwayId")-3])
pathwayId.append(one_line[one_line.find("pathwayId")+12:one_line.find("inputId")-3])
commonName.append(one_line[one_line.find("commonName")+13:len(one_line)-3])
pathwayName.insert(0,"pathwayName")
pathwaySource.insert(0,"pathwaySource")
pathwayId.insert(0,"pathwayId")
inputId.insert(0,"inputId")
commonName.insert(0,"commonName")
list_result=[pathwayName, pathwaySource, pathwayId, inputId, commonName]
df_result=pd.DataFrame(data=list_result).transpose()
excel_file_writer(df_result,outfile, sheetname="Resultats du mapping")
pathwayName.insert(0,"pathwayName")
pathwaySource.insert(0,"pathwaySource")
pathwayId.insert(0,"pathwayId")
inputId.insert(0,"inputId")
commonName.insert(0,"commonName")
list_result=[pathwayName, pathwaySource, pathwayId, inputId, commonName]
df_result=pd.DataFrame(data=list_result).transpose()
excel_file_writer(df_result,outfile, sheetname="Resultats du mapping")
def mapping_from_MA_API(metabolites_list, outfile):
if len(metabolites_list)==0 :
return (" Your metabolite list is empty. Please restart mapping with a non-empty metabolite list. Here's an example ['1,3-Diaminopropane','2-Ketobutyric acid','2-Hydroxybutyric acid']")
else:
headers = {'Content-Type':"application/json",'cache-control':"no-cache"}
metabo_data=''
for name in metabolites_list :
metabo_data=metabo_data+name+';'
payload = {"queryList": metabo_data,"inputType": "name"}
json_data = json.dumps(payload).encode('utf-8')
API_Datas=send_request_to_Mapping_API("https://www.xialab.ca/api/mapcompounds",json_data,headers)
print(API_Datas)
if __name__ == "__main__":
met_L=["KEGG:C01157","hmdb:HMDB0000064","hmdb:HMDB0000148","chebi:16015"]
met_L=['1,3-Diaminopropane','2-Ketobutyric acid','2-Hydroxybutyric acid']
inf="all" # all,only midfile
outf="C:\\Users\\mumec\\Desktop\\fichier_mis_en_forme_programme_total\\API_RAMP_test.xlsx"
mapping_from_RAMP_API(met_L,outf)
\ No newline at end of file
#mapping_from_RAMP_API(met_L,outf)
mapping_from_MA_API(met_L,outf)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment