about summary refs log tree commit diff
path: root/scripts
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-11-09 19:58:04 +0000
committerPjotr <wrk@bosa.lan>2026-11-09 19:58:04 +0000
commit832bf0a6f3961b7ad43635d5770939acba55fb7b (patch)
tree77675385a2b41a0a3cc5d5c018b713b0935c0eef /scripts
parent8dfb6fe64aa815dbe836a461458fa12208d77c2a (diff)
downloadgenenetwork4-832bf0a6f3961b7ad43635d5770939acba55fb7b.tar.gz
fix: DOL maps again on both engines, and the four stale tests
DOL (200 individuals, 112 913 markers, one genotype version) failed on
R/qtl with "There are missing chromosome IDs" and on GEMMA with a bare
wrapper bail-out. Two unrelated causes, both in our own code.

**R/qtl: a genotype code collided with a chromosome name.** DOL's header
says `@unk:X` -- X is the code for a missing genotype -- and the wrapper
passed that to `read.csv(na.strings=...)` for the *whole* file, so every
marker on the X chromosome lost its chromosome ID and `read.cross.csv`
refused the map. The unknown code now applies to the genotype columns
only. DOL scans: 112 913 markers in 42 s, where before it never got past
reading the file. Same fix in scripts/qtl_fitqtl.R, which shares the read.

**GEMMA: its leave-one-chromosome-out run cannot fit the Y.** With LOCO the
DOL panel is scanned per chromosome, and the Y run dies inside BLAS --
`Enforce failed for N>0 in src/fastblas.cpp ... fast_cblas_dgemm` -- taking
the whole request with it (a 502 whose detail is the wrapper's banner).
The mitochondrial chromosome has the same shape of problem. `prepare` for
the GEMMA engine now writes the bimbam pair without the Y and M markers
(cached beside the individual subset, line-aligned so the geno and snps
stay in step), and the note says what was left out: "46 Y/M markers left
out (GEMMA's LOCO cannot fit them)". DOL maps: 106 515 rows in 49 s.

**And the four tests that had been failing since before this work**, all
stale expectations rather than product bugs:

* `benchmark_questions` pinned two traits to "page 2" of a 100-per-page
  search when they rank ~190th and ~219th of 246 -- one re-index pushes
  them over. It now walks up to three pages and asserts presence and LOD,
  not the page; and the helper `search()` pages, because a benchmark row
  can name a trait that ranks outside the first hundred of its own query.
* `no_db_mode_degrades_gracefully` asserted exactly one hit for "alcohol"
  from the index alone. The index grew; the behaviour under test is that
  the search still answers, within the limit, with the database-backed
  fields null. That is what it asserts now.
* `search_proxy_scenarios` and `search_parity_replay` never pinned the
  engine they test, so an ambient `SEARCH_ENGINE=native` made the proxy
  scenarios answer from the local index: a 200 where a 422 was expected,
  and "expected 2 upstream calls, got 0" for every replayed query. Both now
  set `SEARCH_ENGINE=forward`.

* src/genotypes.rs: `DEGENERATE_CHROMOSOMES`, `drop_degenerate_chromosomes`,
  `Prepared.markers_dropped`.
* src/routes/gemma.rs: also the 500 that `eb5320d` introduced -- counting
  parents/F1s from `panel - keep` instead of subtracting the exclusion
  *set*, whose length can exceed the panel (subtract overflow).
* scripts/rqtl_wrapper.R, scripts/qtl_fitqtl.R: na.strings scoped to the
  genotype columns.
* tests/{benchmark,no_db,search,search_parity}.rs: as above.

Verified through the API on DOL trait 10003: R/qtl 112 913 rows, GEMMA
106 515 rows. gemma/rqtl/fitqtl suites green (14, 10, 4).
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/qtl_fitqtl.R12
-rw-r--r--scripts/rqtl_wrapper.R17
2 files changed, 27 insertions, 2 deletions
diff --git a/scripts/qtl_fitqtl.R b/scripts/qtl_fitqtl.R
index 4d3b71a..bf7663a 100755
--- a/scripts/qtl_fitqtl.R
+++ b/scripts/qtl_fitqtl.R
@@ -55,8 +55,11 @@ 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"
+# `na.strings` on the whole file is wrong: DOL's `@unk:X` would turn every
+# X chromosome into a missing chromosome (see scripts/rqtl_wrapper.R).
+unk_code <- get_geno_code(header, 'unk')
 genodata <- read.csv(opt$geno, sep='\t', skip=toskip, header=TRUE,
-                     na.strings=get_geno_code(header, 'unk'),
+                     na.strings=character(0),
                      colClasses='character', comment.char = '#')
 
 # Positions are Chr, Locus and whichever map column the file has: 44 of this
@@ -69,6 +72,13 @@ position_col <- if (scale_col %in% available) scale_col else
   stop("the genotype file has neither an Mb nor a cM column", call.=FALSE)
 first_sample <- match(position_col, available) + 1
 n_individuals <- ncol(genodata) - (first_sample - 1)
+if (!is.null(unk_code) && !identical(unk_code, "")) {
+  genotype_columns <- first_sample:ncol(genodata)
+  genodata[genotype_columns] <- lapply(genodata[genotype_columns], function(column) {
+    column[trim(column) == unk_code] <- NA
+    column
+  })
+}
 
 df <- read.table(opt$pheno, na.strings = "x", header=TRUE, check.names=FALSE)
 trait_name <- colnames(df)[2]
diff --git a/scripts/rqtl_wrapper.R b/scripts/rqtl_wrapper.R
index 490525d..3c5d7d9 100644
--- a/scripts/rqtl_wrapper.R
+++ b/scripts/rqtl_wrapper.R
@@ -91,8 +91,14 @@ geno_to_csvr <- function(genotypes, trait_names, trait_vals, out, type, sex = NU
     genocodes <- c(get_geno_code(header, 'mat'), get_geno_code(header, 'het'),
                    get_geno_code(header, 'pat'))
   }
+  # `na.strings` must apply to the *genotype* columns only. DOL's header
+  # says `@unk:X`, so applying it to the whole file turned every chromosome
+  # labelled X into a missing chromosome and the scan died with "There are
+  # missing chromosome IDs" -- a message about the map, for a genotype-code
+  # collision.
+  unk_code <- get_geno_code(header, 'unk')
   genodata <- read.csv(genotypes, sep='\t', skip=toskip, header=TRUE,
-                       na.strings=get_geno_code(header,'unk'),
+                       na.strings=character(0),
                        colClasses='character', comment.char = '#')
 
   verbose_print('Genodata:', toskip, " ", dim(genodata), genocodes, '\n')
@@ -119,6 +125,15 @@ geno_to_csvr <- function(genotypes, trait_names, trait_vals, out, type, sex = NU
   }
   first_sample <- match(position_col, available) + 1
   individuals <- ncol(genodata) - (first_sample - 1)
+  # The unknown code means "no genotype here", so it is NA in the genotype
+  # columns and nowhere else.
+  if (!is.null(unk_code) && !identical(unk_code, "")) {
+    genotype_columns <- first_sample:ncol(genodata)
+    genodata[genotype_columns] <- lapply(genodata[genotype_columns], function(column) {
+      column[trim(column) == unk_code] <- NA
+      column
+    })
+  }
 
   cross_items = list()