-
Notifications
You must be signed in to change notification settings - Fork 0
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 #13 from BCCDC-PHL/hostile-dehost
Add optional dehosting with bede/hostile
- Loading branch information
Showing
7 changed files
with
349 additions
and
30 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,123 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import argparse | ||
import csv | ||
import json | ||
import sys | ||
|
||
from pathlib import Path | ||
|
||
def parse_fastp_csv(fastp_csv_path: Path): | ||
""" | ||
""" | ||
fastp_report = {} | ||
int_fields = [ | ||
'total_reads_before_filtering', | ||
'total_reads_after_filtering', | ||
'total_bases_before_filtering', | ||
'total_bases_after_filtering', | ||
'read1_mean_length_before_filtering', | ||
'read1_mean_length_after_filtering', | ||
'read2_mean_length_before_filtering', | ||
'read2_mean_length_after_filtering', | ||
'q20_bases_before_filtering', | ||
'q30_bases_before_filtering', | ||
'q20_bases_after_filtering', | ||
'q30_bases_after_filtering', | ||
'adapter_trimmed_reads', | ||
'adapter_trimmed_bases', | ||
|
||
] | ||
float_fields = [ | ||
'q20_rate_before_filtering', | ||
'q30_rate_before_filtering', | ||
'q20_rate_after_filtering', | ||
'q30_rate_after_filtering', | ||
'gc_content_before_filtering', | ||
'gc_content_after_filtering', | ||
] | ||
with open(fastp_csv_path, 'r') as f: | ||
reader = csv.DictReader(f) | ||
for row in reader: | ||
for field, value in row.items(): | ||
if field in int_fields: | ||
try: | ||
fastp_report[field] = int(value) | ||
except ValueError as e: | ||
fastp_report[field] = None | ||
elif field in float_fields: | ||
try: | ||
fastp_report[field] = float(value) | ||
except ValueError as e: | ||
fastp_report[field] = None | ||
else: | ||
fastp_report[field] = value | ||
|
||
return fastp_report | ||
|
||
|
||
def combine_fastp_reports(pre_dehosting_report, post_dehosting_report): | ||
""" | ||
""" | ||
combined_report = {} | ||
|
||
combined_report['sample_id'] = pre_dehosting_report['sample_id'] | ||
|
||
for key, value in pre_dehosting_report.items(): | ||
if key.endswith('before_filtering'): | ||
combined_report[key] = value | ||
for key, value in post_dehosting_report.items(): | ||
if key.endswith('after_filtering'): | ||
combined_report[key] = value | ||
|
||
pre_dehosting_adapter_trimmed_reads = pre_dehosting_report.get('adapter_trimmed_reads', 0) | ||
post_dehosting_adapter_trimmed_reads = post_dehosting_report.get('adapter_trimmed_reads', 0) | ||
combined_report['adapter_trimmed_reads'] = pre_dehosting_adapter_trimmed_reads + post_dehosting_adapter_trimmed_reads | ||
|
||
pre_dehosting_adapter_trimmed_bases = pre_dehosting_report.get('adapter_trimmed_bases', 0) | ||
post_dehosting_adapter_trimmed_bases = post_dehosting_report.get('adapter_trimmed_bases', 0) | ||
combined_report['adapter_trimmed_bases'] = pre_dehosting_adapter_trimmed_bases + post_dehosting_adapter_trimmed_bases | ||
|
||
return combined_report | ||
|
||
|
||
def main(args): | ||
pre_dehosting_report = parse_fastp_csv(Path(args.pre_dehosting)) | ||
post_dehosting_report = parse_fastp_csv(Path(args.post_dehosting)) | ||
|
||
combined_report = combine_fastp_reports(pre_dehosting_report, post_dehosting_report) | ||
|
||
output_fieldnames = [ | ||
'sample_id', | ||
'total_reads_before_filtering', | ||
'total_reads_after_filtering', | ||
'total_bases_before_filtering', | ||
'total_bases_after_filtering', | ||
'read1_mean_length_before_filtering', | ||
'read1_mean_length_after_filtering', | ||
'read2_mean_length_before_filtering', | ||
'read2_mean_length_after_filtering', | ||
'q20_bases_before_filtering', | ||
'q30_bases_after_filtering', | ||
'q20_rate_before_filtering', | ||
'q20_rate_after_filtering', | ||
'q30_bases_before_filtering', | ||
'q30_bases_after_filtering', | ||
'q30_rate_before_filtering', | ||
'q30_rate_after_filtering', | ||
'gc_content_before_filtering', | ||
'gc_content_after_filtering', | ||
'adapter_trimmed_reads', | ||
'adapter_trimmed_bases', | ||
] | ||
writer = csv.DictWriter(sys.stdout, fieldnames=output_fieldnames, dialect='unix', quoting=csv.QUOTE_MINIMAL, extrasaction='ignore') | ||
writer.writeheader() | ||
writer.writerow(combined_report) | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--pre-dehosting', required=True, help='Path to pre-dehosting fastp report') | ||
parser.add_argument('--post-dehosting', required=True, help='Path to post-dehosting fastp report') | ||
args = parser.parse_args() | ||
main(args) |
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,8 @@ | ||
name: basic-sequence-qc-dehosting | ||
channels: | ||
- conda-forge | ||
- bioconda | ||
- defaults | ||
dependencies: | ||
- python=3 | ||
- hostile=1.1.0 |
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,48 @@ | ||
process dehost { | ||
|
||
tag { sample_id } | ||
|
||
publishDir "${params.outdir}/${sample_id}", pattern: "${sample_id}_dehosted_R{1,2}.fastq.gz", mode: 'copy' | ||
publishDir "${params.outdir}/${sample_id}", pattern: "${sample_id}_hostile.log.json", mode: 'copy' | ||
|
||
input: | ||
tuple val(sample_id), path(reads) | ||
|
||
output: | ||
tuple val(sample_id), path("${sample_id}_hostile.log.json"), emit: hostile_log | ||
tuple val(sample_id), path("${sample_id}_dehosted*.fastq.gz"), emit: dehosted_reads | ||
|
||
script: | ||
""" | ||
hostile clean \ | ||
--threads ${task.cpus} \ | ||
--fastq1 ${reads[0]} \ | ||
--fastq2 ${reads[1]} \ | ||
--index ${params.dehosting_index} \ | ||
--out-dir . \ | ||
> ${sample_id}_hostile.log.json | ||
mv ${sample_id}*.clean_1.fastq.gz ${sample_id}_dehosted_R1.fastq.gz | ||
mv ${sample_id}*.clean_2.fastq.gz ${sample_id}_dehosted_R2.fastq.gz | ||
""" | ||
} | ||
|
||
|
||
process combine_fastp_reports { | ||
|
||
tag { sample_id } | ||
|
||
input: | ||
tuple val(sample_id), path(fastp_pre_dehosting), path(fastp_post_dehosting) | ||
|
||
output: | ||
tuple val(sample_id), path("${sample_id}_fastp_combined.csv"), emit: metrics | ||
|
||
script: | ||
""" | ||
combine_fastp_reports.py \ | ||
--pre-dehosting ${fastp_pre_dehosting} \ | ||
--post-dehosting ${fastp_post_dehosting} \ | ||
> ${sample_id}_fastp_combined.csv | ||
""" | ||
} |
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
Oops, something went wrong.