From 9a8ab0ea1bbfc82c79cb8183c37200a09ab27d9c Mon Sep 17 00:00:00 2001 From: zsloan Date: Mon, 26 Jul 2021 20:47:37 +0000 Subject: Added pairscan boolean kwarg and process_rqtl_pairscan function for reading in pairscan results + renamed process_rqtl_output to process_rqtl_mapping to distinguish between that and pairscan --- gn3/api/rqtl.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'gn3/api') diff --git a/gn3/api/rqtl.py b/gn3/api/rqtl.py index b5627c5..ed01a97 100644 --- a/gn3/api/rqtl.py +++ b/gn3/api/rqtl.py @@ -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 = ["covarstruct", "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 boolean_kwargs: + rqtl_output['results'] = process_rqtl_pairscan(rqtl_cmd.get('output_file')) + 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')) -- cgit v1.2.3 From 444095309a8841a0998e74f2f4eba2c3fea6890f Mon Sep 17 00:00:00 2001 From: zsloan Date: Thu, 29 Jul 2021 19:46:34 +0000 Subject: Fix imports to import both process_rqtl_mapping and process_rqtl_pairscan in api/rqtl.py --- gn3/api/rqtl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gn3/api') diff --git a/gn3/api/rqtl.py b/gn3/api/rqtl.py index ed01a97..abce705 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__) -- cgit v1.2.3 From 40e85090b60ef002723b614fd712b6affbd68176 Mon Sep 17 00:00:00 2001 From: zsloan Date: Thu, 26 Aug 2021 20:17:14 +0000 Subject: Added genofile name to inputs for processing R/qtl pair-scan results, since it's needed to store the proximal/distal markers for each position --- gn3/api/rqtl.py | 2 +- gn3/computations/rqtl.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'gn3/api') diff --git a/gn3/api/rqtl.py b/gn3/api/rqtl.py index abce705..9aab9fa 100644 --- a/gn3/api/rqtl.py +++ b/gn3/api/rqtl.py @@ -49,7 +49,7 @@ run the rqtl_wrapper script and return the results as JSON os.system(rqtl_cmd.get('rqtl_cmd')) if "pairscan" in boolean_kwargs: - rqtl_output['results'] = process_rqtl_pairscan(rqtl_cmd.get('output_file')) + rqtl_output['results'] = process_rqtl_pairscan(rqtl_cmd.get('output_file'), genofile) else: rqtl_output['results'] = process_rqtl_mapping(rqtl_cmd.get('output_file')) diff --git a/gn3/computations/rqtl.py b/gn3/computations/rqtl.py index 36c9fa2..b05a577 100644 --- a/gn3/computations/rqtl.py +++ b/gn3/computations/rqtl.py @@ -80,7 +80,7 @@ def process_rqtl_mapping(file_name: str) -> List: return marker_obs -def process_rqtl_pairscan(file_name: str) -> List: +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) @@ -88,7 +88,7 @@ def process_rqtl_pairscan(file_name: str) -> List: """ figure_data = pairscan_for_figure(file_name) - table_data = pairscan_for_table(file_name) + table_data = pairscan_for_table(file_name, geno_file) return [figure_data, table_data] @@ -127,7 +127,7 @@ def pairscan_for_figure(file_name: str) -> Dict: return figure_data -def pairscan_for_table(file_name: str) -> List: +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) -- cgit v1.2.3 From 64aedf4202efc4e50648d496d2a8eed3fc828b2f Mon Sep 17 00:00:00 2001 From: zsloan Date: Sat, 28 Aug 2021 00:46:10 +0000 Subject: Fix issue that causes R/qtl to always run pair-scan even if pair-scan isn't selected --- gn3/api/rqtl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gn3/api') diff --git a/gn3/api/rqtl.py b/gn3/api/rqtl.py index 9aab9fa..b6c04eb 100644 --- a/gn3/api/rqtl.py +++ b/gn3/api/rqtl.py @@ -48,7 +48,7 @@ run the rqtl_wrapper script and return the results as JSON "output", rqtl_cmd.get('output_file'))): os.system(rqtl_cmd.get('rqtl_cmd')) - if "pairscan" in boolean_kwargs: + 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')) -- cgit v1.2.3 From 47c38fe08f27a7de58430c2f7f5635a9ba5836c8 Mon Sep 17 00:00:00 2001 From: zsloan Date: Tue, 12 Oct 2021 20:54:47 +0000 Subject: Fixes pylint errors --- gn3/api/rqtl.py | 3 +- gn3/computations/rqtl.py | 84 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 62 insertions(+), 25 deletions(-) (limited to 'gn3/api') diff --git a/gn3/api/rqtl.py b/gn3/api/rqtl.py index b6c04eb..70ebe12 100644 --- a/gn3/api/rqtl.py +++ b/gn3/api/rqtl.py @@ -6,7 +6,8 @@ from flask import current_app from flask import jsonify from flask import request -from gn3.computations.rqtl import generate_rqtl_cmd, process_rqtl_mapping, process_rqtl_pairscan, 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__) diff --git a/gn3/computations/rqtl.py b/gn3/computations/rqtl.py index 4327cac..65ee6de 100644 --- a/gn3/computations/rqtl.py +++ b/gn3/computations/rqtl.py @@ -118,17 +118,18 @@ def pairscan_for_figure(file_name: str) -> Dict: return figure_data +def get_marker_list(map_file: str) -> List: + """ + Open the map file with the list of markers/pseudomarkers and create list of marker obs -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 = [] + PARAMETERS: + map_file: The map file output by R/qtl containing marker/pseudomarker positions + """ - # Open the map file with the list of markers/pseudomarkers and create list of marker obs + marker_list = [] 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:]): + "output", map_file), "r") as map_fh: + for line in map_fh.readlines()[1:]: line_items = [item.rstrip('\n') for item in line.split(",")] this_marker = { 'name': line_items[0], @@ -138,37 +139,72 @@ def pairscan_for_table(file_name: str, geno_file: str) -> List: marker_list.append(this_marker) - # Get the list of original markers from the .geno file - original_markers = build_marker_pos_dict(geno_file) + return marker_list - # Open the file with the actual results and write the results as - # they will be displayed in the results table +def generate_table_rows(results_file: str, marker_list: List, original_markers: Dict) -> List: + """ + Open the file with the actual R/qtl pair-scan results and write them as + they will be displayed in the results table + + PARAMETERS: + results_file: The filename containing R/qtl pair-scan results + marker_list: List of marker/pseudomarker names/positions from results + original_markers: Dict of markers from the .geno file, for finding proximal/distal + markers to each pseudomarker + """ + + table_data = [] with open(os.path.join(current_app.config.get("TMPDIR", "/tmp"), - "output", file_name), "r") as the_file: + "output", results_file), "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) + marker_1['proximal'], marker_1['distal'] = 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) + marker_2['proximal'], marker_2['distal'] = find_nearest_marker(marker_2['chr'], + marker_2['pos'], + original_markers) try: lod_score = f"{float(item):.3f}" - except: + except ValueError: lod_score = f"{item}" - this_line = { - 'proximal1': proximal1, - 'distal1': distal1, + + table_data.append({ + 'proximal1': marker_1['proximal'], + 'distal1': marker_1['distal'], 'pos1': f"Chr {marker_1['chr']} @ {float(marker_1['pos']):.1f} cM", 'lod': lod_score, - 'proximal2': proximal2, - 'distal2': distal2, + 'proximal2': marker_2['proximal'], + 'distal2': marker_2['distal'], 'pos2': f"Chr {marker_2['chr']} @ {float(marker_2['pos']):.1f} cM" - } + }) - table_data.append(this_line) + return table_data + +def pairscan_for_table(file_name: str, geno_file: str) -> List: + """ + Read in R/qtl pair-scan results and return as List representing + table row contents + + PARAMETERS: + file_name: The filename containing R/qtl pair-scan results + geno_file: Filename of the genotype file (used to get marker positions) + """ + + # Open the map file with the list of markers/pseudomarkers and create list of marker obs + marker_list = get_marker_list("MAP_" + file_name) + + # 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 + table_data = generate_table_rows(file_name, marker_list, original_markers) - return sorted(table_data, key = lambda i: float(i['lod']), reverse=True)[:500] + 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 -- cgit v1.2.3