Repository navigation
Expand file tree
/
Copy pathhsw_utils.R
More file actions
1932 lines (1644 loc) · 79.5 KB
/
Copy pathhsw_utils.R
File metadata and controls
1932 lines (1644 loc) · 79.5 KB
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
## PEDIGREE-RELATED FUNCTIONS SPECIFIC TO HS WEST
## written by Dr. Ben Johnson (bbjohnson@health.ucsd.edu)
## These are used in conjunction with utils.R, which is more general
## These functions are designed to adhere specifically to data and organizational
## conventions used in-house at the HS West colony. They are likely not useful
## to outside users, though utils.R is of general utility once pedigrees have already
## been properly formatted for use in breedail
library(readxl)
# convert HSW animal IDs to HSW access IDS
animalid_to_accessid <- function(id){
# remove letters & underscores, append '11' to the start of the new ID
clean_id <- gsub('[A-Za-z_]', '', id)
accessid <- paste0('11', clean_id)
return(accessid)
}
# convert HSW access IDs to HSW animal IDS
accessid_to_animalid <- function(id){
# remove letters and underscores
id <- as.character(id)
clean_id <- substr(id, start = 3, stop = nchar(id))
rat_no <- substr(clean_id, start = nchar(clean_id)-1, stop = nchar(clean_id))
pair_no <- substr(clean_id, start = nchar(clean_id)-3, stop = nchar(clean_id)-2)
gen_no <- unlist(strsplit(clean_id, pair_no))[1]
# construct the animal ID
animalid <- paste0('G', gen_no, '_', 'B', pair_no, '_', rat_no)
return(animalid)
}
# function to format a raw WFU pedigree into HSW gens 0-41 (54-95) for breedail
wfu_raw_to_hsw <- function(
ped,
wfu_map,
outdir)
{
wfu <- read_wfu_raw_ped(ped)
wfu_map <- read.csv(wfu_map)
# subset only the rows needed to identify breeders using breedail
keep_cols <- c('ID.F51','Dam.ID','Sire.ID','Sex','Generation','Transpondernumber','SW.ID','Dam.SW.ID','Sire.SW.ID')
wfu_keepcols <- setdiff(keep_cols, colnames(wfu))
if (length(wfu_keepcols)>0) {
keep_cols <- c('IDF51','DamID','SireID','Sex','Generation','Transpondernumber','SWID','DamSWID','SireSWID')
wfu_keepcols <- setdiff(keep_cols, colnames(wfu))
if (length(wfu_keepcols)>0) {
cat('Make sure the pedigree has the following columns:', keep_cols, '\n')
cat('File:', ped)
}
}
wfu <- wfu[,keep_cols]
# rename columns for compatibility with breedail
new_colnames <- c('id','dam','sire','sex','generation','rfid','animalid','dam_animalid','sire_animalid')
colnames(wfu) <- new_colnames
# add dam/sire RFID columns
wfu$dam_rfid <- sapply(wfu$dam, function(x) accessid_to_rfid(x, wfu_map))
wfu$sire_rfid <- sapply(wfu$sire, function(x) accessid_to_rfid(x, wfu_map))
# rearrange columns
col_order <- c('id','sire','dam','sex','generation','rfid','animalid',
'sire_rfid','dam_rfid','sire_animalid','dam_animalid')
wfu <- wfu[,col_order]
# format NA's for compatibility with breedail
for (col in 1:ncol(wfu)){
wfu[,col][which(wfu[,col]=='NA')] <- '?'
wfu[,col][which(is.na(wfu[,col]))] <- '?'
}
# remove extra 0's from generation numbers
wfu$generation <- as.character(wfu$generation)
wfu$generation <- gsub('00$','',wfu$generation)
# drop all generations after 40
# (HS West was founded with rats sent from gen042)
# (gen041 ped is only the parents of those rats sent in gen042)
wfu$generation <- as.numeric(wfu$generation)
hsw <- wfu[wfu$generation < 41,]
# add 54 to all generation numbers to equal HSW numbering convention
# (HSW generation counts are in TOTAL HS rat generations, not only WFU generations)
# hsw$wfu_generation <- as.numeric(hsw$generation)
hsw$generation <- as.numeric(hsw$generation) + 54
# # reorder columns
# gen_col <- which(colnames(hsw) == 'generation')
# new_col_order <- c(1:gen_col, ncol(hsw), (gen_col+1):ncol(wfu))
# hsw <- hsw[,new_col_order]
# split the pedigree by generation
ped_gens <- split(hsw, hsw$generation)
# save generations to separate files
if (dir.exists(outdir) == FALSE) {
dir.create(outdir, showWarnings = TRUE)
}
# exit the function for generations > 41 (whose length==0)
if (length(ped_gens)==0) {
return(NULL)
} else {
for (i in 1:length(ped_gens)){
ped <- ped_gens[[i]]
ped <- ped[order(ped$id),]
gen <- names(ped_gens)[[i]]
if (nchar(gen) == 1) {
gen <- paste0('00', gen)
}
if (nchar(gen) == 2) {
gen <- paste0('0', gen)
}
write.csv(ped, file.path(outdir, paste0('hsw_gen', gen, '.csv')),
row.names=F, quote=F, na='?')
}
}
}
# function to produce a raw pedigree file from an HSW assignment sheet
assignment_to_raw_ped <- function(
assignments, # assignment file csv or dataframe
hsw_map, # HSW ID map
outdir,
wfu_ss = NULL, # breeders shipped from WFU, already formatted from shipping sheets, csv or dataframe
wfu_map = NULL,
verbose = FALSE
)
{
if (is.character(assignments) && file.exists(assignments)) {
assignments <- read.csv(assignments)
}
if (is.character(hsw_map) && file.exists(hsw_map)) {
hsw_map <- read.csv(hsw_map)
}
ped_cols <- c('generation','rfid','animalid','accessid','sex','coatcolor','earpunch','dam','sire','comments')
gen <- assignments$generation[1]
ped <- assignments[assignments$assignment=='hsw_breeders',ped_cols]
n_hsw <- nrow(ped)
# add RFIDs, access IDs for dams & sires
names(ped)[which(names(ped)=='dam')] <- 'dam_animalid'
names(ped)[which(names(ped)=='sire')] <- 'sire_animalid'
if (verbose) {
missing_dams <- setdiff(ped$dam_animalid, hsw_map$animalid)
missing_sires <- setdiff(ped$sire_animalid, hsw_map$animalid)
cat(length(missing_dams), 'dam animalids missing from hsw_map \n')
if (length(missing_dams)>0) cat('\t',missing_dams, '\n')
cat(length(missing_sires), 'dam animalids missing from hsw_map \n')
if (length(missing_sires)>0) cat('\t',missing_sires, '\n')
}
ped$dam_rfid <- sapply(ped$dam_animalid, function(x) {
hsw_map$rfid[which(hsw_map$animalid==x)]})
ped$sire_rfid <- sapply(ped$sire_animalid, function(x) {
hsw_map$rfid[which(hsw_map$animalid==x)]})
ped$dam_accessid <- sapply(ped$dam_animalid, function(x) {
hsw_map$accessid[which(hsw_map$animalid==x)]})
ped$sire_accessid <- sapply(ped$sire_animalid, function(x) {
hsw_map$accessid[which(hsw_map$animalid==x)]})
# final column order to include in the raw pedigree
ped_cols <- c('generation','rfid','animalid','accessid','sex','coatcolor','earpunch',
'dam_rfid','sire_rfid','dam_animalid','sire_animalid','dam_accessid','sire_accessid','comments')
ped <- ped[,ped_cols]
# incorporate shipped WFU rats, if provided
if (!is.null(wfu_ss)) {
# read in the WFU ID map
if (is.null(wfu_map)) {
print('Please provide a WFU ID map to incorporate WFU dam/sire IDs into the pedigree')
return
}
if (is.character(wfu_map) && file.exists(wfu_map)) {
wfu_map <- read.csv(wfu_map, na.str=c('','NA','NaN','nan'))
}
# read in shipping sheet
wfu <- read_wfu_shipping_sheet(wfu_ss=wfu_ss)
n_wfu <- nrow(wfu)
keep_cols <- c('rfid','animalid','accessid','sex','coatcolor','earpunch','dob','dow','dam_accessid','sire_accessid')
wfu <- wfu[,keep_cols]
# add pedigree columns to shipping sheet
wfu$generation <- gen
wfu$comments <- NA
wfu$dam_rfid <- sapply(wfu$dam_accessid, function(x) {accessid_to_rfid(x, wfu_map=wfu_map)})
wfu$sire_rfid <- sapply(wfu$sire_accessid, function(x) {accessid_to_rfid(x, wfu_map=wfu_map)})
wfu$dam_animalid <- sapply(wfu$dam_accessid, function(x) {accessid_to_swid(x, wfu_map=wfu_map)})
wfu$sire_animalid <- sapply(wfu$sire_accessid, function(x) {accessid_to_swid(x, wfu_map=wfu_map)})
wfu <- wfu[,ped_cols]
if (verbose) {
cat('wfu:')
print(str(wfu))
}
# add WFU rats to the pedigree
ped <- rbind(ped, wfu)
}
ped <- ped[order(ped$animalid),]
outfile <- file.path(outdir, paste0('hsw_raw_gen', gen, '.csv'))
write.csv(ped, outfile, row.names=F, quote=F, na='')
cat('Raw pedigree written to', outfile, '\n')
if (is.null(wfu_ss)) {
cat('\t', 'Pedigree contains', n_hsw, 'rats \n')
} else {
cat('\t', 'Pedigree contains', n_hsw, 'HSW rats and', n_wfu, 'WFU rats \n')
}
# concatenate all raw pedigree files into an updated single file
all_peds <- list.files(outdir, full.names=T, pattern='hsw_raw_gen')
all_peds <- lapply(all_peds, function(x) {read.csv(x)})
complete_ped <- do.call(rbind, all_peds)
complete_ped <- complete_ped[order(complete_ped$generation, complete_ped$animalid),]
min_gen <- min(complete_ped$generation)
max_gen <- max(complete_ped$generation)
n_zeros <- nchar(max_gen) - nchar(min_gen)
if (n_zeros > 0) {min_gen <- paste0(rep('0',n_zeros),min_gen)}
datestamp <- format(Sys.time(),'%Y%m%d')
complete_ped_file <- file.path(outdir, paste0('hsw_raw_ped_complete_', min_gen, '_', max_gen, '.csv'))
write.csv(complete_ped, complete_ped_file, row.names=F, quote=F, na='')
cat('Complete raw HS West pedigree written to', complete_ped_file, '\n')
return(outfile)
}
# function to produce a breedail pedigree from a raw HSW pedigree
format_hsw_raw_ped <- function(
ped, # an HSW raw pedigree (csv), single- or multi-generation
wfu_map = NULL, # the WFU ID map (csv) with WFU access IDs and animal IDs (SW IDs)
hsw_map = NULL, # the HSW ID map (csv)
outdir = NULL, # directory in which to save the formatted pedigree file
return_df = FALSE, # whether to return the final df to the R console
verbose = FALSE)
{
if (file.exists(ped)) {
ped <- read.csv(ped, na.str=c('','NA','NaN','nan'))
} else if (class(ped) == 'data.frame') {
ped <- ped
}
if (!is.null(wfu_map)) {
if(file.exists(wfu_map)) {
wfu_map <- read.csv(wfu_map)
}
}
if (!is.null(hsw_map)) {
if(file.exists(hsw_map)) {
hsw_map <- read.csv(hsw_map)
}
}
# format the HSW pedigree for breedail
hsw_keep_cols <- c('rfid','generation','animalid','accessid','sex','dam_rfid','sire_rfid','dam_animalid','sire_animalid')
ped <- ped[,hsw_keep_cols]
# ped$wfu_generation <- NA
if (verbose) {
print('ped:')
print(str(ped))
print('hsw_map:')
print(str(hsw_map))
print('wfu_map:')
print(str(wfu_map))
}
# convert dam animal IDs based on HSW vs WFU format
ped$dam <- unlist(sapply(ped$dam_animalid, function(x) {
if (is.na(x)) return(NA)
if (grepl('^WHS', x)) {
swid_to_accessid(x, wfu_map)
} else if (grepl('^HSW', x)) {
hsw_map[hsw_map[['animalid']] == x,][['accessid']]
} else if (grepl('^G', x)) {
if (grepl('^G01', x)) {
hsw_map[hsw_map[['animalid']] == x,][['accessid']]
} else {
animalid_to_accessid(x)
}
} else {
NA
}
}, USE.NAMES = FALSE))
# convert sire IDs
ped$sire <- unlist(sapply(ped$sire_animalid, function(x) {
if (is.na(x)) return(NA)
if (grepl('^WHS', x)) {
swid_to_accessid(x, wfu_map)
} else if (grepl('^HSW', x)) {
hsw_map[hsw_map[['animalid']] == x,][['accessid']]
} else if (grepl('^G', x)) {
if (grepl('^G01', x)) {
hsw_map[hsw_map[['animalid']] == x,][['accessid']]
} else {
animalid_to_accessid(x)
}
} else {
NA
}
}, USE.NAMES = FALSE))
col_order <- c('accessid','sire','dam','sex','generation','rfid','animalid',
'sire_rfid','dam_rfid','sire_animalid','dam_animalid')
ped <- ped[,col_order]
names(ped)[which(names(ped)=='accessid')] <- 'id'
ped <- ped[order(as.numeric(ped$id)),]
if (!is.null(outdir)){
all_gens <- unique(ped$generation)
for (hsw_gen in all_gens) {
if (nchar(hsw_gen) == 1) {
gen <- paste0('00', hsw_gen)
} else if (nchar(hsw_gen) == 2) {
gen <- paste0('0', hsw_gen)
} else if (nchar(hsw_gen) == 3) {
gen <- hsw_gen
}
if (dir.exists(outdir) == FALSE) {
dir.create(outdir, showWarnings = TRUE)
}
gen_ped <- ped[ped$generation==hsw_gen,]
write.csv(gen_ped, file.path(outdir, paste0('hsw_gen', gen, '.csv')), row.names=F, quote=F, na='?')
}
}
if (return_df) {
return(ped)
}
}
# incorporate WFU IDs into the HSW pedigree following an animal transfer
wfu_into_hsw_gen <- function(hsw_raw, # an HSW assignment sheet (csv) w/ breeder assignments
wfu_ss, # a WFU shipping sheet (xlsx) with IDs to add to the current HSW generation
wfu_sheet, # the name of the excel sheet to use from the shipping sheet file
ss_gen, # the generation number being shipped from WFU
wfu_prev, # the raw WFU pedigree from the previous generation (prior to shipment)
outdir=NULL, # directory in which to save the formatted pedigree file
return_df=FALSE) # whether to return the final df to the R console
{
hsw <- read.csv(hsw_raw, na.str=c('','NA','NaN','nan'))
wfu <- as.data.frame(read_excel(wfu_ss, sheet=wfu_sheet, na=c('NA','','?')))
wfu <- wfu[!is.na(wfu[['Animal ID']]),]
# wfu_prev <- read.csv(wfu_prev, na.str=c('','NA','NaN','nan'))
hsw_gen <- hsw$generation[1]
# subset the HSW hsw to only breeders, depending on hsw format
if ('assignment' %in% colnames(hsw)) {
hsw <- hsw[hsw$assignment == 'hsw_breeders',]
} else if ('breeder' %in% colnames(hsw)) {
hsw <- hsw[hsw$breeder == 1,]
} else if ('hsw_breeders' %in% colnames(hsw)) {
hsw <- hsw[hsw$hsw_breeders == 1,]
} else {
cat('Cannot identify an assignment or breeders column to subset \n')
}
# format the HSW hsw for breedail
hsw_keep_cols <- c('rfid','generation','animalid','accessid','sex','dam','sire')
hsw <- hsw[,hsw_keep_cols]
colnames(hsw) <- c('rfid','generation','animalid','id','sex','dam_animalid','sire_animalid')
hsw$dam <- sapply(hsw$dam_animalid, animalid_to_accessid)
hsw$sire <- sapply(hsw$sire_animalid, animalid_to_accessid)
col_order <- c('id','sire','dam','sex','generation','rfid',
'animalid','sire_animalid','dam_animalid')
hsw <- hsw[,col_order]
hsw <- hsw[!is.na(hsw$id),]
# format the WFU shipping sheet
wfu_keep_cols <- c('Dam','Sire','Access ID','Animal ID','Transponder ID','Sex')
wfu <- wfu[,wfu_keep_cols]
colnames(wfu) <- c('dam','sire','id','animalid','rfid','sex')
wfu <- wfu[!is.na(wfu$id),]
wfu$dam <- gsub('_', '', wfu$dam)
wfu$sire <- gsub('_', '', wfu$sire)
wfu$id <- gsub('_', '', wfu$id)
wfu$dam_animalid <- sapply(wfu$dam, get_wfu_swid, wfu_df = wfu_prev)
wfu$sire_animalid <- sapply(wfu$sire, get_wfu_swid, wfu_df = wfu_prev)
wfu$generation <- hsw_gen
wfu <- wfu[,col_order]
wfu <- wfu[!is.na(wfu$id),]
# concatenate HSW and WFU pedigrees
out <- rbind(hsw, wfu)
out <- out[order(as.numeric(out$id)),]
if (!is.null(outdir)){
if (nchar(hsw_gen) == 1) {
gen <- paste0('00', hsw_gen)
} else if (nchar(hsw_gen) == 2) {
gen <- paste0('0', hsw_gen)
} else if (nchar(hsw_gen) == 3) {
gen <- hsw_gen
}
if (dir.exists(outdir) == FALSE) {
dir.create(outdir, showWarnings = TRUE)
}
write.csv(out, file.path(outdir, paste0('hsw_gen', gen, '.csv')), row.names=F, quote=F, na='?')
}
if (!is.logical(return_df)) {
stop("return_df should be a logical value")
}
if (return_df) {
return(out)
}
}
# format a breeder file to assist with pairing in the HSW colony
make_hsw_breeder_file <- function(
pairs, # R dataframe or path to csv, as output by select.breeders, dam/sire must be animal IDs
df, # colony dataframe or assignment file
wfu_ss=NULL, # WFU shipping sheet, if pairing with shipped WFU rats
outdir=NULL,
verbose=FALSE)
{
if (class(pairs) == 'data.frame') {
pairs <- pairs
} else if (class(pairs) == 'character') {
pairs <- read.csv(pairs, na.str=c('','NA','NaN','nan'))
} else if (sum(!names(pairs) %in% c('pairs','file')) == 0) {
pairs <- pairs$pairs
}
if (!is.null(wfu_ss)) {
wfu <- read_wfu_shipping_sheet(wfu_ss)
}
df <- read.csv(df, na.str=c('','NA','NaN','nan'))
gen <- as.numeric(df$generation[1])
pairs$generation <- gen
pairs$breederpair <- c(
paste0('HSW', gen + 1, '_B0', seq.int(1,9)),
paste0('HSW', gen + 1, '_B', seq.int(10,nrow(pairs)))
)
pairs$sire_animalid <- pairs$sire
pairs$dam_animalid <- pairs$dam
# setup columns for the pairing file
all_cols <- c('generation','breederpair','kinship','dam_rfid','dam_animalid','dam_earpunch',
'dam_coatcolor','dam_rack_num','dam_rack_pos','dam_wfu_cage',
'sire_rfid','sire_animalid','sire_earpunch','sire_coatcolor',
'sire_rack_num','sire_rack_pos','sire_wfu_cage')
for (col in setdiff(all_cols, names(pairs))) {
pairs[[col]] <- NA
}
for (i in 1:nrow(pairs)) {
dam <- pairs$dam[i]
if (verbose) cat('dam:', dam, '| ')
if (substr(dam,1,1)=='G') {
dam_df <- df[df$animalid==dam,]
pairs$dam_rfid[i] <- as.character(dam_df$rfid)
pairs$dam_earpunch[i] <- dam_df$earpunch
pairs$dam_coatcolor[i] <- dam_df$coatcolor
pairs$dam_rack_num[i] <- dam_df$rack_number
pairs$dam_rack_pos[i] <- dam_df$rack_position
} else if (substr(dam,1,1)=='H') {
if (is.null(wfu_ss)) {
cat('Please provide a WFU shipping sheet to include WFU IDs in the proposed pairs file \n')
return
}
dam_df <- wfu[wfu$animalid == dam,]
pairs$dam_rfid[i] <- dam_df$rfid
pairs$dam_earpunch[i] <- dam_df$earpunch
pairs$dam_coatcolor[i] <- dam_df$coatcolor
pairs$dam_rack_num[i] <- NA
pairs$dam_rack_pos[i] <- NA
pairs$dam_wfu_cage[i] <- dam_df$ship_box
}
sire <- pairs$sire[i]
if (verbose) cat('sire:', sire, '\n')
if (substr(sire,1,1)=='G') {
sire_df <- df[df$animalid==sire,]
pairs$sire_rfid[i] <- as.character(sire_df$rfid)
pairs$sire_earpunch[i] <- sire_df$earpunch
pairs$sire_coatcolor[i] <- sire_df$coatcolor
pairs$sire_rack_num[i] <- sire_df$rack_number
pairs$sire_rack_pos[i] <- sire_df$rack_position
} else if (substr(sire,1,1)=='H') {
sire_df <- wfu[wfu$animalid == sire,]
pairs$sire_rfid[i] <- sire_df$rfid
pairs$sire_earpunch[i] <- sire_df$earpunch
pairs$sire_coatcolor[i] <- sire_df$coatcolor
pairs$sire_rack_num[i] <- NA
pairs$sire_rack_pos[i] <- NA
pairs$sire_wfu_cage[i] <- sire_df$ship_box
}
}
if (is.null(wfu_ss)) {
col_order <- c('generation','breederpair','kinship','dam_rfid','dam_animalid','dam_earpunch',
'dam_coatcolor','dam_rack_num','dam_rack_pos','sire_rfid',
'sire_animalid','sire_earpunch','sire_coatcolor','sire_rack_num','sire_rack_pos')
} else {
col_order <- c('generation','breederpair','kinship','dam_rfid','dam_animalid','dam_earpunch',
'dam_coatcolor','dam_rack_num','dam_rack_pos','dam_wfu_cage',
'sire_rfid','sire_animalid','sire_earpunch','sire_coatcolor',
'sire_rack_num','sire_rack_pos','sire_wfu_cage')
}
# final formatting
pairs <- pairs[,col_order]
pairs$kinship <-format(round(pairs$kinship, 4), nsmall=4)
pairs$kinship <- sapply(pairs$kinship, function(x) paste0(x, paste0(rep(0,6-nchar(x)), collapse='')))
# final columns: two to fill/log as physical pairing happens, one with pairing notes
pairs$paired_dam <- 'NONE'
pairs$paired_sire <- 'NONE'
pairs$comments <- 'breederpair assigned using breedHS'
if (!is.null(outdir)) {
datestamp <- format(Sys.time(),'%Y%m%d')
outfile <- paste0('hsw_gen', gen, '_', gen+1, '_breeders_proposed_', datestamp, '.csv')
outfile <- file.path(outdir, outfile)
write.csv(pairs, outfile, row.names=F, quote=F, na='')
cat('Proposed breeder file written to', outfile, '\n')
}
return(list(pairs = pairs, file = outfile))
}
# count the parental breeder pairs that have been or currently need assignment/pairing
count_breeder_pairs <- function(
assignments, # assignment sheet with ALL assignments so far
breederpairs, # 'breederpair file' w/ all breeder pairings so far, or the output from make_breeder_file()
outdir=NULL)
{
if (class(assignments) == 'character') {
assignments <- read.csv(assignments, na.str=(c('','NA','NaN','nan')))
} else if (class(assignments) == 'data.frame') {
assignments <- assignments
}
if (class(breederpairs)=='character'){
breederpairs <- read.csv(breederpairs, na.str=c('','NA','NaN','nan'))
} else {
breederpairs <- breederpairs
}
assigned_breeders <- assignments[assignments$assignment=='hsw_breeders',]
# all animal IDs that have been assigned and paired so far
assigned <- assigned_breeders$animalid
paired <- c(breederpairs$dam_animalid, breederpairs$sire_animalid)
all_f <- as.matrix(table(assignments$breederpair, assignments$sex))[,1]
all_m <- as.matrix(table(assignments$breederpair, assignments$sex))[,2]
all_breederpairs <- data.frame(
breederpair = names(all_f),
females = all_f,
males = all_m)
no_females <- all_breederpairs[all_breederpairs$females==0,]$breederpair
no_males <- all_breederpairs[all_breederpairs$males==0,]$breederpair
paired.but.not.assigned <- setdiff(paired, assigned)
assigned.but.not.paired <- setdiff(assigned, paired)
if (length(paired.but.not.assigned) == 0) {paired.but.not.assigned <- NULL}
if (length(assigned.but.not.paired) == 0) {assigned.but.not.paired <- NULL}
# dataframe counting all breederpairs represented in assignments
f_assigned <- as.matrix(table(assigned_breeders$breederpair, assigned_breeders$sex))[,1]
m_assigned <- as.matrix(table(assigned_breeders$breederpair, assigned_breeders$sex))[,2]
assigned.breederpairs <- data.frame(
generation = assignments$generation[1]-1,
breederpair = names(f_assigned),
females_assigned = f_assigned,
males_assigned = m_assigned)
# dataframe counting all breederpairs represented in pairings
paired_assignments <- assigned_breeders[assigned_breeders$animalid %in% paired,]
if (nrow(paired_assignments) > 0) {
f_paired <- as.matrix(table(paired_assignments$breederpair, paired_assignments$sex))[,1]
m_paired <- as.matrix(table(paired_assignments$breederpair, paired_assignments$sex))[,2]
paired.breederpairs <- data.frame(
generation = assignments$generation[1],
breederpair = names(f_paired),
females_paired = f_paired,
males_paired = m_paired)
} else {paired.breederpairs <- NULL}
# dataframe counting bps lacking offspring assignments to hsw_breeders
breederpairs.need.assignment <-
assigned.breederpairs[(assigned.breederpairs$females_assigned < 1) |
(assigned.breederpairs$males_assigned < 1),]
if (nrow(breederpairs.need.assignment) > 0) {
# remove IDs from needs.pairing/needs.assignment lists if there were no M or F to begin with
needs_assignment <- c()
for (i in 1:nrow(breederpairs.need.assignment)) {
pair <- breederpairs.need.assignment$breederpair[i]
total_males <- all_breederpairs[all_breederpairs$breederpair==pair,]$males
total_females <- all_breederpairs[all_breederpairs$breederpair==pair,]$females
males_assigned <- assigned.breederpairs[assigned.breederpairs$breederpair==pair,]$males_assigned
females_assigned <- assigned.breederpairs[assigned.breederpairs$breederpair==pair,]$females_assigned
if ((total_males > 0) & (males_assigned < 1)) {
needs_assignment <- c(needs_assignment, pair)
}
if ((total_females > 0) & (females_assigned < 1)) {
needs_assignment <- c(needs_assignment, pair)
}
}
breederpairs.need.assignment <- breederpairs.need.assignment[needs_assignment,]
} else {
breederpairs.need.assignment <- NULL
}
# dataframe counting bps with offspring assigned to hsw_breeders that still need to be paired
if (!is.null(paired.breederpairs)) {
breederpairs.need.pairing <-
paired.breederpairs[(paired.breederpairs$females_paired < 1) |
(paired.breederpairs$males_paired < 1),]
} else {
breederpairs.need.pairing <- assigned.breederpairs
}
if (nrow(breederpairs.need.pairing) > 0) {
needs_pairing <- c()
for (i in 1:nrow(breederpairs.need.pairing)) {
pair <- breederpairs.need.pairing$breederpair[i]
total_males <- all_breederpairs[all_breederpairs$breederpair==pair,]$males
total_females <- all_breederpairs[all_breederpairs$breederpair==pair,]$females
males_paired <- paired.breederpairs[paired.breederpairs$breederpair==pair,]$males_paired
females_paired <- paired.breederpairs[paired.breederpairs$breederpair==pair,]$females_paired
if (!is.null(males_paired)) {
if ((total_males > 0) & (males_paired < 1)) {
needs_pairing <- c(needs_pairing, pair)
}
}
if (!is.null(females_paired)) {
if ((total_females > 0) & (!is.null(females_paired)) & (females_paired < 1)) {
needs_pairing <- c(needs_pairing, pair)
}
}
}
if (!is.null(needs_pairing)) {
breederpairs.need.pairing <- breederpairs.need.pairing[breederpairs.need.pairing$breederpair %in% needs_pairing,]
}
} else {
breederpairs.need.pairing <- NULL
}
out <- list(all_breederpairs = all_breederpairs,
paired_but_not_assigned = paired.but.not.assigned,
assigned_but_not_paired = assigned.but.not.paired,
assigned_breederpairs = assigned.breederpairs,
paired_breederpairs = paired.breederpairs,
breederpairs_need_assignment = breederpairs.need.assignment,
breederpairs_need_pairing = breederpairs.need.pairing)
if (!is.null(outdir)) {
datestamp <- format(Sys.time(),'%Y%m%d')
out_prefix <- paste0('bp_counts_', datestamp, '_')
paired_bp_file <- file.path(outdir, paste0(out_prefix, 'paired_breederpairs.csv'))
paired_not_assigned_file <- file.path(outdir, paste0(out_prefix, 'paired_but_not_assigned'))
assigned_not_paired_file <- file.path(outdir, paste0(out_prefix, 'assigned_but_not_paired'))
bp_need_pairing_file <- file.path(outdir, paste0(out_prefix, 'breederpairs_need_pairing.csv'))
write.csv(paired.breederpairs, paired_bp_file, row.names=F, quote=F, na='')
write.table(paired.but.not.assigned, paired_not_assigned_file, row.names=F, col.names=F, quote=F, na='')
write.table(assigned.but.not.paired, assigned_not_paired_file, row.names=F, col.names=F, quote=F, na='')
write.csv(breederpairs.need.pairing, bp_need_pairing_file, row.names=F, quote=F, na='')
}
return(out)
}
# get n next-best replacements for a given set of animal IDs, or the n best pairings from all available IDs
best_alt_pairs <- function(
pairs, # breederpair file
kinship, # kinship for all possible pairs, df or csv path
avail_ids, # vector or path to animalids still available for pairing
n_best = 4, # the number of next-best IDs to return per ID that needs replacing
ids_to_replace=NULL, # vector or path
ids_to_pair=NULL, # vector or path
save_k=FALSE, # whether to save a kinship matrix for all available rats
return_pairs=FALSE, # whether to return all identified pairs to the R console
outdir=NULL,
verbose = FALSE
) {
if (class(pairs)=='character') {
pairs <- read.csv(pairs)
}
if (class(kinship)=='character') {
kinship <- read.csv(kinship)
}
if (!is.vector(avail_ids)) {
stop('avail_ids must be a file path or character vector')
}
if (length(avail_ids)==1 && file.exists(avail_ids)) {
avail_ids <- readLines(avail_ids)
}
if (!is.null(ids_to_replace)) {
if (length(ids_to_replace)==1 && file.exists(ids_to_replace)) {
ids_to_replace <- readLines(ids_to_replace)
}
}
if (!is.null(ids_to_pair)) {
if (length(ids_to_pair)==1 && file.exists(ids_to_pair)) {
ids_to_pair <- readLines(ids_to_pair)
}
}
datestamp <- format(Sys.time(),'%Y%m%d')
if (verbose) cat('TROUBLESHOOTING best_alt_pairs() \n\n')
# get the breederpair combination for each possible pairing
kinship$dam_fam <- sapply(kinship$dam_fam, function(x) {
if (nchar(x)==1) return(paste0('0',x)) else return(x)})
kinship$sire_fam <- sapply(kinship$sire_fam, function(x) {
if (nchar(x)==1) return(paste0('0',x)) else return(x)})
kinship$combo <- paste0(kinship$dam_fam, '-', kinship$sire_fam)
# get breederpair for each paired dam or sire
paired_dams <- pairs$paired_dam
paired_sires <- pairs$paired_sire
proposed_dams <- pairs$dam_animalid
proposed_sires <- pairs$sire_animalid
use_dam_ids <- ifelse(paired_dams=='NONE', proposed_dams, paired_dams)
use_sire_ids <- ifelse(paired_sires=='NONE', proposed_sires, paired_sires)
paired_dam_fam <- sapply(use_dam_ids, function(x) sub('.*_B(\\d+)_.*', '\\1', x))
paired_sire_fam <- sapply(use_sire_ids, function(x) sub('.*_B(\\d+)_.*', '\\1', x))
# breederpair combo for all pairs
paired_combos <- paste0(paired_dam_fam, '-', paired_sire_fam)
reverse_pairs <- sapply(strsplit(paired_combos, '-'), function(pair) paste(rev(pair), collapse='-'))
paired_combos <- c(paired_combos, reverse_pairs)
# get all remaining novel pairing combos for all rats available for pairing
avail_int <- sapply(strsplit(avail_ids, "_"), tail, 1)
avail_sex <- sapply(avail_int, function(x) ifelse(as.integer(x) > 8, 'F', 'M'))
avail_f <- avail_ids[which(avail_sex=='F')]
avail_m <- avail_ids[which(avail_sex=='M')]
avail_f_fam <- unique(sapply(avail_f, function(x) sub('.*_B(\\d+)_.*', '\\1', x)))
avail_m_fam <- unique(sapply(avail_m, function(x) sub('.*_B(\\d+)_.*', '\\1', x)))
avail_new_combos <- expand.grid(avail_f_fam, avail_m_fam)
colnames(avail_new_combos) <- c('dam_fam','sire_fam')
avail_new_combos$combo <- paste0(avail_new_combos[,1], '-', avail_new_combos[,2])
avail_new_combos <- avail_new_combos[!avail_new_combos$combo %in% paired_combos,]
if (verbose) {
cat('avail_f:', avail_f, '\n')
cat('avail_m:', avail_m, '\n\n')
cat('avail_f_fam:', avail_f_fam, '\n')
cat('avail_m_fam:', avail_m_fam, '\n\n')
cat('avail_combos: \n')
print(str(avail_new_combos))
}
# kinship of all possible combos still available to pair
k_avail <- kinship[kinship$combo %in% avail_new_combos$combo,]
k_avail <- k_avail[k_avail$dam_fam != k_avail$sire_fam,]
k_avail <- k_avail[order(k_avail$dam_fam, k_avail$kinship),]
k_avail <- k_avail[,3:(ncol(k_avail)-1)] # drop specific animal IDs
# all potential pairings of available females with paired sires
avail_replacement_dams <- expand.grid(paired_sire_fam, avail_f_fam)
colnames(avail_replacement_dams) <- c('paired_sire_fam', 'avail_dam_fam')
avail_replacement_dams$combo <- paste0(avail_replacement_dams[,1], '-', avail_replacement_dams[,2])
k_avail_repl_dams <- kinship[kinship$combo %in% avail_replacement_dams$combo,]
# all potential pairings of available males with paired dams
avail_replacement_sires <- expand.grid(paired_dam_fam, avail_m_fam)
colnames(avail_replacement_sires) <- c('paired_dam_fam', 'avail_sire_fam')
avail_replacement_sires$combo <- paste0(avail_replacement_sires[,1], '-', avail_replacement_sires[,2])
k_avail_repl_sires <- kinship[kinship$combo %in% avail_replacement_sires$combo,]
if (save_k) {
outfile <- file.path(outdir, paste0('kinship_all_avail_fams_',datestamp,'.csv'))
write.csv(k_avail, outfile, row.names=F, quote=F, na='')
cat('\nKinship for all available pairings written to', outfile, '\n')
}
# identify best replacements for each ID to be replaced
if (!is.null(ids_to_replace)) {
replacements_list <- list()
ids_int <- sapply(ids_to_replace, function(x) tail(strsplit(x[1], "_")[[1]], 1))
ids_sex <- sapply(ids_int, function(x) ifelse(as.integer(x) > 8, 'F', 'M'))
f_idx <- which(ids_sex=='F')
m_idx <- which(ids_sex=='M')
f_to_replace <- ids_to_replace[f_idx]
m_to_replace <- ids_to_replace[m_idx]
# get the ids and families of mates proposed for the IDs that need replacement
lonely_dams <- pairs[pairs$sire_animalid %in% m_to_replace,]$dam_animalid
lonely_sires <- pairs[pairs$dam_animalid %in% f_to_replace,]$sire_animalid
lonely_f_fams <- sapply(lonely_dams, function(x) sub('.*_B(\\d+)_.*', '\\1', x))
lonely_m_fams <- sapply(lonely_sires, function(x) sub('.*_B(\\d+)_.*', '\\1', x))
fams_to_replace <- sapply(ids_to_replace, function(x) sub('.*_B(\\d+)_.*', '\\1', x))
f_fams_to_replace <- fams_to_replace[f_idx]
m_fams_to_replace <- fams_to_replace[m_idx]
# designate families to replace as 'available'
repl_avail_f_fam <- unique(c(avail_f_fam, f_fams_to_replace, lonely_f_fams))
repl_avail_m_fam <- unique(c(avail_m_fam, m_fams_to_replace, lonely_m_fams))
repl_avail_combos <- expand.grid(repl_avail_f_fam, repl_avail_m_fam)
colnames(repl_avail_combos) <- c('dam_fam','sire_fam')
repl_avail_combos$combo <- paste0(repl_avail_combos$dam_fam, '-', repl_avail_combos$sire_fam)
repl_k_avail <- kinship[kinship$combo %in% repl_avail_combos$combo,]
repl_k_avail <- repl_k_avail[!repl_k_avail$combo %in% paired_combos,]
if (verbose) {
cat('f_to_replace: \n')
cat('\t', f_to_replace, '\n\n')
cat('m_to_replace: \n')
cat('\t', m_to_replace, '\n\n')
cat('lonely_dams: \n')
cat('\t', lonely_dams, '\n\n')
cat('lonely_sires: \n')
cat('\t', lonely_sires, '\n\n')
cat('available families: \n')
cat('\t', 'repl_avail_f_fam:', repl_avail_f_fam, '\n')
cat('\t', 'repl_avail_m_fam:', repl_avail_m_fam, '\n\n')
}
for (id in ids_to_replace) {
id_int <- tail(strsplit(id[1], "_")[[1]], 1)
id_sex <- ifelse(as.integer(id_int) > 8, 'F', 'M')
id_fam <- sub('.*_B(\\d+)_.*', '\\1', id)
id_col <- ifelse(id_sex=='F', 'dam_animalid', 'sire_animalid')
# identify the proposed mate for the ID that needs replacing
mate_col <- setdiff(c('dam_animalid','sire_animalid'), id_col)
mate_id <- pairs[pairs[[id_col]]==id,][[mate_col]]
mate_sex <- setdiff(c('M','F'), id_sex)
mate_fam <- sub('.*_B(\\d+)_.*', '\\1', mate_id)
if (verbose) {
cat('id_to_replace:', id, '\n')
cat('mate_id:', mate_id, '\n')
}
# get the list of IDs that can replace the input ID
k_id_col <- ifelse(id_sex=='F', 'dam_fam', 'sire_fam')
k_mate_col <- setdiff(c('sire_fam','dam_fam'), k_id_col)
k_replacements <- repl_k_avail[repl_k_avail[[k_mate_col]] == mate_fam,]
k_replacements <- k_replacements[order(k_replacements$kinship),]
n_rows <- nrow(k_replacements)
if (n_rows > n_best) {
k_replacements <- k_replacements[1:n_best,]
n_rows <- n_best
}
# if no replacements are available without repeating an already-executed pairing,
# append the ID of the mate to ids_to_pair
if (n_rows == 0) {
cat('Could not find a novel replacement for', id, 'to be paired with', mate_id, '\n')
cat('Consider new pairings as an alternative \n')
}
replacements <- data.frame(
id_to_replace = rep(id, n_rows),
to_pair_with_id = rep(mate_id, n_rows),
use_fam = k_replacements[[k_id_col]],
kinship = k_replacements$kinship,
replaced_with = rep('NONE', n_rows),
breederpair = rep('NONE', n_rows),
comments = rep(NA, n_rows),
dif_fam = rep(1, n_rows)) # column only used for sorting
# add a first row using the same family as the replacement ID
rep_same_fam <- data.frame(
id_to_replace = id,
to_pair_with_id = mate_id,
use_fam = id_fam,
kinship = kinship[kinship[k_id_col]==id_fam & kinship[k_mate_col]==mate_fam,]$kinship,
replaced_with = 'NONE',
breederpair = 'NONE',
comments = NA,
dif_fam = 0) # column only used for sorting
replacements <- rbind(rep_same_fam, replacements)
replacements_list[[id]] <- replacements
} # end of ID loop
# combine replacement lists for all IDs that need replacing
replacements <- do.call(rbind, replacements_list)
replacements <- replacements[with(replacements, order(id_to_replace, kinship)),]
# replacements <- replacements[with(replacements, order(id_to_replace, dif_fam, kinship)),]
replacements <- replacements[,1:(ncol(replacements)-1)] #drop dif_fam column
out <- replacements
if (!is.null(outdir)) {
outfile <- file.path(outdir, paste0('replacement_pairs_n',n_best,'_',datestamp,'.csv'))
write.csv(replacements, outfile, row.names=F, quote=F, na='')
cat('\nBest replacement pairings written to', outfile, '\n')
}
} # end of ids_to_replace
# identify best alternative pairings for IDs in need of a new mate pair
if (!is.null(ids_to_pair)) {
alt_pair_list <- list()
ids_int <- sapply(ids_to_pair, function(x) tail(strsplit(x[1], "_")[[1]], 1))
ids_sex <- sapply(ids_int, function(x) ifelse(as.integer(x) > 8, 'F', 'M'))
f_idx <- which(ids_sex=='F')
m_idx <- which(ids_sex=='M')
fams_to_pair <- sapply(ids_to_pair, function(x) sub('.*_B(\\d+)_.*', '\\1', x))
f_fams_to_pair <- fams_to_pair[f_idx]
m_fams_to_pair <- fams_to_pair[m_idx]
alt_avail_f_fams <- unique(c(avail_f_fam, f_fams_to_pair))
alt_avail_m_fams <- unique(c(avail_m_fam, m_fams_to_pair))
alt_avail_combos <- (expand.grid(alt_avail_f_fams, alt_avail_m_fams))
colnames(alt_avail_combos) <- c('dam_fam','sire_fam')
alt_avail_combos$combo <- paste0(alt_avail_combos[,1], '-', alt_avail_combos[,2])
if (verbose) {
cat('alt_avail_f_fams:', alt_avail_f_fams, '\n')
cat('alt_avail_m_fams:', alt_avail_m_fams, '\n')
}
# # kinship of all possible combos still available to pair
# alt_k_avail <- kinship[kinship$combo %in% alt_avail_combos$combo,]
# alt_k_avail <- alt_k_avail[!alt_k_avail$combo %in% paired_combos,]
for (id in ids_to_pair) {
id_int <- tail(strsplit(id[1], "_")[[1]], 1)
id_sex <- ifelse(as.integer(id_int) > 8, 'F', 'M')
id_fam <- sub('.*_B(\\d+)_.*', '\\1', id)
id_col <- ifelse(id_sex=='F', 'dam_animalid', 'sire_animalid')
mate_col <- setdiff(c('dam_animalid', 'sire_animalid'), id_col)
mate_id <- pairs[pairs[id_col]==id,mate_col]
mate_fam <- sub('.*_B(\\d+)_.*', '\\1', mate_id)
# for all females to pair, identify alternative sires
if (id_sex == 'F') {
k_alt_pairs <- k_avail_repl_sires[k_avail_repl_sires[['dam_fam']] == id_fam,]
k_id_col <- 'dam_fam'
k_mate_col <- 'sire_fam'
} else if (id_sex == 'M') {
k_alt_pairs <- k_avail_repl_dams[k_avail_repl_dams[['sire_fam']] == id_fam,]
k_id_col <- 'sire_fam'
k_mate_col <- 'dam_fam'
}
k_alt_pairs <- k_alt_pairs[order(k_alt_pairs$kinship),]
n_rows <- nrow(k_alt_pairs)
# # get the list of IDs that can be paired with the input ID
# k_id_col <- ifelse(id_sex=='F', 'dam_fam', 'sire_fam')
# k_mate_col <- setdiff(c('dam_fam','sire_fam'), k_id_col)
# k_alt_pairs <- alt_k_avail[alt_k_avail[[k_id_col]]==id_fam,]
# k_alt_pairs <- k_alt_pairs[order(k_alt_pairs$kinship),]
# n_rows <- nrow(k_alt_pairs)
if (n_rows > n_best) {
k_alt_pairs <- k_alt_pairs[1:n_best,]
n_rows <- n_best
}
alt_pairs <- data.frame(
animalid = rep(id, n_rows),
use_fam = k_alt_pairs[[k_mate_col]],
kinship = k_alt_pairs$kinship,
paired_with = rep('NONE', n_rows),
breederpair = rep('NONE', n_rows),
comments = rep(NA, n_rows),
dif_fam = rep(1, n_rows)) # column used only for sorting