#!/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("--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)) 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" # `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=character(0), colClasses='character', comment.char = '#') # Positions are Chr, Locus and whichever map column the file has: 44 of this # deployment's .geno files carry cM only, and assuming "4 metadata columns" # costs their first sample. available <- names(genodata) position_col <- if (scale_col %in% available) scale_col else if ("Mb" %in% available) "Mb" else if ("cM" %in% available) "cM" 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] if (length(df[[1]]) != n_individuals) { 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]]), n_individuals), call.=FALSE) } cross_file = tempfile(pattern="fitqtl-cross-", fileext=".csv") items = list( c(paste0("T_", trait_name), '', '', unlist(df[2])), c('sex', '', '', rep('m', n_individuals)), cbind(genodata[, c('Locus', 'Chr', position_col)], genodata[, first_sample: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) # 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(n_individuals)) 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() 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, 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 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%s\n", out, nrow(result), best$marker, best$chr, best$pos, best$lod, 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 "")) }