Skip to content

Commit

Permalink
Merge pull request #953 from xazra/suite_dev
Browse files Browse the repository at this point in the history
spatialDWLS bug fixes and 0-gene resolution into NA deconv fractions.
  • Loading branch information
josschavezf authored May 14, 2024
2 parents 14faf9c + a0779a4 commit 0c157d6
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions R/spatial_enrichment.R
Original file line number Diff line number Diff line change
Expand Up @@ -2149,7 +2149,7 @@ enrich_deconvolution <- function(expr,
#####remove negative values
for (i in dim(dwls_results)[1]){
negtive_index<-which(dwls_results[i,]<0)
dwls_results[i,negtive_index]==0
dwls_results[i,negtive_index]=0 ####* fixed a typo, with "==" the negative values weren't removed
}
return(dwls_results)
}
Expand Down Expand Up @@ -2203,6 +2203,10 @@ spot_deconvolution<-function(expr,
for(k in 1:(dim(cluster_cell_exp)[2])){
B<-Matrix::as.matrix(cluster_cell_exp[,k])
ct_spot_k<-rownames(cluster_i_matrix)[which(cluster_i_matrix[,k]==1)]
if (sum(B)==0 || length(ct_spot_k)==0){ ####* must include the case where all genes are 0
dwls_results[,colnames(cluster_cell_exp)[k]]<-NA ####* will produce NAs for some spots in the output
next; ####* no need to look into this spot any more
}
if (length(ct_spot_k)==1){
dwls_results[ct_spot_k[1],colnames(cluster_cell_exp)[k]]<-1
} else {
Expand All @@ -2213,16 +2217,20 @@ spot_deconvolution<-function(expr,
}
uniq_ct_k_gene<-intersect(rownames(ct_exp),unique(ct_k_gene))
S_k<-Matrix::as.matrix(ct_exp[uniq_ct_k_gene,ct_spot_k])
solDWLS<-optimize_solveDampenedWLS(S_k,B[uniq_ct_k_gene,],constant_J)
dwls_results[names(solDWLS),colnames(cluster_cell_exp)[k]]<-solDWLS
if (sum(B[uniq_ct_k_gene,])==0){ ####* must include the case all genes are 0
dwls_results[,colnames(cluster_cell_exp)[k]]<-NA ####* will produce NAs for some spots in the output
} else {
solDWLS<-optimize_solveDampenedWLS(S_k,B[uniq_ct_k_gene,],constant_J)
dwls_results[names(solDWLS),colnames(cluster_cell_exp)[k]]<-solDWLS
}
}
}
}
}
#####remove negative values
for (i in dim(dwls_results)[1]){
negtive_index<-which(dwls_results[i,]<0)
dwls_results[i,negtive_index]==0
dwls_results[i,negtive_index]=0 ####* fixed a typo, with "==" the negative values weren't removed
}
return(dwls_results)
}
Expand Down Expand Up @@ -2314,8 +2322,10 @@ optimize_deconvolute_dwls <- function(exp,
if (sum(B)>0){
solDWLS<-optimize_solveDampenedWLS(S,B,constant_J)
} else{
solDWLS <- rep(0, length(B))
names(solDWLS) <- names(B)
# solDWLS <- rep(0, length(B)) ####* wrong dimension, causes warnings
solDWLS <- rep(0, ncol(S)) ####* corrected dim
# names(solDWLS) <- names(B) ####* wrong dim name
names(solDWLS) <- colnames(S) ####* corrected dim name
}
allCounts_DWLS<-cbind(allCounts_DWLS,solDWLS)
}
Expand Down

0 comments on commit 0c157d6

Please sign in to comment.