diff --git a/R/functions.R b/R/functions.R index df437bb2..beb19282 100755 --- a/R/functions.R +++ b/R/functions.R @@ -321,7 +321,7 @@ get_differential_transcript_abundance_bulk <- function(.data, distinct() %>% # drop factors as it can affect design matrix - mutate_if(is.factor, as.character()) + droplevels() #%>% # # Check if data rectangular @@ -537,7 +537,7 @@ get_differential_transcript_abundance_bulk_voom <- function(.data, distinct() %>% # drop factors as it can affect design matrix - mutate_if(is.factor, as.character()) + droplevels() # %>% # # Check if data rectangular @@ -743,15 +743,7 @@ get_differential_transcript_abundance_deseq2 <- function(.data, distinct() %>% # drop factors as it can affect design matrix - mutate_if(is.factor, as.character()) %>% - - # # Check if data rectangular - # ifelse2_pipe( - # (.) %>% check_if_data_rectangular(!!.sample,!!.transcript,!!.abundance) %>% not() & fill_missing_values, - # (.) %>% check_if_data_rectangular(!!.sample,!!.transcript,!!.abundance) %>% not() & !fill_missing_values, - # ~ .x %>% fill_NA_using_formula(.formula,!!.sample, !!.transcript, !!.abundance), - # ~ .x %>% eliminate_sparse_transcripts(!!.transcript) - # ) %>% + droplevels() %>% # Needed for DESeq2 mutate(!!.abundance := as.integer(!!.abundance)) %>% @@ -770,25 +762,27 @@ get_differential_transcript_abundance_deseq2 <- function(.data, deseq2_object %>% # If I have multiple .contrasts merge the results - ifelse_pipe( - my_contrasts %>% is.null | omit_contrast_in_colnames, - + when( + # Simple comparison - ~ .x %>% - + (my_contrasts %>% is.null | omit_contrast_in_colnames) & (deseq2_object@colData[,parse_formula(.formula)[1]] %>% class %in% c("numeric", "integer", "double")) ~ + (.) %>% DESeq2::results() %>% - as_tibble(rownames = quo_name(.transcript)) - # %>% - - # # Mark DE genes - # mutate(significant = padj < significance_threshold) %>% - - # # Arrange - # arrange(padj), - - # Multiple comparisons + as_tibble(rownames = quo_name(.transcript)), + + # Simple comparison + my_contrasts %>% is.null | omit_contrast_in_colnames ~ + (.) %>% + DESeq2::results(contrast = c( + parse_formula(.formula)[1], + deseq2_object@colData[,parse_formula(.formula)[1]] %>% levels %>% .[2], + deseq2_object@colData[,parse_formula(.formula)[1]] %>% levels %>% .[1] + )) %>% + as_tibble(rownames = quo_name(.transcript)), + + # Multiple comparisons NOT USED AT THE MOMENT ~ { - deseq2_obj = .x + deseq2_obj = (.) 1:ncol(my_contrasts) %>% map_dfr( @@ -800,21 +794,13 @@ get_differential_transcript_abundance_deseq2 <- function(.data, # Convert to tibble as_tibble(rownames = quo_name(.transcript)) %>% mutate(constrast = colnames(my_contrasts)[.x]) - # %>% - # - # # Mark DE genes - # mutate(significant = padj < significance_threshold) + ) %>% pivot_wider(values_from = -c(!!.transcript, constrast), names_from = constrast, names_sep = "___") } ) %>% - # # Add filtering info - # right_join(.data %>% distinct(!!.transcript)) %>% - # when(!"lowly_abundant" %in% colnames(.data) ~ (.) %>% mutate(lowly_abundant = if_else(is.na(log2FoldChange), T, F)) , - # ~ (.)) %>% - # Attach prefix setNames(c( colnames(.)[1], @@ -827,6 +813,7 @@ get_differential_transcript_abundance_deseq2 <- function(.data, # Add raw object attach_to_internals(deseq2_object, "DESeq2") %>% + # Communicate the attribute added { message( diff --git a/tests/testthat/test-bulk_methods.R b/tests/testthat/test-bulk_methods.R index b02d28f9..a65cdf33 100755 --- a/tests/testthat/test-bulk_methods.R +++ b/tests/testthat/test-bulk_methods.R @@ -524,7 +524,7 @@ test_that("DESeq2 differential trancript abundance - no object",{ res_deseq2 = test_deseq2_df %>% DESeq2::DESeq() %>% - results() + DESeq2::results() res_tidybulk = test_deseq2_df %>% @@ -552,7 +552,7 @@ test_that("DESeq2 differential trancript abundance - no object",{ expect_equal( unique(res$log2FoldChange)[1:4], - c(-12.322037, -11.670005 , -9.048954 ,-12.603183), + c(3.449740, 2.459516, 2.433466, 1.951263), tolerance=1e-6 ) @@ -580,7 +580,7 @@ test_that("DESeq2 differential trancript abundance - no object",{ expect_equal( unique(res$log2FoldChange)[1:4], - c(-3.656244 ,-3.241215, -3.037658, 4.173217), + c(-1.1906895, -0.4422231, 0.9656122, -0.3328515), tolerance=1e-6 ) @@ -605,7 +605,7 @@ test_that("DESeq2 differential trancript abundance - no object",{ expect_equal( unique(res$log2FoldChange)[1:4], - c(-10.432623, -6.747017, -7.598174 , 6.598429), + c(1.1110210, 3.0077779, 0.9024256, 4.7259792), tolerance=1e-6 ) diff --git a/vignettes/manuscript_transcriptional_signatures.Rmd b/vignettes/manuscript_transcriptional_signatures.Rmd index 7a575474..b0d6b919 100644 --- a/vignettes/manuscript_transcriptional_signatures.Rmd +++ b/vignettes/manuscript_transcriptional_signatures.Rmd @@ -12,7 +12,7 @@ vignette: > %\usepackage[UTF-8]{inputenc} --- -This decument includes the code used for the manuscript, for the transcriptional signature identification. +This document includes the code used for the manuscript, for the transcriptional signature identification. ```{r, echo=FALSE, include=FALSE}