Skip to content

Commit

Permalink
Merge pull request #122 from Cristianetaniguti/main
Browse files Browse the repository at this point in the history
Avoid HTML + export bgzipped
  • Loading branch information
alex-sandercock authored Feb 20, 2025
2 parents c79849e + e90ba96 commit e32ac39
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 186 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ Imports:
Matrix,
matrixcalc,
markdown,
Rsamtools
Rsamtools,
methods
Remotes:
github::jendelman/GWASpoly,
github::Breeding-Insight/BIGr,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ importFrom(httr,GET)
importFrom(httr,content)
importFrom(httr,status_code)
importFrom(matrixcalc,is.positive.definite)
importFrom(methods,new)
importFrom(plotly,add_markers)
importFrom(plotly,ggplotly)
importFrom(plotly,layout)
Expand Down
22 changes: 14 additions & 8 deletions R/DosageCall_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@
#' @param backcross.gen ToDo
#' @param intercross.gen ToDo
#' @param selfing.gen ToDo
#' @param min_ind_read ToDo
#' @param min_ind_maf ToDo
#' @param contamRate ToDo
#' @param tol ToDo
#' @param session ToDo
#'
#' @import polyRAD
#' @importFrom vcfR read.vcfR is.biallelic write.vcf
#' @importFrom R.utils gunzip
#'
polyRAD_dosage_call <- function(vcf, ploidy, model, p1 = NULL, p2 = NULL, backcross.gen = 0, intercross.gen = 0, selfing.gen = 0, contamRate = 0.001, min_ind_read = 1, min_ind_maf = 0, tol = 1e-05, session) {
polyRAD_dosage_call <- function(vcf, ploidy, model, p1 = NULL, p2 = NULL,
backcross.gen = 0, intercross.gen = 0, selfing.gen = 0,
contamRate = 0.001, min_ind_read = 1, min_ind_maf = 0,
tol = 1e-05, session) {

# Variables
vcf_path <- vcf
Expand All @@ -28,36 +34,36 @@ polyRAD_dosage_call <- function(vcf, ploidy, model, p1 = NULL, p2 = NULL, backcr
# Having some issues formatting the output when multiallelic SNPs is input, so excluding for now
temp_vcf <- vcfR::read.vcfR(vcf_path, verbose = FALSE)
temp_vcf <- temp_vcf[is.biallelic(temp_vcf),]

# Function to randomly assign non-matching bases for polyRAD
assign_random_bases <- function(vcf) {
# Get the number of rows in the VCF
num_rows <- nrow(vcf@fix)

# Generate random bases for REF and ALT
bases <- c("A", "C", "G", "T")
ref_bases <- sample(bases, num_rows, replace = TRUE)
alt_bases <- character(num_rows) # Initialize an empty character vector

# Ensure REF and ALT are different for each row
for (i in 1:num_rows) {
alt_bases[i] <- sample(bases[bases != ref_bases[i]], 1)
}

# Assign the new bases to the vcf object
vcf@fix[, "REF"] <- ref_bases
vcf@fix[, "ALT"] <- alt_bases

return(vcf)
}

#Editing REF and ALT fields randomly temporarily if blank or AB
ref_base <- temp_vcf@fix[1, "REF"]
alt_base <- temp_vcf@fix[1, "ALT"]
if (is.na(ref_base) || is.na(alt_base) || alt_base == "B") {
temp_vcf <- assign_random_bases(temp_vcf)
}

# Adding filtered VCF as a temp object
temp_vcf_path <- tempfile(fileext = ".vcf.gz")
vcfR::write.vcf(temp_vcf, file = temp_vcf_path)
Expand Down
38 changes: 14 additions & 24 deletions R/mod_DosageCall.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ mod_DosageCall_ui <- function(id){
)
),
column(width=4,
valueBoxOutput(ns("MADCsnps"), width=12),
box(title = "Status", width = 12, collapsible = TRUE, status = "info",
progressBar(id = ns("pb_madc"), value = 0, status = "info", display_pct = TRUE, striped = TRUE, title = " ")
)
valueBoxOutput(ns("MADCsnps"), width=12),
box(title = "Status", width = 12, collapsible = TRUE, status = "info",
progressBar(id = ns("pb_madc"), value = 0, status = "info", display_pct = TRUE, striped = TRUE, title = " ")
)
)
)
)
Expand Down Expand Up @@ -256,15 +256,15 @@ mod_DosageCall_server <- function(input, output, session, parent_session){
output$MADCsnps <- renderValueBox({
valueBox(snp_number(), "Markers in uploaded file", icon = icon("dna"), color = "info")
})

#Default model choices
advanced_options <- reactiveValues(
contamRate = 0.001,
min_ind_read = 1,
min_ind_maf = 0,
tol = 1e-05
)

#UI popup window for input
observeEvent(input$advanced_options, {
showModal(modalDialog(
Expand Down Expand Up @@ -310,17 +310,17 @@ mod_DosageCall_server <- function(input, output, session, parent_session){
)
))
})



#Close popup window when user "saves options"
observeEvent(input$save_advanced_options, {
advanced_options$contamRate <- input$contamRate
advanced_options$min_ind_read <- input$min_ind_read
advanced_options$min_ind_maf <- input$min_ind_maf
advanced_options$tol <- input$tol
# Save other inputs as needed

removeModal() # Close the modal after saving
})

Expand Down Expand Up @@ -555,12 +555,7 @@ mod_DosageCall_server <- function(input, output, session, parent_session){
output$download_updog_vcf <- downloadHandler(
filename = function() {
output_name <- gsub("\\.vcf$", "", input$output_name)
if (input$Rpackage == "Updog") {
paste0(output_name, ".vcf.gz")
}else {
paste0(output_name, ".vcf")
}

paste0(output_name, ".vcf.gz")
},
content = function(file) {

Expand All @@ -571,24 +566,19 @@ mod_DosageCall_server <- function(input, output, session, parent_session){
multidog.object = updog_out(),
output.file = temp,
updog_version = packageVersion("updog"),
compress = TRUE
compress = FALSE
)

# Move the file to the path specified by 'file'
file.copy(paste0(temp, ".vcf.gz"), file)

} else {
polyRAD2vcf(updog_out()$Genos,
model = input$polyRAD_model,
vcf_path = input$madc_file$datapath,
hindhe.obj = updog_out()$RADHindHe,
ploidy = input$ploidy,
output.file = temp
)

# Move the file to the path specified by 'file'
file.copy(paste0(temp, ".vcf"), file)
)
}
bgzip_compress(paste0(temp, ".vcf"), file)

# Delete the temporary file
unlink(temp)
Expand Down
34 changes: 19 additions & 15 deletions R/mod_Filtering.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ mod_Filtering_server <- function(input, output, session, parent_session){
format_fields = NULL

)

# Function to choose user selected dataset
current_hist_data <- reactive({
req(input$vcf_type)

# Switch between 'pre-filtered' or 'filtered' data based on user choice
if (input$vcf_type == "Pre-Filter VCF") {
list(
snp_miss = filtering_files$raw_snp_miss_df,
snp_miss = filtering_files$raw_snp_miss_df,
sample_miss = filtering_files$raw_sample_miss_df,
maf_data = filtering_files$raw_maf_df
)
Expand Down Expand Up @@ -226,7 +226,7 @@ mod_Filtering_server <- function(input, output, session, parent_session){
}

req(input$filter_ploidy, input$filter_output_name,input$updog_rdata)

#Status
updateProgressBar(session = session, id = "pb_filter", value = 10, title = "Processing VCF file")

Expand Down Expand Up @@ -289,9 +289,9 @@ mod_Filtering_server <- function(input, output, session, parent_session){
starting_samples <- ncol(gt_matrix)
filtering_files$raw_snp_miss_df <- rowMeans(is.na(gt_matrix)) #SNP missing values
filtering_files$raw_sample_miss_df <- as.numeric(colMeans(is.na(gt_matrix))) #Sample missing values

rm(gt_matrix) #Remove gt matrix

# Filtered VCF
vcf <- filterVCF(vcf.file = vcf,
ploidy=ploidy,
Expand Down Expand Up @@ -348,7 +348,7 @@ mod_Filtering_server <- function(input, output, session, parent_session){
color = "info"
)
})

#User warning if samples were removed during filtering
sample_removed <- starting_samples - final_samples
if (sample_removed > 0) {
Expand All @@ -370,21 +370,21 @@ mod_Filtering_server <- function(input, output, session, parent_session){

# Status
updateProgressBar(session = session, id = "pb_filter", value = 100, title = "Finished!")


vcf
})

#Update plots
output$din_tabs <- renderUI({

if (input$run_filters == 0) {
tabBox(width =12, collapsible = FALSE, status = "info",
id = "updog_tab", height = "600px",
tabPanel("Results", p("Upload VCF file to access results in this section."))
)
} else {

if (!is.null(filtering_files$format_fields) && filtering_files$format_fields == TRUE && input$vcf_type == "Pre-Filter VCF") {
# Include "Bias Histogram", "OD Histogram", and "Prop_mis Histogram" for Pre-Filtered VCF
tabBox(
Expand Down Expand Up @@ -426,12 +426,16 @@ mod_Filtering_server <- function(input, output, session, parent_session){
temp_file <- tempfile(fileext = ".vcf.gz")
write.vcf(vcf(), file = temp_file)

# Avoid exporting gziped instead of bgziped
gunzip(temp_file)
temp_file <- gsub(".gz", "", temp_file)

# Check if the VCF file was created
if (file.exists(temp_file)) {
cat("VCF file created successfully.\n")

# Move the file to the path specified by 'file'
file.copy(temp_file, file, overwrite = TRUE)
bgzip_compress(temp_file, file)

# Delete the temporary file
unlink(temp_file)
Expand Down Expand Up @@ -542,7 +546,7 @@ mod_Filtering_server <- function(input, output, session, parent_session){
xlim = c(0,1),
breaks = as.numeric(input$hist_bins))
axis(1, at = seq(0, 1, by = .1), labels = rep("", length(seq(0, 1, by = 0.1)))) # Add ticks

# Add vertical lines
abline(v = mean(as.numeric(current_hist_data()$snp_miss)), col = "red", lty = 2) # Mean line
abline(v = median(as.numeric(current_hist_data()$snp_miss)), col = "green", lty = 2) # Median line
Expand All @@ -562,7 +566,7 @@ mod_Filtering_server <- function(input, output, session, parent_session){
xlim = c(0,1),
breaks = as.numeric(input$hist_bins))
axis(1, at = seq(0, 1, by = .1), labels = rep("", length(seq(0, 1, by = 0.1)))) # Add ticks

# Add vertical lines
abline(v = mean(as.numeric(current_hist_data()$sample_miss)), col = "red", lty = 2) # Mean line
abline(v = median(as.numeric(current_hist_data()$sample_miss)), col = "green", lty = 2) # Median line
Expand Down Expand Up @@ -678,7 +682,7 @@ mod_Filtering_server <- function(input, output, session, parent_session){
#Missing data
output$missing_snp_hist <- renderPlot({
req(current_hist_data()$snp_miss)

#Histogram
hist(
as.numeric(current_hist_data()$snp_miss),
Expand Down
Loading

0 comments on commit e32ac39

Please sign in to comment.