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

Annotation of /Sprout/ERDBLoader.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (view) (download) (as text)

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     #
4 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
5 :     # for Interpretations of Genomes. All Rights Reserved.
6 :     #
7 :     # This file is part of the SEED Toolkit.
8 :     #
9 :     # The SEED Toolkit is free software. You can redistribute
10 :     # it and/or modify it under the terms of the SEED Toolkit
11 :     # Public License.
12 :     #
13 :     # You should have received a copy of the SEED Toolkit Public License
14 :     # along with this program; if not write to the University of Chicago
15 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
16 :     # Genomes at veronika@thefig.info or download a copy from
17 :     # http://www.theseed.org/LICENSE.TXT.
18 :     #
19 :    
20 :     use strict;
21 :     use Tracer;
22 :     use ERDB;
23 :     use ERDBLoadGroup;
24 :     use ERDBGenerate;
25 :     use Stats;
26 : parrello 1.2 use Time::HiRes;
27 : parrello 1.1
28 :    
29 :     =head1 ERDBLoader Script
30 :    
31 :     ERDBLoader [options] <database> <group1> <group2> ...
32 :    
33 :     ERDB Database Load Finisher
34 :    
35 :     =head2 Introduction
36 :    
37 :     This script finishes the database load process begun by [[ERDBGeneratorPl]].
38 :    
39 :     [[ERDBGeneratorPl]] divides the source data into sections, and generates a
40 :     partial load file for each section of each table. To finish the load process, we
41 :     need to combine the partial files into single files and load the resulting
42 :     single files into the database tables.
43 :    
44 :     Like [[ERDBGeneratorPl]], this script acts on load groups-- sets of related
45 :     tables that are loaded at the same time. For each table in a named group that
46 :     does not exist in the database, the script first attempts to find a completed
47 :     data file. If one does not exist, it attempts to create one by collating section
48 : parrello 1.2 files. Once the collated section files for a load group are finished, they are
49 :     loaded into the database.
50 : parrello 1.1
51 :     =head2 Positional Parameters
52 :    
53 :     =over 4
54 :    
55 :     =item database
56 :    
57 :     Name of the ERDB database. This should be the class name for the subclass used
58 :     to access the database.
59 :    
60 :     =back
61 :    
62 :     =head2 Command-Line Options
63 :    
64 :     =over 4
65 :    
66 :     =item trace
67 :    
68 :     Specifies the tracing level. The higher the tracing level, the more messages
69 :     will appear in the trace log. Use E to specify emergency tracing.
70 :    
71 :     =item user
72 :    
73 :     Name suffix to be used for log files. If omitted, the PID is used.
74 :    
75 :     =item sql
76 :    
77 :     If specified, turns on tracing of SQL activity.
78 :    
79 :     =item background
80 :    
81 :     Save the standard and error output to files. The files will be created
82 :     in the FIG temporary directory and will be named C<err>I<User>C<.log> and
83 :     C<out>I<User>C<.log>, respectively, where I<User> is the value of the
84 :     B<user> option above.
85 :    
86 :     =item help
87 :    
88 :     Display this command's parameters and options.
89 :    
90 : parrello 1.3 =item keepSections
91 :    
92 :     If specified, section files (the fragments of data load files created by
93 :     [[ERDBGeneratorPl]], will not be deleted after they are collated.
94 :    
95 : parrello 1.4 =item sanityCheck
96 :    
97 :     If specified, no tables will be loaded. Instead, the first I<N> records from the
98 :     assembled load files will be displayed so that the file contents can be
99 :     visually matched against the column names.
100 :    
101 : parrello 1.1 =item warn
102 :    
103 :     Create an event in the RSS feed when an error occurs.
104 :    
105 :     =item phone
106 :    
107 :     Phone number to message when the script is complete.
108 :    
109 : parrello 1.6 =item DBD
110 :    
111 :     Name of the DBD file. If specified, the DBD must be in the main FIG directory
112 :     (specified in C<$FIG_Config::fig>). This option allows the use of an alternate
113 :     DBD during load, so that access to the database by other processes is not
114 :     compromised.
115 :    
116 : parrello 1.1 =back
117 :    
118 :     =cut
119 :    
120 :     # Get the command-line options and parameters.
121 :     my ($options, @parameters) = StandardSetup([qw(ERDBLoadGroup ERDB Stats) ],
122 :     {
123 : parrello 1.4 sanityCheck => ["", "don't load, trace contents of first N load file records instead"],
124 : parrello 1.6 trace => ["2", "tracing level"],
125 : parrello 1.3 keepSections => ["", "if specified, section files will not be deleted after being collated"],
126 : parrello 1.6 phone => ["", "phone number (international format) to call when load finishes"],
127 :     DBD => ["", "if specified, the name of a DBD file in the FIG directory"],
128 : parrello 1.1 },
129 :     "<database> <group1> <group2> ...",
130 :     @ARGV);
131 :     # Set a variable to contain return type information.
132 :     my $rtype;
133 :     # Insure we catch errors.
134 :     eval {
135 :     # Get the parameters.
136 :     my ($database, @groups) = @parameters;
137 : parrello 1.6 # Check for an alternate DBD.
138 :     my $altDBD = ($options->{DBD} ? "$FIG_Config::fig/$options->{DBD}" : undef);
139 :     # Connect to the database and get its load directory.
140 :     my $erdb = ERDB::GetDatabase($database, $altDBD);
141 : parrello 1.1 # Fix the group list.
142 : parrello 1.3 my @realGroups = ERDBLoadGroup::ComputeGroups($erdb, \@groups);
143 : parrello 1.1 # Get the source object and load directory for this database.
144 :     my $source = $erdb->GetSourceObject();
145 :     my $directory = $erdb->LoadDirectory();
146 :     # Get the list of sections.
147 :     my @sectionList = $erdb->SectionList($source);
148 :     # Create a statistics object to track our progress.
149 :     my $stats = Stats->new();
150 : parrello 1.4 # Find out if we're doing a sanity check.
151 :     my $sanityCheck = $options->{sanityCheck} || "";
152 : parrello 1.2 # Start a timer.
153 :     my $totalStart = time();
154 : parrello 1.1 # Loop through the groups.
155 :     for my $group (@realGroups) {
156 :     # Get the list of tables for this group.
157 : parrello 1.3 my @tableList = ERDBLoadGroup::GetTables($erdb, $group);
158 : parrello 1.2 # We need to insure there is a data file for every table. If we fail to find one,
159 :     # we set the following error flag, which prevents us from loading the database.
160 :     my $missingTable = 0;
161 :     # Loop through the tables in this group.
162 : parrello 1.3 for my $table (@tableList) {
163 : parrello 1.4 Trace("Processing table $table for assembly.") if T(2);
164 :     # Get the section file names.
165 :     my @sectionFiles =
166 :     map { ERDBGenerate::CreateFileName($table, $_, 'data', $directory) } @sectionList;
167 : parrello 1.2 # Get the data file name.
168 :     my $dataFile = ERDBGenerate::CreateFileName($table, undef, 'data', $directory);
169 :     # Do we have it?
170 :     if (-f $dataFile) {
171 :     # Yes. This is good news.
172 :     $stats->Add('tables-found' => 1);
173 : parrello 1.4 Trace("Table file found for $table.") if T(3);
174 : parrello 1.2 } else {
175 :     # No, we must build it. Verify that we have all the sections.
176 :     my @missingFiles = grep { ! -f $_ } @sectionFiles;
177 :     # Did we find everything?
178 :     if (scalar @missingFiles) {
179 :     # No! Denote that we have a missing table.
180 : parrello 1.3 $missingTable++;
181 : parrello 1.2 $stats->Add('tables-skipped' => 1);
182 : parrello 1.6 # If the user wants a sanity check, we want to give him some
183 :     # data anyway.
184 :     if ($sanityCheck) {
185 :     # Get some data lines in the sections. Note we stop when we've exceeded
186 :     # the number of lines expected by the sanity check.
187 :     my @lines;
188 :     for my $sectionFile (@sectionFiles) {
189 :     if (-s $sectionFile && scalar(@lines) < $sanityCheck) {
190 :     Trace("Reading from $sectionFile for $table.") if T(3);
191 :     push @lines, Tracer::GetFile($sectionFile);
192 :     }
193 :     }
194 :     # Create a new temporary file.
195 :     my $tmpFile = "$FIG_Config::temp/$table$$.temp.dtx";
196 :     my $oh = Open(undef, ">$tmpFile");
197 :     # Put all the data into it.
198 :     Trace(scalar(@lines) . " data lines found.") if T(3);
199 :     print $oh join("\n", @lines);
200 :     close $oh;
201 :     # Sanity check the temp file.
202 :     CheckLoadFile($erdb, $table, $tmpFile, $sanityCheck);
203 :     # Clean it up.
204 :     unlink $tmpFile;
205 :     } else {
206 :     # Otherwise tell the user about all the missing files.
207 :     for my $missingFile (@missingFiles) {
208 :     $stats->Add('sections-missing' => 1);
209 :     $stats->AddMessage("Data file $missingFile not found for table $table.");
210 :     }
211 :     }
212 : parrello 1.2 } else {
213 : parrello 1.6 # We have all the sections. Try to assemble them into a data file.
214 : parrello 1.2 my $sortStart = time();
215 : parrello 1.4 my $sortCommand = $erdb->SortNeeded($table) . " >$dataFile";
216 :     Trace("Sort command: $sortCommand") if T(3);
217 : parrello 1.6 # Pipe to the sort command. Note that we turn on autoflush
218 :     # so there's no buffering.
219 : parrello 1.4 my $oh = Open(undef, "| $sortCommand");
220 : parrello 1.6 select $oh; $| = 1; select STDOUT;
221 :     # Loop through the sections.
222 : parrello 1.2 for my $sectionFile (@sectionFiles) {
223 : parrello 1.4 Trace("Collating $sectionFile.") if T(3);
224 :     $stats->Add("$table-sections" => 1);
225 : parrello 1.6 # Loop through the section file.
226 :     my $ih = Open(undef, "<$sectionFile");
227 :     while (defined (my $line = <$ih>)) {
228 :     print $oh $line;
229 : parrello 1.4 $stats->Add("$table-collations" => 1);
230 : parrello 1.2 }
231 :     }
232 :     # Finish the sort step.
233 : parrello 1.4 Trace("Finishing collate for $table.") if T(3);
234 : parrello 1.2 close $oh;
235 :     $stats->Add('tables-collated' => 1);
236 : parrello 1.4 $stats->Add('collate-time' => time() - $sortStart);
237 :     }
238 :     }
239 :     # Now that we know we have a full data file, we can delete the
240 :     # section files to make room in the data directory. The user can
241 :     # turn this behavior off with the keepSections option.
242 :     if (! $options->{keepSections}) {
243 :     for my $sectionFile (@sectionFiles) {
244 :     if (-e $sectionFile) {
245 :     unlink $sectionFile;
246 :     $stats->Add('files-deleted' => 1);
247 : parrello 1.2 }
248 :     }
249 : parrello 1.4 Trace("Section files for $table deleted.") if T(3);
250 : parrello 1.2 }
251 :     }
252 :     # Were any tables missing?
253 :     if ($missingTable) {
254 :     # Yes, skip this group.
255 :     $stats->Add('groups-skipped' => 1);
256 : parrello 1.6 Trace("Skipping $group group: $missingTable missing tables.") if T(2);
257 : parrello 1.2 } else {
258 : parrello 1.4 # No! Process this group's files.
259 :     if ($sanityCheck eq "") {
260 :     Trace("Loading group $group into database.") if T(2);
261 :     } else {
262 :     Trace("Sanity check for group $group.") if T(2);
263 :     }
264 : parrello 1.2 my $loadStart = time();
265 : parrello 1.3 for my $table (@tableList) {
266 : parrello 1.2 my $fileName = ERDBGenerate::CreateFileName($table, undef, 'data', $directory);
267 : parrello 1.4 # Do we want a real load or a sanity check?
268 :     if ($sanityCheck eq "") {
269 :     # Real load.
270 :     my $newStats = $erdb->LoadTable($fileName, $table, truncate => 1, failOnError => 1);
271 :     $stats->Accumulate($newStats);
272 :     Trace("$fileName loaded into $table.") if T(3);
273 :     } elsif ($sanityCheck > 0) {
274 :     # Here we want a sanity check. Note that if the check value is 0,
275 :     # we don't bother. The user just wants to suppress the load step.
276 :     CheckLoadFile($erdb, $table, $fileName, $sanityCheck);
277 :     }
278 : parrello 1.2 }
279 :     $stats->Add("groups-loaded" => 1);
280 :     $stats->Add('load-time' => 1);
281 :     }
282 : parrello 1.1 }
283 : parrello 1.2 $stats->Add('total-time' => time() - $totalStart);
284 :     # Display the statistics from this run.
285 :     Trace("Statistics for load:\n" . $stats->Show()) if T(2);
286 : parrello 1.1 };
287 :     if ($@) {
288 :     Trace("Script failed with error: $@") if T(0);
289 :     } else {
290 :     Trace("Script complete.") if T(2);
291 :     }
292 :     if ($options->{phone}) {
293 : parrello 1.2 my $msgID = Tracer::SendSMS($options->{phone}, "ERDBLoader completed.");
294 : parrello 1.1 if ($msgID) {
295 :     Trace("Phone message sent with ID $msgID.") if T(2);
296 :     } else {
297 :     Trace("Phone message not sent.") if T(2);
298 :     }
299 :     }
300 :    
301 : parrello 1.4 =head3 CheckLoadFile
302 :    
303 :     CheckLoadFile($erdb, $table, $fileName, $count);
304 :    
305 :     Read the first few records of a load file and trace the contents at level
306 :     2. This allows the user to visually compare the load file contents with
307 :     the database definition.
308 :    
309 :     =over 4
310 :    
311 :     =item erdb
312 :    
313 :     [[ErdbPm]] object describing the database.
314 :    
315 :     =item table
316 :    
317 :     Name of the table to check.
318 :    
319 :     =item fileName
320 :    
321 :     Name of the load file to check.
322 :    
323 :     =item count
324 :    
325 :     Number of records to check.
326 :    
327 :     =back
328 :    
329 :     =cut
330 :    
331 :     sub CheckLoadFile {
332 :     # Get the parameters.
333 :     my ($erdb, $table, $fileName, $count) = @_;
334 :     # Open the file for input.
335 :     my $ih = Open(undef, "<$fileName");
336 :     # Slurp the first N records.
337 :     my @records;
338 :     while (! eof $ih && scalar(@records) < $count) {
339 :     push @records, [ Tracer::GetLine($ih) ];
340 :     }
341 :     my $found = scalar(@records);
342 : parrello 1.6 Trace("$found records for $table found in sanity check using $fileName.") if T(2);
343 : parrello 1.4 # Do we have any data at all?
344 :     if ($found) {
345 :     # Yes. Get the table's descriptor. We use this to determine the field names.
346 :     my $relationData = $erdb->FindRelation($table);
347 :     Confess("Relation $table not found in database.") if (! defined $relationData);
348 :     my @fields = @{$relationData->{Fields}};
349 : parrello 1.6 # If this is a relationship, we need the FROM and TO data.
350 :     my %ends; ($ends{from}, $ends{to}) = $erdb->GetRelationshipEntities($table);
351 : parrello 1.4 # Loop through the fields. We generate one message per field.
352 :     for (my $i = 0; $i <= $#fields; $i++) {
353 :     # Get this field's information.
354 :     my $fieldInfo = $fields[$i];
355 :     my $type = $fieldInfo->{type};
356 : parrello 1.6 my $name = $fieldInfo->{name};
357 :     if ($name =~ /^(from|to)-link$/) {
358 :     # Here it's a relationship link, so add the name of the target table to
359 :     # the type.
360 :     $type .= " ($ends{$1})";
361 :     }
362 : parrello 1.4 # This is going to be a multi-line trace message. We start with the field name and type.
363 : parrello 1.6 my @lines = ("Values for $table($name), type $type:\n");
364 : parrello 1.4 # Loop through the records. We generate one line of data per record.
365 :     for (my $j = 0; $j < $found; $j++) {
366 :     # Get the field value.
367 :     my $field = $records[$j]->[$i];
368 :     # Compute the record label.
369 :     my $line = "Record $j";
370 :     # Check for unusual cases.
371 : parrello 1.6 if (! defined $field || $field eq '') {
372 : parrello 1.4 $line .= "= <empty>";
373 :     } else {
374 :     # Make sure we don't trace something ungodly.
375 :     my $excess = (length $field) - 40;
376 :     if ($excess > 0) {
377 :     $field = substr($field, 0, 40) . " >> + $excess characters";
378 :     }
379 :     $line .= ": $field";
380 :     }
381 :     # Save this line. We indent a little for readability.
382 :     push @lines, " $line";
383 :     }
384 :     # Trace this field.
385 :     Trace(join("\n", @lines)) if T(2);
386 :     }
387 :     }
388 :     }
389 :    
390 :    
391 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3