-
Notifications
You must be signed in to change notification settings - Fork 10
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
fix: update in_clinvar flag in smallvariants (#2222) #2224
Open
stolpeo
wants to merge
1
commit into
main
Choose a base branch
from
2222-update-in_clinvar-flag-in-smallvariants
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.
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
"""Setting in_clinvar field in SmallVariant aligned with Clinvar database.""" | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.db import transaction | ||
from projectroles.models import Project | ||
|
||
from clinvar.models import Clinvar | ||
from variants.models import SmallVariant | ||
|
||
|
||
def update_in_clinvar_full(case): | ||
"""Update the in_clinvar field in SmallVariant with information from Clinvar database.""" | ||
variants = SmallVariant.objects.filter(case_id=case.id) | ||
result = { | ||
"true": 0, | ||
"false": 0, | ||
"total": 0, | ||
} | ||
for var in variants: | ||
coords = { | ||
"release": var.release, | ||
"chromosome": var.chromosome, | ||
"start": var.start, | ||
"end": var.end, | ||
"reference": var.reference, | ||
"alternative": var.alternative, | ||
} | ||
var.in_clinvar = Clinvar.objects.filter(**coords).exists() | ||
var.save() | ||
result["true" if var.in_clinvar else "false"] += 1 | ||
result["total"] += 1 | ||
return result | ||
|
||
|
||
def update_in_clinvar_light(project, printer=lambda x: None): | ||
result = 0 | ||
for cv in Clinvar.objects.all(): | ||
coords = { | ||
"release": cv.release, | ||
"chromosome": cv.chromosome, | ||
"start": cv.start, | ||
"end": cv.end, | ||
"reference": cv.reference, | ||
"alternative": cv.alternative, | ||
} | ||
for case in project.case_set.all(): | ||
for var in SmallVariant.objects.filter(case_id=case.id, **coords): | ||
if var.in_clinvar: | ||
continue | ||
var.in_clinvar = True | ||
var.save() | ||
result += 1 | ||
return result | ||
|
||
|
||
@transaction.atomic | ||
def run(*args, **options): | ||
"""Run the command.""" | ||
|
||
project = Project.objects.get(sodar_uuid=options["project_uuid"]) | ||
updates = { | ||
"true": 0, | ||
"false": 0, | ||
"total": 0, | ||
} | ||
|
||
if options["full"]: | ||
for case in project.case_set.all(): | ||
result = update_in_clinvar_full(case) | ||
updates["true"] += result["true"] | ||
updates["false"] += result["false"] | ||
updates["total"] += result["total"] | ||
else: | ||
updates["true"] += update_in_clinvar_light(project) | ||
|
||
return updates | ||
|
||
|
||
class Command(BaseCommand): | ||
"""Command to set the in_clinvar field in SmallVariant with information from Clinvar database. | ||
|
||
Background: When updating the Clinvar database, the in_clinvar field in SmallVariant | ||
should be updated to reflect the changes in the Clinvar database. | ||
""" | ||
|
||
#: Help message displayed on the command line. | ||
help = "Update in_clinvar field in SmallVariant with information from Clinvar database." | ||
|
||
def add_arguments(self, parser): | ||
"""Add the command's argument to the ``parser``.""" | ||
parser.add_argument("--project-uuid", help="Project UUID to apply on") | ||
parser.add_argument( | ||
"--full", | ||
help="Reset ALL variants instead of only variants in Clinvar.", | ||
action="store_true", | ||
) | ||
# parser.add_argument("--all", help="Apply on all projects", action="store_true") | ||
|
||
def handle(self, *args, **options): | ||
self.stdout.write("Call options:") | ||
self.stdout.write(f"- {options['project_uuid']=}") | ||
self.stdout.write(f"- {options['full']=}") | ||
self.stdout.write(f"Performing ...") | ||
updates = run(*args, **options) | ||
self.stdout.write(f"{updates=}") | ||
self.stdout.write(f"Done.") |
Oops, something went wrong.
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.
🛠️ Refactor suggestion
Optimize database queries and remove unused parameter.
The function has performance and code quality issues:
printer
parameter is unused.Consider this optimized implementation: