[Bio] / Sprout / ERDBLoader.pl Repository:
ViewVC logotype

View of /Sprout/ERDBLoader.pl

Parent Directory Parent Directory | Revision Log Revision Log

Revision 1.5 - (download) (as text) (annotate)
Wed Oct 15 11:41:12 2008 UTC (11 years, 5 months ago) by parrello
Branch: MAIN
CVS Tags: rast_rel_2008_12_18, mgrast_rel_2008_1110_v2, mgrast_rel_2008_1110, rast_rel_2008_10_29, rast_rel_2008_11_24
Changes since 1.4: +0 -1 lines
No discernible change.

#!/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.

use strict;
use Tracer;
use ERDB;
use ERDBLoadGroup;
use ERDBGenerate;
use Stats;
use Time::HiRes;

=head1 ERDBLoader Script

    ERDBLoader [options] <database> <group1> <group2> ...

ERDB Database Load Finisher

=head2 Introduction

This script finishes the database load process begun by [[ERDBGeneratorPl]].

[[ERDBGeneratorPl]] divides the source data into sections, and generates a
partial load file for each section of each table. To finish the load process, we
need to combine the partial files into single files and load the resulting
single files into the database tables.

Like [[ERDBGeneratorPl]], this script acts on load groups-- sets of related
tables that are loaded at the same time. For each table in a named group that
does not exist in the database, the script first attempts to find a completed
data file. If one does not exist, it attempts to create one by collating section
files. Once the collated section files for a load group are finished, they are
loaded into the database.

=head2 Positional Parameters

=over 4

=item database

Name of the ERDB database. This should be the class name for the subclass used
to access the database.


=head2 Command-Line Options

=over 4

=item trace

Specifies the tracing level. The higher the tracing level, the more messages
will appear in the trace log. Use E to specify emergency tracing.

=item user

Name suffix to be used for log files. If omitted, the PID is used.

=item sql

If specified, turns on tracing of SQL activity.

=item background

Save the standard and error output to files. The files will be created
in the FIG temporary directory and will be named C<err>I<User>C<.log> and
C<out>I<User>C<.log>, respectively, where I<User> is the value of the
B<user> option above.

=item help

Display this command's parameters and options.

=item keepSections

If specified, section files (the fragments of data load files created by
[[ERDBGeneratorPl]], will not be deleted after they are collated.

=item sanityCheck

If specified, no tables will be loaded. Instead, the first I<N> records from the
assembled load files will be displayed so that the file contents can be
visually matched against the column names.

=item warn

Create an event in the RSS feed when an error occurs.

=item phone

Phone number to message when the script is complete.



# Get the command-line options and parameters.
my ($options, @parameters) = StandardSetup([qw(ERDBLoadGroup ERDB Stats) ],
                                              sanityCheck => ["", "don't load, trace contents of first N load file records instead"],
                                              trace => ["", "tracing level"],
                                              keepSections => ["", "if specified, section files will not be deleted after being collated"],
                                              phone => ["", "phone number (international format) to call when load finishes"]
                                           "<database> <group1> <group2> ...",
# Set a variable to contain return type information.
my $rtype;
# Insure we catch errors.
eval {
    # Get the parameters.
    my ($database, @groups) = @parameters;
    # Connect to the database.
    my $erdb = ERDB::GetDatabase($database);
    # Fix the group list.
    my @realGroups = ERDBLoadGroup::ComputeGroups($erdb, \@groups);
    # Get the source object and load directory for this database.
    my $source = $erdb->GetSourceObject();
    my $directory = $erdb->LoadDirectory();
    # Get the list of sections.
    my @sectionList = $erdb->SectionList($source);
    # Create a statistics object to track our progress.
    my $stats = Stats->new();
    # Find out if we're doing a sanity check.
    my $sanityCheck = $options->{sanityCheck} || "";
    # Start a timer.
    my $totalStart = time();
    # Loop through the groups.
    for my $group (@realGroups) {
        # Get the list of tables for this group.
        my @tableList = ERDBLoadGroup::GetTables($erdb, $group);
        # We need to insure there is a data file for every table. If we fail to find one,
        # we set the following error flag, which prevents us from loading the database.
        my $missingTable = 0;
        # Loop through the tables in this group.
        for my $table (@tableList) {
            Trace("Processing table $table for assembly.") if T(2);
            # Get the section file names.
            my @sectionFiles =
                map { ERDBGenerate::CreateFileName($table, $_, 'data', $directory) } @sectionList;
            # Get the data file name.
            my $dataFile = ERDBGenerate::CreateFileName($table, undef, 'data', $directory);
            # Do we have it?
            if (-f $dataFile) {
                # Yes. This is good news.
                $stats->Add('tables-found' => 1);
                Trace("Table file found for $table.") if T(3);
            } else {
                # No, we must build it. Verify that we have all the sections.
                my @missingFiles = grep { ! -f $_ } @sectionFiles;
                # Tell the user about all the missing files.
                for my $missingFile (@missingFiles) {
                    $stats->Add('sections-missing' => 1);
                    $stats->AddMessage("Data file $missingFile not found for table $table.");
                # Did we find everything?
                if (scalar @missingFiles) {
                    # No! Denote that we have a missing table.
                    $stats->Add('tables-skipped' => 1);
                } else {
                    # Yes! Try to assemble the sections into a data file.
                    my $sortStart = time();
                    my $sortCommand = $erdb->SortNeeded($table) . " >$dataFile";
                    Trace("Sort command: $sortCommand") if T(3);
                    my $oh = Open(undef, "| $sortCommand");
                    for my $sectionFile (@sectionFiles) {
                        Trace("Collating $sectionFile.") if T(3);
                        $stats->Add("$table-sections" => 1);
                        for my $line (Tracer::GetFile($sectionFile)) {
                            print $oh "$line\n";
                            $stats->Add("$table-collations" => 1);
                    # Finish the sort step.
                    Trace("Finishing collate for $table.") if T(3);
                    close $oh;
                    $stats->Add('tables-collated' => 1);
                    $stats->Add('collate-time' => time() - $sortStart);
            # Now that we know we have a full data file, we can delete the
            # section files to make room in the data directory. The user can
            # turn this behavior off with the keepSections option.
            if (! $options->{keepSections}) {
                for my $sectionFile (@sectionFiles) {
                    if (-e $sectionFile) {
                        unlink $sectionFile;
                        $stats->Add('files-deleted' => 1);
                Trace("Section files for $table deleted.") if T(3);
        # Were any tables missing?
        if ($missingTable) {
            # Yes, skip this group.
            $stats->Add('groups-skipped' => 1);
            Trace("Skipping $group group: $missingTable missing tables.") if T(3);
        } else {
            # No! Process this group's files.
            if ($sanityCheck eq "") {
                Trace("Loading group $group into database.") if T(2);
            } else {
                Trace("Sanity check for group $group.") if T(2);
            my $loadStart = time();
            for my $table (@tableList) {
                my $fileName = ERDBGenerate::CreateFileName($table, undef, 'data', $directory);
                # Do we want a real load or a sanity check?
                if ($sanityCheck eq "") {
                    # Real load.
                    my $newStats = $erdb->LoadTable($fileName, $table, truncate => 1, failOnError => 1);
                    Trace("$fileName loaded into $table.") if T(3);
                } elsif ($sanityCheck > 0) {
                    # Here we want a sanity check. Note that if the check value is 0,
                    # we don't bother. The user just wants to suppress the load step.
                    CheckLoadFile($erdb, $table, $fileName, $sanityCheck);
            $stats->Add("groups-loaded" => 1);
            $stats->Add('load-time' => 1);
    $stats->Add('total-time' => time() - $totalStart);
    # Display the statistics from this run.
    Trace("Statistics for load:\n" . $stats->Show()) if T(2);
if ($@) {
    Trace("Script failed with error: $@") if T(0);
} else {
    Trace("Script complete.") if T(2);
if ($options->{phone}) {
    my $msgID = Tracer::SendSMS($options->{phone}, "ERDBLoader completed.");
    if ($msgID) {
        Trace("Phone message sent with ID $msgID.") if T(2);
    } else {
        Trace("Phone message not sent.") if T(2);

=head3 CheckLoadFile

    CheckLoadFile($erdb, $table, $fileName, $count);

Read the first few records of a load file and trace the contents at level
2. This allows the user to visually compare the load file contents with
the database definition.

=over 4

=item erdb

[[ErdbPm]] object describing the database.

=item table

Name of the table to check.

=item fileName

Name of the load file to check.

=item count

Number of records to check.



sub CheckLoadFile {
    # Get the parameters.
    my ($erdb, $table, $fileName, $count) = @_;
    # Open the file for input.
    my $ih = Open(undef, "<$fileName");
    # Slurp the first N records.
    my @records;
    while (! eof $ih && scalar(@records) < $count) {
        push @records, [ Tracer::GetLine($ih) ];
    my $found = scalar(@records);
    Trace("$found records for $table found in sanity check.") if T(3);
    # Do we have any data at all?
    if ($found) {
        # Yes. Get the table's descriptor. We use this to determine the field names.
        my $relationData = $erdb->FindRelation($table);
        Confess("Relation $table not found in database.") if (! defined $relationData);
        my @fields = @{$relationData->{Fields}};
        # Loop through the fields. We generate one message per field.
        for (my $i = 0; $i <= $#fields; $i++) {
            # Get this field's information.
            my $fieldInfo = $fields[$i];
            my $type = $fieldInfo->{type};
            # This is going to be a multi-line trace message. We start with the field name and type.
            my @lines = ("Values for $fieldInfo->{name}, type $type:\n");
            # Loop through the records. We generate one line of data per record.
            for (my $j = 0; $j < $found; $j++) {
                # Get the field value.
                my $field = $records[$j]->[$i];
                # Compute the record label.
                my $line = "Record $j";
                # Check for unusual cases.
                if (! defined $field) {
                    $line .= "= <null>";
                } elsif ($field eq '') {
                    $line .= "= <empty>";
                } else {
                    # Make sure we don't trace something ungodly.
                    my $excess = (length $field) - 40;
                    if ($excess > 0) {
                        $field = substr($field, 0, 40) . " >> + $excess characters";
                    $line .= ": $field";
                # Save this line. We indent a little for readability.
                push @lines, "   $line";
            # Trace this field.
            Trace(join("\n", @lines)) if T(2);


MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3