From 85847307a66a9ab275f7a9cf42e26ceff3a621eb Mon Sep 17 00:00:00 2001 From: cweng Date: Thu, 23 Nov 2023 17:01:20 -0500 Subject: [PATCH] Add two Legacy function for add_dist_legacy and make_tree_legacy --- NAMESPACE | 4 + R/BuidTree.R | 123 ++++++++++++++++-- ...AddDatatoplot_clustering-redeemR-method.Rd | 2 +- man/AddDist-redeemR-method.Rd | 2 +- man/AddDist_legacy-mitoTracing-method.Rd | 48 +++++++ man/AddDist_legacy.Rd | 16 +++ man/AddTree-redeemR-method.Rd | 2 +- man/Add_DepthMatrix-redeemR-method.Rd | 2 +- man/Make_tree-redeemR-method.Rd | 4 +- man/Make_tree_legacy-mitoTracing-method.Rd | 23 ++++ man/Make_tree_legacy.Rd | 26 ++++ 11 files changed, 238 insertions(+), 14 deletions(-) create mode 100644 man/AddDist_legacy-mitoTracing-method.Rd create mode 100644 man/AddDist_legacy.Rd create mode 100644 man/Make_tree_legacy-mitoTracing-method.Rd create mode 100644 man/Make_tree_legacy.Rd diff --git a/NAMESPACE b/NAMESPACE index 1f4c9df..fc9bbe8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,6 +3,7 @@ export(ATAC_Wrapper) export(AddDatatoplot_clustering) export(AddDist) +export(AddDist_legacy) export(AddHemSignature) export(AddTree) export(Add_AssignVariant) @@ -31,6 +32,7 @@ export(Make_AnnTable) export(Make_Cells4Nodes) export(Make_matrix) export(Make_tree) +export(Make_tree_legacy) export(MergeMtx) export(Motifenrich.binom) export(Multi_Wrapper) @@ -73,12 +75,14 @@ exportClasses(mitoTracing) exportClasses(redeemR) exportMethods(AddDatatoplot_clustering) exportMethods(AddDist) +exportMethods(AddDist_legacy) exportMethods(AddTree) exportMethods(Add_AssignVariant) exportMethods(Add_DepthMatrix) exportMethods(Add_tree_cut) exportMethods(Make_matrix) exportMethods(Make_tree) +exportMethods(Make_tree_legacy) exportMethods(SeuratLSIClustering) exportMethods(show) import(Matrix) diff --git a/R/BuidTree.R b/R/BuidTree.R index e644f36..9e4b409 100755 --- a/R/BuidTree.R +++ b/R/BuidTree.R @@ -152,6 +152,12 @@ setGeneric(name="AddDatatoplot_clustering", def=function(object,...)standardGene #' @export setGeneric(name="AddDist", def=function(object,...) standardGeneric("AddDist")) +#' AddDist_legacy +#' This add Jaccard, Dice, Jaccard3W distance and stored in DistObjects +#' @param object mitoTracing class +#' @export +setGeneric(name="AddDist_legacy", def=function(object,...) standardGeneric("AddDist_legacy")) + #' Make_tree #' This will generate a basic phylogenetic tree #' @param object redeemR class @@ -160,6 +166,14 @@ setGeneric(name="AddDist", def=function(object,...) standardGeneric("AddDist")) #' @export setGeneric(name="Make_tree", def=function(object,d="jaccard", algorithm="upgma",onlyreturntree=F,...) standardGeneric("Make_tree")) +#' Make_tree_legacy +#' This will generate a basic phylogenetic tree +#' @param object mitoTracing class +#' @param d "jaccard" or "Dice" or "jaccard3W" +#' @param algorithm the algorithm used to build the tree, choose from "nj" and "upgma" +#' @export +setGeneric(name="Make_tree_legacy", def=function(object,d="jaccard", algorithm="upgma",onlyreturntree=F,...) standardGeneric("Make_tree_legacy")) + #' Add_Tree #' Optional, if a phylogentic tree object phylo is already available, can be directly added to the redeemR #' @param object redeemR class @@ -242,10 +256,10 @@ setMethod(f="Make_matrix", #' Make_tree #' This will generate a basic phylogenetic tree -#' @param object mitoTracin class +#' @param object redeemR class #' @param d "jaccard" or "Dice" or "jaccard3W" or "w_jaccard" "w_cosine" "LSIdist" #' @param algorithm the algorithm used to build the tree, choose from "nj" and "upgma" -#' @return mitoTracin class +#' @return redeemR class #' @export setMethod(f="Make_tree", signature="redeemR", @@ -266,7 +280,32 @@ setMethod(f="Make_tree", } }) - +#' Make_tree_legacy +#' This will generate a basic phylogenetic tree +#' @param object redeemR class +#' @param d "jaccard" or "Dice" or "jaccard3W" or "w_jaccard" "w_cosine" "LSIdist" +#' @param algorithm the algorithm used to build the tree, choose from "nj" and "upgma" +#' @return redeemR class +#' @export +setMethod(f="Make_tree_legacy", + signature="mitoTracing", + definition=function(object,d,algorithm,onlyreturntree=F){ + message("Make_tree_legacy is a legacy function what works for mitoTracing") + dist<-slot(object@DistObjects,d) + if(algorithm=="nj"){ + phylo<-nj(dist) + }else if (algorithm=="upgma"){ + phylo<-upgma(dist) + } + treedata<-as.treedata(phylo) + TREEobject<-new("TREE",phylo=phylo,treedata=treedata,records=paste(d,algorithm,sep="-")) + if(onlyreturntree){ + return(TREEobject) + }else{ + object@TREE<-TREEobject + return(object) + } +}) #' SeuratLSIClustering #' This will use the mito variants for Seurat clustering (LSI based) @@ -309,7 +348,7 @@ setMethod(f="SeuratLSIClustering", #' AddDatatoplot_clustering #' This prepare the clonal clustering data to plot -#' @param object mitoTracin class +#' @param object redeemR class #' @return redeemR class #' @export setMethod(f="AddDatatoplot_clustering", @@ -324,7 +363,7 @@ setMethod(f="AddDatatoplot_clustering", #' AddDist #' This add Jaccard, Dice, Jaccard3W distance and stored in DistObjects -#' @param object mitoTracin class +#' @param object redeemR class #' @param jaccard default=T #' @param dice default=T #' @param jaccard3w default=T @@ -390,13 +429,81 @@ setMethod(f="AddDist", return(object) }) +#' AddDist_legacy +#' This add Jaccard, Dice, Jaccard3W distance and stored in DistObjects +#' @param object mitoTracin class +#' @param jaccard default=T +#' @param dice default=T +#' @param jaccard3w default=T +#' @param w_jaccard default=T +#' @param w_cosine default=T +#' @param weight A two column dataframe, "Variant"(The variant name should match cell-variant matrix column, e.g, Variants310TC), "weight" (numeric) +#' @param NN To replace NA, which means a variant shown in the object is not shown in the weight vector, with a number, default is 1 for jaccard system. +#' @param LSIdist default=T +#' @param dim the dimensions to use to calculate LSI distance default is 2:50 +#' @return redeemR class +#' @export +setMethod(f="AddDist_legacy", + signature="mitoTracing", + definition=function(object,jaccard=T,dice=T,jaccard3w=T,w_jaccard=T,w_cosine=T,weightDF=NULL,NN=1,LSIdist=T,dim=2:50){ + message("AddDist_legacy is a legacy function what works for mitoTracing") + d.Jaccard<-NA + d.Dice<-NA + d.3WJaccard<-NA + d.w_jaccard<-NA + d.w_cosine<-NA + if(length(weightDF)!=0){ + weight<-data.frame(Variants=colnames(object@Cts.Mtx.bi)) %>% merge(.,weightDF,by="Variants",all.x = T,sort = F) %>% .$weight + } + if(length(which(is.na(weight)))!=0){ + weight[is.na(weight)]<-NN + print("Some variant i weight is not found in cell-variant matrix, use 1") + } + if(length(weight)!=ncol(object@Cts.Mtx.bi)){ + stop("The length of weight does not match the variant numbers in the martix") + } + print("Weight vector matches well with the Cell-Variant matrix, continue...") + if(jaccard){ + d.Jaccard<-BinaryDist(object@Cts.Mtx.bi,method="Jaccard") + message("jaccard distances added") + } + if(dice){ + d.Dice<-BinaryDist(object@Cts.Mtx.bi,method="Dice") + message("dice distances added") + } + if(jaccard3w){ + d.3WJaccard<-BinaryDist(object@Cts.Mtx.bi,method="3WJaccard") + message("3wjaccard distances added") + } + if(w_jaccard){ + if(length(weightDF)==0){ + stop("Please input the weight, otherwise turn off the w_jaccard") + + } + d.w_jaccard<-quick_w_jaccard(object@Cts.Mtx.bi,w=weight) + message("weighted jaccard distances added") + } + if(w_cosine){ + if(length(weightDF)==0){ + stop("Please input the weight, otherwise turn off the w_cosine") + } + d.w_cosine<-quick_w_cosine(object@Cts.Mtx.bi,w=weight) + message("weighted cosine distances added") + } + if(LSIdist){ + d.lsi<-dist(object@Seurat@reductions$lsi@cell.embeddings[,dim]) + message("LSI distances added") + } + object@DistObjects<-new("DistObjects",jaccard=d.Jaccard, Dice=d.Dice,jaccard3W=d.3WJaccard,w_jaccard=d.w_jaccard,w_cosine=d.w_cosine,LSIdist=d.lsi) + return(object) + }) #' Add_Tree #' Optional, if a phylogentic tree object phylo is already available, can be directly added to the redeemR class in slot TREE -#' @param object mitoTracin class +#' @param object redeemR class #' @param phylo phyogenetic tree object -#' @param object mitoTracin class +#' @param object redeemR class #' @return redeemR class #' @export setMethod(f="AddTree", @@ -411,7 +518,7 @@ setMethod(f="AddTree", #' Add_DepthMatrix #' Optional, add a matrix with same dimension with the Cts.Mtx and Cts.Mtx.bi, which display the depths -#' @param object mitoTracin class +#' @param object redeemR class #' @param QualifiedTotalCts a big source data, usually at XXX/mitoV/final, If needed, edit V1, the cell name, which may have additional postfix due to combine #' @return redeemR class #' @export diff --git a/man/AddDatatoplot_clustering-redeemR-method.Rd b/man/AddDatatoplot_clustering-redeemR-method.Rd index 1f1f1f2..622707e 100644 --- a/man/AddDatatoplot_clustering-redeemR-method.Rd +++ b/man/AddDatatoplot_clustering-redeemR-method.Rd @@ -8,7 +8,7 @@ This prepare the clonal clustering data to plot} \S4method{AddDatatoplot_clustering}{redeemR}(object) } \arguments{ -\item{object}{mitoTracin class} +\item{object}{redeemR class} } \value{ redeemR class diff --git a/man/AddDist-redeemR-method.Rd b/man/AddDist-redeemR-method.Rd index 4636ef7..bfd7a9d 100644 --- a/man/AddDist-redeemR-method.Rd +++ b/man/AddDist-redeemR-method.Rd @@ -19,7 +19,7 @@ This add Jaccard, Dice, Jaccard3W distance and stored in DistObjects} ) } \arguments{ -\item{object}{mitoTracin class} +\item{object}{redeemR class} \item{jaccard}{default=T} diff --git a/man/AddDist_legacy-mitoTracing-method.Rd b/man/AddDist_legacy-mitoTracing-method.Rd new file mode 100644 index 0000000..9245e2c --- /dev/null +++ b/man/AddDist_legacy-mitoTracing-method.Rd @@ -0,0 +1,48 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/BuidTree.R +\name{AddDist_legacy,mitoTracing-method} +\alias{AddDist_legacy,mitoTracing-method} +\title{AddDist_legacy +This add Jaccard, Dice, Jaccard3W distance and stored in DistObjects} +\usage{ +\S4method{AddDist_legacy}{mitoTracing}( + object, + jaccard = T, + dice = T, + jaccard3w = T, + w_jaccard = T, + w_cosine = T, + weightDF = NULL, + NN = 1, + LSIdist = T, + dim = 2:50 +) +} +\arguments{ +\item{object}{mitoTracin class} + +\item{jaccard}{default=T} + +\item{dice}{default=T} + +\item{jaccard3w}{default=T} + +\item{w_jaccard}{default=T} + +\item{w_cosine}{default=T} + +\item{NN}{To replace NA, which means a variant shown in the object is not shown in the weight vector, with a number, default is 1 for jaccard system.} + +\item{LSIdist}{default=T} + +\item{dim}{the dimensions to use to calculate LSI distance default is 2:50} + +\item{weight}{A two column dataframe, "Variant"(The variant name should match cell-variant matrix column, e.g, Variants310TC), "weight" (numeric)} +} +\value{ +redeemR class +} +\description{ +AddDist_legacy +This add Jaccard, Dice, Jaccard3W distance and stored in DistObjects +} diff --git a/man/AddDist_legacy.Rd b/man/AddDist_legacy.Rd new file mode 100644 index 0000000..cbe2e0a --- /dev/null +++ b/man/AddDist_legacy.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/BuidTree.R +\name{AddDist_legacy} +\alias{AddDist_legacy} +\title{AddDist_legacy +This add Jaccard, Dice, Jaccard3W distance and stored in DistObjects} +\usage{ +AddDist_legacy(object, ...) +} +\arguments{ +\item{object}{mitoTracing class} +} +\description{ +AddDist_legacy +This add Jaccard, Dice, Jaccard3W distance and stored in DistObjects +} diff --git a/man/AddTree-redeemR-method.Rd b/man/AddTree-redeemR-method.Rd index 502ca6c..59560a2 100644 --- a/man/AddTree-redeemR-method.Rd +++ b/man/AddTree-redeemR-method.Rd @@ -8,7 +8,7 @@ Optional, if a phylogentic tree object phylo is already available, can be direct \S4method{AddTree}{redeemR}(object, phylo, record = "") } \arguments{ -\item{object}{mitoTracin class} +\item{object}{redeemR class} \item{phylo}{phyogenetic tree object} } diff --git a/man/Add_DepthMatrix-redeemR-method.Rd b/man/Add_DepthMatrix-redeemR-method.Rd index 15b5cf6..a6c5522 100644 --- a/man/Add_DepthMatrix-redeemR-method.Rd +++ b/man/Add_DepthMatrix-redeemR-method.Rd @@ -8,7 +8,7 @@ Optional, add a matrix with same dimension with the Cts.Mtx and Cts.Mtx.bi, whic \S4method{Add_DepthMatrix}{redeemR}(object) } \arguments{ -\item{object}{mitoTracin class} +\item{object}{redeemR class} \item{QualifiedTotalCts}{a big source data, usually at XXX/mitoV/final, If needed, edit V1, the cell name, which may have additional postfix due to combine} } diff --git a/man/Make_tree-redeemR-method.Rd b/man/Make_tree-redeemR-method.Rd index 9425ae0..36849dd 100644 --- a/man/Make_tree-redeemR-method.Rd +++ b/man/Make_tree-redeemR-method.Rd @@ -8,14 +8,14 @@ This will generate a basic phylogenetic tree} \S4method{Make_tree}{redeemR}(object, d, algorithm, onlyreturntree = F) } \arguments{ -\item{object}{mitoTracin class} +\item{object}{redeemR class} \item{d}{"jaccard" or "Dice" or "jaccard3W" or "w_jaccard" "w_cosine" "LSIdist"} \item{algorithm}{the algorithm used to build the tree, choose from "nj" and "upgma"} } \value{ -mitoTracin class +redeemR class } \description{ Make_tree diff --git a/man/Make_tree_legacy-mitoTracing-method.Rd b/man/Make_tree_legacy-mitoTracing-method.Rd new file mode 100644 index 0000000..c5561cb --- /dev/null +++ b/man/Make_tree_legacy-mitoTracing-method.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/BuidTree.R +\name{Make_tree_legacy,mitoTracing-method} +\alias{Make_tree_legacy,mitoTracing-method} +\title{Make_tree_legacy +This will generate a basic phylogenetic tree} +\usage{ +\S4method{Make_tree_legacy}{mitoTracing}(object, d, algorithm, onlyreturntree = F) +} +\arguments{ +\item{object}{redeemR class} + +\item{d}{"jaccard" or "Dice" or "jaccard3W" or "w_jaccard" "w_cosine" "LSIdist"} + +\item{algorithm}{the algorithm used to build the tree, choose from "nj" and "upgma"} +} +\value{ +redeemR class +} +\description{ +Make_tree_legacy +This will generate a basic phylogenetic tree +} diff --git a/man/Make_tree_legacy.Rd b/man/Make_tree_legacy.Rd new file mode 100644 index 0000000..4424323 --- /dev/null +++ b/man/Make_tree_legacy.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/BuidTree.R +\name{Make_tree_legacy} +\alias{Make_tree_legacy} +\title{Make_tree_legacy +This will generate a basic phylogenetic tree} +\usage{ +Make_tree_legacy( + object, + d = "jaccard", + algorithm = "upgma", + onlyreturntree = F, + ... +) +} +\arguments{ +\item{object}{mitoTracing class} + +\item{d}{"jaccard" or "Dice" or "jaccard3W"} + +\item{algorithm}{the algorithm used to build the tree, choose from "nj" and "upgma"} +} +\description{ +Make_tree_legacy +This will generate a basic phylogenetic tree +}