-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_2020-11-13 - LM_analysis_Derma_Pleco_01.R
3094 lines (2525 loc) · 158 KB
/
2_2020-11-13 - LM_analysis_Derma_Pleco_01.R
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
remove.parasites = F
mirrored = F
halve.curves = T
# source("//blanke-nas-1.zoologie.uni-koeln.de/PROTOCOLS/_SCRIPTS/R/Peter/functions_PTR.R")
source("//blanke-nas-1/PROTOCOLS/_SCRIPTS/R/Peter/functions_PTR.R")
# load("N:/PAPERS/PTR_Influence of the nymphal life history/R/workspace_good_unmirrored.RData")
# # load array.2D from LM analysis script - if this does not exist, the code to create it is at the end of this script
# if(remove.specimens){
# array.2D <- as_tibble(suppressWarnings({gsheet2tbl('https://docs.google.com/spreadsheets/d/1BjS1ejG5ikzlTCz1sOc-Vcjz5ofDPBsyC_6Am8pisZA#gid=482941805')})) # 482941805 = pooled & filtered
# } else {
# if(mirrored == T){
# array.2D <- as_tibble(suppressWarnings({gsheet2tbl('https://docs.google.com/spreadsheets/d/1BjS1ejG5ikzlTCz1sOc-Vcjz5ofDPBsyC_6Am8pisZA#gid=979750323')})) # 0 = un-pooled; 557348376 = pooled
# } else {
# array.2D <- as_tibble(suppressWarnings({gsheet2tbl('https://docs.google.com/spreadsheets/d/1BjS1ejG5ikzlTCz1sOc-Vcjz5ofDPBsyC_6Am8pisZA#gid=557348376')})) # 0 = un-pooled; 557348376 = pooled
# }
# }
array.2D <- as_tibble(suppressWarnings({gsheet2tbl('https://docs.google.com/spreadsheets/d/1BjS1ejG5ikzlTCz1sOc-Vcjz5ofDPBsyC_6Am8pisZA#gid=0')}))
# array.2D <- read.xlsx2(paste0("O:/_SCRIPTS/R/Peter/checkpoint_LMs/all_Checkpoint_files/", today(), "_array.2D.everything.xlsx"), sheetIndex = 1)
Hemimeridae <- c("1604", "1744", "0015", "0017", "1602", "1603")
Arixeniidae <- c("1601", "0657", "1715", "1600", "1743")
parasites <- c(Hemimeridae, Arixeniidae)
list.positions.parasites <- which(array.2D$ERC %in% parasites)
if(remove.parasites){
array.2D <- array.2D[-list.positions.parasites,]
ERC_numbers_all <- ERC_numbers_all[-list.positions.parasites]
PBT_Derma_Pleco <- PBT_Derma_Pleco %>%
filter(ERC %!in% parasites)
message(paste0("removed ", paste(parasites, collapse = ", "), " from PBT and array.2D"))
}
# G.marginalis.ERC <- "1736"
# PBT_Derma_Pleco <- PBT_Derma_Pleco %>%
# filter(ERC %!in% G.marginalis.ERC)
# Scopura has no superfamily <- "Scopuroidea"
array.2D$superfamily[array.2D$genus == "Scopura"] <- "Scopuroidea"
# delete some taxa
# array.2D <- array.2D %>%
# filter(family != "Hemimeridae" & family != "Arixeniidae")
# load PBT
PBT <- load.PBT()
PBT$ID <- paste0(PBT$genus_GBIF, "_", PBT$species_GBIF)
# set LM.sets dir temprarily
# setwd("//blanke-nas-1.zoologie.uni-koeln.de/PROTOCOLS/_SCRIPTS/R/Peter/checkpoint_LMs/")
setwd("//blanke-nas-1/PROTOCOLS/_SCRIPTS/R/Peter/checkpoint_LMs/")
# get all landmark names that should be analyzed from file
# LMs.hc <- as.character(read.csv("//blanke-nas-1/PROTOCOLS/_SCRIPTS/R/Peter/checkpoint_LMs/2020-06-25 - 1st_paper_head.txt", header = F)$V1)
# LMs.hc.halve.curves <- as.character(read.csv("//blanke-nas-1/PROTOCOLS/_SCRIPTS/R/Peter/checkpoint_LMs/2020-06-25 - 1st_paper_head_halve_curves.txt", header = F)$V1)
# LMs.hc.halve.curves.no.occ.ant <- as.character(read.csv("//blanke-nas-1/PROTOCOLS/_SCRIPTS/R/Peter/checkpoint_LMs/2020-06-25 - 1st_paper_head_halve_curves_no_occ_ant.txt", header = F)$V1)
# LMs.hc.no.curves <- as.character(read.csv("//blanke-nas-1/PROTOCOLS/_SCRIPTS/R/Peter/checkpoint_LMs/2020-06-25 - 1st_paper_head_no_curves.txt", header = F)$V1)
if(mirrored == T & halve.curves == T){
# mirrored +; halve curves +
head.capsule <- as.character(read.csv("./_2020-06-25 - 1st_paper_head_halve_curves_no_ant_no_occ_mirr.txt", header = F)$V1)
} else if(mirrored == F & halve.curves == T){
# mirrored -; halve curves +
head.capsule <- as.character(read.csv("./_2020-06-25 - 1st_paper_head_halve_curves_no_ant_no_occ.txt", header = F)$V1)
} else if(mirrored == F & halve.curves == F){
# mirrored -; halve curves -
head.capsule <- as.character(read.csv("./_2020-06-25 - 1st_paper_head_no_ant_no_occ.txt", header = F)$V1)
} else if(mirrored == T & halve.curves == F){
# mirrored +; halve curves -
head.capsule <- as.character(read.csv("./_2020-06-25 - 1st_paper_head_no_ant_no_occ_mirr.txt", header = F)$V1)
}
# delete landmarks to heck their influence
# head.capsule <- head.capsule[-which(head.capsule == "atp_med" | head.capsule == "atp_lat")]
LMs.Mds <- as.character(read.csv("./2020-06-25 - 1st_paper_Md.txt", header = F)$V1)
# LMs.Lbr <- as.character(read.csv("//blanke-nas-1/PROTOCOLS/_SCRIPTS/R/Peter/checkpoint_LMs/2020-06-25 - 1st_paper_Lbr.txt", header = F)$V1)
LMs.Car <- as.character(read.csv("./2020-06-25 - 1st_paper_Car.txt", header = F)$V1)
LMs.Lab <- as.character(read.csv("./2020-06-25 - 1st_paper_Lab.txt", header = F)$V1)
# load block LM lists with non-overlap paired LMs
PLS.LMs.hc_Md <- as.character(read.csv("./2020-06-25 - 1st_paper_PLS_head_no_curves_against_Md.txt", header = F)$V1)
PLS.LMs.hc_Car <- as.character(read.csv("./2020-06-25 - 1st_paper_PLS_head_no_curves_against_Car.txt", header = F)$V1)
PLS.LMs.hc_Lab <- as.character(read.csv("./2020-06-25 - 1st_paper_PLS_head_no_curves_against_Lab.txt", header = F)$V1)
# curve.LMs <- as.character(read.csv("//blanke-nas-1/PROTOCOLS/_SCRIPTS/R/Peter/checkpoint_LMs/2020-06-25 - 1st_paper_curve_landmarks.txt", header = F)$V1)
# occ.ant.LMs <- as.character(read.csv("//blanke-nas-1/PROTOCOLS/_SCRIPTS/R/Peter/checkpoint_LMs/2020-06-25 - 1st_paper_eye_ant.txt", header = F)$V1)
# ant.LMs <- as.character(read.csv("//blanke-nas-1/PROTOCOLS/_SCRIPTS/R/Peter/checkpoint_LMs/2020-06-25 - 1st_paper_ant.txt", header = F)$V1)
# occ.LMs <- as.character(read.csv("//blanke-nas-1/PROTOCOLS/_SCRIPTS/R/Peter/checkpoint_LMs/2020-06-25 - 1st_paper_occ.txt", header = F)$V1)
# set actual working dir
# setwd("//blanke-nas-1.zoologie.uni-koeln.de/DATA/PAPERS/PTR_Influence of the nymphal life history/R")
setwd("//blanke-nas-1/DATA/PAPERS/PTR_Influence of the nymphal life history/R")
# add LM subsets to list of subsets
LM.subsets.all <- list(head.capsule, LMs.Mds, LMs.Car, LMs.Lab, PLS.LMs.hc_Md, PLS.LMs.hc_Car, PLS.LMs.hc_Lab) # head.capsule, head.capsule
LM.subset.names.all <- c("head.capsule", "LMs.Mds", "LMs.Car", "LMs.Lab", "PLS.LMs.hc_Md", "PLS.LMs.hc_Car", "PLS.LMs.hc_Lab") # "head.capsule", "head.capsule",
# load global tree
tree <- read.nexus(file = "./trees/3rd_combined/2_original_correlated/combined_corr_correlated_ultra_corr2.tre")
# # correct tree file
# tree <- read.nexus(file = "./trees/3rd_combined/2_original_correlated/combined_corr_correlated_ultra_corr.tre")
# plot(ladderize(tree, right = T), cex=.8, edge.lty = 1, use.edge.length = T, label.offset = .002)
# switch taxon positions
## tip.C.amer <- which(tree$tip.label == "Carcinophora_americana")
## tip.G.marginalis <- which(tree$tip.label == "Gonolabis_marginalis")
#
# tree$tip.label[tip.C.amer] <- "Gonolabis_marginalis"
# tree$tip.label[tip.G.marginalis] <- "Carcinophora_americana"
# tip.XXXXX <- which(tree$tip.label == "XXXXXX")
# tree <- drop.tip(tree, tip.XXXXX)
# write.nexus(tree, file = "./trees/3rd_combined/2_original_correlated/combined_corr_correlated_ultra_corr2.tre")
# plot(ladderize(tree, right = T), cex=.8, edge.lty = 1, use.edge.length = T, label.offset = .002)
# create loop to go through each LM.subset and do all the GMM stuff
# cluster <- makeCluster(15)
# registerDoParallel(cluster)
start.time <- Sys.time()
# foreach(s = 1:length(LM.subsets.all), .packages = c("geomorph", "magrittr", "dplyr", "RColorBrewer", "ape")) %dopar% {
# define taxon groups to test
taxon.names <- c("all", "Dermaptera", "Plecoptera")
for(taxon.name in 1:length(taxon.names)){ # length(taxon.names)
for(s in 1:length(LM.subsets.all)){ # length(LM.subsets.all)
# get taxon name
curr.taxon.name <- taxon.names[taxon.name]
message("******************")
message(curr.taxon.name)
# get subset name
curr.subset.name <- LM.subset.names.all[s]
message(curr.subset.name)
message("******************")
# get LM subset
curr.subset <- LM.subsets.all[[s]]
curr.subset.orig <- curr.subset
# add _X, _Y, _Z to landmark names so we can find the columns
LMs.to.analyze <- c(sapply(curr.subset,paste,c("X", "Y", "Z"), sep="_"))
# select the columns with landmark data
LM.col.numbers <- which(colnames(array.2D) %in% LMs.to.analyze)
# delete rows with species that do not belong to curr.taxon.name
if(curr.taxon.name == "all"){
array.2D.taxon <- array.2D %>%
filter(order == "Dermaptera" | order == "Plecoptera")
} else {
array.2D.taxon <- array.2D %>%
filter(order == curr.taxon.name)
}
# delete columns with LMs that are not to be analyzed
array.2D.LMs <- array.2D.taxon[,c(1,LM.col.numbers)]
# clean array from NAs (each SPECIMEN with a single NA will be deleted)
array.2D.max.LMs <- as.data.frame(na.omit(array.2D.LMs))
# turn ERC column into rownames and delete ERC column for 3D array creation
rownames(array.2D.max.LMs) <- as.character(array.2D.max.LMs$ERC)
array.2D.max.LMs$ERC <- NULL
# colnames(array.2D.max.LMs)
# sort(rownames(array.2D.max.LMs))
# print dimensions of array_2D
dim(array.2D.max.LMs)
# save as excel for supplement
LM.df.save <- array.2D.max.LMs
colnames(LM.df.save) <- gsub("frontS_X_coronS", "1", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("lat_clyplbrS", "2", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("atp_med", "3", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("atp_lat", "4", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("dta_at_head", "5", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("dta_at_ata_ant", "6", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("mdcond_ant", "7", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("mdcond_post", "8", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("cardo_artic", "9", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("lat_of_postmentS", "10", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("C_of_clyplbrS", "11", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("C_of_epistS", "12", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("ant_corpotent", "13", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("foramendors", "14", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("foramenvent", "15", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("C_of_postmentS", "16", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("clypeus_shape", "17", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("frons_shape", "18", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("vertex_shape", "19", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("md1_insterion_ant", "20", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("md1_insterion_post", "21", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("md3_insertion", "22", colnames(LM.df.save))
colnames(LM.df.save) <- gsub("dist_incisiv", "23", colnames(LM.df.save))
LM.species.df <- as.data.frame(convert.ERCs.to.IDs(rownames(LM.df.save), classifier))
colnames(LM.species.df) <- "species"
LM.df.save <- cbind(LM.species.df, LM.df.save)
write.xlsx2(LM.df.save, paste0("N://PAPERS//PTR_Influence of the nymphal life history//FIGURES//SUPPLEMENT//array_2D//", today(), "_", curr.subset.name, "_array_2D.xlsx"),
row.names = F)
# turn 2D array into 3D array
array.3D <- arrayspecs(array.2D.max.LMs,(ncol(array.2D.max.LMs)/3),3, sep = ".") # p = # landmarks; k = # dimensions; n = # specimens
dim(array.3D)
# create classifier that contains everything but LM coordinates
classifier <- array.2D.taxon[, (1:which(colnames(array.2D.taxon) == LMs.to.analyze[1])-1)]
# add order color column to classifier
classifier <- classifier %>%
mutate(col.order = case_when(order == "Dermaptera" ~ "orange",
order == "Plecoptera" ~ "blue"))
# add superfamily colors to classifier
superfamilies <- unique(classifier$superfamily)
superfamily_cols <- get.Palette.Set2(length(superfamilies))
for (f in 1:length(superfamilies)) {
classifier$col.superfamily[classifier$superfamily==superfamilies[f]] <- superfamily_cols[f]
classifier$point.shape.superfamily[classifier$superfamily==superfamilies[f]] <- f
}
# add family colors to classifier
families <- unique(classifier$family)
family_cols <- get.Palette.Set2(length(families))
for (f in 1:length(families)) {
classifier$col.family[classifier$family==families[f]] <- family_cols[f]
}
# add larval food colors to classifier
for (v in 1:nrow(classifier)) {
curr.food.l <- classifier$food.larva[v]
if(is.na(curr.food.l)){
classifier$col.food.l[v] <-"grey70"
} else if(curr.food.l == "NA") {
classifier$col.food.l[v] <-"grey70"
} else if(curr.food.l == "herbivory") {
classifier$col.food.l[v] <-"springgreen4"
} else if(curr.food.l == "predatory") {
classifier$col.food.l[v] <-"tomato2"
} else if(curr.food.l == "detritivoric") {
classifier$col.food.l[v] <-"tan4"
} else if(curr.food.l == "ectoparasitic") {
classifier$col.food.l[v] <-"palevioletred1"
} else if(curr.food.l == "scavenger") {
classifier$col.food.l[v] <-"hotpink4"
}
}
food.ls <- unique(classifier$food.larva)
food.l.point.shapes <- c(20, 2, 4, 5, 8, 9)
for (f in 1:length(food.ls)) {
classifier$point.shape.food.l[classifier$food.larva==food.ls[f]] <- food.l.point.shapes[f]
classifier$point.shape.food.l[is.na(classifier$food.larva)] <- 1
}
# add adult food colors to classifier
for (v in 1:nrow(classifier)) {
curr.food.a <- classifier$food.adult[v]
if(is.na(curr.food.a)){
classifier$col.food.a[v] <-"grey70"
} else if(curr.food.a == "herbivory") {
classifier$col.food.a[v] <-"springgreen4"
} else if(curr.food.a == "predatory") {
classifier$col.food.a[v] <-"tomato2"
} else if(curr.food.a == "detritivoric") {
classifier$col.food.a[v] <-"tan4"
} else if(curr.food.a == "ectoparasitic") {
classifier$col.food.a[v] <-"palevioletred1"
} else if(curr.food.a == "scavenger") {
classifier$col.food.a[v] <-"hotpink4"
} else if(curr.food.a == "non_feeding") {
classifier$col.food.a[v] <-"deepskyblue1"
}
}
# since some rows have been deleted from array.2D.max.LMs:
classifier <- as.data.frame(classifier[which(classifier$ERC %in% rownames(array.2D.max.LMs)), ])
rownames(classifier) <- classifier$ERC
# add _1 and _2 to IDs
for(i in 2:nrow(classifier)){
doubles <- which(classifier$ID %in% classifier$ID[i])
if(length(doubles) == 2){
classifier$ID[doubles[1]] <- paste0(classifier$ID[doubles[1]], "_1")
classifier$ID[doubles[2]] <- paste0(classifier$ID[doubles[2]], "_2")
}
}
# check if taxa are missing in tree
if(length(setdiff(classifier$ID, tree$tip.label) == 0)){
message("!!!!!!!!!!!!!!!!!!!!!")
message("Taxa missing in tree:")
message(setdiff(classifier$ID, tree$tip.label))
message("!!!!!!!!!!!!!!!!!!!!!")
}
# drop tree tips that are not part of the classifier
tips.to.drop <- which(tree$tip.label %!in% classifier$ID)
for (t in 1:length(tips.to.drop)){
curr.tip.label <- tree$tip.label[tips.to.drop[t]]
if(sub("_1", "", curr.tip.label) %in% classifier$ID){
tree$tip.label[tips.to.drop[t]] <- sub("_1+", "", curr.tip.label)
}
}
tips.to.drop <- which(tree$tip.label %!in% classifier$ID)
tree.dropped <- drop.tip(tree, tips.to.drop)
# # for supplementary tree figure:
# tree.plot <- ladderize(tree.dropped)
# plot(tree.plot, right = T, cex=.5, edge.lty = 1, use.edge.length = T, label.offset = .002)
# tree.plot.rot <-rotate.multi(tree = tree.plot, node = 221)
# plot(tree.plot.rot, right = T, cex=.5, edge.lty = 1, use.edge.length = T, label.offset = .002)
# is_tip <- tree.plot.rot$edge[,2] <= length(tree.plot.rot$tip.label)
# ordered_tips <- tree.plot.rot$edge[is_tip, 2]
# plot.labels <- rev(tree.plot.rot$tip.label[ordered_tips])
# writeClipboard(plot.labels)
#
# Protodermaptera
#
#
# plot.suborders <- c()
# for(plot.label in plot.labels){
# plot.suborders <- c(plot.suborders, get.suborders.from.PBT(str_split(plot.label, pattern = "_")[[1]][1]))
# }
# writeClipboard(plot.suborders)
#
# plot.infraorders <- c()
# for(plot.label in plot.labels){
# plot.infraorders <- c(plot.infraorders, get.infraorders.from.PBT(str_split(plot.label, pattern = "_")[[1]][1]))
# }
# writeClipboard(plot.infraorders)
#
#
# plot.superfamilies <- c()
# for(plot.label in plot.labels){
# plot.superfamilies <- c(plot.superfamilies, classifier$superfamily[classifier$ID == plot.label])
# }
# writeClipboard(plot.superfamilies)
#
# plot.families <- c()
# for(plot.label in plot.labels){
# plot.families <- c(plot.families, classifier$family[classifier$ID == plot.label])
# }
# writeClipboard(plot.families)
# plot(tree.plot.rot, right = T, cex=.5, edge.lty = 1, use.edge.length = T, label.offset = .002)
# axisPhylo()
# some checks <- should not be necessary to run if everything is good
# comp.df <- data.frame(cbind(sort(classifier$ID), sort(tree.dropped$tip.label)))
# setdiff(classifier$ID, tree.dropped$tip.label)
# for some reason Taeniopteryx_nebulosa is still twice in tree
# tree.dropped <- drop.tip(tree.dropped, which(tree.dropped$tip.label == "Taeniopteryx_nebulosa")[2])
# turn tip label IDs into ERC numbers
tip.label.IDs <- tibble::enframe(tree.dropped$tip.label)
for(i in 1:nrow(tip.label.IDs)){
tip.label.IDs$ERC[i] <- classifier$ERC[classifier$ID == tip.label.IDs$value[i]]
}
tree.dropped_ERC <- tree.dropped
tree.dropped_ERC$tip.label <- unlist(tip.label.IDs$ERC)
plot(ladderize(tree.dropped_ERC, right = T), cex=.4, edge.lty = 1, use.edge.length = T, label.offset = .002)
#### define sliding LM matrix
if(length(grep("shape", curr.subset))>0){
message("sliding LMs:")
# define empty sliding matrices to bind later
sliding.matrix.hc.curves <- create_empty_df(nrow = 0,
names = c("before", "slide", "after"))
sliding.matrix.occ <- create_empty_df(nrow = 0,
names = c("before", "slide", "after"))
sliding.matrix.ant <- create_empty_df(nrow = 0,
names = c("before", "slide", "after"))
# only if curr.subset contains clypeus (& frons & vertex) curve LMs
if(length(grep("clypeus_shape", curr.subset))>0){
print("clypeus, frons & vertex LMs...")
# get type I LM rownumbers to define points between which sliding will happen
type.I.LMs <- c("C_of_clyplbrS", "C_of_epistS", "frontS_X_coronS", "foramendors")
for(t in type.I.LMs){
# find rownumber within array.3D that contains current LM coordinates
assign(paste0("row.number.", t, ".LM"), which(curr.subset == t))
}
# get type III LMs
type.III.LMs <- c("clypeus_shape", "frons_shape", "vertex_shape")
for(t in type.III.LMs){
# find rownumbers within array.3D that contain current LM coordinates
assign(paste0("row.numbers.", t, ".LMs"), grep(t, curr.subset))
}
# now create matrix (this could also be manually done for each specimen via 'define.sliders(array.3D[, 1], nsliders = 10)')
sliding.matrix.hc.curves <- create_empty_df(nrow = length(c(row.numbers.clypeus_shape.LMs,
row.numbers.frons_shape.LMs,
row.numbers.vertex_shape.LMs)),
names = c("before", "slide", "after"))
sliding.matrix.hc.curves$before <- c(row.number.C_of_clyplbrS.LM, row.numbers.clypeus_shape.LMs[1:(length(row.numbers.clypeus_shape.LMs)-1)],
row.number.C_of_epistS.LM, row.numbers.frons_shape.LMs[1:(length(row.numbers.frons_shape.LMs)-1)],
row.number.frontS_X_coronS.LM, row.numbers.vertex_shape.LMs[1:(length(row.numbers.vertex_shape.LMs)-1)])
sliding.matrix.hc.curves$slide <- c(row.numbers.clypeus_shape.LMs,
row.numbers.frons_shape.LMs,
row.numbers.vertex_shape.LMs)
sliding.matrix.hc.curves$after <- c(row.numbers.clypeus_shape.LMs[2:length(row.numbers.clypeus_shape.LMs)], row.number.C_of_epistS.LM,
row.numbers.frons_shape.LMs[2:length(row.numbers.frons_shape.LMs)], row.number.frontS_X_coronS.LM,
row.numbers.vertex_shape.LMs[2:length(row.numbers.vertex_shape.LMs)], row.number.foramendors.LM)
}
# only if curr.subset contains eye curve LMs
if(length(grep("occS_shape", curr.subset))>0){
for(m in 1){
if(m==1){
print("occipital LMs...")
# get type I LM rownumbers to define points between which sliding will happen
type.I.LMs <- c("occS_shape_eq_1")
for(t in type.I.LMs){
# find rownumber within array.3D that contains current LM coordinates
assign(paste0("row.number.", t, ".LM"), which(curr.subset.orig == t))
}
# get type III LMs
type.III.LMs <- c("occS_shape_eq")
for(t in type.III.LMs){
# find rownumbers within array.3D that contain current LM coordinates
assign(paste0("row.numbers.", t, ".LMs"), grep(t, curr.subset.orig))
}
# now create matrix (this could also be manually done for each specimen via 'define.sliders(array.3D[, 1], nsliders = 10)')
sliding.matrix.occ <- create_empty_df(nrow = length(c(row.numbers.occS_shape_eq.LMs))-1,
names = c("before", "slide", "after"))
sliding.matrix.occ$before <- c(row.number.occS_shape_eq_1.LM, row.numbers.occS_shape_eq.LMs[2:(length(row.numbers.occS_shape_eq.LMs)-1)])
sliding.matrix.occ$slide <- c(row.numbers.occS_shape_eq.LMs[2:length(row.numbers.occS_shape_eq.LMs)])
sliding.matrix.occ$after <- c(row.numbers.occS_shape_eq.LMs[3:length(row.numbers.occS_shape_eq.LMs)], row.number.occS_shape_eq_1.LM)
}
}
if(m==2){
print("mirrored occipital LMs...")
# get type I LM rownumbers to define points between which sliding will happen
type.I.LMs <- c("occS_shape_eq_mirr_1")
for(t in type.I.LMs){
# find rownumber within array.3D that contains current LM coordinates
assign(paste0("row.number.", t, ".LM"), which(curr.subset == t))
}
# get type III LMs
type.III.LMs <- c("occS_shape_eq_mirr")
for(t in type.III.LMs){
# find rownumbers within array.3D that contain current LM coordinates
assign(paste0("row.numbers.", t, ".LMs"), grep(t, curr.subset))
}
# now add mirrored LM matrix
sliding.matrix.occ.mirr <- create_empty_df(nrow = length(c(row.numbers.occS_shape_eq_mirr.LMs))-1,
names = c("before", "slide", "after"))
sliding.matrix.occ.mirr$before <- c(row.number.occS_shape_eq_mirr_1.LM, row.numbers.occS_shape_eq_mirr.LMs[2:(length(row.numbers.occS_shape_eq_mirr.LMs)-1)])
sliding.matrix.occ.mirr$slide <- c(row.numbers.occS_shape_eq_mirr.LMs[2:length(row.numbers.occS_shape_eq_mirr.LMs)])
sliding.matrix.occ.mirr$after <- c(row.numbers.occS_shape_eq_mirr.LMs[3:length(row.numbers.occS_shape_eq_mirr.LMs)], row.number.occS_shape_eq_mirr_1.LM)
sliding.matrix.occ <- rbind(sliding.matrix.occ, sliding.matrix.occ.mirr)
}
}
# only if curr.subset contains antennal curve LMs
if(length(grep("antS_shape", curr.subset))>0){
for(m in 1){
if(m==1){
print("antennal LMs...")
# get type I LM rownumbers to define points between which sliding will happen
type.I.LMs <- c("antS_shape_eq_1")
for(t in type.I.LMs){
# find rownumber within array.3D that contains current LM coordinates
assign(paste0("row.number.", t, ".LM"), which(curr.subset.orig == t))
}
# get type III LMs
type.III.LMs <- c("antS_shape_eq")
for(t in type.III.LMs){
# find rownumbers within array.3D that contain current LM coordinates
assign(paste0("row.numbers.", t, ".LMs"), grep(t, curr.subset.orig))
}
# now create matrix (this could also be manually done for each specimen via 'define.sliders(array.3D[, 1], nsliders = 10)')
sliding.matrix.ant <- create_empty_df(nrow = length(c(row.numbers.antS_shape_eq.LMs))-1,
names = c("before", "slide", "after"))
sliding.matrix.ant$before <- c(row.number.antS_shape_eq_1.LM, row.numbers.antS_shape_eq.LMs[2:(length(row.numbers.antS_shape_eq.LMs)-1)])
sliding.matrix.ant$slide <- c(row.numbers.antS_shape_eq.LMs[2:length(row.numbers.antS_shape_eq.LMs)])
sliding.matrix.ant$after <- c(row.numbers.antS_shape_eq.LMs[3:length(row.numbers.antS_shape_eq.LMs)], row.number.antS_shape_eq_1.LM)
}
if(m==2){
print("mirrored antennal LMs...")
# get type I LM rownumbers to define points between which sliding will happen
type.I.LMs <- c("antS_shape_eq_mirr_1")
for(t in type.I.LMs){
# find rownumber within array.3D that contains current LM coordinates
assign(paste0("row.number.", t, ".LM"), which(curr.subset == t))
}
# get type III LMs
type.III.LMs <- c("antS_shape_eq_mirr")
for(t in type.III.LMs){
# find rownumbers within array.3D that contain current LM coordinates
assign(paste0("row.numbers.", t, ".LMs"), grep(t, curr.subset))
}
# now add mirrored LM matrix
sliding.matrix.ant.mirr <- create_empty_df(nrow = length(c(row.numbers.antS_shape_eq_mirr.LMs))-1,
names = c("before", "slide", "after"))
sliding.matrix.ant.mirr$before <- c(row.number.antS_shape_eq_mirr_1.LM, row.numbers.antS_shape_eq_mirr.LMs[2:(length(row.numbers.antS_shape_eq_mirr.LMs)-1)])
sliding.matrix.ant.mirr$slide <- c(row.numbers.antS_shape_eq_mirr.LMs[2:length(row.numbers.antS_shape_eq_mirr.LMs)])
sliding.matrix.ant.mirr$after <- c(row.numbers.antS_shape_eq_mirr.LMs[3:length(row.numbers.antS_shape_eq_mirr.LMs)], row.number.antS_shape_eq_mirr_1.LM)
sliding.matrix.ant <- rbind(sliding.matrix.ant, sliding.matrix.ant.mirr)
}
}
}
# bind all sliding matrices
sliding.matrix <- do.call(rbind, list(sliding.matrix.hc.curves, sliding.matrix.occ, sliding.matrix.ant))
# Procrustes alignment with sliding
gpa <- gpagen(array.3D, curves = sliding.matrix)
summary(gpa$coords)
} else{
# Procrustes alignment without sliding
message("no sliding...")
sliding.matrix <- NULL # create_empty_df(nrow = 1, names = c("before", "slide", "after"))
gpa <- gpagen(array.3D)
summary(gpa$coords)
}
# replace current.moderate and current_montane_fast in converged eco type data with current_fast
classifier$current[which(classifier$current == "current_moderate")] <- "current_fast"
classifier$current[which(classifier$current == "current_montane_fast")] <- "current_fast"
# ECOLOGY REDUCTION
# replace collector_predator in converged eco type data with predator
classifier$feeding.type[which(classifier$feeding.type == "collector_predator")] <- "predator"
# replace collector_browser in converged eco type data with shredder
classifier$feeding.type[which(classifier$feeding.type == "collector_browser")] <- "shredders"
# create geomorph data frmae
gdf <- geomorph.data.frame(gpa,
phy = tree.dropped_ERC,
ERC = as.factor(classifier$ERC),
ID = as.factor(classifier$ID),
order = as.factor(classifier$order),
superfamily = as.factor(classifier$superfamily),
family = as.factor(classifier$family),
feeding.type = as.factor(classifier$feeding.type),
food.larva = as.factor(classifier$food.larva),
food.adult = as.factor(classifier$food.adult),
microhabitat.larva = as.factor(classifier$microhabitat.larva),
microhabitat.adult = as.factor(classifier$microhabitat.adult),
current = as.factor(classifier$current),
wings = as.factor(classifier$wings),
flight = as.factor(classifier$flight))
# ,
# PCoA.1.current.l = as.factor(classifier$PCoA.1.current.l),
# PCoA.2.current.l = as.factor(classifier$PCoA.2.current.l)
# store uncorrected shape data
shape.uncorr <- gpa$coords
# run uncorrected PCA (phy is only added so it can be referenced)
pca.data.uncorr <- gm.prcomp(A = shape.uncorr, phy = tree.dropped_ERC)
############
# simple allometry analysis
allometry.uncorr <- procD.lm(coords~log(Csize), data=gdf, iter=999)
summary(allometry.uncorr)
# Obtain allometry-adjusted residuals
shape.resid.allo.corr <- arrayspecs(allometry.uncorr$residuals, p=dim(gpa$coords)[1], k=dim(gpa$coords)[2])
# store allomtry-corrected shape
shape.allofree <- shape.resid.allo.corr + array(gpa$consensus, dim(shape.resid.allo.corr)) # allometry-free shapes
# run allometry-corrected PCA (phy is only added so it can be referenced)
pca.data.allofree <- gm.prcomp(A = shape.allofree, phy = tree.dropped_ERC)
############
# phylogenetically corrected allomtery
allometry.phylcorr <- procD.pgls(data=gdf, f1 = coords~log(Csize), phy = phy)
summary(allometry.phylcorr)
# Obtain phylogeny- and allometry-adjusted residuals
shape.resid.allo.phylo.corr <- arrayspecs(allometry.phylcorr$pgls.residuals, p=dim(gpa$coords)[1], k=dim(gpa$coords)[2])
# store allomtry-corrected shape
shape.allofree.phylcorr <- shape.resid.allo.phylo.corr + array(gpa$consensus, dim(shape.resid.allo.phylo.corr)) # allometry-free shapes
# run allometry-free and phylocorr PCA (phy is only added so it can be referenced)
pca.data.allofree.phylcorr <- gm.prcomp(A = shape.allofree.phylcorr, phy = tree.dropped_ERC)
############
# assign landmark, GPA, PCA and GPA allo free, size.adjusted shapes, disparity data to work with later
assign(paste0("slid.matrix.", curr.taxon.name, ".", curr.subset.name), sliding.matrix)
assign(paste0("GPA.", curr.taxon.name, ".", curr.subset.name), gpa)
assign(paste0("PCA.uncorr.", curr.taxon.name, ".", curr.subset.name), pca.data.uncorr)
assign(paste0("PCA.allofree.uncorr.", curr.taxon.name, ".", curr.subset.name), pca.data.allofree)
assign(paste0("PCA.allofree.phylcorr.", curr.taxon.name, ".", curr.subset.name), pca.data.allofree.phylcorr)
# assign(paste0("PCA.allofree.phylocorr.", curr.subset.name), pca.data.allofree.phylocorr)
assign(paste0("shape.uncorr.", curr.taxon.name, ".", curr.subset.name), shape.uncorr)
assign(paste0("shape.allofree.phylcorr.", curr.taxon.name, ".", curr.subset.name), shape.allofree.phylcorr)
assign(paste0("allometry.", curr.taxon.name, ".", curr.subset.name), allometry.uncorr)
assign(paste0("allometry.phylcorr.", curr.taxon.name, ".", curr.subset.name), allometry.phylcorr)
# assign(paste0("morph.disp.orders.", curr.subset.name), morphol.disparity.orders)
# assign(paste0("morph.disp.superfamilies.", curr.subset.name), morphol.disparity.superfamilies)
# assign(paste0("array.2D.max.LMs", curr.subset.name), array.2D.max.LMs)
assign(paste0("array.3D.", curr.taxon.name, ".", curr.subset.name), array.3D)
assign(paste0("gdf.", curr.taxon.name, ".", curr.subset.name), gdf)
assign(paste0("classifier.", curr.taxon.name, ".", curr.subset.name), classifier)
# assign trees
assign(paste0("tree.IDs", curr.taxon.name, ".", curr.subset.name), tree.dropped)
assign(paste0("tree.", curr.taxon.name, ".", curr.subset.name), tree.dropped_ERC)
}
}
LM.subsets <- list(head.capsule, LMs.Mds) # head.capsule, head.capsule
LM.subset.names <- c("head.capsule", "LMs.Mds") # "head.capsule", "head.capsule"
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken
# parallel::stopCluster(cluster)
# calculate PCoAs. We use ecology.cats here, which need to be in the created by workspace of script 1
# create list of tibbles with ecological data for stack plotting and PCoA calculation
ecology.cats <- list(current.l, food.l, microhabitat.l,
food.a, microhabitat.a, flight.a, wing.a,
Csize.ant, Csize.occ)
ecology.cat.names <- list("current.l", "food.l", "microhabitat.l",
"food.a", "microhabitat.a", "flight.a", "wing.a",
"Csize.ant", "Csize.occ")
phylo.int.tibble <- create.empty.tibble(nrow = 1, names = c("taxon", "ecol.cat", "LM.set", "n",
"r.pls", "p")) %>%
mutate_at("taxon", as.character) %>%
mutate_at("ecol.cat", as.character) %>%
mutate_at("LM.set", as.character)
PLS.tibble <- create.empty.tibble(nrow = 1, names = c("taxon", "ecol.cat", "LM.set", "n",
"r.pls", "p")) %>%
mutate_at("taxon", as.character) %>%
mutate_at("ecol.cat", as.character) %>%
mutate_at("LM.set", as.character)
row.counter = 1
iterations <- 9999
for(t in 1:length(taxon.names)){ # length(taxon.names)
# run Procrustes PGLS / ANOVAs for all ecol cats and LM sets with corrected and uncorrected data
for(s in 1:length(LM.subset.names)){ # length(LM.subset.names)
for(e in 1:length(ecology.cats)){
curr.taxon.name <- taxon.names[t]
# get current LM.subset name
curr.LM.subset.name <- LM.subset.names[s]
# get current eco cat
curr.eco.cat <- unlist(ecology.cat.names[e])
message(paste0(curr.taxon.name, ": ", curr.eco.cat, ": ", curr.LM.subset.name))
if(curr.taxon.name == "Dermaptera" & curr.eco.cat == "current.l"){
} else{
curr.df.all <- ecology.cats[[e]]
if(curr.taxon.name != "all"){
curr.df <- curr.df.all %>%
filter(order == curr.taxon.name)
} else {
curr.df <- curr.df.all
}
# get classifier according to subset name
curr.classifier <- get(paste0("classifier.", curr.taxon.name, ".", curr.LM.subset.name))
# add ERC
curr.df <- left_join(curr.df, select(curr.classifier, ERC, ID), by = "ID") %>%
drop_na(ERC)
ERCs.with.eco.data <- curr.df$ERC
# get eco data and ID columns only
curr.df <- as.data.frame(curr.df[, 10:ncol(curr.df)])
# delete duplicate rows resulting from multiple scans per species (don't think this is necessary)
curr.df <- distinct(curr.df)
curr.df <- delete.rows.with.na.in.column(curr.df)
# set rownames as ID and delete ID & ERC columns
rownames(curr.df) <- curr.df$ERC
curr.df$ID <- NULL
curr.df$ERC <- NULL
if(curr.eco.cat != "wing.a" & curr.eco.cat != "flight.a" & curr.eco.cat != "Csize.ant" & curr.eco.cat != "Csize.occ"){
# calculate dissimilarity index of community
diss.curr.df <- vegdist(curr.df, method = "bray")
# sim.curr.df <- 1-diss.curr.df
# calculate & plot PCoA
pcoa.curr.df <- pcoa(diss.curr.df)
assign(paste0("PCoA.", curr.taxon.name, ".", curr.LM.subset.name, ".", curr.eco.cat), pcoa.curr.df)
assign(paste0("PCoA.df.", curr.taxon.name, ".", curr.LM.subset.name, ".", curr.eco.cat), curr.df)
# biplot(pcoa.curr.df, curr.df, cex = c(0.5, 0.1), main = paste0("PCoA: ", curr.taxon.name, ": ", curr.eco.cat))
} else {
# safe non-PCoA values for univariate groups
assign(paste0("PCoA.", curr.taxon.name, ".", curr.LM.subset.name, ".", curr.eco.cat), curr.df)
}
############################
if(curr.taxon.name == "Dermaptera" & curr.eco.cat == "current.l"){
# message("Dermaptera have only one value in current.larva PCoA1 and this cannot be tested.")
# ecol.interactions.tibble[nrow(ecol.interactions.tibble)+1, 1] = curr.taxon.name
# ecol.interactions.tibble$ecol.cat[nrow(ecol.interactions.tibble)] = curr.eco.cat
# ecol.interactions.tibble$LM.set[nrow(ecol.interactions.tibble)] = curr.LM.subset.name
# ecol.interactions.tibble$n[nrow(ecol.interactions.tibble)] = 0
# ecol.interactions.tibble$Rsq[nrow(ecol.interactions.tibble)] = 0
# ecol.interactions.tibble$"F"[nrow(ecol.interactions.tibble)] = 0
# ecol.interactions.tibble$Z[nrow(ecol.interactions.tibble)] = 0
# ecol.interactions.tibble$p[nrow(ecol.interactions.tibble)] = 1
# ecol.interactions.tibble$Rsq.corr[nrow(ecol.interactions.tibble)] = 0
# ecol.interactions.tibble$"F.corr"[nrow(ecol.interactions.tibble)] = 0
# ecol.interactions.tibble$Z.corr[nrow(ecol.interactions.tibble)] = 0
# ecol.interactions.tibble$p.corr[nrow(ecol.interactions.tibble)] = 1
} else {
# get tree
curr.tree <- get(paste0("tree.", curr.taxon.name, ".", curr.LM.subset.name))
# find ERC numbers that possess PCoA vectors
if(curr.eco.cat != "wing.a" & curr.eco.cat != "flight.a" & curr.eco.cat != "Csize.ant" & curr.eco.cat != "Csize.occ"){
ERCs.w.PCoAs <- rownames(pcoa.curr.df$vectors)
} else {
ERCs.w.PCoAs <- curr.classifier$ERC[curr.classifier$ERC %in% rownames(curr.df)]
}
# prune tree
tips.to.drop <- setdiff(curr.tree$tip.label, ERCs.w.PCoAs)
curr.tree.dropped <- drop.tip(curr.tree, tips.to.drop)
# find rownumbers in classifier that do not correspond to any PCoA results
if(length(setdiff(curr.classifier$ERC, ERCs.w.PCoAs)) != 0){
curr.classifier <- curr.classifier[-which(curr.classifier$ERC %!in% ERCs.w.PCoAs),]
}
# get array.3D according to subset name
curr.array.3D <- get(paste0("array.3D.", curr.taxon.name, ".", curr.LM.subset.name))
# delete list elements within array.3D that contain no data for PCoA1
curr.array.3D <- prune.array.3D(ERCs = curr.classifier$ERC, array.3D = curr.array.3D)
# run GPA (with sliding in case of "LMs.hc")
if(curr.LM.subset.name == "head.capsule"){
message("sliding...")
curr.gpa <- gpagen(curr.array.3D, curves = get(paste0("slid.matrix.", curr.taxon.name, ".", curr.LM.subset.name)), print.progress = F)
} else {
message("no sliding...")
curr.gpa <- gpagen(curr.array.3D, print.progress = F)
}
# names(curr.gpa$Csize)
# plot(curr.gpa, label = T, plot.param = list(pt.cex = 0.1, txt.cex = 1))
message("PLS analyses...")
if(curr.eco.cat != "wing.a" & curr.eco.cat != "flight.a" & curr.eco.cat != "Csize.ant" & curr.eco.cat != "Csize.occ"){
# sensitivity test for importance of PCoA vectors to check how important their number is
# for(max.PCoA in 2:ncol(pcoa.curr.df$vectors)){
# if(max.PCoA == 2) {
# r.pls.scores <- c()
# p.values <- c()
# }
# message(max.PCoA)
# curr.phylo.integr <- phylo.integration(A = curr.gpa$coords, A2 = pcoa.curr.df$vectors[,1:max.PCoA], phy = curr.tree.dropped,
# iter = iterations, print.progress = F)
# r.pls.scores <- c(r.pls.scores, curr.phylo.integr$r.pls)
# p.values <- c(p.values, curr.phylo.integr$P.value)
# print(curr.phylo.integr)
# }
curr.phylo.integr <- phylo.integration(A = curr.gpa$coords, A2 = pcoa.curr.df$vectors, phy = curr.tree.dropped,
iter = iterations, print.progress = F)
curr.PLS <- two.b.pls(A = curr.gpa$coords, A2 = pcoa.curr.df$vectors,
iter = iterations, print.progress = T)
} else {
# curr.df[,1][curr.df[,1]==100] <- "macropterous"
non.pcoa.vals <- as.data.frame(curr.df[,1]) # setNames(curr.df[,1], rownames(curr.df))
rownames(non.pcoa.vals) <- rownames(curr.df)
if(curr.eco.cat == "Csize.ant" | curr.eco.cat == "Csize.occ"){
for(d in 1:nrow(non.pcoa.vals)){
curr.ERC <- rownames(non.pcoa.vals)[d]
curr.Csize.head <- curr.gpa$Csize[names(curr.gpa$Csize) %in% curr.ERC]
non.pcoa.vals[d,1] <- non.pcoa.vals[d,1]/curr.Csize.head
}
}
curr.phylo.integr <- phylo.integration(A = curr.gpa$coords, A2 = non.pcoa.vals, phy = curr.tree.dropped,
iter = iterations, print.progress = T)
# x <- plot(curr.phylo.integr, label = dimnames(curr.gpa$coords)[[3]])
# picknplot.shape(x)
curr.PLS <- two.b.pls(A = curr.gpa$coords, A2 = non.pcoa.vals,
iter = iterations, print.progress = T)
}
phylo.int.tibble[row.counter,1] <- curr.taxon.name
phylo.int.tibble$ecol.cat[row.counter] <- curr.eco.cat
phylo.int.tibble$LM.set[row.counter] <- curr.LM.subset.name
phylo.int.tibble$n[row.counter] <- nrow(curr.phylo.integr$XScores)
phylo.int.tibble$r.pls[row.counter] <- curr.phylo.integr$r.pls
phylo.int.tibble$p[row.counter] <- curr.phylo.integr$P.value
print(phylo.int.tibble[row.counter,])
PLS.tibble[row.counter,1] <- curr.taxon.name
PLS.tibble$ecol.cat[row.counter] <- curr.eco.cat
PLS.tibble$LM.set[row.counter] <- curr.LM.subset.name
PLS.tibble$n[row.counter] <- nrow(curr.PLS$XScores)
PLS.tibble$r.pls[row.counter] <- curr.PLS$r.pls
PLS.tibble$p[row.counter] <- curr.PLS$P.value
print(PLS.tibble[row.counter,])
assign(paste0("phylo.int__", curr.taxon.name, "__", curr.eco.cat, "__", curr.LM.subset.name), curr.phylo.integr)
row.counter <- row.counter+1
message("**************")
# add proc.D.lm data into tibble
# ecol.interactions.tibble$ecol.cat[nrow(ecol.interactions.tibble)+1] <- curr.LM.subset.name
# ecol.interactions.tibble <- rbind(ecol.interactions.tibble,
# add.procD.lm.data.to.tibble(taxon = curr.taxon.name,
# eco.cat = curr.eco.cat,
# procD.lm.results = curr.procD.lm,
# procD.pgls.results = curr.procD.pgls,
# LM.subset.name = curr.LM.subset.name))
####################
}
}
}
}
}
# sort phylo tibble
phylo.int.tibble <- phylo.int.tibble %>%
# group_by(taxon) %>%
arrange(taxon, LM.set, factor(ecol.cat, levels = c("current.l", "food.l", "food.a", "microhabtitat.l", "microhabtitat.a", "wings", "flight")))
# sort phylo tibble
PLS.tibble <- PLS.tibble %>%
# group_by(taxon) %>%
arrange(taxon, LM.set, factor(ecol.cat, levels = c("current.l", "food.l", "food.a", "microhabtitat.l", "microhabtitat.a", "wings", "flight")))
if(remove.specimens == T){
write.xlsx2(as.data.frame(phylo.int.tibble), paste0("./results/", today(), " - phylo.int.bray-curtis_filtered.xlsx"), row.names = F)
write.xlsx2(as.data.frame(PLS.tibble), paste0("./results/", today(), " - non-phylo.int.bray-curtis_filtered.xlsx"), row.names = F)
} else if(remove.specimens == F){
write.xlsx2(as.data.frame(phylo.int.tibble), paste0("./results/", today(), " - phylo.int.bray-curtis.xlsx"), row.names = F)
write.xlsx2(as.data.frame(PLS.tibble), paste0("./results/", today(), " - non-phylo.int.bray-curtis.xlsx"), row.names = F)
}
# compare PLS
# all
# HC
curr.taxon = "all"
all.phylo.integr <- compare.pls(phylo.int__all__current.l__head.capsule, phylo.int__all__food.l__head.capsule,
phylo.int__all__food.a__head.capsule, phylo.int__all__microhabitat.l__head.capsule,
phylo.int__all__microhabitat.a__head.capsule,
phylo.int__all__flight.a__head.capsule, phylo.int__all__wing.a__head.capsule,
phylo.int__all__Csize.ant__head.capsule, phylo.int__all__Csize.occ__head.capsule)
PLS.compare.tibble.all.hc <- compare.PLS.to.data.frame(pls.list = all.phylo.integr, write = T, taxon = curr.taxon, suffix = "hc")
PLS.compare.tibble.all.hc$taxon <- curr.taxon
PLS.compare.tibble.all.hc$LM.set <- "head.capsule"
compare.PLS.to.pairwise.data.frame(pls.list = all.phylo.integr, m = "p", write = T, taxon = "all", suffix = "hc")
compare.PLS.to.pairwise.data.frame(pls.list = all.phylo.integr, m = "z", write = T, taxon = "all", suffix = "hc")
# all
# Mds
all.phylo.integr.Md <- compare.pls(phylo.int__all__current.l__LMs.Mds, phylo.int__all__food.l__LMs.Mds,
phylo.int__all__food.a__LMs.Mds, phylo.int__all__microhabitat.l__LMs.Mds,
phylo.int__all__microhabitat.a__LMs.Mds,
phylo.int__all__flight.a__LMs.Mds, phylo.int__all__wing.a__LMs.Mds,
phylo.int__all__Csize.ant__LMs.Mds, phylo.int__all__Csize.occ__LMs.Mds)
PLS.compare.tibble.all.Md <- compare.PLS.to.data.frame(pls.list = all.phylo.integr.Md, write = T, taxon = "all", suffix = "Md")
PLS.compare.tibble.all.Md$taxon <- curr.taxon
PLS.compare.tibble.all.Md$LM.set <- "LMs.Mds"
compare.PLS.to.pairwise.data.frame(pls.list = all.phylo.integr.Md, m = "p", write = T, taxon = "all", suffix = "Md")
compare.PLS.to.pairwise.data.frame(pls.list = all.phylo.integr.Md, m = "z", write = T, taxon = "all", suffix = "Md")
# Dermaptera
curr.taxon = "Dermaptera"
# head capsule
Dermaptera.phylo.integr <- compare.pls(phylo.int__Dermaptera__food.l__head.capsule,
phylo.int__Dermaptera__food.a__head.capsule, phylo.int__Dermaptera__microhabitat.l__head.capsule,
phylo.int__Dermaptera__microhabitat.a__head.capsule,
phylo.int__Dermaptera__flight.a__head.capsule, phylo.int__Dermaptera__wing.a__head.capsule,
phylo.int__Dermaptera__Csize.ant__head.capsule, phylo.int__Dermaptera__Csize.occ__head.capsule)
PLS.compare.tibble.Derma.hc <- compare.PLS.to.data.frame(pls.list = Dermaptera.phylo.integr, write = T, taxon = "Dermaptera", suffix = "hc")
PLS.compare.tibble.Derma.hc$taxon <- curr.taxon
PLS.compare.tibble.Derma.hc$LM.set <- "head.capsule"
compare.PLS.to.pairwise.data.frame(pls.list = Dermaptera.phylo.integr, m = "p", write = T, taxon = "Dermaptera", suffix = "hc")
compare.PLS.to.pairwise.data.frame(pls.list = Dermaptera.phylo.integr, m = "z", write = T, taxon = "Dermaptera", suffix = "hc")
# Dermaptera
# Mds
Dermaptera.phylo.integr.Mds <- compare.pls(phylo.int__Dermaptera__food.l__LMs.Mds,
phylo.int__Dermaptera__food.a__LMs.Mds, phylo.int__Dermaptera__microhabitat.l__LMs.Mds,
phylo.int__Dermaptera__microhabitat.a__LMs.Mds,
phylo.int__Dermaptera__flight.a__LMs.Mds, phylo.int__Dermaptera__wing.a__LMs.Mds,
phylo.int__Dermaptera__Csize.ant__LMs.Mds, phylo.int__Dermaptera__Csize.occ__LMs.Mds)
PLS.compare.tibble.Derma.Md <- compare.PLS.to.data.frame(pls.list = Dermaptera.phylo.integr.Mds, write = T, taxon = "Dermaptera", suffix = "Md")
PLS.compare.tibble.Derma.Md$taxon <- curr.taxon
PLS.compare.tibble.Derma.Md$LM.set <- "LMs.Mds"
compare.PLS.to.pairwise.data.frame(pls.list = Dermaptera.phylo.integr.Mds, m = "p", write = T, taxon = "Dermaptera", suffix = "Md")
compare.PLS.to.pairwise.data.frame(pls.list = Dermaptera.phylo.integr.Mds, m = "z", write = T, taxon = "Dermaptera", suffix = "Md")
# Plecoptera
curr.taxon = "Plecoptera"
# head capsule
Plecoptera.phylo.integr <- compare.pls(phylo.int__Plecoptera__current.l__head.capsule, phylo.int__Plecoptera__food.l__head.capsule,
phylo.int__Plecoptera__food.a__head.capsule, phylo.int__Plecoptera__microhabitat.l__head.capsule,
phylo.int__Plecoptera__microhabitat.a__head.capsule,
phylo.int__Plecoptera__flight.a__head.capsule, phylo.int__Plecoptera__wing.a__head.capsule,
phylo.int__Plecoptera__Csize.ant__head.capsule, phylo.int__Plecoptera__Csize.occ__head.capsule)
PLS.compare.tibble.Pleco.hc <- compare.PLS.to.data.frame(pls.list = Plecoptera.phylo.integr, write = T, taxon = "Plecoptera", suffix = "hc")
PLS.compare.tibble.Pleco.hc$taxon <- curr.taxon
PLS.compare.tibble.Pleco.hc$LM.set <- "head.capsule"
compare.PLS.to.pairwise.data.frame(pls.list = Plecoptera.phylo.integr, m = "p", write = T, taxon = "Plecoptera", suffix = "hc")
compare.PLS.to.pairwise.data.frame(pls.list = Plecoptera.phylo.integr, m = "z", write = T, taxon = "Plecoptera", suffix = "hc")
# Plecoptera
# Mds
Plecoptera.phylo.integr.Mds <- compare.pls(phylo.int__Plecoptera__current.l__LMs.Mds, phylo.int__Plecoptera__food.l__LMs.Mds,
phylo.int__Plecoptera__food.a__LMs.Mds, phylo.int__Plecoptera__microhabitat.l__LMs.Mds,
phylo.int__Plecoptera__microhabitat.a__LMs.Mds,
phylo.int__Plecoptera__flight.a__LMs.Mds, phylo.int__Plecoptera__wing.a__LMs.Mds,
phylo.int__Plecoptera__Csize.ant__LMs.Mds, phylo.int__Plecoptera__Csize.occ__LMs.Mds)
PLS.compare.tibble.Pleco.Md <- compare.PLS.to.data.frame(pls.list = Plecoptera.phylo.integr.Mds, write = T, taxon = "Plecoptera", suffix = "Md")
PLS.compare.tibble.Pleco.Md$taxon <- curr.taxon
PLS.compare.tibble.Pleco.Md$LM.set <- "LMs.Mds"
compare.PLS.to.pairwise.data.frame(pls.list = Plecoptera.phylo.integr.Mds, m = "p", write = T, taxon = "Plecoptera", suffix = "Md")
compare.PLS.to.pairwise.data.frame(pls.list = Plecoptera.phylo.integr.Mds, m = "z", write = T, taxon = "Plecoptera", suffix = "Md")
# bind all PLS compare tibbles
PLS.compare.tibble <- bind_rows(PLS.compare.tibble.all.hc, PLS.compare.tibble.all.Md,
PLS.compare.tibble.Derma.hc, PLS.compare.tibble.Derma.Md,
PLS.compare.tibble.Pleco.hc, PLS.compare.tibble.Pleco.Md)
# join phylo.int.tibble and PLS.compare.tibble
phylo.int.tibble <- left_join(phylo.int.tibble, PLS.compare.tibble, by = c("ecol.cat", "taxon", "LM.set"))