-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from phac-nml/assembly
Adding Assembly Stub Output and Generating IRIDA Next-Compliant JSON Output
- Loading branch information
Showing
11 changed files
with
271 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/usr/bin/env python | ||
|
||
import json | ||
import argparse | ||
import sys | ||
import gzip | ||
from mimetypes import guess_type | ||
from functools import partial | ||
from pathlib import Path | ||
|
||
|
||
def flatten_dictionary(dictionary): | ||
result = {} | ||
|
||
def flatten(item, name=""): | ||
if type(item) is dict: | ||
for component in item: | ||
flatten(item[component], str(name) + str(component) + ".") | ||
|
||
elif type(item) is list: | ||
for i in range(len(item)): | ||
flatten(item[i], str(name) + str(i + 1) + ".") # i + 1 because biologists | ||
|
||
else: | ||
result[str(name)[:-1]] = item # [:-1] avoids the "." appended on the previous recursion | ||
|
||
flatten(dictionary) | ||
return result | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
description="Simplifies JSON files for use with IRIDA Next", | ||
epilog="Example: python simplify_irida_json.py --json-output output.json input.json", | ||
) | ||
parser.add_argument("input") | ||
parser.add_argument( | ||
"--json-output", | ||
action="store", | ||
dest="json_output", | ||
type=str, | ||
help="JSON output file", | ||
default=None, | ||
required=True, | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
json_output_location = Path(args.json_output) | ||
if json_output_location.exists(): | ||
sys.stderr.write("Error: --json-output [{json_output_location}] exists!\n") | ||
return 1 | ||
|
||
json_input_file = args.input | ||
|
||
# Handle GZIP and non-GZIP | ||
encoding = guess_type(json_input_file)[1] | ||
open_file = partial(gzip.open, mode="rt") if encoding == "gzip" else open # partial (function pointer) | ||
|
||
with open_file(json_input_file) as input_file: | ||
input_json = json.load(input_file) | ||
|
||
# Flatten metadata: | ||
for sample in input_json["metadata"]["samples"]: | ||
input_json["metadata"]["samples"][sample] = flatten_dictionary(input_json["metadata"]["samples"][sample]) | ||
|
||
json_data = json.dumps(input_json, sort_keys=True, indent=4) | ||
with open(json_output_location, "w") as output_file: | ||
output_file.write(json_data) | ||
|
||
print("Output written to " + str(json_output_location) + "!") | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
process ASSEMBLY_STUB { | ||
tag "$meta.id" | ||
label 'process_single' | ||
|
||
container 'docker.io/python:3.9.17' | ||
|
||
input: | ||
tuple val(meta), path(reads) | ||
|
||
output: | ||
tuple val(meta), path("*.assembly.fa.gz"), emit: assembly | ||
path "versions.yml" , emit: versions | ||
|
||
when: | ||
task.ext.when == null || task.ext.when | ||
|
||
script: | ||
def args = task.ext.args ?: '' | ||
def prefix = task.ext.prefix ?: "${meta.id}" | ||
""" | ||
cat <<-EOF > ${prefix}.assembly.fa | ||
>${meta.id}-stub-assembly | ||
ACGTAACCGGTTAAACCCGGGTTTAAAACCCCGGGGTTTTAAAAACCCCCGGGGGTTTTT | ||
EOF | ||
gzip -n ${prefix}.assembly.fa | ||
cat <<-END_VERSIONS > versions.yml | ||
"${task.process}": | ||
assembly_stub : 0.1.0.dev0 | ||
END_VERSIONS | ||
""" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
process GENERATE_SUMMARY { | ||
label 'process_single' | ||
container 'docker.io/python:3.9.17' | ||
|
||
input: | ||
val summaries | ||
|
||
output: | ||
path("summary.txt.gz"), emit: summary | ||
path "versions.yml", emit: versions | ||
|
||
when: | ||
task.ext.when == null || task.ext.when | ||
|
||
script: | ||
def args = task.ext.args ?: '' | ||
def sorted_summaries = summaries.sort{ it[0].id } | ||
|
||
// Generate summary text: | ||
def summary_text = "IRIDANEXT-EXAMPLE-NF Pipeline Summary\n\nSUCCESS!\n" | ||
|
||
// TODO: Consider the possibility of code injection. | ||
// Should probably be moved to file processing through Python. | ||
for (summary in sorted_summaries) { | ||
summary_text += "\n${summary[0].id}:\n" | ||
summary_text += " reads.1: ${summary[1][0]}\n" | ||
summary_text += " reads.2: ${summary[1][1]}\n" | ||
summary_text += " assembly: ${summary[2]}\n" | ||
} | ||
|
||
version_text = "\"${task.process}\":\n generate_summary : 0.1.0.dev0" | ||
|
||
""" | ||
echo "${summary_text}" > summary.txt | ||
gzip -n summary.txt | ||
echo "${version_text}" > versions.yml | ||
""" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
process SIMPLIFY_IRIDA_JSON { | ||
tag "$meta.id" | ||
label 'process_single' | ||
|
||
container 'docker.io/python:3.9.17' | ||
|
||
input: | ||
tuple val(meta), path(json) | ||
|
||
output: | ||
tuple val(meta), path("*.simple.json.gz") , emit: simple_json | ||
path "versions.yml" , emit: versions | ||
|
||
when: | ||
task.ext.when == null || task.ext.when | ||
|
||
script: | ||
def args = task.ext.args ?: '' | ||
def prefix = task.ext.prefix ?: "${meta.id}" | ||
""" | ||
simplify_irida_json.py \\ | ||
$args \\ | ||
--json-output ${meta.id}.simple.json \\ | ||
${json} | ||
gzip ${meta.id}.simple.json | ||
cat <<-END_VERSIONS > versions.yml | ||
"${task.process}": | ||
simplify_irida_json : 0.1.0.dev0 | ||
END_VERSIONS | ||
""" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -244,5 +244,6 @@ | |
{ | ||
"$ref": "#/definitions/generic_options" | ||
} | ||
] | ||
], | ||
"properties": {} | ||
} |
Oops, something went wrong.