2 # See the NOTICE file distributed with this work for additional information
3 # regarding copyright ownership.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
19 # updates the external db tables on all of the core databases on a given host
30 my ( $host, $user, $pass, $port, $dbname, $file);
32 GetOptions(
"dbhost|host=s", \$host,
33 "dbuser|user=s", \$user,
34 "dbpass|pass=s", \$pass,
35 "dbport|port=i", \$port,
37 "dbname=s", \$dbname);
40 $host ||=
"ens-research";
41 $dbname ||=
"ianl_mus_musculus_core_58_37k";
43 # connect to the database
44 my $dsn =
"DBI:mysql:host=$host;port=$port;database=$dbname";
46 my $db = DBI->connect( $dsn, $user, $pass, {RaiseError => 1} );
49 my %analysis = (
"No products available yet" => 0,
50 "ES cells available" => 0,
51 "Mice available" => 0,
52 "Vector available" => 0);
54 # if the 4 analysis do not exist create them
55 # if they do exist delete entrys in the simple feature table
58 my $find_sth = $db->prepare(
"SELECT analysis_id FROM analysis where logic_name like ?")|| die
"Could not prepare find_sth";
60 my $create_sth = $db->prepare(
"INSERT into analysis (logic_name) values (?)") || die
"Could not prepare create_sth";
62 my $delete_sth = $db->prepare(
"DELETE from simple_feature where analysis_id = ?" )|| die
"Could not prepare delete_sth";
64 foreach my $anal (keys %analysis){
65 my $logic_name =
"IKMC_".$anal;
66 $logic_name =~ s/ /_/g;
69 $find_sth->execute($logic_name);
70 $find_sth->bind_columns(\$id);
72 if(defined($id)){ #
delete existing simple features
73 print STDERR
"Analysis $logic_name already exists so clearing entrys for this ($id) in the simple_feature table\n";
74 $delete_sth->execute($id) || die
"Could not delete form simple table for id = $id";
76 else{ # create analysis
77 $create_sth->execute($logic_name) || die
"Could not create new analysis $logic_name";
78 $id = $create_sth->{
'mysql_insertid'};
79 print STDERR
"Creating new anlysis $logic_name ($id)\n";
81 $analysis{$anal} = $id;
85 $create_sth->finish();
86 $delete_sth->finish();
89 # make a hashes for gene
90 # gene2seqregion{stable_id} = seq_region_id
98 my $gene_sth = $db->prepare(
"SELECT stable_id, seq_region_id, seq_region_start, seq_region_end, seq_region_strand FROM gene") || die
"Could not prepare gene_sth";
100 $gene_sth->execute();
101 my ($stable_id, $seq, $start, $end, $strand);
102 $gene_sth->bind_columns(\$stable_id, \$seq, \$start, \$end, \$strand);
103 while($gene_sth->fetch()){
104 $gene2seqregion{$stable_id} = $seq;
105 $gene2start{$stable_id} = $start;
106 $gene2end{$stable_id} = $end;
107 $gene2strand{$stable_id} = $strand;
109 #process the file and add new simple features
111 my $insert_sth = $db->prepare(
"INSERT INTO simple_feature (seq_region_id, seq_region_start, seq_region_end, seq_region_strand, display_label, analysis_id ) VALUES(?, ?, ?, ?, ?, ?)") || die
"Could not prepare insert_sth";
115 while ( $_ = $ikmc->getline() ) {
118 my ($mgi, $label, $type, $stable_id) = split /\t/;
120 if((defined($stable_id) and $stable_id) and defined($gene2seqregion{$stable_id})){
121 if(!defined($analysis{$type})){
122 print STDERR $_.
"\nUnknown type *$type*\n";
125 $insert_sth->execute($gene2seqregion{$stable_id},
126 $gene2start{$stable_id},
127 $gene2end{$stable_id},
128 $gene2strand{$stable_id},
130 $analysis{$type}) || die
"Could not insert new values";
135 if(!defined($stable_id) or !$stable_id){
136 $count{
"no stable_id"}++;
139 print STDERR
"Could not find stable id $stable_id\n";
144 foreach my $key (keys %count){
145 print $key.
"\t".$count{$key}.
"\n";
151 my ($file_name) = @_;
155 my $alt_file_name = $file_name;
156 $alt_file_name =~ s/\.(gz|Z)$
158 if ( $alt_file_name eq $file_name ) {
159 $alt_file_name .=
'.gz';
162 if ( !-f $file_name ) {
163 carp(
"File '$file_name' does not exist, "
164 .
"will try '$alt_file_name'" );
165 $file_name = $alt_file_name;
168 if ( $file_name =~ /\.(gz|Z)$/ ) {
169 # Read from zcat pipe
170 $io = IO::File->new(
"zcat $file_name |")
171 or carp(
"Can not open file '$file_name' with 'zcat'");
174 $io = IO::File->new($file_name)
175 or carp(
"Can not open file '$file_name'");
178 if ( !defined $io ) {
return undef }
180 print
"Reading from '$file_name'...\n";