aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gn3/api/rqtl.py10
-rw-r--r--gn3/computations/rqtl.py151
-rw-r--r--scripts/rqtl_wrapper.R51
3 files changed, 184 insertions, 28 deletions
diff --git a/gn3/api/rqtl.py b/gn3/api/rqtl.py
index ebb746c..6548e1a 100644
--- a/gn3/api/rqtl.py
+++ b/gn3/api/rqtl.py
@@ -6,7 +6,7 @@ from flask import current_app
from flask import jsonify
from flask import request
-from gn3.computations.rqtl import generate_rqtl_cmd, process_rqtl_output, process_perm_output
+from gn3.computations.rqtl import generate_rqtl_cmd, process_rqtl_mapping, process_rqtl_pairscan, process_perm_output
from gn3.computations.gemma import do_paths_exist
rqtl = Blueprint("rqtl", __name__)
@@ -25,7 +25,7 @@ run the rqtl_wrapper script and return the results as JSON
# Split kwargs by those with values and boolean ones that just convert to True/False
kwargs = ["model", "method", "nperm", "scale", "control_marker"]
- boolean_kwargs = ["addcovar", "interval", "pstrata"]
+ boolean_kwargs = ["addcovar", "interval", "pstrata", "pairscan"]
all_kwargs = kwargs + boolean_kwargs
rqtl_kwargs = {"geno": genofile, "pheno": phenofile}
@@ -48,9 +48,11 @@ run the rqtl_wrapper script and return the results as JSON
"output", rqtl_cmd.get('output_file'))):
os.system(rqtl_cmd.get('rqtl_cmd'))
- rqtl_output['results'] = process_rqtl_output(rqtl_cmd.get('output_file'))
+ if "pairscan" in rqtl_bool_kwargs:
+ rqtl_output['results'] = process_rqtl_pairscan(rqtl_cmd.get('output_file'), genofile)
+ else:
+ rqtl_output['results'] = process_rqtl_mapping(rqtl_cmd.get('output_file'))
- rqtl_output['results'] = process_rqtl_output(rqtl_cmd.get('output_file'))
if int(rqtl_kwargs['nperm']) > 0:
rqtl_output['perm_results'], rqtl_output['suggestive'], rqtl_output['significant'] = \
process_perm_output(rqtl_cmd.get('output_file'))
diff --git a/gn3/computations/rqtl.py b/gn3/computations/rqtl.py
index 0433b3f..45232e3 100644
--- a/gn3/computations/rqtl.py
+++ b/gn3/computations/rqtl.py
@@ -1,6 +1,7 @@
"""Procedures related rqtl computations"""
import os
-from typing import Dict, List, Union
+from bisect import bisect
+from typing import Dict, List, Tuple, Union
import numpy as np
@@ -15,9 +16,7 @@ def generate_rqtl_cmd(rqtl_wrapper_cmd: str,
rqtl_wrapper_bool_kwargs: list) -> Dict:
"""Given the base rqtl_wrapper command and
dict of keyword arguments, return the full rqtl_wrapper command and an
-output filename generated from a hash of the genotype and phenotype files
-
- """
+output filename generated from a hash of the genotype and phenotype files"""
# Generate a hash from contents of the genotype and phenotype files
_hash = get_hash_of_files(
@@ -47,11 +46,9 @@ output filename generated from a hash of the genotype and phenotype files
}
-def process_rqtl_output(file_name: str) -> List:
+def process_rqtl_mapping(file_name: str) -> List:
"""Given an output file name, read in R/qtl results and return
- a List of marker objects
-
- """
+ a List of marker objects"""
marker_obs = []
# Later I should probably redo this using csv.read to avoid the
# awkwardness with removing quotes with [1:-1]
@@ -80,12 +77,144 @@ def process_rqtl_output(file_name: str) -> List:
return marker_obs
+def process_rqtl_pairscan(file_name: str, geno_file: str) -> List:
+ """Given an output file name, read in R/qtl pair-scan results and return
+a list of both the JSON needed for the d3panels figure and a list of results
+to be used when generating the results table (which will include marker names)"""
+ figure_data = pairscan_for_figure(file_name)
+ table_data = pairscan_for_table(file_name, geno_file)
+
+ return [figure_data, table_data]
+
+def pairscan_for_figure(file_name: str) -> Dict:
+ """Given an output file name, read in R/qtl pair-scan results and return
+ the JSON needed for the d3panels figure"""
+ figure_data = {}
+
+ # Open the file with the actual results, written as a list of lists
+ with open(os.path.join(current_app.config.get("TMPDIR", "/tmp"),
+ "output", file_name), "r") as the_file:
+ lod_results = []
+ for i, line in enumerate(the_file):
+ if i == 0: # Skip first line
+ continue
+ line_items = [item.rstrip('\n') for item in line.split(",")]
+ lod_results.append(line_items[1:]) # Append all but first item in line
+ figure_data['lod'] = lod_results
-def process_perm_output(file_name: str):
+ # Open the map file with the list of markers/pseudomarkers and their positions
+ with open(os.path.join(current_app.config.get("TMPDIR", "/tmp"),
+ "output", "MAP_" + file_name), "r") as the_file:
+ chr_list = []
+ pos_list = []
+ for i, line in enumerate(the_file):
+ if i == 0: # Skip first line
+ continue
+ line_items = [item.rstrip('\n') for item in line.split(",")]
+ chr_list.append(line_items[1][1:-1])
+ pos_list.append(line_items[2])
+ figure_data['chr'] = chr_list
+ figure_data['pos'] = pos_list
+
+ return figure_data
+
+def pairscan_for_table(file_name: str, geno_file: str) -> List:
+ """Given an output file name, read in R/qtl pair-scan results and return
+ a list of results to be used when generating the results table (which will include marker names)"""
+ table_data = []
+
+ # Open the map file with the list of markers/pseudomarkers and create list of marker obs
+ with open(os.path.join(current_app.config.get("TMPDIR", "/tmp"),
+ "output", "MAP_" + file_name), "r") as the_file:
+ marker_list = []
+ for i, line in enumerate(the_file.readlines()[1:]):
+ line_items = [item.rstrip('\n') for item in line.split(",")]
+ this_marker = {
+ 'name': line_items[0],
+ 'chr': line_items[1][1:-1], # Strip quotes from beginning and end of chr string
+ 'pos': line_items[2]
+ }
+
+ marker_list.append(this_marker)
+
+ # Get the list of original markers from the .geno file
+ original_markers = build_marker_pos_dict(geno_file)
+
+ # Open the file with the actual results and write the results as
+ # they will be displayed in the results table
+ with open(os.path.join(current_app.config.get("TMPDIR", "/tmp"),
+ "output", file_name), "r") as the_file:
+ for i, line in enumerate(the_file.readlines()[1:]):
+ marker_1 = marker_list[i]
+ proximal1, distal1 = find_nearest_marker(marker_1['chr'], marker_1['pos'], original_markers)
+ line_items = [item.rstrip('\n') for item in line.split(",")]
+ for j, item in enumerate(line_items[1:]):
+ marker_2 = marker_list[j]
+ proximal2, distal2 = find_nearest_marker(marker_2['chr'], marker_2['pos'], original_markers)
+ try:
+ lod_score = f"{float(item):.3f}"
+ except:
+ lod_score = f"{item}"
+ this_line = {
+ 'proximal1': proximal1,
+ 'distal1': distal1,
+ 'pos1': f"Chr {marker_1['chr']} @ {float(marker_1['pos']):.1f} cM",
+ 'lod': lod_score,
+ 'proximal2': proximal2,
+ 'distal2': distal2,
+ 'pos2': f"Chr {marker_2['chr']} @ {float(marker_2['pos']):.1f} cM"
+ }
+
+ table_data.append(this_line)
+
+ return sorted(table_data, key = lambda i: float(i['lod']), reverse=True)[:500]
+
+def build_marker_pos_dict(genotype_file: str) -> Dict:
+ """Gets list of markers and their positions from .geno file
+
+ Basically a pared-down version of parse_genotype_file for R/qtl pair-scan"""
+
+ with open(genotype_file, "r") as infile:
+ contents = infile.readlines()
+
+ # Get all lines after the metadata
+ lines = tuple(line for line in contents if
+ ((not line.strip().startswith("#")) and
+ (not line.strip().startswith("@")) and
+ (not line.strip() == "")))
+
+ header_items = lines[0].split("\t")
+ mb_exists = "Mb" in header_items
+ pos_column = header_items.index("Mb") if mb_exists else header_items.index("cM")
+
+ the_markers = {}
+ for line in lines[1:]: # The lines with markers
+ line_items = line.split("\t")
+ this_chr = line_items[0]
+ if this_chr not in the_markers:
+ the_markers[this_chr] = {}
+ the_markers[this_chr][str(float(line_items[pos_column]))] = line_items[1]
+
+ return the_markers
+
+def find_nearest_marker(the_chr: str, the_pos: str, marker_list: Dict) -> Tuple[str, str]:
+ """Given a chromosome and position of a pseudomarker (from R/qtl pair-scan results),
+ return the nearest real marker"""
+
+ pos_list = [float(pos) for pos in marker_list[the_chr]]
+
+ # Get the position of the pseudomarker in the list of markers for the chr
+ the_pos_index = bisect(pos_list, float(the_pos))
+
+ proximal_marker = marker_list[the_chr][str(pos_list[the_pos_index-1])]
+ distal_marker = marker_list[the_chr][str(pos_list[the_pos_index])]
+
+ return proximal_marker, distal_marker
+
+def process_perm_output(file_name: str) -> Tuple[List, float, float]:
"""Given base filename, read in R/qtl permutation output and calculate
- suggestive and significant thresholds
+ suggestive and significant thresholds"""
- """
perm_results = []
with open(os.path.join(current_app.config.get("TMPDIR", "/tmp"),
"output", "PERM_" + file_name), "r") as the_file:
diff --git a/scripts/rqtl_wrapper.R b/scripts/rqtl_wrapper.R
index 7518175..ffff5b9 100644
--- a/scripts/rqtl_wrapper.R
+++ b/scripts/rqtl_wrapper.R
@@ -11,6 +11,7 @@ option_list = list(
make_option(c("-c", "--addcovar"), action="store_true", default=NULL, help="Use covariates (included as extra columns in the phenotype input file)"),
make_option(c("--model"), type="character", default="normal", help="Mapping Model - Normal or Non-Parametric"),
make_option(c("--method"), type="character", default="hk", help="Mapping Method - hk (Haley Knott), ehk (Extended Haley Knott), mr (Marker Regression), em (Expectation-Maximization), imp (Imputation)"),
+ make_option(c("--pairscan"), action="store_true", default=NULL, help="Run Pair Scan - the R/qtl function scantwo"),
make_option(c("-i", "--interval"), action="store_true", default=NULL, help="Use interval mapping"),
make_option(c("--nperm"), type="integer", default=0, help="Number of permutations"),
make_option(c("--pstrata"), action="store_true", default=NULL, help="Use permutation strata (stored as final column/vector in phenotype input file)"),
@@ -155,6 +156,9 @@ cross_object = geno_to_csvr(geno_file, trait_names, trait_vals, cross_file)
if (!is.null(opt$interval)) {
verbose_print('Calculating genotype probabilities with interval mapping\n')
cross_object <- calc.genoprob(cross_object, step=5, stepwidth="max")
+} else if (!is.null(opt$pairscan)) {
+ verbose_print('Calculating genotype probabilities for pair-scan\n')
+ cross_object <- calc.genoprob(cross_object, step=10)
} else {
verbose_print('Calculating genotype probabilities\n')
cross_object <- calc.genoprob(cross_object)
@@ -163,6 +167,7 @@ if (!is.null(opt$interval)) {
# Pull covariates out of cross object, if they exist
covars = vector(mode = "list", length = length(trait_names) - 1)
if (!is.null(opt$addcovar)) {
+ verbose_print('Pulling covariates out of cross object\n')
#If perm strata are being used, it'll be included as the final column in the phenotype file
if (!is.null(opt$pstrata)) {
covar_names = trait_names[3:length(trait_names) - 1]
@@ -175,16 +180,28 @@ if (!is.null(opt$addcovar)) {
# Pull permutation strata out of cross object, if it is being used
perm_strata = vector()
if (!is.null(opt$pstrata)) {
+ verbose_print('Pulling permutation strata out of cross object\n')
strata_col = trait_names[length(trait_names)]
perm_strata <- pull.pheno(cross_object, strata_col)
}
# If a marker name is supplied as covariate, get its vector of values and add them as a covariate
if (!is.null(opt$control)) {
+ verbose_print('Creating marker covariates and binding them to covariates vector\n')
marker_covars = create_marker_covars(cross_object, opt$control)
covars <- cbind(covars, marker_covars)
}
+if (!is.null(opt$pairscan)) {
+ scan_func <- function(...){
+ scantwo(...)
+ }
+} else {
+ scan_func <- function(...){
+ scanone(...)
+ }
+}
+
# Calculate permutations
if (opt$nperm > 0) {
if (!is.null(opt$filename)){
@@ -195,19 +212,19 @@ if (opt$nperm > 0) {
if (!is.null(opt$addcovar) || !is.null(opt$control)){
if (!is.null(opt$pstrata)) {
- verbose_print('Running ', opt$nperm, ' permutations with cofactors and strata\n')
- perm_results = scanone(cross_object, pheno.col=1, addcovar=covars, n.perm=opt$nperm, perm.strata=perm_strata, model=opt$model, method=opt$method)
+ verbose_print('Running permutations with cofactors and strata\n')
+ perm_results = scan_func(cross_object, pheno.col=1, addcovar=covars, n.perm=opt$nperm, perm.strata=perm_strata, model=opt$model, method=opt$method)
} else {
- verbose_print('Running ', opt$nperm, ' permutations with cofactors\n')
- perm_results = scanone(cross_object, pheno.col=1, addcovar=covars, n.perm=opt$nperm, model=opt$model, method=opt$method)
+ verbose_print('Running permutations with cofactors\n')
+ perm_results = scan_func(cross_object, pheno.col=1, addcovar=covars, n.perm=opt$nperm, model=opt$model, method=opt$method)
}
} else {
if (!is.null(opt$pstrata)) {
- verbose_print('Running ', opt$nperm, ' permutations with strata\n')
- perm_results = scanone(cross_object, pheno.col=1, n.perm=opt$nperm, perm.strata=perm_strata, model=opt$model, method=opt$method)
+ verbose_print('Running permutations with strata\n')
+ perm_results = scan_func(cross_object, pheno.col=1, n.perm=opt$nperm, perm.strata=perm_strata, model=opt$model, method=opt$method)
} else {
- verbose_print('Running ', opt$nperm, ' permutations\n')
- perm_results = scanone(cross_object, pheno.col=1, n.perm=opt$nperm, model=opt$model, method=opt$method)
+ verbose_print('Running permutations\n')
+ perm_results = scan_func(cross_object, pheno.col=1, n.perm=opt$nperm, model=opt$model, method=opt$method)
}
}
write.csv(perm_results, perm_out_file)
@@ -220,10 +237,18 @@ if (!is.null(opt$filename)){
}
if (!is.null(opt$addcovar) || !is.null(opt$control)){
- verbose_print('Running scanone with cofactors\n')
- qtl_results = scanone(cross_object, pheno.col=1, addcovar=covars, model=opt$model, method=opt$method)
+ verbose_print('Running scan with cofactors\n')
+ qtl_results = scan_func(cross_object, pheno.col=1, addcovar=covars, model=opt$model, method=opt$method)
+} else {
+ verbose_print('Running scan\n')
+ qtl_results = scan_func(cross_object, pheno.col=1, model=opt$model, method=opt$method)
+}
+
+verbose_print('Writing results to CSV file\n')
+if (!is.null(opt$pairscan)) {
+ map_out_file = file.path(opt$outdir, paste("MAP_", opt$filename, sep = "" ))
+ write.csv(qtl_results[1], out_file)
+ write.csv(qtl_results[2], map_out_file)
} else {
- verbose_print('Running scanone\n')
- qtl_results = scanone(cross_object, pheno.col=1, model=opt$model, method=opt$method)
+ write.csv(qtl_results, out_file)
}
-write.csv(qtl_results, out_file)