-
Notifications
You must be signed in to change notification settings - Fork 47
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
Remove any variant type #5219
Open
northwestwitch
wants to merge
25
commits into
main
Choose a base branch
from
remove_any_variant_type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+102
−41
Open
Remove any variant type #5219
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
90953fc
Improve the cli options
2776b3f
Remove sys messages
2d5ed42
some adjustments
7885df4
move lines
435b6e7
Remove also omics-variants
90f10bc
delete wts only if specified
96e2e7e
some fixes + changelog
4afda12
Restore some code to not break tests
9184ce2
Thanks SonarQube
e0767c9
Added a test
67cd4a6
tiny thing
07f6530
Removed unused imports
a549b1e
Merge branch 'main' into remove_any_variant_type
northwestwitch 1f4c3f7
Merge branch 'main' into remove_any_variant_type
northwestwitch fe71931
Merge branch 'main' into remove_any_variant_type
dnil 67559e2
Merge branch 'main' into remove_any_variant_type
dnil d16ab5e
Merge branch 'main' into remove_any_variant_type
dnil f82b688
Small fix
2024971
Merge branch 'main' into remove_any_variant_type
northwestwitch 353b611
Merge branch 'main' into remove_any_variant_type
dnil 2a29564
Merge branch 'main' into remove_any_variant_type
northwestwitch beb6390
Merge branch 'main' into remove_any_variant_type
northwestwitch c262f9e
Merge branch 'main' into remove_any_variant_type
dnil ae60320
Merge branch 'main' into remove_any_variant_type
northwestwitch b0926bd
Merge branch 'main' into remove_any_variant_type
northwestwitch 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 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 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 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 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 |
---|---|---|
@@ -1,15 +1,16 @@ | ||
import logging | ||
from typing import List, Tuple | ||
|
||
import click | ||
from flask import current_app, url_for | ||
from flask.cli import with_appcontext | ||
|
||
from scout.constants import CASE_STATUSES | ||
from scout.constants import ANALYSIS_TYPES, CASE_STATUSES, VARIANTS_TARGET_FROM_CATEGORY | ||
from scout.server.extensions import store | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
BYTES_IN_ONE_GIGABYTE = 1073741824 # (1024*1024*1024) | ||
BYTES_IN_ONE_GIGABYTE = 1073741824 | ||
DELETE_VARIANTS_HEADER = [ | ||
"Case n.", | ||
"Ncases", | ||
|
@@ -23,8 +24,18 @@ | |
"Total variants", | ||
"Removed variants", | ||
] | ||
VARIANT_CATEGORIES = list(VARIANTS_TARGET_FROM_CATEGORY.keys()) + ["wts_outliers"] | ||
|
||
VARIANT_CATEGORIES = ["mei", "snv", "sv", "cancer", "cancer_sv", "str"] | ||
|
||
def _set_keep_ctg(keep_ctg: Tuple[str], rm_ctg: Tuple[str]) -> List[str]: | ||
"""Define the categories of variants that should not be removed.""" | ||
if keep_ctg and rm_ctg: | ||
raise click.UsageError("Please use either '--keep-ctg' or '--rm-ctg' parameter, not both.") | ||
if keep_ctg: | ||
return list(keep_ctg) | ||
if rm_ctg: | ||
return list(set(VARIANT_CATEGORIES).difference(set(rm_ctg))) | ||
return [] | ||
|
||
|
||
@click.command("variants", short_help="Delete variants for one or more cases") | ||
|
@@ -47,18 +58,25 @@ | |
@click.option("--older-than", type=click.INT, default=0, help="Older than (months)") | ||
@click.option( | ||
"--analysis-type", | ||
type=click.Choice(["wgs", "wes", "panel"]), | ||
type=click.Choice(ANALYSIS_TYPES), | ||
multiple=True, | ||
help="Type of analysis", | ||
) | ||
@click.option("--rank-threshold", type=click.INT, default=5, help="With rank threshold lower than") | ||
@click.option("--variants-threshold", type=click.INT, help="With more variants than") | ||
@click.option( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New option, mutually exclusive with --keep-ctg |
||
"--rm-ctg", | ||
type=click.Choice(VARIANT_CATEGORIES), | ||
multiple=True, | ||
required=False, | ||
help="Remove only the following categories", | ||
) | ||
@click.option( | ||
"--keep-ctg", | ||
type=click.Choice(VARIANT_CATEGORIES), | ||
multiple=True, | ||
required=False, | ||
help="Do not delete one of more variant categories", | ||
help="Keep the following categories", | ||
) | ||
@click.option( | ||
"--dry-run", | ||
|
@@ -66,146 +84,164 @@ | |
help="Perform a simulation without removing any variant", | ||
) | ||
@with_appcontext | ||
def variants( | ||
user: str, | ||
case_id: list, | ||
case_id: tuple, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are all tuples, I've actually checked |
||
case_file: str, | ||
institute: str, | ||
status: list, | ||
status: tuple, | ||
older_than: int, | ||
analysis_type: list, | ||
analysis_type: tuple, | ||
rank_threshold: int, | ||
variants_threshold: int, | ||
keep_ctg: list, | ||
rm_ctg: tuple, | ||
keep_ctg: tuple, | ||
dry_run: bool, | ||
) -> None: | ||
"""Delete variants for one or more cases""" | ||
|
||
if case_file and case_id: | ||
click.echo( | ||
"You should specify either case ID (multiple times if needed) or the path to a text file containing a list of case IDs (one per line)." | ||
) | ||
return | ||
|
||
user_obj = store.user(user) | ||
if user_obj is None: | ||
click.echo(f"Could not find a user with email '{user}' in database") | ||
return | ||
|
||
total_deleted = 0 | ||
items_name = "deleted variants" | ||
|
||
if dry_run: | ||
click.echo("--------------- DRY RUN COMMAND ---------------") | ||
items_name = "estimated deleted variants" | ||
else: | ||
click.confirm("Variants are going to be deleted from database. Continue?", abort=True) | ||
|
||
if case_file: | ||
case_id = [line.strip() for line in open(case_file).readlines() if line.strip()] | ||
|
||
case_query = store.build_case_query( | ||
case_ids=case_id, | ||
institute_id=institute, | ||
status=status, | ||
older_than=older_than, | ||
analysis_type=analysis_type, | ||
) | ||
# Estimate the average size of a variant document in database | ||
avg_var_size = store.collection_stats("variant").get("avgObjSize", 0) # in bytes | ||
|
||
# Get all cases where case_query applies | ||
n_cases = store.case_collection.count_documents(case_query) | ||
cases = store.cases(query=case_query) | ||
filters = ( | ||
f"Rank-score threshold:{rank_threshold}, case n. variants threshold:{variants_threshold}." | ||
) | ||
click.echo("\t".join(DELETE_VARIANTS_HEADER)) | ||
keep_ctg = _set_keep_ctg(keep_ctg=keep_ctg, rm_ctg=rm_ctg) | ||
|
||
for nr, case in enumerate(cases, 1): | ||
case_id = case["_id"] | ||
institute_id = case["owner"] | ||
case_n_variants = store.variant_collection.count_documents({"case_id": case_id}) | ||
case_n_variants = store.variant_collection.count_documents( | ||
{"case_id": case_id} | ||
) + store.omics_variant_collection.count_documents({"case_id": case_id}) | ||
# Skip case if user provided a number of variants to keep and this number is less than total number of case variants | ||
if variants_threshold and case_n_variants < variants_threshold: | ||
continue | ||
# Get evaluated variants for the case that haven't been dismissed | ||
case_evaluated = store.evaluated_variants(case_id=case_id, institute_id=institute_id) | ||
evaluated_not_dismissed = [ | ||
variant["_id"] for variant in case_evaluated if "dismiss_variant" not in variant | ||
] | ||
# Do not remove variants that are either pinned, causative or evaluated not dismissed | ||
variants_to_keep = ( | ||
case.get("suspects", []) + case.get("causatives", []) + evaluated_not_dismissed or [] | ||
) | ||
variants_query = store.delete_variants_query( | ||
|
||
variants_query: dict = store.delete_variants_query( | ||
case_id, variants_to_keep, rank_threshold, keep_ctg | ||
) | ||
|
||
if dry_run: | ||
items_name = "estimated deleted variants" | ||
# Just print how many variants would be removed for this case | ||
remove_n_variants = store.variant_collection.count_documents(variants_query) | ||
total_deleted += remove_n_variants | ||
remove_n_omics_variants = ( | ||
store.omics_variant_collection.count_documents(variants_query) | ||
if "wts_outliers" not in keep_ctg | ||
else 0 | ||
) | ||
total_deleted += remove_n_variants + remove_n_omics_variants | ||
click.echo( | ||
"\t".join( | ||
[ | ||
str(nr), | ||
str(n_cases), | ||
case["owner"], | ||
case["display_name"], | ||
case_id, | ||
case.get("track", ""), | ||
str(case["analysis_date"]), | ||
case.get("status", ""), | ||
str(case.get("is_research", "")), | ||
str(case_n_variants), | ||
str(remove_n_variants), | ||
str(remove_n_variants + remove_n_omics_variants), | ||
] | ||
) | ||
) | ||
continue | ||
|
||
# delete variants specified by variants_query | ||
result = store.variant_collection.delete_many(variants_query) | ||
total_deleted += result.deleted_count | ||
items_name = "deleted variants" | ||
remove_n_variants = store.variant_collection.delete_many(variants_query).deleted_count | ||
remove_n_omics_variants = ( | ||
store.omics_variant_collection.delete_many(variants_query).deleted_count | ||
if "wts_outliers" not in keep_ctg | ||
else 0 | ||
) | ||
|
||
total_deleted += remove_n_variants + remove_n_omics_variants | ||
click.echo( | ||
"\t".join( | ||
[ | ||
str(nr), | ||
str(n_cases), | ||
case["owner"], | ||
case["display_name"], | ||
case_id, | ||
case.get("track", ""), | ||
str(case["analysis_date"]), | ||
case.get("status", ""), | ||
str(case.get("is_research", "")), | ||
str(case_n_variants), | ||
str(result.deleted_count), | ||
str(remove_n_variants + remove_n_omics_variants), | ||
] | ||
) | ||
) | ||
|
||
# Create event in database | ||
institute_obj = store.institute(case["owner"]) | ||
with current_app.test_request_context("/cases"): | ||
url = url_for( | ||
"cases.case", | ||
institute_id=institute_obj["_id"], | ||
case_name=case["display_name"], | ||
) | ||
store.remove_variants_event( | ||
institute=institute_obj, | ||
case=case, | ||
user=user_obj, | ||
link=url, | ||
content=filters, | ||
) | ||
|
||
# Update case variants count | ||
store.case_variants_count(case_id, institute_obj["_id"], True) | ||
|
||
click.echo(f"Total {items_name}: {total_deleted}") | ||
click.echo( | ||
f"Estimated space freed (GB): {round((total_deleted * avg_var_size) / BYTES_IN_ONE_GIGABYTE, 4)}" | ||
) | ||
|
||
|
||
|
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
["wts_outliers"] is added like that, because these variants are on another collection (omics_variant).