From d675d96d81bfdec8e8e54719e1c7216860b1906e Mon Sep 17 00:00:00 2001 From: replikation Date: Wed, 19 Aug 2020 00:01:48 +0200 Subject: [PATCH] way better samtools error fail save --- modules/samtools.nf | 56 +++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/modules/samtools.nf b/modules/samtools.nf index 2ee9a6a..12adfa2 100644 --- a/modules/samtools.nf +++ b/modules/samtools.nf @@ -1,29 +1,51 @@ process samtools { - publishDir "${params.output}/${name}/phage_positive_contigs", mode: 'copy', pattern: "${name}_positive_contigs.fa" - label 'samtools' + publishDir "${params.output}/${name}/phage_positive_contigs", mode: 'copy', pattern: "${name}_positive_contigs.fa" + label 'samtools' input: - tuple val(name), file(file), file(list) + tuple val(name), file(file), file(list) output: - tuple val(name), file("${name}_positive_contigs.fa") + tuple val(name), file("${name}_positive_contigs.fa") script: - """ - cat ${list} | sort | uniq > tmp_allctgs.txt - cat ${file} > all.fasta - xargs samtools faidx all.fasta < tmp_allctgs.txt > ${name}_positive_contigs.fa - """ + """ + cat ${list} | sort | uniq > tmp_allctgs.txt + cat ${file} > all.fasta + + # get samtools but ignore "samtool fails" + while read fastaheader; do + if grep -qw ">\$fastaheader" all.fasta; then + samtools faidx all.fasta \$fastaheader >> ${name}_positive_contigs.fa + else + echo "\$fastaheader not found" >> error_code_samtools.txt + fi + done < tmp_allctgs.txt + + # old command + # xargs samtools faidx all.fasta < tmp_allctgs.txt > ${name}_positive_contigs.fa + """ } process samtools_fastq { - publishDir "${params.output}/${name}/phage_positive_contigs", mode: 'copy', pattern: "${name}_positive_contigs.fa" - label 'samtools' + publishDir "${params.output}/${name}/phage_positive_contigs", mode: 'copy', pattern: "${name}_positive_contigs.fa" + label 'samtools' input: - tuple val(name), file(file), file(list) + tuple val(name), file(file), file(list) output: - tuple val(name), file("${name}_positive_contigs.fa") + tuple val(name), file("${name}_positive_contigs.fa") script: - """ - cat ${list} | sort | uniq > tmp_allctgs.txt - cat ${file} > all.fasta - xargs samtools faidx all.fasta < tmp_allctgs.txt > ${name}_positive_contigs.fa + """ + cat ${list} | sort | uniq > tmp_allctgs.txt + cat ${file} > all.fasta + + # get samtools but ignore "samtool fails" + while read fastaheader; do + if grep -qw ">\$fastaheader" all.fasta; then + samtools faidx all.fasta \$fastaheader >> ${name}_positive_contigs.fa + else + echo "\$fastaheader not found" >> error_code_samtools.txt + fi + done < tmp_allctgs.txt + + # old command + # xargs samtools faidx all.fasta < tmp_allctgs.txt > ${name}_positive_contigs.fa """ } \ No newline at end of file