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
    Sep 26 at 15:53
  • I have edited the question for clarity. Sep 26 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
    Sep 26 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
    Sep 26 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
    Sep 26 at 16:29

5 Answers 5

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
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:/).

1

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
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
-2

You can achieve this using a text processing tool like sed in Unix/Linux. Here's a sed command to modify the input as you described:

sed 's/^\(>.*\)\.p[0-9]\(.*:\)/\1\2\1/' input.txt > output.txt

Explanation of the sed command:

  • ^: Matches the beginning of a line.
  • \(>.*\) captures the part of the line starting with '>' (including all characters until '.p[number]') into a group.
  • \.p[0-9] matches the '.p[number]' pattern.
  • \(.*:) captures the part of the line following '.p[number]' up to the last ':' into another group.
  • \1\2\1 replaces the entire line with the first group, the second group, and the first group again, effectively adding the desired part before the last ':'.

Replace input.txt with the name of your input file, and the modified lines will be saved to output.txt.

1
  • You will notice that this does not produce the correct output.
    – Kusalananda
    Sep 27 at 6:51

You must log in to answer this question.

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