Bio-MLST-Check

 view release on metacpan or  search on metacpan

lib/Bio/MLST/DatabaseSettings.pm  view on Meta::CPAN

package Bio::MLST::DatabaseSettings;
# ABSTRACT: Read in an XML file of settings and return a hash with the values.
$Bio::MLST::DatabaseSettings::VERSION = '2.1.1706216';

use Moose;
use XML::LibXML;
use LWP::UserAgent;
use HTTP::Request;

has 'filename'          => ( is => 'ro', isa => 'Str', required => 1 );

sub generate_dom
{
	my($self, $location ) = @_;
	
	# local file and remote files need to be treated differently
	if ( !( $location =~ /(http|ftp)/ ) ) {
		return XML::LibXML->load_xml( location => $location );	
	}
	else
	{
		# its a remote file so download content
	        my $ua = LWP::UserAgent->new;
		if(defined($ENV{HTTPS_PROXY}))
		{
	        	$ua->proxy( [ 'http', 'https' ], $ENV{HTTPS_PROXY} );
		}
	        my $req = HTTP::Request->new( GET => $location );
	        my $res = $ua->request($req);
	        $res->is_success or die "Could not connect to $location\n";
		XML::LibXML->load_xml( string => $res->content );
	}
}

sub settings
{
  my($self) = @_;
  my %databases_attributes;
  
  my $dom = $self->generate_dom($self->filename);
  
  for my $species ($dom->findnodes('/data/species')) 
  {
    my $species_name = $self->_clean_string($species->firstChild()->data);

    $databases_attributes{$species_name}{profiles} = $self->_clean_string($species->findnodes('./mlst/database/profiles/url')->[0]->firstChild()->data);
    
    for my $allele ($species->findnodes('./mlst/database/loci/locus'))
    {
      if(! defined ($databases_attributes{$species_name}{alleles}) )
      {
        $databases_attributes{$species_name}{alleles} = [];
      }
      push(@{$databases_attributes{$species_name}{alleles}}, $self->_clean_string($allele->findnodes('./url')->[0]->firstChild()->data));
    }
  }
  return \%databases_attributes;
}


sub _clean_string
{
  my($self, $input_string) = @_;
  chomp($input_string);
  $input_string =~ s![\n\r\t]!!g;
  return $input_string;
}

no Moose;
__PACKAGE__->meta->make_immutable;
1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Bio::MLST::DatabaseSettings - Read in an XML file of settings and return a hash with the values.

=head1 VERSION

version 2.1.1706216

=head1 SYNOPSIS



( run in 0.540 second using v1.01-cache-2.11-cpan-7fcb06a456a )