1

I would like to copy the first part (IxoscaEVm****t1_, without the '.p[number]') of the line starting with ">" and paste a before the last ":" of the same line.

Input:

>IxoscaEVm2293881t1.p1 type:complete len:255 gc:Universal :13-219(+)
MFLRQLGAPRFYYARLFLRFIAVTIGPFLKSFPEKMLFLTYFPFYFWQRFSNINKRRKLLPATFLTLG
>IxoscaEVm2798449t1.p1 type:partial5 len:345 gc:Universal :1-183(+)
CEEREKLQFPTLVELFSTMKLLSCFPGLTLNIMGANIKRVRVSKLLNKYFAVEGRTWRVC
>IxoscaEVm3186316t1.p1 type:partial5 len:317 gc:Universal :315-151(+)
NRIARLIGKNAEAGSVNYFFVSGSLLFGKPEQCALNLFLFLHSYYTLPFFIWCI

expected output:

>IxoscaEVm2293881t1.p1 type:complete len:255 gc:Universal IxoscaEVm2293881t1:13-219(+)
MFLRQLGAPRFYYARLFLRFIAVTIGPFLKSFPEKMLFLTYFPFYFWQRFSNINKRRKLLPATFLTLG
>IxoscaEVm2798449t1.p1 type:partial5 len:345 gc:Universal IxoscaEVm2798449t1:1-183(+)
CEEREKLQFPTLVELFSTMKLLSCFPGLTLNIMGANIKRVRVSKLLNKYFAVEGRTWRVC
>IxoscaEVm3186316t1.p1 type:partial5 len:317 gc:Universal IxoscaEVm3186316t1:315-151(+)
NRIARLIGKNAEAGSVNYFFVSGSLLFGKPEQCALNLFLFLHSYYTLPFFIWCI
6
  • What is the first "part"? Will it be the string between the fasta header (>) and the first whitespace? Can you have spaces in the first "part"?
    – terdon
    Commented Sep 26, 2023 at 15:53
  • I have edited the question for clarity. Commented Sep 26, 2023 at 16:09
  • Thanks. Don't worry, I'll fix the formatting: we can't use markdown (bold etc.) in code blocks, only in quote blocks, but since > denotes a quote, you can't easily have > in that. It's a pain, I know. Just stick to code and forget the bold, even though they help. So, can we be sure there will be no whitespace before the .p1? And will it always be a .p1, never .p2 or nothing at all?
    – terdon
    Commented Sep 26, 2023 at 16:13
  • By the way, what kind of identifiers are these? I've never seen them before. Oh, and you might also be interested in our sister site: Bioinformatics.
    – terdon
    Commented Sep 26, 2023 at 16:19
  • 1
    Yes, I can see they're peptides, I was just wondering about the identifiers. What identifier is this IxoscaEVm2293881t1? I haven't seen that style before. What database does it refer to? Sorry. I'm just curious, I thought I knew most of the standard ones.
    – terdon
    Commented Sep 26, 2023 at 16:29

4 Answers 4

2

Using any awk:

$ awk 'split($1,a,/[>.]/) > 1{ $NF=a[2] $NF } 1' file
>IxoscaEVm2293881t1.p1 type:complete len:255 gc:Universal IxoscaEVm2293881t1:13-219(+)
MFLRQLGAPRFYYARLFLRFIAVTIGPFLKSFPEKMLFLTYFPFYFWQRFSNINKRRKLLPATFLTLG
>IxoscaEVm2798449t1.p1 type:partial5 len:345 gc:Universal IxoscaEVm2798449t1:1-183(+)
CEEREKLQFPTLVELFSTMKLLSCFPGLTLNIMGANIKRVRVSKLLNKYFAVEGRTWRVC
>IxoscaEVm3186316t1.p1 type:partial5 len:317 gc:Universal IxoscaEVm3186316t1:315-151(+)
NRIARLIGKNAEAGSVNYFFVSGSLLFGKPEQCALNLFLFLHSYYTLPFFIWCI
2

With sed:

$ sed 's/^>\(\([^.]*\).*\):/>\1\2:/' file
>IxoscaEVm2293881t1.p1 type:complete len:255 gc:Universal IxoscaEVm2293881t1:13-219(+)
MFLRQLGAPRFYYARLFLRFIAVTIGPFLKSFPEKMLFLTYFPFYFWQRFSNINKRRKLLPATFLTLG
>IxoscaEVm2798449t1.p1 type:partial5 len:345 gc:Universal IxoscaEVm2798449t1:1-183(+)
CEEREKLQFPTLVELFSTMKLLSCFPGLTLNIMGANIKRVRVSKLLNKYFAVEGRTWRVC
>IxoscaEVm3186316t1.p1 type:partial5 len:317 gc:Universal IxoscaEVm3186316t1:315-151(+)
NRIARLIGKNAEAGSVNYFFVSGSLLFGKPEQCALNLFLFLHSYYTLPFFIWCI

The sed substitution replaces everything from the start of any line beginning with the standard Fasta header marker >, up to and including the last : on that line. While doing so, it captures the part between the > up to (but not including) the first dot (this is the identifier), and also the whole start of the line, but not the final : character. The replacement part of the substitution is the line up to where the identifier should be inserted, followed by the identifier and a :.

A similar thing with an in-line Perl subsitution:

$ perl -pe 's/^>((.*?)\..*):/>$1$2:/' file
>IxoscaEVm2293881t1.p1 type:complete len:255 gc:Universal IxoscaEVm2293881t1:13-219(+)
MFLRQLGAPRFYYARLFLRFIAVTIGPFLKSFPEKMLFLTYFPFYFWQRFSNINKRRKLLPATFLTLG
>IxoscaEVm2798449t1.p1 type:partial5 len:345 gc:Universal IxoscaEVm2798449t1:1-183(+)
CEEREKLQFPTLVELFSTMKLLSCFPGLTLNIMGANIKRVRVSKLLNKYFAVEGRTWRVC
>IxoscaEVm3186316t1.p1 type:partial5 len:317 gc:Universal IxoscaEVm3186316t1:315-151(+)
NRIARLIGKNAEAGSVNYFFVSGSLLFGKPEQCALNLFLFLHSYYTLPFFIWCI

With Perl again, using named capture groups:

$ perl -pe 's/^>(?<partial_line>(?<id>.*?)\..*):/>$+{partial_line}$+{id}:/' file
>IxoscaEVm2293881t1.p1 type:complete len:255 gc:Universal IxoscaEVm2293881t1:13-219(+)
MFLRQLGAPRFYYARLFLRFIAVTIGPFLKSFPEKMLFLTYFPFYFWQRFSNINKRRKLLPATFLTLG
>IxoscaEVm2798449t1.p1 type:partial5 len:345 gc:Universal IxoscaEVm2798449t1:1-183(+)
CEEREKLQFPTLVELFSTMKLLSCFPGLTLNIMGANIKRVRVSKLLNKYFAVEGRTWRVC
>IxoscaEVm3186316t1.p1 type:partial5 len:317 gc:Universal IxoscaEVm3186316t1:315-151(+)
NRIARLIGKNAEAGSVNYFFVSGSLLFGKPEQCALNLFLFLHSYYTLPFFIWCI
1

Here's one way:

$ perl -pe 'if(/^\s*>(\S+?)\.p1/){$name=$1; s/(.*):/$1$name:/}' file.pep 
>IxoscaEVm2293881t1.p1 type:complete len:255 gc:Universal IxoscaEVm2293881t1:13-219(+)
MFLRQLGAPRFYYARLFLRFIAVTIGPFLKSFPEKMLFLTYFPFYFWQRFSNINKRRKLLPATFLTLG
>IxoscaEVm2798449t1.p1 type:partial5 len:345 gc:Universal IxoscaEVm2798449t1:1-183(+)
CEEREKLQFPTLVELFSTMKLLSCFPGLTLNIMGANIKRVRVSKLLNKYFAVEGRTWRVC
>IxoscaEVm3186316t1.p1 type:partial5 len:317 gc:Universal IxoscaEVm3186316t1:315-151(+)
NRIARLIGKNAEAGSVNYFFVSGSLLFGKPEQCALNLFLFLHSYYTLPFFIWCI

Assumptions

  • There are no whitespace characters before the first .p1.
  • All lines that begin with a > also have .p1.

The script is relatively straightforward: if this line starts with 0 or more whitespace characters and then a > (if(/^\s*>(\S+?)\.p1/)), so if it is a header line, then store the string before the first .p1 in the variable $name ($name=$1, this works because we captured the (\S+?) using parentheses in the previous step). We then replace everything until the last : on the line with whatever was matched, then the name and then a : (s/(.*):/$1$name:/).

0

Using gawk:

$ awk '{print gensub(/(\s*>)(IxoscaEVm.*t1)(.*)(:.*)/,"\\1\\2\\3\\2\\4", "g")}' file

Or

$ awk '/^>IxoscaEVm.*t1/{$NF = $1 $NF; gsub(/^>|\.p[0-9]{1,}/, "", $NF)}1' ffile

You must log in to answer this question.

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