Skip to content

Commit

Permalink
chore: merge & conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
jiajic committed May 21, 2024
2 parents 333f2fe + c229830 commit cbf05ad
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 25 deletions.
12 changes: 6 additions & 6 deletions R/clustering.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ doLeidenCluster <- function(gobject,
if (isTRUE(set_seed)) {
seed_number <- as.integer(seed_number)
} else {
seed_number <- as.integer(sample(x = seq_len(10000), size = 1))
seed_number <- as.integer(sample(x = seq(10000), size = 1))
}

## extract NN network
Expand Down Expand Up @@ -551,7 +551,7 @@ doGiottoClustree <- function(gobject,
if (isTRUE(set_seed)) {
seed_number <- as.integer(seed_number)
} else {
seed_number <- as.integer(sample(x = seq_len(10000), size = 1))
seed_number <- as.integer(sample(x = seq(10000), size = 1))
}

network_edge_dt <- data.table::as.data.table(igraph::as_data_frame(
Expand Down Expand Up @@ -1102,9 +1102,9 @@ doSNNCluster <- function(gobject,
cell_id_numeric <- unique(x = c(igraph_DT$from, igraph_DT$to))
names(cell_id_numeric) <- seq_along(cell_id_numeric)
igraph_DT[, from_T := as.numeric(names(cell_id_numeric[
cell_id_numeric == from])), by = seq_len(nrow(igraph_DT))]
cell_id_numeric == from])), by = 1:nrow(igraph_DT)]
igraph_DT[, to_T := as.numeric(names(cell_id_numeric[
cell_id_numeric == to])), by = seq_len(nrow(igraph_DT))]
cell_id_numeric == to])), by = 1:nrow(igraph_DT)]
temp_igraph_DT <- igraph_DT[, .(from_T, to_T, weight, distance)]
data.table::setnames(
temp_igraph_DT, old = c("from_T", "to_T"), new = c("from", "to"))
Expand Down Expand Up @@ -2814,7 +2814,7 @@ getClusterSimilarity <- function(gobject,
as.character(group1), as.character(group2))]
cor_table[, unified_group := paste(
sort(c(group1, group2)), collapse = "--"),
by = seq_len(nrow(cor_table))]
by = 1:nrow(cor_table)]
cor_table <- cor_table[!duplicated(cor_table[, .(value, unified_group)])]

cor_table <- merge(
Expand Down Expand Up @@ -2972,7 +2972,7 @@ mergeClusters <- function(gobject,
as.character(get(cluster_column)) %in% finalvec,
names(finalvec[finalvec == as.character(get(cluster_column))]),
as.character(get(cluster_column))
), by = seq_len(nrow(metadata))]
), by = 1:nrow(metadata)]



Expand Down
10 changes: 5 additions & 5 deletions R/cross_section.R
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ createCrossSection <- function(gobject,
message("either point or norm vector was not provided.")
} else {
plane_equation <- c()
plane_equation[seq_len(3)] <- normVector
plane_equation[1:3] <- normVector
plane_equation[4] <- -point1 %*% normVector
}
} else if (method == "point and two plane vectors") {
Expand All @@ -551,7 +551,7 @@ createCrossSection <- function(gobject,
provided.")
} else {
normVector <- crossprod(planeVector1, planeVector2)
plane_equation[seq_len(3)] <- normVector
plane_equation[1:3] <- normVector
plane_equation[4] <- -point1 %*% normVector
}
} else if (method == "3 points") {
Expand All @@ -561,7 +561,7 @@ createCrossSection <- function(gobject,
planeVector1 <- point2 - point1
planeVector2 <- point3 - point1
normVector <- crossprod(planeVector1, planeVector2)
plane_equation[seq_len(3)] <- normVector
plane_equation[1:3] <- normVector
plane_equation[4] <- -point1 %*% normVector
}
}
Expand All @@ -584,7 +584,7 @@ createCrossSection <- function(gobject,
spatial_locations, as.matrix(rep(1, dim(spatial_locations)[1])))
norm_vec <- function(x) sqrt(sum(x^2))
distance_to_plane_vector <- abs(spatial_locations_mat %*% as.matrix(
plane_equation) / norm_vec(plane_equation[seq_len(3)]))
plane_equation) / norm_vec(plane_equation[1:3]))

# select cells within section ###
cell_subset <- distance_to_plane_vector <= max_distance_to_section_plane
Expand All @@ -604,7 +604,7 @@ createCrossSection <- function(gobject,
cell_subset_projection_locations <- t(apply(
cell_subset_spatial_locations, 1,
function(x) projection_fun(x, plane_point = plane_point,
plane_norm = plane_equation[seq_len(3)])))
plane_norm = plane_equation[1:3])))

# get the local coordinates of selected cells on the section plane
cell_subset_projection_PCA <- stats::prcomp(
Expand Down
2 changes: 2 additions & 0 deletions R/kriging.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ setMethod(
overwrite = FALSE,
verbose = NULL,
...) {
sl <- NULL

# This method prepares the data from the giotto object to pass
# downstream where the actual interpolation happens

Expand Down
46 changes: 37 additions & 9 deletions R/python_hmrf.R
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,15 @@ writeHMRFresults <- function(gobject,
#' @param betas_to_add results from different betas that you want to add
#' @param hmrf_name specify a custom name
#' @returns giotto object
#' @examples
#' g <- GiottoData::loadGiottoMini("visium")
#' spat_genes <- binSpect(g)
#'
#' output_folder <- tempdir()
#' doHMRF(g, spatial_genes = spat_genes[seq_len(10)]$feats,
#' output_folder = output_folder)
#'
#' addHMRF(gobject = g, HMRFoutput = doHMRF)
#' @export
addHMRF <- function(gobject,
spat_unit = NULL,
Expand Down Expand Up @@ -1088,7 +1097,7 @@ checkAndFixSpatialGenes <- function(gobject,

#' @title initHMRF_V2
#' @name initHMRF_V2
#' @description Run initialzation for HMRF model
#' @description Run initialization for HMRF model
#' @param gobject giotto object
#' @param spat_unit spatial unit
#' @param feat_type feature type
Expand Down Expand Up @@ -1152,10 +1161,10 @@ checkAndFixSpatialGenes <- function(gobject,
#' Third, once spatial genes are finalized, we are using clustering method to
#' initialize HMRF.
#' Instead of select spatial genes for domain clustering, HMRF method could
#' also applied on unit neighbohood composition of any group
#' also applied on unit neighborhood composition of any group
#' membership(such as cell types), specified by parameter:
#' use_neighborhood_composition, spatial_network_name_for_neighborhood and
#' metadata_to_use. Also HMRF provides the oppertunity for user to do
#' metadata_to_use. Also HMRF provides the opportunity for user to do
#' clustering by any customized spatial enrichment matrix
#' (existing_spatial_enrichm_to_use).
#' There are 3 clustering algorithm: K-means, Leiden, and Louvain to determine
Expand All @@ -1165,6 +1174,11 @@ checkAndFixSpatialGenes <- function(gobject,
#' blocks (graph colors), damp (dampened factor), mu (mean),
#' sigma (covariance), k, genes, edgelist, init.cl (initial clusters),
#' spat_unit, feat_type. This information is needed for the second step, doHMRF.
#' @examples
#' g <- GiottoData::loadGiottoMini("visium")
#' g <- binSpect(g, return_gobject = TRUE)
#'
#' initHMRF_V2(gobject = g, cl.method = "km")
#' @export
initHMRF_V2 <-
function(gobject,
Expand Down Expand Up @@ -1692,6 +1706,12 @@ initHMRF_V2 <-
#' Returns a list of results for betas, spat_unit and feat_type. Result for
#' each beta is a list with probability(normalized or non-normalized), class,
#' and model log-likelihood value.
#' @examples
#' g <- GiottoData::loadGiottoMini("visium")
#' g <- binSpect(g, return_gobject = TRUE)
#' HMRF_init_obj <- initHMRF_V2(gobject = g, cl.method = "km")
#'
#' doHMRF_V2(HMRF_init_obj = HMRF_init_obj, betas = c(0, 5, 2))
#' @export
doHMRF_V2 <- function(HMRF_init_obj, betas = NULL) {
message(
Expand Down Expand Up @@ -1752,7 +1772,7 @@ doHMRF_V2 <- function(HMRF_init_obj, betas = NULL) {
cat(paste0("Default value beta = ", beta_seq, " is used..."))
} else if (length(betas) != 3 || (sum(betas[seq_len(3)] < 0) > 0)) {
stop("please provide betas as a vector of 3 non-negative numbers
(initial value, nicrement, total iteration number)")
(initial value, increment, total iteration number)")
} else {
beta_init <- betas[1]
beta_increment <- betas[2]
Expand Down Expand Up @@ -1782,7 +1802,7 @@ doHMRF_V2 <- function(HMRF_init_obj, betas = NULL) {
tc.hmrfem$mu <- NULL
rownames(tc.hmrfem$prob) <- rownames(y)
rownames(tc.hmrfem$unnormprob) <- rownames(y)
names(tc.hmrfem$class) <- rownames(y)
#names(tc.hmrfem$class) <- rownames(y)
res[[t_key]] <- tc.hmrfem
}
result.hmrf <- res
Expand All @@ -1805,6 +1825,13 @@ doHMRF_V2 <- function(HMRF_init_obj, betas = NULL) {
#' for all the beta values, with the given HMRF model names. For example, if
#' name = ‘hmrf1’ and name of result in HMRFoutput is ‘k=8 b=0.00’, the
#' appended cell meta data column will be named with ‘hmrf1 k=8 b=0.00’
#' @examples
#' g <- GiottoData::loadGiottoMini("visium")
#' g <- binSpect(g, return_gobject = TRUE)
#' HMRF_init_obj <- initHMRF_V2(gobject = g, cl.method = "km")
#' HMRFoutput <- doHMRF_V2(HMRF_init_obj = HMRF_init_obj, betas = c(0, 5, 2))
#'
#' addHMRF_V2(gobject = g, HMRFoutput = HMRFoutput)
#' @export
addHMRF_V2 <- function(gobject, HMRFoutput, name = "hmrf") {
if (!"HMRFoutput" %in% class(HMRFoutput)) {
Expand Down Expand Up @@ -1834,10 +1861,11 @@ addHMRF_V2 <- function(gobject, HMRFoutput, name = "hmrf") {
spat_unit = spat_unit,
feat_type = feat_type,
column_cell_ID = "cell_ID",
new_metadata = HMRFoutput[[i]]$class[match(
ordered_cell_IDs, names(HMRFoutput[[i]]$class))],
vector_name = paste(name, names(HMRFoutput)[i]),
by_column = TRUE
# new_metadata = HMRFoutput[[i]]$class[match(
# ordered_cell_IDs, names(HMRFoutput[[i]]$class))],
new_metadata = HMRFoutput[[i]]$prob[ordered_cell_IDs,],
vector_name = paste(name, names(HMRFoutput)[i])
#by_column = TRUE
)
}
return(gobject)
Expand Down
2 changes: 1 addition & 1 deletion R/spatial_genes.R
Original file line number Diff line number Diff line change
Expand Up @@ -4557,7 +4557,7 @@ run_spatial_sim_tests_one_rep <- function(gobject,
...) {
# data.table variables
genes <- prob <- time <- adj.p.value <- method <- p.val <- sd <-
qval <- pval <- g <- adjusted_pvalue <- NULL
qval <- pval <- g <- adjusted_pvalue <- feats <- NULL

## test if spat_methods, params and names have the same length
if (length(spat_methods) != length(spat_methods_params)) {
Expand Down
2 changes: 1 addition & 1 deletion R/spatial_interaction_spot.R
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,7 @@ plotICFSpot <- function(gobject,
save_param = list(),
default_save_name = "plotICFSpot") {
# data.table variables
cell_type <- int_cell_type <- pcc_diff <- NULL
cell_type <- int_cell_type <- pcc_diff <- feats <- perm_diff <- NULL


if (!"icfObject" %in% class(icfObject)) {
Expand Down
10 changes: 10 additions & 0 deletions man/addHMRF.Rd

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

8 changes: 8 additions & 0 deletions man/addHMRF_V2.Rd

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

7 changes: 7 additions & 0 deletions man/doHMRF_V2.Rd

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

12 changes: 9 additions & 3 deletions man/initHMRF_V2.Rd

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

0 comments on commit cbf05ad

Please sign in to comment.