### R code from vignette source 'STRINGdb.Rnw'

###################################################
### code chunk number 1: initialization
###################################################
library(STRINGdb)
string_db <- STRINGdb$new( version="12.0", species=9606, 
                           score_threshold=400, network_type="full", link_data="detailed", input_directory="")


###################################################
### code chunk number 2: help
###################################################
STRINGdb$methods()              # To list all the methods available.
STRINGdb$help("get_graph")      # To visualize their documentation.


###################################################
### code chunk number 3: load_data
###################################################
data(diff_exp_example1)
head(diff_exp_example1)


###################################################
### code chunk number 4: map
###################################################
example1_mapped <- string_db$map( diff_exp_example1, "gene", removeUnmappedRows = TRUE )


###################################################
### code chunk number 5: STRINGdb.Rnw:112-114
###################################################
options(SweaveHooks=list(fig=function()
par(mar=c(2.1, 0.1, 4.1, 2.1))))


###################################################
### code chunk number 6: get_hits
###################################################
example1_mapped_pval05 <- subset(example1_mapped, pvalue < 0.05)
example1_mapped_pval05 <- example1_mapped_pval05[order(example1_mapped_pval05$pvalue), ]
hits <- example1_mapped_pval05$STRING_id[1:200]


###################################################
### code chunk number 7: plot_network
###################################################
getOption("SweaveHooks")[["fig"]]()
string_db$plot_network( hits )


###################################################
### code chunk number 8: add_diff_exp_color
###################################################
# filter by p-value and add a color column 
# (i.e. green down-regulated gened and red for up-regulated genes)
example1_mapped_pval05 <- string_db$add_diff_exp_color( subset(example1_mapped, pvalue<0.05), 
                                                            logFcColStr="logFC" )    


###################################################
### code chunk number 9: post_payload
###################################################
# post payload information to the STRING server
payload_id <- string_db$post_payload( example1_mapped_pval05$STRING_id, 
                                        colors=example1_mapped_pval05$color )


###################################################
### code chunk number 10: plot_halo_network
###################################################
getOption("SweaveHooks")[["fig"]]()
# display a STRING network png with the "halo"
string_db$plot_network( hits, payload_id=payload_id )


###################################################
### code chunk number 11: enrichment
###################################################
enrichment <- string_db$get_enrichment( hits )
head(enrichment, n=20)


###################################################
### code chunk number 12: enrichment_figure
###################################################
getOption("SweaveHooks")[["fig"]]()
enrichment_img <- string_db$get_enrichment_figure(
  hits,
  category = "Component",
  group_by_similarity = 0.8,
  color_palette = "yellow_pink",
  x_axis = "signal"
)
grid::grid.newpage()
grid::grid.raster(enrichment_img, interpolate = FALSE)


###################################################
### code chunk number 13: background
###################################################
backgroundV <- example1_mapped$STRING_id
string_db$set_background(backgroundV)


###################################################
### code chunk number 14: new_background_inst (eval = FALSE)
###################################################
## string_db <- STRINGdb$new( score_threshold=400, backgroundV = backgroundV )


###################################################
### code chunk number 15: enrichment_with_background
###################################################
enrichment_background_corrected <- string_db$get_enrichment( hits )
head(enrichment_background_corrected, n=20)


###################################################
### code chunk number 16: annotations
###################################################
annotations <- string_db$get_annotations( hits )
head(annotations, n=20)


###################################################
### code chunk number 17: clustering1
###################################################
# get clusters
clustersList <- string_db$get_clusters(example1_mapped_pval05$STRING_id[1:600])


###################################################
### code chunk number 18: clustering2
###################################################
getOption("SweaveHooks")[["fig"]]()
par(mar = c(0, 0, 0.5, 0))
string_db$plot_network(clustersList[[1]], add_summary = FALSE)

par(mar = c(0, 0, 0.5, 0))
string_db$plot_network(clustersList[[2]], add_summary = FALSE)

par(mar = c(0, 0, 0.5, 0))
string_db$plot_network(clustersList[[3]], add_summary = FALSE)

par(mar = c(0, 0, 0.5, 0))
string_db$plot_network(clustersList[[4]], add_summary = FALSE)


###################################################
### code chunk number 19: proteins
###################################################
string_proteins <- string_db$get_proteins()
head(string_proteins)


###################################################
### code chunk number 20: atmtp
###################################################
tp53 = string_db$mp( "tp53" )
atm = string_db$mp( "atm" )


###################################################
### code chunk number 21: interactions
###################################################
string_db$get_interactions( c(tp53, atm) )


###################################################
### code chunk number 22: interaction_partners
###################################################
string_db$get_interaction_partners( c(tp53, atm), limit=5 )[, c("from", "to", "combined_score", "experimental", "from_name", "to_name")]


###################################################
### code chunk number 23: interaction_partners_experimental
###################################################
interaction_partners <- string_db$get_interaction_partners( c(tp53, atm), limit=5 )
interaction_partners_exp <- subset(interaction_partners, experimental > 0,
                                   select = c("from_name", "to_name", "experimental"))
interaction_partners_exp


###################################################
### code chunk number 24: paralogs
###################################################
# Get all homologs of TP53 in human.
string_db$get_paralogs(tp53)


###################################################
### code chunk number 25: Closest homologs from other species
###################################################
# get the best hits of TP53 in all STRING species
tp53_besthits <- string_db$get_homologs_besthits(tp53)
tp53_besthits_100 <- subset(tp53_besthits, bitscore > 100)
nrow(tp53_besthits_100)
head(tp53_besthits_100, n=10)


###################################################
### code chunk number 26: homologs_besthits in target species
###################################################
# get the homologs of the following two proteins in the mouse (i.e. species_id=10090)
string_db$get_homologs_besthits(c(tp53, atm), target_species_id=10090)
