def test_cytof_antibody_metadata_end_to_end(monkeypatch, metadata_df):
    """Test addition of antibody metadata for cytof files"""
    # Mock an CyTOF downloadable file record
    cytof_record = MagicMock()
    cytof_record.object_url = "foo.txt"
    cytof_record.upload_type = "cytof"
    cytof_record.additional_metadata = {"foo": "bar"}
    get_by_object_url = MagicMock()
    get_by_object_url.return_value = cytof_record
    monkeypatch.setattr(DownloadableFiles, "get_by_object_url",
                        get_by_object_url)

    # Mock trial
    ct = MagicMock()
    ct.metadata_json = {
        "assays": {
            "cytof": [{
                "cytof_antibodies": [
                    {
                        "usage": "Ignored"
                    },
                    {
                        "usage": "Used",
                        "stain_type": "Surface Stain",
                        "isotope": "000Foo",
                        "antibody": "Bar",
                        "clone": "Nx/xxx",
                    },
                    {
                        "usage": "Analysis Only",
                        "stain_type": "Intracellular",
                        "isotope": "001Foo",
                        "antibody": "Baz",
                    },
                ],
                "object_url":
                "foo.txt",  # for DeepSearch
            }]
        }
    }
    find_by_trial_id = MagicMock()
    find_by_trial_id.return_value = ct
    monkeypatch.setattr(TrialMetadata, "find_by_trial_id", find_by_trial_id)

    # Mock metadata_df
    _get_metadata_df = MagicMock()
    _get_metadata_df.return_value = metadata_df
    monkeypatch.setattr(functions.visualizations, "_get_metadata_df",
                        _get_metadata_df)

    vis_preprocessing(make_pubsub_event("1"), {})
    get_by_object_url.assert_called_once()
    _get_metadata_df.assert_called_once()

    assert cytof_record.additional_metadata == {
        "foo":
        "bar",
        "cytof.antibodies":
        "surface 000Foo-Bar (Nx/xxx), intracellular 001Foo-Baz",
    }
def test_cytof_clustergrammer_end_to_end(monkeypatch, metadata_df,
                                         upload_type):
    """Test the CyTOF-clustergrammer transform."""
    # Test no file found
    monkeypatch.setattr(DownloadableFiles, "get_by_object_url",
                        lambda *args, **kwargs: None)
    with pytest.raises(Exception, match="No downloadable file"):
        vis_preprocessing(make_pubsub_event("foo/bar"), {})

    # Mock a CyTOF summary downloadable file record
    cytof_record = MagicMock()
    cytof_record.object_url = "foo.txt"
    cytof_record.upload_type = upload_type
    get_by_object_url = MagicMock()
    get_by_object_url.return_value = cytof_record
    monkeypatch.setattr(DownloadableFiles, "get_by_object_url",
                        get_by_object_url)

    # Mock GCS call
    gcs_blob = MagicMock()
    _get_blob_as_stream = MagicMock()
    fake_cytof = open(CYTOF_PATH, "rb")
    _get_blob_as_stream.return_value = fake_cytof
    monkeypatch.setattr(functions.visualizations, "get_blob_as_stream",
                        _get_blob_as_stream)

    # Mock metadata_df
    _get_metadata_df = MagicMock()
    _get_metadata_df.return_value = metadata_df
    monkeypatch.setattr(functions.visualizations, "_get_metadata_df",
                        _get_metadata_df)

    vis_preprocessing(make_pubsub_event("1"), {})
    get_by_object_url.assert_called_once()
    _get_blob_as_stream.assert_called_once()
    _get_metadata_df.assert_called_once()

    # Check contents of the clustergrammer output
    row_names = [
        row["name"] for row in cytof_record.clustergrammer["row_nodes"]
    ]
    col_names = [
        col["name"] for col in cytof_record.clustergrammer["col_nodes"]
    ]
    col_cats = [(col["cat-0"], col["cat-1"], col["cat-2"])
                for col in cytof_record.clustergrammer["col_nodes"]]

    # Based on the contents of fake_cytof_summary.csv...
    assert row_names == ["cell1", "cell2"]
    assert col_names == ["CIMAC Id: CTTTTPPS1.01", "CIMAC Id: CTTTTPPS2.01"]

    # Based on the construction of metadata_df...
    assert col_cats == [
        ("Participant Id: CTTTTPP", "Cohort: Arm_A",
         "Collection Event: Event1"),
        ("Participant Id: CTTTTPP", "Cohort: Arm_A",
         "Collection Event: Event2"),
    ]

    fake_cytof.close()
def test_mif_antibody_metadata_end_to_end(monkeypatch, metadata_df):
    """Test addition of antibody metadata for MIF files"""
    # Mock an MIF downloadable file record
    mif_record = MagicMock()
    mif_record.object_url = "foo.txt"
    mif_record.upload_type = "mif"
    mif_record.additional_metadata = {"foo": "bar"}
    get_by_object_url = MagicMock()
    get_by_object_url.return_value = mif_record
    monkeypatch.setattr(DownloadableFiles, "get_by_object_url",
                        get_by_object_url)

    # Mock GCS call
    ct = MagicMock()
    ct.metadata_json = {
        "assays": {
            "mif": [{
                "antibodies": [
                    {
                        "export_name": "Foo"
                    },
                    {
                        "antibody": "Bar",
                        "clone": "Nx/xxx",
                        "fluor_wavelength": 500
                    },
                    {
                        "antibody": "Baz",
                        "fluor_wavelength": 500
                    },
                ],
                "object_url":
                "foo.txt",  # for DeepSearch
            }]
        }
    }
    find_by_trial_id = MagicMock()
    find_by_trial_id.return_value = ct
    monkeypatch.setattr(TrialMetadata, "find_by_trial_id", find_by_trial_id)

    # Mock metadata_df
    _get_metadata_df = MagicMock()
    _get_metadata_df.return_value = metadata_df
    monkeypatch.setattr(functions.visualizations, "_get_metadata_df",
                        _get_metadata_df)

    vis_preprocessing(make_pubsub_event("1"), {})
    get_by_object_url.assert_called_once()
    _get_metadata_df.assert_called_once()

    assert mif_record.additional_metadata == {
        "foo": "bar",
        "mif.antibodies": "Foo, Bar (Nx/xxx - 500), Baz (500)",
    }
def test_ihc_combined_end_to_end(monkeypatch, metadata_df):
    """Test the IHC combined transform."""
    # Mock an IHC combined downloadable file record
    ihc_record = MagicMock()
    ihc_record.object_url = "foo.txt"
    ihc_record.upload_type = "ihc marker combined"
    get_by_object_url = MagicMock()
    get_by_object_url.return_value = ihc_record
    monkeypatch.setattr(DownloadableFiles, "get_by_object_url",
                        get_by_object_url)

    # Mock GCS call
    gcs_blob = MagicMock()
    _get_blob_as_stream = MagicMock()
    combined_csv = StringIO(
        "cimac_id,foo,bar\nCTTTTPPS1.01,1,2\nCTTTTPPS2.01,3,4")
    _get_blob_as_stream.return_value = combined_csv
    monkeypatch.setattr(functions.visualizations, "get_blob_as_stream",
                        _get_blob_as_stream)

    # Mock metadata_df
    _get_metadata_df = MagicMock()
    _get_metadata_df.return_value = metadata_df
    monkeypatch.setattr(functions.visualizations, "_get_metadata_df",
                        _get_metadata_df)

    vis_preprocessing(make_pubsub_event("1"), {})
    get_by_object_url.assert_called_once()
    _get_blob_as_stream.assert_called_once()
    _get_metadata_df.assert_called_once()

    assert ihc_record.ihc_combined_plot == [
        {
            "cimac_id": "CTTTTPPS1.01",
            "foo": 1,
            "bar": 2,
            "cimac_participant_id": "CTTTTPP",
            "cohort_name": "Arm_A",
            "collection_event_name": "Event1",
        },
        {
            "cimac_id": "CTTTTPPS2.01",
            "foo": 3,
            "bar": 4,
            "cimac_participant_id": "CTTTTPP",
            "cohort_name": "Arm_A",
            "collection_event_name": "Event2",
        },
    ]
def test_ihc_antibody_metadata_end_to_end(monkeypatch, metadata_df):
    """Test addition of antibody metadata for IHC files"""
    # Mock an IHC downloadable file record
    ihc_record = MagicMock()
    ihc_record.object_url = "foo.txt"
    ihc_record.upload_type = "ihc"
    ihc_record.additional_metadata = {"foo": "bar"}
    get_by_object_url = MagicMock()
    get_by_object_url.return_value = ihc_record
    monkeypatch.setattr(DownloadableFiles, "get_by_object_url",
                        get_by_object_url)

    # Mock GCS call
    ct = MagicMock()
    ct.metadata_json = {
        "assays": {
            "ihc": [{
                "antibody": {
                    "antibody": "Bar",
                    "clone": "Nx/xxx"
                },
                "object_url": "foo.txt",  # for DeepSearch
            }]
        }
    }
    find_by_trial_id = MagicMock()
    find_by_trial_id.return_value = ct
    monkeypatch.setattr(TrialMetadata, "find_by_trial_id", find_by_trial_id)

    # Mock metadata_df
    _get_metadata_df = MagicMock()
    _get_metadata_df.return_value = metadata_df
    monkeypatch.setattr(functions.visualizations, "_get_metadata_df",
                        _get_metadata_df)

    vis_preprocessing(make_pubsub_event("1"), {})
    get_by_object_url.assert_called_once()
    _get_metadata_df.assert_called_once()

    assert ihc_record.additional_metadata == {
        "foo": "bar",
        "ihc.antibody": "Bar (Nx/xxx)",
    }
def test_loading_lazily(monkeypatch, metadata_df):
    """Test that files aren't loaded if there are no transformations for them"""
    record = MagicMock()
    record.object_url = "foo.txt"
    record.upload_type = "something"
    get_by_object_url = MagicMock()
    get_by_object_url.return_value = record
    monkeypatch.setattr(DownloadableFiles, "get_by_object_url",
                        get_by_object_url)

    get_blob_as_stream = MagicMock()
    monkeypatch.setattr(functions.visualizations, "get_blob_as_stream",
                        get_blob_as_stream)

    # Mock metadata_df
    _get_metadata_df = MagicMock()
    _get_metadata_df.return_value = metadata_df
    monkeypatch.setattr(functions.visualizations, "_get_metadata_df",
                        _get_metadata_df)

    vis_preprocessing(make_pubsub_event("1"), {})
    get_blob_as_stream.assert_not_called()