fixing typo in SQL
[cxgn-corelibs.git] / bin / bdb_update_blast_dbs.pl
blob606f530bc703942b0b04719c3f1108c622db5de0
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use English;
5 use Carp;
6 #$Carp::Verbose = 1;
7 use FindBin;
8 use Getopt::Std;
10 #use Data::Dumper;
12 use File::Spec;
13 use File::Temp qw/tempfile/;
15 use CXGN::Tools::Wget qw/wget_filter/;
16 use CXGN::BlastDB;
18 sub usage {
19 my $message = shift || '';
20 $message = "Error: $message\n" if $message;
22 my $file_bases = join '', sort map ' '.$_->file_base."\n", CXGN::BlastDB->retrieve_all;
24 die <<EOU;
25 $message
26 Usage:
27 $FindBin::Script [ options ] -d <path>
29 Go over all the BLAST databases we keep in stock and update them if
30 needed. When run with just the -g option, goes over all the BLAST
31 dbs listed in the sgn.blast_db table and updates them if needed,
32 putting them under the top-level BLAST db path given with the -d
33 option.
35 Options:
37 -d <path> required. path where all blast DB files are expected to go.
39 -t <path> path to put tempfiles. must be writable. Defaults to /tmp.
41 -x dry run, just print what you would update
43 -f <db name> force-update the DB with the given file base (e.g. 'genbank/nr')
45 Current list of file_bases:
46 $file_bases
47 EOU
50 our %opt;
51 getopts('xt:d:f:',\%opt) or usage('invalid arguments');
52 $opt{t} ||= File::Spec->tmpdir;
54 #if a alternate blast dbs path was given, set it in the BlastDB
55 #object
56 $opt{d} or usage('-d option is required');
57 -d $opt{d} or usage("directory $opt{d} not found");
58 CXGN::BlastDB->dbpath($opt{d});
60 my @dbs = $opt{f} ? CXGN::BlastDB->search( file_base => $opt{f} )
61 : CXGN::BlastDB->retrieve_all;
62 unless(@dbs) {
63 print $opt{f} ? "No database found with file_base='$opt{f}'.\n"
64 : "No dbs found in database.\n";
67 foreach my $db (@dbs) {
69 #check if the blast db needs an update
70 unless($opt{f} || $db->needs_update) {
71 print $db->file_base." is up to date.\n";
72 next;
75 #skip the DB if it does not have a source url defined
76 unless($db->source_url) {
77 warn $db->file_base." needs to be updated, but has no source_url. Skipped.\n";
78 next;
81 if( $opt{x} ) {
82 print "Would update ".$db->file_base." from source url ".$db->source_url."\n";
83 next;
84 } else {
85 print "Updating ".$db->file_base." from source url...\n";
88 eval {
89 # check whether we have permissions to do the format
90 if( my $perm_error = $db->check_format_permissions() ) {
91 die "Cannot format ".$db->file_base.":\n$perm_error";
94 #download the sequences from the source url to a tempfile
95 print "Downloading source (".$db->source_url.")...\n";
96 my (undef,$sourcefile) = tempfile('blastdb-source-XXXXXXXX',
97 DIR => $opt{t},
98 UNLINK => 1,
101 my $wget_opts = { cache => 0 };
102 $wget_opts->{gunzip} = 1 if $db->source_url =~ /\.gz$/i;
103 wget_filter( $db->source_url => $sourcefile, $wget_opts );
105 #formatdb it into the correct place
106 print "Formatting database...\n";
107 $db->format_from_file($sourcefile);
109 unlink $sourcefile or warn "$! unlinking tempfile '$sourcefile'";
111 print $db->file_base." done.\n";
112 }; if( $EVAL_ERROR ) {
113 print "Update failed for ".$db->file_base.":\n$EVAL_ERROR";