~ubuntu-branches/ubuntu/trusty/bioperl/trusty

« back to all changes in this revision

Viewing changes to scripts/DB/bp_bioflat_index.pl

  • Committer: Package Import Robot
  • Author(s): Charles Plessy
  • Date: 2013-09-22 13:39:48 UTC
  • mfrom: (3.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20130922133948-c6z62zegjyp7ztou
Tags: 1.6.922-1
* New upstream release.
* Replaces and Breaks grinder (<< 0.5.3-3~) because of overlaping contents.
  Closes: #722910
* Stop Replacing and Breaking bioperl ( << 1.6.9 ): not needed anymore. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
 
 
3
 
 
4
=head1 NAME
 
5
 
 
6
bp_bioflat_index.pl - index sequence files using Bio::DB::Flat
 
7
 
 
8
=head1 DESCRIPTION
 
9
 
 
10
 Create or update a biological sequence database indexed with the
 
11
 Bio::DB::Flat indexing scheme.  The arguments are a list of flat files
 
12
 containing the sequence information to be indexed.
 
13
 
 
14
=head1 USAGE
 
15
 
 
16
 bp_bioflat_index.pl <options> file1 file2 file3...
 
17
 
 
18
 Options:
 
19
 
 
20
     --create              Create or reinitialize the index.  If not specified,
 
21
                           the index must already exist.
 
22
 
 
23
     --format   <format>   The format of the sequence files.  Must be one
 
24
                           of "genbank", "swissprot", "embl" or "fasta".
 
25
 
 
26
     --location <path>     Path to the directory in which the index files
 
27
                           are stored.
 
28
 
 
29
     --dbname <name>       The symbolic name of the database to be created.
 
30
 
 
31
     --indextype <type>    Type of index to create.  Either "bdb" or "flat".
 
32
                           "binarysearch" is the same as "flat".
 
33
 
 
34
Options can be abbreviated.  For example, use -i for --indextype.
 
35
 
 
36
The following environment variables will be used as defaults if the 
 
37
corresponding options are not provided:
 
38
 
 
39
     OBDA_FORMAT      format of sequence file
 
40
     OBDA_LOCATION    path to directory in which index files are stored
 
41
     OBDA_DBNAME      name of database
 
42
     OBDA_INDEX       type of index to create
 
43
 
 
44
=cut
 
45
 
 
46
use strict;
 
47
use warnings;
 
48
use Bio::Root::Root;
 
49
use Bio::Root::IO;
 
50
use Bio::DB::Flat;
 
51
use Getopt::Long;
 
52
use File::Path qw(mkpath rmtree);
 
53
 
 
54
my ($CREATE,$FORMAT,$LOCATION,$DBNAME,$INDEXTYPE);
 
55
 
 
56
GetOptions( 'create'      => \$CREATE,
 
57
                                'format:s'    => \$FORMAT,
 
58
                                'location:s'  => \$LOCATION,
 
59
                                'dbname:s'    => \$DBNAME,
 
60
                                'indextype:s' => \$INDEXTYPE );
 
61
 
 
62
$FORMAT    = $ENV{OBDA_FORMAT}    unless defined $FORMAT;
 
63
$LOCATION  = $ENV{OBDA_LOCATION}  unless defined $LOCATION;
 
64
$DBNAME    = $ENV{OBDA_DBNAME}    unless defined $DBNAME;
 
65
$INDEXTYPE = $ENV{OBDA_INDEXTYPE} unless defined $INDEXTYPE;
 
66
 
 
67
my $root = 'Bio::Root::Root';
 
68
my $io   = 'Bio::Root::IO';
 
69
 
 
70
# confirm that database directory is there
 
71
defined $LOCATION or 
 
72
  $root->throw("please provide a base directory with the --location option");
 
73
 
 
74
-d $LOCATION or 
 
75
  $root->throw("$LOCATION is not a valid directory; use --create to create a new index");
 
76
 
 
77
defined $DBNAME or 
 
78
  $root->throw("please provide a database name with the --dbname option");
 
79
 
 
80
defined $FORMAT or 
 
81
  $root->throw("please specify the format for the input files with the --format option");
 
82
 
 
83
unless (defined $INDEXTYPE) {
 
84
        $INDEXTYPE = 'flat';
 
85
        $root->warn('setting index type to "flat", use the --indextype option to override');
 
86
}
 
87
 
 
88
# Confirm that database is there and that --create flag is sensible.
 
89
my $path = $io->catfile($LOCATION,$DBNAME,'config.dat');
 
90
if (-e $path) {
 
91
  if ($CREATE) {
 
92
    $root->warn("existing index detected; deleting.");
 
93
    rmtree($io->catfile($LOCATION,$DBNAME),1,1);
 
94
  } else {
 
95
    $root->warn("existing index detected; ignoring --indextype and --format options.");
 
96
    undef $INDEXTYPE;
 
97
  }
 
98
}
 
99
elsif (!$CREATE) {
 
100
  $root->throw("Cannot find database config file at location $path; use --create to create a new index");
 
101
}
 
102
 
 
103
# open for writing/updating
 
104
my $db = Bio::DB::Flat->new(-directory  => $LOCATION,
 
105
                                                                         -dbname     => $DBNAME,
 
106
                                                                         $INDEXTYPE ? (
 
107
                                                                                                                -index      => $INDEXTYPE
 
108
                                                                                                          )
 
109
                                                                         : (),
 
110
                                                                         -write_flag => 1,
 
111
                                                                         -format     => $FORMAT) or 
 
112
  $root->throw("can't create Bio::DB::Flat object");
 
113
 
 
114
my $entries = $db->build_index(@ARGV);
 
115
 
 
116
print STDERR "(Re)indexed $entries entries.\n ";
 
117
 
 
118
__END__