-
Notifications
You must be signed in to change notification settings - Fork 3
/
.Rhistory
477 lines (477 loc) · 21.2 KB
/
.Rhistory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
setwd("/Users/fmazel/Documents/GitHub/Microbiome_Phylo_Diversity_Workshop")
Tree=read.tree('My_outputs/Saanish_FastTree')
library(ape)
source("./R functions/BDTT_functions.R")
Tree=read.tree('My_outputs/Saanish_FastTree')
getwd()
rm(list=ls())
source("BDTT_functions.R")
library(picante)
data(phylocom)
TreeExmple=phylocom$phylo
plot(TreeExmple)
SiteSpExmple=phylocom$sample
SiteSpExmple
getHnodes(TreeExmple)
hist(getHnodes(TreeExmple))
SiteSpExample=phylocom$sample
SiteSpExample
BDTT(similarity_slices = c(0:3),tree = TreeExample,sampleOTUs = SiteSpExample)
data(phylocom)
TreeExample=phylocom$phylo
plot(TreeExample)
SiteSpExample=phylocom$sample
SiteSpExample
hist(getHnodes(TreeExmple))
BDTT(similarity_slices = c(0:3),tree = TreeExample,sampleOTUs = SiteSpExample)
library(ape)
library(betapart)
library(abind)
library(Matrix)
BDTT(similarity_slices = c(0:3),tree = TreeExample,sampleOTUs = SiteSpExample)
BDTT(similarity_slices = c(0:3),tree = TreeExample,sampleOTUs = t(SiteSpExample))
SiteSpExample
Betas=BDTT(similarity_slices = c(0:3),tree = TreeExample,sampleOTUs = t(SiteSpExample))
Betas
# Function to get Info on Edges
#---------------------------------
getTreeInfo=function(tr2)
{
NH=node.depth.edgelength(tr2)
treeH=max(NH)
edgeInfo=cbind(tr2$edge,treeH-NH[tr2$edge[,1]],treeH-NH[tr2$edge[,2]])
row.names(edgeInfo)=as.character(1:dim(edgeInfo)[1])
return(edgeInfo)
}
# Function to get all branches and their descendant node at a given slice
#------------------------------------------------------------------------
GetBranchNode=function(slice,edgeInfo) {
a=(edgeInfo[,3]>=slice)*(edgeInfo[,4]<=slice)
return(edgeInfo[a==1,])
}
# Functions to fill the branch*sites matrices
# (sites can be environmental sites or hosts for microbial communities)
#----------------------------------------------------------------------
vectorPres=function(node,tree)
{
pres=rep(0,length(tree$tip.label))
names(pres)=tree$tip.label
pres[clade.members(node,tree,tip.label=T)]=1
return(pres)
}
vectorPresBigMat=function(nodes,tree,mat)
{
for (i in nodes)
{
#print(100*match(i,nodes)/length(nodes))
dd=clade.members(i,tree,tip.label=T)
mat[as.character(i),dd]=1
}
return(mat)
}
# Function 'GetBranchOcc': computes a Branch*Sites matrix and directly save it in a file (not in Renv)
# Branch*sites matrices are also frequently named 'OTU tables' in the field of microbiology
#--------------------------------------------------------------------------------------------------------
# slice: the age of the desired slice
# tree: the community phylogenetic tree (must be ultrametric)
# sitesp: the site * species matrice. Species names must match the tip names in the phylogenetic tree
# In microbiology, sitesp is equivalent to the OTU table determining the distribution of unique 16S sequences across samples (100% similarity OTUs)
# pathtoSaveBranchOcc: directory where Branch*sites matrices are stored
# bigmatrix=F: if site*species is a VERY big matrix, it is highly recommended to set bigmatrix=T (use of the bigmemory package)
GetBranchOcc=function(slice,tree,sitesp,pathtoSaveBranchOcc,bigmatrix=F)
{
edgeInfo=getTreeInfo(tree)
if (slice==0)
{OTUmat=sitesp}
else
{
brCorres=GetBranchNode(slice=slice,edgeInfo=edgeInfo)
NodeDes=brCorres[,2]
#print("getting OTUS host matrix")
OTUnames=as.character(brCorres[,2])
if (bigmatrix==F) {branchPAtotal=matrix(0,ncol=length(tree$tip.label),nrow=length(NodeDes),dimnames=list(OTUnames,tree$tip.label))}
else if (bigmatrix==T) {branchPAtotal=filebacked.big.matrix(ncol=length(tree$tip.label),nrow=length(NodeDes),dimnames=list(OTUnames,tree$tip.label),backingfile = paste("branchPAtotal",slice,sep=""),backingpath =paste(pathtoSaveBranchOcc), descriptorfile = paste("branchPAtotalsauv",slice,sep=""))}
mat1=vectorPresBigMat(node=NodeDes,tree=tree,mat=branchPAtotal)
mat1=mat1[,colnames(sitesp)]
mat2=as.matrix(mat1)
mat2=t(mat2)
occM=as.matrix(sitesp)
OTUmat=(occM)%*%(mat2)
}
save(OTUmat,file=paste(pathtoSaveBranchOcc,"Branch_Site_matrix_SliceNo",slice,".rdata",sep=""))
}
# Function to compute raw Jaccard and Sorensen Beta diversities
#----------------------------------------------------------
getBeta=function(mat,ab=F)
{
if (ab==T)
{
h=bray.part(data.frame(mat))
bctu=as.matrix(h[[1]])
bcne=as.matrix(h[[2]])
bc=as.matrix(h[[3]])
res=abind(bctu,bcne,bc,along=0)
dimnames(res)[[1]]=c("bctu","bcne","bc")
}
if (ab==F)
{
h=beta.pair(data.frame(mat), index.family="jaccard")
hh=beta.pair(data.frame(mat), index.family="sorensen")
jtu=as.matrix(h[[1]])
jne=as.matrix(h[[2]])
jac=as.matrix(h[[3]])
stu=as.matrix(hh[[1]])
sne=as.matrix(hh[[2]])
sor=as.matrix(hh[[3]])
res=abind(jtu,jne,jac,stu,sne,sor,along=0)
dimnames(res)[[1]]=c("jtu","jne","jac","stu","sne","sor")
}
return(res)
}
# Function 'GetBetaDiv' to compute Beta-diversities from site*species matrices and to directly save the matrix of beta-diversities
#---------------------------------------------------------------------------------------------------------------------------------
# INPUT VARIABLES
# slice: the age of the desired slice
# pathtoGetBranchOcc: the input file where the branch*sites matrix is saved (result of the function GetBranchOcc)
# pathtoSaveBeta: the output file where the matrices of beta diversities are saved (several beta-diversity metrics are used)
# bigmatrix=F: if you used bigmatrix=T to create the branch*sites matrix with 'GetBranchOcc' (OTU table), use bigmatrix=T again.
# OUTPUT
# save Beta diversity matrices as a 3D array in 'pathtoSaveBeta'
# Array = array of sites*sites*beta diversity metrics
#beta diversity metrics
#"jtu" : True Turnover component of Jaccard (Presence/Absence)
#"jne" : Nestedness component of Jaccard (Presence/Absence)
#"jac" : Jaccard (Presence/Absence)
#"stu" : True Turnover component of Sorensen (Presence/Absence)
#"sne" : Nestedness component of Sorensen (Presence/Absence)
#"sor" : Sorensen (Presence/Absence)
#"bctu" : True Turnover component of Bray-Curtis (Abundance version of Sorensen)
#"bcne" : Nestedness component of Bray-Curtis (Abundance version of Sorensen)
#"bc" : Bray-Curtis (Abundance version of Sorensen)
GetBetaDiv=function(slice,pathtoGetBranchOcc,pathtoSaveBeta)
{
load(paste(pathtoGetBranchOcc,"Branch_Site_matrix_SliceNo",slice,".rdata",sep=""))
OTUmatPA=OTUmat
OTUmatPA[OTUmatPA>0]=1
Be1=getBeta(OTUmatPA,ab=F)
Be=getBeta(OTUmat,ab=T)
Betaa=abind(Be1,Be,along=1)
save(Betaa,file=paste(pathtoSaveBeta,"BetaDiv_BetaDivSliceNo",slice,".rdata",sep=""))
}
# FUNCTION 'GetCorrelations': computes correlations between beta-diversities and environmental distances at the desired slice
#----------------------------------------------------------------------------------------------------------------------------
# INPUT VARIABLES
# slice: the age of the desired slice
# indice: the betadiv metric you want:
# "jtu": True Turnover component of Jaccard (Presence/Absence)
# "jne": Nestedness component of Jaccard (Presence/Absence)
# "jac": Jaccard (Presence/Absence)
# "stu": True Turnover component of Sorensen (Presence/Absence)
# "sne": Nestedness component of Sorensen (Presence/Absence)
# "sor": Sorensen (Presence/Absence)
# "bctu": True Turnover component of Bray-Curtis (Abundance version of Sorensen)
# "bcne": Nestedness component of Bray-Curtis (Abundance version of Sorensen)
# "bc": Bray-Curtis (Abundance version of Sorensen)
# pathtoGetBeta : the file where Beta-Diversity matrices were previously stored (output of the 'GetBetaDiv' function)
# EnvDist: matrix of environmental distances. (e.g. geographic or climatic distances between sites, dietary or phylogenetic distances between hosts, etc).
# TypeofMantel: type of correlation used to run the Mantel test (either "Spearman" or "Pearson")
# nperm: number of permutations to perform to compute a p-value
# OUTPUT
# A 2*n matrix with R2 coefficients and their associated p-values
GetCorrelations=function(slice,indice="sor",pathtoGetBeta="",EnvDist,TypeofMantel="Spearman",nperm=1000)
{
load(file=paste(pathtoGetBeta,"BetaDiv_BetaDivSliceNo",slice,".rdata",sep=""))
#get same sites
site=intersect(dimnames(Betaa)[[2]],colnames(EnvDist))
Betaa=Betaa[indice,site,site]
EnvDist=EnvDist[site,site]
if (TypeofMantel=="Spearman") { multiMant_SE=MRM(as.dist(Betaa)~as.dist(EnvDist),nperm = nperm,mrank=T)}
if (TypeofMantel=="Pearson") { multiMant_SE=MRM(as.dist(Betaa)~as.dist(EnvDist),nperm = nperm)}
return(multiMant_SE$r.squared)
}
slices=seq(from=0,to=3, by=1) # first, time slices are defined
a<-lapply(slices,GetBranchOcc,tree=TreeExmple,sitesp=SiteSpExmple,pathtoSaveBranchOcc="",bigmatrix=F)
library(caper)
a<-lapply(slices,GetBranchOcc,tree=TreeExmple,sitesp=SiteSpExmple,pathtoSaveBranchOcc="",bigmatrix=F)
a
load("/Users/fmazel/Documents/GitHub/BDTT/Branch_Site_matrix_SliceNo3.rdata")
load("/Users/fmazel/Documents/GitHub/BDTT/Branch_Site_matrix_SliceNo3.rdata")
load("/Users/fmazel/Documents/GitHub/BDTT/Branch_Site_matrix_SliceNo2.rdata")
rm(list=ls())
load("/Users/fmazel/Documents/GitHub/BDTT/Branch_Site_matrix_SliceNo2.rdata")
data(phylocom)
TreeExmple=phylocom$phylo
plot(TreeExmple)
SiteSpExmple=phylocom$sample
SiteSpExmple
a<-lapply(slices,GetBetaDiv,pathtoGetBranchOcc="",pathtoSaveBeta="")
# Function to get Info on Edges
#---------------------------------
getTreeInfo=function(tr2)
{
NH=node.depth.edgelength(tr2)
treeH=max(NH)
edgeInfo=cbind(tr2$edge,treeH-NH[tr2$edge[,1]],treeH-NH[tr2$edge[,2]])
row.names(edgeInfo)=as.character(1:dim(edgeInfo)[1])
return(edgeInfo)
}
# Function to get all branches and their descendant node at a given slice
#------------------------------------------------------------------------
GetBranchNode=function(slice,edgeInfo) {
a=(edgeInfo[,3]>=slice)*(edgeInfo[,4]<=slice)
return(edgeInfo[a==1,])
}
# Functions to fill the branch*sites matrices
# (sites can be environmental sites or hosts for microbial communities)
#----------------------------------------------------------------------
vectorPres=function(node,tree)
{
pres=rep(0,length(tree$tip.label))
names(pres)=tree$tip.label
pres[clade.members(node,tree,tip.label=T)]=1
return(pres)
}
vectorPresBigMat=function(nodes,tree,mat)
{
for (i in nodes)
{
#print(100*match(i,nodes)/length(nodes))
dd=clade.members(i,tree,tip.label=T)
mat[as.character(i),dd]=1
}
return(mat)
}
# Function 'GetBranchOcc': computes a Branch*Sites matrix and directly save it in a file (not in Renv)
# Branch*sites matrices are also frequently named 'OTU tables' in the field of microbiology
#--------------------------------------------------------------------------------------------------------
# slice: the age of the desired slice
# tree: the community phylogenetic tree (must be ultrametric)
# sitesp: the site * species matrice. Species names must match the tip names in the phylogenetic tree
# In microbiology, sitesp is equivalent to the OTU table determining the distribution of unique 16S sequences across samples (100% similarity OTUs)
# pathtoSaveBranchOcc: directory where Branch*sites matrices are stored
# bigmatrix=F: if site*species is a VERY big matrix, it is highly recommended to set bigmatrix=T (use of the bigmemory package)
GetBranchOcc=function(slice,tree,sitesp,pathtoSaveBranchOcc,bigmatrix=F)
{
edgeInfo=getTreeInfo(tree)
if (slice==0)
{OTUmat=sitesp}
else
{
brCorres=GetBranchNode(slice=slice,edgeInfo=edgeInfo)
NodeDes=brCorres[,2]
#print("getting OTUS host matrix")
OTUnames=as.character(brCorres[,2])
if (bigmatrix==F) {branchPAtotal=matrix(0,ncol=length(tree$tip.label),nrow=length(NodeDes),dimnames=list(OTUnames,tree$tip.label))}
else if (bigmatrix==T) {branchPAtotal=filebacked.big.matrix(ncol=length(tree$tip.label),nrow=length(NodeDes),dimnames=list(OTUnames,tree$tip.label),backingfile = paste("branchPAtotal",slice,sep=""),backingpath =paste(pathtoSaveBranchOcc), descriptorfile = paste("branchPAtotalsauv",slice,sep=""))}
mat1=vectorPresBigMat(node=NodeDes,tree=tree,mat=branchPAtotal)
mat1=mat1[,colnames(sitesp)]
mat2=as.matrix(mat1)
mat2=t(mat2)
occM=as.matrix(sitesp)
OTUmat=(occM)%*%(mat2)
}
save(OTUmat,file=paste(pathtoSaveBranchOcc,"Branch_Site_matrix_SliceNo",slice,".rdata",sep=""))
}
# Function to compute raw Jaccard and Sorensen Beta diversities
#----------------------------------------------------------
getBeta=function(mat,ab=F)
{
if (ab==T)
{
h=bray.part(data.frame(mat))
bctu=as.matrix(h[[1]])
bcne=as.matrix(h[[2]])
bc=as.matrix(h[[3]])
res=abind(bctu,bcne,bc,along=0)
dimnames(res)[[1]]=c("bctu","bcne","bc")
}
if (ab==F)
{
h=beta.pair(data.frame(mat), index.family="jaccard")
hh=beta.pair(data.frame(mat), index.family="sorensen")
jtu=as.matrix(h[[1]])
jne=as.matrix(h[[2]])
jac=as.matrix(h[[3]])
stu=as.matrix(hh[[1]])
sne=as.matrix(hh[[2]])
sor=as.matrix(hh[[3]])
res=abind(jtu,jne,jac,stu,sne,sor,along=0)
dimnames(res)[[1]]=c("jtu","jne","jac","stu","sne","sor")
}
return(res)
}
# Function 'GetBetaDiv' to compute Beta-diversities from site*species matrices and to directly save the matrix of beta-diversities
#---------------------------------------------------------------------------------------------------------------------------------
# INPUT VARIABLES
# slice: the age of the desired slice
# pathtoGetBranchOcc: the input file where the branch*sites matrix is saved (result of the function GetBranchOcc)
# pathtoSaveBeta: the output file where the matrices of beta diversities are saved (several beta-diversity metrics are used)
# bigmatrix=F: if you used bigmatrix=T to create the branch*sites matrix with 'GetBranchOcc' (OTU table), use bigmatrix=T again.
# OUTPUT
# save Beta diversity matrices as a 3D array in 'pathtoSaveBeta'
# Array = array of sites*sites*beta diversity metrics
#beta diversity metrics
#"jtu" : True Turnover component of Jaccard (Presence/Absence)
#"jne" : Nestedness component of Jaccard (Presence/Absence)
#"jac" : Jaccard (Presence/Absence)
#"stu" : True Turnover component of Sorensen (Presence/Absence)
#"sne" : Nestedness component of Sorensen (Presence/Absence)
#"sor" : Sorensen (Presence/Absence)
#"bctu" : True Turnover component of Bray-Curtis (Abundance version of Sorensen)
#"bcne" : Nestedness component of Bray-Curtis (Abundance version of Sorensen)
#"bc" : Bray-Curtis (Abundance version of Sorensen)
GetBetaDiv=function(slice,pathtoGetBranchOcc,pathtoSaveBeta)
{
load(paste(pathtoGetBranchOcc,"Branch_Site_matrix_SliceNo",slice,".rdata",sep=""))
OTUmatPA=OTUmat
OTUmatPA[OTUmatPA>0]=1
Be1=getBeta(OTUmatPA,ab=F)
Be=getBeta(OTUmat,ab=T)
Betaa=abind(Be1,Be,along=1)
save(Betaa,file=paste(pathtoSaveBeta,"BetaDiv_BetaDivSliceNo",slice,".rdata",sep=""))
}
# FUNCTION 'GetCorrelations': computes correlations between beta-diversities and environmental distances at the desired slice
#----------------------------------------------------------------------------------------------------------------------------
# INPUT VARIABLES
# slice: the age of the desired slice
# indice: the betadiv metric you want:
# "jtu": True Turnover component of Jaccard (Presence/Absence)
# "jne": Nestedness component of Jaccard (Presence/Absence)
# "jac": Jaccard (Presence/Absence)
# "stu": True Turnover component of Sorensen (Presence/Absence)
# "sne": Nestedness component of Sorensen (Presence/Absence)
# "sor": Sorensen (Presence/Absence)
# "bctu": True Turnover component of Bray-Curtis (Abundance version of Sorensen)
# "bcne": Nestedness component of Bray-Curtis (Abundance version of Sorensen)
# "bc": Bray-Curtis (Abundance version of Sorensen)
# pathtoGetBeta : the file where Beta-Diversity matrices were previously stored (output of the 'GetBetaDiv' function)
# EnvDist: matrix of environmental distances. (e.g. geographic or climatic distances between sites, dietary or phylogenetic distances between hosts, etc).
# TypeofMantel: type of correlation used to run the Mantel test (either "Spearman" or "Pearson")
# nperm: number of permutations to perform to compute a p-value
# OUTPUT
# A 2*n matrix with R2 coefficients and their associated p-values
GetCorrelations=function(slice,indice="sor",pathtoGetBeta="",EnvDist,TypeofMantel="Spearman",nperm=1000)
{
load(file=paste(pathtoGetBeta,"BetaDiv_BetaDivSliceNo",slice,".rdata",sep=""))
#get same sites
site=intersect(dimnames(Betaa)[[2]],colnames(EnvDist))
Betaa=Betaa[indice,site,site]
EnvDist=EnvDist[site,site]
if (TypeofMantel=="Spearman") { multiMant_SE=MRM(as.dist(Betaa)~as.dist(EnvDist),nperm = nperm,mrank=T)}
if (TypeofMantel=="Pearson") { multiMant_SE=MRM(as.dist(Betaa)~as.dist(EnvDist),nperm = nperm)}
return(multiMant_SE$r.squared)
}
a<-lapply(slices,GetBetaDiv,pathtoGetBranchOcc="",pathtoSaveBeta="")
slices=seq(from=0,to=3, by=1) # first, time slices are defined
a<-lapply(slices,GetBetaDiv,pathtoGetBranchOcc="",pathtoSaveBeta="")
a
load("/Users/fmazel/Documents/GitHub/BDTT/BetaDiv_BetaDivSliceNo3.rdata")
data(phylocom)
TreeExample=phylocom$phylo
plot(TreeExample)
SiteSpExample=t(phylocom$sample)
SiteSpExample
source("BDTT_functions.R")
library(ape)
library(betapart)
library(abind)
library(Matrix)
hist(getHnodes(TreeExmple))
Betas=BDTT(similarity_slices = c(0:3),tree = TreeExample,sampleOTUs = t(SiteSpExample))
t(SiteSpExample)
Betas=BDTT(similarity_slices = c(0:3),tree = TreeExample,sampleOTUs = (SiteSpExample))
Betas
Betas[,3,]
Betas[,,3]
Betas[3,,]
Betaa
Betaa[,,"jac"]
Betaa[,"jac",]
Betaa["jac",,]
Betas[3,,]
plot(Betas[3,,],Betaa["jac",,])
Betas=BDTT(similarity_slices = 3,tree = TreeExample,sampleOTUs = (SiteSpExample),onlyBeta = F)
slices=3
a<-lapply(slices,GetBranchOcc,tree=TreeExmple,sitesp=SiteSpExmple,pathtoSaveBranchOcc="",bigmatrix=F)
a<-lapply(slices,GetBetaDiv,pathtoGetBranchOcc="",pathtoSaveBeta="")
load("/Users/fmazel/Documents/GitHub/BDTT/BetaDiv_BetaDivSliceNo3.rdata")
load("/Users/fmazel/Documents/GitHub/BDTT/Branch_Site_matrix_SliceNo3.rdata")
plot(Betas[3,,],Betaa["jac",,])
plot(Betas[,],Betaa["jac",,])
Betas
class(Betas)
plot(Betas[1,,],Betaa["jac",,])
Betas=BDTT(similarity_slices = 3,tree = TreeExample,sampleOTUs = (SiteSpExample),onlyBeta = F)
source("BDTT_functions.R")
library(ape)
library(betapart)
library(abind)
library(picante)
data(phylocom)
TreeExample=phylocom$phylo
plot(TreeExample)
SiteSpExample=t(phylocom$sample)
SiteSpExample
hist(getHnodes(TreeExample))
Betas=BDTT(similarity_slices = 3,tree = TreeExample,sampleOTUs = (SiteSpExample),onlyBeta = F)
slices=3
a<-lapply(slices,GetBranchOcc,tree=TreeExample,sitesp=SiteSpExample,pathtoSaveBranchOcc="",bigmatrix=F)
a<-lapply(slices,GetBetaDiv,pathtoGetBranchOcc="",pathtoSaveBeta="")
load("/Users/fmazel/Documents/GitHub/BDTT/BetaDiv_BetaDivSliceNo3.rdata")
load("/Users/fmazel/Documents/GitHub/BDTT/Branch_Site_matrix_SliceNo3.rdata")
plot(TreeExample)
SiteSpExample
a<-lapply(slices,GetBranchOcc,tree=TreeExample,sitesp=t(SiteSpExample),pathtoSaveBranchOcc="",bigmatrix=F)
a<-lapply(slices,GetBetaDiv,pathtoGetBranchOcc="",pathtoSaveBeta="")
load("/Users/fmazel/Documents/GitHub/BDTT/BetaDiv_BetaDivSliceNo3.rdata")
load("/Users/fmazel/Documents/GitHub/BDTT/Branch_Site_matrix_SliceNo3.rdata")
plot(Betas[1,,],Betaa["jac",,])
Betas
source("BDTT_functions.R")
Betas=BDTT(similarity_slices = 3,tree = TreeExample,sampleOTUs = (SiteSpExample),onlyBeta = F)
BDTT
source("BDTT_functions.R")
Betas=BDTT(similarity_slices = 3,tree = TreeExample,sampleOTUs = (SiteSpExample),onlyBeta = F)
axisPhylo()
getBDTT(similarity=3,tree=TreeExample,sampleOTUs=SiteSpExample,onlyBeta=T,metric="jac")
getBDTT(similarity=0,tree=TreeExample,sampleOTUs=SiteSpExample,onlyBeta=F,metric="jac")
getBDTT(similarity=1,tree=TreeExample,sampleOTUs=SiteSpExample,onlyBeta=F,metric="jac")
getBDTT(similarity=1,tree=TreeExample,sampleOTUs=SiteSpExample,onlyBeta=T,metric="jac")
getBDTT(similarity=0,tree=TreeExample,sampleOTUs=SiteSpExample,onlyBeta=T,metric="jac")
getBDTT(similarity=4,tree=TreeExample,sampleOTUs=SiteSpExample,onlyBeta=T,metric="jac")
getBDTT(similarity=4,tree=TreeExample,sampleOTUs=SiteSpExample,onlyBeta=F,metric="jac")
source("BDTT_functions.R")
getBDTT(similarity=4,tree=TreeExample,sampleOTUs=SiteSpExample,onlyBeta=F,metric="jac")
SiteSpExample
tree=TreeExample
sampleOTUs=SiteSpExample
similarity=3
New_OTUs_sample_matrix=getNew_OTUs_sample_matrix(similarity=similarity,sampleOTUs=sampleOTUs,tree=tree)
Ntips=length(tree$tip.label)
tips=1:Ntips
NameTips=tree$tip.label
Hnodes=getHnodes(tree) #get Nodes height
Hbranches=cbind(Hnodes[as.character(tree$edge[,1])],Hnodes[as.character(tree$edge[,2])]) #put them in a matrix of edges
NodeToCluster=tree$edge[(Hbranches[,1]>similarity)&(Hbranches[,2]<similarity),2] #select the branches whom descandant node to be collapsed
NodeToCluster=NodeToCluster[!is.na(NodeToCluster)] #remove tips
DescendandTips=lapply(NodeToCluster,multigetDescendants,tree=tree) #get descendant tips
names(DescendandTips)=as.character(NodeToCluster)
Hbranches
getHnodes(TreeExample)
NodeToCluster
Hbranches
hist(getHnodes(TreeExample))
Betas=BDTT(similarity_slices = 3.1,tree = TreeExample,sampleOTUs = (SiteSpExample),onlyBeta = F)
Betas=BDTT(similarity_slices = 1.1,tree = TreeExample,sampleOTUs = (SiteSpExample),onlyBeta = F)
slices=1.1
a<-lapply(slices,GetBranchOcc,tree=TreeExample,sitesp=t(SiteSpExample),pathtoSaveBranchOcc="",bigmatrix=F)
a<-lapply(slices,GetBetaDiv,pathtoGetBranchOcc="",pathtoSaveBeta="")
load("/Users/fmazel/Documents/GitHub/BDTT/BetaDiv_BetaDivSliceNo3.rdata")
load("/Users/fmazel/Documents/GitHub/BDTT/Branch_Site_matrix_SliceNo3.rdata")
plot(Betas[1,,],Betaa["jac",,])
load("/Users/fmazel/Documents/GitHub/BDTT/BetaDiv_BetaDivSliceNo1.1.rdata")
load("/Users/fmazel/Documents/GitHub/BDTT/Branch_Site_matrix_SliceNo1.1.rdata")
plot(Betas[1,,],Betaa["jac",,])
abline(a=1)
abline(b = 1)
abline(b = 1,a=0)