upendra
upendra

Reputation: 2189

matching a header in fasta file and naming with new names

I have a fasta file with different headers like this..

>Ar000001
>Ar000002
>Ar000003

and so on.. Now i would like to change the names to something like this

>Ar000001 => >Bra000001
>Ar000002 => >Bra000002
>Ar000003 => >Bra000003 

and so on.. I have written a perl script to do that but it does not output anything. Am i doing somehting wrong.

open(INFILE, "test.fasta") or die "Error opening input file"; 
open(OUTFILE, ">", "test_out.fasta") or die "Error creating output file"; 

my $count = 1;
while (my @line = <INFILE>) 
{
    if (my $line[$count] =~ /^>/)
    {
        print OUTFILE ">Bra%.5d\n", $count;
        $count ++;
    }
}

And also apart from changing the fasta header name and outputting into a new file, how do i include the sequence as well under each fasta header. Thanks in Advance.... Upendra

Upvotes: 0

Views: 773

Answers (2)

Borodin
Borodin

Reputation: 126722

Directly from the command line

perl -pe's/^>Ar/>Bra/' test.fasta > test_out.fasta

Upvotes: 1

imran
imran

Reputation: 1560

Sounds like this is all you actually need to do:

open(my $INFILE, "<", "test.fasta") or die "Error opening input file: $!"; 
open(my $OUTFILE, ">", "test_out.fasta") or die "Error creating output file: $!"; 

while (my $line = <$INFILE>) 
{
    $line =~ s/^>Ar/>Bra/;
    print $OUTFILE $line;
}

i.e. replace all occurrences of "^>Ar" with ">Bra", and keep everything else in the file the same.

Upvotes: 0

Related Questions