[Bio] / Sprout / SaplingDataLoader.pm Repository:
ViewVC logotype

View of /Sprout/SaplingDataLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (download) (as text) (annotate)
Tue Jan 25 19:41:04 2011 UTC (9 years, 1 month ago) by parrello
Branch: MAIN
CVS Tags: mgrast_release_3_0, mgrast_release_3_0_2, mgrast_release_3_0_1
Changes relating to the Expression Directory loader.

#!/usr/bin/perl -w

#
# Copyright (c) 2003-2006 University of Chicago and Fellowship
# for Interpretations of Genomes. All Rights Reserved.
#
# This file is part of the SEED Toolkit.
#
# The SEED Toolkit is free software. You can redistribute
# it and/or modify it under the terms of the SEED Toolkit
# Public License.
#
# You should have received a copy of the SEED Toolkit Public License
# along with this program; if not write to the University of Chicago
# at info@ci.uchicago.edu or the Fellowship for Interpretation of
# Genomes at veronika@thefig.info or download a copy from
# http://www.theseed.org/LICENSE.TXT.
#

package SaplingDataLoader;

    use strict;
    use Tracer;
    use Stats;
    use SeedUtils;
    use SAPserver;
    use Sapling;

=head1 Sapling Data Loader

This is the base class for packages that load the Sapling database from
SEED data files.

=head2 Loader Object Methods

=head3 new

    my $loaderObject = SaplingGenomeLoader->new($sap, @stats);

Create a loader object that can be used to facilitate loading Sapling data from a
directory.

=over 4

=item sap

L<Sapling> object used to access the target database.

=item stats

List of names for statistics to be initialized in the statistics object.

=back

The object created contains the following fields.

=over 4

=item supportRecords

A hash of hashes, used to track the support records known to exist in the database.

=item sap

L<Sapling> object used to access the database.

=item stats

L<Stats> object for tracking statistical information about the load.

=back

=cut

sub new {
    # Get the parameters.
    my ($class, $sap, @stats) = @_;
    # Create the object.
    my $retVal = {
        sap => $sap,
        stats => Stats->new(@stats),
        supportRecords => {}
    };
    # Bless and return it.
    bless $retVal, $class;
    return $retVal;
}

=head2 Internal Utility Methods

=head3 DeleteRelatedRecords

    DeleteRelatedRecords($sap, $genome, $stats, $relName, $entityName);

Delete all the records in the named entity and relationship relating to the
specified genome and roll up the statistics in the specified statistics object.

=over 4

=item sap

L<Sapling> object for accessing the database.

=item genome

ID of the relevant genome.

=item stats

L<Stats> object for tracking the delete activity.

=item relName

Name of a relationship from the B<Genome> table.

=item entityName

Name of the entity on the other side of the relationship.

=back

=cut

sub DeleteRelatedRecords {
    # Get the parameters.
    my ($sap, $genome, $stats, $relName, $entityName) = @_;
    # Get all the relationship records.
    my (@targets) = $sap->GetFlat($relName, "$relName(from-link) = ?", [$genome],
                                  "to-link");
    # Loop through the relationship records, deleting them and the target entity
    # records.
    for my $target (@targets) {
        # Delete the relationship instance.
        $sap->DeleteRow($relName, $genome, $target);
        $stats->Add($relName => 1);
        # Delete the entity instance.
        my $subStats = $sap->Delete($entityName, $target);
        # Roll up the statistics.
        $stats->Accumulate($subStats);
    }
}

=head3 ExtractFields

    my %fieldHash = SaplingGenomeLoader::ExtractFields($tableName, $dataHash);

Extract from the incoming hash the field names and values from the specified table.

=over 4

=item tableName

Name of the table whose field names and values are desired.

=item dataHash

Reference to a hash mapping fully-qualified ERDB field names to values.

=item RETURN

Returns a hash containing only the fields from the specified table and their values.

=back

=cut

sub ExtractFields {
    # Get the parameters.
    my ($tableName, $dataHash) = @_;
    # Declare the return variable.
    my %retVal;
    # Extract the desired fields.
    for my $field (keys %$dataHash) {
        # Is this a field for the specified table?
        if ($field =~ /^$tableName\(([^)]+)/) {
            # Yes, put it in the output hash.
            $retVal{$1} = $dataHash->{$field};
        }
    }
    # Return the computed hash.
    return %retVal;
}

=head3 InsureEntity

    my $createdFlag = $loaderObject->InsureEntity($entityType => $id, %fields);

Insure that the specified record exists in the database. If no record is found of the
specified type with the specified ID, one will be created with the indicated fields.

=over 4

=item $entityType

Type of entity to check.

=item id

ID of the entity instance in question.

=item fields

Hash mapping field names to values for all the fields in the desired entity record except
for the ID.

=item RETURN

Returns TRUE if a new object was created, FALSE if it already existed.

=back

=cut

sub InsureEntity {
    # Get the parameters.
    my ($self, $entityType, $id, %fields) = @_;
    # Get the database.
    my $sap = $self->{sap};
    # Get the support record ID hash.
    my $supportHash = $self->{supportRecords};
    # Denote we haven't created a new record.
    my $retVal = 0;
    # Get the sub-hash for this entity type.
    my $entityHash = $supportHash->{$entityType};
    if (! defined $entityHash) {
        $entityHash = {};
        $supportHash->{$entityType} = $entityHash;
    }
    # Check for this instance.
    if (! $entityHash->{$id}) {
        # It's not found. Check the database.
        if (! $sap->Exists($entityType => $id)) {
            # It's not in the database either, so create it.
            $sap->InsertObject($entityType, id => $id, %fields);
            $self->{stats}->Add(insertSupport => 1);
            $retVal = 1;
        }
        # Mark the record in the hash so we know we have it.
        $entityHash->{$id} = 1;
    }
    # Return the insertion indicator.
    return $retVal;
}

1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3