In the below bash the two variables $bname and $sample are extracted correctly however when I pass them to the java command $bname is set to file:///home/cmccabe/NA12878.bam and an exception is thrown in the picard command. I can only guess that $sample is set to ///home/cmccabe/NA12878. I am not sure what I am doing wrong, can a bash variable not be used in java or is there something else? Thank you :)
input
/home/cmccabe/Desktop/fastq/NA12878.bam
result of echo
The bam is NA12878.bam --- this is $bname
The matching sample is NA12878 --- this is $sample
bash
for file in /home/cmccabe/Desktop/fastq/*.bam
do
bname=`basename $file`
echo "The bam file is:" $bname
sample=$(basename $file .bam | cut -d- -f1)
echo "The matching sample is:"$sample
java -XX:ParallelGCThreads=16 -jar /home/cmccabe/Desktop/fastq/picard/build/libs/picard.jar MarkDuplicates \
I=$bname \
O=${sample}_marked_duplicates.bam \
M=${sample}_marked_dup_metrics.txt
done
2 answers
The problem is not totally with your usage of variables. You're pointing Picard to the wrong location for the BAM (from the Picard output you pasted in the comment):
Exception in thread "main" htsjdk.samtools.SAMException: Cannot read non-existent file: file:///home/cmccabe/NA12878.bam
In your picard command, change this:
I=$bname
to this:
I=/home/cmccabe/Desktop/fastq/$bname
And that should work for you.
$bname is not being set to file:///home/cmccabe/NA12878.bam, your output shows it clearly:
The bam file is: NA12878.bam
And
INPUT=[NA12878.bam] OUTPUT=NA12878_marked_duplicates.bam
The problem is where you are running the script: it will only work if you run on the same folder the am files are located. What you want is:
I=$file \
Or cd into the folder where the bams are located.
Log in to answer this question.
Can you paste the error thrown by Picard?
Sorry, I highlighted the variables and error. Thank you :).
Word to the wise, you should quote your variables too:
"${var}"To stop nasty unexpected parameter expansions.
try replacing
$bnamewith$fileor${file}@ cmccabe