-
Notifications
You must be signed in to change notification settings - Fork 0
Add isolate removal utility and CLI subcommand #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| from typing import Callable, Optional | ||
|
|
||
| from sqlalchemy import select | ||
| from sqlalchemy.orm import Session | ||
|
|
||
| from marc_db.db import get_session | ||
| from marc_db.models import ( | ||
| Aliquot, | ||
| Antimicrobial, | ||
| Assembly, | ||
| AssemblyQC, | ||
| Contaminant, | ||
| Isolate, | ||
| TaxonomicAssignment, | ||
| ) | ||
|
|
||
|
|
||
| def _summarize_isolate(session: Session, sample_id: str) -> dict: | ||
| assembly_ids = select(Assembly.id).where(Assembly.isolate_id == sample_id) | ||
| return { | ||
| "aliquots": session.query(Aliquot) | ||
| .filter(Aliquot.isolate_id == sample_id) | ||
| .count(), | ||
| "assemblies": session.query(Assembly) | ||
| .filter(Assembly.isolate_id == sample_id) | ||
| .count(), | ||
| "assembly_qc": session.query(AssemblyQC) | ||
| .filter(AssemblyQC.assembly_id.in_(assembly_ids)) | ||
| .count(), | ||
| "taxonomic_assignments": session.query(TaxonomicAssignment) | ||
| .filter(TaxonomicAssignment.assembly_id.in_(assembly_ids)) | ||
| .count(), | ||
| "contaminants": session.query(Contaminant) | ||
| .filter(Contaminant.assembly_id.in_(assembly_ids)) | ||
| .count(), | ||
| "antimicrobials": session.query(Antimicrobial) | ||
| .filter(Antimicrobial.assembly_id.in_(assembly_ids)) | ||
| .count(), | ||
| } | ||
|
|
||
|
|
||
| def remove_isolate( | ||
| *, | ||
| sample_id: str, | ||
| yes: bool = False, | ||
| session: Optional[Session] = None, | ||
Ulthran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| input_fn: Callable[[str], str] = input, | ||
| ): | ||
| """Remove a single isolate and its associated records.""" | ||
|
|
||
| created_session = False | ||
| if session is None: | ||
| session = get_session() | ||
| created_session = True | ||
|
|
||
| trans = session.begin_nested() if session.in_transaction() else session.begin() | ||
| try: | ||
| isolate = session.get(Isolate, sample_id) | ||
| if isolate is None: | ||
| print(f"No isolate found with SampleID {sample_id}.") | ||
| trans.rollback() | ||
| return | ||
|
|
||
| counts = _summarize_isolate(session, sample_id) | ||
|
|
||
| if not yes: | ||
| print(f"Isolate {sample_id} will be removed with the following records:") | ||
| for label, count in counts.items(): | ||
| print(f" {label.replace('_', ' ')}: {count}") | ||
| answer = input_fn("Proceed with deletion? [y/N]: ").strip().lower() | ||
| if answer not in {"y", "yes"}: | ||
| trans.rollback() | ||
| print("Removal cancelled.") | ||
| return | ||
|
|
||
| assembly_ids = select(Assembly.id).where(Assembly.isolate_id == sample_id) | ||
|
|
||
| session.query(Antimicrobial).filter( | ||
| Antimicrobial.assembly_id.in_(assembly_ids) | ||
| ).delete(synchronize_session=False) | ||
| session.query(Contaminant).filter( | ||
| Contaminant.assembly_id.in_(assembly_ids) | ||
| ).delete(synchronize_session=False) | ||
| session.query(TaxonomicAssignment).filter( | ||
| TaxonomicAssignment.assembly_id.in_(assembly_ids) | ||
| ).delete(synchronize_session=False) | ||
| session.query(AssemblyQC).filter( | ||
| AssemblyQC.assembly_id.in_(assembly_ids) | ||
| ).delete(synchronize_session=False) | ||
| session.query(Assembly).filter( | ||
| Assembly.isolate_id == sample_id | ||
| ).delete(synchronize_session=False) | ||
| session.query(Aliquot).filter(Aliquot.isolate_id == sample_id).delete( | ||
| synchronize_session=False | ||
| ) | ||
| session.query(Isolate).filter(Isolate.sample_id == sample_id).delete( | ||
| synchronize_session=False | ||
| ) | ||
|
|
||
| trans.commit() | ||
| except Exception: | ||
| trans.rollback() | ||
| raise | ||
| finally: | ||
| if created_session: | ||
| session.close() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import pandas as pd | ||
| from pathlib import Path | ||
| from sqlalchemy import create_engine | ||
| from sqlalchemy.orm import sessionmaker | ||
|
|
||
| from marc_db.ingest import ingest_from_tsvs | ||
| from marc_db.models import ( | ||
| Aliquot, | ||
| Antimicrobial, | ||
| Assembly, | ||
| AssemblyQC, | ||
| Base, | ||
| Isolate, | ||
| TaxonomicAssignment, | ||
| ) | ||
| from marc_db.remove import remove_isolate | ||
|
|
||
|
|
||
| data_dir = Path(__file__).parent | ||
|
|
||
|
|
||
| def _build_session(): | ||
| engine = create_engine("sqlite:///:memory:") | ||
| Session = sessionmaker(bind=engine) | ||
| session = Session() | ||
| Base.metadata.create_all(engine) | ||
| return session, engine | ||
|
|
||
|
|
||
| def test_remove_isolate_deletes_associations(): | ||
| session, engine = _build_session() | ||
| isolates_df = pd.read_csv(data_dir / "test_multi_aliquot.tsv", sep="\t") | ||
| assemblies_df = pd.read_csv(data_dir / "test_assembly_data.tsv", sep="\t") | ||
| tax_df = pd.read_csv(data_dir / "test_taxonomic_assignment.tsv", sep="\t") | ||
| amr_df = pd.read_csv(data_dir / "test_amr_data.tsv", sep="\t") | ||
|
|
||
| ingest_from_tsvs( | ||
| isolates=isolates_df, | ||
| assemblies=assemblies_df, | ||
| assembly_qcs=assemblies_df, | ||
| taxonomic_assignments=tax_df, | ||
| antimicrobials=amr_df, | ||
| yes=True, | ||
| session=session, | ||
| ) | ||
|
|
||
| remove_isolate(sample_id="sample1", yes=True, session=session) | ||
|
|
||
| remaining_sample = "sample2" | ||
| expected_aliquots = isolates_df.loc[ | ||
| isolates_df["SampleID"] == remaining_sample | ||
| ].shape[0] | ||
| expected_assemblies = assemblies_df.loc[ | ||
| assemblies_df["SampleID"] == remaining_sample | ||
| ].shape[0] | ||
| expected_taxonomic = tax_df.loc[tax_df["SampleID"] == remaining_sample].shape[0] | ||
| expected_amr = amr_df.loc[amr_df["SampleID"] == remaining_sample].shape[0] | ||
|
|
||
| assert session.query(Isolate).count() == 1 | ||
| assert session.query(Aliquot).count() == expected_aliquots | ||
| assert session.query(Assembly).count() == expected_assemblies | ||
|
|
||
| remaining_assembly_ids = [ | ||
| asm.id | ||
| for asm in session.query(Assembly).filter( | ||
| Assembly.isolate_id == remaining_sample | ||
| ) | ||
| ] | ||
| assert session.query(AssemblyQC).filter( | ||
| AssemblyQC.assembly_id.in_(remaining_assembly_ids) | ||
| ).count() == expected_assemblies | ||
| assert session.query(TaxonomicAssignment).filter( | ||
| TaxonomicAssignment.assembly_id.in_(remaining_assembly_ids) | ||
| ).count() == expected_taxonomic | ||
| assert session.query(Antimicrobial).filter( | ||
| Antimicrobial.assembly_id.in_(remaining_assembly_ids) | ||
| ).count() == expected_amr | ||
|
|
||
| session.close() | ||
| engine.dispose() | ||
|
|
||
|
|
||
| def test_remove_isolate_cancelled_does_not_delete(capsys): | ||
| session, engine = _build_session() | ||
| isolates_df = pd.read_csv(data_dir / "test_multi_aliquot.tsv", sep="\t") | ||
|
|
||
| ingest_from_tsvs(isolates=isolates_df, yes=True, session=session) | ||
|
|
||
| remove_isolate( | ||
| sample_id="sample1", | ||
| yes=False, | ||
| session=session, | ||
| input_fn=lambda _: "n", | ||
| ) | ||
|
|
||
| captured = capsys.readouterr() | ||
| assert "Removal cancelled." in captured.out | ||
| assert session.query(Isolate).count() == 2 | ||
|
|
||
| session.close() | ||
| engine.dispose() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.