intermine / InterMineR

R package for InterMine
GNU Lesser General Public License v2.1
18 stars 16 forks source link

Pass token to enrichment web service #65

Open yochannah opened 5 years ago

yochannah commented 5 years ago

as far as I can tell, it's impossible to enrich private lists with InterMineR. Code like this results in an error 400 You do not have access to a bag named listToEnrich

# okay, to save list on the intermine servce I'll need a token (in this case from humanmine)
# to get your own token log into the web interface (e.g. humanmine.org) and go to the
# myMine tab, then click the Account Details sub-tab, and copy and paste (or generate)
# your token into this script
myToken <- "tokenHerePlease"; #replace this with your own token

# load required libraries
library(InterMineR)
library(httr)

# This is a function to save lists on the intermine server. we'll use it later on in this script
saveIMList = function(
  # a nice intermine link please, e.g. http://www.humanmine.org/humanmine
  anInterMine, 
  #The name you'd like this to be saved on the server as... 
  listName= NULL,
  # The type of object you're saving, e.g. Gene or Protein or something else. 
  # This must match the name of the class in the InterMine model, e.g. "Gene" not "gene" or "genes"
  listType= "Gene",
  # The gene ids (or other entity, e/g/ protein ids) to save
  listContents = NULL,
  # your token. it can't work without this!
  token = NULL) {

  # We need to pass the list of ids as a single comma (or tab, or newline) separated string
  listContentsString <- paste0(
    listContents, 
    collapse=",")

  # The URL we call to save the list, with the name of the list we want to save
  # and the type of objects embedded in the URL as parameters. 
  requestUrl <- paste0(anInterMine,
                       "/service/lists?name=", listName,
                       "&type=", listType)

  # InterMineR doesn't have a built in method to save lists on the InterMine server, but we can call the API directly
  # Load the httr library to make http requests
  response <- POST(
    # the URL we're making an API call against
    url=requestUrl, 
    # tell them which IDs you want saved, as a single string
    body = listContentsString,
    # prove you're you with a token
    add_headers(Authorization = paste0("Token ", token)),
    encode = "multipart", 
    # clear errors please
    verbose(),
    # required, will return 500 error without the content type
    content_type("text/plain;charset=UTF-8"))
}

# querying against my chosen InterMine
myMine <- listMines()["HumanMine"]
im <- initInterMine(mine=myMine, token=myToken)

# Let's make a list of ids I want to enrich
# These are human gene identifiers. 
myGenes <- c(2566,
             57094,
             6323,
             6324,
             6335,
             84059)

# currently enrichment results MUST have a saved list as a background population,
# and the background population MUST have all of the same genes as in the list we're enriching.
# let's make a saved, named HumanMine list with our ids.
# note that this is the same list as above with a few extra ids for demo purposes
# it probably doesn't make much biological sense
backgroundPopulation <- list(2566,
                          57094,
                          6323,
                          6324,
                          6335,
                          84059, 
                          5468, 
                          3983, 
                          10257, 
                          105, 
                          29929,
                          10019,
                          10456,
                          10459,
                          1050,
                          1890 )

#save the BackgroundPopulation list
backgroundPopulationList <- saveIMList(
  myMine, 
  listName= "myBackgroundPopulation",
  listType= "Gene",
  listContents = backgroundPopulation,
  token = myToken) 

#save the list we want to be enriched
listToEnrich <- saveIMList(
  myMine, 
  listName= "listToEnrich",
  listType= "Gene",
  listContents = myGenes,
  token = myToken) 

#enrichment result with saved list name and background population as a saved list
enrichmentResult <- doEnrichment(
  im = im,
  genelist = "listToEnrich",
  widget = "publication_enrichment",
  population = "myBackgroundPopulation")

And looking through https://github.com/intermine/InterMineR/blob/master/R/doEnrichment.R I can't see anywhere where the token is passed through. I also tried intercepting the traffic with a network sniffer so I'm pretty sure the token isn't passed through. This is probably one of the causes of #42