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 test_cache_ipfs_file_cache_hit(self):
     """Test that the correct file location is returned if there's a cache hit"""
     # Create empty file
     test_dir = "/tmp/QmQPeNsJPyVWPFDVHb77w8G42Fvo15z4bG2X8D2GhfbSXc-test"
     if not os.path.exists(test_dir):
         os.mkdir(test_dir)
     open(f"{test_dir}/genotype.txt", "a").close()
     file_loc = cache_ipfs_file(
         ipfs_file=("/ipfs/"
                    "QmQPeNsJPyVWPFDVHb"
                    "77w8G42Fvo15z4bG2X8D2GhfbSXc-test/"
                    "genotype.txt"),
         cache_dir="/tmp")
     # Clean up
     os.remove(f"{test_dir}/genotype.txt")
     os.rmdir(test_dir)
     self.assertEqual(file_loc, f"{test_dir}/genotype.txt")
Esempio n. 5
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. 6
0
    def test_cache_ipfs_file_cache_miss(self,
                                        mock_ipfs):
        """Test that a file is cached if there's a cache miss"""
        mock_ipfs_client = mock.MagicMock()
        mock_ipfs.connect.return_value = mock_ipfs_client

        test_dir = "/tmp/QmQPeNsJPyVWPFDVHb77w8G42Fvo15z4bG2X8D2GhfbSXc-test"
        self.assertEqual(cache_ipfs_file(
            ipfs_file=("/ipfs/"
                       "QmQPeNsJPyVWPFDVHb"
                       "77w8G42Fvo15z4bG2X8D2GhfbSXc-test/"
                       "genotype.txt"),
            cache_dir="/tmp"
        ), f"{test_dir}/genotype.txt")
        mock_ipfs_client.get.assert_called_once_with(
            ("/ipfs/"
             "QmQPeNsJPyVWPFDVHb"
             "77w8G42Fvo15z4bG2X8D2GhfbSXc-test/"
             "genotype.txt"),
            target=f"{test_dir}"
        )
Esempio n. 7
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!")