-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtilities.R
1176 lines (957 loc) · 37.1 KB
/
Utilities.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
#' Merge list of data tables recursively
#'
#' @param data A list of data tables.
#' @param tableNames A character vector holding the names of the tables to merge.
#' @param output.only.last Only returns last merged table.
#' @param ... Extra parameters that will be passed into \code{\link[data.table]{merge}}.
#'
#' @return A merged data table.
#'
#' @export
#'
mergeDataTables <- function(data, tableNames = NULL, output.only.last = FALSE, ...) {
# Better use xsdObjects for getting header vars from XML data for merging
## Get data type:
plen <- NULL
if(!is.null(data[["metadata"]])) {
datatype <- unlist(data[["metadata"]][1, "useXsd"])
plen <- RstoxData::xsdObjects[[paste0(datatype, ".xsd")]]$prefixLens
}
# Merge all tables by default:
if(length(tableNames) == 0) {
tableNames <- names(data)
}
# No merging if only one table given in 'tableNames':
else if(length(tableNames) == 1) {
return(data)
}
# Make sure tableNames are ordered as in the data:
dataNames <- names(data)
tableNames <- dataNames[match(tableNames, dataNames)]
tableNames <- tableNames[!is.na(tableNames)]
# Merge
for(ii in 2:length(tableNames)) {
curr <- tableNames[ii]
prev <- tableNames[(ii-1)]
if(!is.null(plen) && !is.na(plen[prev]))
vars <- names(data[[curr]])[1:plen[prev]]
else
vars <- intersect(names(data[[curr]]), names(data[[prev]]))
# There can be duplicate names between two tables, see that we fix them by adding appropriate suffix before merging
duplicates <- intersect(setdiff(names(data[[prev]]), vars), setdiff(names(data[[curr]]), vars))
for(ddpl in duplicates) {
#message(paste("Duplicate columns in merging", prev, "and", curr, ": ", ddpl, "->", paste0(ddpl, ".", curr)))
setnames(data[[curr]], ddpl, paste0(ddpl, ".", curr))
}
data[[curr]] <- merge(data[[prev]], data[[curr]], by=vars, suffixes = suffixes, ...)
}
# If tableNamestableNames == "last", return the last table:
if(output.only.last) {
data <- data[[utils::tail(tableNames, 1)]]
}
return(data)
}
#' Merge two data tables by the intersect of the names
#'
#' @param x,y Data tables of class \code{\link[data.table]{data.table}}.
#' @param ... Various overrides.
#' @param msg Verbose message switch, default to \code{FALSE}.
#'
#' @return A merged data table.
#'
#' @export
#'
mergeByIntersect <- function(x, y, ..., msg = FALSE) {
# Cascading merge if a list of tables is given:
if(length(x) > 1 &&
is.list(x) &&
!data.table::is.data.table(x) &&
data.table::is.data.table(x[[1]])) {
for(ind in seq(2, length(x))) {
x[[ind]] <- mergeByIntersect(x[[ind - 1]], x[[ind]], ..., msg = msg)
}
output <- x[[ind]]
}
else {
by <- intersect(names(x), names(y))
if(msg) {
message("Merging by ", paste(by, collapse = ", "))
}
if(length(by)) {
output <- merge(x, y, by = by, ...)
}
else {
stop("No intersect between the names of ", deparse(substitute(x)), " and ", deparse(substitute(y)))
}
}
return(output)
}
#' Merge two data tables by StoX keys
#'
#' @param x,y Data tables of class \code{\link[data.table]{data.table}}.
#' @param StoxDataType Input data type. Text string of \code{StoxBiotic}
#' or \code{StoxAcoustic}.
#' @param toMergeFromY Specify key columns from \code{y}. \code{NULL} means
#' all similarly named columns from \code{x} and \code{y} will be
#' merged. Default to \code{NULL}.
#' @param replace Whether to replace the variables in the target.
#' Default to \code{FALSE}.
#' @param unique Logical: If TRUE (default) make the tables unique after merging.
#' @param ... Extra parameters that will be passed into \code{\link[data.table]{merge}}.
#'
#' @return A merged data table.
#'
#' @export
#'
mergeByStoxKeys <- function(x, y, StoxDataType, toMergeFromY = NULL, replace = FALSE, unique = TRUE, ...) {
# Get the keys:
#keys_x <- getKeys(x)
#keys_y <- getKeys(y)
#keys <- intersect(keys_x, keys_y)
keys <- Reduce(intersect,
list(
names(x),
names(y),
getStoxKeys(StoxDataType = StoxDataType)
)
)
# Define the columns to merge:
if(!length(toMergeFromY)) {
toMergeFromY <- names(y)
}
# Make sure the toMergeFromY are present in y:
toMergeFromY <- intersect(names(y), toMergeFromY)
# Exclcude the keys:
toMergeFromY <- setdiff(toMergeFromY, getStoxKeys(StoxDataType = StoxDataType))
# Replace the variable in the target:
if(replace) {
keep <- setdiff(names(x), toMergeFromY)
x <- x[, ..keep]
}
# If there are any left, extract the keys and toMergeFromY:
if(length(toMergeFromY)) {
y <- y[, c(keys, toMergeFromY), with = FALSE]
# Then merge:
x <- merge(x, y, by = keys, ...)
}
else {
x
}
if(unique) {
x <- unique(x)
}
return(x)
}
#getKeys <- function(x, keystring = "Key", ignore.case = FALSE) {
# namesx <- names(x)
# namesx[endsWith(if(ignore.case) tolower(namesx) else namesx, if(ignore.case) tolower(keystring) else keystring#)]
#}
#' Get the keys of a StoX format
#'
#' @param StoxDataType The name of the StoX format (only StoxBiotic implemented yet).
#' @param level The name of the level/table to get keys for.
#' @param keys.out Specification of what to return. One of "all", to return all keys of the level; "only.present", to return only the key of the \code{level}; and "all.but.present", to return all keys except the present key.
#'
#' @importFrom data.table key
#' @export
#'
getStoxKeys <- function(StoxDataType = c("StoxBiotic", "StoxAcoustic"), level = NULL, keys.out = c("all", "only.present", "all.but.present")) {
StoxDataType <- match_arg_informative(StoxDataType)
if(StoxDataType == "StoxBiotic") {
if(!exists("stoxBioticObject")) {
data(stoxBioticObject, package="RstoxData", envir = environment())
}
keys <- stoxBioticObject$convertTable[iskey == "Y", c("variable", "level")]
keys <- split(keys, by = "level")
keys <- lapply(keys, "[[", "variable")
}
else if(StoxDataType == "StoxAcoustic") {
stop("Not yet implemented")
}
if(length(level)) {
keys <- keys[[level]]
}
else {
keys <- unique(unlist(keys))
return(keys)
}
keys.out <- match_arg_informative(keys.out)
if(keys.out == "only.present") {
keys <- utils::tail(keys, 1)
}
else if(keys.out == "all.but.present") {
keys <- keys[-length(keys)]
}
return(keys)
}
# Detect OS
get_os <- function() {
if (.Platform$OS.type == "windows") {
"win"
} else if (Sys.info()["sysname"] == "Darwin") {
"mac"
} else if (.Platform$OS.type == "unix") {
"unix"
} else {
stop("Unknown OS")
}
}
#' Pick a suitable number of cores
#'
#' @inheritParams lapplyOnCores
#' @param n Optional length of the data to apply parallel processing to.
#'
#' @return The number of cores to apply.
#'
#' @export
#'
getNumberOfCores <- function(NumberOfCores = NULL, n = NULL) {
# Detect number of cores if not given:
availableNumberOfCores <- parallel::detectCores()
if(!length(NumberOfCores)) {
NumberOfCores <- as.integer(getOption("mc.cores"))
if (!length(NumberOfCores) || is.na(NumberOfCores)) {
NumberOfCores <- availableNumberOfCores
if (is.na(NumberOfCores)) {
return(1)
}
}
}
# Do not use more cores than the number of elemens:
if(length(n)) {
NumberOfCores <- min(n, NumberOfCores, availableNumberOfCores)
}
return(NumberOfCores)
}
#' Run a function on all elements of x on one or more cores
#'
#' @param x An object to apply \code{FUN} to.
#' @param FUN The function to apply.
#' @inheritParams general_arguments
#' @param ... Additional arguments to \code{FUN}.
#'
#' @return A list of outputs from \code{FUN}.
#'
#' @export
#'
lapplyOnCores <- function(x, FUN, NumberOfCores = 1L, ...) {
# Get the number of cores to use:
NumberOfCores <- getNumberOfCores(NumberOfCores, n = length(x))
# Simple Lapply if onle one core:
if(NumberOfCores == 1) {
out <- lapply(x, FUN, ...)
}
# Run in parallel on Windows and other platforms:
else if(NumberOfCores > 1){
# On Windows run special args to speed up:
if(get_os() == "win") {
cl <- parallel::makeCluster(NumberOfCores, rscript_args = c("--no-init-file", "--no-site-file", "--no-environ"))
parallel::clusterEvalQ(cl, {
library(RstoxData)
})
out <- parallel::parLapply(cl, x, FUN, ...)
parallel::stopCluster(cl)
}
else {
out <- parallel::mclapply(x, FUN, mc.cores = NumberOfCores, ...)
}
}
else {
out <- NULL
}
return(out)
}
#' Run a function on all elements of x on one or more cores
#'
#' @inheritParams lapplyOnCores
#' @param ...,MoreArgs,SIMPLIFY See \code{\link[base]{mapply}}.
#'
#' @return A list of outputs from \code{FUN}.
#'
#' @export
#'
mapplyOnCores <- function(FUN, NumberOfCores = 1L, ..., MoreArgs = NULL, SIMPLIFY = FALSE) {
# Get the number of cores to use:
NumberOfCores <- getNumberOfCores(NumberOfCores, n = max(lengths(list(...))))
# Simple mapply if only one core:
if(NumberOfCores == 1) {
out <- mapply(FUN, ..., MoreArgs = MoreArgs, SIMPLIFY = SIMPLIFY)
}
# Run in parallel on Windows and other platforms:
else if(NumberOfCores > 1){
# The parallel processing was conducted differently on Windows and macOS/Linux before StoX 4.1.0 (for some historic reason), with sockets for the former and forks for the latter. This was a potential memory problem since forking copies the R workspace to all the cores. Instead, sockets are now used for all platforms:
# On Windows run special args to speed up:
#if(get_os() == "win") {
# Removed the rscript_args, because it changes the envoronment compared to the partent environment:
#cl <- parallel::makeCluster(NumberOfCores, rscript_args = c("--no-init-file", "--no-site-file", "--no-environ"))
cl <- parallel::makeCluster(NumberOfCores)
out <- parallel::clusterMap(cl, FUN, ..., MoreArgs = MoreArgs, SIMPLIFY = SIMPLIFY)
parallel::stopCluster(cl)
#}
#else {
# out <- parallel::mcmapply(FUN, mc.cores = NumberOfCores, ..., MoreArgs = MoreArgs, SIMPLIFY = SIMPLIFY)
#}
}
else {
out <- NULL
}
return(out)
}
#' Get the first element returned by class(), and translate "double" to "numeric".
#'
#' @param x An R object.
#'
#' @export
firstClass <- function(x) {
out <- class(x)[1]
if(out == "double") {
out <- "numeric"
}
return(out)
}
## Stolen from https://stackoverflow.com/questions/47190693/count-the-number-of-integer-digits:
#n_int_digits = function(x) {
# result = floor(log10(abs(x)))
# result[!is.finite(result)] = 0
# result
#}
# Function to get the formats of StoX raw data:
getStoxRawDataFormat <- function(x, unlist = FALSE) {
formats <- lapply(x, function(this) this$metadata$useXsd)
names(x) <- names(x)
if(unlist) {
formats <- unlist(formats)
}
return(formats)
}
# Check that the formats are unique:
checkUniqueFormat <- function(x) {
nonUniqueFormats <- getRstoxDataDefinitions("nonUniqueFormats")
uniqueFormat <- !any(getStoxRawDataFormat(x, unlist = TRUE) %in% inapplicableFormats)
return(uniqueFormat)
}
## Function to remove rows with duplicated keys in StoxBioticData:
#removeRowsOfDuplicatedKeysFromStoxBioticData <- function(StoxBioticData) {
# StoxBioticKeys <- getRstoxDataDefinitions("StoxBioticKeys")
#
# # Run through the tables of the StoxBioticData and remove duplicate rows:
# for(tableName in names(StoxBioticData)) {
# # Get the names of the columns which are keys:
# presentKeys <- intersect(names(StoxBioticData[[tableName]]), StoxBioticKeys)
# # Find rows of duplicated keys:
# duplicatedKeys <- duplicated(StoxBioticData[[tableName]][, ..presentKeys])
# # Remove the rows with duplicated keys:
# rowsToKeep <- !duplicatedKeys
# if(any(duplicatedKeys)) {
# warning("StoX: Removing ", sum(duplicatedKeys), " rows of duplicated keys.")
# StoxBioticData[[tableName]] <- StoxBioticData[[tableName]][rowsToKeep, ]
# }
# }
#
# return(StoxBioticData)
#}
# Function to remove rows with duplicated keys in StoxBioticData:
#' @importFrom data.table .I
removeRowsOfDuplicatedKeys <- function(StoxData, stoxDataFormat = c("Biotic", "Acoustic")) {
stoxDataFormat <- match_arg_informative(stoxDataFormat)
StoxKeys <- getRstoxDataDefinitions(paste0("Stox", stoxDataFormat, "Keys"))
# Run through the tables of the StoxData and remove duplicate rows:
for(tableName in names(StoxData)) {
# Get the names of the columns which are keys:
presentKeys <- intersect(names(StoxData[[tableName]]), StoxKeys)
# Find rows of duplicated keys:
duplicatedKeys <- duplicated(StoxData[[tableName]], by = presentKeys)
# Remove the rows with duplicated keys:
if(any(duplicatedKeys)) {
# Get the rows with equal keys, and indicate this in a copy of the data, and write to a tempfile:
#allDuplicated <- duplicated(StoxData[[tableName]], by = presentKeys) | duplicated(StoxData[[tableName]], by = presentKeys, fromLast = TRUE)
#dupData <- data.table::copy(StoxData[[tableName]])
#dupData[, duplicated := ..allDuplicated]
#dupData[, rowIndex := .I]
#fileToWriteDupDataTo <- tempfile()
#data.table::fwrite(dupData, fileToWriteDupDataTo)
#warning("StoX: Removing ", sum(duplicatedKeys), " rows of duplicated keys from table ", tableName, ". This may be due to different files with the same keys, e.g. if different acoustic instruments are stored in different files. In such a case the order of the files is crucial, as only the information from the first file is kept. If not different files, then duplicated keys may be an error. To see the duplicated rows run the following in R: dat <- data.table::fread(\"", fileToWriteDupDataTo, "\")")
#warning("StoX: Removing ", sum(duplicatedKeys), " rows of duplicated keys from table ", tableName, ". To see the duplicated rows run the following in R: dat <- data.table::fread(\"", fileToWriteDupDataTo, "\"), which contains the column \"duplicated\"")
#rowsToKeep <- !duplicatedKeys
StoxData[[tableName]] <- StoxData[[tableName]][!duplicatedKeys, ]
}
}
return(StoxData)
}
# Function to remove rows with duplicated keys in StoxBioticData:
#' @importFrom data.table .I
warningMissingKeys <- function(StoxData, stoxDataFormat = c("Biotic", "Acoustic")) {
stoxDataFormat <- match_arg_informative(stoxDataFormat)
StoxKeys <- getRstoxDataDefinitions(paste0("Stox", stoxDataFormat, "Keys"))
presentKeys <- lapply(StoxData, function(x) intersect(names(x), StoxKeys))
atMaxNumberOfKeys <- which.max(lengths(presentKeys))
hasmissingKeys <- StoxData[[atMaxNumberOfKeys]][, lapply(.SD, is.na), .SDcols = presentKeys[[atMaxNumberOfKeys]]]
hasmissingKeys <- lapply(hasmissingKeys, any)
# Warn if any keys have missing values:
if(any(unlist(hasmissingKeys))) {
warning("The Stox", stoxDataFormat, "Data has missing keys (", paste(names(StoxData)[unlist(hasmissingKeys)], names(hasmissingKeys)[unlist(hasmissingKeys)], sep = ": ", collapse = ", "), ")! Please translate fields in the Read", stoxDataFormat, "process to avoid this. ")
}
}
AddToStoxData <- function(
StoxData,
RawData,
VariableNames = character(),
#AddToLowestTable = FALSE,
SplitTableAllocation = c("Default", "Lowest", "Highest"),
NumberOfCores = 1L,
StoxDataFormat = c("Biotic", "Acoustic")
) {
if(length(VariableNames) == 0) {
warning("StoX: No variables specified to extract. Returning data unchcanged")
return(StoxData)
}
# Check the the BioticData are all from the same source (ICES/NMD):
checkDataSource(RawData)
# If from NMDBiotic <= 1.4, skip all non-relevant tables (as e.g. the prey table does not contain the keys necessary to merge with individual, thus resulting in different columns than data from NMDBiotic >= 3):
invalidTables1.4 <- c("missionlog", "prey", "preylength", "copepodedevstage", "tag")
for(RawDataName in names(RawData)) {
namesInInvalidTables <- unlist(lapply(RawData[[RawDataName]][invalidTables1.4], names))
validTables1.4 <- setdiff(names(RawData[[RawDataName]]), invalidTables1.4)
namesInValidTables <- unlist(lapply(RawData[[RawDataName]][validTables1.4], names))
invalidRawNames <- setdiff(namesInInvalidTables, namesInValidTables)
if(RawData[[RawDataName]]$metadata$useXsd %in% c("nmdbioticv1.1", "nmdbioticv1.4") && any(VariableNames %in% invalidRawNames)) {
warning("StoX: Cannot add variables from the tables ", paste(invalidTables1.4, collapse = ", "), " from NMDBiotic 1.1 and 1.4 due to lack of keys for these tables.")
RawData[[RawDataName]] <- subset(RawData[[RawDataName]], ! names(RawData[[RawDataName]]) %in% c("missionlog", "prey", "preylength", "copepodedevstage", "tag"))
}
}
# Convert from BioticData to the general sampling hierarchy:
StoxDataFormat <- match_arg_informative(StoxDataFormat)
if(StoxDataFormat == "Biotic") {
GeneralSamplingHierarchy <- BioticData2GeneralSamplingHierarchy(
RawData,
NumberOfCores = NumberOfCores,
#AddToLowestTable = AddToLowestTable
SplitTableAllocation = SplitTableAllocation
)
allNames <- unlist(lapply(GeneralSamplingHierarchy, names))
if(any(VariableNames %in% allNames)) {
warning("StoX: The following VariableNames are already present in the data, and will be overwritten: ", paste0(intersect(VariableNames, allNames)))
}
# Define a vector of the variables to extract:
toExtract <- c(
getRstoxDataDefinitions("StoxBioticKeys"),
VariableNames
)
}
else if(StoxDataFormat == "Acoustic") {
stop("Not yet implemented")
}
else {
stop("Invalid StoxDataFormat")
}
# Extract the variables to add:
toAdd <- lapply(GeneralSamplingHierarchy, function(x) lapply(x, extractVariables, var = toExtract))
# Rbind for each StoxBiotic table:
toAdd <- rbindlist_StoxFormat(toAdd)
# Extract only those tables present in StoxBioticData:
toAdd <- toAdd[names(StoxData)]
# Keep only unique rows:
toAdd <- lapply(toAdd, unique)
# add a ccheck for exxisting variables, and remoev these with a warning
# Merge with the present StoxBioticData:
StoxData <- mapply(mergeAndOverwriteDataTable, StoxData, toAdd, all.x = TRUE, sort = FALSE)
# Remove rows of duplicated keys. This is particularly relevant for NMDBiotic files where the station variable has bee used, in which case removeRowsOfDuplicatedKeys selects only the first row (keeping only the first station information):
StoxBioticData <- removeRowsOfDuplicatedKeys(
StoxData = StoxData,
stoxDataFormat = "Biotic"
)
return(StoxData)
}
mergeAndOverwriteDataTable <- function(x, y, ...) {
# Do not merge if y is empty:
if(!length(y)) {
return(x)
}
# Overwrite any non-keys already present in the StoxData
toKeep <- !names(x) %in% names(y) | endsWith(names(x), "Key")
mergeByIntersect(x[, ..toKeep], y, ...)
}
# Function to extracct variables from a table:
extractVariables <- function(x, var) {
varToExtract <- intersect(names(x), var)
if(length(varToExtract)) {
x[, ..varToExtract]
}
else {
#warning("None of the variables present")
data.table::data.table()
}
}
checkDataSource <- function(BioticData) {
# Function to match the metadata against data source strings:
matchSource <- function(x, BioticData) {
matched <- startsWith(sapply(lapply(BioticData, "[[", "metadata"), "[[", "useXsd"), x)
output <- rep(NA, length(matched))
output[matched] <- x
return(output)
}
# Detect the data source:
possibleDataSources <- c("nmd", "ices")
detectedDataSources <- sapply(possibleDataSources, matchSource, BioticData = BioticData, simplify = FALSE)
numberOfFormats <- sum(sapply(detectedDataSources, function(x) any(!is.na(x))))
#detectedDataSources <- apply(detectedDataSources, 1, min, na.rm = TRUE)
# Accept only BioticData from a single source:
if(numberOfFormats > 1) {
stop("The function AddToStoxBiotic can only be applied to BioticData where all files read are of the same data source (NMD or ICES)")
}
return(detectedDataSources)
}
# Find the variables to translate, by looping through the rows of the vocabulary and locating the variables which has any values in the vocabulary$id:
findVariablesMathcinigVocabulary <- function(vocabulary, data) {
# Split into individual rows and find the variables that contain the values of each row, and add the variables as a column VariableName, as per the TranslationRequiredColumns:
vocabularyList <- split(vocabulary, seq_len(nrow(vocabulary)))
vocabularyList <- lapply(vocabularyList, findVariablesMathcinigVocabularyOne, data = data)
# Rbind and rename the columns "id" and "value" to "Value" and "NewValue":
vocabulary <- data.table::rbindlist(vocabularyList)
data.table::setnames(
vocabulary,
c("id", "value"),
c("Value", "NewValue")
)
# Clean the table of irrelevant columns:
vocabulary <- vocabulary[, c("VariableName", "Value", "NewValue")]
# Remove rows with VariableName not found in the data, which has VariableName = NA:
vocabulary <- subset(vocabulary, !is.na(VariableName))
return(vocabulary)
}
findVariablesMathcinigVocabularyOne <- function(vocabularyOne, data) {
# Get the names of the columns which has values in vocabularyOne$id:
#VariableName <- unlist(lapply(data, function(table) names(which(unlist(table[, lapply(.SD, function(x) any(x %in% vocabularyOne$id))])))))
VariableName <- unlist(lapply(data, function(table) if(NROW(table)) names(which(unlist(table[, lapply(.SD, function(x) any(unique(x) %in% vocabularyOne$id))]))) else NULL))
# Add the VariableName to the vocabularyOne
if(!length(VariableName)) {
# Add NA if no variable was recognized (this avoids warnings when cbind with character(0)
VariableName <- NA
}
vocabularyOne <- cbind(
VariableName = VariableName,
vocabularyOne
)
return(vocabularyOne)
}
#' Order a StoX format by keys
#'
#' @param data An object of type \code{\link{StoxAcousticData}} or \code{\link{StoxBioticData}}
#'
#' @export
#'
orderRowsByKeys <- function(data) {
# StoX keys are concatenated only by "/", whereas IDs are concatenated keys by "-":
lapply(data, setorderv_numeric, key = "Key", split = "/")
}
#' Order a data.table (by reference) by interpreting characters as numeric if possible
#'
#' @param dataOne A data.table.
#' @param by Order by the given columns.
#' @param key If given and \code{by} is empty, order by the columns with names ending with \code{key}.
#' @param split Character: A vector of single character to split by. The default c("-", "/") splits between StoX keys and within StoX keys.
#'
#' @export
#'
setorderv_numeric <- function(dataOne, by = NULL, key = NULL, split = "/") {
#setorderv_numeric <- function(dataOne, by = NULL, key = NULL, ...) {
# Locate keys:
if(!length(by)) {
if(length(key)) {
by <- names(dataOne)[endsWith(names(dataOne), key)]
}
else {
by <- names(dataOne)
}
}
if(length(by)) {
orderKeys <- paste0(by, "OrderedAfterSplitting")
# Create keys which are converted to ranks, splitting first and then treating individual elements as numbers if possible:
dataOne[, (orderKeys) := lapply(.SD, createOrderKey, split = split), .SDcols = by]
# Order the rows:
# 2024-10-28: Change to sort out sorting:
data.table::setorderv(dataOne, orderKeys, na.last = TRUE)
# Remove the orderKeys:
dataOne[, (orderKeys) := NULL]
}
return(dataOne)
}
addNAs <- function(x, areNAs) {
if(any(areNAs)) {
NAs <- rep(NA, length(areNAs))
NAs[!areNAs] <- x
x <- NAs
}
return(x)
}
#' Convert a vector to an integer vector where individual string elements at interpreted as numeric if possible.
#'
#' @param x A vector.
#' @param split A character to split strings by.
#'
#' @export
#'
createOrderKey <- function(x, split = "/") {
# Split the keys:
if(!is.character(x)) {
return(x)
}
firstNonNA <- x[1]
if(is.na(firstNonNA)) {
if(all(is.na(x))) {
return(x)
}
firstNonNA <- x[min(which(!is.na(x)))]
}
# If the first element is coercible to numeric, try converting the entire vector to numeric, and check that no NAs were generated:
if(!is.na(suppressWarnings(as.numeric(x[1])))) {
numberOfNAs <- sum(is.na(x))
xnumeric <- suppressWarnings(as.numeric(x))
if(sum(is.na(xnumeric)) > numberOfNAs) {
return(x)
}
else {
return(xnumeric)
}
}
# Split by the 'split' argument:
if(!any(sapply(split, grepl, firstNonNA))) {
return(x)
}
# Split the vector by the 'split' parameter:
splitted <- x
for(thisSplit in split) {
splitted <- lapply(splitted, function(x) unlist(strsplit(x, thisSplit, fixed = TRUE)))
}
#splitted <- strsplit(x, split, fixed = TRUE)
# Check that all have the same number of elements, that is the same number of splits:
if(!all(lengths(splitted) == length(splitted[[1]]))) {
return(x)
}
# Create a data.table of the splitted elements and get the order of these:
splittedDT <- data.table::rbindlist(lapply(splitted, as.list))
suppressWarnings(splittedDT[, names(splittedDT) := lapply(.SD, as.numeric_IfPossible)])
# Only accept if all elements can be converted to numeric:
#if(any(is.na(splittedDT))) {
# Acccpet if any of the values are not NA:
if(all(is.na(splittedDT))) {
return(x)
}
# Convert to integer ranks:
#splittedDT[, names(splittedDT) := lapply(.SD, function(y) match(y, sort(unique(y))))]
# Replicate data.table's soring which happend in C-locale (see ?data.table::setorderv):
#splittedDT[, names(splittedDT) := lapply(.SD, function(y) match(y, stringi::stri_sort(unique(y), locale = "C")))]
#splittedDT[, names(splittedDT) := lapply(.SD, function(y) match(y, stringr::str_sort(unique(y), locale = "C")))]
# This sorting (en_US_POSIX) is the same that data.table uses in setorder/setorderv(), which uses the ICU C locale (https://icu.unicode.org/design/locale/root):
splittedDT[, names(splittedDT) := lapply(.SD, function(y) match(y, stringi::stri_sort(unique(y), locale = "en_US_POSIX")))]
# Count the maximum number of digits for each column, and multiply by the cumulative number of digits:
numberOfDigits <- splittedDT[, lapply(.SD, max)]
numberOfDigits <- nchar(numberOfDigits)
exponent <- rev(c(0, cumsum(rev(numberOfDigits))[ -length(numberOfDigits)]))
names(exponent) <- names(splittedDT)
for(name in names(splittedDT)) {
splittedDT[, (name) := get(name) * 10^(exponent[[name]])]
}
orderKey <- rowSums(splittedDT)
#orderOfSplitted <- do.call(order, splittedDT)
## Match with a sequence to create integers used as order key:
#orderKey <- match(seq_along(x), orderOfSplitted)
#
return(orderKey)
}
as.numeric_IfPossible <- function(x) {
# If any missing values are introduced, keep the original:
num <- as.numeric(x)
#if(all(is.na(num))) {
# 2024-10-28: Change to sort out sorting:
if(any(is.na(num) & !is.na(x))) {
return(x)
}
else {
return(num)
}
}
createEmptyDataTable <- function(names, classes = NULL) {
# Create the named empty data.table:
DT <- stats::setNames(data.table::data.table(matrix(nrow = 0, ncol = length(names))), names)
# Accept classes given as a vector of the same length as the names:
if(length(classes) == length(names) && !is.list(classes)) {
classes <- structure(as.list(classes), names = names)
}
setColumnClasses(DT, classes = classes)
return(DT)
}
#' Convert classes of a data.table:
#'
#' @param DT A data.table
#' @param classes A named list of classes. Columns in \code{DT} with name found in \code{classes} are modified if the existing class differs from the desired class.
#'
#' @export
#'
setColumnClasses <- function(DT, classes = NULL) {
# Create a table of the old and new class:
classTable <- merge(
data.table::data.table(
columnName = names(DT),
oldClass = sapply(DT, firstClass)
),
data.table::data.table(
columnName = names(classes),
newClass = unlist(classes)
),
by = "columnName",
all = FALSE
)
# Change only the columns that differ between old and new class:
classTable <- subset(classTable, oldClass != newClass)
# Accept classes given as a vector of the same length as the names:
for(col in classTable$columnName) {
data.table::set(DT, j = col, value = do.call(paste("as", classes[[col]], sep = "."), list(DT[[col]])))
}
return(DT)
}
#' Sanitize an expression given in a string
#'
#' If the input contains "system" or "system2", an error is issued.
#'
#' @param x A string
#'
#' @export
#'
sanitizeExpression <- function(x) {
# Detect one or more "system" followed by 0 or one "2" and 0 or more spaces and then one or more parenthesis start:
usesSystem <- grepl("system+2? *\\(+", x)
if(any(usesSystem)) {
stop("The following expression applies a call to the operating system, and may contain harmful code (please do not try to hack using StoX): ", x)
}
}
#' Informative match.arg
#'
#' @inheritParams base::match.arg
#' @param arg_name The name of the argument, interpreted by default.
#' @param ignore.case Logical: Should case be igngored.
#'
#' @export
#'
match_arg_informative <- function (arg, choices, several.ok = FALSE, arg_name = substitute(arg), ignore.case = FALSE) {
# Stolen from https://github.com/DarwinAwardWinner/rctutils/blob/master/R/fixes_for_builtins.R:
if (missing(choices)) {
formal.args <- formals(sys.function(sys.parent()))
choices <- eval(formal.args[[as.character(substitute(arg))]])
}
if (is.name(arg_name)) {
arg_name <- as.character(arg_name)
}
arg_name_string <- deparse_onestring(arg_name)
if (is.null(arg))
return(choices[1L])
else if (!is.character(arg))
stop(arg_name_string, " must be NULL or a character vector")
if (!several.ok) {
if (identical(arg, choices))
return(arg[1L])
if (length(arg) > 1L)
stop(arg_name_string, " must be of length 1")
}
else if (length(arg) == 0L)
stop(arg_name_string, " must be of length >= 1")
fold_case <- identity
if (ignore.case) {
fold_case <- tolower
}
i <- pmatch(fold_case(arg), fold_case(choices), nomatch = 0L, duplicates.ok = TRUE)
if (all(i == 0L))
stop(gettextf("%s should be one of %s", arg_name_string, paste0(paste(dQuote(choices),
collapse = ", "), " (was ", dQuote(arg), ")")), domain = NA)
i <- i[i > 0L]
if (!several.ok && length(i) > 1)
stop("There is more than one match for ", arg_name_string, " in \"
match_arg_informative\"")
choices[i]
}
deparse_onestring <- function(...) {
paste(deparse(...), collapse = "\n")
}
#' Function to return the names of the arguments to show for a function:
#'
#' @param functionArgumentHierarchy The function argument hierarchy defined in the stoxFunctionAttributes.
#' @param functionArguments A list of the arguments to the function (both function inputs and function parameters).
#' @param return.only.names Logical: If TRUE return only the names of the arguments to show.
#' @param ignore.condition Character: A vector of strings naming parameters to ignore in the hierarchy. Current use is "UseProcessData" to identify function inputs hidden by that parameter.
#'
#' @export
#'
applyFunctionArgumentHierarchy <- function(
functionArgumentHierarchy,
functionArguments,
return.only.names = TRUE,
#ignore = "UseProcessData"
ignore.condition = NULL
) {
# Support an expression at the top level:
if(inherits(functionArgumentHierarchy, "expression")) {
functionArgumentHierarchy <- eval(functionArgumentHierarchy)
}
# Loop through the arguments given by parent tags in the functionArgumentHierarchy, and set toShow to FALSE if not any of the criteria are fulfilled:
toShow <- logical(length(functionArguments))
names(toShow) <- names(functionArguments)
for(argumentName in names(toShow)) {
# Check whether the argument is given in the functionArgumentHierarchy. If not, it will be shown:
atArgumentName <- which(argumentName == names(functionArgumentHierarchy))
if(length(atArgumentName)) {
# Loop through the occurrences of the argumentName in the functionArgumentHierarchy, applying &&:
hitsOr <- logical(length(atArgumentName))
for(ind in seq_along(atArgumentName)) {
# Loop through the conditions (except the ones given by 'ignore.condition') and set hitsAnd to TRUE if at least one condition is fullfilled:
conditionNames <- setdiff(names(functionArgumentHierarchy[[atArgumentName[ind]]]), ignore.condition)
hitsAnd <- logical(length(conditionNames))
names(hitsAnd) <- conditionNames
for(conditionName in conditionNames) {
if(is.function(functionArgumentHierarchy[[atArgumentName[ind]]][[conditionName]])) {
if(isTRUE(functionArgumentHierarchy[[atArgumentName[ind]]][[conditionName]](functionArguments))) {
hitsAnd[conditionName] <- TRUE
}
}
else {
# Added requirement that functionArguments[[conditionName]] has positive length:
if(length(functionArguments[[conditionName]]) && functionArguments[[conditionName]] %in% eval(functionArgumentHierarchy[[atArgumentName[ind]]][[conditionName]])) {
hitsAnd[conditionName] <- TRUE
}
}
}
# Apply the AND condition, implying that hitsAnd is TRUE if all are TRUE:
hitsOr[ind] <- all(hitsAnd)
}
toShow[[argumentName]] <- any(hitsOr)
}
else {
toShow[[argumentName]] <- TRUE
}
}
# Return only the names of the arguments to show:
if(return.only.names) {
toShow <- names(toShow)[toShow]
}
return(toShow)
}
#' Paste to a linespace + tab separeted string
#'
#' @param errorIDs A vector of strings.