diff options
| author | Alexander Kabui | 2024-12-04 09:21:52 +0300 |
|---|---|---|
| committer | GitHub | 2024-12-04 09:21:52 +0300 |
| commit | 5c6d3befdea924eb9f6df3d2d4199a5dd8be9522 (patch) | |
| tree | b24aa3e4058bffe23f208b883cb05c15f1a49832 /gn3/api/rqtl2.py | |
| parent | fb02b03a32a20572ea0057248fcfbc2a9ac21e6b (diff) | |
| parent | 09b6934461cc14d9c957baa2a51b031ea4c5c7bc (diff) | |
| download | genenetwork3-5c6d3befdea924eb9f6df3d2d4199a5dd8be9522.tar.gz | |
Merge pull request #203 from genenetwork/feature/rqtl2-endpoints-with-files
Feature/rqtl2 endpoints with files
Diffstat (limited to 'gn3/api/rqtl2.py')
| -rw-r--r-- | gn3/api/rqtl2.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/gn3/api/rqtl2.py b/gn3/api/rqtl2.py new file mode 100644 index 0000000..ffa8755 --- /dev/null +++ b/gn3/api/rqtl2.py @@ -0,0 +1,61 @@ +""" File contains endpoints for rqlt2""" + +import subprocess +import uuid +import os +from flask import current_app +from flask import jsonify +from flask import Blueprint +from flask import request + +rqtl2 = Blueprint("rqtl2", __name__) + +@rqtl2.route("/compute", methods=["GET"]) +def compute(): + """Endpoint for computing QTL analysis using R/QTL2""" + # get the run id to act as file identifier default to output + run_id = request.args.get("id", "output") + output_file = os.path.join(current_app.config.get("TMPDIR"), + f"{run_id}.txt") + # this should be computed locally not via files + rscript_cmd = ( + f"Rscript ./scripts/rqtl2_wrapper.R " + f"-i /home/kabui/r_playground/meta_grav.json " + f"-d /home/kabui/r_playground " + f"-o /home/kabui/r_playground/rqtl_output.json " + f"--nperm 100 --threshold 1 --cores 0" + ) + process = subprocess.Popen( + rscript_cmd, shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT + ) + for line in iter(process.stdout.readline, b""): + # these allow endpoint stream to read the file since + # no read and write file same tiem + with open(output_file, "a+") as file_handler: + file_handler.write(line.decode("utf-8")) + process.stdout.close() + process.wait() + if process.returncode == 0: + return jsonify({"msg": "success", + "results": "file_here", + "run_id": run_id}) + return jsonify({"msg": "fail", + "error": "Process failed", + "run_id": run_id}) + + +@rqtl2.route("/stream/<identifier>", methods=["GET"]) +def stream(identifier="output"): + """ This endpoints streams stdout from a file expects + the indetifier to be the file """ + output_file = os.path.join(current_app.config.get("TMPDIR"), + f"{identifier}.txt") + seek_position = int(request.args.get("peak", 0)) + with open(output_file) as file_handler: + # read to the last position default to 0 + file_handler.seek(seek_position) + return jsonify({"data": file_handler.readlines(), + "run_id": identifier, + "pointer": file_handler.tell()}) |
