Skip to content

Commit

Permalink
d0L0
Browse files Browse the repository at this point in the history
  • Loading branch information
noriakis committed Oct 26, 2023
1 parent a5f97ef commit 0a76a7f
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export(consensusSeqMIDAS2)
export(consensusSeqMIDAS2Slow)
export(doAdonis)
export(doBoruta)
export(doL0)
export(drawEGGNOG)
export(drawPATRIC)
export(exportInteractive)
Expand Down
58 changes: 58 additions & 0 deletions R/doL0.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#' doL0
#'
#' feature selection of classification between category
#' using snv frequency and gene matrix, based on L0Learn
#'
#' @param stana stana object
#' @param species candidate species ID
#' @param cl named list of category
#' @param target default to snps
#' @param mat if target is not snps, provide preprocessed gene matrix
#' otherwise the raw gene matrix is used.
#' @param deleteZeroDepth delete zero depth SNV
#' @param argList passed to L0Learn()
#' @return L0Learn object
#' @import ggplot2
#' @export
doL0 <- function(stana, species, cl=NULL, doFix=TRUE,
target="genes", mat=NULL, whichToCount="ec_description", argList=list(),
deleteZeroDepth=FALSE) {
if (length(argList)==0) {
qqcat("Penalty is set to L0L2 by default")
argList[["penalty"]] <- "L0L2"}
if (is.null(cl)) {
qqcat("Using grouping from the slot\n")
cl <- stana@cl
}
if (target!="snps" & is.null(mat)){
qqcat("If needed, please provide preprocessed matrix of genes to `mat`\n")
filtDf <- stana@genes[[species]]
} else if (target=="snps"){
filtDf <- stana@snps[[species]]
if (deleteZeroDepth) {
filtDf <- filtDf[rowSums(filtDf==-1)==0,]
qqcat("After filtering `-1`, position numbers: @{dim(filtDf)[1]}\n")
}
} else {
qqcat("Proceeding with provided matrix\n")
filtDf <- mat
}
qqcat("Feature number: @{dim(filtDf)[1]}\n")
transDf <- data.frame(t(filtDf), check.names=FALSE)
transDf <- transDf[intersect(row.names(transDf), cl |> unlist() |> unique()),]
gr <- NULL
for (cn in rownames(transDf)){
for (clm in seq_along(cl)){
if (cn %in% cl[[clm]]) {
gr <- c(gr, names(cl)[clm])
}
}
}


qqcat("Performing L0Learn\n")
argList[["x"]] <- transDf |> as.matrix()
argList[["y"]] <- as.factor(gr)
l0res <- do.call("L0Learn.cvfit", argList)
return(l0res)
}
41 changes: 41 additions & 0 deletions man/doL0.Rd

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

0 comments on commit 0a76a7f

Please sign in to comment.