--- Sprout.pm 2006/12/06 03:37:26 1.96 +++ Sprout.pm 2008/01/30 22:14:02 1.105 @@ -1,21 +1,20 @@ package Sprout; - require Exporter; - use ERDB; - @ISA = qw(Exporter ERDB); use Data::Dumper; use strict; - use Carp; use DBKernel; use XML::Simple; use DBQuery; - use DBObject; + use ERDBObject; use Tracer; use FIGRules; use FidCheck; use Stats; use POSIX qw(strftime); use BasicLocation; + use CustomAttributes; + use RemoteCustomAttributes; + use base qw(ERDB); =head1 Sprout Database Manipulation Object @@ -28,12 +27,12 @@ on the constructor. For example, the following invocation specifies a PostgreSQL database named I whose definition and data files are in a co-directory named F. -C<< my $sprout = Sprout->new('GenDB', { dbType => 'pg', dataDir => '../Data', xmlFileName => '../Data/SproutDBD.xml' }); >> + my $sprout = Sprout->new('GenDB', { dbType => 'pg', dataDir => '../Data', xmlFileName => '../Data/SproutDBD.xml' }); Once you have a sprout object, you may use it to re-create the database, load the tables from tab-delimited flat files and perform queries. Several special methods are provided for common -query tasks. For example, L lists the IDs of all the genomes in the database and -L returns the DNA sequence for a specified genome location. +query tasks. For example, L lists the IDs of all the genomes in the database and +L returns the DNA sequence for a specified genome location. The Sprout object is a subclass of the ERDB object and inherits all its properties and methods. @@ -45,7 +44,7 @@ =head3 new -C<< my $sprout = Sprout->new($dbName, \%options); >> + my $sprout = Sprout->new($dbName, \%options); This is the constructor for a sprout object. It connects to the database and loads the database definition into memory. The positional first parameter specifies the name of the @@ -79,13 +78,15 @@ * B suppresses the connection to the database if TRUE, else FALSE +* B name of the database host + =back For example, the following constructor call specifies a database named I and a user name of I with a password of I. The database load files are in the directory F. -C<< my $sprout = Sprout->new('Sprout', { userData =>; 'fig/admin', dataDir => '/usr/fig/SproutData' }); >> + my $sprout = Sprout->new('Sprout', { userData => 'fig/admin', dataDir => '/usr/fig/SproutData' }); =cut @@ -109,7 +110,7 @@ port => $FIG_Config::dbport, # database connection port sock => $FIG_Config::dbsock, - host => $FIG_Config::dbhost, + host => $FIG_Config::sprout_host, maxSegmentLength => 4500, # maximum feature segment length maxSequenceLength => 8000, # maximum contig sequence length noDBOpen => 0, # 1 to suppress the database open @@ -122,6 +123,7 @@ # Connect to the database. my $dbh; if (! $optionTable->{noDBOpen}) { + Trace("Connect data: host = $optionTable->{host}, port = $optionTable->{port}.") if T(3); $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName, $password, $optionTable->{port}, $optionTable->{host}, $optionTable->{sock}); } @@ -133,13 +135,24 @@ $retVal->{_xmlName} = $xmlFileName; # Set up space for the group file data. $retVal->{groupHash} = undef; + # Set up space for the genome hash. We use this to identify NMPDR genomes. + $retVal->{genomeHash} = undef; + # Connect to the attributes. + if ($FIG_Config::attrURL) { + Trace("Remote attribute server $FIG_Config::attrURL chosen.") if T(3); + $retVal->{_ca} = RemoteCustomAttributes->new($FIG_Config::attrURL); + } elsif ($FIG_Config::attrDbName) { + Trace("Local attribute database $FIG_Config::attrDbName chosen.") if T(3); + my $user = ($FIG_Config::arch eq 'win' ? 'self' : scalar(getpwent())); + $retVal->{_ca} = CustomAttributes->new(user => $user); + } # Return it. return $retVal; } =head3 MaxSegment -C<< my $length = $sprout->MaxSegment(); >> + my $length = $sprout->MaxSegment(); This method returns the maximum permissible length of a feature segment. The length is important because it enables us to make reasonable guesses at how to find features inside a particular @@ -156,7 +169,7 @@ =head3 MaxSequence -C<< my $length = $sprout->MaxSequence(); >> + my $length = $sprout->MaxSequence(); This method returns the maximum permissible length of a contig sequence. A contig is broken into sequences in order to save memory resources. In particular, when manipulating features, @@ -171,7 +184,7 @@ =head3 Load -C<< $sprout->Load($rebuild); >>; + $sprout->Load($rebuild);; Load the database from files in the data directory, optionally re-creating the tables. @@ -211,7 +224,7 @@ =head3 LoadUpdate -C<< my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >> + my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList); Load updates to one or more database tables. This method enables the client to make changes to one or two tables without reloading the whole database. For each table, there must be a corresponding @@ -257,7 +270,7 @@ Trace("No load file found for $tableName in $dataDir.") if T(0); } else { # Attempt to load this table. - my $result = $self->LoadTable($fileName, $tableName, $truncateFlag); + my $result = $self->LoadTable($fileName, $tableName, truncate => $truncateFlag); # Accumulate the resulting statistics. $retVal->Accumulate($result); } @@ -268,7 +281,7 @@ =head3 GenomeCounts -C<< my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete); >> + my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete); Count the number of genomes in each domain. If I<$complete> is TRUE, only complete genomes will be included in the counts. @@ -313,7 +326,7 @@ =head3 ContigCount -C<< my $count = $sprout->ContigCount($genomeID); >> + my $count = $sprout->ContigCount($genomeID); Return the number of contigs for the specified genome ID. @@ -342,7 +355,7 @@ =head3 GeneMenu -C<< my $selectHtml = $sprout->GeneMenu(\%attributes, $filterString, \@params, $selected); >> + my $selectHtml = $sprout->GeneMenu(\%attributes, $filterString, \@params, $selected); Return an HTML select menu of genomes. Each genome will be an option in the menu, and will be displayed by name with the ID and a contig count attached. The selection @@ -423,7 +436,7 @@ =head3 Build -C<< $sprout->Build(); >> + $sprout->Build(); Build the database. The database will be cleared and the tables re-created from the metadata. This method is useful when a database is brand new or when the database definition has @@ -440,7 +453,7 @@ =head3 Genomes -C<< my @genomes = $sprout->Genomes(); >> + my @genomes = $sprout->Genomes(); Return a list of all the genome IDs. @@ -457,7 +470,7 @@ =head3 GenusSpecies -C<< my $infoString = $sprout->GenusSpecies($genomeID); >> + my $infoString = $sprout->GenusSpecies($genomeID); Return the genus, species, and unique characterization for a genome. @@ -489,7 +502,7 @@ =head3 FeaturesOf -C<< my @features = $sprout->FeaturesOf($genomeID, $ftype); >> + my @features = $sprout->FeaturesOf($genomeID, $ftype); Return a list of the features relevant to a specified genome. @@ -534,7 +547,7 @@ =head3 FeatureLocation -C<< my @locations = $sprout->FeatureLocation($featureID); >> + my @locations = $sprout->FeatureLocation($featureID); Return the location of a feature in its genome's contig segments. In a list context, this method will return a list of the locations. In a scalar context, it will return the locations as a space- @@ -563,57 +576,24 @@ =back =cut -#: Return Type @; -#: Return Type $; + sub FeatureLocation { # Get the parameters. my ($self, $featureID) = @_; - # Create a query for the feature locations. - my $query = $self->Get(['IsLocatedIn'], "IsLocatedIn(from-link) = ? ORDER BY IsLocatedIn(locN)", - [$featureID]); + # Get the feature record. + my $object = $self->GetEntity('Feature', $featureID); + Confess("Feature $featureID not found.") if ! defined($object); + # Get the location string. + my $locString = $object->PrimaryValue('Feature(location-string)'); # Create the return list. - my @retVal = (); - # Set up the variables used to determine if we have adjacent segments. This initial setup will - # not match anything. - my ($prevContig, $prevBeg, $prevDir, $prevLen) = ("", 0, "0", 0); - # Loop through the query results, creating location specifiers. - while (my $location = $query->Fetch()) { - # Get the location parameters. - my ($contigID, $beg, $dir, $len) = $location->Values(['IsLocatedIn(to-link)', - 'IsLocatedIn(beg)', 'IsLocatedIn(dir)', 'IsLocatedIn(len)']); - # Check to see if we are adjacent to the previous segment. - if ($prevContig eq $contigID && $dir eq $prevDir) { - # Here the new segment is in the same direction on the same contig. Insure the - # new segment's beginning is next to the old segment's end. - if ($dir eq "-" && $beg + $len == $prevBeg) { - # Here we're merging two backward blocks, so we keep the new begin point - # and adjust the length. - $len += $prevLen; - # Pop the old segment off. The new one will replace it later. - pop @retVal; - } elsif ($dir eq "+" && $beg == $prevBeg + $prevLen) { - # Here we need to merge two forward blocks. Adjust the beginning and - # length values to include both segments. - $beg = $prevBeg; - $len += $prevLen; - # Pop the old segment off. The new one will replace it later. - pop @retVal; - } - } - # Remember this specifier for the adjacent-segment test the next time through. - ($prevContig, $prevBeg, $prevDir, $prevLen) = ($contigID, $beg, $dir, $len); - # Compute the initial base pair. - my $start = ($dir eq "+" ? $beg : $beg + $len - 1); - # Add the specifier to the list. - push @retVal, "${contigID}_$start$dir$len"; - } + my @retVal = split /\s*,\s*/, $locString; # Return the list in the format indicated by the context. return (wantarray ? @retVal : join(',', @retVal)); } =head3 ParseLocation -C<< my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location); >> + my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location); Split a location specifier into the contig ID, the starting point, the direction, and the length. @@ -632,7 +612,7 @@ =back =cut -#: Return Type @; + sub ParseLocation { # Get the parameter. Note that if we're called as an instance method, we ignore # the first parameter. @@ -659,7 +639,7 @@ =head3 PointLocation -C<< my $found = Sprout::PointLocation($location, $point); >> + my $found = Sprout::PointLocation($location, $point); Return the offset into the specified location of the specified point on the contig. If the specified point is before the location, a negative value will be returned. If it is @@ -688,7 +668,7 @@ =back =cut -#: Return Type $; + sub PointLocation { # Get the parameter. Note that if we're called as an instance method, we ignore # the first parameter. @@ -711,7 +691,7 @@ =head3 DNASeq -C<< my $sequence = $sprout->DNASeq(\@locationList); >> + my $sequence = $sprout->DNASeq(\@locationList); This method returns the DNA sequence represented by a list of locations. The list of locations should be of the form returned by L when in a list context. In other words, @@ -795,7 +775,7 @@ =head3 AllContigs -C<< my @idList = $sprout->AllContigs($genomeID); >> + my @idList = $sprout->AllContigs($genomeID); Return a list of all the contigs for a genome. @@ -825,7 +805,7 @@ =head3 GenomeLength -C<< my $length = $sprout->GenomeLength($genomeID); >> + my $length = $sprout->GenomeLength($genomeID); Return the length of the specified genome in base pairs. @@ -860,7 +840,7 @@ =head3 FeatureCount -C<< my $count = $sprout->FeatureCount($genomeID, $type); >> + my $count = $sprout->FeatureCount($genomeID, $type); Return the number of features of the specified type in the specified genome. @@ -895,7 +875,7 @@ =head3 GenomeAssignments -C<< my $fidHash = $sprout->GenomeAssignments($genomeID); >> + my $fidHash = $sprout->GenomeAssignments($genomeID); Return a list of a genome's assigned features. The return hash will contain each assigned feature of the genome mapped to the text of its most recent functional @@ -938,7 +918,7 @@ =head3 ContigLength -C<< my $length = $sprout->ContigLength($contigID); >> + my $length = $sprout->ContigLength($contigID); Compute the length of a contig. @@ -977,14 +957,14 @@ =head3 ClusterPEGs -C<< my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs); >> + my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs); Cluster the PEGs in a list according to the cluster coding scheme of the specified subsystem. In order for this to work properly, the subsystem object must have -been used recently to retrieve the PEGs using the B method. -This causes the cluster numbers to be pulled into the subsystem's color hash. -If a PEG is not found in the color hash, it will not appear in the output -sequence. +been used recently to retrieve the PEGs using the B or +B methods. This causes the cluster numbers to be pulled into the +subsystem's color hash. If a PEG is not found in the color hash, it will not +appear in the output sequence. =over 4 @@ -1025,7 +1005,7 @@ =head3 GenesInRegion -C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >> + my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); List the features which overlap a specified region in a contig. @@ -1054,21 +1034,95 @@ =back =cut -#: Return Type @@; + sub GenesInRegion { # Get the parameters. my ($self, $contigID, $start, $stop) = @_; # Get the maximum segment length. my $maximumSegmentLength = $self->MaxSegment; - # Create a hash to receive the feature list. We use a hash so that we can eliminate - # duplicates easily. The hash key will be the feature ID. The value will be a two-element - # containing the minimum and maximum offsets. We will use the offsets to sort the results - # when we're building the result set. - my %featuresFound = (); # Prime the values we'll use for the returned beginning and end. my @initialMinMax = ($self->ContigLength($contigID), 0); my ($min, $max) = @initialMinMax; - # Create a table of parameters for each query. Each query looks for features travelling in + # Get the overlapping features. + my @featureObjects = $self->GeneDataInRegion($contigID, $start, $stop); + # We'l use this hash to help us track the feature IDs and sort them. The key is the + # feature ID and the value is a [$left,$right] pair indicating the maximum extent + # of the feature's locations. + my %featureMap = (); + # Loop through them to do the begin/end analysis. + for my $featureObject (@featureObjects) { + # Get the feature's location string. This may contain multiple actual locations. + my ($locations, $fid) = $featureObject->Values([qw(Feature(location-string) Feature(id))]); + my @locationSegments = split /\s*,\s*/, $locations; + # Loop through the locations. + for my $locationSegment (@locationSegments) { + # Construct an object for the location. + my $locationObject = BasicLocation->new($locationSegment); + # Merge the current segment's begin and end into the min and max. + my ($left, $right) = ($locationObject->Left, $locationObject->Right); + my ($beg, $end); + if (exists $featureMap{$fid}) { + ($beg, $end) = @{$featureMap{$fid}}; + $beg = $left if $left < $beg; + $end = $right if $right > $end; + } else { + ($beg, $end) = ($left, $right); + } + $min = $beg if $beg < $min; + $max = $end if $end > $max; + # Store the feature's new extent back into the hash table. + $featureMap{$fid} = [$beg, $end]; + } + } + # Now we must compute the list of the IDs for the features found. We start with a list + # of midpoints / feature ID pairs. (It's not really a midpoint, it's twice the midpoint, + # but the result of the sort will be the same.) + my @list = map { [$featureMap{$_}->[0] + $featureMap{$_}->[1], $_] } keys %featureMap; + # Now we sort by midpoint and yank out the feature IDs. + my @retVal = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @list; + # Return it along with the min and max. + return (\@retVal, $min, $max); +} + +=head3 GeneDataInRegion + + my @featureList = $sprout->GenesInRegion($contigID, $start, $stop); + +List the features which overlap a specified region in a contig. + +=over 4 + +=item contigID + +ID of the contig containing the region of interest. + +=item start + +Offset of the first residue in the region of interest. + +=item stop + +Offset of the last residue in the region of interest. + +=item RETURN + +Returns a list of B for the desired features. Each object will +contain a B record. + +=back + +=cut + +sub GeneDataInRegion { + # Get the parameters. + my ($self, $contigID, $start, $stop) = @_; + # Get the maximum segment length. + my $maximumSegmentLength = $self->MaxSegment; + # Create a hash to receive the feature list. We use a hash so that we can eliminate + # duplicates easily. The hash key will be the feature ID. The value will be the feature's + # ERDBObject from the query. + my %featuresFound = (); + # Create a table of parameters for the queries. Each query looks for features travelling in # a particular direction. The query parameters include the contig ID, the feature direction, # the lowest possible start position, and the highest possible start position. This works # because each feature segment length must be no greater than the maximum segment length. @@ -1077,67 +1131,33 @@ # Loop through the query parameters. for my $parms (values %queryParms) { # Create the query. - my $query = $self->Get(['IsLocatedIn'], + my $query = $self->Get([qw(Feature IsLocatedIn)], "IsLocatedIn(to-link)= ? AND IsLocatedIn(dir) = ? AND IsLocatedIn(beg) >= ? AND IsLocatedIn(beg) <= ?", $parms); # Loop through the feature segments found. while (my $segment = $query->Fetch) { # Get the data about this segment. - my ($featureID, $dir, $beg, $len) = $segment->Values(['IsLocatedIn(from-link)', - 'IsLocatedIn(dir)', 'IsLocatedIn(beg)', 'IsLocatedIn(len)']); - # Determine if this feature actually overlaps the region. The query insures that + my ($featureID, $contig, $dir, $beg, $len) = $segment->Values([qw(IsLocatedIn(from-link) + IsLocatedIn(to-link) IsLocatedIn(dir) IsLocatedIn(beg) IsLocatedIn(len))]); + # Determine if this feature segment actually overlaps the region. The query insures that # this will be the case if the segment is the maximum length, so to fine-tune # the results we insure that the inequality from the query holds using the actual # length. - my ($found, $end) = (0, 0); - if ($dir eq '+') { - $end = $beg + $len; - if ($end >= $start) { - # Denote we found a useful feature. - $found = 1; - } - } elsif ($dir eq '-') { - # Note we switch things around so that the beginning is to the left of the - # ending. - ($beg, $end) = ($beg - $len, $beg); - if ($beg <= $stop) { - # Denote we found a useful feature. - $found = 1; - } - } + my $loc = BasicLocation->new($contig, $beg, $dir, $len); + my $found = $loc->Overlap($start, $stop); if ($found) { - # Here we need to record the feature and update the minima and maxima. First, - # get the current entry for the specified feature. - my ($loc1, $loc2) = (exists $featuresFound{$featureID} ? @{$featuresFound{$featureID}} : - @initialMinMax); - # Merge the current segment's begin and end into the feature begin and end and the - # global min and max. - if ($beg < $loc1) { - $loc1 = $beg; - $min = $beg if $beg < $min; - } - if ($end > $loc2) { - $loc2 = $end; - $max = $end if $end > $max; - } - # Store the entry back into the hash table. - $featuresFound{$featureID} = [$loc1, $loc2]; + # Save this feature in the result list. + $featuresFound{$featureID} = $segment; } } } - # Now we must compute the list of the IDs for the features found. We start with a list - # of midpoints / feature ID pairs. (It's not really a midpoint, it's twice the midpoint, - # but the result of the sort will be the same.) - my @list = map { [$featuresFound{$_}->[0] + $featuresFound{$_}->[1], $_] } keys %featuresFound; - # Now we sort by midpoint and yank out the feature IDs. - my @retVal = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @list; - # Return it along with the min and max. - return (\@retVal, $min, $max); + # Return the ERDB objects for the features found. + return values %featuresFound; } =head3 FType -C<< my $ftype = $sprout->FType($featureID); >> + my $ftype = $sprout->FType($featureID); Return the type of a feature. @@ -1167,7 +1187,7 @@ =head3 FeatureAnnotations -C<< my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag); >> + my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag); Return the annotations of a feature. @@ -1230,7 +1250,7 @@ =head3 AllFunctionsOf -C<< my %functions = $sprout->AllFunctionsOf($featureID); >> + my %functions = $sprout->AllFunctionsOf($featureID); Return all of the functional assignments for a particular feature. The data is returned as a hash of functional assignments to user IDs. A functional assignment is a type of annotation, @@ -1285,7 +1305,7 @@ =head3 FunctionOf -C<< my $functionText = $sprout->FunctionOf($featureID, $userID); >> + my $functionText = $sprout->FunctionOf($featureID, $userID); Return the most recently-determined functional assignment of a particular feature. @@ -1389,7 +1409,7 @@ =head3 FunctionsOf -C<< my @functionList = $sprout->FunctionOf($featureID, $userID); >> + my @functionList = $sprout->FunctionOf($featureID, $userID); Return the functional assignments of a particular feature. @@ -1461,7 +1481,7 @@ =head3 BBHList -C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >> + my $bbhHash = $sprout->BBHList($genomeID, \@featureList); Return a hash mapping the features in a specified list to their bidirectional best hits on a specified target genome. @@ -1511,7 +1531,7 @@ =head3 SimList -C<< my %similarities = $sprout->SimList($featureID, $count); >> + my %similarities = $sprout->SimList($featureID, $count); Return a list of the similarities to the specified feature. @@ -1547,7 +1567,7 @@ =head3 IsComplete -C<< my $flag = $sprout->IsComplete($genomeID); >> + my $flag = $sprout->IsComplete($genomeID); Return TRUE if the specified genome is complete, else FALSE. @@ -1575,7 +1595,7 @@ my $genomeData = $self->GetEntity('Genome', $genomeID); if ($genomeData) { # The genome exists, so get the completeness flag. - ($retVal) = $genomeData->Value('Genome(complete)'); + $retVal = $genomeData->PrimaryValue('Genome(complete)'); } # Return the result. return $retVal; @@ -1583,7 +1603,7 @@ =head3 FeatureAliases -C<< my @aliasList = $sprout->FeatureAliases($featureID); >> + my @aliasList = $sprout->FeatureAliases($featureID); Return a list of the aliases for a specified feature. @@ -1606,14 +1626,14 @@ # Get the parameters. my ($self, $featureID) = @_; # Get the desired feature's aliases - my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(alias)']); + my @retVal = $self->GetFlat(['IsAliasOf'], "IsAliasOf(to-link) = ?", [$featureID], 'IsAliasOf(from-link)'); # Return the result. return @retVal; } =head3 GenomeOf -C<< my $genomeID = $sprout->GenomeOf($featureID); >> + my $genomeID = $sprout->GenomeOf($featureID); Return the genome that contains a specified feature or contig. @@ -1635,14 +1655,13 @@ sub GenomeOf { # Get the parameters. my ($self, $featureID) = @_; - # Create a query to find the genome associated with the incoming ID. - my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ? OR HasContig(to-link) = ?", - [$featureID, $featureID]); # Declare the return value. my $retVal; - # Get the genome ID. - if (my $relationship = $query->Fetch()) { - ($retVal) = $relationship->Value('HasContig(from-link)'); + # Parse the genome ID from the feature ID. + if ($featureID =~ /^fig\|(\d+\.\d+)/) { + $retVal = $1; + } else { + Confess("Invalid feature ID $featureID."); } # Return the value found. return $retVal; @@ -1650,7 +1669,7 @@ =head3 CoupledFeatures -C<< my %coupleHash = $sprout->CoupledFeatures($featureID); >> + my %coupleHash = $sprout->CoupledFeatures($featureID); Return the features functionally coupled with a specified feature. Features are considered functionally coupled if they tend to be clustered on the same chromosome. @@ -1672,32 +1691,23 @@ sub CoupledFeatures { # Get the parameters. my ($self, $featureID) = @_; + # Ask the coupling server for the data. Trace("Looking for features coupled to $featureID.") if T(coupling => 3); - # Create a query to retrieve the functionally-coupled features. - my $query = $self->Get(['ParticipatesInCoupling', 'Coupling'], - "ParticipatesInCoupling(from-link) = ?", [$featureID]); - # This value will be set to TRUE if we find at least one coupled feature. - my $found = 0; - # Create the return hash. + my @rawPairs = FIGRules::NetCouplingData('coupled_to', id1 => $featureID); + Trace(scalar(@rawPairs) . " couplings returned.") if T(coupling => 3); + # Form them into a hash. my %retVal = (); - # Retrieve the relationship records and store them in the hash. - while (my $clustering = $query->Fetch()) { - # Get the ID and score of the coupling. - my ($couplingID, $score) = $clustering->Values(['Coupling(id)', - 'Coupling(score)']); - Trace("$featureID coupled with score $score to ID $couplingID.") if T(coupling => 4); - # Get the other feature that participates in the coupling. - my ($otherFeatureID) = $self->GetFlat(['ParticipatesInCoupling'], - "ParticipatesInCoupling(to-link) = ? AND ParticipatesInCoupling(from-link) <> ?", - [$couplingID, $featureID], 'ParticipatesInCoupling(from-link)'); - Trace("$couplingID target feature is $otherFeatureID.") if T(coupling => 4); - # Attach the other feature's score to its ID. - $retVal{$otherFeatureID} = $score; - $found = 1; + for my $pair (@rawPairs) { + # Get the feature ID and score. + my ($featureID2, $score) = @{$pair}; + # Only proceed if the feature is in NMPDR. + if ($self->_CheckFeature($featureID2)) { + $retVal{$featureID2} = $score; + } } # Functional coupling is reflexive. If we found at least one coupled feature, we must add # the incoming feature as well. - if ($found) { + if (keys %retVal) { $retVal{$featureID} = 9999; } # Return the hash. @@ -1706,7 +1716,7 @@ =head3 CouplingEvidence -C<< my @evidence = $sprout->CouplingEvidence($peg1, $peg2); >> + my @evidence = $sprout->CouplingEvidence($peg1, $peg2); Return the evidence for a functional coupling. @@ -1754,99 +1764,21 @@ my ($self, $peg1, $peg2) = @_; # Declare the return variable. my @retVal = (); - # Our first task is to find out the nature of the coupling: whether or not - # it exists, its score, and whether the features are stored in the same - # order as the ones coming in. - my ($couplingID, $inverted, $score) = $self->GetCoupling($peg1, $peg2); - # Only proceed if a coupling exists. - if ($couplingID) { - # Determine the ordering to place on the evidence items. If we're - # inverted, we want to see feature 2 before feature 1 (descending); otherwise, - # we want feature 1 before feature 2 (normal). - Trace("Coupling evidence for ($peg1, $peg2) with inversion flag $inverted.") if T(Coupling => 4); - my $ordering = ($inverted ? "DESC" : ""); - # Get the coupling evidence. - my @evidenceList = $self->GetAll(['IsEvidencedBy', 'PCH', 'UsesAsEvidence'], - "IsEvidencedBy(from-link) = ? ORDER BY PCH(id), UsesAsEvidence(pos) $ordering", - [$couplingID], - ['PCH(used)', 'UsesAsEvidence(to-link)']); - # Loop through the evidence items. Each piece of evidence is represented by two - # positions in the evidence list, one for each feature on the other side of the - # evidence link. If at some point we want to generalize to couplings with - # more than two positions, this section of code will need to be re-done. - while (@evidenceList > 0) { - my $peg1Data = shift @evidenceList; - my $peg2Data = shift @evidenceList; - Trace("Peg 1 is " . $peg1Data->[1] . " and Peg 2 is " . $peg2Data->[1] . ".") if T(Coupling => 4); - push @retVal, [$peg1Data->[1], $peg2Data->[1], $peg1Data->[0]]; - } - Trace("Last index in evidence result is is $#retVal.") if T(Coupling => 4); + # Get the coupling and evidence data. + my @rawData = FIGRules::NetCouplingData('coupling_evidence', id1 => $peg1, id2 => $peg2); + # Loop through the raw data, saving the ones that are in NMPDR genomes. + for my $rawTuple (@rawData) { + if ($self->_CheckFeature($rawTuple->[0]) && $self->_CheckFeature($rawTuple->[1])) { + push @retVal, $rawTuple; + } } # Return the result. return @retVal; } -=head3 GetCoupling - -C<< my ($couplingID, $inverted, $score) = $sprout->GetCoupling($peg1, $peg2); >> - -Return the coupling (if any) for the specified pair of PEGs. If a coupling -exists, we return the coupling ID along with an indicator of whether the -coupling is stored as C<(>I<$peg1>C<, >I<$peg2>C<)> or C<(>I<$peg2>C<, >I<$peg1>C<)>. -In the second case, we say the coupling is I. The importance of an -inverted coupling is that the PEGs in the evidence will appear in reverse order. - -=over 4 - -=item peg1 - -ID of the feature of interest. - -=item peg2 - -ID of the potentially coupled feature. - -=item RETURN - -Returns a three-element list. The first element contains the database ID of -the coupling. The second element is FALSE if the coupling is stored in the -database in the caller specified order and TRUE if it is stored in the -inverted order. The third element is the coupling's score. If the coupling -does not exist, all three list elements will be C. - -=back - -=cut -#: Return Type $%@; -sub GetCoupling { - # Get the parameters. - my ($self, $peg1, $peg2) = @_; - # Declare the return values. We'll start with the coupling ID and undefine the - # flag and score until we have more information. - my ($retVal, $inverted, $score) = ($self->CouplingID($peg1, $peg2), undef, undef); - # Find the coupling data. - my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'], - "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)", - [$retVal], ["ParticipatesInCoupling(from-link)", "Coupling(score)"]); - # Check to see if we found anything. - if (!@pegs) { - Trace("No coupling found.") if T(Coupling => 4); - # No coupling, so undefine the return value. - $retVal = undef; - } else { - # We have a coupling! Get the score and check for inversion. - $score = $pegs[0]->[1]; - my $firstFound = $pegs[0]->[0]; - $inverted = ($firstFound ne $peg1); - Trace("Coupling score is $score. First peg is $firstFound, peg 1 is $peg1.") if T(Coupling => 4); - } - # Return the result. - return ($retVal, $inverted, $score); -} - =head3 GetSynonymGroup -C<< my $id = $sprout->GetSynonymGroup($fid); >> + my $id = $sprout->GetSynonymGroup($fid); Return the synonym group name for the specified feature. @@ -1885,7 +1817,7 @@ =head3 GetBoundaries -C<< my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList); >> + my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList); Determine the begin and end boundaries for the locations in a list. All of the locations must belong to the same contig and have mostly the same direction in @@ -1947,48 +1879,9 @@ return ($contig, $beg, $end); } -=head3 CouplingID - -C<< my $couplingID = $sprout->CouplingID($peg1, $peg2); >> - -Return the coupling ID for a pair of feature IDs. - -The coupling ID is currently computed by joining the feature IDs in -sorted order with a space. Client modules (that is, modules which -use Sprout) should not, however, count on this always being the -case. This method provides a way for abstracting the concept of a -coupling ID. All that we know for sure about it is that it can be -generated easily from the feature IDs and the order of the IDs -in the parameter list does not matter (i.e. C -will have the same value as C. - -=over 4 - -=item peg1 - -First feature of interest. - -=item peg2 - -Second feature of interest. - -=item RETURN - -Returns the ID that would be used to represent a functional coupling of -the two specified PEGs. - -=back - -=cut -#: Return Type $; -sub CouplingID { - my ($self, @pegs) = @_; - return $self->DigestKey(join " ", sort @pegs); -} - =head3 ReadFasta -C<< my %sequenceData = Sprout::ReadFasta($fileName, $prefix); >> + my %sequenceData = Sprout::ReadFasta($fileName, $prefix); Read sequence data from a FASTA-format file. Each sequence in a FASTA file is represented by one or more lines of data. The first line begins with a > character and contains an ID. @@ -2054,7 +1947,7 @@ =head3 FormatLocations -C<< my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat); >> + my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat); Insure that a list of feature locations is in the Sprout format. The Sprout feature location format is I_I where I<*> is C<+> for a forward gene and C<-> for a backward @@ -2119,7 +2012,7 @@ =head3 DumpData -C<< $sprout->DumpData(); >> + $sprout->DumpData(); Dump all the tables to tab-delimited DTX files. The files will be stored in the data directory. @@ -2136,7 +2029,7 @@ =head3 XMLFileName -C<< my $fileName = $sprout->XMLFileName(); >> + my $fileName = $sprout->XMLFileName(); Return the name of this database's XML definition file. @@ -2147,9 +2040,90 @@ return $self->{_xmlName}; } +=head3 GetGenomeNameData + + my ($genus, $species, $strain) = $sprout->GenomeNameData($genomeID); + +Return the genus, species, and unique characterization for a genome. This +is similar to L, with the exception that it returns the +values in three seperate fields. + +=over 4 + +=item genomeID + +ID of the genome whose name data is desired. + +=item RETURN + +Returns a three-element list, consisting of the genus, species, and strain +of the specified genome. If the genome is not found, an error occurs. + +=back + +=cut + +sub GetGenomeNameData { + # Get the parameters. + my ($self, $genomeID) = @_; + # Get the desired values. + my ($genus, $species, $strain) = $self->GetEntityValues('Genome', $genomeID => + [qw(Genome(genus) Genome(species) Genome(unique-characterization))]); + # Throw an error if they were not found. + if (! defined $genus) { + Confess("Genome $genomeID not found in database."); + } + # Return the results. + return ($genus, $species, $strain); +} + +=head3 GetGenomeByNameData + + my @genomes = $sprout->GetGenomeByNameData($genus, $species, $strain); + +Return a list of the IDs of the genomes with the specified genus, +species, and strain. In almost every case, there will be either zero or +one IDs returned; however, two or more IDs could be returned if there are +multiple versions of the genome in the database. + +=over 4 + +=item genus + +Genus of the desired genome. + +=item species + +Species of the desired genome. + +=item strain + +Strain (unique characterization) of the desired genome. This may be an empty +string, in which case it is presumed that the desired genome has no strain +specified. + +=item RETURN + +Returns a list of the IDs of the genomes having the specified genus, species, and +strain. + +=back + +=cut + +sub GetGenomeByNameData { + # Get the parameters. + my ($self, $genus, $species, $strain) = @_; + # Try to find the genomes. + my @retVal = $self->GetFlat(['Genome'], "Genome(genus) = ? AND Genome(species) = ? AND Genome(unique-characterization) = ?", + [$genus, $species, $strain], 'Genome(id)'); + # Return the result. + return @retVal; +} + =head3 Insert -C<< $sprout->Insert($objectType, \%fieldHash); >> + $sprout->Insert($objectType, \%fieldHash); Insert an entity or relationship instance into the database. The entity or relationship of interest is defined by a type name and then a hash of field names to values. Field values in the primary @@ -2158,12 +2132,12 @@ list references. For example, the following line inserts an inactive PEG feature named C with aliases C and C. -C<< $sprout->Insert('Feature', { id => 'fig|188.1.peg.1', active => 0, feature-type => 'peg', alias => ['ZP_00210270.1', 'gi|46206278']}); >> + $sprout->Insert('Feature', { id => 'fig|188.1.peg.1', active => 0, feature-type => 'peg', alias => ['ZP_00210270.1', 'gi|46206278']}); The next statement inserts a C relationship between feature C and property C<4> with an evidence URL of C. -C<< $sprout->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence => 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >> + $sprout->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence => 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); =over 4 @@ -2188,7 +2162,7 @@ =head3 Annotate -C<< my $ok = $sprout->Annotate($fid, $timestamp, $user, $text); >> + my $ok = $sprout->Annotate($fid, $timestamp, $user, $text); Annotate a feature. This inserts an Annotation record into the database and links it to the specified feature and user. @@ -2242,7 +2216,7 @@ =head3 AssignFunction -C<< my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser); >> + my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser); This method assigns a function to a feature. Functions are a special type of annotation. The general format is described in L. @@ -2302,7 +2276,7 @@ =head3 FeaturesByAlias -C<< my @features = $sprout->FeaturesByAlias($alias); >> + my @features = $sprout->FeaturesByAlias($alias); Returns a list of features with the specified alias. The alias is parsed to determine the type of the alias. A string of digits is a GenBack ID and a string of exactly 6 @@ -2336,7 +2310,7 @@ push @retVal, $mappedAlias; } else { # Here we have a non-FIG alias. Get the features with the normalized alias. - @retVal = $self->GetFlat(['Feature'], 'Feature(alias) = ?', [$mappedAlias], 'Feature(id)'); + @retVal = $self->GetFlat(['IsAliasOf'], 'IsAliasOf(from-link) = ?', [$mappedAlias], 'IsAliasOf(to-link)'); } # Return the result. return @retVal; @@ -2344,7 +2318,7 @@ =head3 FeatureTranslation -C<< my $translation = $sprout->FeatureTranslation($featureID); >> + my $translation = $sprout->FeatureTranslation($featureID); Return the translation of a feature. @@ -2372,13 +2346,13 @@ =head3 Taxonomy -C<< my @taxonomyList = $sprout->Taxonomy($genome); >> + my @taxonomyList = $sprout->Taxonomy($genome); Return the taxonomy of the specified genome. This will be in the form of a list containing the various classifications in order from domain (eg. C, C, or C) to sub-species. For example, -C<< (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12) >> + (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12) =over 4 @@ -2413,7 +2387,7 @@ =head3 CrudeDistance -C<< my $distance = $sprout->CrudeDistance($genome1, $genome2); >> + my $distance = $sprout->CrudeDistance($genome1, $genome2); Returns a crude estimate of the distance between two genomes. The distance is construed so that it will be 0 for genomes with identical taxonomies and 1 for genomes from different domains. @@ -2465,7 +2439,7 @@ =head3 RoleName -C<< my $roleName = $sprout->RoleName($roleID); >> + my $roleName = $sprout->RoleName($roleID); Return the descriptive name of the role with the specified ID. In general, a role will only have a descriptive name if it is coded as an EC number. @@ -2499,7 +2473,7 @@ =head3 RoleDiagrams -C<< my @diagrams = $sprout->RoleDiagrams($roleID); >> + my @diagrams = $sprout->RoleDiagrams($roleID); Return a list of the diagrams containing a specified functional role. @@ -2529,98 +2503,99 @@ =head3 GetProperties -C<< my @list = $sprout->GetProperties($fid, $key, $value, $url); >> + my @list = $sprout->GetProperties($fid, $key, $value, $url); Return a list of the properties with the specified characteristics. -Properties are arbitrary key-value pairs associated with a feature. (At some point they -will also be associated with genomes.) A property value is represented by a 4-tuple of -the form B<($fid, $key, $value, $url)>. These exactly correspond to the parameter +Properties are the Sprout analog of the FIG attributes. The call is +passed directly to the CustomAttributes or RemoteCustomAttributes object +contained in this object. -=over 4 +This method returns a series of tuples that match the specified criteria. Each tuple +will contain an object ID, a key, and one or more values. The parameters to this +method therefore correspond structurally to the values expected in each tuple. In +addition, you can ask for a generic search by suffixing a percent sign (C<%>) to any +of the parameters. So, for example, -=item fid + my @attributeList = $sprout->GetProperties('fig|100226.1.peg.1004', 'structure%', 1, 2); -ID of the feature possessing the property. +would return something like -=item key + ['fig}100226.1.peg.1004', 'structure', 1, 2] + ['fig}100226.1.peg.1004', 'structure1', 1, 2] + ['fig}100226.1.peg.1004', 'structure2', 1, 2] + ['fig}100226.1.peg.1004', 'structureA', 1, 2] -Name or key of the property. +Use of C in any position acts as a wild card (all values). You can also specify +a list reference in the ID column. Thus, -=item value + my @attributeList = $sprout->GetProperties(['100226.1', 'fig|100226.1.%'], 'PUBMED'); -Value of the property. +would get the PUBMED attribute data for Streptomyces coelicolor A3(2) and all its +features. -=item url +In addition to values in multiple sections, a single attribute key can have multiple +values, so even -URL of the document that indicated the property should have this particular value, or an -empty string if no such document exists. + my @attributeList = $sprout->GetProperties($peg, 'virulent'); -=back +which has no wildcard in the key or the object ID, may return multiple tuples. -The parameters act as a filter for the desired data. Any non-null parameter will -automatically match all the tuples returned. So, specifying just the I<$fid> will -return all the properties of the specified feature; similarly, specifying the I<$key> -and I<$value> parameters will return all the features having the specified property -value. +=over 4 + +=item objectID + +ID of object whose attributes are desired. If the attributes are desired for multiple +objects, this parameter can be specified as a list reference. If the attributes are +desired for all objects, specify C or an empty string. Finally, you can specify +attributes for a range of object IDs by putting a percent sign (C<%>) at the end. + +=item key -A single property key can have many values, representing different ideas about the -feature in question. For example, one paper may declare that a feature C is -virulent, and another may declare that it is not virulent. A query about the virulence of -C would be coded as +Attribute key name. A value of C or an empty string will match all +attribute keys. If the values are desired for multiple keys, this parameter can be +specified as a list reference. Finally, you can specify attributes for a range of +keys by putting a percent sign (C<%>) at the end. + +=item values + +List of the desired attribute values, section by section. If C +or an empty string is specified, all values in that section will match. A +generic match can be requested by placing a percent sign (C<%>) at the end. +In that case, all values that match up to and not including the percent sign +will match. You may also specify a regular expression enclosed +in slashes. All values that match the regular expression will be returned. For +performance reasons, only values have this extra capability. - my @list = $sprout->GetProperties('fig|83333.1.peg.10', 'virulence', '', ''); +=item RETURN -Here the I<$value> and I<$url> fields are left blank, indicating that those fields are -not to be filtered. The tuples returned would be +Returns a list of tuples. The first element in the tuple is an object ID, the +second is an attribute key, and the remaining elements are the sections of +the attribute value. All of the tuples will match the criteria set forth in +the parameter list. - ('fig|83333.1.peg.10', 'virulence', 'yes', 'http://www.somewhere.edu/first.paper.pdf') - ('fig|83333.1.peg.10', 'virulence', 'no', 'http://www.somewhere.edu/second.paper.pdf') +=back =cut -#: Return Type @@; + sub GetProperties { # Get the parameters. my ($self, @parms) = @_; # Declare the return variable. - my @retVal = (); - # Now we need to create a WHERE clause that will get us the data we want. First, - # we create a list of the columns containing the data for each parameter. - my @colNames = ('HasProperty(from-link)', 'Property(property-name)', - 'Property(property-value)', 'HasProperty(evidence)'); - # Now we build the WHERE clause and the list of parameter values. - my @where = (); - my @values = (); - for (my $i = 0; $i <= $#colNames; $i++) { - my $parm = $parms[$i]; - if (defined $parm && ($parm ne '')) { - push @where, "$colNames[$i] = ?"; - push @values, $parm; - } - } - # Format the WHERE clause. - my $filter = (@values > 0 ? (join " AND ", @where) : undef); - # Ask for all the propertie values with the desired characteristics. - my $query = $self->Get(['HasProperty', 'Property'], $filter, \@values); - while (my $valueObject = $query->Fetch()) { - my @tuple = $valueObject->Values(\@colNames); - push @retVal, \@tuple; - } + my @retVal = $self->{_ca}->GetAttributes(@parms); # Return the result. return @retVal; } =head3 FeatureProperties -C<< my @properties = $sprout->FeatureProperties($featureID); >> + my @properties = $sprout->FeatureProperties($featureID); Return a list of the properties for the specified feature. Properties are key-value pairs that specify special characteristics of the feature. For example, a property could indicate that a feature is essential to the survival of the organism or that it has benign influence on the activities of a pathogen. Each property is returned as a triple of the form -C<($key,$value,$url)>, where C<$key> is the property name, C<$value> is its value (commonly -a 1 or a 0, but possibly a string or a floating-point value), and C<$url> is a string describing -the web address or citation in which the property's value for the feature was identified. +C<($key,@values)>, where C<$key> is the property name and C<@values> are its values. =over 4 @@ -2630,8 +2605,7 @@ =item RETURN -Returns a list of triples, each triple containing the property name, its value, and a URL or -citation. +Returns a list of tuples, each tuple containing the property name and its values. =back @@ -2641,16 +2615,20 @@ # Get the parameters. my ($self, $featureID) = @_; # Get the properties. - my @retVal = $self->GetAll(['HasProperty', 'Property'], "HasProperty(from-link) = ?", [$featureID], - ['Property(property-name)', 'Property(property-value)', - 'HasProperty(evidence)']); + my @attributes = $self->{_ca}->GetAttributes($featureID); + # Strip the feature ID off each tuple. + my @retVal = (); + for my $attributeRow (@attributes) { + shift @{$attributeRow}; + push @retVal, $attributeRow; + } # Return the resulting list. return @retVal; } =head3 DiagramName -C<< my $diagramName = $sprout->DiagramName($diagramID); >> + my $diagramName = $sprout->DiagramName($diagramID); Return the descriptive name of a diagram. @@ -2678,10 +2656,11 @@ =head3 PropertyID -C<< my $id = $sprout->PropertyID($propName, $propValue); >> + my $id = $sprout->PropertyID($propName, $propValue); Return the ID of the specified property name and value pair, if the -pair exists. +pair exists. Only a small subset of the FIG attributes are stored as +Sprout properties, mostly for use in search optimization. =over 4 @@ -2714,7 +2693,7 @@ =head3 MergedAnnotations -C<< my @annotationList = $sprout->MergedAnnotations(\@list); >> + my @annotationList = $sprout->MergedAnnotations(\@list); Returns a merged list of the annotations for the features in a list. Each annotation is represented by a 4-tuple of the form C<($fid, $timestamp, $userID, $annotation)>, where @@ -2763,7 +2742,7 @@ =head3 RoleNeighbors -C<< my @roleList = $sprout->RoleNeighbors($roleID); >> + my @roleList = $sprout->RoleNeighbors($roleID); Returns a list of the roles that occur in the same diagram as the specified role. Because diagrams and roles are in a many-to-many relationship with each other, the list is @@ -2806,7 +2785,7 @@ =head3 FeatureLinks -C<< my @links = $sprout->FeatureLinks($featureID); >> + my @links = $sprout->FeatureLinks($featureID); Return a list of the web hyperlinks associated with a feature. The web hyperlinks are to external websites describing either the feature itself or the organism containing it @@ -2837,7 +2816,7 @@ =head3 SubsystemsOf -C<< my %subsystems = $sprout->SubsystemsOf($featureID); >> + my %subsystems = $sprout->SubsystemsOf($featureID); Return a hash describing all the subsystems in which a feature participates. Each subsystem is mapped to the roles the feature performs. @@ -2885,7 +2864,7 @@ =head3 SubsystemList -C<< my @subsystems = $sprout->SubsystemList($featureID); >> + my @subsystems = $sprout->SubsystemList($featureID); Return a list containing the names of the subsystems in which the specified feature participates. Unlike L, this method only returns the @@ -2917,7 +2896,7 @@ =head3 GenomeSubsystemData -C<< my %featureData = $sprout->GenomeSubsystemData($genomeID); >> + my %featureData = $sprout->GenomeSubsystemData($genomeID); Return a hash mapping genome features to their subsystem roles. @@ -2977,7 +2956,7 @@ =head3 RelatedFeatures -C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >> + my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); Return a list of the features which are bi-directional best hits of the specified feature and have been assigned the specified function by the specified user. If no such features exists, @@ -3028,7 +3007,7 @@ =head3 TaxonomySort -C<< my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs); >> + my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs); Return a list formed by sorting the specified features by the taxonomy of the containing genome. This will cause genomes from similar organisms to float close to each other. @@ -3063,7 +3042,7 @@ my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?", [$fid], 'Genome(taxonomy)'); # Add this feature to the hash buffer. - Tracer::AddToListMap(\%hashBuffer, $taxonomy, $fid); + push @{$hashBuffer{$taxonomy}}, $fid; } # Sort the keys and get the elements. my @retVal = (); @@ -3076,7 +3055,7 @@ =head3 Protein -C<< my $protein = Sprout::Protein($sequence, $table); >> + my $protein = Sprout::Protein($sequence, $table); Translate a DNA sequence into a protein sequence. @@ -3162,7 +3141,7 @@ =head3 LoadInfo -C<< my ($dirName, @relNames) = $sprout->LoadInfo(); >> + my ($dirName, @relNames) = $sprout->LoadInfo(); Return the name of the directory from which data is to be loaded and a list of the relation names. This information is useful when trying to analyze what needs to be put where in order @@ -3183,7 +3162,7 @@ =head3 BBHMatrix -C<< my %bbhMap = $sprout->BBHMatrix($genomeID, $cutoff, @targets); >> + my %bbhMap = $sprout->BBHMatrix($genomeID, $cutoff, @targets); Find all the bidirectional best hits for the features of a genome in a specified list of target genomes. The return value will be a hash mapping @@ -3234,9 +3213,80 @@ return %retVal; } + +=head3 SimMatrix + + my %simMap = $sprout->SimMatrix($genomeID, $cutoff, @targets); + +Find all the similarities for the features of a genome in a +specified list of target genomes. The return value will be a hash mapping +features in the original genome to their similarites in the +target genomes. + +=over 4 + +=item genomeID + +ID of the genome whose features are to be examined for similarities. + +=item cutoff + +A cutoff value. Only hits with a score lower than the cutoff will be returned. + +=item targets + +List of target genomes. Only pairs originating in the original +genome and landing in one of the target genomes will be returned. + +=item RETURN + +Returns a hash mapping each feature in the original genome to a hash mapping its +similar pegs in the target genomes to their scores. + +=back + +=cut + +sub SimMatrix { + # Get the parameters. + my ($self, $genomeID, $cutoff, @targets) = @_; + # Declare the return variable. + my %retVal = (); + # Get the list of features in the source organism. + my @fids = $self->FeaturesOf($genomeID); + # Ask for the sims. We only want similarities to fig features. + my $simList = FIGRules::GetNetworkSims($self, \@fids, {}, 1000, $cutoff, "fig"); + if (! defined $simList) { + Confess("Unable to retrieve similarities from server."); + } else { + Trace("Processing sims.") if T(3); + # We now have a set of sims that we need to convert into a hash of hashes. First, we + # Create a hash for the target genomes. + my %targetHash = map { $_ => 1 } @targets; + for my $simData (@{$simList}) { + # Get the PEGs and the score. + my ($peg1, $peg2, $score) = ($simData->id1, $simData->id2, $simData->psc); + # Insure the second ID is in the target list. + my ($genome2) = FIGRules::ParseFeatureID($peg2); + if (exists $targetHash{$genome2}) { + # Here it is. Now we need to add it to the return hash. How we do that depends + # on whether or not $peg1 is new to us. + if (! exists $retVal{$peg1}) { + $retVal{$peg1} = { $peg2 => $score }; + } else { + $retVal{$peg1}->{$peg2} = $score; + } + } + } + } + # Return the result. + return %retVal; +} + + =head3 LowBBHs -C<< my %bbhMap = $sprout->LowBBHs($featureID, $cutoff); >> + my %bbhMap = $sprout->LowBBHs($featureID, $cutoff); Return the bidirectional best hits of a feature whose score is no greater than a specified cutoff value. A higher cutoff value will allow inclusion of hits with @@ -3280,7 +3330,7 @@ =head3 Sims -C<< my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters); >> + my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters); Get a list of similarities for a specified feature. Similarity information is not kept in the Sprout database; rather, they are retrieved from a network server. The similarities are @@ -3290,14 +3340,15 @@ Similarities can be either raw or expanded. The raw similarities are basic hits between features with similar DNA. Expanding a raw similarity drags in any features considered substantially identical. So, for example, if features B, -B, and B are all substatially identical to B, then a raw similarity +B, and B are all substantially identical to B, then a raw similarity B<[C,A]> would be expanded to B<[C,A] [C,A1] [C,A2] [C,A3]>. =over 4 =item fid -ID of the feature whose similarities are desired. +ID of the feature whose similarities are desired, or reference to a list of IDs +of features whose similarities are desired. =item maxN @@ -3345,7 +3396,7 @@ =head3 IsAllGenomes -C<< my $flag = $sprout->IsAllGenomes(\@list, \@checkList); >> + my $flag = $sprout->IsAllGenomes(\@list, \@checkList); Return TRUE if all genomes in the second list are represented in the first list at least one. Otherwise, return FALSE. If the second list is omitted, the first list is @@ -3394,7 +3445,7 @@ =head3 GetGroups -C<< my %groups = $sprout->GetGroups(\@groupList); >> + my %groups = $sprout->GetGroups(\@groupList); Return a hash mapping each group to the IDs of the genomes in the group. A list of groups may be specified, in which case only those groups will be @@ -3426,14 +3477,9 @@ [$FIG_Config::otherGroup], ['Genome(id)', 'Genome(primary-group)']); # Loop through the genomes found. for my $genome (@genomes) { - # Pop this genome's ID off the current list. - my @groups = @{$genome}; - my $genomeID = shift @groups; - # Loop through the groups, adding the genome ID to each group's - # list. - for my $group (@groups) { - Tracer::AddToListMap(\%retVal, $group, $genomeID); - } + # Get the genome ID and group, and add this genome to the group's list. + my ($genomeID, $group) = @{$genome}; + push @{$retVal{$group}}, $genomeID; } } # Return the hash we just built. @@ -3442,7 +3488,7 @@ =head3 MyGenomes -C<< my @genomes = Sprout::MyGenomes($dataDir); >> + my @genomes = Sprout::MyGenomes($dataDir); Return a list of the genomes to be included in the Sprout. @@ -3474,7 +3520,7 @@ =head3 LoadFileName -C<< my $fileName = Sprout::LoadFileName($dataDir, $tableName); >> + my $fileName = Sprout::LoadFileName($dataDir, $tableName); Return the name of the load file for the specified table in the specified data directory. @@ -3515,7 +3561,7 @@ =head3 DeleteGenome -C<< my $stats = $sprout->DeleteGenome($genomeID, $testFlag); >> + my $stats = $sprout->DeleteGenome($genomeID, $testFlag); Delete a genome from the database. @@ -3551,11 +3597,10 @@ =head3 Fix -C<< my %fixedHash = Sprout::Fix(%groupHash); >> + my %fixedHash = $sprout->Fix(%groupHash); -Prepare a genome group hash (like that returned by L for processing. -Groups with the same primary name will be combined. The primary name is the -first capitalized word in the group name. +Prepare a genome group hash (like that returned by L) for processing. +The groups will be combined into the appropriate super-groups. =over 4 @@ -3573,19 +3618,26 @@ sub Fix { # Get the parameters. - my (%groupHash) = @_; + my ($self, %groupHash) = @_; # Create the result hash. my %retVal = (); + # Get the super-group table. + my %superTable = $self->CheckGroupFile(); # Copy over the genomes. for my $groupID (keys %groupHash) { - # Make a safety copy of the group ID. - my $realGroupID = $groupID; - # Yank the primary name. + # Get the super-group name. + my $realGroupID; if ($groupID =~ /([A-Z]\w+)/) { - $realGroupID = $1; + if (! defined($superTable{$1})) { + Confess("Super-group name not found for group $groupID."); + } else { + $realGroupID = $1; + } + } else { + Confess("Invalid group name $groupID."); } # Append this group's genomes into the result hash. - Tracer::AddToListMap(\%retVal, $realGroupID, @{$groupHash{$groupID}}); + push @{$retVal{$realGroupID}}, @{$groupHash{$groupID}}; } # Return the result hash. return %retVal; @@ -3593,7 +3645,7 @@ =head3 GroupPageName -C<< my $name = $sprout->GroupPageName($group); >> + my $name = $sprout->GroupPageName($group); Return the name of the page for the specified NMPDR group. @@ -3618,146 +3670,113 @@ # Declare the return variable. my $retVal; # Check for the group file data. - if (! defined $self->{groupHash}) { - # Read the group file. - my %groupData = Sprout::ReadGroupFile($self->{_options}->{dataDir} . "/groups.tbl"); - # Store it in our object. - $self->{groupHash} = \%groupData; - } + my %superTable = $self->CheckGroupFile(); # Compute the real group name. - my $realGroup = $group; if ($group =~ /([A-Z]\w+)/) { - $realGroup = $1; + my $realGroup = $1; + if (! defined($superTable{$1})) { + Confess("No super-group found for \"$group\"."); + } else { + $retVal = "../content/$superTable{$1}->{page}"; + } + } else { + Confess("\"group\" is not a valid group name."); } - # Return the page name. - $retVal = "../content/" . $self->{groupHash}->{$realGroup}->[1]; # Return the result. return $retVal; } -=head3 ReadGroupFile - -C<< my %groupData = Sprout::ReadGroupFile($groupFileName); >> - -Read in the data from the specified group file. The group file contains information -about each of the NMPDR groups. - -=over 4 - -=item name - -Name of the group. - -=item page - -Name of the group's page on the web site (e.g. C for -Campylobacter) -=item genus +=head3 AddProperty -Genus of the group + $sprout->AddProperty($featureID, $key, @values); -=item species +Add a new attribute value (Property) to a feature. -Species of the group, or an empty string if the group is for an entire -genus. If the group contains more than one species, the species names -should be separated by commas. +=over 4 -=back +=item peg -The parameters to this method are as follows +ID of the feature to which the attribute is to be added. -=over 4 +=item key -=item groupFile +Name of the attribute (key). -Name of the file containing the group data. +=item values -=item RETURN - -Returns a hash keyed on group name. The value of each hash +Values of the attribute. =back =cut - -sub ReadGroupFile { +#: Return Type ; +sub AddProperty { # Get the parameters. - my ($groupFileName) = @_; - # Declare the return variable. - my %retVal; - # Read the group file. - my @groupLines = Tracer::GetFile($groupFileName); - for my $groupLine (@groupLines) { - my ($name, $page, $genus, $species) = split(/\t/, $groupLine); - $retVal{$name} = [$page, $genus, $species]; - } - # Return the result. - return %retVal; + my ($self, $featureID, $key, @values) = @_; + # Add the property using the attached attributes object. + $self->{_ca}->AddAttribute($featureID, $key, @values); } -=head3 AddProperty - -C<< my = $sprout->AddProperty($featureID, $key, $value, $url); >> - -Add a new attribute value (Property) to a feature. In the SEED system, attributes can -be added to almost any object. In Sprout, they can only be added to features. In -Sprout, attributes are implemented using I. A property represents a key/value -pair. If the particular key/value pair coming in is not already in the database, a new -B record is created to hold it. - -=over 4 +=head3 CheckGroupFile -=item peg + my %groupData = $sprout->CheckGroupFile(); -ID of the feature to which the attribute is to be replied. +Get the group file hash. The group file hash describes the relationship +between a group and the super-group to which it belongs for purposes of +display. The super-group name is computed from the first capitalized word +in the actual group name. For each super-group, the group file contains +the page name and a list of the species expected to be in the group. +Each species is specified by a genus and a species name. A species name +of C<0> implies an entire genus. -=item key +This method returns a hash from super-group names to a hash reference. Each +resulting hash reference contains the following fields. -Name of the attribute (key). +=over 4 -=item value +=item page -Value of the attribute. +The super-group's web page in the NMPDR. -=item url +=item contents -URL or text citation from which the property was obtained. +A list of 2-tuples, each containing a genus name followed by a species name +(or 0, indicating all species). This list indicates which organisms belong +in the super-group. =back =cut -#: Return Type ; -sub AddProperty { + +sub CheckGroupFile{ # Get the parameters. - my ($self, $featureID, $key, $value, $url) = @_; - # Declare the variable to hold the desired property ID. - my $propID; - # Attempt to find a property record for this key/value pair. - my @properties = $self->GetFlat(['Property'], - "Property(property-name) = ? AND Property(property-value) = ?", - [$key, $value], 'Property(id)'); - if (@properties) { - # Here the property is already in the database. We save its ID. - $propID = $properties[0]; - # Here the property value does not exist. We need to generate an ID. It will be set - # to a number one greater than the maximum value in the database. This call to - # GetAll will stop after one record. - my @maxProperty = $self->GetAll(['Property'], "ORDER BY Property(id) DESC", [], ['Property(id)'], - 1); - $propID = $maxProperty[0]->[0] + 1; - # Insert the new property value. - $self->Insert('Property', { 'property-name' => $key, 'property-value' => $value, id => $propID }); + my ($self) = @_; + # Check to see if we already have this hash. + if (! defined $self->{groupHash}) { + # We don't, so we need to read it in. + my %groupHash; + # Read the group file. + my @groupLines = Tracer::GetFile("$FIG_Config::sproutData/groups.tbl"); + # Loop through the list of sort-of groups. + for my $groupLine (@groupLines) { + my ($name, $page, @contents) = split /\t/, $groupLine; + $groupHash{$name} = { page => $page, + contents => [ map { [ split /\s*,\s*/, $_ ] } @contents ] + }; + } + # Save the hash. + $self->{groupHash} = \%groupHash; } - # Now we connect the incoming feature to the property. - $self->Insert('HasProperty', { 'from-link' => $featureID, 'to-link' => $propID, evidence => $url }); + # Return the result. + return %{$self->{groupHash}}; } =head2 Virtual Methods =head3 CleanKeywords -C<< my $cleanedString = $sprout->CleanKeywords($searchExpression); >> + my $cleanedString = $sprout->CleanKeywords($searchExpression); Clean up a search expression or keyword list. This involves converting the periods in EC numbers to underscores, converting non-leading minus signs to underscores, @@ -3807,7 +3826,8 @@ A functional assignment is always of the form - CIC< function to\n>I + set YYYY function to + ZZZZ where I is the B, and I is the actual functional role. In most cases, the user and the assigning user (from MadeAnnotation) will be the same, but that is @@ -3860,6 +3880,40 @@ return @retVal; } +=head3 _CheckFeature + + my $flag = $sprout->_CheckFeature($fid); + +Return TRUE if the specified FID is probably an NMPDR feature ID, else FALSE. + +=over 4 + +=item fid + +Feature ID to check. + +=item RETURN + +Returns TRUE if the FID is for one of the NMPDR genomes, else FALSE. + +=back + +=cut + +sub _CheckFeature { + # Get the parameters. + my ($self, $fid) = @_; + # Insure we have a genome hash. + if (! defined $self->{genomeHash}) { + my %genomeHash = map { $_ => 1 } $self->GetFlat(['Genome'], "", [], 'Genome(id)'); + $self->{genomeHash} = \%genomeHash; + } + # Get the feature's genome ID. + my ($genomeID) = FIGRules::ParseFeatureID($fid); + # Return an indicator of whether or not the genome ID is in the hash. + return ($self->{genomeHash}->{$genomeID} ? 1 : 0); +} + =head3 FriendlyTimestamp Convert a time number to a user-friendly time stamp for display. @@ -3887,4 +3941,4 @@ } -1; +1; \ No newline at end of file