Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit65fdd06

Browse files
committed
'update'
1 parentae30659 commit65fdd06

24 files changed

+234
-162
lines changed

‎.Rhistory‎

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,3 @@
1-
n = nrow(wei_sp_mat)
2-
n2 <- n - n1
3-
if(perm.num != 0 && !is.numeric(perm.num)){
4-
stop('incorrect perm_num')
5-
}
6-
weighted_edge_count <- fast_weighted_R1R2(wei_sp_mat, 1:n1)
7-
# browser()
8-
asy_res <- fast_theo_mu_sig(wei_sp_mat, n1, n2)
9-
res_list <- list()
10-
if("ori" %in% test.type){
11-
#original edge count test
12-
ori_test <- (weighted_edge_count$R - asy_res$mu)/sqrt(asy_res$sig)
13-
ori_asy_p <- pnorm(ori_test)
14-
res_list <- c(res_list, asy.ori.statistic = ori_test, asy.ori.pval = ori_asy_p)
15-
}
16-
if("gen" %in% test.type){
17-
#generalized edge count test
18-
gen_asy_p <- asy_gen(asy_res, weighted_edge_count$R1, weighted_edge_count$R2)
19-
res_list <- c(res_list, asy.gen.statistic = gen_asy_p$test_statistic, asy.gen.pval = gen_asy_p$p_value)
20-
}
21-
if("wei" %in% test.type){
22-
#weighted edge count test
23-
wei_asy_p <- asy_wei(asy_res, weighted_edge_count$R1, weighted_edge_count$R2, n1, n2)
24-
res_list <- c(res_list, asy.wei.statistic = wei_asy_p$test_statistic, asy.wei.pval = wei_asy_p$p_value)
25-
}
26-
if("max" %in% test.type){
27-
#max-type edge count test
28-
max_asy_p <- asy_max(asy_res, weighted_edge_count$R1, weighted_edge_count$R2, n1, n2)
29-
res_list <- c(res_list, asy.max.statistic = max_asy_p$test_statistic, asy.max.pval = max_asy_p$p_value)
30-
}
31-
if (perm.num != 0 && is.numeric(perm.num)) {
32-
if (perm.num > 10000) {
33-
message("Doing more than 10,000 permutations could be time consuming.\n")
34-
}
35-
n_per = perm.num
36-
# temp_list = permu_edge(n_per, weis, progress_bar)
37-
temp_list = fast.permu_edge(n_per, wei_sp_mat, n1, n2, progress_bar)
38-
if("ori" %in% test.type){
39-
#original edge count test
40-
ori = sum(temp_list$R < weighted_edge_count$R)
41-
ori = ori/n_per
42-
res_list = c(res_list, perm.ori.pval = ori)
43-
}
44-
if("gen" %in% test.type){
45-
#generalized edge count test
46-
gen_p_res <- fast.permu_gen(temp_list$R1, temp_list$R2, weighted_edge_count$R1, weighted_edge_count$R2, n_per, eta)
47-
res_list <- c(res_list, perm.gen.pval = gen_p_res$pval)
48-
}
49-
if("wei" %in% test.type){
50-
#weighted edge count test
51-
wei_p_res <- fast.permu_wei(temp_list$R1, temp_list$R2, weighted_edge_count$R1, weighted_edge_count$R2, n1, n2, n_per)
52-
res_list <- c(res_list, perm.wei.pval = wei_p_res$pval)
53-
}
54-
if("max" %in% test.type){
55-
#max-type edge count test
56-
max_p_res <- fast.permu_max(temp_list$R1, temp_list$R2, weighted_edge_count$R1, weighted_edge_count$R2, n1, n2, n_per)
57-
res_list <- c(res_list, perm.max.pval = max_p_res$pval)
58-
}
59-
}
60-
return(res_list)
611
}
622
res.rgTest2 <- pathway.rgTest(
633
Embeddings(seu, reduction = reduction.name), cell.use, cell.set.list, gene.set.cutoff = 3,
@@ -510,3 +450,63 @@ pkgdown::build_article("CosMx")
510450
pkgdown::build_article("CosMx")
511451
pkgdown::preview_site(path='articles/CosMx.html')
512452
pkgdown::build_home()
453+
progress::progress_bar
454+
progress::progress_bar$new
455+
devtools::document()
456+
data(CosMx_subset)
457+
> library(Seurat)
458+
library(Seurat)
459+
CosMx_subset <- Addcoord2embed(CosMx_subset, coord.name = c("x", "y"))
460+
Idents(CosMx_subset) <- 'cell_type'
461+
dat.sp.score <- AggregationScore(CosMx_subset, reduction.name = 'Spatial')
462+
CosMx_subset
463+
progress_bar$new
464+
devtools::document()
465+
example( AggregationScore)
466+
progress_bar$new(format = "[:bar] :percent ETA: :eta")
467+
progress::progress_bar$new(format = "[:bar] :percent ETA: :eta")
468+
example( AggregationScore)
469+
example("AddAdj")
470+
example("Addcoord2embed")
471+
example("coembed_plot")
472+
example("coembed_umap")
473+
example("coembedding_umap")
474+
example("diagnostic.cor.eigs")
475+
example("find.signature.genes")
476+
example("get.top.signature.dat")
477+
example(NCFM)
478+
example("pdistance")
479+
example("AggregationScore")
480+
devtools::document()
481+
library(devtools)
482+
install_github("blhansen/BAPmultiNMF")
483+
library(BAPmultiNMF)
484+
utils::setTxtProgressBar()
485+
utils::setTxtProgressBar
486+
devtools::document()
487+
devtools::document()
488+
pbsapply
489+
pbsapply::
490+
devtools::document()
491+
devtools::document()
492+
devtools::document()
493+
pbmc3k_subset
494+
data(CosMx_subset)
495+
CosMx_subset
496+
pbmc3k_subset <- AddCluster(pbmc3k_subset, reduction='ncfm')
497+
devtools::document()
498+
example("AddCluster")
499+
top5_signatures
500+
example("AggregationScore")
501+
devtools::check()
502+
devtools::run_examples()
503+
DR.SC::getAdj_auto
504+
ProFAST::FAST_run
505+
devtools::document()
506+
devtools::document()
507+
devtools::document()
508+
devtools::document()
509+
devtools::document()
510+
3550*8
511+
3550*8+2248+6500
512+
28400 + 2500 + 5000+ 4000

‎.gitignore‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.Ruserdata
5+
src/*.o
6+
src/*.so
7+
src/*.dll

‎DESCRIPTION‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: coFAST
22
Type: Package
3-
Title: Spatially-Aware Cell Clustering AlgorithmWith Cluster Significant Assessment
3+
Title: Spatially-Aware Cell Clustering Algorithmwith Cluster Significant Assessment
44
Version: 0.1.0
5-
Date: 2025-03-14
5+
Date: 2025-11-12
66
Authors@R:
77
c(person(given = "Wei",
88
family = "Liu",
@@ -25,7 +25,7 @@ Authors@R:
2525
role = "aut"))
2626
Author: Wei Liu [aut, cre], Xiao Zhang [aut], Yi Yang [aut], Peng Xie [aut], Chengqi Lin [aut], Jin Liu [aut]
2727
Maintainer: Wei Liu <liuweideng@gmail.com>
28-
Description:coFAST is aspatially-aware cell clustering algorithm with clustersignificant assessment. It comprises four key modules: spatially-aware cell-gene co-embedding, cell clustering, signature gene identification, and cluster significant assessment.
28+
Description:Aspatially-aware cell clustering algorithmis providedwith clustersignificance assessment. It comprises four key modules: spatially-aware cell-gene co-embedding, cell clustering, signature gene identification, and cluster significant assessment. More details can be referred to Peng Xie, et al. (2025) <doi:10.1016/j.cell.2025.05.035>.
2929
License: GPL-3
3030
Depends:
3131
R (>= 4.0.0),
@@ -39,10 +39,12 @@ Imports:
3939
PRECAST,
4040
ProFAST,
4141
Matrix,
42+
ade4,
43+
progress,
44+
pbapply,
4245
dplyr,
4346
Seurat,
4447
stats,
45-
ProFAST,
4648
utils
4749
LazyData: true
4850
URL: https://github.com/feiyoung/coFAST

‎NAMESPACE‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export(find.signature.genes)
1515
export(get.top.signature.dat)
1616
export(pdistance)
1717
import(ggplot2)
18+
import(progress)
1819
importFrom(DR.SC,getAdj_auto)
1920
importFrom(Matrix,sparseMatrix)
2021
importFrom(Matrix,t)
@@ -26,12 +27,16 @@ importFrom(Seurat,CreateAssayObject)
2627
importFrom(Seurat,CreateDimReducObject)
2728
importFrom(Seurat,DefaultAssay)
2829
importFrom(Seurat,Embeddings)
30+
importFrom(Seurat,FindClusters)
31+
importFrom(Seurat,FindNeighbors)
2932
importFrom(Seurat,FindVariableFeatures)
3033
importFrom(Seurat,GetAssayData)
3134
importFrom(Seurat,Idents)
3235
importFrom(Seurat,Loadings)
36+
importFrom(Seurat,`Idents<-`)
3337
importFrom(ade4,mstree)
3438
importFrom(dplyr,`%>%`)
39+
importFrom(furrr,furrr_options)
3540
importFrom(furrr,future_map)
3641
importFrom(future,plan)
3742
importFrom(ggplot2,aes_string)
@@ -49,8 +54,11 @@ importFrom(irlba,irlba)
4954
importFrom(pbapply,pblapply)
5055
importFrom(stats,as.dist)
5156
importFrom(stats,cov)
57+
importFrom(stats,p.adjust)
5258
importFrom(stats,pchisq)
5359
importFrom(stats,pnorm)
5460
importFrom(stats,rnorm)
5561
importFrom(utils,head)
62+
importFrom(utils,setTxtProgressBar)
63+
importFrom(utils,txtProgressBar)
5664
useDynLib(coFAST, .registration = TRUE)

‎R/CoFAST.R‎

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# generate man files
22
# devtools::document()
3-
# R CMD check --as-cranProFAST_1.4.tar.gz
3+
# R CMD check --as-crancoFAST_0.1.0.tar.gz
44
## usethis::use_data(pbmc3k_subset)
55
# pkgdown::build_site()
66
# pkgdown::build_home()
@@ -75,7 +75,7 @@ get_varfeature_fromSeurat <- function(seu, assay=NULL){
7575
#' @param platform a string, specify the platform of the provided data, default as "Others". There are more platforms to be chosen, including "Visuim", "ST" and "Others" ("Others" represents the other SRT platforms except for 'Visium' and 'ST')
7676
#' The platform helps to calculate the adjacency matrix by defining the neighborhoods when type="fixed_distance" is chosen.
7777
#' @param neighbors an optional postive integer, specify how many neighbors used in calculation, default as 6.
78-
#' @param ... Other arguments passed to \code{\link{getAdj_auto}}.
78+
#' @param ... Other arguments passed to \code{\link[DR.SC]{getAdj_auto}}.
7979
#' @return return a sparse matrix, representing the adjacency matrix.
8080
#' @details When the type = "fixed_distance", then the spots within the Euclidean distance cutoffs from one spot are regarded as the neighbors of this spot. When the type = "fixed_number", the K-nearest spots are regarded as the neighbors of each spot.
8181
#'
@@ -102,12 +102,12 @@ AddAdj <- function(pos, type="fixed_distance", platform=c("Others","Visium", "ST
102102

103103
if(tolower(type)=='fixed_distance'){
104104
if(tolower(platform)%in% c("st","visium")){
105-
Adj<-PRECAST::getAdj_reg(pos,platform=platform)
105+
Adj<- getAdj_reg(pos,platform=platform)
106106
}else{
107107
Adj<- getAdj_auto(pos,lower.med=neighbors-2,upper.med=neighbors+2,...)
108108
}
109109
}elseif (tolower(type)=="fixed_number") {
110-
Adj<-PRECAST::getAdj_fixedNumber(pos,number=neighbors)
110+
Adj<- getAdj_fixedNumber(pos,number=neighbors)
111111
}else {
112112
stop("AddAdj: Unsupported adjacency type\"",type,"\".")
113113
}
@@ -390,6 +390,7 @@ diagnostic.cor.eigs.Seurat <- function(
390390
#' @param weighted an optional logical value, specify whether use weighted method.
391391
#' @param var.features an optional string vector, specify the variable features used to calculate cell embedding.
392392
#'
393+
#' @return return a revised Seurat object with a new reduction slot reduction.name obtained by NCFM co-embedding method, where reduction.name is default as `ncfm`.
393394
#' @rdname NCFM
394395
#'
395396
#' @importFrom Seurat DefaultAssay GetAssayData CreateDimReducObject FindVariableFeatures
@@ -491,13 +492,15 @@ Factor_nc <- function(
491492
#' @param q an optional positive integer, specify the dimension of low dimensional embeddings to compute and store. Default is 10.
492493
#' @param reduction.name an optional string, dimensional reduction name, `cofast` by default.
493494
#' @param var.features an optional string vector, specify the variable features, used to calculate cell embedding.
494-
#' @param ... Other argument passed to the \code{\link{FAST_run}}.
495+
#' @param ... Other argument passed to the \code{\link[ProFAST]{FAST_run}}.
495496
#'
497+
#' @return return a revised Seurat object with a new reduction slot reduction.name obtained by coFAST co-embedding, where default reduction.name is `cofast`.
496498
#' @importFrom Seurat DefaultAssay GetAssayData CreateDimReducObject
497499
#'
498500
#' @export
499501
#'
500502
#' @examples
503+
#' library(Seurat)
501504
#' data(CosMx_subset)
502505
#' pos <- as.matrix(CosMx_subset@meta.data[,c("x", "y")])
503506
#' Adj_sp <- AddAdj(pos)
@@ -586,10 +589,11 @@ pdistance.matrix <- function (Ar, Br, eta = 1e-10) {
586589
#'Calculate the cell-feature distance matrix
587590
#' @description Calculate the cell-feature distance matrix based on coembeddings.
588591
#' @param object a Seurat object.
589-
#' @param reduction aopstional string, dimensional reduction name, `cofast` by default.
590-
#' @param assay.name aopstional string, specify the new generated assay name, `distce` by default.
591-
#' @param eta an optionalpostive real, a quantity to avoid numerical errors. 1e-10 by default.
592+
#' @param reduction aoptional string, dimensional reduction name, `cofast` by default.
593+
#' @param assay.name aoptional string, specify the new generated assay name, `distce` by default.
594+
#' @param eta an optionalpositive real, a quantity to avoid numerical errors. 1e-10 by default.
592595
#'
596+
#' @return return a revised Seurat object with a assay slot `assay.name`.
593597
#' @details This function calculate the distance matrix between cells/spots and features, and then put the distance matrix in a new generated assay. This
594598
#' distance matrix will be used in the siganture gene identification.
595599
#' @importFrom Seurat Loadings Embeddings CreateAssayObject
@@ -616,6 +620,7 @@ pdistance <- function(
616620

617621

618622
# Cell/spot clustering----------------------------------------------------
623+
#' @importFrom Seurat FindClusters `Idents<-` FindNeighbors
619624
search.res.louvain<-function(seu,K,res.start=0.01,res.end=2,step=0.02){
620625
# res.start=0.1; res.end=2; step = 0.1
621626
res.vec<- seq(res.start,res.end,by=step)
@@ -638,16 +643,16 @@ search.res.louvain <- function(seu, K, res.start=0.01, res.end=2, step = 0.02){
638643

639644
#' Find clusters for SRT data
640645
#' @description Identify clusters of spots by a shared nearest neighbor (SNN) modularity optimization based on coFAST's embeddings.
641-
#' @paramdf.list alist that is obtained by the function \code{\link{find.signature.genes}}.
642-
#' @paramntop an optionalpositive integer, specify the how many top signature genes extracted, default as 5.
643-
#' @paramexpr.prop.cutoff an optionalpostive real ranging from 0 to 1,specifycutoff of expression proportion of features, default as 0.1.
644-
#' @return return a `data.frame` object with four columns: `distance`,`expr.prop`, `label` and `gene`.
645-
#' @details Using this funciton, we obtainthetop signature genes and organize them into a data.frame. The `row.names` are gene names.
646-
#'The colname `distance` means the distance between gene (i.e., VPREB3) and cells with the specific cell type (i.e., B cell),
647-
#' which is calculated based on the coembedding of genes and cells in the coembedding space. The distance is smaller, the association between gene and the cell type is stronger.
648-
#' The colname `expr.prop` represents the expression proportion of the gene (i.e., VPREB3) within the cell type (i.e., B cell).
649-
#' The colname `label` means the cell types and colname `gene` denotes the genename.
650-
#' By the data.frame object, we know `VPREB3` is the one of the top signature gene of B cell.
646+
#' @paramseu aSeurat object.
647+
#' @paramreduction a optionalstring, dimensional reduction name, `cofast` by default.
648+
#' @paramcluster.name an optionalstring,specifythe colname in meta.data for clusters, `cofast.cluster` by default.
649+
#' @param res a positive real, speficy the resolution parameter for Louvain clustering, default as 0.8.
650+
#' @param K a positive integer or NULL, specifythenumber of clusters, default as NULL that indicates not specify the number of clusters.
651+
#'@param res.start a positive real, when K is not NULL, starting value of resolution to be searched, default as 0.2.
652+
#'@param res.end a positive real, when K is not NULL, ending value of resolution to be searched, default as 2.
653+
#'@param step a positive real, when K is not NULL, step size of resolution to be searched, default as 0.02.
654+
#'@return return a revised Seurat object with a new column in meta.data named cluster.name.
655+
#'@details None
651656
#' @seealso None
652657
#' @references None
653658
#' @export
@@ -656,16 +661,14 @@ search.res.louvain <- function(seu, K, res.start=0.01, res.end=2, step = 0.02){
656661
#' @examples
657662
#' library(Seurat)
658663
#' data(pbmc3k_subset)
659-
#' pbmc3k_subset <- pdistance(pbmc3k_subset, reduction='ncfm')
660-
#' df_list_rna <- find.signature.genes(pbmc3k_subset)
661-
#' dat.sig <- get.top.signature.dat(df_list_rna, ntop=5)
662-
#' head(dat.sig)
664+
#' pbmc3k_subset <- AddCluster(pbmc3k_subset, reduction='ncfm')
665+
#' head(pbmc3k_subset)
663666

664667
AddCluster<-function(seu,reduction='cofast',cluster.name='cofast.cluster',res=0.8,K=NULL,res.start=0.2,res.end=2,step=0.02){
665668

666669
# res.start=0.2; res.end=2; step=0.02
667-
q.use<- ncol(Embeddings(seu,reduction='cofast'))
668-
seu<- FindNeighbors(seu,reduction="cofast",dims=1:q.use)
670+
q.use<- ncol(Embeddings(seu,reduction=reduction))
671+
seu<- FindNeighbors(seu,reduction=reduction,dims=1:q.use)
669672
if(is.null(K)){
670673
seu<- FindClusters(seu,resolution=res,cluster.name=cluster.name)
671674
}else{
@@ -709,7 +712,7 @@ AddCluster <- function(seu, reduction='cofast',cluster.name = 'cofast.cluster',
709712
#' head(dat.sig)
710713

711714
get.top.signature.dat<-function(df.list,ntop=5,expr.prop.cutoff=0.1) {
712-
top5<-pbapply::pblapply(seq_along(df.list),function(j) {
715+
top5<- pblapply(seq_along(df.list),function(j) {
713716
#message("j = ", j)
714717
#j <- 1
715718
x<-df.list[[j]]
@@ -766,7 +769,7 @@ find.signature.genes <- function(seu, distce.assay='distce', ident=NULL, expr.pr
766769
names(barcodeList)<-cell_ID
767770

768771

769-
df_co_list<-pbapply::pblapply(seq_along(cell_ID),function(r){
772+
df_co_list<- pblapply(seq_along(cell_ID),function(r){
770773
cell.set.tmp<-barcodeList[[r]]
771774
df_co_genes.tmp<- gene.activity.score.seu(seu,cell.set=cell.set.tmp,assay=assay,
772775
distce.assay=distce.assay,genes.use=genes.use)
@@ -836,26 +839,6 @@ gene.activity.score <- function (distce, genes.expr.prop, cells.use, cell.set){
836839

837840
## plot related functions------------------------------------
838841

839-
#' @importFrom ggplot2 ggsave scale_x_reverse scale_y_reverse
840-
write_fig<-function(
841-
plt,filename="myfigure",dir_name="Figs",y_reverse=FALSE,
842-
x_reverse=FALSE,width=7,height=5.5,dpi=200) {
843-
if (y_reverse) {
844-
plt<-plt+ scale_y_reverse()
845-
}
846-
if (x_reverse) {
847-
plt<-plt+ scale_x_reverse()
848-
}
849-
if (!is.null(filename)) {
850-
if (!dir.exists(dir_name)) {
851-
dir.create(dir_name)
852-
}
853-
ggplot2::ggsave(
854-
file= paste0("./",dir_name,"/",filename,".png"),plot=plt,
855-
width=width,height=height,units="in",dpi=dpi)
856-
}
857-
}
858-
859842

860843
#' Calculate UMAP projections for coembedding of cells and features
861844
#' @description Calculate UMAP projections for coembedding of cells and features
@@ -874,6 +857,7 @@ write_fig <- function(
874857
#' @importFrom Seurat Loadings Embeddings CreateDimReducObject
875858
#'
876859
#' @examples
860+
#' library(Seurat)
877861
#' data(pbmc3k_subset)
878862
#' data(top5_signatures)
879863
#' \donttest{
@@ -940,6 +924,7 @@ coembedding_umap <- function(seu, reduction, reduction.name, gene.set = NULL,
940924
#' @importFrom ggplot2 ggplot aes_string geom_point scale_colour_manual scale_shape_manual theme_classic theme guides
941925
#' @importFrom Seurat Loadings Embeddings Idents
942926
#' @examples
927+
#' library(Seurat)
943928
#' data(pbmc3k_subset)
944929
#' data(top5_signatures)
945930
#' coembed_plot(pbmc3k_subset, reduction = "UMAPsig",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp