diff options
| author | Pjotr <wrk@bosa.lan> | 2026-11-09 16:04:45 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-11-09 16:04:45 +0000 |
| commit | c642f87fb33a41032c4af5fda38e6434c2251aae (patch) | |
| tree | 7888fbead2d7de28d12f3fecef24d859316d87ee /scripts | |
| parent | 002cdc41a54f6b398933f9e536217ec433a17eac (diff) | |
| download | genenetwork4-c642f87fb33a41032c4af5fda38e6434c2251aae.tar.gz | |
compute: fit covariates in /compute/fitqtl too
`fitqtl` takes a `covar` argument, so the effect it reports can be the one *after* conditioning -- which is usually the number worth quoting for a peak that overlaps a known confounder. `covariates` now works on /compute/fitqtl exactly as on /compute/rqtl: the same `covariates::resolve` (case attribute by name, inline values, explicit map, same coding and same note), so the two endpoints report a covariate identically. The R side does what R/qtl needs: the phenotype file carries one column per covariate after the trait, `--covarstruct` says which are categorical, and the script dummy-expands those (k-1 against the first sorted level, missing values left missing). Two things it has to get right: * `model.matrix()` drops rows with a missing value, which silently misaligns the covariate frame against the cross -- so the dummies are built directly (`ifelse(is.na(x), NA, x == level)`), not through `model.matrix`; * a covariate can be perfectly spread across the panel and **constant among the individuals a trait covers** -- BXD's `Status` is exactly that -- and R/qtl then stops with `Lapack routine dgesv: system is exactly singular`. Columns that do not vary among the phenotyped individuals are left out, and the reply names them, the same rule the GEMMA covariate file applies. Measured on BXD trait 10001 at its peak, `BXD.4`: no covariate LOD 2.9310 additive +2.394 32.8% var with Status LOD 3.3602 additive +2.463 36.6% var with an inline numeric covariate LOD 2.9373 additive +2.361 The LOD is no longer the scan's once a covariate is fitted -- it is a different model -- which the docs now say, next to the no-covariate parity check that still holds. * scripts/qtl_fitqtl.R: `--covarstruct`, dummy expansion, the degenerate column rule, and the dropped names in the summary line. * src/fitqtl.rs: writes the covariates into the phenotype file and the struct file, and passes both. * src/routes/fitqtl.rs: the `covariates` field, resolved after alignment so the coding and the complete-case count match /compute/rqtl. * tests/fitqtl.rs: a covariate run (the note, the fitted row, a changed LOD) and an unknown attribute refused with the list of valid ones. * README.md, docs/www/SKILL.md: covariates on this endpoint, and why its LOD stops matching the scan's. 306 tests pass; the same four as before do not.
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/qtl_fitqtl.R | 61 |
1 files changed, 58 insertions, 3 deletions
diff --git a/scripts/qtl_fitqtl.R b/scripts/qtl_fitqtl.R index 1763d27..8dfc720 100755 --- a/scripts/qtl_fitqtl.R +++ b/scripts/qtl_fitqtl.R @@ -36,6 +36,7 @@ option_list = list( 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("--covarstruct"), type="character", default=NULL, help="file listing covariate names and kinds (categorical/numerical)"), make_option(c("-f", "--filename"), type="character", default="fitqtl.csv", help="output file name") ) opt = parse_args(OptionParser(option_list=option_list)) @@ -88,11 +89,60 @@ cross <- if (identical(type, '4-way')) { if (identical(type, 'riset')) cross <- convert2riself(cross) unlink(cross_file) +# Covariates, if any: the phenotype file carries the trait in column 2 and +# one column per covariate after it, and --covarstruct says which of those +# are categorical. Those are dummy-expanded here (R/qtl's fitqtl wants a +# numeric covar frame), one column per non-reference level, the same coding +# the R/qtl wrapper applies for /compute/rqtl. +covar <- NULL +if (!is.null(opt$covarstruct) && file.exists(opt$covarstruct)) { + struct <- read.table(opt$covarstruct, header=FALSE, sep="\t", stringsAsFactors=FALSE, + col.names=c("name", "kind"), quote="") + covar <- data.frame(row.names=seq_len(ncol(genodata) - 4)) + for (i in seq_len(nrow(struct))) { + column <- df[[2 + i]] # column 2 is the trait; covariates follow + if (identical(struct$kind[i], "categorical")) { + # k-1 dummies against the first level (sorted, as the server codes + # them), with a missing value left missing -- model.matrix() would + # drop those rows and misalign the covariate frame with the cross. + levels_all <- sort(unique(column[!is.na(column)])) + if (length(levels_all) < 2) { + stop(sprintf("covariate '%s' has %d usable level(s)", struct$name[i], length(levels_all)), + call.=FALSE) + } + for (j in 2:length(levels_all)) { + covar[[paste0(struct$name[i], ".", levels_all[j])]] <- + ifelse(is.na(column), NA, as.numeric(column == levels_all[j])) + } + } else { + covar[[struct$name[i]]] <- as.numeric(column) + } + } + # A column that does not vary among the individuals with a phenotype is + # singular in the fit (R/qtl stops with "system is exactly singular"), and + # contributes nothing anyway -- the same rule the GEMMA covariate file + # applies. It matters in practice: a covariate can be perfectly spread + # across the panel and constant in the subset a trait happens to cover. + with_phenotype <- !is.na(df[[2]]) + varies <- vapply(covar, function(column) { + values <- column[with_phenotype] + length(unique(values[!is.na(values)])) > 1 + }, logical(1)) + dropped <<- colnames(covar)[!varies] + if (length(dropped) > 0) { + covar <- covar[, varies, drop=FALSE] + } + if (ncol(covar) == 0) covar <- NULL + cat("covariates:", paste(colnames(covar), collapse=", "), "\n") +} + # 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) +dropped_covariates <- if (exists("dropped")) dropped else character(0) + wanted <- trim(strsplit(opt$markers, ',')[[1]]) wanted <- wanted[nzchar(wanted)] rows <- list() @@ -106,7 +156,8 @@ for (marker in wanted) { 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), + fitqtl(cross, qtl=qtl, pheno.col=1, model=opt$model, method=opt$method, + covar=if (is.null(covar)) NULL else covar, get.ests=TRUE), error = function(e) { warning(sprintf("fitqtl failed at %s: %s", marker, conditionMessage(e)), call.=FALSE); NULL }) if (is.null(fit)) next @@ -139,7 +190,11 @@ if (length(rows) == 0) { 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", + cat(sprintf("wrote %s: %d marker(s) fitted; strongest %s chr%s %.1f Mb LOD %.2f, additive %+.3f (SE %.3f), %.1f%% variance%s\n", out, nrow(result), best$marker, best$chr, best$pos, best$lod, - best$additive, best$se, best$pct_var)) + best$additive, best$se, best$pct_var, + if (length(dropped_covariates) > 0) + paste0("; covariate column(s) left out because they do not vary among the fitted individuals: ", + paste(dropped_covariates, collapse=", ")) + else "")) } |
