This is a test version of Biostars. For the public version, visit https://www.biostars.org.
extract part of the sample name (file name) and run bwa mem for all samples using loop script

I'm new in bash scripting. I have some sample files in different directories and wanted to extract part of the filename or directory name and create a variable with that part and apply this variable on the other command. My directory and file name follow this pattern:

user/Sample_001_002_5/001_002_5_fgdfgh_hgf00_R1.fastq.gz

user/Sample_254_128_3/254_128_3_wserer_hgf00_R2.fastq.gz

May I know how I can setup a variable such as i with the number part of the directory name

i=001_002_5 

i=254_128_3

I wrote bellow script but i is not recognized in bwa mem command:

dir=/users/Sample_*

for DIR in $dir

do i= basename ${DIR} | cut -c 8-

echo $i               # run correctly#

R1="${DIR}/${i}*R1.fastq.gz"   # Run correctly#

R2="${DIR}/${i}*R2.fastq.gz"   # run  correctly#

BWA mem -M -t 24 -R '@RG\tID:UW\tSM:'${i}'\tPL:Illumina\tPU:'${i} ${Ref} $R1 $R2 > ${DIR}/BAM/TSH_${i}_aln.sam      

# i is not recoginzed here#

done
next-gen

1 answer

Well today you discover that not all quotes are equal ;-) Could you try again the BWA mem command with double quotes? R1="${DIR}/${i}*R1.fastq.gz", but R1='${DIR}/${i}*R1.fastq.gz' doesn't.

Thanks for reply. But it wasn't the issue. Is there any way I can write the loop?

Then what's the issue? Can you check with echo '@RG\tID:UW\tSM:'${i}'\tPL:Illumina\tPU:'${i} ${Ref} $R1 $R2 > ${DIR}/BAM/TSH_${i}_aln.sam to confirm?

'@RG\tID:UW\tSM:''\tPL:Illumina\tPU:' > /users/az/Sample_697_876_4/BAM/TSH__aln.sam

#it couldn't substitute i with its value: here is should be 697_876_4 and I need to get:

'@RG\tID:UW\tSM:697-876_4''\tPL:Illumina\tPU:697_876_4' > /users/az/Sample_697_876_4/BAM/TSH_697_876_4_aln.sam

Is there any way I can write the loop?

Please use ADD COMMENT/ADD REPLY when responding to existing posts to keep threads logically organized.

Log in to answer this question.