This file is indexed.

/usr/share/perl5/Bio/DB/Fasta.pm is in libbio-perl-perl 1.6.901-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
#
# BioPerl module for Bio::DB::Fasta
#
# You may distribute this module under the same terms as perl itself
#

# POD documentation - main docs before the code

=head1 NAME

Bio::DB::Fasta -- Fast indexed access to a directory of fasta files

=head1 SYNOPSIS

  use Bio::DB::Fasta;

  # create database from directory of fasta files
  my $db      = Bio::DB::Fasta->new('/path/to/fasta/files');

  # simple access (for those without Bioperl)
  my $seq      = $db->seq('CHROMOSOME_I',4_000_000 => 4_100_000);
  my $revseq   = $db->seq('CHROMOSOME_I',4_100_000 => 4_000_000);
  my @ids     = $db->ids;
  my $length   = $db->length('CHROMOSOME_I');
  my $alphabet = $db->alphabet('CHROMOSOME_I');
  my $header   = $db->header('CHROMOSOME_I');

  # Bioperl-style access
  my $db      = Bio::DB::Fasta->new('/path/to/fasta/files');

  my $obj     = $db->get_Seq_by_id('CHROMOSOME_I');
  my $seq     = $obj->seq; # sequence string
  my $subseq  = $obj->subseq(4_000_000 => 4_100_000); # string
  my $trunc   = $obj->trunc(4_000_000 => 4_100_000); # seq object
  my $length  = $obj->length;
  # (etc)

  # Bio::SeqIO-style access
  my $stream  = Bio::DB::Fasta->new('/path/to/files')->get_PrimarySeq_stream;
  while (my $seq = $stream->next_seq) {
    # Bio::PrimarySeqI stuff
  }

  my $fh = Bio::DB::Fasta->newFh('/path/to/fasta/files');
  while (my $seq = <$fh>) {
    # Bio::PrimarySeqI stuff
  }

  # tied hash access
  tie %sequences,'Bio::DB::Fasta','/path/to/fasta/files';
  print $sequences{'CHROMOSOME_I:1,20000'};

=head1 DESCRIPTION

Bio::DB::Fasta provides indexed access to one or more Fasta files.  It
provides random access to each sequence entry, and to subsequences
within each entry, allowing you to retrieve portions of very large
sequences without bringing the entire sequence into memory.

When you initialize the module, you point it at a single fasta file or
a directory of multiple such files.  The first time it is run, the
module generates an index of the contents of the file or directory
using the AnyDBM module (Berkeley DB* preferred, followed by GDBM_File,
NDBM_File, and SDBM_File).  Thereafter it uses the index file to find
the file and offset for any requested sequence.  If one of the source
fasta files is updated, the module reindexes just that one file.  (You
can also force reindexing manually).  For improved performance, the
module keeps a cache of open filehandles, closing less-recently used
ones when the cache is full.

The fasta files may contain any combination of nucleotide and protein
sequences; during indexing the module guesses the molecular type.
Entries may have any line length up to 65,536 characters, and
different line lengths are allowed in the same file.  However, within
a sequence entry, all lines must be the same length except for the
last.

An error will be thrown if this is not the case.

The module uses /^E<gt>(\S+)/ to extract the primary ID of each sequence 
from the Fasta header.  During indexing, you may pass a callback routine to
modify this primary ID.  For example, you may wish to extract a
portion of the gi|gb|abc|xyz nonsense that GenBank Fasta files use.
The original header line can be recovered later.

This module was developed for use with the C. elegans and human
genomes, and has been tested with sequence segments as large as 20
megabases.  Indexing the C. elegans genome (100 megabases of genomic
sequence plus 100,000 ESTs) takes ~5 minutes on my 300 MHz pentium
laptop. On the same system, average access time for any 200-mer within
the C. elegans genome was E<lt>0.02s.

*Berkeley DB can be obtained free from www.sleepycat.com. After it is 
installed you will need to install the BerkeleyDB Perl module.

=head1 DATABASE CREATION AND INDEXING

The two constructors for this class are new() and newFh().  The former
creates a Bio::DB::Fasta object which is accessed via method calls.
The latter creates a tied filehandle which can be used Bio::SeqIO
style to fetch sequence objects in a stream fashion.  There is also a
tied hash interface.

=over 2

=item $db = Bio::DB::Fasta-E<gt>new($fasta_path [,%options])

Create a new Bio::DB::Fasta object from the Fasta file or files
indicated by $fasta_path.  Indexing will be performed automatically if
needed.  If successful, new() will return the database accessor
object.  Otherwise it will return undef.

$fasta_path may be an individual Fasta file, or may refer to a
directory containing one or more of such files.  Following the path,
you may pass a series of name=E<gt>value options or a hash with these
same name=E<gt>value pairs.  Valid options are:

 Option Name   Description               Default
 -----------   -----------               -------

 -glob         Glob expression to use    *.{fa,fasta,fast,FA,FASTA,FAST,dna}
               for searching for Fasta
	            files in directories. 

 -makeid       A code subroutine for     None
	            transforming Fasta IDs.

 -maxopen      Maximum size of		     32
	            filehandle cache.

 -debug        Turn on status		        0
	            messages.

 -reindex      Force the index to be     0
               rebuilt.

 -dbmargs      Additional arguments      none
               to pass to the DBM
               routines when tied
               (scalar or array ref).

-dbmargs can be used to control the format of the index.  For example,
you can pass $DB_BTREE to this argument so as to force the IDs to be
sorted and retrieved alphabetically.  Note that you must use the same
arguments every time you open the index!

-reindex can be used to force the index to be recreated from scratch.

=item $fh = Bio::DB::Fasta-E<gt>newFh($fasta_path [,%options])

Create a tied filehandle opened on a Bio::DB::Fasta object.  Reading
from this filehandle with E<lt>E<gt> will return a stream of sequence objects,
Bio::SeqIO style.

=back

The -makeid option gives you a chance to modify sequence IDs during
indexing.  The option value should be a code reference that will
take a scalar argument and return a scalar result, like this:

  $db = Bio::DB::Fasta->new("file.fa",-makeid=>\&make_my_id);

  sub make_my_id {
    my $description_line = shift;
    # get a different id from the fasta header, e.g.
	 $description_line =~ /(\S+)$/;
    return $1;
  }

make_my_id() will be called with the full fasta id line (including the
"E<gt>" symbol!).  For example:

 >A12345.3 Predicted C. elegans protein egl-2

By default, this module will use the regular expression /^E<gt>(\S+)/
to extract "A12345.3" for use as the ID.  If you pass a -makeid
callback, you can extract any portion of this, such as the "egl-2"
symbol.

The -makeid option is ignored after the index is constructed.

=head1 OBJECT METHODS

The following object methods are provided.

=over 10

=item $raw_seq = $db-E<gt>seq($id [,$start, $stop])

Return the raw sequence (a string) given an ID and optionally a start
and stop position in the sequence.  In the case of DNA sequence, if
$stop is less than $start, then the reverse complement of the sequence
is returned (this violates Bio::Seq conventions).

For your convenience, subsequences can be indicated with any of the
following compound IDs:

   $db->seq("$id:$start,$stop")

   $db->seq("$id:$start..$stop")

   $db->seq("$id:$start-$stop")

=item $length = $db-E<gt>length($id)

Return the length of the indicated sequence.

=item $header = $db-E<gt>header($id)

Return the header line for the ID, including the initial "E<gt>".

=item $type  = $db-E<gt>alphabet($id)

Return the molecular type of the indicated sequence.  One of "dna",
"rna" or "protein".

=item $filename  = $db-E<gt>file($id)

Return the name of the file in which the indicated sequence can be
found.

=item $offset    = $db-E<gt>offset($id)

Return the offset of the indicated sequence from the beginning of the
file in which it is located.  The offset points to the beginning of
the sequence, not the beginning of the header line.

=item $header_length = $db-E<gt>headerlen($id)

Return the length of the header line for the indicated sequence.

=item $header_offset = $db-E<gt>header_offset($id)

Return the offset of the header line for the indicated sequence from
the beginning of the file in which it is located.

=item $index_name  = $db-E<gt>index_name

Return the path to the index file.

=item $path = $db-E<gt>path

Return the path to the Fasta file(s).

=back

For BioPerl-style access, the following methods are provided:

=over 4

=item $seq = $db-E<gt>get_Seq_by_id($id)

Return a Bio::PrimarySeq::Fasta object, which obeys the
Bio::PrimarySeqI conventions.  For example, to recover the raw DNA or
protein sequence, call $seq-E<gt>seq().

Note that get_Seq_by_id() does not bring the entire sequence into
memory until requested.  Internally, the returned object uses the
accessor to generate subsequences as needed.

=item $seq = $db-E<gt>get_Seq_by_acc($id)

=item $seq = $db-E<gt>get_Seq_by_primary_id($id)

These methods all do the same thing as get_Seq_by_id().

=item $stream = $db-E<gt>get_PrimarySeq_stream()

Return a Bio::DB::Fasta::Stream object, which supports a single method
next_seq(). Each call to next_seq() returns a new
Bio::PrimarySeq::Fasta object, until no more sequences remain.

=back

See L<Bio::PrimarySeqI> for methods provided by the sequence objects
returned from get_Seq_by_id() and get_PrimarySeq_stream().

=head1 TIED INTERFACES

This module provides two tied interfaces, one which allows you to
treat the sequence database as a hash, and the other which allows you
to treat the database as an I/O stream.

=head2 Creating a Tied Hash

The tied hash interface is very straightforward

=over 1

=item $obj = tie %db,'Bio::DB::Fasta','/path/to/fasta/files' [,@args]

Tie %db to Bio::DB::Fasta using the indicated path to the Fasta files.
The optional @args list is the same set of named argument/value pairs
used by Bio::DB::Fasta-E<gt>new().

If successful, tie() will return the tied object.  Otherwise it will
return undef.

=back

Once tied, you can use the hash to retrieve an individual sequence by
its ID, like this:

  my $seq = $db{CHROMOSOME_I};

You may select a subsequence by appending the comma-separated range to 
the sequence ID in the format "$id:$start,$stop".  For example, here
is the first 1000 bp of the sequence with the ID "CHROMOSOME_I":

  my $seq = $db{'CHROMOSOME_I:1,1000'};

(The regular expression used to parse this format allows sequence IDs
to contain colons.)

When selecting subsequences, if $start E<gt> stop, then the reverse
complement will be returned for DNA sequences.

The keys() and values() functions will return the sequence IDs and
their sequences, respectively.  In addition, each() can be used to
iterate over the entire data set:

 while (my ($id,$sequence) = each %db) {
    print "$id => $sequence\n";
 }

When dealing with very large sequences, you can avoid bringing them
into memory by calling each() in a scalar context.  This returns the
key only.  You can then use tied(%db) to recover the Bio::DB::Fasta
object and call its methods.

 while (my $id = each %db) {
    print "$id => $db{$sequence:1,100}\n";
    print "$id => ",tied(%db)->length($id),"\n";
 }

You may, in addition invoke Bio::DB::Fasta the FIRSTKEY and NEXTKEY tied
hash methods directly.

=over 2

=item $id = $db-E<gt>FIRSTKEY

Return the first ID in the database.

=item $id = $db-E<gt>NEXTKEY($id)

Given an ID, return the next ID in sequence.

=back

This allows you to write the following iterative loop using just the
object-oriented interface:

 my $db = Bio::DB::Fasta->new('/path/to/fasta/files');
 for (my $id=$db->FIRSTKEY; $id; $id=$db->NEXTKEY($id)) {
    # do something with sequence
 }

=head2 Creating a Tied Filehandle

The Bio::DB::Fasta-E<gt>newFh() method creates a tied filehandle from
which you can read Bio::PrimarySeq::Fasta sequence objects
sequentially.  The following bit of code will iterate sequentially
over all sequences in the database:

 my $fh = Bio::DB::Fasta->newFh('/path/to/fasta/files');
 while (my $seq = <$fh>) {
   print $seq->id,' => ',$seq->length,"\n";
 }

When no more sequences remain to be retrieved, the stream will return
undef.

=head1 BUGS

When a sequence is deleted from one of the Fasta files, this deletion
is not detected by the module and removed from the index.  As a
result, a "ghost" entry will remain in the index and will return
garbage results if accessed.

Currently, the only way to accommodate deletions is to rebuild the
entire index, either by deleting it manually, or by passing
-reindex=E<gt>1 to new() when initializing the module.

=head1 SEE ALSO

L<bioperl>

=head1 AUTHOR

Lincoln Stein E<lt>lstein@cshl.orgE<gt>.  

Copyright (c) 2001 Cold Spring Harbor Laboratory.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.  See DISCLAIMER.txt for
disclaimers of warranty.

=cut

#'
package Bio::DB::Fasta;

BEGIN {
  @AnyDBM_File::ISA = qw(DB_File GDBM_File NDBM_File SDBM_File)
}

use strict;
use IO::File;
use AnyDBM_File;
use Fcntl;
use File::Glob ':glob';
use File::Basename qw(basename dirname);

use base qw(Bio::DB::SeqI Bio::Root::Root);

*seq = *sequence = \&subseq;
*ids = \&get_all_ids;
*get_seq_by_primary_id = *get_Seq_by_acc  = \&get_Seq_by_id;

use constant STRUCT =>'NNnnCa*';
use constant STRUCTBIG =>'QQnnCa*'; # 64-bit file offset and seq length
use constant DNA     => 1;
use constant RNA     => 2;
use constant PROTEIN => 3;
use constant DIE_ON_MISSMATCHED_LINES => 1; # if you want 

# Bio::DB-like object
# providing fast random access to a directory of FASTA files

=head2 new

 Title   : new
 Usage   : my $db = Bio::DB::Fasta->new( $path, @options);
 Function: initialize a new Bio::DB::Fasta object
 Returns : new Bio::DB::Fasta object
 Args    : path to dir of fasta files or a single filename

These are optional arguments to pass in as well.

 -glob         Glob expression to use    *.{fa,fasta,fast,FA,FASTA,FAST}
               for searching for Fasta
	             files in directories. 

 -makeid       A code subroutine for     none
	             transforming Fasta IDs.

 -maxopen      Maximum size of		       32
	             filehandle cache.

 -debug        Turn on status		         0
	             messages.

 -reindex      Force the index to be     0
               rebuilt.

 -dbmargs      Additional arguments      none
               to pass to the DBM
               routines when tied
               (scalar or array ref).

=cut

sub new {
  my $class = shift;
  my $path  = shift;
  my %opts  = @_;

  my $self = bless { debug      => $opts{-debug},
	  makeid     => $opts{-makeid},
	  glob       => $opts{-glob}    || '*.{fa,fasta,FA,FASTA,fast,FAST,dna,FNA,fna,FAA,faa,FSA,fsa}',
	  maxopen    => $opts{-maxopen} || 32,
	  dbmargs    => $opts{-dbmargs} || undef,
	  fhcache    => {},
	  cacheseq   => {},
	  curopen    => 0,
	  openseq    => 1,
	  dirname    => undef,
	  offsets    => undef,
		   }, $class;
  my ($offsets,$dirname);

  if (-d $path) {
    # because Win32 glob() is broken with respect to long file names
    # that contain whitespace.
    $path = Win32::GetShortPathName($path)
      if $^O =~ /^MSWin/i && eval 'use Win32; 1';
    $offsets = $self->index_dir($path,$opts{-reindex}) or return;
    $dirname = $path;
  } elsif (-f _) {
    $offsets = $self->index_file($path,$opts{-reindex});
    $dirname = dirname($path);
  } else {
    $self->throw( "$path: Invalid file or dirname");
  }
  @{$self}{qw(dirname offsets)} = ($dirname,$offsets);

  $self;
}

=head2 newFh

 Title   : newFh
 Function: gets a new Fh for a file
 Example : internal method
 Returns : GLOB 
 Args    :

=cut

sub newFh {
  my $class = shift;
  my $self  = $class->new(@_);
  require Symbol;
  my $fh = Symbol::gensym or return;
  tie $$fh,'Bio::DB::Fasta::Stream',$self or return;
  $fh;
}

sub _open_index {
  my $self = shift;
  my ($index,$write) = @_;
  my %offsets;
  my $flags = $write ? O_CREAT|O_RDWR : O_RDONLY;
  my @dbmargs = $self->dbmargs;
  eval {
      tie %offsets,'AnyDBM_File',$index,$flags,0644,@dbmargs 
	  or die "Can't open sequence index file $index: $!";
  };
  warn $@ if $@;
  return \%offsets;
}

sub _close_index {
  my $self = shift;
  my $index = shift;
  untie %$index;
}

=head2 index_dir

 Title   : index_dir
 Usage   : $db->index_dir($dir)
 Function: set the index dir and load all files in the dir
 Returns : hashref of seq offsets in each file
 Args    : dirname, boolean to force a reload of all files

=cut

sub index_dir {
  my $self = shift;
  my $dir  = shift;
  my $force_reindex = shift;

  # find all fasta files
  my @files = glob("$dir/$self->{glob}");
  return unless @files;

  # get name of index
  my $index = $self->index_name($dir,1);

  # if caller has requested reindexing, then unlink
  # the index file.
  unlink $index if $force_reindex;

  # get the modification time of the index
  my $indextime   = 0;
  for my $suffix('','.pag','.dir') {
    $indextime ||= (stat("${index}${suffix}"))[9];
  }
  $indextime ||= 0;  # prevent some uninit variable warnings

  # get the most recent modification time of any of the contents
  my $modtime = 0;
  my %modtime;
  $self->set_pack_method( @files );
  foreach (@files) {
    my $m = (stat($_))[9];
    $modtime{$_} = $m;
    $modtime = $m if defined $m && $modtime < $m;
  }

  my $reindex      = $force_reindex || $indextime < $modtime;
  $self->{offsets} = $self->_open_index($index,$reindex) or return;

  # no indexing needed
  return $self->{offsets} unless $reindex;

  # otherwise reindex contents of changed files
  $self->{indexing} = $index;
  foreach (@files) {
    next if( defined $indextime && $modtime{$_} <= $indextime);
    $self->calculate_offsets($_,$self->{offsets});
  }
  delete $self->{indexing};

  # we've been having troubles with corrupted index files on Windows systems,
  # so possibly closing and reopening will help
  $self->_close_index($self->{offsets});

  return $self->{offsets}  = $self->_open_index($index);
}

=head2 get_Seq_by_id

 Title   : get_Seq_by_id
 Usage   : my $seq = $db->get_Seq_by_id($id)
 Function: Bio::DB::RandomAccessI method implemented
 Returns : Bio::PrimarySeqI object
 Args    : id

=cut

sub get_Seq_by_id {
  my $self = shift;
  my $id   = shift;
  return unless exists $self->{offsets}{$id};
  return Bio::PrimarySeq::Fasta->new($self,$id);
}

=head2 set_pack_method

 Title   : set_pack_method
 Usage   : $db->set_pack_method( @files )
 Function: Determines whether data packing uses 32 or 64 bit integers
 Returns :
 Args    : one or more file paths

=cut

sub set_pack_method {
  my $self = shift;
  # Find the maximum file size:eq
  my ($maxsize) = sort { $b <=> $a } map { -s $_ } @_;
  my $fourGB    = (2 ** 32) - 1;

  if ($maxsize > $fourGB) {
      # At least one file exceeds 4Gb - we will need to use 64 bit ints
      $self->{packmeth}   = \&_packBig;
      $self->{unpackmeth} = \&_unpackBig;
  } else {
      $self->{packmeth}   = \&_pack;
      $self->{unpackmeth} = \&_unpack;
  }
}

=head2 index_file

 Title   : index_file
 Usage   : $db->index_file($filename)
 Function: (re)loads a sequence file and indexes sequences offsets in the file
 Returns : seq offsets in the file
 Args    : filename, 
           boolean to force reloading a file

=cut


sub index_file {
  my $self = shift;
  my $file = shift;
  my $force_reindex = shift;

  $self->set_pack_method( $file );
  my $index = $self->index_name($file);
  # if caller has requested reindexing, then unlink the index
  unlink $index if $force_reindex;

  # get the modification time of the index
  my $indextime = (stat($index))[9] || 0;
  my $modtime   = (stat($file))[9]  || 0;

  my $reindex = $force_reindex || $indextime < $modtime;
  my $offsets = $self->_open_index($index,$reindex) or return;
  $self->{offsets} = $offsets;

  return $self->{offsets} unless $reindex;

  $self->{indexing} = $index;
  $self->calculate_offsets($file,$offsets);
  delete $self->{indexing};
  return $self->{offsets};
}

=head2 dbmargs

 Title   : dbmargs
 Usage   : my @args = $db->dbmargs;
 Function: gets stored dbm arguments
 Returns : array
 Args    : none

=cut

sub dbmargs {
  my $self = shift;
  my $args = $self->{dbmargs} or return;
  return ref($args) eq 'ARRAY' ? @$args : $args;
}

=head2 index_name

 Title   : index_name
 Usage   : my $indexname = $db->index_name($path,$isdir);
 Function: returns the name of the index for a specific path 
 Returns : string
 Args    : path to check, 
           boolean if it is a dir

=cut

sub index_name {
  my $self  = shift;
  my ($path,$isdir) = @_;
  unless ($path) {
    my $dir = $self->{dirname} or return;
    return $self->index_name($dir,-d $dir);
  } 
  return "$path/directory.index" if $isdir;
  return "$path.index";
}

=head2 calculate_offsets

 Title   : calculate_offsets
 Usage   : $db->calculate_offsets($filename,$offsets);
 Function: calculates the sequence offsets in a file based on id
 Returns : offset hash for each file
 Args    : file to process
           $offsets - hashref of id to offset storage

=cut

sub calculate_offsets {
  my $self = shift;
  my ($file,$offsets) = @_;
  my $base = $self->path2fileno(basename($file));

  my $fh = IO::File->new($file) or $self->throw( "Can't open $file: $!");
  binmode $fh;
  warn "indexing $file\n" if $self->{debug};
  my ($offset,@id,$linelength,$type,$firstline,$count,
      $termination_length,$seq_lines,$last_line,%offsets);
  my ($l3_len,$l2_len,$l_len)=(0,0,0);

  while (<$fh>) {		# don't try this at home
    $termination_length ||= /\r\n$/ ? 2 : 1; # account for crlf-terminated Windows files
    next unless /\S/;
    if (index($_, ">") == 0) {
        if (/^>(\S+)/) {
          print STDERR "indexed $count sequences...\n" 
        if $self->{debug} && (++$count%1000) == 0;
          
        
          my $pos = tell($fh);
          if (@id) {
        my $seqlength    = $pos - $offset - length($_);
        $seqlength      -= $termination_length * $seq_lines;
        my $ppos = &{$self->{packmeth}}($offset,$seqlength,
                           $linelength,$firstline,
                           $type,$base);
        for my $id (@id) { $offsets->{$id}  = $ppos }
          }
          @id = ref($self->{makeid}) eq 'CODE' ? $self->{makeid}->($_) : $1;
          ($offset,$firstline,$linelength) = ($pos,length($_),0);
          $self->_check_linelength($linelength);
          ($l3_len,$l2_len,$l_len)=(0,0,0);
          $seq_lines = 0;
        } else {
          # catch bad header lines, bug 3172
          $self->throw("FASTA header doesn't match '>(\\S+)': $_")
        }
    } else {
      $l3_len= $l2_len; $l2_len= $l_len; $l_len= length($_); # need to check every line :(
      if (DIE_ON_MISSMATCHED_LINES &&
	  $l3_len>0 && $l2_len>0 && $l3_len!=$l2_len) {
	  my $fap= substr($_,0,20)."..";
	  $self->throw("Each line of the fasta entry must be the same length except the last.
   Line above #$. '$fap' is $l2_len != $l3_len chars.");
      }
      $linelength ||= length($_);
      $type       ||= $self->_type($_);
      $seq_lines++;
    }
    $last_line = $_;
  }

  $self->_check_linelength($linelength);
  # deal with last entry
  if (@id) {
    my $pos = tell($fh);
    my $seqlength   = $pos - $offset;
    if ($linelength == 0) { # yet another pesky empty chr_random.fa file
      $seqlength = 0;
    } else {
      if ($last_line !~ /\s$/) {
	$seq_lines--;
      }
      $seqlength -= $termination_length * $seq_lines;
    };
    my $ppos = &{$self->{packmeth}}($offset,$seqlength,
					   $linelength,$firstline,
					   $type,$base);
    for my $id (@id) { $offsets->{$id}  = $ppos }
  }
  $offsets->{__termination_length} = $termination_length;
  return \%offsets;
}

=head2 get_all_ids

 Title   : get_all_ids
 Usage   : my @ids = $db->get_all_ids
 Function: gets all the stored ids in all indexes
 Returns : list of ids
 Args    : none

=cut

sub get_all_ids  { grep {!/^__/} keys %{shift->{offsets}} }

sub offset {
  my $self = shift;
  my $id   = shift;
  my $offset = $self->{offsets}{$id} or return;
  (&{$self->{unpackmeth}}($offset))[0];
}

sub length {
  my $self = shift;
  my $id   = shift;
  my $offset = $self->{offsets}{$id} or return;
  (&{$self->{unpackmeth}}($offset))[1];
}

sub linelen {
  my $self = shift;
  my $id   = shift;
  my $offset = $self->{offsets}{$id} or return;
  (&{$self->{unpackmeth}}($offset))[2];
}

sub headerlen {
  my $self = shift;
  my $id   = shift;
  my $offset = $self->{offsets}{$id} or return;
  (&{$self->{unpackmeth}}($offset))[3];
}

sub alphabet {
  my $self = shift;
  my $id   = shift;
  my $offset = $self->{offsets}{$id} or return;
  my $type = (&{$self->{unpackmeth}}($offset))[4];
  return $type == DNA ? 'dna'
         : $type == RNA ? 'rna'
         : 'protein';

}

sub path { shift->{dirname} } 

sub header_offset {
    my $self = shift;
    my $id   = shift;
    return unless $self->{offsets}{$id};
    return $self->offset($id) - $self->headerlen($id);
}

sub file {
  my $self = shift;
  my $id   = shift;
  my $offset = $self->{offsets}{$id} or return;
  $self->fileno2path((&{$self->{unpackmeth}}($offset))[5]);
}

sub fileno2path {
  my $self = shift;
  my $no   = shift;
  return $self->{offsets}{"__file_$no"};
}

sub path2fileno {
  my $self = shift;
  my $path = shift;
  if ( !defined $self->{offsets}{"__path_$path"} ) {
    my $fileno  = ($self->{offsets}{"__path_$path"} = 0+ $self->{fileno}++);
    $self->{offsets}{"__file_$fileno"} = $path;
  }
  return $self->{offsets}{"__path_$path"}
}

sub _check_linelength {
  my $self       = shift;
  my $linelength = shift;
  return unless defined $linelength;
  $self->throw("Each line of the fasta file must be less than 65,536 characters.  Line $. is $linelength chars.")	if $linelength > 65535.

}

=head2 subseq

 Title   : subseq
 Usage   : $seqdb->subseq($id,$start,$stop);
 Function: returns a subseq of a sequence in the db
 Returns : subsequence data
 Args    : id of sequence, starting point, ending point

=cut

sub subseq {
  my ($self,$id,$start,$stop) = @_;
  if ($id =~ /^(.+):([\d_]+)(?:,|-|\.\.)([\d_]+)$/) {
    ($id,$start,$stop) = ($1,$2,$3);
    $start =~ s/_//g;
    $stop =~ s/_//g;
  }
  $start ||= 1;
  $stop  ||= $self->length($id);

  my $reversed;
  if (defined $stop && $start > $stop) {
    ($start,$stop) = ($stop,$start);
    $reversed++;
  }

  my $data;

  my $fh = $self->fh($id) or return;
  my $filestart = $self->caloffset($id,$start);
  my $filestop  = $self->caloffset($id,$stop);

  seek($fh,$filestart,0);
  read($fh,$data,$filestop-$filestart+1);
  $data =~ s/\n//g;
  $data =~ s/\r//g;
  if ($reversed) {
    $data = reverse $data;
    $data =~ tr/gatcGATC/ctagCTAG/;
  }
  $data;
}

sub fh {
  my $self = shift;
  my $id   = shift;
  my $file = $self->file($id) or return;
  $self->fhcache("$self->{dirname}/$file") or $self->throw( "Can't open file $file");
}

sub header {
  my $self = shift;
  my $id   = shift;
  my ($offset,$seqlength,$linelength,$firstline,$type,$file) 
    = &{$self->{unpackmeth}}($self->{offsets}{$id}) or return;
  $offset -= $firstline;
  my $data;
  my $fh = $self->fh($id) or return;
  seek($fh,$offset,0);
  read($fh,$data,$firstline);
  chomp $data;
  substr($data,0,1) = '';
  $data;
}

sub caloffset {
  my $self = shift;
  my $id   = shift;
  my $a    = shift()-1;
  my ($offset,$seqlength,$linelength,$firstline,$type,$file) = &{$self->{unpackmeth}}($self->{offsets}{$id});
  $a = 0            if $a < 0;
  $a = $seqlength-1 if $a >= $seqlength;
  my $tl = $self->{offsets}{__termination_length};
  $offset + $linelength * int($a/($linelength-$tl)) + $a % ($linelength-$tl);
}

sub fhcache {
  my $self = shift;
  my $path = shift;
  if (!$self->{fhcache}{$path}) {
    if ($self->{curopen} >= $self->{maxopen}) {
      my @lru = sort {$self->{cacheseq}{$a} <=> $self->{cacheseq}{$b};} keys %{$self->{fhcache}};
      splice(@lru, $self->{maxopen} / 3);
      $self->{curopen} -= @lru;
      for (@lru) { delete $self->{fhcache}{$_} }
    }
    $self->{fhcache}{$path} = IO::File->new($path) or return;
    binmode $self->{fhcache}{$path};
    $self->{curopen}++;
  }
  $self->{cacheseq}{$path}++;
  $self->{fhcache}{$path}
}

sub _pack {
  pack STRUCT,@_;
}

sub _packBig {
  pack STRUCTBIG,@_;
}

sub _unpack {
  unpack STRUCT,shift;
}

sub _unpackBig {
  unpack STRUCTBIG,shift;
}

sub _type {
  shift;
  local $_ = shift;
  return /^[gatcnGATCN*-]+$/   ? DNA
         : /^[gaucnGAUCN*-]+$/ ? RNA
	 : PROTEIN;
}

=head2 get_PrimarySeq_stream

 Title   : get_PrimarySeq_stream
 Usage   :
 Function:
 Example :
 Returns : 
 Args    :

=cut

sub get_PrimarySeq_stream {
  my $self = shift;
  return Bio::DB::Fasta::Stream->new($self);
}

sub TIEHASH {
  my $self = shift;
  return $self->new(@_);
}

sub FETCH {
  shift->subseq(@_);
}
sub STORE {
    shift->throw("Read-only database");
}
sub DELETE {
    shift->throw("Read-only database");
}
sub CLEAR {
    shift->throw("Read-only database");
}
sub EXISTS {
  defined shift->offset(@_);
}
sub FIRSTKEY { tied(%{shift->{offsets}})->FIRSTKEY(@_); }
sub NEXTKEY  { tied(%{shift->{offsets}})->NEXTKEY(@_);  }

sub DESTROY {
  my $self = shift;
  if ($self->{indexing}) {  # killed prematurely, so index file is no good!
    warn "indexing was interrupted, so unlinking $self->{indexing}";
    unlink $self->{indexing};
  }
}

#-------------------------------------------------------------
# Bio::PrimarySeqI compatibility
#
package Bio::PrimarySeq::Fasta;
use overload '""' => 'display_id';

use base qw(Bio::Root::Root Bio::PrimarySeqI);

sub new {
  my $class = shift;
  $class = ref($class) if ref $class;
  my ($db,$id,$start,$stop) = @_;
  return bless { db    => $db,
		 id    => $id,
		 start => $start || 1,
		 stop  => $stop  || $db->length($id)
	       },$class;
}

sub fetch_sequence { shift->seq(@_) }

sub seq {
  my $self = shift;
  return $self->{db}->seq($self->{id},$self->{start},$self->{stop});
}

sub subseq {
  my $self = shift;
  $self->trunc(@_)->seq();	
}

sub trunc {
  my $self = shift;
  my ($start,$stop) = @_;
  $self->throw("Stop cannot be smaller than start")  unless $start <= $stop;
  return $self->{start} <= $self->{stop} ?  $self->new($self->{db},
						       $self->{id},
						       $self->{start}+$start-1,
						       $self->{start}+$stop-1)
                                         :  $self->new($self->{db},
						       $self->{id},
						       $self->{start}-($start-1),
						       $self->{start}-($stop-1)
						      );  
	
}

sub is_circular {
  my $self = shift;
  return $self->{is_circular};
}

sub display_id {
  my $self = shift;
  return $self->{id};
}

sub accession_number {
  my $self = shift;
  return "unknown";
}

sub primary_id {
  my $self = shift;
  return overload::StrVal($self);
}

sub can_call_new { return 0 }

sub alphabet {
  my $self = shift;
  return $self->{db}->alphabet($self->{id});
}

sub revcom {
  my $self = shift;
  return $self->new(@{$self}{'db','id','stop','start'});
}

sub length {
  my $self = shift;
  #return $self->{db}->length($self->{id}); # wrong because ignores sequence start and stop values
  return length($self->seq);

}

sub description  { 
    my $self = shift;
    my $header = $self->{'db'}->header($self->{id});
    # remove the id from the header
    return (split(/\s+/,$header,2))[1];
}

*desc = \&description;

#-------------------------------------------------------------
# stream-based access to the database
#
package Bio::DB::Fasta::Stream;
use base qw(Tie::Handle Bio::DB::SeqI);


sub new {
  my $class = shift;
  my $db    = shift;
  my $key = $db->FIRSTKEY;
  return bless { db=>$db,key=>$key },$class;
}

sub next_seq {
  my $self = shift;
  my ($key,$db) = @{$self}{'key','db'};
  while ($key =~ /^__/) {
    $key = $db->NEXTKEY($key);
    return unless defined $key;
  }
  my $value = $db->get_Seq_by_id($key);
  $self->{key} = $db->NEXTKEY($key);
  $value;
}

sub TIEHANDLE {
  my $class = shift;
  my $db    = shift;
  return $class->new($db);
}
sub READLINE {
  my $self = shift;
  $self->next_seq;
}

1;

__END__