add the two cyclic ref testers (not required for general users, but nice
[bioperl-live.git] / scripts / index / bp_index.pl
blobb636da0bf2673712adddb43ba58b0d2fe675c798
1 #!/usr/bin/perl
3 =head1 NAME
5 bp_index.pl - indexes files for use by bp_fetch.pl
7 =head1 SYNOPSIS
9 bp_index.pl index_name file1 file2 etc.
11 =head1 DESCRIPTION
13 bp_index.pl builds a bioperl index for the sequence files given in the
14 argument list, under the index name. For example
16 bp_index.pl nrdb /data/nrdb/nrdb.fasta
18 would build an index called 'nrdb' as the index name for the file
19 nrdb.fasta, and
21 bp_index.pl -fmt EMBL swiss /data/swiss/*.dat
23 would build an index called swiss for all the files in /data/swiss
24 which end in .dat which are in EMBL format.
26 The indexes are built using the Bio/Index/* modules, in particular,
27 Bio::Index::EMBL and the Bio::Index::Fasta modules. Any script which
28 uses these modules can use the index. A good example script is bp_fetch
29 which fetches sequences and pipes them to STDOUT, for example
31 bp_fetch swiss:ROA1_HUMAN
33 gets the ROA1_HUMAN sequence from the swiss index and writes it as
34 fasta format on STDOUT.
36 =head1 OPTIONS
38 -fmt <format> - Fasta (default), swiss or EMBL
39 -dir <dir> - directory where the index files are found
40 (overrides BIOPERL_INDEX environment variable)
42 Options for expert use
44 -type <db_type> - DBM_file type.
45 (overrides BIOPERL_INDEX_TYPE environment variable)
46 -v - report every index addition (debugging)
48 =head1 ENVIRONMENT
50 bp_index and bp_fetch coordinate where the databases lie using the
51 enviroment variable BIOPERL_INDEX. This can be overridden using the
52 -dir option. There is no default value, so you must use the -dir option
53 or set BIOPERL_INDEX.
55 The DB type is coordinated with BIOPERL_INDEX_TYPE which if it
56 is not there, defaults to whatever the bioperl modules have installed,
57 which itself defaults to SDBM_File.
59 =head1 USING IT YOURSELF
61 bp_index.pl is a script that drives the Index modules. If you want to
62 use this script heavily in your work, if it is Perl based, it is
63 almost certainly better to look at the code in this script and copy
64 it across (probably you will be more likely to want to use the bp_fetch
65 code).
67 =head1 EXTENDING IT
69 bp_index is just a wrapper around James Gilbert's excellent Index modules
70 found in bioperl
72 =head1 FEEDBACK
74 =head2 Mailing Lists
76 User feedback is an integral part of the evolution of this and other
77 Bioperl modules. Send your comments and suggestions preferably to
78 the Bioperl mailing list. Your participation is much appreciated.
80 bioperl-l@bioperl.org - General discussion
81 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
83 =head2 Reporting Bugs
85 Report bugs to the Bioperl bug tracking system to help us keep track
86 of the bugs and their resolution. Bug reports can be submitted via the
87 web:
89 https://github.com/bioperl/bioperl-live/issues
91 =head1 AUTHOR - Ewan Birney
93 Ewan Birney E<lt>birney@ebi.ac.ukE<gt>
95 =cut
98 use strict;
99 use warnings;
102 # Doofus catcher for people who are trying this script without
103 # installing bioperl
106 BEGIN {
107 eval {
108 require Bio::Index::Fasta;
109 require Bio::Index::EMBL;
110 require Bio::Index::Swissprot;
111 require Bio::Index::GenBank;
112 require Bio::Index::SwissPfam;
114 if ( $@ ) {
115 # one up from here is Bio directory - we hope!
116 push(@INC,"..");
117 eval {
118 require Bio::Index::Fasta;
119 require Bio::Index::EMBL;
121 if ( $@ ) {
122 print STDERR ("\nbp_index cannot find Bio::Index::Fasta and Bio::Index::EMBL\nbp_index needs to have bioperl installed for it to run.\nBioperl is very easy to install\nSee http://bio.perl.org for more information\n\n");
123 exit(1);
124 } else {
125 print STDERR ("\nYou are running bp_index.pl without installing bioperl.\nYou have done it from bioperl/scripts, and so we can find the necessary information\nbut it is much better to install bioperl\n\nPlease read the README in the bioperl distribution\n\n");
130 my $dir = $ENV{'BIOPERL_INDEX'};
131 my $type = $ENV{'BIOPERL_INDEX_TYPE'};
132 my $fmt = 'Fasta';
133 my $verbose = 0;
135 use Getopt::Long;
136 &GetOptions("f|fmt=s" => \$fmt,
137 "d|dir=s" => \$dir,
138 "t|type=s" => \$type,
139 "v!" => \$verbose);
141 exec('perldoc',$0) unless @ARGV;
143 my $name = shift;
145 if( !$dir ) {
146 print STDERR "\nNo directory specified for index\nDirectory must be specified by the environment varaible BIOPERL_INDEX or -dir option\ngo bp_index with no arguments for more help\n\n";
147 exit(1);
151 # Reset the type if needed
154 if( $type ) {
155 $Bio::Index::Abstract::USE_DBM_TYPE = $type;
158 # Rock and roll...
160 my $index;
161 $_ = $fmt;
162 SWITCH : {
163 /Fasta/i && do {
164 $index = Bio::Index::Fasta->new("$dir/$name", 'WRITE');
165 last;
167 /EMBL/i && do {
168 $index = Bio::Index::EMBL->new("$dir/$name", 'WRITE');
169 last;
171 /swisspfam|pfam/i && do {
172 $index = Bio::Index::SwissPfam->new("$dir/$name", 'WRITE');
173 last;
175 /swiss/i && do {
176 $index = Bio::Index::Swissprot->new("$dir/$name", 'WRITE');
177 last;
179 /GenBank/i && do {
180 $index = Bio::Index::GenBank->new("$dir/$name", 'WRITE');
181 last;
183 die("No index format called $fmt");
186 if( $verbose != 0 ) {
187 $index->verbose(1);
190 $index->make_index(@ARGV);
192 # finished. Neat eh.
195 # if you are using this in a script, to
196 # to force deallocation + closing of the index, go
197 # $index = undef;