Skip to content

Commit c6b8b02

Browse files
authored
Labrador PCA (#17)
Code and resulting gif for Labrador PCA
1 parent e5714bf commit c6b8b02

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

notebooks/LabradorPCA.R

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# devtools::install_github("HighlanderLab/RandPedigreePCA", subdir = "randPedPCA",
2+
# ref="v0.9.3", build_vignettes = T)
3+
4+
rm(list = ls())
5+
library(randPedPCA)
6+
library(readr)
7+
library(tidyverse)
8+
9+
Labrador <- read_csv(file = "/Users/roscraddock/Documents/University\ of\ Edinburgh/HighlanderLab - Data_2025/LabPed2025.csv")
10+
Labrador<- Labrador[order(Labrador$gen),]
11+
12+
# Add coat colour? Four official categories: Black, Chocolate, Liver, Yellow then unknown.
13+
Labrador$Coat_Colour <- Labrador$Colour
14+
labcolours <- c("BLACK", "YELLOW", "LIVER", "CHOCOLATE")
15+
Labrador <- Labrador %>%
16+
transform(Coat_Colour = ifelse(Coat_Colour %in% labcolours, Coat_Colour, "Unknown/nonStandard"))
17+
Labrador <- Labrador %>%
18+
transform(Coat_Colour = ifelse(Coat_Colour == "BLACK", "Black", Coat_Colour))
19+
Labrador <- Labrador %>%
20+
transform(Coat_Colour = ifelse(Coat_Colour == "YELLOW", "Yellow", Coat_Colour))
21+
Labrador <- Labrador %>%
22+
transform(Coat_Colour = ifelse(Coat_Colour == "LIVER", "Chocolate", Coat_Colour))
23+
Labrador <- Labrador %>%
24+
transform(Coat_Colour = ifelse(Coat_Colour == "CHOCOLATE", "Chocolate", Coat_Colour))
25+
26+
# Create Pedigree Object
27+
ped <- pedigree(Labrador$fatherID,
28+
Labrador$motherID,
29+
Labrador$id)
30+
31+
# Obtain centred estimate from L inverse using Hutch++
32+
li <- sparse2spam(getLInv(ped)) # generate L inverse and convert to spam format
33+
set.seed(123345) # set random seed as Hutch++ uses random numbers
34+
hp <- hutchpp(li,num_queries = 100, center = T) # estimate
35+
36+
# Run randPedPCA
37+
pc <- rppca(ped, center=T, totVar = hp)
38+
summary(pc)
39+
40+
# Collect proportion of variances explained by PC1 and PC2
41+
var <- pc[["varProps"]]*100
42+
# Labels for x and y axis
43+
pc1 <- paste("PC1 (",round(var[1], 2), "%)", sep = "")
44+
pc2 <- paste("PC2 (",round(var[2], 2), "%)", sep = "")
45+
46+
# Collect PC in t
47+
t <- pc[["x"]]
48+
# Plot PC1 and PC2, labelled by coat colour
49+
ggplot(data = t, aes(x = t[,1], y = t[,2], colour = Labrador$Coat_Colour)) +
50+
geom_point() +
51+
theme_minimal() +
52+
labs(title = "PCA of Labrador Pedigree",
53+
x = pc1,
54+
y = pc2,
55+
colour = "Coat Colour") +
56+
scale_colour_manual(values = c("Black" = "black",
57+
"Yellow" = "yellow",
58+
"Chocolate" = "brown",
59+
"Unknown/nonStandard" = "grey"),
60+
breaks = c("Black", "Yellow", "Chocolate", "Unknown/nonStandard"),
61+
labels = c("Black", "Yellow", "Chocolate", "Non Standard/ Unknown")) +
62+
theme(
63+
axis.title.x = element_text(size = 22), # Increase x-axis label text size
64+
axis.title.y = element_text(size = 22), # Increase y-axis label text size
65+
plot.title = element_text(size = 24), # Increase plot title text size
66+
legend.title = element_text(size = 22), # Increase legend title text size
67+
legend.text = element_text(size = 20),
68+
axis.text.x = element_text(size = 20), # Increase x-axis tick labels text size
69+
axis.text.y = element_text(size = 20)) # Increase y-axis tick labels text size
70+
71+
# Animate PCA
72+
library(gifski)
73+
library(gganimate)
74+
75+
Labrador$year <- as.integer(Labrador$year)
76+
77+
# Collect frames of the PCA plot labelled by coat colour for each year of birth. Total: 70 frames
78+
gapplot <- ggplot(data = t, aes(x = t[,1], y = t[,2], colour = Labrador$Coat_Colour)) +
79+
geom_point(alpha = 0.7) +
80+
theme_minimal() +
81+
labs(title = "PCA of Labrador Pedigree, Year: {frame_time}",
82+
x = pc1,
83+
y = pc2,
84+
colour = "Coat Colour") +
85+
scale_colour_manual(values = c("Black" = "black",
86+
"Yellow" = "yellow",
87+
"Chocolate" = "brown",
88+
"Unknown/nonStandard" = "grey"),
89+
breaks = c("Black", "Yellow", "Chocolate", "Unknown/nonStandard"),
90+
labels = c("Black", "Yellow", "Chocolate", "Non Standard/ Unknown")) +
91+
theme(
92+
axis.title.x = element_text(size = 22), # Increase x-axis label text size
93+
axis.title.y = element_text(size = 22), # Increase y-axis label text size
94+
plot.title = element_text(size = 24), # Increase plot title text size
95+
legend.title = element_text(size = 22), # Increase legend title text size
96+
legend.text = element_text(size = 20),
97+
axis.text.x = element_text(size = 20), # Increase x-axis tick labels text size
98+
axis.text.y = element_text(size = 20)) + # Increase y-axis tick labels text size
99+
transition_time(Labrador$year) +
100+
ease_aes('linear')
101+
102+
# Save the animated plot as a gif
103+
animate(gapplot, height = 600, width = 800, nframes = 70, fps = 5, renderer=gifski_renderer("LabradorPCA2.gif"))
104+

notebooks/LabradorPCA2.gif

1.55 MB
Loading

0 commit comments

Comments
 (0)