Skip to content

Instantly share code, notes, and snippets.

@averissimo
Last active July 18, 2018 17:25
Show Gist options
  • Save averissimo/b52774cd77f4380861f66629e51b8b43 to your computer and use it in GitHub Desktop.
Save averissimo/b52774cd77f4380861f66629e51b8b43 to your computer and use it in GitHub Desktop.
File to use in support ticket
library(tidyverse)
library(STRINGdb)
STRINGdb::get_STRING_species(version = version, species_name=NULL) %>%
dplyr::arrange(official_name) %>%
dplyr::filter(official_name == 'homo_sapiens')
# downloading Homo sapiens
string_db <- STRINGdb::STRINGdb$new(version = version,
species = 9606,
score_threshold = 0)
# Load to memory the database (by calling a method)
tp53 <- string_db$mp( "tp53" )
# get all interactions
all.interactions <- as.tbl(string_db$get_interactions(string_db$proteins$protein_external_id))
col.ixs <- (!colnames(all.interactions) %in% 'combined_score')
# columns without from and to
col.vals.ixs <- col.ixs & (!colnames(all.interactions) %in% c('from', 'to'))
p <- 0.041
mat <- as.matrix(all.interactions[,col.vals.ixs]) / 1000
#
# Calculate new combined score
# https://string-db.org/help/faq/#how-are-the-scores-computed
combined.score <- mat %>%
apply( 2, function(ix) {
res <- (ix - p) / (1 - p)
res[ix == 0] <- 0
return(res)
}) %>% {
# Normalize Co-Occurence and Text mining scores
# https://string-db.org/help/faq/#how-are-the-scores-computed
for (ix in c('cooccurence', 'textmining', 'textmining_transferred')) {
if (ix %in% colnames(.)) {
.[, ix] <- .[, ix] * (1 - .[, 'homology'])
}
}
.[,!colnames(.) %in% c('homology')]
#.
} %>% {
res <- (1 - .[,1])
for (ix in seq(ncol(.))[-1]) {
res <- res * (1 - .[,ix])
}
1 - res
} %>% {
. + p * (1 - .)
} %>% {
floor(. * 1000)
}
abs(combined.score - all.interactions$combined_score) %>%
data.frame() %>% filter(. > 1) %>% { qplot(.[,'.'], bins = 100) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment