[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.21, Fri Sep 9 20:40:41 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            Trace("Coupling evidence for ($peg1, $peg2) with inversion flag $inverted.") if T(Coupling => 4);
1525            my $ordering = ($inverted ? "DESC" : "");
1526            # Get the coupling evidence.
1527            my @evidenceList = $self->GetAll(['IsEvidencedBy', 'PCH', 'UsesAsEvidence'],
1528                                              "IsEvidencedBy(from-link) = ? ORDER BY PCH(id), UsesAsEvidence(pos) $ordering",
1529                                              [$couplingID],
1530                                              ['PCH(used)', 'UsesAsEvidence(to-link)']);
1531            # Loop through the evidence items. Each piece of evidence is represented by two
1532            # positions in the evidence list, one for each feature on the other side of the
1533            # evidence link. If at some point we want to generalize to couplings with
1534            # more than two positions, this section of code will need to be re-done.
1535            while (@evidenceList > 0) {
1536                my $peg1Data = shift @evidenceList;
1537                my $peg2Data = shift @evidenceList;
1538                Trace("Peg 1 is " . $peg1Data->[1] . " and Peg 2 is " . $peg2Data->[1] . ".") if T(Coupling => 4);
1539                push @retVal, [$peg1Data->[1], $peg2Data->[1], $peg1Data->[0]];
1540            }
1541        }
1542        # Return the result.
1543        return @retVal;
1544    }
1545    
1546    =head3 GetCoupling
1547    
1548    C<< my ($couplingID, $inverted, $score) = $sprout->GetCoupling($peg1, $peg2); >>
1549    
1550    Return the coupling (if any) for the specified pair of PEGs. If a coupling
1551    exists, we return the coupling ID along with an indicator of whether the
1552    coupling is stored as C<(>I<$peg1>C<, >I<$peg2>C<)> or C<(>I<$peg2>C<, >I<$peg1>C<)>.
1553    In the second case, we say the coupling is I<inverted>. The importance of an
1554    inverted coupling is that the PEGs in the evidence will appear in reverse order.
1555    
1556    =over 4
1557    
1558    =item peg1
1559    
1560    ID of the feature of interest.
1561    
1562    =item peg2
1563    
1564    ID of the potentially coupled feature.
1565    
1566    =item RETURN
1567    
1568    Returns a three-element list. The first element contains the database ID of
1569    the coupling. The second element is FALSE if the coupling is stored in the
1570    database in the caller specified order and TRUE if it is stored in the
1571    inverted order. The third element is the coupling's score. If the coupling
1572    does not exist, all three list elements will be C<undef>.
1573    
1574    =back
1575    
1576    =cut
1577    #: Return Type $%@;
1578    sub GetCoupling {
1579        # Get the parameters.
1580        my ($self, $peg1, $peg2) = @_;
1581        # Declare the return values. We'll start with the coupling ID and undefine the
1582        # flag and score until we have more information.
1583        my ($retVal, $inverted, $score) = (CouplingID($peg1, $peg2), undef, undef);
1584        # Find the coupling data.
1585        my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'],
1586                                     "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)",
1587                                     [$retVal], ["ParticipatesInCoupling(from-link)", "Coupling(score)"]);
1588        # Check to see if we found anything.
1589        if (!@pegs) {
1590            # No coupling, so undefine the return value.
1591            $retVal = undef;
1592        } else {
1593            # We have a coupling! Get the score and check for inversion.
1594            $score = $pegs[0]->[1];
1595            $inverted = ($pegs[0]->[0] eq $peg1);
1596        }
1597        # Return the result.
1598        return ($retVal, $inverted, $score);
1599    }
1600    
1601    =head3 CouplingID
1602    
1603    C<< my $couplingID = Sprout::CouplingID($peg1, $peg2); >>
1604    
1605    Return the coupling ID for a pair of feature IDs.
1606    
1607    The coupling ID is currently computed by joining the feature IDs in
1608    sorted order with a space. Client modules (that is, modules which
1609    use Sprout) should not, however, count on this always being the
1610    case. This method provides a way for abstracting the concept of a
1611    coupling ID. All that we know for sure about it is that it can be
1612    generated easily from the feature IDs and the order of the IDs
1613    in the parameter list does not matter (i.e. C<CouplingID("a1", "b1")>
1614    will have the same value as C<CouplingID("b1", "a1")>.
1615    
1616    =over 4
1617    
1618    =item peg1
1619    
1620    First feature of interest.
1621    
1622    =item peg2
1623    
1624    Second feature of interest.
1625    
1626    =item RETURN
1627    
1628    Returns the ID that would be used to represent a functional coupling of
1629    the two specified PEGs.
1630    
1631    =back
1632    
1633    =cut
1634    #: Return Type $;
1635    sub CouplingID {
1636        return join " ", sort @_;
1637    }
1638    
1639  =head3 GetEntityTypes  =head3 GetEntityTypes
1640    
1641  C<< my @entityList = $sprout->GetEntityTypes(); >>  C<< my @entityList = $sprout->GetEntityTypes(); >>
# Line 1572  Line 1830 
1830  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
1831  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>.
1832    
1833  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'}); >>
1834    
1835  =over 4  =over 4
1836    
# Line 1970  Line 2228 
2228          return @retVal;          return @retVal;
2229  }  }
2230    
2231    =head3 GetProperties
2232    
2233    C<< my @list = $sprout->GetProperties($fid, $key, $value, $url); >>
2234    
2235    Return a list of the properties with the specified characteristics.
2236    
2237    Properties are arbitrary key-value pairs associated with a feature. (At some point they
2238    will also be associated with genomes.) A property value is represented by a 4-tuple of
2239    the form B<($fid, $key, $value, $url)>. These exactly correspond to the parameter
2240    
2241    =over 4
2242    
2243    =item fid
2244    
2245    ID of the feature possessing the property.
2246    
2247    =item key
2248    
2249    Name or key of the property.
2250    
2251    =item value
2252    
2253    Value of the property.
2254    
2255    =item url
2256    
2257    URL of the document that indicated the property should have this particular value, or an
2258    empty string if no such document exists.
2259    
2260    =back
2261    
2262    The parameters act as a filter for the desired data. Any non-null parameter will
2263    automatically match all the tuples returned. So, specifying just the I<$fid> will
2264    return all the properties of the specified feature; similarly, specifying the I<$key>
2265    and I<$value> parameters will return all the features having the specified property
2266    value.
2267    
2268    A single property key can have many values, representing different ideas about the
2269    feature in question. For example, one paper may declare that a feature C<fig|83333.1.peg.10> is
2270    virulent, and another may declare that it is not virulent. A query about the virulence of
2271    C<fig|83333.1.peg.10> would be coded as
2272    
2273        my @list = $sprout->GetProperties('fig|83333.1.peg.10', 'virulence', '', '');
2274    
2275    Here the I<$value> and I<$url> fields are left blank, indicating that those fields are
2276    not to be filtered. The tuples returned would be
2277    
2278        ('fig|83333.1.peg.10', 'virulence', 'yes', 'http://www.somewhere.edu/first.paper.pdf')
2279        ('fig|83333.1.peg.10', 'virulence', 'no', 'http://www.somewhere.edu/second.paper.pdf')
2280    
2281    =cut
2282    #: Return Type @@;
2283    sub GetProperties {
2284        # Get the parameters.
2285        my ($self, @parms) = @_;
2286        # Declare the return variable.
2287        my @retVal = ();
2288        # Now we need to create a WHERE clause that will get us the data we want. First,
2289        # we create a list of the columns containing the data for each parameter.
2290        my @colNames = ('HasProperty(from-link)', 'Property(property-name)',
2291                        'Property(property-value)', 'HasProperty(evidence)');
2292        # Now we build the WHERE clause and the list of parameter values.
2293        my @where = ();
2294        my @values = ();
2295        for (my $i = 0; $i <= $#colNames; $i++) {
2296            my $parm = $parms[$i];
2297            if (defined $parm && ($parm ne '')) {
2298                push @where, "$colNames[$i] = ?";
2299                push @values, $parm;
2300            }
2301        }
2302        # Format the WHERE clause.
2303        my $filter = (@values > 0 ? (join " AND ", @where) : undef);
2304        # Ask for all the propertie values with the desired characteristics.
2305        my $query = $self->Get(['HasProperty', 'Property'], $filter, \@values);
2306        while (my $valueObject = $query->Fetch()) {
2307            my @tuple = $valueObject->Values(\@colNames);
2308            push @retVal, \@tuple;
2309        }
2310        # Return the result.
2311        return @retVal;
2312    }
2313    
2314  =head3 FeatureProperties  =head3 FeatureProperties
2315    
2316  C<< my @properties = $sprout->FeatureProperties($featureID); >>  C<< my @properties = $sprout->FeatureProperties($featureID); >>
# Line 2164  Line 2505 
2505  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>
2506    
2507  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
2508  to the role the feature performs.  to the roles the feature performs.
2509    
2510  =over 4  =over 4
2511    
# Line 2174  Line 2515 
2515    
2516  =item RETURN  =item RETURN
2517    
2518  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.
2519    
2520  =back  =back
2521    
2522  =cut  =cut
2523  #: Return Type %;  #: Return Type %@;
2524  sub SubsystemsOf {  sub SubsystemsOf {
2525          # Get the parameters.          # Get the parameters.
2526          my ($self, $featureID) = @_;          my ($self, $featureID) = @_;
2527          # Use the SSCell to connect features to subsystems.      # Get the subsystem list.
2528          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],
2529                                                                          "ContainsFeature(to-link) = ?", [$featureID],                                                                          "ContainsFeature(to-link) = ?", [$featureID],
2530                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);
# Line 2191  Line 2532 
2532          my %retVal = ();          my %retVal = ();
2533          # Loop through the results, adding them to the hash.          # Loop through the results, adding them to the hash.
2534          for my $record (@subsystems) {          for my $record (@subsystems) {
2535                  $retVal{$record->[0]} = $record->[1];          my ($subsys, $role) = @{$record};
2536            if (exists $retVal{$subsys}) {
2537                push @{$retVal{$subsys}}, $role;
2538            } else {
2539                $retVal{$subsys} = [$role];
2540            }
2541          }          }
2542          # Return the hash.          # Return the hash.
2543          return %retVal;          return %retVal;
2544  }  }
2545    
2546    =head3 SubsystemList
2547    
2548    C<< my @subsystems = $sprout->SubsystemList($featureID); >>
2549    
2550    Return a list containing the names of the subsystems in which the specified
2551    feature participates. Unlike L</SubsystemsOf>, this method only returns the
2552    subsystem names, not the roles.
2553    
2554    =over 4
2555    
2556    =item featureID
2557    
2558    ID of the feature whose subsystem names are desired.
2559    
2560    =item RETURN
2561    
2562    Returns a list of the names of the subsystems in which the feature participates.
2563    
2564    =back
2565    
2566    =cut
2567    #: Return Type @;
2568    sub SubsystemList {
2569        # Get the parameters.
2570        my ($self, $featureID) = @_;
2571        # Get the list of names.
2572        my @retVal = $self->GetFlat(['ContainsFeature', 'HasSSCell'], "ContainsFeature(to-link) = ?",
2573                                    [$featureID], 'HasSSCell(from-link)');
2574        # Return the result.
2575        return @retVal;
2576    }
2577    
2578  =head3 RelatedFeatures  =head3 RelatedFeatures
2579    
2580  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>
# Line 2615  Line 2993 
2993      return %retVal;      return %retVal;
2994  }  }
2995    
2996    =head3 MyGenomes
2997    
2998    C<< my @genomes = Sprout::MyGenomes($dataDir); >>
2999    
3000    Return a list of the genomes to be included in the Sprout.
3001    
3002    This method is provided for use during the Sprout load. It presumes the Genome load file has
3003    already been created. (It will be in the Sprout data directory and called either C<Genome>
3004    or C<Genome.dtx>.) Essentially, it reads in the Genome load file and strips out the genome
3005    IDs.
3006    
3007    =over 4
3008    
3009    =item dataDir
3010    
3011    Directory containing the Sprout load files.
3012    
3013    =back
3014    
3015    =cut
3016    #: Return Type @;
3017    sub MyGenomes {
3018        # Get the parameters.
3019        my ($dataDir) = @_;
3020        # Compute the genome file name.
3021        my $genomeFileName = LoadFileName($dataDir, "Genome");
3022        # Extract the genome IDs from the files.
3023        my @retVal = map { $_ =~ /^(\S+)/; $1 } Tracer::GetFile($genomeFileName);
3024        # Return the result.
3025        return @retVal;
3026    }
3027    
3028    =head3 LoadFileName
3029    
3030    C<< my $fileName = Sprout::LoadFileName($dataDir, $tableName); >>
3031    
3032    Return the name of the load file for the specified table in the specified data
3033    directory.
3034    
3035    =over 4
3036    
3037    =item dataDir
3038    
3039    Directory containing the Sprout load files.
3040    
3041    =item tableName
3042    
3043    Name of the table whose load file is desired.
3044    
3045    =item RETURN
3046    
3047    Returns the name of the file containing the load data for the specified table, or
3048    C<undef> if no load file is present.
3049    
3050    =back
3051    
3052    =cut
3053    #: Return Type $;
3054    sub LoadFileName {
3055        # Get the parameters.
3056        my ($dataDir, $tableName) = @_;
3057        # Declare the return variable.
3058        my $retVal;
3059        # Check for the various file names.
3060        if (-e "$dataDir/$tableName") {
3061            $retVal = "$dataDir/$tableName";
3062        } elsif (-e "$dataDir/$tableName.dtx") {
3063            $retVal = "$dataDir/$tableName.dtx";
3064        }
3065        # Return the result.
3066        return $retVal;
3067    }
3068    
3069  =head2 Internal Utility Methods  =head2 Internal Utility Methods
3070    
3071  =head3 ParseAssignment  =head3 ParseAssignment
# Line 2690  Line 3141 
3141      return $retVal;      return $retVal;
3142  }  }
3143    
3144    =head3 AddProperty
3145    
3146    C<< my  = $sprout->AddProperty($featureID, $key, $value, $url); >>
3147    
3148    Add a new attribute value (Property) to a feature. In the SEED system, attributes can
3149    be added to almost any object. In Sprout, they can only be added to features. In
3150    Sprout, attributes are implemented using I<properties>. A property represents a key/value
3151    pair. If the particular key/value pair coming in is not already in the database, a new
3152    B<Property> record is created to hold it.
3153    
3154    =over 4
3155    
3156    =item peg
3157    
3158    ID of the feature to which the attribute is to be replied.
3159    
3160    =item key
3161    
3162    Name of the attribute (key).
3163    
3164    =item value
3165    
3166    Value of the attribute.
3167    
3168    =item url
3169    
3170    URL or text citation from which the property was obtained.
3171    
3172    =back
3173    
3174    =cut
3175    #: Return Type ;
3176    sub AddProperty {
3177        # Get the parameters.
3178        my ($self, $featureID, $key, $value, $url) = @_;
3179        # Declare the variable to hold the desired property ID.
3180        my $propID;
3181        # Attempt to find a property record for this key/value pair.
3182        my @properties = $self->GetFlat(['Property'],
3183                                       "Property(property-name) = ? AND Property(property-value) = ?",
3184                                       [$key, $value], 'Property(id)');
3185        if (@properties) {
3186            # Here the property is already in the database. We save its ID.
3187            $propID = $properties[0];
3188            # Here the property value does not exist. We need to generate an ID. It will be set
3189            # to a number one greater than the maximum value in the database. This call to
3190            # GetAll will stop after one record.
3191            my @maxProperty = $self->GetAll(['Property'], "ORDER BY Property(id) DESC", [], ['Property(id)'],
3192                                            1);
3193            $propID = $maxProperty[0]->[0] + 1;
3194            # Insert the new property value.
3195            $self->Insert('Property', { 'property-name' => $key, 'property-value' => $value, id => $propID });
3196        }
3197        # Now we connect the incoming feature to the property.
3198        $self->Insert('HasProperty', { 'from-link' => $featureID, 'to-link' => $propID, evidence => $url });
3199    }
3200    
3201    
3202    
3203  1;  1;

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3