]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/get_embl_entry
fixed seq qual length check
[biopieces.git] / bp_bin / get_embl_entry
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 # Get an EMBL entry from the MySQL database and flatfiles.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Data::Dumper;
32 use Maasha::SQL;
33 use Maasha::Biopieces;
34 use Maasha::Filesys;
35 use Maasha::EMBL;
36
37
38 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
39
40
41 my ( $options, $user, $password, $database, $table, $dbh, $in, $out, $record, $entry, @ids, $id, $file, $offset, $len, $results, $result, $fh, %fh_hash );
42
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" );
47
48 $options = Maasha::Biopieces::parse_options(
49     [
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 },
58     ]   
59 );
60
61 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
62 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
63
64 if ( not Maasha::SQL::database_exists( $options->{ 'database' }, $options->{ 'user' }, $options->{ 'password' } ) ) {
65     Maasha::Common::error( qq(Database "$options->{ 'database' }" don't exists) );
66 }
67
68 $dbh = Maasha::SQL::connect( $options->{ 'database' }, $options->{ 'user' }, $options->{ 'password' } );
69
70 if ( not Maasha::SQL::table_exists( $dbh, $options->{ 'table' } ) ) {
71     Maasha::Common::error( qq(Table "$options->{ 'table' }" don't exists) );
72 }
73
74 @ids = @{ $options->{ 'ids' } } if defined $options->{ 'ids' };
75
76 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
77 {
78     push @ids, $record->{ 'ID' } if exists $record->{ 'ID' };
79
80     Maasha::Biopieces::put_record( $record, $out );
81 }
82
83 foreach $id ( @ids )
84 {
85     $results = Maasha::SQL::query_array( $dbh, qq(SELECT FILE,OFFSET,LEN from $options->{ 'table' } WHERE ID="$id") );
86
87     foreach $result ( @{ $results } )
88     {
89         ( $file, $offset, $len ) =  @{ $result };
90
91         if ( not exists $fh_hash{ $file } )
92         {
93             $fh = Maasha::Filesys::file_read_open( $file );
94
95             $fh_hash{ $file } = $fh;
96         }
97
98         $entry = Maasha::Filesys::file_read( $fh_hash{ $file }, $offset, $len );
99
100         map { Maasha::Biopieces::put_record( $_, $out ) } Maasha::EMBL::embl2biopieces( $entry, $options );
101     }
102 }
103
104 map { close $fh_hash{ $_ } } keys %fh_hash;
105
106 Maasha::Biopieces::close_stream( $in );
107 Maasha::Biopieces::close_stream( $out );
108
109
110 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
111
112
113 BEGIN
114 {
115     Maasha::Biopieces::status_set();
116 }
117
118
119 END
120 {
121     Maasha::SQL::disconnect( $dbh ) if $dbh;
122     Maasha::Biopieces::status_log();
123 }
124
125
126 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
127
128
129 __END__