[Bio] / Sprout / CustomAttributes.pm Repository:
ViewVC logotype

Annotation of /Sprout/CustomAttributes.pm

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 :     package CustomAttributes;
4 :    
5 :     require Exporter;
6 :     use ERDB;
7 : parrello 1.4 @ISA = qw(ERDB);
8 : parrello 1.1 use strict;
9 :     use Tracer;
10 :     use ERDBLoad;
11 : parrello 1.13 use Stats;
12 : parrello 1.1
13 :     =head1 Custom SEED Attribute Manager
14 :    
15 :     =head2 Introduction
16 :    
17 :     The Custom SEED Attributes Manager allows the user to upload and retrieve
18 :     custom data for SEED objects. It uses the B<ERDB> database system to
19 : parrello 1.10 store the attributes.
20 :    
21 :     Attributes are organized by I<attribute key>. Attribute values are
22 :     assigned to I<objects>. In the real world, objects have types and IDs;
23 :     however, to the attribute database only the ID matters. This will create
24 :     a problem if we have a single ID that applies to two objects of different
25 :     types, but it is more consistent with the original attribute implementation
26 : parrello 1.11 in the SEED (which this implementation replaces).
27 : parrello 1.10
28 : parrello 1.11 The actual attribute values are stored as a relationship between the attribute
29 :     keys and the objects. There can be multiple values for a single key/object pair.
30 : parrello 1.1
31 :     The full suite of ERDB retrieval capabilities is provided. In addition,
32 :     custom methods are provided specific to this application. To get all
33 : parrello 1.6 the values of the attribute C<essential> in a specified B<Feature>, you
34 : parrello 1.1 would code
35 :    
36 : parrello 1.10 my @values = $attrDB->GetAttributes($fid, 'essential');
37 : parrello 1.1
38 : parrello 1.10 where I<$fid> contains the ID of the desired feature.
39 : parrello 1.1
40 : parrello 1.10 New attribute keys must be defined before they can be used. A web interface
41 :     is provided for this purpose.
42 : parrello 1.1
43 :     =head2 FIG_Config Parameters
44 :    
45 :     The following configuration parameters are used to manage custom attributes.
46 :    
47 :     =over 4
48 :    
49 :     =item attrDbms
50 :    
51 :     Type of database manager used: C<mysql> for MySQL or C<pg> for PostGres.
52 :    
53 :     =item attrDbName
54 :    
55 :     Name of the attribute database.
56 :    
57 :     =item attrHost
58 :    
59 :     Name of the host server for the database. If omitted, the current host
60 :     is used.
61 :    
62 :     =item attrUser
63 :    
64 :     User name for logging in to the database.
65 :    
66 :     =item attrPass
67 :    
68 :     Password for logging in to the database.
69 :    
70 :     =item attrPort
71 :    
72 :     TCP/IP port for accessing the database.
73 :    
74 :     =item attrSock
75 :    
76 :     Socket name used to access the database. If omitted, the default socket
77 :     will be used.
78 :    
79 :     =item attrDBD
80 :    
81 :     Fully-qualified file name for the database definition XML file. This file
82 :     functions as data to the attribute management process, so if the data is
83 :     moved, this file must go with it.
84 :    
85 :     =back
86 :    
87 :     =head2 Public Methods
88 :    
89 :     =head3 new
90 :    
91 : parrello 1.3 C<< my $attrDB = CustomAttributes->new($splitter); >>
92 : parrello 1.1
93 : parrello 1.10 Construct a new CustomAttributes object.
94 : parrello 1.3
95 :     =over 4
96 :    
97 :     =item splitter
98 :    
99 :     Value to be used to split attribute values into sections in the
100 :     L</Fig Replacement Methods>. The default is a double colon C<::>.
101 :     If you do not use the replacement methods, you do not need to
102 :     worry about this parameter.
103 :    
104 :     =back
105 : parrello 1.1
106 :     =cut
107 :    
108 :     sub new {
109 :     # Get the parameters.
110 : parrello 1.3 my ($class, $splitter) = @_;
111 : parrello 1.1 # Connect to the database.
112 :     my $dbh = DBKernel->new($FIG_Config::attrDbms, $FIG_Config::attrDbName,
113 :     $FIG_Config::attrUser, $FIG_Config::attrPass,
114 :     $FIG_Config::attrPort, $FIG_Config::attrHost,
115 :     $FIG_Config::attrSock);
116 :     # Create the ERDB object.
117 :     my $xmlFileName = $FIG_Config::attrDBD;
118 :     my $retVal = ERDB::new($class, $dbh, $xmlFileName);
119 : parrello 1.3 # Store the splitter value.
120 :     $retVal->{splitter} = (defined($splitter) ? $splitter : '::');
121 : parrello 1.1 # Return the result.
122 :     return $retVal;
123 :     }
124 :    
125 : parrello 1.10 =head3 StoreAttributeKey
126 :    
127 :     C<< $attrDB->StoreAttributeKey($attributeName, $type, $notes, \@groups); >>
128 :    
129 :     Create or update an attribute for the database.
130 :    
131 :     =over 4
132 : parrello 1.1
133 :     =item attributeName
134 :    
135 :     Name of the attribute. It must be a valid ERDB field name, consisting entirely of
136 :     letters, digits, and hyphens, with a letter at the beginning. If it does not
137 :     exist already, it will be created.
138 :    
139 :     =item type
140 :    
141 :     Data type of the attribute. This must be a valid ERDB data type name.
142 :    
143 :     =item notes
144 :    
145 :     Descriptive notes about the attribute. It is presumed to be raw text, not HTML.
146 :    
147 : parrello 1.10 =item groups
148 : parrello 1.1
149 : parrello 1.10 Reference to a list of the groups to which the attribute should be associated.
150 :     This will replace any groups to which the attribute is currently attached.
151 : parrello 1.1
152 :     =back
153 :    
154 :     =cut
155 :    
156 : parrello 1.3 sub StoreAttributeKey {
157 : parrello 1.1 # Get the parameters.
158 : parrello 1.10 my ($self, $attributeName, $type, $notes, $groups) = @_;
159 : parrello 1.8 # Declare the return variable.
160 :     my $retVal;
161 : parrello 1.1 # Get the data type hash.
162 :     my %types = ERDB::GetDataTypes();
163 :     # Validate the initial input values.
164 :     if (! ERDB::ValidateFieldName($attributeName)) {
165 :     Confess("Invalid attribute name \"$attributeName\" specified.");
166 :     } elsif (! $notes || length($notes) < 25) {
167 :     Confess("Missing or incomplete description for $attributeName.");
168 :     } elsif (! exists $types{$type}) {
169 :     Confess("Invalid data type \"$type\" for $attributeName.");
170 :     } else {
171 : parrello 1.10 # Okay, we're ready to begin. See if this key exists.
172 :     my $attribute = $self->GetEntity('AttributeKey', $attributeName);
173 :     if (defined($attribute)) {
174 :     # It does, so we do an update.
175 :     $self->UpdateEntity('AttributeKey', $attributeName,
176 :     { description => $notes, 'data-type' => $type });
177 :     # Detach the key from its current groups.
178 :     $self->Disconnect('IsInGroup', 'AttributeKey', $attributeName);
179 :     } else {
180 :     # It doesn't, so we do an insert.
181 :     $self->InsertObject('AttributeKey', { id => $attributeName,
182 :     description => $notes, 'data-type' => $type });
183 : parrello 1.8 }
184 : parrello 1.10 # Attach the key to the specified groups. (We presume the groups already
185 :     # exist.)
186 :     for my $group (@{$groups}) {
187 :     $self->InsertObject('IsInGroup', { 'from-link' => $attributeName,
188 :     'to-link' => $group });
189 : parrello 1.1 }
190 :     }
191 :     }
192 :    
193 : parrello 1.3 =head3 LoadAttributeKey
194 : parrello 1.1
195 : parrello 1.10 C<< my $stats = $attrDB->LoadAttributeKey($keyName, $fh, $keyCol, $dataCol, %options); >>
196 : parrello 1.1
197 :     Load the specified attribute from the specified file. The file should be a
198 :     tab-delimited file with internal tab and new-line characters escaped. This is
199 :     the typical TBL-style file used by most FIG applications. One of the columns
200 : parrello 1.10 in the input file must contain the appropriate object id value and the other the
201 : parrello 1.1 corresponding attribute value.
202 :    
203 :     =over 4
204 :    
205 : parrello 1.10 =item keyName
206 : parrello 1.1
207 : parrello 1.10 Key of the attribute to load.
208 : parrello 1.1
209 :     =item fh
210 :    
211 :     Open file handle for the input file.
212 :    
213 : parrello 1.10 =item idCol
214 : parrello 1.1
215 : parrello 1.10 Index (0-based) of the column containing the ID field. The ID field should
216 : parrello 1.1 contain the ID of an instance of the named entity.
217 :    
218 :     =item dataCol
219 :    
220 :     Index (0-based) of the column containing the data value field.
221 :    
222 : parrello 1.10 =item options
223 :    
224 :     Hash specifying the options for this load.
225 :    
226 : parrello 1.1 =item RETURN
227 :    
228 :     Returns a statistics object for the load process.
229 :    
230 :     =back
231 :    
232 : parrello 1.10 The available options are as follows.
233 :    
234 :     =over 4
235 :    
236 :     =item erase
237 :    
238 :     If TRUE, the key's values will all be erased before loading. (Doing so
239 :     makes for a faster load.)
240 :    
241 :     =back
242 :    
243 : parrello 1.1 =cut
244 :    
245 : parrello 1.3 sub LoadAttributeKey {
246 : parrello 1.1 # Get the parameters.
247 : parrello 1.10 my ($self, $keyName, $fh, $idCol, $dataCol, %options) = @_;
248 : parrello 1.1 # Create the return variable.
249 : parrello 1.11 my $retVal = Stats->new("lineIn", "shortLine", "newObject");
250 : parrello 1.10 # Compute the minimum number of fields required in each input line.
251 :     my $minCols = ($idCol < $dataCol ? $idCol : $idCol) + 1;
252 :     # Insure the attribute key exists.
253 :     my $found = $self->GetEntity('AttributeKey', $keyName);
254 :     if (! defined $found) {
255 :     Confess("Attribute key \"$keyName\" not found in database.");
256 : parrello 1.1 } else {
257 : parrello 1.11 # Erase the key's current values.
258 :     $self->EraseAttribute($keyName);
259 :     # Save a list of the object IDs we need to add.
260 :     my %objectIDs = ();
261 : parrello 1.10 # Loop through the input file.
262 :     while (! eof $fh) {
263 :     # Get the next line of the file.
264 :     my @fields = Tracer::GetLine($fh);
265 :     $retVal->Add(lineIn => 1);
266 :     # Now we need to validate the line.
267 :     if (scalar(@fields) < $minCols) {
268 :     $retVal->Add(shortLine => 1);
269 :     } else {
270 :     # It's valid, so get the ID and value.
271 :     my ($id, $value) = ($fields[$idCol], $fields[$dataCol]);
272 :     # Denote we're using this input line.
273 :     $retVal->Add(lineUsed => 1);
274 : parrello 1.11 # Now the fun begins. Find out if we need to create a target object record for this object ID.
275 :     if (! exists $objectIDs{$id}) {
276 :     my $found = $self->Exists('TargetObject', $id);
277 :     if (! $found) {
278 :     $self->InsertObject('TargetObject', { id => $id });
279 : parrello 1.10 }
280 : parrello 1.11 $objectIDs{$id} = 1;
281 :     $retVal->Add(newObject => 1);
282 : parrello 1.1 }
283 : parrello 1.11 # Now we insert the attribute.
284 :     $self->InsertObject('HasValueFor', { from => $keyName, to => $id, value => $value });
285 :     $retVal->Add(newValue => 1);
286 : parrello 1.1 }
287 :     }
288 :     }
289 :     # Return the statistics.
290 :     return $retVal;
291 :     }
292 :    
293 :    
294 : parrello 1.3 =head3 DeleteAttributeKey
295 :    
296 : parrello 1.10 C<< my $stats = $attrDB->DeleteAttributeKey($attributeName); >>
297 : parrello 1.1
298 :     Delete an attribute from the custom attributes database.
299 :    
300 :     =over 4
301 :    
302 : parrello 1.10 =item attributeName
303 : parrello 1.1
304 : parrello 1.10 Name of the attribute to delete.
305 : parrello 1.1
306 : parrello 1.10 =item RETURN
307 : parrello 1.1
308 : parrello 1.10 Returns a statistics object describing the effects of the deletion.
309 : parrello 1.1
310 :     =back
311 :    
312 :     =cut
313 :    
314 : parrello 1.3 sub DeleteAttributeKey {
315 : parrello 1.1 # Get the parameters.
316 : parrello 1.10 my ($self, $attributeName) = @_;
317 :     # Delete the attribute key.
318 :     my $retVal = $self->Delete('AttributeKey', $attributeName);
319 :     # Return the result.
320 :     return $retVal;
321 :    
322 :     }
323 :    
324 :     =head3 NewName
325 :    
326 :     C<< my $text = CustomAttributes::NewName(); >>
327 :    
328 :     Return the string used to indicate the user wants to add a new attribute.
329 :    
330 :     =cut
331 :    
332 :     sub NewName {
333 :     return "(new)";
334 : parrello 1.1 }
335 :    
336 :     =head3 ControlForm
337 :    
338 : parrello 1.10 C<< my $formHtml = $attrDB->ControlForm($cgi, $name, \%keys); >>
339 : parrello 1.1
340 :     Return a form that can be used to control the creation and modification of
341 : parrello 1.10 attributes. Only a subset of the attribute keys will be displayed, as
342 :     determined by the incoming list.
343 : parrello 1.1
344 :     =over 4
345 :    
346 :     =item cgi
347 :    
348 :     CGI query object used to create HTML.
349 :    
350 :     =item name
351 :    
352 :     Name to give to the form. This should be unique for the web page.
353 :    
354 : parrello 1.10 =item keys
355 :    
356 :     Reference to a hash mapping attribute keys to n-tuples. Each tuple will contain the
357 :     attribute's data type, its description, and a list of the groups in which it participates.
358 :    
359 : parrello 1.1 =item RETURN
360 :    
361 : parrello 1.10 Returns the HTML for a form that can be used to submit instructions to the C<Attributes.cgi> script
362 :     for loading, creating, displaying, changing, or deleting an attribute. Note that only the form
363 :     controls are generated. The form tags are left to the caller.
364 : parrello 1.1
365 :     =back
366 :    
367 :     =cut
368 :    
369 :     sub ControlForm {
370 :     # Get the parameters.
371 : parrello 1.10 my ($self, $cgi, $name, $keys) = @_;
372 : parrello 1.1 # Declare the return list.
373 :     my @retVal = ();
374 :     # We'll put the controls in a table. Nothing else ever seems to look nice.
375 :     push @retVal, $cgi->start_table({ border => 2, cellpadding => 2 });
376 :     # The first row is for selecting the field name.
377 :     push @retVal, $cgi->Tr($cgi->th("Select a Field"),
378 : parrello 1.10 $cgi->td($self->FieldMenu($cgi, 10, 'fieldName', $keys,
379 :     new => 1,
380 :     notes => "document.$name.notes.value",
381 :     type => "document.$name.dataType.value",
382 :     groups => "document.$name.groups")));
383 : parrello 1.1 # Now we set up a dropdown for the data types. The values will be the
384 :     # data type names, and the labels will be the descriptions.
385 :     my %types = ERDB::GetDataTypes();
386 :     my %labelMap = map { $_ => $types{$_}->{notes} } keys %types;
387 :     my $typeMenu = $cgi->popup_menu(-name => 'dataType',
388 :     -values => [sort keys %types],
389 : parrello 1.10 -labels => \%labelMap,
390 :     -default => 'string');
391 :     # Allow the user to specify a new field name. This is required if the
392 :     # user has selected the "(new)" marker. We put a little scriptlet in here that
393 :     # selects the (new) marker when the user enters the field.
394 :     push @retVal, "<script language=\"javaScript\">";
395 :     my $fieldField = "document.$name.fieldName";
396 :     my $newName = "\"" . NewName() . "\"";
397 :     push @retVal, $cgi->Tr($cgi->th("New Field Name"),
398 :     $cgi->td($cgi->textfield(-name => 'newName',
399 :     -size => 30,
400 :     -value => "",
401 :     -onFocus => "setIfEmpty($fieldField, $newName);")),
402 :     );
403 : parrello 1.1 push @retVal, $cgi->Tr($cgi->th("Data type"),
404 :     $cgi->td($typeMenu));
405 :     # The next row is for the notes.
406 :     push @retVal, $cgi->Tr($cgi->th("Description"),
407 :     $cgi->td($cgi->textarea(-name => 'notes',
408 :     -rows => 6,
409 :     -columns => 80))
410 :     );
411 : parrello 1.10 # Now we have the groups, which are implemented as a checkbox group.
412 :     my @groups = $self->GetGroups();
413 :     push @retVal, $cgi->Tr($cgi->th("Groups"),
414 :     $cgi->td($cgi->checkbox_group(-name=>'groups',
415 :     -values=> \@groups))
416 :     );
417 : parrello 1.1 # If the user wants to upload new values for the field, then we have
418 :     # an upload file name and column indicators.
419 :     push @retVal, $cgi->Tr($cgi->th("Upload Values"),
420 :     $cgi->td($cgi->filefield(-name => 'newValueFile',
421 :     -size => 20) .
422 :     " Key&nbsp;" .
423 :     $cgi->textfield(-name => 'keyCol',
424 :     -size => 3,
425 :     -default => 0) .
426 :     " Value&nbsp;" .
427 :     $cgi->textfield(-name => 'valueCol',
428 :     -size => 3,
429 :     -default => 1)
430 :     ),
431 :     );
432 : parrello 1.10 # Now the three buttons: STORE, SHOW, and DELETE.
433 : parrello 1.1 push @retVal, $cgi->Tr($cgi->th("&nbsp;"),
434 :     $cgi->td({align => 'center'},
435 :     $cgi->submit(-name => 'Delete', -value => 'DELETE') . " " .
436 : parrello 1.7 $cgi->submit(-name => 'Store', -value => 'STORE') . " " .
437 :     $cgi->submit(-name => 'Show', -value => 'SHOW')
438 : parrello 1.1 )
439 :     );
440 :     # Close the table and the form.
441 :     push @retVal, $cgi->end_table();
442 :     # Return the assembled HTML.
443 :     return join("\n", @retVal, "");
444 :     }
445 :    
446 : parrello 1.11 =head3 LoadAttributesFrom
447 :    
448 :     C<< my $stats = $attrDB->LoadAttributesFrom($fileName, %options); >>
449 :    
450 :     Load attributes from the specified tab-delimited file. Each line of the file must
451 :     contain an object ID in the first column, an attribute key name in the second
452 :     column, and attribute values in the remaining columns. The attribute values will
453 :     be assembled into a single value using the splitter code.
454 :    
455 :     =over 4
456 :    
457 :     =item fileName
458 :    
459 :     Name of the file from which to load the attributes.
460 :    
461 :     =item options
462 :    
463 :     Hash of options for modifying the load process.
464 :    
465 :     =item RETURN
466 :    
467 :     Returns a statistics object describing the load.
468 :    
469 :     =back
470 :    
471 :     Permissible option values are as follows.
472 :    
473 :     =over 4
474 :    
475 :     =item append
476 :    
477 :     If TRUE, then the attributes will be appended to existing data; otherwise, the
478 :     first time a key name is encountered, it will be erased.
479 :    
480 :     =back
481 :    
482 :     =cut
483 :    
484 :     sub LoadAttributesFrom {
485 :     # Get the parameters.
486 :     my ($self, $fileName, %options) = @_;
487 :     # Declare the return variable.
488 :     my $retVal = Stats->new('keys', 'values');
489 :     # Check for append mode.
490 :     my $append = ($options{append} ? 1 : 0);
491 :     # Create a hash of key names found.
492 :     my %keyHash = ();
493 :     # Open the file for input.
494 :     my $fh = Open(undef, "<$fileName");
495 :     # Loop through the file.
496 :     while (! eof $fh) {
497 :     my ($id, $key, @values) = Tracer::GetLine($fh);
498 :     $retVal->Add(linesIn => 1);
499 :     # Do some validation.
500 :     if (! defined($id)) {
501 :     # We ignore blank lines.
502 :     $retVal->Add(blankLines => 1);
503 :     } elsif (! defined($key)) {
504 :     # An ID without a key is a serious error.
505 :     my $lines = $retVal->Ask('linesIn');
506 :     Confess("Line $lines in $fileName has no attribute key.");
507 :     } else {
508 :     # Now we need to check for a new key.
509 :     if (! exists $keyHash{$key}) {
510 :     # This is a new key. Verify that it exists.
511 :     if (! $self->Exists('AttributeKey', $key)) {
512 :     my $line = $retVal->Ask('linesIn');
513 :     Confess("Attribute \"$key\" on line $line of $fileName not found in database.");
514 :     } else {
515 :     # Make sure we know this is no longer a new key.
516 :     $keyHash{$key} = 1;
517 :     $retVal->Add(keys => 1);
518 :     # If this is NOT append mode, erase the key.
519 :     if (! $append) {
520 :     $self->EraseAttribute($key);
521 :     }
522 :     }
523 :     Trace("Key $key found.") if T(3);
524 :     }
525 :     # Now we know the key is valid. Add this value.
526 :     $self->AddAttribute($id, $key, @values);
527 :     my $progress = $retVal->Add(values => 1);
528 :     Trace("$progress values loaded.") if T(3) && ($progress % 1000 == 0);
529 :    
530 :     }
531 :     }
532 :     # Return the result.
533 :     return $retVal;
534 :     }
535 :    
536 : parrello 1.13 =head3 BackupKeys
537 :    
538 :     C<< my $stats = $attrDB->BackupKeys($fileName, %options); >>
539 :    
540 :     Backup the attribute key information from the attribute database.
541 :    
542 :     =over 4
543 :    
544 :     =item fileName
545 :    
546 :     Name of the output file.
547 :    
548 :     =item options
549 :    
550 :     Options for modifying the backup process.
551 :    
552 :     =item RETURN
553 :    
554 :     Returns a statistics object for the backup.
555 :    
556 :     =back
557 :    
558 :     Currently there are no options. The backup is straight to a text file in
559 :     tab-delimited format. Each key is backup up to two lines. The first line
560 :     is all of the data from the B<AttributeKey> table. The second is a
561 :     tab-delimited list of all the groups.
562 :    
563 :     =cut
564 :    
565 :     sub BackupKeys {
566 :     # Get the parameters.
567 :     my ($self, $fileName, %options) = @_;
568 :     # Declare the return variable.
569 :     my $retVal = Stats->new();
570 :     # Open the output file.
571 :     my $fh = Open(undef, ">$fileName");
572 :     # Set up to read the keys.
573 :     my $keyQuery = $self->Get(['AttributeKey'], "", []);
574 :     # Loop through the keys.
575 :     while (my $keyData = $keyQuery->Fetch()) {
576 :     $retVal->Add(key => 1);
577 :     # Get the fields.
578 :     my ($id, $type, $description) = $keyData->Values(['AttributeKey(id)', 'AttributeKey(data-type)',
579 :     'AttributeKey(description)']);
580 :     # Escape any tabs or new-lines in the description.
581 :     my $escapedDescription = Tracer::Escape($description);
582 :     # Write the key data to the output.
583 :     Tracer::PutLine($fh, [$id, $type, $escapedDescription]);
584 :     # Get the key's groups.
585 :     my @groups = $self->GetFlat(['IsInGroup'], "IsInGroup(from-link) = ?", [$id],
586 :     'IsInGroup(to-link)');
587 :     $retVal->Add(memberships => scalar(@groups));
588 :     # Write them to the output. Note we put a marker at the beginning to insure the line
589 :     # is nonempty.
590 :     Tracer::PutLine($fh, ['#GROUPS', @groups]);
591 :     }
592 :     # Return the result.
593 :     return $retVal;
594 :     }
595 :    
596 :     =head3 RestoreKeys
597 :    
598 :     C<< my $stats = $attrDB->RestoreKeys($fileName, %options); >>
599 :    
600 :     Restore the attribute keys and groups from a backup file.
601 :    
602 :     =over 4
603 :    
604 :     =item fileName
605 :    
606 :     Name of the file containing the backed-up keys. Each key has a pair of lines,
607 :     one containing the key data and one listing its groups.
608 :    
609 :     =back
610 :    
611 :     =cut
612 :    
613 :     sub RestoreKeys {
614 :     # Get the parameters.
615 :     my ($self, $fileName, %options) = @_;
616 :     # Declare the return variable.
617 :     my $retVal = Stats->new();
618 :     # Set up a hash to hold the group IDs.
619 :     my %groups = ();
620 :     # Open the file.
621 :     my $fh = Open(undef, "<$fileName");
622 :     # Loop until we're done.
623 :     while (! eof $fh) {
624 :     # Get a key record.
625 :     my ($id, $dataType, $description) = Tracer::GetLine($fh);
626 :     if ($id eq '#GROUPS') {
627 :     Confess("Group record found when key record expected.");
628 :     } elsif (! defined($description)) {
629 :     Confess("Invalid format found for key record.");
630 :     } else {
631 :     $retVal->Add("keyIn" => 1);
632 :     # Add this key to the database.
633 :     $self->InsertObject('AttributeKey', { id => $id, 'data-type' => $dataType,
634 :     description => Tracer::UnEscape($description) });
635 :     Trace("Attribute $id stored.") if T(3);
636 :     # Get the group line.
637 :     my ($marker, @groups) = Tracer::GetLine($fh);
638 :     if (! defined($marker)) {
639 :     Confess("End of file found where group record expected.");
640 :     } elsif ($marker ne '#GROUPS') {
641 :     Confess("Group record not found after key record.");
642 :     } else {
643 :     $retVal->Add(memberships => scalar(@groups));
644 :     # Connect the groups.
645 :     for my $group (@groups) {
646 :     # Find out if this is a new group.
647 :     if (! $groups{$group}) {
648 :     $retVal->Add(newGroup => 1);
649 :     # Add the group.
650 :     $self->InsertObject('AttributeGroup', { id => $group });
651 :     Trace("Group $group created.") if T(3);
652 :     # Make sure we know it's not new.
653 :     $groups{$group} = 1;
654 :     }
655 :     # Connect the group to our key.
656 :     $self->InsertObject('IsInGroup', { 'from-link' => $id, 'to-link' => $group });
657 :     }
658 :     Trace("$id added to " . scalar(@groups) . " groups.") if T(3);
659 :     }
660 :     }
661 :     }
662 :     # Return the result.
663 :     return $retVal;
664 :     }
665 :    
666 :    
667 : parrello 1.11 =head3 BackupAllAttributes
668 :    
669 :     C<< my $stats = $attrDB->BackupAllAttributes($fileName, %options); >>
670 :    
671 :     Backup all of the attributes to a file. The attributes will be stored in a
672 :     tab-delimited file suitable for reloading via L</LoadAttributesFrom>.
673 :    
674 :     =over 4
675 :    
676 :     =item fileName
677 :    
678 :     Name of the file to which the attribute data should be backed up.
679 :    
680 :     =item options
681 :    
682 :     Hash of options for the backup.
683 :    
684 :     =item RETURN
685 :    
686 :     Returns a statistics object describing the backup.
687 :    
688 :     =back
689 :    
690 :     Currently there are no options defined.
691 :    
692 :     =cut
693 :    
694 :     sub BackupAllAttributes {
695 :     # Get the parameters.
696 :     my ($self, $fileName, %options) = @_;
697 :     # Declare the return variable.
698 :     my $retVal = Stats->new();
699 :     # Get a list of the keys.
700 :     my @keys = $self->GetFlat(['AttributeKey'], "", [], 'AttributeKey(id)');
701 :     Trace(scalar(@keys) . " keys found during backup.") if T(2);
702 :     # Open the file for output.
703 : parrello 1.12 my $fh = Open(undef, ">$fileName");
704 : parrello 1.11 # Loop through the keys.
705 :     for my $key (@keys) {
706 :     Trace("Backing up attribute $key.") if T(3);
707 :     $retVal->Add(keys => 1);
708 :     # Loop through this key's values.
709 : parrello 1.12 my $query = $self->Get(['HasValueFor'], "HasValueFor(from-link) = ?", [$key]);
710 : parrello 1.11 my $valuesFound = 0;
711 :     while (my $line = $query->Fetch()) {
712 :     $valuesFound++;
713 :     # Get this row's data.
714 : parrello 1.13 my @row = $line->Values(['HasValueFor(to-link)', 'HasValueFor(from-link)',
715 : parrello 1.11 'HasValueFor(value)']);
716 :     # Write it to the file.
717 :     Tracer::PutLine($fh, \@row);
718 :     }
719 :     Trace("$valuesFound values backed up for key $key.") if T(3);
720 :     $retVal->Add(values => $valuesFound);
721 :     }
722 :     # Return the result.
723 :     return $retVal;
724 :     }
725 :    
726 : parrello 1.1 =head3 FieldMenu
727 :    
728 : parrello 1.10 C<< my $menuHtml = $attrDB->FieldMenu($cgi, $height, $name, $keys, %options); >>
729 : parrello 1.1
730 :     Return the HTML for a menu to select an attribute field. The menu will
731 :     be a standard SELECT/OPTION thing which is called "popup menu" in the
732 :     CGI package, but actually looks like a list. The list will contain
733 : parrello 1.10 one selectable row per field.
734 : parrello 1.1
735 :     =over 4
736 :    
737 :     =item cgi
738 :    
739 :     CGI query object used to generate HTML.
740 :    
741 :     =item height
742 :    
743 :     Number of lines to display in the list.
744 :    
745 :     =item name
746 :    
747 :     Name to give to the menu. This is the name under which the value will
748 :     appear when the form is submitted.
749 :    
750 : parrello 1.10 =item keys
751 :    
752 :     Reference to a hash mapping each attribute key name to a list reference,
753 :     the list itself consisting of the attribute data type, its description,
754 :     and a list of its groups.
755 :    
756 :     =item options
757 :    
758 :     Hash containing options that modify the generation of the menu.
759 :    
760 :     =item RETURN
761 :    
762 :     Returns the HTML to create a form field that can be used to select an
763 :     attribute from the custom attributes system.
764 :    
765 :     =back
766 :    
767 :     The permissible options are as follows.
768 :    
769 :     =over 4
770 :    
771 :     =item new
772 : parrello 1.1
773 :     If TRUE, then extra rows will be provided to allow the user to select
774 :     a new attribute. In other words, the user can select an existing
775 :     attribute, or can choose a C<(new)> marker to indicate a field to
776 :     be created in the parent entity.
777 :    
778 : parrello 1.10 =item notes
779 : parrello 1.1
780 :     If specified, the name of a variable for displaying the notes attached
781 :     to the field. This must be in Javascript form ready for assignment.
782 :     So, for example, if you have a variable called C<notes> that
783 :     represents a paragraph element, you should code C<notes.innerHTML>.
784 :     If it actually represents a form field you should code C<notes.value>.
785 :     If an C<innerHTML> coding is used, the text will be HTML-escaped before
786 :     it is copied in. Specifying this parameter generates Javascript for
787 :     displaying the field description when a field is selected.
788 :    
789 : parrello 1.10 =item type
790 : parrello 1.1
791 :     If specified, the name of a variable for displaying the field's
792 :     data type. Data types are a much more controlled vocabulary than
793 :     notes, so there is no worry about HTML translation. Instead, the
794 :     raw value is put into the specified variable. Otherwise, the same
795 :     rules apply to this value that apply to I<$noteControl>.
796 :    
797 : parrello 1.10 =item groups
798 : parrello 1.1
799 : parrello 1.10 If specified, the name of a multiple-selection list control (also called
800 :     a popup menu) which shall be used to display the selected groups.
801 : parrello 1.1
802 :     =back
803 :    
804 :     =cut
805 :    
806 :     sub FieldMenu {
807 :     # Get the parameters.
808 : parrello 1.10 my ($self, $cgi, $height, $name, $keys, %options) = @_;
809 :     # Reformat the list of keys.
810 :     my %keys = %{$keys};
811 :     # Add the (new) key, if needed.
812 :     if ($options{new}) {
813 :     $keys{NewName()} = ["string", ""];
814 : parrello 1.1 }
815 : parrello 1.10 # Get a sorted list of key.
816 :     my @keys = sort keys %keys;
817 :     # We need to create the name for the onChange function. This function
818 : parrello 1.1 # may not do anything, but we need to know the name to generate the HTML
819 :     # for the menu.
820 :     my $changeName = "${name}_setNotes";
821 :     my $retVal = $cgi->popup_menu({name => $name,
822 :     size => $height,
823 :     onChange => "$changeName(this.value)",
824 : parrello 1.10 values => \@keys,
825 :     });
826 : parrello 1.1 # Create the change function.
827 :     $retVal .= "\n<script language=\"javascript\">\n";
828 :     $retVal .= " function $changeName(fieldValue) {\n";
829 : parrello 1.10 # The function only has a body if we have a control to store data about the
830 :     # attribute.
831 :     if ($options{notes} || $options{type} || $options{groups}) {
832 : parrello 1.1 # Check to see if we're storing HTML or text into the note control.
833 : parrello 1.10 my $noteControl = $options{notes};
834 : parrello 1.1 my $htmlMode = ($noteControl && $noteControl =~ /innerHTML$/);
835 :     # We use a CASE statement based on the newly-selected field value. The
836 :     # field description will be stored in the JavaScript variable "myText"
837 :     # and the data type in "myType". Note the default data type is a normal
838 :     # string, but the default notes is an empty string.
839 :     $retVal .= " var myText = \"\";\n";
840 :     $retVal .= " var myType = \"string\";\n";
841 :     $retVal .= " switch (fieldValue) {\n";
842 : parrello 1.10 # Loop through the keys.
843 :     for my $key (@keys) {
844 :     # Generate this case.
845 :     $retVal .= " case \"$key\" :\n";
846 :     # Here we either want to update the note display, the
847 :     # type display, the group list, or a combination of them.
848 :     my ($type, $notes, @groups) = @{$keys{$key}};
849 :     if ($noteControl) {
850 :     # Insure it's in the proper form.
851 :     if ($htmlMode) {
852 :     $notes = ERDB::HTMLNote($notes);
853 : parrello 1.1 }
854 : parrello 1.10 # Escape it for use as a string literal.
855 :     $notes =~ s/\n/\\n/g;
856 :     $notes =~ s/"/\\"/g;
857 :     $retVal .= " myText = \"$notes\";\n";
858 :     }
859 :     if ($options{type}) {
860 :     # Here we want the type updated.
861 :     $retVal .= " myType = \"$type\";\n";
862 :     }
863 :     if ($options{groups}) {
864 :     # Here we want the groups shown. Get a list of this attribute's groups.
865 :     # We'll search through this list for each group to see if it belongs with
866 :     # our attribute.
867 :     my $groupLiteral = "=" . join("=", @groups) . "=";
868 :     # Now we need some variables containing useful code for the javascript. It's
869 :     # worth knowing we go through a bit of pain to insure $groupField[i] isn't
870 :     # parsed as an array element.
871 :     my $groupField = $options{groups};
872 :     my $currentField = $groupField . "[i]";
873 :     # Do the javascript.
874 :     $retVal .= " var groupList = \"$groupLiteral\";\n";
875 :     $retVal .= " for (var i = 0; i < $groupField.length; i++) {\n";
876 :     $retVal .= " var srchString = \"=\" + $currentField.value + \"=\";\n";
877 :     $retVal .= " var srchLoc = groupList.indexOf(srchString);\n";
878 :     $retVal .= " $currentField.checked = (srchLoc >= 0);\n";
879 :     $retVal .= " }\n";
880 : parrello 1.1 }
881 : parrello 1.10 # Close this case.
882 :     $retVal .= " break;\n";
883 : parrello 1.1 }
884 :     # Close the CASE statement and make the appropriate assignments.
885 :     $retVal .= " }\n";
886 :     if ($noteControl) {
887 :     $retVal .= " $noteControl = myText;\n";
888 :     }
889 : parrello 1.10 if ($options{type}) {
890 :     $retVal .= " $options{type} = myType;\n";
891 : parrello 1.1 }
892 :     }
893 :     # Terminate the change function.
894 :     $retVal .= " }\n";
895 :     $retVal .= "</script>\n";
896 :     # Return the result.
897 :     return $retVal;
898 :     }
899 :    
900 : parrello 1.10 =head3 GetGroups
901 : parrello 1.3
902 : parrello 1.10 C<< my @groups = $attrDB->GetGroups(); >>
903 : parrello 1.3
904 : parrello 1.10 Return a list of the available groups.
905 : parrello 1.3
906 :     =cut
907 :    
908 : parrello 1.10 sub GetGroups {
909 : parrello 1.3 # Get the parameters.
910 : parrello 1.10 my ($self) = @_;
911 :     # Get the groups.
912 :     my @retVal = $self->GetFlat(['AttributeGroup'], "", [], 'AttributeGroup(id)');
913 :     # Return them.
914 :     return @retVal;
915 : parrello 1.3 }
916 :    
917 : parrello 1.10 =head3 GetAttributeData
918 : parrello 1.3
919 : parrello 1.10 C<< my %keys = $attrDB->GetAttributeData($type, @list); >>
920 : parrello 1.3
921 : parrello 1.10 Return attribute data for the selected attributes. The attribute
922 :     data is a hash mapping each attribute key name to a n-tuple containing the
923 :     data type, the description, and the groups. This is the same format expected in
924 :     the L</FieldMenu> and L</ControlForm> methods for the list of attributes to display.
925 : parrello 1.3
926 :     =over 4
927 :    
928 : parrello 1.10 =item type
929 : parrello 1.4
930 : parrello 1.10 Type of attribute criterion: C<name> for attributes whose names begin with the
931 :     specified string, or C<group> for attributes in the specified group.
932 : parrello 1.4
933 : parrello 1.10 =item list
934 : parrello 1.4
935 : parrello 1.10 List containing the names of the groups or keys for the desired attributes.
936 : parrello 1.4
937 :     =item RETURN
938 :    
939 : parrello 1.10 Returns a hash mapping each attribute key name to its data type, description, and
940 :     parent groups.
941 : parrello 1.4
942 :     =back
943 :    
944 :     =cut
945 :    
946 : parrello 1.10 sub GetAttributeData {
947 : parrello 1.4 # Get the parameters.
948 : parrello 1.10 my ($self, $type, @list) = @_;
949 :     # Set up a hash to store the attribute data.
950 :     my %retVal = ();
951 :     # Loop through the list items.
952 :     for my $item (@list) {
953 :     # Set up a query for the desired attributes.
954 :     my $query;
955 :     if ($type eq 'name') {
956 :     # Here we're doing a generic name search. We need to escape it and then tack
957 :     # on a %.
958 :     my $parm = $item;
959 :     $parm =~ s/_/\\_/g;
960 :     $parm =~ s/%/\\%/g;
961 :     $parm .= "%";
962 :     # Ask for matching attributes. (Note that if the user passed in a null string
963 :     # he'll get everything.)
964 :     $query = $self->Get(['AttributeKey'], "AttributeKey(id) LIKE ?", [$parm]);
965 :     } elsif ($type eq 'group') {
966 :     $query = $self->Get(['IsInGroup', 'AttributeKey'], "IsInGroup(to-link) = ?", [$item]);
967 : parrello 1.4 } else {
968 : parrello 1.10 Confess("Unknown attribute query type \"$type\".");
969 :     }
970 :     while (my $row = $query->Fetch()) {
971 :     # Get this attribute's data.
972 :     my ($key, $type, $notes) = $row->Values(['AttributeKey(id)', 'AttributeKey(data-type)',
973 :     'AttributeKey(description)']);
974 :     # If it's new, get its groups and add it to the return hash.
975 :     if (! exists $retVal{$key}) {
976 :     my @groups = $self->GetFlat(['IsInGroup'], "IsInGroup(from-link) = ?",
977 :     [$key], 'IsInGroup(to-link)');
978 :     $retVal{$key} = [$type, $notes, @groups];
979 : parrello 1.4 }
980 :     }
981 :     }
982 :     # Return the result.
983 : parrello 1.10 return %retVal;
984 : parrello 1.4 }
985 :    
986 : parrello 1.3 =head2 FIG Method Replacements
987 :    
988 :     The following methods are used by B<FIG.pm> to replace the previous attribute functionality.
989 : parrello 1.10 Some of the old functionality is no longer present: controlled vocabulary is no longer
990 : parrello 1.3 supported and there is no longer any searching by URL. Fortunately, neither of these
991 :     capabilities were used in the old system.
992 :    
993 : parrello 1.4 The methods here are the only ones supported by the B<RemoteCustomAttributes> object.
994 :     The idea is that these methods represent attribute manipulation allowed by all users, while
995 :     the others are only for privileged users with access to the attribute server.
996 :    
997 : parrello 1.3 In the previous implementation, an attribute had a value and a URL. In the new implementation,
998 :     there is only a value. In this implementation, each attribute has only a value. These
999 :     methods will treat the value as a list with the individual elements separated by the
1000 :     value of the splitter parameter on the constructor (L</new>). The default is double
1001 :     colons C<::>.
1002 :    
1003 : parrello 1.10 So, for example, an old-style keyword with a value of C<essential> and a URL of
1004 : parrello 1.3 C<http://www.sciencemag.org/cgi/content/abstract/293/5538/2266> using the default
1005 :     splitter value would be stored as
1006 :    
1007 :     essential::http://www.sciencemag.org/cgi/content/abstract/293/5538/2266
1008 :    
1009 :     The best performance is achieved by searching for a particular key for a specified
1010 :     feature or genome.
1011 :    
1012 :     =head3 GetAttributes
1013 :    
1014 : parrello 1.10 C<< my @attributeList = $attrDB->GetAttributes($objectID, $key, @values); >>
1015 : parrello 1.3
1016 :     In the database, attribute values are sectioned into pieces using a splitter
1017 :     value specified in the constructor (L</new>). This is not a requirement of
1018 :     the attribute system as a whole, merely a convenience for the purpose of
1019 : parrello 1.10 these methods. If a value has multiple sections, each section
1020 :     is matched against the corresponding criterion in the I<@valuePatterns> list.
1021 : parrello 1.3
1022 :     This method returns a series of tuples that match the specified criteria. Each tuple
1023 :     will contain an object ID, a key, and one or more values. The parameters to this
1024 : parrello 1.10 method therefore correspond structurally to the values expected in each tuple. In
1025 :     addition, you can ask for a generic search by suffixing a percent sign (C<%>) to any
1026 :     of the parameters. So, for example,
1027 : parrello 1.3
1028 : parrello 1.10 my @attributeList = $attrDB->GetAttributes('fig|100226.1.peg.1004', 'structure%', 1, 2);
1029 : parrello 1.3
1030 :     would return something like
1031 :    
1032 :     ['fig}100226.1.peg.1004', 'structure', 1, 2]
1033 :     ['fig}100226.1.peg.1004', 'structure1', 1, 2]
1034 :     ['fig}100226.1.peg.1004', 'structure2', 1, 2]
1035 :     ['fig}100226.1.peg.1004', 'structureA', 1, 2]
1036 :    
1037 : parrello 1.10 Use of C<undef> in any position acts as a wild card (all values). You can also specify
1038 :     a list reference in the ID column. Thus,
1039 :    
1040 :     my @attributeList = $attrDB->GetAttributes(['100226.1', 'fig|100226.1.%'], 'PUBMED');
1041 :    
1042 :     would get the PUBMED attribute data for Streptomyces coelicolor A3(2) and all its
1043 :     features.
1044 : parrello 1.3
1045 :     In addition to values in multiple sections, a single attribute key can have multiple
1046 :     values, so even
1047 :    
1048 : parrello 1.10 my @attributeList = $attrDB->GetAttributes($peg, 'virulent');
1049 : parrello 1.3
1050 :     which has no wildcard in the key or the object ID, may return multiple tuples.
1051 :    
1052 : parrello 1.10 Value matching in this system works very poorly, because of the way multiple values are
1053 :     stored. For the object ID and key name, we create queries that filter for the desired
1054 :     results. For the values, we do a comparison after the attributes are retrieved from the
1055 :     database. As a result, queries in which filter only on value end up reading the entire
1056 :     attribute table to find the desired results.
1057 : parrello 1.3
1058 :     =over 4
1059 :    
1060 :     =item objectID
1061 :    
1062 : parrello 1.10 ID of object whose attributes are desired. If the attributes are desired for multiple
1063 :     objects, this parameter can be specified as a list reference. If the attributes are
1064 :     desired for all objects, specify C<undef> or an empty string. Finally, you can specify
1065 :     attributes for a range of object IDs by putting a percent sign (C<%>) at the end.
1066 : parrello 1.3
1067 :     =item key
1068 :    
1069 : parrello 1.10 Attribute key name. A value of C<undef> or an empty string will match all
1070 :     attribute keys. If the values are desired for multiple keys, this parameter can be
1071 :     specified as a list reference. Finally, you can specify attributes for a range of
1072 :     keys by putting a percent sign (C<%>) at the end.
1073 : parrello 1.3
1074 : parrello 1.10 =item values
1075 : parrello 1.3
1076 :     List of the desired attribute values, section by section. If C<undef>
1077 : parrello 1.10 or an empty string is specified, all values in that section will match. A
1078 :     generic match can be requested by placing a percent sign (C<%>) at the end.
1079 :     In that case, all values that match up to and not including the percent sign
1080 : parrello 1.14 will match. You may also specify a regular expression enclosed
1081 :     in slashes. All values that match the regular expression will be returned. For
1082 :     performance reasons, only values have this extra capability.
1083 : parrello 1.3
1084 :     =item RETURN
1085 :    
1086 :     Returns a list of tuples. The first element in the tuple is an object ID, the
1087 :     second is an attribute key, and the remaining elements are the sections of
1088 :     the attribute value. All of the tuples will match the criteria set forth in
1089 :     the parameter list.
1090 :    
1091 :     =back
1092 :    
1093 :     =cut
1094 :    
1095 :     sub GetAttributes {
1096 : parrello 1.4 # Get the parameters.
1097 : parrello 1.10 my ($self, $objectID, $key, @values) = @_;
1098 :     # We will create one big honking query. The following hash will build the filter
1099 :     # clause and a parameter list.
1100 : parrello 1.11 my %data = ('HasValueFor(from-link)' => $key, 'HasValueFor(to-link)' => $objectID);
1101 : parrello 1.10 my @filter = ();
1102 :     my @parms = ();
1103 :     # This next loop goes through the different fields that can be specified in the
1104 :     # parameter list and generates filters for each.
1105 :     for my $field (keys %data) {
1106 :     # Accumulate filter information for this field. We will OR together all the
1107 :     # elements accumulated to create the final result.
1108 :     my @fieldFilter = ();
1109 :     # Get the specified data from the caller.
1110 :     my $fieldPattern = $data{$field};
1111 :     # Only proceed if the pattern is one that won't match everything.
1112 :     if (defined($fieldPattern) && $fieldPattern ne "" && $fieldPattern ne "%") {
1113 :     # Convert the pattern to an array.
1114 :     my @patterns = ();
1115 :     if (ref $fieldPattern eq 'ARRAY') {
1116 :     push @patterns, @{$fieldPattern};
1117 :     } else {
1118 :     push @patterns, $fieldPattern;
1119 :     }
1120 :     # Only proceed if the array is nonempty. The loop will work fine if the
1121 :     # array is empty, but when we build the filter string at the end we'll
1122 :     # get "()" in the filter list, which will result in an SQL syntax error.
1123 :     if (@patterns) {
1124 :     # Loop through the individual patterns.
1125 :     for my $pattern (@patterns) {
1126 :     # Check for a generic request.
1127 :     if (substr($pattern, -1, 1) ne '%') {
1128 :     # Here we have a normal request.
1129 :     push @fieldFilter, "$field = ?";
1130 :     push @parms, $pattern;
1131 :     } else {
1132 :     # Here we have a generate request, so we will use the LIKE operator to
1133 :     # filter the field to this value pattern.
1134 :     push @fieldFilter, "$field LIKE ?";
1135 :     # We must convert the pattern value to an SQL match pattern. First
1136 : parrello 1.11 # we get a copy of it.
1137 :     my $actualPattern = $pattern;
1138 : parrello 1.10 # Now we escape the underscores. Underscores are an SQL wild card
1139 :     # character, but they are used frequently in key names and object IDs.
1140 : parrello 1.11 $actualPattern =~ s/_/\\_/g;
1141 : parrello 1.10 # Add the escaped pattern to the bound parameter list.
1142 :     push @parms, $actualPattern;
1143 :     }
1144 :     }
1145 :     # Form the filter for this field.
1146 :     my $fieldFilterString = join(" OR ", @fieldFilter);
1147 :     push @filter, "($fieldFilterString)";
1148 :     }
1149 :     }
1150 :     }
1151 :     # Now @filter contains one or more filter strings and @parms contains the parameter
1152 :     # values to bind to them.
1153 :     my $actualFilter = join(" AND ", @filter);
1154 : parrello 1.3 # Declare the return variable.
1155 :     my @retVal = ();
1156 : parrello 1.10 # Get the number of value sections we have to match.
1157 :     my $sectionCount = scalar(@values);
1158 :     # Now we're ready to make our query.
1159 : parrello 1.11 my $query = $self->Get(['HasValueFor'], $actualFilter, \@parms);
1160 : parrello 1.10 # Loop through the assignments found.
1161 :     while (my $row = $query->Fetch()) {
1162 :     # Get the current row's data.
1163 : parrello 1.11 my ($id, $key, $valueString) = $row->Values(['HasValueFor(to-link)', 'HasValueFor(from-link)',
1164 :     'HasValueFor(value)']);
1165 :     # Break the value into sections.
1166 :     my @sections = split($self->{splitter}, $valueString);
1167 :     # Match each section against the incoming values. We'll assume we're
1168 :     # okay unless we learn otherwise.
1169 :     my $matching = 1;
1170 :     for (my $i = 0; $i < $sectionCount && $matching; $i++) {
1171 :     # We need to check to see if this section is generic.
1172 : parrello 1.14 my $value = $values[$i];
1173 :     Trace("Current value pattern is \"$value\".") if T(4);
1174 :     if (substr($value, -1, 1) eq '%') {
1175 :     Trace("Generic match used.") if T(4);
1176 :     # Here we have a generic match.
1177 : parrello 1.11 my $matchLen = length($values[$i] - 1);
1178 :     $matching = substr($sections[$i], 0, $matchLen) eq
1179 :     substr($values[$i], 0, $matchLen);
1180 : parrello 1.14 } elsif ($value =~ m#^/(.+)/[a-z]*$#) {
1181 :     Trace("Regular expression detected.") if T(4);
1182 :     # Here we have a regular expression match.
1183 :     my $section = $sections[$i];
1184 :     $matching = eval("\$section =~ $value");
1185 : parrello 1.11 } else {
1186 : parrello 1.14 # Here we have a strict match.
1187 :     Trace("Strict match used.") if T(4);
1188 : parrello 1.11 $matching = ($sections[$i] eq $values[$i]);
1189 : parrello 1.10 }
1190 : parrello 1.11 }
1191 :     # If we match, output this row to the return list.
1192 :     if ($matching) {
1193 :     push @retVal, [$id, $key, @sections];
1194 : parrello 1.3 }
1195 :     }
1196 : parrello 1.10 # Return the rows found.
1197 : parrello 1.3 return @retVal;
1198 :     }
1199 :    
1200 :     =head3 AddAttribute
1201 :    
1202 :     C<< $attrDB->AddAttribute($objectID, $key, @values); >>
1203 :    
1204 :     Add an attribute key/value pair to an object. This method cannot add a new key, merely
1205 :     add a value to an existing key. Use L</StoreAttributeKey> to create a new key.
1206 :    
1207 :     =over 4
1208 :    
1209 :     =item objectID
1210 :    
1211 : parrello 1.10 ID of the object to which the attribute is to be added.
1212 : parrello 1.3
1213 :     =item key
1214 :    
1215 : parrello 1.10 Attribute key name.
1216 : parrello 1.3
1217 :     =item values
1218 :    
1219 :     One or more values to be associated with the key. The values are joined together with
1220 :     the splitter value before being stored as field values. This enables L</GetAttributes>
1221 :     to split them apart during retrieval. The splitter value defaults to double colons C<::>.
1222 :    
1223 :     =back
1224 :    
1225 :     =cut
1226 :    
1227 :     sub AddAttribute {
1228 :     # Get the parameters.
1229 : parrello 1.4 my ($self, $objectID, $key, @values) = @_;
1230 : parrello 1.3 # Don't allow undefs.
1231 :     if (! defined($objectID)) {
1232 :     Confess("No object ID specified for AddAttribute call.");
1233 :     } elsif (! defined($key)) {
1234 :     Confess("No attribute key specified for AddAttribute call.");
1235 :     } elsif (! @values) {
1236 :     Confess("No values specified in AddAttribute call for key $key.");
1237 :     } else {
1238 : parrello 1.11 # Okay, now we have some reason to believe we can do this. Form the values
1239 :     # into a scalar.
1240 : parrello 1.3 my $valueString = join($self->{splitter}, @values);
1241 : parrello 1.11 # Connect the object to the key.
1242 :     $self->InsertObject('HasValueFor', { 'from-link' => $key,
1243 :     'to-link' => $objectID,
1244 :     'value' => $valueString,
1245 :     });
1246 : parrello 1.3 }
1247 : parrello 1.10 # Return a one, indicating success. We do this for backward compatability.
1248 : parrello 1.3 return 1;
1249 :     }
1250 :    
1251 :     =head3 DeleteAttribute
1252 :    
1253 :     C<< $attrDB->DeleteAttribute($objectID, $key, @values); >>
1254 :    
1255 :     Delete the specified attribute key/value combination from the database.
1256 :    
1257 :     =over 4
1258 :    
1259 :     =item objectID
1260 :    
1261 : parrello 1.10 ID of the object whose attribute is to be deleted.
1262 : parrello 1.3
1263 :     =item key
1264 :    
1265 : parrello 1.10 Attribute key name.
1266 : parrello 1.3
1267 :     =item values
1268 :    
1269 : parrello 1.10 One or more values associated with the key. If no values are specified, then all values
1270 :     will be deleted. Otherwise, only a matching value will be deleted.
1271 : parrello 1.3
1272 :     =back
1273 :    
1274 :     =cut
1275 :    
1276 :     sub DeleteAttribute {
1277 :     # Get the parameters.
1278 : parrello 1.4 my ($self, $objectID, $key, @values) = @_;
1279 : parrello 1.3 # Don't allow undefs.
1280 :     if (! defined($objectID)) {
1281 :     Confess("No object ID specified for DeleteAttribute call.");
1282 :     } elsif (! defined($key)) {
1283 :     Confess("No attribute key specified for DeleteAttribute call.");
1284 : parrello 1.11 } elsif (scalar(@values) == 0) {
1285 :     # Here we erase the entire key.
1286 :     $self->EraseAttribute($key);
1287 : parrello 1.3 } else {
1288 : parrello 1.11 # Here we erase the matching values.
1289 :     my $valueString = join($self->{splitter}, @values);
1290 :     $self->DeleteRow('HasValueFor', $key, $objectID, { value => $valueString });
1291 : parrello 1.3 }
1292 :     # Return a one. This is for backward compatability.
1293 :     return 1;
1294 :     }
1295 :    
1296 :     =head3 ChangeAttribute
1297 :    
1298 :     C<< $attrDB->ChangeAttribute($objectID, $key, \@oldValues, \@newValues); >>
1299 :    
1300 :     Change the value of an attribute key/value pair for an object.
1301 :    
1302 :     =over 4
1303 :    
1304 :     =item objectID
1305 :    
1306 :     ID of the genome or feature to which the attribute is to be changed. In general, an ID that
1307 :     starts with C<fig|> is treated as a feature ID, and an ID that is all digits and periods
1308 :     is treated as a genome ID. For IDs of other types, this parameter should be a reference
1309 :     to a 2-tuple consisting of the entity type name followed by the object ID.
1310 :    
1311 :     =item key
1312 :    
1313 :     Attribute key name. This corresponds to the name of a field in the database.
1314 :    
1315 :     =item oldValues
1316 :    
1317 :     One or more values identifying the key/value pair to change.
1318 :    
1319 :     =item newValues
1320 :    
1321 :     One or more values to be put in place of the old values.
1322 :    
1323 :     =back
1324 :    
1325 :     =cut
1326 :    
1327 :     sub ChangeAttribute {
1328 :     # Get the parameters.
1329 : parrello 1.4 my ($self, $objectID, $key, $oldValues, $newValues) = @_;
1330 : parrello 1.3 # Don't allow undefs.
1331 :     if (! defined($objectID)) {
1332 :     Confess("No object ID specified for ChangeAttribute call.");
1333 :     } elsif (! defined($key)) {
1334 :     Confess("No attribute key specified for ChangeAttribute call.");
1335 :     } elsif (! defined($oldValues) || ref $oldValues ne 'ARRAY') {
1336 :     Confess("No old values specified in ChangeAttribute call for key $key.");
1337 :     } elsif (! defined($newValues) || ref $newValues ne 'ARRAY') {
1338 :     Confess("No new values specified in ChangeAttribute call for key $key.");
1339 :     } else {
1340 : parrello 1.10 # We do the change as a delete/add.
1341 : parrello 1.3 $self->DeleteAttribute($objectID, $key, @{$oldValues});
1342 :     $self->AddAttribute($objectID, $key, @{$newValues});
1343 :     }
1344 :     # Return a one. We do this for backward compatability.
1345 :     return 1;
1346 :     }
1347 :    
1348 : parrello 1.7 =head3 EraseAttribute
1349 :    
1350 : parrello 1.11 C<< $attrDB->EraseAttribute($key); >>
1351 : parrello 1.7
1352 :     Erase all values for the specified attribute key. This does not remove the
1353 :     key from the database; it merely removes all the values.
1354 :    
1355 :     =over 4
1356 :    
1357 :     =item key
1358 :    
1359 :     Key to erase.
1360 :    
1361 :     =back
1362 :    
1363 :     =cut
1364 :    
1365 :     sub EraseAttribute {
1366 :     # Get the parameters.
1367 : parrello 1.10 my ($self, $key) = @_;
1368 :     # Delete everything connected to the key. The "keepRoot" option keeps the key in the
1369 :     # datanase while deleting everything attached to it.
1370 :     $self->Delete('AttributeKey', $key, keepRoot => 1);
1371 : parrello 1.7 # Return a 1, for backward compatability.
1372 :     return 1;
1373 :     }
1374 :    
1375 : parrello 1.9 =head3 GetAttributeKeys
1376 :    
1377 : parrello 1.10 C<< my @keyList = $attrDB->GetAttributeKeys($groupName); >>
1378 : parrello 1.9
1379 : parrello 1.10 Return a list of the attribute keys for a particular group.
1380 : parrello 1.9
1381 :     =over 4
1382 :    
1383 : parrello 1.10 =item groupName
1384 : parrello 1.9
1385 : parrello 1.10 Name of the group whose keys are desired.
1386 : parrello 1.9
1387 :     =item RETURN
1388 :    
1389 : parrello 1.10 Returns a list of the attribute keys for the specified group.
1390 : parrello 1.9
1391 :     =back
1392 :    
1393 :     =cut
1394 :    
1395 :     sub GetAttributeKeys {
1396 :     # Get the parameters.
1397 : parrello 1.10 my ($self, $groupName) = @_;
1398 :     # Get the attributes for the specified group.
1399 :     my @groups = $self->GetFlat(['IsInGroup'], "IsInGroup(to-link) = ?", [$groupName],
1400 :     'IsInGroup(from-link)');
1401 : parrello 1.9 # Return the keys.
1402 : parrello 1.10 return sort @groups;
1403 : parrello 1.9 }
1404 :    
1405 : parrello 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3