diff --git a/DESCRIPTION b/DESCRIPTION index 0a1c846..ef408fd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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"), diff --git a/NEWS.md b/NEWS.md index 0effd09..7ad7ed9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,166 +1,178 @@ -# BIGr 0.10.0 +# BIGr 0.11.0 -* New `madc_summary()`: read-only summary tables for a fixed allele ID MADC - a per-marker microhaplotype frequency distribution, per-sample and per-marker total read depth (`depth`) and on-target `|Ref`/`|Alt` depth (`target_depth`) alongside missing rate, number of mHaps, and off-target read fraction, a missingness threshold sweep, an overall summary, and optional aggregation by a sample-metadata category (e.g. species). -* New `madc_plot()`: visualize a fixed allele ID MADC via a `plot.type` selector - a PCA of alternative read ratios (color by a metadata category, optional second variable by point `shape.col`, custom `palette`), a linear marker-distribution genome plot, a markers x samples read-depth/#mHaps heatmap (failed cells black; depth on a colorblind-safe diverging scale; samples sorted/labeled by category and optionally faceted by chromosome), a per-sample missing-data boxplot (optionally ordered by median missing rate via `miss.sort` and drawn horizontally via `horizontal`), and a publication-oriented `circos` circular plot. The `circos` figure carries four concentric tracks - dominant per-marker locus ticks with a distinct Mb coordinate axis ("Loci"), a marker-density heatmap ring ("Density", window set by `density.window`), log-scaled #mHaps two-tone bars - a neutral base with a red over-threshold segment above a dashed reference ring - flagging paralog-suspect loci ("mHaps", threshold `paralog.flag`), and a gapless depth ribbon flagging problematic and high-depth-outlier loci ("Depth") - identified by horizontal track titles in a 12 o'clock corridor (`label.gap`) with a single compact legend in the center; track colors are customizable (`mhap.col`, `depth.col`, `density.col`). The "Depth" ribbon flags a marker as problematic when its mean depth is below `min.depth` OR its missing rate exceeds `depth.qc.maxmiss` (default 0.5), so high-dropout markers (e.g. 50% zeros / 50% deep) that a mean-depth cut alone reads as normal are caught. Requesting several (non-circos) types returns a multi-panel figure; figures can be saved via `output.file`. Plotting uses `ggplot2` + base `grid`; the `circos` plot uses the suggested `circlize` package. -* `madc_plot()` also provides two QC-focused `plot.type`s: `"balance"`, a ploidy-aware allele read-ratio diagnostic (alt read ratio vs target depth as a 2D density), and `"depth"`, a log-scaled marker mean-depth (uniformity) distribution with a `min.depth` reference line and median/IQR. The `"balance"` plot shows every cell with a defined ratio (including below `min.depth`, marked by a dotted reference line) and, when `ploidy` is given, dashed guides at the expected `(0:ploidy)/ploidy` ratios plus a depth-dependent 95% binomial expectation band around each interior expected ratio (the spread of `A/D` under `Binomial(D, ratio)`), so allele-balance dispersion wider than sequencing-depth sampling noise stands out directly. `balance.density = TRUE` stacks a marginal histogram of the alt-read ratio above the heatmap (returning a composite grob). Heatmap sub-sampling (`max.loci`/`max.samples`) is now deterministic (evenly spaced) so repeated calls give identical figures. -* `madc_plot(titles = FALSE)` drops the title and subtitle from every requested plot (including the `circos` figure, the multi-panel, and the `balance.density` composite), leaving axis labels, legends, and the `"balance"` caption - for figures captioned externally (manuscript, report, slide). Default `TRUE` keeps the current behavior. -* `madc_summary()` additions: per-sample/per-marker depth is summarized with robust statistics (`median_depth`, `depth_mad`, `depth_q10`, `depth_q90`) alongside the mean, plus `total_reads`/`target_reads` and `call_rate`; off-target is reported both as the mean per-locus ratio (`offtarget_fraction`) and read-weighted `sum(offtarget)/sum(total)` (`offtarget_fraction_weighted`, the "% of reads off target" number); the ambiguous mHap count is split into `n_mhaps_defined` (allele rows in the panel) and `n_mhaps_observed` (alleles actually supported by reads). Two new tables: `depth_sweep` (a read-depth threshold sweep mirroring the missingness sweep) and `marker_qc` (advisory per-marker `PASS`/`FLAG`/`FAIL` status with a reason string, thresholds `min.locus.depth`/`max.locus.depth`/`max.missing`/`max.offtarget`/`max.mhaps.per.loci` reusing `filterMADC`'s vocabulary; it classifies without removing). Tables can be written as per-table CSVs or, with `output.format = "xlsx"`, a single workbook with one sheet per table (requires the suggested `writexl` package). -* `filterMADC` updates: - * Now only accepts fixed allele ID MADC files (processed through HapApp). The input is validated with `check_madc_sanity` and raw DArT MADC files (or any file without fixed AlleleIDs) are rejected with an error. This also fixes a crash on raw MADC files caused by non-unique AlleleIDs. - * Removed the `n.summary.columns` argument (breaking change). Fixed allele ID MADC has a fixed column layout, so summary columns no longer need to be specified or detected. - * Replaced the per-mhap `min.mean.reads`/`max.mean.reads` arguments with a per-locus depth window `min.locus.depth`/`max.locus.depth` (breaking change). Depth is the mean reads per sample at a locus (sum of all mhap reads at the locus divided by the number of samples), and loci outside the window are removed whole (Ref and Alt together) - e.g. `max.locus.depth` to exclude paralogous over-amplification. - * `max.mhaps.per.loci` and `target.only` now retain strictly the target `|Ref`/`|Alt` alleles at affected loci, also removing `|Other` alleles (previously only `|RefMatch`/`|AltMatch` were removed). - * The `min.ind.with.reads` presence filter now protects the target `|Ref`/`|Alt` alleles, only pruning non-target (`|RefMatch`/`|AltMatch`/`|Other`) mhaps, so a locus never loses its Ref/Alt pair. -* Fixed `madc2vcf_targets()` removing markers with no match in `markers_info` as invalid POS values, without the warning "markers removed: no matching entry found in markers_info". The POS correction now runs after these markers are removed and reported. -* Fixed `filterVCF()` deciding whether to apply `filter.DP` and `filter.MPP` from the FORMAT column of the first record only. The FORMAT fields are now read from every record, and the missing call written for a filtered genotype matches the FORMAT of its own record. Calls without a DP or MPP value are left unchanged. -* `filterVCF()` now gives an error when `filter.SAMPLE.miss` or `filter.SNP.miss` is outside 0 to 1, so a percentage such as `20` is no longer accepted as a threshold that keeps every sample or variant. -* `madc2vcf_all()` now records `markers_info`, `add_others`, `others_max_snps`, `others_rm_with_indels`, `others_min_dist`, and `others_max_close_snps` in the `##BIGrCommandLine` header line of the VCF. -* `updog2vcf()` now removes SNPs whose position cannot be read from the SNP ID as a positive integer (expected format CHR_POS), with a warning, instead of writing a POS of `0` or `NA` to the VCF. +- `madc2vcf_multi()` fixes: + - Fixed the error "missing value where TRUE/FALSE needed" when the best overdispersion value was the lowest value tested. This error comes from `polyRAD::TestOverdispersion()` (polyRAD 2.0.0). `madc2vcf_multi()` now catches it and tests lower values. When the best value is at the low or high end of the range tested, the range is moved and tested again. If no value inside the range is found, the best value found is used. + - Fixed the error "alleleDepth must be in integer format" when the MADC read counts are written as decimals (e.g. `12.0`). Whole-number counts are now converted to integers before the MADC is loaded into polyRAD. An error is given if a count is not a whole number. + - Fixed the error "'start' or 'width' cannot contain NAs" when writing the VCF with CloneIDs that do not follow the Chr_Pos format. The `Chr` and `Pos` columns of `markers_info` are now used for the marker positions, also when the botloci IDs match the MADC CloneIDs. An error naming the markers is given if a position still cannot be found. -# BIGr 0.9.0 -* `check_madc_sanity` updates: distinguish presence of IUPAC codes on REF/ALT (return logical variable IUPACcodes) from RefMatch/AltMatch/Others (returned logical variable IUPACcodes_MatchAlleles) and from Identical IUPAC code in identical positions in REF/ALT (returned logical variable IUPACcodes_IdenticalRefAlt) -* Two new arguments to the `madc2vcf_all` function: - * `others_min_dist` (default 5bp) - * `others_max_close_snps` (default 3 SNPs) +# BIGr 0.10.0 + +- New `madc_summary()`: read-only summary tables for a fixed allele ID MADC - a per-marker microhaplotype frequency distribution, per-sample and per-marker total read depth (`depth`) and on-target `|Ref`/`|Alt` depth (`target_depth`) alongside missing rate, number of mHaps, and off-target read fraction, a missingness threshold sweep, an overall summary, and optional aggregation by a sample-metadata category (e.g. species). +- New `madc_plot()`: visualize a fixed allele ID MADC via a `plot.type` selector - a PCA of alternative read ratios (color by a metadata category, optional second variable by point `shape.col`, custom `palette`), a linear marker-distribution genome plot, a markers x samples read-depth/#mHaps heatmap (failed cells black; depth on a colorblind-safe diverging scale; samples sorted/labeled by category and optionally faceted by chromosome), a per-sample missing-data boxplot (optionally ordered by median missing rate via `miss.sort` and drawn horizontally via `horizontal`), and a publication-oriented `circos` circular plot. The `circos` figure carries four concentric tracks - dominant per-marker locus ticks with a distinct Mb coordinate axis ("Loci"), a marker-density heatmap ring ("Density", window set by `density.window`), log-scaled #mHaps two-tone bars - a neutral base with a red over-threshold segment above a dashed reference ring - flagging paralog-suspect loci ("mHaps", threshold `paralog.flag`), and a gapless depth ribbon flagging problematic and high-depth-outlier loci ("Depth") - identified by horizontal track titles in a 12 o'clock corridor (`label.gap`) with a single compact legend in the center; track colors are customizable (`mhap.col`, `depth.col`, `density.col`). The "Depth" ribbon flags a marker as problematic when its mean depth is below `min.depth` OR its missing rate exceeds `depth.qc.maxmiss` (default 0.5), so high-dropout markers (e.g. 50% zeros / 50% deep) that a mean-depth cut alone reads as normal are caught. Requesting several (non-circos) types returns a multi-panel figure; figures can be saved via `output.file`. Plotting uses `ggplot2` + base `grid`; the `circos` plot uses the suggested `circlize` package. +- `madc_plot()` also provides two QC-focused `plot.type`s: `"balance"`, a ploidy-aware allele read-ratio diagnostic (alt read ratio vs target depth as a 2D density), and `"depth"`, a log-scaled marker mean-depth (uniformity) distribution with a `min.depth` reference line and median/IQR. The `"balance"` plot shows every cell with a defined ratio (including below `min.depth`, marked by a dotted reference line) and, when `ploidy` is given, dashed guides at the expected `(0:ploidy)/ploidy` ratios plus a depth-dependent 95% binomial expectation band around each interior expected ratio (the spread of `A/D` under `Binomial(D, ratio)`), so allele-balance dispersion wider than sequencing-depth sampling noise stands out directly. `balance.density = TRUE` stacks a marginal histogram of the alt-read ratio above the heatmap (returning a composite grob). Heatmap sub-sampling (`max.loci`/`max.samples`) is now deterministic (evenly spaced) so repeated calls give identical figures. +- `madc_plot(titles = FALSE)` drops the title and subtitle from every requested plot (including the `circos` figure, the multi-panel, and the `balance.density` composite), leaving axis labels, legends, and the `"balance"` caption - for figures captioned externally (manuscript, report, slide). Default `TRUE` keeps the current behavior. +- `madc_summary()` additions: per-sample/per-marker depth is summarized with robust statistics (`median_depth`, `depth_mad`, `depth_q10`, `depth_q90`) alongside the mean, plus `total_reads`/`target_reads` and `call_rate`; off-target is reported both as the mean per-locus ratio (`offtarget_fraction`) and read-weighted `sum(offtarget)/sum(total)` (`offtarget_fraction_weighted`, the "% of reads off target" number); the ambiguous mHap count is split into `n_mhaps_defined` (allele rows in the panel) and `n_mhaps_observed` (alleles actually supported by reads). Two new tables: `depth_sweep` (a read-depth threshold sweep mirroring the missingness sweep) and `marker_qc` (advisory per-marker `PASS`/`FLAG`/`FAIL` status with a reason string, thresholds `min.locus.depth`/`max.locus.depth`/`max.missing`/`max.offtarget`/`max.mhaps.per.loci` reusing `filterMADC`'s vocabulary; it classifies without removing). Tables can be written as per-table CSVs or, with `output.format = "xlsx"`, a single workbook with one sheet per table (requires the suggested `writexl` package). +- `filterMADC` updates: + - Now only accepts fixed allele ID MADC files (processed through HapApp). The input is validated with `check_madc_sanity` and raw DArT MADC files (or any file without fixed AlleleIDs) are rejected with an error. This also fixes a crash on raw MADC files caused by non-unique AlleleIDs. + - Removed the `n.summary.columns` argument (breaking change). Fixed allele ID MADC has a fixed column layout, so summary columns no longer need to be specified or detected. + - Replaced the per-mhap `min.mean.reads`/`max.mean.reads` arguments with a per-locus depth window `min.locus.depth`/`max.locus.depth` (breaking change). Depth is the mean reads per sample at a locus (sum of all mhap reads at the locus divided by the number of samples), and loci outside the window are removed whole (Ref and Alt together) - e.g. `max.locus.depth` to exclude paralogous over-amplification. + - `max.mhaps.per.loci` and `target.only` now retain strictly the target `|Ref`/`|Alt` alleles at affected loci, also removing `|Other` alleles (previously only `|RefMatch`/`|AltMatch` were removed). + - The `min.ind.with.reads` presence filter now protects the target `|Ref`/`|Alt` alleles, only pruning non-target (`|RefMatch`/`|AltMatch`/`|Other`) mhaps, so a locus never loses its Ref/Alt pair. +- Fixed `madc2vcf_targets()` removing markers with no match in `markers_info` as invalid POS values, without the warning "markers removed: no matching entry found in markers_info". The POS correction now runs after these markers are removed and reported. +- Fixed `filterVCF()` deciding whether to apply `filter.DP` and `filter.MPP` from the FORMAT column of the first record only. The FORMAT fields are now read from every record, and the missing call written for a filtered genotype matches the FORMAT of its own record. Calls without a DP or MPP value are left unchanged. +- `filterVCF()` now gives an error when `filter.SAMPLE.miss` or `filter.SNP.miss` is outside 0 to 1, so a percentage such as `20` is no longer accepted as a threshold that keeps every sample or variant. +- Fixed the `##BIGrCommandLine` header lines of `madc2vcf_targets()`, `madc2vcf_all()`, `madc2vcf_multi()`, and `updog2vcf()` not following the VCF format when an argument was given as a quoted string (e.g. a file path): double quotes and backslashes in the `CommandLine` value are now escaped, and the space before the `CommandLine` key is removed. +- `madc2vcf_all()` now records `markers_info`, `add_others`, `others_max_snps`, `others_rm_with_indels`, `others_min_dist`, and `others_max_close_snps` in the `##BIGrCommandLine` header line of the VCF. +- New `vcf_header` and `reference` arguments in `madc2vcf_targets()`, `madc2vcf_all()`, `madc2vcf_multi()`, `dosage2vcf()`, `updog2vcf()`, and `filterVCF()` to add the reference genome information to the VCF header: + - `vcf_header`: path or URL to a VCF header file, such as the VCF header files provided with the microhaplotype databases. Its `##reference` and `##contig` lines (reference genome and scaffold lengths) are written to the output VCF. The other lines of the file are not used, since each function writes its own `##fileformat`, `##INFO`, and `##FORMAT` lines. A warning is given if a chromosome in the VCF is not listed in the `##contig` lines. + - `reference`: the reference genome ID, written as the `##reference` line. Used when no header file is available, or to replace the `##reference` line of `vcf_header`. + - When neither is given, the `##reference` and `##contig` lines are left out of the header with a warning. The `##reference=NA` and `##contig=` placeholder lines are no longer written, since they break tools that read the header, such as GIGWA. `filterVCF()` removes these placeholder lines from VCFs written by earlier versions of BIGr and otherwise keeps the reference genome lines of the input VCF. + - The `##BIGrCommandLine` lines are always added to the header. `madc2vcf_multi()` now also adds a `##BIGrCommandLine` line to the header written by polyRAD. +- `updog2vcf()` now writes SNPs whose position cannot be read from the SNP ID as a positive integer (expected format CHR_POS) with the SNP ID as CHROM and `1` as POS, with a warning, instead of writing a POS of `0` or `NA` to the VCF. -* By default, `Other` tags will be discarded if they have more than 3 SNPs with less than 5bp distance between them -* Adapt `madc2vcf_all` code to let pass identical IUPAC code in identical position in REF/ALT but ignore polymorphisms in Match or Other alleles at the same position -* `madc2vcf_targets` and `madc2vcf_multi` will only throw an error if different IUPAC are present in REF/ALT sequences (IUPACcodes = TRUE) +# BIGr 0.9.0 +- `check_madc_sanity` updates: distinguish presence of IUPAC codes on REF/ALT (return logical variable IUPACcodes) from RefMatch/AltMatch/Others (returned logical variable IUPACcodes_MatchAlleles) and from Identical IUPAC code in identical positions in REF/ALT (returned logical variable IUPACcodes_IdenticalRefAlt) +- Two new arguments to the `madc2vcf_all` function: + - `others_min_dist` (default 5bp) + - `others_max_close_snps` (default 3 SNPs) +- By default, `Other` tags will be discarded if they have more than 3 SNPs with less than 5bp distance between them\ +- Adapt `madc2vcf_all` code to let pass identical IUPAC code in identical position in REF/ALT but ignore polymorphisms in Match or Other alleles at the same position +- `madc2vcf_targets` and `madc2vcf_multi` will only throw an error if different IUPAC are present in REF/ALT sequences (IUPACcodes = TRUE) # BIGr 0.8.1 -- Fixed `filterVCF()` discarding SNPs whose INFO values are written in scientific notation. The `OD`, `BIAS`, and `PMC` filters read INFO values with a pattern that accepted only digits and decimal points, so a value such as `PMC=4.78e-07` was read as `4.78` and failed a `filter.PMC = 0.05` threshold that it actually passed by five orders of magnitude. `updog2vcf()` writes these values with `paste0()`, so R's default formatting produces scientific notation for small numbers, and updog's `od` and `prop_mis` are routinely small for good markers - meaning the SNPs most likely to be dropped were the cleanest ones. INFO values are now parsed with `as.numeric()` on the whole field, accepting any valid numeric representation. Field names are also matched against a complete INFO entry rather than as a substring, so a field such as `XOD` is no longer mistaken for `OD`. -- Fixed `filterVCF()` writing corrupt all-`NA` variants when a filter value could not be read. A logical index containing `NA` does not drop a row in R, it inserts a row of `NA`s, so any variant with an unreadable `OD`, `BIAS` or `PMC` value, or an `NA` minor allele frequency, was written into the output with an empty `CHROM`, `POS`, `REF` and `ALT`. The `NA` frequency case was reachable through normal use, because `vcfR::maf()` returns `NA` for a variant left with no called genotypes and the `filter.DP` and `filter.MPP` masking can produce exactly that. These variants are now removed, and the number removed is reported in a warning so that they are never discarded silently. -- `filterVCF()` now removes variants when none of them have a readable value for a requested filter, instead of returning the data unfiltered. Previously the `OD`, `BIAS` and `PMC` filters skipped filtering entirely in that situation and reported only "No valid values found", so a run that filtered nothing could look like a run that succeeded. All of the filters now behave the same way, and the warning names the likely cause so it can be diagnosed. -- Fixed `filterVCF()` skipping a requested `OD`, `BIAS` or `PMC` filter when the first variant in the file did not carry that INFO field. The available INFO fields were read from the first record only, so a field that was absent from that one record disabled its filter for the entire file, with no warning and no indication in the output that filtering had not happened. A field may legally be absent from any individual record, so requested filters are now applied to every record, and records without a usable value are removed and reported. This also removes the last case where the order of records in the file could change the result of filtering. -- Corrected and expanded the documentation for `filterVCF()`. `filter.DP` was described as a total read depth filter applied to each SNP; it is applied to each genotype call using the `DP` value in the FORMAT column, setting calls below the threshold to missing rather than removing variants. `filter.MPP` works the same way and was previously undescribed. The remaining filters now state which field they read and which direction the comparison runs, `filter.SAMPLE.miss` and `filter.SNP.miss` state that they take a proportion rather than a percentage, and `filter.BIAS.min` and `filter.BIAS.max` state that neither has any effect unless both are supplied. The return value is documented as a vcfR object when `output.file` is omitted, rather than always a gzipped file. -- Updated madc2vcf_all and madc2vcf_targets. Before, it was possible for POS to be exported as scientific notation instead of integers, and for negative POS values to be present for off target SNPs. POS are corrected to be integers, and SNPs with a negative POS value are removed. -- Removed the breed composition and pedigree functions `allele_freq_poly()`, `solve_composition_poly()`, `check_ped()`, `find_parentage()`, and `validate_pedigree()` (breaking change). They are now part of the independent BIGpopA package: https://github.com/Breeding-Insight/BIGpopA -- Fixed `madc2vcf_all()` error "invalid substring arguments" that occurred with `add_others = TRUE` when an off-target ("Other") allele aligned to the reference with no mismatch positions remaining after the target SNP position was removed. The reference/alternate base lookups for off-target alleles are now guarded by the existing non-empty check, matching how the off-target Match alleles are already handled. +- Fixed `filterVCF()` discarding SNPs whose INFO values are written in scientific notation. The `OD`, `BIAS`, and `PMC` filters read INFO values with a pattern that accepted only digits and decimal points, so a value such as `PMC=4.78e-07` was read as `4.78` and failed a `filter.PMC = 0.05` threshold that it actually passed by five orders of magnitude. `updog2vcf()` writes these values with `paste0()`, so R's default formatting produces scientific notation for small numbers, and updog's `od` and `prop_mis` are routinely small for good markers - meaning the SNPs most likely to be dropped were the cleanest ones. INFO values are now parsed with `as.numeric()` on the whole field, accepting any valid numeric representation. Field names are also matched against a complete INFO entry rather than as a substring, so a field such as `XOD` is no longer mistaken for `OD`. +- Fixed `filterVCF()` writing corrupt all-`NA` variants when a filter value could not be read. A logical index containing `NA` does not drop a row in R, it inserts a row of `NA`s, so any variant with an unreadable `OD`, `BIAS` or `PMC` value, or an `NA` minor allele frequency, was written into the output with an empty `CHROM`, `POS`, `REF` and `ALT`. The `NA` frequency case was reachable through normal use, because `vcfR::maf()` returns `NA` for a variant left with no called genotypes and the `filter.DP` and `filter.MPP` masking can produce exactly that. These variants are now removed, and the number removed is reported in a warning so that they are never discarded silently. +- `filterVCF()` now removes variants when none of them have a readable value for a requested filter, instead of returning the data unfiltered. Previously the `OD`, `BIAS` and `PMC` filters skipped filtering entirely in that situation and reported only "No valid values found", so a run that filtered nothing could look like a run that succeeded. All of the filters now behave the same way, and the warning names the likely cause so it can be diagnosed. +- Fixed `filterVCF()` skipping a requested `OD`, `BIAS` or `PMC` filter when the first variant in the file did not carry that INFO field. The available INFO fields were read from the first record only, so a field that was absent from that one record disabled its filter for the entire file, with no warning and no indication in the output that filtering had not happened. A field may legally be absent from any individual record, so requested filters are now applied to every record, and records without a usable value are removed and reported. This also removes the last case where the order of records in the file could change the result of filtering. +- Corrected and expanded the documentation for `filterVCF()`. `filter.DP` was described as a total read depth filter applied to each SNP; it is applied to each genotype call using the `DP` value in the FORMAT column, setting calls below the threshold to missing rather than removing variants. `filter.MPP` works the same way and was previously undescribed. The remaining filters now state which field they read and which direction the comparison runs, `filter.SAMPLE.miss` and `filter.SNP.miss` state that they take a proportion rather than a percentage, and `filter.BIAS.min` and `filter.BIAS.max` state that neither has any effect unless both are supplied. The return value is documented as a vcfR object when `output.file` is omitted, rather than always a gzipped file. +- Updated madc2vcf_all and madc2vcf_targets. Before, it was possible for POS to be exported as scientific notation instead of integers, and for negative POS values to be present for off target SNPs. POS are corrected to be integers, and SNPs with a negative POS value are removed. +- Removed the breed composition and pedigree functions `allele_freq_poly()`, `solve_composition_poly()`, `check_ped()`, `find_parentage()`, and `validate_pedigree()` (breaking change). They are now part of the independent BIGpopA package: +- Fixed `madc2vcf_all()` error "invalid substring arguments" that occurred with `add_others = TRUE` when an off-target ("Other") allele aligned to the reference with no mismatch positions remaining after the target SNP position was removed. The reference/alternate base lookups for off-target alleles are now guarded by the existing non-empty check, matching how the off-target Match alleles are already handled. # BIGr 0.7.2 -- Fixed manual text errors +- Fixed manual text errors # BIGr 0.7.1 -- Updated `check_ped()` to return corrected pedigree data in the result list instead of assigning objects to the global environment -- Skipped long remote `madc2vcf_all` integration tests on CRAN while keeping them enabled in GitHub Actions +- Updated `check_ped()` to return corrected pedigree data in the result list instead of assigning objects to the global environment +- Skipped long remote `madc2vcf_all` integration tests on CRAN while keeping them enabled in GitHub Actions # BIGr 0.7.0 ## Updates on `dosage2vcf` -- Added support for DArT SNP/INDEL 1-row and 2-row report formats -- `dosage2vcf` now validates marker and sample sets between report and counts files, then aligns counts to the report order before writing VCF genotypes -- VCF `CHROM` and `POS` are derived from `Chrom`/`ChromPos` when present, otherwise from `MarkerName`; `MarkerName` is retained in the VCF `ID` field -- Missing SNP/INDEL genotype calls (`-`/`NA`) are written as diploid missing genotypes (`./.`) +- Added support for DArT SNP/INDEL 1-row and 2-row report formats +- `dosage2vcf` now validates marker and sample sets between report and counts files, then aligns counts to the report order before writing VCF genotypes +- VCF `CHROM` and `POS` are derived from `Chrom`/`ChromPos` when present, otherwise from `MarkerName`; `MarkerName` is retained in the VCF `ID` field +- Missing SNP/INDEL genotype calls (`-`/`NA`) are written as diploid missing genotypes (`./.`) ## New function `madc2vcf_multi` -- New function `madc2vcf_multi` to convert a DArTag MADC file to a VCF using the polyRAD pipeline for multiallelic genotyping -- Runs `check_madc_sanity` before loading the data and stops with informative errors if: - - Required columns are missing - - IUPAC (non-ATCG) codes are present in AlleleSequence - - Ref/Alt sequences are unpaired (`RefAltSeqs = FALSE`) - - Allele IDs have not been fixed by HapApp (`FixAlleleIDs = FALSE`) - - CloneIDs do not follow `Chr_Pos` format and no `markers_info` is provided -- New argument `markers_info`: optional path or URL to a CSV with `CloneID`/`BI_markerID`, `Chr`, and `Pos` columns; required when CloneIDs do not follow the `Chr_Pos` format -- Runs `check_botloci` to validate and reconcile CloneIDs between the MADC and botloci file, automatically fixing padding mismatches -- A corrected temp file is written and passed to `readDArTag` only when needed (all-NA rows/columns detected, CloneIDs remapped by `check_botloci`, or botloci IDs remapped) -- Accepts paths or URLs for `madc_file`, `botloci_file`, and `markers_info` -- Estimates overdispersion with `polyRAD::TestOverdispersion`, iterates priors with `polyRAD::IterateHWE`, and exports the result with `polyRAD::RADdata2VCF` -- `polyRAD` is a soft dependency (listed under `Suggests`); an informative error is raised if it is not installed +- New function `madc2vcf_multi` to convert a DArTag MADC file to a VCF using the polyRAD pipeline for multiallelic genotyping +- Runs `check_madc_sanity` before loading the data and stops with informative errors if: + - Required columns are missing + - IUPAC (non-ATCG) codes are present in AlleleSequence + - Ref/Alt sequences are unpaired (`RefAltSeqs = FALSE`) + - Allele IDs have not been fixed by HapApp (`FixAlleleIDs = FALSE`) + - CloneIDs do not follow `Chr_Pos` format and no `markers_info` is provided +- New argument `markers_info`: optional path or URL to a CSV with `CloneID`/`BI_markerID`, `Chr`, and `Pos` columns; required when CloneIDs do not follow the `Chr_Pos` format +- Runs `check_botloci` to validate and reconcile CloneIDs between the MADC and botloci file, automatically fixing padding mismatches +- A corrected temp file is written and passed to `readDArTag` only when needed (all-NA rows/columns detected, CloneIDs remapped by `check_botloci`, or botloci IDs remapped) +- Accepts paths or URLs for `madc_file`, `botloci_file`, and `markers_info` +- Estimates overdispersion with `polyRAD::TestOverdispersion`, iterates priors with `polyRAD::IterateHWE`, and exports the result with `polyRAD::RADdata2VCF` +- `polyRAD` is a soft dependency (listed under `Suggests`); an informative error is raised if it is not installed # BIGr 0.6.6 ## Updates on `madc2vcf_all` -- New arguments for controlling processing of `Other` alleles: - - `add_others`: if `TRUE` (default), alleles labeled "Other" in the MADC are included in off-target SNP extraction - - `others_max_snps`: discards Other alleles with more than this many SNP differences relative to the Ref sequence (default: 5) - - `others_rm_with_indels`: discards Other alleles containing insertions or deletions relative to the Ref sequence (default: `TRUE`) -- Others alleles that carry a different base at the target SNP position are now reported as a 3rd allele in the VCF instead of being silently dropped -- Target position is now correctly removed from Others alignments, preventing duplicate VCF positions and marker IDs -- Fixed a bug where Others alleles with "Ref_" or "Alt_" in their AlleleID would corrupt the target SNP REF/ALT fields and read depth counts in `merge_counts` -- Improved verbose messages throughout: counts of Other alleles found, kept, and discarded (by indel filter and by max SNP filter) are now reported; multiallelic target SNPs with a 3rd allele from Others are counted and reported -- Debug-level message (level 3) listing each Other allele added and its genomic position +- New arguments for controlling processing of `Other` alleles: + - `add_others`: if `TRUE` (default), alleles labeled "Other" in the MADC are included in off-target SNP extraction + - `others_max_snps`: discards Other alleles with more than this many SNP differences relative to the Ref sequence (default: 5) + - `others_rm_with_indels`: discards Other alleles containing insertions or deletions relative to the Ref sequence (default: `TRUE`) +- Others alleles that carry a different base at the target SNP position are now reported as a 3rd allele in the VCF instead of being silently dropped +- Target position is now correctly removed from Others alignments, preventing duplicate VCF positions and marker IDs +- Fixed a bug where Others alleles with "Ref\_" or "Alt\_" in their AlleleID would corrupt the target SNP REF/ALT fields and read depth counts in `merge_counts` +- Improved verbose messages throughout: counts of Other alleles found, kept, and discarded (by indel filter and by max SNP filter) are now reported; multiallelic target SNPs with a 3rd allele from Others are counted and reported +- Debug-level message (level 3) listing each Other allele added and its genomic position # BIGr 0.6.5 ## Updates on madc2vcf functions + Details: -- both functions targets and all (targets + off-targets) markers now have `check_madc_sanity` function implemented. It tests: - - [Columns] If MADC has the expected columns - - [allNArow | allNAcol] Presence of columns and rows with all NA (happens often when people open the MADC in excel before loading in R) - - [IUPACcodes] Presence of IUPAC codes on AlleleSequence - - [LowerCase] Presence of lower case bases on AlleleSequence - - [Indels] Presence of Indels - - [ChromPos] If CloneID follows the format Chr_Pos - - [RefAltSeqs] If all Ref Allele has corresponding Alt and vice-versa - - [OtherAlleles] If "Other" exists in the MADC AlleleID - -- Better messages if `verbose = TRUE` in `madc2vcf_all` -- `madc2vcf_all` support for Indels - markers_info with Indels position is required; only the target indel is extracted, off-targets are ignored for the tag -- `madc2vcf_targets` doesn’t run if: - - MADC Column names are not correct - - Ignore Other alleles - but inform the user if they exist or not and direct them to `madc2vcf_all` in case they want to extract them as well -- See the table for madc2vcf_targets requirements accordingly to MADC content: - -  | check status | get_REF_ALT | Requires --- | -- | -- | -- -IUPAC | TRUE | TRUE | markers_info REF/ALT -  | TRUE | FALSE | - -  | FALSE | TRUE | botloci or markers_info REF/ALT -  | FALSE | FALSE | - -Indels | TRUE | TRUE | markers_info REF/ALT -  | TRUE | FALSE | - -  | FALSE | TRUE | botloci or markers_info REF/ALT -  | FALSE | FALSE | - -ChromPos | TRUE | TRUE | botloci or markers_info REF/ALT -  | TRUE | FALSE | - -  | FALSE | TRUE | markers_info CHR/POS/REF/ALT or markers_info CHR/POS/ + botloci -  | FALSE | FALSE | markers_info CHR/POS -FixAlleleIDs | TRUE | TRUE | botloci or markers_info REF/ALT -  | TRUE | FALSE | - -  | FALSE | TRUE | markers_info REF/ALT -  | FALSE | FALSE | - +- both functions targets and all (targets + off-targets) markers now have `check_madc_sanity` function implemented. It tests: + - [Columns] If MADC has the expected columns + - [allNArow \| allNAcol] Presence of columns and rows with all NA (happens often when people open the MADC in excel before loading in R) + - [IUPACcodes] Presence of IUPAC codes on AlleleSequence + - [LowerCase] Presence of lower case bases on AlleleSequence + - [Indels] Presence of Indels + - [ChromPos] If CloneID follows the format Chr_Pos + - [RefAltSeqs] If all Ref Allele has corresponding Alt and vice-versa + - [OtherAlleles] If "Other" exists in the MADC AlleleID +- Better messages if `verbose = TRUE` in `madc2vcf_all` +- `madc2vcf_all` support for Indels - markers_info with Indels position is required; only the target indel is extracted, off-targets are ignored for the tag +- `madc2vcf_targets` doesn’t run if: + - MADC Column names are not correct + - Ignore Other alleles - but inform the user if they exist or not and direct them to `madc2vcf_all` in case they want to extract them as well +- See the table for madc2vcf_targets requirements accordingly to MADC content: + +|   | check status | get_REF_ALT | Requires | +|----|----|----|----| +| IUPAC | TRUE | TRUE | markers_info REF/ALT | +|   | TRUE | FALSE | \- | +|   | FALSE | TRUE | botloci or markers_info REF/ALT | +|   | FALSE | FALSE | \- | +| Indels | TRUE | TRUE | markers_info REF/ALT | +|   | TRUE | FALSE | \- | +|   | FALSE | TRUE | botloci or markers_info REF/ALT | +|   | FALSE | FALSE | \- | +| ChromPos | TRUE | TRUE | botloci or markers_info REF/ALT | +|   | TRUE | FALSE | \- | +|   | FALSE | TRUE | markers_info CHR/POS/REF/ALT or markers_info CHR/POS/ + botloci | +|   | FALSE | FALSE | markers_info CHR/POS | +| FixAlleleIDs | TRUE | TRUE | botloci or markers_info REF/ALT | +|   | TRUE | FALSE | \- | +|   | FALSE | TRUE | markers_info REF/ALT | +|   | FALSE | FALSE | \- | # BIGr 0.6.4 -- Add function `vmsg` to organize messages printed on the console -- Add metadata to VCF header from madc2vcf_targets -- Add argument `madc_object` to `get_countsMADC` to avoid reading the MADC file twice and to get directly the MADC fixed padding output from `check_botloci` -- Organize messages from `madc2vcf_targets` checks -- Add argument `collapse_matches_counts` and `verbose` to `madc2vcf_targets` function +- Add function `vmsg` to organize messages printed on the console +- Add metadata to VCF header from madc2vcf_targets +- Add argument `madc_object` to `get_countsMADC` to avoid reading the MADC file twice and to get directly the MADC fixed padding output from `check_botloci` +- Organize messages from `madc2vcf_targets` checks +- Add argument `collapse_matches_counts` and `verbose` to `madc2vcf_targets` function # BIGr 0.6.3 -- New function to check MADC files: `check_madc_sanity`. Currently, it checks for the presence of required columns, whether fixed allele IDs were assigned, the presence of IUPAC codes, lowercase sequence bases, indels, and chromosome and position information. -- Added new argument `markers_info`, which allows users to provide a CSV file with marker information such as CHROM, POS, marker type, and position of indels. For BI species, this information is available from [PanelHub](https://github.com/Breeding-Insight/BIGapp-PanelHub). -- Checked inputs for `madc2vcf_all`. -- Updated affiliation in `DESCRIPTION`. +- New function to check MADC files: `check_madc_sanity`. Currently, it checks for the presence of required columns, whether fixed allele IDs were assigned, the presence of IUPAC codes, lowercase sequence bases, indels, and chromosome and position information. +- Added new argument `markers_info`, which allows users to provide a CSV file with marker information such as CHROM, POS, marker type, and position of indels. For BI species, this information is available from [PanelHub](https://github.com/Breeding-Insight/BIGapp-PanelHub). +- Checked inputs for `madc2vcf_all`. +- Updated affiliation in `DESCRIPTION`. # BIGr 0.6.2 -- Fixed the doi and name list in the CITATION file +- Fixed the doi and name list in the CITATION file # BIGr 0.6.1 -- Added new functions for filtering MADC files and converting to relationship matrices -- Added function thinSNPs to thin SNPs based on physical distance -- Added bug fixes and improvements to existing functions +- Added new functions for filtering MADC files and converting to relationship matrices +- Added function thinSNPs to thin SNPs based on physical distance +- Added bug fixes and improvements to existing functions # BIGr 0.5.5 -- Updated DESCRIPTION -- Added return value for merge_MADCs -- Added optional seed for check_ped -- Added verbose option +- Updated DESCRIPTION +- Added return value for merge_MADCs +- Added optional seed for check_ped +- Added verbose option # BIGr 0.5.4 diff --git a/R/dosage2vcf.R b/R/dosage2vcf.R index 0a15c48..cce4399 100644 --- a/R/dosage2vcf.R +++ b/R/dosage2vcf.R @@ -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 @@ -30,10 +32,12 @@ #' #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) @@ -41,7 +45,7 @@ #' ##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 @@ -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=", + ref_lines$lines, '##INFO=', '##INFO=', '##INFO=', @@ -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) diff --git a/R/filterVCF.R b/R/filterVCF.R index 0b71af6..a74af94 100644 --- a/R/filterVCF.R +++ b/R/filterVCF.R @@ -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=` 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 @@ -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 @@ -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) @@ -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 @@ -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=]", vcf@meta) + if (any(placeholder)) { + message("Removing ##reference=NA and ##contig= 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)) { diff --git a/R/madc2vcf_all.R b/R/madc2vcf_all.R index 2000139..4c8015d 100644 --- a/R/madc2vcf_all.R +++ b/R/madc2vcf_all.R @@ -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. @@ -59,6 +61,7 @@ #' multiallelic_SNP_sample_thr = 5, #' alignment_score_thr = 40, #' out_vcf = output_file, +#' reference = "example_genome_v1", #' verbose = TRUE #' ) #' @@ -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){ @@ -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 @@ -137,7 +144,7 @@ madc2vcf_all <- function(madc, bigr_meta <- paste0('##BIGrCommandLine.madc2vcf_all=') + "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) @@ -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")])){ @@ -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 = ">>") @@ -294,8 +306,7 @@ madc2vcf_all <- function(madc, #Make a header separate from the dataframe vcf_header <- c( "##fileformat=VCFv4.3", - "##reference=NA", - "##contig=", + ref_lines$lines, '##INFO=', '##INFO=', '##FORMAT=', @@ -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") diff --git a/R/madc2vcf_multi.R b/R/madc2vcf_multi.R index 04858c8..3ef52f2 100644 --- a/R/madc2vcf_multi.R +++ b/R/madc2vcf_multi.R @@ -15,6 +15,19 @@ #' \code{BI_markerID}), \code{Chr}, and \code{Pos} columns. #' @param ploidy integer. Ploidy level of the samples passed to \code{taxaPloidy}. #' Default is 2. +#' @param vcf_header character or NULL. Optional path or URL to a VCF header file +#' with the \code{##reference} and \code{##contig} lines of the reference +#' genome, such as the VCF header files provided with the microhaplotype +#' databases. Its \code{##contig} lines replace the ones written by polyRAD, +#' which have no contig lengths. The other lines of the file are not used. A +#' warning is given if a chromosome in the output is not listed in the +#' \code{##contig} lines. +#' @param reference character or NULL. Optional reference genome ID written to +#' the \code{##reference} line (e.g. \code{"GCF_020283565.1"}). Use it when no +#' \code{vcf_header} file is available, or to replace the \code{##reference} +#' line of \code{vcf_header}. If neither \code{vcf_header} nor +#' \code{reference} is given, a warning is given and the header has no +#' \code{##reference} line and only the \code{##contig} lines written by polyRAD. #' @param verbose logical. Whether to print progress messages. Default is TRUE. #' #' @return Invisible NULL. Writes a VCF file to \code{outfile}. @@ -32,6 +45,8 @@ #' \item Estimates overdispersion with \code{polyRAD::TestOverdispersion} and #' calls \code{polyRAD::IterateHWE}, then exports the result with #' \code{polyRAD::RADdata2VCF}. +#' \item Adds the reference genome lines from \code{vcf_header} and/or +#' \code{reference} and a \code{##BIGrCommandLine} line to the VCF header. #' } #' #' **Sanity check behaviour and requirements** @@ -55,6 +70,8 @@ madc2vcf_multi <- function(madc_file, outfile, markers_info = NULL, ploidy = 2L, + vcf_header = NULL, + reference = NULL, verbose = TRUE) { vmsg("Running BIGr madc2vcf_multi", verbose = verbose, level = 0, type = ">>") @@ -63,6 +80,8 @@ madc2vcf_multi <- function(madc_file, vmsg("markers_info : %s", verbose = verbose, level = 1, type = ">>", if (is.null(markers_info)) "NULL" else markers_info) vmsg("outfile : %s", verbose = verbose, level = 1, type = ">>", outfile) vmsg("ploidy : %s", verbose = verbose, level = 1, type = ">>", ploidy) + 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 = ">>") @@ -71,6 +90,21 @@ madc2vcf_multi <- function(madc_file, if (!is.null(markers_info) && !(file.exists(markers_info) | url_exists(markers_info))) stop("markers_info file not found. Please provide a valid path or URL.") if (!is.numeric(ploidy) || ploidy < 1) stop("ploidy must be a positive integer.") + # Create a VCF header line with metadata about the command and its parameters + bigr_meta <- paste0('##BIGrCommandLine.madc2vcf_multi=') + + # Reference genome lines for the VCF header + ref_lines <- vcf_reference_lines(vcf_header, reference) + # ---- Load markers_info if provided ---- mi_df <- if (!is.null(markers_info)) read.csv(markers_info) else NULL @@ -132,6 +166,22 @@ madc2vcf_multi <- function(madc_file, # ---- Corrections: only create a temp file if needed ---- need_temp <- isTRUE(checks$checks["allNArow"]) || isTRUE(checks$checks["allNAcol"]) || cloneid_changed + # polyRAD requires integer read counts. Counts stored as e.g. "12.0" are read + # as double, so coerce whole-number count columns to integer and pass a temp file. + count_cols <- setdiff(colnames(report), c("AlleleID", "CloneID", "AlleleSequence", "readCountSum")) + non_int <- count_cols[!vapply(report[count_cols], is.integer, logical(1))] + if (length(non_int) > 0) { + vals <- unlist(report[non_int], use.names = FALSE) + if (!is.numeric(vals) || any(vals %% 1 != 0, na.rm = TRUE)) + stop("MADC read count columns must contain whole numbers. Non-integer values found in: ", + paste(utils::head(non_int, 5), collapse = ", "), + if (length(non_int) > 5) ", ..." else "") + vmsg("Converting %s read count column(s) stored as decimals to integer", + verbose = verbose, level = 1, type = ">>", length(non_int)) + report[non_int] <- lapply(report[non_int], as.integer) + need_temp <- TRUE + } + if (need_temp) { if (checks$checks["LowerCase"]) { vmsg("MADC Allele Sequences contain lowercase characters. Converting to uppercase", @@ -176,8 +226,70 @@ madc2vcf_multi <- function(madc_file, taxaPloidy = as.integer(ploidy) ) - overdispersionP <- polyRAD::TestOverdispersion(raddat) - my_ovdisp <- overdispersionP$optimal + # polyRAD derives Chr/Pos by splitting the CloneID at its last "_". When + # CloneIDs do not follow Chr_Pos (and were not remapped by check_botloci), + # take Chr/Pos from markers_info instead so RADdata2VCF gets valid positions. + loc_ids <- rownames(raddat$locTable) + if (!isTRUE(checks$checks["ChromPos"]) && !is.null(mi_df) && + (anyNA(raddat$locTable$Pos) || !cloneid_changed)) { + use_col <- pick_markers_info_id_col(mi_df, loc_ids) + idx <- match(loc_ids, mi_df[[use_col]]) + found <- !is.na(idx) + vmsg("Using Chr and Pos from markers_info (%s column) for %s of %s loci", + verbose = verbose, level = 1, type = ">>", use_col, sum(found), length(loc_ids)) + raddat$locTable$Chr[found] <- as.character(mi_df$Chr[idx[found]]) + raddat$locTable$Pos[found] <- as.integer(mi_df$Pos[idx[found]]) + } + bad_pos <- loc_ids[is.na(raddat$locTable$Pos)] + if (length(bad_pos) > 0) + stop("Could not determine the position of ", length(bad_pos), " marker(s) (e.g. ", + paste(utils::head(bad_pos, 5), collapse = ", "), + "). Make sure they are in markers_info with valid 'Chr' and 'Pos' values.") + + # polyRAD returns optimal = NA when the best value is at the edge of the + # tested range, so widen the range until an interior optimum is found. + # Note: polyRAD (<= 2.0.0) errors instead of returning NA when the best value + # is the minimum of to_test ("missing value where TRUE/FALSE needed"), so the + # call is wrapped in tryCatch and the best value is parsed from its printed + # "Optimal value is X." line. + run_test_od <- function(to_test) { + od <- NULL + printed <- utils::capture.output( + od <- tryCatch(polyRAD::TestOverdispersion(raddat, to_test = to_test), + error = function(e) e) + ) + if (verbose) cat(printed, sep = "\n") + hit <- regmatches(printed, regexpr("(?<=Optimal value is )[0-9.eE+-]+(?=\\.$)", printed, perl = TRUE)) + best <- if (length(hit)) as.numeric(hit[length(hit)]) else NA_real_ + if (inherits(od, "error")) { + if (is.na(best)) stop(od) + optimal <- NA_real_ + } else { + optimal <- od$optimal + } + list(optimal = optimal, best = best) + } + + to_test <- seq(6, 20, by = 2) # polyRAD default + my_ovdisp <- NA + best <- NA + for (attempt in 1:4) { + res <- run_test_od(to_test) + my_ovdisp <- res$optimal + if (!is.na(my_ovdisp)) break + best <- res$best + if (is.na(best)) break + if (best == min(to_test) && min(to_test) > 0.5) { + to_test <- seq(0.5, min(to_test) + 1, by = 0.5) + } else if (best == max(to_test)) { + to_test <- seq(max(to_test) - 1, max(to_test) * 3, length.out = 20) + } else { + break + } + } + if (is.na(my_ovdisp)) my_ovdisp <- best + if (is.na(my_ovdisp)) + stop("Could not estimate the overdispersion parameter with polyRAD::TestOverdispersion().") vmsg("Running HWE iteration (overdispersion = %s)", verbose = verbose, level = 0, type = ">>", my_ovdisp) @@ -187,7 +299,59 @@ madc2vcf_multi <- function(madc_file, polyRAD::RADdata2VCF(raddat_hwe, file = outfile, asSNPs = FALSE, hindhe = FALSE) + # Add the reference genome lines and the BIGr command line to the header + # written by polyRAD + meta <- update_vcf_reference_lines(read_vcf_meta(outfile), ref_lines) + replace_vcf_meta(outfile, c(meta, bigr_meta)) + check_vcf_contigs(raddat_hwe$locTable$Chr, ref_lines$contigs) + vmsg("Done!", verbose = verbose, level = 0, type = ">>") invisible(NULL) } + +#' Read the meta lines (starting with ##) of a VCF file +#' +#' @param file Path to an uncompressed VCF file. +#' @return Character vector with the meta lines. +#' @noRd +read_vcf_meta <- function(file) { + con <- file(file, "r") + on.exit(close(con)) + meta <- character(0) + repeat { + lines <- readLines(con, n = 1000L) + if (length(lines) == 0) break + is_meta <- startsWith(lines, "##") + meta <- c(meta, lines[is_meta]) + if (!all(is_meta)) break + } + meta +} + +#' Replace the meta lines (starting with ##) of a VCF file +#' +#' The #CHROM line and the records are copied unchanged, in chunks, so the file +#' is not read into memory at once. +#' +#' @param file Path to an uncompressed VCF file. +#' @param meta Character vector with the new meta lines. +#' @return No return value, called for side effects. +#' @noRd +replace_vcf_meta <- function(file, meta) { + tmp <- tempfile(fileext = ".vcf") + con_in <- file(file, "r") + con_out <- file(tmp, "w") + writeLines(meta, con_out) + repeat { + lines <- readLines(con_in, n = 10000L) + if (length(lines) == 0) break + lines <- lines[!startsWith(lines, "##")] + if (length(lines) > 0) writeLines(lines, con_out) + } + close(con_in) + close(con_out) + file.copy(tmp, file, overwrite = TRUE) + unlink(tmp) + invisible() +} diff --git a/R/madc2vcf_targets.R b/R/madc2vcf_targets.R index 32d4750..112357b 100644 --- a/R/madc2vcf_targets.R +++ b/R/madc2vcf_targets.R @@ -28,6 +28,9 @@ #' - `FORMAT` fields (per sample): #' * `DP` - total reads (ref + alt) #' * `RA` - reads supporting the reference allele +#' - Header: the `##reference` and `##contig` lines come from `vcf_header` and/or +#' `reference`. A `##BIGrCommandLine` line with the function call is always +#' added. #' * `AD` - `"ref,alt"` counts #' #' **Strand handling** @@ -90,6 +93,18 @@ #' `|AltMatch` and `|RefMatch` rows are summed into their corresponding `|Ref` #' and `|Alt` rows before building the matrices. Useful when the MADC contains #' multiple allele rows per target that should be aggregated. +#' @param vcf_header character or `NULL` (default `NULL`). 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. +#' @param reference character or `NULL` (default `NULL`). 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. #' @param verbose logical (default `TRUE`). If `TRUE`, prints detailed progress #' messages about each processing step. #' @@ -115,7 +130,8 @@ #' madc_file = madc_file, #' output.file = out_vcf, #' botloci_file = bot_file, -#' get_REF_ALT = TRUE +#' get_REF_ALT = TRUE, +#' reference = "example_genome_v1" #' ) #' } #' @@ -140,6 +156,8 @@ madc2vcf_targets <- function(madc_file, markers_info = NULL, get_REF_ALT = FALSE, collapse_matches_counts = FALSE, + vcf_header = NULL, + reference = NULL, verbose = TRUE) { vmsg("Running BIGr madc2vcf_targets", verbose = verbose, level = 0, type = ">>") @@ -149,6 +167,8 @@ madc2vcf_targets <- function(madc_file, vmsg("markers_info : %s", verbose = verbose, level = 1, type = ">>", if (is.null(markers_info)) "NULL" else markers_info) vmsg("get_REF_ALT : %s", verbose = verbose, level = 1, type = ">>", get_REF_ALT) vmsg("collapse_matches_counts : %s", verbose = verbose, level = 1, type = ">>", collapse_matches_counts) + 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 = ">>") @@ -163,12 +183,17 @@ madc2vcf_targets <- function(madc_file, # Create a VCF header line with metadata about the command and its parameters bigr_meta <- paste0('##BIGrCommandLine.madc2vcf_targets=') + "vcf_header= ", vcf_header, ', ', + "reference= ", reference, ', ', + "verbose= ", verbose,')')), '">') + + # Reference genome lines for the VCF header + ref_lines <- vcf_reference_lines(vcf_header, reference) # MADC checks report <- read.csv(madc_file, check.names = FALSE) @@ -469,8 +494,7 @@ madc2vcf_targets <- function(madc_file, vcf_header <- c( "##fileformat=VCFv4.3", paste0("##BIGr_madc2vcf=",packageVersion("BIGr")), - "##reference=NA", - "##contig=", + ref_lines$lines, '##INFO=', '##INFO=', '##FORMAT=', @@ -522,9 +546,7 @@ madc2vcf_targets <- function(madc_file, # Add # to the CHROM column name colnames(vcf_df)[1] <- "#CHROM" - # Remove markers with NA CHROM/POS (unmatched in markers_info, Case 3) - # This runs before the POS correction below, which would otherwise remove these - # markers as invalid POS values without the markers_info warning + # Remove markers with NA CHROM/POS (unmatched in markers_info, Case 3), before the POS correction below na_coord <- is.na(vcf_df[, 1]) | is.na(vcf_df$POS) if(any(na_coord)) { vmsg(paste(sum(na_coord), "markers removed: no matching entry found in markers_info."), verbose = verbose, level = 1, type = ">>") @@ -565,6 +587,8 @@ madc2vcf_targets <- function(madc_file, vcf_df <- vcf_df[-which(is.na(vcf_df$REF)),] } + check_vcf_contigs(vcf_df[[1]], ref_lines$contigs) + # Write the header to the file writeLines(vcf_header, con = output.file) diff --git a/R/updog2vcf.R b/R/updog2vcf.R index 3a8862b..b1cf0c4 100644 --- a/R/updog2vcf.R +++ b/R/updog2vcf.R @@ -6,11 +6,17 @@ #' This function will convert the updog output file to a VCF file, while retaining the information for the values that are commonly #' used to filter low quality and low confident dosage calls. #' +#' The CHROM and POS columns are read from the SNP IDs, which are expected in the CHR_POS format. +#' SNPs whose ID does not end in a positive integer position are written with the SNP ID as CHROM +#' and 1 as POS, with a warning. +#' #' @param multidog.object updog output object with class "multidog" from dosage calling #' @param output.file output file name and path #' @param updog_version character defining updog package version used to generate the multidog object #' @param RefAlt optional data frame with four columns named "Chr", "Pos", "Ref", and "Alt" containing the reference and alternate alleles for each SNP in the same order as in the multidog object #' @param compress logical. If TRUE returns a vcf.gz file +#' @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 #' @import tidyr @@ -30,14 +36,16 @@ #' multidog.object = mout, #' output.file = temp_file, #' updog_version = "0.0.0", -#' compress = TRUE +#' compress = TRUE, +#' reference = "example_genome_v1" #' ) #' #' #Removing the example vcf #' rm(temp_file) #' #' @export -updog2vcf <- function(multidog.object, output.file, updog_version = NULL, RefAlt = NULL, compress = TRUE) { +updog2vcf <- function(multidog.object, output.file, updog_version = NULL, RefAlt = NULL, compress = TRUE, + vcf_header = NULL, reference = NULL) { #Making the VCF (This is highly dependent on snps being in a format where the SNP IDs are the CHR_POS) mout <- multidog.object @@ -51,15 +59,19 @@ updog2vcf <- function(multidog.object, output.file, updog_version = NULL, RefAlt bigr_meta <- paste0('##BIGrCommandLine.updog2vcf=') + updog_version, ',', + "vcf_header= ", vcf_header, ',', + "reference= ", reference,')')), '">') + + #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", - "##reference=NA", - "##contig=", + ref_lines$lines, '##INFO=', '##INFO=', '##INFO=', @@ -226,17 +238,22 @@ updog2vcf <- function(multidog.object, output.file, updog_version = NULL, RefAlt #Add samples to vcf df vcf_df <- cbind(vcf_df,format_wide[,-1]) - #Remove SNPs without a valid position in the SNP ID (POS must be a positive integer) + #SNPs without a valid position in the SNP ID (POS must be a positive integer) are kept, + #using the SNP ID as CHROM and 1 as POS so that their positions don't collide bad_pos <- is.na(vcf_df$POS) | !grepl("^[1-9][0-9]*$", vcf_df$POS) if (any(bad_pos)) { - warning(sum(bad_pos), " SNPs removed because their position could not be read from the SNP ID. ", - "SNP IDs are expected in the CHR_POS format, with POS a positive integer.", call. = FALSE) - vcf_df <- vcf_df[!bad_pos, , drop = FALSE] + warning(sum(bad_pos), " SNPs have a position that could not be read from the SNP ID. ", + "SNP IDs are expected in the CHR_POS format, with POS a positive integer. ", + "These SNPs were written with the SNP ID as CHROM and 1 as POS.", call. = FALSE) + vcf_df$CHROM[bad_pos] <- vcf_df$ID[bad_pos] + vcf_df$POS[bad_pos] <- "1" } # Add # to the CHROM column name colnames(vcf_df)[1] <- "#CHROM" + check_vcf_contigs(vcf_df[[1]], ref_lines$contigs) + if(!compress){ # Write the header to the file writeLines(vcf_header, con = output.file) diff --git a/R/utils.R b/R/utils.R index 37cb127..842f2e7 100644 --- a/R/utils.R +++ b/R/utils.R @@ -97,3 +97,124 @@ url_exists <- function(u) { TRUE }, error = function(e) FALSE, warning = function(w) FALSE) } + +#' Build the reference genome lines of a VCF header +#' +#' Returns the ##reference and ##contig lines to write in a VCF header, using +#' the most informative input available: +#' 1. `vcf_header`: a VCF header file with the ##reference and ##contig lines +#' (reference genome and scaffold lengths). Only these lines are taken from +#' the file; the calling function writes its own ##fileformat, ##INFO and +#' ##FORMAT lines. +#' 2. `reference`: the reference genome ID only, written as the ##reference line. +#' 3. Nothing: no ##reference or ##contig lines are written, since placeholder +#' lines such as "##reference=NA" break tools that parse the header (e.g., +#' GIGWA). A warning is given when `warn` is TRUE. +#' +#' @param vcf_header Path or URL to a VCF header file, or NULL. +#' @param reference Reference genome ID, or NULL. If given together with +#' `vcf_header`, it is used for the ##reference line. +#' @param warn Logical. Warn when neither `vcf_header` nor `reference` is given. +#' +#' @return A list with `lines` (character vector of header lines, possibly +#' empty) and `contigs` (contig IDs from the ##contig lines, or NULL when no +#' ##contig lines were provided). +#' @noRd +vcf_reference_lines <- function(vcf_header = NULL, reference = NULL, warn = TRUE) { + + if (!is.null(reference) && (!is.character(reference) || length(reference) != 1L || + is.na(reference) || !nzchar(trimws(reference)))) { + stop("reference must be a single character string with the reference genome ID.", call. = FALSE) + } + + ref_line <- if (!is.null(reference)) paste0("##reference=", trimws(reference)) else character(0) + contig_lines <- character(0) + + if (!is.null(vcf_header)) { + if (!is.character(vcf_header) || length(vcf_header) != 1L) { + stop("vcf_header must be the path or URL to a VCF header file.", call. = FALSE) + } + if (!(file.exists(vcf_header) | url_exists(vcf_header))) { + stop("vcf_header file not found. Please provide a valid path or URL.", call. = FALSE) + } + header <- trimws(readLines(vcf_header, warn = FALSE), which = "right") + + file_ref <- grep("^##reference=", header, value = TRUE) + contig_lines <- grep("^##contig=", header, value = TRUE) + + if (length(file_ref) == 0 && length(contig_lines) == 0) { + stop("vcf_header has no ##reference or ##contig lines: ", vcf_header, call. = FALSE) + } + if (length(file_ref) > 1) { + stop("vcf_header has more than one ##reference line: ", vcf_header, call. = FALSE) + } + bad_contig <- !grepl("^##contig=]+[,>]", contig_lines) + if (any(bad_contig)) { + stop("vcf_header has ##contig lines without an ID, e.g. ", contig_lines[bad_contig][1], + ". Expected ##contig=.", call. = FALSE) + } + if (length(ref_line) == 0) ref_line <- file_ref + } + + if (warn && is.null(vcf_header) && is.null(reference)) { + warning("No reference genome information was provided (vcf_header or reference). ", + "The ##reference and ##contig lines are left out of the VCF header.", call. = FALSE) + } + + contigs <- if (length(contig_lines) > 0) sub("^##contig=]+).*", "\\1", contig_lines) else NULL + + list(lines = c(ref_line, contig_lines), contigs = contigs) +} + +#' Escape a value for a quoted field of a VCF header line +#' +#' Double quotes and backslashes are escaped with a backslash, as required by the +#' VCF specification for quoted values (e.g., the CommandLine field). +#' +#' @param x Character string. +#' @return The escaped string. +#' @noRd +vcf_quote_escape <- function(x) { + gsub('(["\\\\])', '\\\\\\1', x) +} + +#' Replace the reference genome lines of an existing VCF header +#' +#' The ##reference and ##contig lines from `vcf_reference_lines()` replace the +#' ones in `meta` and are placed after the ##fileformat line. Lines of `meta` that +#' are not replaced are kept. +#' +#' @param meta Meta lines (starting with ##) of a VCF header. +#' @param ref_lines List returned by `vcf_reference_lines()`. +#' +#' @return Character vector with the updated meta lines. +#' @noRd +update_vcf_reference_lines <- function(meta, ref_lines) { + new_ref <- grep("^##reference=", ref_lines$lines, value = TRUE) + new_contig <- grep("^##contig=", ref_lines$lines, value = TRUE) + if (length(new_ref) > 0) meta <- meta[!grepl("^##reference=", meta)] + if (length(new_contig) > 0) meta <- meta[!grepl("^##contig=", meta)] + fileformat_pos <- grep("^##fileformat=", meta)[1] + if (is.na(fileformat_pos)) fileformat_pos <- 0 + append(meta, c(new_ref, new_contig), after = fileformat_pos) +} + +#' Warn when VCF chromosomes are missing from the ##contig lines +#' +#' @param chrom CHROM values of the VCF records. +#' @param contigs Contig IDs from `vcf_reference_lines()`. Nothing is checked when NULL. +#' +#' @return No return value, called for side effects. +#' @noRd +check_vcf_contigs <- function(chrom, contigs) { + if (is.null(contigs)) return(invisible()) + missing_chrom <- setdiff(unique(as.character(chrom)), contigs) + missing_chrom <- missing_chrom[!is.na(missing_chrom)] + if (length(missing_chrom) > 0) { + warning(length(missing_chrom), " chromosome(s) in the VCF are not listed in the ##contig lines of vcf_header: ", + paste(utils::head(missing_chrom, 5), collapse = ", "), + if (length(missing_chrom) > 5) paste0(" and ", length(missing_chrom) - 5, " more") else "", + ". Check that the header is for the same reference genome.", call. = FALSE) + } + invisible() +} diff --git a/man/dosage2vcf.Rd b/man/dosage2vcf.Rd index b6a1978..6266d17 100644 --- a/man/dosage2vcf.Rd +++ b/man/dosage2vcf.Rd @@ -4,7 +4,14 @@ \alias{dosage2vcf} \title{Convert DArTag genotype reports and counts to VCF} \usage{ -dosage2vcf(dart.report, dart.counts, ploidy, output.file) +dosage2vcf( + dart.report, + dart.counts, + ploidy, + output.file, + vcf_header = NULL, + reference = NULL +) } \arguments{ \item{dart.report}{Path to the DArT Allele Dose Report or SNP/INDEL report .csv file.} @@ -14,6 +21,10 @@ dosage2vcf(dart.report, dart.counts, ploidy, output.file) \item{ploidy}{The ploidy of the species being analyzed} \item{output.file}{output file name and path} + +\item{vcf_header}{optional path or URL to a VCF header file. Only its ##reference and ##contig lines are added to the output VCF header} + +\item{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} } \value{ A vcf file @@ -40,10 +51,12 @@ The VCF format is v4.3 #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) diff --git a/man/filterVCF.Rd b/man/filterVCF.Rd index 39af09e..c6c2386 100644 --- a/man/filterVCF.Rd +++ b/man/filterVCF.Rd @@ -16,7 +16,9 @@ filterVCF( filter.SAMPLE.miss = NULL, filter.SNP.miss = NULL, ploidy, - output.file = NULL + output.file = NULL, + vcf_header = NULL, + reference = NULL ) } \arguments{ @@ -69,6 +71,20 @@ supplied, the filtered VCF is written as a gzipped file and nothing is returned. \code{.vcf.gz} is appended when \code{vcf.file} is a path, and \verb{_filtered.vcf.gz} when \code{vcf.file} is a vcfR object. When not supplied, a vcfR object is returned instead and no file is written.} + +\item{vcf_header}{Path or URL to a VCF header file with the \verb{##reference} and +\verb{##contig} lines of the reference genome (optional), such as the VCF header +files provided with the microhaplotype databases. Its \verb{##contig} lines +replace those of the input VCF, and so does its \verb{##reference} line unless +\code{reference} is given. The other lines of the file are not used. A warning is +given if a \code{CHROM} in the output is not listed in the \verb{##contig} lines.} + +\item{reference}{Reference genome ID written to the \verb{##reference} line, +replacing the one of the input VCF (optional), e.g. \code{"GCF_020283565.1"}. +When neither \code{vcf_header} nor \code{reference} is given, the reference genome lines +of the input VCF are kept, except the \verb{##reference=NA} and +\verb{##contig=} lines written by earlier versions of BIGr, which +are removed.} } \value{ A vcfR object when \code{output.file} is not supplied. Otherwise the diff --git a/man/madc2vcf_all.Rd b/man/madc2vcf_all.Rd index 990e500..5ac3655 100644 --- a/man/madc2vcf_all.Rd +++ b/man/madc2vcf_all.Rd @@ -20,6 +20,8 @@ madc2vcf_all( others_rm_with_indels = TRUE, others_min_dist = 5, others_max_close_snps = 3, + vcf_header = NULL, + reference = NULL, verbose = TRUE ) } @@ -54,6 +56,10 @@ madc2vcf_all( \item{others_max_close_snps}{An integer or NULL. If not NULL, Other alleles with more than this many SNPs that are closer than \code{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 \code{others_min_dist} or \code{others_max_close_snps} is NULL. Default is 3.} +\item{vcf_header}{A string specifying the path or URL to a VCF header file with the \verb{##reference} and \verb{##contig} lines of the reference genome, such as the VCF header files provided with the microhaplotype databases. Only the \verb{##reference} and \verb{##contig} lines are used; the \verb{##fileformat}, \verb{##INFO} and \verb{##FORMAT} lines are written by the function. A warning is given if a \code{CHROM} in the output is not listed in the \verb{##contig} lines. Default is NULL.} + +\item{reference}{A string with the reference genome ID written to the \verb{##reference} line (e.g. \code{"GCF_020283565.1"}). Use it when no \code{vcf_header} file is available, or to replace the \verb{##reference} line of \code{vcf_header}. If neither \code{vcf_header} nor \code{reference} is given, the \verb{##reference} and \verb{##contig} lines are left out of the header with a warning. Default is NULL.} + \item{verbose}{A logical value indicating whether to print metrics and progress to the console. Default is TRUE.} } \value{ @@ -98,6 +104,7 @@ madc2vcf_all( multiallelic_SNP_sample_thr = 5, alignment_score_thr = 40, out_vcf = output_file, + reference = "example_genome_v1", verbose = TRUE ) diff --git a/man/madc2vcf_multi.Rd b/man/madc2vcf_multi.Rd index b6a3324..ca77723 100644 --- a/man/madc2vcf_multi.Rd +++ b/man/madc2vcf_multi.Rd @@ -10,6 +10,8 @@ madc2vcf_multi( outfile, markers_info = NULL, ploidy = 2L, + vcf_header = NULL, + reference = NULL, verbose = TRUE ) } @@ -29,6 +31,21 @@ with marker metadata. Required when CloneIDs do not follow the \item{ploidy}{integer. Ploidy level of the samples passed to \code{taxaPloidy}. Default is 2.} +\item{vcf_header}{character or NULL. Optional path or URL to a VCF header file +with the \code{##reference} and \code{##contig} lines of the reference +genome, such as the VCF header files provided with the microhaplotype +databases. Its \code{##contig} lines replace the ones written by polyRAD, +which have no contig lengths. The other lines of the file are not used. A +warning is given if a chromosome in the output is not listed in the +\code{##contig} lines.} + +\item{reference}{character or NULL. Optional reference genome ID written to +the \code{##reference} line (e.g. \code{"GCF_020283565.1"}). Use it when no +\code{vcf_header} file is available, or to replace the \code{##reference} +line of \code{vcf_header}. If neither \code{vcf_header} nor +\code{reference} is given, a warning is given and the header has no +\code{##reference} line and only the \code{##contig} lines written by polyRAD.} + \item{verbose}{logical. Whether to print progress messages. Default is TRUE.} } \value{ @@ -53,6 +70,8 @@ The function performs the following steps: \item Estimates overdispersion with \code{polyRAD::TestOverdispersion} and calls \code{polyRAD::IterateHWE}, then exports the result with \code{polyRAD::RADdata2VCF}. +\item Adds the reference genome lines from \code{vcf_header} and/or +\code{reference} and a \code{##BIGrCommandLine} line to the VCF header. } \strong{Sanity check behaviour and requirements} diff --git a/man/madc2vcf_targets.Rd b/man/madc2vcf_targets.Rd index 3ed4aaf..d9dac1f 100644 --- a/man/madc2vcf_targets.Rd +++ b/man/madc2vcf_targets.Rd @@ -11,6 +11,8 @@ madc2vcf_targets( markers_info = NULL, get_REF_ALT = FALSE, collapse_matches_counts = FALSE, + vcf_header = NULL, + reference = NULL, verbose = TRUE ) } @@ -51,6 +53,20 @@ are set to \code{"."} in the output VCF.} and \verb{|Alt} rows before building the matrices. Useful when the MADC contains multiple allele rows per target that should be aggregated.} +\item{vcf_header}{character or \code{NULL} (default \code{NULL}). Path or URL to a VCF +header file with the \verb{##reference} and \verb{##contig} lines of the reference +genome, such as the VCF header files provided with the microhaplotype +databases. Only the \verb{##reference} and \verb{##contig} lines are used; the +\verb{##fileformat}, \verb{##INFO} and \verb{##FORMAT} lines are written by the function. +A warning is given if a \code{CHROM} in the output is not listed in the +\verb{##contig} lines.} + +\item{reference}{character or \code{NULL} (default \code{NULL}). Reference genome ID +written to the \verb{##reference} line (e.g. \code{"GCF_020283565.1"}). Use it when no +\code{vcf_header} file is available, or to replace the \verb{##reference} line of +\code{vcf_header}. If neither \code{vcf_header} nor \code{reference} is given, the +\verb{##reference} and \verb{##contig} lines are left out of the header with a warning.} + \item{verbose}{logical (default \code{TRUE}). If \code{TRUE}, prints detailed progress messages about each processing step.} } @@ -93,6 +109,11 @@ Targets with >1 polymorphism between sequences are discarded. \itemize{ \item \code{DP} - total reads (ref + alt) \item \code{RA} - reads supporting the reference allele +} +\item Header: the \verb{##reference} and \verb{##contig} lines come from \code{vcf_header} and/or +\code{reference}. A \verb{##BIGrCommandLine} line with the function call is always +added. +\itemize{ \item \code{AD} - \code{"ref,alt"} counts } } @@ -148,7 +169,8 @@ madc2vcf_targets( madc_file = madc_file, output.file = out_vcf, botloci_file = bot_file, - get_REF_ALT = TRUE + get_REF_ALT = TRUE, + reference = "example_genome_v1" ) } diff --git a/man/updog2vcf.Rd b/man/updog2vcf.Rd index b0837ae..e321565 100644 --- a/man/updog2vcf.Rd +++ b/man/updog2vcf.Rd @@ -9,7 +9,9 @@ updog2vcf( output.file, updog_version = NULL, RefAlt = NULL, - compress = TRUE + compress = TRUE, + vcf_header = NULL, + reference = NULL ) } \arguments{ @@ -22,6 +24,10 @@ updog2vcf( \item{RefAlt}{optional data frame with four columns named "Chr", "Pos", "Ref", and "Alt" containing the reference and alternate alleles for each SNP in the same order as in the multidog object} \item{compress}{logical. If TRUE returns a vcf.gz file} + +\item{vcf_header}{optional path or URL to a VCF header file. Only its ##reference and ##contig lines are added to the output VCF header} + +\item{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} } \value{ A vcf file @@ -33,6 +39,10 @@ This function will convert an Updog output to a VCF file When performing dosage calling for multiple SNPs using Updog, the output file contains information for all loci and all samples. This function will convert the updog output file to a VCF file, while retaining the information for the values that are commonly used to filter low quality and low confident dosage calls. + +The CHROM and POS columns are read from the SNP IDs, which are expected in the CHR_POS format. +SNPs whose ID does not end in a positive integer position are written with the SNP ID as CHROM +and 1 as POS, with a warning. } \examples{ @@ -46,7 +56,8 @@ updog2vcf( multidog.object = mout, output.file = temp_file, updog_version = "0.0.0", - compress = TRUE + compress = TRUE, + reference = "example_genome_v1" ) #Removing the example vcf diff --git a/tests/testthat/test-dosage2vcf.R b/tests/testthat/test-dosage2vcf.R index 4ea57df..ec287d0 100644 --- a/tests/testthat/test-dosage2vcf.R +++ b/tests/testthat/test-dosage2vcf.R @@ -79,7 +79,7 @@ test_that("test dosage report conversion",{ output_file <- tempfile() #Convert the dart files to vcf - dosage2vcf(dart.report = report, dart.counts = counts, ploidy = ploidy, output.file = output_file) + dosage2vcf(dart.report = report, dart.counts = counts, ploidy = ploidy, output.file = output_file, reference = "test") #Test validity of VCF vcf <- read.vcfR(paste0(output_file,".vcf"), verbose = FALSE) @@ -102,7 +102,7 @@ test_that("SNP/INDEL 1 row reports use diploid genotype codes and align counts b write_snp_1row_fixture(report) write_dart_counts_fixture(counts) - dosage2vcf(dart.report = report, dart.counts = counts, ploidy = 2, output.file = output_file) + dosage2vcf(dart.report = report, dart.counts = counts, ploidy = 2, output.file = output_file, reference = "test") vcf <- read_dosage2vcf_body(output_file) expect_equal(colnames(vcf)[10:12], c("S1", "S2", "S3")) @@ -127,8 +127,8 @@ test_that("SNP/INDEL 2 row reports collapse to the same genotype codes as 1 row write_snp_2row_fixture(snp_2row) write_dart_counts_fixture(counts) - dosage2vcf(dart.report = snp_1row, dart.counts = counts, ploidy = 2, output.file = output_1row) - dosage2vcf(dart.report = snp_2row, dart.counts = counts, ploidy = 2, output.file = output_2row) + dosage2vcf(dart.report = snp_1row, dart.counts = counts, ploidy = 2, output.file = output_1row, reference = "test") + dosage2vcf(dart.report = snp_2row, dart.counts = counts, ploidy = 2, output.file = output_2row, reference = "test") expect_equal(read_dosage2vcf_body(output_2row), read_dosage2vcf_body(output_1row)) }) @@ -140,7 +140,32 @@ test_that("SNP/INDEL reports require diploid ploidy", { write_dart_counts_fixture(counts) expect_error( - dosage2vcf(dart.report = report, dart.counts = counts, ploidy = 4, output.file = tempfile()), + dosage2vcf(dart.report = report, dart.counts = counts, ploidy = 4, output.file = tempfile(), reference = "test"), "diploid genotype reports" ) }) + +test_that("vcf_header and reference set the reference genome lines of the header", { + report <- tempfile(fileext = ".csv") + counts <- tempfile(fileext = ".csv") + write_snp_1row_fixture(report) + write_dart_counts_fixture(counts) + header <- tempfile(fileext = ".txt") + writeLines(c("##reference=file:test_genome.fna", + "##contig=", + "##contig="), header) + + output_file <- tempfile() + dosage2vcf(dart.report = report, dart.counts = counts, ploidy = 2, output.file = output_file, + vcf_header = header) + meta <- grep("^##", readLines(paste0(output_file, ".vcf")), value = TRUE) + expect_equal(sum(meta == "##reference=file:test_genome.fna"), 1) + expect_equal(sum(grepl("^##contig=", meta)), 2) + expect_false(any(grepl("=NA", meta))) + + output_file <- tempfile() + expect_warning(dosage2vcf(dart.report = report, dart.counts = counts, ploidy = 2, output.file = output_file), + "No reference genome information") + meta <- grep("^##", readLines(paste0(output_file, ".vcf")), value = TRUE) + expect_false(any(grepl("^##(reference|contig)=", meta))) +}) diff --git a/tests/testthat/test-filterVCF.R b/tests/testthat/test-filterVCF.R index e777efb..73fc718 100644 --- a/tests/testthat/test-filterVCF.R +++ b/tests/testthat/test-filterVCF.R @@ -246,3 +246,28 @@ test_that("DP and MPP filters run whatever the first record's FORMAT contains",{ expect_equal(filtered@gt[1, -1], vcf@gt[1, -1]) }) + +test_that("Reference genome lines of the header can be updated",{ + + vcf <- read.vcfR(system.file("iris_DArT_VCF.vcf.gz", package = "BIGr"), verbose = FALSE) + + #The ##reference=NA and ##contig= lines are removed. + filtered <- suppressMessages(filterVCF(vcf, ploidy = 2)) + expect_false(any(grepl("^##(reference|contig)=", filtered@meta))) + + #Reference genome lines from a header file, placed after ##fileformat. + chrom <- unique(vcf@fix[, "CHROM"]) + header <- tempfile(fileext = ".txt") + writeLines(c("##reference=file:test_genome.fna", + paste0("##contig=")), header) + filtered <- suppressMessages(filterVCF(vcf, ploidy = 2, vcf_header = header)) + expect_equal(filtered@meta[2], "##reference=file:test_genome.fna") + expect_equal(sum(grepl("^##contig=", filtered@meta)), length(chrom)) + + #The reference argument replaces only the ##reference line. + refiltered <- suppressMessages(filterVCF(filtered, ploidy = 2, reference = "GCF_test.1")) + expect_equal(sum(grepl("^##reference=", refiltered@meta)), 1) + expect_true("##reference=GCF_test.1" %in% refiltered@meta) + expect_equal(sum(grepl("^##contig=", refiltered@meta)), length(chrom)) + +}) diff --git a/tests/testthat/test-madc2vcf_all.R b/tests/testthat/test-madc2vcf_all.R index 2c0a07b..2382046 100644 --- a/tests/testthat/test-madc2vcf_all.R +++ b/tests/testthat/test-madc2vcf_all.R @@ -22,7 +22,8 @@ test_that("test madc offtargets",{ multiallelic_SNP_sample_thr = 0, alignment_score_thr = 40, out_vcf = temp, - verbose = FALSE) + verbose = FALSE, + reference = "test") set.seed(456) madc2vcf_all(madc = madc_file, @@ -34,7 +35,8 @@ test_that("test madc offtargets",{ multiallelic_SNP_sample_thr = 0, alignment_score_thr = 40, out_vcf = temp_multi, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(temp) vcf_multi <- read.vcfR(temp_multi) @@ -56,7 +58,8 @@ test_that("test madc offtargets",{ multiallelic_SNP_dp_thr = 0, multiallelic_SNP_sample_thr = 0, out_vcf = temp, - verbose = FALSE)) + verbose = FALSE, + reference = "test")) vcf <- read.vcfR(temp) @@ -77,7 +80,8 @@ test_that("madc2vcf_all preserves comma-separated AD for biallelic targets", { hap_seq_file = db_file, n.cores = 1, out_vcf = temp, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(temp, verbose = FALSE) ad <- extract.gt(vcf, "AD") @@ -108,7 +112,8 @@ test_that("madc2vcf_all accepts BI_markerID matches when CloneID does not match" markers_info = temp_markers, n.cores = 1, out_vcf = temp, - verbose = FALSE) + verbose = FALSE, + reference = "test") ) }) @@ -125,7 +130,8 @@ test_that("madc2vcf_all surfaces missing-column validation error without crashin madc2vcf_all(madc = temp_madc, botloci_file = bot_file, out_vcf = tempfile(fileext = ".vcf"), - verbose = FALSE), + verbose = FALSE, + reference = "test"), "One or more required columns missing" ) }) @@ -176,7 +182,8 @@ test_that("simu alfalfa",{ multiallelic_SNP_dp_thr = 0, alignment_score_thr = 40, out_vcf = out, - verbose = FALSE) + verbose = FALSE, + reference = "test") ) vcf <- read.vcfR(out, verbose = FALSE) expect_s4_class(vcf, "vcfR") @@ -196,7 +203,8 @@ test_that("simu alfalfa",{ hap_seq_file = NULL, n.cores = 1, out_vcf = out, - verbose = FALSE) + verbose = FALSE, + reference = "test") ) vcf <- read.vcfR(out, verbose = FALSE) expect_s4_class(vcf, "vcfR") @@ -216,7 +224,8 @@ test_that("simu alfalfa",{ hap_seq_file = alfalfa_microhapDB, n.cores = 1, out_vcf = out, - verbose = FALSE) + verbose = FALSE, + reference = "test") ) # Test that it also works when markers_info is provided together with botloci @@ -228,7 +237,8 @@ test_that("simu alfalfa",{ n.cores = 1, markers_info = alfalfa_markers_info, out_vcf = out, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) expect_s4_class(vcf, "vcfR") @@ -249,7 +259,8 @@ test_that("simu alfalfa",{ n.cores = 1, markers_info = alfalfa_markers_info, out_vcf = out, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) expect_s4_class(vcf, "vcfR") @@ -272,7 +283,8 @@ test_that("simu alfalfa",{ hap_seq_file = alfalfa_microhapDB, n.cores = 1, out_vcf = out, - verbose = FALSE), + verbose = FALSE, + reference = "test"), regexp = "Check marker IDs in both MADC and botloci files. They should be the same." ) @@ -284,7 +296,8 @@ test_that("simu alfalfa",{ n.cores = 1, markers_info = alfalfa_markers_info, out_vcf = out, - verbose = FALSE), + verbose = FALSE, + reference = "test"), regexp = "None of the markers_info CloneID, Marker_ID, or BI_markerID values match the MADC CloneID column. Please make sure they use the same marker IDs." ) @@ -295,7 +308,8 @@ test_that("simu alfalfa",{ n.cores = 1, markers_info = alfalfa_markers_info_ChromPos, out_vcf = out, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) expect_s4_class(vcf, "vcfR") @@ -321,7 +335,8 @@ test_that("simu alfalfa",{ hap_seq_file = alfalfa_microhapDB, n.cores = 1, out_vcf = out, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(alfalfa_markers_info) @@ -341,7 +356,8 @@ test_that("simu alfalfa",{ hap_seq_file = NULL, n.cores = 1, out_vcf = out, - verbose = FALSE)) + verbose = FALSE, + reference = "test")) vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(alfalfa_markers_info) @@ -362,7 +378,8 @@ test_that("simu alfalfa",{ n.cores = 1, markers_info = alfalfa_markers_info, out_vcf = out, - verbose = FALSE)) + verbose = FALSE, + reference = "test")) vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(alfalfa_markers_info) @@ -388,7 +405,8 @@ test_that("simu alfalfa",{ hap_seq_file = alfalfa_microhapDB, n.cores = 1, out_vcf = out, - verbose = FALSE), + verbose = FALSE, + reference = "test"), regexp = "IUPAC \\(non-ATCG\\) codes found in Ref_/Alt_ AlleleSequence that differ between alleles or are present in only one. These codes are not currently supported by BIGr/BIGapp" ) }) @@ -402,7 +420,8 @@ test_that("simu alfalfa",{ hap_seq_file = potato_microhapDB, n.cores = 1, out_vcf = out, - verbose = FALSE), + verbose = FALSE, + reference = "test"), regexp = "CloneID does not have the expected Chromosome_Position format. Please check your CloneIDs or provide a file with this information" ) @@ -413,7 +432,8 @@ test_that("simu alfalfa",{ n.cores = 1, markers_info = potato_markers_info, out_vcf = out, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(potato_markers_info) @@ -434,7 +454,8 @@ test_that("simu alfalfa",{ n.cores = 1, markers_info = potato_markers_info, out_vcf = out, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(potato_markers_info) @@ -456,7 +477,8 @@ test_that("simu alfalfa",{ hap_seq_file = NULL, n.cores = 1, out_vcf = out, - verbose = FALSE), + verbose = FALSE, + reference = "test"), regexp = "CloneID does not have the expected Chromosome_Position format. Please check your CloneIDs or provide a file with this information" ) @@ -471,7 +493,8 @@ test_that("simu alfalfa",{ hap_seq_file = potato_microhapDB, n.cores = 1, out_vcf = out, - verbose = FALSE), + verbose = FALSE, + reference = "test"), regexp = "CloneID does not have the expected Chromosome_Position format. Please check your CloneIDs or provide a file with this information" ) @@ -481,7 +504,8 @@ test_that("simu alfalfa",{ n.cores = 1, markers_info = potato_markers_info, out_vcf = out, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(potato_markers_info) @@ -502,7 +526,8 @@ test_that("simu alfalfa",{ n.cores = 1, markers_info = potato_markers_info, out_vcf = out, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(potato_markers_info) @@ -524,7 +549,8 @@ test_that("simu alfalfa",{ hap_seq_file = NULL, n.cores = 1, out_vcf = out, - verbose = FALSE), + verbose = FALSE, + reference = "test"), regexp = "CloneID does not have the expected Chromosome_Position format. Please check your CloneIDs or provide a file with this information" ) }) @@ -537,7 +563,8 @@ test_that("simu alfalfa",{ n.cores = 1, markers_info = potato_markers_info, out_vcf = out, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(potato_markers_info) @@ -560,7 +587,8 @@ test_that("simu alfalfa",{ n.cores = 1, markers_info = potato_markers_info_ChromPos, out_vcf = out, - verbose = FALSE), + verbose = FALSE, + reference = "test"), regexp = "Indels detected in MADC file. The markers_info file must contain 'Ref', 'Alt', and 'Indel_pos' columns." ) }) @@ -575,7 +603,8 @@ test_that("simu alfalfa",{ n.cores = 1, markers_info = potato_markers_info, out_vcf = out, - verbose = FALSE), + verbose = FALSE, + reference = "test"), regexp = "IUPAC \\(non-ATCG\\) codes found in Ref_/Alt_ AlleleSequence that differ between alleles or are present in only one. These codes are not currently supported by BIGr/BIGapp" ) }) @@ -589,7 +618,8 @@ test_that("simu alfalfa",{ hap_seq_file = alfalfa_microhapDB, n.cores = 1, out_vcf = out, - verbose = FALSE), regexp = "MADC not processed by HapApp" + verbose = FALSE, + reference = "test"), regexp = "MADC not processed by HapApp" ) }) }) diff --git a/tests/testthat/test-madc2vcf_multi.R b/tests/testthat/test-madc2vcf_multi.R index 58450dd..60b68f3 100644 --- a/tests/testthat/test-madc2vcf_multi.R +++ b/tests/testthat/test-madc2vcf_multi.R @@ -42,7 +42,8 @@ test_that("madc2vcf_multi — alfalfa (BIGapp-PanelHub)", { botloci_file = alfalfa_botloci, outfile = out, ploidy = 4L, - verbose = TRUE + verbose = TRUE, + reference = "test" ) ) @@ -60,7 +61,8 @@ test_that("madc2vcf_multi — alfalfa (BIGapp-PanelHub)", { botloci_file = alfalfa_botloci, outfile = out, ploidy = 4L, - verbose = FALSE + verbose = FALSE, + reference = "test" ), regexp = "The MADC file does not have fixed AlleleIDs. Please process the MADC file through HapApp before using this function." ) @@ -71,7 +73,8 @@ test_that("madc2vcf_multi — alfalfa (BIGapp-PanelHub)", { botloci_file = alfalfa_botloci, outfile = out, ploidy = 4L, - verbose = TRUE + verbose = TRUE, + reference = "test" ) ) @@ -83,7 +86,8 @@ test_that("madc2vcf_multi — alfalfa (BIGapp-PanelHub)", { botloci_file = alfalfa_botloci, outfile = out, ploidy = 4L, - verbose = TRUE + verbose = TRUE, + reference = "test" ), regexp = "Check marker IDs in both MADC and botloci files. They should be the same." ) @@ -95,7 +99,8 @@ test_that("madc2vcf_multi — alfalfa (BIGapp-PanelHub)", { outfile = out, markers_info = alfalfa_markers_info_ChromPos, ploidy = 4L, - verbose = TRUE + verbose = TRUE, + reference = "test" ) ) @@ -113,7 +118,8 @@ test_that("madc2vcf_multi — alfalfa (BIGapp-PanelHub)", { botloci_file = alfalfa_botloci, outfile = out, ploidy = 4L, - verbose = TRUE + verbose = TRUE, + reference = "test" ), regexp = "MADC Allele Sequences contain different IUPAC \\(non-ATCG\\) codes in REF and ALT sequences. Please run HapApp to clean MADC file before using this function." ) @@ -124,7 +130,8 @@ test_that("madc2vcf_multi — alfalfa (BIGapp-PanelHub)", { botloci_file = alfalfa_botloci, outfile = out, ploidy = 4L, - verbose = TRUE + verbose = TRUE, + reference = "test" ), regexp = "Not all Ref sequences have a corresponding Alt or vice versa. Please provide a complete MADC file before using this function." ) @@ -136,7 +143,8 @@ test_that("madc2vcf_multi — alfalfa (BIGapp-PanelHub)", { outfile = out, markers_info = potato_markers_info_ChromPos, ploidy = 4L, - verbose = TRUE + verbose = TRUE, + reference = "test" ) ) diff --git a/tests/testthat/test-madc2vcf_targets.R b/tests/testthat/test-madc2vcf_targets.R index 9d04042..38afd3d 100644 --- a/tests/testthat/test-madc2vcf_targets.R +++ b/tests/testthat/test-madc2vcf_targets.R @@ -10,7 +10,7 @@ test_that("test madc conversion",{ #Convert the dart files to vcf suppressWarnings( - madc2vcf_targets(madc_file = madc_file, output.file = temp, get_REF_ALT = FALSE) + madc2vcf_targets(madc_file = madc_file, output.file = temp, get_REF_ALT = FALSE, reference = "test") ) #Test validity of VCF @@ -33,7 +33,7 @@ test_that("test madc conversion",{ temp <- tempfile(fileext = ".vcf") suppressWarnings( - madc2vcf_targets(madc_file = madc_file, output.file = temp, get_REF_ALT = TRUE, botloci_file = bot_file) + madc2vcf_targets(madc_file = madc_file, output.file = temp, get_REF_ALT = TRUE, botloci_file = bot_file, reference = "test") ) #Test validity of VCF @@ -69,7 +69,8 @@ test_that("bottom strand markers have correct REF/ALT", { # Get results from both functions suppressWarnings( madc2vcf_targets(madc_file = madc_file, output.file = temp_targets, - get_REF_ALT = TRUE, botloci_file = bot_file) + get_REF_ALT = TRUE, botloci_file = bot_file, + reference = "test") ) vcf_targets <- read.vcfR(temp_targets, verbose = FALSE) @@ -97,7 +98,7 @@ test_that("madc2vcf_targets preserves original sample names", { write.csv(report, temp_madc, row.names = FALSE, quote = TRUE) suppressWarnings( - madc2vcf_targets(madc_file = temp_madc, output.file = temp_vcf, get_REF_ALT = FALSE) + madc2vcf_targets(madc_file = temp_madc, output.file = temp_vcf, get_REF_ALT = FALSE, reference = "test") ) vcf <- read.vcfR(temp_vcf, verbose = FALSE) @@ -117,7 +118,8 @@ test_that("madc2vcf_targets surfaces missing-column validation error without cra madc2vcf_targets(madc_file = temp_madc, output.file = tempfile(fileext = ".vcf"), get_REF_ALT = FALSE, - verbose = FALSE), + verbose = FALSE, + reference = "test"), "One or more required columns missing" ) }) @@ -159,7 +161,8 @@ test_that("simu alfalfa",{ madc2vcf_targets(madc_file = alfalfa_madc, output.file = out, get_REF_ALT = FALSE, - verbose = FALSE) + verbose = FALSE, + reference = "test") ) vcf <- read.vcfR(out, verbose = FALSE) expect_s4_class(vcf, "vcfR") @@ -175,7 +178,8 @@ test_that("simu alfalfa",{ output.file = out, get_REF_ALT = FALSE, collapse_matches_counts = TRUE, - verbose = TRUE) + verbose = TRUE, + reference = "test") ) vcf <- read.vcfR(out, verbose = FALSE) expect_s4_class(vcf, "vcfR") @@ -190,7 +194,8 @@ test_that("simu alfalfa",{ madc2vcf_targets(madc_file = alfalfa_madc, output.file = out, get_REF_ALT = TRUE, - verbose = FALSE), + verbose = FALSE, + reference = "test"), regexp = "get_REF_ALT = TRUE but no markers_info file with Ref and Alt columns was provided neither a botloci_file to extrat REF/ALT from probe sequences. Please provide one of the these files or set get_REF_ALT to FALSE." ) @@ -199,7 +204,8 @@ test_that("simu alfalfa",{ output.file = out, get_REF_ALT = TRUE, botloci_file = alfalfa_botloci, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(alfalfa_markers_info) @@ -218,7 +224,8 @@ test_that("simu alfalfa",{ get_REF_ALT = TRUE, botloci_file = alfalfa_botloci, markers_info = alfalfa_markers_info, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(alfalfa_markers_info) @@ -241,7 +248,8 @@ test_that("simu alfalfa",{ output.file = out, get_REF_ALT = TRUE, botloci_file = alfalfa_botloci, - verbose = FALSE), + verbose = FALSE, + reference = "test"), regexp = "Check marker IDs in both MADC and botloci files. They should be the same." ) @@ -252,7 +260,8 @@ test_that("simu alfalfa",{ get_REF_ALT = TRUE, botloci_file = alfalfa_botloci, markers_info = alfalfa_markers_info, - verbose = FALSE), + verbose = FALSE, + reference = "test"), "None of the MADC CloneID could be found in the markers_info CloneID, Marker_ID or BI_markerID. Please make sure they match." ) @@ -264,7 +273,8 @@ test_that("simu alfalfa",{ get_REF_ALT = TRUE, botloci_file = alfalfa_botloci, markers_info = alfalfa_markers_info_ChromPos, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(alfalfa_markers_info) @@ -285,7 +295,8 @@ test_that("simu alfalfa",{ output.file = out, get_REF_ALT = TRUE, botloci_file = alfalfa_botloci, - verbose = FALSE) + verbose = FALSE, + reference = "test") ) vcf <- read.vcfR(out, verbose = FALSE) @@ -305,7 +316,8 @@ test_that("simu alfalfa",{ get_REF_ALT = TRUE, botloci_file = alfalfa_botloci, markers_info = alfalfa_markers_info, - verbose = FALSE) + verbose = FALSE, + reference = "test") ) vcf <- read.vcfR(out, verbose = FALSE) @@ -325,7 +337,8 @@ test_that("simu alfalfa",{ get_REF_ALT = FALSE, botloci_file = alfalfa_botloci, markers_info = alfalfa_markers_info, - verbose = FALSE) + verbose = FALSE, + reference = "test") ) vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(alfalfa_markers_info) @@ -344,14 +357,16 @@ test_that("simu alfalfa",{ output.file = out, get_REF_ALT = TRUE, botloci_file = alfalfa_botloci, - verbose = FALSE) + verbose = FALSE, + reference = "test") ) madc2vcf_targets(madc_file = alfalfa_iupac, output.file = out, get_REF_ALT = TRUE, markers_info = alfalfa_markers_info, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(alfalfa_markers_info) @@ -369,7 +384,8 @@ test_that("simu alfalfa",{ get_REF_ALT = TRUE, markers_info = alfalfa_markers_info, collapse_matches_counts = TRUE, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(alfalfa_markers_info) @@ -387,7 +403,8 @@ test_that("simu alfalfa",{ output.file = out, get_REF_ALT = FALSE, botloci_file = alfalfa_botloci, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(alfalfa_markers_info) @@ -407,14 +424,16 @@ test_that("simu alfalfa",{ output.file = out, get_REF_ALT = TRUE, botloci_file = potato_botloci, - verbose = FALSE) + verbose = FALSE, + reference = "test") ) madc2vcf_targets(madc_file = potato_indel_madc, output.file = out, get_REF_ALT = TRUE, markers_info = potato_markers_info, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(potato_markers_info) @@ -432,7 +451,8 @@ test_that("simu alfalfa",{ get_REF_ALT = TRUE, markers_info = potato_markers_info, collapse_matches_counts = TRUE, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(potato_markers_info) @@ -451,14 +471,16 @@ test_that("simu alfalfa",{ output.file = out, get_REF_ALT = FALSE, botloci_file = potato_botloci, - verbose = FALSE) + verbose = FALSE, + reference = "test") ) madc2vcf_targets(madc_file = potato_indel_madc, output.file = out, get_REF_ALT = FALSE, markers_info = potato_markers_info, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(potato_markers_info) @@ -477,14 +499,16 @@ test_that("simu alfalfa",{ output.file = out, get_REF_ALT = TRUE, botloci_file = potato_botloci, - verbose = FALSE) + verbose = FALSE, + reference = "test") ) madc2vcf_targets(madc_file = potato_more_indels_chrompos_false, output.file = out, get_REF_ALT = TRUE, markers_info = potato_markers_info, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(potato_markers_info) @@ -502,7 +526,8 @@ test_that("simu alfalfa",{ get_REF_ALT = TRUE, markers_info = potato_markers_info, collapse_matches_counts = TRUE, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(potato_markers_info) @@ -521,14 +546,16 @@ test_that("simu alfalfa",{ output.file = out, get_REF_ALT = FALSE, botloci_file = potato_botloci, - verbose = FALSE) + verbose = FALSE, + reference = "test") ) madc2vcf_targets(madc_file = potato_more_indels_chrompos_false, output.file = out, get_REF_ALT = FALSE, markers_info = potato_markers_info, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(potato_markers_info) @@ -546,7 +573,8 @@ test_that("simu alfalfa",{ output.file = out, get_REF_ALT = TRUE, markers_info = potato_markers_info, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(potato_markers_info) @@ -565,7 +593,8 @@ test_that("simu alfalfa",{ get_REF_ALT = TRUE, markers_info = potato_markers_info_ChromPos, botloci_file = potato_botloci, - verbose = FALSE) + verbose = FALSE, + reference = "test") ) madc2vcf_targets(madc_file = potato_indel_lowercase, @@ -573,7 +602,8 @@ test_that("simu alfalfa",{ get_REF_ALT = TRUE, markers_info = potato_markers_info, botloci_file = potato_botloci, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(potato_markers_info) @@ -591,7 +621,8 @@ test_that("simu alfalfa",{ get_REF_ALT = TRUE, markers_info = potato_markers_info, collapse_matches_counts = TRUE, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(potato_markers_info) @@ -609,7 +640,8 @@ test_that("simu alfalfa",{ output.file = out, get_REF_ALT = FALSE, markers_info = potato_markers_info, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(potato_markers_info) @@ -628,7 +660,8 @@ test_that("simu alfalfa",{ output.file = out, get_REF_ALT = TRUE, markers_info = potato_markers_info, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(potato_markers_info) @@ -646,7 +679,8 @@ test_that("simu alfalfa",{ get_REF_ALT = TRUE, markers_info = potato_markers_info, collapse_matches_counts = TRUE, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(potato_markers_info) @@ -664,7 +698,8 @@ test_that("simu alfalfa",{ output.file = out, get_REF_ALT = FALSE, markers_info = potato_markers_info, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(potato_markers_info) @@ -683,7 +718,8 @@ test_that("simu alfalfa",{ madc2vcf_targets(madc_file = alfalfa_madc_raw, output.file = out, get_REF_ALT = TRUE, - verbose = FALSE) + verbose = FALSE, + reference = "test") ) expect_error( @@ -691,13 +727,15 @@ test_that("simu alfalfa",{ output.file = out, get_REF_ALT = TRUE, botloci_file = alfalfa_botloci, - verbose = FALSE) + verbose = FALSE, + reference = "test") ) madc2vcf_targets(madc_file = alfalfa_madc_raw, output.file = out, get_REF_ALT = FALSE, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(alfalfa_markers_info) @@ -712,7 +750,8 @@ test_that("simu alfalfa",{ output.file = out, get_REF_ALT = FALSE, collapse_matches_counts = TRUE, - verbose = TRUE) + verbose = TRUE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) expect_s4_class(vcf, "vcfR") @@ -727,7 +766,8 @@ test_that("simu alfalfa",{ get_REF_ALT = TRUE, markers_info = alfalfa_markers_info, collapse_matches_counts = FALSE, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(alfalfa_markers_info) @@ -745,7 +785,8 @@ test_that("simu alfalfa",{ get_REF_ALT = TRUE, markers_info = alfalfa_markers_info, collapse_matches_counts = TRUE, - verbose = FALSE) + verbose = FALSE, + reference = "test") vcf <- read.vcfR(out, verbose = FALSE) lut <- read.csv(alfalfa_markers_info) @@ -777,7 +818,8 @@ test_that("markers missing from markers_info are reported before the POS correct warns <- character(0) withCallingHandlers( madc2vcf_targets(madc_file = madc_file, output.file = temp, markers_info = mi_file, - get_REF_ALT = FALSE, verbose = FALSE), + get_REF_ALT = FALSE, verbose = FALSE, + reference = "test"), warning = function(w) { warns <<- c(warns, conditionMessage(w)) invokeRestart("muffleWarning") @@ -790,3 +832,54 @@ test_that("markers missing from markers_info are reported before the POS correct expect_equal(nrow(vcf), 19) expect_false("chr1.1_000194324" %in% vcf@fix[, "ID"]) }) + +test_that("vcf_header and reference set the reference genome lines of the header", { + madc_file <- system.file("example_MADC_FixedAlleleID.csv", package="BIGr") + header <- tempfile(fileext = ".txt") + writeLines(c("##fileformat=VCFv4.3", + "##reference=file:test_genome.fna", + "##contig=", + '##INFO='), + header) + read_meta <- function(f) grep("^##", readLines(f), value = TRUE) + + # Header file: its ##reference and ##contig lines are used, its INFO lines are not + temp <- tempfile(fileext = ".vcf") + suppressWarnings(madc2vcf_targets(madc_file = madc_file, output.file = temp, vcf_header = header, verbose = FALSE)) + meta <- read_meta(temp) + expect_equal(sum(meta == "##reference=file:test_genome.fna"), 1) + expect_equal(sum(meta == "##contig="), 1) + expect_false(any(grepl("=NA", meta))) + expect_false(any(grepl("ID=HH", meta))) + expect_true(any(grepl("^##BIGrCommandLine.madc2vcf_targets", meta))) + expect_equal(meta[1], "##fileformat=VCFv4.3") + + # Reference ID only + temp <- tempfile(fileext = ".vcf") + suppressWarnings(madc2vcf_targets(madc_file = madc_file, output.file = temp, reference = "GCF_test.1", verbose = FALSE)) + meta <- read_meta(temp) + expect_equal(sum(meta == "##reference=GCF_test.1"), 1) + expect_false(any(grepl("^##contig=", meta))) + + # Nothing provided: no reference genome lines and a warning + temp <- tempfile(fileext = ".vcf") + expect_warning(madc2vcf_targets(madc_file = madc_file, output.file = temp, verbose = FALSE), + "No reference genome information") + meta <- read_meta(temp) + expect_false(any(grepl("^##(reference|contig)=", meta))) + expect_true(any(grepl("^##BIGrCommandLine.madc2vcf_targets", meta))) + + # A path given as a quoted string is escaped in the ##BIGrCommandLine line + temp <- tempfile(fileext = ".vcf") + suppressWarnings(madc2vcf_targets(madc_file = system.file("example_MADC_FixedAlleleID.csv", package="BIGr"), + output.file = temp, reference = "GCF_test.1", verbose = FALSE)) + cmd <- grep("^##BIGrCommandLine", readLines(temp), value = TRUE) + expect_true(grepl('",CommandLine="> madc2vcf_targets(system.file(\\"example_MADC_FixedAlleleID.csv\\"', cmd, fixed = TRUE)) + expect_true(endsWith(cmd, ')">')) + + # Header for another genome + writeLines(c("##reference=file:other_genome.fna", "##contig="), header) + expect_warning(madc2vcf_targets(madc_file = madc_file, output.file = tempfile(fileext = ".vcf"), + vcf_header = header, verbose = FALSE), + "not listed in the ##contig lines") +}) diff --git a/tests/testthat/test-updog2vcf.R b/tests/testthat/test-updog2vcf.R index dab1188..fd99ca2 100644 --- a/tests/testthat/test-updog2vcf.R +++ b/tests/testthat/test-updog2vcf.R @@ -12,7 +12,8 @@ test_that("test updog conversion",{ multidog.object = mout, output.file = temp_file, updog_version = "0.0.0", - compress = TRUE + compress = TRUE, + reference = "test" ) vcf_result <- read.vcfR(paste0(temp_file,".vcf.gz"), verbose = FALSE) @@ -26,3 +27,45 @@ test_that("test updog conversion",{ expect_equal(MPP, 39.99, tolerance = 0.01) }) + +test_that("reference sets the ##reference line of the header", { + load(system.file("extdata", "iris-multidog.rdata", package = "BIGr")) + + temp_file <- tempfile() + updog2vcf( + multidog.object = mout, + output.file = temp_file, + updog_version = "0.0.0", + compress = FALSE, + reference = "GCF_test.1" + ) + + meta <- grep("^##", readLines(paste0(temp_file, ".vcf")), value = TRUE) + expect_equal(sum(meta == "##reference=GCF_test.1"), 1) + expect_false(any(grepl("^##contig=", meta))) + expect_true(any(grepl("^##BIGrCommandLine.updog2vcf", meta))) +}) + +test_that("SNPs without a position in the SNP ID are kept with the ID as CHROM and POS 1", { + load(system.file("extdata", "iris-multidog.rdata", package = "BIGr")) + + # One ID without an underscore and two with a position that is not a number + new_ids <- c(Chr1_10 = "markerA", Chr1_100 = "Chr1_abc", Chr4_810 = "Chr1_xyz") + mout$snpdf$snp <- ifelse(mout$snpdf$snp %in% names(new_ids), new_ids[mout$snpdf$snp], mout$snpdf$snp) + mout$inddf$snp <- ifelse(mout$inddf$snp %in% names(new_ids), new_ids[mout$inddf$snp], mout$inddf$snp) + + temp_file <- tempfile() + expect_warning( + updog2vcf(multidog.object = mout, output.file = temp_file, updog_version = "0.0.0", + compress = FALSE, reference = "GCF_test.1"), + "3 SNPs have a position that could not be read from the SNP ID" + ) + + lines <- readLines(paste0(temp_file, ".vcf")) + body <- do.call(rbind, strsplit(lines[!startsWith(lines, "#")], "\t")) + expect_equal(nrow(body), 10) + changed <- body[body[, 3] %in% new_ids, , drop = FALSE] + expect_equal(changed[, 1], changed[, 3]) + expect_true(all(changed[, 2] == "1")) + expect_false(any(body[, 2] %in% c("0", "NA"))) +}) diff --git a/tests/testthat/test-vcf_header.R b/tests/testthat/test-vcf_header.R new file mode 100644 index 0000000..557aa18 --- /dev/null +++ b/tests/testthat/test-vcf_header.R @@ -0,0 +1,100 @@ +context("VCF header reference genome lines") + +write_header_fixture <- function(path, contigs = c("Chr01", "Chr02"), reference = TRUE) { + writeLines(c("##fileformat=VCFv4.3", + if (reference) "##reference=file:test_genome.fna", + paste0("##contig="), + '##INFO=', + '##FORMAT='), + path) +} + +test_that("vcf_header gives the reference and contig lines only", { + header <- tempfile(fileext = ".txt") + write_header_fixture(header) + + res <- vcf_reference_lines(vcf_header = header) + expect_equal(res$lines, c("##reference=file:test_genome.fna", + "##contig=", + "##contig=")) + expect_equal(res$contigs, c("Chr01", "Chr02")) + + # reference replaces the ##reference line of the file + res <- vcf_reference_lines(vcf_header = header, reference = "GCF_020283565.1") + expect_equal(res$lines[1], "##reference=GCF_020283565.1") + expect_equal(length(res$lines), 3) +}) + +test_that("reference alone gives the ##reference line", { + res <- vcf_reference_lines(reference = "GCF_020283565.1") + expect_equal(res$lines, "##reference=GCF_020283565.1") + expect_null(res$contigs) +}) + +test_that("no reference information gives no lines and a warning", { + expect_warning(res <- vcf_reference_lines(), "No reference genome information") + expect_equal(res$lines, character(0)) + expect_null(res$contigs) + expect_no_warning(vcf_reference_lines(warn = FALSE)) +}) + +test_that("invalid vcf_header and reference inputs give errors", { + expect_error(vcf_reference_lines(vcf_header = tempfile()), "vcf_header file not found") + + no_ref <- tempfile(fileext = ".txt") + writeLines(c("##fileformat=VCFv4.3", '##INFO='), no_ref) + expect_error(vcf_reference_lines(vcf_header = no_ref), "no ##reference or ##contig lines") + + two_ref <- tempfile(fileext = ".txt") + writeLines(c("##reference=a.fna", "##reference=b.fna"), two_ref) + expect_error(vcf_reference_lines(vcf_header = two_ref), "more than one ##reference line") + + bad_contig <- tempfile(fileext = ".txt") + writeLines(c("##reference=a.fna", "##contig="), bad_contig) + expect_error(vcf_reference_lines(vcf_header = bad_contig), "without an ID") + + expect_error(vcf_reference_lines(reference = c("a", "b")), "single character string") + expect_error(vcf_reference_lines(reference = ""), "single character string") +}) + +test_that("chromosomes missing from the contig lines give a warning", { + expect_warning(check_vcf_contigs(c("Chr01", "Chr03", "Chr03"), c("Chr01", "Chr02")), + "1 chromosome\\(s\\) in the VCF are not listed.*Chr03") + expect_no_warning(check_vcf_contigs(c("Chr01", "Chr02"), c("Chr01", "Chr02"))) + expect_no_warning(check_vcf_contigs(c("Chr01", "Chr03"), NULL)) +}) + +test_that("the meta lines of a VCF file can be replaced", { + vcf <- tempfile(fileext = ".vcf") + body <- c("#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO", + "Chr01\t100\tChr01_000000100\tA\tG\t.\t.\tDP=10") + writeLines(c("##fileformat=VCFv4.3", "##source=polyRADv2", "##contig=", body), vcf) + + expect_equal(read_vcf_meta(vcf), c("##fileformat=VCFv4.3", "##source=polyRADv2", "##contig=")) + + new_meta <- c("##fileformat=VCFv4.3", "##reference=file:test_genome.fna", + "##contig=", "##source=polyRADv2") + replace_vcf_meta(vcf, new_meta) + expect_equal(readLines(vcf), c(new_meta, body)) +}) + +test_that("reference genome lines of an existing header are replaced", { + meta <- c("##fileformat=VCFv4.3", "##source=polyRADv2", "##contig=", "##contig=") + ref_lines <- list(lines = c("##reference=file:test_genome.fna", "##contig="), + contigs = "Chr01") + expect_equal(update_vcf_reference_lines(meta, ref_lines), + c("##fileformat=VCFv4.3", "##reference=file:test_genome.fna", + "##contig=", "##source=polyRADv2")) + + # A reference ID alone keeps the existing contig lines + ref_lines <- list(lines = "##reference=GCF_test.1", contigs = NULL) + expect_equal(update_vcf_reference_lines(meta, ref_lines), + c("##fileformat=VCFv4.3", "##reference=GCF_test.1", "##source=polyRADv2", + "##contig=", "##contig=")) +}) + +test_that("quotes and backslashes are escaped for quoted header values", { + expect_equal(vcf_quote_escape('> f("a.csv")'), '> f(\\"a.csv\\")') + expect_equal(vcf_quote_escape("C:\\data\\a.csv"), "C:\\\\data\\\\a.csv") + expect_equal(vcf_quote_escape("no quotes"), "no quotes") +})