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

Diff of /Sprout/Sprout.pm

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

revision 1.14, Thu Jun 9 19:06:55 2005 UTC revision 1.20, Wed Aug 24 19:31:20 2005 UTC
# Line 86  Line 86 
86          # Compute the options. We do this by starting with a table of defaults and overwriting with          # Compute the options. We do this by starting with a table of defaults and overwriting with
87          # the incoming data.          # the incoming data.
88          my $optionTable = Tracer::GetOptions({          my $optionTable = Tracer::GetOptions({
89                                             dbType               => 'mysql',                     # database type                         dbType       => $FIG_Config::dbms,
90                                             dataDir              => 'Data',                      # data file directory                                                          # database type
91                                             xmlFileName  => 'SproutDBD.xml', # database definition file name                         dataDir      => $FIG_Config::sproutData,
92                                             userData             => 'root/',                     # user name and password                                                          # data file directory
93                                             port                 => 0,                           # database connection port                         xmlFileName  => "$FIG_Config::sproutData/SproutDBD.xml",
94                                                            # database definition file name
95                           userData     => "$FIG_Config::dbuser/$FIG_Config::dbpass",
96                                                            # user name and password
97                           port         => $FIG_Config::dbport,
98                                                            # database connection port
99                                             maxSegmentLength => 4500,            # maximum feature segment length                                             maxSegmentLength => 4500,            # maximum feature segment length
100                                             maxSequenceLength => 8000,           # maximum contig sequence length                                             maxSequenceLength => 8000,           # maximum contig sequence length
101                                            }, $options);                                            }, $options);
# Line 420  Line 425 
425          # Loop through the incoming table names.          # Loop through the incoming table names.
426          for my $tableName (@{$tableList}) {          for my $tableName (@{$tableList}) {
427                  # Find the table's file.                  # Find the table's file.
428                  my $fileName = "$dataDir/$tableName";          my $fileName = LoadFileName($dataDir, $tableName);
429                  if (! -e $fileName) {          if (! $fileName) {
430                          $fileName = "$fileName.dtx";              Trace("No load file found for $tableName in $dataDir.") if T(0);
431                  }          } else {
432                  # Attempt to load this table.                  # Attempt to load this table.
433                  my $result = $erdb->LoadTable($fileName, $tableName, $truncateFlag);                  my $result = $erdb->LoadTable($fileName, $tableName, $truncateFlag);
434                  # Accumulate the resulting statistics.                  # Accumulate the resulting statistics.
435                  $retVal->Accumulate($result);                  $retVal->Accumulate($result);
436          }          }
437        }
438          # Return the statistics.          # Return the statistics.
439          return $retVal;          return $retVal;
440  }  }
# Line 570  Line 576 
576  =item RETURN  =item RETURN
577    
578  Returns a list of the feature's contig segments. The locations are returned as a list in a list  Returns a list of the feature's contig segments. The locations are returned as a list in a list
579  context and as a space-delimited string in a scalar context.  context and as a comma-delimited string in a scalar context.
580    
581  =back  =back
582    
# Line 613  Line 619 
619                  push @retVal, "${contigID}_$beg$dir$len";                  push @retVal, "${contigID}_$beg$dir$len";
620          }          }
621          # Return the list in the format indicated by the context.          # Return the list in the format indicated by the context.
622          return (wantarray ? @retVal : join(' ', @retVal));      return (wantarray ? @retVal : join(',', @retVal));
623  }  }
624    
625  =head3 ParseLocation  =head3 ParseLocation
# Line 1230  Line 1236 
1236    
1237  =item RETURN  =item RETURN
1238    
1239  Returns a reference to a hash that maps the IDs of the incoming features to the IDs of  Returns a reference to a hash that maps the IDs of the incoming features to the best hits
1240  their best hits.  on the target genome.
1241    
1242  =back  =back
1243    
# Line 1259  Line 1265 
1265          return \%retVal;          return \%retVal;
1266  }  }
1267    
1268    =head3 SimList
1269    
1270    C<< my %similarities = $sprout->SimList($featureID, $count); >>
1271    
1272    Return a list of the similarities to the specified feature.
1273    
1274    Sprout does not support real similarities, so this method just returns the bidirectional
1275    best hits.
1276    
1277    =over 4
1278    
1279    =item featureID
1280    
1281    ID of the feature whose similarities are desired.
1282    
1283    =item count
1284    
1285    Maximum number of similar features to be returned, or C<0> to return them all.
1286    
1287    =back
1288    
1289    =cut
1290    #: Return Type %;
1291    sub SimList {
1292        # Get the parameters.
1293        my ($self, $featureID, $count) = @_;
1294        # Ask for the best hits.
1295        my @lists = $self->GetAll(['IsBidirectionalBestHitOf'],
1296                                  "IsBidirectionalBestHitOf(from-link) = ? ORDER BY IsBidirectionalBestHitOf(score) DESC",
1297                                  [$featureID], ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(score)'],
1298                                  $count);
1299        # Create the return value.
1300        my %retVal = ();
1301        for my $tuple (@lists) {
1302            $retVal{$tuple->[0]} = $tuple->[1];
1303        }
1304        # Return the result.
1305        return %retVal;
1306    }
1307    
1308    
1309    
1310    =head3 IsComplete
1311    
1312    C<< my $flag = $sprout->IsComplete($genomeID); >>
1313    
1314    Return TRUE if the specified genome is complete, else FALSE.
1315    
1316    =over 4
1317    
1318    =item genomeID
1319    
1320    ID of the genome whose completeness status is desired.
1321    
1322    =item RETURN
1323    
1324    Returns TRUE if the genome is complete, FALSE if it is incomplete, and C<undef> if it is
1325    not found.
1326    
1327    =back
1328    
1329    =cut
1330    #: Return Type $;
1331    sub IsComplete {
1332        # Get the parameters.
1333        my ($self, $genomeID) = @_;
1334        # Declare the return variable.
1335        my $retVal;
1336        # Get the genome's data.
1337        my $genomeData = $self->GetEntity('Genome', $genomeID);
1338        if ($genomeData) {
1339            # The genome exists, so get the completeness flag.
1340            ($retVal) = $genomeData->Value('complete');
1341        }
1342        # Return the result.
1343        return $retVal;
1344    }
1345    
1346  =head3 FeatureAliases  =head3 FeatureAliases
1347    
1348  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>
# Line 1378  Line 1462 
1462          return %retVal;          return %retVal;
1463  }  }
1464    
1465    =head3 CouplingEvidence
1466    
1467    C<< my @evidence = $sprout->CouplingEvidence($peg1, $peg2); >>
1468    
1469    Return the evidence for a functional coupling.
1470    
1471    A pair of features is considered evidence of a coupling between two other
1472    features if they occur close together on a contig and both are similar to
1473    the coupled features. So, if B<A1> and B<A2> are close together on a contig,
1474    B<B1> and B<B2> are considered evidence for the coupling if (1) B<B1> and
1475    B<B2> are close together, (2) B<B1> is similar to B<A1>, and (3) B<B2> is
1476    similar to B<A2>.
1477    
1478    The score of a coupling is determined by the number of pieces of evidence
1479    that are considered I<representative>. If several evidence items belong to
1480    a group of genomes that are close to each other, only one of those items
1481    is considered representative. The other evidence items are presumed to be
1482    there because of the relationship between the genomes rather than because
1483    the two proteins generated by the features have a related functionality.
1484    
1485    Each evidence item is returned as a three-tuple in the form C<[>I<$peg1a>C<,>
1486    I<$peg2a>C<,> I<$rep>C<]>, where I<$peg1a> is similar to I<$peg1>, I<$peg2a>
1487    is similar to I<$peg2>, and I<$rep> is TRUE if the evidence is representative
1488    and FALSE otherwise.
1489    
1490    =over 4
1491    
1492    =item peg1
1493    
1494    ID of the feature of interest.
1495    
1496    =item peg2
1497    
1498    ID of a feature functionally coupled to the feature of interest.
1499    
1500    =item RETURN
1501    
1502    Returns a list of 3-tuples. Each tuple consists of a feature similar to the feature
1503    of interest, a feature similar to the functionally coupled feature, and a flag
1504    that is TRUE for a representative piece of evidence and FALSE otherwise.
1505    
1506    =back
1507    
1508    =cut
1509    #: Return Type @@;
1510    sub CouplingEvidence {
1511        # Get the parameters.
1512        my ($self, $peg1, $peg2) = @_;
1513        # Declare the return variable.
1514        my @retVal = ();
1515        # Our first task is to find out the nature of the coupling: whether or not
1516        # it exists, its score, and whether the features are stored in the same
1517        # order as the ones coming in.
1518        my ($couplingID, $inverted, $score) = $self->GetCoupling($peg1, $peg2);
1519        # Only proceed if a coupling exists.
1520        if ($couplingID) {
1521            # Determine the ordering to place on the evidence items. If we're
1522            # inverted, we want to see feature 2 before feature 1 (descending); otherwise,
1523            # we want feature 1 before feature 2 (normal).
1524            my $ordering = ($inverted ? "DESC" : "");
1525            # Get the coupling evidence.
1526            my @evidenceList = $self->GetAll(['IsEvidencedBy', 'PCH', 'UsesAsEvidence'],
1527                                              "IsEvidencedBy(from-link) = ? ORDER BY PCH(id), UsesAsEvidence(pos) $ordering",
1528                                              [$couplingID],
1529                                              ['PCH(used)', 'UsesAsEvidence(to-link)']);
1530            # Loop through the evidence items. Each piece of evidence is represented by two
1531            # positions in the evidence list, one for each feature on the other side of the
1532            # evidence link. If at some point we want to generalize to couplings with
1533            # more than two positions, this section of code will need to be re-done.
1534            while (@evidenceList > 0) {
1535                my $peg1Data = shift @evidenceList;
1536                my $peg2Data = shift @evidenceList;
1537                push @retVal, [$peg1Data->[1], $peg2Data->[1], $peg1Data->[0]];
1538            }
1539        }
1540        # Return the result.
1541        return @retVal;
1542    }
1543    
1544    =head3 GetCoupling
1545    
1546    C<< my ($couplingID, $inverted, $score) = $sprout->GetCoupling($peg1, $peg2); >>
1547    
1548    Return the coupling (if any) for the specified pair of PEGs. If a coupling
1549    exists, we return the coupling ID along with an indicator of whether the
1550    coupling is stored as C<(>I<$peg1>C<, >I<$peg2>C<)> or C<(>I<$peg2>C<, >I<$peg1>C<)>.
1551    In the second case, we say the coupling is I<inverted>. The importance of an
1552    inverted coupling is that the PEGs in the evidence will appear in reverse order.
1553    
1554    =over 4
1555    
1556    =item peg1
1557    
1558    ID of the feature of interest.
1559    
1560    =item peg2
1561    
1562    ID of the potentially coupled feature.
1563    
1564    =item RETURN
1565    
1566    Returns a three-element list. The first element contains the database ID of
1567    the coupling. The second element is FALSE if the coupling is stored in the
1568    database in the caller specified order and TRUE if it is stored in the
1569    inverted order. The third element is the coupling's score. If the coupling
1570    does not exist, all three list elements will be C<undef>.
1571    
1572    =back
1573    
1574    =cut
1575    #: Return Type $%@;
1576    sub GetCoupling {
1577        # Get the parameters.
1578        my ($self, $peg1, $peg2) = @_;
1579        # Declare the return values. We'll start with the coupling ID and undefine the
1580        # flag and score until we have more information.
1581        my ($retVal, $inverted, $score) = (CouplingID($peg1, $peg2), undef, undef);
1582        # Find the coupling data.
1583        my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'],
1584                                     "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)",
1585                                     [$retVal], ["ParticipatesInCoupling(from-link)", "Coupling(score)"]);
1586        # Check to see if we found anything.
1587        if (!@pegs) {
1588            # No coupling, so undefine the return value.
1589            $retVal = undef;
1590        } else {
1591            # We have a coupling! Get the score and check for inversion.
1592            $score = $pegs[0]->[1];
1593            $inverted = ($pegs[0]->[0] eq $peg1);
1594        }
1595        # Return the result.
1596        return ($retVal, $inverted, $score);
1597    }
1598    
1599    =head3 CouplingID
1600    
1601    C<< my $couplingID = Sprout::CouplingID($peg1, $peg2); >>
1602    
1603    Return the coupling ID for a pair of feature IDs.
1604    
1605    The coupling ID is currently computed by joining the feature IDs in
1606    sorted order with a space. Client modules (that is, modules which
1607    use Sprout) should not, however, count on this always being the
1608    case. This method provides a way for abstracting the concept of a
1609    coupling ID. All that we know for sure about it is that it can be
1610    generated easily from the feature IDs and the order of the IDs
1611    in the parameter list does not matter (i.e. C<CouplingID("a1", "b1")>
1612    will have the same value as C<CouplingID("b1", "a1")>.
1613    
1614    =over 4
1615    
1616    =item peg1
1617    
1618    First feature of interest.
1619    
1620    =item peg2
1621    
1622    Second feature of interest.
1623    
1624    =item RETURN
1625    
1626    Returns the ID that would be used to represent a functional coupling of
1627    the two specified PEGs.
1628    
1629    =back
1630    
1631    =cut
1632    #: Return Type $;
1633    sub CouplingID {
1634        return join " ", sort @_;
1635    }
1636    
1637  =head3 GetEntityTypes  =head3 GetEntityTypes
1638    
1639  C<< my @entityList = $sprout->GetEntityTypes(); >>  C<< my @entityList = $sprout->GetEntityTypes(); >>
# Line 1572  Line 1828 
1828  The next statement inserts a C<HasProperty> relationship between feature C<fig|158879.1.peg.1> and  The next statement inserts a C<HasProperty> relationship between feature C<fig|158879.1.peg.1> and
1829  property C<4> with an evidence URL of C<http://seedu.uchicago.edu/query.cgi?article_id=142>.  property C<4> with an evidence URL of C<http://seedu.uchicago.edu/query.cgi?article_id=142>.
1830    
1831  C<< $sprout->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence = 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >>  C<< $sprout->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence => 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >>
1832    
1833  =over 4  =over 4
1834    
# Line 1970  Line 2226 
2226          return @retVal;          return @retVal;
2227  }  }
2228    
2229    =head3 GetProperties
2230    
2231    C<< my @list = $sprout->GetProperties($fid, $key, $value, $url); >>
2232    
2233    Return a list of the properties with the specified characteristics.
2234    
2235    Properties are arbitrary key-value pairs associated with a feature. (At some point they
2236    will also be associated with genomes.) A property value is represented by a 4-tuple of
2237    the form B<($fid, $key, $value, $url)>. These exactly correspond to the parameter
2238    
2239    =over 4
2240    
2241    =item fid
2242    
2243    ID of the feature possessing the property.
2244    
2245    =item key
2246    
2247    Name or key of the property.
2248    
2249    =item value
2250    
2251    Value of the property.
2252    
2253    =item url
2254    
2255    URL of the document that indicated the property should have this particular value, or an
2256    empty string if no such document exists.
2257    
2258    =back
2259    
2260    The parameters act as a filter for the desired data. Any non-null parameter will
2261    automatically match all the tuples returned. So, specifying just the I<$fid> will
2262    return all the properties of the specified feature; similarly, specifying the I<$key>
2263    and I<$value> parameters will return all the features having the specified property
2264    value.
2265    
2266    A single property key can have many values, representing different ideas about the
2267    feature in question. For example, one paper may declare that a feature C<fig|83333.1.peg.10> is
2268    virulent, and another may declare that it is not virulent. A query about the virulence of
2269    C<fig|83333.1.peg.10> would be coded as
2270    
2271        my @list = $sprout->GetProperties('fig|83333.1.peg.10', 'virulence', '', '');
2272    
2273    Here the I<$value> and I<$url> fields are left blank, indicating that those fields are
2274    not to be filtered. The tuples returned would be
2275    
2276        ('fig|83333.1.peg.10', 'virulence', 'yes', 'http://www.somewhere.edu/first.paper.pdf')
2277        ('fig|83333.1.peg.10', 'virulence', 'no', 'http://www.somewhere.edu/second.paper.pdf')
2278    
2279    =cut
2280    #: Return Type @@;
2281    sub GetProperties {
2282        # Get the parameters.
2283        my ($self, @parms) = @_;
2284        # Declare the return variable.
2285        my @retVal = ();
2286        # Now we need to create a WHERE clause that will get us the data we want. First,
2287        # we create a list of the columns containing the data for each parameter.
2288        my @colNames = ('HasProperty(from-link)', 'Property(property-name)',
2289                        'Property(property-value)', 'HasProperty(evidence)');
2290        # Now we build the WHERE clause and the list of parameter values.
2291        my @where = ();
2292        my @values = ();
2293        for (my $i = 0; $i <= $#colNames; $i++) {
2294            my $parm = $parms[$i];
2295            if (defined $parm && ($parm ne '')) {
2296                push @where, "$colNames[$i] = ?";
2297                push @values, $parm;
2298            }
2299        }
2300        # Format the WHERE clause.
2301        my $filter = (@values > 0 ? (join " AND ", @where) : undef);
2302        # Ask for all the propertie values with the desired characteristics.
2303        my $query = $self->Get(['HasProperty', 'Property'], $filter, \@values);
2304        while (my $valueObject = $query->Fetch()) {
2305            my @tuple = $valueObject->Values(\@colNames);
2306            push @retVal, \@tuple;
2307        }
2308        # Return the result.
2309        return @retVal;
2310    }
2311    
2312  =head3 FeatureProperties  =head3 FeatureProperties
2313    
2314  C<< my @properties = $sprout->FeatureProperties($featureID); >>  C<< my @properties = $sprout->FeatureProperties($featureID); >>
# Line 2164  Line 2503 
2503  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>
2504    
2505  Return a hash describing all the subsystems in which a feature participates. Each subsystem is mapped  Return a hash describing all the subsystems in which a feature participates. Each subsystem is mapped
2506  to the role the feature performs.  to the roles the feature performs.
2507    
2508  =over 4  =over 4
2509    
# Line 2174  Line 2513 
2513    
2514  =item RETURN  =item RETURN
2515    
2516  Returns a hash mapping all the feature's subsystems to the feature's role.  Returns a hash mapping all the feature's subsystems to a list of the feature's roles.
2517    
2518  =back  =back
2519    
2520  =cut  =cut
2521  #: Return Type %;  #: Return Type %@;
2522  sub SubsystemsOf {  sub SubsystemsOf {
2523          # Get the parameters.          # Get the parameters.
2524          my ($self, $featureID) = @_;          my ($self, $featureID) = @_;
2525          # Use the SSCell to connect features to subsystems.      # Get the subsystem list.
2526          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],
2527                                                                          "ContainsFeature(to-link) = ?", [$featureID],                                                                          "ContainsFeature(to-link) = ?", [$featureID],
2528                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);
# Line 2191  Line 2530 
2530          my %retVal = ();          my %retVal = ();
2531          # Loop through the results, adding them to the hash.          # Loop through the results, adding them to the hash.
2532          for my $record (@subsystems) {          for my $record (@subsystems) {
2533                  $retVal{$record->[0]} = $record->[1];          my ($subsys, $role) = @{$record};
2534            if (exists $retVal{$subsys}) {
2535                push @{$retVal{$subsys}}, $role;
2536            } else {
2537                $retVal{$subsys} = [$role];
2538            }
2539          }          }
2540          # Return the hash.          # Return the hash.
2541          return %retVal;          return %retVal;
2542  }  }
2543    
2544    =head3 SubsystemList
2545    
2546    C<< my @subsystems = $sprout->SubsystemList($featureID); >>
2547    
2548    Return a list containing the names of the subsystems in which the specified
2549    feature participates. Unlike L</SubsystemsOf>, this method only returns the
2550    subsystem names, not the roles.
2551    
2552    =over 4
2553    
2554    =item featureID
2555    
2556    ID of the feature whose subsystem names are desired.
2557    
2558    =item RETURN
2559    
2560    Returns a list of the names of the subsystems in which the feature participates.
2561    
2562    =back
2563    
2564    =cut
2565    #: Return Type @;
2566    sub SubsystemList {
2567        # Get the parameters.
2568        my ($self, $featureID) = @_;
2569        # Get the list of names.
2570        my @retVal = $self->GetFlat(['ContainsFeature', 'HasSSCell'], "ContainsFeature(to-link) = ?",
2571                                    [$featureID], 'HasSSCell(from-link)');
2572        # Return the result.
2573        return @retVal;
2574    }
2575    
2576  =head3 RelatedFeatures  =head3 RelatedFeatures
2577    
2578  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>
# Line 2615  Line 2991 
2991      return %retVal;      return %retVal;
2992  }  }
2993    
2994    =head3 MyGenomes
2995    
2996    C<< my @genomes = Sprout::MyGenomes($dataDir); >>
2997    
2998    Return a list of the genomes to be included in the Sprout.
2999    
3000    This method is provided for use during the Sprout load. It presumes the Genome load file has
3001    already been created. (It will be in the Sprout data directory and called either C<Genome>
3002    or C<Genome.dtx>.) Essentially, it reads in the Genome load file and strips out the genome
3003    IDs.
3004    
3005    =over 4
3006    
3007    =item dataDir
3008    
3009    Directory containing the Sprout load files.
3010    
3011    =back
3012    
3013    =cut
3014    #: Return Type @;
3015    sub MyGenomes {
3016        # Get the parameters.
3017        my ($dataDir) = @_;
3018        # Compute the genome file name.
3019        my $genomeFileName = LoadFileName($dataDir, "Genome");
3020        # Extract the genome IDs from the files.
3021        my @retVal = map { $_ =~ /^(\S+)/; $1 } Tracer::GetFile($genomeFileName);
3022        # Return the result.
3023        return @retVal;
3024    }
3025    
3026    =head3 LoadFileName
3027    
3028    C<< my $fileName = Sprout::LoadFileName($dataDir, $tableName); >>
3029    
3030    Return the name of the load file for the specified table in the specified data
3031    directory.
3032    
3033    =over 4
3034    
3035    =item dataDir
3036    
3037    Directory containing the Sprout load files.
3038    
3039    =item tableName
3040    
3041    Name of the table whose load file is desired.
3042    
3043    =item RETURN
3044    
3045    Returns the name of the file containing the load data for the specified table, or
3046    C<undef> if no load file is present.
3047    
3048    =back
3049    
3050    =cut
3051    #: Return Type $;
3052    sub LoadFileName {
3053        # Get the parameters.
3054        my ($dataDir, $tableName) = @_;
3055        # Declare the return variable.
3056        my $retVal;
3057        # Check for the various file names.
3058        if (-e "$dataDir/$tableName") {
3059            $retVal = "$dataDir/$tableName";
3060        } elsif (-e "$dataDir/$tableName.dtx") {
3061            $retVal = "$dataDir/$tableName.dtx";
3062        }
3063        # Return the result.
3064        return $retVal;
3065    }
3066    
3067  =head2 Internal Utility Methods  =head2 Internal Utility Methods
3068    
3069  =head3 ParseAssignment  =head3 ParseAssignment
# Line 2690  Line 3139 
3139      return $retVal;      return $retVal;
3140  }  }
3141    
3142    =head3 AddProperty
3143    
3144    C<< my  = $sprout->AddProperty($featureID, $key, $value, $url); >>
3145    
3146    Add a new attribute value (Property) to a feature. In the SEED system, attributes can
3147    be added to almost any object. In Sprout, they can only be added to features. In
3148    Sprout, attributes are implemented using I<properties>. A property represents a key/value
3149    pair. If the particular key/value pair coming in is not already in the database, a new
3150    B<Property> record is created to hold it.
3151    
3152    =over 4
3153    
3154    =item peg
3155    
3156    ID of the feature to which the attribute is to be replied.
3157    
3158    =item key
3159    
3160    Name of the attribute (key).
3161    
3162    =item value
3163    
3164    Value of the attribute.
3165    
3166    =item url
3167    
3168    URL or text citation from which the property was obtained.
3169    
3170    =back
3171    
3172    =cut
3173    #: Return Type ;
3174    sub AddProperty {
3175        # Get the parameters.
3176        my ($self, $featureID, $key, $value, $url) = @_;
3177        # Declare the variable to hold the desired property ID.
3178        my $propID;
3179        # Attempt to find a property record for this key/value pair.
3180        my @properties = $self->GetFlat(['Property'],
3181                                       "Property(property-name) = ? AND Property(property-value) = ?",
3182                                       [$key, $value], 'Property(id)');
3183        if (@properties) {
3184            # Here the property is already in the database. We save its ID.
3185            $propID = $properties[0];
3186            # Here the property value does not exist. We need to generate an ID. It will be set
3187            # to a number one greater than the maximum value in the database. This call to
3188            # GetAll will stop after one record.
3189            my @maxProperty = $self->GetAll(['Property'], "ORDER BY Property(id) DESC", [], ['Property(id)'],
3190                                            1);
3191            $propID = $maxProperty[0]->[0] + 1;
3192            # Insert the new property value.
3193            $self->Insert('Property', { 'property-name' => $key, 'property-value' => $value, id => $propID });
3194        }
3195        # Now we connect the incoming feature to the property.
3196        $self->Insert('HasProperty', { 'from-link' => $featureID, 'to-link' => $propID, evidence => $url });
3197    }
3198    
3199    
3200    
3201  1;  1;

Legend:
Removed from v.1.14  
changed lines
  Added in v.1.20

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3