Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: BIGr
Title: Breeding Insight Genomics Functions for Polyploid and Diploid
Species
Version: 0.10.0
Version: 0.11.0
Authors@R: c(
person("Alexander M.", "Sandercock", , "sandercock.alex@gmail.com", role = c("cre", "aut")),
person("Cristiane", "Taniguti", role = "aut"),
Expand Down
240 changes: 126 additions & 114 deletions NEWS.md

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions R/dosage2vcf.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#' @param dart.counts Path to the DArT counts .csv file. Typically contains "Counts" in the file name.
#' @param ploidy The ploidy of the species being analyzed
#' @param output.file output file name and path
#' @param vcf_header optional path or URL to a VCF header file. Only its ##reference and ##contig lines are added to the output VCF header
#' @param reference optional reference genome ID for the ##reference line (e.g. "GCF_020283565.1"). Replaces the ##reference line of vcf_header if both are given. Without vcf_header or reference, the ##reference and ##contig lines are left out with a warning
#' @return A vcf file
#' @import dplyr
#' @importFrom readr read_csv
Expand All @@ -30,18 +32,20 @@
#' #Temp location (only for example)
#' output_file <- tempfile()
#'
#' #The reference genome ID is written to the ##reference line of the VCF header
#' dosage2vcf(dart.report = system.file("iris_DArT_Allele_Dose_Report_small.csv", package = "BIGr"),
#' dart.counts = system.file("iris_DArT_Counts_small.csv", package = "BIGr"),
#' ploidy = 2,
#' output.file = output_file)
#' output.file = output_file,
#' reference = "example_genome_v1")
#'
#' # Removing the output for the example
#' rm(output_file)
#'
#' ##The function will output the converted VCF using information from the DArT files
#'
#' @export
dosage2vcf <- function(dart.report, dart.counts, ploidy, output.file) {
dosage2vcf <- function(dart.report, dart.counts, ploidy, output.file, vcf_header = NULL, reference = NULL) {

dosage_report <- dart.report
counts_file <- dart.counts
Expand All @@ -50,12 +54,14 @@ dosage2vcf <- function(dart.report, dart.counts, ploidy, output.file) {

message("Reading input files\n")

#Reference genome lines for the header
ref_lines <- vcf_reference_lines(vcf_header, reference)

#Make a header separate from the dataframe
vcf_header <- c(
"##fileformat=VCFv4.3",
paste0("##BIGr_Dosage2VCF=",packageVersion("BIGr")),
"##reference=NA",
"##contig=<ID=NA,length=NA>",
ref_lines$lines,
'##INFO=<ID=DP,Number=1,Type=Integer,Description="Total Depth">',
'##INFO=<ID=ADS,Number=R,Type=Integer,Description="Depths for the ref and each alt allele in the order listed">',
'##INFO=<ID=BIAS,Number=1,Type=Float,Description="The estimated allele bias of the SNP from updog">',
Expand Down Expand Up @@ -526,6 +532,8 @@ dosage2vcf <- function(dart.report, dart.counts, ploidy, output.file) {
# Add # to the CHROM column name
colnames(vcf_df)[1] <- "#CHROM"

check_vcf_contigs(vcf_df[[1]], ref_lines$contigs)

# Write the header to the file
writeLines(vcf_header, con = output.file)

Expand Down
32 changes: 30 additions & 2 deletions R/filterVCF.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@
#' returned. `.vcf.gz` is appended when `vcf.file` is a path, and
#' `_filtered.vcf.gz` when `vcf.file` is a vcfR object. When not supplied, a
#' vcfR object is returned instead and no file is written.
#' @param vcf_header Path or URL to a VCF header file with the `##reference` and
#' `##contig` lines of the reference genome (optional), such as the VCF header
#' files provided with the microhaplotype databases. Its `##contig` lines
#' replace those of the input VCF, and so does its `##reference` line unless
#' `reference` is given. The other lines of the file are not used. A warning is
#' given if a `CHROM` in the output is not listed in the `##contig` lines.
#' @param reference Reference genome ID written to the `##reference` line,
#' replacing the one of the input VCF (optional), e.g. `"GCF_020283565.1"`.
#' When neither `vcf_header` nor `reference` is given, the reference genome lines
#' of the input VCF are kept, except the `##reference=NA` and
#' `##contig=<ID=NA,length=NA>` lines written by earlier versions of BIGr, which
#' are removed.
#' @return A vcfR object when `output.file` is not supplied. Otherwise the
#' filtered VCF is written to disk and nothing is returned.
#' @importFrom vcfR read.vcfR
Expand Down Expand Up @@ -92,7 +104,9 @@ filterVCF <- function(vcf.file,
filter.SAMPLE.miss = NULL,
filter.SNP.miss = NULL,
ploidy,
output.file = NULL) {
output.file = NULL,
vcf_header = NULL,
reference = NULL) {

#Should allow for any INFO field to be entered to be filtered

Expand Down Expand Up @@ -127,6 +141,9 @@ filterVCF <- function(vcf.file,
filter.SAMPLE.miss <- validate_filter_number(filter.SAMPLE.miss, "filter.SAMPLE.miss", proportion = TRUE)
filter.SNP.miss <- validate_filter_number(filter.SNP.miss, "filter.SNP.miss", proportion = TRUE)

#Reference genome lines for the header (checked before reading the VCF)
ref_lines <- vcf_reference_lines(vcf_header, reference, warn = FALSE)

# Import VCF (can be .vcf or .vcf.gz)
if (!inherits(vcf.file, "vcfR")) {
vcf <- read.vcfR(vcf.file, verbose = FALSE)
Expand All @@ -146,7 +163,9 @@ filterVCF <- function(vcf.file,
filter.MAF = filter.MAF,
filter.SAMPLE.miss = filter.SAMPLE.miss,
filter.SNP.miss = filter.SNP.miss,
ploidy = ploidy
ploidy = ploidy,
vcf_header = vcf_header,
reference = reference
)

# Filter out NULL values
Expand Down Expand Up @@ -325,6 +344,15 @@ filterVCF <- function(vcf.file,
"This happens when no variant has any called genotypes, which the filter.DP and filter.MPP masking can cause."), ]
}
### Export the modified VCF file (this exports as a .vcf.gz, so make sure to have the name end in .vcf.gz)
#Update the reference genome lines of the header. Placeholder lines from earlier
#versions of BIGr are removed, since they break tools that read the header (e.g., GIGWA)
placeholder <- vcf@meta == "##reference=NA" | grepl("^##contig=<ID=NA[,>]", vcf@meta)
if (any(placeholder)) {
message("Removing ##reference=NA and ##contig=<ID=NA,length=NA> lines from the header\n")
}
vcf@meta <- update_vcf_reference_lines(vcf@meta[!placeholder], ref_lines)
check_vcf_contigs(vcf@fix[, "CHROM"], ref_lines$contigs)

message("Exporting VCF\n")
if (!inherits(vcf.file, "vcfR")) {
if (!is.null(output.file)) {
Expand Down
25 changes: 19 additions & 6 deletions R/madc2vcf_all.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#' @param others_rm_with_indels A logical value. If TRUE, Other alleles that contain insertions or deletions relative to the Ref sequence (as detected by pairwise alignment) are discarded. Default is TRUE.
#' @param others_min_dist An integer or NULL. Distance in base pairs below which two SNPs in an Other allele are considered close to each other. Used together with `others_max_close_snps`. Default is 5.
#' @param others_max_close_snps An integer or NULL. If not NULL, Other alleles with more than this many SNPs that are closer than `others_min_dist` base pairs to each other are discarded. The SNPs are counted before the target position and positions with N or IUPAC codes are removed. The filter is not applied if either `others_min_dist` or `others_max_close_snps` is NULL. Default is 3.
#' @param vcf_header A string specifying the path or URL to a VCF header file with the `##reference` and `##contig` lines of the reference genome, such as the VCF header files provided with the microhaplotype databases. Only the `##reference` and `##contig` lines are used; the `##fileformat`, `##INFO` and `##FORMAT` lines are written by the function. A warning is given if a `CHROM` in the output is not listed in the `##contig` lines. Default is NULL.
#' @param reference A string with the reference genome ID written to the `##reference` line (e.g. `"GCF_020283565.1"`). Use it when no `vcf_header` file is available, or to replace the `##reference` line of `vcf_header`. If neither `vcf_header` nor `reference` is given, the `##reference` and `##contig` lines are left out of the header with a warning. Default is NULL.
#' @param verbose A logical value indicating whether to print metrics and progress to the console. Default is TRUE.
#'
#' @return This function does not return an R object. It writes the processed VCF file v4.3 to the specified `out_vcf` path.
Expand Down Expand Up @@ -59,6 +61,7 @@
#' multiallelic_SNP_sample_thr = 5,
#' alignment_score_thr = 40,
#' out_vcf = output_file,
#' reference = "example_genome_v1",
#' verbose = TRUE
#' )
#'
Expand All @@ -84,6 +87,8 @@ madc2vcf_all <- function(madc,
others_rm_with_indels = TRUE,
others_min_dist = 5,
others_max_close_snps = 3,
vcf_header = NULL,
reference = NULL,
verbose = TRUE){
Comment on lines 89 to 92


Expand All @@ -108,6 +113,8 @@ madc2vcf_all <- function(madc,
vmsg("others_min_dist : %s", verbose = verbose, level = 1, type = ">>", if (is.null(others_min_dist)) "NULL" else others_min_dist)
vmsg("others_max_close_snps : %s", verbose = verbose, level = 1, type = ">>", if (is.null(others_max_close_snps)) "NULL" else others_max_close_snps)
vmsg("out_vcf : %s", verbose = verbose, level = 1, type = ">>", if (is.null(out_vcf)) "NULL" else out_vcf)
vmsg("vcf_header : %s", verbose = verbose, level = 1, type = ">>", if (is.null(vcf_header)) "NULL" else vcf_header)
vmsg("reference : %s", verbose = verbose, level = 1, type = ">>", if (is.null(reference)) "NULL" else reference)
vmsg("Checking inputs", verbose = verbose, level = 0, type = ">>")

# Input checks
Expand Down Expand Up @@ -137,7 +144,7 @@ madc2vcf_all <- function(madc,

bigr_meta <- paste0('##BIGrCommandLine.madc2vcf_all=<ID=madc2vcf_all,Version="',
packageVersion("BIGr"), '",Data="',
Sys.time(),'", CommandLine="> madc2vcf_all(',deparse(substitute(madc)),', ',
Sys.time(),'",CommandLine="', vcf_quote_escape(paste0('> madc2vcf_all(',deparse1(substitute(madc)),', ',
"botloci_file= ", botloci_file, ', ',
"hap_seq_file= ", hap_seq_file, ', ',
"n.cores= ", n.cores, ', ',
Expand All @@ -152,7 +159,12 @@ madc2vcf_all <- function(madc,
"others_min_dist= ", others_min_dist, ', ',
"others_max_close_snps= ", others_max_close_snps, ', ',
"out_vcf= ", out_vcf, ', ',
"verbose= ", verbose,')">')
"vcf_header= ", vcf_header, ', ',
"reference= ", reference, ', ',
"verbose= ", verbose,')')), '">')

# Reference genome lines for the VCF header
ref_lines <- vcf_reference_lines(vcf_header, reference)

report <- read.csv(madc, check.names = FALSE)
checks <- check_madc_sanity(report)
Expand Down Expand Up @@ -203,7 +215,7 @@ madc2vcf_all <- function(madc,
n_match <- sum(mi_df$CloneID %in% report$CloneID)
n_total <- length(unique(report$CloneID))
if(n_match < n_total)
vmsg("%s of %s MADC CloneIDs found in markers_info. Unmatched markers will be removed", verbose = verbose, level = 1, n_match, n_total)
vmsg("%s of %s MADC CloneIDs found in markers_info. Unmatched markers will be removed", verbose = verbose, level = 1, type = ">>", n_match, n_total)
} else mi_df <- NULL

if(any(!checks$checks[c("ChromPos")])){
Expand Down Expand Up @@ -232,7 +244,7 @@ madc2vcf_all <- function(madc,
mi_df$Type <- ifelse(nchar(as.character(mi_df$Ref)) > 1 | nchar(as.character(mi_df$Alt)) > 1,
"Indel", "SNP")
vmsg("markers_info: 'Type' column not found. Derived from Ref/Alt lengths (%s SNPs, %s Indels)",
verbose = verbose, level = 1,
verbose = verbose, level = 1, type = ">>",
sum(mi_df$Type == "SNP"), sum(mi_df$Type == "Indel"))
}
vmsg("The MADC file contains indels and markers_info file was provided with all required columns",verbose = verbose, level = 1, type = ">>")
Expand Down Expand Up @@ -294,8 +306,7 @@ madc2vcf_all <- function(madc,
#Make a header separate from the dataframe
vcf_header <- c(
"##fileformat=VCFv4.3",
"##reference=NA",
"##contig=<ID=NA,length=NA>",
ref_lines$lines,
'##INFO=<ID=DP,Number=1,Type=Integer,Description="Total Depth">',
'##INFO=<ID=ADS,Number=R,Type=Integer,Description="Depths for the ref and each alt allele in the order listed">',
'##FORMAT=<ID=DP,Number=1,Type=Integer,Description="Read depth">',
Expand All @@ -304,6 +315,8 @@ madc2vcf_all <- function(madc,
bigr_meta
)

check_vcf_contigs(vcf_body[[1]], ref_lines$contigs)

vcf_term <- sapply(strsplit(out_vcf, "[.]"), function(x) x[length(x)])
if(length(vcf_term) != 0) if(vcf_term != "vcf") out_vcf <- paste0(out_vcf,".vcf")

Expand Down
Loading
Loading