Skip to content

Commit

Permalink
Add initial qptiff cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjtaylor committed Dec 6, 2023
1 parent 950546a commit 6f91af7
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 6 deletions.
33 changes: 33 additions & 0 deletions bin/clean_qptiff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python

import sys
import tifftools
import re
import os

input = sys.argv[1]

os.rename(input, input + ".original")

date_pattern = r"(<Date>((\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})(\.\d+)?Z?))</Date>"
date_replacement = "<Date />"


sample_pattern = r"<SampleDescription>.+</SampleDescription>"
sample_replacement = "<SampleDescription />"

qptiff_info = tifftools.read_tiff(input + ".original")

for i, ifd in enumerate(qptiff_info["ifds"]):
for tag in ifd["tags"]:
if tag == tifftools.Tag.IMAGEDESCRIPTION:
old_description = ifd["tags"][tifftools.Tag.IMAGEDESCRIPTION.value]["data"]
new_description = re.sub(date_pattern, date_replacement, old_description)
new_description = re.sub(sample_pattern, sample_replacement, new_description)
if new_description != old_description:
print(f"Redacting date in IFD {i}, tag {tag}")
qptiff_info["ifds"][i]["tags"][tifftools.Tag.IMAGEDESCRIPTION.value][
"data"
] = new_description

tifftools.write_tiff(qptiff_info, input, allowExisting=True)
15 changes: 12 additions & 3 deletions bin/clean_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def split_all_ext(filename):
print(new_filename)


unset_list = [
base_unset_list = [
"DateTime",
"NDPI_ScanTime",
"NDPI_WriteTime",
Expand All @@ -46,9 +46,18 @@ def split_all_ext(filename):
"45494"
]

info = tifftools.read_tiff(args.input)
num_ifds = len(info['ifds'])

unset_list_for_all_ifds = []
for ifd in range(num_ifds):
unset_list_for_ifd = [f"{tag},{ifd}" for tag in base_unset_list]
unset_list_for_all_ifds.extend(unset_list_for_ifd)

# Call tiff_set only once after all IFD modifications are specified
tifftools.tiff_set(
args.input,
output=new_filename,
overwrite=False,
unset=unset_list
)
unset=unset_list_for_all_ifds
)
14 changes: 14 additions & 0 deletions modules/clean_qptiff.nf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
process clean_qptiff {
input:
tuple val(meta), file(image)
output:
tuple val(meta), file(image)
stub:
"""
touch image_cleaned.svs
"""
script:
"""
clean_qptiff.py $image
"""
}
17 changes: 17 additions & 0 deletions subworkflows/clean_qptiff.nf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//include { remove_qptiff_label } from "../modules/remove_qptiff_label.nf"
//include { remove_qptiff_macro } from "../modules/remove_qptiff_macro.nf"
include { clean_qptiff } from "../modules/clean_qptiff.nf"


workflow CLEAN_QPTIFF {
take:
images

main:

clean_qptiff(images)
clean_qptiff.out.set{cleaned}

emit:
cleaned
}
2 changes: 2 additions & 0 deletions subworkflows/samplesheet_split.nf
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ workflow SAMPLESHEET_SPLIT {
.branch {
ome: it[1] =~ /.+\.ome\.tif{1,2}$/
svs: it[1] =~ /.+\.svs$/
qptiff: it[1] =~ /.+\.qptiff$/
other: true
}
.set{ images }

emit:
ome = images.ome
svs = images.svs
qptiff = images.qptiff
other = images.other
}
8 changes: 5 additions & 3 deletions workflows/cleaner.nf
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
include { SAMPLESHEET_SPLIT } from '../subworkflows/samplesheet_split.nf'
include { CLEAN_OME } from '../subworkflows/clean_ome.nf'
include { CLEAN_SVS } from '../subworkflows/clean_svs.nf'
include { CLEAN_QPTIFF } from '../subworkflows/clean_qptiff.nf'
include { CLEAN_TIFF } from '../subworkflows/clean_tiff.nf'

workflow CLEANER {
SAMPLESHEET_SPLIT (params.input)
CLEAN_OME ( SAMPLESHEET_SPLIT.out.ome )
CLEAN_SVS ( SAMPLESHEET_SPLIT.out.svs )
CLEAN_OME ( SAMPLESHEET_SPLIT.out.ome )
CLEAN_SVS ( SAMPLESHEET_SPLIT.out.svs )
CLEAN_QPTIFF ( SAMPLESHEET_SPLIT.out.qptiff )
SAMPLESHEET_SPLIT.out.other
.mix ( CLEAN_OME.out.cleaned, CLEAN_SVS.out.cleaned )
.mix ( CLEAN_OME.out.cleaned, CLEAN_SVS.out.cleaned, CLEAN_QPTIFF.out.cleaned )
.set { combined }
CLEAN_TIFF ( combined )
}

0 comments on commit 6f91af7

Please sign in to comment.