Skip to content

Commit

Permalink
Merge pull request #141 from MangiolaLaboratory/pass-foldchange
Browse files Browse the repository at this point in the history
update README and vignette
  • Loading branch information
stemangiola authored Jun 30, 2024
2 parents 50e0f8f + e16c1cf commit ff735d7
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 81 deletions.
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ S3method(sccomp_remove_unwanted_variation,sccomp_tbl)
S3method(sccomp_replicate,sccomp_tbl)
S3method(sccomp_test,sccomp_tbl)
S3method(simulate_data,tbl)
export(plot_1d_intervals)
export(plot_2d_intervals)
export(plot_1D_intervals)
export(plot_2D_intervals)
export(plot_summary)
export(sccomp_boxplot)
export(sccomp_estimate)
Expand Down
4 changes: 2 additions & 2 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -1589,10 +1589,10 @@ else {
}

# 1D intervals
plots$credible_intervals_1D = plot_1d_intervals(.data = x, .cell_group = !!.cell_group, significance_threshold = significance_threshold)
plots$credible_intervals_1D = plot_1D_intervals(.data = x, significance_threshold = significance_threshold)

# 2D intervals
if("v_effect" %in% colnames(x) && (x |> filter(!is.na(v_effect)) |> nrow()) > 0) plots$credible_intervals_2D = plot_2d_intervals(.data = x, .cell_group = !!.cell_group, significance_threshold = significance_threshold)
if("v_effect" %in% colnames(x) && (x |> filter(!is.na(v_effect)) |> nrow()) > 0) plots$credible_intervals_2D = plot_2D_intervals(.data = x, significance_threshold = significance_threshold)

plots

Expand Down
20 changes: 7 additions & 13 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -1706,7 +1706,6 @@ get_FDR = function(x){
#' This function creates a series of 1D interval plots for cell-group effects, highlighting significant differences based on a given significance threshold.
#'
#' @param .data Data frame containing the main data.
#' @param .cell_group The cell group to be analysed.
#' @param significance_threshold Numeric value specifying the significance threshold for highlighting differences. Default is 0.025.
#' @param test_composition_above_logit_fold_change A positive integer. It is the effect threshold used for the hypothesis test. A value of 0.2 correspond to a change in cell proportion of 10% for a cell type with baseline proportion of 50%. That is, a cell type goes from 45% to 50%. When the baseline proportion is closer to 0 or 1 this effect thrshold has consistent value in the logit uncontrained scale.
#' @importFrom patchwork wrap_plots
Expand All @@ -1718,15 +1717,15 @@ get_FDR = function(x){
#' @return A combined plot of 1D interval plots.
#' @examples
#' # Example usage:
#' # plot_1d_intervals(.data, "cell_group", 0.025, theme_minimal())
plot_1d_intervals = function(.data, .cell_group, significance_threshold = 0.05, test_composition_above_logit_fold_change = .data |> attr("test_composition_above_logit_fold_change")){
#' # plot_1D_intervals(.data, "cell_group", 0.025, theme_minimal())
plot_1D_intervals = function(.data, significance_threshold = 0.05, test_composition_above_logit_fold_change = .data |> attr("test_composition_above_logit_fold_change")){

# Define the variables as NULL to avoid CRAN NOTES
parameter <- NULL
estimate <- NULL
value <- NULL

.cell_group = enquo(.cell_group)
.cell_group = attr(.data, ".cell_group")

# Check if test have been done
if(.data |> select(ends_with("FDR")) |> ncol() |> equals(0))
Expand Down Expand Up @@ -1782,7 +1781,6 @@ plot_1d_intervals = function(.data, .cell_group, significance_threshold = 0.05,
#' This function creates a 2D interval plot for mean-variance association, highlighting significant differences based on a given significance threshold.
#'
#' @param .data Data frame containing the main data.
#' @param .cell_group The cell group to be analysed.
#' @param significance_threshold Numeric value specifying the significance threshold for highlighting differences. Default is 0.025.
#' @param test_composition_above_logit_fold_change A positive integer. It is the effect threshold used for the hypothesis test. A value of 0.2 correspond to a change in cell proportion of 10% for a cell type with baseline proportion of 50%. That is, a cell type goes from 45% to 50%. When the baseline proportion is closer to 0 or 1 this effect thrshold has consistent value in the logit uncontrained scale.
#'
Expand All @@ -1799,8 +1797,8 @@ plot_1d_intervals = function(.data, .cell_group, significance_threshold = 0.05,
#' @return A ggplot object representing the 2D interval plot.
#' @examples
#' # Example usage:
#' # plot_2d_intervals(.data, "cell_group", theme_minimal(), 0.025)
plot_2d_intervals = function(.data, .cell_group, significance_threshold = 0.05, test_composition_above_logit_fold_change = .data |> attr("test_composition_above_logit_fold_change")){
#' # plot_2D_intervals(.data, "cell_group", theme_minimal(), 0.025)
plot_2D_intervals = function(.data, significance_threshold = 0.05, test_composition_above_logit_fold_change = .data |> attr("test_composition_above_logit_fold_change")){

# Define the variables as NULL to avoid CRAN NOTES
v_effect <- NULL
Expand All @@ -1816,7 +1814,7 @@ plot_2d_intervals = function(.data, .cell_group, significance_threshold = 0.05,
multipanel_theme <- NULL


.cell_group = enquo(.cell_group)
.cell_group = attr(.data, ".cell_group")

# Check if test have been done
if(.data |> select(ends_with("FDR")) |> ncol() |> equals(0))
Expand Down Expand Up @@ -1875,11 +1873,7 @@ plot_2d_intervals = function(.data, .cell_group, significance_threshold = 0.05,
facet_wrap(~parameter, scales = "free") +

# Apply custom theme
multipanel_theme +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
multipanel_theme
}
}

Expand Down
71 changes: 37 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,6 @@ sccomp_result =

## Summary plots

``` r
plots = sccomp_result |> plot()
```

## Joining with `by = join_by(cell_group, sample)`
## Joining with `by = join_by(cell_group, type)`

A plot of group proportion, faceted by groups. The blue boxplots
represent the posterior predictive check. If the model is likely to be
descriptively adequate to the data, the blue box plot should roughly
Expand All @@ -129,12 +122,14 @@ represents the significant associations for composition and/or
variability.

``` r
plots$boxplot
sccomp_result |>
sccomp_boxplot(factor = "type")
```

## [[1]]
## Joining with `by = join_by(cell_group, sample)`
## Joining with `by = join_by(cell_group, type)`

![](inst/figures/unnamed-chunk-11-1.png)<!-- -->
![](inst/figures/unnamed-chunk-10-1.png)<!-- -->

A plot of estimates of differential composition (c\_) on the x-axis and
differential variability (v\_) on the y-axis. The error bars represent
Expand All @@ -144,10 +139,11 @@ significant if bigger than the minimal effect according to the 95%
credible interval. Facets represent the covariates in the model.

``` r
plots$credible_intervals_1D
sccomp_result |>
plot_1D_intervals()
```

![](inst/figures/unnamed-chunk-12-1.png)<!-- -->
![](inst/figures/unnamed-chunk-11-1.png)<!-- -->

We can plot the relationship between abundance and variability. As we
can see below, they are positively correlated, you also appreciate that
Expand All @@ -158,10 +154,17 @@ estimates of both the abundance and the variability. This shrinkage is
adaptive as it is modelled jointly, thanks for Bayesian inference.

``` r
plots$credible_intervals_2D
sccomp_result |>
plot_2D_intervals()
```

![](inst/figures/unnamed-chunk-13-1.png)<!-- -->
![](inst/figures/unnamed-chunk-12-1.png)<!-- -->

You can produce the series of plots calling the `plot` method.

``` r
sccomp_result |> plot()
```

## Contrasts

Expand All @@ -180,16 +183,16 @@ seurat_obj |>
## # A tibble: 60 × 18
## cell_group parameter factor c_lower c_effect c_upper c_pH0 c_FDR c_n_eff
## <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 B immature typecanc… <NA> -1.79 -1.24 -0.676 0 0 NA
## 2 B immature typeheal… <NA> 0.676 1.24 1.79 0 0 NA
## 3 B mem typecanc… <NA> -2.44 -1.78 -1.16 0 0 NA
## 4 B mem typeheal… <NA> 1.16 1.78 2.44 0 0 NA
## 5 CD4 cm S10… typecanc… <NA> -1.32 -0.927 -0.535 0 0 NA
## 6 CD4 cm S10… typeheal… <NA> 0.535 0.927 1.32 0 0 NA
## 7 CD4 cm hig… typecanc… <NA> 0.780 1.73 2.71 0.00100 1.67e-4 NA
## 8 CD4 cm hig… typeheal… <NA> -2.71 -1.73 -0.780 0.00100 1.67e-4 NA
## 9 CD4 cm rib… typecanc… <NA> 0.398 1.05 1.66 0.00200 5.00e-4 NA
## 10 CD4 cm rib… typeheal… <NA> -1.66 -1.05 -0.398 0.00200 5.00e-4 NA
## 1 B immature typecanc… <NA> -1.94 -1.40 -0.869 0 0 NA
## 2 B immature typeheal… <NA> 0.869 1.40 1.94 0 0 NA
## 3 B mem typecanc… <NA> -2.32 -1.80 -1.24 0 0 NA
## 4 B mem typeheal… <NA> 1.24 1.80 2.32 0 0 NA
## 5 CD4 cm S10… typecanc… <NA> -1.54 -1.10 -0.648 0 0 NA
## 6 CD4 cm S10… typeheal… <NA> 0.648 1.10 1.54 0 0 NA
## 7 CD4 cm hig… typecanc… <NA> 0.833 1.87 2.94 2.50e-4 5.00e-5 NA
## 8 CD4 cm hig… typeheal… <NA> -2.94 -1.87 -0.833 2.50e-4 5.00e-5 NA
## 9 CD4 cm rib… typecanc… <NA> 0.467 1.11 1.78 2.00e-3 5.23e-4 NA
## 10 CD4 cm rib… typeheal… <NA> -1.78 -1.11 -0.467 2.00e-3 5.23e-4 NA
## # ℹ 50 more rows
## # ℹ 9 more variables: c_R_k_hat <dbl>, v_lower <dbl>, v_effect <dbl>,
## # v_upper <dbl>, v_pH0 <dbl>, v_FDR <dbl>, v_n_eff <dbl>, v_R_k_hat <dbl>,
Expand Down Expand Up @@ -267,16 +270,16 @@ res
## # A tibble: 60 × 14
## cell_group parameter factor c_lower c_effect c_upper c_n_eff c_R_k_hat
## <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 B immature (Interce… <NA> 0.225 0.634 1.03 NaN 3.17
## 2 B immature typeheal… type 1.20 1.80 2.36 NaN 3.15
## 3 B mem (Interce… <NA> -1.26 -0.864 -0.466 NaN 3.14
## 4 B mem typeheal… type 1.26 1.81 2.46 NaN 3.14
## 5 CD4 cm S100A4 (Interce… <NA> 1.48 1.74 2.02 NaN 3.09
## 6 CD4 cm S100A4 typeheal… type 0.575 0.967 1.38 NaN 3.12
## 7 CD4 cm high cyto… (Interce… <NA> -1.17 -0.655 -0.182 NaN 3.03
## 8 CD4 cm high cyto… typeheal… type -1.97 -1.27 -0.610 NaN 3.20
## 9 CD4 cm ribosome (Interce… <NA> -0.0221 0.411 0.848 NaN 3.17
## 10 CD4 cm ribosome typeheal… type -1.60 -0.974 -0.420 NaN 3.20
## 1 B immature (Interce… <NA> 0.459 0.791 1.12 NaN 2.43
## 2 B immature typeheal… type 0.946 1.41 1.89 NaN 2.42
## 3 B mem (Interce… <NA> -1.25 -0.828 -0.401 NaN 2.51
## 4 B mem typeheal… type 1.33 1.95 2.56 NaN 2.51
## 5 CD4 cm S100A4 (Interce… <NA> 1.36 1.62 1.88 NaN 2.43
## 6 CD4 cm S100A4 typeheal… type 0.757 1.15 1.53 NaN 2.38
## 7 CD4 cm high cyto… (Interce… <NA> -1.08 -0.495 0.0909 NaN 2.44
## 8 CD4 cm high cyto… typeheal… type -2.32 -1.58 -0.852 NaN 2.45
## 9 CD4 cm ribosome (Interce… <NA> -0.0578 0.339 0.724 NaN 2.46
## 10 CD4 cm ribosome typeheal… type -1.57 -1.05 -0.516 NaN 2.43
## # ℹ 50 more rows
## # ℹ 6 more variables: v_lower <dbl>, v_effect <dbl>, v_upper <dbl>,
## # v_n_eff <dbl>, v_R_k_hat <dbl>, count_data <list>
Expand Down
Binary file modified inst/figures/unnamed-chunk-10-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified inst/figures/unnamed-chunk-11-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified inst/figures/unnamed-chunk-12-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified inst/figures/unnamed-chunk-17-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified inst/figures/unnamed-chunk-18-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified inst/figures/unnamed-chunk-19-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 12 additions & 6 deletions man/fragments/intro.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,19 @@ sccomp_result =

## Summary plots

```{r, out.height="200%"}
plots = sccomp_result |> plot()
```

A plot of group proportion, faceted by groups. The blue boxplots represent the posterior predictive check. If the model is likely to be descriptively adequate to the data, the blue box plot should roughly overlay with the black box plot, which represents the observed data. The outliers are coloured in red. A box plot will be returned for every (discrete) covariate present in `formula_composition`. The colour coding represents the significant associations for composition and/or variability.

```{r}
plots$boxplot
sccomp_result |>
sccomp_boxplot(factor = "type")
```

A plot of estimates of differential composition (c\_) on the x-axis and differential variability (v\_) on the y-axis. The error bars represent 95% credible intervals. The dashed lines represent the minimal effect that the hypothesis test is based on. An effect is labelled as significant if bigger than the minimal effect according to the 95% credible interval. Facets represent the covariates in the model.

```{r}
plots$credible_intervals_1D
sccomp_result |>
plot_1D_intervals()
```


Expand All @@ -127,7 +126,14 @@ We can plot the relationship between abundance and variability. As we can see be


```{r}
plots$credible_intervals_2D
sccomp_result |>
plot_2D_intervals()
```

You can produce the series of plots calling the `plot` method.

```{r, out.height="200%", eval=FALSE}
sccomp_result |> plot()
```


Expand Down
11 changes: 4 additions & 7 deletions man/plot_1d_intervals.Rd

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

11 changes: 4 additions & 7 deletions man/plot_2d_intervals.Rd

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

6 changes: 2 additions & 4 deletions tests/testthat/test-sccomp_.R
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,7 @@ test_that("plot_1d_intervals function works correctly", {

my_estimate |>
sccomp_test() |>
plot_1d_intervals(
.cell_group = cell_group,
plot_1D_intervals(
significance_threshold = 0.025
) |>
expect_s3_class("patchwork")
Expand All @@ -429,8 +428,7 @@ test_that("plot_1d_intervals function works correctly", {
test_that("plot_2d_intervals function works correctly", {
my_estimate |>
sccomp_test() |>
plot_2d_intervals(
.cell_group = cell_group,
plot_2D_intervals(
significance_threshold = 0.025
) |>
expect_s3_class("ggplot")
Expand Down
17 changes: 11 additions & 6 deletions vignettes/introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,19 @@ sccomp_result =

## Summary plots

```{r, out.height="200%"}
plots = sccomp_result |> plot()
```

A plot of group proportion, faceted by groups. The blue boxplots represent the posterior predictive check. If the model is likely to be descriptively adequate to the data, the blue box plot should roughly overlay with the black box plot, which represents the observed data. The outliers are coloured in red. A box plot will be returned for every (discrete) covariate present in `formula_composition`. The colour coding represents the significant associations for composition and/or variability.

```{r}
plots$boxplot
sccomp_result |>
sccomp_boxplot(factor = "type")
```

A plot of estimates of differential composition (c\_) on the x-axis and differential variability (v\_) on the y-axis. The error bars represent 95% credible intervals. The dashed lines represent the minimal effect that the hypothesis test is based on. An effect is labelled as significant if bigger than the minimal effect according to the 95% credible interval. Facets represent the covariates in the model.

```{r}
plots$credible_intervals_1D
sccomp_result |>
plot_1D_intervals()
```


Expand All @@ -136,9 +135,15 @@ We can plot the relationship between abundance and variability. As we can see be


```{r}
plots$credible_intervals_2D
sccomp_result |>
plot_2D_intervals()
```

You can produce the series of plots calling the `plot` method.

```{r, out.height="200%", eval=FALSE}
sccomp_result |> plot()
```

## Contrasts

Expand Down

0 comments on commit ff735d7

Please sign in to comment.