Skip to content

Commit

Permalink
Merge pull request #56 from Cristianetaniguti/main
Browse files Browse the repository at this point in the history
GWAS tab updates
  • Loading branch information
alex-sandercock authored Sep 20, 2024
2 parents 0c9aeee + 8be91b9 commit a5a6852
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 80 deletions.
39 changes: 29 additions & 10 deletions R/mod_PCA.R
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ mod_PCA_server <- function(input, output, session, parent_session){
} else{

#Import genotype information if in VCF format
vcf <- read.vcfR(geno)
vcf <- read.vcfR(geno, verbose = FALSE)

#Get items in FORMAT column
info <- vcf@gt[1,"FORMAT"] #Getting the first row FORMAT
Expand Down Expand Up @@ -252,6 +252,25 @@ mod_PCA_server <- function(input, output, session, parent_session){
# Print the modified dataframe
row.names(info_df) <- info_df[,1]

# Check ploidy
if(input$pca_ploidy != max(genomat, na.rm = T)){
shinyalert(
title = "Wrong ploidy",
text = "The input ploidy does not match the maximum dosage found in the genotype file",
size = "s",
closeOnEsc = TRUE,
closeOnClickOutside = FALSE,
html = TRUE,
type = "error",
showConfirmButton = TRUE,
confirmButtonText = "OK",
confirmButtonCol = "#004192",
showCancelButton = FALSE,
animation = TRUE
)
req(input$pca_ploidy == max(genomat, na.rm = T))
}

#Plotting
#First build a relationship matrix using the genotype values
G.mat.updog <- Gmatrix(t(genomat), method = "VanRaden", ploidy = as.numeric(ploidy), missingValue = "NA")
Expand Down Expand Up @@ -312,14 +331,14 @@ mod_PCA_server <- function(input, output, session, parent_session){

#End of PCA section
})


##2D PCA plotting
pca_2d <- reactive({
validate(
need(!is.null(pca_data$pc_df_pop), "Input Genotype file, Species ploidy, and run the analysis to access results in this section.")
)


# Generate colors
if (!is.null(pca_data$my_palette)) {
Expand Down Expand Up @@ -396,7 +415,7 @@ mod_PCA_server <- function(input, output, session, parent_session){

tit = paste0('Total Explained Variance =', sum(pca_data$variance_explained[1:3]))

fig <- plot_ly(pca_data$pc_df_pop, x = ~PC1, y = ~PC2, z = ~PC3, color = pca_data$pc_df_pop[[input$group_info]],
fig <- plot_ly(pca_data$pc_df_pop, x = ~PC1, y = ~PC2, z = ~PC3, color = as.factor(pca_data$pc_df_pop[[input$group_info]]),
colors = my_palette) %>%
add_markers(size = 12, text = paste0("Sample:",pca_data$pc_df_pop$Row.names))

Expand Down Expand Up @@ -448,14 +467,14 @@ mod_PCA_server <- function(input, output, session, parent_session){
output$scree_plot <- renderPlot({
pca_scree()
})

##Summary Info
pca_summary_info <- function() {
# Handle possible NULL values for inputs
dosage_file_name <- if (!is.null(input$dosage_file$name)) input$dosage_file$name else "No file selected"
passport_file_name <- if (!is.null(input$passport_file$name)) input$passport_file$name else "No file selected"
selected_ploidy <- if (!is.null(input$pca_ploidy)) as.character(input$pca_ploidy) else "Not selected"

# Print the summary information
cat(
"BIGapp PCA Summary\n",
Expand Down Expand Up @@ -483,7 +502,7 @@ mod_PCA_server <- function(input, output, session, parent_session){
sep = ""
)
}

# Popup for analysis summary
observeEvent(input$pca_summary, {
showModal(modalDialog(
Expand All @@ -499,8 +518,8 @@ mod_PCA_server <- function(input, output, session, parent_session){
)
))
})


# Download Summary Info
output$download_pca_info <- downloadHandler(
filename = function() {
Expand All @@ -511,7 +530,7 @@ mod_PCA_server <- function(input, output, session, parent_session){
writeLines(paste(capture.output(pca_summary_info()), collapse = "\n"), file)
}
)


#Download figures for PCA
output$download_pca <- downloadHandler(
Expand Down
58 changes: 18 additions & 40 deletions R/mod_diversity.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ mod_diversity_server <- function(input, output, session, parent_session){
updateProgressBar(session = session, id = "pb_diversity", value = 20, title = "Importing VCF")

#Import genotype information if in VCF format
vcf <- read.vcfR(geno)
vcf <- read.vcfR(geno, verbose = FALSE)

#Save position information
diversity_items$pos_df <- data.frame(vcf@fix[, 1:2])
Expand All @@ -163,7 +163,6 @@ mod_diversity_server <- function(input, output, session, parent_session){
geno_mat <- extract.gt(vcf, element = "GT")
geno_mat <- apply(geno_mat, 2, convert_to_dosage)
rm(vcf) #Remove VCF


print(class(geno_mat))
#Convert genotypes to alternate counts if they are the reference allele counts
Expand Down Expand Up @@ -248,46 +247,17 @@ mod_diversity_server <- function(input, output, session, parent_session){
box_plot()
})

#Het plot
het_plot <- reactive({
output$het_plot <- renderPlot({
validate(
need(!is.null(diversity_items$het_df) & !is.null(input$hist_bins), "Input VCF, define parameters and click `run analysis` to access results in this session.")
)
hist(diversity_items$het_df$ObservedHeterozygosity, breaks = as.numeric(input$hist_bins), col = "tan3", border = "black", xlim= c(0,1),
xlab = "Observed Heterozygosity",
ylab = "Number of Samples",
main = "Sample Observed Heterozygosity")

axis(1, at = seq(0, 1, by = 0.1), labels = TRUE)
})

output$het_plot <- renderPlot({
het_plot()
})

#AF Plot
#af_plot <- reactive({
# validate(
# need(!is.null(diversity_items$maf_df) & !is.null(input$hist_bins), "Input VCF, define parameters and click `run analysis` to access results in this session.")
# )
# hist(diversity_items$maf_df$AF, breaks = as.numeric(input$hist_bins), col = "grey", border = "black", xlab = "Alternate Allele Frequency",
# ylab = "Frequency", main = "Alternate Allele Frequency Distribution")
#})

#output$af_plot <- renderPlot({
# af_plot()
#})

#MAF plot
maf_plot <- reactive({
validate(
need(!is.null(diversity_items$maf_df) & !is.null(input$hist_bins), "Input VCF, define parameters and click `run analysis` to access results in this session.")
)

hist(diversity_items$maf_df$MAF, breaks = as.numeric(input$hist_bins), col = "grey", border = "black", xlab = "Minor Allele Frequency (MAF)",
ylab = "Frequency", main = "Minor Allele Frequency Distribution")
})

#Marker plot
marker_plot <- reactive({
validate(
Expand All @@ -297,8 +267,8 @@ mod_diversity_server <- function(input, output, session, parent_session){
diversity_items$pos_df$POS <- as.numeric(diversity_items$pos_df$POS)
# Sort the dataframe and pad with a 0 if only a single digit is provided
diversity_items$pos_df$CHROM <- ifelse(
nchar(diversity_items$pos_df$CHROM) == 1,
paste0("0", diversity_items$pos_df$CHROM),
nchar(diversity_items$pos_df$CHROM) == 1,
paste0("0", diversity_items$pos_df$CHROM),
diversity_items$pos_df$CHROM
)
diversity_items$pos_df <- diversity_items$pos_df[order(diversity_items$pos_df$CHROM), ]
Expand Down Expand Up @@ -346,7 +316,12 @@ mod_diversity_server <- function(input, output, session, parent_session){
})

output$maf_plot <- renderPlot({
maf_plot()
validate(
need(!is.null(diversity_items$maf_df) & !is.null(input$hist_bins), "Input VCF, define parameters and click `run analysis` to access results in this session.")
)

hist(diversity_items$maf_df$MAF, breaks = as.numeric(input$hist_bins), col = "grey", border = "black", xlab = "Minor Allele Frequency (MAF)",
ylab = "Frequency", main = "Minor Allele Frequency Distribution")
})

sample_table <- reactive({
Expand Down Expand Up @@ -393,12 +368,15 @@ mod_diversity_server <- function(input, output, session, parent_session){
# Conditional plotting based on input selection
if (input$div_figure == "Dosage Plot") {
print(box_plot())
} else if (input$div_figure == "AF Histogram") {
af_plot()
} else if (input$div_figure == "MAF Histogram") {
maf_plot()
hist(diversity_items$maf_df$MAF, breaks = as.numeric(input$hist_bins), col = "grey", border = "black", xlab = "Minor Allele Frequency (MAF)",
ylab = "Frequency", main = "Minor Allele Frequency Distribution")
} else if (input$div_figure == "OHet Histogram") {
het_plot()
hist(diversity_items$het_df$ObservedHeterozygosity, breaks = as.numeric(input$hist_bins), col = "tan3", border = "black", xlim= c(0,1),
xlab = "Observed Heterozygosity",
ylab = "Number of Samples",
main = "Sample Observed Heterozygosity")
axis(1, at = seq(0, 1, by = 0.1), labels = TRUE)
} else if (input$div_figure == "Marker Plot") {
print(marker_plot())
}
Expand Down Expand Up @@ -456,4 +434,4 @@ mod_diversity_server <- function(input, output, session, parent_session){
# mod_diversity_ui("diversity_1")

## To be copied in the server
# mod_diversity_server("diversity_1")
# mod_diversity_server("diversity_1")
Loading

0 comments on commit a5a6852

Please sign in to comment.