-1

I have a multiFASTA file i.e. a file containing more than 1 FASTA sequences. Each sequence is preceded by a header which looks like this:

>NC_005042.1 etc
>NC_003272.1 etc
>NC_003276.1 etc 
...

I would like to rename these headers for numbers, so the output would be like this:

 >1
 >2
 >3
 ...

In conclusion, I would need to replace each tag after the ">" for a series of ordered numbers.

6
  • 1
    Please show what you have done and what went wrong. awk 'BEGIN {nr=0} /^NC_/ {nr++; print ">" nr; next} 1' fastafile might be something.
    – Walter A
    Oct 12, 2018 at 22:06
  • I tried awk by comparison with a somehow similar case, already asked here: stackoverflow.com/questions/40164013/… Oct 14, 2018 at 9:25
  • Still no code in the post …
    – Alfe
    Oct 15, 2018 at 9:20
  • Hi, I couldn't post code since I have no clue how would it look like... Oct 20, 2018 at 16:18
  • Did you try the suggestion in my comment?
    – Walter A
    Oct 20, 2018 at 21:02

1 Answer 1

0

When you know that the headers all start with > and other lines not, you can use

awk 'BEGIN {nr=0} /^>/ {nr++; print ">" nr; next} {print}' fastafile

In words: Before eading lines set the variable nr to 0.
When a line starts with >, raise nr, print header line with nr and skip the rest of the commands.
The rest of the commands is only printing the current line.

awk '/^>/ {nr++; print ">" nr; next} 1' fastafile

is shorter, using default value 0 var awk variables and default print action after 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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