View Single Post
Old 09-26-2013, 09:47 PM   #2
atcghelix
Member
 
Location: CA

Join Date: Jul 2013
Posts: 74
Default

Here's one way using Perl. Save the text in a file named numbers.pl (or whatever). Usage would be:

perl numbers.pl --in file_to_change.fasta --out revised_file.fasta


Code:
#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long;

my $inFile;
my $outFile;

GetOptions  ("in=s"      => \$inFile,
             "out=s"      => \$outFile);

if (!$inFile or !$outFile) {
    die "Must supply both infile and outfile as command line arguments.\n";
}

open(my $inFH, "<", $inFile) or die "couldn't open infile for reading.\n";
if (-e $outFile) {
    die "Output file $outFile already exists--aborting so you don't overwrite.\n";
}
open(my $outFH, ">", $outFile) or die "couldn't open outfile for writing.\n";
    
my $counter = 1;
while (my $line = <$inFH>) {
    chomp $line;
    if ($line =~ /^(>.*)/) {
        print $outFH $1 . "_$counter\n";
        $counter++;
    } else {
        print $outFH "$line\n";
    }
}

Last edited by atcghelix; 09-26-2013 at 09:57 PM. Reason: Edited to move $counter++ so that you didn't just get odd-numbered sequences
atcghelix is offline   Reply With Quote