ensembl-hive  2.7.0
meta_levels.pl
Go to the documentation of this file.
1 #!/usr/bin/env perl
2 #
3 # See the NOTICE file distributed with this work for additional information
4 # regarding copyright ownership.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 
18 
19 # Populate meta table with (e.g.) genebuild.level = toplevel if all genes are
20 # top level. Using v41 API code this can speed fetching & dumping greatly.
21 #
22 
23 use strict;
24 use warnings;
25 
26 use DBI;
27 
28 use Getopt::Long;
29 
32 
33 my $cli_helper = Bio::EnsEMBL::Utils::CliHelper->new();
34 
35 our @feature_types =
36  qw[gene transcript exon repeat_feature dna_align_feature protein_align_feature simple_feature prediction_transcript prediction_exon];
37 
38 # get the basic options for connecting to a database server
39 my $optsd = $cli_helper->get_dba_opts();
40 # add the print option
41 push(@{$optsd},"print");
42 # process the command line with the supplied options plus a help subroutine
43 my $opts = $cli_helper->process_args($optsd,\&usage);
44 
45 # use the command line options to get an array of database details
46 for my $db_args (@{$cli_helper->get_dba_args_for_opts($opts)}) {
47  # use the args to create a DBA
48  my $dba = Bio::EnsEMBL::DBSQL::DBAdaptor->new(%{$db_args});
49  if($dba) {
50  process_dba($dba,$opts->{print});
51  }
52 }
53 
54 sub process_dba {
55 
56  my ($dba,$print) = @_;
57  my $ma = $dba->get_MetaContainer();
58 
59  my @inserted;
60  my @not_inserted;
61 
62  foreach my $type (@feature_types) {
63 
64  delete_existing( $ma, $type ) if ( !$print );
65 
66  if ( can_use_key( $dba, $type ) ) {
67 
68  insert_key( $ma, $type ) if ( !$print );
69  push @inserted, $type;
70 
71  } else {
72 
73  push @not_inserted, $type;
74 
75  }
76 
77  }
78 
79  print $dba->dbc()->dbname()
80  . " (species_id "
81  . $dba->species_id()
82  . ") inserted keys for "
83  . join( ", ", @inserted ) . ".\n"
84  if (@inserted);
85  print "Did not insert keys for " . join( ", ", @not_inserted ) . ".\n"
86  if (@not_inserted);
87 } ## end sub process_dba
88 
89 #------------------------------------------------------------------------------
90 
91 sub delete_existing {
92 
93  my ( $ma, $type ) = @_;
94 
95  $ma->delete_key( $type . "build.level" );
96 
97 }
98 
99 #------------------------------------------------------------------------------
100 
101 sub can_use_key {
102 
103  my ( $dba, $type ) = @_;
104 
105  # compare total count of typewith number of toplevel type, if they're the same,
106  # then we can use the key
107 
108  my $sth = $dba->dbc()->prepare("SELECT COUNT(*) FROM $type");
109  $sth->execute();
110  my $total = ( $sth->fetchrow_array() )[0];
111 
112  $sth =
113  $dba->dbc()
114  ->prepare( "SELECT COUNT(*) "
115  . "FROM $type t, seq_region_attrib sra, attrib_type at "
116  . "WHERE t.seq_region_id=sra.seq_region_id "
117  . "AND sra.attrib_type_id=at.attrib_type_id "
118  . "AND at.code='toplevel'" );
119  $sth->execute();
120  my $toplevel = ( $sth->fetchrow_array() )[0];
121 
122  if ( $toplevel > 0 ) {
123  return $total == $toplevel;
124  }
125 } ## end sub can_use_key
126 
127 #------------------------------------------------------------------------------
128 
129 sub insert_key {
130  my ( $ma, $type ) = @_;
131  $ma->store_key_value( $type . "build.level", "toplevel" );
132 }
133 
134 #------------------------------------------------------------------------------
135 
136 sub usage {
137  print <<EOF; exit(0);
138 
139 Populate meta table with (e.g.) genebuild.level = toplevel if all genes
140 are top level. Using v41 API code this can speed fetching and dumping
141 greatly.
142 
143 Usage: perl $0 <options>
144 
145  --host|dbhost Database host to connect to.
146 
147  --port|dbport Database port to connect to (default is 3306).
148 
149  --dbpattern Database name regexp
150 
151  --user|dbuser Database username.
152 
153  --pass|dbpass Password for user.
154 
155  --print Just print, don't insert or delete keys.
156 
157  --help This message.
158 
159  --verbose Prints out the name of the database
160  which is going to be patched.
161 
162 
163 
164  If you like to patch just a single database you can also use this
165  command line :
166 
167  perl meta_levels.pl --host ... --user ... --pass ... --port ... --dbname ...
168 EOF
169 } ## end sub usage
transcript
public transcript()
Bio::EnsEMBL::DBSQL::DBAdaptor
Definition: DBAdaptor.pm:40
Bio::EnsEMBL::Utils::CliHelper::get_dba_opts
public Arrayref get_dba_opts()
usage
public usage()
delete_existing
public delete_existing()
exon
public exon()
process_dba
public process_dba()
can_use_key
public can_use_key()
insert_key
public insert_key()
Bio::EnsEMBL::DBSQL::DBAdaptor::new
public Bio::EnsEMBL::DBSQL::DBAdaptor new()
Bio::EnsEMBL::Utils::CliHelper
Definition: CliHelper.pm:55