my ($self, $config_path) = @_;
my $config = $self->_load_xref_config($config_path);
# Populate species table with species taxa
print "Iterating over species groups\n";
my %sources;
# First build up records for each potential source
foreach my $section ( $config->GroupMembers('source')) {
my ( $source_name ) = $section =~ /
\A
source\s+(.*)
\Z
/x;
$sources{$source_name} = $self->_mangle_source_block($section, $config);
}
my %compiled_config;
# Next, build a config hash from the species entries in the config file
# and populate them with relevant source information from the %sources
foreach my $section ( $config->GroupMembers('species') ) {
my ( $species_name ) = $section =~ /
\A
species\s+(\S+)
\s*
\Z
/x;
my $taxon_id = $config->val( $section, 'taxonomy_id' );
my @source_names = $config->val( $section, 'source', ());
my %sources_for_species;
foreach my $source_entry (@source_names) {
if (! exists $sources{$source_entry}) {
confess 'Species config references a source that is not defined in the config: '.$source_entry;
}
$sources_for_species{$source_entry} = $sources{$source_entry};
}
$compiled_config{$species_name} = {
species_id => $taxon_id, # species_id == taxon_id in xref system,
taxonomy_id => $taxon_id,
sources => \%sources_for_species
}
}
# Now populate the database with the result each source entry in the config with source-specific info
foreach my $species ( keys %compiled_config ) {
my $species_record = $self->schema->resultset('Species')->create({
species_id => $compiled_config{$species}{species_id},
name => $species,
taxonomy_id => $compiled_config{$species}{taxonomy_id}
});
my $compiled_sources = $compiled_config{$species}{sources};
foreach my $source ( keys %$compiled_sources ) {
my $parser = delete $compiled_sources->{$source}->{parser};
my $source_record = $self->schema->resultset('Source')->find_or_create(
$compiled_sources->{$source} # once trimmed, we can pump the source hash straight into DBIC
);
delete $sources{$source};
$source_record->create_related(
'source_url',
{
species_id => $compiled_config{$species}{species_id},
parser => $parser
}
);
} # End foreach source
} # End foreach species
# Add any sources which are not explicitly linked to a species
foreach my $source ( keys %sources ) {
my $parser = delete $sources{$source}->{parser};
my $source_record = $self->schema->resultset('Source')->find_or_create(
$sources{$source}
);
}
return;
}