Hi Everyone!!
I am trying to write a nextflow script for bwa alignment, however, I am facing problem in adding read group in bwa mem command. The sample code and error is given below:
- Using 
shellblock 
nextflow.enable.dsl=2
params.raw = "data/*{1,2}.fastq.gz"
params.ref = "results/00_indexes/bwaidx"
params.outdir="results/04_alignments"
process BWAMEM{
    publishDir "$params.outdir/bwa.alignments"
    input:
    tuple val(sample_id), path(reads)
    output:
    path '${sample_id}.sorted.bam'
    shell:
    '''
    id=$(zcat !{reads[0]} | head -n 1 | cut -f 3-4 -d":" | sed 's/@//')
    echo "$id"
    bwa mem -M -R "$(echo "@RG\\tID:${id}\\tSM:!{sample_id}\\tPL:ILLUMINA")" -t 8 !{params.ref} !{reads[0]} !{reads[1]} | samtools sort -@8 -o !{sample_id}.sorted.bam -
    '''
}
reads_ch = Channel.fromFilePairs(params.raw, checkIfExists: true )
workflow {
  BWAMEM(reads_ch)
}
################# Error ###########################
Error executing process > 'BWAMEM (1)'
Caused by:
  Process `BWAMEM (1)` terminated with an error exit status (1)
Command executed:
  id=$(zcat S9_1.fastq.gz | head -n 1 | cut -f 3-4 -d":" | sed 's/@//')
  echo "$id"
  bwa mem -M -R "$(echo "@RG\tID:${id}\tSM:S9\tPL:ILLUMINA")" -t 8 results/00_indexes/bwaidx S9_1.fastq.gz S9_2.fastq.gz | samtools sort -@8 -o S9.sorted.bam -
Command exit status:
  1
Command output:
  000000000-JLT9H:1
Command error:
  [E::bwa_idx_load_from_disk] fail to locate the index files
  samtools sort: failed to read header from "-"
Work dir:
  /home/subudhak/Documents/nxtflow/work/73/7f89163a91a197851e8b84284c192b
Tip: you can replicate the issue by changing to the process work dir and entering the command `bash .command.run`
Even usage of \ didn't help.
Also why do you think that
$idisn't getting substituted by anything in read group?Thanks. Adding
$baseDirworked. Here is the final code: