Skip to content

Commit

Permalink
Merge pull request #100 from Breeding-Insight/svg_add
Browse files Browse the repository at this point in the history
polyRAD Bug Fix
  • Loading branch information
alex-sandercock authored Feb 13, 2025
2 parents 86fa4a8 + c31cee4 commit 55bfa85
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion R/DosageCall_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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

0 comments on commit 55bfa85

Please sign in to comment.