ensembl-hive  2.7.0
ChecksumBasic.pm
Go to the documentation of this file.
1 =head1 LICENSE
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 =cut
19 
20 package XrefMapper::Methods::ChecksumBasic;
21 
22 use strict;
23 use warnings;
24 
25 use Bio::SeqIO;
26 use Bio::EnsEMBL::Utils::Argument qw(rearrange);
27 use Bio::EnsEMBL::Utils::Exception qw(throw);
28 use Digest::MD5;
29 
30 my $DEFAULT_BATCH_SIZE = 1000;
31 
32 sub new {
33  my ($class, @args) = @_;
34  my $self = bless({}, $class);
35 
36  my ($mapper, $batch_size) = rearrange([qw(mapper batch_size)], @args);
37 
38  throw 'No -MAPPER given' unless $mapper;
39  $batch_size = $DEFAULT_BATCH_SIZE unless $batch_size;
40 
41  $self->mapper($mapper);
42  $self->batch_size($batch_size);
43  return $self;
44 }
45 
46 sub mapper {
47  my ($self, $_mapper) = @_;
48  $self->{mapper} = $_mapper if defined $_mapper;
49  return $self->{mapper};
50 }
51 
52 sub batch_size {
53  my ($self, $batch_size) = @_;
54  $self->{batch_size} = $batch_size if defined $batch_size;
55  return $self->{batch_size};
56 }
57 
58 sub run {
59  my ($self, $target, $source_id, $object_type, $db_url) = @_;
60 
61  my $reader = $self->_get_sequence_parser($target);
62  my @results;
63  my @tmp_list;
64  my $batch_size = $self->batch_size();
65  my $count = 0;
66  while ( my $sequence = $reader->next_seq() ) {
67  push(@tmp_list, $sequence);
68  $count++;
69  if( ($count % $batch_size) == 0) {
70  my $res = $self->perform_mapping(\@tmp_list, $source_id, $object_type, $db_url);
71  push(@results, @{$res});
72  $self->mapper()->log_progress("Finished batch mapping of %d sequences\n", $batch_size);
73  $count = 0;
74  @tmp_list = ();
75  }
76  }
77 
78  #Final mapping if there were some left over
79  if(@tmp_list) {
80  $self->mapper()->log_progress("Finishing progess\n");
81  my $res = $self->perform_mapping(\@tmp_list, $source_id, $object_type, $db_url);
82  push(@results, @{$res});
83  @tmp_list = ();
84  }
85 
86  $reader->close();
87  return \@results;
88 }
89 
90 sub perform_mapping {
91  my ($self, $sequences) = @_;
92  throw('Override to perform the mapping you require');
93 }
94 
95 sub _get_sequence_parser {
96  my ($self, $target) = @_;
97  throw "Cannot find the file '${target}'" unless -f $target;
98  my $reader = Bio::SeqIO->new(-FILE => $target, -FORMAT => 'fasta');
99  return $reader;
100 }
101 
102 sub md5_checksum {
103  my ($self, $sequence) = @_;
104  my $digest = Digest::MD5->new();
105  $digest->add($sequence->seq());
106  return $digest->hexdigest();
107 }
108 
109 1;
run
public run()
Bio::EnsEMBL::Utils::Argument
Definition: Argument.pm:34
Bio::EnsEMBL::Utils::Exception
Definition: Exception.pm:68