0

I am new to bash scripting. I have written a small script containing set of commands using defined declared variables (e.g. SAMPLENAME=Alex) . Now, what I want is to loop this script with different values of declared variables (e.g SAMPLENAME=John) from a file everytime.

For example: I have set following values of variables

#!/bin/bash
tID=000H003HG.TAAGGCGA+GCGATCTA
tSM=Alex
tLB=lib1
O_FOLDER_NAME=HD690_Alex

This is the command which will be executed using abolve values of variables,

bwa mem -V -M -R "@RG\tID:${tID}\tSM:${tSM}\tPL:ILLUMINA\tLB:${tLB}"       REFERENCES/$REFERENCE.fa "<zcat    OUTPUT/2_TRIMMED_DATA/$O_FOLDER_NAME/split-adapter-quality-trimmed/${O_FOLDER_NAME}-READ1.fastq.gz" "<zcat OUTPUT/2_TRIMMED_DATA/${O_FOLDER_NAME}/split-adapter-quality-trimmed/${O_FOLDER_NAME}-READ2.fastq.gz" > OUTPUT/3_MAPPED_READS/${O_FOLDER_NAME}/aligned_reads.sam

Now after the execution of above command, I want it to loop with following different set of values for declared variables,

#!/bin/bash
tID=000998U3HG.STPUIHY+UIYUSIA
tSM=John
tLB=lib2
O_FOLDER_NAME=HD700_John

Thanks!

2 Answers 2

1

If you have a whitespace-separated file with the fields tID, tSM, tLB, and O_FOLDER_NAME, just read those.

while read -r tID tSM tLB O_FOLDER_NAME; do
    bwa mem -V -M \
      -R "@RG\tID:${tID}\tSM:${tSM}\tPL:ILLUMINA\tLB:${tLB}" \
     REFERENCES/"$REFERENCE".fa \
     "<zcat    OUTPUT/2_TRIMMED_DATA/$O_FOLDER_NAME/split-adapter-quality-trimmed/${O_FOLDER_NAME}-READ1.fastq.gz" \
     "<zcat OUTPUT/2_TRIMMED_DATA/${O_FOLDER_NAME}/split-adapter-quality-trimmed/${O_FOLDER_NAME}-READ2.fastq.gz" \
      > OUTPUT/3_MAPPED_READS/"${O_FOLDER_NAME}"/aligned_reads.sam
done <file

If your input file is in CSV format, you will need to fiddle with IFS or something, but this is the basic principle.

For your two examples, the file could contain the following.

000H003HG.TAAGGCGA+GCGATCTA Alex lib1 HD690_Alex
000998U3HG.STPUIHY+UIYUSIA  John lib2 HD700_John

If the file has no use outside of your script, you might want to use a here document instead.

Sign up to request clarification or add additional context in comments.

2 Comments

The "<zcat ..." things look really weird; do you mean $(zcat ...)?
So, what I comprehended from your answer is that all I have to do is to create a whitespace-seperated file . E.g tID tSM tLB and their different values below this line? or in front of this line? After the execution of script for once, bash script will call another set of values from the file? Kindly correct me if I'm misunderstanding any point.
0

A way to do it is to change the file of variables to be -

#!/bin/bash
echo tID=000H003HG.TAAGGCGA+GCGATCTA
echo tSM=Alex
echo tLB=lib1
echo O_FOLDER_NAME=HD690_Alex

and then run -

eval $(./var_file.sh) && your command

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.