Created
April 17, 2020 19:22
-
-
Save timedreamer/6e612be18d1914fdcbc0bb917e7aa7ef to your computer and use it in GitHub Desktop.
Calculate random network AUPR.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calculate random network AUPR. | |
## Author: Ji Huang | |
## Date: 2020-04-15 | |
library(here) | |
library(tidyverse) | |
library(precrec) | |
# function to manipulate the edge for each network. | |
confirm_edge <- function(network) { | |
mutate({{network}}, edge = paste0(regulator, "-", target)) %>% | |
arrange(desc(weight)) %>% | |
filter(regulator %in% all_regulator) %>% | |
mutate(label = if_else(edge %in% filter_edge, "1", "0")) %>% | |
select(weight, label) %>% | |
mutate(label = as.integer(label)) | |
} | |
# function to calculate a random network AUPR. | |
calc_random_ntwk_aupr <- function(i) { | |
set.seed(i) | |
random1 <- op3 %>% mutate(weight = sample(weight, replace = F)) | |
random1 <- confirm_edge(random1) | |
msmdat <- mmdata(random1$weight, random1$label) | |
rand_aupr <- auc(evalmod(msmdat))[2,4] | |
return(rand_aupr) | |
} | |
## Load OutPredict result. | |
op3 <- read_csv(here("result", "Ranked_list_TF_gene_best_model_Nonly_ntree1000.csv"), | |
col_names = c("regulator", "target", "weight"), | |
skip = 1) %>% | |
filter(regulator!= target) | |
## Load the positive TARGET result. Keep the 29 TFs within the network (Discard 4 TFs) | |
ptarget <- read_tsv(here("data", "at_33target_result.txt")) | |
all_regulator <- unique(op3$regulator)[unique(op3$regulator) %in% | |
unique(ptarget$regulator)] | |
## Combine TF and edges, filter 29 TFs and check whether edges are confirmed by TARGET. | |
ptarget <- ptarget %>% filter(regulator %in% all_regulator) %>% | |
mutate(edge = paste0(regulator, "-", target)) | |
filter_edge <- ptarget$edge | |
## Repeat 10000 times for random network. | |
random_aupr <- map_dbl(1:10000, calc_random_ntwk_aupr) | |
random_aupr <- data.frame(aupr = random_aupr) | |
write_tsv(random_aupr, path = here("result", "ath_root_random_network_1E5rep.tsv.gz")) | |
## Take a quick look. | |
tt1 <- read_tsv(here("result", "ath_root_random_network_1E5rep.tsv.gz")) | |
fivenum(tt1$aupr) | |
plot(density(tt1$aupr)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment