about summary refs log tree commit diff
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/qtl_fitqtl.R61
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 ""))
 }