@@ -59,15 +59,15 @@ Host_tree_cons = as.polytomy(Host_tree_best, feature = 'node.label', fun=functio
59
59
60
60
##1 . Phytools function
61
61
``` {r}
62
- #This function takes 6 inputs and calculate the mean and standard deviation of test statistics and p-values based on 10 tests using N permutations
62
+ #This function takes 6 inputs and calculate the mean and standard deviation of test statistics and p-values using N permutations
63
63
#1. Bac_name: name of bacteria
64
64
#2. Bac_tree: bacterial tree (best tree or consensus tree)
65
65
#3. Host_tree: host tree (best tree or consensus tree)
66
66
#4. N_perm: the number of permutations for a given cospeciation function
67
67
#5. assoc_f: association file to match the host tips and bacterial tips
68
68
#6. output_dir: output directory of the resulting file
69
69
70
- phytools.MeanSD. function = function(Bac_name, Bac_tree, Host_tree, N_perm, assoc_f, output_dir){
70
+ phytools.function = function(Bac_name, Bac_tree, Host_tree, N_perm, assoc_f, output_dir){
71
71
72
72
#Get bac tips
73
73
Bac_tip = Bac_tree$tip.label
@@ -77,8 +77,8 @@ phytools.MeanSD.function = function(Bac_name, Bac_tree, Host_tree, N_perm, assoc
77
77
Host_Bac_filtered = merge(x=Bac_tip_df, y=assoc_f)
78
78
Host_Bac_filtered_m = as.matrix(Host_Bac_filtered)
79
79
80
- #Run cospeciation function with N_perm permutation 10 times
81
- result = replicate(10, cospeciation(Bac_tree, Host_tree, distance = c("RF"), method = c("permutation"), assoc=Host_Bac_filtered_m, nsim = N_perm), simplify = FALSE )
80
+ #Run cospeciation function with N_perm permutation
81
+ result = cospeciation(Bac_tree, Host_tree, distance = c("RF"), method = c("permutation"), assoc=Host_Bac_filtered_m, nsim = N_perm)
82
82
83
83
#Change matrix to list, extract row, and then to vector
84
84
result2 = unlist(result, recursive = FALSE, use.names= TRUE)
@@ -87,20 +87,10 @@ phytools.MeanSD.function = function(Bac_name, Bac_tree, Host_tree, N_perm, assoc
87
87
pvalues = as.numeric(result3$P.val)
88
88
null = as.numeric(result3$d.null)
89
89
90
- #Store mean and sd
91
- mean_p = mean(pvalues)
92
- SD_p = sd(pvalues)
93
- mean_null = mean(null)
94
- SD_null = sd(null)
95
-
96
90
#Write output
97
91
sink(paste0(output_dir, Bac_name, "_n", N_perm, "_phytools_output.txt"), append=TRUE)
98
92
print(Bac_name)
99
- print(paste0("p-values (mean) = ",mean_p))
100
- print(paste0("p-value (SD) = ",SD_p))
101
- print(paste0("Null RF distance (mean) = ",mean_null))
102
- print(paste0("Null RF distance (SD) = ",SD_null))
103
- print(paste0("Obs RF distance = ",obs_RFdist))
93
+ print(result)
104
94
sink()
105
95
}
106
96
```
@@ -110,21 +100,21 @@ phytools.MeanSD.function = function(Bac_name, Bac_tree, Host_tree, N_perm, assoc
110
100
#output directory
111
101
phytools_example_output = "/ebio/abt3_projects/Bifido_Coevolution/publication_codes/Example_data/"
112
102
113
- phytools.MeanSD. function(Bac_name, Bac_tree_best, Host_tree_best, 999, assoc_f, phytools_example_output)
103
+ phytools.function(Bac_name, Bac_tree_best, Host_tree_best, 999, assoc_f, phytools_example_output)
114
104
```
115
105
116
106
117
107
118
108
##2 . Paco function
119
109
``` {r}
120
- #This function takes 5 inputs and calculate the mean and standard deviation of Paco test statistics and p-values based on 10 tests using N permutations
110
+ #This function takes 5 inputs and calculate the Paco test statistics and p-values using N permutations
121
111
#1. Bac_name: name of bacteria
122
112
#2. Bac_tree: bacterial tree (best tree or consensus tree)
123
113
#3. Host_tree: host tree (best tree or consensus tree)
124
114
#4. N_perm: the number of permutations for a given cospeciation function
125
115
#5. output_dir: output directory of the resulting file
126
116
127
- PACO.cutree.MeanSD. function = function(Bac_name, Bac_tree, Host_tree, N_perm, output_dir){
117
+ PACO.cutree.function = function(Bac_name, Bac_tree, Host_tree, N_perm, output_dir){
128
118
129
119
##Required subfunctions:
130
120
#PACo
@@ -151,9 +141,7 @@ RemoveDups <- function(df, column) {
151
141
}
152
142
153
143
154
- ##Main function
155
- PACO.cutree.function = function(Bac_name, Bac_tree, Host_tree, N_perm, output_dir){
156
-
144
+ ##Main function
157
145
##1. Create Bac tree using cutree
158
146
#Get tip labels
159
147
Bac_tip_label = Bac_tree$tip.label
@@ -231,15 +219,7 @@ for (n in c(1:N.perm))
231
219
{P.value = P.value + 1}
232
220
}
233
221
P.value <- P.value/N.perm
234
- }
235
-
236
222
237
- #Run the above function 10 times
238
- PACo_result = replicate(10, PACO.cutree.function(Bac_name, Bac_tree, Host_tree, N_perm, output_dir))
239
-
240
- #Calculate p-value mean
241
- mean_p = mean(PACo_result)
242
- SD_p = sd(PACo_result)
243
223
244
224
#Calculate m2_null mean and sd
245
225
m2_null = read.table(file = paste0(output_dir,Bac_name,"_n",N_perm,"_m2_null.txt"), sep ="\t", header = FALSE)
@@ -250,36 +230,35 @@ m2_obs = read.table(file = paste0(output_dir,Bac_name,"_n",N_perm,"_m2_obs.txt")
250
230
#print output
251
231
sink(paste0(output_dir, Bac_name, "_n", N_perm, "_PACO_output.txt"), append=TRUE)
252
232
print(Bac_name)
253
- print(paste0("Mean p-values = ",mean_p ))
254
- print(paste0("SD p-values = ",SD_p ))
233
+ print(paste0("p-value = ",P.value ))
234
+ print(paste0("m2_obs = ",m2_obs ))
255
235
print(paste0("Mean m2_null = ",mean_null))
256
236
print(paste0("SD m2_null = ",sd_null))
257
- print(paste0("m2_obs = ",m2_obs))
258
237
sink()
259
238
}
260
239
261
240
```
262
241
263
- #2 .1. Phytools Example
242
+ #2 .1. PACo Example
264
243
``` {r}
265
244
#output directory
266
245
paco_example_output = "/ebio/abt3_projects/Bifido_Coevolution/publication_codes/Example_data/"
267
246
268
- PACO.cutree.MeanSD. function(Bac_name, Bac_tree_best, Host_tree_best, 999, paco_example_output)
247
+ PACO.cutree.function(Bac_name, Bac_tree_best, Host_tree_best, 999, paco_example_output)
269
248
```
270
249
271
250
272
251
273
252
##3 . Parafit function
274
253
``` {r}
275
- #This function takes 5 inputs and calculate the mean and standard deviation of Parfit test statistics and p-values based on 10 tests using N permutations
254
+ #This function takes 5 inputs and calculate the mean and standard deviation of Parfit test statistics and p-values using N permutations
276
255
#1. Bac_name: name of bacteria
277
256
#2. Bac_tree: bacterial tree (best tree or consensus tree)
278
257
#3. Host_tree: host tree (best tree or consensus tree)
279
258
#4. N_perm: the number of permutations for a given cospeciation function
280
259
#5. output_dir: output directory of the resulting file
281
260
282
- parafit.cutree.MeanSD. function = function(Bac_name, Bac_tree, Host_tree, N_perm, output_dir){
261
+ parafit.cutree.function = function(Bac_name, Bac_tree, Host_tree, N_perm, output_dir){
283
262
284
263
##1. Create Bac tree using cutree
285
264
#Get tip labels
@@ -498,33 +477,13 @@ out
498
477
}
499
478
##########################
500
479
501
-
502
- #Run parafit with N_perm permutation 10 times
503
- parafit_result = replicate(10, parafit_Legendre(Host_tree_filter_m, Bac_dist_cutree, HP_file, nperm = N_perm, correction="cailliez"))
504
-
505
- #Store results from 10 replicates
506
- parafitglobal_obs = parafit_result[2] #no need for mean because all same values
507
- parafitglobal_obs_ul = unlist(parafitglobal_obs)
508
- parafitglobal_null = parafit_result[c(1,7,13,19,25,31,37,43,49,55)]
509
- parafitglobal_null_ul = unlist(parafitglobal_null)
510
- parafit_pvalue = parafit_result[c(3,9,15,21,27,33,39,45,51,57)]
511
- parafit_pvalue_ul = unlist(parafit_pvalue)
512
-
513
- #Calculate mean and sd
514
- mean_null = mean(parafitglobal_null_ul)
515
- SD_null = sd(parafitglobal_null_ul)
516
- mean_p = mean(parafit_pvalue_ul)
517
- SD_p = sd(parafit_pvalue_ul)
518
-
480
+ #Run parafit with N_perm permutation
481
+ parafit_result = parafit_Legendre(Host_tree_filter_m, Bac_dist_cutree, HP_file, nperm = N_perm, correction="cailliez")
519
482
520
483
#Write output
521
484
sink(paste0(output_dir, Bac_name, "_n", N_perm, "_parafit_output.txt"), append=TRUE)
522
485
print(Bac_name)
523
- print(paste0("ParaFitGlobal_obs =",parafitglobal_obs_ul))
524
- print(paste0("ParaFitGlobal_null_mean =",mean_null))
525
- print(paste0("ParaFitGlobal_null_SD =",SD_null))
526
- print(paste0("p-values_mean =",mean_p))
527
- print(paste0("p-values_SD = ",SD_p))
486
+ print(parafit_result)
528
487
sink()
529
488
}
530
489
```
@@ -535,5 +494,5 @@ SD_p = sd(parafit_pvalue_ul)
535
494
#output directory
536
495
parafit_example_output = "/ebio/abt3_projects/Bifido_Coevolution/publication_codes/Example_data/"
537
496
538
- parafit.cutree.MeanSD. function(Bac_name, Bac_tree_best, Host_tree_best, 999, parafit_example_output)
497
+ parafit.cutree.function(Bac_name, Bac_tree_best, Host_tree_best, 999, parafit_example_output)
539
498
```
0 commit comments