Skip to content

Commit

Permalink
Merge pull request #664 from AlexsLemonade/allyhawkins/no-sample-meta…
Browse files Browse the repository at this point in the history
…data-for-cellhash

Skip sample metadata addition to colData if cellhash is present
  • Loading branch information
allyhawkins authored Jan 23, 2024
2 parents 6cf64ed + e738e81 commit e24c08b
Showing 1 changed file with 43 additions and 33 deletions.
76 changes: 43 additions & 33 deletions bin/sce_to_anndata.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) |>
Expand Down Expand Up @@ -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.
")
)
}
}
}

0 comments on commit e24c08b

Please sign in to comment.