Skip to content

Instantly share code, notes, and snippets.

@jwaage
Created October 12, 2016 14:06
Show Gist options
  • Save jwaage/b713ec64b7ed6177a67725709e985630 to your computer and use it in GitHub Desktop.
Save jwaage/b713ec64b7ed6177a67725709e985630 to your computer and use it in GitHub Desktop.
StringDB PPI grabber
# Libraries
# ------------------------------------------------------------------------------
library("RCurl")
library("bitops")
# Define functions
# ------------------------------------------------------------------------------
getInteractions = function(id,score,limit,species=9606,print=FALSE){
# --------------------------------------------------------------------
# Param Example Description
# --------------------------------------------------------------------
# id id="PLCG1" The id to search the stringdb for
# score score=800 800/1000 = 80% chance of interaction
# limit limit=10 Only include the top 10 interactions
# species species=9606 The species, 9606 is human
# print print=TRUE Print the browser pastable link to stringdb
# --------------------------------------------------------------------
# Check parameters
if( length(id) != 1 ){ stop("getInteractions error: 'id' must have length 1!") }
if( !is.numeric(score) & 0 <= score & score <= 999 ){ stop("getInteractions error: 'score' must be numeric between 0 and 999!") }
if( !is.numeric(limit) ){ stop("getInteractions error: 'limit' must be numeric!") }
if( !is.numeric(species) ){ stop("getInteractions error: 'species' must be numeric!") }
if( !is.logical(print) ){ stop("getInteractions error: 'print' must be logical!") }
# Create API request
stringdb = "http://string-db.org/api/psi-mi-tab/interactions?"
request = paste0("identifier=",id,"&required_score=",score,"&limit=",limit,"&species=",species)
url = paste0(stringdb,request)
if( print ){ cat(url,"\n") }
# Retrieve request and check if 'id' is recognised by stringdb
text = getURL(url)
if( print ){ print(text) }
if( text == "\n" | grepl('ERROR_NOT_FOUND',text) ){ return(NA) }
# Read and parse results
results = read.table(textConnection(text),stringsAsFactors=FALSE)
results = subset(results,subset=(V3==id | V4==id))
match = regexpr("[[:digit:]].[[:digit:]]+", results$V15)
scores = as.numeric(regmatches(results$V15, match))
output = cbind(results$V3,results$V4,scores)
output = as.data.frame(t(apply(output,1,function(x){ return(x[-which(x==id)]) })))
colnames(output) = c("PPI","SCORE")
return(output)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment