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

Diff of /Sprout/CustomAttributes.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4, Fri Nov 10 21:00:14 2006 UTC revision 1.9, Thu Nov 16 22:09:33 2006 UTC
# Line 20  Line 20 
20    
21  The full suite of ERDB retrieval capabilities is provided. In addition,  The full suite of ERDB retrieval capabilities is provided. In addition,
22  custom methods are provided specific to this application. To get all  custom methods are provided specific to this application. To get all
23  the values of the attribute C<essential> in the B<Feature> entity, you  the values of the attribute C<essential> in a specified B<Feature>, you
24  would code  would code
25    
26      my @values = $attrDB->GetAttributeValues($fid, Feature => 'essential');      my @values = $attrDB->GetAttributes([Feature => $fid], 'essential');
27    
28  where I<$fid> contains the ID of the desired feature. Each attribute has  where I<$fid> contains the ID of the desired feature. Each attribute has
29  an alternate index to allow searching for attributes by value.  an alternate index to allow searching for attributes by value.
# Line 76  Line 76 
76    
77  =back  =back
78    
79    The DBD file is critical, and must have reasonable contents before we can
80    begin using the system. In the old system, attributes were only provided
81    for Genomes and Features, so the initial XML file was the following.
82    
83        <Database>
84          <Title>SEED Custom Attribute Database</Title>
85          <Entities>
86            <Entity name="Feature" keyType="id-string">
87              <Notes>A [i]feature[/i] is a part of the genome
88              that is of special interest. Features may be spread
89              across multiple contigs of a genome, but never across
90              more than one genome. Features can be assigned to roles
91              via spreadsheet cells, and are the targets of
92              annotation.</Notes>
93            </Entity>
94            <Entity name="Genome" keyType="name-string">
95              <Notes>A [i]genome[/i] describes a particular individual
96              organism's DNA.</Notes>
97            </Entity>
98          </Entities>
99        </Database>
100    
101    It is not necessary to put any tables into the database; however, you should
102    run
103    
104        AttrDBRefresh
105    
106    periodically to insure it has the correct Genomes and Features in it. When
107    converting from the old system, use
108    
109        AttrDBRefresh -migrate
110    
111    to initialize the database and migrate the legacy data. You should only need
112    to do that once.
113    
114  =head2 Implementation Note  =head2 Implementation Note
115    
116  The L</Refresh> method reloads the entities in the database. If new  The L</Refresh> method reloads the entities in the database. If new
# Line 162  Line 197 
197  sub StoreAttributeKey {  sub StoreAttributeKey {
198      # Get the parameters.      # Get the parameters.
199      my ($entityName, $attributeName, $type, $notes) = @_;      my ($entityName, $attributeName, $type, $notes) = @_;
200        # Declare the return variable.
201        my $retVal;
202      # Get the data type hash.      # Get the data type hash.
203      my %types = ERDB::GetDataTypes();      my %types = ERDB::GetDataTypes();
204      # Validate the initial input values.      # Validate the initial input values.
# Line 182  Line 219 
219          # Okay, we're ready to begin. Get the entity hash and the field hash.          # Okay, we're ready to begin. Get the entity hash and the field hash.
220          my $entityData = $entityHash->{$entityName};          my $entityData = $entityHash->{$entityName};
221          my $fieldHash = ERDB::GetEntityFieldHash($metadata, $entityName);          my $fieldHash = ERDB::GetEntityFieldHash($metadata, $entityName);
222            # Compare the old attribute data to the new data.
223            my $bigChange = 1;
224            if (exists $fieldHash->{$attributeName} && $fieldHash->{$attributeName}->{type} eq $type) {
225                $bigChange = 0;
226            }
227          # Compute the attribute's relation name.          # Compute the attribute's relation name.
228          my $relName = join("", $entityName, map { ucfirst $_ } split(/-|_/, $attributeName));          my $relName = join("", $entityName, map { ucfirst $_ } split(/-|_/, $attributeName));
229          # Store the attribute's field data. Note the use of the "content" hash for          # Store the attribute's field data. Note the use of the "content" hash for
# Line 197  Line 239 
239          }          }
240          # Write the XML back out.          # Write the XML back out.
241          ERDB::WriteMetaXML($metadata, $FIG_Config::attrDBD);          ERDB::WriteMetaXML($metadata, $FIG_Config::attrDBD);
     }  
242      # Open a database with the new XML.      # Open a database with the new XML.
243      my $retVal = CustomAttributes->new();          $retVal = CustomAttributes->new();
244            # Create the table if there has been a significant change.
245            if ($bigChange) {
246                $retVal->CreateTable($relName);
247            }
248        }
249      return $retVal;      return $retVal;
250  }  }
251    
# Line 471  Line 517 
517                                                      -default => 1)                                                      -default => 1)
518                                     ),                                     ),
519                            );                            );
520      # Now the two buttons: UPDATE and DELETE.      # Now the three buttons: UPDATE, SHOW, and DELETE.
521      push @retVal, $cgi->Tr($cgi->th("&nbsp;"),      push @retVal, $cgi->Tr($cgi->th("&nbsp;"),
522                             $cgi->td({align => 'center'},                             $cgi->td({align => 'center'},
523                                      $cgi->submit(-name => 'Delete', -value => 'DELETE') . " " .                                      $cgi->submit(-name => 'Delete', -value => 'DELETE') . " " .
524                                      $cgi->submit(-name => 'Store',  -value => 'STORE')                                      $cgi->submit(-name => 'Store',  -value => 'STORE') . " " .
525                                        $cgi->submit(-name => 'Show',   -value => 'SHOW')
526                                     )                                     )
527                            );                            );
528      # Close the table and the form.      # Close the table and the form.
# Line 819  Line 866 
866                  # Get the key, value, and URL. We ignore the first element because that's the                  # Get the key, value, and URL. We ignore the first element because that's the
867                  # object ID, and we already know the object ID.                  # object ID, and we already know the object ID.
868                  my (undef, $key, $value, $url) = @{$dataTuple};                  my (undef, $key, $value, $url) = @{$dataTuple};
869                    # Remove the buggy "1" for $url.
870                    if ($url eq "1") {
871                        $url = undef;
872                    }
873                  # Only proceed if this is not an old key.                  # Only proceed if this is not an old key.
874                  if (! $myOldKeys->{$key}) {                  if (! $myOldKeys->{$key}) {
875                      # See if we've run into this key before.                      # See if we've run into this key before.
# Line 1075  Line 1126 
1126          # MatchSqlPattern method          # MatchSqlPattern method
1127          my %secondaries = $self->GetSecondaryFields($entityType);          my %secondaries = $self->GetSecondaryFields($entityType);
1128          my @fieldList = grep { MatchSqlPattern($_, $key) } keys %secondaries;          my @fieldList = grep { MatchSqlPattern($_, $key) } keys %secondaries;
1129          # Now we figure out whether or not we need to filter by object.          # Now we figure out whether or not we need to filter by object. We will always
1130            # filter by key to a limited extent, so if we're filtering by object we need an
1131            # AND to join the object ID filter with the key filter.
1132          my $filter = "";          my $filter = "";
1133          my @params = ();          my @params = ();
1134          if (! $actualObjectID) {          if (defined($actualObjectID)) {
1135              # Here the caller wants to filter on object ID.              # Here the caller wants to filter on object ID. Check for a pattern.
1136              $filter = "$entityType(id) = ?";              my $comparator = ($actualObjectID =~ /%/ ? "LIKE" : "=");
1137                # Update the filter and the parameter list.
1138                $filter = "$entityType(id) $comparator ? AND ";
1139              push @params, $actualObjectID;              push @params, $actualObjectID;
1140          }          }
1141          # It's time to begin making queries. We process one attribute key at a time, because          # It's time to begin making queries. We process one attribute key at a time, because
# Line 1089  Line 1144 
1144          # the DBD. That's a good thing, because an invalid key name will cause an SQL error.          # the DBD. That's a good thing, because an invalid key name will cause an SQL error.
1145          for my $key (@fieldList) {          for my $key (@fieldList) {
1146              # Get all of the attribute values for this key.              # Get all of the attribute values for this key.
1147              my @dataRows = $self->GetAll([$entityType], $filter, \@params,              my @dataRows = $self->GetAll([$entityType], "$filter$entityType($key) IS NOT NULL",
1148                                           ["$entityType(id)", "$entityType($key)"]);                                           \@params, ["$entityType(id)", "$entityType($key)"]);
1149              # Process each value separately. We need to verify the values and reformat the              # Process each value separately. We need to verify the values and reformat the
1150              # tuples. Note that GetAll will give us one row per matching object ID,              # tuples. Note that GetAll will give us one row per matching object ID,
1151              # with the ID first followed by a list of the data values. This is very              # with the ID first followed by a list of the data values. This is very
# Line 1286  Line 1341 
1341      return 1;      return 1;
1342  }  }
1343    
1344    =head3 EraseAttribute
1345    
1346    C<< $attrDB->EraseAttribute($entityName, $key); >>
1347    
1348    Erase all values for the specified attribute key. This does not remove the
1349    key from the database; it merely removes all the values.
1350    
1351    =over 4
1352    
1353    =item entityName
1354    
1355    Name of the entity to which the key belongs. If undefined, all entities will be
1356    examined for the desired key.
1357    
1358    =item key
1359    
1360    Key to erase.
1361    
1362    =back
1363    
1364    =cut
1365    
1366    sub EraseAttribute {
1367        # Get the parameters.
1368        my ($self, $entityName, $key) = @_;
1369        # Determine the relevant entity types.
1370        my @objects = ();
1371        if (! $entityName) {
1372            push @objects, $self->GetEntityTypes();
1373        } else {
1374            push @objects, $entityName;
1375        }
1376        # Loop through the entity types.
1377        for my $entityType (@objects) {
1378            # Now check for this key in this entity.
1379            my %secondaries = $self->GetSecondaryFields($entityType);
1380            if (exists $secondaries{$key}) {
1381                # We found it, so delete all the values of the key.
1382                $self->DeleteValue($entityType, undef, $key);
1383            }
1384        }
1385        # Return a 1, for backward compatability.
1386        return 1;
1387    }
1388    
1389    =head3 GetAttributeKeys
1390    
1391    C<< my @keyList = $attrDB->GetAttributeKeys($entityName); >>
1392    
1393    Return a list of the attribute keys for a particular entity type.
1394    
1395    =over 4
1396    
1397    =item entityName
1398    
1399    Name of the entity whose keys are desired.
1400    
1401    =item RETURN
1402    
1403    Returns a list of the attribute keys for the specified entity.
1404    
1405    =back
1406    
1407    =cut
1408    
1409    sub GetAttributeKeys {
1410        # Get the parameters.
1411        my ($self, $entityName) = @_;
1412        # Get the entity's secondary fields.
1413        my %keyList = $self->GetSecondaryFields($entityName);
1414        # Return the keys.
1415        return sort keys %keyList;
1416    }
1417    
1418  1;  1;

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.9

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3