| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | # documentation at end of file | ||
| 4 | |||
| 5 | use warnings; | ||
| 6 | use strict; | ||
| 7 | use English qw(-no_match_vars); | ||
| 8 | use Getopt::Long qw(:config no_ignore_case bundling); | ||
| 9 | use Pod::Usage; | ||
| 10 | use File::Spec; | ||
| 11 | use FindBin qw($Bin); | ||
| 12 | use Bio::ToolBox::db_helper::config qw(add_database); | ||
| 13 | use Bio::ToolBox::db_helper qw(open_db_connection); | ||
| 14 | use Bio::DB::SeqFeature::Store; | ||
| 15 | use Bio::DB::SeqFeature::Store::GFF3Loader; | ||
| 16 | |||
| 17 | # check for additional requirements | ||
| 18 | my $sql = 0; | ||
| 19 | eval { | ||
| 20 | require DBD::SQLite; | ||
| 21 | $sql = 1; | ||
| 22 | }; | ||
| 23 | |||
| 24 | our $VERSION = '2.00'; | ||
| 25 | |||
| 26 | print "\n This program will set up an annotation database\n\n"; | ||
| 27 | |||
| 28 | ### Quick help | ||
| 29 | unless (@ARGV) { | ||
| 30 | |||
| 31 | # when no command line options are present | ||
| 32 | # print SYNOPSIS | ||
| 33 | pod2usage( | ||
| 34 | { | ||
| 35 | '-verbose' => 0, | ||
| 36 | '-exitval' => 1, | ||
| 37 | } | ||
| 38 | ); | ||
| 39 | } | ||
| 40 | |||
| 41 | ### Get command line options and initialize values | ||
| 42 | my ( $ucscdb, $path, $keep, $verbose, $help, $print_version, ); | ||
| 43 | my @tables; | ||
| 44 | |||
| 45 | # Command line options | ||
| 46 | GetOptions( | ||
| 47 | 'd|db=s' => \$ucscdb, # the UCSC database shortname | ||
| 48 | 'p|path=s' => \$path, # the optional path for the database | ||
| 49 | 't|table=s' => \@tables, # which tables to collect | ||
| 50 | 'k|keep!' => \$keep, # keep the annotation files | ||
| 51 | 'V|verbose!' => \$verbose, # show db loading | ||
| 52 | 'h|help' => \$help, # request help | ||
| 53 | 'v|version' => \$print_version, # print the version | ||
| 54 | ) or die " unrecognized option(s)!! please refer to the help documentation\n\n"; | ||
| 55 | |||
| 56 | # Print help | ||
| 57 | if ($help) { | ||
| 58 | |||
| 59 | # print entire POD | ||
| 60 | pod2usage( | ||
| 61 | { | ||
| 62 | '-verbose' => 2, | ||
| 63 | '-exitval' => 1, | ||
| 64 | } | ||
| 65 | ); | ||
| 66 | } | ||
| 67 | |||
| 68 | # Print version | ||
| 69 | if ($print_version) { | ||
| 70 | print " BioToolBox script db_setup.pl, version $VERSION\n"; | ||
| 71 | eval { | ||
| 72 | require Bio::ToolBox; | ||
| 73 | my $v = Bio::ToolBox->VERSION; | ||
| 74 | print " Biotoolbox package version $v\n"; | ||
| 75 | }; | ||
| 76 | exit; | ||
| 77 | } | ||
| 78 | |||
| 79 | ### Check for requirements | ||
| 80 | unless ($sql) { | ||
| 81 | die " Please install Perl module DBD::SQLite to set up a database\n"; | ||
| 82 | } | ||
| 83 | unless ($ucscdb) { | ||
| 84 | $ucscdb = shift @ARGV | ||
| 85 | or die " no database name provided! use --help for more information\n"; | ||
| 86 | } | ||
| 87 | if ($path) { | ||
| 88 | $path = File::Spec->rel2abs($path); | ||
| 89 | unless ( -e $path ) { | ||
| 90 | mkdir $path or die "unable to make database path $path $OS_ERROR\n"; | ||
| 91 | } | ||
| 92 | unless ( -w _ ) { | ||
| 93 | die " $path is not writable!\n"; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | else { | ||
| 97 | # determine a path | ||
| 98 | if ( -e File::Spec->catdir( $ENV{HOME}, 'Library' ) ) { | ||
| 99 | $path = File::Spec->catdir( $ENV{HOME}, 'Library' ); | ||
| 100 | } | ||
| 101 | elsif ( -e File::Spec->catdir( $ENV{HOME}, 'lib' ) ) { | ||
| 102 | $path = File::Spec->catdir( $ENV{HOME}, 'lib' ); | ||
| 103 | } | ||
| 104 | else { | ||
| 105 | # make one for us | ||
| 106 | $path = File::Spec->catdir( $ENV{HOME}, 'lib' ); | ||
| 107 | mkdir $path or die "unable to make database path $path $OS_ERROR\n"; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | if (@tables) { | ||
| 111 | if ( $tables[0] =~ /,/ ) { | ||
| 112 | my $t = shift @tables; | ||
| 113 | @tables = split /,/, $t; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | @tables = qw(refgene knowngene); | ||
| 118 | } | ||
| 119 | my $start_time = time; | ||
| 120 | |||
| 121 | ### Get UCSC annotation | ||
| 122 | print "##### Fetching annotation from UCSC. This may take a while ######\n"; | ||
| 123 | system( File::Spec->catdir( $Bin, 'ucsc_table2gff3.pl' ), | ||
| 124 | '--db', $ucscdb, '--ftp', join( ',', @tables ), '--gz' ) == 0 | ||
| 125 | or die "unable to execute ucsc_table2gff3 script!\n"; | ||
| 126 | my @gff = glob("$ucscdb*.gff3.gz"); | ||
| 127 | my @source = glob("$ucscdb*.txt.gz"); | ||
| 128 | unless (@gff) { | ||
| 129 | die "unable to find new GFF3 files!\n"; | ||
| 130 | } | ||
| 131 | |||
| 132 | ### Build database | ||
| 133 | print "##### Building database. This may take a while ######\n"; | ||
| 134 | my $database = File::Spec->catdir( $path, "$ucscdb.sqlite" ); | ||
| 135 | my $temp = File::Spec->tmpdir(); | ||
| 136 | |||
| 137 | # create a database | ||
| 138 | my $store = Bio::DB::SeqFeature::Store->new( | ||
| 139 | -dsn => $database, | ||
| 140 | -adaptor => 'DBI::SQLite', | ||
| 141 | -tmpdir => $temp, | ||
| 142 | -write => 1, | ||
| 143 | -create => 1, | ||
| 144 | -compress => 0, # compression seems to be broken, cannot read db | ||
| 145 | ) or die " Cannot create a SeqFeature database connection!\n"; | ||
| 146 | |||
| 147 | # load the database | ||
| 148 | my $loader = Bio::DB::SeqFeature::Store::GFF3Loader->new( | ||
| 149 | -store => $store, | ||
| 150 | -sf_class => 'Bio::DB::SeqFeature', | ||
| 151 | -verbose => $verbose, | ||
| 152 | -tmpdir => $temp, | ||
| 153 | -fast => 1, | ||
| 154 | -ignore_seqregion => 0, | ||
| 155 | -index_subfeatures => 1, | ||
| 156 | -noalias_target => 0, | ||
| 157 | -summary_stats => 0, | ||
| 158 | ) or die " Cannot create a GFF3 loader for the database!\n"; | ||
| 159 | |||
| 160 | # on signals, give objects a chance to call their DESTROY methods | ||
| 161 | # borrowed from bp_seqfeature_load.pl | ||
| 162 | local $SIG{TERM} = local $SIG{INT} = | ||
| 163 | sub { undef $loader; undef $store; die "Aborted..."; }; | ||
| 164 | $loader->load(@gff); | ||
| 165 | |||
| 166 | ### Check database | ||
| 167 | my $db; | ||
| 168 | if ( -e $database and -s _ ) { | ||
| 169 | $db = open_db_connection($database); | ||
| 170 | } | ||
| 171 | if ($db) { | ||
| 172 | print "\n##### Created database $database ######\n"; | ||
| 173 | printf " Finished in %.1f minutes\n\n", ( time - $start_time ) / 60; | ||
| 174 | my $a = add_database( | ||
| 175 | 'name' => $ucscdb, | ||
| 176 | 'dsn' => $database, | ||
| 177 | 'adaptor' => 'DBI::SQLite', | ||
| 178 | ); | ||
| 179 | if ($a) { | ||
| 180 | print <<SUCCESS; | ||
| 181 | The database configuration was added to the BioToolBox configuration | ||
| 182 | file. You may use the database in any BioToolBox script with the | ||
| 183 | option --db $ucscdb. | ||
| 184 | |||
| 185 | You can check the database now by running | ||
| 186 | db_types.pl $ucscdb | ||
| 187 | SUCCESS | ||
| 188 | } | ||
| 189 | } | ||
| 190 | else { | ||
| 191 | print "##### Something went wrong! Database could not be opened #####\n"; | ||
| 192 | unlink $database if -e $database; | ||
| 193 | } | ||
| 194 | |||
| 195 | ### Clean up | ||
| 196 | if ( $db and not $keep ) { | ||
| 197 | unlink @gff; | ||
| 198 | unlink @source; | ||
| 199 | } | ||
| 200 | |||
| 201 | __END__ |