Example #1
0
def create_experiment(
    bam,
    bedfile="/a/fake/bedfile",
    assembly="GRCh37",
    raw_data=None,
    method="TD",
    species="HUMAN",
    category=None,
    technique_name=None,
    sample=None,
):
    """Easily create an experiment for testing purposes."""
    # create technique
    technique = factories.TechniqueFactory(method=method,
                                           name=technique_name
                                           or str(uuid.uuid4()))

    # create experiment
    bed_file_dict = dict(url=bedfile, description="")
    technique["reference_data"][f"{assembly}_targets_bedfile"] = bed_file_dict
    technique["reference_data"][f"{assembly}_baits_bedfile"] = bed_file_dict
    experiment = factories.ExperimentFactory(technique=technique)

    # force sample if provided
    if sample:  # pragma: no cover
        experiment["sample"] = sample
    else:
        category = category or experiment["sample"]["category"]
        experiment["sample"]["individual"]["species"] = species
        experiment["sample"]["category"] = category

    experiment["raw_data"] = raw_data or []
    experiment["bam_files"][assembly] = dict(url=bam, analysis=1)
    return api.create_instance("experiments", **experiment)
def test_get_bed():
    runner = CliRunner()
    technique = api.create_instance("techniques",
                                    **factories.TechniqueFactory())
    args = [str(technique.pk)]
    result = runner.invoke(commands.get_bed, args, catch_exceptions=False)
    assert "No BED files" in result.output

    api.patch_instance(
        "techniques",
        technique.pk,
        reference_data={"test_targets_bedfile": {
            "url": "/hello/world"
        }},
    )

    result = runner.invoke(commands.get_bed, args, catch_exceptions=False)
    assert "/hello/world" in result.output

    api.patch_instance(
        "techniques",
        technique.pk,
        reference_data={
            "test_targets_bedfile": {
                "url": "/hello/world"
            },
            "another_targets_bedfile": {
                "url": "/hello/world"
            },
        },
    )

    result = runner.invoke(commands.get_bed, args, catch_exceptions=False)
    assert "Multiple BEDs" in result.output
Example #3
0
def test_get_instances():
    technique = api.create_instance("techniques",
                                    **factories.TechniqueFactory())
    assert api.get_instances("techniques",
                             [technique.name])[0].pk == technique.pk

    experiment = api.create_instance("experiments",
                                     **factories.ExperimentFactory())
    individual = experiment.sample.individual
    project = experiment.projects[0]
    assert api.get_experiments([experiment.pk])[0].pk == experiment.pk
    assert api.get_projects([project.pk])[0].pk == project.pk
    assert api.get_tree(individual.pk).pk == individual.pk
    assert api.get_trees([individual.pk])[0].pk == individual.pk
Example #4
0
def test_import_bedfiles(tmpdir):
    technique = api.create_instance("techniques",
                                    **factories.TechniqueFactory())
    targets = tmpdir.join("targets.bed")
    baits = tmpdir.join("baits.bed")
    targets.write("2\t1\t2\n1\t1\t2\n")
    baits.write("2\t1\t2\n1\t1\t2\n")
    species = "HUMAN"
    runner = CliRunner()
    technique = data.LocalBedImporter.import_bedfiles(
        species=species,
        technique=technique["pk"],
        targets_path=targets.strpath,
        baits_path=baits.strpath,
        assembly="AnAssembly",
        description="these are test BED files",
    )

    for i in "targets", "baits":
        bedfile = technique["reference_data"][f"AnAssembly_{i}_bedfile"]["url"]
        assert os.path.isfile(bedfile)
        assert os.path.isfile(bedfile + ".tbi")
        assert os.path.isfile(bedfile.replace(".gz", ""))
        args = [str(technique.pk), "--bed-type", i]
        result = runner.invoke(commands.get_bed, args, catch_exceptions=False)
        assert bedfile in result.output
        with open(bedfile.replace(".gz", ""), "r") as f:  # test bed is sorted
            assert next(f).startswith("1")

    command = data.LocalBedImporter.as_cli_command()
    args = [
        "--targets-path",
        targets.strpath,
        "--baits-path",
        baits.strpath,
        "--technique",
        technique["pk"],
        "--assembly",
        "AnAssembly",
        "--species",
        species,
        "--description",
        "Test",
    ]
    result = runner.invoke(command, args, catch_exceptions=False)
    assert "has registered BED files for" in result.output
Example #5
0
def create_pair(
    tumor_bam,
    normal_bam,
    bedfile="/a/fake/bedfile",
    assembly="GRCh37",
    method="TD",
    species="HUMAN",
):
    """Create pair."""
    pair = []
    bed_file_dict = dict(url=bedfile, description="")
    technique = factories.TechniqueFactory(method=method)
    technique["reference_data"][f"{assembly}_targets_bedfile"] = bed_file_dict
    technique["reference_data"][f"{assembly}_baits_bedfile"] = bed_file_dict

    for (i, category) in [(tumor_bam, "TUMOR"), (normal_bam, "NORMAL")]:
        experiment = factories.ExperimentFactory(technique=technique)
        experiment["sample"]["individual"]["species"] = species
        experiment["sample"]["category"] = category
        experiment["bam_files"][assembly] = dict(url=i, analysis=1)
        pair.append(api.create_instance("experiments", **experiment))

    return pair[0], pair[1]