This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Help Needed For Formatting Fasta Headers
 >HWI-ST667:190:C0TPFACXX:1:1101:2885:1985 1:N:0:GATCAG
 TCGGATAGAGCTCCAAATCTATCT
 >HWI-ST667:190:C0TPFACXX:1:1101:3058:1999 1:N:0:GATCAG
 CAATATCAACTGCTGCAACTCTCT
 >HWI-ST667:190:C0TPFACXX:1:1101:3372:1992 1:N:0:GATCAG
 TCAAAGGTTGAAGAGAATGAAATTTCT
 ......

How to use perl script to change the above FASTA file (just the header) into the following format? Many thanks! I'm a biologist with little programing background.

 >seq_1
 TCGGATAGAGCTCCAAATCTATCT      
 >seq_2
 CAATATCAACTGCTGCAACTCTCT
 >seq_3
 TCAAAGGTTGAAGAGAATGAAATTTCT
 ......
perl fasta

3 answers

On a linux command line do:

awk 'BEGIN{OFS="_";seq=1}{if($0 ~ /^>/){print ">seq",seq;seq++}else{print $0}}' yourFile.fasta

Of course change yourFile.fasta for the name of your file...

gives you:

>seq_1
TCGGATAGAGCTCCAAATCTATCT
>seq_2
CAATATCAACTGCTGCAACTCTCT
>seq_3
TCAAAGGTTGAAGAGAATGAAATTTCT

Here are two more options:

use strict;
use warnings;

my $i = 0;
while (<>) {
    s/^>\K.+/'seq_' . ++$i/e;
    print;
}

Usage: perl script.pl inFile [>outFile]

The last, optional parameter directs output to a file.

As a oneliner:

perl -ne 's/^>\K.+/'seq_' . ++$i/e; print' inFile [>outFile]

Output on your dataset from both:

>seq_1
TCGGATAGAGCTCCAAATCTATCT
>seq_2
CAATATCAACTGCTGCAACTCTCT
>seq_3
TCAAAGGTTGAAGAGAATGAAATTTCT

Hope this helps!

Any clues would be highly appreciated!

Log in to answer this question.