Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add index format to samtools view #7481

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
36 changes: 31 additions & 5 deletions modules/nf-core/samtools/view/main.nf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ process SAMTOOLS_VIEW {
tuple val(meta), path(input), path(index)
tuple val(meta2), path(fasta)
path qname
val index_format

output:
tuple val(meta), path("${prefix}.bam"), emit: bam, optional: true
Expand All @@ -20,7 +21,7 @@ process SAMTOOLS_VIEW {
tuple val(meta), path("${prefix}.${file_type}.csi"), emit: csi, optional: true
tuple val(meta), path("${prefix}.${file_type}.crai"), emit: crai, optional: true
tuple val(meta), path("${prefix}.unselected.${file_type}"), emit: unselected, optional: true
tuple val(meta), path("${prefix}.unselected.${file_type}.{bai,csi,crsi}"), emit: unselected_index, optional: true
tuple val(meta), path("${prefix}.unselected.${file_type}.{csi,crai}"), emit: unselected_index, optional: true
path "versions.yml", emit: versions

when:
Expand All @@ -35,16 +36,27 @@ process SAMTOOLS_VIEW {
args.contains("--output-fmt bam") ? "bam" :
args.contains("--output-fmt cram") ? "cram" :
input.getExtension()

output_file = index_format ? "${prefix}.${file_type}##idx##${prefix}.${file_type}.${index_format} --write-index" : "${prefix}.${file_type}"
// Can't choose index type of unselected file
readnames = qname ? "--qname-file ${qname} --output-unselected ${prefix}.unselected.${file_type}": ""

if ("$input" == "${prefix}.${file_type}") error "Input and output names are the same, use \"task.ext.prefix\" to disambiguate!"
if (index_format) {
if (!index_format.matches('bai|csi|crai')) {
error "Index format not one of bai, csi, crai."
} else if (file_type == "sam") {
error "Indexing not compatible with SAM output"
}
}
"""
samtools \\
view \\
--threads ${task.cpus-1} \\
${reference} \\
${readnames} \\
$args \\
-o ${prefix}.${file_type} \\
-o ${output_file} \\
$input \\
$args2

Expand All @@ -61,13 +73,27 @@ process SAMTOOLS_VIEW {
args.contains("--output-fmt bam") ? "bam" :
args.contains("--output-fmt cram") ? "cram" :
input.getExtension()
if ("$input" == "${prefix}.${file_type}") error "Input and output names are the same, use \"task.ext.prefix\" to disambiguate!"

index = args.contains("--write-index") ? "touch ${prefix}.${file_type}.csi" : ""
default_index_format =
file_type == "bam" ? "csi" :
file_type == "cram" ? "crai" : ""
index = index_format ? "touch ${prefix}.${file_type}.${index_format}" : args.contains("--write-index") ? "touch ${prefix}.${file_type}.${default_index_format}" : ""
unselected = qname ? "touch ${prefix}.unselected.${file_type}" : ""
// Can't choose index type of unselected file
unselected_index = qname && (args.contains("--write-index") || index_format) ? "touch ${prefix}.unselected.${file_type}.${default_index_format}" : ""

if ("$input" == "${prefix}.${file_type}") error "Input and output names are the same, use \"task.ext.prefix\" to disambiguate!"
if (index_format) {
if (!index_format.matches('bai|csi|crai')) {
error "Index format not one of bai, csi, crai."
} else if (file_type == "sam") {
error "Indexing not compatible with SAM output."
}
}
"""
touch ${prefix}.${file_type}
${index}
${unselected}
${unselected_index}

cat <<-END_VERSIONS > versions.yml
"${task.process}":
Expand Down
8 changes: 6 additions & 2 deletions modules/nf-core/samtools/view/meta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ input:
type: file
description: Optional file with read names to output only select alignments
pattern: "*.{txt,list}"
- - index_format:
type: string
description: Index format, used together with ext.args = '--write-index'
pattern: "*.{bai,csi,crai}"
output:
- bam:
- meta:
Expand Down Expand Up @@ -120,10 +124,10 @@ output:
description: |
Groovy Map containing sample information
e.g. [ id:'test', single_end:false ]
- ${prefix}.unselected.${file_type}.{bai,csi,crsi}:
- ${prefix}.unselected.${file_type}.{csi,crai}:
type: file
description: index for the "unselected" file
pattern: "*.unselected.{bai,csi,crai}"
pattern: "*.unselected.{csi,crai}"
- versions:
- versions.yml:
type: file
Expand Down
3 changes: 3 additions & 0 deletions modules/nf-core/samtools/view/tests/cram_index.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
process {
ext.args = "--output-fmt cram --write-index"
}
Loading
Loading