diff --git a/bin/sce_to_anndata.R b/bin/sce_to_anndata.R index 364feca8..9714fdc4 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) |> @@ -129,36 +132,43 @@ if (!is.null(opt$feature_name)) { # 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) + # 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.") - # export altExp sce as anndata object - scpcaTools::sce_to_anndata( - alt_sce, - anndata_file = opt$output_feature_h5 - ) + # convert altExp } 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. - ") - ) + # 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 + warning( + glue::glue(" + Only 1 row found in altExp named: {opt$feature_name}. + This altExp will not be converted to an AnnData object. + ") + ) + } } }