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

Annotation of /Sprout/ERDBLoader.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.18 - (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 : parrello 1.11 use ERDBExtras;
26 : parrello 1.1 use Stats;
27 : parrello 1.2 use Time::HiRes;
28 : parrello 1.1
29 :    
30 :     =head1 ERDBLoader Script
31 :    
32 :     ERDBLoader [options] <database> <group1> <group2> ...
33 :    
34 :     ERDB Database Load Finisher
35 :    
36 :     =head2 Introduction
37 :    
38 : parrello 1.12 This script finishes the database load process begun by L<ERDBGenerator.pl>.
39 : parrello 1.1
40 : parrello 1.12 L<ERDBGenerator.pl> divides the source data into sections, and generates a
41 : parrello 1.1 partial load file for each section of each table. To finish the load process, we
42 :     need to combine the partial files into single files and load the resulting
43 :     single files into the database tables.
44 :    
45 : parrello 1.12 Like L<ERDBGenerator.pl>, this script acts on load groups-- sets of related
46 : parrello 1.1 tables that are loaded at the same time. For each table in a named group that
47 : parrello 1.15 does not exist in the database, the script first attempts to find a complete set
48 :     of section files that it will collate into a data file. If there are no sections,
49 :     then it will look for a data file that is already collated. Once the collated
50 :     section files for a load group are all verified, they are loaded into the database.
51 : parrello 1.1
52 :     =head2 Positional Parameters
53 :    
54 :     =over 4
55 :    
56 :     =item database
57 :    
58 :     Name of the ERDB database. This should be the class name for the subclass used
59 :     to access the database.
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 : parrello 1.12 L<ERDBGenerator.pl>, will not be deleted after they are collated.
95 : parrello 1.3
96 : parrello 1.1 =item warn
97 :    
98 :     Create an event in the RSS feed when an error occurs.
99 :    
100 :     =item phone
101 :    
102 :     Phone number to message when the script is complete.
103 :    
104 : parrello 1.6 =item DBD
105 :    
106 : parrello 1.10 Fully-qualified name of the DBD file. This option allows the use of an alternate
107 : parrello 1.9 DBD during load so that access to the database by other processes is not
108 : parrello 1.6 compromised.
109 :    
110 : parrello 1.8 =item loadDirectory
111 :    
112 :     Directoty containing the load files. This option allows you to request that
113 :     load files from another version of the NMPDR be used, which is useful when
114 :     creating a new NMPDR: we can yank in the data from the previous database while
115 :     waiting for the new load files to be generated.
116 :    
117 : parrello 1.10 =item dbName
118 :    
119 :     SQL name of the target database. If not specified, the default name is used.
120 :     This option allows you to specify a backup or alternate database that can
121 :     be loaded without compromising the main database.
122 :    
123 : parrello 1.1 =back
124 :    
125 :     =cut
126 :    
127 :     # Get the command-line options and parameters.
128 :     my ($options, @parameters) = StandardSetup([qw(ERDBLoadGroup ERDB Stats) ],
129 :     {
130 : parrello 1.10 dbName => ["", "if specified, the SQL name of the target database"],
131 : parrello 1.6 trace => ["2", "tracing level"],
132 : parrello 1.3 keepSections => ["", "if specified, section files will not be deleted after being collated"],
133 : parrello 1.6 phone => ["", "phone number (international format) to call when load finishes"],
134 :     DBD => ["", "if specified, the name of a DBD file in the FIG directory"],
135 : parrello 1.8 loadDirectory => ["", "if specified, an alternate directory containing the load files"],
136 : parrello 1.1 },
137 :     "<database> <group1> <group2> ...",
138 :     @ARGV);
139 :     # Set a variable to contain return type information.
140 :     my $rtype;
141 :     # Insure we catch errors.
142 :     eval {
143 :     # Get the parameters.
144 :     my ($database, @groups) = @parameters;
145 : parrello 1.6 # Connect to the database and get its load directory.
146 : parrello 1.13 my $erdb = ERDB::GetDatabase($database, undef, %$options, externalDBD => 1);
147 : parrello 1.1 # Fix the group list.
148 : parrello 1.3 my @realGroups = ERDBLoadGroup::ComputeGroups($erdb, \@groups);
149 : parrello 1.1 # Get the source object and load directory for this database.
150 :     my $source = $erdb->GetSourceObject();
151 : parrello 1.8 my $directory = $options->{loadDirectory} || $erdb->LoadDirectory();
152 : parrello 1.1 # Get the list of sections.
153 :     my @sectionList = $erdb->SectionList($source);
154 :     # Create a statistics object to track our progress.
155 :     my $stats = Stats->new();
156 : parrello 1.11 # We make one pass to assemble all the tables in all the groups, and
157 :     # then another to do the actual loads. The groups that are ready to load
158 :     # in the second pass will go in this list.
159 :     my @goodGroups;
160 : parrello 1.2 # Start a timer.
161 :     my $totalStart = time();
162 : parrello 1.1 # Loop through the groups.
163 :     for my $group (@realGroups) {
164 :     # Get the list of tables for this group.
165 : parrello 1.3 my @tableList = ERDBLoadGroup::GetTables($erdb, $group);
166 : parrello 1.2 # We need to insure there is a data file for every table. If we fail to find one,
167 :     # we set the following error flag, which prevents us from loading the database.
168 :     my $missingTable = 0;
169 :     # Loop through the tables in this group.
170 : parrello 1.3 for my $table (@tableList) {
171 : parrello 1.4 Trace("Processing table $table for assembly.") if T(2);
172 :     # Get the section file names.
173 :     my @sectionFiles =
174 :     map { ERDBGenerate::CreateFileName($table, $_, 'data', $directory) } @sectionList;
175 : parrello 1.2 # Get the data file name.
176 :     my $dataFile = ERDBGenerate::CreateFileName($table, undef, 'data', $directory);
177 :     # Do we have it?
178 : parrello 1.15 my $haveFile = -f $dataFile;
179 :     # See if we can build it. Verify that we have all the sections.
180 :     my @missingFiles = grep { ! -f $_ } @sectionFiles;
181 :     # Did we find everything?
182 :     if (scalar(@missingFiles) && ! $haveFile) {
183 :     # No, and there's no main file! Denote that we have a missing table.
184 :     $missingTable++;
185 :     $stats->Add('tables-skipped' => 1);
186 :     # Tell the user about all the missing files.
187 :     for my $missingFile (@missingFiles) {
188 :     $stats->Add('sections-missing' => 1);
189 :     $stats->AddMessage("Data file $missingFile not found for table $table.");
190 :     }
191 :     } elsif (! scalar @missingFiles) {
192 :     # We have all the sections. Try to assemble them into a data file.
193 :     my $sortStart = time();
194 :     my $sortCommand = $erdb->SortNeeded($table) . " >$dataFile";
195 :     Trace("Sort command: $sortCommand") if T(3);
196 :     # Pipe to the sort command. Note that we turn on autoflush
197 :     # so there's no buffering.
198 :     my $oh = Open(undef, "| $sortCommand");
199 :     select $oh; $| = 1; select STDOUT;
200 :     # Loop through the sections.
201 :     for my $sectionFile (@sectionFiles) {
202 :     Trace("Collating $sectionFile.") if T(3);
203 :     $stats->Add("$table-sections" => 1);
204 :     # Loop through the section file.
205 :     my $ih = Open(undef, "<$sectionFile");
206 :     while (defined (my $line = <$ih>)) {
207 :     print $oh $line;
208 :     $stats->Add("$table-collations" => 1);
209 : parrello 1.2 }
210 : parrello 1.4 }
211 : parrello 1.15 # Finish the sort step.
212 : parrello 1.17 Trace("Finishing collate for $table.") if T(2);
213 : parrello 1.15 close $oh;
214 :     $stats->Add('tables-collated' => 1);
215 :     $stats->Add('collate-time' => time() - $sortStart);
216 : parrello 1.18 # Now that we know we have a full data file, we can delete the
217 :     # section files to make room in the data directory. The user can
218 :     # turn this behavior off with the keepSections option.
219 :     if (! $options->{keepSections}) {
220 :     for my $sectionFile (@sectionFiles) {
221 :     if (-e $sectionFile) {
222 :     unlink $sectionFile;
223 :     $stats->Add('files-deleted' => 1);
224 :     }
225 :     }
226 :     Trace("Section files for $table deleted.") if T(3);
227 :     }
228 : parrello 1.15 } else {
229 :     # We have a data file and no sections, so we use the data file.
230 :     $stats->Add('tables-found' => 1);
231 : parrello 1.4 }
232 : parrello 1.2 }
233 :     # Were any tables missing?
234 :     if ($missingTable) {
235 :     # Yes, skip this group.
236 :     $stats->Add('groups-skipped' => 1);
237 : parrello 1.6 Trace("Skipping $group group: $missingTable missing tables.") if T(2);
238 : parrello 1.2 } else {
239 : parrello 1.11 # No! File this group for processing in the second pass.
240 :     push @goodGroups, $group;
241 :     }
242 :     }
243 :     # Now we loop through the good groups, doing the actual loads.
244 :     for my $group (@goodGroups) {
245 :     # Get a group object.
246 :     my $groupData = $erdb->Loader($group);
247 :     # Do the post-processing.
248 :     my $postStats = $groupData->PostProcess();
249 :     # Determine what happened.
250 :     if (! defined $postStats) {
251 :     Trace("Post-processing not required for $group.") if T(3);
252 :     } else {
253 :     $stats->Accumulate($postStats);
254 :     $stats->Add('post-processing' => 1);
255 :     }
256 :     # Process this group's files.
257 :     Trace("Loading group $group into database.") if T(2);
258 :     # Get the list of tables.
259 :     my @tableList = $groupData->GetTables();
260 :     # Start a timer.
261 :     my $loadStart = time();
262 :     for my $table (@tableList) {
263 :     # Compute the load file name.
264 :     my $fileName = ERDBGenerate::CreateFileName($table, undef, 'data', $directory);
265 :     # Do the actual load.
266 :     my $newStats = $erdb->LoadTable($fileName, $table, truncate => 1, failOnError => 1);
267 :     $stats->Accumulate($newStats);
268 :     Trace("$fileName loaded into $table.") if T(3);
269 : parrello 1.2 }
270 : parrello 1.11 $stats->Add("groups-loaded" => 1);
271 : parrello 1.14 $stats->Add('load-time' => (time() - $loadStart));
272 : parrello 1.1 }
273 : parrello 1.13 # Save the DBD.
274 :     Trace("Saving DBD.") if T(2);
275 :     $erdb->InternalizeDBD();
276 : parrello 1.2 $stats->Add('total-time' => time() - $totalStart);
277 :     # Display the statistics from this run.
278 :     Trace("Statistics for load:\n" . $stats->Show()) if T(2);
279 : parrello 1.1 };
280 :     if ($@) {
281 :     Trace("Script failed with error: $@") if T(0);
282 :     } else {
283 :     Trace("Script complete.") if T(2);
284 :     }
285 :     if ($options->{phone}) {
286 : parrello 1.2 my $msgID = Tracer::SendSMS($options->{phone}, "ERDBLoader completed.");
287 : parrello 1.1 if ($msgID) {
288 :     Trace("Phone message sent with ID $msgID.") if T(2);
289 :     } else {
290 :     Trace("Phone message not sent.") if T(2);
291 :     }
292 :     }
293 :    
294 : parrello 1.4
295 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3