Skip to content

Commit

Permalink
add cram support to bowtie2 (nf-core#5180)
Browse files Browse the repository at this point in the history
* add cram support to bowtie2

* fix tests

* fastq_align_bowtie2 > nf-test

* fix tests

* more robust test

* fix cram tests
  • Loading branch information
matthdsm authored Mar 19, 2024
1 parent 176c11c commit 0fe3083
Show file tree
Hide file tree
Showing 13 changed files with 507 additions and 266 deletions.
26 changes: 22 additions & 4 deletions modules/nf-core/bowtie2/align/main.nf
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ process BOWTIE2_ALIGN {
input:
tuple val(meta) , path(reads)
tuple val(meta2), path(index)
tuple val(meta3), path(fasta)
val save_unaligned
val sort_bam

output:
tuple val(meta), path("*.{bam,sam}"), emit: aligned
tuple val(meta), path("*.sam") , emit: sam , optional:true
tuple val(meta), path("*.bam") , emit: bam , optional:true
tuple val(meta), path("*.cram") , emit: cram , optional:true
tuple val(meta), path("*.csi") , emit: csi , optional:true
tuple val(meta), path("*.crai") , emit: crai , optional:true
tuple val(meta), path("*.log") , emit: log
tuple val(meta), path("*fastq.gz") , emit: fastq, optional:true
tuple val(meta), path("*fastq.gz") , emit: fastq , optional:true
path "versions.yml" , emit: versions

when:
Expand All @@ -39,7 +44,10 @@ process BOWTIE2_ALIGN {

def samtools_command = sort_bam ? 'sort' : 'view'
def extension_pattern = /(--output-fmt|-O)+\s+(\S+)/
def extension = (args2 ==~ extension_pattern) ? (args2 =~ extension_pattern)[0][2].toLowerCase() : "bam"
def extension_matcher = (args2 =~ extension_pattern)
def extension = extension_matcher.getCount() > 0 ? extension_matcher[0][2].toLowerCase() : "bam"
def reference = fasta && extension=="cram" ? "--reference ${fasta}" : ""
if (!fasta && extension=="cram") error "Fasta reference is required for CRAM output"

"""
INDEX=`find -L ./ -name "*.rev.1.bt2" | sed "s/\\.rev.1.bt2\$//"`
Expand All @@ -53,7 +61,7 @@ process BOWTIE2_ALIGN {
$unaligned \\
$args \\
2> >(tee ${prefix}.bowtie2.log >&2) \\
| samtools $samtools_command $args2 --threads $task.cpus -o ${prefix}.${extension} -
| samtools $samtools_command $args2 --threads $task.cpus ${reference} -o ${prefix}.${extension} -
if [ -f ${prefix}.unmapped.fastq.1.gz ]; then
mv ${prefix}.unmapped.fastq.1.gz ${prefix}.unmapped_1.fastq.gz
Expand Down Expand Up @@ -82,9 +90,19 @@ process BOWTIE2_ALIGN {
} else {
create_unmapped = save_unaligned ? "touch ${prefix}.unmapped_1.fastq.gz && touch ${prefix}.unmapped_2.fastq.gz" : ""
}
def reference = fasta && extension=="cram" ? "--reference ${fasta}" : ""
if (!fasta && extension=="cram") error "Fasta reference is required for CRAM output"

def create_index = ""
if (extension == "cram") {
create_index = "touch ${prefix}.crai"
} else if (extension == "bam") {
create_index = "touch ${prefix}.csi"
}

"""
touch ${prefix}.${extension}
${create_index}
touch ${prefix}.bowtie2.log
${create_unmapped}
Expand Down
5 changes: 5 additions & 0 deletions modules/nf-core/bowtie2/align/tests/cram_crai.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
process {
withName: BOWTIE2_ALIGN {
ext.args2 = '--output-fmt cram --write-index'
}
}
Loading

0 comments on commit 0fe3083

Please sign in to comment.