diff options
| author | Pjotr <wrk@bosa.lan> | 2026-11-17 13:02:21 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-11-17 13:02:21 +0000 |
| commit | df7fb3dd11be0a27de8904f7a4cb1309bdb64575 (patch) | |
| tree | 34995c2e6cc2797e9c14b169fb9ce6282871b356 | |
| parent | 1fa28faac92bd206cca46b6987c4973360ddadbe (diff) | |
| download | genenetwork4-df7fb3dd11be0a27de8904f7a4cb1309bdb64575.tar.gz | |
qtl2-convert-geno.R: convert a .geno file to a cross in R, where the format lives
The conversion was Rust writing qtl2's files by hand, and one of qtl2's
conventions was wrong in it: the genotype codes. This does the same
conversion as an R script and lets qtl2 write its own JSON metadata with
write_control_file, so the layout and the codes come from the format's
definition rather than from a second implementation of it.
qtl2-convert-geno.R --geno BXD.8.geno --out DIR [--het drop|keep]
[--crosstype riself|f2]
The allele letters are read from the file's @mat/@pat/@het header lines
(B/D as often as A/B), autosomes only, the heterozygous class dropped for
a riself cross as R/qtl's convert2riself does, and missing calls left
missing because imputing them is the point of the cross.
Measured on BXD.8.geno: 20453 autosomal markers, 235 individuals, 610684
heterozygous calls dropped and 1533 missing cells left to the HMM.
read_cross2 -> calc_genoprob(error_prob 0.002) -> maxmarg(minprob 0.5)
reproduces 4193962 of 4194238 kept calls (99.99%) and imputes the 612217
dropped ones almost evenly (A 306841, B 305376). At rsm10000005700,
where the old codes produced 218 AA and 17 BB against a truth of 84 A and
125 B, all 209 kept calls are reproduced.
The Rust converter still runs; handing /compute/qtl2 over to this script
is the next commit.
| -rwxr-xr-x | scripts/qtl2-convert-geno.R | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/scripts/qtl2-convert-geno.R b/scripts/qtl2-convert-geno.R new file mode 100755 index 0000000..c688930 --- /dev/null +++ b/scripts/qtl2-convert-geno.R @@ -0,0 +1,135 @@ +#!/usr/bin/env Rscript +# qtl2-convert-geno.R -- convert a GeneNetwork `.geno` file into an R/qtl2 cross. +# +# This is the conversion that used to live in src/qtl2.rs, done where it belongs: +# the output *is* an R/qtl2 cross, the format is qtl2's own, and the JSON +# metadata that describes it is written by qtl2's `write_control_file` rather +# than by hand. Writing those files in Rust meant reimplementing qtl2's +# conventions in another language, and one of them was wrong: the genotype +# codes. With the heterozygous class dropped (the `riself` default) the cross +# carries two alleles, and they must be numbered 1 and 2. Numbering the second +# one 3 gave qtl2's two-state HMM an allele code it cannot use -- it read that +# allele as unusable, P(second allele) never rose above 0.5, every individual +# was called the first allele, and a scan on those calls lost the signal +# (2.0e-01 where the same marker is 5.9e-07 from the BIMBAM). +# +# qtl2-convert-geno.R --geno GROUP.geno --out DIR [--het drop|keep] [--crosstype riself|f2] +# +# writes DIR/{control.json,geno.csv,gmap.csv,pmap.csv,pheno.csv} -- the layout +# `read_cross2(DIR/control.json)` reads back -- and prints a one-line summary. +# Autosomes only: the X needs a sex covariate and the other chromosomes +# degenerate the per-chromosome kinship (the same limit /compute/qtl2 has). + +suppressPackageStartupMessages(library(qtl2)) + +args <- commandArgs(trailingOnly = TRUE) +get <- function(flag, default = NULL) { + i <- match(flag, args) + if (is.na(i)) return(default) + args[i + 1] +} +geno_path <- get("--geno") +out_dir <- get("--out") +het <- match.arg(get("--het", "drop"), c("drop", "keep")) +crosstype <- match.arg(get("--crosstype", "riself"), c("riself", "f2")) +if (is.null(geno_path) || is.null(out_dir)) { + stop("usage: qtl2-convert-geno.R --geno GROUP.geno --out DIR [--het drop|keep] [--crosstype riself|f2]", + call. = FALSE) +} + +lines <- readLines(geno_path, warn = FALSE) +meta <- function(key) { + hit <- grep(paste0("^@", key, ":"), lines, value = TRUE) + if (length(hit) == 0) NULL else trimws(sub(paste0("^@", key, ":"), "", hit[1])) +} +# The allele letters are declared by the file, not assumed: GeneNetwork panels +# use B/D as often as A/B. +mat <- meta("mat"); pat <- meta("pat"); het_code <- meta("het") +if (is.null(mat) || is.null(pat)) { + stop("the .geno header declares no @mat/@pat codes, so its genotypes cannot be coded", + call. = FALSE) +} + +hdr <- lines[tolower(substr(lines, 1, 4)) == "chr\t"][1] +if (is.na(hdr)) stop("the .geno file has no 'Chr' header row", call. = FALSE) +cols <- strsplit(hdr, "\t")[[1]] +individuals <- cols[-(1:4)] + +body <- lines[!startsWith(lines, "#") & lines != hdr] +autosomes <- as.character(1:19) + +geno <- list(); gmap <- list(); pmap <- list() +dropped <- 0L +for (line in body) { + f <- strsplit(line, "\t")[[1]] + if (length(f) < 5) next + chr <- f[1] + if (!(chr %in% autosomes)) next + marker <- f[2] + calls <- f[-(1:4)] + # A call is A, B or (if the heterozygous class is kept) H; anything else -- + # U or blank -- is missing and stays missing, because that is what the HMM is + # for. + calls[calls == mat] <- "A" + calls[calls == pat] <- "B" + if (!is.null(het_code)) { + calls[calls == het_code] <- if (het == "keep") "H" else "" + dropped <- dropped + if (het == "drop") sum(f[-(1:4)] == het_code) else 0L + } + calls[!(calls %in% c("A", "B", "H"))] <- "" + geno[[marker]] <- calls + gmap[[marker]] <- c(chr = chr, pos = suppressWarnings(as.numeric(f[3]))) + pmap[[marker]] <- c(chr = chr, pos = suppressWarnings(as.numeric(f[4])) * 1e6) +} + +if (length(geno) == 0) stop("no autosomal markers in the .geno file", call. = FALSE) + +# A degenerate genetic map leaves the HMM nothing to smooth along (the .geno +# files carry no cM), so missing positions fall back to physical distance at +# 1 cM per Mb -- crude, monotone, and better than zeros. +for (marker in names(gmap)) { + g <- gmap[[marker]] + if (!is.finite(as.numeric(g["pos"]))) { + p <- pmap[[marker]] + gmap[[marker]]["pos"] <- if (is.finite(as.numeric(p["pos"]))) as.numeric(p["pos"]) / 1e6 else 0 + } +} + +dir.create(out_dir, recursive = TRUE, showWarnings = FALSE) +write.csv( + data.frame(marker = names(geno), chr = vapply(gmap, `[`, "", "chr"), + pos = as.numeric(vapply(gmap, `[`, "", "pos")), row.names = NULL), + file.path(out_dir, "gmap.csv"), row.names = FALSE, quote = FALSE) +write.csv( + data.frame(marker = names(geno), chr = vapply(pmap, `[`, "", "chr"), + pos = as.numeric(vapply(pmap, `[`, "", "pos")), row.names = NULL), + file.path(out_dir, "pmap.csv"), row.names = FALSE, quote = FALSE) +# qtl2's default genotype file has one row per individual and one column per +# marker, with the first column named `id` (see qtl2's own iron example). +geno_df <- data.frame(id = individuals, + do.call(cbind, lapply(geno, function(calls) calls)), + check.names = FALSE, stringsAsFactors = FALSE) +names(geno_df) <- c("id", names(geno)) +write.csv(geno_df, file.path(out_dir, "geno.csv"), row.names = FALSE, quote = FALSE) +# qtl2 wants a phenotype column to be able to read a cross; the trait comes from +# the request, through the scan script's --pheno. +write.csv(data.frame(id = individuals, trait = 1), + file.path(out_dir, "pheno.csv"), row.names = FALSE, quote = FALSE) + +# The JSON metadata is qtl2's own description of a cross: one call, no +# hand-rolled keys, and it stays right when qtl2 changes. +# 1..n over the genotypes the file carries: A/B for a riself cross with the +# heterozygous class dropped, A/H/B when it is kept. +codes <- if (crosstype == "riself" && het == "drop") c(A = 1L, B = 2L) else c(A = 1L, H = 2L, B = 3L) +write_control_file( + file.path(out_dir, "control.json"), + crosstype = crosstype, + geno_file = "geno.csv", gmap_file = "gmap.csv", pmap_file = "pmap.csv", + pheno_file = "pheno.csv", + geno_transposed = FALSE, sep = ",", + na.strings = "", + geno_codes = codes, overwrite = TRUE) + +cat(sprintf("geno2cross: %d markers, %d individuals, %d heterozygous calls %s, crosstype %s -> %s\n", + length(geno), length(individuals), dropped, + if (het == "drop") "dropped" else "kept", crosstype, out_dir)) |
