-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 16.8 KB
/
.Rhistory
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
tb_pit$date <- ymd(tb_pit$date)
tb_pit <- tb_pit %>% rename(date_pit=date)
tb$date <- ymd(tb$date)
tb_obs_pit <-
tb %>%
mutate(observation = row_number()) %>%
left_join(tb_pit,by="pit") %>%
filter(!is.na(date_pit)) %>%
filter(date_pit <= date) %>%
arrange(date_pit) %>%
group_by(observation) %>%
summarise(ID = ID[n()])
tb %>%
mutate(observation = row_number()) %>%
left_join(tb_obs_pit,by="observation") %>%
select(-observation)
}
#' @title re_dnaID
#' @export
re_dnaID <- function(tb,tb_rednaid){
require(glue)
#1: Go through each row in the rednaID table
#2: for the given pit, and date:
#3: find the item in the datepit table with:
#4: the same pit
#5: that was registered the time before that datepit
#6 then, rename the ID in that item with the one from the current row in the rednaID table
for( i_row in 1:nrow(tb_rednaid) )
{
row <- tb_rednaid[i_row,]
pit <- row$pit
date <- row$date
dnaid <- row$dnaID
if(nrow( tb[tb$pit==pit & tb$date < date,])==0) message(glue("re-dnaid: pit {pit} for id {dnaid} and date {date} not found"))
tb[tb$pit==pit & tb$date < date,][1,][["ID"]] <- dnaid
}
return(tb)
}
#' @export
write_datepit_file <- function(tb, finclip_matches){
identify_fleeters <- function(tb_pits){
tb_lonely_measurements <-
tb_pits %>%
group_by(i_measurement) %>%
summarise(
lonely = if_else(sum(!is.na(pit)) == 1 & all(is.na(dnaID)),T,F)
) %>% ungroup()
# if first mention of pit is missing dnaID and another pit (is lonely), make fleeter
# alternatively, if a "pit_new" is lonely, also make fleeter
tb_pits %>%
left_join(tb_lonely_measurements,by="i_measurement") %>%
group_by(pit) %>%
arrange(date) %>%
filter(pit!="" & !is.na(pit)) %>%
mutate(
dnaID = ifelse((1:n())==1 & lonely, yes=glue::glue("Fleeter{date}{measOrder}"), no=dnaID),
dnaID = ifelse(i_pit == 2 & lonely, yes=glue::glue("Fleeter{date}{measOrder}"), no=dnaID)
) %>%
ungroup() %>%
select(-lonely)
}
cycle <- function(tb_ID,tb_raw) {
tb_IDtemp <-
tb_raw %>%
select(-c(dnaID)) %>%
filter(!is.na(pit),nchar(pit)==23) %>%
datepit_to_ID2(tb_ID) %>%
pivot_wider(id_cols = c(i_measurement,date,ID), names_from = i_pit, values_from=pit) %>% group_by(i_measurement) %>% summarise_all(function(x){x[!is.na(x)][1]}) %>%
pivot_longer(-c(date,ID,i_measurement),names_to="i_pit",values_to="pit") %>%
group_by(pit,i_pit,i_measurement) %>%
summarise(
date = ymd(date[1]),
ID = ID[1]
) %>%
filter(!is.na(ID),!is.na(pit)) %>%
group_by(ID,pit) %>%
summarise_all(function(x){x[!is.na(x)][1]}) %>%
ungroup()
}
# a table with where every record of a pit tag is one row
# incl. pit, date, i_measurement, dnaID (if any), pit_i
tf_pit_raw_long <<-
tb %>%
clean_ID_df(column_name="dnaID",prefix="Offsp",numLength=4,keep_name=T,remove_NA=F) %>%
rename(
pit.1=pit,
pit.2=pit_new,
pit.3=pit_second,
pit.4=pit_third
) %>%
mutate(i_measurement = row_number()) %>%
pivot_longer(c(pit.1,pit.2,pit.3,pit.4),names_to="i_pit",values_to="pit") %>%
filter(!is.na(pit),nchar(pit)==23) %>%
identify_fleeters() %>%
select(pit, date, dnaID, i_pit,i_measurement)
# finclip matches
# keep only rows with a DNAID - and translate DNAID to ID_original
tf_pit_DNAID <-
tf_pit_raw_long %>%
filter(!is.na(dnaID), !is.na(pit)) %>%
lookup::lookup(
from = finclip_matches,
by.x = "dnaID",
by.y = "ID",
what = "ID_original",
overwrite = T,
overwriteNA = F
)
tf_pit_ID <-
tf_pit_DNAID %>%
rename(ID=dnaID) %>%
cycle(tf_pit_raw_long) %>%
cycle(tf_pit_raw_long) %>%
cycle(tf_pit_raw_long) %>%
select(pit,date,ID)
return(tf_pit_ID)
}
tb_rednaid <- tb_fish %>%
filter(period==8 | period==9) %>%
filter(!is.na(dnaID), !is.na(pit)) %>%
select(pit,pit_new,pit_second,pit_third,dnaID,date) %>%
pivot_longer(c(pit,pit_new,pit_second,pit_third),names_to="pit_i",values_to="pit") %>%
select(-pit_i) %>%
filter(!is.na(pit))
tb_finclips <- read_delim("lookup - lammisalmon fin clip matches 5.csv")
tb_datepit <- write_datepit_file(tb_fish,tb_finclips)
View(tb_datepit)
tb_datepit2 <- tb_datepit %>%
datepit::re_dnaID(tb_rednaid)
View(tb_datepit2)
#' @title re_dnaID
#' @export
re_dnaID <- function(tb,tb_rednaid){
require(glue)
#1: Go through each row in the rednaID table
#2: for the given pit, and date:
#3: find the item in the datepit table with:
#4: the same pit
#5: that was registered the time before that datepit
#6 then, rename the ID in that item with the one from the current row in the rednaID table
tb <- tb %>% clean_ID_df(column_name="dnaID",prefix="Offsp",numLength=4,keep_name=T,remove_NA=F) %>%
for( i_row in 1:nrow(tb_rednaid) )
{
row <- tb_rednaid[i_row,]
pit <- row$pit
date <- row$date
dnaid <- row$dnaID
if(nrow( tb[tb$pit==pit & tb$date < date,])==0) message(glue("re-dnaid: pit {pit} for id {dnaid} and date {date} not found"))
tb[tb$pit==pit & tb$date < date,][1,][["ID"]] <- dnaid
}
return(tb)
}
tb_datepit2 <- tb_datepit %>%
datepit::re_dnaID(tb_rednaid)
View(tb_datepit2)
View(tb_datepit)
tb_datepit2 <- tb_datepit %>%
re_dnaID(tb_rednaid)
tb_rednaid <- tb_fish %>%
filter(period==8 | period==9) %>%
filter(!is.na(dnaID), !is.na(pit)) %>%
select(pit,pit_new,pit_second,pit_third,dnaID,date) %>%
pivot_longer(c(pit,pit_new,pit_second,pit_third),names_to="pit_i",values_to="pit") %>%
select(-pit_i) %>%
filter(!is.na(pit))
#' @title re_dnaID
#' @export
re_dnaID <- function(tb,tb_rednaid){
require(glue)
#1: Go through each row in the rednaID table
#2: for the given pit, and date:
#3: find the item in the datepit table with:
#4: the same pit
#5: that was registered the time before that datepit
#6 then, rename the ID in that item with the one from the current row in the rednaID table
tb <- tb %>% clean_ID_df(column_name="dnaID",prefix="Offsp",numLength=4,keep_name=T,remove_NA=F)
for( i_row in 1:nrow(tb_rednaid) )
{
row <- tb_rednaid[i_row,]
pit <- row$pit
date <- row$date
dnaid <- row$dnaID
if(nrow( tb[tb$pit==pit & tb$date < date,])==0) message(glue("re-dnaid: pit {pit} for id {dnaid} and date {date} not found"))
tb[tb$pit==pit & tb$date < date,][1,][["ID"]] <- dnaid
}
return(tb)
}
tb_rednaid <- tb_fish %>%
filter(period==8 | period==9) %>%
filter(!is.na(dnaID), !is.na(pit)) %>%
select(pit,pit_new,pit_second,pit_third,dnaID,date) %>%
pivot_longer(c(pit,pit_new,pit_second,pit_third),names_to="pit_i",values_to="pit") %>%
select(-pit_i) %>%
filter(!is.na(pit))
tb_datepit2 <- tb_datepit %>%
re_dnaID(tb_rednaid)
#' @title re_dnaID
#' @export
re_dnaID <- function(tb,tb_rednaid){
require(glue)
#1: Go through each row in the rednaID table
#2: for the given pit, and date:
#3: find the item in the datepit table with:
#4: the same pit
#5: that was registered the time before that datepit
#6 then, rename the ID in that item with the one from the current row in the rednaID table
tb <- tb %>% clean_ID_df(column_name="dnaID",prefix="Offsp",numLength=4,keep_name=T,remove_NA=F)
for( i_row in 1:nrow(tb_rednaid) )
{
row <- tb_rednaid[i_row,]
pit <- row$pit
date <- row$date
dnaid <- row$ID
if(nrow( tb[tb$pit==pit & tb$date < date,])==0) message(glue("re-dnaid: pit {pit} for id {dnaid} and date {date} not found"))
tb[tb$pit==pit & tb$date < date,][1,][["ID"]] <- dnaid
}
return(tb)
}
tb_datepit2 <- tb_datepit %>%
re_dnaID(tb_rednaid)
tb_rednaid <- tb_rednaid %>% clean_ID_df(column_name="dnaID",prefix="Offsp",numLength=4,keep_name=T,remove_NA=F)
for( i_row in 1:nrow(tb_rednaid) )
#' @title re_dnaID
#' @export
re_dnaID <- function(tb,tb_rednaid){
require(glue)
#1: Go through each row in the rednaID table
#2: for the given pit, and date:
#3: find the item in the datepit table with:
#4: the same pit
#5: that was registered the time before that datepit
#6 then, rename the ID in that item with the one from the current row in the rednaID table
tb_rednaid <- tb_rednaid %>% clean_ID_df(column_name="dnaID",prefix="Offsp",numLength=4,keep_name=T,remove_NA=F)
for( i_row in 1:nrow(tb_rednaid) )
{
row <- tb_rednaid[i_row,]
pit <- row$pit
date <- row$date
dnaid <- row$ID
if(nrow( tb[tb$pit==pit & tb$date < date,])==0) message(glue("re-dnaid: pit {pit} for id {dnaid} and date {date} not found"))
tb[tb$pit==pit & tb$date < date,][1,][["ID"]] <- dnaid
}
return(tb)
}
tb_datepit2 <- tb_datepit %>%
re_dnaID(tb_rednaid)
tb_datepit2 <- tb_datepit %>%
re_dnaID(tb_rednaid)
#' @title re_dnaID
#' @export
re_dnaID <- function(tb,tb_rednaid){
require(glue)
#1: Go through each row in the rednaID table
#2: for the given pit, and date:
#3: find the item in the datepit table with:
#4: the same pit
#5: that was registered the time before that datepit
#6 then, rename the ID in that item with the one from the current row in the rednaID table
tb_rednaid <- tb_rednaid %>% clean_ID_df(column_name="dnaID",prefix="Offsp",numLength=4,keep_name=T,remove_NA=F)
for( i_row in 1:nrow(tb_rednaid) )
{
row <- tb_rednaid[i_row,]
pit <- row$pit
date <- row$date
dnaid <- row$dnaID
if(nrow( tb[tb$pit==pit & tb$date < date,])==0) message(glue("re-dnaid: pit {pit} for id {dnaid} and date {date} not found"))
tb[tb$pit==pit & tb$date < date,][1,][["ID"]] <- dnaid
}
return(tb)
}
tb_datepit2 <- tb_datepit %>%
re_dnaID(tb_rednaid)
View(tb_datepit2)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
devtools::document()
setwd("C:/Users/asheimer/files/Jottacloud/github/datepit")
devtools::document()
devtools::install_github("eiriksen/eiriktools")
devtools::install_github("eiriksen/datepit")
devtools::install_github("eiriksen/lookup")
library(lubridate)
library(tidyverse)
library(lammidrive) # devtools::install_github("eiriksen/lammidrive")
library(datepit) # devtools::install_github("eiriksen/datepit")
library(eiriktools) # devtools::install_github("eiriksen/eiriktools")
library(glue)
library(lookup) # devtools::install_github("eiriksen/lookup")
.version="v2"
.month="Feb2022"
tb_fish <-
eiriktools::read_delim_multi(
list_filepaths =list.files(pattern="pheno-P\\d.*txt",recursive=T),
col_types = list(
id = col_integer(),
mature_female = col_character(),
dnaID = col_character(),
ID_fem = col_character(),
weight_postEggs = col_number(),
cataracts = col_number(),
mature = col_number(),
parr = col_number(),
kype = col_number()
)
) %>%
mutate(pit = toupper(pit)) %>%
lookup::lookup(
read_delim("data - eggs/2020 egg sampling data/data - lammi_mature_female_and_egg_2020.csv"),
by.x = c("pit","date"),
by.y = c("PIT","date"),
what = "PIT_new",
new_name = "pit_new"
) %>%
lookup::lookup(
read_delim("data - eggs/2021 egg sampling data/raw data - lammi_mature_female_and_egg_2021.csv"),
by.x = c("pit","date"),
by.y = c("pit","date_meas"),
what = "new_pit",
new_name = "pit_new"
)
setwd("C:/Users/asheimer/files/Drive/delte prosjekter/Lammi salmon/Project Big Tanks")
tb_fish <-
eiriktools::read_delim_multi(
list_filepaths =list.files(pattern="pheno-P\\d.*txt",recursive=T),
col_types = list(
id = col_integer(),
mature_female = col_character(),
dnaID = col_character(),
ID_fem = col_character(),
weight_postEggs = col_number(),
cataracts = col_number(),
mature = col_number(),
parr = col_number(),
kype = col_number()
)
) %>%
mutate(pit = toupper(pit)) %>%
lookup::lookup(
read_delim("data - eggs/2020 egg sampling data/data - lammi_mature_female_and_egg_2020.csv"),
by.x = c("pit","date"),
by.y = c("PIT","date"),
what = "PIT_new",
new_name = "pit_new"
) %>%
lookup::lookup(
read_delim("data - eggs/2021 egg sampling data/raw data - lammi_mature_female_and_egg_2021.csv"),
by.x = c("pit","date"),
by.y = c("pit","date_meas"),
what = "new_pit",
new_name = "pit_new"
)
tb_fish <-
eiriktools::read_delim_multi(
list_filepaths =list.files(pattern="pheno-P\\d.*txt",recursive=T),
col_types = list(
id = col_integer(),
mature_female = col_character(),
dnaID = col_character(),
ID_fem = col_character(),
weight_postEggs = col_number(),
cataracts = col_number(),
mature = col_number(),
parr = col_number(),
kype = col_number()
)
) %>%
mutate(pit = toupper(pit)) %>%
lookup::lookup(
read_delim("data - eggs/2020 egg sampling data/data - lammi_mature_female_and_egg_2020.csv"),
by.x = c("pit","date"),
by.y = c("PIT","date"),
what = "PIT_new",
new_name = "pit_new",
default=""
) %>%
lookup::lookup(
read_delim("data - eggs/2021 egg sampling data/raw data - lammi_mature_female_and_egg_2021.csv"),
by.x = c("pit","date"),
by.y = c("pit","date_meas"),
what = "new_pit",
new_name = "pit_new",
default=""
)
warnings()
tb_rednaid <- tb_fish %>%
filter(period==8 | period==9) %>%
filter(!is.na(dnaID), !is.na(pit)) %>%
select(pit,pit_new,pit_second,pit_third,dnaID,date) %>%
pivot_longer(c(pit,pit_new,pit_second,pit_third),names_to="pit_i",values_to="pit") %>%
select(-pit_i) %>%
filter(!is.na(pit))
tb_finclips <- read_delim("lookup - lammisalmon fin clip matches 5.csv")
tb_datepit <- datepit::write_datepit_file(tb_fish,tb_finclips) %>%
datepit::re_dnaID(tb_rednaid)
# devtools::install_github("eiriksen/datepit")
devtools::install_github("eiriksen/datepit")
library(datepit) # devtools::install_github("eiriksen/datepit")
View(write_datepit_file)
tb_datepit <- datepit::write_datepit_file(tb_fish,tb_finclips) %>%
datepit::re_dnaID(tb_rednaid)
library(lubridate)
library(tidyverse)
library(lammidrive) # devtools::install_github("eiriksen/lammidrive")
library(datepit) # devtools::install_github("eiriksen/datepit")
library(eiriktools) # devtools::install_github("eiriksen/eiriktools")
library(glue)
library(lookup) # devtools::install_github("eiriksen/lookup")
.version="v2"
.month="Feb2022"
tb_fish <-
eiriktools::read_delim_multi(
list_filepaths =list.files(pattern="pheno-P\\d.*txt",recursive=T),
col_types = list(
id = col_integer(),
mature_female = col_character(),
dnaID = col_character(),
ID_fem = col_character(),
weight_postEggs = col_number(),
cataracts = col_number(),
mature = col_number(),
parr = col_number(),
kype = col_number()
)
) %>%
mutate(pit = toupper(pit)) %>%
lookup::lookup(
read_delim("data - eggs/2020 egg sampling data/data - lammi_mature_female_and_egg_2020.csv"),
by.x = c("pit","date"),
by.y = c("PIT","date"),
what = "PIT_new",
new_name = "pit_new",
default=""
) %>%
lookup::lookup(
read_delim("data - eggs/2021 egg sampling data/raw data - lammi_mature_female_and_egg_2021.csv"),
by.x = c("pit","date"),
by.y = c("pit","date_meas"),
what = "new_pit",
new_name = "pit_new",
default=""
)
tb_rednaid <- tb_fish %>%
filter(period==8 | period==9) %>%
filter(!is.na(dnaID), !is.na(pit)) %>%
select(pit,pit_new,pit_second,pit_third,dnaID,date) %>%
pivot_longer(c(pit,pit_new,pit_second,pit_third),names_to="pit_i",values_to="pit") %>%
select(-pit_i) %>%
filter(!is.na(pit))
tb_finclips <- read_delim("lookup - lammisalmon fin clip matches 5.csv")
tb_datepit <- datepit::write_datepit_file(tb_fish,tb_finclips) %>%
datepit::re_dnaID(tb_rednaid)
write_delim(
tb_datepit,
glue::glue("lookup-pit-to-ID-{.month}-{.version}.txt")
)
parse_date_time("02-02-22")
lubridate::parse_date_time("02-02-22")
lubridate::parse_date_time("02-02-22",c("ymd","dmy"))
lubridate::parse_date_time("22-02-22",c("ymd","dmy"))
lubridate::parse_date_time(c("02-02-22","22-02-02"),c("ymd","dmy"))
typeof(ymd("02-02-22"))
typeof(lubridate::ymd("02-02-22"))
is.Date("02-02-22")
lubridate::is.Date("02-02-22")
lubridate::is.Date(dmy("02-02-22"))
lubridate::is.Date(lubridate::dmy("02-02-22"))
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
devtools::document()
setwd("G:/.shortcut-targets-by-id/1DXAvQYtRSS3l3vODEIEnoYkFD4GfDr9k/Lammi salmon/Project Big Tanks/9 - feb 2021")
library(tidyverse)
library(lubridate)
library(eiriktools) # devtools::install_github("eiriksen/eiriktools")
library(lookup) # devtools::install_github("eiriksen/lookup")
tb_fish <-
read_delim("raw data - feb 2021 - phenotype.txt") %>%
merge(
read_delim("raw data - feb 2021 - secondary.txt"),
by="measOrder"
) %>%
mutate(
weight = eiriktools::numextract(weight),
length = eiriktools::numextract(length),
mature_female = as.character(mature_female),
ID_fem = as.character(ID_fem),
date = dmy(date),
temp = recode(tank,
"T21"="hot","T22"="cold","T23"="hot","T24"="cold","T25"="hot","T26"="cold",
"T27"="hot", "T28"="cold","T29"="hot","T30"="cold","T31"="hot","T32"="cold"
),
feed = recode(tank,
"T21"="LF","T22"="HF","T23"="LF","T24"="LF","T25"="HF","T26"="LF",
"T27"="HF","T28"="LF","T29"="HF","T30"="HF","T31"="LF","T32"="HF"
)
) %>%
mutate(period=9) %>%
eiriktools::duplicates_cut_adv()
# Fill in some missing PIT-tags recovered by detective-work:
#3677 is A0000000900226000410851 (Later found not, 851 is still swimming in the tank)
#3698 is A0000000900226000994473
#3106 is A0000000900226000332726
#tb_fish[tb_fish$measOrder==3677,]$pit="A0000000900226000410851"
tb_fish[tb_fish$measOrder==3698,]$pit="A0000000900226000994473"
tb_fish[tb_fish$measOrder==3106,]$pit="A0000000900226000332726"
# this one needs a fin clip,
tb_fish[tb_fish$measOrder=="3677",]$dnaID = "need_finclip_1"
# row 2250 is not a fish
tb_fish <- tb_fish %>% filter(measOrder!=2250)
# duplicated pit tags (none)
eiriktools::duplicates_find(df_fish, by="pit")
# Remove PIT's that are not PIT's
tb_fish <- tb_fish %>% mutate(pit=ifelse(nchar(pit) != 23, NA,pit))
# save
tb_fish %>% write_delim("data-lammisalmon-pheno-P9-feb2020.txt")