I agree with what Steve said, but
path("${prefix}.bam{,.bai}")
might be problematic, it is my understanding that if the index part failed and bai file are missing, the output will still execute without error.