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

Annotation of /Sprout/ERDBLoader.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (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 :    
61 :     =back
62 :    
63 :     =head2 Command-Line Options
64 :    
65 :     =over 4
66 :    
67 :     =item trace
68 :    
69 :     Specifies the tracing level. The higher the tracing level, the more messages
70 :     will appear in the trace log. Use E to specify emergency tracing.
71 :    
72 :     =item user
73 :    
74 :     Name suffix to be used for log files. If omitted, the PID is used.
75 :    
76 :     =item sql
77 :    
78 :     If specified, turns on tracing of SQL activity.
79 :    
80 :     =item background
81 :    
82 :     Save the standard and error output to files. The files will be created
83 :     in the FIG temporary directory and will be named C<err>I<User>C<.log> and
84 :     C<out>I<User>C<.log>, respectively, where I<User> is the value of the
85 :     B<user> option above.
86 :    
87 :     =item help
88 :    
89 :     Display this command's parameters and options.
90 :    
91 : parrello 1.3 =item keepSections
92 :    
93 :     If specified, section files (the fragments of data load files created by
94 :     [[ERDBGeneratorPl]], will not be deleted after they are collated.
95 :    
96 : parrello 1.4 =item sanityCheck
97 :    
98 :     If specified, no tables will be loaded. Instead, the first I<N> records from the
99 :     assembled load files will be displayed so that the file contents can be
100 :     visually matched against the column names.
101 :    
102 : parrello 1.1 =item warn
103 :    
104 :     Create an event in the RSS feed when an error occurs.
105 :    
106 :     =item phone
107 :    
108 :     Phone number to message when the script is complete.
109 :    
110 :     =back
111 :    
112 :     =cut
113 :    
114 :     # Get the command-line options and parameters.
115 :     my ($options, @parameters) = StandardSetup([qw(ERDBLoadGroup ERDB Stats) ],
116 :     {
117 : parrello 1.4 sanityCheck => ["", "don't load, trace contents of first N load file records instead"],
118 : parrello 1.1 trace => ["", "tracing level"],
119 : parrello 1.3 keepSections => ["", "if specified, section files will not be deleted after being collated"],
120 : parrello 1.1 phone => ["", "phone number (international format) to call when load finishes"]
121 :     },
122 :     "<database> <group1> <group2> ...",
123 :     @ARGV);
124 :     # Set a variable to contain return type information.
125 :     my $rtype;
126 :     # Insure we catch errors.
127 :     eval {
128 :     # Get the parameters.
129 :     my ($database, @groups) = @parameters;
130 :     # Connect to the database.
131 :     my $erdb = ERDB::GetDatabase($database);
132 :     # Fix the group list.
133 : parrello 1.3 my @realGroups = ERDBLoadGroup::ComputeGroups($erdb, \@groups);
134 : parrello 1.1 # Get the source object and load directory for this database.
135 :     my $source = $erdb->GetSourceObject();
136 :     my $directory = $erdb->LoadDirectory();
137 :     # Get the list of sections.
138 :     my @sectionList = $erdb->SectionList($source);
139 :     # Create a statistics object to track our progress.
140 :     my $stats = Stats->new();
141 : parrello 1.4 # Find out if we're doing a sanity check.
142 :     my $sanityCheck = $options->{sanityCheck} || "";
143 : parrello 1.2 # Start a timer.
144 :     my $totalStart = time();
145 : parrello 1.1 # Loop through the groups.
146 :     for my $group (@realGroups) {
147 :     # Get the list of tables for this group.
148 : parrello 1.3 my @tableList = ERDBLoadGroup::GetTables($erdb, $group);
149 : parrello 1.2 # We need to insure there is a data file for every table. If we fail to find one,
150 :     # we set the following error flag, which prevents us from loading the database.
151 :     my $missingTable = 0;
152 :     # Loop through the tables in this group.
153 : parrello 1.3 for my $table (@tableList) {
154 : parrello 1.4 Trace("Processing table $table for assembly.") if T(2);
155 :     # Get the section file names.
156 :     my @sectionFiles =
157 :     map { ERDBGenerate::CreateFileName($table, $_, 'data', $directory) } @sectionList;
158 : parrello 1.2 # Get the data file name.
159 :     my $dataFile = ERDBGenerate::CreateFileName($table, undef, 'data', $directory);
160 :     # Do we have it?
161 :     if (-f $dataFile) {
162 :     # Yes. This is good news.
163 :     $stats->Add('tables-found' => 1);
164 : parrello 1.4 Trace("Table file found for $table.") if T(3);
165 : parrello 1.2 } else {
166 :     # No, we must build it. Verify that we have all the sections.
167 :     my @missingFiles = grep { ! -f $_ } @sectionFiles;
168 :     # Tell the user about all the missing files.
169 :     for my $missingFile (@missingFiles) {
170 :     $stats->Add('sections-missing' => 1);
171 :     $stats->AddMessage("Data file $missingFile not found for table $table.");
172 :     }
173 :     # Did we find everything?
174 :     if (scalar @missingFiles) {
175 :     # No! Denote that we have a missing table.
176 : parrello 1.3 $missingTable++;
177 : parrello 1.2 $stats->Add('tables-skipped' => 1);
178 :     } else {
179 :     # Yes! Try to assemble the sections into a data file.
180 :     my $sortStart = time();
181 : parrello 1.4 my $sortCommand = $erdb->SortNeeded($table) . " >$dataFile";
182 :     Trace("Sort command: $sortCommand") if T(3);
183 :     my $oh = Open(undef, "| $sortCommand");
184 : parrello 1.2 for my $sectionFile (@sectionFiles) {
185 : parrello 1.4 Trace("Collating $sectionFile.") if T(3);
186 :     $stats->Add("$table-sections" => 1);
187 : parrello 1.2 for my $line (Tracer::GetFile($sectionFile)) {
188 :     print $oh "$line\n";
189 : parrello 1.4 $stats->Add("$table-collations" => 1);
190 : parrello 1.2 }
191 :     }
192 :     # Finish the sort step.
193 : parrello 1.4 Trace("Finishing collate for $table.") if T(3);
194 : parrello 1.2 close $oh;
195 :     $stats->Add('tables-collated' => 1);
196 : parrello 1.4 $stats->Add('collate-time' => time() - $sortStart);
197 :     }
198 :     }
199 :     # Now that we know we have a full data file, we can delete the
200 :     # section files to make room in the data directory. The user can
201 :     # turn this behavior off with the keepSections option.
202 :     if (! $options->{keepSections}) {
203 :     for my $sectionFile (@sectionFiles) {
204 :     if (-e $sectionFile) {
205 :     unlink $sectionFile;
206 :     $stats->Add('files-deleted' => 1);
207 : parrello 1.2 }
208 :     }
209 : parrello 1.4 Trace("Section files for $table deleted.") if T(3);
210 : parrello 1.2 }
211 :     }
212 :     # Were any tables missing?
213 :     if ($missingTable) {
214 :     # Yes, skip this group.
215 :     $stats->Add('groups-skipped' => 1);
216 : parrello 1.3 Trace("Skipping $group group: $missingTable missing tables.") if T(3);
217 : parrello 1.2 } else {
218 : parrello 1.4 # No! Process this group's files.
219 :     if ($sanityCheck eq "") {
220 :     Trace("Loading group $group into database.") if T(2);
221 :     } else {
222 :     Trace("Sanity check for group $group.") if T(2);
223 :     }
224 : parrello 1.2 my $loadStart = time();
225 : parrello 1.3 for my $table (@tableList) {
226 : parrello 1.2 my $fileName = ERDBGenerate::CreateFileName($table, undef, 'data', $directory);
227 : parrello 1.4 # Do we want a real load or a sanity check?
228 :     if ($sanityCheck eq "") {
229 :     # Real load.
230 :     my $newStats = $erdb->LoadTable($fileName, $table, truncate => 1, failOnError => 1);
231 :     $stats->Accumulate($newStats);
232 :     Trace("$fileName loaded into $table.") if T(3);
233 :     } elsif ($sanityCheck > 0) {
234 :     # Here we want a sanity check. Note that if the check value is 0,
235 :     # we don't bother. The user just wants to suppress the load step.
236 :     CheckLoadFile($erdb, $table, $fileName, $sanityCheck);
237 :     }
238 : parrello 1.2 }
239 :     $stats->Add("groups-loaded" => 1);
240 :     $stats->Add('load-time' => 1);
241 :     }
242 : parrello 1.1 }
243 : parrello 1.2 $stats->Add('total-time' => time() - $totalStart);
244 :     # Display the statistics from this run.
245 :     Trace("Statistics for load:\n" . $stats->Show()) if T(2);
246 : parrello 1.1 };
247 :     if ($@) {
248 :     Trace("Script failed with error: $@") if T(0);
249 :     } else {
250 :     Trace("Script complete.") if T(2);
251 :     }
252 :     if ($options->{phone}) {
253 : parrello 1.2 my $msgID = Tracer::SendSMS($options->{phone}, "ERDBLoader completed.");
254 : parrello 1.1 if ($msgID) {
255 :     Trace("Phone message sent with ID $msgID.") if T(2);
256 :     } else {
257 :     Trace("Phone message not sent.") if T(2);
258 :     }
259 :     }
260 :    
261 : parrello 1.4 =head3 CheckLoadFile
262 :    
263 :     CheckLoadFile($erdb, $table, $fileName, $count);
264 :    
265 :     Read the first few records of a load file and trace the contents at level
266 :     2. This allows the user to visually compare the load file contents with
267 :     the database definition.
268 :    
269 :     =over 4
270 :    
271 :     =item erdb
272 :    
273 :     [[ErdbPm]] object describing the database.
274 :    
275 :     =item table
276 :    
277 :     Name of the table to check.
278 :    
279 :     =item fileName
280 :    
281 :     Name of the load file to check.
282 :    
283 :     =item count
284 :    
285 :     Number of records to check.
286 :    
287 :     =back
288 :    
289 :     =cut
290 :    
291 :     sub CheckLoadFile {
292 :     # Get the parameters.
293 :     my ($erdb, $table, $fileName, $count) = @_;
294 :     # Open the file for input.
295 :     my $ih = Open(undef, "<$fileName");
296 :     # Slurp the first N records.
297 :     my @records;
298 :     while (! eof $ih && scalar(@records) < $count) {
299 :     push @records, [ Tracer::GetLine($ih) ];
300 :     }
301 :     my $found = scalar(@records);
302 :     Trace("$found records for $table found in sanity check.") if T(3);
303 :     # Do we have any data at all?
304 :     if ($found) {
305 :     # Yes. Get the table's descriptor. We use this to determine the field names.
306 :     my $relationData = $erdb->FindRelation($table);
307 :     Confess("Relation $table not found in database.") if (! defined $relationData);
308 :     my @fields = @{$relationData->{Fields}};
309 :     # Loop through the fields. We generate one message per field.
310 :     for (my $i = 0; $i <= $#fields; $i++) {
311 :     # Get this field's information.
312 :     my $fieldInfo = $fields[$i];
313 :     my $type = $fieldInfo->{type};
314 :     # This is going to be a multi-line trace message. We start with the field name and type.
315 :     my @lines = ("Values for $fieldInfo->{name}, type $type:\n");
316 :     # Loop through the records. We generate one line of data per record.
317 :     for (my $j = 0; $j < $found; $j++) {
318 :     # Get the field value.
319 :     my $field = $records[$j]->[$i];
320 :     # Compute the record label.
321 :     my $line = "Record $j";
322 :     # Check for unusual cases.
323 :     if (! defined $field) {
324 :     $line .= "= <null>";
325 :     } elsif ($field eq '') {
326 :     $line .= "= <empty>";
327 :     } else {
328 :     # Make sure we don't trace something ungodly.
329 :     my $excess = (length $field) - 40;
330 :     if ($excess > 0) {
331 :     $field = substr($field, 0, 40) . " >> + $excess characters";
332 :     }
333 :     $line .= ": $field";
334 :     }
335 :     # Save this line. We indent a little for readability.
336 :     push @lines, " $line";
337 :     }
338 :     # Trace this field.
339 :     Trace(join("\n", @lines)) if T(2);
340 :     }
341 :     }
342 :     }
343 :    
344 :    
345 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3