3 # Copyright (C) 2007-2009 Martin A. Hansen.
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.
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.
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.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24 # Get an EMBL entry from the MySQL database and flatfiles.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
33 use Maasha::Biopieces;
38 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
41 my ( $options, $user, $password, $database, $table, $dbh, $in, $out, $record, $entry, @ids, $id, $file, $offset, $len, $results, $result, $fh, %fh_hash );
43 $user = Maasha::Biopieces::biopiecesrc( "MYSQL_USER" );
44 $password = Maasha::Biopieces::biopiecesrc( "MYSQL_PASSWORD" );
45 $database = Maasha::Biopieces::biopiecesrc( "EMBL_DATABASE" );
46 $table = Maasha::Biopieces::biopiecesrc( "EMBL_TABLE" );
48 $options = Maasha::Biopieces::parse_options(
50 { long => 'user', short => 'u', type => 'string', mandatory => 'no', default => $user, allowed => undef, disallowed => undef },
51 { long => 'password', short => 'p', type => 'string', mandatory => 'no', default => $password, allowed => undef, disallowed => undef },
52 { long => 'database', short => 'd', type => 'string', mandatory => 'no', default => $database, allowed => undef, disallowed => undef },
53 { long => 'table', short => 't', type => 'string', mandatory => 'no', default => $table, allowed => undef, disallowed => undef },
54 { long => 'ids', short => 'i', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
55 { long => 'keys', short => 'k', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
56 { long => 'features', short => 'f', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
57 { long => 'qualifiers', short => 'q', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
61 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
62 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
64 if ( not Maasha::SQL::database_exists( $options->{ 'database' }, $options->{ 'user' }, $options->{ 'password' } ) ) {
65 Maasha::Common::error( qq(Database "$options->{ 'database' }" don't exists) );
68 $dbh = Maasha::SQL::connect( $options->{ 'database' }, $options->{ 'user' }, $options->{ 'password' } );
70 if ( not Maasha::SQL::table_exists( $dbh, $options->{ 'table' } ) ) {
71 Maasha::Common::error( qq(Table "$options->{ 'table' }" don't exists) );
74 @ids = @{ $options->{ 'ids' } } if defined $options->{ 'ids' };
76 while ( $record = Maasha::Biopieces::get_record( $in ) )
78 push @ids, $record->{ 'ID' } if exists $record->{ 'ID' };
80 Maasha::Biopieces::put_record( $record, $out );
85 $results = Maasha::SQL::query_array( $dbh, qq(SELECT FILE,OFFSET,LEN from $options->{ 'table' } WHERE ID="$id") );
87 foreach $result ( @{ $results } )
89 ( $file, $offset, $len ) = @{ $result };
91 if ( not exists $fh_hash{ $file } )
93 $fh = Maasha::Filesys::file_read_open( $file );
95 $fh_hash{ $file } = $fh;
98 $entry = Maasha::Filesys::file_read( $fh_hash{ $file }, $offset, $len );
100 map { Maasha::Biopieces::put_record( $_, $out ) } Maasha::EMBL::embl2biopieces( $entry, $options );
104 map { close $fh_hash{ $_ } } keys %fh_hash;
106 Maasha::Biopieces::close_stream( $in );
107 Maasha::Biopieces::close_stream( $out );
110 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
115 Maasha::Biopieces::status_set();
121 Maasha::SQL::disconnect( $dbh ) if $dbh;
122 Maasha::Biopieces::status_log();
126 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<