Esempio n. 1
0
def compute_k_gwa_with_loco_and_cavar(chromosomes, maf, token):
    """k-gwa-compute; Loco with covars; lmm defaults to 9!

    """
    working_dir = os.path.join(current_app.config.get("TMPDIR"), token)
    _dict = jsonfile_to_dict(os.path.join(working_dir, "metadata.json"))
    try:
        phenofile, snpsfile, covarfile = [
            os.path.join(working_dir, _dict.get(x))
            for x in ["pheno", "snps", "covar"]
        ]
        genofile = cache_ipfs_file(
            ipfs_file=_dict.get("geno"),
            cache_dir=current_app.config.get('CACHEDIR'))
        if not do_paths_exist([genofile, phenofile, snpsfile]):
            raise FileNotFoundError
        gemma_kwargs = {"g": genofile, "p": phenofile, "a": snpsfile}
        gemma_k_cmd = generate_gemma_cmd(
            gemma_cmd=current_app.config.get("GEMMA_"
                                             "WRAPPER_CMD"),
            output_dir=current_app.config.get('TMPDIR'),
            token=token,
            gemma_kwargs=gemma_kwargs,
            chromosomes=chromosomes)
        gemma_kwargs["c"] = covarfile
        gemma_kwargs["maf"] = float(maf)
        gemma_kwargs["lmm"] = _dict.get("lmm", 9)
        gemma_gwa_cmd = generate_gemma_cmd(
            gemma_cmd=current_app.config.get("GEMMA_"
                                             "WRAPPER_CMD"),
            output_dir=current_app.config.get('TMPDIR'),
            token=token,
            gemma_kwargs=gemma_kwargs,
            gemma_wrapper_kwargs={
                "loco":
                ("--input "
                 f"{os.path.join(working_dir, gemma_k_cmd.get('output_file'))}"
                 )
            })
        return jsonify(unique_id=queue_cmd(
            conn=redis.Redis(),
            email=(request.get_json() or {}).get('email'),
            job_queue=current_app.config.get("REDIS_JOB_QUEUE"),
            cmd=(f"{gemma_k_cmd.get('gemma_cmd')} && "
                 f"{gemma_gwa_cmd.get('gemma_cmd')}")),
                       status="queued",
                       output_file=gemma_gwa_cmd.get("output_file"))
    # pylint: disable=W0703
    except Exception:
        return jsonify(
            status=128,
            # use better message
            message="Metadata file non-existent!")
Esempio n. 2
0
def compute_k_gwa_with_covars_only(token):
    """Given a genofile, traitfile, snpsfile, and the token, compute the k-values
and return <hash-of-inputs>.json with a UNIQUE-ID of the job. The genofile,
traitfile, and snpsfile are extracted from a metadata.json file. No Loco no
covars; lmm defaults to 9!

    """
    working_dir = os.path.join(current_app.config.get("TMPDIR"), token)
    _dict = jsonfile_to_dict(os.path.join(working_dir, "metadata.json"))
    try:
        phenofile, snpsfile, covarfile = [
            os.path.join(working_dir, _dict.get(x))
            for x in ["pheno", "snps", "covar"]
        ]
        genofile = cache_ipfs_file(
            ipfs_file=_dict.get("geno"),
            cache_dir=current_app.config.get('CACHEDIR'))
        if not do_paths_exist([genofile, phenofile, snpsfile]):
            raise FileNotFoundError
        gemma_kwargs = {"g": genofile, "p": phenofile, "a": snpsfile}
        gemma_k_cmd = generate_gemma_cmd(
            gemma_cmd=current_app.config.get("GEMMA_"
                                             "WRAPPER_CMD"),
            output_dir=current_app.config.get('TMPDIR'),
            token=token,
            gemma_kwargs=gemma_kwargs)
        gemma_kwargs["c"] = covarfile
        gemma_kwargs["lmm"] = _dict.get("lmm", 9)
        gemma_gwa_cmd = generate_gemma_cmd(
            gemma_cmd=current_app.config.get("GEMMA_"
                                             "WRAPPER_CMD"),
            output_dir=current_app.config.get('TMPDIR'),
            token=token,
            gemma_kwargs=gemma_kwargs,
            gemma_wrapper_kwargs={
                "input": os.path.join(working_dir,
                                      gemma_k_cmd.get("output_file"))
            })
        return jsonify(unique_id=queue_cmd(
            conn=redis.Redis(),
            email=(request.get_json() or {}).get('email'),
            job_queue=current_app.config.get("REDIS_JOB_QUEUE"),
            cmd=(f"{gemma_k_cmd.get('gemma_cmd')} && "
                 f"{gemma_gwa_cmd.get('gemma_cmd')}")),
                       status="queued",
                       output_file=gemma_gwa_cmd.get("output_file"))
    # pylint: disable=W0703
    except Exception:
        return jsonify(
            status=128,
            # use better message
            message="Metadata file non-existent!")
Esempio n. 3
0
def compute_gwa_with_loco_covar(k_filename, maf, token):
    """Compute GWA values. No Covariates provided. Only loco and maf vals given.

    """
    working_dir = os.path.join(current_app.config.get("TMPDIR"), token)
    _dict = jsonfile_to_dict(os.path.join(working_dir, "metadata.json"))
    try:
        phenofile, snpsfile, covarfile = [
            os.path.join(working_dir, _dict.get(x))
            for x in ["pheno", "snps", "covar"]
        ]
        genofile = cache_ipfs_file(
            ipfs_file=_dict.get("geno"),
            cache_dir=current_app.config.get('CACHEDIR'))
        if not do_paths_exist([genofile, phenofile, snpsfile, covarfile]):
            raise FileNotFoundError
        gemma_kwargs = {
            "g": genofile,
            "p": phenofile,
            "a": snpsfile,
            "c": covarfile,
            "lmm": _dict.get("lmm", 9),
            "maf": float(maf)
        }
        results = generate_gemma_cmd(
            gemma_cmd=current_app.config.get("GEMMA_"
                                             "WRAPPER_CMD"),
            output_dir=current_app.config.get('TMPDIR'),
            token=token,
            gemma_kwargs=gemma_kwargs,
            gemma_wrapper_kwargs={
                "loco": f"--input {os.path.join(working_dir, k_filename)}"
            })
        return jsonify(unique_id=queue_cmd(
            conn=redis.Redis(),
            email=(request.get_json() or {}).get('email'),
            job_queue=current_app.config.get("REDIS_JOB_QUEUE"),
            cmd=results.get("gemma_cmd")),
                       status="queued",
                       output_file=results.get("output_file"))
    # pylint: disable=W0703
    except Exception:
        return jsonify(
            status=128,
            # use better message
            message="Metadata file non-existent!")
Esempio n. 4
0
def compute_gwa(k_filename, token):
    """Compute GWA values. No loco no covariates provided.

    """
    working_dir = os.path.join(current_app.config.get("TMPDIR"), token)
    _dict = jsonfile_to_dict(os.path.join(working_dir, "metadata.json"))
    try:
        phenofile, snpsfile = [
            os.path.join(working_dir, _dict.get(x)) for x in ["pheno", "snps"]
        ]
        genofile = cache_ipfs_file(
            ipfs_file=_dict.get("geno"),
            cache_dir=current_app.config.get('CACHEDIR'))
        gemma_kwargs = {
            "g": genofile,
            "p": phenofile,
            "a": snpsfile,
            "lmm": _dict.get("lmm", 9)
        }
        results = generate_gemma_cmd(
            gemma_cmd=current_app.config.get("GEMMA_"
                                             "WRAPPER_CMD"),
            output_dir=current_app.config.get('TMPDIR'),
            token=token,
            gemma_kwargs=gemma_kwargs,
            gemma_wrapper_kwargs={
                "input": os.path.join(working_dir, k_filename)
            })
        return jsonify(unique_id=queue_cmd(
            conn=redis.Redis(),
            email=(request.get_json() or {}).get('email'),
            job_queue=current_app.config.get("REDIS_JOB_QUEUE"),
            cmd=results.get("gemma_cmd")),
                       status="queued",
                       output_file=results.get("output_file"))
    # pylint: disable=W0703
    except Exception:
        return jsonify(
            status=128,
            # use better message
            message="Metadata file non-existent!")
Esempio n. 5
0
def compute_k_loco(chromosomes, token):
    """Similar to 'compute_k' with the extra option of using loco given chromosome
values.

    """
    working_dir = os.path.join(current_app.config.get("TMPDIR"), token)
    _dict = jsonfile_to_dict(os.path.join(working_dir, "metadata.json"))
    try:
        phenofile, snpsfile = [
            os.path.join(working_dir, _dict.get(x)) for x in ["pheno", "snps"]
        ]
        genofile = cache_ipfs_file(
            ipfs_file=_dict.get("geno"),
            cache_dir=current_app.config.get('CACHEDIR'))
        if not do_paths_exist([genofile, phenofile, snpsfile]):
            raise FileNotFoundError
        gemma_kwargs = {"g": genofile, "p": phenofile, "a": snpsfile}
        results = generate_gemma_cmd(
            gemma_cmd=current_app.config.get("GEMMA_"
                                             "WRAPPER_CMD"),
            output_dir=current_app.config.get('TMPDIR'),
            token=token,
            gemma_kwargs=gemma_kwargs,
            chromosomes=chromosomes)
        return jsonify(unique_id=queue_cmd(
            conn=redis.Redis(),
            email=(request.get_json() or {}).get('email'),
            job_queue=current_app.config.get("REDIS_JOB_QUEUE"),
            cmd=results.get("gemma_cmd")),
                       status="queued",
                       output_file=results.get("output_file"))
    # pylint: disable=W0703
    except Exception:
        return jsonify(
            status=128,
            # use better message
            message="Metadata file non-existent!")
Esempio n. 6
0
 def test_jsonfile_to_dict(self):
     """Test that a json file is parsed correctly""" ""
     json_file = os.path.join(os.path.dirname(__file__), "test_data",
                              "metadata.json")
     self.assertEqual("Longer description",
                      jsonfile_to_dict(json_file).get("description"))