-
Notifications
You must be signed in to change notification settings - Fork 3
/
Compilation_provisioning_DataExtraction.R
2793 lines (2049 loc) · 118 KB
/
Compilation_provisioning_DataExtraction.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
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Malika IHLE [email protected]
# Compile provisioning data sparrows
# Extract data from excel files and DB
# Start : 21/12/2015
# last modif :22/03/2016
# commit: remove coordination measures
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{### Important remarks to read !
{### remarks about packages
## library 'xlsx' run with rjava, does not have enough memory to go through all excel files, hence the change to 'openxlsx', that can only read xlsx files (not xls)
## but to get colors out of old templates, need to have excel files as java object, hence use of package 'xlsx'.
## In addition, this require to convert all xls files to xlsx
## within package xlsx:
## if load workbook as a java object > header is index = 1 ; while with read.xlsx function > header has no index but the 1st row of data has index = 1
# in addition: load workbook supress lines without data, which read.xlsx does not do, so index not matching if long excel files with blank lines (needs to be deleted)
}
{### overview decisions taken
## excel files considered are only the one which have:
# an entry in tblDVD_XlsFiles (rk: some files have an entry in DB but are not included here: see 'FILES THAT SHOULD BE INCLUDED')
# OR that should have one and belong to 'missingDVDFilenames'...
# a situation = 4 in tblDVDInfo (i.e. only chicks)
# wrong = No in tblDVDInfo
# methodYN = either yes or no as no one figured out what it was about...
## nb of chicks considered in nest at time of recording
# DVDInfo nb of offspring
# mismatches with nb of chiks alive at time of recording bigger than 1 were SOMETIMES corrected
# for 2004-2005 nest checks were actually done after the recording and reported in DVD Info as Shinishi wanted to use those data, so its correct for those years
## last seen alive is the output of the DB query
#> needs to be updated when new pedigree imported !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
## decision still to be taken:
# should excluded early age chicks because can still be brooded ??
# should exclude recordings when the next nest visit revealed every chicks were dead ?
## LHT calculation
# prior residence accross year (does not start fresh each year)
## Nb Visit1 calculation:
# if OF is followed by IN and if time end and start are similar > this is merged into one visit
# this is the procedure for Malika's protocol, but the code has been applied to all files
# since time in decimal can be similar despite the bird leaving for 6 second, this can lead to (very few) 'mistakes'
# Nevertheless, in Shinishi's files, if a bird stay around the nest boxe for a few second before entering again, this was counted as two visits
# it still is in this code, although this is arguable.
}
{### annotations
## New and Old template mess up
# New means Issie's template
# Old means Shinichi's
# Malika means Malika's
## Mother Father Mum Dad Male Female mess up
# for me M always stands for Male, F for Female
#'Mum' and 'Dad' are sometimes used
## Time 0 1 2
# time 0 = absent from the nest box
# time 1 = time feeding (IN or OF)
# time 2 = time around the nest box
# time 02 = time not feeding = time not 1
}
{### forseen code breaks in the future
# when metadata for excel files (Tape length, Observer, Protocol, and Notes) will be imported to tblDVDInfo rather than tblParentalCare: SOLVED
}
}
rm(list = ls(all = TRUE))
TimeStart <- Sys.time()
{### packages, settings, working directories, connection to Access DB, sqlFetch and sqlQuery
{# packages
library(RODBC)
# library(openxlsx) # package openxlsx will be needed later in the code (after detaching the conflicting xlsx nevertheless needed to get colors for old templates)
# library(xlsx) # package xlsx will be needed later in the code (after detaching the conflicting openxlsx nevertheless needed at first to be faster/not crashing)
require(zoo)
}
{# input from excel files in dropbox
pathdropboxfolder <- "C:\\Users\\Malika\\\\Dropbox\\Sparrow Lundy\\Sparrow video files"
}
{# text files from input folder
input_folder <- "R_input"
sys_LastSeenAlive <- read.table(file= paste(input_folder,"sys_LastSeenAlive_20170324.txt", sep="/"), sep='\t', header=T) ## !!! to update when new pedigree !!! (and other corrections potentially)
sunrise <- read.table(file= paste(input_folder,"sunrise.txt", sep="/"), sep='\t', header=T)
pedigree <- read.table(file= paste(input_folder,"Pedigree_20160309.txt", sep="/"), sep='\t', header=T) ## !!! to update when new pedigree !!!
}
{# input from database
conDB= odbcConnectAccess("C:\\Users\\Malika\\Documents\\_Malika_Sheffield\\_CURRENT BACKUP\\db\\SparrowData.mdb")
# SqlFetch
tblDVD_XlsFiles <- sqlFetch(conDB, "tblDVD_XlsFiles")
tblDVD_XlsFiles <- tblDVD_XlsFiles[with(tblDVD_XlsFiles, order(tblDVD_XlsFiles$Filename)),]
tblBirdID <- sqlFetch(conDB, "tblBirdID")
tblParentalCare <- sqlFetch(conDB, "zzz_OldtblParentalCare")
tblBroodEvents <- sqlFetch(conDB, "tblBroodEvents")
tblBroods <- sqlFetch(conDB, "tblBroods")
tblAllCodes <- sqlFetch(conDB, "tblAllCodes")
tblDVDInfo <- sqlFetch(conDB, "tblDVDInfo")
tblNestVisits <- sqlFetch(conDB, "tblNestVisits")
# select video made when provisioning chick (situation = 4 ) # this is the query that I used to select which excel files to be considered, but it appears that some files in 2005 and all files in 2009 dont have an entry in tblXlsFiles...
tblDVD_XlsFilesALLDBINFO <- sqlQuery(conDB, "
SELECT tblDVD_XlsFiles.DVDRef, tblDVD_XlsFiles.Filename, tblDVDInfo.BroodRef, tblDVDInfo.Situation, tblDVDInfo.Deaths, tblDVDInfo.OffspringNo, tblDVDInfo.Age, tblDVDInfo.Wrong, tblDVDInfo.DVDdate, tblDVDInfo.DVDtime, tblDVDInfo.Weather, tblDVDInfo.Wind, tblDVDInfo.Notes, tblDVDInfo.TapeLength, zzz_OldtblParentalCare.EffectTime, zzz_OldtblParentalCare.Method, tblDVDInfo.Observer, zzz_OldtblParentalCare.MTime, zzz_OldtblParentalCare.FTime, zzz_OldtblParentalCare.ShareTime, zzz_OldtblParentalCare.MVisit1, zzz_OldtblParentalCare.FVisit1, zzz_OldtblParentalCare.MVisit2, zzz_OldtblParentalCare.FVisit2, zzz_OldtblParentalCare.MBout, zzz_OldtblParentalCare.FBout
FROM tblDVDInfo INNER JOIN (tblDVD_XlsFiles INNER JOIN zzz_OldtblParentalCare ON tblDVD_XlsFiles.DVDRef = zzz_OldtblParentalCare.DVDRef) ON (tblDVDInfo.DVDRef = zzz_OldtblParentalCare.DVDRef) AND (tblDVDInfo.DVDRef = tblDVD_XlsFiles.DVDRef)
WHERE (((tblDVDInfo.Situation)=4) AND ((tblDVDInfo.Wrong)=False));
")
# get the missing DVD Filenames (those in zzz_OldtblParentalCare but not in tblXlsFiles)
missingDVDFilenames <- sqlQuery(conDB, "
SELECT zzz_OldtblParentalCare.DVDRef, Year([DVDdate]) & '\\' & [DVDNumber] & '.xlsx' AS Filename, tblDVDInfo.BroodRef, tblDVDInfo.OffspringNo, tblDVDInfo.Age, tblDVDInfo.DVDdate, tblDVDInfo.DVDtime, tblDVDInfo.Notes,tblDVDInfo.TapeLength, zzz_OldtblParentalCare.EffectTime
FROM tblDVDInfo INNER JOIN (zzz_OldtblParentalCare LEFT JOIN tblDVD_XlsFiles ON zzz_OldtblParentalCare.[DVDRef] = tblDVD_XlsFiles.[DVDRef]) ON tblDVDInfo.DVDRef = zzz_OldtblParentalCare.DVDRef
WHERE (((tblDVDInfo.TapeLength) Is Not Null) AND ((tblDVD_XlsFiles.DVDRef) Is Null) AND ((tblDVDInfo.Situation)=4) AND ((tblDVDInfo.Wrong)=No));
")
# get the rearing brood for all birds
RearingBrood_allBirds <- sqlQuery(conDB, "
SELECT tblBirdID.BirdID, tblBirdID.Cohort, IIf([FosterBrood] Is Null,[BroodRef],[FosterBrood]) AS RearingBrood, tblBirdID.DeathDate, tblBirdID.LastStage, tblBirdID.DeathStatus
FROM tblBirdID LEFT JOIN tblFosterBroods ON tblBirdID.BirdID = tblFosterBroods.BirdID
WHERE (((tblBirdID.BroodRef) Is Not Null));
")
# get AvMass and AvTarsus per brood d12 (see annotated sql query 'LastMassTarsusChick')
{LastMassTarsusChick <- sqlQuery(conDB, "
SELECT usys_qRearingBrood.RearingBrood,
Avg(tblMeasurements.Mass) AS AvgMass,
Min(tblMeasurements.Mass) AS MinMass,
Max(tblMeasurements.Mass) AS MaxMass,
StDev(tblMeasurements.Mass) / Avg(tblMeasurements.Mass) AS CVMass,
Count(tblMeasurements.Mass) AS nMass,
Avg(tblMeasurements.Tarsus) AS AvgTarsus,
Count(tblMeasurements.Tarsus) AS nTarsus,
Count(tblMeasurements.Mass) - Count(tblMeasurements.Tarsus) AS nDiff,
Avg(usys_qRelativeChickMassClassesForCaptures.Age) AS AvgClassAge
FROM (((
(SELECT tblBirdID.BirdID, IIf([FosterBrood] Is Null,[BroodRef],[FosterBrood]) AS RearingBrood
FROM tblBirdID LEFT JOIN tblFosterBroods ON tblBirdID.BirdID = tblFosterBroods.BirdID
WHERE (((tblBirdID.BroodRef) Is Not Null)))
AS usys_qRearingBrood
INNER JOIN tblCaptures ON usys_qRearingBrood.BirdID = tblCaptures.BirdID)
INNER JOIN
(SELECT tblCaptures.CaptureRef,
14 AS MassClass,
First(tblCaptures.CaptureDate-[HatchDate])+1 AS Age
FROM (tblBirdID INNER JOIN tblCaptures ON tblBirdID.BirdID = tblCaptures.BirdID)
INNER JOIN
(
SELECT tblBirdID.BirdID,
usys_qBroodEggDate.LayDate AS EggDate,
usys_qBroodHatchDate.HatchDate,
usys_qBroodEggDate.DateEstimated AS EggDateEst,
IIf(usys_qBroodHatchDate.BroodRef Is Not Null,usys_qBroodHatchDate.DateEstimated,0) AS HatchDateEst
FROM ((tblBroods
LEFT JOIN
(SELECT tblBroods.BroodRef,
IIf(usys_qBroodTrueEggDate.LayDate,
usys_qBroodTrueEggDate.LayDate,
usys_qBroodEggDateFromFirstSeen.LayDate) AS LayDate,
IIf(usys_qBroodTrueEggDate.BroodRef,
usys_qBroodTrueEggDate.DateEstimated,True) AS DateEstimated
FROM (
(SELECT tblBroodEvents.BroodRef,
tblBroodEvents.EventDate AS LayDate,
tblBroodEvents.DateEstimated
FROM tblBroodEvents
WHERE (((tblBroodEvents.EventDate) Is Not Null)
AND ((tblBroodEvents.EventNumber)=0))
)
AS usys_qBroodTrueEggDate
RIGHT JOIN tblBroods ON usys_qBroodTrueEggDate.BroodRef = tblBroods.BroodRef)
LEFT JOIN
(SELECT tblBroodEvents.BroodRef,
IIf([usys_qBroodHatchDatesFromTable].[Hatchdate] Is Null,
[EventDate]-[EggCount],
[Hatchdate]-14) AS LayDate,
IIf([usys_qBroodHatchDatesFromTable].[Hatchdate] Is Null,
'EggCount','HatchDate') AS EstimateSource
FROM tblBroodEvents
LEFT JOIN
(SELECT tblBroodEvents.BroodRef,
tblBroodEvents.EventDate AS HatchDate,
tblBroodEvents.DateEstimated
FROM tblBroodEvents
WHERE (((tblBroodEvents.EventDate) Is Not Null)
AND ((tblBroodEvents.EventNumber)=1))
)
AS usys_qBroodHatchDatesFromTable
ON tblBroodEvents.BroodRef = usys_qBroodHatchDatesFromTable.BroodRef
WHERE (((tblBroodEvents.EventDate) Is Not Null)
AND ((tblBroodEvents.EventNumber)=4)
AND ((usys_qBroodHatchDatesFromTable.HatchDate)>=[EventDate]))
OR (((tblBroodEvents.EventNumber)=4)
AND ((tblBroodEvents.EggCount) Is Not Null))
)
AS usys_qBroodEggDateFromFirstSeen
ON tblBroods.BroodRef = usys_qBroodEggDateFromFirstSeen.BroodRef
WHERE (((IIf([usys_qBroodTrueEggDate].[LayDate],[usys_qBroodTrueEggDate].[LayDate],[usys_qBroodEggDateFromFirstSeen].[LayDate])) Is Not Null))
)
AS usys_qBroodEggDate
ON tblBroods.BroodRef = usys_qBroodEggDate.BroodRef)
LEFT JOIN
(
SELECT usys_qBroodsWithHatchlings.BroodRef,
IIf(usys_qBroodHatchDatesFromTable.HatchDate Is Not Null,
usys_qBroodHatchDatesFromTable.HatchDate,
usys_qBroodEggDate.LayDate+14) AS HatchDate,
usys_qBroodHatchDatesFromTable.HatchDate Is Null Or usys_qBroodHatchDatesFromTable.DateEstimated AS DateEstimated
FROM (
(SELECT DISTINCT tblBirdID.BroodRef,
Count(*) AS NoHatchlings
FROM tblBirdID
WHERE (((tblBirdID.LastStage)>1) AND ((tblBirdID.BroodRef) Is Not Null))
GROUP BY tblBirdID.BroodRef
)
AS usys_qBroodsWithHatchlings
LEFT JOIN
(SELECT tblBroodEvents.BroodRef,
tblBroodEvents.EventDate AS HatchDate,
tblBroodEvents.DateEstimated
FROM tblBroodEvents
WHERE (((tblBroodEvents.EventDate) Is Not Null) AND ((tblBroodEvents.EventNumber)=1))
)
AS usys_qBroodHatchDatesFromTable
ON usys_qBroodsWithHatchlings.BroodRef = usys_qBroodHatchDatesFromTable.BroodRef)
LEFT JOIN
(SELECT tblBroods.BroodRef,
IIf(usys_qBroodTrueEggDate.LayDate,
usys_qBroodTrueEggDate.LayDate,
usys_qBroodEggDateFromFirstSeen.LayDate) AS LayDate,
IIf(usys_qBroodTrueEggDate.BroodRef,
usys_qBroodTrueEggDate.DateEstimated,True) AS DateEstimated
FROM (
(SELECT tblBroodEvents.BroodRef,
tblBroodEvents.EventDate AS LayDate,
tblBroodEvents.DateEstimated
FROM tblBroodEvents
WHERE (((tblBroodEvents.EventDate) Is Not Null)
AND ((tblBroodEvents.EventNumber)=0))
)
AS usys_qBroodTrueEggDate
RIGHT JOIN tblBroods ON usys_qBroodTrueEggDate.BroodRef = tblBroods.BroodRef)
LEFT JOIN
(SELECT tblBroodEvents.BroodRef,
IIf([usys_qBroodHatchDatesFromTable].[Hatchdate] Is Null,
[EventDate]-[EggCount],
[Hatchdate]-14) AS LayDate,
IIf([usys_qBroodHatchDatesFromTable].[Hatchdate] Is Null,
'EggCount','HatchDate') AS EstimateSource
FROM tblBroodEvents
LEFT JOIN
(SELECT tblBroodEvents.BroodRef,
tblBroodEvents.EventDate AS HatchDate,
tblBroodEvents.DateEstimated
FROM tblBroodEvents
WHERE (((tblBroodEvents.EventDate) Is Not Null)
AND ((tblBroodEvents.EventNumber)=1))
)
AS usys_qBroodHatchDatesFromTable
ON tblBroodEvents.BroodRef = usys_qBroodHatchDatesFromTable.BroodRef
WHERE (((tblBroodEvents.EventDate) Is Not Null)
AND ((tblBroodEvents.EventNumber)=4)
AND ((usys_qBroodHatchDatesFromTable.HatchDate)>=[EventDate]))
OR (((tblBroodEvents.EventNumber)=4)
AND ((tblBroodEvents.EggCount) Is Not Null))
)
AS usys_qBroodEggDateFromFirstSeen
ON tblBroods.BroodRef = usys_qBroodEggDateFromFirstSeen.BroodRef
WHERE (((IIf([usys_qBroodTrueEggDate].[LayDate],[usys_qBroodTrueEggDate].[LayDate],[usys_qBroodEggDateFromFirstSeen].[LayDate])) Is Not Null))
) AS usys_qBroodEggDate
ON usys_qBroodsWithHatchlings.BroodRef = usys_qBroodEggDate.BroodRef
)
AS usys_qBroodHatchDate
ON tblBroods.BroodRef = usys_qBroodHatchDate.BroodRef)
INNER JOIN tblBirdID ON tblBroods.BroodRef = tblBirdID.BroodRef
WHERE (((tblBirdID.BroodRef) Is Not Null))
)
AS usys_qBirdEggHatchDates
ON tblCaptures.BirdID = usys_qBirdEggHatchDates.BirdID
WHERE (((([tblCaptures].[CaptureDate]-[HatchDate])+1)<=14)
AND ((tblCaptures.Stage)<3)
AND (((tblBirdID.DeathDate) Is Null Or (tblBirdID.DeathDate)<>[Capturedate])))
GROUP BY tblCaptures.CaptureRef
HAVING (((First(tblCaptures.CaptureDate-[HatchDate])+1)=11 Or
(First(tblCaptures.CaptureDate-[HatchDate])+1)=12 Or
(First(tblCaptures.CaptureDate-[HatchDate])+1)=13 Or
(First(tblCaptures.CaptureDate-[HatchDate])+1)=14))
)
AS usys_qRelativeChickMassClassesForCaptures
ON tblCaptures.CaptureRef = usys_qRelativeChickMassClassesForCaptures.CaptureRef)
INNER JOIN tblMeasurements ON tblCaptures.CaptureRef = tblMeasurements.CaptureRef)
WHERE ((usys_qRearingBrood.RearingBrood) Is Not Null)
GROUP BY usys_qRearingBrood.RearingBrood, usys_qRelativeChickMassClassesForCaptures.MassClass
HAVING (((Count(tblMeasurements.Mass))>0));
")}
# MassTarsusRearinBrood_allChicks (see annotated sql query 'LastMassTarsusChick')
# considering only chicks alive and measured between 11 and 14 days
{MassTarsusRearinBrood_allChicks <- sqlQuery(conDB, "
SELECT tblCaptures.BirdID AS ChickID,
usys_qRearingBrood.NatalBrood,
usys_qRearingBrood.RearingBrood,
usys_qRearingBrood.CrossFosteredYN,
Avg(tblMeasurements.Mass) AS AvgOfMass,
Avg(tblMeasurements.Tarsus) AS AvgOfTarsus,
Avg(usys_qRelativeChickMassClassesForCaptures.Age) AS AvgOfAge,
Count(usys_qRearingBrood.BirdID) AS nMeasures
FROM tblBirdID INNER JOIN
((
(SELECT tblBirdID.BirdID, IIf([FosterBrood] Is Null,[BroodRef],[FosterBrood]) AS RearingBrood,
tblBirdID.BroodRef AS NatalBrood, IIf([FosterBrood] Is Null,0,1) AS CrossFosteredYN
FROM tblBirdID LEFT JOIN tblFosterBroods ON tblBirdID.BirdID = tblFosterBroods.BirdID
WHERE (((tblBirdID.BroodRef) Is Not Null)))
AS usys_qRearingBrood
INNER JOIN (
(SELECT First(tblCaptures.CaptureRef) AS CaptureRef,
14 AS MassClass,
First(tblCaptures.CaptureDate-[HatchDate])+1 AS Age
FROM (tblBirdID INNER JOIN tblCaptures ON tblBirdID.BirdID = tblCaptures.BirdID)
INNER JOIN
(
SELECT tblBirdID.BirdID,
usys_qBroodEggDate.LayDate AS EggDate,
usys_qBroodHatchDate.HatchDate,
usys_qBroodEggDate.DateEstimated AS EggDateEst,
IIf(usys_qBroodHatchDate.BroodRef Is Not Null,usys_qBroodHatchDate.DateEstimated,0) AS HatchDateEst
FROM ((tblBroods
LEFT JOIN
(SELECT tblBroods.BroodRef,
IIf(usys_qBroodTrueEggDate.LayDate,
usys_qBroodTrueEggDate.LayDate,
usys_qBroodEggDateFromFirstSeen.LayDate) AS LayDate,
IIf(usys_qBroodTrueEggDate.BroodRef,
usys_qBroodTrueEggDate.DateEstimated,True) AS DateEstimated
FROM (
(SELECT tblBroodEvents.BroodRef,
tblBroodEvents.EventDate AS LayDate,
tblBroodEvents.DateEstimated
FROM tblBroodEvents
WHERE (((tblBroodEvents.EventDate) Is Not Null)
AND ((tblBroodEvents.EventNumber)=0))
)
AS usys_qBroodTrueEggDate
RIGHT JOIN tblBroods ON usys_qBroodTrueEggDate.BroodRef = tblBroods.BroodRef)
LEFT JOIN
(SELECT tblBroodEvents.BroodRef,
IIf([usys_qBroodHatchDatesFromTable].[Hatchdate] Is Null,
[EventDate]-[EggCount],
[Hatchdate]-14) AS LayDate,
IIf([usys_qBroodHatchDatesFromTable].[Hatchdate] Is Null,
'EggCount','HatchDate') AS EstimateSource
FROM tblBroodEvents
LEFT JOIN
(SELECT tblBroodEvents.BroodRef,
tblBroodEvents.EventDate AS HatchDate,
tblBroodEvents.DateEstimated
FROM tblBroodEvents
WHERE (((tblBroodEvents.EventDate) Is Not Null)
AND ((tblBroodEvents.EventNumber)=1))
)
AS usys_qBroodHatchDatesFromTable
ON tblBroodEvents.BroodRef = usys_qBroodHatchDatesFromTable.BroodRef
WHERE (((tblBroodEvents.EventDate) Is Not Null)
AND ((tblBroodEvents.EventNumber)=4)
AND ((usys_qBroodHatchDatesFromTable.HatchDate)>=[EventDate]))
OR (((tblBroodEvents.EventNumber)=4)
AND ((tblBroodEvents.EggCount) Is Not Null))
)
AS usys_qBroodEggDateFromFirstSeen
ON tblBroods.BroodRef = usys_qBroodEggDateFromFirstSeen.BroodRef
WHERE (((IIf([usys_qBroodTrueEggDate].[LayDate],[usys_qBroodTrueEggDate].[LayDate],[usys_qBroodEggDateFromFirstSeen].[LayDate])) Is Not Null))
)
AS usys_qBroodEggDate
ON tblBroods.BroodRef = usys_qBroodEggDate.BroodRef)
LEFT JOIN
(
SELECT usys_qBroodsWithHatchlings.BroodRef,
IIf(usys_qBroodHatchDatesFromTable.HatchDate Is Not Null,
usys_qBroodHatchDatesFromTable.HatchDate,
usys_qBroodEggDate.LayDate+14) AS HatchDate,
usys_qBroodHatchDatesFromTable.HatchDate Is Null Or usys_qBroodHatchDatesFromTable.DateEstimated AS DateEstimated
FROM (
(SELECT DISTINCT tblBirdID.BroodRef,
Count(*) AS NoHatchlings
FROM tblBirdID
WHERE (((tblBirdID.LastStage)>1) AND ((tblBirdID.BroodRef) Is Not Null))
GROUP BY tblBirdID.BroodRef
)
AS usys_qBroodsWithHatchlings
LEFT JOIN
(SELECT tblBroodEvents.BroodRef,
tblBroodEvents.EventDate AS HatchDate,
tblBroodEvents.DateEstimated
FROM tblBroodEvents
WHERE (((tblBroodEvents.EventDate) Is Not Null) AND ((tblBroodEvents.EventNumber)=1))
)
AS usys_qBroodHatchDatesFromTable
ON usys_qBroodsWithHatchlings.BroodRef = usys_qBroodHatchDatesFromTable.BroodRef)
LEFT JOIN
(SELECT tblBroods.BroodRef,
IIf(usys_qBroodTrueEggDate.LayDate,
usys_qBroodTrueEggDate.LayDate,
usys_qBroodEggDateFromFirstSeen.LayDate) AS LayDate,
IIf(usys_qBroodTrueEggDate.BroodRef,
usys_qBroodTrueEggDate.DateEstimated,True) AS DateEstimated
FROM (
(SELECT tblBroodEvents.BroodRef,
tblBroodEvents.EventDate AS LayDate,
tblBroodEvents.DateEstimated
FROM tblBroodEvents
WHERE (((tblBroodEvents.EventDate) Is Not Null)
AND ((tblBroodEvents.EventNumber)=0))
)
AS usys_qBroodTrueEggDate
RIGHT JOIN tblBroods ON usys_qBroodTrueEggDate.BroodRef = tblBroods.BroodRef)
LEFT JOIN
(SELECT tblBroodEvents.BroodRef,
IIf([usys_qBroodHatchDatesFromTable].[Hatchdate] Is Null,
[EventDate]-[EggCount],
[Hatchdate]-14) AS LayDate,
IIf([usys_qBroodHatchDatesFromTable].[Hatchdate] Is Null,
'EggCount','HatchDate') AS EstimateSource
FROM tblBroodEvents
LEFT JOIN
(SELECT tblBroodEvents.BroodRef,
tblBroodEvents.EventDate AS HatchDate,
tblBroodEvents.DateEstimated
FROM tblBroodEvents
WHERE (((tblBroodEvents.EventDate) Is Not Null)
AND ((tblBroodEvents.EventNumber)=1))
)
AS usys_qBroodHatchDatesFromTable
ON tblBroodEvents.BroodRef = usys_qBroodHatchDatesFromTable.BroodRef
WHERE (((tblBroodEvents.EventDate) Is Not Null)
AND ((tblBroodEvents.EventNumber)=4)
AND ((usys_qBroodHatchDatesFromTable.HatchDate)>=[EventDate]))
OR (((tblBroodEvents.EventNumber)=4)
AND ((tblBroodEvents.EggCount) Is Not Null))
)
AS usys_qBroodEggDateFromFirstSeen
ON tblBroods.BroodRef = usys_qBroodEggDateFromFirstSeen.BroodRef
WHERE (((IIf([usys_qBroodTrueEggDate].[LayDate],[usys_qBroodTrueEggDate].[LayDate],[usys_qBroodEggDateFromFirstSeen].[LayDate])) Is Not Null))
) AS usys_qBroodEggDate
ON usys_qBroodsWithHatchlings.BroodRef = usys_qBroodEggDate.BroodRef
)
AS usys_qBroodHatchDate
ON tblBroods.BroodRef = usys_qBroodHatchDate.BroodRef)
INNER JOIN tblBirdID ON tblBroods.BroodRef = tblBirdID.BroodRef
WHERE (((tblBirdID.BroodRef) Is Not Null))
)
AS usys_qBirdEggHatchDates ON tblCaptures.BirdID = usys_qBirdEggHatchDates.BirdID
WHERE (((([tblCaptures].[CaptureDate]-[HatchDate])+1)<=14)
AND ((tblCaptures.Stage)<3)
AND (((tblBirdID.DeathDate) Is Null Or (tblBirdID.DeathDate)<>[Capturedate])))
GROUP BY tblCaptures.CaptureRef
HAVING (((First(tblCaptures.CaptureDate-[HatchDate])+1)=11 Or
(First(tblCaptures.CaptureDate-[HatchDate])+1)=12 Or
(First(tblCaptures.CaptureDate-[HatchDate])+1)=13 Or
(First(tblCaptures.CaptureDate-[HatchDate])+1)=14))
) AS usys_qRelativeChickMassClassesForCaptures
INNER JOIN tblCaptures ON usys_qRelativeChickMassClassesForCaptures.CaptureRef = tblCaptures.CaptureRef) ON usys_qRearingBrood.BirdID = tblCaptures.BirdID)
INNER JOIN tblMeasurements ON tblCaptures.CaptureRef = tblMeasurements.CaptureRef) ON tblBirdID.BirdID = tblCaptures.BirdID
WHERE (((usys_qRelativeChickMassClassesForCaptures.MassClass)=14) AND ((usys_qRearingBrood.RearingBrood) Is Not Null) AND ((tblMeasurements.Mass)>0) AND ((tblBirdID.DeathDate) Is Null Or (tblBirdID.DeathDate)<>[CaptureDate]))
GROUP BY tblCaptures.BirdID, usys_qRearingBrood.RearingBrood, usys_qRearingBrood.NatalBrood,usys_qRearingBrood.CrossFosteredYN, tblCaptures.CaptureDate;
")
}
# taking the first measurement for chicks measured twice in the right range of age
# (even though likely measured when tarsus forgotten the first time but here put priority on getting chicks with the same age...)
MassTarsusRearinBrood_allChicks[!is.na(MassTarsusRearinBrood_allChicks$ChickID) & MassTarsusRearinBrood_allChicks$ChickID == '4764',]
MassTarsusRearinBrood_allChicks <- MassTarsusRearinBrood_allChicks[order(MassTarsusRearinBrood_allChicks$AvgOfAge), ]
MassTarsusRearinBrood_allChicks <- MassTarsusRearinBrood_allChicks[!duplicated(MassTarsusRearinBrood_allChicks$ChickID),]
tblChicks <- merge(x=MassTarsusRearinBrood_allChicks, y= pedigree[,c("id","dam","sire")], all.x=TRUE, by.x="ChickID", by.y = "id")
# get the hatching date for each brood
{usys_qBroodHatchDate <- sqlQuery(conDB, "
SELECT usys_qBroodsWithHatchlings.BroodRef,
IIf(usys_qBroodHatchDatesFromTable.HatchDate Is Not Null,
usys_qBroodHatchDatesFromTable.HatchDate,
usys_qBroodEggDate.LayDate+14) AS HatchDate,
usys_qBroodHatchDatesFromTable.HatchDate Is Null Or usys_qBroodHatchDatesFromTable.DateEstimated AS DateEstimated
FROM (
(SELECT DISTINCT tblBirdID.BroodRef,
Count(*) AS NoHatchlings
FROM tblBirdID
WHERE (((tblBirdID.LastStage)>1) AND ((tblBirdID.BroodRef) Is Not Null))
GROUP BY tblBirdID.BroodRef
)
AS usys_qBroodsWithHatchlings
LEFT JOIN
(SELECT tblBroodEvents.BroodRef,
tblBroodEvents.EventDate AS HatchDate,
tblBroodEvents.DateEstimated
FROM tblBroodEvents
WHERE (((tblBroodEvents.EventDate) Is Not Null) AND ((tblBroodEvents.EventNumber)=1))
)
AS usys_qBroodHatchDatesFromTable
ON usys_qBroodsWithHatchlings.BroodRef = usys_qBroodHatchDatesFromTable.BroodRef)
LEFT JOIN
(SELECT tblBroods.BroodRef,
IIf(usys_qBroodTrueEggDate.LayDate,
usys_qBroodTrueEggDate.LayDate,
usys_qBroodEggDateFromFirstSeen.LayDate) AS LayDate,
IIf(usys_qBroodTrueEggDate.BroodRef,
usys_qBroodTrueEggDate.DateEstimated,True) AS DateEstimated
FROM (
(SELECT tblBroodEvents.BroodRef,
tblBroodEvents.EventDate AS LayDate,
tblBroodEvents.DateEstimated
FROM tblBroodEvents
WHERE (((tblBroodEvents.EventDate) Is Not Null)
AND ((tblBroodEvents.EventNumber)=0))
)
AS usys_qBroodTrueEggDate
RIGHT JOIN tblBroods ON usys_qBroodTrueEggDate.BroodRef = tblBroods.BroodRef)
LEFT JOIN
(SELECT tblBroodEvents.BroodRef,
IIf([usys_qBroodHatchDatesFromTable].[Hatchdate] Is Null,
[EventDate]-[EggCount],
[Hatchdate]-14) AS LayDate,
IIf([usys_qBroodHatchDatesFromTable].[Hatchdate] Is Null,
'EggCount','HatchDate') AS EstimateSource
FROM tblBroodEvents
LEFT JOIN
(SELECT tblBroodEvents.BroodRef,
tblBroodEvents.EventDate AS HatchDate,
tblBroodEvents.DateEstimated
FROM tblBroodEvents
WHERE (((tblBroodEvents.EventDate) Is Not Null)
AND ((tblBroodEvents.EventNumber)=1))
)
AS usys_qBroodHatchDatesFromTable
ON tblBroodEvents.BroodRef = usys_qBroodHatchDatesFromTable.BroodRef
WHERE (((tblBroodEvents.EventDate) Is Not Null)
AND ((tblBroodEvents.EventNumber)=4)
AND ((usys_qBroodHatchDatesFromTable.HatchDate)>=[EventDate]))
OR (((tblBroodEvents.EventNumber)=4)
AND ((tblBroodEvents.EggCount) Is Not Null))
)
AS usys_qBroodEggDateFromFirstSeen
ON tblBroods.BroodRef = usys_qBroodEggDateFromFirstSeen.BroodRef
WHERE (((IIf([usys_qBroodTrueEggDate].[LayDate],[usys_qBroodTrueEggDate].[LayDate],[usys_qBroodEggDateFromFirstSeen].[LayDate])) Is Not Null))
) AS usys_qBroodEggDate
ON usys_qBroodsWithHatchlings.BroodRef = usys_qBroodEggDate.BroodRef;
")}
{usys_qBroodEggDate <- sqlQuery(conDB, "
SELECT tblBroods.BroodRef,
IIf(usys_qBroodTrueEggDate.LayDate,
usys_qBroodTrueEggDate.LayDate,
usys_qBroodEggDateFromFirstSeen.LayDate) AS LayDate,
IIf(usys_qBroodTrueEggDate.BroodRef,
usys_qBroodTrueEggDate.DateEstimated,True) AS DateEstimated
FROM (
(SELECT tblBroodEvents.BroodRef,
tblBroodEvents.EventDate AS LayDate,
tblBroodEvents.DateEstimated
FROM tblBroodEvents
WHERE (((tblBroodEvents.EventDate) Is Not Null)
AND ((tblBroodEvents.EventNumber)=0))
)
AS usys_qBroodTrueEggDate
RIGHT JOIN tblBroods ON usys_qBroodTrueEggDate.BroodRef = tblBroods.BroodRef)
LEFT JOIN
(SELECT tblBroodEvents.BroodRef,
IIf([usys_qBroodHatchDatesFromTable].[Hatchdate] Is Null,
[EventDate]-[EggCount],
[Hatchdate]-14) AS LayDate,
IIf([usys_qBroodHatchDatesFromTable].[Hatchdate] Is Null,
'EggCount','HatchDate') AS EstimateSource
FROM tblBroodEvents
LEFT JOIN
(SELECT tblBroodEvents.BroodRef,
tblBroodEvents.EventDate AS HatchDate,
tblBroodEvents.DateEstimated
FROM tblBroodEvents
WHERE (((tblBroodEvents.EventDate) Is Not Null)
AND ((tblBroodEvents.EventNumber)=1))
)
AS usys_qBroodHatchDatesFromTable
ON tblBroodEvents.BroodRef = usys_qBroodHatchDatesFromTable.BroodRef
WHERE (((tblBroodEvents.EventDate) Is Not Null)
AND ((tblBroodEvents.EventNumber)=4)
AND ((usys_qBroodHatchDatesFromTable.HatchDate)>=[EventDate]))
OR (((tblBroodEvents.EventNumber)=4)
AND ((tblBroodEvents.EggCount) Is Not Null))
)
AS usys_qBroodEggDateFromFirstSeen
ON tblBroods.BroodRef = usys_qBroodEggDateFromFirstSeen.BroodRef
WHERE (((IIf([usys_qBroodTrueEggDate].[LayDate],[usys_qBroodTrueEggDate].[LayDate],[usys_qBroodEggDateFromFirstSeen].[LayDate])) Is Not Null));
")}
# BreedingYear for all brood
BreedingYear <- data.frame(c("Z",LETTERS[1:25]), 2000:2025)
colnames(BreedingYear) <- c('Letter','BreedingYear' )
# table with all chicks and natal brood (for analysis of chick survival) # added 20190207 corrected 20190718 (added criteria laststage >0 to remove unhatched eggs)
tblChicks_All <- sqlQuery(conDB, "SELECT tblBirdID.BirdID, tblBirdID.BroodRef AS NatalBroodID, IIf([FosterBrood] Is Null,[BroodRef],[FosterBrood]) AS BroodID, IIf([tblAllCodes]![BirdID]>0,1,0) AS RingedYN, tblBirdID.LastStage, tblBirdID.DeathDate
FROM (tblBirdID LEFT JOIN tblAllCodes ON tblBirdID.BirdID = tblAllCodes.BirdID) LEFT JOIN tblFosterBroods ON tblBirdID.BirdID = tblFosterBroods.BirdID
GROUP BY tblBirdID.BirdID, tblBirdID.BroodRef, IIf([FosterBrood] Is Null,[BroodRef],[FosterBrood]), IIf([tblAllCodes]![BirdID]>0,1,0), tblBirdID.LastStage, tblBirdID.DeathDate
HAVING (((tblBirdID.BroodRef) Is Not Null) AND ((tblBirdID.LastStage)>0));
")
names(tblChicks_All)[names(tblChicks_All) == 'BroodID'] <- 'BroodRef'
tblChicks_All$CrossFosteredYN[tblChicks_All$NatalBroodID != tblChicks_All$BroodRef] <- 1
tblChicks_All$CrossFosteredYN[tblChicks_All$NatalBroodID == tblChicks_All$BroodRef] <- 0
tblChicks_All <- merge(tblChicks_All,sys_LastSeenAlive[,c('BirdID', 'LastLiveRecord')], by='BirdID', all.x=TRUE)
tblChicks_All <- merge(tblChicks_All,usys_qBroodHatchDate[,c('BroodRef', 'HatchDate')], by='BroodRef', all.x=TRUE)
tblChicks_All$LastLiveRecord <- as.POSIXct(tblChicks_All$LastLiveRecord, format = "%d-%b-%y")
tblChicks_All$LastLiveRecord[is.na(tblChicks_All$LastLiveRecord) & !is.na(tblChicks_All$DeathDate)] <- tblChicks_All$DeathDate[is.na(tblChicks_All$LastLiveRecord) & !is.na(tblChicks_All$DeathDate)]-1
close(conDB)
}
options(warn=2) # when loop generate a error at one iteration, the loop stop, so one can call the filename and check what's wrong with it
}
head(tblDVD_XlsFiles)
head(tblBirdID)
head(tblParentalCare)
head(tblBroodEvents)
head(tblBroods)
head(tblAllCodes)
head(tblDVDInfo)
head(tblDVD_XlsFilesALLDBINFO)
head(missingDVDFilenames)
head(RearingBrood_allBirds)
head(sys_LastSeenAlive)
head(sunrise)
head(LastMassTarsusChick)
head(tblChicks)
head(tblChicks_All)
head(usys_qBroodHatchDate)
head(usys_qBroodEggDate)
{### extract provisioning raw data from excel files
{### create list of filenames for New and Old Template: check those not included yet but that should be
{## create list of file names from files analysed from 2015 with Malika's protocol in Issie's template
# not elegant as require that the DVDRef of the DB not to change
FilenamesMalikaTemplate <- sort(tblDVD_XlsFiles$Filename[tblDVD_XlsFiles$RowRef >= 3180 & tblDVD_XlsFiles$Filename%in%tblDVD_XlsFilesALLDBINFO$Filename ])
length(FilenamesMalikaTemplate) # 83 (20160322)
}
{## create list of file names from files analysed between 2012 and 2015 with Issie's protocol
# not elegant as require that the DVDRef of the DB not to change
#Filenames1215 <- sort(tblDVD_XlsFiles$Filename[tblDVD_XlsFiles$DVDRef >=2933 & tblDVD_XlsFiles$DVDRef <=5146 & tblDVD_XlsFiles$Filename%in%tblDVD_XlsFilesALLDBINFO$Filename ])
Filenames1215 <- sort(tblDVD_XlsFiles$Filename[tblDVD_XlsFiles$DVDRef >=2933 & tblDVD_XlsFiles$RowRef <3180 & tblDVD_XlsFiles$Filename%in%tblDVD_XlsFilesALLDBINFO$Filename & tblDVD_XlsFiles$DVDRef != 5147]) # file 4001LM19 added as DVD5147 in march 2016
}
head(Filenames1215)
{## create list of file names from files analysed in 2010 and 2011 with Issie's template
filename1011_oldtemplate <- c(
"2010\\VJ0039.xlsx", "2010\\VJ0040.xlsx", "2010\\VJ0041.xlsx", "2010\\VJ0044.xlsx", "2010\\VJ0050.xlsx", "2010\\VJ0052.xlsx",
"2010\\VJ0058.xlsx", "2010\\VJ0059.xlsx", "2010\\VJ0060.xlsx", "2010\\VJ0064.xlsx", "2010\\VJ0066.xlsx", "2010\\VJ0067.xlsx",
"2010\\VJ0068.xlsx", "2010\\VJ0070.xlsx", "2010\\VJ0078.xlsx", "2010\\VJ0079.xlsx", "2010\\VJ0080.xlsx", "2010\\VJ0081.xlsx",
"2011\\VK0001.xlsx", "2011\\VK0002.xlsx", "2011\\VK0003.xlsx", "2011\\VK0005.xlsx", "2011\\VK0006.xlsx", "2011\\VK0007.xlsx",
"2011\\VK0010.xlsx", "2011\\VK0011.xlsx", "2011\\VK0012.xlsx", "2011\\VK0013.xlsx", "2011\\VK0017.xlsx", "2011\\VK0019.xlsx", "2011\\VK0020.xlsx",
"2011\\VK0021.xlsx", "2011\\VK0022.xlsx", "2011\\VK0024.xlsx", "2011\\VK0025.xlsx", "2011\\VK0026.xlsx", "2011\\VK0027.xlsx", "2011\\VK0028.xlsx",
"2011\\VK0029.xlsx", "2011\\VK0031.xlsx", "2011\\VK0034.xlsx", "2011\\VK0037.xlsx", "2011\\VK0038.xlsx", "2011\\VK0039.xlsx", "2011\\VK0040.xlsx",
"2011\\VK0041.xlsx", "2011\\VK0042.xlsx", "2011\\VK0044.xlsx", "2011\\VK0045.xlsx", "2011\\VK0046.xlsx", "2011\\VK0047.xlsx", "2011\\VK0048.xlsx",
"2011\\VK0050.xlsx", "2011\\VK0051.xlsx", "2011\\VK0056.xlsx", "2011\\VK0061.xlsx", "2011\\VK0062.xlsx", "2011\\VK0063.xlsx", "2011\\VK0067.xlsx",
"2011\\VK0069.xlsx", "2011\\VK0070.xlsx", "2011\\VK0072.xlsx",
"2011\\VK0101.xlsx", "2011\\VK0102.xlsx", "2011\\VK0103.xlsx",
"2011\\VK0105.xlsx", "2011\\VK0106.xlsx",
"2011\\VK0410.xlsx", "2011\\VK0412.xlsx", "2011\\VK0413.xlsx", "2011\\VK0416.xlsx",
"2011\\VK0418.xlsx", "2011\\VK0419.xlsx", "2011\\VK0421.xlsx", "2011\\VK0422.xlsx", "2011\\VK0423.xlsx"
)
Filenames1011newtemplate <- tblDVD_XlsFiles$Filename[grepl("2010|2011", tblDVD_XlsFiles$Filename) == TRUE & !tblDVD_XlsFiles$Filename%in%filename1011_oldtemplate & tblDVD_XlsFiles$Filename%in%tblDVD_XlsFilesALLDBINFO$Filename]
# Filenames that contained 2010 or 2011 and that do not belong to the list above where situation = 4 (only chicks)
}
head(Filenames1011newtemplate)
{## combine all files analyzed with Issie's template (will take newly analyzed files only if those are put in the root of the year folder, with a normal file name)
FilenamesNewTemplate <- c(as.character(Filenames1011newtemplate), as.character(Filenames1215))
length(FilenamesNewTemplate) # 915 files, situation 4, new template # 20160321 with addition AJ files with Issie's template: 935
}
{## combined all files analyzed with Shinichi's templates
{FilenamesOldTemplate <- tblDVD_XlsFiles$Filename[
# where situation = 4
tblDVD_XlsFiles$Filename%in%tblDVD_XlsFilesALLDBINFO$Filename &
# years before 2010, or after 2010 but belonging to list created above
(tblDVD_XlsFiles$DVDRef <2016 | tblDVD_XlsFiles$DVDRef ==5147 | tblDVD_XlsFiles$Filename%in%filename1011_oldtemplate) &
# EXCLUDED BUT WITH DATA IN DB (COULD BE INCLUDED if rewatched)
tblDVD_XlsFiles$Filename != "2004\\40055.xlsx" & # files that contain comments that are not standardized (data in DB)
tblDVD_XlsFiles$Filename != "2004\\40061.xlsx" & # files that contain comments that are not standardized (data in DB)
tblDVD_XlsFiles$Filename != "2008\\80055.xlsx" & # file empty (data in DB)
tblDVD_XlsFiles$Filename != "2005\\50268.xlsx" # commented: too difficult to distinguish nale and female (and therefore file is empty, DB parental care = line of NA)
]
}
which(duplicated(merge(x=data.frame(FilenamesOldTemplate), y=tblDVD_XlsFilesALLDBINFO[,c("DVDRef","Filename")], by.x= "FilenamesOldTemplate", by.y= "Filename",all.x=TRUE)[,"DVDRef"])) # no duplicates of DVDRef
# add missing Filenames
FilenamesOldTemplate <- c(as.character(FilenamesOldTemplate),as.character(missingDVDFilenames$Filename[missingDVDFilenames$Filename != "2007\\70004.xlsx"])) # excel file not found !
which(duplicated(missingDVDFilenames[,c("DVDRef","Filename")][,"DVDRef"])) # no duplicates of DVDRef
length(FilenamesOldTemplate) # 889 files, situation 4, old template # on 20160314: addition missing Filename > 1094 files
}
}
head(FilenamesNewTemplate)
head(FilenamesOldTemplate)
head(FilenamesMalikaTemplate)
require(openxlsx)
search() # make sure package 'xlsx' is not in the list
{### extraction data in Excel files analyzed with Malika's excel template and error checking
outMalika = list()
warninggz <- list()
warninggzz <- list()
for (j in 1:length(FilenamesMalikaTemplate)){
filenamej <- paste(pathdropboxfolder, FilenamesMalikaTemplate[j], sep="\\DVDs ")
b <- read.xlsx(filenamej, sheet="DVD NO") # read.xlsx function from library 'openxlsx' (not library 'xlsx'): make sure xlsx is not in the list given by 'search()'
warninggz[[j]] <- as.character(FilenamesMalikaTemplate[j])
warninggzz[[j]] <- as.character(FilenamesMalikaTemplate[j])
{### warningz in raw data (only numbers and no blank)
# check if no missing Time or state
if ((length(b$Fend[!is.na(b$Fend)]) != length(b$Fstart[!is.na(b$Fstart)])) | (length(b$Mend[!is.na(b$Mend)]) != length(b$Mstart[!is.na(b$Mstart)])))
{warninggz[[j]] <- c(warninggz[[j]],"missing Time !")}
# check if no comments in raw data
if (is.numeric(b$Fstart) == FALSE | is.numeric(b$Fend) == FALSE | is.numeric(b$Mstart) == FALSE | is.numeric(b$Mend) == FALSE)
{warninggz[[j]] <- c(warninggz[[j]],"character in raw data")}
# check if no state
if ((length(b$Fend[!is.na(b$Fend)]) != length(b$Fstate[!is.na(b$Fstate)])) | (length(b$Mend[!is.na(b$Mend)]) != length(b$Mstate[!is.na(b$Mstate)])))
{warninggz[[j]] <- c(warninggz[[j]],"missing state !")}
}
{### if no warningz: extract Female and Male raw data separately
if (length(warninggz[[j]]) == 1)
{
bbF <- data.frame(b$Fstart, b$Fend, b$Fstate, 0, NA,NA)
colnames(bbF) <- c("Tstart", "Tend", "State", "Sex", "prevEnd","Diff_Tstart_prevEnd")
bbF <- bbF[!is.na(bbF$Tstart) | !is.na(bbF$Tend),]
bbM <- data.frame(b$Mstart, b$Mend, b$Mstate,1,NA,NA)
colnames(bbM) <- c("Tstart", "Tend", "State", "Sex", "prevEnd","Diff_Tstart_prevEnd")
bbM <- bbM[!is.na(bbM$Tstart) | !is.na(bbM$Tend),]
if (nrow(bbF)>0)
{
bbF$prevEnd <- c(NA,bbF$Tend[-nrow(bbF)])
bbF$Diff_Tstart_prevEnd <- bbF$Tstart-bbF$prevEnd
}
if (nrow(bbM)>0)
{
bbM$prevEnd <- c(NA,bbM$Tend[-nrow(bbM)])
bbM$Diff_Tstart_prevEnd <- bbM$Tstart-bbM$prevEnd
}
}
}
{### warningzz in chronology
if ((nrow(bbF[bbF$Tend - bbF$Tstart <0,]) > 0) | (nrow(bbF[!is.na(bbF$Diff_Tstart_prevEnd) & bbF$Diff_Tstart_prevEnd <0,]) > 0) |
(nrow(bbM[bbM$Tend - bbM$Tstart <0,])) > 0 | (nrow(bbM[!is.na(bbM$Diff_Tstart_prevEnd) & bbM$Diff_Tstart_prevEnd <0,]) > 0))
{warninggzz[[j]] <-c(warninggzz[[j]], "wrong chronology in female or male!")
bbF <- NULL
bbM <- NULL}
}
{## if no warningzz: create bb
if (length(warninggz[[j]])==1 & length(warninggzz[[j]])==1)
{
# when no bird ever visited: keep a line with NA
if (nrow(bbF)== 0 & nrow(bbM)== 0)
{
bb <- data.frame(rbind(c(NA,NA,NA,NA,NA)))
colnames(bb) <- c('Tstart','Tend','State','Sex','Filename') # filename will be filled in later
}
# when only one bird visited
if (nrow(bbF)!= 0 & nrow(bbM)== 0)
{
bb <- bbF[,c('Tstart','Tend','State','Sex')]
}
if (nrow(bbF)== 0 & nrow(bbM)!= 0)
{
bb <- bbM[,c('Tstart','Tend','State','Sex')]
}
# when both birds visited, combine both sex data and order by Tstart then Tend
if(nrow(bbF)!= 0 & nrow(bbM)!= 0)
{
bb <- rbind(bbF[,c('Tstart','Tend','State','Sex')], bbM[,c('Tstart','Tend','State','Sex')])
bb <- bb[with(bb,order(bb$Tstart, bb$Tend)),]
}
# add filename
bb$Filename <- as.character(FilenamesMalikaTemplate[j])
}
outMalika[[j]] <- bb
bb <-NULL
}
}
condout <- sapply(outMalika, function(x) length(x) > 1)
outMalika <- outMalika[condout]
length(outMalika)
combinedprovisioningMalikaTemplate = do.call(rbind, outMalika)
{# error check for Malika's Template
length(unique(combinedprovisioningMalikaTemplate$Filename)) # 83 files, situation 4, Malika's template
# weird comments or missing info
condwarninggz <- sapply(warninggz, function(x) length(x) > 1)
warninggz <- warninggz[condwarninggz]
warninggz # should be empty list
# mistake in chronology
condwarninggzz <- sapply(warninggzz, function(x) length(x) > 1)
warninggzz <- warninggzz[condwarninggzz]
warninggzz # should be empty list
}
}
head(combinedprovisioningMalikaTemplate)
{### extraction data in Excel files analyzed with Issie's excel template (after conversion all files to xlsx) and error checking
outIssie = list()
warninggz <- list()
warninggzz <- list()