-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_2020-11-02 - create array2D_01.R
1456 lines (1222 loc) · 66.8 KB
/
1_2020-11-02 - create array2D_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
# plot_cols = "order" # order family subfamily
# order_to_plot = c("Dermaptera", "Plecoptera") # all Dermaptera Plecoptera Blattodea Odonata
remove.specimens = F
analyse_eyes = F
analyse_antennae = F
recalculate_eco_struff = T
mirrored = F
orders_to_analyze = c("Dermaptera", "Plecoptera") # "Plecoptera", "Dermaptera", "Psocodea", "Zoraptera" "all"
# plot_alometry = T
# plot_LMs_3D = T
source("//blanke-nas-1/PROTOCOLS/_SCRIPTS/R/Peter/functions_PTR.R")
############## 1 #############
start.time <- Sys.time()
wd <- "//blanke-nas-1/PROTOCOLS/_SCRIPTS/R/Peter/checkpoint_LMs/all_Checkpoint_files/" # test_add_eye_curve/
setwd(wd)
# load LM set for current subgroup <- use all possible landmarks because it makes more sense to filter after array_2D creation
if(mirrored == F){
subgroup_landmarkset <- as.character(read.csv("//blanke-nas-1/PROTOCOLS/_SCRIPTS/R/Peter/checkpoint_LMs/all_possible_head_landmarks.csv",
header = F)$V1)
} else {
subgroup_landmarkset <- as.character(read.csv("//blanke-nas-1/PROTOCOLS/_SCRIPTS/R/Peter/checkpoint_LMs/all_possible_head_landmarks_mirr.csv",
header = F)$V1)
}
# load all checkpoint files in wd
file_list <- list.files(wd, pattern = "*.ckpt$", recursive = F, full.names = T)
# load basetable
PBT <- load.PBT()
PBT_Derma_Pleco <- PBT[PBT$order=="Plecoptera" | PBT$order=="Dermaptera" & PBT$LMs != "-",]
ERCs_Derma_Pleco <- PBT_Derma_Pleco$ERC[complete.cases(PBT_Derma_Pleco$ERC)]
# find empty checkpoint files
no_LM_files <- find.empty.checkpoint.files(file.list = file_list)
# remove empty checkpoint files
file_list <- setdiff(file_list, no_LM_files)
# reduce to file names
file_names <- basename(file_list)
# get ERC numbers from filenames
ERC_numbers_all <- sub("^(\\d+)_.+$", "\\1", file_names)
# remove some taxa
if(remove.specimens){
Hemimeridae <- c("1604", "1744", "0015", "0017", "1602", "1603")
Arixeniidae <- c("1601", "0657", "1715", "1600", "1743")
parasites <- c(Hemimeridae, Arixeniidae)
list.positions.parasites <- which(ERC_numbers_all %in% parasites)
ERC_numbers_all <- ERC_numbers_all[-list.positions.parasites]
file_list <- file_list[-list.positions.parasites]
PBT_Derma_Pleco <- PBT_Derma_Pleco %>%
filter(ERC %!in% parasites)
message(paste0("removed ", paste(parasites, collapse = ", "), " from PBT and file_list."))
}
# filter out files that are new or have been modified
# old_file_infos <- read.csv("file_infos.csv")
# for(f in 1:length(file_list)){
# if(f==1){
# files_to_analyze <- c()
# }
# curr_mod_time <- file.info(file_list[f])[1,4]
# curr_old_mod_time <- as.character(old_file_infos$mtime[old_file_infos$file %in% file_names[f]])
# time_diff <- floor(as.numeric(difftime(curr_mod_time, curr_old_mod_time)))
#
# if(time_diff != 0){
# files_to_analyze <- c(files_to_analyze, ERC_numbers_all[f])
# }
# }
if(mirrored == F){
landmarkset_list <- get.checkpoint.LMs(file_list = file_list, orders_to_analyze = orders_to_analyze, mirror = F)
} else {
landmarkset_list <- get.checkpoint.LMs(file_list = file_list, orders_to_analyze = orders_to_analyze, mirror = T)
}
array_2D <- array.2D.from.LM.list(LM.list = landmarkset_list, LM.names = subgroup_landmarkset)
# check dims of array_2D
dim(array_2D)
# store a copy of array_2D to play with
array_2D_ <- array_2D
# array_2D <- array_2D_
# create interpolated equidistant eye type 3 LMs
if(analyse_eyes==T){
# define the number of type 3 LMs that should represent the occipital suture in the end
no_eye_LMs = 20
# define the number of interpolated points
no_curve_LMs_before_thrsh = 1000 # 1000!
# add columns to array_2D to house the equidistant occS LM x, y, z coords
array_2D[, (ncol(array_2D)+1):(ncol(array_2D)+no_eye_LMs*3)] <- NA
# add colnames to those columns
LM.counter = 1
for(o in seq((ncol(array_2D)-(no_eye_LMs*3)+1), (ncol(array_2D)), 3)){
colnames(array_2D)[o] <- paste0("occS_shape_eq_", LM.counter, "_X")
colnames(array_2D)[o+1] <- paste0("occS_shape_eq_", LM.counter, "_Y")
colnames(array_2D)[o+2] <- paste0("occS_shape_eq_", LM.counter, "_Z")
LM.counter = LM.counter+1
}
# go through every row of array_2D
for(r in 1:nrow(array_2D)){
show.progress(r, nrow(array_2D))
# check if row contains occS landmarks and mandibular condyle
if(!is.na(array_2D$occS_shape_1_X[r]) & !is.na(array_2D$mdcond_ant_X[r])){
# get subset of array_2D that contains occ. suture data of current ERC
curr.occS.coords <- array_2D[r, ] %>%
select(matches("occS_shape_\\d+")) %>%
t()
# create nicer dataframe from the subset
current_eye_LMs <- create_empty_df(nrow = nrow(curr.occS.coords)/3+1, names = c("x", "y", "z", "name", "count"))
df.row.counter = 1
for(t in seq(1, nrow(curr.occS.coords), 3)){
current_eye_LMs$x[df.row.counter] <- curr.occS.coords[t,1]
current_eye_LMs$y[df.row.counter] <- curr.occS.coords[t+1,1]
current_eye_LMs$z[df.row.counter] <- curr.occS.coords[t+2,1]
# current_eye_LMs$name[df.row.counter] <- paste0("occS_shape_", df.row.counter)
df.row.counter <- df.row.counter+1
}
# take first row and copy it to be the last row as well to close the loop to prevent weird interpolation behaviour
current_eye_LMs[nrow(current_eye_LMs),] <- current_eye_LMs[1,]
# add LM names to name columne
current_eye_LMs$name <- paste0("occS_shape_", 1:nrow(current_eye_LMs))
# add numbers into coutner column
current_eye_LMs$count <- 1:nrow(current_eye_LMs)
# create sequence of numbers that range from 1 (= min(current_eye_LMs$count)) to ncol(current_eye_LMs) (= max(current_eye_LMs$count) with the length "no_curve_LMs_before_thrsh"
current_eye_LMs_ts <- seq(from = min(current_eye_LMs$count), max(current_eye_LMs$count), length = no_curve_LMs_before_thrsh)
# create interpolated curve along the curve LMs' coordinates
current_eye_LMs_curve <- apply(current_eye_LMs[,-c(4,5)], 2, function(u) spline(current_eye_LMs$count, u, xout = current_eye_LMs_ts)$y)
# get coordinates of mdcond_ant LM
md_ant_coords <- array_2D[r, ] %>%
select(matches("frontS_X_coronS")) # !! here:! changed!
# find closest eye-curve-point to anterior Md condyle
for(l in 1:nrow(current_eye_LMs_curve)){
# before first loop:
if(l == 1){
# set the current minimal distance to infinity
min_dist = Inf
# create empty list of distances
dists <- c()
}
# get current curve interpolated point coordinates
curr_curvepoint_coords <- current_eye_LMs_curve[l,]
# calculate the distance between these coordinates and the mdcond_ant coordinates
curr_dist <- dist(rbind(md_ant_coords,curr_curvepoint_coords))
# print(paste0(l, " --> ", curr_dist))
# add this value to the list of distances
dists[l] <- curr_dist
# check if current distance is smaller then the minimal distance that was found before
if(curr_dist < min_dist){
# save new minimal distance
min_dist = as.numeric(curr_dist)
# save row number of curve point with new minimal distance
min_dist_row <- l
# print(paste0("MIN: ", l, " --> ", min_dist, " (", min_dist_row, ")"))
}
}
# sort eye-curve according to minimal distance
current_eye_LMs_sorted <- rbind(current_eye_LMs_curve[min_dist_row:nrow(current_eye_LMs_curve),],
current_eye_LMs_curve[1:(min_dist_row-1),])
# create dataframe of curve landmarks to calculate the distances between them in order to decrease number of curve points
sorted.LMs.curve.df <- data.frame(current_eye_LMs_sorted) # sorted.LMs.curve.df[,"dist"] <- NA
# calculate all distances to previous curve LMs
system.time({
sorted.LMs.curve.df$dist <- 0
for(l in 2:nrow(sorted.LMs.curve.df)){
sorted.LMs.curve.df$dist[l] <- dist(rbind(sorted.LMs.curve.df[(l-1),1:3], sorted.LMs.curve.df[l,1:3]))
}
})
# calculate total distance between all curve points ~~ circumference of occipital suture
current_total_eye_dist <- sum(sorted.LMs.curve.df$dist)
# calculate the distance threshold to reduce curve points to "no_eye_LMs" equidistant points
eye_threshold <- current_total_eye_dist/no_eye_LMs
# print original LMs and curve in 3D
# plot3d( current_eye_LMs_curve, type="l", lwd=5, col="navy" )
# spheres3d(current_eye_LMs[,-c(4,5)], radius=4, col="orange")
# spheres3d(md_ant_coords, radius=5, col="red")
# distance_line <- segments3d(x=c(md_ant_coords[1,1],current_eye_LMs_sorted[1,1]),
# y=c(md_ant_coords[1,2],current_eye_LMs_sorted[1,2]),
# z=c(md_ant_coords[1,3],current_eye_LMs_sorted[1,3]))
# aspect3d("iso")
# reduce eye curve to "no_eye_LMs" LMs by checking if cumulative distance of neighboring curve points crossed eye_threshold-distance
for(d in 1:nrow(sorted.LMs.curve.df)){
if(d==1){
curr_cumul_dist = 0
row.numbers <- c(d)
# spheres3d(current_eye_LMs_sorted[d,], radius=5, col="green")
} else{
curr_cumul_dist = curr_cumul_dist+sorted.LMs.curve.df$dist[d]
if(curr_cumul_dist >= eye_threshold){
# spheres3d(current_eye_LMs_sorted[d,], radius=5, col="green")
row.numbers <- c(row.numbers, d)
curr_cumul_dist = 0
}
}
}
# reduce sorted.LMs.curve.df to contain only the equidistant LMs
eq.LMs.df <- sorted.LMs.curve.df[row.numbers, 1:3]
# convert this df into a vector of all coords
eq.LMs.vector <- as.vector(t(eq.LMs.df))
# convert array_2D back into data.frame
array_2D <- as.data.frame(array_2D)
# insert this vector into the end of the current row of the array_2D
array_2D[r, (ncol(array_2D)-(no_eye_LMs*3)+1):(ncol(array_2D))] <- eq.LMs.vector
}
}
}
# create interpolated equidistant antenna type 3 LMs
if(analyse_antennae==T){
# define the number of type 3 LMs that should represent the occipital suture in the end
no.eq.LMs = 20
# define the number of interpolated points
no_curve_LMs_before_thrsh = 1000 # 1000!
# add columns to array_2D to house the equidistant occS LM x, y, z coords
array_2D[, (ncol(array_2D)+1):(ncol(array_2D)+no.eq.LMs*3)] <- NA
# add colnames to those columns
LM.counter = 1
for(o in seq((ncol(array_2D)-(no.eq.LMs*3)+1), (ncol(array_2D)), 3)){
colnames(array_2D)[o] <- paste0("antS_shape_eq_", LM.counter, "_X")
colnames(array_2D)[o+1] <- paste0("antS_shape_eq_", LM.counter, "_Y")
colnames(array_2D)[o+2] <- paste0("antS_shape_eq_", LM.counter, "_Z")
LM.counter = LM.counter+1
}
# go through every row of array_2D
for(r in 1:nrow(array_2D)){
show.progress(r, nrow(array_2D))
# check if row contains antennal landmarks and mandibular condyle LMs
if(!is.na(array_2D$antS_shape_1_X[r]) & !is.na(array_2D$mdcond_ant_X[r])){
# get subset of array_2D that contains occ. suture data of current ERC
curr.shape.coords <- array_2D[r, ] %>%
select(matches("antS_shape_\\d+")) %>%
t()
# create nicer dataframe from the subset
current.LMs.df <- create_empty_df(nrow = nrow(curr.shape.coords)/3+1, names = c("x", "y", "z", "name", "count"))
df.row.counter = 1
for(t in seq(1, nrow(curr.shape.coords), 3)){
current.LMs.df$x[df.row.counter] <- curr.shape.coords[t,1]
current.LMs.df$y[df.row.counter] <- curr.shape.coords[t+1,1]
current.LMs.df$z[df.row.counter] <- curr.shape.coords[t+2,1]
# current.LMs.df$name[df.row.counter] <- paste0("antS_shape_", df.row.counter)
df.row.counter <- df.row.counter+1
}
# take first row and copy it to be the last row as well to close the loop to prevent weird interpolation behaviour
current.LMs.df[nrow(current.LMs.df),] <- current.LMs.df[1,]
# add LM names to name columne
current.LMs.df$name <- paste0("antS_shape_", 1:nrow(current.LMs.df))
# add numbers into coutner column
current.LMs.df$count <- 1:nrow(current.LMs.df)
# create sequence of numbers that range from 1 (= min(current.LMs.df$count)) to ncol(current.LMs.df) (= max(current.LMs.df$count) with the length "no_curve_LMs_before_thrsh"
current.LMs.df.ts <- seq(from = min(current.LMs.df$count), max(current.LMs.df$count), length = no_curve_LMs_before_thrsh)
# create interpolated curve along the curve LMs' coordinates
current.LMs.df.curve <- apply(current.LMs.df[,-c(4,5)], 2, function(u) spline(current.LMs.df$count, u, xout = current.LMs.df.ts)$y)
# get coordinates of mdcond_ant LM
md_ant_coords <- array_2D[r, ] %>%
select(matches("mdcond_ant"))
# find closest eye-curve-point to anterior Md condyle
for(l in 1:nrow(current.LMs.df.curve)){
# before first loop:
if(l == 1){
# set the current minimal distance to infinity
min_dist = Inf
# create empty list of distances
dists <- c()
}
# get current curve interpolated point coordinates
curr_curvepoint_coords <- current.LMs.df.curve[l,]
# calculate the distance between these coordinates and the mdcond_ant coordinates
curr_dist <- dist(rbind(md_ant_coords,curr_curvepoint_coords))
# print(paste0(l, " --> ", curr_dist))
# add this value to the list of distances
dists <- c(dists, curr_dist)
# check if current distance is smaller then the minimal distance that was found before
if(curr_dist < min_dist){
# save new minimal distance
min_dist = as.numeric(curr_dist)
# save row number of curve point with new minimal distance
min_dist_row <- l
# print(paste0("MIN: ", l, " --> ", min_dist, " (", min_dist_row, ")"))
}
}
# sort eye-curve according to minimal distance
current.LMs.df.sorted <- rbind(current.LMs.df.curve[min_dist_row:nrow(current.LMs.df.curve),],
current.LMs.df.curve[1:(min_dist_row-1),])
# create dataframe of curve landmarks to calculate the distances between them in order to decrease number of curve points
sorted.LMs.curve.df <- data.frame(current.LMs.df.sorted)
# add NA column with name "dist"
sorted.LMs.curve.df[,"dist"] <- NA
sorted.LMs.curve.df$dist <- 0
# calculate all distances to previous curve LMs
for(l in 2:nrow(sorted.LMs.curve.df)){
sorted.LMs.curve.df$dist[l] <- dist(rbind(sorted.LMs.curve.df[(l-1),1:3], sorted.LMs.curve.df[l,1:3]))
}
# calculate total distance between all curve points ~~ circumference of occipital suture
current_total_eye_dist <- sum(sorted.LMs.curve.df$dist)
# calculate the distance threshold to reduce curve points to "no.eq.LMs" equidistant points
eye_threshold <- current_total_eye_dist/no.eq.LMs
# print original LMs and curve in 3D
# plot3d( current.LMs.df.curve, type="l", lwd=5, col="navy" )
# spheres3d(current.LMs_df[,-c(4,5)], radius=4, col="orange")
# spheres3d(md_ant_coords, radius=5, col="red")
# distance_line <- segments3d(x=c(md_ant_coords[1,1],current.LMs.df.sorted[1,1]),
# y=c(md_ant_coords[1,2],current.LMs.df.sorted[1,2]),
# z=c(md_ant_coords[1,3],current.LMs.df.sorted[1,3]))
# aspect3d("iso")
# reduce eye curve to "no.eq.LMs" LMs by checking if cumulative distance of neighboring curve points crossed eye_threshold-distance
for(d in 1:nrow(sorted.LMs.curve.df)){
if(d==1){
curr_cumul_dist = 0
row.numbers <- c(d)
# spheres3d(current.LMs.df.sorted[d,], radius=5, col="green")
} else{
curr_cumul_dist = curr_cumul_dist+sorted.LMs.curve.df$dist[d]
if(curr_cumul_dist >= eye_threshold){
# spheres3d(current.LMs.df.sorted[d,], radius=5, col="green")
row.numbers <- c(row.numbers, d)
curr_cumul_dist = 0
}
}
}
# reduce sorted.LMs.curve.df to contain only the equidistant LMs
eq.LMs.df <- sorted.LMs.curve.df[row.numbers, 1:3]
# convert this df into a vector of all coords
eq.LMs.vector <- as.vector(t(eq.LMs.df))
# convert array_2D back into data.frame
array_2D <- as.data.frame(array_2D)
# insert eq.LMs.vector into the end of the current row of the array_2D
array_2D[r, (ncol(array_2D)-(no.eq.LMs*3)+1):(ncol(array_2D))] <- eq.LMs.vector
}
}
}
# save a current version of the array_2D to play with
array_2D_save1 <- array_2D
# put ERC numbers back as rownames and delete ERC column
array_2D <- as.data.frame(array_2D)
rownames(array_2D) <- array_2D$ERC
array_2D$ERC <- NULL
# save a current version of the array_2D to play with
array_2D_save2 <- array_2D
# # create 2D array to check which landmarks are present: delete all y- and z-coord. columns
# array_2D.check <- array_2D[, seq(1,ncol(array_2D),3)]
#
# # delete columns so that curve landmarks are only represented by one the x-coord. column
# for (i in 2:28) {
# delete.col <- paste0("antS_shape_", i, "_X")
# array_2D.check <- array_2D.check[, -which(names(array_2D.check) %in% delete.col)]
# }
#
# for (i in 2:35) {
# delete.col <- paste0("occS_shape_", i, "_X")
# array_2D.check <- array_2D.check[, -which(names(array_2D.check) %in% delete.col)]
# }
#
# for (i in 2:10) {
# delete.col <- paste0("clypeus_shape_", i, "_X")
# array_2D.check <- array_2D.check[, -which(names(array_2D.check) %in% delete.col)]
# }
#
# for (i in 2:20) {
# delete.col <- paste0("frons_shape_", i, "_X")
# array_2D.check <- array_2D.check[, -which(names(array_2D.check) %in% delete.col)]
# }
#
# for (i in 2:20) {
# delete.col <- paste0("vertex_shape_", i, "_X")
# array_2D.check <- array_2D.check[, -which(names(array_2D.check) %in% delete.col)]
# }
#
# for (i in 2:20) {
# delete.col <- paste0("occS_shape_eq_", i, "_X")
# array_2D.check <- array_2D.check[, -which(names(array_2D.check) %in% delete.col)]
# delete.col <- paste0("antS_shape_eq_", i, "_X")
# array_2D.check <- array_2D.check[, -which(names(array_2D.check) %in% delete.col)]
# }
#
# # create array 2D to easily identify missing LMs: replace all non-NAs with x
# array_2D_x <- array_2D.check
# array_2D_x[!is.na(array_2D_x)] <- "x"
#
# write.xlsx2(as.data.frame(array_2D_x), paste0(today(), "_array.2D.LMs.x.xlsx"), row.names = T)
# # clean array from NAs (each LANDMARK with a single NA will be deleted)
# completed_array_2D_max_specimen <- array_2D[,colMeans(is.na(array_2D)) == 0] # use only landmarks without NAs
#
# # clean array from NAs (each SPECIMEN with a single NA will be deleted)
# completed_array_2D_max_LMs<- array_2D[rowMeans(is.na(array_2D)) == 0,] # use only landmarks without NAs
#
if(remove.specimens){
write.xlsx2(as.data.frame(array_2D), paste0(today(), "_array.2D.LMs_filtered.xlsx"), row.names = T)
message(paste0("all done! Copy data from ", paste0(today(), "_array.2D.LMs_filtered.xlsx"), " into sheet tab 'array.2D.LM."))
} else {
write.xlsx2(as.data.frame(array_2D), paste0(today(), "_array.2D.LMs.xlsx"), row.names = T)
message(paste0("all done! Copy data from ", paste0(today(), "_array.2D.LMs.xlsx"), " into sheet tab 'array.2D.LM."))
}
##### 2 #####
if (recalculate_eco_struff == T){
###### load species file taxonomy #####
SF.taxonomy <- load.SF.taxonomy() %>%
filter(order == "Dermaptera" | order == "Plecoptera") %>%
mutate(ID = paste0(genus, "_", species))
###### load PBT #####
PBT <- load.PBT()
# filter Dermas and Plecos that are in array.2D; create IDs (also for GBFIf tax.infos); select columns of interest only
PBT.Derma.Pleco.LMs <- PBT %>%
filter(order == "Dermaptera" | order == "Plecoptera") %>%
# filter(LMs == "x" | LMs == "a" | LMs == "g" | LMs == "gg" | LMs == "x (in Public)") %>%
mutate(ID = paste0(genus_GBIF, "_", species_GBIF)) %>%
# mutate(ID_GBIF = paste0(genus_GBIF, "_", species_GBIF)) %>%
dplyr::select(ERC, genus_GBIF, species_GBIF, LMs, ID) %>%#genus, species, order, suborder, infraorder, superfamily, family, subfamily,
left_join(select(SF.taxonomy, order, suborder, infraorder, superfamily, family, subfamily, genus, species),
by = c("genus_GBIF" = "genus", "species_GBIF" = "species"))
# add taxon infos for species that have no species name (e.g. Kamimura sp.)
for (i in which(is.na(PBT.Derma.Pleco.LMs$order))){
curr.PBT.row <- which(SF.taxonomy$genus == PBT.Derma.Pleco.LMs$genus_GBIF[i])[1]
PBT.Derma.Pleco.LMs$order[i] <- SF.taxonomy$order[curr.PBT.row]
PBT.Derma.Pleco.LMs$suborder[i] <- SF.taxonomy$suborder[curr.PBT.row]
PBT.Derma.Pleco.LMs$infraorder[i] <- SF.taxonomy$infraorder[curr.PBT.row]
PBT.Derma.Pleco.LMs$superfamily[i] <- SF.taxonomy$superfamily[curr.PBT.row]
PBT.Derma.Pleco.LMs$family[i] <- SF.taxonomy$family[curr.PBT.row]
PBT.Derma.Pleco.LMs$subfamily[i] <- SF.taxonomy$subfamily[curr.PBT.row]
}
# delete rows that contain NA
PBT.Derma.Pleco.LMs <- delete.rows.with.na.in.column(PBT.Derma.Pleco.LMs, 6) # col 6 = order
# remove lines from PBT that contain data on non-landmarked species
PBT.Derma.Pleco.LMs <- PBT.Derma.Pleco.LMs %>%
filter(ERC %in% rownames(array_2D))
# remove 'GBIF' from colnames
colnames(PBT.Derma.Pleco.LMs)[2:3] <- c("genus", "species")
###### load CHW #####
CWH <- as_tibble(suppressWarnings({gsheet2tbl('https://docs.google.com/spreadsheets/d/1aP6E7rKyUBn7WhX_rQLDXlFFS0egGN3tFkeCUeQa2yo#gid=0')}))
# delete first four columns which contain taxonomic infos
CWH <- CWH[, -c(1:4)]
# use second row elements as colnames
colnames(CWH) <- CWH[1,]
# change first to column names to genus and species
colnames(CWH)[1:2] <- c("genus", "species")
# delete first row; only use certain columns
CWH <- CWH[-1,1:29]
# change data column types to double and replace NAs with 0
CWH <- CWH %>%
mutate_at(vars(current_fast,current_moderate,current_slow,current_none,
aquatic_on_under_stones_logs, aquatic_in_sand_mud_detritus, aquatic_on_plants_incl_moss,
on_stones_bark_plants_ice, in_leaf_litter, under_stones_bark_etc, epizoic, detritus,
macropterous, brachypterous, micropterous, apterous, yes,
yes_no_further_infos, agile, marginal, flightless), funs(as.double)) %>%
mutate_at(vars(current_fast,current_moderate,current_slow,current_none,
aquatic_on_under_stones_logs, aquatic_in_sand_mud_detritus, aquatic_on_plants_incl_moss, on_stones_bark_plants_ice,
in_leaf_litter, under_stones_bark_etc, epizoic, detritus,
macropterous, brachypterous, micropterous, apterous, yes,
yes_no_further_infos, agile, marginal, flightless), ~replace_na(., 0)) %>%
mutate(ID = paste0(genus, "_", species))
# remove lines from CWH that contain data on non-landmarked species
CWH <- CWH %>%
filter(ID %in% PBT.Derma.Pleco.LMs$ID)
for(l in 1:nrow(CWH)){
# fuse micropterous and brachypterous into brachypterous
if(!is.na(CWH$micropterous[l])){
CWH$brachypterous[l] <- sum(CWH$brachypterous[l], CWH$micropterous[l], na.rm = T)
}
# fuse micropterous and apterous into apterous
if(!is.na(CWH$brachypterous[l])){
CWH$apterous[l] <- sum(CWH$apterous[l], CWH$brachypterous[l], na.rm = T)
}
# fuse yes_no_further_infos and agile into agile
if(!is.na(CWH$yes_no_further_infos[l])){
CWH$agile[l] <- sum(CWH$yes_no_further_infos[l], CWH$agile[l], na.rm = T)
}
# fuse marginal and agile into agile
if(!is.na(CWH$marginal[l])){
CWH$agile[l] <- sum(CWH$marginal[l], CWH$agile[l], na.rm = T)
}
# fuse aquatic_on_under_stones_logs and under_stones_bark_etc into under_stones_bark_etc
if(!is.na(CWH$aquatic_on_under_stones_logs[l])){
CWH$under_stones_bark_etc[l] <- sum(CWH$aquatic_on_under_stones_logs[l], CWH$under_stones_bark_etc[l], na.rm = T)
}
# fuse aquatic_in_sand_mud_detritus and detritus into detritus
if(!is.na(CWH$aquatic_in_sand_mud_detritus[l])){
CWH$detritus[l] <- sum(CWH$aquatic_in_sand_mud_detritus[l], CWH$detritus[l], na.rm = T)
}
# fuse aquatic_on_plants_incl_moss and on_stones_bark_plants_ice into on_stones_bark_plants_ice
if(!is.na(CWH$aquatic_on_plants_incl_moss[l])){
CWH$on_stones_bark_plants_ice[l] <- sum(CWH$aquatic_on_plants_incl_moss[l], CWH$on_stones_bark_plants_ice[l], na.rm = T)
}
# fuse current_fast and current_moderate into current_fast
if(!is.na(CWH$aquatic_on_plants_incl_moss[l])){
CWH$current_fast [l] <- sum(CWH$current_moderate[l], CWH$current_fast [l], na.rm = T)
}
# fuse in_leaf_litter and detritus into detritus
if(!is.na(CWH$in_leaf_litter[l])){
CWH$detritus [l] <- sum(CWH$in_leaf_litter[l], CWH$detritus [l], na.rm = T)
}
show.progress(l, nrow(CWH))
}
# add discrete lines for l and a where d is value of larva_adult
for(l in 1:nrow(CWH)){
curr.larva_adult <- CWH$larva_adult[l]
if (curr.larva_adult == "b"){
# print("yo")
CWH$larva_adult[l] <- "l"
CWH[(nrow(CWH)+1), ] <- CWH[l, ]
CWH$larva_adult[nrow(CWH)] <- "a"
}
show.progress(l, nrow(CWH))
}
# extract species level data only
CWH.species <- CWH %>%
filter(taxonrank == "species") %>%
mutate(ID = paste0(genus, "_", species))
# get taxon infos (order, family) from PBT into CWH; extract the species that have landmarks as well and discard all other rows
CWH.species <- CWH.species %>%
left_join(select(PBT.Derma.Pleco.LMs, order, family, ID), by = c("ID" = "ID")) %>%
filter(!is.na(order))
###### load WHW #####
WHW <- as_tibble(suppressWarnings({gsheet2tbl('https://docs.google.com/spreadsheets/d/1XLVK4xOpIwXsZkxIIWEX8pnkTTHS7V8pHhwTwopqPb8#gid=0')}))
# delete first four columns which contain taxonomic infos
WHW <- WHW[, -c(1:4)]
# use second row elements as colnames
colnames(WHW) <- WHW[1,]
# change first to column names to genus and species
colnames(WHW)[1:2] <- c("genus", "species")
# delete first row; only use certain columns
WHW <- WHW[-1,1:28]
# change data column types to double
WHW <- WHW %>%
mutate_at(vars(shredders, scrapers_grazers, collector_browser, collector_scraper, predator, wood_gouger, filter_feeders,
fruitivory, fungivory, phytophagy, herbivory, pollen,
predatory, scavenger, parasitoid_endop, ectoparasitic, diatoms, algae,
lichens, detritivoric, omnivore, yes_unspecified, non_feeding), funs(as.double)) %>%
mutate(ID = paste0(genus, "_", species))
# remove lines from CWH that contain data on non-landmarked species
WHW <- WHW %>%
filter(ID %in% PBT.Derma.Pleco.LMs$ID)
# delete rows with food: yes_unspecified
WHW <- WHW %>%
filter(is.na(yes_unspecified))
# fuse/change columns
for(l in 1:nrow(WHW)){
# wood_gauger and shredders into shredders
if(!is.na(WHW$wood_gouger[l])){
WHW$shredders[l] <- sum(WHW$shredders[l], WHW$wood_gouger[l], na.rm = T)
}
# collector_browser and shredders into shredders
if(!is.na(WHW$collector_browser[l])){
WHW$shredders[l] <- sum(WHW$shredders[l], WHW$collector_browser[l], na.rm = T)
}
# collector_scraper and scrapers_grazers to scrapers_grazers
if(!is.na(WHW$collector_scraper[l])){
WHW$scrapers_grazers[l] <- sum(WHW$scrapers_grazers[l], WHW$collector_scraper[l], na.rm = T)
}
# # phytophagy and herbivory to herbivory
# if(!is.na(WHW$phytophagy[l])){
# WHW$herbivory[l] <- sum(WHW$herbivory[l], WHW$phytophagy[l], na.rm = T)
# }
# # fruitivory and herbivory to herbivory
# if(!is.na(WHW$fruitivory[l])){
# WHW$herbivory[l] <- sum(WHW$herbivory[l], WHW$fruitivory[l], na.rm = T)
# }
# # pollen and herbivory to herbivory
# if(!is.na(WHW$pollen[l])){
# WHW$herbivory[l] <- sum(WHW$herbivory[l], WHW$pollen[l], na.rm = T)
# }
# pollen and herbivory to herbivory
if(!is.na(WHW$fruitivory[l]) | !is.na(WHW$fungivory[l]) | !is.na(WHW$phytophagy[l]) | !is.na(WHW$pollen[l]) |
!is.na(WHW$diatoms[l]) | !is.na(WHW$algae[l]) | !is.na(WHW$lichens[l])){
WHW$herbivory[l] <- sum(WHW$herbivory[l],
WHW$fruitivory[l], WHW$fungivory[l], WHW$phytophagy[l], WHW$pollen[l], WHW$diatoms[l], WHW$algae[l],WHW$lichens[l],
na.rm = T)
}
show.progress(l, nrow(WHW))
}
# add discrete lines for l and a where d is value of larva_adult
for(l in 1:nrow(WHW)){
curr.larva_adult <- WHW$larva_adult[l]
if (curr.larva_adult == "b"){
# print("yo")
WHW$larva_adult[l] <- "l"
WHW[(nrow(WHW)+1), ] <- WHW[l, ]
WHW$larva_adult[nrow(WHW)] <- "a"
}
show.progress(l, nrow(WHW))
}
# extract species level data only
WHW.species <- WHW %>%
filter(taxonrank == "species") %>%
mutate(ID = paste0(genus, "_", species))
# get taxon infos (order, family) from PBT into WHW; extract the species that have landmarks as well and discard all other rows
WHW.species <- WHW.species %>%
left_join(select(PBT.Derma.Pleco.LMs, order, family, ID), by = c("ID" = "ID")) %>%
filter(!is.na(order))
###### Analysis CHW #####
# calculate the percentage sums for each microhabitat item for each species in larval and in adult stage
microhabitat <- CWH.species %>%
filter(type == "m") %>%
select(ID, larva_adult, on_stones_bark_plants_ice, under_stones_bark_etc, epizoic, detritus) %>%
group_by(ID, larva_adult) %>%
summarise_all(funs(sum(., na.rm = T))) %>%
group_by(ID, larva_adult) %>%
mutate(sum.on_stones_bark_plants_ice = sum(on_stones_bark_plants_ice, na.rm = T)*100/sum(on_stones_bark_plants_ice, under_stones_bark_etc,
epizoic, detritus, na.rm = T)) %>%
mutate(sum.under_stones_bark_etc = sum(under_stones_bark_etc, na.rm = T)*100/sum(on_stones_bark_plants_ice, under_stones_bark_etc,
epizoic, detritus, na.rm = T)) %>%
mutate(sum.epizoic = sum(epizoic, na.rm = T)*100/sum(on_stones_bark_plants_ice, under_stones_bark_etc,
epizoic, detritus, na.rm = T)) %>%
mutate(sum.detritus = sum(detritus, na.rm = T)*100/sum(on_stones_bark_plants_ice, under_stones_bark_etc,
epizoic, detritus, na.rm = T)) %>%
left_join(., select(PBT.Derma.Pleco.LMs, order, suborder, infraorder, superfamily, family, subfamily, genus, species, ID), by = "ID") %>%
select(order, suborder, infraorder, superfamily, family, subfamily, genus, species, larva_adult, sum.on_stones_bark_plants_ice,
sum.under_stones_bark_etc, sum.epizoic, sum.detritus, ID) %>%
mutate_all(~replace(., is.nan(.), NA)) %>%
distinct()
# calculate the percentage sums for each current item for each species in larval and in adult stage
current <- CWH.species %>%
filter(type == "c" & larva_adult == "l") %>%
select(ID, larva_adult, current_fast, current_slow, current_none) %>%
group_by(ID, larva_adult) %>%
summarise_all(funs(sum(., na.rm = T))) %>%
group_by(ID, larva_adult) %>%
mutate(sum.current_fast = sum(current_fast, na.rm = T)*100/sum(current_fast, current_slow, current_none, na.rm = T)) %>%
mutate(sum.current_slow = sum(current_slow, na.rm = T)*100/sum(current_fast, current_slow, current_none, na.rm = T)) %>%
mutate(sum.current_none = sum(current_none, na.rm = T)*100/sum(current_fast, current_slow, current_none, na.rm = T)) %>%
left_join(., select(PBT.Derma.Pleco.LMs, order, suborder, infraorder, superfamily, family, subfamily, genus, species, ID), by = "ID") %>%
select(order, suborder, infraorder, superfamily, family, subfamily, genus, species, larva_adult, sum.current_fast, sum.current_slow, sum.current_none, ID) %>%
mutate_all(~replace(., is.nan(.), NA)) %>%
distinct()
# calculate the percentage sums for each wing item for each species in larval and in adult stage
# wing <- CWH.species %>%
# filter(type == "w" & larva_adult == "a") %>%
# select(ID, larva_adult, macropterous, brachypterous, apterous, yes) %>%
# group_by(ID, larva_adult) %>%
# summarise_all(funs(sum(., na.rm = T))) %>%
# group_by(ID, larva_adult) %>%
# mutate(sum.macropterous = sum(macropterous, na.rm = T)*100/sum(macropterous, brachypterous, apterous, na.rm = T)) %>%
# mutate(sum.brachypterous = sum(brachypterous, na.rm = T)*100/sum(macropterous, brachypterous, apterous, na.rm = T)) %>%
# mutate(sum.apterous = sum(apterous, na.rm = T)*100/sum(macropterous, brachypterous, apterous, na.rm = T)) %>%
# left_join(., select(PBT.Derma.Pleco.LMs, order, suborder, infraorder, superfamily, family, subfamily, genus, species, ID), by = "ID") %>%
# select(order, suborder, infraorder, superfamily, family, subfamily, genus, species, larva_adult, sum.macropterous, sum.brachypterous, sum.apterous, ID) %>%
# mutate_all(~replace(., is.nan(.), NA)) %>%
# distinct()
wing <- CWH.species %>%
filter(type == "w" & larva_adult == "a") %>%
select(ID, larva_adult, macropterous, apterous) %>%
group_by(ID, larva_adult) %>%
summarise_all(funs(sum(., na.rm = T))) %>%
group_by(ID, larva_adult) %>%
mutate(sum.macropterous = sum(macropterous, na.rm = T)*100/sum(macropterous, apterous, na.rm = T)) %>%
mutate(sum.apterous = sum(apterous, na.rm = T)*100/sum(macropterous, apterous, na.rm = T)) %>%
left_join(., select(PBT.Derma.Pleco.LMs, order, suborder, infraorder, superfamily, family, subfamily, genus, species, ID), by = "ID") %>%
select(order, suborder, infraorder, superfamily, family, subfamily, genus, species, larva_adult, sum.macropterous, sum.apterous, ID) %>%
mutate_all(~replace(., is.nan(.), NA)) %>%
distinct()
# calculate the percentage sums for each flight item for each species in larval and in adult stage
flight <- CWH.species %>%
filter(type == "f" & larva_adult == "a") %>%
select(ID, larva_adult, agile, flightless) %>%
group_by(ID, larva_adult) %>%
summarise_all(funs(sum(., na.rm = T))) %>%
group_by(ID, larva_adult) %>%
mutate(sum.agile = sum(agile, na.rm = T)*100/sum(agile, flightless, na.rm = T)) %>%
mutate(sum.flightless = sum(flightless, na.rm = T)*100/sum(agile, flightless, na.rm = T)) %>%
left_join(., select(PBT.Derma.Pleco.LMs, order, suborder, infraorder, superfamily, family, subfamily, genus, species, ID), by = "ID") %>%
select(order, suborder, infraorder, superfamily, family, subfamily, genus, species, larva_adult, sum.agile, sum.flightless, ID) %>%
mutate_all(~replace(., is.nan(.), NA)) %>%
distinct()
###### Analysis WHW #####
# calculate the percentage sums for each feeding type item for each species in larval and in adult stage
feeding_type <- WHW.species %>%
filter(type == "p" & larva_adult == "l") %>%
select(ID, larva_adult, shredders, scrapers_grazers, predator, filter_feeders) %>%
group_by(ID, larva_adult) %>%
summarise_all(funs(sum(., na.rm = T))) %>%
group_by(ID, larva_adult) %>%
mutate(sum.shredders = sum(shredders, na.rm = T)*100/sum(shredders, scrapers_grazers, predator,
filter_feeders, na.rm = T)) %>%
mutate(sum.scrapers_grazers = sum(scrapers_grazers, na.rm = T)*100/sum(shredders, scrapers_grazers, predator,
filter_feeders, na.rm = T)) %>%
mutate(sum.predator = sum(predator, na.rm = T)*100/sum(shredders, scrapers_grazers, predator,
filter_feeders, na.rm = T)) %>%
mutate(sum.filter_feeders = sum(filter_feeders, na.rm = T)*100/sum(shredders, scrapers_grazers, predator,
filter_feeders, na.rm = T)) %>%
left_join(., select(PBT.Derma.Pleco.LMs, order, suborder, infraorder, superfamily, family, subfamily, genus, species, ID), by = "ID") %>%
select(order, suborder, infraorder, superfamily, family, subfamily, genus, species, larva_adult, sum.shredders, sum.scrapers_grazers,
sum.predator, sum.filter_feeders, ID) %>%
mutate_all(~replace(., is.nan(.), NA)) %>%
distinct()
# calculate the percentage sums for each food item for each species in larval and in adult stage
food <- WHW.species %>%
filter(type == "g") %>%
select(ID, larva_adult, herbivory,
predatory, ectoparasitic, scavenger, detritivoric, non_feeding) %>% # omnivore, yes_unspecified, phytophagy,
group_by(ID, larva_adult) %>%
summarise_all(funs(sum(., na.rm = T))) %>%
group_by(ID, larva_adult) %>%
mutate(sum.herbivory = sum(herbivory, na.rm = T)*100/sum(herbivory, predatory,
ectoparasitic, scavenger, detritivoric,
non_feeding, na.rm = T)) %>%
mutate(sum.predatory = sum(predatory, na.rm = T)*100/sum(herbivory, predatory,
ectoparasitic, scavenger, detritivoric,
non_feeding, na.rm = T)) %>%
mutate(sum.ectoparasitic = sum(ectoparasitic, na.rm = T)*100/sum(herbivory, predatory,
ectoparasitic, scavenger, detritivoric,
non_feeding, na.rm = T)) %>%
mutate(sum.scavenger = sum(scavenger, na.rm = T)*100/sum(herbivory, predatory,
ectoparasitic, scavenger, detritivoric,
non_feeding, na.rm = T)) %>%
mutate(sum.detritivoric = sum(detritivoric, na.rm = T)*100/sum(herbivory, predatory,
ectoparasitic, scavenger, detritivoric,
non_feeding, na.rm = T)) %>%
mutate(sum.non_feeding = sum(non_feeding, na.rm = T)*100/sum(herbivory, predatory,
ectoparasitic, scavenger, detritivoric,
non_feeding, na.rm = T)) %>%
# mutate(sum.omnivore = sum(omnivore, na.rm = T)*100/sum(fungivory, phytophagy, herbivory, pollen, predatory, lignivorous,
# ectoparasitic, scavenger, diatoms, algae, lichens, detritivoric,
# non_feeding, omnivore, yes_unspecified, na.rm = T)) %>%
left_join(., select(PBT.Derma.Pleco.LMs, order, suborder, infraorder, superfamily, family, subfamily, genus, species, ID), by = "ID") %>%
select(order, suborder, infraorder, superfamily, family, subfamily, genus, species, larva_adult,
sum.herbivory, sum.predatory, sum.ectoparasitic, sum.scavenger, sum.detritivoric,
sum.non_feeding, ID) %>% # , sum.omnivore
mutate_all(~replace(., is.nan(.), NA)) %>%
distinct()
##### create separated tibbles for larvae and adults so that they can have separate columns names for the huge 2D df ####
current.l <- current %>%
filter(larva_adult == "l") %>%
ungroup()
colnames(current.l)[10:ncol(current.l)-1] <- paste0(colnames(current.l)[10:ncol(current.l)-1], ".l")
feeding_type.l <- feeding_type %>%
filter(larva_adult == "l") %>%
ungroup()
colnames(feeding_type.l)[10:ncol(feeding_type.l)-1] <- paste0(colnames(feeding_type.l)[10:ncol(feeding_type.l)-1], ".l")
food.l <- food %>%
filter(larva_adult == "l") %>%
ungroup()
colnames(food.l)[10:ncol(food.l)-1] <- paste0(colnames(food.l)[10:ncol(food.l)-1], ".l")
microhabitat.l <- microhabitat %>%
filter(larva_adult == "l") %>%
ungroup()
colnames(microhabitat.l)[10:ncol(microhabitat.l)-1] <- paste0(colnames(microhabitat.l)[10:ncol(microhabitat.l)-1], ".l")
flight.a <- flight %>%
filter(larva_adult == "a") %>%
ungroup()
# flight.a$sum.both <- 0
for(l in 1:nrow(flight.a)){
if(flight.a$sum.agile[l] != 0 & flight.a$sum.flightless[l] != 0){
# flight.a$sum.both[l] <- 100
flight.a$sum.agile[l] <- 100 # 0
flight.a$sum.flightless[l] <- 0
}
else if(flight.a$sum.agile[l] != 0 & flight.a$sum.flightless[l] == 0){
flight.a$sum.agile[l] <- 100
}
else if(flight.a$sum.agile[l] == 0 & flight.a$sum.flightless[l] != 0){
flight.a$sum.flightless[l] <- 100
}
}
# flight.a <- flight.a[, colnames(flight.a[c(1:(ncol(flight.a)-2), ncol(flight.a), ncol(flight.a)-1)])]
colnames(flight.a)[10:ncol(flight.a)-1] <- paste0(colnames(flight.a)[10:ncol(flight.a)-1], ".a")
food.a <- food %>%
filter(larva_adult == "a") %>%
ungroup()
colnames(food.a)[10:ncol(food.a)-1] <- paste0(colnames(food.a)[10:ncol(food.a)-1], ".a")
microhabitat.a <- microhabitat %>%
filter(larva_adult == "a") %>%
ungroup()
colnames(microhabitat.a)[10:ncol(microhabitat.a)-1] <- paste0(colnames(microhabitat.a)[10:ncol(microhabitat.a)-1], ".a")
wing.a <- wing %>%
filter(larva_adult == "a") %>%
ungroup()
colnames(wing.a)[10:ncol(wing.a)-1] <- paste0(colnames(wing.a)[10:ncol(wing.a)-1], ".a")
##### create one single 2D dataframe for all analyses to follow
array.2D.full <- PBT.Derma.Pleco.LMs %>%
left_join(., select(current.l, colnames(current.l[10:ncol(current.l)])), by = "ID") %>%
left_join(., select(feeding_type.l, colnames(feeding_type.l[9:ncol(feeding_type.l)])), by = "ID") %>%
left_join(., select(food.l, colnames(food.l[9:ncol(food.l)])), by = "ID") %>%
left_join(., select(food.a, colnames(food.a[9:ncol(food.a)])), by = "ID") %>%
left_join(., select(flight.a, colnames(flight.a[9:ncol(flight.a)])), by = "ID") %>%
left_join(., select(microhabitat.l, colnames(microhabitat.l[9:ncol(microhabitat.l)])), by = "ID") %>%
left_join(., select(microhabitat.a, colnames(microhabitat.a[9:ncol(microhabitat.a)])), by = "ID") %>%
left_join(., select(wing, colnames(wing[9:ncol(wing)])), by = "ID")
# replace all zeros with NA
array.2D.full[array.2D.full == 0] <- NA
### calculate pcoas for ecological data and create stacked plots PDF
# little function to plot stacked bar plots for current subsets
plot.stacked.bar <- function(df){
plot.df <- df %>%
select(ID, 10:(ncol(df)-1))
plot.df.long <- gather(data = plot.df, key = attribute, value = percentage, 2:ncol(plot.df))
print(
ggplot(data = plot.df.long, aes(fill=attribute, y = percentage, x = ID))+
geom_bar(position = "stack", stat = "identity") +
coord_flip()
)
}
# 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
ecology.cat.names <- list("current.l", "food.l", "microhabitat.l",
"food.a", "microhabitat.a") # , "flight.a", "wing.a"
# go through all ecological category tibbles, stack plot into PDF and calculate PCoA
pdf(paste0("//blanke-nas-1/DATA/PAPERS/PTR_Influence of the nymphal life history/R/", gsub("_", "-", today()), "_stacked.pdf"), height = 20)
# pdf(paste0("//blanke-nas-1/DATA/PAPERS/PTR_Influence of the nymphal life history/R/", gsub("_", "-", today()), "__PCoAs.pdf"), paper = "a4")
# par(mfrow=c(2,2), mar=c(2, 2, 1, 1))
# par(mfrow=c(1,1), mar=c(5.1, 4.1, 4.1, 2.1))
taxon.names <- c("all", "Dermaptera", "Plecoptera")
for(t in 1:length(taxon.names)){ # :length(taxon.names)
for(e in 1:length(ecology.cats)){
# define taxon
curr.taxon.name <- taxon.names[t]
message(curr.taxon.name)
curr.eco.cat <- ecology.cat.names[e]
message(curr.eco.cat)
# skip Dermaptera current
if((curr.eco.cat == "current.l" & curr.taxon.name == "Dermaptera") | (curr.eco.cat == "feeding_type.l" & curr.taxon.name == "Dermaptera")){
message(paste0("skipping ", curr.eco.cat, " for Dermaptera..."))
} else {
# get current ecology tibble
curr.df <- ecology.cats[[e]]
# get only curr.taxon data from curr.df
if(curr.taxon.name != "all"){
curr.df <- curr.df %>%
filter(order == curr.taxon.name)
}
# stack plot into PDF
plot.stacked.bar(curr.df)
if(ecology.cat.names[e] != "wing.a" & ecology.cat.names[e] != "flight.a" & ecology.cat.names[e] != "Csize.occ" & ecology.cat.names[e] != "Csize.ant"){
# convert relevant ecology tibble columns into data.frame
curr.df <- as.data.frame(curr.df[, 10:ncol(curr.df)])
# delete duplicate rows resulting from multiple scans per species
curr.df <- distinct(curr.df)
# set rownames as ID and delet ID column
rownames(curr.df) <- curr.df$ID
curr.df$ID <- NULL
# # delete rows that contain no data # should not be necessary because it is already filtered
# replace all Nas with zeros
curr.df[is.na(curr.df)] <- 0
# ind <- find.all.NA.rows(curr.df)
# curr.df <- curr.df[ !ind, ]
# calculate distance matrix and store as distance object
# dist.food.l.d <- as.dist(dist(food.l.df))
# calculate distance matrix and store as matrix object
# dist.curr.df <- as.matrix(dist(curr.df))
# plot(hclust(dist.curr.df))
# heatmap(dist.curr.df)
# levelplot(dist.curr.df, pretty = T)
# levelplot for upper triangle
# upper <- get.upper.tri(dist.curr.df)
# myPal <- colorRampPalette(brewer.pal('YlOrRd', n=9))(ncol(upper))
# myTheme <- rasterTheme(region = myPal)
# levelplot(upper, par.settings = myTheme, scales=list(x=list(rot=90)), pretty = T)