rename_fa.pl 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/perl -w
  2. #
  3. # Author: Ruan Jue
  4. #
  5. use strict;
  6. use Getopt::Std;
  7. my $prefix = $ENV{'PARAM_RENAME_FA_PREFIX'} || 'S';
  8. my $suffix = $ENV{'PARAM_RENAME_FA_SUFFIX'} || '';
  9. our ($opt_p, $opt_s, $opt_h, $opt_f, $opt_b, $opt_I);
  10. getopts("hp:s:f:b:I");
  11. die("Usage: $0 [-p name_prefix] [-s name_suffix] [-f trans_file] [-b begin_idx] [-I:include orignial_name] <fasta_file>\n") if($opt_h);
  12. $prefix = $opt_p if(defined $opt_p);
  13. $suffix = $opt_s if(defined $opt_s);
  14. my %hash;
  15. if(defined $opt_f){
  16. open(IN, "<", $opt_f) or die;
  17. %hash = ();
  18. while(<IN>){
  19. my @ts = split;
  20. $hash{$ts[0]} = $ts[1];
  21. #print STDERR "$ts[0]\t$ts[1]\n";
  22. }
  23. close IN;
  24. }
  25. my $idx = $opt_b || 0;
  26. while(<>){
  27. if(/^>(\S+)/){
  28. my $desc = substr($_, length($1) + 1);
  29. my $ori = $opt_I? " $1" : "";
  30. if(%hash){
  31. if(exists $hash{$1}){
  32. my $tag = $hash{$1};
  33. print ">$tag$ori$desc", substr($_, length($1) + 1);
  34. } else {
  35. print;
  36. }
  37. } else {
  38. printf(">$prefix%010d$suffix$ori$desc", $idx);
  39. }
  40. $idx ++;
  41. } else {
  42. print;
  43. }
  44. }
  45. 1;