MOSES-MOBY

 view release on metacpan or  search on metacpan

lib/MOSES/MOBY/Cache/Central.pm  view on Meta::CPAN

#-----------------------------------------------------------------
# MOSES::MOBY::Cache::Central
# Author: Edward Kawas <edward.kawas@gmail.com>,
#         Martin Senger <martin.senger@gmail.com>
# For copyright and disclaimer see below.
#
# $Id: Central.pm,v 1.9 2009/10/09 17:29:28 kawas Exp $
#-----------------------------------------------------------------

package MOSES::MOBY::Cache::Central;

use MOSES::MOBY::Base;
use base qw( MOSES::MOBY::Base );
use MOSES::MOBY::Cache::Registries;
use MOSES::MOBY::Def::DataType;
use MOSES::MOBY::Def::Service;
use MOSES::MOBY::Def::Data;
use MOSES::MOBY::Def::Namespace;
use MOSES::MOBY::Def::Relationship;
use SOAP::Lite;
use XML::LibXML;
use File::Spec;
use strict;
use vars qw ($DEFAULT_REGISTRY_URL $VERSION);

# names of cache directories/files
use constant LIST_FILE          => '__L__I__S__T__';
use constant DATATYPES_CACHE    => 'dataTypes';
use constant SERVICES_CACHE     => 'services';
use constant NAMESPACES_CACHE   => 'namespaces';
use constant SERVICETYPES_CACHE => 'serviceTypes';

# the version of this file:
$VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /: (\d+)\.(\d+)/;

=head1 NAME

MOSES::MOBY::Cache::Central - access to locally cached Moby entities

=cut

=head1 SYNOPSIS

 use MOSES::MOBY::Cache::Central;

 # create an aceess to a Moby registry cache
 # (use 'registry' only for non-default registries)
 my $cache = new MOSES::MOBY::Cache::Central
    ( cachedir =>'/usr/local/cache/',
     );

 my $cache = new MOSES::MOBY::Cache::Central
    ( cachedir =>'/usr/local/cache/',
      registry => 'IRRI'
    );

 # get the location of the cache and the URL of a registry (whose
 # cache we are accessing)
 print $cache->cachedir;
 print $cache->registry();

 # create a cache for datatypes and fill it up
 $cache->create_datatype_cache;
 
 #update the datatype cache
 $cache->update_datatype_cache;
 
 # create a cache for services and fill it up
 $cache->create_service_cache;
 
 #update the services cache
 $cache->update_service_cache;
 
 # get a data type called DNASequence
 my $dna = $cache->get_datatype ('DNASequence');
	
 # get all datatypes from cache
 my @dts = $cache->get_datatypes;

 # get all services provided by the given authority
 my @services = $cache->get_service ('bioinfo.inibap.org');

 # get some services provided by the given authority
 my @services = $cache->get_service
    ('bioinfo.inibap.org',
     qw( Get_TropGENE_Distance_Matrix Get_TropGENE_Nj_Tree ));

 # get all authorities and service names
 require Data::Dumper;
 my %all = $cache->get_service_names;
 print Data::Dumper->Dump ( [ \%all ], ['By_authorities']);

lib/MOSES/MOBY/Cache/Central.pm  view on Meta::CPAN

		my  $name = $node->getAttribute('name');
		my $authority = $node->getAttribute('authURI');
		if (exists $hash{$authority}) {
			my $array_ref = $hash{$authority};
			my @array = @{$array_ref};
			push @{array}, $name;
			$hash{$authority} = \@array;
		}else {
			my @array = ();
			push @array, $name;
			$hash{$authority} = \@array;
		}
    }
    return %hash;
}

#-----------------------------------------------------------------
# cache_exists
#-----------------------------------------------------------------

=head2 cache_exists

Return true if a local cache for the given registry exists (or
probably exists). An argument is a synonym, or an endpoint, of a
registry. See more about registry synonyms in
L<MOSES::MOBY::Cache::Registries>.

Here is how to ask for all existing registries:



=cut

sub cache_exists {
    my ($self, $registry) = @_;
    my $pathToList = File::Spec->catfile ($self->cachedir,
					  $self->_clean ($self->_endpoint ($registry)),
					  SERVICES_CACHE);
    return -e $pathToList;
}


#-----------------------------------------------------------------
# create_cache_dirs
#-----------------------------------------------------------------

=head2 create_cache_dirs

Creates the cache directories needed for generating datatypes and services.

Throws an exception if there are problems creating the directories.

=cut

sub create_cache_dirs {
    my ($self)= @_;
    my @dirs = (
    	File::Spec->catfile ($self->cachedir,$self->_clean ($self->_endpoint),DATATYPES_CACHE),
    	File::Spec->catdir ($self->cachedir,$self->_clean ($self->_endpoint),SERVICES_CACHE),
    	File::Spec->catdir ($self->cachedir,$self->_clean ($self->_endpoint),NAMESPACES_CACHE),
    	File::Spec->catdir ($self->cachedir,$self->_clean ($self->_endpoint),SERVICETYPES_CACHE),
     );
    
    foreach my $file (@dirs) {
    	my ($v, $d, $f) = File::Spec->splitpath( $file );
    	my $dir = File::Spec->catdir($v);
    	foreach my $part ( File::Spec->splitdir( ($d.$f ) ) ) {
        	$dir = File::Spec->catdir($dir, $part);
        	next if -d $dir or -e $dir;
        	mkdir( $dir ) || $self->throw("Error creating caching directory '".$dir."':\n$!");
        	$LOG->debug("creating the directory, '$dir'.");
    	}
    }
}

1;
__END__



( run in 1.401 second using v1.01-cache-2.11-cpan-5837b0d9d2c )