|
| 1 | +__author__ = "Patrik Smeds" |
| 2 | +__copyright__ = "Copyright 2023, Patrik Smed" |
| 3 | + |
| 4 | +__license__ = "MIT" |
| 5 | + |
| 6 | +import os |
| 7 | +import tempfile |
| 8 | +from snakemake.shell import shell |
| 9 | +from snakemake_wrapper_utils.java import get_java_opts |
| 10 | + |
| 11 | + |
| 12 | +denoised_copy_ratios = "" |
| 13 | +if snakemake.input.get("denoised_copy_ratios", None): |
| 14 | + denoised_copy_ratios = ( |
| 15 | + f"--denoised-copy-ratios {snakemake.input.denoised_copy_ratios}" |
| 16 | + ) |
| 17 | + |
| 18 | +allelic_counts = "" |
| 19 | +if snakemake.input.get("allelic_counts", None): |
| 20 | + allelic_counts = f"--allelic-counts {snakemake.input.allelic_counts}" |
| 21 | + |
| 22 | +normal_allelic_counts = "" |
| 23 | +if snakemake.input.get("normal_allelic_counts", None): |
| 24 | + matched_normal_allelic_counts = ( |
| 25 | + f"--normal-allelic-counts {snakemake.inut.normal_allelic_counts}" |
| 26 | + ) |
| 27 | + |
| 28 | +segments = "" |
| 29 | +if snakemake.input.get("segments", None): |
| 30 | + interval_list = f"--segments {snakemake.input.segments}" |
| 31 | + |
| 32 | +if not allelic_counts and not denoised_copy_ratios: |
| 33 | + raise Exception( |
| 34 | + "wrapper input requires either 'allelic_counts' or 'denoise_copy_ratios' to be set" |
| 35 | + ) |
| 36 | + |
| 37 | +if normal_allelic_counts and not allelic_counts: |
| 38 | + raise Exception( |
| 39 | + "'allelica_counts' is required when 'normal-allelic-counts' is an input to the rule!" |
| 40 | + ) |
| 41 | + |
| 42 | +extra = snakemake.params.get("extra", "") |
| 43 | +java_opts = get_java_opts(snakemake) |
| 44 | + |
| 45 | +log = snakemake.log_fmt_shell(stdout=True, stderr=True) |
| 46 | + |
| 47 | + |
| 48 | +with tempfile.TemporaryDirectory() as tmpdir: |
| 49 | + output_folder = os.path.join(tmpdir, "output_folder") |
| 50 | + shell( |
| 51 | + "gatk --java-options '{java_opts}' ModelSegments" |
| 52 | + " {segments}" |
| 53 | + " {denoised_copy_ratios}" |
| 54 | + " {allelic_counts}" |
| 55 | + " {normal_allelic_counts}" |
| 56 | + " --output-prefix temp_name__" |
| 57 | + " -O {output_folder}" |
| 58 | + " --tmp-dir {tmpdir}" |
| 59 | + " {extra}" |
| 60 | + " {log}" |
| 61 | + ) |
| 62 | + |
| 63 | + created_files = {} |
| 64 | + # Find all created files |
| 65 | + for new_file in os.listdir(output_folder): |
| 66 | + file_path = os.path.join(output_folder, new_file) |
| 67 | + if os.path.isfile(file_path): |
| 68 | + file_end = os.path.basename(file_path).split("__")[1] |
| 69 | + created_files[file_end] = file_path |
| 70 | + |
| 71 | + # Match expected output with found files |
| 72 | + for output in snakemake.output: |
| 73 | + file_found = False |
| 74 | + for file_ending in created_files: |
| 75 | + if output.endswith(file_ending): |
| 76 | + shell(f"cp {created_files[file_ending]} {output}") |
| 77 | + file_found = True |
| 78 | + break |
| 79 | + if not file_found: |
| 80 | + created_files_list = [f"{e}" for e in created_files] |
| 81 | + raise IOError( |
| 82 | + f"Could not create file {output}, possible files ends with {created_files_list}" |
| 83 | + ) |
0 commit comments