Skip to content

Commit

Permalink
refactor: update copy back logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Smeds committed May 3, 2023
1 parent 1a2c1f1 commit 6f2e848
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 45 deletions.
1 change: 0 additions & 1 deletion bio/gatk/modelsegments/meta.yaml
Expand Up @@ -10,7 +10,6 @@ input:
- matched_normal allelic-counts, Optional
- segments picard interval-list file containing a multisample segmentation output by a previous run, optional
params:
- prefix for output filenames, if not specified the wrapper will try to extract it from the output files
- The `java_opts` param allows for additional arguments to be passed to the java compiler, e.g. "-XX:ParallelGCThreads=10" (not for `-XmX` or `-Djava.io.tmpdir`, since they are handled automatically).
- The `extra` param allows for additional program arguments.
output:
Expand Down
2 changes: 1 addition & 1 deletion bio/gatk/modelsegments/test/Snakefile
Expand Up @@ -3,7 +3,7 @@ rule modelsegments_denoise_input:
denoised_copy_ratios="a.denoisedCR.tsv",
output:
"a.den.modelFinal.seg",
"a.den.cr.seg",
"a.n.cr.seg",
log:
"logs/gatk/modelsegments_denoise.log",
params:
Expand Down
64 changes: 21 additions & 43 deletions bio/gatk/modelsegments/wrapper.py
Expand Up @@ -39,45 +39,6 @@
"'allelica_counts' is required when 'normal-allelic-counts' is an input to the rule!"
)


output_prefix = None
if snakemake.params.get("output_prefix"):
# Make sure specified prefix matches listed output files
output_prefix = snakemake.params.output_prefix
for output in snakemake.output:
output_file = os.path.basename(output)
if not output_file.startswith(prefix):
raise Exception(
f"output file {output_file} doesn't start with the provided prefix {prefix}"
)
else:
expected_output_file_endings = [
".modelFinal.seq",
".cr.seg",
".af.igv.seg",
".cr.igv.seg",
".hets.tsv",
".modelBegin.cr.param",
".modelBegin.af.param",
".modelBegin.seg",
".modelFinal.af.param",
".modelFinal.cr.param",
]
# Create prefix from listed output files
for output in snakemake.output:
output_file = os.path.basename(output)
for output_extensions in expected_output_file_endings:
if output_file.endswith(output_extensions):
output_prefix = output_file.replace(output_extensions, "")
break
if output_prefix:
break
if not output_prefix:
raise Exception(
"Unable to extract prefix from listed files, expecting file(s) ending "
f"with at least one of {expected_output_file_endings}"
)

extra = snakemake.params.get("extra", "")
java_opts = get_java_opts(snakemake)

Expand All @@ -92,14 +53,31 @@
" {denoised_copy_ratios}"
" {allelic_counts}"
" {normal_allelic_counts}"
" --output-prefix {output_prefix}"
" --output-prefix temp_name__"
" -O {output_folder}"
" --tmp-dir {tmpdir}"
" {extra}"
" {log}"
)

created_files = {}
# Find all created files
for new_file in os.listdir(output_folder):
file_path = os.path.join(output_folder, new_file)
if os.path.isfile(file_path):
file_end = os.path.basename(file_path).split("__")[1]
created_files[file_end] = file_path

# Match expected output with found files
for output in snakemake.output:
filename = os.path.basename(output)
outfile = os.path.join(output_folder, filename)
shell("cp {outfile} {output}")
file_found = False
for file_ending in created_files:
if output.endswith(file_ending):
shell(f"cp {created_files[file_ending]} {output}")
file_found = True
break
if not file_found:
created_files_list = [f"{e}" for e in created_files]
raise IOError(
f"Could not create file {output}, possible files ends with {created_files_list}"
)

0 comments on commit 6f2e848

Please sign in to comment.