Skip to content

Commit

Permalink
Merge pull request #64 from jiajic/dev
Browse files Browse the repository at this point in the history
minor: catch up to main
  • Loading branch information
jiajic authored Oct 23, 2023
2 parents a4b26f6 + 08c929d commit 848dd32
Show file tree
Hide file tree
Showing 10 changed files with 371 additions and 29 deletions.
11 changes: 9 additions & 2 deletions R/interoperability.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ check_py_for_scanpy = function(){
#' @param feat_type desired feature type for conversion, default NULL
#' @param h5_file name to create and on-disk HDF5 file
#' @param python_path path to python executable within a conda/miniconda environment
#' @param env_name name of environment containing python_path executable
#'
#' @return Giotto object
#' @details Function in beta. Converts a .h5ad file into a Giotto object.
Expand All @@ -174,8 +175,9 @@ anndataToGiotto = function(anndata_path = NULL,
deluanay_spat_net = TRUE,
spat_unit = NULL,
feat_type = NULL,
h5_file = NULL,
python_path = NULL,
h5_file = NULL) {
env_name = "giotto_env") {

# Preliminary file checks and guard clauses
if (is.null(anndata_path)) {
Expand All @@ -196,7 +198,8 @@ anndataToGiotto = function(anndata_path = NULL,
# Required step to properly initialize reticualte
instrs = createGiottoInstructions(python_path = python_path)

check_py_for_scanpy()
scanpy_installed = checkPythonPackage("scanpy", env_to_use = env_name)
# should trigger a stop() downstream if not installed

# Import ad2g, a python module for parsing anndata
ad2g_path <- system.file("python","ad2g.py",package="Giotto")
Expand Down Expand Up @@ -492,6 +495,7 @@ anndataToGiotto = function(anndata_path = NULL,
#' @param spat_unit spatial unit which will be used in conversion.
#' @param feat_type feature type which will be used in conversion.
#' @param python_path path to python executable within a conda/miniconda environment
#' @param env_name name of environment containing python_path executable
#' @param save_directory directory in which the file will be saved.
#' @return vector containing .h5ad file path(s)
#' @details Function in beta. Converts a Giotto object into .h5ad file(s).
Expand All @@ -515,6 +519,7 @@ giottoToAnnData <- function(gobject = NULL,
spat_unit = NULL,
feat_type = NULL,
python_path = NULL,
env_name = "giotto_env",
save_directory = NULL){

# Check gobject
Expand All @@ -523,6 +528,8 @@ giottoToAnnData <- function(gobject = NULL,
stop(wrap_msg("Please provide a valid Giotto Object for conversion."))
}

scanpy_installed = checkPythonPackage("scanpy", env_to_use = env_name)

# Python module import
g2ad_path <- system.file("python","g2ad.py",package="Giotto")
reticulate::source_python(g2ad_path)
Expand Down
14 changes: 13 additions & 1 deletion R/python_bento.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
#' @name createBentoAdata
#' @description Create bento adata object from gobject
#' @param gobject Giotto object
#' @param env_to_use Python environment within which bento is installed.
#' If it is not already installed, the user
#' will be prompted to install `bento-tools`
#' DEFAULT: "giotto_env"
#' @return bento_adata bento adata object
#' @export
createBentoAdata <- function(gobject){
createBentoAdata <- function(gobject = NULL,
env_to_use = "giotto_env"){
if(!c("giotto") %in% class(gobject)) stop(wrap_txt("Please provide a valid Giotto Object.", errWidth=TRUE))
# Transcripts
transcripts_df <- as.data.frame(sf::st_as_sf(gobject@feat_info$rna@spatVector))
coordinates_df <- lapply(transcripts_df['geometry'], sf::st_coordinates)$geometry
Expand All @@ -21,6 +27,12 @@ createBentoAdata <- function(gobject){
nucleus_poly <- spatVector_to_dt(gobject@spatial_info$nucleus@spatVector)
nucleus_poly <- data.frame(cell_id = nucleus_poly$poly_ID, x = nucleus_poly$x, y = nucleus_poly$y, batch = 0L)

# Install bento-tools / Check python environment for bento-tools
bento_installed = checkPythonPackage(github_package_url = "git+https://github.com/wwang-chcn/bento-tools.git",
env_to_use = env_to_use)
# Will crash downstream if installation unsuccessful/denied
# or if the package is not found.

# Create AnnData object
g2bento_path <- system.file("python","g2bento.py",package="Giotto")
reticulate::source_python(g2bento_path)
Expand Down
Loading

0 comments on commit 848dd32

Please sign in to comment.