From 6f12493db40ce30236a67fddeb024e5f044182fb Mon Sep 17 00:00:00 2001 From: jpquast Date: Mon, 30 Sep 2024 18:57:38 +0200 Subject: [PATCH] Fix some issues --- NAMESPACE | 3 --- R/assign_peptide_type.R | 6 +++--- R/qc_cvs.R | 2 +- R/try_query.R | 12 ++++++------ man/assign_peptide_type.Rd | 2 +- tests/testthat/test-auxiliary_functions.R | 18 +++++++++--------- 6 files changed, 20 insertions(+), 23 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index d81a3a5d..e4e57730 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -116,7 +116,6 @@ importFrom(httr,modify_url) importFrom(httr,timeout) importFrom(janitor,clean_names) importFrom(janitor,make_clean_names) -importFrom(jsonlite,fromJSON) importFrom(magrittr,"%>%") importFrom(methods,is) importFrom(plotly,ggplotly) @@ -193,5 +192,3 @@ importFrom(utils,data) importFrom(utils,download.file) importFrom(utils,head) importFrom(utils,untar) -importFrom(xml2,read_html) -importFrom(xml2,read_xml) diff --git a/R/assign_peptide_type.R b/R/assign_peptide_type.R index 26601581..defe7360 100644 --- a/R/assign_peptide_type.R +++ b/R/assign_peptide_type.R @@ -94,13 +94,13 @@ assign_peptide_type <- function(data, )) %>% # Reassign semi-tryptic peptides at position 2 to fully-tryptic if no start == 1 dplyr::mutate(pep_type = dplyr::if_else( - pep_type == "semi-tryptic" & {{ start }} == 2 & !.data$has_start_1 & .data$C_term_tryp, + .data$pep_type == "semi-tryptic" & {{ start }} == 2 & !.data$has_start_1 & .data$C_term_tryp, "fully-tryptic", .data$pep_type )) %>% # Reassign non-tryptic peptides at position 2 to semi-tryptic if no start == 1 dplyr::mutate(pep_type = dplyr::if_else( - pep_type == "non-tryptic" & {{ start }} == 2 & !.data$has_start_1 & !.data$C_term_tryp, + .data$pep_type == "non-tryptic" & {{ start }} == 2 & !.data$has_start_1 & !.data$C_term_tryp, "fully-tryptic", .data$pep_type )) %>% @@ -111,7 +111,7 @@ assign_peptide_type <- function(data, result <- data %>% dplyr::left_join( peptide_data %>% - dplyr::select({{ aa_before }}, {{ last_aa }}, {{ aa_after }}, {{ protein_id }}, {{ start }}, .data$pep_type), + dplyr::select({{ aa_before }}, {{ last_aa }}, {{ aa_after }}, {{ protein_id }}, {{ start }}, "pep_type"), by = c( rlang::as_name(rlang::enquo(aa_before)), rlang::as_name(rlang::enquo(last_aa)), diff --git a/R/qc_cvs.R b/R/qc_cvs.R index 713303d3..3ffa1ff2 100644 --- a/R/qc_cvs.R +++ b/R/qc_cvs.R @@ -122,7 +122,7 @@ The function does not handle log2 transformed data.", dplyr::mutate({{ condition }} := forcats::fct_expand({{ condition }}, "combined")) %>% dplyr::mutate({{ condition }} := replace({{ condition }}, .data$type == "cv_combined", "combined")) %>% dplyr::mutate({{ condition }} := forcats::fct_relevel({{ condition }}, "combined")) %>% - dplyr::select(-.data$type) %>% + dplyr::select(-"type") %>% dplyr::group_by({{ condition }}) %>% dplyr::mutate(median = stats::median(.data$values)) %>% dplyr::distinct() diff --git a/R/try_query.R b/R/try_query.R index 20a4008d..ad72c9ca 100644 --- a/R/try_query.R +++ b/R/try_query.R @@ -22,8 +22,6 @@ #' @importFrom curl has_internet #' @importFrom httr GET timeout http_error message_for_status http_status content accept #' @importFrom readr read_tsv read_csv -#' @importFrom jsonlite fromJSON -#' @importFrom xml2 read_html read_xml #' #' @return A data frame that contains the table from the url. try_query <- @@ -90,11 +88,13 @@ try_query <- # Change variable to not show progress if readr is used options(readr.show_progress = FALSE) - # Check if the content is gzip compressed - if (!is.null(query_result$headers[["content-encoding"]]) && query_result$headers[["content-encoding"]] == "gzip") { - # Retrieve the content as raw bytes using httr::content - raw_content <- httr::content(query_result, type = "raw") + # Retrieve the content as raw bytes using httr::content + raw_content <- httr::content(query_result, type = "raw") + # Check for gzip magic number (1f 8b) before decompression + compressed <- length(raw_content) >= 2 && raw_content[1] == as.raw(0x1f) && raw_content[2] == as.raw(0x8b) + # Check if the content is gzip compressed + if (!is.null(query_result$headers[["content-encoding"]]) && query_result$headers[["content-encoding"]] == "gzip" && compressed) { # Decompress the raw content using base R's `memDecompress` decompressed_content <- memDecompress(raw_content, type = "gzip") diff --git a/man/assign_peptide_type.Rd b/man/assign_peptide_type.Rd index 0a8e54e0..baff1587 100644 --- a/man/assign_peptide_type.Rd +++ b/man/assign_peptide_type.Rd @@ -54,7 +54,7 @@ data <- data.frame( aa_after = c("T", "R", "T", "R", "T", "R", "T"), protein_id = c("P1", "P1", "P3", "P3", "P2", "P2", "P2"), start = c(38, 2, 1, 2, 10, 2, 1) - ) +) assign_peptide_type(data, aa_before, last_aa, aa_after, protein_id, start) } diff --git a/tests/testthat/test-auxiliary_functions.R b/tests/testthat/test-auxiliary_functions.R index 00a4973e..4c762dc8 100644 --- a/tests/testthat/test-auxiliary_functions.R +++ b/tests/testthat/test-auxiliary_functions.R @@ -38,7 +38,7 @@ if (Sys.getenv("TEST_PROTTI") == "true") { last_aa = last_aa, aa_after = aa_after, protein_id = protein_id, - start_pos = start + start = start )) }) expect_is(assigned_types, "data.frame") @@ -61,9 +61,9 @@ if (Sys.getenv("TEST_PROTTI") == "true") { test_that("find_peptide and assign_peptide_type work", { expect_is(assigned_types, "data.frame") - expect_equal(nrow(assigned_types), 3) + expect_equal(nrow(assigned_types), 6) expect_equal(ncol(assigned_types), 9) - expect_equal(assigned_types$pep_type, c("fully-tryptic", "fully-tryptic", "non-tryptic")) + expect_equal(assigned_types$pep_type, c("fully-tryptic", "semi-tryptic", "non-tryptic", "non-tryptic", "fully-tryptic", "fully-tryptic")) }) test_that("deprecated sequence_coverage works", { @@ -75,9 +75,9 @@ if (Sys.getenv("TEST_PROTTI") == "true") { )) }) expect_is(coverage, "data.frame") - expect_equal(nrow(coverage), 3) + expect_equal(nrow(coverage), 6) expect_equal(ncol(coverage), 10) - expect_equal(unique(round(coverage$coverage, digits = 1)), 7.7) + expect_equal(unique(round(coverage$coverage, digits = 1)), c(7.7, 3.2)) }) coverage <- calculate_sequence_coverage( @@ -88,14 +88,14 @@ if (Sys.getenv("TEST_PROTTI") == "true") { test_that("calculate_sequence_coverage works", { expect_is(coverage, "data.frame") - expect_equal(nrow(coverage), 3) + expect_equal(nrow(coverage), 6) expect_equal(ncol(coverage), 10) - expect_equal(unique(round(coverage$coverage, digits = 1)), 7.7) + expect_equal(unique(round(coverage$coverage, digits = 1)), c(7.7, 3.2)) }) plot_data <- coverage %>% dplyr::mutate( - fold_change = c(3, -0.4, 2.1), + fold_change = c(3, -0.4, 2.1, 0.1, -0.1, 0.2), protein_length = nchar(protein_sequence) ) @@ -121,7 +121,7 @@ if (Sys.getenv("TEST_PROTTI") == "true") { end_position = end, protein_length = protein_length, coverage = coverage, - protein_id = protein_id, + facet = protein_id, colouring = pep_type ) expect_is(p, "ggplot")