def test_api_summary(db: Session, client: TestClient):
    # TODO: This would be better queried against the real data
    for _ in range(10):
        fakes.BiosampleFactory()
        fakes.MetagenomeAnnotationFactory()
        fakes.MetagenomeAssemblyFactory()
        fakes.MetaproteomicAnalysisFactory()
        fakes.DataObjectFactory()
    db.commit()
    assert_status(client.get("/api/summary"))
    assert_status(client.get("/api/stats"))
Exemple #2
0
def test_query_gene_function_mga_metap(db: Session):
    sample1 = fakes.BiosampleFactory(id="sample1")
    fakes.BiosampleFactory(id="sample2")
    gene_functions = [
        fakes.MGAGeneFunction(function__id=f"function{i}") for i in range(10)
    ]
    fakes.MetagenomeAnnotationFactory(id="mga1",
                                      gene_functions=gene_functions,
                                      omics_processing__biosample=sample1)
    metap = fakes.MetaproteomicAnalysisFactory(id="metap1")
    peptide = fakes.MetaproteomicPeptideFactory(
        metaproteomic_analysis=metap, best_protein_object=gene_functions[2])
    fakes.PeptideMGAGeneFunctionFactory(mga_gene_function=gene_functions[1],
                                        metaproteomic_peptide=peptide)
    db.commit()
    models.MGAGeneFunctionAggregation.populate(db)
    models.MetaPGeneFunctionAggregation.populate(db)
    db.commit()

    q = query.MetagenomeAnnotationQuerySchema(conditions=[{
        "table": "gene_function",
        "field": "id",
        "value": "function1",
    }], )
    assert {r.id for r in q.execute(db)} == {"mga1"}

    q = query.MetagenomeAnnotationQuerySchema(conditions=[{
        "table": "gene_function",
        "field": "id",
        "value": "invalid",
    }], )
    assert {r.id for r in q.execute(db)} == set()

    q1 = query.MetaproteomicAnalysisQuerySchema(conditions=[{
        "table":
        "gene_function",
        "field":
        "id",
        "value":
        "function1",
    }], )
    assert {r.id for r in q1.execute(db)} == {"metap1"}

    q1 = query.MetaproteomicAnalysisQuerySchema(conditions=[{
        "table": "gene_function",
        "field": "id",
        "value": "invalid",
    }], )
    assert {r.id for r in q1.execute(db)} == set()
def test_list_data_objects(db: Session, client: TestClient, endpoint: str):
    data_object = fakes.DataObjectFactory(id="do")
    omics_processing = fakes.OmicsProcessingFactory(id="1")
    reads_qc = fakes.ReadsQCFactory(id="1")
    assembly = fakes.MetagenomeAssemblyFactory(id="1")
    annotation = fakes.MetagenomeAnnotationFactory(id="1")
    analysis = fakes.MetaproteomicAnalysisFactory(id="1")

    omics_processing.outputs = [data_object]
    reads_qc.outputs = [data_object]
    assembly.outputs = [data_object]
    annotation.outputs = [data_object]
    analysis.outputs = [data_object]
    db.commit()

    resp = client.get(f"/api/{endpoint}/1/outputs")
    assert_status(resp)
    assert ["do"] == [r["id"] for r in resp.json()]
Exemple #4
0
def test_basic_query(db: Session, table):
    tests: Dict[str, Tuple[fakes.AnnotatedFactory, query.BaseQuerySchema]] = {
        "study": (fakes.StudyFactory(), query.StudyQuerySchema()),
        "omics_processing":
        (fakes.OmicsProcessingFactory(), query.OmicsProcessingQuerySchema()),
        "biosample": (fakes.BiosampleFactory(), query.BiosampleQuerySchema()),
        "reads_qc": (fakes.ReadsQCFactory(), query.ReadsQCQuerySchema()),
        "metagenome_assembly": (
            fakes.MetagenomeAssemblyFactory(),
            query.MetagenomeAssemblyQuerySchema(),
        ),
        "metagenome_annotation": (
            fakes.MetagenomeAnnotationFactory(),
            query.MetagenomeAnnotationQuerySchema(),
        ),
        "metaproteomic_analysis": (
            fakes.MetaproteomicAnalysisFactory(),
            query.MetaproteomicAnalysisQuerySchema(),
        ),
    }
    db.commit()
    q = tests[table][1].execute(db)
    assert tests[table][0].id in {r.id for r in q.all()}
Exemple #5
0
def test_pipeline_query(db: Session, table):
    omics_processing1 = fakes.OmicsProcessingFactory(name="omics_processing1")
    omics_processing2 = fakes.OmicsProcessingFactory(name="omics_processing2")

    fakes.ReadsQCFactory(omics_processing=omics_processing1, name="reads_qc1")
    fakes.ReadsQCFactory(omics_processing=omics_processing1, name="reads_qc2")
    fakes.ReadsQCFactory(omics_processing=omics_processing2, name="reads_qc3")

    fakes.MetagenomeAssemblyFactory(omics_processing=omics_processing1,
                                    name="assembly1")
    fakes.MetagenomeAssemblyFactory(omics_processing=omics_processing1,
                                    name="assembly2")
    fakes.MetagenomeAssemblyFactory(omics_processing=omics_processing2,
                                    name="assembly3")

    fakes.MetagenomeAnnotationFactory(omics_processing=omics_processing1,
                                      name="annotation1")
    fakes.MetagenomeAnnotationFactory(omics_processing=omics_processing1,
                                      name="annotation2")
    fakes.MetagenomeAnnotationFactory(omics_processing=omics_processing2,
                                      name="annotation3")

    fakes.MetaproteomicAnalysisFactory(omics_processing=omics_processing1,
                                       name="analysis1")
    fakes.MetaproteomicAnalysisFactory(omics_processing=omics_processing1,
                                       name="analysis2")
    fakes.MetaproteomicAnalysisFactory(omics_processing=omics_processing2,
                                       name="analysis3")

    fakes.MAGsAnalysisFactory(omics_processing=omics_processing1, name="mags1")
    fakes.MAGsAnalysisFactory(omics_processing=omics_processing1, name="mags2")
    fakes.MAGsAnalysisFactory(omics_processing=omics_processing2, name="mags3")

    fakes.ReadBasedAnalysisFactory(omics_processing=omics_processing1,
                                   name="reads1")
    fakes.ReadBasedAnalysisFactory(omics_processing=omics_processing1,
                                   name="reads2")
    fakes.ReadBasedAnalysisFactory(omics_processing=omics_processing2,
                                   name="reads3")

    fakes.NOMAnalysisFactory(omics_processing=omics_processing1, name="nom1")
    fakes.NOMAnalysisFactory(omics_processing=omics_processing1, name="nom2")
    fakes.NOMAnalysisFactory(omics_processing=omics_processing2, name="nom3")

    fakes.MetabolomicsAnalysisFactory(omics_processing=omics_processing1,
                                      name="metab1")
    fakes.MetabolomicsAnalysisFactory(omics_processing=omics_processing1,
                                      name="metab2")
    fakes.MetabolomicsAnalysisFactory(omics_processing=omics_processing2,
                                      name="metab3")
    db.commit()

    # test omics_processing not associated with biosamples
    omics_processing1.biosample_id = None
    db.add(omics_processing1)

    # test omics_processing not associated with studies
    omics_processing2.study_id = None
    db.add(omics_processing2)
    db.commit()

    query_schema = {
        "reads_qc": query.ReadsQCQuerySchema,
        "assembly": query.MetagenomeAssemblyQuerySchema,
        "annotation": query.MetagenomeAnnotationQuerySchema,
        "analysis": query.MetaproteomicAnalysisQuerySchema,
        "mags": query.MAGsAnalysisQuerySchema,
        "reads": query.ReadBasedAnalysisQuerySchema,
        "nom": query.NOMAnalysisQuerySchema,
        "metab": query.MetabolomicsAnalysisQuerySchema,
    }[table]

    q = query_schema()
    assert {f"{table}{i}"
            for i in [1, 2, 3]} == {r.name
                                    for r in q.execute(db).all()}

    q = query_schema(conditions=[{
        "table": "omics_processing",
        "field": "name",
        "value": "omics_processing1",
    }])
    assert {f"{table}{i}"
            for i in [1, 2]} == {r.name
                                 for r in q.execute(db).all()}

    q = query.OmicsProcessingQuerySchema(conditions=[{
        "table": q.table.value,
        "field": "name",
        "value": f"{table}1"
    }])
    assert ["omics_processing1"] == [r.name for r in q.execute(db).all()]