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

Annotation of /Sprout/AttrDBRefresh.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (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.14 =item mapSubkey
110 :    
111 :     Subkey mapping rule. Consists of a key name, followed by a substitution command enclosed in
112 :     slashes. For example, to remove the word C<ZINC> from the beginning of C<docking_results>
113 :     subkeys, you would code
114 :    
115 :     -mapSubkey=docking_results/^ZINC//
116 :    
117 : parrello 1.1 =back
118 :    
119 :     =cut
120 :    
121 :     use strict;
122 :     use Tracer;
123 :     use DocUtils;
124 :     use TestUtils;
125 :     use Cwd;
126 :     use File::Copy;
127 :     use File::Path;
128 :     use CustomAttributes;
129 : parrello 1.5 use ERDBLoad;
130 : parrello 1.14 use Stats;
131 : parrello 1.2 use FIG;
132 : parrello 1.10 use CGI;
133 : parrello 1.1
134 :     # Get the command-line options and parameters.
135 : parrello 1.5 my ($options, @parameters) = StandardSetup([qw(CustomAttributes FIG) ],
136 : parrello 1.1 {
137 : parrello 1.2 trace => [3, "trace level"],
138 : parrello 1.4 initializeAndClear => ["", "if specified, the tables of the attribute database will be re-created"],
139 : parrello 1.2 migrate => ["", "if specified, attribute data will be migrated along with the object IDs"],
140 : parrello 1.1 phone => ["", "phone number (international format) to call when load finishes"],
141 : parrello 1.5 load => ["", "file from which to load attribute data"],
142 : parrello 1.7 loadKey => ["", "file from which to load attribute key data"],
143 : parrello 1.6 backup => ["", "file to which attribute data should be dumped"],
144 : parrello 1.8 hh_copy => ["", "if specified, the attributes used by the collections system will be converted"],
145 : parrello 1.10 compare => ["", "name of a file into which a comparison report will be written"],
146 :     summary => ["", "name of a file into which a summary report will be written"],
147 :     trimSpaces => ["", "if specified, the name of a backup file, which will be processed to remove excess spaces"],
148 : parrello 1.12 appendLoad => ["", "if specified, no data will be erased before loading from the load file"],
149 : parrello 1.13 showKeyDef => ["", "if specified, the name of a key whose descriptive data is to be displayed"],
150 : parrello 1.14 mapSubkey => ["", "instructions for fixing subkey values"],
151 : parrello 1.1 },
152 :     "",
153 :     @ARGV);
154 :     # Set a variable to contain return type information.
155 :     my $rtype;
156 : parrello 1.10 # Create a CGI object.
157 :     my $cgi = CGI->new();
158 : parrello 1.1 # Insure we catch errors.
159 :     eval {
160 : parrello 1.3 # Insure we don't use the new attribute system for accessing the old attributes.
161 :     $FIG_Config::attrOld = 1;
162 : parrello 1.2 # Get the FIG object.
163 : parrello 1.14 Trace("Connecting to legacy attribute database via FIG object.") if T(2);
164 : parrello 1.2 my $fig = FIG->new();
165 : parrello 1.4 # Get the attribute database.
166 : parrello 1.14 Trace("Connecting to local attribute database.") if T(2);
167 : parrello 1.4 my $ca = CustomAttributes->new();
168 :     # Process according to the options selected.
169 : parrello 1.7 if ($options->{backup}) {
170 :     # Back up the attributes to the specified file.
171 :     my $backupFileName = $options->{backup};
172 :     Trace("Backing up attribute data.") if T(2);
173 :     my $stats = $ca->BackupAllAttributes($backupFileName);
174 :     Trace("Attribute backup statistics:\n" . $stats->Show()) if T(2);
175 :     Trace("Backing up key data.") if T(2);
176 :     $stats = $ca->BackupKeys("$backupFileName.key");
177 :     Trace("Key backup statistics:\n" . $stats->Show()) if T(2);
178 : parrello 1.5 }
179 : parrello 1.4 if ($options->{initializeAndClear}) {
180 :     # Create the tables.
181 :     $ca->CreateTables();
182 :     Trace("Tables recreated.") if T(2);
183 : parrello 1.2 }
184 :     if ($options->{migrate}) {
185 : parrello 1.10 # Migrate data from the old system.
186 :     Trace("Migrating attributes: $options->{migrate}.") if T(2);
187 :     # Create a statistics object to track our progress.
188 :     my $stats = Stats->new('keys', 'values');
189 :     # Loop through the attributes specified.
190 :     my @keys = split /\s*,\s*/, $options->{migrate};
191 :     for my $key (@keys) {
192 :     # Look for the key in the new system.
193 :     if (! $ca->Exists('AttributeKey', $key)) {
194 :     # If it's not found, we have an error.
195 :     Trace("Attribute key $key not found in new system.") if T(0);
196 :     $stats->AddMessage("$key not found.");
197 :     $stats->Add(badKeys => 1);
198 :     } else {
199 :     # Denote we're processing another key.
200 :     $stats->Add(keys => 1);
201 :     # Get the values from the old system.
202 :     Trace("Retrieving values for key $key.") if T(3);
203 :     my @valueList = $fig->get_attributes(undef, $key);
204 :     Trace("Erasing existing values for key $key.") if T(3);
205 :     # Erase the values in the new attribute system.
206 :     $ca->EraseAttribute($key);
207 :     Trace("Moving values for $key into new system.") if T(3);
208 :     # Loop through the old system values, adding them to the new system.
209 :     for my $valueRow (@valueList) {
210 :     my ($id, $key, @values) = @{$valueRow};
211 :     $ca->AddAttribute($id, $key, @values);
212 :     }
213 :     # Record what we did.
214 :     my $count = scalar(@valueList);
215 :     Trace("$count values migrated for $key.") if T(3);
216 :     $stats->Add(values => $count);
217 :     }
218 :     }
219 : parrello 1.5 Trace("Migration statistics:\n" . $stats->Show()) if T(2);
220 :     }
221 : parrello 1.6 if ($options->{hh_copy}) {
222 :     # Migrate the collections keys to the new system.
223 :     Trace("Migrating collections data.") if T(2);
224 :     my $stats = MigrateCollections($ca, $fig);
225 :     Trace("Collection statistics:\n" . $stats->Show()) if T(2);
226 :     }
227 : parrello 1.10 if ($options->{trimSpaces}) {
228 :     # Here we need to remove unnecessary spaces from an attribute values backup
229 :     # file. First, we open the input backup file.
230 :     my $fileName = $options->{trimSpaces};
231 :     my $ih = Open(undef, "<$fileName");
232 :     # Now we open the proposed output file.
233 :     my $oh = Open(undef, ">$fileName.fixed");
234 : parrello 1.11 # Create a statistics object to track our progress.
235 :     my $stats = Stats->new('lines', 'trims');
236 : parrello 1.10 Trace("Cleaning $fileName and copying to $fileName.fixed.") if T(2);
237 :     # Loop through the input file.
238 :     while (! eof $ih) {
239 :     # Get the next record in the input file.
240 : parrello 1.11 $stats->Add(lines => 1);
241 : parrello 1.10 my ($id, $key, @values) = Tracer::GetLine($ih);
242 :     # Trim the values.
243 : parrello 1.11 for my $value (@values) {
244 :     if ($value =~ /(\S.+\S)\s+/) {
245 :     $value = $1;
246 :     $stats->Add(trims => 1);
247 :     }
248 :     }
249 : parrello 1.10 # Write the result to the output file.
250 :     Tracer::PutLine($oh, [$id, $key, @values]);
251 :     }
252 :     # Close the files.
253 :     close $ih;
254 :     close $oh;
255 : parrello 1.11 Trace("$fileName.fixed is now a cleaned backup.\n" . $stats->Show()) if T(2);
256 : parrello 1.10 }
257 : parrello 1.7 if ($options->{loadKey}) {
258 :     # We want to load the attribute data from the specified file, but
259 :     # first we need to verify that the file exists.
260 :     my $loadFileName = $options->{loadKey};
261 :     if (! -f $loadFileName) {
262 :     Confess("Cannot load keys: file \"$loadFileName\" is not found or not a file.");
263 :     } else {
264 :     Trace("Loading key data from $loadFileName.") if T(2);
265 :     my $stats = $ca->RestoreKeys($loadFileName);
266 :     Trace("Load statistics:\n" . $stats->Show()) if T(2);
267 :     }
268 :     }
269 : parrello 1.13 if ($options->{showKeyDef}) {
270 :     # We want to display the identified key's description. Get the key name.
271 :     my $name = $options->{showKeyDef};
272 :     # Look for keys with the same name.
273 :     my %keys = $ca->GetAttributeData('find', $options->{showKeyDef});
274 :     # See if we found the key.
275 :     if (! $keys{$name}) {
276 :     print "Key $name not found.\n";
277 :     } else {
278 :     print "Description for $name.\n\n";
279 :     print $keys{$name}->[1];
280 :     print "\n\n";
281 :     }
282 :     }
283 : parrello 1.5 if ($options->{load}) {
284 :     # We want to load the attribute data from the specified file, but
285 :     # first we need to verify that the file exists.
286 :     my $loadFileName = $options->{load};
287 :     if (! -f $loadFileName) {
288 :     Confess("Cannot load: file \"$loadFileName\" is not found or not a file.");
289 :     } else {
290 : parrello 1.12 # Set up options. We may need to specify the append option.
291 :     my %loadOptions;
292 :     if ($options->{appendLoad}) {
293 :     $loadOptions{append} = 1;
294 :     }
295 :     # Now we can load.
296 : parrello 1.5 Trace("Loading attribute data from $loadFileName.") if T(2);
297 : parrello 1.12 my $stats = $ca->LoadAttributesFrom($loadFileName, %loadOptions);
298 : parrello 1.5 Trace("Load statistics:\n" . $stats->Show()) if T(2);
299 :     }
300 :     }
301 : parrello 1.10 if ($options->{summary}) {
302 :     # Here we have a summary report. The value of the option is the name of a file that is to
303 :     # contain an html-formatted report. We start by getting a complete list of the keys and
304 :     # the associated counts.
305 :     my $keyCounts = GetAllKeys($ca);
306 :     # Buffer the lines in the following list.
307 :     my @lines = ();
308 :     # Start the table.
309 :     push @lines, $cgi->start_table({border => 2});
310 :     push @lines, $cgi->Tr($cgi->th({align => 'left'}, 'Key Name'), $cgi->th({align => 'right'}, 'Values'));
311 :     # Loop through the key hash, building row data.
312 :     for my $key (sort keys %{$keyCounts}) {
313 :     push @lines, $cgi->Tr($cgi->td({align => 'left'}, $key), $cgi->td({align => 'right'}, $keyCounts->{$key}));
314 :     }
315 :     # Close off the table.
316 :     push @lines, $cgi->end_table();
317 :     # Output the page.
318 :     WritePage($options->{summary}, 'Attribute Key Summary', \@lines);
319 :     }
320 :     if ($options->{compare}) {
321 :     # Here we have a comparison report. The value of the option is the name of a file that is
322 :     # to contain an html-formatted report. We need access to the SEED database to pull this
323 :     # off.
324 :     my $dbh = $fig->db_handle();
325 :     Trace("Retrieving key data.") if T(2);
326 :     # Get counts for all the keys in the old system.
327 :     my $oldKeys = $dbh->SQL("SELECT tag, COUNT(*) FROM attribute GROUP BY tag");
328 :     # Insure the query worked.
329 :     if (! defined($oldKeys)) {
330 :     Trace("Database error retrieving old system keys:" . $dbh->errstr) if T(0);
331 :     } else {
332 :     # Convert the key data into a hash.
333 :     my %oldKeyHash = map { $_->[0] => $_->[1] } @{$oldKeys};
334 :     # Get the counts for all the keys in the new system.
335 :     my $newKeyHashRef = GetNewKeyCounts($ca);
336 :     # We've got our data, so the next step is to start accumulating the lines of the web page.
337 :     Trace("Processing key table for new system.") if T(2);
338 :     my @lines = ();
339 :     # Start the first table.
340 :     push @lines, $cgi->h3("New System Keys");
341 :     push @lines, $cgi->start_table({border => 2});
342 :     push @lines, $cgi->Tr($cgi->th({align => 'left'}, 'Key Name'), $cgi->th({align => 'right'}, 'New Values'),
343 :     $cgi->th({align => 'right'}, 'Old Values'));
344 :     # Now we process the new keys. As we run through them, we'll delete matching keys from
345 :     # the old key hash. The remaining keys will be output as a missing-keys table.
346 :     for my $key (sort keys %{$newKeyHashRef}) {
347 :     # Get the new system count.
348 :     my $newCount = $newKeyHashRef->{$key};
349 :     # Default to a blank in the old system count column.
350 :     my $oldCount = '&nbsp;';
351 :     # Check to see if this key exists in the old system.
352 :     if (exists $oldKeyHash{$key}) {
353 :     # If it does, save its count and delete it from the old system hash.
354 :     $oldCount = $oldKeyHash{$key};
355 :     delete $oldKeyHash{$key};
356 :     }
357 :     # Output this table row.
358 :     push @lines, $cgi->Tr($cgi->td({align => 'left'}, $key), $cgi->td({align => 'right'}, $newCount),
359 :     $cgi->td({align => 'right'}, $oldCount));
360 :     }
361 :     # Close the table.
362 :     push @lines, $cgi->end_table();
363 :     # Now the remaining keys in the old key hash are missing from the new system. We create a new table
364 :     # to display them.
365 :     my @missingKeys = sort keys %oldKeyHash;
366 :     # Only do this if there's at least one missing key.
367 :     if (@missingKeys == 0) {
368 :     push @lines, $cgi->p("No missing keys found.");
369 :     } else {
370 :     Trace("Processing missing key table.") if T(2);
371 :     # Start the second table.
372 :     push @lines, $cgi->h3("Keys Missing from New System");
373 :     push @lines, $cgi->start_table({border => 2});
374 :     push @lines, $cgi->Tr($cgi->th({align => 'left'}, 'Key Name'), $cgi->td({align => 'right'}, 'Values'));
375 :     # Loop through the missing keys, writing them to the table.
376 :     for my $key (@missingKeys) {
377 :     push @lines, $cgi->Tr($cgi->td({align => 'left'}, $key), $cgi->td({align => 'right'}, $oldKeyHash{$key}));
378 :     }
379 :     # Close the table.
380 :     push @lines, $cgi->end_table();
381 :     }
382 :     # Write the web page.
383 :     WritePage($options->{compare}, 'Attribute System Comparison', \@lines);
384 :     }
385 :     }
386 : parrello 1.14 if ($options->{mapSubkey}) {
387 :     # Parse out the main key.
388 :     my $mapSubkey = $options->{mapSubkey};
389 :     if ($mapSubkey =~ m#([^/]+)(/.+)#) {
390 :     my ($keyName, $pattern) = ($1, $2);
391 :     Trace("Processing subkey mapping for $keyName with pattern s$pattern.") if T(2);
392 :     # Create a statistics object.
393 :     my $results = Stats->new();
394 :     # Get all the subkey values.
395 :     my %subkeys = map { $_ => 1 } $ca->GetFlat(['HasValueFor'], "HasValueFor(from-link) = ?", [$keyName],
396 :     'HasValueFor(subkey)');
397 :     my $totalSubkeys = scalar keys %subkeys;
398 :     Trace("$totalSubkeys subkeys found.") if T(2);
399 :     # Loop through them, doing updates where necessary.
400 :     for my $subKey (keys %subkeys) {
401 :     # Count this subkey.
402 :     my $subkeyCount = $results->Add(subkeys => 1);
403 :     # Apply the substitution.
404 :     my $newSubKey = $subKey;
405 :     eval("\$newSubKey =~ s$pattern");
406 :     # If the evaluation resulted in an error, stop immediately.
407 :     if ($@) {
408 :     Confess("Error in substitution pattern: $@");
409 :     } elsif ($newSubKey ne $subKey) {
410 :     # Here the substitution worked and it changed the key value.
411 :     # We need to update the database.
412 :     $results->Add(updates => 1);
413 :     my $count = $ca->UpdateField('HasValueFor(subkey)', $subKey, $newSubKey,
414 :     "HasValueFor(from-link) = ?", [$keyName]);
415 :     $results->Add(rowsChanged => $count);
416 :     }
417 :     # Trace our progress.
418 :     if ($subkeyCount % 100 == 0) {
419 :     Trace("$subkeyCount of $totalSubkeys processed.") if T(3);
420 :     }
421 :     }
422 :     # Display the statistics.
423 :     Trace("Statistics from mapSubkey update:\n" . $results->Show()) if T(2);
424 :     } else {
425 :     # Here the incoming parameter was in the wrong format. Mostly this means there
426 :     # was nothing before the slash or no slash was found.
427 :     Confess("Invalid substitution syntax in mapSubkey option.");
428 :     }
429 :     }
430 : parrello 1.2 Trace("Processing complete.") if T(2);
431 : parrello 1.1 };
432 :     if ($@) {
433 :     Trace("Script failed with error: $@") if T(0);
434 :     $rtype = "error";
435 :     } else {
436 :     Trace("Script complete.") if T(2);
437 :     $rtype = "no error";
438 :     }
439 :     if ($options->{phone}) {
440 :     my $msgID = Tracer::SendSMS($options->{phone}, "RefreshAttrDB terminated with $rtype.");
441 :     if ($msgID) {
442 :     Trace("Phone message sent with ID $msgID.") if T(2);
443 :     } else {
444 :     Trace("Phone message not sent.") if T(2);
445 :     }
446 :     }
447 :    
448 : parrello 1.4 =head3 MigrateAttributes
449 :    
450 : parrello 1.6 C<< my $stats = MigrateAttributes($ca, $fig, $preserve); >>
451 : parrello 1.4
452 :     Migrate all the attributes data from the specified FIG instance. This is a long, slow
453 :     method used to convert the old attribute data to the new system. Only attribute
454 :     keys that are already in the database will be loaded, and they will completely
455 :     replace the existing values for those keys. Therefore, it is very important that the
456 :     FIG instance not be connected to the attribute database.
457 :    
458 :     =over 4
459 :    
460 :     =item ca
461 :    
462 :     B<CustomAttributes> object used to access the attribute database.
463 :    
464 :     =item fig
465 :    
466 :     A FIG object that can be used to retrieve attributes for migration purposes.
467 :    
468 : parrello 1.6 =item preserve (optional)
469 :    
470 :     A comma-delimited list of attributes that are not to be migrated.
471 :    
472 : parrello 1.4 =item RETURN
473 :    
474 :     Returns a statistical object for the load process.
475 :    
476 :     =back
477 :    
478 :     =cut
479 :    
480 :     sub MigrateAttributes {
481 :     # Get the parameters.
482 : parrello 1.6 my ($ca, $fig, $preserve) = @_;
483 : parrello 1.4 # Create the return value.
484 : parrello 1.5 my $retVal = Stats->new('keysIn');
485 :     # Create a loader for the value table.
486 :     my $hasValueFor = ERDBLoad->new($ca, 'HasValueFor', $FIG_Config::temp);
487 :     # Create a hash for the target objects.
488 :     my %targetObjectHash = ();
489 : parrello 1.6 # Get a list of the attributes we're to preserve.
490 :     my %preserve = ();
491 :     if (defined $preserve) {
492 :     %preserve = map { $_ => 1 } split /\s*,\s*/, $preserve;
493 :     }
494 :     # Put the preserved keys into the load file.
495 :     for my $key (keys %preserve) {
496 :     Trace("Preserving key $key.") if T(3);
497 :     my @newValues = $ca->GetAttributes(undef, $key);
498 :     Trace(scalar(@newValues) . " values of $key will be preserved.");
499 :     # Put the values into the load file.
500 :     PutValue($hasValueFor, $ca, @newValues);
501 :     }
502 : parrello 1.4 # Get a list of all our attribute keys.
503 : parrello 1.6 my @allKeys = $ca->GetFlat(['AttributeKey'], "", [], 'AttributeKey(id)');
504 :     # Delete the preserved keys.
505 :     my @keys = grep { ! $preserve{$_} } @allKeys;
506 :     # Loop through the reset, building the load files.
507 : parrello 1.4 for my $key (@keys) {
508 :     Trace("Migrating key $key.") if T(3);
509 :     $retVal->Add(keysIn => 1);
510 :     # Get all the values of the specified key.
511 :     my @oldValues = $fig->get_attributes(undef, $key);
512 :     my $count = scalar(@oldValues);
513 :     Trace("$count values found for $key in source system.") if T(3);
514 : parrello 1.6 # Put the values into the load file.
515 :     PutValue($hasValueFor, $ca, @oldValues);
516 : parrello 1.4 }
517 : parrello 1.5 # Close and finish the loads to upload the data.
518 :     Trace("Closing value table.") if T(2);
519 :     my $hvfStats = $hasValueFor->FinishAndLoad();
520 :     Trace("Statistics from value table load:\n" . $hvfStats->Show()) if T(2);
521 :     # Merge the statistics.
522 :     $retVal->Accumulate($hvfStats);
523 : parrello 1.4 # Return the statistics object.
524 :     return $retVal;
525 :     }
526 :    
527 : parrello 1.6 =head3 PutValue
528 :    
529 :     C<< PutValue($hasValueFor, $ca, @values); >>
530 :    
531 :     Put the values from an attribute value list into a HasValueFor load file.
532 :    
533 :     =over 4
534 :    
535 :     =item hasValueFor
536 :    
537 :     Load object for the HasValueFor table.
538 :    
539 :     =item ca
540 :    
541 :     A CustomAttribute object. We get the splitter value from it.
542 :    
543 :     =item value
544 :    
545 :     A list of tuples, each consisting of an object ID, a key name, and one or more values.
546 :    
547 :     =back
548 :    
549 :     =cut
550 :    
551 :     sub PutValue {
552 :     # Get the parameters.
553 :     my ($hasValueFor, $ca, @values) = @_;
554 :     # Loop through the value rows.
555 :     for my $row (@values) {
556 :     # Get this row's data.
557 :     my ($id, $key, @values) = @{$row};
558 :     # Format the values.
559 :     my $valueString = join($ca->{splitter}, @values);
560 :     # Add the value.
561 :     $hasValueFor->Put($key, $id, $valueString);
562 :     }
563 :     }
564 :    
565 :     =head3 MigrateCollections
566 :    
567 :     C<< my $stats = MigrateCollections($ca, $fig); >>
568 :    
569 :     This method copies the collection data from the specified FIG object and stores it as values
570 :     of the C<collection> attribute in the specified custom attribute database.
571 :    
572 :     =over 4
573 :    
574 :     =item ca
575 :    
576 :     Custom attribute database into which the collections are to be stored.
577 :    
578 :     =item fig
579 :    
580 :     FIG object from which the collection attributes are to be harvested.
581 :    
582 :     =item RETURN
583 :    
584 :     Returns a statistics object with informatino about the migration.
585 :    
586 :     =back
587 :    
588 :     =cut
589 :    
590 :     sub MigrateCollections {
591 :     # Get the parameters.
592 :     my ($ca, $fig) = @_;
593 :     # Declare the return variable.
594 :     my $retVal = Stats->new();
595 :     # Get the collection names.
596 :     my @collections = qw(higher_plants eukaryotic_ps nonoxygenic_ps hundred_hundred functional_coupling_paper ecoli_essentiality_paper);
597 :     # Erase the current collection date.
598 :     $ca->EraseAttribute('collection');
599 :     # Loop through the collection attributes.
600 :     for my $cname (@collections) {
601 :     $retVal->Add(collection => 1);
602 :     # Get this attribute from the old system.
603 :     my @rows = $fig->get_attributes(undef, $cname);
604 :     # Loop through its values.
605 :     for my $row (@rows) {
606 :     $retVal->Add($cname => 1);
607 :     # Determine the object key.
608 :     my $objectID = ($row->[0] eq 'Subsystem' ? $row->[2] : $row->[0]);
609 :     $ca->AddAttribute($objectID, 'collection', $cname);
610 :     }
611 :     }
612 :     # Return the statistics.
613 :     return $retVal;
614 :     }
615 :    
616 : parrello 1.10 =head3 GetAllKeys
617 :    
618 :     C<< my @keys = GetAllKeys($ca); >>
619 :    
620 :     Return a sorted list of the attribute keys.
621 :    
622 :     =over 4
623 :    
624 :     =item ca
625 :    
626 :     CustomAttributes object used to access the database.
627 :    
628 :     =item RETURN
629 :    
630 :     Returns a sorted list of all the attribute keys.
631 :    
632 :     =back
633 :    
634 :     =cut
635 :    
636 :     sub GetAllKeys {
637 :     # Get the parameters.
638 :     my ($ca) = @_;
639 :     # Get the attribute data.
640 :     my %keyData = $ca->GetAttributeData('name', '');
641 :     # Sort the keys.
642 :     my @retVal = sort keys %keyData;
643 :     # Return the result.
644 :     return @retVal;
645 :     }
646 :    
647 :     =head3 OpenPage
648 :    
649 :     C<< my $fh = OpenPage($fileName, $title); >>
650 :    
651 :     Start writing an HTML page to a file and return the file handle.
652 :    
653 :     =over 4
654 :    
655 :     =item fileName
656 :    
657 :     Name of the file to which the page will be written.
658 :    
659 :     =item title
660 :    
661 :     Title for the page.
662 :    
663 :     =item RETURN
664 :    
665 :     Returns the file handle for writing the rest of the page.
666 :    
667 :     =back
668 :    
669 :     =cut
670 :    
671 :     sub OpenPage {
672 :     # Get the parameters.
673 :     my ($fileName, $title) = @_;
674 :     # Open the file.
675 :     my $retVal = Open(undef, ">$fileName");
676 :     # Write out the HTML headers.
677 :     print $retVal $cgi->start_html(-title => $title, -BGCOLOR => 'silver');
678 :     print $retVal "\n";
679 :     # Return the file handle.
680 :     return $retVal;
681 :     }
682 :    
683 :     =head3 GetNewKeyCounts
684 :    
685 :     C<< my %counts = GetNewKeyCounts($ca); >>
686 :    
687 :     Return a hash mapping attribute key names to counts.
688 :    
689 :     =over 4
690 :    
691 :     =item ca
692 :    
693 :     CustomAttributes object for accessing the attribute database.
694 :    
695 :     =item RETURN
696 :    
697 :     Returns a reference to a hash mapping each key name to a count of the key's values.
698 :    
699 :     =back
700 :    
701 :     =cut
702 :    
703 :     sub GetNewKeyCounts {
704 :     # Get the parameters.
705 :     my ($ca) = @_;
706 :     # Declare the return variable.
707 :     my $retVal = {};
708 :     # Get all of the keys.
709 :     my @keys = GetAllKeys($ca);
710 :     # Loop through the list, filling the hash.
711 :     for my $key (@keys) {
712 :     my $count = $ca->GetCount(['HasValueFor'], "HasValueFor(from-link) = ?", [$key]);
713 :     $retVal->{$key} = $count;
714 :     }
715 :     # Return the result.
716 :     return $retVal;
717 :     }
718 :    
719 :     =head3 WritePage
720 :    
721 :     C<< WritePage($fileName, $title, \@lines); >>
722 :    
723 :     Write the specified lines to the specified file as a web page. The lines are assumed to be raw
724 :     HTML body text. They will be preceded by a standard HTML header and followed by a standard
725 :     HTML footer.
726 :    
727 :     =over 4
728 :    
729 :     =item fileName
730 :    
731 :     Name of the output file.
732 :    
733 :     =item title
734 :    
735 :     Title for the web page.
736 :    
737 :     =item lines
738 :    
739 :     Reference to a list of lines of HTML.
740 :    
741 :     =back
742 :    
743 :     =cut
744 :    
745 :     sub WritePage {
746 :     # Get the parameters.
747 :     my ($fileName, $title, $lines) = @_;
748 :     # Open the file and write the header to it. The header includes everything up to and including
749 :     # the BODY tag.
750 :     Trace("Writing web page to $fileName.") if T(2);
751 :     my $oh = OpenPage($fileName, $title);
752 :     # Write the lines one at a time.
753 :     for my $line (@{$lines}) {
754 :     print $oh "$line\n";
755 :     }
756 :     # Write the HTML footer.
757 :     print $oh $cgi->end_html();
758 :     # Close the output file.
759 :     close $oh;
760 :     Trace("Web page created in $fileName.") if T(2);
761 :     }
762 :    
763 : parrello 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3