Skip to content

Commit

Permalink
Merge branch 'developer' into fetch_pdb_improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
jpquast authored Oct 31, 2024
2 parents ee4f706 + fb2bd6c commit cc89485
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
* The `find_peptide_in_structure()` function now has additional outputs:
* `start_adjusted` and `end_adjusted`: Are the adjusted start and end positions of a peptide if it is only partially covered by the structure.
* `percentage_covered_peptides`: Is the percentage of all provided peptides that are at least partially covered by the structure.
* `calculate_go_enrichment()` received the argument `label_size` that allows the user to specifiy the size of the labels in the plot.

## Bug fixes

* Fixed issue #193. This makes sure that information in retained columns can be propagated to newly created combinations, which were not present in the original data.
* Fixed issue #251. Sodium ions (Na) are not read as `NA` anymore, when using the `fetch_pdb()` function.
* `calculate_go_enrichment()` can now correctly handle groups that are of type factor.

## Additional Changes

Expand Down
60 changes: 39 additions & 21 deletions R/calculate_go_enrichment.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ go_enrichment <- function(...) {
#' should be reversed in order. Default is `TRUE`.
#' @param label a logical argument indicating whether labels should be added to the plot.
#' Default is TRUE.
#' @param label_size a numeric argument that specifies the text size of the labels with the unit "pt".
#' The default is 7.
#' @param enrichment_type a character argument that is either "all", "enriched" or "deenriched". This
#' determines if the enrichment analysis should be performed in order to check for both enrichemnt and
#' deenrichemnt or only one of the two. This affects the statistics performed and therefore also the displayed
Expand Down Expand Up @@ -223,6 +225,7 @@ calculate_go_enrichment <- function(data,
heatmap_fill_colour = protti::mako_colours,
heatmap_fill_colour_rev = TRUE,
label = TRUE,
label_size = 7,
enrichment_type = "all",
replace_long_name = TRUE,
label_move_frac = 0.2,
Expand Down Expand Up @@ -408,12 +411,14 @@ if you used the right organism ID.", prefix = "\n", initial = ""))
}

result_table <- cont_table %>%
dplyr::arrange({{ group }}) %>%
{ # group argument is missing
if (group_missing) {
dplyr::left_join(., fisher_test, by = "go_id")
} else {
# group argument is not missing
dplyr::left_join(., fisher_test, by = c("go_id", rlang::as_name(enquo(group)))) %>%
dplyr::mutate({{ group }} := forcats::fct_inorder({{ group }})) %>%
dplyr::group_by({{ group }})
}
} %>%
Expand Down Expand Up @@ -481,7 +486,14 @@ if you used the right organism ID.", prefix = "\n", initial = ""))

# move label if bar is less than 20% (default) of largest bar
plot_input <- plot_input %>%
dplyr::mutate(hjust = ifelse((.data$neg_log_sig / max(.data$neg_log_sig)) < label_move_frac, -0.15, 1.05))
dplyr::mutate(hjust = ifelse((.data$neg_log_sig / max(.data$neg_log_sig)) < label_move_frac, -0.15, 1.05)) %>%
dplyr::mutate(label = ifelse(!is.na(.data$n_significant_proteins_in_process), paste0(
.data$n_significant_proteins_in_process, "/",
.data$n_detected_proteins_in_process, " (",
round(.data$n_significant_proteins_in_process / .data$n_detected_proteins_in_process * 100, digits = 1), "%)"
),
NA
))

if (plot_style == "barplot") {
# Check if ggforce package is available. If not prompt user to install it.
Expand Down Expand Up @@ -519,22 +531,19 @@ if you used the right organism ID.", prefix = "\n", initial = ""))
geom_text(
data = plot_input,
aes(
label = paste0(
.data$n_significant_proteins_in_process,
"/",
.data$n_detected_proteins_in_process,
"(",
round(.data$n_significant_proteins_in_process / .data$n_detected_proteins_in_process * 100, digits = 1),
"%)"
),
label = label,
y = .data$neg_log_sig - 0.1,
hjust = .data$hjust
)
),
size = label_size / .pt
)
}
} +
ggplot2::scale_fill_manual(values = c(Deenriched = barplot_fill_colour[1], Enriched = barplot_fill_colour[2])) +
ggplot2::scale_y_continuous(labels = scales::number_format(accuracy = 1)) +
ggplot2::scale_y_continuous(
labels = scales::number_format(accuracy = 1),
expand = expansion(mult = c(0, 0.05))
) +
ggplot2::coord_flip() +
{
if (!missing(group)) {
Expand Down Expand Up @@ -605,7 +614,11 @@ if you used the right organism ID.", prefix = "\n", initial = ""))
if (group_missing) {
dplyr::mutate(., grouping = "")
} else {
dplyr::mutate(., grouping = forcats::fct_inorder({{ group }}))
if (!is(dplyr::pull(plot_input, {{ group }}), "factor")) { # if the column is already a factor do not reorder
dplyr::mutate(., grouping = forcats::fct_inorder({{ group }}))
} else {
dplyr::mutate(., grouping = {{ group }})
}
}
}

Expand All @@ -631,7 +644,15 @@ if you used the right organism ID.", prefix = "\n", initial = ""))

# Add text colours to data
plot_input_heatmap <- plot_input_heatmap %>%
dplyr::mutate(text_col = ifelse(hcl[, "l"] > 50, "#000000", "#FFFFFF"))
dplyr::mutate(text_col = ifelse(hcl[, "l"] > 50, "#000000", "#FFFFFF")) %>%
# Add label names
dplyr::mutate(label = ifelse(!is.na(.data$n_significant_proteins_in_process), paste0(
.data$n_significant_proteins_in_process, "/",
.data$n_detected_proteins_in_process, " (",
round(.data$n_significant_proteins_in_process / .data$n_detected_proteins_in_process * 100), "%)"
),
NA
))

# Heatmap Plot
enrichment_plot <-
Expand All @@ -651,18 +672,15 @@ if you used the right organism ID.", prefix = "\n", initial = ""))
aes(
y = .data$term,
x = .data$grouping,
label = ifelse(!is.na(.data$n_significant_proteins_in_process), paste0(
.data$n_significant_proteins_in_process, "/",
.data$n_detected_proteins_in_process, " (",
round(.data$n_significant_proteins_in_process / .data$n_detected_proteins_in_process * 100), "%)"
),
NA
)
label = label
),
colour = plot_input_heatmap$text_col
colour = plot_input_heatmap$text_col,
size = label_size / .pt
)
}
} +
ggplot2::scale_x_discrete(expand = expansion(mult = c(0, 0))) +
ggplot2::scale_y_discrete(expand = expansion(mult = c(0, 0))) +
ggplot2::scale_fill_gradientn(colours = colours) +
ggplot2::labs(
title = plot_title,
Expand Down
4 changes: 4 additions & 0 deletions man/calculate_go_enrichment.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cc89485

Please sign in to comment.