From 6eea26db6ceadbc0dbdd77288f94cecff0fbc8b6 Mon Sep 17 00:00:00 2001 From: souzadevinicius Date: Wed, 11 Dec 2024 15:37:21 +0000 Subject: [PATCH] feat(runner.py): new function added: implemented a function to download and extract the exomiser runner this function will handle the preinstallation processes for exomiser runner, and must exist for every different pheval runner ensuring a streamlined setup. --- src/pheval_exomiser/runner.py | 72 ++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/src/pheval_exomiser/runner.py b/src/pheval_exomiser/runner.py index 6a6f862..edf7855 100644 --- a/src/pheval_exomiser/runner.py +++ b/src/pheval_exomiser/runner.py @@ -1,13 +1,20 @@ """Exomiser Runner""" +import os +import zipfile from dataclasses import dataclass from pathlib import Path +import requests from pheval.runners.runner import PhEvalRunner from pheval_exomiser.post_process.post_process import post_process_result_format -from pheval_exomiser.prepare.tool_specific_configuration_options import ExomiserConfigurations -from pheval_exomiser.prepare.write_application_properties import ExomiserConfigurationFileWriter +from pheval_exomiser.prepare.tool_specific_configuration_options import ( + ExomiserConfigurations, +) +from pheval_exomiser.prepare.write_application_properties import ( + ExomiserConfigurationFileWriter, +) from pheval_exomiser.run.run import prepare_batch_files, run_exomiser @@ -22,6 +29,67 @@ class ExomiserPhEvalRunner(PhEvalRunner): config_file: Path version: str + def install(self, overwrite_existing: bool = False): + # Parse configuration options + configuration = ExomiserConfigurations.parse_obj( + self.input_dir_config.tool_specific_configuration_options + ) + phenotype_version = configuration.application_properties.phenotype_data_version + + # Define filenames and URLs + base_url = "https://data.monarchinitiative.org/exomiser" + filenames = [ + f"{phenotype_version}_hg19.zip", + f"{phenotype_version}_hg38.zip", + f"{phenotype_version}_phenotype.zip", + ] + urls = [ + f"{base_url}/{self.version}/exomiser-cli-{self.version}-distribution.zip", + *[f"{base_url}/data/{filename}" for filename in filenames], + ] + + # Download files + os.makedirs("./data/tmp/", exist_ok=True) + for url in urls: + filename = url.split("/")[-1] + full_filename = f"./data/tmp/{filename}" + + if os.path.exists(full_filename) and not overwrite_existing: + continue + + response = requests.get(url) + response.raise_for_status() # Raise an error for bad responses + with open(full_filename, "wb") as f: + f.write(response.content) + + # Check if data directory exists + data_dir = f"exomiser-cli-{self.version}/data" + if os.path.exists(data_dir) and not overwrite_existing: + return + + # Unzip distribution file + with zipfile.ZipFile( + f"exomiser-cli-{self.version}-distribution.zip", "r" + ) as zip_ref: + zip_ref.extractall(".") + + # Unzip data files into the appropriate directory + os.makedirs(data_dir, exist_ok=True) + for filename in filenames: + with zipfile.ZipFile(filename, "r") as zip_ref: + zip_ref.extractall(data_dir) + + # Update application.properties + properties_path = os.path.join(data_dir, "application.properties") + with open(properties_path, "a") as prop_file: + prop_file.writelines( + [ + f"exomiser.hg19.data-version={phenotype_version}\n", + f"exomiser.hg38.data-version={phenotype_version}\n", + f"exomiser.phenotype.data-version={phenotype_version}\n", + ] + ) + def prepare(self): """prepare""" print("preparing")