Skip to content

Commit 460c7c8

Browse files
Alexander PeltzerAlexander Peltzer
authored andcommitted
Add better bam filtering / extraction protocol
1 parent 077d4ac commit 460c7c8

2 files changed

Lines changed: 21 additions & 19 deletions

File tree

main.nf

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ def helpMessage() {
7676
--bwamem Turn on BWA Mem instead of CM/BWA aln for mapping
7777
7878
BAM Filtering
79-
--bam_analyse_mapped_only Only consider mapped reads for downstream analysis. Unmapped reads are extracted to separate output.
80-
--bam_retain_all_reads Keep all reads in BAM file for downstream analysis
79+
--bam_retain_unmapped Keep all reads in BAM file for downstream analysis
8180
--bam_mapping_quality_threshold Minimum mapping quality for reads filter
8281
8382
DeDuplication
@@ -173,11 +172,14 @@ params.circularfilter = false
173172
params.bwamem = false
174173

175174
//BAM Filtering steps (default = keep mapped and unmapped in BAM file)
176-
params.bam_analyse_mapped_only = false
177-
params.bam_keep_all = true
178-
params.bam_retain_all_reads = false
175+
params.bam_retain_unmapped = false
176+
params.bam_discard_unmapped = false
177+
params.bam_unmapped_to_fastq = false
178+
params.bam_unmapped_keep_type = 'bam'
179+
179180
params.bam_mapping_quality_threshold = 0
180181

182+
181183
//DamageProfiler settings
182184
params.damageprofiler_length = 100
183185
params.damageprofiler_threshold = 15
@@ -715,23 +717,24 @@ process samtools_filter {
715717
file "*.unmapped.bam" optional true
716718
file "*.bai"
717719

718-
when: "${params.bam_retain_all_reads}"
719-
720720
script:
721721
prefix="$bam" - ~/(\.bam)?/
722-
rm_unmapped = "${params.bam_discard_unmapped_entirely}" ? 'rm *.unmapped.*' : ''
722+
rm_type = "${params.bam_unmapped_keep_type}" == 'bam' ? 'bam' : 'fastq'
723+
rm_unmapped = "${params.bam_discard_unmapped}" ? "rm *.unmapped.${rm_type}" : ''
724+
fq_convert = "${params.bam_unmapped_to_fastq}" ? "samtools fastq -tn "${prefix}.unmapped.bam" | gzip > "${prefix}.unmapped.fq.gz"" :
725+
723726

724-
if("${params.bam_analyse_mapped_only}"){
725-
"""
726-
samtools view -h $bam | tee >(samtools view - -@ ${task.cpus} -f4 -q ${params.bam_mapping_quality_threshold} -o ${prefix}.unmapped.bam) >(samtools view - -@ ${task.cpus} -F4 -q ${params.bam_mapping_quality_threshold} -o ${prefix}.filtered.bam)
727-
samtools fastq -tn "${prefix}.unmapped.bam" | gzip > "${prefix}.unmapped.fq.gz"
728-
samtools index ${prefix}.filtered.bam
729-
"""
727+
if("${params.bam_separate_unmapped}"){
728+
"""
729+
samtools view -h $bam | tee >(samtools view - -@ ${task.cpus} -f4 -q ${params.bam_mapping_quality_threshold} -o ${prefix}.unmapped.bam) >(samtools view - -@ ${task.cpus} -F4 -q ${params.bam_mapping_quality_threshold} -o ${prefix}.filtered.bam)
730+
samtools index ${prefix}.filtered.bam
731+
$fq_convert
732+
"""
730733
} else {
731-
"""
732-
samtools view -h $bam | tee >(samtools view - -@ ${task.cpus} -f4 -q ${params.bam_mapping_quality_threshold} -o ${prefix}.unmapped.bam) >(samtools view - -@ ${task.cpus} -q ${params.bam_mapping_quality_threshold} -o ${prefix}.filtered.bam)
733-
samtools index ${prefix}.filtered.bam
734-
"""
734+
"""
735+
samtools view -h $bam -@ ${task.cpus} -q ${params.bam_mapping_quality_threshold} -o ${prefix}.filtered.bam)
736+
samtools index ${prefix}.filtered.bam
737+
"""
735738
}
736739
}
737740

nextflow.config

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ params {
2727
complexity_filter = false
2828
complexity_filter_poly_g_min = 10
2929
trim_bam = false
30-
bam_discard_unmapped_entirely = false
3130

3231
// AWS Batch
3332
awsqueue = false

0 commit comments

Comments
 (0)