-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpca_inferred_ancestry_report.R
343 lines (282 loc) · 13.4 KB
/
pca_inferred_ancestry_report.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
#!/usr/bin/Rscript
# Authors: Angela Taravella and Anagha Deshpande
#usage: pca_inferred_ancestry_report.R evec_file eval_file reference_panel_info.txt unkpop_info.txt output_fileSetemName output_inferredPopReport_fileStemName
### LOAD PACKAGES ###
library("car")
library("plotrix")
library("viridis")
### DEFINE FUNCTIONS ###
# Function 1: circle_function #
# circle_function plots centroid of each population coordinate along with plotting 1, and 3 standard deviation circles around the cluster mean.
circle_function <- function(continent) {
continent_of_choice <- continent
continent_of_choice_data <- evec_file_full[evec_file_full$V3.x %in% continent_of_choice,]
mean_x <- mean(continent_of_choice_data$V2.y)
mean_y <- mean(continent_of_choice_data$V3.y)
points(mean_x, mean_y, pch = 3, cex = 1)
test <- sqrt( ((mean_x - (continent_of_choice_data$V2.y))^2) + ((mean_y - (continent_of_choice_data$V3.y))^2) )
radius <- sqrt(sum((test-0)^2/(length(test)-1))) # this looks good
draw.circle(mean_x, mean_y, radius)
draw.circle(mean_x, mean_y, radius*3)
}
# Function 2: Get centroid coordinates and radii for each population cluster #
circle_function_noplot <- function(continent) {
continent_of_choice <- continent
continent_of_choice_data <- evec_file_full[evec_file_full$V3.x %in% continent_of_choice,]
mean_x <- mean(continent_of_choice_data$V2.y)
mean_y <- mean(continent_of_choice_data$V3.y)
test <- sqrt( ((mean_x - (continent_of_choice_data$V2.y))^2) + ((mean_y - (continent_of_choice_data$V3.y))^2) )
radius <- sqrt(sum((test-0)^2/(length(test)-1))) # this looks good
my_list <- list("radius" = radius, "mean_x" = mean_x, "mean_y" = mean_y)
return(my_list)
}
### SET UP COMMAND INPUTS ###
args = commandArgs(trailingOnly=TRUE)
evec = args[1]
eval = args[2]
pop = args[3]
unkpop = args[4]
out = args[5]
out_inferred_report = args[6]
### SET UP OUTPUT PDF ###
pfdfilename <- paste(out,".pdf",sep="")
pdf(pfdfilename, 8,8) #save as pdf, with size 8x8
# THIS IS FOR TESTING PURPOSES ONLY AND SHOULD BE DELETED WHEN NO LONGER NEEDED!!!
#evec_file <- read.table("merge_all_chr_reference_panel_unknown_set_SNPs_no_missing_plink_LDprune_Fix2.evec", header = FALSE)
#eval_file <- read.table("merge_all_chr_reference_panel_unknown_set_SNPs_no_missing_plink_LDprune.eval", header = FALSE)
#known_population_file <- read.table("ThousandGenomesSamples_AdmxRm.txt", header = FALSE, sep = "\t")
#unkownpop_file <- read.table("GTExSamples.txt", header = FALSE, sep = "\t")
### READ IN DATA ###
evec_file <- read.table(evec, header = FALSE)
eval_file <- read.table(eval, header = FALSE)
known_population_file <- read.table(pop, header = FALSE, sep = "\t")
unkownpop_file <- read.table(unkpop, header = FALSE, sep = "\t")
print("All data has been imported into the program")
### SET UP INITIAL DATA FRAMES ###
known_sample_info_merge <- merge(known_population_file, evec_file, by.x = "V1", by.y = "V1")
UnkownPop_data_WithInfo <- merge(unkownpop_file, evec_file, by.x = "V1", by.y = "V1")
evec_file_full <- rbind(known_sample_info_merge, UnkownPop_data_WithInfo)
### CALCULATE PERCENT VARIANCE EXPLAINED FOR EACH PC ###
eval_file$Perc_Var<- prop.table(eval_file$V1)*100
print("Plotting has started")
### PLOTTING ###
# Set up colors
n_colors <- length(levels(evec_file_full$V3.x))
#plot_colors <- rainbow(n_colors)
plot_colors <- viridis(n_colors)
# Generate the legend
plot(1, type="n", axes=FALSE, xlab="", ylab="", main = "Key")
legend('topleft', bty="n", legend = c(levels(evec_file_full$V3.x)), fill = c(plot_colors),
cex = 1, title=expression(bold("Population Clusters")))
legend('top', bty="n", legend = c(levels(evec_file_full$V2.x)),
cex = 1, pch = c(0, 1)[evec_file_full$V2.x], title=expression(bold("Genetic Sex")))
# Plot the graphs of the first 10 PCs
i <- 1
a <- 0
pc1 <- 0
pc2 <- 0
while (i<10)
{
j <- 4 + a
z <- 5 + a
pc1 <- eval_file[i,2]
pc1 <- round(pc1, digits = 2)
axis_pc1 <- paste("PC",i," (",pc1,"%)",sep="")
pc2 <- eval_file[j,2]
pc2 <- round(pc2, digits = 2) # round so number doesnt have a lot of decimal places
axis_pc2 <- paste("PC",i+1," (",pc2,"%)",sep="")
plot(evec_file_full[,j],evec_file_full[,z], xlab = axis_pc1, ylab = axis_pc2,
col=c(plot_colors)[evec_file_full$V3.x], pch =c(20, 15)[evec_file_full$V2.x], asp = 1)
i <- i+2
a <- a + 1
}
### SET UP OUTPUT INFERRED POPULATION REPORT ###
inferredpopreport <- paste(out_inferred_report,".txt",sep="")
#inferredpopreport <- paste("out_inferred_report_new",".txt",sep="")
### INFERRED ANCESTRY REPORT ###
print("Inferred population report has started")
# Generate the legend for the plot with cluster and midpoint info
plot(1, type="n", axes=FALSE, xlab="", ylab="", main = "Key")
legend('topleft', bty="n", legend = c(levels(evec_file_full$V3.x)), fill = c(plot_colors),
cex = 1, title=expression(bold("Population Clusters")))
legend('top', bty="n", legend = c(levels(evec_file_full$V2.x)),
cex = 1, pch = c(0, 1)[evec_file_full$V2.x], title=expression(bold("Genetic Sex")))
legend('bottomleft', bty="n", legend = c("Cluster Centroid",
"1 and 3 Standard Deviations from Cluster Mean",
"Pair-wise Cluster Mid Points"),
cex = 1, pch = c(3, 1, 4))
# Plot
plot(evec_file_full[,4],evec_file_full[,5], xlab = "PC1", ylab = "PC2",
col=c(plot_colors)[evec_file_full$V3.x], pch =c(20, 15)[evec_file_full$V2.x], asp = 1)
# Print all clusters onto the scatter plot
test_vector <- levels(known_sample_info_merge$V3.x)
for (i in 1:length(test_vector)) {
circle_function(test_vector[i])
}
### Store relevant information in matricies ###
# Stores values of radius, mean X, and mean Y in a martrix called "mat_meanx_meany_rad"
n <- length(test_vector)
mat_meanx_meany_rad <- matrix(ncol=4, nrow=n)
test_vector <- levels(known_sample_info_merge$V3.x)
for (i in 1:length(test_vector)) {
my_list <- circle_function_noplot(test_vector[i])
mat_meanx_meany_rad[i,] <- c(test_vector[i],my_list$mean_x,my_list$mean_y,my_list$radius)
}
# Find the mid point (X and Y coordinates) for each pairwise comparison and store in a matrix called "mat_meanx_meany_compares"
n_com <- factorial(length(test_vector))/((factorial((length(test_vector)-2)))*factorial(2))
mat_meanx_meany_compares <- matrix(ncol=4, nrow=n_com)
combination_num <- 0
for (i in 1:length(test_vector)) {
num <- i + 1
for (num in i:length(test_vector)) {
if (num > length(test_vector)) {} # we will skip this index because it is past the length of our vector and uninformative (no data)
else {
if (test_vector[i] == test_vector[num]) {} #skip this comparison. This is a self-self comparison
else {
comparison <- c(test_vector[i], test_vector[num])
# so now we have to get our values we need to find midpoints
my_list <- circle_function_noplot(test_vector[i])
test_mean_x_first <- my_list$mean_x
test_mean_y_first <- my_list$mean_y
my_list <- circle_function_noplot(test_vector[num])
test_mean_x_second <- my_list$mean_x
test_mean_y_second <- my_list$mean_y
x_mid <- (test_mean_x_first+test_mean_x_second)/2
y_mid <- (test_mean_y_first+test_mean_y_second)/2
points(x_mid,y_mid, col = "black", pch = 4)
comparison_name <- paste(test_vector[i], test_vector[num], sep = "-")
combination_num <- combination_num + 1
mat_meanx_meany_compares[combination_num,] <- c(comparison_name,x_mid,y_mid,0) # need a fourth column for merging with other matrix. I will add 0 to the 4th column
}
}
}
}
### Generate inferred population report ###
# Below makes the empty data frame with the correct number of rows and columns
# with informative information.
row_num <- length(UnkownPop_data_WithInfo[,1])
col_num <- length(test_vector) + 5
df <- data.frame(matrix(ncol = col_num, nrow = row_num))
vector_col_names <- c("Sample",
"Confident Inferred Pop: 1SD",
"Confident Inferred Pop: 2SD",
"Confident Inferred Pop: 3SD",
"Less Restricted Inferred Pop")
for (i in 1:length(test_vector)) {
col_name_cluster_i <- paste("Distance to cluster", test_vector[i], sep = " ")
vector_col_names <- c(vector_col_names, col_name_cluster_i)
}
colnames(df) <- vector_col_names
# This is looping through each individual in UnkownPop_data_WithInfo (so each
# unknown sample) and see if they are within 3 standard deviations to a known
# population. If not, the program will see how close that individual is to
# a mid point between 2 populations and if that is closer than any population's
# 3rd standard deviations, it will be called as the two populations of that
# midpoint. If not, then that individual will be called to the population's 3rd
# SD it is closest to.
# Begin with a 0 iterator so that we can loop through all individuals
iterator <- 0
for (i in UnkownPop_data_WithInfo[,1]) {
iterator <- iterator + 1
sample_i <- paste(UnkownPop_data_WithInfo[iterator,1])
pc_x <- UnkownPop_data_WithInfo[iterator,4]
pc_y <- UnkownPop_data_WithInfo[iterator,5]
vector_to_add_df <- c(sample_i)
inferred_pop_1SD <- c()
inferred_pop_2SD <- c()
inferred_pop_3SD <- c()
test_all_for_ans_1SD <- FALSE
test_all_for_ans_2SD <- FALSE
test_all_for_ans_3SD <- FALSE
for (cluster in 1:length(mat_meanx_meany_rad[,1])) {
x <- as.numeric(mat_meanx_meany_rad[cluster,2])
y <- as.numeric(mat_meanx_meany_rad[cluster,3])
dist_i_to_cluster_centriod <- sqrt( ((x - pc_x)^2) + ((y - pc_y)^2) )
sd_1 <- as.numeric(mat_meanx_meany_rad[cluster,4])
if (dist_i_to_cluster_centriod <= sd_1) {
test_all_for_ans_1SD <- TRUE
test_all_for_ans_1SD <- FALSE
inferred_pop_1SD <- paste(inferred_pop_1SD, mat_meanx_meany_rad[cluster,1], sep = "")
}
sd_2 <- as.numeric(mat_meanx_meany_rad[cluster,4]) * 2
if (dist_i_to_cluster_centriod <= sd_2) {
test_all_for_ans_2SD <- TRUE
#inferred_pop_2SD <- c()
if (is.null(inferred_pop_2SD)) {
inferred_pop_2SD <- paste(inferred_pop_2SD, mat_meanx_meany_rad[cluster,1], sep = "")
} else
inferred_pop_2SD <- paste(inferred_pop_2SD, mat_meanx_meany_rad[cluster,1], sep = "-")
}
sd_3 <- as.numeric(mat_meanx_meany_rad[cluster,4]) * 3
if (dist_i_to_cluster_centriod <= sd_3) {
test_all_for_ans_3SD <- TRUE
#inferred_pop_3SD <- c()
if (is.null(inferred_pop_3SD)) {
inferred_pop_3SD <- paste(inferred_pop_3SD, mat_meanx_meany_rad[cluster,1], sep = "")
} else
inferred_pop_3SD <- paste(inferred_pop_3SD, mat_meanx_meany_rad[cluster,1], sep = "-")
}
}
if (!test_all_for_ans_1SD) {
inferred_pop_1SD <- "-"
}
if (!test_all_for_ans_2SD) {
inferred_pop_2SD <- "-"
}
if (!test_all_for_ans_3SD) {
inferred_pop_3SD <- "-"
}
vector_to_add_df <- c(vector_to_add_df, inferred_pop_1SD, inferred_pop_2SD, inferred_pop_3SD)
# If the individual does not fall within 3SD of any cluster, the script will
# compare the distance from 3SD of all clusters with distance from midpoints
# of all pairwise clusters.
less_rest_inf_pop <- c()
cat_points <- rbind(mat_meanx_meany_rad, mat_meanx_meany_compares)
n_all <- length(cat_points[,1])
com_all_pnts <- matrix(ncol=2, nrow=n_all)
if (inferred_pop_3SD == "-"){
for (test_all_pts in 1:length(cat_points[,1])) {
if (cat_points[test_all_pts,4] != 0){
# so below put the calculation for comparing i or test_all_points to 3SD not the centroid
x <- as.numeric(cat_points[test_all_pts,2])
y <- as.numeric(cat_points[test_all_pts,3])
dist_i_to_centroid <- sqrt( ((x - pc_x)^2) + ((y - pc_y)^2) )
dist_i_to_pnt <- abs(dist_i_to_centroid - (3*as.numeric(mat_meanx_meany_rad[test_all_pts,4])))
} else {
x <- as.numeric(cat_points[test_all_pts,2])
y <- as.numeric(cat_points[test_all_pts,3])
dist_i_to_pnt <- sqrt( ((x - pc_x)^2) + ((y - pc_y)^2) )
# have to figure out what to do with lines 285-286 and possibly 284
#com_all_pnts[test_all_pts,] <- c(cat_points[test_all_pts,1],dist_i_to_pnt)
#index_num <- which(com_all_pnts == min(com_all_pnts)) - n_all
#less_rest_inf_pop <- com_all_pnts[index_num]
}
# then here put finding the lowest value and getting the less_rest_inf_pop from com_all_pnts[index_num]. Below may have to change
#com_all_pnts[test_all_pts,] <- c(cat_points[test_all_pts,1],dist_i_to_pnt)
com_all_pnts[test_all_pts,] <- c(cat_points[test_all_pts,1],format(as.numeric(dist_i_to_pnt), scientific=F))
index_num <- which(com_all_pnts == min(com_all_pnts)) - n_all
less_rest_inf_pop <- com_all_pnts[index_num]
}
} else {
if (inferred_pop_1SD != "-"){
less_rest_inf_pop <- inferred_pop_1SD
} else {
if (inferred_pop_2SD != "-") {
less_rest_inf_pop <- inferred_pop_2SD
} else {
less_rest_inf_pop <- inferred_pop_3SD
}
}}
vector_to_add_df <- c(vector_to_add_df, less_rest_inf_pop)
for (cluster in 1:length(mat_meanx_meany_rad[,1])) {
x <- as.numeric(mat_meanx_meany_rad[cluster,2])
y <- as.numeric(mat_meanx_meany_rad[cluster,3])
dist_i_to_cluster_centriod <- sqrt( ((x - pc_x)^2) + ((y - pc_y)^2) )
#cluster_dist_i <- paste(dist_i_to_cluster_centriod)
cluster_dist_i <- format(round(dist_i_to_cluster_centriod, 5), nsmall = 5)
vector_to_add_df <- c(vector_to_add_df, cluster_dist_i)
}
df[iterator, ] = vector_to_add_df
}
write.table(df, inferredpopreport, sep = "\t", quote = F, row.names = F)
dev.off() # close pdf