3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
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
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.
20 package XrefMapper::ChecksumMapper;
30 my $DEFAULT_METHOD =
'XrefMapper::Methods::MySQLChecksum';
33 my($class, $mapper) = @_;
34 my $self = bless {}, $class;
35 $self->core($mapper->core);
36 $self->xref($mapper->xref);
37 $self->mapper($mapper);
43 return $self->xref()->dbc()->sql_helper();
48 return 'xrefchecksum';
52 my ($self, $mapper) = @_;
53 $self->{mapper} = $mapper
if defined $mapper;
54 return $self->{mapper};
58 my ($self, $method) = @_;
59 $self->{method} = $method
if defined $method;
60 return $self->{method};
65 return $self->mapper()->verbose();
68 # No default target file, implemented by subclasses where necessary
74 my ($self, $db_url, $species_id) = @_;
76 $self->_update_status(
'checksum_xrefs_started');
77 my $source_id = $self->source_id();
78 my $target = $self->target();
79 my $object_type = $self->object_type;
81 if($self->_map_checksums($db_url)) {
82 my $method = $self->get_method();
83 my $results = $method->run($target, $source_id, $object_type, $db_url);
84 $self->log_progress(
'Starting upload');
85 $self->upload($results, $species_id);
88 $self->_update_status(
'checksum_xrefs_finished');
93 my ($self, $results, $species_id) = @_;
94 #The elements come in as an array looking like
95 # [ { id => 1, upi => 'UPI00000A', object_type => 'Translation' } ]
97 my $insert_xref = <<
'SQL';
98 INSERT INTO xref (source_id,
accession, label, version, species_id, info_type)
101 my $insert_object_xref = <<'SQL';
102 INSERT INTO object_xref (ensembl_id, ensembl_object_type, xref_id, linkage_type, ox_status)
106 my $h = $self->_xref_helper();
107 my $source_id = $self->source_id();
108 $species_id = $self->species_id() unless defined $species_id;
109 if (!defined $species_id) {
return; }
111 $h->transaction(-CALLBACK => sub {
113 $self->log_progress(
'Deleting records from previous possible upload runs');
114 $self->_delete_entries(
'object_xref');
115 $self->_delete_entries(
'xref');
117 $self->log_progress(
'Starting xref insertion');
118 #Record UPIs to make sure we do not attempt to insert duplicate UPIs
120 $h->batch(-SQL => $insert_xref, -CALLBACK => sub {
122 foreach my $e (@{$results}) {
124 if(exists $upi_xref_id{$upi}) {
125 $e->{xref_id} = $upi_xref_id{$upi};
128 $sth->execute($source_id, $e->{upi}, $e->{upi}, 1, $species_id,
'CHECKSUM');
129 my $id = $sth->{
'mysql_insertid'};
131 $upi_xref_id{$upi} = $id;
137 $self->log_progress(
'Starting object_xref insertion');
138 $h->batch(-SQL => $insert_object_xref, -CALLBACK => sub {
140 foreach my $e (@{$results}) {
141 $sth->execute($e->{
id}, $e->{object_type}, $e->{xref_id},
'CHECKSUM',
'DUMP_OUT');
147 $self->log_progress(
'Finished insertions');
152 sub _delete_entries {
153 my ($self, $table) = @_;
154 $self->log_progress(
'Deleting entries from %s', $table);
159 WHERE x.source_id = ?
161 object_xref => <<
'SQL',
165 WHERE x.source_id = ?
166 AND ox.xref_id = x.xref_id
170 my $sql = $lookup->{$table};
171 throw "Cannot find delete SQL for the table $table" unless $sql;
172 my $source_id = $self->source_id();
173 my $count = $self->_xref_helper()->execute_update(-SQL => $sql, -PARAMS => [$source_id]);
174 my $type = ($count == 1) ?
'entry' :
'entries';
175 $self->log_progress(
'Deleted %s %s from %s', $count, $type, $table);
181 return $self->_xref_helper()->execute_single_result(
182 -SQL =>
'select source_id from source where name=?',
183 -PARAMS => [$self->external_db_name()]
189 my $species_id = $self->SUPER::species_id();
190 if(! defined $species_id) {
191 $species_id = $self->get_id_from_species_name($self->core()->species());
192 $self->SUPER::species_id($species_id);
199 my $method_class = $DEFAULT_METHOD;
200 eval
"require ${method_class};";
202 throw "Cannot require the class ${method_class}. Make sure your PERL5LIB is correct: $@";
204 return $method_class->new( -MAPPER => $self );
207 ############# INTERNAL METHODS
210 my ($self, $status) = @_;
212 my $h = $self->_xref_helper();
213 my $sql = q{insert into process_status (status, date) values(?,now())};
214 $h->execute_update(-SQL => $sql, -PARAMS => [$status]);
217 my $time = localtime();
218 $self->log_progress(q{Status Update
'%s' @ %s}.
"\n", $status, $time);
224 my ($self, $db_url) = @_;
225 my $source_id = $self->source_id();
226 my $dbc = $self->mapper->xref->dbc;
227 if (defined $db_url) {
229 my ($dbconn_part, $driver, $user, $pass, $host, $port, $dbname, $table_name, $tparam_name, $tparam_value, $conn_param_string) =
230 $db_url =~ m{^((\w*):
243 my ( $self, $fmt, @params ) = @_;
244 return if (!$self->verbose);
245 printf( STDERR
"CHKSM==> %s\n", sprintf( $fmt, @params ) );