diff options
| author | Pjotr <wrk@bosa.lan> | 2026-11-09 14:31:27 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-11-09 14:31:27 +0000 |
| commit | 002cdc41a54f6b398933f9e536217ec433a17eac (patch) | |
| tree | e6cff1ba06cf1ec25348503c80961a1173aa70ed /scripts | |
| parent | a698c48ee204cd68efc479b01132ae9c037213bb (diff) | |
| download | genenetwork4-002cdc41a54f6b398933f9e536217ec433a17eac.tar.gz | |
compute: add /compute/fitqtl for effect sizes at named markers
`/compute/rqtl` answers "where" -- scanone gives a LOD per marker -- but not "how much": a scan statistic is not a fitted model. R/qtl's `fitqtl` fits one, and reports the QTL's effect, its standard error, the model LOD and the variance explained. That is what a table of effects needs, and it is a separate endpoint because it is a separate question, asked about a handful of markers (a scan's peaks) rather than a genome: `fitqtl` fits one model per marker, so a genome-wide version would be thousands of fits. Cap: 100 markers per request. Rows are positional: marker, chr, pos, lod, pct_var, additive, se, t, p `additive` is fitqtl's estimate for the QTL term -- R/qtl codes the QTL genotype +-1, so it is half the difference between the two homozygote classes, the same convention GeneNetwork's published `additive` uses. **Two independent checks that the numbers are right.** On BXD trait 10001 (`BXD.4`), at the scan's peak: * `lod` = 2.930985, and `/compute/rqtl`'s scanone gives 2.930985 at the same marker -- identical to every digit the CSV carries, because they are the same cross and the same phenotype. That parity is now an integration test; * `additive` = 2.394472, against the value genenetwork.org publishes for that trait (`/api/v_pre1/trait/BXD/10001` -> 2.394444), which is 28 ppm away -- and that published number came from qtlreaper, a different engine. * scripts/qtl_fitqtl.R (new): reads the same `.geno` and two-column phenotype file as `rqtl_wrapper.R` (which is a verbatim copy of GN3's script and so not extended), builds the cross the same way, fits `fitqtl` at each named marker with `get.ests=TRUE`, and writes one CSV row per marker -- plus a one-line summary naming the strongest fit. `calc.genoprob(step=0)`: marker-level probabilities are all a marker-level fit needs. * src/fitqtl.rs (new): runs it, parses the CSV into positional rows (`chr` stays text, it can be `X`), keeps the command line for `notes`. * src/routes/fitqtl.rs (new): the endpoint -- same alignment rules as `/compute/rqtl` (by name onto the panel, parents/F1s excluded unless asked), the same `normalize`/`winsor_*` pipeline (effects come out on that scale), and the same R/qtl gate, so a fitqtl request and a scan queue against one another rather than competing for cores. * src/config.rs: `FITQTL_SCRIPT_PATH` (default `scripts/qtl_fitqtl.R`). * tests/fitqtl.rs: the fixture (columns, sign of the effect, notes), the LOD parity against scanone, and the refusals (no markers, both genotype inputs). * README.md, docs/www/SKILL.md: the endpoint, what each column means, and when to reach for it instead of the scan. 305 tests pass; the same four as before do not.
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/qtl_fitqtl.R | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/scripts/qtl_fitqtl.R b/scripts/qtl_fitqtl.R new file mode 100755 index 0000000..1763d27 --- /dev/null +++ b/scripts/qtl_fitqtl.R @@ -0,0 +1,145 @@ +#!/usr/bin/env Rscript +# Effect sizes for named markers, via R/qtl's fitqtl. +# +# The scan (/compute/rqtl) gives a LOD per marker but no effect: scanone +# reports a statistic, not a fitted model. fitqtl fits one -- at a marker +# or a QTL position -- and reports the QTL's effect, its standard error, +# the model LOD and the variance explained, which is what a table of +# effects needs. +# +# Reads the same GeneNetwork .geno file and the same two-column phenotype +# file (Sample + trait, "x" for missing) as rqtl_wrapper.R, builds the +# cross the same way, and writes one CSV row per requested marker: +# +# marker,chr,pos,lod,pct_var,additive,se,t,p +# +# `additive` is fitqtl's estimate for the QTL term. In R/qtl's coding the +# QTL genotype is +-1 for a riself cross, so this is half the difference +# between the two homozygote classes -- the same convention GeneNetwork's +# `additive` uses, and the number to quote beside a peak. +# +# The geno/pheno reading below is the same approach as rqtl_wrapper.R +# (GeneNetwork3's script, copied verbatim in this repository): that file is +# a verbatim copy and is not extended here, so the helpers it needs are +# repeated rather than sourced (sourcing it would run its option parser). + +suppressPackageStartupMessages({ + library(optparse) + library(qtl) +}) + +option_list = list( + make_option(c("-g", "--geno"), type="character", help=".geno file with the dataset's genotypes"), + make_option(c("-p", "--pheno"), type="character", help="two-column phenotype file: Sample, trait"), + make_option(c("-m", "--markers"), type="character", help="comma-separated marker names to fit"), + make_option(c("--model"), type="character", default="normal", help="normal or np"), + make_option(c("--method"), type="character", default="hk", help="hk, ehk, mr, em, imp"), + make_option(c("-s", "--scale"), type="character", default="mb", help="mb or cm"), + make_option(c("-o", "--outdir"), type="character", default=tempdir(), help="output directory"), + make_option(c("-f", "--filename"), type="character", default="fitqtl.csv", help="output file name") +) +opt = parse_args(OptionParser(option_list=option_list)) +if (is.null(opt$geno) || is.null(opt$pheno) || is.null(opt$markers)) { + stop("--geno, --pheno and --markers are required", call.=FALSE) +} + +trim <- function(x) gsub("(^[[:space:]]+|[[:space:]]+$)", "", x) +get_geno_code <- function(header, name) { + mat = which(vapply(header, function(x) length(grep(paste0('@', name), x)) == 1, logical(1))) + if (length(mat) == 0) return(NULL) + trim(strsplit(header[mat], ':')[[1]][2]) +} + +header = readLines(opt$geno, 40) +toskip = which(vapply(header, function(x) length(grep("Chr\t", x)) == 1, logical(1))) - 1 +type = get_geno_code(header, 'type') +scale_col = if (toupper(opt$scale) == "CM") "cM" else "Mb" +genodata <- read.csv(opt$geno, sep='\t', skip=toskip, header=TRUE, + na.strings=get_geno_code(header, 'unk'), + colClasses='character', comment.char = '#') + +df <- read.table(opt$pheno, na.strings = "x", header=TRUE, check.names=FALSE) +trait_name <- colnames(df)[2] +if (length(df[[1]]) != ncol(genodata) - 4) { + stop(sprintf(paste("The phenotype file has %d samples but the genotype file has %d", + "individuals. Values are matched to individuals by position, so the", + "two must be one for one, in the genotype file's column order."), + length(df[[1]]), ncol(genodata) - 4), call.=FALSE) +} + +cross_file = tempfile(pattern="fitqtl-cross-", fileext=".csv") +items = list( + c(paste0("T_", trait_name), '', '', unlist(df[2])), + c('sex', '', '', rep('m', ncol(genodata) - 4)), + cbind(genodata[, c('Locus', 'Chr', scale_col)], genodata[, 5:ncol(genodata)]) +) +write.table(do.call(rbind, items), file=cross_file, row.names=FALSE, + col.names=FALSE, quote=FALSE, sep=',') + +genocodes <- if (identical(type, '4-way')) NULL else + c(get_geno_code(header, 'mat'), get_geno_code(header, 'het'), get_geno_code(header, 'pat')) +cross <- if (identical(type, '4-way')) { + read.cross(file=cross_file, 'csvr', genotypes=NULL, crosstype="4way") +} else if (identical(type, 'f2')) { + read.cross(file=cross_file, 'csvr', genotypes=genocodes, crosstype="f2") +} else { + read.cross(file=cross_file, 'csvr', genotypes=genocodes) +} +if (identical(type, 'riset')) cross <- convert2riself(cross) +unlink(cross_file) + +# step=0: probabilities at the markers themselves, which is all fitqtl +# needs for a marker-level fit (and much cheaper than a dense grid). +cross <- calc.genoprob(cross, step=0) +map <- pull.map(cross) + +wanted <- trim(strsplit(opt$markers, ',')[[1]]) +wanted <- wanted[nzchar(wanted)] +rows <- list() +for (marker in wanted) { + hit <- which(vapply(map, function(m) marker %in% names(m), logical(1))) + if (length(hit) == 0) { + warning(sprintf("marker '%s' is not in this genotype file", marker), call.=FALSE) + next + } + chr <- hit[1] + pos <- as.numeric(map[[chr]][[marker]]) + qtl <- makeqtl(cross, chr=chr, pos=pos, what="prob") + fit <- tryCatch( + fitqtl(cross, qtl=qtl, pheno.col=1, model=opt$model, method=opt$method, get.ests=TRUE), + error = function(e) { warning(sprintf("fitqtl failed at %s: %s", marker, conditionMessage(e)), + call.=FALSE); NULL }) + if (is.null(fit)) next + full <- fit$result.full + pvalue <- if ("Pvalue(F)" %in% colnames(full)) full["Model", "Pvalue(F)"] else + if ("Pvalue(Chi2)" %in% colnames(full)) full["Model", "Pvalue(Chi2)"] else NA + additive <- NA; se <- NA; tstat <- NA + if (!is.null(fit$ests)) { + s <- summary(fit)$ests + # Row 1 is the intercept; the QTL term is the last row. + if (nrow(s) >= 2) { + term <- s[nrow(s), ] + additive <- as.numeric(term["est"]) + se <- as.numeric(term["SE"]) + tstat <- as.numeric(term["t"]) + } + } + rows[[length(rows) + 1]] <- data.frame( + marker = marker, chr = chr, pos = pos, lod = fit$lod, + pct_var = as.numeric(full["Model", "%var"]), + additive = additive, se = se, t = tstat, p = as.numeric(pvalue), + stringsAsFactors = FALSE) +} + +out <- file.path(opt$outdir, opt$filename) +if (length(rows) == 0) { + writeLines("marker,chr,pos,lod,pct_var,additive,se,t,p", out) + cat("wrote", out, ": no marker could be fitted\n") +} else { + result <- do.call(rbind, rows) + write.csv(result, out, row.names=FALSE, quote=FALSE) + best <- result[which.max(result$lod), ] + cat(sprintf("wrote %s: %d marker(s) fitted; strongest %s chr%s %.1f Mb LOD %.2f, additive %+.3f (SE %.3f), %.1f%% variance\n", + out, nrow(result), best$marker, best$chr, best$pos, best$lod, + best$additive, best$se, best$pct_var)) +} |
