Beispiel #1
0
def run_sample(args: object):
    """
    Runs sample ID through snakemake pipeline
    """
    sample: list[dict] = datahandling.get_samples(
        sample_ids=[args.sample_id]
    )  #this syntax is type annotation. That is sample (the annotation target) is of type list[dict]. this is done before variable assignment and can be useful for use with type checkers
    component: list[dict] = datahandling.get_components(
        component_names=[COMPONENT['name']],
        component_versions=[COMPONENT['version']])
    if len(component) == 0:
        print(
            f"component not found in DB, would you like to install it (Y/N)?:")
        install: string = input()
        if install.upper() == "Y":
            datahandling.post_component(COMPONENT)
            component: list[dict] = datahandling.get_components(
                component_names=[COMPONENT['name']],
                component_versions=[COMPONENT['version']])
            if len(component) != 1:
                print(
                    f"Error with installation of {COMPONENT['name']} v:{COMPONENT['version']} \n"
                )
                exit()
        else:
            print(f"To continue please install the component")
            exit()

    if len(sample) == 0:
        # Invalid sample id
        print(f"sample_id not found in DB")
        pass
    elif len(sample) != 1:
        print(f"Error with sample_id")
    elif len(component) != 1:
        print(f"Error with component_id")
    else:
        print(
            f"snakemake -s /bifrost/{COMPONENT['name']}/pipeline.smk --config sample_id={str(sample[0]['_id'])} component_id={str(component[0]['_id'])}"
        )
        try:
            process: subprocess.Popen = subprocess.Popen(
                f"snakemake -s /bifrost/{COMPONENT['name']}/pipeline.smk --config sample_id={str(sample[0]['_id'])} component_id={str(component[0]['_id'])}",
                stdout=sys.stdout,
                stderr=sys.stderr,
                shell=True)
            process.communicate()
        except:
            print(traceback.format_exc())
Beispiel #2
0
def test_delete_run():
    sample = {"name": "test_sample"}
    sample_db = datahandling.post_sample(sample)
    component = {"name": "assemblatron"}
    component_db = datahandling.post_component(component)
    s_c = {
        "sample": {
            "_id": sample_db["_id"]
        },
        "component": {
            "_id": component_db["_id"],
            "name": component_db["name"]
        },
        "results": {},
        "summary": {},
        "setup_date": datetime.datetime.now()
    }
    datahandling.post_sample_component(s_c)

    run = {
        "name": "test_run",
        "samples": [{
            "_id": sample_db["_id"]
        }],
        "components": [{
            "_id": component_db["_id"]
        }
                       # Missing name
                       ]
    }
    run_db = datahandling.post_run(run)

    deleted = datahandling.delete_run(run_id=str(run_db["_id"]))

    assert deleted == 1
Beispiel #3
0
def test_get_sample_using_component_id():
    component = {"name": "assemblatron"}
    component_db = datahandling.post_component(component)

    sample = {"name": "test_sample", "components": [component_db]}
    sample_db = datahandling.post_sample(sample)
    sample["_id"] = sample_db["_id"]
    sample_received = datahandling.get_samples(
        component_ids=[str(component_db["_id"])])[0]
    assert sample_received == sample
Beispiel #4
0
def install_component():
    component: list[dict] = datahandling.get_components(
        component_names=[COMPONENT['name']],
        component_versions=[COMPONENT['version']])
    if len(component) == 1:
        print(f"Component has already been installed")
    elif len(component) > 1:
        print(
            f"Component exists multiple times in DB, please contact an admin to fix this in order to proceed"
        )
    else:
        datahandling.post_component(COMPONENT)
        component: list[dict] = datahandling.get_components(
            component_names=[COMPONENT['name']],
            component_versions=[COMPONENT['version']])
        if len(component) != 1:
            print(
                f"Error with installation of {COMPONENT['name']} v:{COMPONENT['version']} \n"
            )
            exit()
Beispiel #5
0
def test_post_sample_component():
    sample = {"name": "test_sample"}
    sample_db = datahandling.post_sample(sample)
    component = {"name": "assemblatron"}
    component_db = datahandling.post_component(component)
    s_c = {
        "sample": {
            "_id": sample_db["_id"]
        },
        "component": {
            "_id": component_db["_id"]
        },
        "results": {},
        "summary": {}
    }
    s_c_db = datahandling.post_sample_component(s_c)
    s_c["_id"] = s_c_db["_id"]
    # we can't get this timestamp so we won't test it
    s_c_db.pop('metadata', None)
    s_c.pop('metadata', None)
    assert s_c == s_c_db
Beispiel #6
0
def test_get_sample_components():
    sample = {"name": "test_sample"}
    sample_db = datahandling.post_sample(sample)
    component = {"name": "assemblatron"}
    component_db = datahandling.post_component(component)
    s_c = {
        "sample": {
            "_id": sample_db["_id"]
        },
        "component": {
            "_id": component_db["_id"],
            "name": component_db["name"]
        },
        "results": {},
        "summary": {},
        "setup_date": datetime.datetime.now()
    }
    s_c_db = datahandling.post_sample_component(s_c)
    # Testing this.
    s_c_2 = datahandling.get_sample_components(
        sample_ids=[str(sample_db["_id"])], component_names=["assemblatron"])
    assert s_c_2[0] == s_c_db
Beispiel #7
0
def test_delete_sample():
    sample = {"name": "test_sample"}
    sample_db = datahandling.post_sample(sample)
    component = {"name": "assemblatron"}
    component_db = datahandling.post_component(component)
    s_c = {
        "sample": {
            "_id": sample_db["_id"]
        },
        "component": {
            "_id": component_db["_id"],
            "name": component_db["name"]
        },
        "results": {},
        "summary": {},
        "setup_date": datetime.datetime.now()
    }
    datahandling.post_sample_component(s_c)

    # Testing this
    deleted = datahandling.delete_sample(str(sample_db["_id"]))
    assert deleted == 1
Beispiel #8
0
def test_delete_component():
    component = {"name": "assemblatron"}
    component_db = datahandling.post_component(component)

    deleted = datahandling.delete_component(str(component_db["_id"]))
    assert deleted == 1
Beispiel #9
0
def test_post_component():
    component = {"name": "assemblatron"}
    component_db = datahandling.post_component(component)
    component["_id"] = component_db["_id"]
    assert component == component_db
Beispiel #10
0
def test_import_run():
    component = {"name": "assemblatron"}
    component_db = datahandling.post_component(component)
    component_2 = {"name": "whats_my_species"}
    component_db_2 = datahandling.post_component(component_2)
    sample = {
        "name":
        "test_sample",
        "components": [{
            "_id": component_db["_id"]
        }, {
            "_id": component_db_2["_id"]
        }]
    }
    sample_db = datahandling.post_sample(sample)
    s_c = {
        "sample": {
            "_id": sample_db["_id"]
        },
        "component": {
            "_id": component_db["_id"],
            "name": component_db["name"]
        },
        "results": {},
        "summary": {},
        "setup_date": datetime.datetime.now()
    }
    datahandling.post_sample_component(s_c)

    time.sleep(0.2)
    # we need to wait otherwise they share the same setup_date and
    # it screws up the order

    s_c_2 = {
        "sample": {
            "_id": sample_db["_id"]
        },
        "component": {
            "_id": component_db_2["_id"],
            "name": component_db_2["name"]
        },
        "results": {},
        "summary": {},
        "setup_date": datetime.datetime.now()
    }
    datahandling.post_sample_component(s_c_2)

    run = {
        "name":
        "test_run",
        "samples": [{
            "_id": sample_db["_id"]
        }],
        "components": [{
            "_id": component_db["_id"]
        }, {
            "_id": component_db_2["_id"]
        }
                       # Missing name
                       ]
    }
    run_db = datahandling.post_run(run)

    run_export = datahandling.get_run_export(names=[str(run_db["name"])])

    #drop database
    conn = mongomock.MongoClient('mongodb://server.example.com:27017')
    conn.drop_database("serumqc_prod")
    conn.close()

    datahandling.post_run_export(run_export)

    get_items = {
        "samples": [],
        "sample_components": [],
        "runs": [],
        "components": []
    }

    run_dict = run_export[str(run_db["name"])]

    for s in run_dict["samples"]:
        get_s = datahandling.get_samples(sample_ids=[str(s["_id"])])[0]
        get_items["samples"].append(get_s)

    for s_c in run_dict["sample_components"]:
        get_s_c = datahandling.get_sample_components(
            sample_component_ids=[str(s_c["_id"])])[0]
        get_items["sample_components"].append(get_s_c)

    for r in run_dict["runs"]:
        get_r = datahandling.get_runs(run_id=str(r["_id"]))[0]
        get_items["runs"].append(get_r)

    for c in run_dict["components"]:
        get_c = datahandling.get_components(component_ids=[str(c["_id"])])[0]
        get_items["components"].append(get_c)

    assert get_items == run_dict
Beispiel #11
0
def test_export_run():
    component = {"name": "assemblatron"}
    component_db = datahandling.post_component(component)
    component_2 = {"name": "whats_my_species"}
    component_db_2 = datahandling.post_component(component_2)
    sample = {
        "name":
        "test_sample",
        "components": [{
            "_id": component_db["_id"]
        }, {
            "_id": component_db_2["_id"]
        }]
    }
    sample_db = datahandling.post_sample(sample)
    s_c = {
        "sample": {
            "_id": sample_db["_id"]
        },
        "component": {
            "_id": component_db["_id"],
            "name": component_db["name"]
        },
        "results": {},
        "summary": {},
        "setup_date": datetime.datetime.now()
    }
    s_c_db = datahandling.post_sample_component(s_c)
    time.sleep(0.2)
    # we need to wait otherwise they share the same setup_date and
    # it screws up the order
    s_c_2 = {
        "sample": {
            "_id": sample_db["_id"]
        },
        "component": {
            "_id": component_db_2["_id"],
            "name": component_db_2["name"]
        },
        "results": {},
        "summary": {},
        "setup_date": datetime.datetime.now()
    }
    s_c_db_2 = datahandling.post_sample_component(s_c_2)

    run = {
        "name":
        "test_run",
        "samples": [{
            "_id": sample_db["_id"]
        }],
        "components": [{
            "_id": component_db["_id"]
        }, {
            "_id": component_db_2["_id"]
        }
                       # Missing name
                       ]
    }
    run_db = datahandling.post_run(run)

    run_export = datahandling.get_run_export(names=[str(run_db["name"])])

    run_id = str(run_db["_id"])

    run_expected = {
        run_db["name"]: {
            "components": [component_db_2, component_db],
            "samples": [sample_db],
            "sample_components": [s_c_db_2, s_c_db],
            "runs": [run_db]
        }
    }

    assert run_export == run_expected