1

I have a file with the following format

>M03117:162:000000000-ATLWF:1:1114:22047:12565:307
TCCGAAAGT-ACAACGTGT
>M03117:162:000000000-ATLWF:1:2104:9367:8166:307
TTCGAAAGTT-GGTGAGGTGTGGG
>M03117:162:000000000-ATLWF:1:1119:11492:8271:307
TCCGAAAGTTCTCCGA-CTTGGCTTCCTAG
>M03117:162:000000000-ATLWF:1:2111:19039:8200:307
GACGAAAGTTCACCGATA-GAGGTAGAAGGTGCAGTGGGGA

I would like to manipulate and change the lines so that the 2nd line is split into two separated by "-" to a new line and the name before identified by ">" copied in the new line and renamed by _2 at the end of the name, as follows;

>M03117:162:000000000-ATLWF:1:1114:22047:12565:307
TCCGAAAGT
>M03117:162:000000000-ATLWF:1:1114:22047:12565:307_2
ACAACGTGT
>M03117:162:000000000-ATLWF:1:2104:9367:8166:307
TTCGAAAGTT
>M03117:162:000000000-ATLWF:1:2104:9367:8166:307_2
GGTGAGGTGTGGG
>M03117:162:000000000-ATLWF:1:1119:11492:8271:307
TCCGAAAGTTCTCCGA
>M03117:162:000000000-ATLWF:1:1119:11492:8271:307_2
CTTGGCTTCCTAG
>M03117:162:000000000-ATLWF:1:2111:19039:8200:307
GACGAAAGTTCACCGATA
>M03117:162:000000000-ATLWF:1:2111:19039:8200:307_2
GAGGTAGAAGGTGCAGTGGGGA

I know this could be done by awk but I can't come up with anything.

2
  • 2
    Please, could you post your actual efforts?
    – John Goofy
    Jul 14, 2017 at 11:58
  • you can save lines starting with > into a variable, use - as field separator, and print as per requirements...
    – Sundeep
    Jul 14, 2017 at 11:59

4 Answers 4

2

Since you specifically asked about "using awk"

awk -F- '
  /^>/ {hdr=$0; next} 
  {print hdr ORS $1; for(i=2;i<=NF;i++) print hdr"_"i ORS $i}
' file
2
  • You have answered the question. Good job.
    – John Goofy
    Jul 14, 2017 at 13:09
  • But you don't need a loop. This also works: awk -F- '/^>/{hdr=$0; next}{print hdr ORS $1; i=2; print hdr"_"i ORS $i}' file
    – John Goofy
    Jul 14, 2017 at 13:19
2

Capture lines which start with > in header and go to next input record for further processing. Than print a line with the header a \n and column 1 of the new input record. Than repeat this for the second column and add 2 to your header.

$ awk -F- '/^>/{header=$0; next}{print header ORS $1; print header"_2" ORS $2}' file
>M03117:162:000000000-ATLWF:1:1114:22047:12565:307
TCCGAAAGT
>M03117:162:000000000-ATLWF:1:1114:22047:12565:307_2
ACAACGTGT
>M03117:162:000000000-ATLWF:1:2104:9367:8166:307
TTCGAAAGTT
>M03117:162:000000000-ATLWF:1:2104:9367:8166:307_2
GGTGAGGTGTGGG
>M03117:162:000000000-ATLWF:1:1119:11492:8271:307
TCCGAAAGTTCTCCGA
>M03117:162:000000000-ATLWF:1:1119:11492:8271:307_2
CTTGGCTTCCTAG
>M03117:162:000000000-ATLWF:1:2111:19039:8200:307
GACGAAAGTTCACCGATA
>M03117:162:000000000-ATLWF:1:2111:19039:8200:307_2
GAGGTAGAAGGTGCAGTGGGGA
1

Perl to the rescue

perl -laF/-/ -ne '
    if (/^>/) { print; $header = "$_\_2"; }
    else { print join "\n", $F[0], $header, $F[1] }
' -- input-file
  • -n reads the input line by line
  • -l removes newlines from input and adds them to print statements
  • -aF/-/ splits the input on -

When reading a header line, it outputs it and stores the header for later use. When processing a sequence, it prints the first part, the stored header, and the second part.

1
  • This works. thanks a lot choroba but i must admit am not as good with perl Jul 18, 2017 at 3:16
1

If you like to help yourself, but don't want to learn programming, you can do stuff like this with sed. You simply always read the next line with N, separate the double-line into it's parts surrounded by () and combine those part referred to as \1 for the first and so on as needed:

sed -E 'N;s/(.*)(\n)(.*)-(.*)/\1\2\3\2\1_2\2\4/' file

You can also do it without extended regular expressions (no option -E), but you may get lost in backslashes:

sed 'N;s/\(.*\)\(\n\)\(.*\)-\(.*\)/\1\2\3\2\1_2\2\4/' file

Same result, achieved by hold buffer usage, but not so obvious if you are new to sed:

sed 'h;n;y/-/\n/;P;s/.*\n//;x;s/$/_2/;G' file
1
  • Now I finally expanded GNU sed a tiny bit: \h in the replacement string inserts the hold space, a function I was missing many times. The script becomes very compact and readable then: sed 'h;n;s/-/\n\h_2\n/' file
    – Philippos
    Jul 18, 2017 at 7:48

You must log in to answer this question.

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