diff --git a/.gitignore b/.gitignore index 7bd206b..2e72980 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,9 @@ .Rproj.user +.Rhistory +.Rdata +.httr-oauth +.quarto +*.Rproj + experiments docs diff --git a/CHANGELOG.md b/CHANGELOG.md index 36d02f2..e35fd61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ * Render a pkgdown website (PR #13). +* Add `to_string()` and `print()` methods to the `Record` class and (incomplete) `describe()` method to the `Artifact()` class (PR #22) + ## MAJOR CHANGES * Refactored the internal class data structures for better modularity and extensibility (PR #8). diff --git a/DESCRIPTION b/DESCRIPTION index b834ce3..2209960 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: laminr Title: LaminDB interface in R -Version: 0.1.0.9000 +Version: 0.1.0.9001 Authors@R: c(person("Robrecht", "Cannoodt", role = c("aut", "cre"), diff --git a/R/Artifact.R b/R/Artifact.R index 10266cd..c602889 100644 --- a/R/Artifact.R +++ b/R/Artifact.R @@ -49,6 +49,55 @@ ArtifactRecord <- R6::R6Class( # nolint object_name_linter } else { cli_abort(paste0("Unsupported storage type: ", artifact_storage$type)) } + }, + describe = function(style = TRUE) { + provenance_fields <- c( + storage = "root", + transform = "name", + run = "started_at", + created_by = "handle", + test = "fail" + ) + + output_strings <- character() + + provenance_strings <- purrr::map_chr( + names(provenance_fields), + function(.field) { + field_name <- try(self[[.field]][[provenance_fields[.field]]]) + if (inherits(field_name, "try-error") || is.null(field_name)) { + return(NA_character_) + } + + if (is.character(field_name)) { + field_name <- paste0("'", field_name, "'") + } + + paste0( + cli::col_blue(" $", .field), + cli::col_br_blue(" = "), + cli::col_yellow(field_name) + ) + } + ) |> + purrr::discard(is.na) + + if (length(provenance_strings) > 0) { + output_strings <- c( + output_strings, + cli::style_italic(cli::col_br_magenta(" Provenance")), + provenance_strings + ) + } + + self$print(style) + for (.line in output_strings) { + if (isFALSE(style)) { + .line <- cli::ansi_strip(.line) + } + + cli::cat_line(.line) + } } ) ) diff --git a/R/Record.R b/R/Record.R index 2deb0c3..cfc9b1d 100644 --- a/R/Record.R +++ b/R/Record.R @@ -94,6 +94,62 @@ Record <- R6::R6Class( # nolint object_name_linter ) ) } + }, + print = function(style = TRUE) { + cli::cat_line(self$to_string(style)) + }, + to_string = function(style = FALSE) { + important_fields <- c( + "uid", + "handle", + "name", + "root", + "description", + "key" + ) + + record_fields <- private$.api$get_record( + module_name = private$.registry$module$name, + registry_name = private$.registry$name, + id_or_uid = private$.data[["uid"]], + include_foreign_keys = TRUE + ) + + # Get the important fields that are in the record + important_fields <- intersect(important_fields, names(record_fields)) + # Put important fields before all other fields + field_names <- c( + important_fields, setdiff(names(record_fields), important_fields) + ) + + field_strings <- purrr::map_chr(field_names, function(.name) { + value <- record_fields[[.name]] + + if (is.null(value)) { + return(NA_character_) + } + + if (is.character(value)) { + value <- paste0("'", value, "'") + } + + paste0( + cli::col_blue(.name), cli::col_br_blue("="), cli::col_yellow(value) + ) + }) |> + purrr::discard(is.na) + + string <- paste0( + cli::style_bold(cli::col_green(private$.registry$class_name)), "(", + paste(field_strings, collapse = ", "), + ")" + ) + + if (isFALSE(style)) { + string <- cli::ansi_strip(string) + } + + return(string) } ), private = list( diff --git a/README.md b/README.md index f10ba7b..4b61ce1 100644 --- a/README.md +++ b/README.md @@ -48,8 +48,11 @@ Get an artifact: ``` r artifact <- db$Artifact$get("KBW89Mf7IGcekja2hADu") +artifact ``` + Artifact(uid='KBW89Mf7IGcekja2hADu', description='Myeloid compartment', key='cell-census/2024-07-01/h5ads/fe52003e-1460-4a65-a213-2bb1a508332f.h5ad', version='2024-07-01', _accessor='AnnData', id=3659, transform_id=22, size=691757462, is_latest=TRUE, created_by_id=1, _hash_type='md5-n', type='dataset', created_at='2024-07-12T12:34:10.345829+00:00', n_observations=51552, updated_at='2024-07-12T12:40:48.837026+00:00', run_id=27, suffix='.h5ad', visibility=1, _key_is_virtual=FALSE, hash='SZ5tB0T4YKfiUuUkAL09ZA', storage_id=2) + Access some of its fields: ``` r diff --git a/README.qmd b/README.qmd index 2cba7d5..870e7de 100644 --- a/README.qmd +++ b/README.qmd @@ -49,6 +49,7 @@ Get an artifact: ```{r get_artifact} artifact <- db$Artifact$get("KBW89Mf7IGcekja2hADu") +artifact ``` Access some of its fields: