-
Notifications
You must be signed in to change notification settings - Fork 1
/
Triton_PNstructure.R
4469 lines (4192 loc) · 223 KB
/
Triton_PNstructure.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
# function for putting data into the correct structure for the PN --------
# input:
# - excel / csv file in one sheet; - details on which columns contain sp. info (and genus if separate); - details on ODP or not. If so, then leg, site, hole, core, section, interval.top; - depth; - age; - age model; - lat; - long; - paleolat; - paleolong; - abundance; - sample group abundance; - preservation; - source; - Database
# output: a dataframe with a predictable set of headings:
# Database level:
# - Database; - date stamp; - inputter
# site level:
# - Age model; - latitude; - longitude; - ODP site?; - leg; - site; - hole; - core; - Water depth; - Source
# Sample level:
# - Age; - Depth; - Section; - Interval top; - Interval bottom; - Paleolat; - Paleolong; - Sample type; - Preservation; - Processing; - Total IDd;
# Species level:
# - Species (corrected for synonyms); - Abundance; - Abundance categories; - Sample.Group.Abundance
PNstructure <- function(data, input.init, database.source, ODP = "Y", sheet.no = 1, model = "MATTHEWS2016", choices = NULL, pal.lat.full = TRUE, multiple.abun = FALSE, db.ID = database.source) {
require("svDialogs")
library(devtools)
#install_github("macroecology/mapast")
library(mapast)
library(sp)
#source("Code/Triton_ForamSynonyms.R")
# create a choices list
if (is.null(choices)) {
choices <- list()
}
# read in the file
if (is.character(data)) {
data <- read.xlsx(data, sheet = sheet.no)
}
# # append database information ----------------
# (database source, date stamp, inputter)
data <- cbind(data, db.source = database.source, date = Sys.Date(), person = input.init, dbID = db.ID, stringsAsFactors = FALSE)
# add a column of why the data was collected
if (is.null(choices$reason)) {
choices$reason <- dlg_list(c("Community analysis", "Selected species", "Proxies", "Extreme event", "Biostratigraphy", "Unspecified"), title = "What is the reason for the data collection?")$res
}
data$reason <- factor(choices$reason, levels = c("Community analysis", "Selected species", "Proxies", "Extreme event", "Biostratigraphy", "Unspecified"))
# # rename site-level columns ----------------
# Source ----------------------------------
if (is.null(choices$source)) {
check <- FALSE
while(check == FALSE) {
print(head(data))
choices$source <- dlg_list(c("Yes", "No"), title = "Does the dataset contain the source reference?")$res
if (choices$source == "No") {
choices$source <- readline("Enter the source: ")
print(paste("So source: ", choices$source, sep = ""))
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
} else { # the source reference is one / more columns
source.cols <- NULL
source.loop <- "Yes"
while(source.loop == "Yes") {
source.cols <- c(source.cols, dlg_list(names(data), title = "Enter the source column")$res)
source.loop <- dlg_list(c("Yes", "No"), title = "Do you want to enter another column?")$res
}
print(paste(c("So source: ", source.cols)))
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
choices$source <- source.cols
}
}
}
# if the source is one/more of the data columns
if (all(choices$source %in% names(data))) {
if (length(choices$source) == 1) {
names(data)[names(data) == choices$source] <- "source"
} else {
data$source <- apply(data[,choices$source], 1, paste, collapse = ",")
}
} else {
data <- cbind(data, source = choices$source, stringsAsFactors = FALSE)
}
if (is.null(choices$year)) {
# get the year from the citation
choices$year <- as.numeric(gsub("^.*(19\\d{2}|20\\d{2}).*$", "\\1", data$source))
}
data <- cbind(data, year = choices$year)
# age model -----------------------------------------
if (is.null(choices$age.model)) {
while(is.null(choices$age.model)) {
# print metadata with potentially relevant info for the age model
print(c(unique(data[,grep("zone", names(data), ignore.case = TRUE)])))
choices$age.model <- dlg_list(c("Wade2011 (P,E,O,M,Pl,Pt)", "Berggren2005 (P,E,O)", "Berggren1995 (P,M,Pl,Pt)", "Berggren1977 (Pl)", "GTSBlow1969 (P,N)", "C&P1997/P&C1997 (P,N)", "B&M1988 (P)", "K&S1983 (N)", "Blow1969 (P13-N12)", "Ericson1968 (P-Z)", "Huber2005 (AP,AE,AO)", "Huber1991 (AP)", "Raffi2006 (NN)", "GTS2012 (MoveDB uses this)", "Other", "Extra"), title = "What is the age model?")$res
if (length(choices$age.model) == 0)
choices$age.model <- "Other"
# remove the bit in brackets (i.e. after the space)
choices$age.model <- gsub( "\\ .*$", "", choices$age.model)
}
}
data <- cbind(data, age.model = choices$age.model, stringsAsFactors = FALSE)
# site info ---------------------------------------------------------------
# If not an ODP site, create blank columns
if (is.null(choices$holeID) & !is.null(choices$coreID)) {
choices$holeID <- choices$coreID
choices$coreID <- NULL
}
if (ODP == "N") {
data$leg <- data$site <- data$hole <- data$core <- data$section <- data$sample.top <- as.character(NA)
if (is.null(choices$holeID)) {
check <- FALSE
while(check == FALSE) {
choices$holeID <- dlg_list(names(data), title = "Which column is the hole ID?")$res
print(paste("So hole ID: ", database.source, "$", choices$holeID, sep = ""))
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
}
}
names(data)[names(data) == choices$holeID] <- "holeID"
} else {
# if it is an ODP site, then identify columns
if (is.null(choices$leg) | is.null(choices$site) | is.null(choices$hole) | is.null(choices$core)) {
check <- FALSE
while(check == FALSE) {
choices$leg <- dlg_list(names(data), title = "Which column is the ODP leg?")$res
if (length(choices$leg) == 0)
choices$leg <- readline("Enter the leg: ")
choices$site <- dlg_list(names(data), title = "Which column is the ODP site?")$res
if (length(choices$site) == 0)
choices$site <- readline("Enter the site: ")
choices$hole <- dlg_list(names(data), title = "Which column is the ODP hole?")$res
if (length(choices$hole) == 0)
choices$hole <- readline("Enter the hole: ")
choices$core <- dlg_list(names(data), title = "Which column is the ODP core?")$res
if (length(choices$core) == 0)
choices$core <- readline("Enter the core: ")
print(paste("So leg: ", choices$leg, sep = ""))
print(paste("site: ", choices$site, sep = ""))
print(paste("hole: ", choices$hole, sep = ""))
print(paste("core: ", choices$core, sep = ""))
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
}
}
# if the odp info is in columns
if (choices$leg %in% names(data) & choices$site %in% names(data) & choices$hole %in% names(data)) {
names(data)[names(data) == choices$leg] <- "leg"
names(data)[names(data) == choices$site] <- "site"
names(data)[names(data) == choices$hole] <- "hole"
data$hole <- gsub("^.*_", "", data$hole) # remove the site info from the hole info
data$leg <- as.character(data$leg)
data$site <- as.character(data$site)
} else {
# otherwise add it in
data <- cbind(data, leg = as.character(choices$leg), site = as.character(choices$site), hole = as.character(choices$hole), stringsAsFactors = FALSE)
}
# core info
if (choices$core %in% names(data)) {
names(data)[names(data) == choices$core] <- "core"
data$core <- as.character(data$core)
} else {
data <- cbind(data, core = as.character(choices$core), stringsAsFactors = FALSE)
}
# hole ID (if it exists apart from the leg / hole)
if (is.null(choices$holeID)) {
check <- FALSE
while(check == FALSE) {
choices$holeID <- dlg_list(names(data), title = "Is any column the hole ID (and not leg/hole etc.)?")$res
print(paste("So hole ID: ", database.source, "$", choices$holeID, sep = ""))
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
}
}
if (length(choices$holeID) > 0) {
names(data)[names(data) == choices$holeID] <- "holeID"
} else{
data$holeID <- paste(data$leg, data$hole, sep = "-")
}
}
# coordinates -------------------------------------------------------------
# latitude
if (is.null(choices$latitude) | is.null(choices$longitude)) {
check <- FALSE
while(check == FALSE) {
choices$latitude <- dlg_list(names(data), title = "Which column is the latitude?")$res
choices$longitude <- dlg_list(names(data), title = "Which column is the longitude?")$res
if ((length(choices$latitude)|length(choices$longitude)) == 0) {
choices$latitude <- as.numeric(readline("Enter the latitude: "))
choices$longitude <- as.numeric(readline("And the longitude: "))
print(paste("So latitude: ", choices$latitude, sep = ""))
print(paste("longitude: ", choices$longitude, sep = ""))
} else {
print(paste("So latitude: ", database.source, "$", choices$latitude, sep = ""))
print(paste("longitude: ", database.source, "$", choices$longitude, sep = ""))
}
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
}
}
if (choices$latitude %in% names(data)) {
names(data)[names(data) == choices$latitude] <- "latitude"
names(data)[names(data) == choices$longitude] <- "longitude"
} else {
data <- cbind(data, latitude = choices$latitude, longitude = choices$longitude)
}
# Water depth
if(is.null(choices$water.depth)) {
check <- FALSE
while(check == FALSE) {
choices$water.depth <- dlg_list(names(data), title = "Does any column contain the water depth?")$res
if (length(choices$water.depth) == 0) {
choices$water.depth <- readline("Enter the water depth: ")
if (choices$water.depth == "")
choices$water.depth <- NA
print(paste("So water depth: ", choices$water.depth, " m", sep = ""))
} else {
print(paste("So water depth: ", database.source, "$", choices$water.depth, " ", sep = ""))
}
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
}
}
if (choices$water.depth %in% names(data)) {
names(data)[names(data) == choices$water.depth] <- "water.depth"
data$water.depth <- as.numeric(data$water.depth)
} else {
data <- cbind(data, water.depth = as.numeric(choices$water.depth), stringsAsFactors = FALSE)
}
# Fill Sample-level information ------------------
# add row ID
if (!is.null(data$row.num)) {
data$sampleID <- paste(data$holeID, gsub("R", "", data$row.num), sep = "_")
} else {
if (is.null(choices$sampleID)) {
check <- FALSE
while(check == FALSE) {
choices$sampleID <- dlg_list(names(data), title = "Is any column the sample ID (and not leg/hole etc.)?")$res
print(paste("So sample ID: ", database.source, "$", choices$sampleID, sep = ""))
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
}
data$sampleID <- data[, names(data) == choices$sampleID]
} else if (length(choices$sampleID) > 0) {
data$sampleID <- paste(data$holeID, data[, names(data) == choices$sampleID], sep = "_")
} else if (sum(!is.na(data$sample.depth)) > 0) {
data$sampleID <- paste(data$holeID, data$sample.depth, sep = "_")
if (any(is.na(data$sample.depth))) { # use age rather than depth where necessary
data$sampleID[is.na(data$sample.depth)] <- paste(data$holeID[is.na(data$sample.depth)], data$age[is.na(data$sample.depth)], sep = ".")
}
} else {
data$sampleID <- data$holeID
}
}
tmp.rowID <- paste(database.source, data$sampleID, sep = "_")
data$rowID[order(tmp.rowID)] <- paste(sort(tmp.rowID), "_R", unlist(tapply(tmp.rowID, tmp.rowID, function(x) 1:length(x))), sep = "")
# Age / depth -------------------------------
if (is.null(choices$sample.age) | is.null(choices$sample.depth)) {
check <- FALSE
while(check == FALSE) {
choices$sample.age <- dlg_list(names(data), title = "Which column is the numeric age?")$res
if (length(choices$sample.age) == 0) {
choices$sample.age <- readline("Enter the age: ")
choices$sample.age <- ifelse(choices$sample.age == "", NA, as.numeric(choices$sample.age))
# convert the age to a number
if(choices$age.model == 1) {
choices$sample.age <- as.numeric(as.character(choices$sample.age))
}
}
choices$sample.depth <- dlg_list(names(data), title = "Which column is the sample depth?")$res
if (length(choices$sample.depth) == 0)
choices$sample.depth <- NA
print(paste("So age: ", choices$sample.age))
print(paste("sample depth: ", database.source, "$", choices$sample.depth, sep = ""))
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
}
}
# what is the zone age
if (is.null(choices$sample.zone)) {
check <- FALSE
while(check == FALSE) {
choices$sample.zone <- dlg_list(names(data), title = "Which column is the zone age?")$res
if (length(choices$sample.zone) == 0) {
choices$sample.zone <- NA
}
print(paste("So zone: ", choices$sample.zone, sep = ""))
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
}
}
# add age / depth / zone columns
if (choices$sample.age[1] %in% names(data)) {
names(data)[names(data) == choices$sample.age] <- "age"
} else {
data <- cbind(data, age = as.numeric(as.character(choices$sample.age)))
}
if (choices$sample.depth %in% names(data)) {
names(data)[names(data) == choices$sample.depth] <- "sample.depth"
data$sample.depth <- as.numeric(data$sample.depth)
} else {
data <- cbind(data, sample.depth = as.numeric(choices$sample.depth))
}
if (choices$sample.zone %in% names(data)) {
names(data)[names(data) == choices$sample.zone] <- "zone"
} else {
data <- cbind(data, zone = choices$sample.zone, stringsAsFactors = FALSE)
}
# for which rows should the zones be used to estimate age
if (is.null(choices$no.num.age))
choices$no.num.age <- is.na(data$age)
# set up columns for zone based age data
data$rng.age <- NA
data$AM.type <- as.character(NA)
data$age.st <- as.numeric(NA)
data$age.en <- as.numeric(NA)
data$segment <- "s"
data$age.calc <- factor("Orig", levels = c("Interp", "Magneto", "Model", "Orig", "Zone"))
data$zon.age <- as.numeric(NA)
data$age.err <- as.numeric(NA)
data$int.age <- as.numeric(NA)
data$err.int.age <- as.numeric(NA)
data$mod.age <- as.numeric(NA)
data$r2 <- as.numeric(NA)
data$n.pts <- as.numeric(NA)
# zone type
if (is.null(choices$zone.type) & !is.na(choices$sample.zone)) {
print(unique(data$zone))
choices$zone.type <- dlg_list(c("Foram", "Nanno", "Magneto", "Other", "Mixed"), title = "What is the age model type?")$res
}
if (any(choices$zone.type == "Chrono"))
choices$zone.type[choices$zone.type == "Chrono"] <- "Magneto"
# for samples where we don't have numeric ages but do have zones, then calculate the ages
if (any(!is.na(data$zone)) & (any(is.na(choices$sample.age)) | !is.null(choices$zone.ck) | any(is.na(data[, names(data) == "age"])))) {
dat.zone <- data.frame(zone = data$zone, ocean = factor("IndoPac", levels = c("Atl", "IndoPac")), region = factor("Temp", levels = c("Temp", "Trop")), segment = data$segment, stringsAsFactors = FALSE)
# get ocean polygons and work out origin of sites to get ocean specific ages
load("Data/OceanPolygons.RData")
dat.zone$ocean[point.in.polygon(data$longitude, data$latitude, atlantic.1$x, atlantic.1$y) == 1] <- "Atl"
# work out site region
if (any(data$latitude < 23.5))
dat.zone$region[data$latitude < 23.5] <- "Trop"
# identify which zones are not known so they can be added / re-run
check <- "Yes"
while(check == "Yes") {
# get a unique list of zones
tmp.age <- zones.fun(unique(dat.zone), scheme = choices$age.model, type = choices$zone.type)
print(tmp.age)
print("Unknown zones:")
print(tmp.age$zone[is.na(tmp.age$zon.age)])
if (is.null(choices$zone.ck))
check <- dlg_list(c("Yes", "No"), title = "Do you want to make changes and re-run this?")$res
else
check <- "No"
}
choices$zone.ck <- "No"
dat.zone$ID <- 1:nrow(dat.zone)
dat.zone <- merge(dat.zone, tmp.age)
dat.zone <- dat.zone[order(dat.zone$ID), ]
dat.zone$sch[dat.zone$sch == "NA"] <- NA
dat.zone$type[dat.zone$type == "NA"] <- NA
# # if (any(dat.zone$ocean == "IndoPac")) {
# # tmp.zn <- tmp.age[tmp.age$ocean == "IndoPac",]
# # dat.zone$age[dat.zone$ocean == "IndoPac"] <- tmp.zn$zon.age[match(dat.zone$zone[dat.zone$ocean == "IndoPac"], tmp.zn$zone)]
# # dat.zone$age.rng[dat.zone$ocean == "IndoPac"] <- tmp.zn$age.rng[match(dat.zone$zone[dat.zone$ocean == "IndoPac"], tmp.zn$zone[tmp.zn$ocean == "IndoPac"])]
# # dat.zone$AM.type[dat.zone$ocean == "IndoPac"] <- tmp.zn$type[match(dat.zone$zone[dat.zone$ocean == "IndoPac"], tmp.zn$zone[tmp.zn$ocean == "IndoPac"])]
# # dat.zone$age.model[dat.zone$ocean == "IndoPac"] <- tmp.zn$sch[match(dat.zone$zone[dat.zone$ocean == "IndoPac"], tmp.zn$zone[tmp.zn$ocean == "IndoPac"])]
# # }
# # if (any(dat.zone$ocean == "Atl")) {
# # tmp.zn <- tmp.age[tmp.age$ocean == "Atl",]
# # dat.zone$age[dat.zone$ocean == "Atl"] <- tmp.zn$zon.age[match(dat.zone$zone[dat.zone$ocean == "Atl"], tmp.zn$zone[tmp.zn$ocean == "Atl"])]
# # dat.zone$age.rng[dat.zone$ocean == "Atl"] <- tmp.zn$age.rng[match(dat.zone$zone[dat.zone$ocean == "Atl"], tmp.zn$zone[tmp.zn$ocean == "Atl"])]
# # dat.zone$AM.type[dat.zone$ocean == "Atl"] <- tmp.zn$type[match(dat.zone$zone[dat.zone$ocean == "Atl"], tmp.zn$zone[tmp.zn$ocean == "Atl"])]
# # }
data$zon.age <- dat.zone$zon.age
data$age.st <- dat.zone$st
data$age.en <- dat.zone$en
data$rng.age <- dat.zone$age.rng
if (!is.null(choices$no.num.age)) {
choices$est.zone.type <- choices$age.st <- choices$age.en <- choices$range.age <- choices$est.age.model <- rep(NA, nrow(data))
choices$est.age <- data$age
data$AM.type[choices$no.num.age] <- choices$est.zone.type[choices$no.num.age] <- dat.zone$type[choices$no.num.age]
#data$age.st[choices$no.num.age] <- choices$age.st[choices$no.num.age] <- dat.zone$st[choices$no.num.age]
#data$age.en[choices$no.num.age] <- choices$age.en[choices$no.num.age] <- dat.zone$en[choices$no.num.age]
#data$rng.age[choices$no.num.age] <- choices$range.age[choices$no.num.age] <- dat.zone$age.rng[choices$no.num.age]
data$age.model[choices$no.num.age] <- choices$est.age.model[choices$no.num.age] <- dat.zone$sch[choices$no.num.age]
#data$age[choices$no.num.age] <- choices$est.age[choices$no.num.age] <- dat.zone$zon.age[choices$no.num.age]
#data$age.calc[choices$no.num.age] <- "Zone"
} else {
data$age <- choices$sample.age <- dat.zone$age
data$rng.age <- choices$range.age <- dat.zone$age.rng
data$AM.type <- choices$type <- dat.zone$AM.type
data$age.model <- choices$age.model <- dat.zone$sch
data$age.st <- choices$age.st <- dat.zone$st
data$age.en <- choices$age.en <- dat.zone$en
}
choices$zones.tab <- tmp.age
# calculate interpolated / modelled ages
for (i in unique(data$holeID)) {
if (any(!is.na(data$zon.age[data$holeID == i]) & !is.na(data$sample.depth[data$holeID == i]))) {
# interpolated ages
tmp.zon <- data[!duplicated(data$sampleID) & data$holeID == i, grep("core|hole|sample|age|zone", names(data))]
tmp.int <- age.interp(tmp.zon)
tmp.int <- tmp.int[tmp.int$interp == "TRUE",]
if (nrow(tmp.int) != 0) {
data$int.age[data$holeID == i] <- tmp.int$int.age[match(data$sampleID[data$holeID == i], tmp.int$sampleID)]
data$err.int.age[data$holeID == i] <- tmp.int$err.int.age[match(data$sampleID[data$holeID == i], tmp.int$sampleID)]
}
# create a age-depth chart
tmp.dat.age <- data.frame(Chron = c(paste("T", unique(tmp.zon$zone)), paste("B", unique(tmp.zon$zone))), Type = c(rep("T", length(unique(tmp.zon$zone))), rep("B", length(unique(tmp.zon$zone)))), Zone = rep(unique(tmp.zon$zone), 2), stringsAsFactors = FALSE)
tmp.dat.age$Age[tmp.dat.age$Type == "B"] <- tmp.zon$age.st[match(tmp.dat.age$Zone[tmp.dat.age$Type == "B"], tmp.zon$zone)]
tmp.dat.age$Age[tmp.dat.age$Type == "T"] <- tmp.zon$age.en[match(tmp.dat.age$Zone[tmp.dat.age$Type == "T"], tmp.zon$zone)]
# remove zones with identical ages
tmp.dat.age <- tmp.dat.age[!duplicated(paste(tmp.dat.age$Age, tmp.dat.age$Type)), ]
# calculate depths
tmp.max <- tapply(tmp.zon$sample.depth, tmp.zon$age.st, max)
tmp.dat.age$Depth[tmp.dat.age$Type == "B"] <- tmp.max[match(tmp.dat.age$Age[tmp.dat.age$Type == "B"], names(tmp.max))]
tmp.min <- tapply(tmp.zon$sample.depth, tmp.zon$age.en, min)
tmp.dat.age$Depth[tmp.dat.age$Type == "T"] <- tmp.min[match(tmp.dat.age$Age[tmp.dat.age$Type == "T"], names(tmp.min))]
tmp.dat.age <- tmp.dat.age[order(tmp.dat.age$Depth), ]
# set first / last depth to NA, as not clear they are the start / end of zones
tmp.dat.age$Depth[c(1, nrow(tmp.dat.age))] <- NA
tmp.dat.age <- tmp.dat.age[!is.na(tmp.dat.age$Depth) & !is.na(tmp.dat.age$Age), ]
if (nrow(tmp.dat.age) > 1) {
# add in gaps
tmp.gap <- tmp.dat.age$Chron[1:(nrow(tmp.dat.age) - 1)][diff(tmp.dat.age$Depth) < 10 & diff(tmp.dat.age$Age) > 2 & tmp.dat.age$Type[1:(nrow(tmp.dat.age) - 1)] != "T"]
if (length(tmp.gap) > 0) {
gap <- c("Gap", NA, NA, NA, NA)
for (j in tmp.gap) {
gap.id <- which(tmp.dat.age$Chron == j)
tmp.dat.age <- rbind(tmp.dat.age[1:gap.id,], gap, gap, tmp.dat.age[(gap.id+1):nrow(tmp.dat.age), ])
}
tmp.dat.age$Depth[is.na(tmp.dat.age$Depth)] <- as.numeric(tmp.dat.age$Depth[which(is.na(tmp.dat.age$Depth)) - 1]) + 0.1
tmp.dat.age$Depth[is.na(tmp.dat.age$Depth)] <- as.numeric(tmp.dat.age$Depth[which(is.na(tmp.dat.age$Depth)) + 1]) - 0.1
tmp.dat.age$Depth <- as.numeric(tmp.dat.age$Depth)
tmp.dat.age$Age <- as.numeric(tmp.dat.age$Age)
}
choices[[paste("data.age", i, sep = "_")]] <- tmp.dat.age
# calculate the modelled ages
tmp.age.res <- age.model(tmp.dat.age, choices$zones.tab, data[!duplicated(data$sampleID) & data$holeID == i,], hole = i, plots = FALSE, pan = TRUE)
data$mod.age[data$holeID == i] <- tmp.age.res$mod.age[match(data$sampleID[data$holeID == i], tmp.age.res$sampleID)]
data$r2[data$holeID == i] <- tmp.age.res$r2[match(data$sampleID[data$holeID == i], tmp.age.res$sampleID)]
data$n.pts[data$holeID == i] <- tmp.age.res$n.pts[match(data$sampleID[data$holeID == i], tmp.age.res$sampleID)]
}
# add in the estimated age
data$age[choices$no.num.age & data$holeID == i] <- NA
if (length(unique(data$age[data$holeID == i])) < length(unique(data$sample.depth[data$holeID == i])) & !all(is.na(data$age[data$holeID == i]))) {
tmp.orig <- data$age[data$holeID == i]
data$age[data$holeID == i] <- NA
tmp.no.num.age <- choices$no.num.age[data$holeID == i]
choices$no.num.age <- rep(TRUE, nrow(data[data$holeID == i,]))
}
uni.dat <- data[!duplicated(data$sampleID) & data$holeID == i, c("sampleID", "sample.depth", grep("age", names(data), value = TRUE), "zone")]
uni.dat <- rbind(uni.dat, uni.dat[nrow(uni.dat),])
if (grepl("MoveDB", db.ID))
age.mod <- movedb.model.type[movedb.model.type$Hole == i, ]
else
age.mod <- data.frame(Hole = i, Original = 0, Model = 0, Interp = 0, Zone = 1)
if (nrow(age.mod) == 1) {
if (age.mod$Model == 1) {
tmp.row <- choices$no.num.age & is.na(data$age) & !is.na(data$mod.age) & data$holeID == i & data$mod.age >= 0
uni.dat$age[is.na(uni.dat$age)] <- uni.dat$mod.age[is.na(uni.dat$age)]
if (any(diff(uni.dat$sample.depth) > 0 & diff(uni.dat$age) < -0.01, na.rm = TRUE)) {
tmp.ex <- which(diff(uni.dat$sample.depth) > 0 & diff(uni.dat$age) < 0) + 1
tmp.ex <- tmp.ex[!is.na(tmp.ex)]
tmp.ex <- data$zone %in% uni.dat$zone[tmp.ex]
tmp.row <- tmp.row & !tmp.ex
}
data$age[tmp.row] <- data$mod.age[tmp.row]
data$age.err[tmp.row] <- 0
data$age.calc[tmp.row] <- "Model"
uni.dat$age <- data$age[match(uni.dat$sampleID, data$sampleID)]
}
if (age.mod$Interp == 1|age.mod$Interp == 2) {
tmp.row <- choices$no.num.age & is.na(data$age) & !is.na(data$int.age) & data$holeID == i
uni.dat$age[is.na(uni.dat$age)] <- uni.dat$int.age[is.na(uni.dat$age)]
if (any(diff(uni.dat$sample.depth) > 0 & diff(uni.dat$age) < -0.01, na.rm = TRUE)) {
tmp.ex <- which(diff(uni.dat$sample.depth) > 0 & diff(uni.dat$age) < 0) + 1
tmp.ex <- tmp.ex[!is.na(tmp.ex)]
#tmp.ex <- data$int.age < max(uni.dat$int.age[tmp.ex], na.rm = TRUE) & data$sample.depth > min(uni.dat$sample.depth[tmp.ex], na.rm = TRUE)
tmp.ex <- data$zone %in% uni.dat$zone[tmp.ex]
tmp.row <- tmp.row & !tmp.ex
}
data$age[tmp.row] <- data$int.age[tmp.row]
data$age.err[tmp.row] <- data$err.int.age[tmp.row]
data$age.calc[tmp.row] <- "Interp"
}
if (age.mod$Model == 2) {
tmp.row <- choices$no.num.age & is.na(data$age) & !is.na(data$mod.age) & data$holeID == i & data$mod.age >= 0
uni.dat$age[is.na(uni.dat$age)] <- uni.dat$mod.age[is.na(uni.dat$age)]
if (any(diff(uni.dat$sample.depth) > 0 & diff(uni.dat$age) < -0.01, na.rm = TRUE)) {
tmp.ex <- which(diff(uni.dat$sample.depth) > 0 & diff(uni.dat$age) < 0) + 1
tmp.ex <- tmp.ex[!is.na(tmp.ex)]
tmp.ex <- data$zone %in% uni.dat$zone[tmp.ex]
tmp.row <- tmp.row & !tmp.ex
}
data$age[tmp.row] <- data$mod.age[tmp.row]
data$age.err[tmp.row] <- 0
data$age.calc[tmp.row] <- "Model"
uni.dat$age <- data$age[match(uni.dat$sampleID, data$sampleID)]
}
}
rm(tmp.dat.age)
}
tmp.row <- choices$no.num.age & is.na(data$age) & !is.na(data$zon.age) & data$holeID == i
data$age[tmp.row] <- data$zon.age[tmp.row]
data$age.err[tmp.row] <- data$rng.age[tmp.row]
data$age.calc[tmp.row] <- "Zone"
if (any(grepl("tmp.orig", ls()))) {
data$age[data$holeID == i][is.na(data$age)] <- tmp.orig[data$holeID == i][is.na(data$age)]
choices$no.num.age[data$holeID == i] <- tmp.no.num.age
}
data$age.calc[is.na(data$age) & data$holeID == i] <- NA
data$age.calc <- factor(data$age.calc, levels = c("Interp", "Magneto", "Model", "Orig", "Zone"))
}
}
# sample level info -------------------------------------------------------
# - Section
# - Interval top
# - Interval bottom
if (ODP == "N") {
data$section <- data$sample.top <- as.character(NA)
} else {
if (is.null(choices$section) | is.null(choices$sample.top)) {
check <- FALSE
while(check == FALSE) {
choices$section <- dlg_list(names(data), title = "Which column is the ODP section?")$res
if (length(choices$section) == 0)
choices$section <- readline("Enter the section: ")
choices$sample.top <- dlg_list(names(data), title = "Which column is the ODP sample.top?")$res
if (length(choices$sample.top) == 0)
choices$sample.top <- readline("Enter the sample top: ")
print(paste("So section: ", choices$section, sep = ""))
print(paste("sample.top: ", choices$sample.top, sep = ""))
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
}
}
if (choices$sample.top %in% names(data) | choices$section %in% names(data)) {
names(data)[names(data) == choices$section] <- "section"
names(data)[names(data) == choices$sample.top] <- "sample.top"
data$section <- as.character(data$section)
data$sample.top <- as.character(data$sample.top)
} else {
data <- cbind(data, section = as.character(choices$section), sample.top = as.character(choices$sample.top), stringsAsFactors = FALSE)
}
}
# - Sample type
if (is.null(choices$sample.type)) {
check <- FALSE
while(check == FALSE) {
choices$sample.type <- dlg_list(names(data), title = "Does any column contain the sample type?")$res
if (length(choices$sample.type) == 0) {
choices$sample.type <- readline("Enter the sample type: ")
print(paste("So sample type: ", choices$sample.type, sep = ""))
} else {
print(paste("So sample type: ", database.source, "$", choices$sample.type, " ", sep = ""))
}
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
}
}
if (choices$sample.type %in% names(data)) {
names(data)[names(data) == choices$sample.type] <- "sample.type"
} else {
data <- cbind(data, sample.type = choices$sample.type, stringsAsFactors = FALSE)
}
# - Preservation
if(is.null(choices$preservation)) {
check <- FALSE
while(check == FALSE) {
choices$preservation <- dlg_list(names(data), title = "Does any column contain the preservation?")$res
if (length(choices$preservation) == 0) {
choices$preservation <- readline("Enter the preservation: ")
print(paste("So preservation: ", choices$preservation, sep = ""))
} else {
print(paste("So preservation: ", database.source, "$", choices$preservation, " ", sep = ""))
}
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
}
}
if (choices$preservation %in% names(data)) {
names(data)[names(data) == choices$preservation] <- "preservation"
data$preservation <- as.character(data$preservation)
} else {
data <- cbind(data, preservation = choices$preservation, stringsAsFactors = FALSE)
}
# - Processing
if (is.null(choices$processing)) {
check <- FALSE
while(check == FALSE) {
choices$processing <- dlg_list(names(data), title = "Does any column contain the sample processing information (e.g. sieve fraction)?")$res
if (length(choices$processing) == 0) {
choices$processing <- readline("Enter the sample processing information: ")
if (choices$processing == "")
choices$processing <- NA
print(paste("So sample processing information: ", choices$processing, sep = ""))
} else {
print(paste("So sample processing information: ", database.source, "$", choices$processing, " ", sep = ""))
}
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
}
}
if (choices$processing %in% names(data)) {
names(data)[names(data) == choices$processing] <- "processing"
} else {
data <- cbind(data, processing = as.character(choices$processing), stringsAsFactors = FALSE)
}
# - Total IDd
if(is.null(choices$total.IDd)) {
check <- FALSE
while(check == FALSE) {
choices$total.IDd <- dlg_list(names(data), title = "Does any column contain the total specimens IDd?")$res
if (length(choices$total.IDd) == 0) {
choices$total.IDd <- readline("Enter the total specimens IDd: ")
if (choices$total.IDd == "")
choices$total.IDd <- NA
print(paste("So total specimens IDd: ", choices$total.IDd, sep = ""))
} else {
print(paste("So total specimens IDd: ", database.source, "$", choices$total.IDd, " ", sep = ""))
}
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
}
}
if (choices$total.IDd %in% names(data)) {
names(data)[names(data) == choices$total.IDd] <- "total.IDd"
data$total.IDd <- as.character(data$total.IDd)
} else {
data <- cbind(data, total.IDd = as.character(choices$total.IDd), stringsAsFactors = FALSE)
}
# paleolatitude ------------------------------
if (is.null(choices$pal.lat.full))
choices$pal.lat.full <- pal.lat.full
if (any(!is.na(data$age))|!is.null(choices$pal.lat)) {
if (max(as.numeric(as.character(data$age)), na.rm = TRUE) == 0) {
# if all the data is extant, then no need for palaeolatitude
data$pal.lat <- data$latitude
data$pal.long <- data$longitude
} else {
if (is.null(choices$pal.lat) | is.null(choices$pal.long)) {
# if there is no palaeolatitude information
print(head(data))
pl.check <- dlg_list(c("Yes", "No"), title = "Is there palaeolatitude information?")$res
if (length(pl.check) == 0)
pl.check <- "No"
if (pl.check == "Yes") {
check <- FALSE
while(check == FALSE) {
choices$pal.lat <- dlg_list(names(data), title = "Which column is the palaeolatitude?")$res
choices$pal.long <- dlg_list(names(data), title = "Which column is the palaeolongitude?")$res
print(paste("So palaeolatitude: data$", choices$pal.lat, sep = ""))
print(paste("palaeolongitude: data$", choices$pal.long, sep = ""))
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
}
names(data)[names(data) == choices$pal.lat] <- "pal.lat"
names(data)[names(data) == choices$pal.long] <- "pal.long"
} else {
# # if necessary extract palaeo lat information
## this is from palaeocoords, which is a function in mapast
if (!pal.lat.full) { # i.e. if only testing
# extract 5 random ages to test it on
tmp.uni.ages <- unique(data$age[!is.na(data$age)])[1:5]
tmp.pal.rows <- which(data$age %in% tmp.uni.ages[!is.na(tmp.uni.ages)])
tmp <- pal.coord(data[tmp.pal.rows,], model = model)
choices$pal.lat <- choices$pal.long <- data$pal.lat <- data$pal.long <- as.numeric(rep(NA, nrow(data)))
choices$pal.lat[tmp.pal.rows] <- data$pal.lat[tmp.pal.rows] <- tmp$paleolat
choices$pal.long[tmp.pal.rows] <- data$pal.long[tmp.pal.rows] <- tmp$paleolng
} else {
tmp.pal.rows <- which(!is.na(data$age))
tmp <- pal.coord(data[tmp.pal.rows,], model = model)
choices$pal.lat <- choices$pal.long <- data$pal.lat <- data$pal.long <- as.numeric(rep(NA, nrow(data)))
choices$pal.lat[tmp.pal.rows] <- data$pal.lat[tmp.pal.rows] <- tmp$paleolat
choices$pal.long[tmp.pal.rows] <- data$pal.long[tmp.pal.rows] <- tmp$paleolng
}
}
} else if (!(choices$pal.lat.full) & pal.lat.full) {
# if the choices$pal.lat are only a subset and you want a full run
tmp.pal.rows <- which(!is.na(data$age))
tmp <- pal.coord(data[tmp.pal.rows,], model = model)
choices$pal.lat <- choices$pal.long <- data$pal.lat <- data$pal.long <- as.numeric(rep(NA, nrow(data)))
choices$pal.lat[tmp.pal.rows] <- data$pal.lat[tmp.pal.rows] <- tmp$paleolat
choices$pal.long[tmp.pal.rows] <- data$pal.long[tmp.pal.rows] <- tmp$paleolng
} else {
if (choices$pal.lat[1] %in% names(data)) {
names(data)[names(data) == choices$pal.lat] <- "pal.lat"
names(data)[names(data) == choices$pal.long] <- "pal.long"
} else {
data$pal.lat <- NA
data$pal.long <- NA
data$pal.lat <- as.numeric(choices$pal.lat)
data$pal.long <- as.numeric(choices$pal.long)
}
}
}
}
choices$pal.lat.full <- pal.lat.full
# species names -----------------------------------------------------------
# # Extract species level information
sp.check <- "unknown"
if(is.null(choices$species) & is.null(choices$genus)) {
print(head(data))
sp.check <- dlg_list(c("Yes", "No"), title = "Is the species data currently in the form of a binomial?")$res
if(sp.check == "Yes") {
check <- FALSE
while(check == FALSE) {
choices$species <- dlg_list(names(data), title = "Which column is the species binomial?")$res
print(paste("So species: ", database.source, "$", choices$species, sep = ""))
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
}
names(data)[names(data) == choices$species] <- "orig.species"
} else {
check <- FALSE
while(check == FALSE) {
choices$genus <- dlg_list(names(data), title = "Which column is the genus?")$res
choices$sp <- dlg_list(names(data), title = "Which column is the species?")$res
choices$q.sp <- dlg_list(names(data), title = "Which column is the species qualifier?")$res
choices$subsp <- dlg_list(names(data), title = "Which column is the subspecies?")$res
print(paste("So genus is: ", database.source, "$", choices$genus, sep = ""))
print(paste("species: ", database.source, "$", choices$sp, sep = ""))
print(paste("species qualifier: ", database.source, "$", choices$q.sp, sep = ""))
print(paste("subspecies: ", database.source, "$", choices$subsp, sep = ""))
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
}
}
}
if (!is.null(choices$species) | sp.check == "Yes") {
names(data)[names(data) == choices$species] <- "orig.species"
}
if (!is.null(choices$genus) | sp.check == "No") {
species <- paste(data[, names(data) == choices$genus], data[, names(data) == choices$sp])
if (length(choices$subsp) != 0) {
sub.sp.col <- data[, names(data) == choices$subsp]
species[!is.na(sub.sp.col)] <- paste(species[!is.na(sub.sp.col)], sub.sp.col[!is.na(sub.sp.col)])
}
if (length(choices$q.sp) != 0) {
sp.q.col <- data[, names(data) == choices$q.sp]
species[!is.na(sp.q.col)] <- paste(species[!is.na(sp.q.col)], sp.q.col[!is.na(sp.q.col)])
}
data$orig.species <- species
}
# # Query database to correct for synonyms
check <- "Yes"
new.species <- NULL
while(check == "Yes") {
if (is.null(new.species)) {
comp.list <- unique(data$orig.species)
comp.rerun <- 1:length(comp.list)
} else {
comp.rerun <- which(new.species %in% c("Cretaceous", "Unsure", "unknown"))
comp.list <- unique(data$orig.species)[comp.rerun]
}
new.species[comp.rerun] <- compare(comp.list, micro = TRUE)
print("The following species were classified as 'unknown'")
print(sort(unique(data$orig.species)[new.species == "unknown"]))
print("The following species were classified as 'Cretaceous'")
print(sort(unique(data$orig.species)[new.species == "Cretaceous"]))
print("The following species were classified as 'Unsure'")
print(sort(unique(data$orig.species)[new.species == "Unsure"]))
print("The following species were classified as 'Vague'")
print(sort(unique(data$orig.species)[new.species == "Vague"]))
if (!is.null(choices$synonymy)) {
check <- "No"
} else {
check <- dlg_list(c("Yes", "No"), title = "Do you want to make some corrections and re-run this")$res
}
}
data$species <- new.species[match(data$orig.species, unique(data$orig.species))]
choices$synonymy <- "checked"
# abundance information
if (is.null(choices$orig.abundance)) {
check <- FALSE
while(check == FALSE) {
choices$orig.abundance <- dlg_list(names(data), title = "Does any column contain the abundance?")$res
if (length(choices$orig.abundance) == 0) {
choices$orig.abundance <- "P"
} else {
print(paste("So abundance: ", database.source, "$", choices$orig.abundance, " ", sep = ""))
}
check <- dlg_list(c(TRUE, FALSE), title = "Is this correct?")$res
}
}
if (choices$orig.abundance %in% names(data)) {
names(data)[names(data) == choices$orig.abundance] <- "orig.abundance"
} else {
data <- cbind(data, orig.abundance = choices$orig.abundance, stringsAsFactors = FALSE)
}
if (is.null(choices$abun.calc)) {
choices$abun.calc <- dlg_list(c(TRUE, FALSE), title = "Should the numeric abundance be calculated?")$res
}
# abundance ---------------------------------------------------------------
if (choices$abun.calc == TRUE) {
if (is.null(choices$full.abun)) {
if (multiple.abun == FALSE) {
# What are the abundance units
print("Numeric characters:")
print(summary(as.numeric(gsub("#|\\?|>|<", "", grep("\\d", data$orig.abundance, value = TRUE)))))
print("Non numeric characters:")
print(grep("[^(0-9)|\\.]", unique(data$orig.abundance), value = TRUE))
if (is.null(choices$abun.units)) {
# what are the abundance units
choices$abun.units <- dlg_list(c("Relative abundance", "Count", "Number per gram", "Binned", "P/A", "Mixed") , title = "What are the units of abundance?")$res
}
data$abun.units <- factor(choices$abun.units, levels = c("Relative abundance", "Count", "Number per gram", "Binned", "P/A", "Mixed"))
# convert everything to numbers
data$abundance <- data$orig.abundance
# where there are mixed units, identify numeric / non-numeric rows
if (any(choices$abun.units == "Mixed")) {
mix.tab <- tapply(data$abundance, data$holeID, max)
mix.tab[!is.na(mix.tab)] <- "Num"
mix.tab[is.na(mix.tab)] <- "NotNum"
mix.row <- rep(NA, nrow(data))
mix.row[data$holeID %in% names(mix.tab)[mix.tab == "Num"]] <- "Num"
mix.row[data$holeID %in% names(mix.tab)[mix.tab == "NotNum"]] <- "NotNum"
mix.row[mix.row == "Num"] <- dlg_list(c("Relative abundance", "Count", "Number per gram") , title = "What are the units of abundance for the numeric data?")$res
mix.row[mix.row == "NotNum"] <- dlg_list(c("Binned", "P/A") , title = "What are the units of abundance for the non-numeric data?")$res
data$abun.units <- choices$abun.units <- mix.row
}
if (any(choices$abun.units %in% c("Relative abundance", "Count", "Number per gram"))) {
tmp.row <- which(data$abun.units %in% c("Relative abundance", "Count", "Number per gram"))
tmp.abun <- data$abundance[tmp.row]
if (any(grepl("[^(0-9)|\\.]", unique(tmp.abun))) & any(is.na(as.numeric(grepl("[^(0-9)|\\.]", unique(tmp.abun)))))) {
if(is.null(choices$abun.num)) {
print(grep("[^(0-9)|\\.]", unique(tmp.abun), value = TRUE))
choices$abun.num <- as.numeric(readline("What should this be replaced with? "))
}
tmp.abun[grep("[^(0-9)|\\.]", tmp.abun)] <- choices$abun.num
}
data$abundance[tmp.row] <- as.numeric(tmp.abun)
}
if (any(choices$abun.units == "P/A")) {
tmp.row <- which(data$abun.units == "P/A")
tmp.abun <- data$abundance[tmp.row]
if (is.null(choices$abun.p)) {
print(unique(tmp.abun))
choices$abun.p <- readline("What symbol indicates presence? ")
choices$abun.a <- readline("What symbol indicates absence? ")
for (i in unique(tmp.abun)[!(unique(tmp.abun) %in% c(choices$abun.p, choices$abun.a))]) {
tmp.pa <- dlg_list(c("Presence", "Absence") , title = paste("Should", i, "be presence or absence?"))$res
if (tmp.pa == "Presence"){
choices$abun.p <- c(choices$abun.p, i)
} else if (tmp.pa == "Absence") {
choices$abun.a <- c(choices$abun.a, i)
}
}
}
tmp.abun[tmp.abun %in% choices$abun.p] <- 1
tmp.abun[tmp.abun %in% choices$abun.a] <- 0
data$abundance[tmp.row] <- tmp.abun
}
if (any(choices$abun.units == "Binned")) {
tmp.row <- which(data$abun.units == "Binned")
tmp.abun <- data$abundance[tmp.row]
if (is.null(choices$abun.bin)) {
print(grep("[^(0-9)|\\.]", unique(tmp.abun), value = TRUE))
choices$abun.bin <- readline("Enter the binned abundances starting with absent (e.g. n,r,c,a), leave reworked out: ")
choices$abun.bin <- unlist(strsplit(choices$abun.bin, ","))
}
tmp.abun <- match(tmp.abun, choices$abun.bin) - 1
tmp.abun[is.na(tmp.abun)] <- 0
data$abundance[tmp.row] <- tmp.abun
}
} else {
# add in the necessary additional columns
if (is.null(choices$abun)) {
choices$abun <- tibble(source = unique(data$source), abun = NA, units = NA)
choices$abun.smID <- tibble(source = unique(data$sampleID), abun = NA, units = NA)
}
data$abun.units <- NA
# calculate abundances based initially on the source
for (i in unique(data$source)) {
if (any(is.na(data$abun.units[data$source == i & !is.na(data$source)]))) {
tmp <- num.abun(i, data = data, choices = choices, sample_source = "source")
if (!is.null(tmp)) {
data$abundance[data$source == i & !is.na(data$source)] <- tmp$data
data$abun.units[data$source == i & !is.na(data$source)] <- choices$abun$units[choices$abun$source == i] <- tmp$abun.units
choices$abun$abun[choices$abun$source == i] <- list(tmp$choices)
}
#save(data, choices, file = paste("Outputs/tmp_dat/", substr(gsub("[[:punct:]]| ", "", i), 0, 40), ".RData", sep = ""))
}
}
rm(i, tmp)
# where there is no source, use the sample ID
for (i in unique(data$sampleID)) {
if (any(is.na(data$abun.units[data$sampleID == i]) | data$abun.units[data$sampleID == i] == "Mixed" | choices$abun$units[choices$abun$source == unique(data$source[match(i, data$sampleID)])] == "Mixed")) {
tmp <- num.abun(i, data = data, choices = choices, sample_source = "sample")
data$abundance[data$sampleID == i] <- tmp$data
data$abun.units[data$sampleID == i] <- choices$abun.smID$units[choices$abun.smID$source == i] <- tmp$abun.units
choices$abun.smID$abun[choices$abun.smID$source == i] <- list(tmp$choices)
save(data, choices, file = paste("Outputs/tmp_dat/", i, ".RData", sep = ""))
}
}
names(choices$abun.smID)[1] <- "sampleID"
rm(i, tmp)
data$abun.units <- factor(data$abun.units, levels = c("Relative abundance", "Count", "Number per gram", "Binned", "P/A", "Mixed"))
choices$full.abun <- data$abundance
choices$full.abun.units <- data$abun.units
}
#data$abundance <- as.numeric(as.character(data$abundance))
} else {
choices$full.abun <- choices$full.abun[!is.na(choices$full.abun)]
if (length(choices$full.abun) == nrow(data)) {
data$abundance <- choices$full.abun
data$abun.units <- choices$full.abun.units
} else {
data$abundance <- NA
data$abun.units <- NA
# working through the sources
for (i in unique(data$source[!is.na(data$source)])) {
# what is the abundance type
tmp.type <- c("abun.num", "abun.p", "abun.a", "abun.bin")[unlist(lapply(choices$abun$abun[choices$abun$source == i & !is.na(choices$abun$source)][[1]], function(x) !is.null(x)))]
# which rows belong to that data type
tmp.row <- which(data$source == i & !is.na(data$source))
# if there is an abundance type
if (length(tmp.type) > 0 & tmp.type[1] != "abun.num") {
# identify the abundance type
tmp.abun <- unlist(choices$abun$abun[choices$abun$source == i & !is.na(choices$abun$source)][[1]][tmp.type])
if (tmp.type[1] == "abun.p") {
data$abun.units[tmp.row] <- "P/A"
tmp.orig.abun <- data$orig.abundance[tmp.row]
tmp.new.abun <- rep(NA, length(tmp.orig.abun))
tmp.new.abun[tmp.orig.abun %in% tmp.abun[grep("abun.p", names(tmp.abun))]] <- 1
tmp.new.abun[tmp.orig.abun %in% tmp.abun[grep("abun.a", names(tmp.abun))]] <- 0
data$abundance[tmp.row] <- tmp.new.abun
rm(tmp.orig.abun, tmp.new.abun)
}
if (tmp.type[1] == "abun.bin") {
# add data units
data$abun.units[tmp.row] <- "Binned"
tmp.orig.abun <- data$orig.abundance[tmp.row]
# convert binned abundances
tmp.new.abun <- match(tmp.orig.abun, tmp.abun) - 1
tmp.new.abun[is.na(tmp.new.abun)] <- 0 # convert any that don't match to 0
data$abundance[tmp.row] <- tmp.new.abun
rm(tmp.orig.abun, tmp.new.abun)
}
rm(tmp.abun)
} else {
# if all of them are numeric
data$abundance[tmp.row] <- suppressWarnings(as.numeric(data$orig.abundance[tmp.row]))
if (length(tmp.type) > 0|sum(is.na(data$abundance[tmp.row] > 1))) {
data$abundance[tmp.row][is.na(data$abundance[tmp.row])] <- 0.01
}
if (max(with(data[data$source == i & !is.na(data$source), ], tapply(abundance, sampleID, sum))) <= 100){
data$abun.units[tmp.row] <- "Relative abundance"
} else if (sum(data$abundance[tmp.row] %% 1) == 0) {
data$abun.units[tmp.row] <- "Count"
}
}
# if there are actually mixed abundance units
if (tmp.type[1] == "abun.bin" & !is.na(tmp.type[1]) & any(with(data[data$source == i & !is.na(data$source),], tapply(orig.abundance, sampleID, function (x) length(grep("[0-9]", x)) > 0)))) {
# which should be numeric
tmp.num <- names(which(with(data[data$source == i & !is.na(data$source),], tapply(orig.abundance, sampleID, function (x) length(grep("[0-9]", x)) > 0))))
# convert to numbers
data$abundance[data$sampleID %in% tmp.num] <- suppressWarnings(as.numeric(data$orig.abundance[data$sampleID %in% tmp.num]))
data$abundance[data$sampleID %in% tmp.num & is.na(data$abundance)] <- 0.01
if (max(with(data[data$source == i & !is.na(data$source), ], tapply(abundance, sampleID, sum))) <= 115){
data$abun.units[data$sampleID %in% tmp.num] <- "Relative abundance"
} else if (sum(data$abundance[tmp.row] %% 1) == 0) {
data$abun.units[data$sampleID %in% tmp.num] <- "Count"
}
}
rm(tmp.row, tmp.type)
}
# where there aren't sources, working through the sample.ID
for (i in sort(unique(data$Sample.ID[!is.na(data$Sample.ID)]))) {
if (any(is.na(data$abun.units[data$Sample.ID == i]))) {
# what is the abundance type