Hi,
The variable in your loop, $BAM, is not defined (you defined ‘$bam’). When you run the command one at a time you are not using this variable, but handcoding the outputfile. Just use the appropriate file name, which is stored in $sample, or use the proper case for the variable name:
$ cat popmap.tsv | cut -f 1 | while read sample; do bwa mem ./vcor_db/vcor_db ./samples/${sample}.1.fq.gz ./samples/${sample}.2.fq.gz | samtools view -b -h | samtools sort -o "${sample}.bam"; done
Or
$ cat popmap.tsv | cut -f 1 | while read sample; do fq1=./samples/${sample}.1.fq.gz; fq2=./samples/${sample}.2.fq.gz; bam=./aligned_samples2/${sample}.bam | bwa mem ./vcor_db/vcor_db "$fq1" "$fq2" | samtools view -b -h | samtools sort -o "$bam"; done
Cheers,
julian
Right, sorry about that. The first version of the command I gave will work. Also, looking at your code again, it wasn’t the case of the variable name (I somehow imagined that), but instead you have an extra pipe (fixed in red). It should read:
$ cat popmap.tsv | cut -f 1 | while read sample; do fq1=./samples/${sample}.1.fq.gz; fq2=./samples/${sample}.2.fq.gz; bam=./aligned_samples2/${sample}.bam; bwa mem ./vcor_db/vcor_db "$fq1" "$fq2" | samtools view -b -h | samtools sort -o "$bam"; done
From: stacks...@googlegroups.com <stacks...@googlegroups.com> on behalf of Ira Herniter <iher...@gmail.com>
Date: Monday, December 13, 2021 at 1:56 PM
To: Stacks <stacks...@googlegroups.com>
Subject: Re: [stacks] Re: samtools sort fail to open file
Hi Julian,
Thanks for the quick response though I'm not sure I follow. My initial code is exactly the same are your second code. I don't have a variable labelled "$BAM" unless I'm missing something.
Thanks,
Ira
On Monday, December 13, 2021 at 12:52:43 PM UTC-5 jcatchen wrote:
Hi,
The variable in your loop, $BAM, is not defined (you defined ‘$bam’). When you run the command one at a time you are not using this variable, but handcoding the outputfile. Just use the appropriate file name, which is stored in $sample, or use the proper case for the variable name:
$ cat popmap.tsv | cut -f 1 | while read sample; do bwa mem ./vcor_db/vcor_db ./samples/${sample}.1.fq.gz ./samples/${sample}.2.fq.gz | samtools view -b -h | samtools sort -o "./aligned_samples2/${sample}.bam"; done
Or