rename_fq.pl 933 B

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);
  10. getopts("hp:s:f:");
  11. die("Usage: $0 [-p name_prefix] [-s name_suffix] [-f trans_file] <fastq_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 = 0;
  26. my $line = 0;
  27. while(<>){
  28. if(($line % 4) == 0 and /^@(\S+)/){
  29. my $comm = substr($_, length($1) + 1);
  30. $idx ++;
  31. if(%hash){
  32. if(exists $hash{$1}){
  33. my $tag = $hash{$1};
  34. print "\@$tag", $comm;
  35. } else {
  36. print;
  37. }
  38. } else {
  39. printf("\@$prefix%012d$suffix$comm", $idx);
  40. }
  41. } else {
  42. print;
  43. }
  44. $line ++;
  45. }
  46. 1;