This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Split Fastq Paired-Read Using Awk

Hi guys,

I have a fastq file that contains paired reads. I know that F3 and R3 tag beginning with G and T, respectively in second line. I want split the file in R3.fastq and F3.fastq using awk, but I can not. Anybody help me?

@745_15_830
G..13...10...033..3323.331..131..3.0..3311.333....2
+
!!@@!!!8'!!!A%@!!@A'B!&3:!!?A&!!B!<!!B?(&!B=7!!!!%
@745_15_830
T103223.0303002200023123021022000003020..301233..0.
+
6+%'<(!%-?B-)4%=(<(?+&%%)-)%%91%-)<*5)!!4%%&A2!!%!
@745_15_908
G..12...032..030..1120.311..130..3.1..0233.231....1
+
!!-;!!!%*@!!@A1!!A57%!0+A!!4=<!!@!/!!-'*@!(1A!!!!4
@745_15_908
T303313.2231300002222330212112022210120..311031..0.
+
&@B,(*!,'/1B%%)%=&'(9*%0&0:&8%&&&.')>)!!;:3(%2!!0!
split fastq awk

1 answer

If you are sure that pair F3 is ALWAYS written before pair G3, you can try this :

awk 'BEGIN{count=0}{count++; if(count<=4) {print $0 > "F3.fastq"} else {print $0 > "R3.fastq" } if(count==8) count=0 }' input.fastq

Nice Awk command. It could be improved a bit:

awk '{++count ; if (count<=4) {print > "F3.fastq"} else {print > "R3.fastq" } if (count==8) count=0 }' input.fastq

You don't need to specify $0, and you do not need to initialize the variable (count).

Thanks so much! Just a correction: F3 beginning with base T and R3 beginning with G!

Log in to answer this question.