Skip to content

Commit c4ac82f

Browse files
authored
Merge pull request #94 from mikelove/master
Adding `mutate_features` and `mutate_samples`
2 parents 3f427dc + 7774163 commit c4ac82f

File tree

10 files changed

+127
-23
lines changed

10 files changed

+127
-23
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: tidySummarizedExperiment
33
Title: Brings SummarizedExperiment to the Tidyverse
4-
Version: 1.15.0
4+
Version: 1.15.1
55
Authors@R: c(person("Stefano", "Mangiola", email = "mangiolastefano@gmail.com",
66
role = c("aut", "cre")) )
77
Description: The tidySummarizedExperiment package provides a set of tools for creating and
@@ -54,7 +54,7 @@ Biarch: true
5454
biocViews: AssayDomain, Infrastructure, RNASeq, DifferentialExpression, GeneExpression, Normalization, Clustering, QualityControl, Sequencing, Transcription, Transcriptomics
5555
Encoding: UTF-8
5656
LazyData: true
57-
RoxygenNote: 7.2.3
57+
RoxygenNote: 7.3.1
5858
Roxygen: list(markdown = TRUE)
5959
LazyDataCompression: xz
6060
URL: https://github.com/stemangiola/tidySummarizedExperiment

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ S3method(tidy,SummarizedExperiment)
3838
S3method(unite,SummarizedExperiment)
3939
S3method(unnest,tidySummarizedExperiment_nested)
4040
export("%>%")
41+
export(mutate_features)
42+
export(mutate_samples)
4143
export(tidy)
4244
export(unnest_summarized_experiment)
4345
importFrom(S4Vectors,"metadata<-")

R/dplyr_methods.R

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,23 +350,23 @@ mutate.SummarizedExperiment <- function(.data, ...) {
350350
.data <- ping_old_special_column_into_metadata(.data)
351351
}
352352

353-
secial_columns <- get_special_columns(
353+
special_columns <- get_special_columns(
354354
# Decrease the size of the dataset
355355
.data[1:min(100, nrow(.data)), 1:min(20, ncol(.data))]
356356
) |> c(get_needed_columns(.data))
357357

358358
tst <-
359359
intersect(
360360
cols,
361-
secial_columns
361+
special_columns
362362
) |>
363363
length() |>
364364
gt(0)
365365

366366

367367
if (tst) {
368368
columns <-
369-
secial_columns |>
369+
special_columns |>
370370
paste(collapse=", ")
371371
stop(
372372
"tidySummarizedExperiment says:",
@@ -390,6 +390,46 @@ mutate.SummarizedExperiment <- function(.data, ...) {
390390
update_SE_from_tibble(.data)
391391
}
392392

393+
#' Mutate features
394+
#'
395+
#' Allows mutate call on features (rowData)
396+
#' of a SummarizedExperiment
397+
#'
398+
#' @param .data a SummarizedExperiment
399+
#' @param ... extra arguments passed to dplyr::mutate
400+
#'
401+
#' @return a SummarizedExperiment with modified rowData
402+
#'
403+
#' @export
404+
mutate_features <- function(.data, ...) {
405+
feature_info <- rowData(.data) |>
406+
tibble::as_tibble() |>
407+
dplyr::mutate(...) |>
408+
as("DataFrame")
409+
rowData(.data) <- feature_info
410+
return(.data)
411+
}
412+
413+
#' Mutate samples
414+
#'
415+
#' Allows mutate call on samples (colData)
416+
#' of a SummarizedExperiment
417+
#'
418+
#' @param .data a SummarizedExperiment
419+
#' @param ... extra arguments passed to dplyr::mutate
420+
#'
421+
#' @return a SummarizedExperiment with modified colData
422+
#'
423+
#' @export
424+
mutate_samples <- function(.data, ...) {
425+
sample_info <- colData(.data) |>
426+
tibble::as_tibble() |>
427+
dplyr::mutate(...) |>
428+
as("DataFrame")
429+
colData(.data) <- sample_info
430+
return(.data)
431+
}
432+
393433
#' @name rename
394434
#' @rdname rename
395435
#' @inherit dplyr::rename
@@ -434,23 +474,23 @@ rename.SummarizedExperiment <- function(.data, ...) {
434474
" Please run two `rename` commands for",
435475
" sample-wise and feature-wise columns.")
436476

437-
secial_columns <- get_special_columns(
477+
special_columns <- get_special_columns(
438478
# Decrease the size of the dataset
439479
.data[1:min(100, nrow(.data)), 1:min(20, ncol(.data))]
440480
) |> c(get_needed_columns(.data))
441481

442482
tst <-
443483
intersect(
444484
cols |> names(),
445-
secial_columns
485+
special_columns
446486
) |>
447487
length() |>
448488
gt(0)
449489

450490
# If column in view-only columns stop
451491
if (tst) {
452492
columns <-
453-
secial_columns |>
493+
special_columns |>
454494
paste(collapse=", ")
455495
stop(
456496
"tidySummarizedExperiment says:",

R/print_method.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#' @importFrom pillar get_extent
1414
#' @importFrom pillar style_subtle
1515
#' @importFrom pillar tbl_format_header
16+
#' @importFrom cli col_br_black
1617
#' @export
1718
tbl_format_header.tidySummarizedExperiment <- function(x, setup, ...) {
1819

@@ -32,12 +33,12 @@ tbl_format_header.tidySummarizedExperiment <- function(x, setup, ...) {
3233
named_header
3334
) %>%
3435
# Add further info single-cell
35-
append(sprintf(
36-
"\033[90m Features=%s | Samples=%s | Assays=%s\033[39m",
36+
append( cli::col_br_black( sprintf(
37+
" Features=%s | Samples=%s | Assays=",
3738
number_of_features,
3839
number_of_samples,
3940
assay_names %>% paste(collapse=", ")
40-
), after = 1)
41+
)), after = 1)
4142
}
4243
style_subtle(pillar___format_comment(header, width=setup$width))
4344
}

R/tidyr_methods.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,19 +362,19 @@ extract.SummarizedExperiment <- function(data, col,
362362
}
363363

364364

365-
secial_columns <- get_special_columns(
365+
special_columns <- get_special_columns(
366366
# Decrease the size of the dataset
367367
data[1:min(100, nrow(data)), 1:min(20, ncol(data))]
368368
) |>
369369
c(get_needed_columns(data))
370370

371-
tst <- intersect(quo_names(into), secial_columns) %>%
371+
tst <- intersect(quo_names(into), special_columns) %>%
372372
length() %>%
373373
gt(0) & remove
374374

375375

376376
if (tst) {
377-
columns <- secial_columns |> paste(collapse=", ")
377+
columns <- special_columns |> paste(collapse=", ")
378378
stop(
379379
"tidySummarizedExperiment says:",
380380
" you are trying to rename a column that is view only",
@@ -554,7 +554,7 @@ unite.SummarizedExperiment <- function(data, col, ...,
554554
data <- ping_old_special_column_into_metadata(data)
555555
}
556556

557-
secial_columns <- get_special_columns(
557+
special_columns <- get_special_columns(
558558
# Decrease the size of the dataset
559559
data[1:min(100, nrow(data)), 1:min(20, ncol(data))]
560560
) |>
@@ -563,13 +563,13 @@ unite.SummarizedExperiment <- function(data, col, ...,
563563
tst <-
564564
intersect(
565565
cols %>% quo_names(),
566-
secial_columns
566+
special_columns
567567
) %>%
568568
length() %>%
569569
gt(0) & remove
570570

571571
if (tst) {
572-
columns <- secial_columns |> paste(collapse=", ")
572+
columns <- special_columns |> paste(collapse=", ")
573573
stop(
574574
"tidySummarizedExperiment says:",
575575
" you are trying to rename a column that is view only",
@@ -656,7 +656,7 @@ separate.SummarizedExperiment <- function(data, col,
656656
data <- ping_old_special_column_into_metadata(data)
657657
}
658658

659-
secial_columns <- get_special_columns(
659+
special_columns <- get_special_columns(
660660
# Decrease the size of the dataset
661661
data[1:min(100, nrow(data)), 1:min(20, ncol(data))]
662662
) |>
@@ -665,13 +665,13 @@ separate.SummarizedExperiment <- function(data, col,
665665
tst <-
666666
intersect(
667667
cols %>% quo_names(),
668-
secial_columns
668+
special_columns
669669
) %>%
670670
length() %>%
671671
gt(0) & remove
672672

673673
if (tst) {
674-
columns <- secial_columns |> paste(collapse=", ")
674+
columns <- special_columns |> paste(collapse=", ")
675675
stop(
676676
"tidySummarizedExperiment says:",
677677
" you are trying to rename a column that is view only",

R/utilities.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ update_SE_from_tibble <- function(.data_mutated, se, column_belonging = NULL) {
377377
# where a unique value cannot be linked to sample or feature
378378
c(names(column_belonging[column_belonging == f_(se)$name]))
379379

380-
secial_columns <- get_special_columns(
380+
special_columns <- get_special_columns(
381381
# Decrease the size of the dataset
382382
se[1:min(100, nrow(se)), min(1, ncol(se)):min(20, ncol(se))]
383383
)
@@ -393,7 +393,7 @@ update_SE_from_tibble <- function(.data_mutated, se, column_belonging = NULL) {
393393
select_if(!colnames(.) %in% setdiff(colnames_col, s_(se)$name)) %>%
394394

395395
# Eliminate special columns that are read only. Assays
396-
select_if(!colnames(.) %in% secial_columns) %>%
396+
select_if(!colnames(.) %in% special_columns) %>%
397397
select_if(!colnames(.) %in% colnames_row) %>%
398398
# Replace for subset
399399
select(!!s_(se)$symbol, get_subset_columns(., !!s_(se)$symbol)) %>%
@@ -436,7 +436,7 @@ update_SE_from_tibble <- function(.data_mutated, se, column_belonging = NULL) {
436436
.data_mutated %>%
437437

438438
# Eliminate special columns that are read only
439-
select_if(!colnames(.) %in% secial_columns) %>%
439+
select_if(!colnames(.) %in% special_columns) %>%
440440

441441
#eliminate sample columns directly
442442
select_if(!colnames(.) %in% c(s_(se)$name, colnames(col_data))) %>%

man/ggplot.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.

man/mutate_features.Rd

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

man/mutate_samples.Rd

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

tests/testthat/test-dplyr_methods.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,21 @@ test_that("group_split splits with mutliple arguments", {
205205
length() |>
206206
expect_equal(4)
207207
})
208+
209+
test_that("mutate features", {
210+
pasilla %>%
211+
mutate_features(new = 1:nrow(pasilla)) %>%
212+
rowData() %>%
213+
as_tibble() %>%
214+
pull(new) %>%
215+
expect_equal(1:nrow(pasilla))
216+
})
217+
218+
test_that("mutate samples", {
219+
pasilla %>%
220+
mutate_samples(new = 1:ncol(pasilla)) %>%
221+
colData() %>%
222+
as_tibble() %>%
223+
pull(new) %>%
224+
expect_equal(1:ncol(pasilla))
225+
})

0 commit comments

Comments
 (0)