From 638d1b0a44246e3f7198c4de0d8b87faa5f2416e Mon Sep 17 00:00:00 2001 From: Ally Hawkins <54039191+allyhawkins@users.noreply.github.com> Date: Mon, 22 Jan 2024 09:48:06 -0600 Subject: [PATCH 1/5] make sure sample metadata to coldata doesn't happen with cellhash --- bin/sce_to_anndata.R | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/bin/sce_to_anndata.R b/bin/sce_to_anndata.R index 364feca8..13fb12d8 100755 --- a/bin/sce_to_anndata.R +++ b/bin/sce_to_anndata.R @@ -63,11 +63,14 @@ format_czi <- function(sce) { # need this column to join in the sample metadata with the colData sce$library_id <- metadata(sce)$library_id - # add sample metadata to colData sce - sce <- scpcaTools::metadata_to_coldata( - sce, - join_columns = "library_id" - ) + # only move sample metadata if not a multiplexed library + if (!("cellhash" %in% altExpNames(sce))) { + # add sample metadata to colData sce + sce <- scpcaTools::metadata_to_coldata( + sce, + join_columns = "library_id" + ) + } # modify colData to be AnnData and CZI compliant coldata_df <- colData(sce) |> @@ -125,7 +128,7 @@ scpcaTools::sce_to_anndata( # AltExp to AnnData ----------------------------------------------------------- # if feature data exists, grab it and export to AnnData -if (!is.null(opt$feature_name)) { +if (!is.null(opt$feature_name) & (opt$feature_name != "cellhash")) { # make sure the feature data is present if (!(opt$feature_name %in% altExpNames(sce))) { stop("feature_name must match name of altExp in provided SCE object.") From a42ea3c87fa02662c7e59528715399dd0341043d Mon Sep 17 00:00:00 2001 From: Ally Hawkins <54039191+allyhawkins@users.noreply.github.com> Date: Mon, 22 Jan 2024 11:17:32 -0600 Subject: [PATCH 2/5] reorganize if statement for cellhash altexp --- bin/sce_to_anndata.R | 68 ++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/bin/sce_to_anndata.R b/bin/sce_to_anndata.R index 13fb12d8..87693911 100755 --- a/bin/sce_to_anndata.R +++ b/bin/sce_to_anndata.R @@ -128,40 +128,46 @@ scpcaTools::sce_to_anndata( # AltExp to AnnData ----------------------------------------------------------- # if feature data exists, grab it and export to AnnData -if (!is.null(opt$feature_name) & (opt$feature_name != "cellhash")) { - # make sure the feature data is present - if (!(opt$feature_name %in% altExpNames(sce))) { - stop("feature_name must match name of altExp in provided SCE object.") - } - - # check for output file - if (!(stringr::str_ends(opt$output_feature_h5, ".hdf5|.h5"))) { - stop("output feature file name must end in .hdf5 or .h5") - } - - # extract altExp - alt_sce <- altExp(sce, opt$feature_name) - - # only convert altExp with > 1 rows - if (nrow(alt_sce) > 1) { - # add sample metadata from main sce to alt sce metadata - metadata(alt_sce)$sample_metadata <- sample_metadata - - # make sce czi compliant - alt_sce <- format_czi(alt_sce) - - # export altExp sce as anndata object - scpcaTools::sce_to_anndata( - alt_sce, - anndata_file = opt$output_feature_h5 - ) +if (!is.null(opt$feature_name)) { + # if the feature name is cell hash, skip conversion + if (opt$feature_name != "cellhash") { + warning("Conversion of altExp data from multiplexed data is not supported. + The altExp will not be converted.") } else { - # warn that the altExp cannot be converted - message( - glue::glue(" + # make sure the feature data is present + if (!(opt$feature_name %in% altExpNames(sce))) { + stop("feature_name must match name of altExp in provided SCE object.") + } + + # check for output file + if (!(stringr::str_ends(opt$output_feature_h5, ".hdf5|.h5"))) { + stop("output feature file name must end in .hdf5 or .h5") + } + + # extract altExp + alt_sce <- altExp(sce, opt$feature_name) + + # only convert altExp with > 1 rows + if (nrow(alt_sce) > 1) { + # add sample metadata from main sce to alt sce metadata + metadata(alt_sce)$sample_metadata <- sample_metadata + + # make sce czi compliant + alt_sce <- format_czi(alt_sce) + + # export altExp sce as anndata object + scpcaTools::sce_to_anndata( + alt_sce, + anndata_file = opt$output_feature_h5 + ) + } else { + # warn that the altExp cannot be converted + message( + glue::glue(" Only 1 row found in altExp named: {opt$feature_name}. This altExp will not be converted to an AnnData object. ") - ) + ) + } } } From 60961fb1b5fc8d63ec4c8a57c69ff611eafcc46b Mon Sep 17 00:00:00 2001 From: Ally Hawkins <54039191+allyhawkins@users.noreply.github.com> Date: Tue, 23 Jan 2024 13:54:21 -0600 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: Joshua Shapiro --- bin/sce_to_anndata.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/sce_to_anndata.R b/bin/sce_to_anndata.R index 87693911..c7858ba9 100755 --- a/bin/sce_to_anndata.R +++ b/bin/sce_to_anndata.R @@ -130,7 +130,7 @@ scpcaTools::sce_to_anndata( # if feature data exists, grab it and export to AnnData if (!is.null(opt$feature_name)) { # if the feature name is cell hash, skip conversion - if (opt$feature_name != "cellhash") { + if (opt$feature_name == "cellhash") { warning("Conversion of altExp data from multiplexed data is not supported. The altExp will not be converted.") } else { From ddf51e107cfedf764b55d23889d4709038ee211d Mon Sep 17 00:00:00 2001 From: Ally Hawkins <54039191+allyhawkins@users.noreply.github.com> Date: Tue, 23 Jan 2024 13:58:00 -0600 Subject: [PATCH 4/5] move feature name check --- bin/sce_to_anndata.R | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/bin/sce_to_anndata.R b/bin/sce_to_anndata.R index c7858ba9..e2593958 100755 --- a/bin/sce_to_anndata.R +++ b/bin/sce_to_anndata.R @@ -129,16 +129,17 @@ scpcaTools::sce_to_anndata( # if feature data exists, grab it and export to AnnData if (!is.null(opt$feature_name)) { - # if the feature name is cell hash, skip conversion - if (opt$feature_name == "cellhash") { + # make sure the feature data is present + if (!(opt$feature_name %in% altExpNames(sce))) { + stop("feature_name must match name of altExp in provided SCE object.") + + # if the feature name is cell hash, skip conversion + } else if (opt$feature_name == "cellhash") { warning("Conversion of altExp data from multiplexed data is not supported. The altExp will not be converted.") - } else { - # make sure the feature data is present - if (!(opt$feature_name %in% altExpNames(sce))) { - stop("feature_name must match name of altExp in provided SCE object.") - } + # convert altExp + } else { # check for output file if (!(stringr::str_ends(opt$output_feature_h5, ".hdf5|.h5"))) { stop("output feature file name must end in .hdf5 or .h5") @@ -162,7 +163,7 @@ if (!is.null(opt$feature_name)) { ) } else { # warn that the altExp cannot be converted - message( + warning( glue::glue(" Only 1 row found in altExp named: {opt$feature_name}. This altExp will not be converted to an AnnData object. From e738e81d7a13ad53693fdc0794427cc8619f9c1e Mon Sep 17 00:00:00 2001 From: Ally Hawkins <54039191+allyhawkins@users.noreply.github.com> Date: Tue, 23 Jan 2024 14:17:13 -0600 Subject: [PATCH 5/5] Apply suggestions from code review Co-authored-by: Joshua Shapiro --- bin/sce_to_anndata.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/sce_to_anndata.R b/bin/sce_to_anndata.R index e2593958..9714fdc4 100755 --- a/bin/sce_to_anndata.R +++ b/bin/sce_to_anndata.R @@ -165,9 +165,9 @@ if (!is.null(opt$feature_name)) { # warn that the altExp cannot be converted warning( glue::glue(" - Only 1 row found in altExp named: {opt$feature_name}. - This altExp will not be converted to an AnnData object. - ") + Only 1 row found in altExp named: {opt$feature_name}. + This altExp will not be converted to an AnnData object. + ") ) } }