Skip to content

Commit a595916

Browse files
Update Codiversification_tests.Rmd
Edited code to reflect the changes made in the revised manuscript. Not running the test 10 times using replicate().
1 parent be9b3e1 commit a595916

File tree

1 file changed

+19
-60
lines changed

1 file changed

+19
-60
lines changed

Codiversification_tests.Rmd

Lines changed: 19 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ Host_tree_cons = as.polytomy(Host_tree_best, feature = 'node.label', fun=functio
5959

6060
##1. Phytools function
6161
```{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
6363
#1. Bac_name: name of bacteria
6464
#2. Bac_tree: bacterial tree (best tree or consensus tree)
6565
#3. Host_tree: host tree (best tree or consensus tree)
6666
#4. N_perm: the number of permutations for a given cospeciation function
6767
#5. assoc_f: association file to match the host tips and bacterial tips
6868
#6. output_dir: output directory of the resulting file
6969
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){
7171
7272
#Get bac tips
7373
Bac_tip = Bac_tree$tip.label
@@ -77,8 +77,8 @@ phytools.MeanSD.function = function(Bac_name, Bac_tree, Host_tree, N_perm, assoc
7777
Host_Bac_filtered = merge(x=Bac_tip_df, y=assoc_f)
7878
Host_Bac_filtered_m = as.matrix(Host_Bac_filtered)
7979
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)
8282
8383
#Change matrix to list, extract row, and then to vector
8484
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
8787
pvalues = as.numeric(result3$P.val)
8888
null = as.numeric(result3$d.null)
8989
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-
9690
#Write output
9791
sink(paste0(output_dir, Bac_name, "_n", N_perm, "_phytools_output.txt"), append=TRUE)
9892
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)
10494
sink()
10595
}
10696
```
@@ -110,21 +100,21 @@ phytools.MeanSD.function = function(Bac_name, Bac_tree, Host_tree, N_perm, assoc
110100
#output directory
111101
phytools_example_output = "/ebio/abt3_projects/Bifido_Coevolution/publication_codes/Example_data/"
112102
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)
114104
```
115105

116106

117107

118108
##2. Paco function
119109
```{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
121111
#1. Bac_name: name of bacteria
122112
#2. Bac_tree: bacterial tree (best tree or consensus tree)
123113
#3. Host_tree: host tree (best tree or consensus tree)
124114
#4. N_perm: the number of permutations for a given cospeciation function
125115
#5. output_dir: output directory of the resulting file
126116
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){
128118
129119
##Required subfunctions:
130120
#PACo
@@ -151,9 +141,7 @@ RemoveDups <- function(df, column) {
151141
}
152142
153143
154-
##Main function
155-
PACO.cutree.function = function(Bac_name, Bac_tree, Host_tree, N_perm, output_dir){
156-
144+
##Main function
157145
##1. Create Bac tree using cutree
158146
#Get tip labels
159147
Bac_tip_label = Bac_tree$tip.label
@@ -231,15 +219,7 @@ for (n in c(1:N.perm))
231219
{P.value = P.value + 1}
232220
}
233221
P.value <- P.value/N.perm
234-
}
235-
236222
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)
243223
244224
#Calculate m2_null mean and sd
245225
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")
250230
#print output
251231
sink(paste0(output_dir, Bac_name, "_n", N_perm, "_PACO_output.txt"), append=TRUE)
252232
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))
255235
print(paste0("Mean m2_null = ",mean_null))
256236
print(paste0("SD m2_null = ",sd_null))
257-
print(paste0("m2_obs = ",m2_obs))
258237
sink()
259238
}
260239
261240
```
262241

263-
#2.1. Phytools Example
242+
#2.1. PACo Example
264243
```{r}
265244
#output directory
266245
paco_example_output = "/ebio/abt3_projects/Bifido_Coevolution/publication_codes/Example_data/"
267246
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)
269248
```
270249

271250

272251

273252
##3. Parafit function
274253
```{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
276255
#1. Bac_name: name of bacteria
277256
#2. Bac_tree: bacterial tree (best tree or consensus tree)
278257
#3. Host_tree: host tree (best tree or consensus tree)
279258
#4. N_perm: the number of permutations for a given cospeciation function
280259
#5. output_dir: output directory of the resulting file
281260
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){
283262
284263
##1. Create Bac tree using cutree
285264
#Get tip labels
@@ -498,33 +477,13 @@ out
498477
}
499478
##########################
500479
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")
519482
520483
#Write output
521484
sink(paste0(output_dir, Bac_name, "_n", N_perm, "_parafit_output.txt"), append=TRUE)
522485
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)
528487
sink()
529488
}
530489
```
@@ -535,5 +494,5 @@ SD_p = sd(parafit_pvalue_ul)
535494
#output directory
536495
parafit_example_output = "/ebio/abt3_projects/Bifido_Coevolution/publication_codes/Example_data/"
537496
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)
539498
```

0 commit comments

Comments
 (0)