dbm_index_fa.pl 680 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/perl -w
  2. #
  3. #Author: Ruan Jue
  4. #
  5. use strict;
  6. use DB_File;
  7. my $fasta_file = shift or die("Usage: $0 <fasta_file>\n");
  8. die("$fasta_file.dbm already exists!!!") if(-e "$fasta_file.dbm");
  9. open(IN, "<", $fasta_file) or die;
  10. my %seqs;
  11. tie %seqs, 'DB_File', "$fasta_file.dbm", O_RDWR | O_CREAT, 0644, $DB_HASH or die "Cannot open $fasta_file.dbm: $!";
  12. my $name = '';
  13. #my $seq = '';
  14. my $off = 0;
  15. my $len = 0;
  16. while(<IN>){
  17. if(/^>(\S+)/){
  18. #$seqs{$name} = $seq if($name);
  19. $seqs{$name} = $off;
  20. $name = $1;
  21. $off += $len;
  22. $len = 0;
  23. #$seq = '';
  24. #} else {
  25. #chomp;
  26. #$seq .= $_;
  27. }
  28. $len += length($_);
  29. }
  30. $seqs{$name} = $off if($name);
  31. untie %seqs;
  32. close IN;
  33. 1;