2

I have fastq files from RNA-sequencing, in which I want to append part of each 1st line and an extra string of text to the next line.

These are the first 8 lines of my file:

@V300088128L1C001R0010000004#TCCTCGCATG/1
TTCAAGTAATCCAGGATAGGCT
+
FFDFFFFFFFFGFFFFFFFFFF
@V300088128L1C001R0010000019#CTGATATAGA/1
GTGGATCACTTGGCTCGGGA
+
F:FFFGF?FGGFFFFGFFEE

I want to copy the part between # and /1 from every line starting with @ to the end of the next line, followed by the string AGTCGGAGGC. The result should be:

@V300088128L1C001R0010000004#TCCTCGCATG/1
TTCAAGTAATCCAGGATAGGCTTCCTCGCATGAGTCGGAGGC
+
FFDFFFFFFFFGFFFFFFFFFF
@V300088128L1C001R0010000019#CTGATATAGA/1
GTGGATCACTTGGCTCGGGACTGATATAGAAGTCGGAGGC
+
F:FFFGF?FGGFFFFGFFEE

I checked How to find a text, copy it and insert in next line in a file?, but in that example a new line is inserted instead of appending to an existing line. I also tried the following, but I cannot figure out how to append/copy the searched string to the end of the (already existing) next line.

sed -E 's%#(.*)/%$\1AGTCGGAGGC%g' test3.txt > test3new.txt

4 Answers 4

1
$ cat tst.awk
str != "" { $0 = $0 str "AGTCGGAGGC"; str="" }
match($0,"#.*/") { str = substr($0,RSTART+1,RLENGTH-2) }
{ print }

$ awk -f tst.awk file
@V300088128L1C001R0010000004#TCCTCGCATG/1
TTCAAGTAATCCAGGATAGGCTTCCTCGCATGAGTCGGAGGC
+
FFDFFFFFFFFGFFFFFFFFFF
@V300088128L1C001R0010000019#CTGATATAGA/1
GTGGATCACTTGGCTCGGGACTGATATAGAAGTCGGAGGC
+
F:FFFGF?FGGFFFFGFFEE
0

Using sed:

sed -E '/^@/{ N; s/^(.*)#(.*)\/1(.*)/\1#\2\/1\3\2AGTCGGAGGC/; }' infile

if a line was start with @ then read Next line and capture 3 groups that later we weill use them to return back them based on the order requirements you have and append 2AGTCGGAGGC as well.

3
  • Thanks for your suggestion! It seems to work in the sense that the '2AGTCGGAGGC' is added to the next line, but instead of adding it to the end of the line, it is now replacing the characters at the beginning of that line.
    – Leo
    Commented Jan 21, 2021 at 8:57
  • @Leo I'm not sure what you are saying. the command does and produce output exactly what you showed as output according to the given input.. Commented Jan 21, 2021 at 10:06
  • My best guess is the OP is saying they are seeing the effect of having DOS line endings, see stackoverflow.com/questions/45772525/….
    – Ed Morton
    Commented Jan 21, 2021 at 14:11
0

I suggest that you use a combination of awk and seqkit. The following command transforms the data into a tab-separated file that is easier to manipulate and then reverts it back to fastq.

seqkit fx2tab text.fq | awk '{ tag=$1; gsub(/(^.*#)|(\/1$)/,"" , tag); print $1 "\t" $2 tag "\t" $3 "FFFFFFFFFF"}' | seqkit tab2fx

You then have your desired output including an extension of the quality scores using phred+33 to maintain proper fastq formatting:

@V300088128L1C001R0010000004#TCCTCGCATG/1
TTCAAGTAATCCAGGATAGGCTTCCTCGCATG
+
FFDFFFFFFFFGFFFFFFFFFFFFFFFFFFFF
@V300088128L1C001R0010000019#CTGATATAGA/1
GTGGATCACTTGGCTCGGGACTGATATAGA
+
F:FFFGF?FGGFFFFGFFEEFFFFFFFFFF

if your tags are of a variable size you can use the following variation based on code found here:

seqkit fx2tab text.fq | awk ' function str_repeat(s1, n1) {s2 = ""; for (n2 = 1; n2 <= n1; n2++) {s2 = s2 s1} return s2 }; {tag=$1; gsub(/(^.*#)|(\/1$)/,"" , tag);  print $1 "\t" $2 tag "\t" $3 str_repeat("F", length(tag)) }'
0

Using GNU sed with extended regex mode -E we stick the next line to the @ line unless it's the last. Then we grab the text between # and /1 and also put a \n in the regex to ensure its from the @ line, and stick it to the end of pattern space.

sed -Ee '
  /@/{$!N;}
  s|#(.*)/1.*\n.*|&\1AGTCGGAGGC|
' file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .