xia-lab / MetaboAnalystR

R package for MetaboAnalyst
Other
318 stars 157 forks source link

Info API usage in R #267

Open KristinaGagalova opened 1 year ago

KristinaGagalova commented 1 year ago

Hi I was wondering if you can share some functions in R to access HMDB, SMPDB, ChemFont? Do you access these dbs through an API? Thank you in advance Kristina

howtofindme commented 1 year ago

Hi I was wondering if you can share some functions in R to access HMDB, SMPDB, ChemFont? Do you access these dbs through an API? Thank you in advance Kristina

have you solved the problem?

3.2 #Compound name mapping

First create a list containing a vector of the compounds to be queried (separated by a semi-colon)

and another character vector containing the compound id type.

The items in the list MUST be queryList and inputType

Valid input types are: "name", "hmdb", "kegg", "pubchem", "chebi", "metlin"

name.vec <- sig_up_volcano_list #c("1,3-Diaminopropane;2-Ketobutyric acid;2-Hydroxybutyric acid;2-Methoxyestrone") toSend = list(queryList = name.vec, inputType = "hmdb_id") head(toSend) library(httr)

The MetaboAnalyst API url

call <- "http://api.xialab.ca/mapcompounds"

Use httr::POST to send the request to the MetaboAnalyst API

The response will be saved in query_results

query_results <- httr::POST(call, body = toSend, encode = "json") head(query_results)

Check if response is ok (TRUE)

200 is ok! 401 means an error has occured on the user's end.

query_results$status_code==200

Parse the response into a table

Will show mapping to "hmdb_id", "kegg_id", "pubchem_id", "chebi_id", "metlin_id", "smiles"

query_results_text <- content(query_results, "text", encoding = "UTF-8") query_results_json <- rjson::fromJSON(query_results_text, flatten = TRUE) query_results_table <- t(rbind.data.frame(query_results_json)) rownames(query_results_table) <- query_results_table[,1]