Skip to content

Commit

Permalink
forgot one file
Browse files Browse the repository at this point in the history
  • Loading branch information
stolpeo committed Oct 23, 2023
1 parent f73f858 commit 3e8124d
Showing 1 changed file with 75 additions and 28 deletions.
103 changes: 75 additions & 28 deletions variants/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ def create_queryresultset(case_uuid=None, project_uuid=None, all=False):
"comments": 0,
"flags": 0,
}, # user annotations added to SvQueryResultSet's
"removed": {
"comments": 0,
"flags": 0,
}, # user annotations without corresponding query result row
"removed": 0, # user annotations without corresponding query result row
"salvable": {
"comments": 0,
"flags": 0,
Expand All @@ -62,11 +59,7 @@ def create_queryresultset(case_uuid=None, project_uuid=None, all=False):
"flags": 0,
"acmg_ratings": 0,
},
"removed": {
"comments": 0,
"flags": 0,
"acmg_ratings": 0,
},
"removed": 0,
"salvable": {
"comments": 0,
"flags": 0,
Expand Down Expand Up @@ -131,15 +124,17 @@ def _perform_create(_case):

return _sm_result_set, _sv_result_set

def _perform_fill(sm_result_set, sv_result_set, tsv_lines):
def _perform_fill(sm_result_set, sv_result_set):
sm_count, sm_salvable, sm_duplicates, sm_tsv_lines = fill_sm_queryresultset(sm_result_set)
sv_count, sv_salvable, sv_duplicates, sv_tsv_lines = fill_sv_queryresultset(sv_result_set)
for i in ("added", "removed", "salvable", "lost"):
for i in ("added", "salvable", "lost"):
count["svs"][i]["flags"] += sv_count[i]["flags"]
count["svs"][i]["comments"] += sv_count[i]["comments"]
count["sms"][i]["flags"] += sm_count[i]["flags"]
count["sms"][i]["comments"] += sm_count[i]["comments"]
count["sms"][i]["acmg_ratings"] += sm_count[i]["acmg_ratings"]
count["svs"]["removed"] += sv_count["removed"]
count["sms"]["removed"] += sm_count["removed"]
salvable[str(sm_result_set.case.sodar_uuid)] = {
"sms": list(set(sm_salvable)),
"svs": list(set(sv_salvable)),
Expand All @@ -154,21 +149,27 @@ def _perform_fill(sm_result_set, sv_result_set, tsv_lines):
tsv_lines["svs"]["flags"].extend(sv_tsv_lines["flags"])
tsv_lines["svs"]["comments"].extend(sv_tsv_lines["comments"])

def _perform_clear(sm_result_set, sv_result_set):
count["sms"]["removed"] += clear_sm_queryresultset(sm_result_set)
count["svs"]["removed"] += clear_sv_queryresultset(sv_result_set)

def _handle_case(_case):
sm_result_set, sv_result_set = _perform_create(_case)
_perform_clear(sm_result_set, sv_result_set)
_perform_fill(sm_result_set, sv_result_set)

if bool(case_uuid) + bool(project_uuid) + bool(all) != 1:
return

if case_uuid:
_case = Case.objects.get(sodar_uuid=case_uuid)
sm_result_set, sv_result_set = _perform_create(_case)
_perform_fill(sm_result_set, sv_result_set, tsv_lines)
_handle_case(_case)
elif project_uuid:
for _case in Case.objects.filter(project__sodar_uuid=project_uuid):
sm_result_set, sv_result_set = _perform_create(_case)
_perform_fill(sm_result_set, sv_result_set, tsv_lines)
_handle_case(_case)
else:
for _case in Case.objects.all():
sm_result_set, sv_result_set = _perform_create(_case)
_perform_fill(sm_result_set, sv_result_set, tsv_lines)
_handle_case(_case)

return count, salvable, duplicates, tsv_lines

Expand All @@ -182,11 +183,7 @@ def fill_sm_queryresultset(result_set):
"comments": 0,
"acmg_ratings": 0,
},
"removed": {
"flags": 0,
"comments": 0,
"acmg_ratings": 0,
},
"removed": 0,
"salvable": {
"flags": 0,
"comments": 0,
Expand Down Expand Up @@ -247,7 +244,7 @@ def _perform_create(obj):
}
)
for result_row in result_rows:
count["removed"][obj_type] += 1
count["removed"] += 1
result_row.delete()
else:
if result_rows.count() == 0:
Expand Down Expand Up @@ -311,10 +308,7 @@ def fill_sv_queryresultset(result_set):
"flags": 0,
"comments": 0,
},
"removed": {
"flags": 0,
"comments": 0,
},
"removed": 0,
"salvable": {
"flags": 0,
"comments": 0,
Expand Down Expand Up @@ -377,7 +371,7 @@ def _perform_create(obj):
}
)
for result_row in result_rows:
count["removed"][obj_type] += 1
count["removed"] += 1
result_row.delete()
else:
if not result_rows:
Expand Down Expand Up @@ -445,3 +439,56 @@ def _perform_create(obj):
_perform_create(obj)

return count, salvable, duplicates, tsv_lines


def clear_sm_queryresultset(result_set):
count = 0
anno_to_class = {
"flags": SmallVariantFlags,
"comments": SmallVariantComment,
"acmg_ratings": AcmgCriteriaRating,
}

for row in result_set.smallvariantqueryresultrow_set.all():
for anno_type in anno_to_class.keys():
anno = anno_to_class[anno_type].objects.filter(
case=result_set.case,
release=row.release,
chromosome=row.chromosome,
start=row.start,
end=row.end,
reference=row.reference,
alternative=row.alternative,
)
if anno.exists():
break
else:
count += 1
row.delete()

return count


def clear_sv_queryresultset(result_set):
count = 0
anno_to_class = {
"flags": StructuralVariantFlags,
"comments": StructuralVariantComment,
}

for row in result_set.svqueryresultrow_set.all():
for anno_type in anno_to_class.keys():
anno = anno_to_class[anno_type].objects.filter(
case=result_set.case,
release=row.release,
chromosome=row.chromosome,
start=row.start,
end=row.end,
sv_type=row.sv_type,
sv_sub_type=row.sv_sub_type,
)
if not anno.exists():
count += 1
row.delete()

return count

0 comments on commit 3e8124d

Please sign in to comment.