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

Annotation of /Sprout/AttrDBRefresh.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     =head1 AttrDBRefresh
4 :    
5 : parrello 1.5 This script performs useful function on the custom attributes database.
6 : parrello 1.1
7 :     The currently-supported command-line options are as follows.
8 :    
9 :     =over 4
10 :    
11 :     =item user
12 :    
13 :     Name suffix to be used for log files. If omitted, the PID is used.
14 :    
15 :     =item trace
16 :    
17 :     Numeric trace level. A higher trace level causes more messages to appear. The
18 : parrello 1.2 default trace level is C<3>.
19 : parrello 1.1
20 :     =item sql
21 :    
22 :     If specified, turns on tracing of SQL activity.
23 :    
24 :     =item background
25 :    
26 :     Save the standard and error output to files. The files will be created
27 :     in the FIG temporary directory and will be named C<err>I<User>C<.log> and
28 :     C<out>I<User>C<.log>, respectively, where I<User> is the value of the
29 :     B<user> option above.
30 :    
31 :     =item h
32 :    
33 :     Display this command's parameters and options.
34 :    
35 :     =item phone
36 :    
37 :     Phone number to message when the script is complete.
38 :    
39 : parrello 1.2 =item migrate
40 :    
41 : parrello 1.10 If specified, a comma-delimited list of attributes to be migrated from the old system to the
42 :     new one. The attributes will be erased before migration.
43 : parrello 1.2
44 : parrello 1.4 =item initializeAndClear
45 : parrello 1.2
46 : parrello 1.4 If specified, then the tables in the attribute database are dropped and re-created.
47 : parrello 1.2
48 : parrello 1.12 =item appendLoad
49 :    
50 :     If specified, existing keys will not be erased before loading the attribute
51 :     data. This option only makes sense if C<load> is specified.
52 :    
53 : parrello 1.5 =item load
54 :    
55 :     If specified, the name of a file containing attribute data to be loaded into the
56 :     system. The file is presumed to be tab-delimited. The first column must be the
57 :     object ID, the second the attribute key name, and the remaining columns the
58 :     attribute values. All attribute keys mentioned in the file will be erased before
59 : parrello 1.12 loading unless the C<appendLoad> option is specified.
60 : parrello 1.5
61 : parrello 1.7 =item loadKey
62 :    
63 :     If specified, the name of a tab-delimited file containing attribute key data. For each key,
64 :     there is a pair of lines. The first line contains the ID, data type, and description
65 :     of the key. The second line contains the marker C<#GROUPS> followed by zero or more
66 :     group names. The attribute will be connected to all the specified groups.
67 :    
68 : parrello 1.5 =item backup
69 :    
70 :     If specified, the name of a file into which all the attribute data should be
71 : parrello 1.7 dumped. The file itself will receive the attribute data in the format expected
72 :     by C<load>. A second file, consisting of the same file name with the suffix C<.key>,
73 :     will contain the attribute key data in the format expected by C<loadKey>.
74 : parrello 1.5
75 : parrello 1.6 =item hh_copy
76 :    
77 :     If specified, the attributes used by the collections system will be converted from the
78 :     old system to the new one. In the old system, these attributes are assigned either to
79 :     a genome or to the word "Subsystem". If they are attached to the word "Subsystem", then
80 :     the value is the subsystem name; otherwise, the value is ignored. In the new attribute
81 :     system, the attribute key is always "collection" and the value is the collection name.
82 :     Rather than attaching attributes to the word "Subsystem", we attach them to the
83 :     subsystem itself.
84 :    
85 : parrello 1.10 =item compare
86 :    
87 :     If specified, the name of a file to contain a comparision report. The comparison report
88 :     contains a table of the attribute keys and the number of values of the key in the new
89 :     and old systems, along with a list of the attributes and values not in the new system.
90 :     The report is formatted as a web page.
91 :    
92 :     =item summary
93 :    
94 :     If specified, the name of a file to contain a summary report. The summary report
95 :     contains a table of the attribute keys and the number of values of each. The report
96 :     is formatted as a web page.
97 :    
98 :     =item trimSpaces
99 :    
100 :     If specified, the name of an attribute value backup file. The file will be processed to
101 :     remove excess spaces. The fixed file will have the same name as the incoming backup
102 :     file with the extension <.fixed>. This new file can then be reloaded using the
103 :     C<load> option.
104 :    
105 : parrello 1.13 =item showKeyDef
106 :    
107 :     If specified, the name of an attribute key. The key's descriptive data will be displayed.
108 :    
109 : parrello 1.1 =back
110 :    
111 :     =cut
112 :    
113 :     use strict;
114 :     use Tracer;
115 :     use DocUtils;
116 :     use TestUtils;
117 :     use Cwd;
118 :     use File::Copy;
119 :     use File::Path;
120 :     use CustomAttributes;
121 : parrello 1.5 use ERDBLoad;
122 : parrello 1.2 use FIG;
123 : parrello 1.10 use CGI;
124 : parrello 1.1
125 :     # Get the command-line options and parameters.
126 : parrello 1.5 my ($options, @parameters) = StandardSetup([qw(CustomAttributes FIG) ],
127 : parrello 1.1 {
128 : parrello 1.2 trace => [3, "trace level"],
129 : parrello 1.4 initializeAndClear => ["", "if specified, the tables of the attribute database will be re-created"],
130 : parrello 1.2 migrate => ["", "if specified, attribute data will be migrated along with the object IDs"],
131 : parrello 1.1 phone => ["", "phone number (international format) to call when load finishes"],
132 : parrello 1.5 load => ["", "file from which to load attribute data"],
133 : parrello 1.7 loadKey => ["", "file from which to load attribute key data"],
134 : parrello 1.6 backup => ["", "file to which attribute data should be dumped"],
135 : parrello 1.8 hh_copy => ["", "if specified, the attributes used by the collections system will be converted"],
136 : parrello 1.10 compare => ["", "name of a file into which a comparison report will be written"],
137 :     summary => ["", "name of a file into which a summary report will be written"],
138 :     trimSpaces => ["", "if specified, the name of a backup file, which will be processed to remove excess spaces"],
139 : parrello 1.12 appendLoad => ["", "if specified, no data will be erased before loading from the load file"],
140 : parrello 1.13 showKeyDef => ["", "if specified, the name of a key whose descriptive data is to be displayed"],
141 : parrello 1.1 },
142 :     "",
143 :     @ARGV);
144 :     # Set a variable to contain return type information.
145 :     my $rtype;
146 : parrello 1.10 # Create a CGI object.
147 :     my $cgi = CGI->new();
148 : parrello 1.1 # Insure we catch errors.
149 :     eval {
150 : parrello 1.3 # Insure we don't use the new attribute system for accessing the old attributes.
151 :     $FIG_Config::attrOld = 1;
152 : parrello 1.2 # Get the FIG object.
153 :     my $fig = FIG->new();
154 : parrello 1.4 # Get the attribute database.
155 :     Trace("Connecting to attribute database.") if T(2);
156 :     my $ca = CustomAttributes->new();
157 :     # Process according to the options selected.
158 : parrello 1.7 if ($options->{backup}) {
159 :     # Back up the attributes to the specified file.
160 :     my $backupFileName = $options->{backup};
161 :     Trace("Backing up attribute data.") if T(2);
162 :     my $stats = $ca->BackupAllAttributes($backupFileName);
163 :     Trace("Attribute backup statistics:\n" . $stats->Show()) if T(2);
164 :     Trace("Backing up key data.") if T(2);
165 :     $stats = $ca->BackupKeys("$backupFileName.key");
166 :     Trace("Key backup statistics:\n" . $stats->Show()) if T(2);
167 : parrello 1.5 }
168 : parrello 1.4 if ($options->{initializeAndClear}) {
169 :     # Create the tables.
170 :     $ca->CreateTables();
171 :     Trace("Tables recreated.") if T(2);
172 : parrello 1.2 }
173 :     if ($options->{migrate}) {
174 : parrello 1.10 # Migrate data from the old system.
175 :     Trace("Migrating attributes: $options->{migrate}.") if T(2);
176 :     # Create a statistics object to track our progress.
177 :     my $stats = Stats->new('keys', 'values');
178 :     # Loop through the attributes specified.
179 :     my @keys = split /\s*,\s*/, $options->{migrate};
180 :     for my $key (@keys) {
181 :     # Look for the key in the new system.
182 :     if (! $ca->Exists('AttributeKey', $key)) {
183 :     # If it's not found, we have an error.
184 :     Trace("Attribute key $key not found in new system.") if T(0);
185 :     $stats->AddMessage("$key not found.");
186 :     $stats->Add(badKeys => 1);
187 :     } else {
188 :     # Denote we're processing another key.
189 :     $stats->Add(keys => 1);
190 :     # Get the values from the old system.
191 :     Trace("Retrieving values for key $key.") if T(3);
192 :     my @valueList = $fig->get_attributes(undef, $key);
193 :     Trace("Erasing existing values for key $key.") if T(3);
194 :     # Erase the values in the new attribute system.
195 :     $ca->EraseAttribute($key);
196 :     Trace("Moving values for $key into new system.") if T(3);
197 :     # Loop through the old system values, adding them to the new system.
198 :     for my $valueRow (@valueList) {
199 :     my ($id, $key, @values) = @{$valueRow};
200 :     $ca->AddAttribute($id, $key, @values);
201 :     }
202 :     # Record what we did.
203 :     my $count = scalar(@valueList);
204 :     Trace("$count values migrated for $key.") if T(3);
205 :     $stats->Add(values => $count);
206 :     }
207 :     }
208 : parrello 1.5 Trace("Migration statistics:\n" . $stats->Show()) if T(2);
209 :     }
210 : parrello 1.6 if ($options->{hh_copy}) {
211 :     # Migrate the collections keys to the new system.
212 :     Trace("Migrating collections data.") if T(2);
213 :     my $stats = MigrateCollections($ca, $fig);
214 :     Trace("Collection statistics:\n" . $stats->Show()) if T(2);
215 :     }
216 : parrello 1.10 if ($options->{trimSpaces}) {
217 :     # Here we need to remove unnecessary spaces from an attribute values backup
218 :     # file. First, we open the input backup file.
219 :     my $fileName = $options->{trimSpaces};
220 :     my $ih = Open(undef, "<$fileName");
221 :     # Now we open the proposed output file.
222 :     my $oh = Open(undef, ">$fileName.fixed");
223 : parrello 1.11 # Create a statistics object to track our progress.
224 :     my $stats = Stats->new('lines', 'trims');
225 : parrello 1.10 Trace("Cleaning $fileName and copying to $fileName.fixed.") if T(2);
226 :     # Loop through the input file.
227 :     while (! eof $ih) {
228 :     # Get the next record in the input file.
229 : parrello 1.11 $stats->Add(lines => 1);
230 : parrello 1.10 my ($id, $key, @values) = Tracer::GetLine($ih);
231 :     # Trim the values.
232 : parrello 1.11 for my $value (@values) {
233 :     if ($value =~ /(\S.+\S)\s+/) {
234 :     $value = $1;
235 :     $stats->Add(trims => 1);
236 :     }
237 :     }
238 : parrello 1.10 # Write the result to the output file.
239 :     Tracer::PutLine($oh, [$id, $key, @values]);
240 :     }
241 :     # Close the files.
242 :     close $ih;
243 :     close $oh;
244 : parrello 1.11 Trace("$fileName.fixed is now a cleaned backup.\n" . $stats->Show()) if T(2);
245 : parrello 1.10 }
246 : parrello 1.7 if ($options->{loadKey}) {
247 :     # We want to load the attribute data from the specified file, but
248 :     # first we need to verify that the file exists.
249 :     my $loadFileName = $options->{loadKey};
250 :     if (! -f $loadFileName) {
251 :     Confess("Cannot load keys: file \"$loadFileName\" is not found or not a file.");
252 :     } else {
253 :     Trace("Loading key data from $loadFileName.") if T(2);
254 :     my $stats = $ca->RestoreKeys($loadFileName);
255 :     Trace("Load statistics:\n" . $stats->Show()) if T(2);
256 :     }
257 :     }
258 : parrello 1.13 if ($options->{showKeyDef}) {
259 :     # We want to display the identified key's description. Get the key name.
260 :     my $name = $options->{showKeyDef};
261 :     # Look for keys with the same name.
262 :     my %keys = $ca->GetAttributeData('find', $options->{showKeyDef});
263 :     # See if we found the key.
264 :     if (! $keys{$name}) {
265 :     print "Key $name not found.\n";
266 :     } else {
267 :     print "Description for $name.\n\n";
268 :     print $keys{$name}->[1];
269 :     print "\n\n";
270 :     }
271 :     }
272 : parrello 1.5 if ($options->{load}) {
273 :     # We want to load the attribute data from the specified file, but
274 :     # first we need to verify that the file exists.
275 :     my $loadFileName = $options->{load};
276 :     if (! -f $loadFileName) {
277 :     Confess("Cannot load: file \"$loadFileName\" is not found or not a file.");
278 :     } else {
279 : parrello 1.12 # Set up options. We may need to specify the append option.
280 :     my %loadOptions;
281 :     if ($options->{appendLoad}) {
282 :     $loadOptions{append} = 1;
283 :     }
284 :     # Now we can load.
285 : parrello 1.5 Trace("Loading attribute data from $loadFileName.") if T(2);
286 : parrello 1.12 my $stats = $ca->LoadAttributesFrom($loadFileName, %loadOptions);
287 : parrello 1.5 Trace("Load statistics:\n" . $stats->Show()) if T(2);
288 :     }
289 :     }
290 : parrello 1.10 if ($options->{summary}) {
291 :     # Here we have a summary report. The value of the option is the name of a file that is to
292 :     # contain an html-formatted report. We start by getting a complete list of the keys and
293 :     # the associated counts.
294 :     my $keyCounts = GetAllKeys($ca);
295 :     # Buffer the lines in the following list.
296 :     my @lines = ();
297 :     # Start the table.
298 :     push @lines, $cgi->start_table({border => 2});
299 :     push @lines, $cgi->Tr($cgi->th({align => 'left'}, 'Key Name'), $cgi->th({align => 'right'}, 'Values'));
300 :     # Loop through the key hash, building row data.
301 :     for my $key (sort keys %{$keyCounts}) {
302 :     push @lines, $cgi->Tr($cgi->td({align => 'left'}, $key), $cgi->td({align => 'right'}, $keyCounts->{$key}));
303 :     }
304 :     # Close off the table.
305 :     push @lines, $cgi->end_table();
306 :     # Output the page.
307 :     WritePage($options->{summary}, 'Attribute Key Summary', \@lines);
308 :     }
309 :     if ($options->{compare}) {
310 :     # Here we have a comparison report. The value of the option is the name of a file that is
311 :     # to contain an html-formatted report. We need access to the SEED database to pull this
312 :     # off.
313 :     my $dbh = $fig->db_handle();
314 :     Trace("Retrieving key data.") if T(2);
315 :     # Get counts for all the keys in the old system.
316 :     my $oldKeys = $dbh->SQL("SELECT tag, COUNT(*) FROM attribute GROUP BY tag");
317 :     # Insure the query worked.
318 :     if (! defined($oldKeys)) {
319 :     Trace("Database error retrieving old system keys:" . $dbh->errstr) if T(0);
320 :     } else {
321 :     # Convert the key data into a hash.
322 :     my %oldKeyHash = map { $_->[0] => $_->[1] } @{$oldKeys};
323 :     # Get the counts for all the keys in the new system.
324 :     my $newKeyHashRef = GetNewKeyCounts($ca);
325 :     # We've got our data, so the next step is to start accumulating the lines of the web page.
326 :     Trace("Processing key table for new system.") if T(2);
327 :     my @lines = ();
328 :     # Start the first table.
329 :     push @lines, $cgi->h3("New System Keys");
330 :     push @lines, $cgi->start_table({border => 2});
331 :     push @lines, $cgi->Tr($cgi->th({align => 'left'}, 'Key Name'), $cgi->th({align => 'right'}, 'New Values'),
332 :     $cgi->th({align => 'right'}, 'Old Values'));
333 :     # Now we process the new keys. As we run through them, we'll delete matching keys from
334 :     # the old key hash. The remaining keys will be output as a missing-keys table.
335 :     for my $key (sort keys %{$newKeyHashRef}) {
336 :     # Get the new system count.
337 :     my $newCount = $newKeyHashRef->{$key};
338 :     # Default to a blank in the old system count column.
339 :     my $oldCount = '&nbsp;';
340 :     # Check to see if this key exists in the old system.
341 :     if (exists $oldKeyHash{$key}) {
342 :     # If it does, save its count and delete it from the old system hash.
343 :     $oldCount = $oldKeyHash{$key};
344 :     delete $oldKeyHash{$key};
345 :     }
346 :     # Output this table row.
347 :     push @lines, $cgi->Tr($cgi->td({align => 'left'}, $key), $cgi->td({align => 'right'}, $newCount),
348 :     $cgi->td({align => 'right'}, $oldCount));
349 :     }
350 :     # Close the table.
351 :     push @lines, $cgi->end_table();
352 :     # Now the remaining keys in the old key hash are missing from the new system. We create a new table
353 :     # to display them.
354 :     my @missingKeys = sort keys %oldKeyHash;
355 :     # Only do this if there's at least one missing key.
356 :     if (@missingKeys == 0) {
357 :     push @lines, $cgi->p("No missing keys found.");
358 :     } else {
359 :     Trace("Processing missing key table.") if T(2);
360 :     # Start the second table.
361 :     push @lines, $cgi->h3("Keys Missing from New System");
362 :     push @lines, $cgi->start_table({border => 2});
363 :     push @lines, $cgi->Tr($cgi->th({align => 'left'}, 'Key Name'), $cgi->td({align => 'right'}, 'Values'));
364 :     # Loop through the missing keys, writing them to the table.
365 :     for my $key (@missingKeys) {
366 :     push @lines, $cgi->Tr($cgi->td({align => 'left'}, $key), $cgi->td({align => 'right'}, $oldKeyHash{$key}));
367 :     }
368 :     # Close the table.
369 :     push @lines, $cgi->end_table();
370 :     }
371 :     # Write the web page.
372 :     WritePage($options->{compare}, 'Attribute System Comparison', \@lines);
373 :     }
374 :     }
375 : parrello 1.2 Trace("Processing complete.") if T(2);
376 : parrello 1.1 };
377 :     if ($@) {
378 :     Trace("Script failed with error: $@") if T(0);
379 :     $rtype = "error";
380 :     } else {
381 :     Trace("Script complete.") if T(2);
382 :     $rtype = "no error";
383 :     }
384 :     if ($options->{phone}) {
385 :     my $msgID = Tracer::SendSMS($options->{phone}, "RefreshAttrDB terminated with $rtype.");
386 :     if ($msgID) {
387 :     Trace("Phone message sent with ID $msgID.") if T(2);
388 :     } else {
389 :     Trace("Phone message not sent.") if T(2);
390 :     }
391 :     }
392 :    
393 : parrello 1.4 =head3 MigrateAttributes
394 :    
395 : parrello 1.6 C<< my $stats = MigrateAttributes($ca, $fig, $preserve); >>
396 : parrello 1.4
397 :     Migrate all the attributes data from the specified FIG instance. This is a long, slow
398 :     method used to convert the old attribute data to the new system. Only attribute
399 :     keys that are already in the database will be loaded, and they will completely
400 :     replace the existing values for those keys. Therefore, it is very important that the
401 :     FIG instance not be connected to the attribute database.
402 :    
403 :     =over 4
404 :    
405 :     =item ca
406 :    
407 :     B<CustomAttributes> object used to access the attribute database.
408 :    
409 :     =item fig
410 :    
411 :     A FIG object that can be used to retrieve attributes for migration purposes.
412 :    
413 : parrello 1.6 =item preserve (optional)
414 :    
415 :     A comma-delimited list of attributes that are not to be migrated.
416 :    
417 : parrello 1.4 =item RETURN
418 :    
419 :     Returns a statistical object for the load process.
420 :    
421 :     =back
422 :    
423 :     =cut
424 :    
425 :     sub MigrateAttributes {
426 :     # Get the parameters.
427 : parrello 1.6 my ($ca, $fig, $preserve) = @_;
428 : parrello 1.4 # Create the return value.
429 : parrello 1.5 my $retVal = Stats->new('keysIn');
430 :     # Create a loader for the value table.
431 :     my $hasValueFor = ERDBLoad->new($ca, 'HasValueFor', $FIG_Config::temp);
432 :     # Create a hash for the target objects.
433 :     my %targetObjectHash = ();
434 : parrello 1.6 # Get a list of the attributes we're to preserve.
435 :     my %preserve = ();
436 :     if (defined $preserve) {
437 :     %preserve = map { $_ => 1 } split /\s*,\s*/, $preserve;
438 :     }
439 :     # Put the preserved keys into the load file.
440 :     for my $key (keys %preserve) {
441 :     Trace("Preserving key $key.") if T(3);
442 :     my @newValues = $ca->GetAttributes(undef, $key);
443 :     Trace(scalar(@newValues) . " values of $key will be preserved.");
444 :     # Put the values into the load file.
445 :     PutValue($hasValueFor, $ca, @newValues);
446 :     }
447 : parrello 1.4 # Get a list of all our attribute keys.
448 : parrello 1.6 my @allKeys = $ca->GetFlat(['AttributeKey'], "", [], 'AttributeKey(id)');
449 :     # Delete the preserved keys.
450 :     my @keys = grep { ! $preserve{$_} } @allKeys;
451 :     # Loop through the reset, building the load files.
452 : parrello 1.4 for my $key (@keys) {
453 :     Trace("Migrating key $key.") if T(3);
454 :     $retVal->Add(keysIn => 1);
455 :     # Get all the values of the specified key.
456 :     my @oldValues = $fig->get_attributes(undef, $key);
457 :     my $count = scalar(@oldValues);
458 :     Trace("$count values found for $key in source system.") if T(3);
459 : parrello 1.6 # Put the values into the load file.
460 :     PutValue($hasValueFor, $ca, @oldValues);
461 : parrello 1.4 }
462 : parrello 1.5 # Close and finish the loads to upload the data.
463 :     Trace("Closing value table.") if T(2);
464 :     my $hvfStats = $hasValueFor->FinishAndLoad();
465 :     Trace("Statistics from value table load:\n" . $hvfStats->Show()) if T(2);
466 :     # Merge the statistics.
467 :     $retVal->Accumulate($hvfStats);
468 : parrello 1.4 # Return the statistics object.
469 :     return $retVal;
470 :     }
471 :    
472 : parrello 1.6 =head3 PutValue
473 :    
474 :     C<< PutValue($hasValueFor, $ca, @values); >>
475 :    
476 :     Put the values from an attribute value list into a HasValueFor load file.
477 :    
478 :     =over 4
479 :    
480 :     =item hasValueFor
481 :    
482 :     Load object for the HasValueFor table.
483 :    
484 :     =item ca
485 :    
486 :     A CustomAttribute object. We get the splitter value from it.
487 :    
488 :     =item value
489 :    
490 :     A list of tuples, each consisting of an object ID, a key name, and one or more values.
491 :    
492 :     =back
493 :    
494 :     =cut
495 :    
496 :     sub PutValue {
497 :     # Get the parameters.
498 :     my ($hasValueFor, $ca, @values) = @_;
499 :     # Loop through the value rows.
500 :     for my $row (@values) {
501 :     # Get this row's data.
502 :     my ($id, $key, @values) = @{$row};
503 :     # Format the values.
504 :     my $valueString = join($ca->{splitter}, @values);
505 :     # Add the value.
506 :     $hasValueFor->Put($key, $id, $valueString);
507 :     }
508 :     }
509 :    
510 :     =head3 MigrateCollections
511 :    
512 :     C<< my $stats = MigrateCollections($ca, $fig); >>
513 :    
514 :     This method copies the collection data from the specified FIG object and stores it as values
515 :     of the C<collection> attribute in the specified custom attribute database.
516 :    
517 :     =over 4
518 :    
519 :     =item ca
520 :    
521 :     Custom attribute database into which the collections are to be stored.
522 :    
523 :     =item fig
524 :    
525 :     FIG object from which the collection attributes are to be harvested.
526 :    
527 :     =item RETURN
528 :    
529 :     Returns a statistics object with informatino about the migration.
530 :    
531 :     =back
532 :    
533 :     =cut
534 :    
535 :     sub MigrateCollections {
536 :     # Get the parameters.
537 :     my ($ca, $fig) = @_;
538 :     # Declare the return variable.
539 :     my $retVal = Stats->new();
540 :     # Get the collection names.
541 :     my @collections = qw(higher_plants eukaryotic_ps nonoxygenic_ps hundred_hundred functional_coupling_paper ecoli_essentiality_paper);
542 :     # Erase the current collection date.
543 :     $ca->EraseAttribute('collection');
544 :     # Loop through the collection attributes.
545 :     for my $cname (@collections) {
546 :     $retVal->Add(collection => 1);
547 :     # Get this attribute from the old system.
548 :     my @rows = $fig->get_attributes(undef, $cname);
549 :     # Loop through its values.
550 :     for my $row (@rows) {
551 :     $retVal->Add($cname => 1);
552 :     # Determine the object key.
553 :     my $objectID = ($row->[0] eq 'Subsystem' ? $row->[2] : $row->[0]);
554 :     $ca->AddAttribute($objectID, 'collection', $cname);
555 :     }
556 :     }
557 :     # Return the statistics.
558 :     return $retVal;
559 :     }
560 :    
561 : parrello 1.10 =head3 GetAllKeys
562 :    
563 :     C<< my @keys = GetAllKeys($ca); >>
564 :    
565 :     Return a sorted list of the attribute keys.
566 :    
567 :     =over 4
568 :    
569 :     =item ca
570 :    
571 :     CustomAttributes object used to access the database.
572 :    
573 :     =item RETURN
574 :    
575 :     Returns a sorted list of all the attribute keys.
576 :    
577 :     =back
578 :    
579 :     =cut
580 :    
581 :     sub GetAllKeys {
582 :     # Get the parameters.
583 :     my ($ca) = @_;
584 :     # Get the attribute data.
585 :     my %keyData = $ca->GetAttributeData('name', '');
586 :     # Sort the keys.
587 :     my @retVal = sort keys %keyData;
588 :     # Return the result.
589 :     return @retVal;
590 :     }
591 :    
592 :     =head3 OpenPage
593 :    
594 :     C<< my $fh = OpenPage($fileName, $title); >>
595 :    
596 :     Start writing an HTML page to a file and return the file handle.
597 :    
598 :     =over 4
599 :    
600 :     =item fileName
601 :    
602 :     Name of the file to which the page will be written.
603 :    
604 :     =item title
605 :    
606 :     Title for the page.
607 :    
608 :     =item RETURN
609 :    
610 :     Returns the file handle for writing the rest of the page.
611 :    
612 :     =back
613 :    
614 :     =cut
615 :    
616 :     sub OpenPage {
617 :     # Get the parameters.
618 :     my ($fileName, $title) = @_;
619 :     # Open the file.
620 :     my $retVal = Open(undef, ">$fileName");
621 :     # Write out the HTML headers.
622 :     print $retVal $cgi->start_html(-title => $title, -BGCOLOR => 'silver');
623 :     print $retVal "\n";
624 :     # Return the file handle.
625 :     return $retVal;
626 :     }
627 :    
628 :     =head3 GetNewKeyCounts
629 :    
630 :     C<< my %counts = GetNewKeyCounts($ca); >>
631 :    
632 :     Return a hash mapping attribute key names to counts.
633 :    
634 :     =over 4
635 :    
636 :     =item ca
637 :    
638 :     CustomAttributes object for accessing the attribute database.
639 :    
640 :     =item RETURN
641 :    
642 :     Returns a reference to a hash mapping each key name to a count of the key's values.
643 :    
644 :     =back
645 :    
646 :     =cut
647 :    
648 :     sub GetNewKeyCounts {
649 :     # Get the parameters.
650 :     my ($ca) = @_;
651 :     # Declare the return variable.
652 :     my $retVal = {};
653 :     # Get all of the keys.
654 :     my @keys = GetAllKeys($ca);
655 :     # Loop through the list, filling the hash.
656 :     for my $key (@keys) {
657 :     my $count = $ca->GetCount(['HasValueFor'], "HasValueFor(from-link) = ?", [$key]);
658 :     $retVal->{$key} = $count;
659 :     }
660 :     # Return the result.
661 :     return $retVal;
662 :     }
663 :    
664 :     =head3 WritePage
665 :    
666 :     C<< WritePage($fileName, $title, \@lines); >>
667 :    
668 :     Write the specified lines to the specified file as a web page. The lines are assumed to be raw
669 :     HTML body text. They will be preceded by a standard HTML header and followed by a standard
670 :     HTML footer.
671 :    
672 :     =over 4
673 :    
674 :     =item fileName
675 :    
676 :     Name of the output file.
677 :    
678 :     =item title
679 :    
680 :     Title for the web page.
681 :    
682 :     =item lines
683 :    
684 :     Reference to a list of lines of HTML.
685 :    
686 :     =back
687 :    
688 :     =cut
689 :    
690 :     sub WritePage {
691 :     # Get the parameters.
692 :     my ($fileName, $title, $lines) = @_;
693 :     # Open the file and write the header to it. The header includes everything up to and including
694 :     # the BODY tag.
695 :     Trace("Writing web page to $fileName.") if T(2);
696 :     my $oh = OpenPage($fileName, $title);
697 :     # Write the lines one at a time.
698 :     for my $line (@{$lines}) {
699 :     print $oh "$line\n";
700 :     }
701 :     # Write the HTML footer.
702 :     print $oh $cgi->end_html();
703 :     # Close the output file.
704 :     close $oh;
705 :     Trace("Web page created in $fileName.") if T(2);
706 :     }
707 :    
708 : parrello 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3