Skip to content

Commit

Permalink
Merge branch 'feat/DEVSU-2349-add-async-and-sync-upload-test' of http…
Browse files Browse the repository at this point in the history
…s://github.com/bcgsc/pori_python into refactor/lint_unify_types
  • Loading branch information
dustinbleile committed Jul 19, 2024
2 parents e53c510 + 36b8eda commit 9c1e70c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 63 deletions.
24 changes: 19 additions & 5 deletions pori_python/ipr/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ def command_interface() -> None:
parser.add_argument("--graphkb_url", default=None)
parser.add_argument("--log_level", default="info", choices=LOG_LEVELS.keys())
parser.add_argument(
"--therapeutics", default=False, help="Generate therapeutic options", action="store_true"
"--therapeutics",
default=False,
help="Generate therapeutic options",
action="store_true",
)
parser.add_argument(
"--skip_comments",
Expand All @@ -83,7 +86,9 @@ def command_interface() -> None:
help="Turn off generating the analyst comments section of the report",
)
parser.add_argument(
"-o", "--output_json_path", help="path to a JSON to output the report upload body"
"-o",
"--output_json_path",
help="path to a JSON to output the report upload body",
)
parser.add_argument(
"-w",
Expand Down Expand Up @@ -365,7 +370,10 @@ def ipr_report(
logger.info(f"annotating {len(structural_variants)} structural variants")
gkb_matches.extend(
annotate_positional_variants(
graphkb_conn, structural_variants, kb_disease_match, show_progress=interactive
graphkb_conn,
structural_variants,
kb_disease_match,
show_progress=interactive,
)
)
logger.debug(f"\tgkb_matches: {len(gkb_matches)}")
Expand All @@ -381,7 +389,10 @@ def ipr_report(
logger.info(f"annotating {len(expression_variants)} expression variants")
gkb_matches.extend(
annotate_expression_variants(
graphkb_conn, expression_variants, kb_disease_match, show_progress=interactive
graphkb_conn,
expression_variants,
kb_disease_match,
show_progress=interactive,
)
)
logger.debug(f"\tgkb_matches: {len(gkb_matches)}")
Expand Down Expand Up @@ -418,7 +429,10 @@ def ipr_report(
if generate_comments:
comments = {
"comments": summarize(
graphkb_conn, gkb_matches, disease_name=kb_disease_match, variants=all_variants
graphkb_conn,
gkb_matches,
disease_name=kb_disease_match,
variants=all_variants,
)
}
else:
Expand Down
142 changes: 84 additions & 58 deletions tests/test_ipr/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,62 +31,78 @@ def get_test_file(name: str) -> str:


@pytest.fixture(scope="module")
def loaded_report(tmp_path_factory) -> Dict:
mock = MagicMock()
def loaded_reports(tmp_path_factory) -> Dict:
json_file = tmp_path_factory.mktemp("inputs") / "content.json"
async_json_file = tmp_path_factory.mktemp("inputs") / "async_content.json"
patient_id = f"TEST_{str(uuid.uuid4())}"
json_file.write_text(
json.dumps(
async_patient_id = f"TEST_ASYNC_{str(uuid.uuid4())}"
json_contents = {
"comparators": [
{"analysisRole": "expression (disease)", "name": "1"},
{"analysisRole": "expression (primary site)", "name": "2"},
{"analysisRole": "expression (biopsy site)", "name": "3"},
{
"comparators": [
{"analysisRole": "expression (disease)", "name": "1"},
{"analysisRole": "expression (primary site)", "name": "2"},
{"analysisRole": "expression (biopsy site)", "name": "3"},
{"analysisRole": "expression (internal pancancer cohort)", "name": "4"},
],
"patientId": patient_id,
"project": "TEST",
"expressionVariants": json.loads(
pd.read_csv(get_test_file("expression.short.tab"), sep="\t").to_json(
orient="records"
)
),
"smallMutations": json.loads(
pd.read_csv(get_test_file("small_mutations.short.tab"), sep="\t").to_json(
orient="records"
)
),
"copyVariants": json.loads(
pd.read_csv(get_test_file("copy_variants.short.tab"), sep="\t").to_json(
orient="records"
)
),
"structuralVariants": json.loads(
pd.read_csv(get_test_file("fusions.tab"), sep="\t").to_json(orient="records")
),
"kbDiseaseMatch": "colorectal cancer",
"analysisRole": "expression (internal pancancer cohort)",
"name": "4",
},
],
"patientId": patient_id,
"project": "TEST",
"expressionVariants": json.loads(
pd.read_csv(get_test_file("expression.short.tab"), sep="\t").to_json(orient="records")
),
"smallMutations": json.loads(
pd.read_csv(get_test_file("small_mutations.short.tab"), sep="\t").to_json(
orient="records"
)
),
"copyVariants": json.loads(
pd.read_csv(get_test_file("copy_variants.short.tab"), sep="\t").to_json(
orient="records"
)
),
"structuralVariants": json.loads(
pd.read_csv(get_test_file("fusions.tab"), sep="\t").to_json(orient="records")
),
"kbDiseaseMatch": "colorectal cancer",
}
json_file.write_text(
json.dumps(
json_contents,
allow_nan=False,
)
)
with patch.object(
sys,
"argv",
[
"ipr",
"--username",
os.environ.get("IPR_USER", os.environ["USER"]),
"--password",
os.environ["IPR_PASS"],
"--ipr_url",
os.environ["IPR_TEST_URL"],
"--graphkb_url",
os.environ.get("GRAPHKB_URL", False),
"--content",
str(json_file),
"--therapeutics",
],
):

json_contents["patientId"] = async_patient_id
async_json_file.write_text(
json.dumps(
json_contents,
allow_nan=False,
)
)

argslist = [
"ipr",
"--username",
os.environ.get("IPR_USER", os.environ["USER"]),
"--password",
os.environ["IPR_PASS"],
"--ipr_url",
os.environ["IPR_TEST_URL"],
"--graphkb_url",
os.environ.get("GRAPHKB_URL", False),
"--therapeutics",
]

sync_argslist = argslist.copy()
sync_argslist.extend(["--content", str(json_file)])
with patch.object(sys, "argv", sync_argslist):
with patch.object(IprConnection, "get_spec", return_value=get_test_spec()):
command_interface()

async_argslist = argslist.copy()
async_argslist.extend(["--content", str(async_json_file), "--async_upload"])
with patch.object(sys, "argv", async_argslist):
with patch.object(IprConnection, "get_spec", return_value=get_test_spec()):
command_interface()

Expand All @@ -96,7 +112,12 @@ def loaded_report(tmp_path_factory) -> Dict:
url=os.environ["IPR_TEST_URL"],
)
loaded_report = ipr_conn.get(uri=f"reports?searchText={patient_id}")
return (patient_id, loaded_report)
async_loaded_report = ipr_conn.get(uri=f"reports?searchText={async_patient_id}")

return {
"sync": (patient_id, loaded_report),
"async": (async_patient_id, async_loaded_report),
}


def get_section(loaded_report, section_name):
Expand All @@ -114,11 +135,16 @@ def get_section(loaded_report, section_name):
)
@pytest.mark.skipif(EXCLUDE_INTEGRATION_TESTS, reason="excluding long running integration tests")
class TestCreateReport:
def test_patient_id_loaded_once(self, loaded_report: Tuple) -> None:
patient_id = loaded_report[0]
assert loaded_report[1]["total"] == 1
assert loaded_report[1]["reports"][0]["patientId"] == patient_id

def test_analyst_comments_loaded(self, loaded_report: Tuple) -> None:
section = get_section(loaded_report, "summary/analyst-comments")
assert section["comments"]
def test_patient_id_loaded_once(self, loaded_reports) -> None:
sync_patient_id = loaded_reports["sync"][0]
assert loaded_reports["sync"][1]["total"] == 1
assert loaded_reports["sync"][1]["reports"][0]["patientId"] == sync_patient_id
async_patient_id = loaded_reports["async"][0]
assert loaded_reports["async"][1]["total"] == 1
assert loaded_reports["async"][1]["reports"][0]["patientId"] == async_patient_id

def test_analyst_comments_loaded(self, loaded_reports) -> None:
sync_section = get_section(loaded_reports["sync"], "summary/analyst-comments")
assert sync_section["comments"]
async_section = get_section(loaded_reports["async"], "summary/analyst-comments")
assert async_section["comments"]

0 comments on commit 9c1e70c

Please sign in to comment.