File Coverage

lib/Bio/MLST/DatabaseSettings.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package Bio::MLST::DatabaseSettings;
2             # ABSTRACT: Read in an XML file of settings and return a hash with the values.
3             $Bio::MLST::DatabaseSettings::VERSION = '2.1.1706216';
4              
5 4     4   245951 use Moose;
  4         585589  
  4         23  
6 4     4   20450 use XML::LibXML;
  0            
  0            
7             use LWP::UserAgent;
8             use HTTP::Request;
9              
10             has 'filename' => ( is => 'ro', isa => 'Str', required => 1 );
11              
12             sub generate_dom
13             {
14             my($self, $location ) = @_;
15            
16             # local file and remote files need to be treated differently
17             if ( !( $location =~ /(http|ftp)/ ) ) {
18             return XML::LibXML->load_xml( location => $location );
19             }
20             else
21             {
22             # its a remote file so download content
23             my $ua = LWP::UserAgent->new;
24             if(defined($ENV{HTTPS_PROXY}))
25             {
26             $ua->proxy( [ 'http', 'https' ], $ENV{HTTPS_PROXY} );
27             }
28             my $req = HTTP::Request->new( GET => $location );
29             my $res = $ua->request($req);
30             $res->is_success or die "Could not connect to $location\n";
31             XML::LibXML->load_xml( string => $res->content );
32             }
33             }
34              
35             sub settings
36             {
37             my($self) = @_;
38             my %databases_attributes;
39            
40             my $dom = $self->generate_dom($self->filename);
41            
42             for my $species ($dom->findnodes('/data/species'))
43             {
44             my $species_name = $self->_clean_string($species->firstChild()->data);
45              
46             $databases_attributes{$species_name}{profiles} = $self->_clean_string($species->findnodes('./mlst/database/profiles/url')->[0]->firstChild()->data);
47            
48             for my $allele ($species->findnodes('./mlst/database/loci/locus'))
49             {
50             if(! defined ($databases_attributes{$species_name}{alleles}) )
51             {
52             $databases_attributes{$species_name}{alleles} = [];
53             }
54             push(@{$databases_attributes{$species_name}{alleles}}, $self->_clean_string($allele->findnodes('./url')->[0]->firstChild()->data));
55             }
56             }
57             return \%databases_attributes;
58             }
59              
60              
61             sub _clean_string
62             {
63             my($self, $input_string) = @_;
64             chomp($input_string);
65             $input_string =~ s![\n\r\t]!!g;
66             return $input_string;
67             }
68              
69             no Moose;
70             __PACKAGE__->meta->make_immutable;
71             1;
72              
73             __END__
74              
75             =pod
76              
77             =encoding UTF-8
78              
79             =head1 NAME
80              
81             Bio::MLST::DatabaseSettings - Read in an XML file of settings and return a hash with the values.
82              
83             =head1 VERSION
84              
85             version 2.1.1706216
86              
87             =head1 SYNOPSIS
88              
89             Read in an XML file of settings and return a hash with the values.
90              
91             use Bio::MLST::DatabaseSettings;
92             my $database_settings = Bio::MLST::DatabaseSettings->new(
93             filename => 'filename'
94             );
95             $database_settings->settings;
96              
97             =head1 METHODS
98              
99             =head2 settings
100              
101             Returns a hash containing the settings for the database, separated by species name, and giving alleles and the profile location.
102              
103             =head1 AUTHOR
104              
105             Andrew J. Page <ap13@sanger.ac.uk>
106              
107             =head1 COPYRIGHT AND LICENSE
108              
109             This software is Copyright (c) 2012 by Wellcome Trust Sanger Institute.
110              
111             This is free software, licensed under:
112              
113             The GNU General Public License, Version 3, June 2007
114              
115             =cut