--- /dev/null
+#!/usr/bin/env perl
+
+# Copyright (C) 2007-2009 Martin A. Hansen.
+
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+# http://www.gnu.org/copyleft/gpl.html
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+# Write Biopiece records to a KISS MySQL database.
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+use warnings;
+use strict;
+use Maasha::Common;
+use Maasha::SQL;
+use Maasha::KISS::IO;
+use Maasha::Biopieces;
+use Maasha::Filesys;
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+my ( $options, $user, $password, $dbh, $in, $out, $tmp_dir, $tmp_file, $fh_out, $record, $entry );
+
+$user = Maasha::Biopieces::biopiecesrc( "MYSQL_USER" );
+$password = Maasha::Biopieces::biopiecesrc( "MYSQL_PASSWORD" );
+
+$options = Maasha::Biopieces::parse_options(
+ [
+ { long => 'user', short => 'u', type => 'string', mandatory => 'no', default => $user, allowed => undef, disallowed => undef },
+ { long => 'password', short => 'p', type => 'string', mandatory => 'no', default => $password, allowed => undef, disallowed => undef },
+ { long => 'database', short => 'd', type => 'string', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
+ { long => 'table', short => 't', type => 'string', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
+ { long => 'table_replace', short => 'T', type => 'flag', mandatory => 'no' , default => undef, allowed => undef, disallowed => undef },
+ { long => 'table_append', short => 'A', type => 'flag', mandatory => 'no' , default => undef, allowed => undef, disallowed => undef },
+ { long => 'no_stream', short => 'x', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
+ ]
+);
+
+$in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
+$out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
+
+$tmp_dir = Maasha::Biopieces::get_tmpdir();
+$tmp_file = "$tmp_dir/upload_to_KISS.kiss";
+
+$dbh = Maasha::SQL::connect( $options->{ 'database' }, $options->{ 'user' }, $options->{ 'password' } );
+
+if ( Maasha::SQL::table_exists( $dbh, $options->{ 'table' } ) )
+{
+ if ( not $options->{ 'table_replace' } and not $options->{ 'table_append' } ) {
+ Maasha::Common::error( qq(Table "$options->{ 'table' }" exists. Use --table_replace or --table_append) )
+ }
+
+ if ( $options->{ 'table_replace' } ) {
+ Maasha::SQL::delete_table( $dbh, $options->{ 'table' } ) if Maasha::SQL::table_exists( $dbh, $options->{ 'table' } );
+ }
+
+ if ( $options->{ 'sql' } ) {
+ Maasha::SQL::request( $dbh, $options->{ 'sql' } );
+ } elsif ( not $options->{ 'table_append' } ) {
+ create_table( $dbh, $options->{ 'table' }, $options->{ 'verbose' } );
+ }
+}
+else
+{
+ create_table( $dbh, $options->{ 'table' }, $options->{ 'verbose' } );
+}
+
+$fh_out = Maasha::Filesys::file_write_open( $tmp_file );
+
+while ( $record = Maasha::Biopieces::get_record( $in ) )
+{
+ if ( $entry = Maasha::KISS::IO::biopiece2kiss( $record ) )
+ {
+ map { $entry->{ $_ } = '\N' if $entry->{ $_ } eq '.' } keys %{ $entry };
+
+ Maasha::KISS::IO::kiss_entry_put( $entry, $fh_out );
+ }
+
+ Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
+}
+
+close $fh_out;
+
+bulk_load_file( $dbh, $tmp_file, $options->{ 'table' }, $options->{ 'verbose' } );
+
+Maasha::SQL::disconnect( $dbh );
+
+unlink $tmp_file;
+
+Maasha::Biopieces::close_stream( $in );
+Maasha::Biopieces::close_stream( $out );
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+sub bulk_load_file
+{
+ # Martin A. Hansen, October 2009
+ #
+ # Bulk load a tab separated file to MySQL while
+ # converting . to NULL.
+
+ my ( $dbh, # Database handle
+ $file, # File to load
+ $table, # Table name
+ $verbose, # Verbose flag
+ ) = @_;
+
+ # Returns nothing.
+
+ my ( $sql );
+
+ #LOAD DATA INFILE....(col1, @dummy) SET col2 = IF(@dummy = '.', NULL, @dummy)
+
+ $sql = qq(LOAD DATA LOCAL INFILE "$file" INTO TABLE $table);
+
+ print STDERR "$sql\n" if $verbose;
+
+ Maasha::SQL::request( $dbh, $sql );
+}
+
+
+sub create_table
+{
+ # Martin A. Hansen, July 2009
+
+ # Create a new MySQL table.
+
+ my ( $dbh, # Database handle
+ $table, # Table name
+ $verbose, # Verbose switch
+ ) = @_;
+
+ # Returns nothing.
+
+ my ( @fields, $field_str, $sql );
+
+ @fields = (
+ "S_ID VARCHAR(256), INDEX S_ID_index (S_ID)",
+ "S_BEG INT, INDEX S_BEG_index (S_BEG)",
+ "S_LEN INT, INDEX S_LEN_index (S_LEN)",
+ "Q_ID VARCHAR(256), INDEX Q_ID_index (Q_ID)",
+ "SCORE FLOAT, INDEX SCORE_index (SCORE)",
+ "STRAND CHAR(1), INDEX STRAND_index (STRAND)",
+ "HITS INT, INDEX HITS_index (HITS)",
+ "ALIGN VARCHAR(256)",
+ "BLOCK_COUNT TINYINT, INDEX BLOCK_COUNT_index (BLOCK_COUNT)",
+ "BLOCK_BEGS VARCHAR(1024)",
+ "BLOCK_LENS VARCHAR(1024)",
+ );
+
+ $field_str = join( ", ", @fields );
+
+ $sql = "CREATE TABLE $table ($field_str)";
+
+ print STDERR "$sql\n" if $verbose;
+
+ Maasha::SQL::request( $dbh, $sql );
+}
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+BEGIN
+{
+ Maasha::Biopieces::status_set();
+}
+
+
+END
+{
+ Maasha::SQL::disconnect( $dbh ) if $dbh;
+ Maasha::Biopieces::status_log();
+}
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+__END__