Home > Software design >  Using bash how to iterate through lines in a txt file and sequentially pair up every two lines
Using bash how to iterate through lines in a txt file and sequentially pair up every two lines

Time:01-24

Hi I am attempting to use bash to iterate through a .txt file which contains the following lines. This is a smaller subset of the full list of fastq files, but all samples follow the same patterns.

/path/10-xxx-111-sample_S1_R1.fastq.gz
/path/10-xxx-111-sample_S1_R2.fastq.gz
/path/12-xxx-222-sample_S2_R1.fastq.gz
/path/12-xxx-222-sample_S2_R2.fastq.gz
/path/13-xxx-333-sample_S3_R1.fastq.gz
/path/13-xxx-333-sample_S3_R2.fastq.gz

And the aim is to pair every two lines and use the paths to provide information to further code in bash.

bwa mem ${index} ${r1} ${r2} -M -t 8 \
-R "@RG\tID:FlowCell.${name}\tSM:${name}\tPL:illumina\tLB:${Job}.${name}"   | \
samtools sort -O bam -o ${bam}/${name}_bwa_output.bam

The first R1 and R2 should should correspond to ${r1} and ${r2} respectively, and in a sequential order.

The ${name}'s are contained in another file and consist of "10-xxx-111-sample_S1_" type information.

Any help in iterating through this text file to inform the downstream code would be really appreciated.

Intended output: First two lines of the .txt file will inform downstream code. e.g.

bwa mem ${index} /path/10-xxx-111-sample_S1_R1.fastq.gz /path/10-xxx-111-sample_S1_R2.fastq.gz -M -t 8 \ 

Following this, the next two lines will inform the downstream code and so forth. e.g.

bwa mem ${index} /path/12-xxx-222-sample_S2_R1.fastq.gz /path/12-xxx-222-sample_S2_R2.fastq.gz -M -t 8 \ 

CodePudding user response:

Why not reading 2 lines at a time? Remove echo before bwa... when you'll be satisfied with the result.

$ cat myScript.sh
#!/usr/bin/env bash

while IFS= read -r r1 && IFS= read -r r2; do
  echo bwa mem "${index}" "$r1" "$r2" -M -t 8 ...
done < fastq_filepaths.txt
$ index=myIndex ./myScript.sh 
bwa mem myIndex /path/10-xxx-111-sample_S1_R1.fastq.gz /path/10-xxx-111-sample_S1_R2.fastq.gz -M -t 8 ...
bwa mem myIndex /path/12-xxx-222-sample_S2_R1.fastq.gz /path/12-xxx-222-sample_S2_R2.fastq.gz -M -t 8 ...
bwa mem myIndex /path/13-xxx-333-sample_S3_R1.fastq.gz /path/13-xxx-333-sample_S3_R2.fastq.gz -M -t 8 ...
  •  Tags:  
  • Related