Skip to content

Commit 1fa7815

Browse files
committed
Add downsample feature
1 parent 6c3ba0a commit 1fa7815

7 files changed

Lines changed: 70 additions & 18 deletions

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
## Bug fixes
44
* `plot_celltype_severity`: Ensure cell types are ordered by composite severity.
55
* `get_ggstatsplot_stats`: Return all stats, not just specific labels.
6+
* `ttd_check` \ `ttd_plot`
7+
- Make % prioritised targets a percentage of the total number of rows.
8+
- Fix mistake where I was including non-gene therapies too
9+
(due to how grepping collapsed listed works in data.tables).
10+
- Account for situations when there's no failed targets to check.
611

12+
## New features
13+
* `validate_associations_correlate_ctd`: Add `downsample` arg to reduce plot size.
714

815
# MSTExplorer 1.0.7
916

R/plot_celltype_severity.R

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#' set.seed(2025)
2727
#' results <- load_example_results()
2828
#' results <- results[sample(seq(nrow(results)), 5000),]
29-
#' out <- plot_celltype_severity(results, types="bar")
29+
#' out <- plot_celltype_severity(results, types=c("bar","dot"))
3030
plot_celltype_severity <- function(results,
3131
cl = get_cl(),
3232
q_threshold=.05,
@@ -64,9 +64,14 @@ plot_celltype_severity <- function(results,
6464
"blindness","sensory_impairments","immunodeficiency","cancer",
6565
"reduced_fertility","congenital_onset")]|>
6666
data.table::melt.data.table(id.vars=c("cl_id","cl_name"))
67+
6768
)[,value:=factor(value,levels = c(0:3),ordered = TRUE)]
69+
70+
# Split variable names for plotting
6871
agg_gpt[,variable:=gsub("_"," ",variable)]
69-
agg_gpt[,variable:=factor(variable, levels = unique(variable),ordered = TRUE)]
72+
agg_gpt[,variable_split:=gsub(" ","\n",variable)]
73+
agg_gpt[variable_split=="immunodeficiency",variable_split:="immuno-\ndeficiency"]
74+
agg_gpt[,variable_split:=factor(variable_split, levels = unique(variable_split),ordered = TRUE)]
7075
# Set celltype name order
7176
agg_gpt[,cl_name:=factor(cl_name, levels = celltype_order,ordered = TRUE)]
7277

@@ -80,6 +85,8 @@ plot_celltype_severity <- function(results,
8085
out <- list()
8186
if("bar" %in% types){
8287
messager("Creating bar plot.")
88+
89+
# Subplot with composite severity scores
8390
gg_severity <- ggplot2::ggplot(celltypes_gpt,
8491
ggplot2::aes(x=cl_name,y=severity_score_gpt,
8592
fill=severity_score_gpt)) +
@@ -89,9 +96,11 @@ plot_celltype_severity <- function(results,
8996
fill="GPT\nseverity\nscore") +
9097
ggplot2::coord_flip()+
9198
ggplot2::theme_minimal()
99+
100+
# Subplot with individual severity annotations
92101
gg_annot <- ggplot2::ggplot(agg_gpt[!is.na(value)],
93102
ggplot2::aes(x=cl_name, y=1, fill=value))+
94-
ggplot2::facet_grid(.~variable, scales="free_y")+
103+
ggplot2::facet_grid(.~variable_split, scales="free_y")+
95104
ggplot2::geom_bar(stat="identity", position="fill")+
96105
ggplot2::scale_fill_viridis_d(option="plasma",
97106
labels=c(`0`="never",
@@ -101,7 +110,8 @@ plot_celltype_severity <- function(results,
101110
ggplot2::scale_y_continuous(breaks = c(0,.5,1), labels=c("0","0.5","1")) +
102111
ggplot2::labs(y="Proportion of associated phenotypes", x="Cell type") +
103112
ggplot2::coord_flip()+
104-
ggplot2::theme_minimal()
113+
ggplot2::theme_minimal() +
114+
ggplot2::theme(axis.text.x = ggplot2::element_text(vjust = 1) )
105115

106116
out[["bar"]][["plot"]] <- (gg_annot | gg_severity) +
107117
patchwork::plot_layout(axes = "collect", widths = c(1,.1))

R/plot_density_cor.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@ plot_density_cor <- function(res,
55
density_alpha=.7,
66
min_colors=15,
77
log_vars=FALSE,
8+
downsample=NULL,
89
show.legend=FALSE){
10+
res <- data.table::copy(res)
911
res <- res[!is.na(get(x)) & !is.na(get(y))]
12+
13+
### Downsample for plotting
14+
if (!is.null(downsample) && downsample < nrow(res)) {
15+
downsample <- min(downsample, nrow(res))
16+
messager("Downsampling to", downsample, "points.")
17+
res <- res[sample(.N, downsample)]
18+
}
19+
1020
p<- res |>
1121
ggstatsplot::ggscatterstats(x=!!ggplot2::sym(x),
1222
y=!!ggplot2::sym(y),

R/plot_ttd.R

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ plot_ttd <- function(dat_sub,
2121
subtitle = paste0(
2222
subtitle_prefix,
2323
"(",
24-
round(sum(dat_sub[prioritised==TRUE,]$n_drugs)/
25-
sum(dat_sub$n_drugs)*100,2),
24+
round(sum(dat_sub$prioritised)/
25+
nrow(dat_sub)*100,2),
2626
"% prioritised)"
2727
),
2828
width = NULL,
@@ -124,6 +124,14 @@ plot_ttd <- function(dat_sub,
124124
as_percent = as_percent)
125125
ylims <- c(0,
126126
max(p1$data$n_drugs_total))
127+
128+
if (nrow(fail) == 0) {
129+
messager("No failed therapeutics found, only plotting non-failed therapeutics.")
130+
return(list(
131+
plot = p1,
132+
data = dat_sub
133+
))
134+
}
127135
p2 <- plot_ttd_make(fail,
128136
subtitle_prefix="Failed therapeutics\n",
129137
as_percent = as_percent,
@@ -142,5 +150,8 @@ plot_ttd <- function(dat_sub,
142150
plt <- plot_ttd_make(dat_sub,
143151
as_percent = as_percent)
144152
}
145-
return(plt)
153+
return(list(
154+
plot= plt,
155+
data= dat_sub
156+
))
146157
}

R/ttd_check.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,14 @@ ttd_check <- function(top_targets,
9393
baseline <- dat_sub[,list(
9494
baseline_prop=sum(GENENAME3 %in% unique(phenotype_to_genes$gene_symbol))/.N
9595
), by=c("HIGHEST_STATUS")]
96+
9697
#### Filter by drug type ####
9798
if(!is.null(drug_types)){
9899
dat_sub <- dat_sub[
99100
tolower(DRUGNAME) %in% tolower(drug_types) |
100-
tolower(DRUGTYPE) %in% tolower(drug_types) |
101-
grepl(paste(drug_types,collapse = "|"),DRUGNAME,ignore.case = TRUE) |
102-
grepl(paste(drug_types,collapse = "|"),DRUGTYPE,ignore.case = TRUE),
101+
tolower(DRUGTYPE) %in% tolower(drug_types),
102+
# grepl(paste(drug_types,collapse = "|"),DRUGNAME,ignore.case = TRUE) |
103+
# grepl(paste(drug_types,collapse = "|"),DRUGTYPE,ignore.case = TRUE),
103104
]
104105
}
105106
#### Filter by status ####
@@ -147,18 +148,18 @@ ttd_check <- function(top_targets,
147148
base_size = base_size,
148149
label_size = label_size)
149150
#### Show ####
150-
if(isTRUE(show_plot)) methods::show(plt)
151+
if(isTRUE(show_plot)) methods::show(plt$plot)
151152
#### Save ####
152-
KGExplorer::plot_save(plt = plt,
153+
KGExplorer::plot_save(plt = plt$plot,
153154
save_path=save_path,
154155
height=height,
155156
width=width)
156157
#### Return ####
157158
return(
158-
list(data=dat_sub,
159+
list(data=plt$data,
159160
data_overlap=dat_sub2,
160161
pct_captured,
161-
plot=plt,
162+
plot=plt$plot,
162163
ttd_hypergeo_out=ttd_hypergeo_out)
163164
)
164165
}

R/validate_associations_correlate_ctd.R

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#' that contains the group variable to compare across.
77
#' @param celltype_var A character string specifying the column in \code{results}
88
#' that contains the cell type variable to compare across.
9+
#' @param downsample Downsample the data to this many points when plotting.
910
#' @param ... Additional arguments passed to \code{plot_density_cor}.
1011
#' @inheritParams prioritise_targets
1112
#' @inheritParams KGExplorer::filter_dt
@@ -26,6 +27,7 @@ validate_associations_correlate_ctd <- function(results=load_example_results(),
2627
group_var="ctd",
2728
celltype_var="cl_name",
2829
q_threshold=0.05,
30+
downsample=NULL,
2931
...){
3032
test_id <- NULL;
3133
results <- map_celltype(results)
@@ -60,21 +62,29 @@ validate_associations_correlate_ctd <- function(results=load_example_results(),
6062
paste0("@FDR<",q_threshold,"."))
6163
#### Plot ####
6264
messager("Generating plots.")
65+
66+
67+
6368
plots <- list()
6469
plots[["p.all"]] <- plot_density_cor(res2,
6570
x=paste0("p_",group_values[1]),
66-
y=paste0("p_",group_values[2])
71+
y=paste0("p_",group_values[2]),
72+
downsample=downsample,
6773
)
6874
plots[["logFC.all"]] <- plot_density_cor(res2,
6975
x=paste0("logFC_",group_values[1]),
70-
y=paste0("logFC_",group_values[2])
76+
y=paste0("logFC_",group_values[2]),
77+
downsample=downsample
7178
)
7279
plots[["p.significant"]] <- plot_density_cor(res_sig,
7380
x=paste0("p_",group_values[1]),
74-
y=paste0("p_",group_values[2]))
81+
y=paste0("p_",group_values[2]),
82+
downsample=downsample
83+
)
7584
plots[["logFC.significant"]] <- plot_density_cor(res_sig,
7685
x=paste0("logFC_",group_values[1]),
77-
y=paste0("logFC_",group_values[2])
86+
y=paste0("logFC_",group_values[2]),
87+
downsample=downsample
7888
)
7989
messager("Gathering statistics.")
8090
data_stats <- lapply(plots, get_ggstatsplot_stats)

man/validate_associations_correlate_ctd.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)