-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCIS-dump-ICMP.R
1539 lines (1145 loc) · 56.6 KB
/
CIS-dump-ICMP.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
## R Script to process data exported from the CIS databases ##
# Author: B.S. Weir (2017-2025)
#============Load all the packages needed================
library(tidyverse)
library(lubridate)
library(RColorBrewer)
#library(svglite)
library(skimr)
library(janitor)
library(sf) #Simple feature
library(ggspatial) #make maps fancy
#versions
R.version.string
#============Load data================
#loaded as a tibble
ICMP.as.imported.df <- read_csv("ICMP-export-3-jan-2025.csv", #also line 99
guess_max = Inf,
show_col_types = FALSE)
#============Subset and massage the Data================
#tidyverse way of sub setting - remove viruses and deaccessioned cultures
ICMP.df <- ICMP.as.imported.df |>
distinct(AccessionNumber, .keep_all= TRUE) |> #remove dupes
filter(SpecimenType == "Bacterial Culture" |
SpecimenType == "Chromist Culture" |
SpecimenType == "Fungal Culture" |
SpecimenType == "Yeast Culture") |>
filter(Deaccessioned == "FALSE") |>
glimpse()
ICMP.NZ.df <- ICMP.df %>%
filter(StandardCountry_CE1 == "New Zealand")
ICMP.types <- ICMP.df %>%
filter(TypeStatus != "")
#============Check imported data for issues================
# get duplicates based due to component duplication
# may need correction in CIS if TaxonName_C2 = NA, export as a CSV:
ICMP.dupes <- ICMP.as.imported.df %>%
get_dupes(AccessionNumber) %>%
select(AccessionNumber, dupe_count, CurrentNamePart_C1, TaxonName_C2, Substrate_C2, PartAffected_C2) %>%
filter(is.na(TaxonName_C2)) %>% #comment this out to get all
write_csv(file='./outputs/ICMP/ICMP.dupes.csv')
#setting up per specimen type subsets, with summaries of each specimen type
ICMP.bacteria <- ICMP.df %>%
filter(SpecimenType == "Bacterial Culture")
table(ICMP.bacteria$Phylum_C1) #this is a validation check for misclassified
ICMP.chromist <- ICMP.df %>%
filter(SpecimenType == "Chromist Culture")
table(ICMP.chromist$Phylum_C1) #this is a validation check for misclassified
ICMP.fungi <- ICMP.df %>%
filter(SpecimenType == "Fungal Culture")
table(ICMP.fungi$Phylum_C1) #this is a validation check for misclassified
ICMP.yeast <- ICMP.df %>%
filter(SpecimenType == "Yeast Culture")
table(ICMP.yeast$Phylum_C1) #this is a validation check for misclassified
#change this code below to find errors then fix in CIS
ICMP.fungi %>%
select("AccessionNumber", "SpecimenType", "CurrentNamePart_C1", "Phylum_C1" ) %>%
filter(Phylum_C1 == "Oomycota" | Phylum_C1 == "Amoebozoa")
ICMP.chromist %>%
select("AccessionNumber", "SpecimenType", "CurrentNamePart_C1", "Phylum_C1" ) %>%
filter(Phylum_C1 == "Ascomycota" | Phylum_C1 == "Basidiomycota")
#============Data quality checks================
#saves the head of the dataset (to GitHub) to understand data structure
ICMP.as.imported.df %>%
filter(SpecimenSecurityLevelText == "Public") %>% #public only!
head() %>%
write_csv(file='./outputs/ICMP/ICMP-head.csv')
#save a summary of the data to txt
ICMP.string.factors <- read.csv("ICMP-export-18-dec-2024.csv",
stringsAsFactors = TRUE) %>%
summary(maxsum=25) %>%
capture.output(file='./outputs/ICMP/ICMP-summary.txt')
#check specimen count before and after filtering
count(ICMP.as.imported.df,SpecimenType)
count(ICMP.df,SpecimenType)
#check security level
count(ICMP.df,SpecimenSecurityLevelText)
#skim the data for numerics on each column
ICMP.df %>%
skim()
# counts the number of unique values per column
sapply(ICMP.df, function(x) length(unique(x))) %>%
capture.output(file = "./outputs/ICMP/ICMP-unique-count.txt")
# counts the number of unique values per column for NZ
sapply(ICMP.NZ.df, function(x) length(unique(x)))
#============Colours and notes================
# notes here: https://www.datanovia.com/en/blog/the-a-z-of-rcolorbrewer-palette/
#display.brewer.all(colorblindFriendly = TRUE)
#display.brewer.all(colorblindFriendly = FALSE)
# OK ones are Paired if you have heaps of data. Others are: Set2
#============Missing biostatus================
#filters for blank occurrence description
#then de-duplicate
ICMP.df |>
select(CurrentNamePart_C1, StandardCountry_CE1, OccurrenceDescription_C1, BiostatusDescription_C1) |>
filter(is.na(OccurrenceDescription_C1)) |>
distinct() |>
arrange(CurrentNamePart_C1) |>
write_csv(file='./outputs/ICMP/ICMP-missing-occurrence.csv')
#============No identification================
#filters for blank identification
#also finds blank name part
ICMP.df |>
select(AccessionNumber, CurrentNamePart_C1, VerbatimName_C1, TaxonName_C1, StandardCountry_CE1) |>
filter(is.na(CurrentNamePart_C1)) |>
arrange(CurrentNamePart_C1) |>
write_csv(file='./outputs/ICMP/ICMP-missing-identification.csv')
#============Un-sequenced species================
#chat gpt version, all species from NZ that don't have a sequence
unsequenced.df <- ICMP.df |>
filter(StandardCountry_CE1 == "New Zealand") |>
distinct(CurrentNamePart_C1, GenBank)
# Identify values that have both TRUE and FALSE
to_exclude <- unsequenced.df %>%
group_by(CurrentNamePart_C1) %>%
filter(n_distinct(GenBank) > 1) %>%
pull(CurrentNamePart_C1) %>%
unique()
# Filter out those values
filtered_data <- unsequenced.df |>
filter(!CurrentNamePart_C1 %in% to_exclude) |>
filter(GenBank == "FALSE") |>
write_csv(file='./outputs/ICMP/ICMP-unsequenced-species.csv')
#============General stats================
#Table of number of cultures and types
total.table <- ICMP.df %>% # this one gets a total count
group_by(SpecimenType) %>%
dplyr::summarize(
All.ICMP = n(),
.groups = "drop"
)
types.table <- ICMP.df %>% # this one gets a types count
filter(TypeStatus != "") %>%
group_by(SpecimenType) %>%
dplyr::summarize(
Types = n(),
.groups = "drop"
)
NZ.table <- ICMP.df %>% # this one gets a New Zealand count
filter(StandardCountry_CE1 == "New Zealand") %>%
group_by(SpecimenType) %>%
dplyr::summarize(
NZ = n(),
.groups = "drop"
)
NZtype.table <- ICMP.df %>% # NZ and types
filter(StandardCountry_CE1 == "New Zealand" & TypeStatus != "") %>%
group_by(SpecimenType) %>%
dplyr::summarize(
NZ.Types = n(),
.groups = "drop"
)
icmp.count.table <- left_join(total.table, types.table) %>%
left_join(NZ.table) %>%
left_join(NZtype.table) %>%
bind_rows(summarise(.,
across(where(is.numeric), sum),
across(where(is.character), ~"Total"))) %>%
write_csv(file='./outputs/ICMP/ICMP-count-table.csv')
#This lists the number of different values for each column
ICMP.df %>%
map_int(n_distinct)
#how to use this to say how many countries?
ICMP.df %>%
select("AccessionNumber", "StandardCountry_CE1") %>%
map_int(n_distinct)
#create a Genus column
#split out genus by matching everything up to the first space:
#not working
# str_view(ICMP.df$CurrentNamePart_C1, "^[a-zA-Z-]*")
#not perfect but ¯\_(ツ)_/¯ now add it as a mutate:
#not working
#ICMP.df %>%
# mutate(Genus = str_extract(CurrentNamePart_C1, "^[a-zA-Z-]*")) %>%
# glimpse()
#============Unwanted orgs================
unwanted.table <- ICMP.df |>
filter(str_detect(SpecimenFlags, "Unwanted")) |>
group_by(SpecimenType) |>
dplyr::summarize(
NZ = n(),
.groups = "drop"
)
unwanted.table
unwanted.df <- ICMP.df |>
filter(str_detect(SpecimenFlags, "Unwanted")) |>
filter(str_detect(CurrentNamePart_C1, "PDD")) |>
write_csv(file='./outputs/ICMP/ICMP_unwanted_cultures.csv')
unwanted.df <- ICMP.df |>
filter(str_detect(SpecimenFlags, "Unwanted")) |>
distinct(CurrentNamePart_C1) |>
write_csv(file='./outputs/ICMP/ICMP_unwanted_species.csv')
#============Type cultures================
#sorting plots:
# https://www.r-graph-gallery.com/267-reorder-a-variable-in-ggplot2.html
#barchart of all ICMP types sorted by 'kind' of type
ggplot(ICMP.types, aes(TypeStatus)) +
labs(title = "Types in the ICMP") +
labs(x = "'Kind' of type", y = "number of isolates") +
geom_bar() +
coord_flip()
ggsave(file='./outputs/ICMP/ICMP_types.png', width=10, height=10)
## KEY CHART ##
#barchart of all ICMP types sorted by 'kind' of type, coloured by kind of organism
positions <- c("Type strain", "Pathotype strain", "Neopathotype strain", "Type", "ex Type", "Holotype", "ex Holotype", "Paratype", "ex Paratype", "Isotype", "Neotype", "ex Neotype", "Epitype", "ex Epitype", "Syntype", "Reference strain")
ggplot(ICMP.types, aes(TypeStatus, fill=SpecimenType)) +
labs(title = "Types in the ICMP culture collection") +
labs(x = "'Kind' of type", y = "number of cultures") +
theme_bw()+
geom_bar() +
coord_flip() +
scale_fill_brewer(palette = "Set2") +
scale_x_discrete(limits = positions)
ggsave(file='./outputs/ICMP/ICMP.types.by.kind.png', width=10, height=10)
#barchart of all ICMP types sorted by 'kind' of type, with genbank status
ggplot(ICMP.types, aes(SpecimenType, fill=GenBank)) +
labs(title = "Types in the ICMP with a GenBank seqeunce") +
labs(x = "'Kind' of type", y = "number of isolates") +
geom_bar() +
coord_flip()
ggsave(file='./outputs/ICMP/ICMP_types-by-SpecimenType.png', width=10, height=10)
#Types in the ICMP with images
ggplot(ICMP.types, aes(TypeStatus, fill=Images)) +
labs(title = "Types in the ICMP with images") +
labs(x = "'Kind' of type", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip()
ggsave(file='./outputs/ICMP/ICMP.types.with.images.png', width=10, height=10)
#Types in ICMP with sequences in GenBank
ggplot(ICMP.types, aes(TypeStatus, fill=GenBank)) +
labs(title = "Types in ICMP with sequences in GenBank") +
labs(x = "'Kind' of type", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip()
ggsave(file='./outputs/ICMP/ICMP.types.with.sequence.png', width=10, height=10)
#depositors of types
ICMP.dupes <- ICMP.as.imported.df %>%
get_dupes(AccessionNumber) %>%
select(AccessionNumber, dupe_count, CurrentNamePart_C1, TaxonName_C2, Substrate_C2, PartAffected_C2) %>%
filter(is.na(TaxonName_C2)) %>% #comment this out to get all
write_csv(file='./outputs/ICMP/ICMP.dupes.csv')
#============PIE CHART!================
#this is borked code, but if sorted correctly could be a barcode map of deposits over time
#sort would have to strip ICMP number
#sort(table(ICMP.df$AccessionNumber),decreasing=TRUE)
#ggplot(ICMP.df, aes(x="", y=AccessionNumber, fill=SpecimenType))+
# geom_bar(width = 1, stat = "identity")
#pie chart by specimen type
ggplot(ICMP.df, aes(x=factor(1), fill=SpecimenType)) +
geom_bar(width = 1) +
coord_polar("y") +
theme_void() +
scale_fill_brewer(palette = "Set2")
ggsave(file='./outputs/ICMP/ICMP_specimen_pie.png', width=5, height=5)
#pie chart by last updated user
ICMP.df$LastUpdatedBy <- sapply(ICMP.df$UpdatedBy, tolower)
ggplot(ICMP.df, aes(x=factor(1), fill=LastUpdatedBy)) +
labs(title = "Last User to update an ICMP record") +
geom_bar(width = 1) +
coord_polar("y") +
theme_void() +
scale_fill_brewer(palette = "Paired")
ggsave(file='./outputs/ICMP/ICMP_last-updated_pie.png', width=5, height=5)
#============Extended specimen data================
#install gridextra
library(gridExtra)
#GenBank status by sample type
#genbank.plot <- ggplot(ICMP.df, aes(SpecimenType, fill=GenBank)) +
ggplot(ICMP.df, aes(SpecimenType, fill=GenBank)) +
labs(title = "Cultures in the ICMP in GenBank") +
labs(x = "Taxon", y = "number of isolates") +
scale_fill_brewer(palette = "Paired") +
geom_bar()
ggsave(file='./outputs/ICMP/extended-specimen-genbank.png', width=7, height=7)
#Literature status by sample type
#literature.plot <- ggplot(ICMP.df, aes(SpecimenType, fill=Literature)) +
ggplot(ICMP.df, aes(SpecimenType, fill=Literature)) +
labs(title = "Cultures in the ICMP with a citation in literature") +
labs(x = "Taxon", y = "number of isolates") +
scale_fill_brewer(palette = "Paired") +
geom_bar()
ggsave(file='./outputs/ICMP/extended-specimen-literature.png', width=7, height=7)
#Image status by sample type
#image.plot <- ggplot(ICMP.df, aes(SpecimenType, fill=Images)) +
ggplot(ICMP.df, aes(SpecimenType, fill=Images)) +
labs(title = "Cultures in the ICMP with images") +
labs(x = "Taxon", y = "number of isolates") +
scale_fill_brewer(palette = "Paired") +
geom_bar()
ggsave(file='./outputs/ICMP/extended-specimen-images.png', width=7, height=7)
#This is a way to arrange on a grid, need to uncomment above
#grid.arrange(genbank.plot, literature.plot, image.plot, nrow = 2, ncol = 2)
#Add code here from the rmd file for faceted graph
#============Sample kind Level barcharts================
#Simple sample type barchart
ggplot(ICMP.df, aes(SpecimenType)) +
labs(title = "Cultures in the ICMP by Sample type") +
labs(x = "Taxon", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip()
ggsave(file='./outputs/ICMP/ICMP_kingdoms.png', width=7, height=7)
#Occurrence in NZ
ggplot(ICMP.df, aes(SpecimenType, fill=OccurrenceDescription_C1)) +
labs(title = "Cultures in the ICMP by occurrence in NZ") +
labs(x = "Taxon", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip() +
scale_fill_brewer(palette = "Set2")
ggsave(file='./outputs/ICMP/ICMP_kingdoms_occurrence.png', width=7, height=7)
#Occurrence in NZ
ggplot(ICMP.df, aes(OccurrenceDescription_C1, fill=SpecimenType)) +
labs(title = "Cultures in the ICMP by occurrence in NZ") +
labs(x = "Taxon", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip() +
scale_fill_brewer(palette = "Set2")
ggsave(file='./outputs/ICMP/ICMP_kingdoms_occurrence-alt.png', width=7, height=7)
#Order Status
ggplot(ICMP.df, aes(SpecimenType, fill= LoanStatus)) +
labs(title = "ICMP Order Status") +
labs(x = "Taxon", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip() +
scale_fill_brewer(palette = "Set2")
ggsave(file='./outputs/ICMP/ICMP_kingdoms_LoanStatus.png', width=7, height=7)
#kingdoms by last updated
#need to filter out low users and just as a bar or pie graph?
ICMP.df$UpdatedBy3 <- sapply(ICMP.df$UpdatedBy, tolower)
ggplot(ICMP.df, aes(SpecimenType, fill= UpdatedBy3)) +
labs(title = "ICMP Last updated by") +
labs(x = "Taxon", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip() +
scale_fill_brewer(palette = "Paired")
ggsave(file='./outputs/ICMP/ICMP_kingdoms_updated_by.png', width=7, height=7)
#need a kingdoms by NZ cultures??
#============High Taxonomy================
# ----- bacterial taxon grouping -----
sort(table(ICMP.df$Family_C1),decreasing=TRUE)[1:11] #top 11 families
sort(table(ICMP.NZ.df$Family_C1),decreasing=TRUE)[1:11] #top 11 NZ families
#Bacterial Phylum_C1
ggplot(ICMP.bacteria, aes(Phylum_C1)) +
labs(title = "ICMP by bacterial Phylum_C1") +
labs(x = "Taxon", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip()
ggsave(file='./outputs/ICMP/ICMP_bacteria-Phylum_C1.png', width=10, height=10)
#Bacterial Class
ggplot(ICMP.bacteria, aes(Class_C1)) +
labs(title = "ICMP by bacterial class") +
labs(x = "Taxon", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip()
ggsave(file='./outputs/ICMP/ICMP_bacteria-class.png', width=10, height=10)
#Bacterial Order
ggplot(ICMP.bacteria, aes(Order_C1)) +
labs(title = "ICMP by bacterial order") +
labs(x = "Taxon", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip()
ggsave(file='./outputs/ICMP/ICMP_bacteria-order.png', width=10, height=10)
#Bacterial Family
ggplot(ICMP.bacteria, aes(Family_C1)) +
labs(title = "ICMP by bacterial family") +
labs(x = "Taxon", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip()
ggsave(file='./outputs/ICMP/ICMP_bacteria-family.png', width=10, height=20)
# ----- fungal taxon grouping -----
#Fungal Phylum_C1
ggplot(ICMP.fungi, aes(Phylum_C1)) +
labs(title = "ICMP by fungal Phylum_C1") +
labs(x = "Taxon", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip()
ggsave(file='./outputs/ICMP/ICMP_fungal-Phylum_C1.png', width=10, height=10)
#Fungal Class
ggplot(ICMP.fungi, aes(Class_C1)) +
labs(title = "ICMP by fungal class") +
labs(x = "Taxon", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip()
ggsave(file='./outputs/ICMP/ICMP_fungal-class.png', width=10, height=10)
#Fungal Order
ggplot(ICMP.fungi, aes(Order_C1)) +
labs(title = "ICMP by fungal order") +
labs(x = "Taxon", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip()
ggsave(file='./outputs/ICMP/ICMP_fungal-order.png', width=10, height=20)
#Fungal Family
ggplot(ICMP.fungi, aes(Family_C1)) +
labs(title = "ICMP by fungal family") +
labs(x = "Taxon", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip()
ggsave(file='./outputs/ICMP/ICMP_fungal-family.png', width=10, height=20)
#============Literature================
ICMP.df |>
filter(Literature == "TRUE") |>
count(CurrentNamePart_C1, sort = TRUE) |>
drop_na() |>
slice_head(n=10)
ICMPlit.df <- ICMP.df |>
filter(Literature == "TRUE") |>
{then filter by these names}
#Fungal Family
ggplot(ICMPlit.df, aes(Family_C1)) +
labs(title = "Most cited species") +
labs(x = "Taxon", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip()
#============batches================
ICMP.df |>
count(ICMPBatchesTotal, sort = TRUE) |>
drop_na() |>
slice_tail(n=10)
#============Other names================
#Yeast Phylum_C1
ggplot(ICMP.yeast, aes(Phylum_C1)) +
labs(title = "ICMP by yeast Phylum_C1") +
labs(x = "Taxon", y = "number of cultures with a reference") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip()
ggsave(file='./outputs/ICMP/ICMP_yeast-Phylum_C1.png', width=10, height=10)
#Chromist Phylum_C1
ggplot(ICMP.chromist, aes(Phylum_C1)) +
labs(title = "ICMP by chromist Phylum_C1") +
labs(x = "Taxon", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip()
ggsave(file='./outputs/ICMP/ICMP_chromist-Phylum_C1.png', width=10, height=10)
# fungi present in NZ
#names.present.fungi <- subset(ICMP.df,(Kingdom == "Fungi" & OccurrenceDescription_C1 == "Present"))
#summary(names.present.fungi, maxsum=40)
#ggplot code for fungal Phylum_C1
require(ggplot2)
p <- ggplot(names, aes(names$Phlum)) + labs(title = "names by Phylum_C1") + labs(x = "Taxon", y = "number of names")
p <- p + theme(axis.text.x=element_text(angle=-90, hjust=0))
p + geom_bar()+ coord_flip()
print_bars <- p + geom_bar()+ coord_flip()
ggsave(print_bars, file='names-Phylum_C1.png', width=10, height=10)
#ggplot code for fungal Phylum_C1
require(ggplot2)
p <- ggplot(names, aes(names$Phlum, fill=OccurrenceDescription_C1)) + labs(title = "names by Phylum_C1") + labs(x = "Taxon", y = "number of names")
p <- p + theme(axis.text.x=element_text(angle=-90, hjust=0))
p + geom_bar()+ coord_flip()
print_bars <- p + geom_bar()+ coord_flip()
ggsave(print_bars, file='names-Phylum_C1-occurrence.png', width=10, height=10)
#ggplot code for Kingdom
require(ggplot2)
p <- ggplot(names, aes(names$Kingdom, fill=OccurrenceDescription_C1)) + labs(title = "names by Kingdom") + labs(x = "Taxon", y = "number of names")
p <- p + theme(axis.text.x=element_text(angle=-90, hjust=0))
p + geom_bar()+ coord_flip()
print_bars <- p + geom_bar()+ coord_flip()
ggsave(print_bars, file='names-Kindom-occurrence.png', width=10, height=10)
#ggplot code for Kingdom biostatus
require(ggplot2)
p <- ggplot(names, aes(names$Kingdom, fill=BioStatusDescription)) + labs(title = "names by Kingdom") + labs(x = "Taxon", y = "number of names")
p <- p + theme(axis.text.x=element_text(angle=-90, hjust=0))
p + geom_bar()+ coord_flip()
print_bars <- p + geom_bar()+ coord_flip()
ggsave(print_bars, file='names-Kindom-biostatus.png', width=10, height=10)
#ggplot code for Fungal Family present in NZ
require(ggplot2)
p <- ggplot(names.present.fungi, aes(names.present.fungi$Family_C1)) + labs(title = "names by family in NZ") + labs(x = "Taxon", y = "number of species")
p <- p + theme(axis.text.x=element_text(angle=-90, hjust=0))
p + geom_bar()+ coord_flip()
print_bars <- p + geom_bar()+ coord_flip()
ggsave(print_bars, file='names-Family-occurrence-NZ.png', width=10, height=35)
#============Countries================
count(ICMP.df)
ICMP.df |>
count(StandardCountry_CE1 == "New Zealand")
ICMP.df |>
filter(StandardCountry_CE1 == "New Zealand") |>
count()
countNZ <- ICMP.df |>
count(StandardCountry_CE1 == "New Zealand")
#convert to a percentage
countNZp <-(countNZ / count(ICMP.df))*100
sort(table(ICMP.df$StandardCountry_CE1),decreasing=TRUE)[1:12] #top 11 countries
#ggplot code for top ten countries by specimen type
positions <- c("New Zealand", "United States", "Australia", "United Kingdom", "Brazil", "Japan", "Thailand", "China", "India", "Italy")
ICMP.10county <- subset(ICMP.df, (StandardCountry_CE1 == "New Zealand" | StandardCountry_CE1 == "United States" | StandardCountry_CE1 == "Australia" | StandardCountry_CE1 == "United Kingdom" | StandardCountry_CE1 == "Brazil" | StandardCountry_CE1 == "Thailand" | StandardCountry_CE1 == "China" | StandardCountry_CE1 == "Japan" | StandardCountry_CE1 == "India" | StandardCountry_CE1 == "Italy"))
ggplot(ICMP.10county, aes(StandardCountry_CE1, fill=SpecimenType)) +
labs(title = "Top 10 Countries in the ICMP") +
labs(x = "Country", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
theme_bw() +
geom_bar() +
coord_flip() +
scale_fill_brewer(palette = "Set2") +
scale_x_discrete(limits = positions)
ggsave(file='./outputs/ICMP/ICMP_country_by_kind.png', width=8, height=4.5)
#ggplot code for pacific country
pacific <- subset(ICMP.df, (StandardCountry_CE1 == "Fiji" | StandardCountry_CE1 == "American Samoa" | StandardCountry_CE1 == "Cook Islands" | StandardCountry_CE1 == "Solomon Islands" | StandardCountry_CE1 == "Micronesia" | StandardCountry_CE1 == "New Caledonia" | StandardCountry_CE1 == "Niue" | StandardCountry_CE1 == "Norfolk Island" | StandardCountry_CE1 == "Samoa" | StandardCountry_CE1 == "Vanuatu"))
ggplot(pacific, aes(StandardCountry_CE1, fill=SpecimenType)) +
labs(title = "Pacific Countries cultures in the ICMP") +
labs(x = "Country", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
scale_fill_brewer(palette = "Set2") +
coord_flip()
ggsave(file='./outputs/ICMP/ICMP-pacific-countries.png', width=10, height=10)
#ggplot code for country
positions <- c("United States", "Australia", "United Kingdom", "Brazil", "Japan", "Thailand", "China", "India", "Italy", "Iran, Islamic Republic Of")
ICMP.10county.noNZ <- subset(ICMP.df, (StandardCountry_CE1 == "United States" | StandardCountry_CE1 == "Australia" | StandardCountry_CE1 == "United Kingdom" | StandardCountry_CE1 == "Brazil" | StandardCountry_CE1 == "Thailand" | StandardCountry_CE1 == "China" | StandardCountry_CE1 == "Japan" | StandardCountry_CE1 == "India" | StandardCountry_CE1 == "Italy") | StandardCountry_CE1 == "Iran, Islamic Republic Of")
ggplot(ICMP.10county.noNZ, aes(StandardCountry_CE1, fill=SpecimenType)) +
labs(title = "Top 10 Countries in the ICMP (not including NZ)") +
labs(x = "StandardCountry_CE1", y = "number of isolates") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
scale_fill_brewer(palette = "Set2") +
geom_bar()+ coord_flip() + scale_x_discrete(limits = positions)
ggsave(file='./outputs/ICMP/ICMP_country_by_kind_not_nz.png', width=10, height=10)
## could make a pseudo dataset manually replacing all non target countries with "other"
#============Over time================
## ideas ##
# Do a trend line of growth. so do a scatterplot and fir a trend line to project growth.
# Do a cumulative graph?
#should make a tibble of the below
#all cultures sorted by date. Add a new column date.isolated
ICMP.df$date.isolated <- ymd(ICMP.df$ICMPIsolatedDateISO, truncated = 3)
arrange(ICMP.df, date.isolated) |>
select("AccessionNumber","SpecimenType", "StandardCountry_CE1", "date.isolated", "CurrentNamePart_C1") |>
filter(str_detect(CurrentNamePart_C1, "(^Ralstonia)")) |>
filter(StandardCountry_CE1 == "New Zealand") |>
slice_head(n=25)
#all cultures sorted by deposited date. Add a new column date.isolated
ICMP.df$date.deposited <- ymd(ICMP.df$DepositedDateISO, truncated = 3)
arrange(ICMP.df, date.deposited) %>%
select("AccessionNumber","SpecimenType", "StandardCountry_CE1", "date.deposited") %>%
slice_head(n=10)
#New Zealand cultures sorted by date. Add a new column date.isolated
ICMP.NZ.df$date.isolated <- ymd(ICMP.NZ.df$ICMPIsolatedDateISO, truncated = 3)
arrange(ICMP.NZ.df, date.isolated) %>%
select("AccessionNumber","SpecimenType", "StandardCountry_CE1", "date.isolated") %>%
slice_head(n=25)
#new month ICMP culture isolated (in NZ)
month.isolated <- ymd(ICMP.NZ.df$ICMPIsolatedDateISO, truncated = 1)
mergemonths <- floor_date(month.isolated, unit = "month")
ggplot(ICMP.NZ.df, aes(month(mergemonths, label = TRUE), fill = SpecimenType)) +
labs(title = "Isolation month of ICMP cultures from NZ") +
labs(x = "Month of isolation", y = "Number of cultures" , fill = "") +
scale_fill_brewer(palette = "Set2") +
geom_bar() +
scale_x_discrete(na.translate = FALSE) # this removes NAs
ggsave(file='./outputs/ICMP/ICMP-isolation-month.png', width=8, height=5)
#new week ICMP culture isolated (in NZ)
week.isolated <- ymd(ICMP.NZ.df$ICMPIsolatedDateISO, truncated = 0)
mergeweeks <- round_date(week.isolated, unit = "week")
ggplot(ICMP.NZ.df, aes(isoweek(mergeweeks), fill = SpecimenType)) +
labs(title = "Isolation month of ICMP cultures from NZ") +
labs(x = "Week of isolation", y = "Number of cultures" , fill = "") +
scale_fill_brewer(palette = "Set2") +
geom_bar() +
scale_x_discrete(na.translate = FALSE) # this removes NAs
ggsave(file='./outputs/ICMP/ICMP-isolation-month.png', width=8, height=5)
#years test, this works and produces a bar chart but not super useful
#ICMP.df$date.isolated <-ymd(ICMP.df$ICMPIsolatedDateISO, truncated = 3) %>%
# floor_date(ICMP.df$date.isolated, unit = "year")
#ggplot(ICMP.df, aes(date.isolated, fill = SpecimenType)) +
# labs(title = "Isolation dates of ICMP cultures") +
# labs(x = "Date of isolation", y = "Number of cultures" , fill = "") +
# scale_fill_brewer(palette = "Set2") +
# theme(legend.position = c(0.1, 0.8)) +
# scale_x_date(date_breaks = "10 years", date_labels = "%Y") +
# geom_bar() # this is a bin of two years: binwidth=730
ICMP.df$date.isolated <-ymd(ICMP.df$ICMPIsolatedDateISO, truncated = 3) %>%
floor_date(ICMP.df$date.isolated, unit = "year")
ICMP.df$date.isolated
#ICMP isolation dates playing with plotting
#could do a histogram with a wider binned freqpoly as a 'trendline'
date.isolated <-ymd(ICMP.df$ICMPIsolatedDateISO, truncated = 3)
ggplot(ICMP.df, aes(date.isolated, fill = SpecimenType, colour = SpecimenType)) +
labs(title = "Isolation dates of ICMP cultures") +
labs(x = "Date of isolation", y = "Number of cultures" , fill = "") +
scale_fill_brewer(palette = "Set2") +
theme(legend.position = c(0.1, 0.8)) +
scale_x_date(date_breaks = "10 years", date_labels = "%Y") +
#geom_dotplot(binwidth=365.25) #too much data
#stat_bin(binwidth=365.25) #fine i guess
geom_freqpoly(binwidth=1461) + # a bin of 4 years
geom_histogram(binwidth=1461) # this is a bin of two years: binwidth=730
ggsave(file='./outputs/ICMP/ICMP-isolation-dates2.png', width=8, height=5)
#testing some new code for overlaying a trend
ICMP.bacteria$date.deposited <- ymd(ICMP.bacteria$DepositedDateISO, truncated = 3)
ggplot(ICMP.bacteria, aes(date.deposited)) +
theme_bw() +
labs(title = "Date bacterial cultures were deposited in ICMP") +
labs(x = "Date of deposit", y = "Number of cultures" , fill = "") +
scale_x_date(date_breaks = "10 years", date_labels = "%Y") +
geom_histogram(binwidth=365.25, alpha = 0.2) +
geom_density(aes(y=500 * ..count..), size = 2) #multiplies the density by 500x so it is visible
ggsave(file='./outputs/ICMP/ICMP-bacteria-depost-dates-smoothed.png', width=8, height=5)
#deposits over time
ICMP.df$date.deposited <- ymd(ICMP.df$DepositedDateISO, truncated = 3)
ggplot(ICMP.df, aes(date.deposited)) +
theme_bw() +
labs(title = "Date cultures were deposited in ICMP") +
labs(x = "Date of deposit", y = "Number of cultures" , fill = "") +
scale_x_date(date_breaks = "5 years", date_labels = "%Y") +
geom_histogram(binwidth=365.25, alpha = 0.2) +
geom_density(aes(y=500 * ..count..), size = 2) #multiplies the density by 500x so it is visible
ggsave(file='./outputs/ICMP/ICMP-total-deposits-smoothed.png', width=8, height=5)
#ICMP isolation dates
date.isolated <-ymd(ICMP.df$ICMPIsolatedDateISO, truncated = 3)
ggplot(ICMP.df, aes(date.isolated, fill = SpecimenType)) +
theme_bw()+
labs(title = "Isolation dates of ICMP cultures") +
labs(x = "Date of isolation", y = "Number of cultures" , fill = "") +
scale_fill_brewer(palette = "Set2") +
theme(legend.position = c(0.1, 0.8)) +
geom_histogram(binwidth=365.25, show.legend = TRUE) + # this is a bin of two years: binwidth=730
scale_x_date(date_breaks = "10 years", date_labels = "%Y")
ggsave(file='./outputs/ICMP/ICMP-isolation-dates.png', width=8, height=5)
#ICMP isolation dates faceted
date.isolated <-ymd(ICMP.df$ICMPIsolatedDateISO, truncated = 3)
ggplot(ICMP.df, aes(date.isolated, fill = SpecimenType)) +
labs(title = "Isolation dates of ICMP cultures") +
labs(x = "Date of isolation", y = "Number of cultures" , fill = "") +
scale_fill_brewer(palette = "Set2") +
geom_histogram(binwidth=365.25, show.legend = FALSE) + # this is a bin of two years: binwidth=730
scale_x_date(date_breaks = "10 years", date_labels = "%Y") +
facet_grid(SpecimenType ~ .)
ggsave(file='./outputs/ICMP/ICMP-isolation-dates-facet.png', width=8, height=5)
#ICMP deposit date faceted
date.deposited <-ymd(ICMP.df$DepositedDateISO, truncated = 3)
ggplot(ICMP.df, aes(date.deposited, fill = SpecimenType)) +
labs(title = "Deposit dates of ICMP cultures") +
labs(x = "Date of deposit in ICMP", y = "Number of cultures" , fill = "") +
scale_fill_brewer(palette = "Set2") +
scale_x_date(date_breaks = "10 years", date_labels = "%Y") +
geom_histogram(binwidth=365.25, show.legend = FALSE) + # this is a bin of two years: binwidth=730
facet_grid(SpecimenType ~ .)
ggsave(file='./outputs/ICMP/ICMP-deposit-dates-facet.png', width=8, height=5)
#ICMP deposit dates over time
date.deposited <-ymd(ICMP.df$DepositedDateISO, truncated = 3)
ggplot(ICMP.df, aes(date.deposited, fill = SpecimenType)) +
labs(title = "Deposit dates of ICMP cultures") +
labs(x = "Date of deposit in ICMP", y = "Number of cultures" , fill = "") +
scale_fill_brewer(palette = "Set2") +
scale_x_date(date_breaks = "10 years", date_labels = "%Y") +
geom_histogram(binwidth=365.25, show.legend = FALSE) # this is a bin of two years: binwidth=730
ggsave(file='./outputs/ICMP/ICMP-deposit-dates.png', width=8, height=5)
#testing some new code
ICMP.df$date.deposited <- ymd(ICMP.df$DepositedDateISO, truncated = 3)
builder <- ggplot(ICMP.df, aes(date.deposited)) +
labs(title = "Date bacterial cultures were deposited in ICMP") +
labs(x = "Date of deposit", y = "Number of cultures" , fill = "") +
scale_x_date(date_breaks = "10 years", date_labels = "%Y") +
geom_histogram(binwidth=365.25) # this is a bin of two years binwidth=730
# geom_hline(yintercept=392, linetype=2)
ggplot_build(builder)
builder.data<-ggplot_build(builder)$data
head(builder.data)
mean(builder.data$count)
#could do this early on and convert all to proper dates?? - nah make a seperate Date column
#for intercep do count per year to seperate table?
#collection dates. really only for comparing with isolation %% CollectionDateISO
ggplot(ICMP.df, aes(as.Date(CollectionDateISO, format='%Y-%m-%d'))) +
labs(title = "Date cultures were collected") +
labs(x = "Date of deposit", y = "Number of cultures" , fill = "") +
scale_x_date() +
geom_histogram(binwidth=365.25) # this is a bin of two years binwidth=730
ggsave(file='./outputs/ICMP/ICMP-Collection-dates.png', width=5, height=5)
ICMP.df$topcontrib <- ifelse(ICMP.df$Contributor == "NZP", "NZP", "other")
ICMP.df$topcontrib
attach(ICMP.df)
require(ggplot2)
dr <- ggplot(ICMP.df, aes(as.Date(ReceivedDateISO, format='%Y-%m-%d'),fill=topcontrib)) + labs(title = "Main Contributors to the ICMP collection") + labs(x = "Date of Receipt", y = "Number of cultures" , fill = "") #Alternatively, dates can be specified by a numeric value, representing the number of days since January 1, 1970. To input dates stored as the day of the year, the origin= argument can be used to interpret numeric dates relative to a different date.
dr <- dr + scale_x_date()
dr + geom_hline(yintercept=392, linetype=3) + geom_histogram(binwidth=365.25)
drp <- dr + geom_histogram(binwidth=365.25) + geom_hline(yintercept=392, linetype=2)
ggsave(drp, file='ICMP-received-dates-contributor.png', width=15, height=10)
sum2 <- ggplot_build(drp) #this extracts the values from the histogram
sum2
attach(ICMP.df) #this means we don't need the $ sign
require(ggplot2)
dr <- ggplot(ICMP.df, aes(as.Date(ReceivedDateISO),fill=topcontrib)) + labs(title = "Main Contributors to the ICMP collection") + labs(x = "Date of Receipt", y = "Number of cultures" , fill = "") #Alternatively, dates can be specified by a numeric value, representing the number of days since January 1, 1970. To input dates stored as the day of the year, the origin= argument can be used to interpret numeric dates relative to a different date.
dr <- dr + scale_x_date()
dr + geom_hline(yintercept=392, linetype=3) + geom_histogram(binwidth=365.25)
drp <- dr + geom_histogram(binwidth=365.25) + geom_hline(yintercept=392, linetype=2)
ggsave(drp, file='ICMP-received-dates-contributor.png', width=15, height=10)
ICMP.df$topcontrib <- ifelse(ICMP.df$Contributor == "NZP", "NZP", "other")
ICMP.df$topcontrib
#======NZ Maps========
#New Zealand Area codes bar chart
ICMP.NZ.df <- subset(ICMP.df,(StandardCountry_CE1 == "New Zealand"))
positions <- c("New Zealand", "Campbell Island", "Auckland Islands", "Snares Islands", "Chatham Islands", "Stewart Island", "Southland", "Fiordland", "Dunedin", "Central Otago", "Otago Lakes", "South Canterbury", "Mackenzie", "Westland", "Mid Canterbury", "North Canterbury", "Buller", "Kaikoura", "Marlborough", "Nelson", "Marlborough Sounds", "South Island", "Wairarapa", "Wellington", "Hawkes Bay", "Rangitikei", "Wanganui", "Gisborne", "Taupo", "Taranaki", "Bay of Plenty", "Waikato", "Coromandel", "Auckland", "Northland", "North Island", "Three Kings Islands", "Kermadec Islands")
ggplot(ICMP.NZ.df, aes(NZAreaCode)) +
theme_bw() +
labs(title = "ICMP cultures by NZ region") +
labs(x = "Crosby Region", y = "number of cultures") +
theme(axis.text.x=element_text(angle=-90, hjust=0)) +
geom_bar() +
coord_flip() +
scale_x_discrete(limits = positions)
ggsave(file='./outputs/ICMP/ICMP_NZAreaCode.png', width=8, height=5)
#require(ggplot2)
#require(ggmap)
#require(maps)
#require(mapdata)
#Good code for NZ map plotting
library(dplyr) #Pipes/filter because I am lazy
library(sf) #Simple feature
library(ggplot2) #Mapping
library(ggspatial) #Add's fancy shit to the map
# Loading in data
#Reading in a NZ specific map
nz.sf <- st_read(dsn = "./data/coastline/coastline.shp") %>%
st_transform(2193) #Setting map projection - NZGD2000
#Transforming to an SF object
rhizoNZ.sf <- ICMP.df %>%
filter(!is.na(DecimalLat)) %>% #Removing missing obs as sf doesn't play with these
st_as_sf(coords = c("DecimalLong", "DecimalLat")) %>% #Defining what the coord columns are
st_set_crs(4326) %>% #Telling sf it is in WSG84 projection
st_transform(2193) %>% #Changing it to NZGD2000 to match coastline polygon
st_crop(st_bbox(nz.sf)) #Cropping out points that are outside the coastline polygons bounding box (e.g. not NZ)
#Plotting - takes a second to execute
ggplot() +
geom_sf(data = nz.sf) +
geom_sf(data = rhizoNZ.sf, aes(colour = NZAreaCode),
size = 1, alpha = 0.2, show.legend = FALSE) +
annotation_scale(location = "br") +
annotation_north_arrow(location = "tl",
which_north = "true",
style = north_arrow_fancy_orienteering) +