#! /usr/bin/perl # Time-stamp: <2005-05-04 12:50:03 kasper> use warnings; use strict; use SQLWriter; use Getopt::Long; use Bio::SearchIO; use DBI; use constant USAGE =>< ... DESCRIPTION: This script creates database from blast report. OPTIONS: --help Prints this help. --quiet Makes the script run without progress messages. --prefix Prefix to names of tables. --noempty Skips blast reports with no hits. --overwrite Forces overwrite of old database of same name. --password Promts for database password. --host Name of machine hosting the database. --user Name of the user. Defualts to the environmental variable USER. --port Port number. EXAMPLES: blast2sql.pl --prefix myblast_ --noempty mydatabase blast.report # .my.cnf may look like this: [client] host=appserver user=kurt password=kurt DEPENDENCIES: A MySQL database. The class: SQLWriter. It is not on CPAN but send me an email (kasper at binf.ku.dk) and I will be more than happy to send it. AUTHOR: Kasper Munch COPYRIGHT: This program is free software. You may copy and redistribute it under the same terms as Perl itself. END my $help = 0; my $prefix = ''; my $noempty = 0; my $overwrite = 0; my $quiet = 0; my $adding = 0; my $password = ''; my $user = $ENV{USER}; my $host = 'db'; my $port = 3306; GetOptions( "help" => \$help, "prefix=s" => \$prefix, "quiet" => \$quiet, "overwrite" => \$overwrite, "noempty" => \$noempty, "password" => \$password, "host=s" => \$host, "user=s" => \$user, "port=i" => \$port ) or die USAGE; $help and die USAGE; my $database = shift @ARGV or die $!; my @files = @ARGV or die "You have to specify at least one blast report file.\n"; my $files = scalar @files; # Ask for password if option specified without option value: if ($password) { #turn off echoing but don't interfere with STDIN open (TTY, "/dev/tty") or die "Cannot open terminal\n"; system ("stty -echo < /dev/tty"); print STDERR "Enter password: "; chomp ($password = ); system ("stty echo < /dev/tty"); close (TTY); print STDERR "\n"; } # Defualt password: $password ||= 'kasper'; my $result = "${prefix}result"; my $hit = "${prefix}hit"; my $hsp = "${prefix}hsp"; # Connect to the database: my $dsn = "DBI:mysql:$database:$host;port=$port"; my %attr = ( RaiseError => 1, AutoCommit => 0 ); my $dbh = DBI->connect ($dsn, $user, $password, \%attr); if ($overwrite) { eval { $dbh->do("DROP TABLE $result") if exist($dbh, $result); $dbh->do("DROP TABLE $hit") if exist($dbh, $hit); $dbh->do("DROP TABLE $hsp") if exist($dbh, $hsp); $dbh->commit; }; if ($@) { $dbh->rollback; die "Failed dropping existing tables."; } } # Create a SQLWriter obj.: my $writer = SQLWriter->new(dbh => $dbh, result => $result, hit => $hit, hsp => $hsp, noempty => $noempty # this is new. ); print STDERR "\nDumping blastreport file to database: $database" if !$quiet and $files == 1; print STDERR ".\n\n" unless $quiet; foreach my $file (@files) { my $searchio = new Bio::SearchIO(-format => 'blast', -file => $file); # $writer->dumpsql($searchio, $noempty); $writer->dumpsql($searchio); # This is new. } $dbh->disconnect() or warn $dbh->errstr(); sub exist { my $table = shift; my $exist = 0; my $check = $dbh->prepare(qq{show tables}); $check->execute(); my $tables = $check->fetchall_arrayref(); for my $t (@$tables) { $exist = 1 if $$t[0] eq "$table"; } return $exist; } =head1 SYNOPSIS: blast2sql.pl [OPTIONS] ... =head1 DESCRIPTION: This script creates database from blast report. =head1 OPTIONS: =over 4 =item --help Prints this help. =item --quiet Makes the script run without progress messages. =item --prefix Prefix to names of tables. =item --noempty Skips blast reports with no hits. =item --overwrite Forces overwrite of old database of same name. =item --password Promts for database password. =item --host Name of machine hosting the database. =item --user Name of the user. Defualts to the environmental variable USER. =item --port Port number. =back =head1 EXAMPLES: blast2sql.pl --prefix myblast_ --noempty mydatabase blast.report # .my.cnf may look like this: [client] host=appserver user=kurt password=kurt =head1 DEPENDENCIES: A MySQL database. The class: SQLWriter. It is not on CPAN but send me an email (kasper at binf.ku.dk) and I will be more than happy to send it. =head1 AUTHOR: Kasper Munch =head1 COPYRIGHT: This program is free software. You may copy and redistribute it under the same terms as Perl itself. =cut