0

I am trying to keep the characters in my fasta headers between the first and the second colon. I've failed trying with sed and awk since my coding skills are very limited.

In:

>TRINITY_DN10034_c0_g1:TRINITY_DN10034_c0_g1_i1:g.1561:m.1561
CCAGCGCCACGGAGGGCGAAGGCGAAGGCGGTGGTTTTGAACGATGAGGTGCCGCCGGTG
>TRINITY_DN10322_c0_g1:TRINITY_DN10322_c0_g1_i2:g.1424:m.1424
GATCCGCCTTCTATAACTGGAAAAGAAGAGGATGAGGTGCCGCCGATGAGTCTACCTTCT

Expected:

>TRINITY_DN10034_c0_g1_i1
CCAGCGCCACGGAGGGCGAAGGCGAAGGCGGTGGTTTTGAACGATGAGGTGCCGCCGGTG
>TRINITY_DN10322_c0_g1_i2
GATCCGCCTTCTATAACTGGAAAAGAAGAGGATGAGGTGCCGCCGATGAGTCTACCTTCT

Any help?

  • Rather than adding a "Thank you" comment, upvote the answer(s) that helped you, and consider "accepting" the answer that was the most helpful in resolving your issue. These are the best ways of showing gratitude on this site. Accepting an answer not only marks the question as resolved, but also signals to future readers that the accepted answer actually solved the issue. More information about this is available here: unix.stackexchange.com/help/someone-answers – Kusalananda Sep 14 '19 at 12:06
1

With awk:

awk -F: '{ if (NF==1) { print } else { print ">"$2 } }' file

or a little more compressed

awk -F: '{ print (NF==1) ? $1 : ">"$2 }' file

Use field separator : and print the first field if the line has only one field and print > and the second field otherwise.

With sed:

sed 's/^>[^:]\+:\([^:]\+\).*/>\1/' file
| improve this answer | |
1

With standard sed:

$ sed '/^>/{ s/[^:]*:/>/; s/:.*//; }' file.fa
>TRINITY_DN10034_c0_g1_i1
CCAGCGCCACGGAGGGCGAAGGCGAAGGCGGTGGTTTTGAACGATGAGGTGCCGCCGGTG
>TRINITY_DN10322_c0_g1_i2
GATCCGCCTTCTATAACTGGAAAAGAAGAGGATGAGGTGCCGCCGATGAGTCTACCTTCT

This applies the two substitutions s/[^:]*:/>/ and s/:.*// to all lines in file.fa that starts with a > (the fasta header lines).

The first substitution would replace the start of the line, up to and including the first : character, with >.

The second substitution would remove everything from the first : in the resulting data (the second : in the original line) to the end of the line.

This would leave the second :-delimited field, prefixed by a >.


With standard awk:

$ awk -F ':' '/^>/ { $0 = ">" $2 } { print }' file.fa
>TRINITY_DN10034_c0_g1_i1
CCAGCGCCACGGAGGGCGAAGGCGAAGGCGGTGGTTTTGAACGATGAGGTGCCGCCGGTG
>TRINITY_DN10322_c0_g1_i2
GATCCGCCTTCTATAACTGGAAAAGAAGAGGATGAGGTGCCGCCGATGAGTCTACCTTCT

This would use : as a field delimiter, and when coming across a line that starts with >, it would replace the complete line with > followed by the second :-delimited field. All lines, whether modified or not, would then be printed.

| improve this answer | |
0

A propposed solution:

With GNU sed

cut -f2 -d':' file | sed '1~2s/^/>/'

Output:

>TRINITY_DN10034_c0_g1_i1
CCAGCGCCACGGAGGGCGAAGGCGAAGGCGGTGGTTTTGAACGATGAGGTGCCGCCGGTG
>TRINITY_DN10322_c0_g1_i2
GATCCGCCTTCTATAACTGGAAAAGAAGAGGATGAGGTGCCGCCGATGAGTCTACCTTCT
| improve this answer | |
  • Thank you this worked perfectly. – Mert Dogan Sep 13 '19 at 15:54
0
$ awk -F':' '{$0 = (NF>1 ? ">"$2 : $0)} 1' file
>TRINITY_DN10034_c0_g1_i1
CCAGCGCCACGGAGGGCGAAGGCGAAGGCGGTGGTTTTGAACGATGAGGTGCCGCCGGTG
>TRINITY_DN10322_c0_g1_i2
GATCCGCCTTCTATAACTGGAAAAGAAGAGGATGAGGTGCCGCCGATGAGTCTACCTTCT

$ awk -F':' '{sub(/>.*/,">"$2)}1' file
>TRINITY_DN10034_c0_g1_i1
CCAGCGCCACGGAGGGCGAAGGCGAAGGCGGTGGTTTTGAACGATGAGGTGCCGCCGGTG
>TRINITY_DN10322_c0_g1_i2
GATCCGCCTTCTATAACTGGAAAAGAAGAGGATGAGGTGCCGCCGATGAGTCTACCTTCT
| improve this answer | |
0

There are a multitude of ways to handle this pbm, some of them being:

$ cut -d: -f2 | sed -e '$!N;s/^/>/' file

$ awk -F: '/^>/{$0 = ">" $2}1' file

$ perl -F: -pale ' s/^>\K.*/$F[1]/' file

$ sed -e '
   /\n/{P;d;}
   /^>/y/:/\n/
   s/\n/&>/;//D
' file

Results

>TRINITY_DN10034_c0_g1_i1 
CCAGCGCCACGGAGGGCGAAGGCGAAGGCGGTGGTTTTGAACGATGAGGTGCCGCCGGTG
>TRINITY_DN10322_c0_g1_i2 
GATCCGCCTTCTATAACTGGAAAAGAAGAGGATGAGGTGCCGCCGATGAGTCTACCTTCT
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.