extract part of the sample name (file name) and run bwa mem for all samples using loop script
1
0
Entering edit mode
8.0 years ago
Ati ▴ 10

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 • 3.2k views
ADD COMMENT
0
Entering edit mode
8.0 years ago

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.

ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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?

ADD REPLY
0
Entering edit mode

'@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

ADD REPLY
0
Entering edit mode

Is there any way I can write the loop?

ADD REPLY
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 1996 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6