Skip to content

Commit

Permalink
Refactor case_append_alignments and fix a bug creating alignments
Browse files Browse the repository at this point in the history
  • Loading branch information
Chiara Rasi committed Nov 19, 2024
1 parent f3a9778 commit 52faa24
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
2 changes: 1 addition & 1 deletion scout/server/blueprints/alignviewers/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def set_sample_tracks(display_obj: dict, case_groups: list, chromosome: str):
return

for count, sample in enumerate(case.get("sample_names")):
if case[track_items][count] == "missing":
if case[track_items][count] == "missing" or case[track_index_items][count] == "missing":
continue
sample_tracks.append(
{
Expand Down
56 changes: 25 additions & 31 deletions scout/server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,49 +300,43 @@ def _check_path_name(ind: Dict, path_name: str) -> bool:
def case_append_alignments(case_obj):
"""Deconvolute information about files to case_obj.
This function prepares the bam/cram files in a certain way so that they are easily accessed in
the templates.
Loops over the the individuals and gather bam/cram files, indexes and sample display names in
lists
This function prepares bam/cram files and their indexes for easy access in templates.
Args:
case_obj(scout.models.Case)
case_obj (scout.models.Case): The case object to process.
"""
unwrap_settings = [
{"path": "bam_file", "append_to": "bam_files", "index": "bai_files"},
{"path": "mt_bam", "append_to": "mt_bams", "index": "mt_bais"},
{"path": "rhocall_bed", "append_to": "rhocall_beds", "index": "no_index"},
{"path": "rhocall_wig", "append_to": "rhocall_wigs", "index": "no_index"},
{
"path": "upd_regions_bed",
"append_to": "upd_regions_beds",
"index": "no_index",
},
{"path": "upd_sites_bed", "append_to": "upd_sites_beds", "index": "no_index"},
{
"path": "tiddit_coverage_wig",
"append_to": "tiddit_coverage_wigs",
"index": "no_index",
},
{"path": "rhocall_bed", "append_to": "rhocall_beds", "index": None},
{"path": "rhocall_wig", "append_to": "rhocall_wigs", "index": None},
{"path": "upd_regions_bed", "append_to": "upd_regions_beds", "index": None},
{"path": "upd_sites_bed", "append_to": "upd_sites_beds", "index": None},
{"path": "tiddit_coverage_wig", "append_to": "tiddit_coverage_wigs", "index": None},
]

for individual in case_obj["individuals"]:
def process_file(case_obj, individual, setting):
"""Process a single file and its optional index."""
file_path = individual.get(setting["path"])
append_safe(
case_obj,
"sample_names",
case_obj.get("display_name", "") + " - " + individual.get("display_name", ""),
setting["append_to"],
file_path if file_path and os.path.exists(file_path) else "missing",
)
if setting["index"]:
index_path = (
find_index(file_path) if file_path and os.path.exists(file_path) else "missing"
)
append_safe(case_obj, setting["index"], index_path)

for individual in case_obj["individuals"]:
# Add sample name
sample_name = f"{case_obj.get('display_name', '')} - {individual.get('display_name', '')}"
append_safe(case_obj, "sample_names", sample_name)

# Process all file settings
for setting in unwrap_settings:
file_path = individual.get(setting["path"])
if not (file_path and os.path.exists(file_path)):
append_safe(case_obj, setting["append_to"], "missing")
continue
append_safe(case_obj, setting["append_to"], file_path)
if not setting["index"] == "no_index":
append_safe(
case_obj, setting["index"], find_index(file_path)
) # either bai_files or mt_bais
process_file(case_obj, individual, setting)


def append_safe(obj, obj_index, elem):
Expand Down

0 comments on commit 52faa24

Please sign in to comment.