Skip to content

Commit 978e3a6

Browse files
committed
Bug fixed: covariates indication, factor column error
1 parent e156756 commit 978e3a6

File tree

6 files changed

+131
-47
lines changed

6 files changed

+131
-47
lines changed

NAMESPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ importFrom(SummarizedExperiment,colData)
1919
importFrom(SummarizedExperiment,rowData)
2020
importFrom(SummarizedExperiment,rowRanges)
2121
importFrom(cli,col_br_black)
22+
importFrom(cli,col_br_blue)
23+
importFrom(dplyr,across)
2224
importFrom(dplyr,full_join)
2325
importFrom(dplyr,if_else)
2426
importFrom(dplyr,left_join)
27+
importFrom(dplyr,mutate)
2528
importFrom(dplyr,select)
2629
importFrom(fansi,strwrap_ctl)
30+
importFrom(magrittr,"%>%")
2731
importFrom(magrittr,`%>%`)
2832
importFrom(methods,setMethod)
2933
importFrom(pillar,align)
@@ -37,6 +41,7 @@ importFrom(pillar,pillar_component)
3741
importFrom(pillar,style_subtle)
3842
importFrom(pillar,tbl_format_header)
3943
importFrom(pkgconfig,get_config)
44+
importFrom(purrr,keep)
4045
importFrom(purrr,map)
4146
importFrom(purrr,map2)
4247
importFrom(purrr,map2_chr)
@@ -47,6 +52,7 @@ importFrom(purrr,when)
4752
importFrom(rlang,enquo)
4853
importFrom(rlang,names2)
4954
importFrom(stats,setNames)
55+
importFrom(stringr,str_locate_all)
5056
importFrom(stringr,str_replace)
5157
importFrom(stringr,str_replace_all)
5258
importFrom(tibble,as_tibble)

R/pillar_utlis.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ pillar___strwrap2 <- function (x, width, indent, strip.spaces = TRUE)
1919

2020
pillar___wrap <- function (..., indent=0, prefix="", width, strip.spaces = TRUE)
2121
{
22-
22+
2323
x <- paste0(..., collapse="")
24-
wrapped <- pillar___strwrap2(x, width - get_extent(prefix), indent, strip.spaces = strip.spaces)
24+
wrapped <- pillar___strwrap2(x, width, indent, strip.spaces = strip.spaces)
2525
wrapped <- paste0(prefix, wrapped)
2626
wrapped <- gsub(NBSP, " ", wrapped)
2727
paste0(wrapped, collapse="\n")

R/print_methods.R

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
#' @importFrom SummarizedExperiment assayNames assays rowData assays<- rowRanges
77
#' @importFrom stats setNames
88
#' @importFrom S4Vectors coolcat
9-
#' @importFrom purrr when map_chr
9+
#' @importFrom purrr when map_chr keep
1010
#' @importFrom stringr str_replace
1111
#' @importFrom magrittr `%>%`
12-
#' @importFrom dplyr if_else
12+
#' @importFrom dplyr if_else mutate across
1313
#' @export
1414
print.SummarizedExperiment <- function(x, design = 4, n_print = 10, ...) {
1515

@@ -97,7 +97,7 @@ but they do not completely overlap.")
9797
~ .[, 1:min(20, ncol(x)), drop=FALSE]
9898
) %>%
9999
as_tibble()
100-
100+
101101
my_tibble |>
102102
vctrs::new_data_frame(class=c("tidySummarizedExperiment", "tbl")) %>%
103103
add_attr(nrow(x), "number_of_features") %>%
@@ -246,12 +246,20 @@ but they do not completely overlap.")
246246
max_width <- max(nchar(as.character(col)), na.rm = TRUE) # Get max width in the column
247247
paste(rep("-", max_width), collapse = "") # Generate a separator of the same length
248248
})
249+
249250
# Modify the entire tibble to include a separator row across all columns
251+
## temporalily convert factor cols to char
252+
fct_col = map(out_sub, is.factor) %>% keep(~{.x == T}) %>% names
253+
if (length(fct_col)) out_sub[, fct_col] = out_sub[, fct_col] %>% mutate(across(all_of(fct_col), as.character))
254+
255+
250256
out_sub <- suppressWarnings(rbind(
251257
out_sub[seq_len(top_n),],
252258
as.list(separator_row), # Adaptive separator row
253259
out_sub[(top_n+1):nrow(out_sub), ]
254260
))
261+
## reverse to factor cols
262+
if (length(fct_col)) out_sub[, fct_col] = out_sub[, fct_col] %>% mutate(across(all_of(fct_col), as.factor))
255263

256264
# attr(out_sub, "n") <- n
257265
# attr(out_sub, "total_rows") <- x %>% dim %>% {(.)[1] * (.)[2]}
@@ -265,15 +273,15 @@ but they do not completely overlap.")
265273
add_attr(nrow(x), "number_of_features") %>%
266274
add_attr(ncol(x), "number_of_samples") %>%
267275
add_attr(assays(x) %>% names, "assay_names") %>%
268-
add_attr(separator_row, "separator_row") |>
269-
add_attr(names(col_), "covariate_names") |>
270-
271-
add_attr(
272-
# Get the actual column names that will be printed on screen
273-
# This uses tibble's internal method to determine visible columns
274-
pillar::tbl_format_setup(out_sub, width = getOption("width", 80) + 4)$body[1] |> as.character(),
275-
"printed_colnames"
276-
) %>%
276+
add_attr(separator_row, "separator_row") |>
277+
add_attr(names(col_), "covariate_names") |>
278+
279+
# add_attr(
280+
# # Get the actual column names that will be printed on screen
281+
# # This uses tibble's internal method to determine visible columns
282+
# pillar::tbl_format_setup(out_sub, width = getOption("width", 80) + 4)$body[1] |> as.character(),
283+
# "printed_colnames"
284+
# ) %>%
277285
add_attr(
278286
'' %>%
279287
setNames("A SummarizedExperiment-tibble abstraction"),
@@ -286,7 +294,7 @@ but they do not completely overlap.")
286294
invisible(x)
287295
}
288296

289-
297+
290298
print_tidyprint_1(x, ...)
291299
invisible(x)
292300

R/tidyprint_1_utlis.R

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ ctl_new_pillar.SE_print_abstraction <- function(controller, x, width, ..., title
8585

8686

8787
#' Format covariate header by distributing label across covariate columns
88-
#'
88+
#'
8989
#' @importFrom rlang names2
9090
#' @importFrom pillar align
9191
#' @importFrom pillar get_extent
@@ -95,7 +95,7 @@ ctl_new_pillar.SE_print_abstraction <- function(controller, x, width, ..., title
9595
#' @importFrom tibble as_tibble
9696
#' @importFrom stringr str_replace_all
9797
#' @importFrom purrr map2_chr
98-
#'
98+
#'
9999
#' @param separator_row The separator row with column widths
100100
#' @param printed_colnames The printed column names
101101
#' @param covariate_names The names of covariate columns
@@ -120,10 +120,10 @@ format_covariate_header <- function(separator_row, printed_colnames, covariate_n
120120
label,
121121
paste(rep("-", right_pad), collapse = "")
122122
)
123-
123+
124124
# Add '|' at the beginning and end
125125
merged_label <- paste0("|", merged_label, "|")
126-
126+
127127
# Guarantee the merged_label is exactly total_covariate_width + 2
128128
merged_label <- substr(merged_label, 1, total_covariate_width + 2)
129129

@@ -133,64 +133,84 @@ format_covariate_header <- function(separator_row, printed_colnames, covariate_n
133133

134134
# remove the other covariate columns
135135
header_row[covariate_indices[-1]] <- ""
136-
136+
137137
# Add row ID spacing at the beginning
138138
header_row <- c(paste(rep(" ", number_of_total_rows |> nchar() - 4), collapse = ""), header_row)
139139

140140
# Step 2: Collapse everything with space
141141
paste(header_row, collapse = " ")
142142

143-
143+
144144
}
145145

146+
#' Custom header for SE_print_abstraction tibbles
147+
#'
148+
#' Draws a banner aligned to the first rendered body line, and prints a
149+
#' one-line summary with feature/sample/assay counts.
150+
#'
151+
#' @importFrom pillar tbl_format_header align style_subtle
152+
#' @importFrom cli col_br_blue col_br_black
153+
#' @importFrom stringr str_locate_all
154+
#' @importFrom rlang names2
155+
#' @importFrom magrittr %>%
146156
#' @export
147157
tbl_format_header.SE_print_abstraction <- function(x, setup, ...) {
158+
148159
number_of_features <- x |> attr("number_of_features")
149160
number_of_samples <- x |> attr("number_of_samples")
150161
named_header <- x |> attr("named_header")
151162
assay_names <- x |> attr("assay_names")
152163
separator_row <- x |> attr("separator_row")
153164
covariate_names <- x |> attr("covariate_names")
154-
165+
166+
x<<-x
167+
168+
155169
number_of_total_rows = (x |> attr("number_of_features")) * (x |> attr("number_of_samples"))
156-
157-
printed_colnames <- x |> attr("printed_colnames")
158-
170+
171+
# printed_colnames <- x |> attr("printed_colnames")
172+
printed_colnames <- pillar::tbl_format_setup(x)$body[1] |> as.character()
173+
159174
# Find the positions of all '|' characters in the string
160175
pipe_positions <- stringr::str_locate_all(printed_colnames, "\\|")[[1]][, "start"]
161-
176+
162177
# Calculate character length to the start of the second '|'
163-
chars_to_second_pipe <- pipe_positions[2] - 2
164-
178+
chars_to_second_pipe <- pipe_positions[2] - 3
179+
165180
# Check if there's a third pipe
166181
if (length(pipe_positions) >= 3) {
167182
# Calculate character length between second and third pipe
168-
chars_to_third_pipe <- pipe_positions[3] - pipe_positions[2] - 2
183+
chars_to_third_pipe <- pipe_positions[3] - pipe_positions[2] -1
169184
} else {
170185
# Calculate character length to the end of the line
171-
chars_to_third_pipe <- nchar(printed_colnames) - pipe_positions[2]
186+
chars_to_third_pipe <- nchar(printed_colnames) - pipe_positions[2] -1
172187
}
173188

174189
label = " COVARIATES "
175190
label_length <- nchar(label)
176191

177192
# Center the label in the total covariate width, using only dashes and the label
178193
left_pad <- floor((chars_to_third_pipe - label_length) / 2)
179-
right_pad <- chars_to_third_pipe - label_length - left_pad
180-
merged_label <- paste0(
181-
paste(rep("-", left_pad), collapse = ""),
182-
label,
183-
paste(rep("-", right_pad), collapse = "")
184-
)
185-
186-
# Add '|' at the beginning and end
187-
merged_label <- paste0("|", merged_label, "|")
188-
189-
# Pad with the spaces until chars to second pipe
190-
merged_label <- c(paste(rep(" ", chars_to_second_pipe), collapse = ""), merged_label) |>
191-
paste0(collapse = "")
194+
right_pad <- chars_to_third_pipe - label_length - left_pad -1
195+
196+
if (left_pad >0 & right_pad >0){
197+
merged_label <- paste0(
198+
paste(rep("-", left_pad), collapse = ""),
199+
label,
200+
paste(rep("-", right_pad), collapse = "")
201+
)
202+
203+
# Add '|' at the beginning and end
204+
merged_label <- paste0("|-", merged_label, "|")
192205

193-
covariate_header <- cli::col_br_blue(merged_label)
206+
# Pad with the spaces until chars to second pipe
207+
merged_label <- c(paste(rep(" ", chars_to_second_pipe), collapse = ""), merged_label) |>
208+
paste0(collapse = "")
209+
210+
covariate_header <- cli::col_br_blue(merged_label)
211+
}else{
212+
covariate_header = NULL
213+
}
194214

195215

196216
# Compose the main header as before
@@ -217,8 +237,10 @@ tbl_format_header.SE_print_abstraction <- function(x, setup, ...) {
217237
if (!is.null(covariate_header)) {
218238
header <- c(header, covariate_header)
219239
}
220-
240+
241+
221242
style_subtle(pillar___format_comment(header, width=setup$width, strip.spaces = FALSE))
243+
222244
}
223245

224246
# type_sum.sep <- function(x, ...) {

R/tidyse_utils.R

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,42 @@
44
#' @importFrom dplyr full_join
55
# This file is a replacement of the unexported functions in the tibble
66
# package, in order to specify "tibble abstraction in the header"
7+
#' @importFrom rlang names2
8+
#' @importFrom pillar align
9+
#' @importFrom pillar get_extent
10+
#' @importFrom pillar style_subtle
11+
#' @importFrom pillar tbl_format_header
12+
#' @importFrom cli col_br_black
13+
#' @importFrom tibble as_tibble
14+
#' @export
15+
tbl_format_header.tidySummarizedExperiment <- function(x, setup, ...) {
16+
17+
number_of_features <- x |> attr("number_of_features")
18+
number_of_samples <- x |> attr("number_of_samples")
19+
named_header <- x |> attr("named_header")
20+
assay_names <- x |> attr("assay_names")
21+
22+
23+
if (all(names2(named_header) == "")) {
24+
header <- named_header
25+
} else {
26+
header <-
27+
paste0(
28+
align(paste0(names2(named_header), ":"), space=NBSP),
29+
" ",
30+
named_header
31+
) %>%
32+
# Add further info single-cell
33+
append( cli::col_br_black( sprintf(
34+
" Features=%s | Samples=%s | Assays=%s",
35+
number_of_features,
36+
number_of_samples,
37+
assay_names %>% paste(collapse=", ")
38+
)), after = 1)
39+
}
40+
style_subtle(pillar___format_comment(header, width=setup$width))
41+
}
42+
743
check_if_assays_are_NOT_overlapped <- function(se, dim = "cols") {
844

945
stopifnot(dim %in% c("rows", "cols"))
@@ -227,9 +263,9 @@ add_attr <- function(var, attribute, name) {
227263
eliminate_GRanges_metadata_columns_also_present_in_Rowdata <- function(.my_data, se) {
228264
.my_data %>%
229265
select(-one_of(colnames(rowData(se)))) %>%
230-
266+
231267
# In case there is not metadata column
232-
suppressWarnings()
268+
suppressWarnings()
233269
}
234270

235271
get_special_datasets <- function(se) {

man/tbl_format_header.SE_print_abstraction.Rd

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

0 commit comments

Comments
 (0)