]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/write_mysql
finished write_mysql
[biopieces.git] / bp_bin / write_mysql
1 #!/usr/bin/env perl
2
3 # Copyright (C) 2007-2009 Martin A. Hansen.
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24 # Write Biopiece records to a MySQL database.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Maasha::SQL;
32 use Maasha::Biopieces;
33 use Maasha::Filesys;
34 use Maasha::Calc;
35
36
37 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
38
39
40 my ( $options, $user, $password, $dbh, $in, $out, $tmp_dir, $tmp_file, $fh_out, $record, $key, $type_hash, $i, @keys, $line );
41
42 $user     = $ENV{ "USER" };
43 $password = $ENV{ "USER" };
44
45 $options = Maasha::Biopieces::parse_options(
46     [
47         { long => 'user',             short => 'u', type => 'string', mandatory => 'no',  default => $user,     allowed => undef, disallowed => undef },
48         { long => 'password',         short => 'p', type => 'string', mandatory => 'no',  default => $password, allowed => undef, disallowed => undef },
49         { long => 'database',         short => 'd', type => 'string', mandatory => 'yes', default => undef,     allowed => undef, disallowed => undef },
50         { long => 'table',            short => 't', type => 'string', mandatory => 'yes', default => undef,     allowed => undef, disallowed => undef },
51         { long => 'index',            short => 'i', type => 'list',   mandatory => 'no' , default => undef,     allowed => undef, disallowed => undef },
52         { long => 'database_replace', short => 'D', type => 'flag',   mandatory => 'no' , default => undef,     allowed => undef, disallowed => undef },
53         { long => 'table_replace',    short => 'T', type => 'flag',   mandatory => 'no' , default => undef,     allowed => undef, disallowed => undef },
54         { long => 'table_append',     short => 'A', type => 'flag',   mandatory => 'no' , default => undef,     allowed => undef, disallowed => undef },
55         { long => 'guess_type',       short => 'g', type => 'uint',   mandatory => 'no',  default => 1000,      allowed => undef, disallowed => 0     },
56         { long => 'sql',              short => 's', type => 'string', mandatory => 'no',  default => undef,     allowed => undef, disallowed => undef },
57         { long => 'no_stream',        short => 'x', type => 'flag',   mandatory => 'no',  default => undef,     allowed => undef, disallowed => undef },
58     ]   
59 );
60
61 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
62 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
63
64 $options->{ 'guess_type' } = 0 if $options->{ 'table_append' } or $options->{ 'sql' }; # no point in analyzing data types.
65
66 $tmp_dir  = Maasha::Biopieces::get_tmpdir();
67 $tmp_file = "$tmp_dir/mysql.tab";
68
69 $fh_out  = Maasha::Filesys::file_write_open( $tmp_file );
70
71 $type_hash = {};
72
73 $i = 0;
74
75 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
76 {
77     $line = "";
78     @keys = sort keys %{ $record } if not @keys;
79
80     guess_types( $type_hash, $record ) if $i < $options->{ 'guess_type' };
81
82     map { $line .= "$record->{ $_ }\t" } @keys;
83
84     chop $line;
85     print $fh_out "$line\n";
86
87     Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
88
89     $i++;
90 }
91
92 close $fh_out;
93
94 if ( Maasha::SQL::database_exists( $options->{ 'database' }, $options->{ 'user' }, $options->{ 'password' } ) )
95 {
96     if ( $options->{ 'database_replace' } )
97     {
98         Maasha::SQL::delete_database( $options->{ 'database' }, $options->{ 'user' }, $options->{ 'password' } );
99         Maasha::SQL::create_database( $options->{ 'database' }, $options->{ 'user' }, $options->{ 'password' } );
100     }
101 }
102 else
103 {
104     Maasha::SQL::create_database( $options->{ 'database' }, $options->{ 'user' }, $options->{ 'password' } );
105 }
106
107 $dbh = Maasha::SQL::connect( $options->{ 'database' }, $options->{ 'user' }, $options->{ 'password' } );
108
109 if ( Maasha::SQL::table_exists( $dbh, $options->{ 'table' } ) )
110 {
111     if ( not $options->{ 'table_replace' } and not $options->{ 'table_append' } ) {
112         Maasha::Common::error( qq(Table "$options->{ 'table' }" exists. Use --table_replace or --table_append) )
113     }
114
115     if ( $options->{ 'table_replace' } ) {
116         Maasha::SQL::delete_table( $dbh, $options->{ 'table' } ) if Maasha::SQL::table_exists( $dbh, $options->{ 'table' } );
117     }
118
119     if ( $options->{ 'sql' } ) {
120         Maasha::SQL::request( $dbh, $options->{ 'sql' } );
121     } elsif ( not $options->{ 'table_append' } ) {
122         create_table( $dbh, $options->{ 'table' }, $type_hash, $options->{ 'index' }, $options->{ 'verbose' } );
123     }
124 }
125
126 Maasha::SQL::bulk_load_file( $dbh, $tmp_file, $options->{ 'table' }, "\t" ) if Maasha::SQL::table_exists( $dbh, $options->{ 'table' } );
127
128 Maasha::SQL::disconnect( $dbh );
129
130 unlink $tmp_file;
131
132 Maasha::Biopieces::close_stream( $in );
133 Maasha::Biopieces::close_stream( $out );
134
135
136 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
137
138
139 sub guess_types
140 {
141     # Martin A. Hansen, July 2009.
142
143     # Guess the MySQL table types from Biopiece records.
144
145     my ( $type_hash,   # Hash with type info
146          $record,      # Biopiece record
147        ) = @_;
148
149     # Returns nothing.
150
151     my ( $key );
152
153     foreach $key ( keys %{ $record } )
154     {
155         if ( exists $type_hash->{ $key } )
156         {
157             if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
158             {
159                 if ( $record->{ $key } =~ tr/.// ) {
160                     $type_hash->{ $key }->{ "TYPE" } = "FLOAT";
161                 } else {
162                     $type_hash->{ $key }->{ "TYPE" } = "INTEGER";
163                 }
164
165                 $type_hash->{ $key }->{ "MIN" }  = Maasha::Calc::min( $record->{ $key }, $type_hash->{ $key }->{ "MIN" } );
166                 $type_hash->{ $key }->{ "MAX" }  = Maasha::Calc::max( $record->{ $key }, $type_hash->{ $key }->{ "MAX" } );
167             } 
168             else
169             {
170                 $type_hash->{ $key }->{ "TYPE" } = "ALPH";
171                 $type_hash->{ $key }->{ "MIN" }  = Maasha::Calc::min( length $record->{ $key }, $type_hash->{ $key }->{ "MIN" } );
172                 $type_hash->{ $key }->{ "MAX" }  = Maasha::Calc::max( length $record->{ $key }, $type_hash->{ $key }->{ "MAX" } );
173             }
174         }
175         else
176         {
177             if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
178             {
179                 $type_hash->{ $key }->{ "TYPE" } = "NUMBER";
180                 $type_hash->{ $key }->{ "MIN" }  = $record->{ $key };
181                 $type_hash->{ $key }->{ "MAX" }  = $record->{ $key };
182             } 
183             else
184             {
185                 $type_hash->{ $key }->{ "TYPE" } = "ALPH";
186                 $type_hash->{ $key }->{ "MIN" }  = length $record->{ $key };
187                 $type_hash->{ $key }->{ "MAX" }  = length $record->{ $key };
188             }
189         }
190     }
191 }
192
193
194 sub create_table
195 {
196     # Martin A. Hansen, July 2009
197
198     # Create a new MySQL table.
199
200     my ( $dbh,            # Database handle
201          $table,          # Table name
202          $type_hash,      # Hashref with type info.
203          $fields_index,   # Fields to index
204          $verbose,        # Verbose switch
205        ) = @_;
206
207     # Returns nothing.
208
209     my ( %index_hash, $key, $field, @fields, $fields, $diff, $sql );
210
211     map { $index_hash{ $_ } = 1 } @{ $fields_index };
212
213     foreach $key ( sort keys %{ $type_hash } )
214     {
215         $field = "";
216
217         $diff = $type_hash->{ $key }->{ "MAX" } - $type_hash->{ $key }->{ "MIN" };
218
219         if ( $type_hash->{ $key }->{ "TYPE" } eq "INTEGER" )
220         {
221             $field = "$key ";
222
223             if ( $type_hash->{ $key }->{ "MIN" } >= 0 ) {
224        #         $field .= "UNSIGNED ";
225             }
226
227             if ( $diff <= 255 ) {
228                 $field .= "TINYINT";
229             } elsif ( $diff <= 65535 ) {
230                 $field .= "SMALLINT";
231             } elsif ( $diff <= 16777215 ) {
232                 $field .= "MEDIUMINT";
233             } elsif ( $diff <= 4294967295 ) {
234                 $field .= "INT";
235             } else {
236                 $field .= "BIGINT";
237             }
238         }
239         elsif ( $type_hash->{ $key }->{ "TYPE" } eq "FLOAT" )
240         {
241                 $field .= "$key FLOAT";  # FLOAT(p) where p=precision from 0 to 23 results in a four-byte single-precision FLOAT column.
242                                     # A precision from 24 to 53 results in an eight-byte double-precision coulumn.
243         }
244         else
245         {
246             if ( $type_hash->{ $key }->{ "MAX" } > 1024 ) {
247                 $field = "$key BLOB";
248             } if ( $diff == 0 ) {
249                 $field = "$key CHAR($type_hash->{ $key }->{ 'MAX' })";
250             } else {
251                 $field = "$key VARCHAR($type_hash->{ $key }->{ 'MAX' })";
252             }
253         }
254
255         $field .= ", INDEX $key" . "_index ($key)" if exists $index_hash{ $key };
256
257         push @fields, $field;
258     }
259
260     $fields = join( ", ", @fields ); 
261
262     $sql = "CREATE TABLE $table ($fields)"; #. " MAX_ROWS = 100000000";
263
264     print STDERR "$sql\n" if $verbose;
265
266     Maasha::SQL::request( $dbh, $sql );
267 }
268
269
270 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
271
272
273 BEGIN
274 {
275     Maasha::Biopieces::status_set();
276 }
277
278
279 END
280 {
281     Maasha::SQL::disconnect( $dbh ) if $dbh;
282     Maasha::Biopieces::status_log();
283 }
284
285
286 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
287
288
289 __END__