Slightly modifying Heng's answer: If your sequence name is longer than 80 chars using fold will wrap the header onto a new line causing part of the header to be incorrectly read as sequence.
To avoid this we can first print the fasta header line and then separately wrap the sequence lines with fold.
Support long sequence names
awk '/^S/{print ">"$2; printf "%s", $3 | "fold -w 80"; close("fold -w 80"); print ""}' in.gfa > out.fa
Include GFA tags in fasta header line
If you also want to preserve the gfa segment tags (i.e. segment length LN, or depth DP) in the fasta description you can append them to the headers:
awk '/^S/{header=">"$2; for(i=4; i<=NF; i++) {header=header" "$i}; print header; printf "%s", $3 | "fold -w 80"; close("fold -w 80"); print ""}' in.gfa > out.fa
Note: close() is required to prevent the headers and sequences from getting out of sync or being skipped when sequences are very long.
Hi,
could you describe what does GFA stand for? I don't know about this format.
GFA is the Graphical Fragment Assembly format. Here is a post by Heng Li (@lh3) on this: https://lh3.github.io/2014/07/19/a-proposal-of-the-grapical-fragment-assembly-format.
Awesome!!
Thank you so much!