#!/usr/bin/perl
my
(
$CREATE
,
$FORMAT
,
$LOCATION
,
$DBNAME
,
$INDEXTYPE
);
GetOptions(
'create'
=> \
$CREATE
,
'format:s'
=> \
$FORMAT
,
'location:s'
=> \
$LOCATION
,
'dbname:s'
=> \
$DBNAME
,
'indextype:s'
=> \
$INDEXTYPE
);
$FORMAT
=
$ENV
{OBDA_FORMAT}
unless
defined
$FORMAT
;
$LOCATION
=
$ENV
{OBDA_LOCATION}
unless
defined
$LOCATION
;
$DBNAME
=
$ENV
{OBDA_DBNAME}
unless
defined
$DBNAME
;
$INDEXTYPE
=
$ENV
{OBDA_INDEXTYPE}
unless
defined
$INDEXTYPE
;
my
$root
=
'Bio::Root::Root'
;
my
$io
=
'Bio::Root::IO'
;
defined
$LOCATION
or
$root
->throw(
"please provide a base directory with the --location option"
);
-d
$LOCATION
or
$root
->throw(
"$LOCATION is not a valid directory; use --create to create a new index"
);
defined
$DBNAME
or
$root
->throw(
"please provide a database name with the --dbname option"
);
defined
$FORMAT
or
$root
->throw(
"please specify the format for the input files with the --format option"
);
unless
(
defined
$INDEXTYPE
) {
$INDEXTYPE
=
'flat'
;
$root
->
warn
(
'setting index type to "flat", use the --indextype option to override'
);
}
my
$path
=
$io
->catfile(
$LOCATION
,
$DBNAME
,
'config.dat'
);
if
(-e
$path
) {
if
(
$CREATE
) {
$root
->
warn
(
"existing index detected; deleting."
);
rmtree(
$io
->catfile(
$LOCATION
,
$DBNAME
),1,1);
}
else
{
$root
->
warn
(
"existing index detected; ignoring --indextype and --format options."
);
undef
$INDEXTYPE
;
}
}
elsif
(!
$CREATE
) {
$root
->throw(
"Cannot find database config file at location $path; use --create to create a new index"
);
}
my
$db
= Bio::DB::Flat->new(
-directory
=>
$LOCATION
,
-dbname
=>
$DBNAME
,
$INDEXTYPE
? (
-index
=>
$INDEXTYPE
)
: (),
-write_flag
=> 1,
-format
=>
$FORMAT
) or
$root
->throw(
"can't create Bio::DB::Flat object"
);
my
$entries
=
$db
->build_index(
@ARGV
);
print
STDERR
"(Re)indexed $entries entries.\n "
;