|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +from cg.apps.scout.scoutapi import ScoutAPI |
| 4 | +from cg.constants import FileExtensions, GenePanelMasterList, Workflow |
| 5 | +from cg.constants.gene_panel import GenePanelCombo, GenePanelGenomeBuild |
| 6 | +from cg.io.txt import write_txt |
| 7 | +from cg.services.analysis_starter.configurator.file_creators.utils import get_genome_build |
| 8 | +from cg.store.models import Case |
| 9 | +from cg.store.store import Store |
| 10 | + |
| 11 | + |
| 12 | +class GenePanelFileCreator: |
| 13 | + def __init__(self, store: Store, scout_api: ScoutAPI): |
| 14 | + self.store = store |
| 15 | + self.scout_api = scout_api |
| 16 | + |
| 17 | + @staticmethod |
| 18 | + def get_file_path(case_path: Path) -> Path: |
| 19 | + return Path(case_path, "gene_panels").with_suffix(FileExtensions.BED) |
| 20 | + |
| 21 | + def create(self, case_id: str, case_path: Path) -> None: |
| 22 | + file_path: Path = self.get_file_path(case_path) |
| 23 | + content: list[str] = self._get_content(case_id) |
| 24 | + write_txt(file_path=file_path, content=content) |
| 25 | + |
| 26 | + def _get_content(self, case_id: str) -> list[str]: |
| 27 | + case: Case = self.store.get_case_by_internal_id(internal_id=case_id) |
| 28 | + genome_build: GenePanelGenomeBuild = get_genome_build(workflow=Workflow(case.data_analysis)) |
| 29 | + all_panels: list[str] = self._get_aggregated_panels( |
| 30 | + customer_id=case.customer.internal_id, default_panels=set(case.panels) |
| 31 | + ) |
| 32 | + return self.scout_api.export_panels(build=genome_build, panels=all_panels) |
| 33 | + |
| 34 | + def _get_aggregated_panels(self, customer_id: str, default_panels: set[str]) -> list[str]: |
| 35 | + """Check if customer is collaborator for gene panel master list |
| 36 | + and if all default panels are included in the gene panel master list. |
| 37 | + If not, add gene panel combo and broad non-specific gene panels. |
| 38 | + Return an aggregated gene panel.""" |
| 39 | + if GenePanelMasterList.is_customer_collaborator_and_panels_in_gene_panels_master_list( |
| 40 | + customer_id=customer_id, gene_panels=default_panels |
| 41 | + ): |
| 42 | + return GenePanelMasterList.get_panel_names() |
| 43 | + all_panels: set[str] = self._add_gene_panel_combo(gene_panels=default_panels) |
| 44 | + all_panels |= GenePanelMasterList.get_non_specific_gene_panels() |
| 45 | + return list(all_panels) |
| 46 | + |
| 47 | + @staticmethod |
| 48 | + def _add_gene_panel_combo(gene_panels: set[str]) -> set[str]: |
| 49 | + """ |
| 50 | + Add gene panels combinations for gene panels being part of gene panel combination and |
| 51 | + return updated gene panels. |
| 52 | + """ |
| 53 | + additional_panels = set() |
| 54 | + for panel in gene_panels: |
| 55 | + if panel in GenePanelCombo.COMBO_1: |
| 56 | + additional_panels |= GenePanelCombo.COMBO_1.get(panel) |
| 57 | + gene_panels |= additional_panels |
| 58 | + return gene_panels |
0 commit comments