Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
treediff
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Admin message
A compter du 1er avril, attention à vos pipelines :
Nouvelles limitations de Docker Hub
Show more breadcrumbs
SCALES
treediff
Commits
cbac3d7e
Commit
cbac3d7e
authored
2 years ago
by
CARDENAS GWENDAELLE
Browse files
Options
Downloads
Patches
Plain Diff
add scale and order_labels
parent
c0217284
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
R/treediff.R
+65
-12
65 additions, 12 deletions
R/treediff.R
with
65 additions
and
12 deletions
R/treediff.R
+
65
−
12
View file @
cbac3d7e
...
...
@@ -2,6 +2,9 @@
#'
#' @description Perform the treediff test to compare two sets of trees.
#'
#' @aliases summary.treediff
#' @aliases print.treeTest
#'
#' @details This function compares two sets of trees using a p-value aggregation
#' method. The p-values are obtained by the treediff method, as described in
#' (Neuvial \emph{et al.}, 2023).
...
...
@@ -16,8 +19,13 @@
#' different from that of \code{trees1}.
#' @param replicates A numeric vector of length 2 with the number of replicates
#' for each condition.
#' @param scale Logical. If \code{TRUE}, the trees are all rescalled to have a
#' minimum height equal to 0 (translation) and a maximum height equal to 1
#' (dilatation).
#' @param order_labels Logical. If \code{TRUE}, take a reference tree and keep
#' track of leave order.
#'
#' @return An object of class \code{treeTest} with the following entries:
#' @return An object of class \code{treeTest} with the following entries:
#' \itemize{
#' \item{p.value}{ the p-value for the treediff test.}
#' \item{statistic}{ the value of the Student's statistic of each leaf pair of
...
...
@@ -30,7 +38,7 @@
#' conditions.}
#' }
#'
#' @author Gwenda
e
lle Cardena
c
\cr
#' @author Gwenda
ë
lle Cardena
s
\cr
#' Marie Chavent \email{marie.chavent@u-bordeaux.fr}\cr
#' Sylvain Foissac \email{sylvain.foissac@inrae.fr}\cr
#' Pierre Neuvial \email{pierre.neuvial@math.univ-toulouse.fr}\cr
...
...
@@ -88,7 +96,9 @@
#' ## 4 p-values, one for each cluster
#' tree_pvals$p.value
treediff
<-
function
(
trees1
,
trees2
,
replicates
)
{
treediff
<-
function
(
trees1
,
trees2
,
replicates
,
scale
=
FALSE
,
order_labels
=
FALSE
)
{
# Check if `replicates` is numeric vector
if
(
inherits
(
replicates
,
"numeric"
)
!=
TRUE
)
{
stop
(
"`replicates` is not a numeric vector"
)
...
...
@@ -101,8 +111,8 @@ treediff <- function(trees1, trees2, replicates) {
# Check if the number of clusters is equal for both conditions
if
(
length
(
trees1
)
/
replicates
[
1
]
!=
length
(
trees2
)
/
replicates
[
2
])
{
stop
(
paste
(
"The number of clusters is different between conditions (or"
,
"`replicates` is not correct)."
)
)
stop
(
"The number of clusters is different between conditions (or
"
,
"`replicates` is not correct)."
)
}
# Check the number of leaves is the same for each cluster
...
...
@@ -120,14 +130,36 @@ treediff <- function(trees1, trees2, replicates) {
# Merge trees from both conditions
trees
<-
c
(
trees1
,
trees2
)
## if order_labels is TRUE, take a reference tree and keep track of leave order
if
(
order_labels
)
{
ref_order
<-
trees1
[[
1
]]
$
labels
labels_perm
<-
lapply
(
trees
,
function
(
atree
)
match
(
ref_order
,
atree
$
labels
))
}
# Compute cophenetic distance
coph_dist
<-
sapply
(
trees
,
cophenetic
,
simplify
=
FALSE
)
# normalize
if
(
scale
)
{
coph_dist
<-
normalize_trees
(
coph_dist
)
}
# Convert cophenetic distances to vector
coph_vect
<-
lapply
(
coph_dist
,
function
(
adist
)
{
adist
<-
as.matrix
(
adist
)
return
(
adist
[
upper.tri
(
adist
)])
})
if
(
order_labels
)
{
coph_vect
<-
mapply
(
function
(
adist
,
anorder
)
{
adist
<-
as.matrix
(
adist
)
adist
<-
adist
[
anorder
,
anorder
]
return
(
adist
[
upper.tri
(
adist
)])
},
coph_dist
,
labels_perm
)
coph_vect
<-
lapply
(
1
:
ncol
(
coph_vect
),
function
(
col
)
{
return
(
as.vector
(
coph_vect
[,
col
]))
})
}
else
{
coph_vect
<-
lapply
(
coph_dist
,
function
(
adist
)
{
adist
<-
as.matrix
(
adist
)
return
(
adist
[
upper.tri
(
adist
)])
})
}
# Compute squeeze factor
outs
<-
compute_squeeze
(
coph_vect
,
replicates
)
...
...
@@ -156,13 +188,25 @@ treediff <- function(trees1, trees2, replicates) {
return
(
out
)
}
print.treeTest
<-
function
(
x
,
prefix
=
"\t"
,
...
){
#' @export
#' @rdname treediff
print.treeTest
<-
function
(
x
,
prefix
=
"\t"
,
...
)
{
cat
(
"\n"
)
cat
(
strwrap
(
x
$
method
,
prefix
=
prefix
),
sep
=
"\n"
)
cat
(
"\n"
)
cat
(
"data: "
,
x
$
data.name
,
"\n"
,
sep
=
""
)
options
(
"max.print"
=
5
)
print
(
colsplit
(
x
$
p.value
,
" "
,
"p.value :"
))
print
(
colsplit
(
x
$
p.value
,
" "
,
"p.values :"
),
max
=
5
)
}
#' @method summary treediff
#' @export
#' @rdname treediff
summary.treediff
<-
function
(
object
,
...
)
{
cat
(
"\nSummary\n\n"
)
cat
(
" Class : "
,
class
(
object
),
"\n\n"
)
print
(
object
)
}
compute_squeeze
<-
function
(
dist_coph
,
replicates
)
{
...
...
@@ -261,3 +305,12 @@ compute_pvalue <- function(average_coph, squeezed_var, replicates) {
# Return the output data frame
return
(
out
)
}
normalize_trees
<-
function
(
coph_dist
)
{
# tree_cophs is a list of 'dist' objects as returned by the function cophenetic
all_minima
<-
sapply
(
coph_dist
,
min
)
out
<-
mapply
(
"-"
,
coph_dist
,
all_minima
,
SIMPLIFY
=
FALSE
)
all_maxima
<-
sapply
(
out
,
max
)
out
<-
mapply
(
"/"
,
out
,
all_maxima
,
SIMPLIFY
=
FALSE
)
return
(
out
)
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment