Skip to content

Commit

Permalink
Merge pull request #47 from jiajic/dev
Browse files Browse the repository at this point in the history
`giottoPoints` update
  • Loading branch information
jiajic authored Oct 5, 2023
2 parents 128debb + f12cfcb commit cc8b7d7
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 15 deletions.
64 changes: 52 additions & 12 deletions R/create.R
Original file line number Diff line number Diff line change
Expand Up @@ -1698,8 +1698,12 @@ create_featureNetwork_object = function(name = 'feat_network',
#' @name createGiottoPoints
#' @description Creates Giotto point object from a structured dataframe-like object
#' @param x spatVector or data.frame-like object with points coordinate information (x, y, feat_ID)
#' @param feat_type feature type
#' @param feat_type character. feature type. Provide more than one value if
#' using the `split_keyword` param. For each set of keywords to split by, an
#' additional feat_type should be provided in the same order.
#' @param verbose be verbose
#' @param split_keyword list of character vectors of keywords to split the
#' giottoPoints object based on their feat_ID.
#' @param unique_IDs (optional) character vector of unique IDs present within
#' the spatVector data. Provided for cacheing purposes
#' @return giottoPoints
Expand All @@ -1708,30 +1712,66 @@ create_featureNetwork_object = function(name = 'feat_network',
createGiottoPoints = function(x,
feat_type = 'rna',
verbose = TRUE,
split_keyword = NULL,
unique_IDs = NULL) {
checkmate::assert_character(feat_type)
if (!is.null(split_keyword)) checkmate::assert_list(split_keyword)

if(inherits(x, 'data.frame')) {
# 1. read in data
if (inherits(x, 'data.frame')) {

spatvec = create_spatvector_object_from_dfr(x = x,
verbose = verbose)
g_points = create_giotto_points_object(feat_type = feat_type,
spatVector = spatvec,
unique_IDs = unique_IDs)
spatvec = create_spatvector_object_from_dfr(
x = x,
verbose = verbose
)
gpoints = create_giotto_points_object(
feat_type = feat_type[[1]],
spatVector = spatvec,
unique_IDs = unique_IDs
)

} else if(inherits(x, 'SpatVector')) {
} else if (inherits(x, 'SpatVector')) {

g_points = create_giotto_points_object(feat_type = feat_type,
spatVector = x,
unique_IDs = unique_IDs)
gpoints = create_giotto_points_object(
feat_type = feat_type[[1]],
spatVector = x,
unique_IDs = unique_IDs
)

} else {

stop('Class ', class(x), ' is not supported')

}

return(g_points)
# 2. perform split if needed
if (is.null(split_keyword)) return(gpoints)

# create booleans using grepl and the given keywords
gpoints_feat_ids = featIDs(gpoints, uniques = FALSE)
split_bools = lapply(split_keyword, function(keyword) {
grepl(paste(keyword, sep = '|'), gpoints_feat_ids)
})
# default_bool is the main set of points that do not get selected by any
# keywords. Usually the actual features being detected are here. These
# will get mapped to the first feat_type.
# default_bool must be made as a list for it to combine properly using c()
# with split_bools which are already a list of logical vectors
default_bool = list(!Reduce('|', split_bools))
split_bools = c(default_bool, split_bools)
names(split_bools) = feat_type

# split the created gpoints object into several using the booleans.
gpoints_list = lapply(split_bools, function(feat) {
gpoints[feat]
})

# set object name to match the feat_type.
for(name_i in seq_along(feat_type)) {
objName(gpoints_list[[name_i]]) = feat_type[[name_i]]
}

return(gpoints_list)
}


Expand Down
1 change: 0 additions & 1 deletion R/giotto_structures.R
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,6 @@ smoothGiottoPolygons = function(gpolygon,
create_spatvector_object_from_dfr = function(x,
verbose = TRUE) {


x = data.table::as.data.table(x)

# data.frame like object needs to have 2 coordinate columns and
Expand Down
10 changes: 10 additions & 0 deletions R/methods-extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ setReplaceMethod('[', signature(x = 'coordDataDT', i = 'missing', j = 'missing',
x
})

#' @rdname extract-methods
#' @export
setMethod('[', signature(x = 'giottoPoints', i = "ANY", j = "missing", drop = "missing"),
function(x, i, j) {
x@spatVector = x@spatVector[i]
x@unique_ID_cache = featIDs(x, uniques = TRUE, use_cache = FALSE)
x
})


# setMethod("[[")

## * metaData ####
Expand Down
15 changes: 13 additions & 2 deletions man/createGiottoPoints.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/extract-methods.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cc8b7d7

Please sign in to comment.