]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/write_mysql
refactoring of assemble_pairs
[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     = Maasha::Biopieces::biopiecesrc( "MYSQL_USER" );
43 $password = Maasha::Biopieces::biopiecesrc( "MYSQL_PASSWORD" );
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 else
126 {
127     create_table( $dbh, $options->{ 'table' }, $type_hash, $options->{ 'index' }, $options->{ 'verbose' } );
128 }
129
130 Maasha::SQL::bulk_load_file( $dbh, $tmp_file, $options->{ 'table' }, "\t" ) if Maasha::SQL::table_exists( $dbh, $options->{ 'table' } );
131
132 Maasha::SQL::disconnect( $dbh );
133
134 unlink $tmp_file;
135
136 Maasha::Biopieces::close_stream( $in );
137 Maasha::Biopieces::close_stream( $out );
138
139
140 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
141
142
143 sub guess_types
144 {
145     # Martin A. Hansen, July 2009.
146
147     # Guess the MySQL table types from Biopiece records.
148
149     my ( $type_hash,   # Hash with type info
150          $record,      # Biopiece record
151        ) = @_;
152
153     # Returns nothing.
154
155     my ( $key );
156
157     foreach $key ( keys %{ $record } )
158     {
159         if ( exists $type_hash->{ $key } )
160         {
161             if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
162             {
163                 if ( $record->{ $key } =~ tr/.// ) {
164                     $type_hash->{ $key }->{ "TYPE" } = "FLOAT";
165                 } else {
166                     $type_hash->{ $key }->{ "TYPE" } = "INTEGER";
167                 }
168
169                 $type_hash->{ $key }->{ "MIN" }  = Maasha::Calc::min( $record->{ $key }, $type_hash->{ $key }->{ "MIN" } );
170                 $type_hash->{ $key }->{ "MAX" }  = Maasha::Calc::max( $record->{ $key }, $type_hash->{ $key }->{ "MAX" } );
171             } 
172             else
173             {
174                 $type_hash->{ $key }->{ "TYPE" } = "ALPH";
175                 $type_hash->{ $key }->{ "MIN" }  = Maasha::Calc::min( length $record->{ $key }, $type_hash->{ $key }->{ "MIN" } );
176                 $type_hash->{ $key }->{ "MAX" }  = Maasha::Calc::max( length $record->{ $key }, $type_hash->{ $key }->{ "MAX" } );
177             }
178         }
179         else
180         {
181             if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
182             {
183                 $type_hash->{ $key }->{ "TYPE" } = "NUMBER";
184                 $type_hash->{ $key }->{ "MIN" }  = $record->{ $key };
185                 $type_hash->{ $key }->{ "MAX" }  = $record->{ $key };
186             } 
187             else
188             {
189                 $type_hash->{ $key }->{ "TYPE" } = "ALPH";
190                 $type_hash->{ $key }->{ "MIN" }  = length $record->{ $key };
191                 $type_hash->{ $key }->{ "MAX" }  = length $record->{ $key };
192             }
193         }
194     }
195 }
196
197
198 sub create_table
199 {
200     # Martin A. Hansen, July 2009
201
202     # Create a new MySQL table.
203
204     my ( $dbh,            # Database handle
205          $table,          # Table name
206          $type_hash,      # Hashref with type info.
207          $fields_index,   # Fields to index
208          $verbose,        # Verbose switch
209        ) = @_;
210
211     # Returns nothing.
212
213     my ( %index_hash, $key, $field, @fields, $fields, $diff, $sql );
214
215     map { $index_hash{ $_ } = 1 } @{ $fields_index };
216
217     foreach $key ( sort keys %{ $type_hash } )
218     {
219         $field = "";
220
221         $diff = $type_hash->{ $key }->{ "MAX" } - $type_hash->{ $key }->{ "MIN" };
222
223         if ( $type_hash->{ $key }->{ "TYPE" } eq "INTEGER" )
224         {
225             $field = "$key ";
226
227             if ( $type_hash->{ $key }->{ "MIN" } >= 0 ) {
228        #         $field .= "UNSIGNED ";
229             }
230
231             if ( $diff <= 255 ) {
232                 $field .= "TINYINT";
233             } elsif ( $diff <= 65535 ) {
234                 $field .= "SMALLINT";
235             } elsif ( $diff <= 16777215 ) {
236                 $field .= "MEDIUMINT";
237             } elsif ( $diff <= 4294967295 ) {
238                 $field .= "INT";
239             } else {
240                 $field .= "BIGINT";
241             }
242         }
243         elsif ( $type_hash->{ $key }->{ "TYPE" } eq "FLOAT" )
244         {
245                 $field .= "$key FLOAT";  # FLOAT(p) where p=precision from 0 to 23 results in a four-byte single-precision FLOAT column.
246                                     # A precision from 24 to 53 results in an eight-byte double-precision coulumn.
247         }
248         else
249         {
250             if ( $type_hash->{ $key }->{ "MAX" } > 1024 ) {
251                 $field = "$key BLOB";
252             } if ( $diff == 0 ) {
253                 $field = "$key CHAR($type_hash->{ $key }->{ 'MAX' })";
254             } else {
255                 $field = "$key VARCHAR($type_hash->{ $key }->{ 'MAX' })";
256             }
257         }
258
259         $field .= ", INDEX $key" . "_index ($key)" if exists $index_hash{ $key };
260
261         push @fields, $field;
262     }
263
264     $fields = join( ", ", @fields ); 
265
266     $sql = "CREATE TABLE $table ($fields)"; #. " MAX_ROWS = 100000000";
267
268     print STDERR "$sql\n" if $verbose;
269
270     Maasha::SQL::request( $dbh, $sql );
271 }
272
273
274 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
275
276
277 BEGIN
278 {
279     Maasha::Biopieces::status_set();
280 }
281
282
283 END
284 {
285     Maasha::SQL::disconnect( $dbh ) if $dbh;
286     Maasha::Biopieces::status_log();
287 }
288
289
290 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
291
292
293 __END__