File Coverage

lib/Bio/MLST/Download/Databases.pm
Criterion Covered Total %
statement 24 69 34.7
branch 0 10 0.0
condition 0 5 0.0
subroutine 8 14 57.1
pod 1 1 100.0
total 33 99 33.3


line stmt bran cond sub pod time code
1             package Bio::MLST::Download::Databases;
2             # ABSTRACT: Represents multiple databases of species
3             $Bio::MLST::Download::Databases::VERSION = '2.1.1630910';
4              
5 4     4   9176 use Moose;
  4         9  
  4         28  
6 4     4   24474 use Bio::MLST::Download::Database;
  4         13  
  4         172  
7 4     4   3514 use Parallel::ForkManager;
  4         32102  
  4         181  
8              
9 4     4   93 use Try::Tiny;
  4         8  
  4         366  
10 4     4   25 use File::Copy qw(move);
  4         8  
  4         315  
11 4     4   23 use File::Path qw(make_path rmtree);
  4         7  
  4         235  
12 4     4   25 use POSIX qw(strftime);
  4         9  
  4         29  
13              
14             has 'databases_attributes' => ( is => 'ro', isa => 'HashRef', required => 1 );
15             has 'base_directory' => ( is => 'ro', isa => 'Str', required => 1 );
16              
17             has 'parallel_processes' => ( is => 'ro', isa => 'Int', default => 0 );
18              
19             has '_species_to_exclude' => ( is => 'ro', isa => 'Str', default => 'Pediococcus' );
20              
21             sub update {
22 0     0 1   my($self) = @_;
23 0           my $paths_to_database_updates = $self->databases_attributes;
24 0           my $species_to_exclude = $self->_species_to_exclude;
25 0           my $temp_folder = strftime "temp_%Y%m%d%H%M%S", localtime; # e.g. temp_20150402102622
26 0           my $staging_directory = join('/', ($self->base_directory, 'staging', $temp_folder));
27             try {
28 0     0     $self->_download_to_staging($species_to_exclude,
29             $paths_to_database_updates,
30             $staging_directory);
31             } catch {
32 0   0 0     my $original_error = $_ || 'Unknown error';
33 0           die "Sorry, there was a problem updating the database. ",
34             "Some of the updates have been downloaded to $staging_directory ",
35             "but this is likely to be incomplete\n",
36             "The original message was as follows:\n$original_error";
37 0           };
38 0           my $production_directory = join('/',($self->base_directory));
39 0           $self->_update_all_from_staging($staging_directory,
40             $production_directory);
41 0           rmtree($staging_directory);
42 0           1;
43             }
44              
45             sub _download_to_staging
46             {
47 0     0     my($self, $species_to_exclude, $paths_to_downloads, $staging_directory) = @_;
48 0           my $pm = new Parallel::ForkManager($self->parallel_processes);
49              
50 0           for my $species (keys %{$paths_to_downloads})
  0            
51             {
52 0 0         $pm->start and next; # do the fork
53 0 0         if($species =~ /$species_to_exclude/i) {
54 0           $pm->finish; # exit child process
55 0           next;
56             }
57              
58             my $database = Bio::MLST::Download::Database->new(
59             species => $species,
60 0           database_attributes => $paths_to_downloads->{$species},
61             base_directory => $staging_directory
62             );
63 0           $database->update();
64 0           $pm->finish; # exit the child process
65             }
66 0           $pm->wait_all_children;
67 0           1;
68             }
69              
70             sub _get_sub_directories
71             {
72 0     0     my($self, $parent_folder) = @_;
73 0 0         return [] if (! -d $parent_folder);
74 0           opendir(my $FOLDER_HANDLE, $parent_folder);
75 0           my @folder_contents = readdir($FOLDER_HANDLE);
76 0   0       my @real_contents = grep { ! /^\.$/ and ! /^\.\.$/ } @folder_contents; # Remove '.' and '..' directories
  0            
77 0           my @child_directories = grep { -d "$parent_folder/$_" } @real_contents;
  0            
78 0           return \@child_directories;
79             }
80              
81             sub _update_all_from_staging
82             {
83 0     0     my($self, $staging_directory, $production_directory) = @_;
84              
85 0           my $species_directories = $self->_get_sub_directories($staging_directory);
86              
87 0           foreach my $species_directory (@{$species_directories}){
  0            
88 0           my $species_staging_path = join("/", ($staging_directory, $species_directory));
89 0           my $species_production_path = join("/", ($production_directory, $species_directory));
90 0 0         if (-d $species_staging_path ) {
91 0 0         rmtree($species_production_path) if ( -d $species_production_path );
92 0           move($species_staging_path, $species_production_path);
93             }
94             }
95 0           1;
96             }
97              
98 4     4   2720 no Moose;
  4         7  
  4         40  
99             __PACKAGE__->meta->make_immutable;
100             1;
101              
102             __END__
103              
104             =pod
105              
106             =encoding UTF-8
107              
108             =head1 NAME
109              
110             Bio::MLST::Download::Databases - Represents multiple databases of species
111              
112             =head1 VERSION
113              
114             version 2.1.1630910
115              
116             =head1 SYNOPSIS
117              
118             Represents multiple databases of species
119              
120             use Bio::MLST::Download::Databases;
121             my $databases = Bio::MLST::Download::Databases->new(
122             databases_attributes => \@databases_attributes
123             base_directory => '/path/to/dir'
124             );
125             $databases->update;
126              
127             =head1 METHODS
128              
129             =head2 update
130              
131             Download the database files.
132              
133             =head1 SEE ALSO
134              
135             =over 4
136              
137             =item *
138              
139             L<Bio::MLST::Download::Downloadable>
140              
141             =back
142              
143             =head1 AUTHOR
144              
145             Andrew J. Page <ap13@sanger.ac.uk>
146              
147             =head1 COPYRIGHT AND LICENSE
148              
149             This software is Copyright (c) 2012 by Wellcome Trust Sanger Institute.
150              
151             This is free software, licensed under:
152              
153             The GNU General Public License, Version 3, June 2007
154              
155             =cut