--- Sprout.pm 2005/06/09 19:06:55 1.14 +++ Sprout.pm 2007/08/20 23:29:24 1.101 @@ -1,18 +1,22 @@ package Sprout; - use Data::Dumper; - use strict; - use Carp; - use DBKernel; - use XML::Simple; - use DBQuery; - use DBObject; - use ERDB; - use Tracer; - use FIGRules; - use Stats; + require Exporter; + use ERDB; + @ISA = qw(Exporter ERDB); + use Data::Dumper; + use strict; + use DBKernel; + use XML::Simple; + use DBQuery; + use ERDBObject; + use Tracer; + use FIGRules; + use FidCheck; + use Stats; use POSIX qw(strftime); - + use BasicLocation; + use CustomAttributes; + use RemoteCustomAttributes; =head1 Sprout Database Manipulation Object @@ -32,6 +36,8 @@ 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. + =cut #: Constructor SFXlate->new_sprout_only(); @@ -62,14 +68,18 @@ * B name of the XML file containing the database definition (default C) -* B user name and password, delimited by a slash (default C) +* B user name and password, delimited by a slash (default same as SEED) * B connection port (default C<0>) +* B connection socket (default same as SEED) + * B maximum number of residues per feature segment, (default C<4500>) * B maximum number of residues per sequence, (default C<8000>) +* B suppresses the connection to the database if TRUE, else FALSE + =back For example, the following constructor call specifies a database named I and a user name of @@ -81,34 +91,62 @@ =cut sub new { - # Get the parameters. - my ($class, $dbName, $options) = @_; - # Compute the options. We do this by starting with a table of defaults and overwriting with - # the incoming data. - my $optionTable = Tracer::GetOptions({ - dbType => 'mysql', # database type - dataDir => 'Data', # data file directory - xmlFileName => 'SproutDBD.xml', # database definition file name - userData => 'root/', # user name and password - port => 0, # database connection port - maxSegmentLength => 4500, # maximum feature segment length - maxSequenceLength => 8000, # maximum contig sequence length - }, $options); - # Get the data directory. - my $dataDir = $optionTable->{dataDir}; - # Extract the user ID and password. - $optionTable->{userData} =~ m!([^/]*)/(.*)$!; - my ($userName, $password) = ($1, $2); - # Connect to the database. - my $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName, $password, $optionTable->{port}); - # Create the ERDB object. - my $xmlFileName = "$optionTable->{xmlFileName}"; - my $erdb = ERDB->new($dbh, $xmlFileName); - # Create this object. - my $self = { _erdb => $erdb, _options => $optionTable, _xmlName => $xmlFileName }; - # Bless and return it. - bless $self; - return $self; + # Get the parameters. + my ($class, $dbName, $options) = @_; + # Compute the DBD directory. + my $dbd_dir = (defined($FIG_Config::dbd_dir) ? $FIG_Config::dbd_dir : + $FIG_Config::fig ); + # Compute the options. We do this by starting with a table of defaults and overwriting with + # the incoming data. + my $optionTable = Tracer::GetOptions({ + dbType => $FIG_Config::dbms, + # database type + dataDir => $FIG_Config::sproutData, + # data file directory + xmlFileName => "$dbd_dir/SproutDBD.xml", + # database definition file name + userData => "$FIG_Config::dbuser/$FIG_Config::dbpass", + # user name and password + port => $FIG_Config::dbport, + # database connection port + sock => $FIG_Config::dbsock, + host => $FIG_Config::dbhost, + maxSegmentLength => 4500, # maximum feature segment length + maxSequenceLength => 8000, # maximum contig sequence length + noDBOpen => 0, # 1 to suppress the database open + }, $options); + # Get the data directory. + my $dataDir = $optionTable->{dataDir}; + # Extract the user ID and password. + $optionTable->{userData} =~ m!([^/]*)/(.*)$!; + my ($userName, $password) = ($1, $2); + # Connect to the database. + my $dbh; + if (! $optionTable->{noDBOpen}) { + $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName, + $password, $optionTable->{port}, $optionTable->{host}, $optionTable->{sock}); + } + # Create the ERDB object. + my $xmlFileName = "$optionTable->{xmlFileName}"; + my $retVal = ERDB::new($class, $dbh, $xmlFileName); + # Add the option table and XML file name. + $retVal->{_options} = $optionTable; + $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 @@ -124,8 +162,8 @@ =cut #: Return Type $; sub MaxSegment { - my ($self) = @_; - return $self->{_options}->{maxSegmentLength}; + my ($self) = @_; + return $self->{_options}->{maxSegmentLength}; } =head3 MaxSequence @@ -139,298 +177,260 @@ =cut #: Return Type $; sub MaxSequence { - my ($self) = @_; - return $self->{_options}->{maxSequenceLength}; + my ($self) = @_; + return $self->{_options}->{maxSequenceLength}; } -=head3 Get - -C<< my $query = $sprout->Get(\@objectNames, $filterClause, \@parameterList); >> - -This method allows a general query against the Sprout data using a specified filter clause. - -The filter is a standard WHERE/ORDER BY clause with question marks as parameter markers and each -field name represented in the form B(I)>. For example, the -following call requests all B objects for the genus specified in the variable -$genus. - -C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ?", [$genus]); >> - -The WHERE clause contains a single question mark, so there is a single additional -parameter representing the parameter value. It would also be possible to code - -C<< $query = $sprout->Get(['Genome'], "Genome(genus) = \'$genus\'"); >> - -however, this version of the call would generate a syntax error if there were any quote -characters inside the variable C<$genus>. - -The use of the strange parenthesized notation for field names enables us to distinguish -hyphens contained within field names from minus signs that participate in the computation -of the WHERE clause. All of the methods that manipulate fields will use this same notation. - -It is possible to specify multiple entity and relationship names in order to retrieve more than -one object's data at the same time, which allows highly complex joined queries. For example, - -C<< $query = $sprout->Get(['Genome', 'ComesFrom', 'Source'], "Genome(genus) = ?", [$genus]); >> - -This query returns all the genomes for a particular genus and allows access to the -sources from which they came. The join clauses to go from Genome to Source are generated -automatically. - -Finally, the filter clause can contain sort information. To do this, simply put an C -clause at the end of the filter. Field references in the ORDER BY section follow the same rules -as they do in the filter itself; in other words, each one must be of the form B(I)>. -For example, the following filter string gets all genomes for a particular genus and sorts -them by species name. - -C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ? ORDER BY Genome(species)", [$genus]); >> +=head3 Load -It is also permissible to specify I an ORDER BY clause. For example, the following invocation gets -all genomes ordered by genus and species. +C<< $sprout->Load($rebuild); >>; -C<< $query = $sprout->Get(['Genome'], "ORDER BY Genome(genus), Genome(species)"); >> +Load the database from files in the data directory, optionally re-creating the tables. -Odd things may happen if one of the ORDER BY fields is in a secondary relation. So, for example, an -attempt to order Bs by alias may (depending on the underlying database engine used) cause -a single feature to appear more than once. +This method always deletes the data from the database before loading, even if the tables are not +re-created. The data is loaded into the relations from files in the data directory either having the +same name as the target relation with no extension or with an extension of C<.dtx>. Files without an +extension are used in preference to the files with an extension. -If multiple names are specified, then the query processor will automatically determine a -join path between the entities and relationships. The algorithm used is very simplistic. -In particular, you can't specify any entity or relationship more than once, and if a -relationship is recursive, the path is determined by the order in which the entity -and the relationship appear. For example, consider a recursive relationship B -which relates B objects to other B objects. If the join path is -coded as C<['People', 'IsParentOf']>, then the people returned will be parents. If, however, -the join path is C<['IsParentOf', 'People']>, then the people returned will be children. +The files are loaded based on the presumption that each line of the file is a record in the +relation, and the individual fields are delimited by tabs. Tab and new-line characters inside +fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must +be presented in the order given in the relation tables produced by the L method. =over 4 -=item objectNames - -List containing the names of the entity and relationship objects to be retrieved. - -=item filterClause - -WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can -be parameterized with parameter markers (C). Each field used must be specified in the standard form -B(I)>. Any parameters specified in the filter clause should be added to the -parameter list as additional parameters. The fields in a filter clause can come from primary -entity relations, relationship relations, or secondary entity relations; however, all of the -entities and relationships involved must be included in the list of object names. - -=item parameterList +=item rebuild -List of the parameters to be substituted in for the parameters marks in the filter clause. +TRUE if the data tables need to be created or re-created, else FALSE =item RETURN -Returns a B that can be used to iterate through all of the results. +Returns a statistical object containing the number of records read, the number of duplicates found, +the number of errors, and a list of the error messages. =back =cut - -sub Get { - # Get the parameters. - my ($self, $objectNames, $filterClause, $parameterList) = @_; - # We differ from the ERDB Get method in that the parameter list is passed in as a list reference - # rather than a list of parameters. The next step is to convert the parameters from a reference - # to a real list. We can only do this if the parameters have been specified. - my @parameters; - if ($parameterList) { @parameters = @{$parameterList}; } - return $self->{_erdb}->Get($objectNames, $filterClause, @parameters); +#: Return Type %; +sub Load { + # Get the parameters. + my ($self, $rebuild) = @_; + # Load the tables from the data directory. + my $retVal = $self->LoadTables($self->{_options}->{dataDir}, $rebuild); + # Return the statistics. + return $retVal; } -=head3 GetEntity +=head3 LoadUpdate -C<< my $entityObject = $sprout->GetEntity($entityType, $ID); >> +C<< my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >> -Return an object describing the entity instance with a specified ID. +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 +file in the data directory, either with the same name as the table, or with a C<.dtx> suffix. So, +for example, to make updates to the B relation, there must be a +C file in the data directory. Unlike a full load, files without an extension +are not examined. This allows update files to co-exist with files from an original load. =over 4 -=item entityType +=item truncateFlag -Entity type name. +TRUE if the tables should be rebuilt before loading, else FALSE. A value of TRUE therefore causes +current data and schema of the tables to be replaced, while a value of FALSE means the new data +is added to the existing data in the various relations. -=item ID +=item tableList -ID of the desired entity. +List of the tables to be updated. =item RETURN -Returns a B representing the desired entity instance, or an undefined value if no -instance is found with the specified key. +Returns a statistical object containing the number of records read, the number of duplicates found, +the number of errors encountered, and a list of error messages. =back =cut - -sub GetEntity { - # Get the parameters. - my ($self, $entityType, $ID) = @_; - # Call the ERDB method. - return $self->{_erdb}->GetEntity($entityType, $ID); +#: Return Type $%; +sub LoadUpdate { + # Get the parameters. + my ($self, $truncateFlag, $tableList) = @_; + # Declare the return value. + my $retVal = Stats->new(); + # Get the data directory. + my $optionTable = $self->{_options}; + my $dataDir = $optionTable->{dataDir}; + # Loop through the incoming table names. + for my $tableName (@{$tableList}) { + # Find the table's file. + my $fileName = LoadFileName($dataDir, $tableName); + if (! $fileName) { + 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); + # Accumulate the resulting statistics. + $retVal->Accumulate($result); + } + } + # Return the statistics. + return $retVal; } -=head3 GetEntityValues +=head3 GenomeCounts -C<< my @values = GetEntityValues($entityType, $ID, \@fields); >> +C<< my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete); >> -Return a list of values from a specified entity instance. +Count the number of genomes in each domain. If I<$complete> is TRUE, only complete +genomes will be included in the counts. =over 4 -=item entityType - -Entity type name. - -=item ID - -ID of the desired entity. +=item complete -=item fields - -List of field names, each of the form IC<(>IC<)>. +TRUE if only complete genomes are to be counted, FALSE if all genomes are to be +counted =item RETURN -Returns a flattened list of the values of the specified fields for the specified entity. +A six-element list containing the number of genomes in each of six categories-- +Archaea, Bacteria, Eukaryota, Viral, Environmental, and Unknown, respectively. =back =cut -#: Return Type @; -sub GetEntityValues { - # Get the parameters. - my ($self, $entityType, $ID, $fields) = @_; - # Call the ERDB method. - return $self->{_erdb}->GetEntityValues($entityType, $ID, $fields); + +sub GenomeCounts { + # Get the parameters. + my ($self, $complete) = @_; + # Set the filter based on the completeness flag. + my $filter = ($complete ? "Genome(complete) = 1" : ""); + # Get all the genomes and the related taxonomy information. + my @genomes = $self->GetAll(['Genome'], $filter, [], ['Genome(id)', 'Genome(taxonomy)']); + # Clear the counters. + my ($arch, $bact, $euk, $vir, $env, $unk) = (0, 0, 0, 0, 0, 0); + # Loop through, counting the domains. + for my $genome (@genomes) { + if ($genome->[1] =~ /^archaea/i) { ++$arch } + elsif ($genome->[1] =~ /^bacter/i) { ++$bact } + elsif ($genome->[1] =~ /^eukar/i) { ++$euk } + elsif ($genome->[1] =~ /^vir/i) { ++$vir } + elsif ($genome->[1] =~ /^env/i) { ++$env } + else { ++$unk } + } + # Return the counts. + return ($arch, $bact, $euk, $vir, $env, $unk); } -=head3 ShowMetaData +=head3 ContigCount -C<< $sprout->ShowMetaData($fileName); >> +C<< my $count = $sprout->ContigCount($genomeID); >> -This method outputs a description of the database to an HTML file in the data directory. +Return the number of contigs for the specified genome ID. =over 4 -=item fileName +=item genomeID + +ID of the genome whose contig count is desired. -Fully-qualified name to give to the output file. +=item RETURN + +Returns the number of contigs for the specified genome. =back =cut -sub ShowMetaData { - # Get the parameters. - my ($self, $fileName) = @_; - # Compute the file name. - my $options = $self->{_options}; - # Call the show method on the underlying ERDB object. - $self->{_erdb}->ShowMetaData($fileName); +sub ContigCount { + # Get the parameters. + my ($self, $genomeID) = @_; + # Get the contig count. + my $retVal = $self->GetCount(['Contig', 'HasContig'], "HasContig(from-link) = ?", [$genomeID]); + # Return the result. + return $retVal; } -=head3 Load - -C<< $sprout->Load($rebuild); >>; - -Load the database from files in the data directory, optionally re-creating the tables. +=head3 GeneMenu -This method always deletes the data from the database before loading, even if the tables are not -re-created. The data is loaded into the relations from files in the data directory either having the -same name as the target relation with no extension or with an extension of C<.dtx>. Files without an -extension are used in preference to the files with an extension. +C<< my $selectHtml = $sprout->GeneMenu(\%attributes, $filterString, \@params, $selected); >> -The files are loaded based on the presumption that each line of the file is a record in the -relation, and the individual fields are delimited by tabs. Tab and new-line characters inside -fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must -be presented in the order given in the relation tables produced by the L method. +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 +value will be the genome ID. The genomes will be sorted by genus/species name. =over 4 -=item rebuild - -TRUE if the data tables need to be created or re-created, else FALSE - -=item RETURN - -Returns a statistical object containing the number of records read, the number of duplicates found, -the number of errors, and a list of the error messages. - -=back +=item attributes -=cut -#: Return Type %; -sub Load { - # Get the parameters. - my ($self, $rebuild) = @_; - # Get the database object. - my $erdb = $self->{_erdb}; - # Load the tables from the data directory. - my $retVal = $erdb->LoadTables($self->{_options}->{dataDir}, $rebuild); - # Return the statistics. - return $retVal; -} +Reference to a hash mapping attributes to values for the SELECT tag generated. -=head3 LoadUpdate +=item filterString -C<< my %stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >> +A filter string for use in selecting the genomes. The filter string must conform +to the rules for the C<< ERDB->Get >> method. -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 -file in the data directory, either with the same name as the table, or with a C<.dtx> suffix. So, -for example, to make updates to the B relation, there must be a -C file in the data directory. Unlike a full load, files without an extension -are not examined. This allows update files to co-exist with files from an original load. +=item params -=over 4 +Reference to a list of values to be substituted in for the parameter marks in +the filter string. -=item truncateFlag +=item selected (optional) -TRUE if the tables should be rebuilt before loading, else FALSE. A value of TRUE therefore causes -current data and schema of the tables to be replaced, while a value of FALSE means the new data -is added to the existing data in the various relations. +ID of the genome to be initially selected. -=item tableList +=item fast (optional) -List of the tables to be updated. +If specified and TRUE, the contig counts will be omitted to improve performance. =item RETURN -Returns a statistical object containing the number of records read, the number of duplicates found, -the number of errors encountered, and a list of error messages. +Returns an HTML select menu with the specified genomes as selectable options. =back =cut -#: Return Type $%; -sub LoadUpdate { - # Get the parameters. - my ($self, $truncateFlag, $tableList) = @_; - # Get the database object. - my $erdb = $self->{_erdb}; - # Declare the return value. - my $retVal = Stats->new(); - # Get the data directory. - my $optionTable = $self->{_options}; - my $dataDir = $optionTable->{dataDir}; - # Loop through the incoming table names. - for my $tableName (@{$tableList}) { - # Find the table's file. - my $fileName = "$dataDir/$tableName"; - if (! -e $fileName) { - $fileName = "$fileName.dtx"; - } - # Attempt to load this table. - my $result = $erdb->LoadTable($fileName, $tableName, $truncateFlag); - # Accumulate the resulting statistics. - $retVal->Accumulate($result); - } - # Return the statistics. - return $retVal; + +sub GeneMenu { + # Get the parameters. + my ($self, $attributes, $filterString, $params, $selected, $fast) = @_; + my $slowMode = ! $fast; + # Default to nothing selected. This prevents an execution warning if "$selected" + # is undefined. + $selected = "" unless defined $selected; + Trace("Gene Menu called with slow mode \"$slowMode\" and selection \"$selected\".") if T(3); + # Start the menu. + my $retVal = "\n"; + # Return the result. + return $retVal; } =head3 Build @@ -444,10 +444,10 @@ =cut #: Return Type ; sub Build { - # Get the parameters. - my ($self) = @_; - # Create the tables. - $self->{_erdb}->CreateTables; + # Get the parameters. + my ($self) = @_; + # Create the tables. + $self->CreateTables(); } =head3 Genomes @@ -459,12 +459,12 @@ =cut #: Return Type @; sub Genomes { - # Get the parameters. - my ($self) = @_; - # Get all the genomes. - my @retVal = $self->GetFlat(['Genome'], "", [], 'Genome(id)'); - # Return the list of IDs. - return @retVal; + # Get the parameters. + my ($self) = @_; + # Get all the genomes. + my @retVal = $self->GetFlat(['Genome'], "", [], 'Genome(id)'); + # Return the list of IDs. + return @retVal; } =head3 GenusSpecies @@ -489,14 +489,14 @@ =cut #: Return Type $; sub GenusSpecies { - # Get the parameters. - my ($self, $genomeID) = @_; - # Get the data for the specified genome. - my @values = $self->GetEntityValues('Genome', $genomeID, ['Genome(genus)', 'Genome(species)', - 'Genome(unique-characterization)']); - # Format the result and return it. - my $retVal = join(' ', @values); - return $retVal; + # Get the parameters. + my ($self, $genomeID) = @_; + # Get the data for the specified genome. + my @values = $self->GetEntityValues('Genome', $genomeID, ['Genome(genus)', 'Genome(species)', + 'Genome(unique-characterization)']); + # Format the result and return it. + my $retVal = join(' ', @values); + return $retVal; } =head3 FeaturesOf @@ -525,23 +525,23 @@ =cut #: Return Type @; sub FeaturesOf { - # Get the parameters. - my ($self, $genomeID,$ftype) = @_; - # Get the features we want. - my @features; - if (!$ftype) { - @features = $self->GetFlat(['HasContig', 'IsLocatedIn'], "HasContig(from-link) = ?", - [$genomeID], 'IsLocatedIn(from-link)'); - } else { - @features = $self->GetFlat(['HasContig', 'IsLocatedIn', 'Feature'], - "HasContig(from-link) = ? AND Feature(feature-type) = ?", - [$genomeID, $ftype], 'IsLocatedIn(from-link)'); - } - # Return the list with duplicates merged out. We need to merge out duplicates because - # a feature will appear twice if it spans more than one contig. - my @retVal = Tracer::Merge(@features); - # Return the list of feature IDs. - return @retVal; + # Get the parameters. + my ($self, $genomeID,$ftype) = @_; + # Get the features we want. + my @features; + if (!$ftype) { + @features = $self->GetFlat(['HasContig', 'IsLocatedIn'], "HasContig(from-link) = ?", + [$genomeID], 'IsLocatedIn(from-link)'); + } else { + @features = $self->GetFlat(['HasContig', 'IsLocatedIn', 'Feature'], + "HasContig(from-link) = ? AND Feature(feature-type) = ?", + [$genomeID, $ftype], 'IsLocatedIn(from-link)'); + } + # Return the list with duplicates merged out. We need to merge out duplicates because + # a feature will appear twice if it spans more than one contig. + my @retVal = Tracer::Merge(@features); + # Return the list of feature IDs. + return @retVal; } =head3 FeatureLocation @@ -570,50 +570,24 @@ =item RETURN Returns a list of the feature's contig segments. The locations are returned as a list in a list -context and as a space-delimited string in a scalar context. +context and as a comma-delimited string in a scalar context. =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]); - # 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 == $prevBeg - $prevLen) || - ($dir eq "+" && $beg == $prevBeg + $prevLen)) { - # Here we need to merge two segments. 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); - # Add the specifier to the list. - push @retVal, "${contigID}_$beg$dir$len"; - } - # Return the list in the format indicated by the context. - return (wantarray ? @retVal : join(' ', @retVal)); + # Get the parameters. + my ($self, $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 = split /\s*,\s*/, $locString; + # Return the list in the format indicated by the context. + return (wantarray ? @retVal : join(',', @retVal)); } =head3 ParseLocation @@ -637,37 +611,39 @@ =back =cut -#: Return Type @; + sub ParseLocation { - # Get the parameter. Note that if we're called as an instance method, we ignore + # Get the parameter. Note that if we're called as an instance method, we ignore # the first parameter. shift if UNIVERSAL::isa($_[0],__PACKAGE__); - my ($location) = @_; - # Parse it into segments. - $location =~ /^(.*)_(\d*)([+-_])(\d*)$/; - my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4); - # If the direction is an underscore, convert it to a + or -. - if ($dir eq "_") { - if ($start < $len) { - $dir = "+"; - $len = $len - $start + 1; - } else { - $dir = "-"; - $len = $start - $len + 1; - } - } - # Return the result. - return ($contigID, $start, $dir, $len); + my ($location) = @_; + # Parse it into segments. + $location =~ /^(.+)_(\d+)([+\-_])(\d+)$/; + my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4); + # If the direction is an underscore, convert it to a + or -. + if ($dir eq "_") { + if ($start < $len) { + $dir = "+"; + $len = $len - $start + 1; + } else { + $dir = "-"; + $len = $start - $len + 1; + } + } + # Return the result. + return ($contigID, $start, $dir, $len); } + + =head3 PointLocation C<< 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 -beyond the location, an undefined value will be returned. It is assumed that the offset -is for the location's contig. The location can either be new-style (using a C<+> or C<-> +beyond the location, an undefined value will be returned. It is assumed that the offset +is for the location's contig. The location can either be new-style (using a C<+> or C<-> and a length) or old-style (using C<_> and start and end positions. =over 4 @@ -691,12 +667,12 @@ =back =cut -#: Return Type $; + sub PointLocation { - # Get the parameter. Note that if we're called as an instance method, we ignore + # Get the parameter. Note that if we're called as an instance method, we ignore # the first parameter. shift if UNIVERSAL::isa($_[0],__PACKAGE__); - my ($location, $point) = @_; + my ($location, $point) = @_; # Parse out the location elements. Note that this works on both old-style and new-style # locations. my ($contigID, $start, $dir, $len) = ParseLocation($location); @@ -720,12 +696,17 @@ should be of the form returned by L when in a list context. In other words, each location is of the form IC<_>III. +For example, the following would return the DNA sequence for contig C<83333.1:NC_000913> +between positions 1401 and 1532, inclusive. + + my $sequence = $sprout->DNASeq('83333.1:NC_000913_1401_1532'); + =over 4 =item locationList -List of location specifiers, each in the form IC<_>III (see -L for more about this format). +List of location specifiers, each in the form IC<_>III or +IC<_>IC<_>I (see L for more about this format). =item RETURN @@ -736,56 +717,59 @@ =cut #: Return Type $; sub DNASeq { - # Get the parameters. - my ($self, $locationList) = @_; - # Create the return string. - my $retVal = ""; - # Loop through the locations. - for my $location (@{$locationList}) { - # Set up a variable to contain the DNA at this location. - my $locationDNA = ""; - # Parse out the contig ID, the beginning point, the direction, and the end point. - my ($contigID, $beg, $dir, $len) = ParseLocation($location); - # Now we must create a query to return all the sequences in the contig relevant to the region - # specified. First, we compute the start and stop points when reading through the sequences. - # For a forward transcription, the start point is the beginning; for a backward transcription, - # the start point is the ending. Note that in the latter case we must reverse the DNA string - # before putting it in the return value. - my ($start, $stop); - if ($dir eq "+") { - $start = $beg; - $stop = $beg + $len - 1; - } else { - $start = $beg + $len + 1; - $stop = $beg; - } - my $query = $self->Get(['IsMadeUpOf','Sequence'], - "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " . - " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)", - [$contigID, $start, $stop]); - # Loop through the sequences. - while (my $sequence = $query->Fetch()) { - # Determine whether the location starts, stops, or continues through this sequence. - my ($startPosition, $sequenceData, $sequenceLength) = - $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)', - 'IsMadeUpOf(len)']); - my $stopPosition = $startPosition + $sequenceLength; - # Figure out the start point and length of the relevant section. - my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition); - my $len = ($stopPosition <= $stop ? $stopPosition : $stop) - $startPosition - $pos1; - # Add the relevant data to the location data. - $locationDNA .= substr($sequenceData, $pos1, $len); - } - # Add this location's data to the return string. Note that we may need to reverse it. - if ($dir eq '+') { - $retVal .= $locationDNA; - } else { - $locationDNA = join('', reverse split //, $locationDNA); - $retVal .= $locationDNA; - } - } - # Return the result. - return $retVal; + # Get the parameters. + my ($self, $locationList) = @_; + # Create the return string. + my $retVal = ""; + # Loop through the locations. + for my $location (@{$locationList}) { + # Set up a variable to contain the DNA at this location. + my $locationDNA = ""; + # Parse out the contig ID, the beginning point, the direction, and the end point. + my ($contigID, $beg, $dir, $len) = ParseLocation($location); + # Now we must create a query to return all the sequences in the contig relevant to the region + # specified. First, we compute the start and stop points when reading through the sequences. + # For a forward transcription, the start point is the beginning; for a backward transcription, + # the start point is the ending. Note that in the latter case we must reverse the DNA string + # before putting it in the return value. + my ($start, $stop); + Trace("Parse of \"$location\" is $beg$dir$len.") if T(SDNA => 4); + if ($dir eq "+") { + $start = $beg; + $stop = $beg + $len - 1; + } else { + $start = $beg - $len + 1; + $stop = $beg; + } + Trace("Looking for sequences containing $start through $stop.") if T(SDNA => 4); + my $query = $self->Get(['IsMadeUpOf','Sequence'], + "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " . + " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)", + [$contigID, $start, $stop]); + # Loop through the sequences. + while (my $sequence = $query->Fetch()) { + # Determine whether the location starts, stops, or continues through this sequence. + my ($startPosition, $sequenceData, $sequenceLength) = + $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)', + 'IsMadeUpOf(len)']); + my $stopPosition = $startPosition + $sequenceLength; + Trace("Sequence is from $startPosition to $stopPosition.") if T(SDNA => 4); + # Figure out the start point and length of the relevant section. + my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition); + my $len1 = ($stopPosition < $stop ? $stopPosition : $stop) + 1 - $startPosition - $pos1; + Trace("Position is $pos1 for length $len1.") if T(SDNA => 4); + # Add the relevant data to the location data. + $locationDNA .= substr($sequenceData, $pos1, $len1); + } + # Add this location's data to the return string. Note that we may need to reverse it. + if ($dir eq '+') { + $retVal .= $locationDNA; + } else { + $retVal .= FIG::reverse_comp($locationDNA); + } + } + # Return the result. + return $retVal; } =head3 AllContigs @@ -809,306 +793,513 @@ =cut #: Return Type @; sub AllContigs { - # Get the parameters. - my ($self, $genomeID) = @_; - # Ask for the genome's Contigs. - my @retVal = $self->GetFlat(['HasContig'], "HasContig(from-link) = ?", [$genomeID], - 'HasContig(to-link)'); - # Return the list of Contigs. - return @retVal; + # Get the parameters. + my ($self, $genomeID) = @_; + # Ask for the genome's Contigs. + my @retVal = $self->GetFlat(['HasContig'], "HasContig(from-link) = ?", [$genomeID], + 'HasContig(to-link)'); + # Return the list of Contigs. + return @retVal; } -=head3 ContigLength +=head3 GenomeLength -C<< my $length = $sprout->ContigLength($contigID); >> +C<< my $length = $sprout->GenomeLength($genomeID); >> -Compute the length of a contig. +Return the length of the specified genome in base pairs. =over 4 -=item contigID +=item genomeID -ID of the contig whose length is desired. +ID of the genome whose base pair count is desired. =item RETURN -Returns the number of positions in the contig. +Returns the number of base pairs in all the contigs of the specified +genome. =back =cut -#: Return Type $; -sub ContigLength { - # Get the parameters. - my ($self, $contigID) = @_; - # Get the contig's last sequence. - my $query = $self->Get(['IsMadeUpOf'], - "IsMadeUpOf(from-link) = ? ORDER BY IsMadeUpOf(start-position) DESC", - [$contigID]); - my $sequence = $query->Fetch(); - # Declare the return value. - my $retVal = 0; - # Set it from the sequence data, if any. - if ($sequence) { - my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']); - $retVal = $start + $len; - } - # Return the result. - return $retVal; + +sub GenomeLength { + # Get the parameters. + my ($self, $genomeID) = @_; + # Declare the return variable. + my $retVal = 0; + # Get the genome's contig sequence lengths. + my @lens = $self->GetFlat(['HasContig', 'IsMadeUpOf'], 'HasContig(from-link) = ?', + [$genomeID], 'IsMadeUpOf(len)'); + # Sum the lengths. + map { $retVal += $_ } @lens; + # Return the result. + return $retVal; } -=head3 GenesInRegion +=head3 FeatureCount -C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >> +C<< my $count = $sprout->FeatureCount($genomeID, $type); >> -List the features which overlap a specified region in a contig. +Return the number of features of the specified type in the specified genome. =over 4 -=item contigID - -ID of the contig containing the region of interest. - -=item start +=item genomeID -Offset of the first residue in the region of interest. +ID of the genome whose feature count is desired. -=item stop +=item type -Offset of the last residue in the region of interest. +Type of feature to count (eg. C, C, etc.). =item RETURN -Returns a three-element list. The first element is a list of feature IDs for the features that -overlap the region of interest. The second and third elements are the minimum and maximum -locations of the features provided on the specified contig. These may extend outside -the start and stop values. The first element (that is, the list of features) is sorted -roughly by location. +Returns the number of features of the specified type for the specified genome. =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 - # 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. - my %queryParms = (forward => [$contigID, '+', $start - $maximumSegmentLength + 1, $stop], - reverse => [$contigID, '-', $start, $stop + $maximumSegmentLength - 1]); - # Loop through the query parameters. - for my $parms (values %queryParms) { - # Create the query. - my $query = $self->Get(['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 - # 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; - } - } - 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]; - } - } - } - # 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); + +sub FeatureCount { + # Get the parameters. + my ($self, $genomeID, $type) = @_; + # Compute the count. + my $retVal = $self->GetCount(['HasFeature', 'Feature'], + "HasFeature(from-link) = ? AND Feature(feature-type) = ?", + [$genomeID, $type]); + # Return the result. + return $retVal; } -=head3 FType +=head3 GenomeAssignments -C<< my $ftype = $sprout->FType($featureID); >> +C<< my $fidHash = $sprout->GenomeAssignments($genomeID); >> -Return the type of a feature. +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 +assignment. =over 4 -=item featureID +=item genomeID -ID of the feature whose type is desired. +ID of the genome whose functional assignments are desired. =item RETURN -A string indicating the type of feature (e.g. peg, rna). If the feature does not exist, returns an -undefined value. +Returns a reference to a hash which maps each feature to its most recent +functional assignment. =back =cut -#: Return Type $; -sub FType { - # Get the parameters. - my ($self, $featureID) = @_; - # Get the specified feature's type. - my ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(feature-type)']); - # Return the result. - return $retVal; + +sub GenomeAssignments { + # Get the parameters. + my ($self, $genomeID) = @_; + # Declare the return variable. + my $retVal = {}; + # Query the genome's features. + my $query = $self->Get(['HasFeature', 'Feature'], "HasFeature(from-link) = ?", + [$genomeID]); + # Loop through the features. + while (my $data = $query->Fetch) { + # Get the feature ID and assignment. + my ($fid, $assignment) = $data->Values(['Feature(id)', 'Feature(assignment)']); + if ($assignment) { + $retVal->{$fid} = $assignment; + } + } + # Return the result. + return $retVal; } -=head3 FeatureAnnotations +=head3 ContigLength -C<< my @descriptors = $sprout->FeatureAnnotations($featureID); >> +C<< my $length = $sprout->ContigLength($contigID); >> -Return the annotations of a feature. +Compute the length of a contig. =over 4 -=item featureID +=item contigID -ID of the feature whose annotations are desired. +ID of the contig whose length is desired. =item RETURN -Returns a list of annotation descriptors. Each descriptor is a hash with the following fields. - -* B ID of the relevant feature. - -* B time the annotation was made, in user-friendly format. - -* B ID of the user who made the annotation - -* B text of the annotation. +Returns the number of positions in the contig. =back =cut -#: Return Type @%; -sub FeatureAnnotations { - # Get the parameters. - my ($self, $featureID) = @_; - # Create a query to get the feature's annotations and the associated users. - my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'], - "IsTargetOfAnnotation(from-link) = ?", [$featureID]); - # Create the return list. - my @retVal = (); - # Loop through the annotations. - while (my $annotation = $query->Fetch) { - # Get the fields to return. - my ($featureID, $timeStamp, $user, $text) = - $annotation->Values(['IsTargetOfAnnotation(from-link)', - 'Annotation(time)', 'MadeAnnotation(from-link)', - 'Annotation(annotation)']); - # Assemble them into a hash. - my $annotationHash = { featureID => $featureID, - timeStamp => FriendlyTimestamp($timeStamp), - user => $user, text => $text }; - # Add it to the return list. - push @retVal, $annotationHash; - } - # Return the result list. - return @retVal; +#: Return Type $; +sub ContigLength { + # Get the parameters. + my ($self, $contigID) = @_; + # Get the contig's last sequence. + my $query = $self->Get(['IsMadeUpOf'], + "IsMadeUpOf(from-link) = ? ORDER BY IsMadeUpOf(start-position) DESC", + [$contigID]); + my $sequence = $query->Fetch(); + # Declare the return value. + my $retVal = 0; + # Set it from the sequence data, if any. + if ($sequence) { + my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']); + $retVal = $start + $len - 1; + } + # Return the result. + return $retVal; } -=head3 AllFunctionsOf +=head3 ClusterPEGs -C<< my %functions = $sprout->AllFunctionsOf($featureID); >> +C<< my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs); >> -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, -Functional assignments are described in the L function. Its worth noting that -we cannot filter on the content of the annotation itself because it's a text field; however, -this is not a big problem because most features only have a small number of annotations. -Finally, if a single user has multiple functional assignments, we will only keep the most -recent one. +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. =over 4 -=item featureID +=item sub -ID of the feature whose functional assignments are desired. +Sprout subsystem object for the relevant subsystem, from the L +method. + +=item pegs + +Reference to the list of PEGs to be clustered. =item RETURN -Returns a hash mapping the functional assignment IDs to user IDs. +Returns a list of the PEGs, grouped into smaller lists by cluster number. =back =cut -#: Return Type %; -sub AllFunctionsOf { - # Get the parameters. - my ($self, $featureID) = @_; - # Get all of the feature's annotations. - my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation'], - "IsTargetOfAnnotation(from-link) = ?", - [$featureID], ['Annotation(time)', 'Annotation(annotation)']); - # Declare the return hash. - my %retVal; - # Declare a hash for insuring we only make one assignment per user. - my %timeHash = (); - # Now we sort the assignments by timestamp in reverse. - my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query; - # Loop until we run out of annotations. +#: Return Type $@@; +sub ClusterPEGs { + # Get the parameters. + my ($self, $sub, $pegs) = @_; + # Declare the return variable. + my $retVal = []; + # Loop through the PEGs, creating arrays for each cluster. + for my $pegID (@{$pegs}) { + my $clusterNumber = $sub->get_cluster_number($pegID); + # Only proceed if the PEG is in a cluster. + if ($clusterNumber >= 0) { + # Push this PEG onto the sub-list for the specified cluster number. + push @{$retVal->[$clusterNumber]}, $pegID; + } + } + # Return the result. + return $retVal; +} + +=head3 GenesInRegion + +C<< my (\@featureIDList, $beg, $end) = $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 three-element list. The first element is a list of feature IDs for the features that +overlap the region of interest. The second and third elements are the minimum and maximum +locations of the features provided on the specified contig. These may extend outside +the start and stop values. The first element (that is, the list of features) is sorted +roughly by location. + +=back + +=cut + +sub GenesInRegion { + # Get the parameters. + my ($self, $contigID, $start, $stop) = @_; + # Get the maximum segment length. + my $maximumSegmentLength = $self->MaxSegment; + # Prime the values we'll use for the returned beginning and end. + my @initialMinMax = ($self->ContigLength($contigID), 0); + my ($min, $max) = @initialMinMax; + # 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 + +C<< 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. + my %queryParms = (forward => [$contigID, '+', $start - $maximumSegmentLength + 1, $stop], + reverse => [$contigID, '-', $start, $stop + $maximumSegmentLength - 1]); + # Loop through the query parameters. + for my $parms (values %queryParms) { + # Create the query. + 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, $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 $loc = BasicLocation->new($contig, $beg, $dir, $len); + my $found = $loc->Overlap($start, $stop); + if ($found) { + # Save this feature in the result list. + $featuresFound{$featureID} = $segment; + } + } + } + # Return the ERDB objects for the features found. + return values %featuresFound; +} + +=head3 FType + +C<< my $ftype = $sprout->FType($featureID); >> + +Return the type of a feature. + +=over 4 + +=item featureID + +ID of the feature whose type is desired. + +=item RETURN + +A string indicating the type of feature (e.g. peg, rna). If the feature does not exist, returns an +undefined value. + +=back + +=cut +#: Return Type $; +sub FType { + # Get the parameters. + my ($self, $featureID) = @_; + # Get the specified feature's type. + my ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(feature-type)']); + # Return the result. + return $retVal; +} + +=head3 FeatureAnnotations + +C<< my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag); >> + +Return the annotations of a feature. + +=over 4 + +=item featureID + +ID of the feature whose annotations are desired. + +=item rawFlag + +If TRUE, the annotation timestamps will be returned in raw form; otherwise, they +will be returned in human-readable form. + +=item RETURN + +Returns a list of annotation descriptors. Each descriptor is a hash with the following fields. + +* B ID of the relevant feature. + +* B time the annotation was made. + +* B ID of the user who made the annotation + +* B text of the annotation. + +=back + +=cut +#: Return Type @%; +sub FeatureAnnotations { + # Get the parameters. + my ($self, $featureID, $rawFlag) = @_; + # Create a query to get the feature's annotations and the associated users. + my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'], + "IsTargetOfAnnotation(from-link) = ?", [$featureID]); + # Create the return list. + my @retVal = (); + # Loop through the annotations. + while (my $annotation = $query->Fetch) { + # Get the fields to return. + my ($featureID, $timeStamp, $user, $text) = + $annotation->Values(['IsTargetOfAnnotation(from-link)', + 'Annotation(time)', 'MadeAnnotation(from-link)', + 'Annotation(annotation)']); + # Convert the time, if necessary. + if (! $rawFlag) { + $timeStamp = FriendlyTimestamp($timeStamp); + } + # Assemble them into a hash. + my $annotationHash = { featureID => $featureID, + timeStamp => $timeStamp, + user => $user, text => $text }; + # Add it to the return list. + push @retVal, $annotationHash; + } + # Return the result list. + return @retVal; +} + +=head3 AllFunctionsOf + +C<< 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, +Functional assignments are described in the L function. Its worth noting that +we cannot filter on the content of the annotation itself because it's a text field; however, +this is not a big problem because most features only have a small number of annotations. +Finally, if a single user has multiple functional assignments, we will only keep the most +recent one. + +=over 4 + +=item featureID + +ID of the feature whose functional assignments are desired. + +=item RETURN + +Returns a hash mapping the user IDs to functional assignment IDs. + +=back + +=cut +#: Return Type %; +sub AllFunctionsOf { + # Get the parameters. + my ($self, $featureID) = @_; + # Get all of the feature's annotations. + my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'], + "IsTargetOfAnnotation(from-link) = ?", + [$featureID], ['Annotation(time)', 'Annotation(annotation)', + 'MadeAnnotation(from-link)']); + # Declare the return hash. + my %retVal; + # Now we sort the assignments by timestamp in reverse. + my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query; + # Loop until we run out of annotations. for my $annotation (@sortedQuery) { # Get the annotation fields. - my ($timeStamp, $text) = @{$annotation}; - # Check to see if this is a functional assignment. - my ($user, $function) = _ParseAssignment($text); - if ($user && ! exists $timeHash{$user}) { + my ($timeStamp, $text, $user) = @{$annotation}; + # Check to see if this is a functional assignment. + my ($actualUser, $function) = _ParseAssignment($user, $text); + if ($actualUser && ! exists $retVal{$actualUser}) { # Here it is a functional assignment and there has been no # previous assignment for this user, so we stuff it in the # return hash. - $retVal{$function} = $user; - # Insure we don't assign to this user again. - $timeHash{$user} = 1; - } - } - # Return the hash of assignments found. - return %retVal; + $retVal{$actualUser} = $function; + } + } + # Return the hash of assignments found. + return %retVal; } =head3 FunctionOf @@ -1118,11 +1309,8 @@ Return the most recently-determined functional assignment of a particular feature. The functional assignment is handled differently depending on the type of feature. If -the feature is identified by a FIG ID (begins with the string C), then a functional -assignment is a type of annotation. The format of an assignment is described in -L. Its worth noting that we cannot filter on the content of the -annotation itself because it's a text field; however, this is not a big problem because -most features only have a small number of annotations. +the feature is identified by a FIG ID (begins with the string C), then the functional +assignment is taken from the B or C table, depending. Each user has an associated list of trusted users. The assignment returned will be the most recent one by at least one of the trusted users. If no trusted user list is available, then @@ -1141,8 +1329,8 @@ =item userID (optional) -ID of the user whose function determination is desired. If omitted, only the latest -C assignment will be returned. +ID of the user whose function determination is desired. If omitted, the primary +functional assignment in the B table will be returned. =item RETURN @@ -1153,1515 +1341,2500 @@ =cut #: Return Type $; sub FunctionOf { - # Get the parameters. - my ($self, $featureID, $userID) = @_; + # Get the parameters. + my ($self, $featureID, $userID) = @_; # Declare the return value. my $retVal; # Determine the ID type. if ($featureID =~ m/^fig\|/) { - # Here we have a FIG feature ID. We must build the list of trusted - # users. - my %trusteeTable = (); - # Check the user ID. + # Here we have a FIG feature ID. if (!$userID) { - # No user ID, so only FIG is trusted. - $trusteeTable{FIG} = 1; + # Use the primary assignment. + ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(assignment)']); } else { - # Add this user's ID. - $trusteeTable{$userID} = 1; - # Look for the trusted users in the database. - my @trustees = $self->GetFlat(['IsTrustedBy'], 'IsTrustedBy(from-link) = ?', [$userID], 'IsTrustedBy(to-link)'); - if (! @trustees) { - # None were found, so build a default list. + # We must build the list of trusted users. + my %trusteeTable = (); + # Check the user ID. + if (!$userID) { + # No user ID, so only FIG is trusted. $trusteeTable{FIG} = 1; } else { - # Otherwise, put all the trustees in. - for my $trustee (@trustees) { - $trusteeTable{$trustee} = 1; + # Add this user's ID. + $trusteeTable{$userID} = 1; + # Look for the trusted users in the database. + my @trustees = $self->GetFlat(['IsTrustedBy'], 'IsTrustedBy(from-link) = ?', [$userID], 'IsTrustedBy(to-link)'); + if (! @trustees) { + # None were found, so build a default list. + $trusteeTable{FIG} = 1; + } else { + # Otherwise, put all the trustees in. + for my $trustee (@trustees) { + $trusteeTable{$trustee} = 1; + } + } + } + # Build a query for all of the feature's annotations, sorted by date. + my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'], + "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC", + [$featureID]); + my $timeSelected = 0; + # Loop until we run out of annotations. + while (my $annotation = $query->Fetch()) { + # Get the annotation text. + my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)', + 'Annotation(time)', 'MadeAnnotation(from-link)']); + # Check to see if this is a functional assignment for a trusted user. + my ($actualUser, $function) = _ParseAssignment($user, $text); + Trace("Assignment user is $actualUser, text is $function.") if T(4); + if ($actualUser) { + # Here it is a functional assignment. Check the time and the user + # name. The time must be recent and the user must be trusted. + if ((exists $trusteeTable{$actualUser}) && ($time > $timeSelected)) { + $retVal = $function; + $timeSelected = $time; + } } } } + } else { + # Here we have a non-FIG feature ID. In this case the user ID does not + # matter. We simply get the information from the External Alias Function + # table. + ($retVal) = $self->GetEntityValues('ExternalAliasFunc', $featureID, ['ExternalAliasFunc(func)']); + } + # Return the assignment found. + return $retVal; +} + +=head3 FunctionsOf + +C<< my @functionList = $sprout->FunctionOf($featureID, $userID); >> + +Return the functional assignments of a particular feature. + +The functional assignment is handled differently depending on the type of feature. If +the feature is identified by a FIG ID (begins with the string C), then a functional +assignment is a type of annotation. The format of an assignment is described in +L. Its worth noting that we cannot filter on the content of the +annotation itself because it's a text field; however, this is not a big problem because +most features only have a small number of annotations. + +If the feature is B identified by a FIG ID, then the functional assignment +information is taken from the B table. If the table does +not contain an entry for the feature, an empty list is returned. + +=over 4 + +=item featureID + +ID of the feature whose functional assignments are desired. + +=item RETURN + +Returns a list of 2-tuples, each consisting of a user ID and the text of an assignment by +that user. + +=back + +=cut +#: Return Type @@; +sub FunctionsOf { + # Get the parameters. + my ($self, $featureID) = @_; + # Declare the return value. + my @retVal = (); + # Determine the ID type. + if ($featureID =~ m/^fig\|/) { + # Here we have a FIG feature ID. We must build the list of trusted + # users. + my %trusteeTable = (); # Build a query for all of the feature's annotations, sorted by date. - my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation'], + my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'], "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC", [$featureID]); my $timeSelected = 0; # Loop until we run out of annotations. while (my $annotation = $query->Fetch()) { # Get the annotation text. - my ($text, $time) = $annotation->Values(['Annotation(annotation)','Annotation(time)']); + my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)', + 'Annotation(time)', + 'MadeAnnotation(user)']); # Check to see if this is a functional assignment for a trusted user. - my ($user, $function) = _ParseAssignment($text); - if ($user) { - # Here it is a functional assignment. Check the time and the user - # name. The time must be recent and the user must be trusted. - if ((exists $trusteeTable{$user}) && ($time > $timeSelected)) { - $retVal = $function; - $timeSelected = $time; - } + my ($actualUser, $function) = _ParseAssignment($user, $text); + if ($actualUser) { + # Here it is a functional assignment. + push @retVal, [$actualUser, $function]; } } } else { # Here we have a non-FIG feature ID. In this case the user ID does not # matter. We simply get the information from the External Alias Function # table. - ($retVal) = $self->GetEntityValues('ExternalAliasFunc', $featureID, ['ExternalAliasFunc(func)']); + my @assignments = $self->GetEntityValues('ExternalAliasFunc', $featureID, + ['ExternalAliasFunc(func)']); + push @retVal, map { ['master', $_] } @assignments; + } + # Return the assignments found. + return @retVal; +} + +=head3 BBHList + +C<< 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. + +=over 4 + +=item genomeID + +ID of the genome from which the best hits should be taken. + +=item featureList + +List of the features whose best hits are desired. + +=item RETURN + +Returns a reference to a hash that maps the IDs of the incoming features to the best hits +on the target genome. + +=back + +=cut +#: Return Type %; +sub BBHList { + # Get the parameters. + my ($self, $genomeID, $featureList) = @_; + # Create the return structure. + my %retVal = (); + # Loop through the incoming features. + for my $featureID (@{$featureList}) { + # Ask the server for the feature's best hit. + my @bbhData = FIGRules::BBHData($featureID); + # Peel off the BBHs found. + my @found = (); + for my $bbh (@bbhData) { + my $fid = $bbh->[0]; + my $bbGenome = $self->GenomeOf($fid); + if ($bbGenome eq $genomeID) { + push @found, $fid; + } + } + $retVal{$featureID} = \@found; } - # Return the assignment found. - return $retVal; + # Return the mapping. + return \%retVal; +} + +=head3 SimList + +C<< my %similarities = $sprout->SimList($featureID, $count); >> + +Return a list of the similarities to the specified feature. + +This method just returns the bidirectional best hits for performance reasons. + +=over 4 + +=item featureID + +ID of the feature whose similarities are desired. + +=item count + +Maximum number of similar features to be returned, or C<0> to return them all. + +=back + +=cut +#: Return Type %; +sub SimList { + # Get the parameters. + my ($self, $featureID, $count) = @_; + # Ask for the best hits. + my @lists = FIGRules::BBHData($featureID); + # Create the return value. + my %retVal = (); + for my $tuple (@lists) { + $retVal{$tuple->[0]} = $tuple->[1]; + } + # Return the result. + return %retVal; +} + +=head3 IsComplete + +C<< my $flag = $sprout->IsComplete($genomeID); >> + +Return TRUE if the specified genome is complete, else FALSE. + +=over 4 + +=item genomeID + +ID of the genome whose completeness status is desired. + +=item RETURN + +Returns TRUE if the genome is complete, FALSE if it is incomplete, and C if it is +not found. + +=back + +=cut +#: Return Type $; +sub IsComplete { + # Get the parameters. + my ($self, $genomeID) = @_; + # Declare the return variable. + my $retVal; + # Get the genome's data. + my $genomeData = $self->GetEntity('Genome', $genomeID); + if ($genomeData) { + # The genome exists, so get the completeness flag. + $retVal = $genomeData->PrimaryValue('Genome(complete)'); + } + # Return the result. + return $retVal; +} + +=head3 FeatureAliases + +C<< my @aliasList = $sprout->FeatureAliases($featureID); >> + +Return a list of the aliases for a specified feature. + +=over 4 + +=item featureID + +ID of the feature whose aliases are desired. + +=item RETURN + +Returns a list of the feature's aliases. If the feature is not found or has no aliases, it will +return an empty list. + +=back + +=cut +#: Return Type @; +sub FeatureAliases { + # Get the parameters. + my ($self, $featureID) = @_; + # Get the desired feature's aliases + 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); >> + +Return the genome that contains a specified feature or contig. + +=over 4 + +=item featureID + +ID of the feature or contig whose genome is desired. + +=item RETURN + +Returns the ID of the genome for the specified feature or contig. If the feature or contig is not +found, returns an undefined value. + +=back + +=cut +#: Return Type $; +sub GenomeOf { + # Get the parameters. + my ($self, $featureID) = @_; + # Declare the return value. + my $retVal; + # 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; +} + +=head3 CoupledFeatures + +C<< 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. + +=over 4 + +=item featureID + +ID of the feature whose functionally-coupled brethren are desired. + +=item RETURN + +A hash mapping the functionally-coupled feature IDs to the coupling score. + +=back + +=cut +#: Return Type %; +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); + my @rawPairs = FIGRules::NetCouplingData('coupled_to', id1 => $featureID); + Trace(scalar(@rawPairs) . " couplings returned.") if T(coupling => 3); + # Form them into a hash. + my %retVal = (); + 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 (keys %retVal) { + $retVal{$featureID} = 9999; + } + # Return the hash. + return %retVal; +} + +=head3 CouplingEvidence + +C<< my @evidence = $sprout->CouplingEvidence($peg1, $peg2); >> + +Return the evidence for a functional coupling. + +A pair of features is considered evidence of a coupling between two other +features if they occur close together on a contig and both are similar to +the coupled features. So, if B and B are close together on a contig, +B and B are considered evidence for the coupling if (1) B and +B are close together, (2) B is similar to B, and (3) B is +similar to B. + +The score of a coupling is determined by the number of pieces of evidence +that are considered I. If several evidence items belong to +a group of genomes that are close to each other, only one of those items +is considered representative. The other evidence items are presumed to be +there because of the relationship between the genomes rather than because +the two proteins generated by the features have a related functionality. + +Each evidence item is returned as a three-tuple in the form C<[>I<$peg1a>C<,> +I<$peg2a>C<,> I<$rep>C<]>, where I<$peg1a> is similar to I<$peg1>, I<$peg2a> +is similar to I<$peg2>, and I<$rep> is TRUE if the evidence is representative +and FALSE otherwise. + +=over 4 + +=item peg1 + +ID of the feature of interest. + +=item peg2 + +ID of a feature functionally coupled to the feature of interest. + +=item RETURN + +Returns a list of 3-tuples. Each tuple consists of a feature similar to the feature +of interest, a feature similar to the functionally coupled feature, and a flag +that is TRUE for a representative piece of evidence and FALSE otherwise. + +=back + +=cut +#: Return Type @@; +sub CouplingEvidence { + # Get the parameters. + my ($self, $peg1, $peg2) = @_; + # Declare the return variable. + my @retVal = (); + # 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 GetSynonymGroup + +C<< my $id = $sprout->GetSynonymGroup($fid); >> + +Return the synonym group name for the specified feature. + +=over 4 + +=item fid + +ID of the feature whose synonym group is desired. + +=item RETURN + +The name of the synonym group to which the feature belongs. If the feature does +not belong to a synonym group, the feature ID itself is returned. + +=back + +=cut + +sub GetSynonymGroup { + # Get the parameters. + my ($self, $fid) = @_; + # Declare the return variable. + my $retVal; + # Find the synonym group. + my @groups = $self->GetFlat(['IsSynonymGroupFor'], "IsSynonymGroupFor(to-link) = ?", + [$fid], 'IsSynonymGroupFor(from-link)'); + # Check to see if we found anything. + if (@groups) { + $retVal = $groups[0]; + } else { + $retVal = $fid; + } + # Return the result. + return $retVal; +} + +=head3 GetBoundaries + +C<< 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 +order for this method to produce a meaningful result. The resulting +begin/end pair will contain all of the bases in any of the locations. + +=over 4 + +=item locList + +List of locations to process. + +=item RETURN + +Returns a 3-tuple consisting of the contig ID, the beginning boundary, +and the ending boundary. The beginning boundary will be left of the +end for mostly-forward locations and right of the end for mostly-backward +locations. + +=back + +=cut + +sub GetBoundaries { + # Get the parameters. + my ($self, @locList) = @_; + # Set up the counters used to determine the most popular direction. + my %counts = ( '+' => 0, '-' => 0 ); + # Get the last location and parse it. + my $locObject = BasicLocation->new(pop @locList); + # Prime the loop with its data. + my ($contig, $beg, $end) = ($locObject->Contig, $locObject->Left, $locObject->Right); + # Count its direction. + $counts{$locObject->Dir}++; + # Loop through the remaining locations. Note that in most situations, this loop + # will not iterate at all, because most of the time we will be dealing with a + # singleton list. + for my $loc (@locList) { + # Create a location object. + my $locObject = BasicLocation->new($loc); + # Count the direction. + $counts{$locObject->Dir}++; + # Get the left end and the right end. + my $left = $locObject->Left; + my $right = $locObject->Right; + # Merge them into the return variables. + if ($left < $beg) { + $beg = $left; + } + if ($right > $end) { + $end = $right; + } + } + # If the most common direction is reverse, flip the begin and end markers. + if ($counts{'-'} > $counts{'+'}) { + ($beg, $end) = ($end, $beg); + } + # Return the result. + return ($contig, $beg, $end); +} + +=head3 ReadFasta + +C<< 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. +The remaining lines contain the sequence data in order. + +=over 4 + +=item fileName + +Name of the FASTA file. + +=item prefix (optional) + +Prefix to be put in front of each ID found. + +=item RETURN + +Returns a hash that maps each ID to its sequence. + +=back + +=cut +#: Return Type %; +sub ReadFasta { + # Get the parameters. + my ($fileName, $prefix) = @_; + # Create the return hash. + my %retVal = (); + # Open the file for input. + open FASTAFILE, '<', $fileName; + # Declare the ID variable and clear the sequence accumulator. + my $sequence = ""; + my $id = ""; + # Loop through the file. + while () { + # Get the current line. + my $line = $_; + # Check for a header line. + if ($line =~ m/^>\s*(.+?)(\s|\n)/) { + # Here we have a new header. Store the current sequence if we have one. + if ($id) { + $retVal{$id} = lc $sequence; + } + # Clear the sequence accumulator and save the new ID. + ($id, $sequence) = ("$prefix$1", ""); + } else { + # Here we have a data line, so we add it to the sequence accumulator. + # First, we get the actual data out. Note that we normalize to lower + # case. + $line =~ /^\s*(.*?)(\s|\n)/; + $sequence .= $1; + } + } + # Flush out the last sequence (if any). + if ($sequence) { + $retVal{$id} = lc $sequence; + } + # Close the file. + close FASTAFILE; + # Return the hash constructed from the file. + return %retVal; +} + +=head3 FormatLocations + +C<< 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 +gene. The old format is I_I_I. If a feature is in the new format already, +it will not be changed; otherwise, it will be converted. This method can also be used to +perform the reverse task-- insuring that all the locations are in the old format. + +=over 4 + +=item prefix + +Prefix to be put in front of each contig ID (or an empty string if the contig ID should not +be changed. + +=item locations + +List of locations to be normalized. + +=item oldFormat + +TRUE to convert the locations to the old format, else FALSE + +=item RETURN + +Returns a list of updated location descriptors. + +=back + +=cut +#: Return Type @; +sub FormatLocations { + # Get the parameters. + my ($self, $prefix, $locations, $oldFormat) = @_; + # Create the return list. + my @retVal = (); + # Check to see if any locations were passed in. + if ($locations eq '') { + Confess("No locations specified."); + } else { + # Loop through the locations, converting them to the new format. + for my $location (@{$locations}) { + # Parse the location elements. + my ($contig, $beg, $dir, $len) = ParseLocation($location); + # Process according to the desired output format. + if (!$oldFormat) { + # Here we're producing the new format. Add the location to the return list. + push @retVal, "$prefix${contig}_$beg$dir$len"; + } elsif ($dir eq '+') { + # Here we're producing the old format and it's a forward gene. + my $end = $beg + $len - 1; + push @retVal, "$prefix${contig}_${beg}_$end"; + } else { + # Here we're producting the old format and it's a backward gene. + my $end = $beg - $len + 1; + push @retVal, "$prefix${contig}_${beg}_$end"; + } + } + } + # Return the normalized list. + return @retVal; +} + +=head3 DumpData + +C<< $sprout->DumpData(); >> + +Dump all the tables to tab-delimited DTX files. The files will be stored in the data directory. + +=cut + +sub DumpData { + # Get the parameters. + my ($self) = @_; + # Get the data directory name. + my $outputDirectory = $self->{_options}->{dataDir}; + # Dump the relations. + $self->DumpRelations($outputDirectory); +} + +=head3 XMLFileName + +C<< my $fileName = $sprout->XMLFileName(); >> + +Return the name of this database's XML definition file. + +=cut +#: Return Type $; +sub XMLFileName { + my ($self) = @_; + return $self->{_xmlName}; +} + +=head3 Insert + +C<< $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 +relation are represented by scalars. (Note that for relationships, the primary relation is +the B relation.) Field values for the other relations comprising the entity are always +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']}); >> + +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'}); >> + +=over 4 + +=item newObjectType + +Type name of the entity or relationship to insert. + +=item fieldHash + +Hash of field names to values. + +=back + +=cut +#: Return Type ; +sub Insert { + # Get the parameters. + my ($self, $objectType, $fieldHash) = @_; + # Call the underlying method. + $self->InsertObject($objectType, $fieldHash); +} + +=head3 Annotate + +C<< 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. + +=over 4 + +=item fid + +ID of the feature to be annotated. + +=item timestamp + +Numeric timestamp to apply to the annotation. This is concatenated to the feature ID to create the +key. + +=item user + +ID of the user who is making the annotation. + +=item text + +Text of the annotation. + +=item RETURN + +Returns 1 if successful, 2 if an error occurred. + +=back + +=cut +#: Return Type $; +sub Annotate { + # Get the parameters. + my ($self, $fid, $timestamp, $user, $text) = @_; + # Create the annotation ID. + my $aid = "$fid:$timestamp"; + # Insert the Annotation object. + my $retVal = $self->Insert('Annotation', { id => $aid, time => $timestamp, annotation => $text }); + if ($retVal) { + # Connect it to the user. + $retVal = $self->Insert('MadeAnnotation', { 'from-link' => $user, 'to-link' => $aid }); + if ($retVal) { + # Connect it to the feature. + $retVal = $self->Insert('IsTargetOfAnnotation', { 'from-link' => $fid, + 'to-link' => $aid }); + } + } + # Return the success indicator. + return $retVal; +} + +=head3 AssignFunction + +C<< 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. + +=over 4 + +=item featureID + +ID of the feature to which the assignment is being made. + +=item user + +Name of the user group making the assignment, such as C or C. + +=item function + +Text of the function being assigned. + +=item assigningUser (optional) + +Name of the individual user making the assignment. If omitted, defaults to the user group. + +=item RETURN + +Returns 1 if successful, 0 if an error occurred. + +=back + +=cut +#: Return Type $; +sub AssignFunction { + # Get the parameters. + my ($self, $featureID, $user, $function, $assigningUser) = @_; + # Default the assigning user. + if (! $assigningUser) { + $assigningUser = $user; + } + # Create an annotation string from the parameters. + my $annotationText = "$assigningUser\nset $user function to\n$function"; + # Get the current time. + my $now = time; + # Declare the return variable. + my $retVal = 1; + # Locate the genome containing the feature. + my $genome = $self->GenomeOf($featureID); + if (!$genome) { + # Here the genome was not found. This probably means the feature ID is invalid. + Trace("No genome found for feature $featureID.") if T(0); + $retVal = 0; + } else { + # Here we know we have a feature with a genome. Store the annotation. + $retVal = $self->Annotate($featureID, $now, $user, $annotationText); + } + # Return the success indicator. + return $retVal; +} + +=head3 FeaturesByAlias + +C<< 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 +alphanumerics is a UniProt ID. A built-in FIG.pm method is used to analyze the alias +string and attach the necessary prefix. If the result is a FIG ID then it is returned +unmodified; otherwise, we look for an alias. + +=over 4 + +=item alias + +Alias whose features are desired. + +=item RETURN + +Returns a list of the features with the given alias. + +=back + +=cut +#: Return Type @; +sub FeaturesByAlias { + # Get the parameters. + my ($self, $alias) = @_; + # Declare the return variable. + my @retVal = (); + # Parse the alias. + my ($mappedAlias, $flag) = FIGRules::NormalizeAlias($alias); + # If it's a FIG alias, we're done. + if ($flag) { + push @retVal, $mappedAlias; + } else { + # Here we have a non-FIG alias. Get the features with the normalized alias. + @retVal = $self->GetFlat(['IsAliasOf'], 'IsAliasOf(from-link) = ?', [$mappedAlias], 'IsAliasOf(to-link)'); + } + # Return the result. + return @retVal; +} + +=head3 FeatureTranslation + +C<< my $translation = $sprout->FeatureTranslation($featureID); >> + +Return the translation of a feature. + +=over 4 + +=item featureID + +ID of the feature whose translation is desired + +=item RETURN + +Returns the translation of the specified feature. + +=back + +=cut +#: Return Type $; +sub FeatureTranslation { + # Get the parameters. + my ($self, $featureID) = @_; + # Get the specified feature's translation. + my ($retVal) = $self->GetEntityValues("Feature", $featureID, ['Feature(translation)']); + return $retVal; +} + +=head3 Taxonomy + +C<< 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) >> + +=over 4 + +=item genome + +ID of the genome whose taxonomy is desired. + +=item RETURN + +Returns a list containing all the taxonomy classifications for the specified genome's organism. + +=back + +=cut +#: Return Type @; +sub Taxonomy { + # Get the parameters. + my ($self, $genome) = @_; + # Find the specified genome's taxonomy string. + my ($list) = $self->GetEntityValues('Genome', $genome, ['Genome(taxonomy)']); + # Declare the return variable. + my @retVal = (); + # If we found the genome, return its taxonomy string. + if ($list) { + @retVal = split /\s*;\s*/, $list; + } else { + Trace("Genome \"$genome\" does not have a taxonomy in the database.\n") if T(0); + } + # Return the value found. + return @retVal; +} + +=head3 CrudeDistance + +C<< 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. + +=over 4 + +=item genome1 + +ID of the first genome to compare. + +=item genome2 + +ID of the second genome to compare. + +=item RETURN + +Returns a value from 0 to 1, with 0 meaning identical organisms, and 1 meaning organisms from +different domains. + +=back + +=cut +#: Return Type $; +sub CrudeDistance { + # Get the parameters. + my ($self, $genome1, $genome2) = @_; + # Insure that the distance is commutative by sorting the genome IDs. + my ($genomeA, $genomeB); + if ($genome2 < $genome2) { + ($genomeA, $genomeB) = ($genome1, $genome2); + } else { + ($genomeA, $genomeB) = ($genome2, $genome1); + } + my @taxA = $self->Taxonomy($genomeA); + my @taxB = $self->Taxonomy($genomeB); + # Initialize the distance to 1. We'll reduce it each time we find a match between the + # taxonomies. + my $retVal = 1.0; + # Initialize the subtraction amount. This amount determines the distance reduction caused + # by a mismatch at the current level. + my $v = 0.5; + # Loop through the taxonomies. + for (my $i = 0; ($i < @taxA) && ($i < @taxB) && ($taxA[$i] eq $taxB[$i]); $i++) { + $retVal -= $v; + $v /= 2; + } + return $retVal; +} + +=head3 RoleName + +C<< 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. + +=over 4 + +=item roleID + +ID of the role whose description is desired. + +=item RETURN + +Returns the descriptive name of the desired role. + +=back + +=cut +#: Return Type $; +sub RoleName { + # Get the parameters. + my ($self, $roleID) = @_; + # Get the specified role's name. + my ($retVal) = $self->GetEntityValues('Role', $roleID, ['Role(name)']); + # Use the ID if the role has no name. + if (!$retVal) { + $retVal = $roleID; + } + # Return the name. + return $retVal; } -=head3 BBHList +=head3 RoleDiagrams -C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >> +C<< my @diagrams = $sprout->RoleDiagrams($roleID); >> -Return a hash mapping the features in a specified list to their bidirectional best hits -on a specified target genome. +Return a list of the diagrams containing a specified functional role. =over 4 -=item genomeID - -ID of the genome from which the best hits should be taken. - -=item featureList +=item roleID -List of the features whose best hits are desired. +ID of the role whose diagrams are desired. =item RETURN -Returns a reference to a hash that maps the IDs of the incoming features to the IDs of -their best hits. +Returns a list of the IDs for the diagrams that contain the specified functional role. =back =cut -#: Return Type %; -sub BBHList { - # Get the parameters. - my ($self, $genomeID, $featureList) = @_; - # Create the return structure. - my %retVal = (); - # Loop through the incoming features. - for my $featureID (@{$featureList}) { - # Create a query to get the feature's best hit. - my $query = $self->Get(['IsBidirectionalBestHitOf'], - "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?", - [$featureID, $genomeID]); - # Look for the best hit. - my $bbh = $query->Fetch; - if ($bbh) { - my ($targetFeature) = $bbh->Value('IsBidirectionalBestHitOf(to-link)'); - $retVal{$featureID} = $targetFeature; - } - } - # Return the mapping. - return \%retVal; +#: Return Type @; +sub RoleDiagrams { + # Get the parameters. + my ($self, $roleID) = @_; + # Query for the diagrams. + my @retVal = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID], + 'RoleOccursIn(to-link)'); + # Return the result. + return @retVal; } -=head3 FeatureAliases +=head3 GetProperties -C<< my @aliasList = $sprout->FeatureAliases($featureID); >> +C<< my @list = $sprout->GetProperties($fid, $key, $value, $url); >> -Return a list of the aliases for a specified feature. +Return a list of the properties with the specified characteristics. -=over 4 +Properties are the Sprout analog of the FIG attributes. The call is +passed directly to the CustomAttributes or RemoteCustomAttributes object +contained in this object. -=item featureID +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, -ID of the feature whose aliases are desired. + my @attributeList = $sprout->GetProperties('fig|100226.1.peg.1004', 'structure%', 1, 2); -=item RETURN +would return something like -Returns a list of the feature's aliases. If the feature is not found or has no aliases, it will -return an empty list. + ['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] -=back +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, -=cut -#: Return Type @; -sub FeatureAliases { - # Get the parameters. - my ($self, $featureID) = @_; - # Get the desired feature's aliases - my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(alias)']); - # Return the result. - return @retVal; -} + my @attributeList = $sprout->GetProperties(['100226.1', 'fig|100226.1.%'], 'PUBMED'); -=head3 GenomeOf +would get the PUBMED attribute data for Streptomyces coelicolor A3(2) and all its +features. -C<< my $genomeID = $sprout->GenomeOf($featureID); >> +In addition to values in multiple sections, a single attribute key can have multiple +values, so even + + my @attributeList = $sprout->GetProperties($peg, 'virulent'); -Return the genome that contains a specified feature. +which has no wildcard in the key or the object ID, may return multiple tuples. =over 4 -=item featureID +=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. -ID of the feature whose genome is desired. +=item key + +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. =item RETURN -Returns the ID of the genome for the specified feature. If the feature is not found, returns -an undefined value. +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. =back =cut -#: Return Type $; -sub GenomeOf { - # Get the parameters. - my ($self, $featureID) = @_; - # Create a query to find the genome associated with the feature. - my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ?", [$featureID]); - # Declare the return value. - my $retVal; - # Get the genome ID. - if (my $relationship = $query->Fetch()) { - ($retVal) = $relationship->Value('HasContig(from-link)'); - } - # Return the value found. - return $retVal; + +sub GetProperties { + # Get the parameters. + my ($self, @parms) = @_; + # Declare the return variable. + my @retVal = $self->{_ca}->GetAttributes(@parms); + # Return the result. + return @retVal; } -=head3 CoupledFeatures +=head3 FeatureProperties -C<< my %coupleHash = $sprout->CoupledFeatures($featureID); >> +C<< my @properties = $sprout->FeatureProperties($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. +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,@values)>, where C<$key> is the property name and C<@values> are its values. =over 4 =item featureID -ID of the feature whose functionally-coupled brethren are desired. +ID of the feature whose properties are desired. =item RETURN -A hash mapping the functionally-coupled feature IDs to the coupling score. +Returns a list of tuples, each tuple containing the property name and its values. =back =cut -#: Return Type %; -sub CoupledFeatures { - # Get the parameters. - my ($self, $featureID) = @_; - # 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 %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)']); - # The coupling ID contains the two feature IDs separated by a space. We use - # this information to find the ID of the other feature. - my ($fid1, $fid2) = split / /, $couplingID; - my $otherFeatureID = ($featureID eq $fid1 ? $fid2 : $fid1); - # Attach the other feature's score to its ID. - $retVal{$otherFeatureID} = $score; - $found = 1; - } - # Functional coupling is reflexive. If we found at least one coupled feature, we must add - # the incoming feature as well. - if ($found) { - $retVal{$featureID} = 9999; - } - # Return the hash. - return %retVal; +#: Return Type @@; +sub FeatureProperties { + # Get the parameters. + my ($self, $featureID) = @_; + # Get the properties. + 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 GetEntityTypes +=head3 DiagramName + +C<< my $diagramName = $sprout->DiagramName($diagramID); >> + +Return the descriptive name of a diagram. + +=over 4 + +=item diagramID + +ID of the diagram whose description is desired. + +=item RETURN -C<< my @entityList = $sprout->GetEntityTypes(); >> +Returns the descripive name of the specified diagram. -Return the list of supported entity types. +=back =cut -#: Return Type @; -sub GetEntityTypes { - # Get the parameters. - my ($self) = @_; - # Get the underlying database object. - my $erdb = $self->{_erdb}; - # Get its entity type list. - my @retVal = $erdb->GetEntityTypes(); +#: Return Type $; +sub DiagramName { + # Get the parameters. + my ($self, $diagramID) = @_; + # Get the specified diagram's name and return it. + my ($retVal) = $self->GetEntityValues('Diagram', $diagramID, ['Diagram(name)']); + return $retVal; } -=head3 ReadFasta +=head3 PropertyID -C<< my %sequenceData = Sprout::ReadFasta($fileName, $prefix); >> +C<< my $id = $sprout->PropertyID($propName, $propValue); >> -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. -The remaining lines contain the sequence data in order. +Return the ID of the specified property name and value pair, if the +pair exists. Only a small subset of the FIG attributes are stored as +Sprout properties, mostly for use in search optimization. =over 4 -=item fileName +=item propName -Name of the FASTA file. +Name of the desired property. -=item prefix (optional) +=item propValue -Prefix to be put in front of each ID found. +Value expected for the desired property. =item RETURN -Returns a hash that maps each ID to its sequence. +Returns the ID of the name/value pair, or C if the pair does not exist. =back =cut -#: Return Type %; -sub ReadFasta { - # Get the parameters. - my ($fileName, $prefix) = @_; - # Create the return hash. - my %retVal = (); - # Open the file for input. - open FASTAFILE, '<', $fileName; - # Declare the ID variable and clear the sequence accumulator. - my $sequence = ""; - my $id = ""; - # Loop through the file. - while () { - # Get the current line. - my $line = $_; - # Check for a header line. - if ($line =~ m/^>\s*(.+?)(\s|\n)/) { - # Here we have a new header. Store the current sequence if we have one. - if ($id) { - $retVal{$id} = uc $sequence; - } - # Clear the sequence accumulator and save the new ID. - ($id, $sequence) = ("$prefix$1", ""); - } else { - # Here we have a data line, so we add it to the sequence accumulator. - # First, we get the actual data out. Note that we normalize to upper - # case. - $line =~ /^\s*(.*?)(\s|\n)/; - $sequence .= $1; - } - } - # Flush out the last sequence (if any). - if ($sequence) { - $retVal{$id} = uc $sequence; - } - # Close the file. - close FASTAFILE; - # Return the hash constructed from the file. - return %retVal; + +sub PropertyID { + # Get the parameters. + my ($self, $propName, $propValue) = @_; + # Try to find the ID. + my ($retVal) = $self->GetFlat(['Property'], + "Property(property-name) = ? AND Property(property-value) = ?", + [$propName, $propValue], 'Property(id)'); + # Return the result. + return $retVal; } -=head3 FormatLocations +=head3 MergedAnnotations -C<< my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat); >> +C<< my @annotationList = $sprout->MergedAnnotations(\@list); >> -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 -gene. The old format is I_I_I. If a feature is in the new format already, -it will not be changed; otherwise, it will be converted. This method can also be used to -perform the reverse task-- insuring that all the locations are in the old format. +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 +C<$fid> is the ID of a feature, C<$timestamp> is the time at which the annotation was made, +C<$userID> is the ID of the user who made the annotation, and C<$annotation> is the annotation +text. The list is sorted by timestamp. =over 4 -=item prefix +=item list -Prefix to be put in front of each contig ID (or an empty string if the contig ID should not -be changed. +List of the IDs for the features whose annotations are desired. -=item locations +=item RETURN -List of locations to be normalized. +Returns a list of annotation descriptions sorted by the annotation time. -=item oldFormat +=back -TRUE to convert the locations to the old format, else FALSE +=cut +#: Return Type @; +sub MergedAnnotations { + # Get the parameters. + my ($self, $list) = @_; + # Create a list to hold the annotation tuples found. + my @tuples = (); + # Loop through the features in the input list. + for my $fid (@{$list}) { + # Create a list of this feature's annotation tuples. + my @newTuples = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'], + "IsTargetOfAnnotation(from-link) = ?", [$fid], + ['IsTargetOfAnnotation(from-link)', 'Annotation(time)', + 'MadeAnnotation(from-link)', 'Annotation(annotation)']); + # Put it in the result list. + push @tuples, @newTuples; + } + # Sort the result list by timestamp. + my @retVal = sort { $a->[1] <=> $b->[1] } @tuples; + # Loop through and make the time stamps friendly. + for my $tuple (@retVal) { + $tuple->[1] = FriendlyTimestamp($tuple->[1]); + } + # Return the sorted list. + return @retVal; +} + +=head3 RoleNeighbors + +C<< 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 +essentially the set of roles from all of the maps that contain the incoming role. Such +roles are considered neighbors because they are used together in cellular subsystems. + +=over 4 + +=item roleID + +ID of the role whose neighbors are desired. =item RETURN -Returns a list of updated location descriptors. +Returns a list containing the IDs of the roles that are related to the incoming role. =back =cut #: Return Type @; -sub FormatLocations { - # Get the parameters. - my ($self, $prefix, $locations, $oldFormat) = @_; - # Create the return list. - my @retVal = (); - # Check to see if any locations were passed in. - if ($locations eq '') { - Confess("No locations specified."); - } else { - # Loop through the locations, converting them to the new format. - for my $location (@{$locations}) { - # Parse the location elements. - my ($contig, $beg, $dir, $len) = ParseLocation($location); - # Process according to the desired output format. - if (!$oldFormat) { - # Here we're producing the new format. Add the location to the return list. - push @retVal, "$prefix${contig}_$beg$dir$len"; - } elsif ($dir eq '+') { - # Here we're producing the old format and it's a forward gene. - my $end = $beg + $len - 1; - push @retVal, "$prefix${contig}_${beg}_$end"; - } else { - # Here we're producting the old format and it's a backward gene. - my $end = $beg - $len + 1; - push @retVal, "$prefix${contig}_${beg}_$end"; - } - } - } - # Return the normalized list. - return @retVal; +sub RoleNeighbors { + # Get the parameters. + my ($self, $roleID) = @_; + # Get all the diagrams containing this role. + my @diagrams = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID], + 'RoleOccursIn(to-link)'); + # Create the return list. + my @retVal = (); + # Loop through the diagrams. + for my $diagramID (@diagrams) { + # Get all the roles in this diagram. + my @roles = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(to-link) = ?", [$diagramID], + 'RoleOccursIn(from-link)'); + # Add them to the return list. + push @retVal, @roles; + } + # Merge the duplicates from the list. + return Tracer::Merge(@retVal); } -=head3 DumpData +=head3 FeatureLinks -C<< $sprout->DumpData(); >> +C<< my @links = $sprout->FeatureLinks($featureID); >> -Dump all the tables to tab-delimited DTX files. The files will be stored in the data directory. +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 +and are represented in raw HTML. -=cut +=over 4 -sub DumpData { - # Get the parameters. - my ($self) = @_; - # Get the data directory name. - my $outputDirectory = $self->{_options}->{dataDir}; - # Dump the relations. - $self->{_erdb}->DumpRelations($outputDirectory); -} +=item featureID -=head3 XMLFileName +ID of the feature whose links are desired. -C<< my $fileName = $sprout->XMLFileName(); >> +=item RETURN -Return the name of this database's XML definition file. +Returns a list of the web links for this feature. + +=back =cut -#: Return Type $; -sub XMLFileName { - my ($self) = @_; - return $self->{_xmlName}; +#: Return Type @; +sub FeatureLinks { + # Get the parameters. + my ($self, $featureID) = @_; + # Get the feature's links. + my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(link)']); + # Return the feature's links. + return @retVal; } -=head3 Insert - -C<< $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 -relation are represented by scalars. (Note that for relationships, the primary relation is -the B relation.) Field values for the other relations comprising the entity are always -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']}); >> +=head3 SubsystemsOf -The next statement inserts a C relationship between feature C and -property C<4> with an evidence URL of C. +C<< my %subsystems = $sprout->SubsystemsOf($featureID); >> -C<< $sprout->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence = 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >> +Return a hash describing all the subsystems in which a feature participates. Each subsystem is mapped +to the roles the feature performs. =over 4 -=item newObjectType +=item featureID -Type name of the entity or relationship to insert. +ID of the feature whose subsystems are desired. -=item fieldHash +=item RETURN -Hash of field names to values. +Returns a hash mapping all the feature's subsystems to a list of the feature's roles. =back =cut -#: Return Type ; -sub Insert { - # Get the parameters. - my ($self, $objectType, $fieldHash) = @_; - # Call the underlying method. - $self->{_erdb}->InsertObject($objectType, $fieldHash); +#: Return Type %@; +sub SubsystemsOf { + # Get the parameters. + my ($self, $featureID) = @_; + # Get the subsystem list. + my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'], + "ContainsFeature(to-link) = ?", [$featureID], + ['HasSSCell(from-link)', 'IsRoleOf(from-link)']); + # Create the return value. + my %retVal = (); + # Build a hash to weed out duplicates. Sometimes the same PEG and role appears + # in two spreadsheet cells. + my %dupHash = (); + # Loop through the results, adding them to the hash. + for my $record (@subsystems) { + # Get this subsystem and role. + my ($subsys, $role) = @{$record}; + # Insure it's the first time for both. + my $dupKey = "$subsys\n$role"; + if (! exists $dupHash{"$subsys\n$role"}) { + $dupHash{$dupKey} = 1; + push @{$retVal{$subsys}}, $role; + } + } + # Return the hash. + return %retVal; } -=head3 Annotate +=head3 SubsystemList -C<< my $ok = $sprout->Annotate($fid, $timestamp, $user, $text); >> +C<< my @subsystems = $sprout->SubsystemList($featureID); >> -Annotate a feature. This inserts an Annotation record into the database and links it to the -specified feature and user. +Return a list containing the names of the subsystems in which the specified +feature participates. Unlike L, this method only returns the +subsystem names, not the roles. =over 4 -=item fid +=item featureID -ID of the feature to be annotated. +ID of the feature whose subsystem names are desired. -=item timestamp +=item RETURN -Numeric timestamp to apply to the annotation. This is concatenated to the feature ID to create the -key. +Returns a list of the names of the subsystems in which the feature participates. -=item user +=back -ID of the user who is making the annotation. +=cut +#: Return Type @; +sub SubsystemList { + # Get the parameters. + my ($self, $featureID) = @_; + # Get the list of names. + my @retVal = $self->GetFlat(['HasRoleInSubsystem'], "HasRoleInSubsystem(from-link) = ?", + [$featureID], 'HasRoleInSubsystem(to-link)'); + # Return the result, sorted. + return sort @retVal; +} -=item text +=head3 GenomeSubsystemData -Text of the annotation. +C<< my %featureData = $sprout->GenomeSubsystemData($genomeID); >> + +Return a hash mapping genome features to their subsystem roles. + +=over 4 + +=item genomeID + +ID of the genome whose subsystem feature map is desired. =item RETURN -Returns 1 if successful, 2 if an error occurred. +Returns a hash mapping each feature of the genome to a list of 2-tuples. Eacb +2-tuple contains a subsystem name followed by a role ID. =back =cut -#: Return Type $; -sub Annotate { - # Get the parameters. - my ($self, $fid, $timestamp, $user, $text) = @_; - # Create the annotation ID. - my $aid = "$fid:$timestamp"; - # Insert the Annotation object. - my $retVal = $self->Insert('Annotation', { id => $aid, time => $timestamp, annotation => $text }); - if ($retVal) { - # Connect it to the user. - $retVal = $self->Insert('MadeAnnotation', { 'from-link' => $user, 'to-link' => $aid }); - if ($retVal) { - # Connect it to the feature. - $retVal = $self->Insert('IsTargetOfAnnotation', { 'from-link' => $fid, - 'to-link' => $aid }); - } - } - # Return the success indicator. - return $retVal; + +sub GenomeSubsystemData { + # Get the parameters. + my ($self, $genomeID) = @_; + # Declare the return variable. + my %retVal = (); + # Get a list of the genome features that participate in subsystems. For each + # feature we get its spreadsheet cells and the corresponding roles. + my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf'], + "HasFeature(from-link) = ?", [$genomeID], + ['HasFeature(to-link)', 'IsRoleOf(to-link)', 'IsRoleOf(from-link)']); + # Now we get a list of the spreadsheet cells and their associated subsystems. Subsystems + # with an unknown variant code (-1) are skipped. Note the genome ID is at both ends of the + # list. We use it at the beginning to get all the spreadsheet cells for the genome and + # again at the end to filter out participation in subsystems with a negative variant code. + my @cellData = $self->GetAll(['IsGenomeOf', 'HasSSCell', 'ParticipatesIn'], + "IsGenomeOf(from-link) = ? AND ParticipatesIn(variant-code) >= 0 AND ParticipatesIn(from-link) = ?", + [$genomeID, $genomeID], ['HasSSCell(to-link)', 'HasSSCell(from-link)']); + # Now "@roleData" lists the spreadsheet cell and role for each of the genome's features. + # "@cellData" lists the subsystem name for each of the genome's spreadsheet cells. We + # link these two lists together to create the result. First, we want a hash mapping + # spreadsheet cells to subsystem names. + my %subHash = map { $_->[0] => $_->[1] } @cellData; + # We loop through @cellData to build the hash. + for my $roleEntry (@roleData) { + # Get the data for this feature and cell. + my ($fid, $cellID, $role) = @{$roleEntry}; + # Check for a subsystem name. + my $subsys = $subHash{$cellID}; + if ($subsys) { + # Insure this feature has an entry in the return hash. + if (! exists $retVal{$fid}) { $retVal{$fid} = []; } + # Merge in this new data. + push @{$retVal{$fid}}, [$subsys, $role]; + } + } + # Return the result. + return %retVal; } -=head3 AssignFunction +=head3 RelatedFeatures -C<< my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser); >> +C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >> -This method assigns a function to a feature. Functions are a special type of annotation. The general -format is described in L. +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, +an empty list will be returned. =over 4 =item featureID -ID of the feature to which the assignment is being made. - -=item user - -Name of the user group making the assignment, such as C or C. +ID of the feature to whom the desired features are related. =item function -Text of the function being assigned. +Functional assignment (as returned by C) that is used to determine which related +features should be selected. -=item assigningUser (optional) +=item userID -Name of the individual user making the assignment. If omitted, defaults to the user group. +ID of the user whose functional assignments are to be used. If omitted, C is assumed. =item RETURN -Returns 1 if successful, 0 if an error occurred. +Returns a list of the related features with the specified function. =back =cut -#: Return Type $; -sub AssignFunction { - # Get the parameters. - my ($self, $featureID, $user, $function, $assigningUser) = @_; - # Default the assigning user. - if (! $assigningUser) { - $assigningUser = $user; +#: Return Type @; +sub RelatedFeatures { + # Get the parameters. + my ($self, $featureID, $function, $userID) = @_; + # Get a list of the features that are BBHs of the incoming feature. + my @bbhFeatures = map { $_->[0] } FIGRules::BBHData($featureID); + # Now we loop through the features, pulling out the ones that have the correct + # functional assignment. + my @retVal = (); + for my $bbhFeature (@bbhFeatures) { + # Get this feature's functional assignment. + my $newFunction = $self->FunctionOf($bbhFeature, $userID); + # If it matches, add it to the result list. + if ($newFunction eq $function) { + push @retVal, $bbhFeature; + } } - # Create an annotation string from the parameters. - my $annotationText = "$assigningUser\nset $user function to\n$function"; - # Get the current time. - my $now = time; - # Declare the return variable. - my $retVal = 1; - # Locate the genome containing the feature. - my $genome = $self->GenomeOf($featureID); - if (!$genome) { - # Here the genome was not found. This probably means the feature ID is invalid. - Trace("No genome found for feature $featureID.") if T(0); - $retVal = 0; - } else { - # Here we know we have a feature with a genome. Store the annotation. - $retVal = $self->Annotate($featureID, $now, $user, $annotationText); - } - # Return the success indicator. - return $retVal; + # Return the result list. + return @retVal; } -=head3 FeaturesByAlias +=head3 TaxonomySort -C<< my @features = $sprout->FeaturesByAlias($alias); >> +C<< my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs); >> -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 -alphanumerics is a UniProt ID. A built-in FIG.pm method is used to analyze the alias -string and attach the necessary prefix. If the result is a FIG ID then it is returned -unmodified; otherwise, we look for an alias. +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. + +This task could almost be handled by the database; however, the taxonomy string in the +database is a text field and can't be indexed. Instead, we create a hash table that maps +taxonomy strings to lists of features. We then process the hash table using a key sort +and merge the feature lists together to create the output. =over 4 -=item alias +=item $featureIDs -Alias whose features are desired. +List of features to be taxonomically sorted. =item RETURN -Returns a list of the features with the given alias. +Returns the list of features sorted by the taxonomies of the containing genomes. =back =cut #: Return Type @; -sub FeaturesByAlias { - # Get the parameters. - my ($self, $alias) = @_; - # Declare the return variable. - my @retVal = (); - # Parse the alias. - my ($mappedAlias, $flag) = FIGRules::NormalizeAlias($alias); - # If it's a FIG alias, we're done. - if ($flag) { - 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)'); - } - # Return the result. - return @retVal; +sub TaxonomySort { + # Get the parameters. + my ($self, $featureIDs) = @_; + # Create the working hash table. + my %hashBuffer = (); + # Loop through the features. + for my $fid (@{$featureIDs}) { + # Get the taxonomy of the feature's genome. + 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); + } + # Sort the keys and get the elements. + my @retVal = (); + for my $taxon (sort keys %hashBuffer) { + push @retVal, @{$hashBuffer{$taxon}}; + } + # Return the result. + return @retVal; } -=head3 Exists +=head3 Protein -C<< my $found = $sprout->Exists($entityName, $entityID); >> +C<< my $protein = Sprout::Protein($sequence, $table); >> -Return TRUE if an entity exists, else FALSE. +Translate a DNA sequence into a protein sequence. =over 4 -=item entityName +=item sequence -Name of the entity type (e.g. C) relevant to the existence check. +DNA sequence to translate. -=item entityID +=item table (optional) -ID of the entity instance whose existence is to be checked. +Reference to a Hash that translates DNA triples to proteins. A triple that does not +appear in the hash will be translated automatically to C. =item RETURN -Returns TRUE if the entity instance exists, else FALSE. +Returns the protein sequence that would be created by the DNA sequence. =back =cut -#: Return Type $; -sub Exists { - # Get the parameters. - my ($self, $entityName, $entityID) = @_; - # Check for the entity instance. - my $testInstance = $self->GetEntity($entityName, $entityID); - # Return an existence indicator. - my $retVal = ($testInstance ? 1 : 0); - return $retVal; + +# This is the translation table for protein synthesis. +my $ProteinTable = { AAA => 'K', AAG => 'K', AAT => 'N', AAC => 'N', + AGA => 'R', AGG => 'R', AGT => 'S', AGC => 'S', + ATA => 'I', ATG => 'M', ATT => 'I', ATC => 'I', + ACA => 'T', ACG => 'T', ACT => 'T', ACC => 'T', + GAA => 'E', GAG => 'E', GAT => 'D', GAC => 'D', + GTA => 'V', GTG => 'V', GTT => 'V', GTC => 'V', + GGA => 'G', GGG => 'G', GGT => 'G', GGC => 'G', + GCA => 'A', GCG => 'A', GCT => 'A', GCC => 'A', + CAA => 'Q', CAG => 'Q', CAT => 'H', CAC => 'H', + CTA => 'L', CTG => 'L', CTT => 'L', CTC => 'L', + CGA => 'R', CGG => 'R', CGT => 'R', CGC => 'R', + CCA => 'P', CCG => 'P', CCT => 'P', CCC => 'P', + TAA => '*', TAG => '*', TAT => 'Y', TAC => 'Y', + TGA => '*', TGG => 'W', TGT => 'C', TGC => 'C', + TTA => 'L', TTG => 'L', TTT => 'F', TTC => 'F', + TCA => 'S', TCG => 'S', TCT => 'S', TCC => 'S', + AAR => 'K', AAY => 'N', + AGR => 'R', AGY => 'S', + ATY => 'I', + ACR => 'T', ACY => 'T', 'ACX' => 'T', + GAR => 'E', GAY => 'D', + GTR => 'V', GTY => 'V', GTX => 'V', + GGR => 'G', GGY => 'G', GGX => 'G', + GCR => 'A', GCY => 'A', GCX => 'A', + CAR => 'Q', CAY => 'H', + CTR => 'L', CTY => 'L', CTX => 'L', + CGR => 'R', CGY => 'R', CGX => 'R', + CCR => 'P', CCY => 'P', CCX => 'P', + TAR => '*', TAY => 'Y', + TGY => 'C', + TTR => 'L', TTY => 'F', + TCR => 'S', TCY => 'S', TCX => 'S' + }; + +sub Protein { + # Get the paraeters. + my ($sequence, $table) = @_; + # If no table was specified, use the default. + if (!$table) { + $table = $ProteinTable; + } + # Create the return value. + my $retVal = ""; + # Loop through the input triples. + my $n = length $sequence; + for (my $i = 0; $i < $n; $i += 3) { + # Get the current triple from the sequence. Note we convert to + # upper case to insure a match. + my $triple = uc substr($sequence, $i, 3); + # Translate it using the table. + my $protein = "X"; + if (exists $table->{$triple}) { $protein = $table->{$triple}; } + $retVal .= $protein; + } + # Remove the stop codon (if any). + $retVal =~ s/\*$//; + # Return the result. + return $retVal; } -=head3 FeatureTranslation +=head3 LoadInfo -C<< my $translation = $sprout->FeatureTranslation($featureID); >> +C<< my ($dirName, @relNames) = $sprout->LoadInfo(); >> -Return the translation of a feature. +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 +to load the entire database. + +=cut +#: Return Type @; +sub LoadInfo { + # Get the parameters. + my ($self) = @_; + # Create the return list, priming it with the name of the data directory. + my @retVal = ($self->{_options}->{dataDir}); + # Concatenate the table names. + push @retVal, $self->GetTableNames(); + # Return the result. + return @retVal; +} + +=head3 BBHMatrix + +C<< 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 +features in the original genome to their bidirectional best hits in the +target genomes. =over 4 -=item featureID +=item genomeID -ID of the feature whose translation is desired +ID of the genome whose features are to be examined for bidirectional best hits. + +=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 the translation of the specified feature. +Returns a hash mapping each feature in the original genome to a hash mapping its +BBH pegs in the target genomes to their scores. =back =cut -#: Return Type $; -sub FeatureTranslation { - # Get the parameters. - my ($self, $featureID) = @_; - # Get the specified feature's translation. - my ($retVal) = $self->GetEntityValues("Feature", $featureID, ['Feature(translation)']); - return $retVal; + +sub BBHMatrix { + # Get the parameters. + my ($self, $genomeID, $cutoff, @targets) = @_; + # Declare the return variable. + my %retVal = (); + # Ask for the BBHs. + my @bbhList = FIGRules::BatchBBHs("fig|$genomeID.%", $cutoff, @targets); + # We now have a set of 4-tuples that we need to convert into a hash of hashes. + for my $bbhData (@bbhList) { + my ($peg1, $peg2, $score) = @{$bbhData}; + if (! exists $retVal{$peg1}) { + $retVal{$peg1} = { $peg2 => $score }; + } else { + $retVal{$peg1}->{$peg2} = $score; + } + } + # Return the result. + return %retVal; } -=head3 Taxonomy -C<< my @taxonomyList = $sprout->Taxonomy($genome); >> +=head3 SimMatrix -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<< my %simMap = $sprout->SimMatrix($genomeID, $cutoff, @targets); >> -C<< (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12) >> +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 genome +=item genomeID -ID of the genome whose taxonomy is desired. +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 list containing all the taxonomy classifications for the specified genome's organism. +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 -#: Return Type @; -sub Taxonomy { - # Get the parameters. - my ($self, $genome) = @_; - # Find the specified genome's taxonomy string. - my ($list) = $self->GetEntityValues('Genome', $genome, ['Genome(taxonomy)']); - # Declare the return variable. - my @retVal = (); - # If we found the genome, return its taxonomy string. - if ($list) { - @retVal = split /\s*;\s*/, $list; - } else { - Trace("Genome \"$genome\" does not have a taxonomy in the database.\n") if T(0); - } - # Return the value found. - return @retVal; + +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 CrudeDistance -C<< my $distance = $sprout->CrudeDistance($genome1, $genome2); >> +=head3 LowBBHs -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. +C<< 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 +a greater score. The value returned is a map of feature IDs to scores. =over 4 -=item genome1 +=item featureID -ID of the first genome to compare. +ID of the feature whose best hits are desired. -=item genome2 +=item cutoff -ID of the second genome to compare. +Maximum permissible score for inclusion in the results. =item RETURN -Returns a value from 0 to 1, with 0 meaning identical organisms, and 1 meaning organisms from -different domains. +Returns a hash mapping feature IDs to scores. =back =cut -#: Return Type $; -sub CrudeDistance { - # Get the parameters. - my ($self, $genome1, $genome2) = @_; - # Insure that the distance is commutative by sorting the genome IDs. - my ($genomeA, $genomeB); - if ($genome2 < $genome2) { - ($genomeA, $genomeB) = ($genome1, $genome2); - } else { - ($genomeA, $genomeB) = ($genome2, $genome1); - } - my @taxA = $self->Taxonomy($genomeA); - my @taxB = $self->Taxonomy($genomeB); - # Initialize the distance to 1. We'll reduce it each time we find a match between the - # taxonomies. - my $retVal = 1.0; - # Initialize the subtraction amount. This amount determines the distance reduction caused - # by a mismatch at the current level. - my $v = 0.5; - # Loop through the taxonomies. - for (my $i = 0; ($i < @taxA) && ($i < @taxB) && ($taxA[$i] eq $taxB[$i]); $i++) { - $retVal -= $v; - $v /= 2; - } - return $retVal; +#: Return Type %; +sub LowBBHs { + # Get the parsameters. + my ($self, $featureID, $cutoff) = @_; + # Create the return hash. + my %retVal = (); + # Query for the desired BBHs. + my @bbhList = FIGRules::BBHData($featureID, $cutoff); + # Form the results into the return hash. + for my $pair (@bbhList) { + my $fid = $pair->[0]; + if ($self->Exists('Feature', $fid)) { + $retVal{$fid} = $pair->[1]; + } + } + # Return the result. + return %retVal; } -=head3 RoleName +=head3 Sims -C<< my $roleName = $sprout->RoleName($roleID); >> +C<< my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters); >> -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. +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 +returned as B objects. A Sim object is actually a list reference that has been blessed +so that its elements can be accessed by name. + +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 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 roleID +=item fid -ID of the role whose description is desired. +ID of the feature whose similarities are desired, or reference to a list of IDs +of features whose similarities are desired. -=item RETURN +=item maxN -Returns the descriptive name of the desired role. +Maximum number of similarities to return. -=back +=item maxP -=cut -#: Return Type $; -sub RoleName { - # Get the parameters. - my ($self, $roleID) = @_; - # Get the specified role's name. - my ($retVal) = $self->GetEntityValues('Role', $roleID, ['Role(name)']); - # Use the ID if the role has no name. - if (!$retVal) { - $retVal = $roleID; - } - # Return the name. - return $retVal; -} +Minumum allowable similarity score. -=head3 RoleDiagrams +=item select -C<< my @diagrams = $sprout->RoleDiagrams($roleID); >> +Selection criterion: C means only raw similarities are returned; C +means only similarities to FIG features are returned; C means all expanded +similarities are returned; and C means similarities are expanded until the +number of FIG features equals the maximum. -Return a list of the diagrams containing a specified functional role. +=item max_expand -=over 4 +The maximum number of features to expand. -=item roleID +=item filters -ID of the role whose diagrams are desired. +Reference to a hash containing filter information, or a subroutine that can be +used to filter the sims. =item RETURN -Returns a list of the IDs for the diagrams that contain the specified functional role. +Returns a reference to a list of similarity objects, or C if an error +occurred. =back =cut -#: Return Type @; -sub RoleDiagrams { - # Get the parameters. - my ($self, $roleID) = @_; - # Query for the diagrams. - my @retVal = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID], - 'RoleOccursIn(to-link)'); - # Return the result. - return @retVal; + +sub Sims { + # Get the parameters. + my ($self, $fid, $maxN, $maxP, $select, $max_expand, $filters) = @_; + # Create the shim object to test for deleted FIDs. + my $shim = FidCheck->new($self); + # Ask the network for sims. + my $retVal = FIGRules::GetNetworkSims($shim, $fid, {}, $maxN, $maxP, $select, $max_expand, $filters); + # Return the result. + return $retVal; } -=head3 FeatureProperties +=head3 IsAllGenomes -C<< my @properties = $sprout->FeatureProperties($featureID); >> +C<< my $flag = $sprout->IsAllGenomes(\@list, \@checkList); >> -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. +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 +compared to a list of all the genomes. =over 4 -=item featureID +=item list -ID of the feature whose properties are desired. +Reference to the list to be compared to the second list. + +=item checkList (optional) + +Reference to the comparison target list. Every genome ID in this list must occur at +least once in the first list. If this parameter is omitted, a list of all the genomes +is used. =item RETURN -Returns a list of triples, each triple containing the property name, its value, and a URL or -citation. +Returns TRUE if every item in the second list appears at least once in the +first list, else FALSE. =back =cut -#: Return Type @@; -sub FeatureProperties { - # 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)']); - # Return the resulting list. - return @retVal; -} - -=head3 DiagramName - -C<< my $diagramName = $sprout->DiagramName($diagramID); >> - -Return the descriptive name of a diagram. - -=over 4 - -=item diagramID -ID of the diagram whose description is desired. +sub IsAllGenomes { + # Get the parameters. + my ($self, $list, $checkList) = @_; + # Supply the checklist if it was omitted. + $checkList = [$self->Genomes()] if ! defined($checkList); + # Create a hash of the original list. + my %testList = map { $_ => 1 } @{$list}; + # Declare the return variable. We assume that the representation + # is complete and stop at the first failure. + my $retVal = 1; + my $n = scalar @{$checkList}; + for (my $i = 0; $retVal && $i < $n; $i++) { + if (! $testList{$checkList->[$i]}) { + $retVal = 0; + } + } + # Return the result. + return $retVal; +} -=item RETURN +=head3 GetGroups -Returns the descripive name of the specified diagram. +C<< my %groups = $sprout->GetGroups(\@groupList); >> -=back +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 +shown. Alternatively, if no parameter is supplied, all groups will be +included. Genomes that are not in any group are omitted. =cut -#: Return Type $; -sub DiagramName { - # Get the parameters. - my ($self, $diagramID) = @_; - # Get the specified diagram's name and return it. - my ($retVal) = $self->GetEntityValues('Diagram', $diagramID, ['Diagram(name)']); - return $retVal; +#: Return Type %@; +sub GetGroups { + # Get the parameters. + my ($self, $groupList) = @_; + # Declare the return value. + my %retVal = (); + # Determine whether we are getting all the groups or just some. + if (defined $groupList) { + # Here we have a group list. Loop through them individually, + # getting a list of the relevant genomes. + for my $group (@{$groupList}) { + my @genomeIDs = $self->GetFlat(['Genome'], "Genome(primary-group) = ?", + [$group], "Genome(id)"); + $retVal{$group} = \@genomeIDs; + } + } else { + # Here we need all of the groups. In this case, we run through all + # of the genome records, putting each one found into the appropriate + # group. Note that we use a filter clause to insure that only genomes + # in real NMPDR groups are included in the return set. + my @genomes = $self->GetAll(['Genome'], "Genome(primary-group) <> ?", + [$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); + } + } + } + # Return the hash we just built. + return %retVal; } -=head3 MergedAnnotations - -C<< my @annotationList = $sprout->MergedAnnotations(\@list); >> +=head3 MyGenomes -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 -C<$fid> is the ID of a feature, C<$timestamp> is the time at which the annotation was made, -C<$userID> is the ID of the user who made the annotation, and C<$annotation> is the annotation -text. The list is sorted by timestamp. +C<< my @genomes = Sprout::MyGenomes($dataDir); >> -=over 4 +Return a list of the genomes to be included in the Sprout. -=item list +This method is provided for use during the Sprout load. It presumes the Genome load file has +already been created. (It will be in the Sprout data directory and called either C +or C.) Essentially, it reads in the Genome load file and strips out the genome +IDs. -List of the IDs for the features whose annotations are desired. +=over 4 -=item RETURN +=item dataDir -Returns a list of annotation descriptions sorted by the annotation time. +Directory containing the Sprout load files. =back =cut #: Return Type @; -sub MergedAnnotations { - # Get the parameters. - my ($self, $list) = @_; - # Create a list to hold the annotation tuples found. - my @tuples = (); - # Loop through the features in the input list. - for my $fid (@{$list}) { - # Create a list of this feature's annotation tuples. - my @newTuples = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'], - "IsTargetOfAnnotation(from-link) = ?", [$fid], - ['IsTargetOfAnnotation(from-link)', 'Annotation(time)', - 'MadeAnnotation(from-link)', 'Annotation(annotation)']); - # Put it in the result list. - push @tuples, @newTuples; - } - # Sort the result list by timestamp. - my @retVal = sort { $a->[1] <=> $b->[1] } @tuples; - # Loop through and make the time stamps friendly. - for my $tuple (@retVal) { - $tuple->[1] = FriendlyTimestamp($tuple->[1]); - } - # Return the sorted list. - return @retVal; +sub MyGenomes { + # Get the parameters. + my ($dataDir) = @_; + # Compute the genome file name. + my $genomeFileName = LoadFileName($dataDir, "Genome"); + # Extract the genome IDs from the files. + my @retVal = map { $_ =~ /^(\S+)/; $1 } Tracer::GetFile($genomeFileName); + # Return the result. + return @retVal; } -=head3 RoleNeighbors +=head3 LoadFileName -C<< my @roleList = $sprout->RoleNeighbors($roleID); >> +C<< my $fileName = Sprout::LoadFileName($dataDir, $tableName); >> -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 -essentially the set of roles from all of the maps that contain the incoming role. Such -roles are considered neighbors because they are used together in cellular subsystems. +Return the name of the load file for the specified table in the specified data +directory. =over 4 -=item roleID +=item dataDir -ID of the role whose neighbors are desired. +Directory containing the Sprout load files. + +=item tableName + +Name of the table whose load file is desired. =item RETURN -Returns a list containing the IDs of the roles that are related to the incoming role. +Returns the name of the file containing the load data for the specified table, or +C if no load file is present. =back =cut -#: Return Type @; -sub RoleNeighbors { - # Get the parameters. - my ($self, $roleID) = @_; - # Get all the diagrams containing this role. - my @diagrams = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID], - 'RoleOccursIn(to-link)'); - # Create the return list. - my @retVal = (); - # Loop through the diagrams. - for my $diagramID (@diagrams) { - # Get all the roles in this diagram. - my @roles = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(to-link) = ?", [$diagramID], - 'RoleOccursIn(from-link)'); - # Add them to the return list. - push @retVal, @roles; - } - # Merge the duplicates from the list. - return Tracer::Merge(@retVal); +#: Return Type $; +sub LoadFileName { + # Get the parameters. + my ($dataDir, $tableName) = @_; + # Declare the return variable. + my $retVal; + # Check for the various file names. + if (-e "$dataDir/$tableName") { + $retVal = "$dataDir/$tableName"; + } elsif (-e "$dataDir/$tableName.dtx") { + $retVal = "$dataDir/$tableName.dtx"; + } + # Return the result. + return $retVal; } -=head3 FeatureLinks +=head3 DeleteGenome -C<< my @links = $sprout->FeatureLinks($featureID); >> +C<< my $stats = $sprout->DeleteGenome($genomeID, $testFlag); >> -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 -and are represented in raw HTML. +Delete a genome from the database. =over 4 -=item featureID +=item genomeID -ID of the feature whose links are desired. +ID of the genome to delete + +=item testFlag + +If TRUE, then the DELETE statements will be traced, but no deletions will occur. =item RETURN -Returns a list of the web links for this feature. +Returns a statistics object describing the rows deleted. =back =cut -#: Return Type @; -sub FeatureLinks { - # Get the parameters. - my ($self, $featureID) = @_; - # Get the feature's links. - my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(link)']); - # Return the feature's links. - return @retVal; +#: Return Type $%; +sub DeleteGenome { + # Get the parameters. + my ($self, $genomeID, $testFlag) = @_; + # Perform the delete for the genome's features. + my $retVal = $self->Delete('Feature', "fig|$genomeID.%", testMode => $testFlag); + # Perform the delete for the primary genome data. + my $stats = $self->Delete('Genome', $genomeID, testMode => $testFlag); + $retVal->Accumulate($stats); + # Return the result. + return $retVal; } -=head3 SubsystemsOf +=head3 Fix -C<< my %subsystems = $sprout->SubsystemsOf($featureID); >> +C<< my %fixedHash = Sprout::Fix(%groupHash); >> -Return a hash describing all the subsystems in which a feature participates. Each subsystem is mapped -to the role the feature performs. +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. =over 4 -=item featureID +=item groupHash -ID of the feature whose subsystems are desired. +Hash to be fixed up. =item RETURN -Returns a hash mapping all the feature's subsystems to the feature's role. +Returns a fixed-up version of the hash. =back =cut -#: Return Type %; -sub SubsystemsOf { - # Get the parameters. - my ($self, $featureID) = @_; - # Use the SSCell to connect features to subsystems. - my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'], - "ContainsFeature(to-link) = ?", [$featureID], - ['HasSSCell(from-link)', 'IsRoleOf(from-link)']); - # Create the return value. - my %retVal = (); - # Loop through the results, adding them to the hash. - for my $record (@subsystems) { - $retVal{$record->[0]} = $record->[1]; - } - # Return the hash. - return %retVal; + +sub Fix { + # Get the parameters. + my (%groupHash) = @_; + # Create the result hash. + my %retVal = (); + # Copy over the genomes. + for my $groupID (keys %groupHash) { + # Make a safety copy of the group ID. + my $realGroupID = $groupID; + # Yank the primary name. + if ($groupID =~ /([A-Z]\w+)/) { + $realGroupID = $1; + } + # Append this group's genomes into the result hash. + Tracer::AddToListMap(\%retVal, $realGroupID, @{$groupHash{$groupID}}); + } + # Return the result hash. + return %retVal; } -=head3 RelatedFeatures +=head3 GroupPageName -C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >> +C<< my $name = $sprout->GroupPageName($group); >> -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, -an empty list will be returned. +Return the name of the page for the specified NMPDR group. =over 4 -=item featureID - -ID of the feature to whom the desired features are related. - -=item function - -Functional assignment (as returned by C) that is used to determine which related -features should be selected. - -=item userID +=item group -ID of the user whose functional assignments are to be used. If omitted, C is assumed. +Name of the relevant group. =item RETURN -Returns a list of the related features with the specified function. +Returns the relative page name (e.g. C<../content/campy.php>). If the group file is not in +memory it will be read in. =back =cut -#: Return Type @; -sub RelatedFeatures { - # Get the parameters. - my ($self, $featureID, $function, $userID) = @_; - # Get a list of the features that are BBHs of the incoming feature. - my @bbhFeatures = $self->GetFlat(['IsBidirectionalBestHitOf'], - "IsBidirectionalBestHitOf(from-link) = ?", [$featureID], - 'IsBidirectionalBestHitOf(to-link)'); - # Now we loop through the features, pulling out the ones that have the correct - # functional assignment. - my @retVal = (); - for my $bbhFeature (@bbhFeatures) { - # Get this feature's functional assignment. - my $newFunction = $self->FunctionOf($bbhFeature, $userID); - # If it matches, add it to the result list. - if ($newFunction eq $function) { - push @retVal, $bbhFeature; - } - } - # Return the result list. - return @retVal; -} -=head3 TaxonomySort +sub GroupPageName { + # Get the parameters. + my ($self, $group) = @_; + # 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; + } + # Compute the real group name. + my $realGroup = $group; + if ($group =~ /([A-Z]\w+)/) { + $realGroup = $1; + } + # Return the page name. + $retVal = "../content/" . $self->{groupHash}->{$realGroup}->[1]; + # Return the result. + return $retVal; +} -C<< my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs); >> +=head3 ReadGroupFile -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. +C<< my %groupData = Sprout::ReadGroupFile($groupFileName); >> -This task could almost be handled by the database; however, the taxonomy string in the -database is a text field and can't be indexed. Instead, we create a hash table that maps -taxonomy strings to lists of features. We then process the hash table using a key sort -and merge the feature lists together to create the output. +Read in the data from the specified group file. The group file contains information +about each of the NMPDR groups. =over 4 -=item $featureIDs - -List of features to be taxonomically sorted. +=item name -=item RETURN +Name of the group. -Returns the list of features sorted by the taxonomies of the containing genomes. +=item page -=back +Name of the group's page on the web site (e.g. C for +Campylobacter) -=cut -#: Return Type @; -sub TaxonomySort { - # Get the parameters. - my ($self, $featureIDs) = @_; - # Create the working hash table. - my %hashBuffer = (); - # Loop through the features. - for my $fid (@{$featureIDs}) { - # Get the taxonomy of the feature's genome. - 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); - } - # Sort the keys and get the elements. - my @retVal = (); - for my $taxon (sort keys %hashBuffer) { - push @retVal, @{$hashBuffer{$taxon}}; - } - # Return the result. - return @retVal; -} +=item genus -=head3 GetAll +Genus of the group -C<< my @list = $sprout->GetAll(\@objectNames, $filterClause, \@parameters, \@fields, $count); >> +=item species -Return a list of values taken from the objects returned by a query. The first three -parameters correspond to the parameters of the L method. The final parameter is -a list of the fields desired from each record found by the query. The field name -syntax is the standard syntax used for fields in the B system-- -B(I)>-- where I is the name of the relevant entity -or relationship and I is the name of the field. +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. -The list returned will be a list of lists. Each element of the list will contain -the values returned for the fields specified in the fourth parameter. If one of the -fields specified returns multiple values, they are flattened in with the rest. For -example, the following call will return a list of the features in a particular -spreadsheet cell, and each feature will be represented by a list containing the -feature ID followed by all of its aliases. +=back -C<< $query = $sprout->Get(['ContainsFeature', 'Feature'], "ContainsFeature(from-link) = ?", [$ssCellID], ['Feature(id)', 'Feature(alias)']); >> +The parameters to this method are as follows =over 4 -=item objectNames - -List containing the names of the entity and relationship objects to be retrieved. - -=item filterClause - -WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can -be parameterized with parameter markers (C). Each field used must be specified in the standard form -B(I)>. Any parameters specified in the filter clause should be added to the -parameter list as additional parameters. The fields in a filter clause can come from primary -entity relations, relationship relations, or secondary entity relations; however, all of the -entities and relationships involved must be included in the list of object names. - -=item parameterList - -List of the parameters to be substituted in for the parameters marks in the filter clause. +=item groupFile -=item fields - -List of the fields to be returned in each element of the list returned. - -=item count - -Maximum number of records to return. If omitted or 0, all available records will be returned. +Name of the file containing the group data. =item RETURN -Returns a list of list references. Each element of the return list contains the values for the -fields specified in the B parameter. +Returns a hash keyed on group name. The value of each hash =back =cut -#: Return Type @@; -sub GetAll { - # Get the parameters. - my ($self, $objectNames, $filterClause, $parameterList, $fields, $count) = @_; - # Call the ERDB method. - my @retVal = $self->{_erdb}->GetAll($objectNames, $filterClause, $parameterList, - $fields, $count); - # Return the resulting list. - return @retVal; + +sub ReadGroupFile { + # 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; } -=head3 GetFlat +=head3 AddProperty -C<< my @list = $sprout->GetFlat(\@objectNames, $filterClause, $parameterList, $field); >> +C<< my = $sprout->AddProperty($featureID, $key, @values); >> -This is a variation of L that asks for only a single field per record and -returns a single flattened list. +Add a new attribute value (Property) to a feature. =over 4 -=item objectNames - -List containing the names of the entity and relationship objects to be retrieved. - -=item filterClause +=item peg -WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can -be parameterized with parameter markers (C). Each field used must be specified in the standard form -B(I)>. Any parameters specified in the filter clause should be added to the -parameter list as additional parameters. The fields in a filter clause can come from primary -entity relations, relationship relations, or secondary entity relations; however, all of the -entities and relationships involved must be included in the list of object names. +ID of the feature to which the attribute is to be added. -=item parameterList +=item key -List of the parameters to be substituted in for the parameters marks in the filter clause. +Name of the attribute (key). -=item field +=item values -Name of the field to be used to get the elements of the list returned. - -=item RETURN - -Returns a list of values. +Values of the attribute. =back =cut -#: Return Type @; -sub GetFlat { - # Get the parameters. - my ($self, $objectNames, $filterClause, $parameterList, $field) = @_; - # Construct the query. - my $query = $self->Get($objectNames, $filterClause, $parameterList); - # Create the result list. - my @retVal = (); - # Loop through the records, adding the field values found to the result list. - while (my $row = $query->Fetch()) { - push @retVal, $row->Value($field); - } - # Return the list created. - return @retVal; +#: Return Type ; +sub AddProperty { + # Get the parameters. + my ($self, $featureID, $key, @values) = @_; + # Add the property using the attached attributes object. + $self->{_ca}->AddAttribute($featureID, $key, @values); } -=head3 Protein - -C<< my $protein = Sprout::Protein($sequence, $table); >> +=head2 Virtual Methods -Translate a DNA sequence into a protein sequence. +=head3 CleanKeywords -=over 4 +C<< my $cleanedString = $sprout->CleanKeywords($searchExpression); >> -=item sequence +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, +a vertical bar or colon to an apostrophe, and forcing lower case for all alphabetic +characters. In addition, any extra spaces are removed. -DNA sequence to translate. +=over 4 -=item table (optional) +=item searchExpression -Reference to a Hash that translates DNA triples to proteins. A triple that does not -appear in the hash will be translated automatically to C. +Search expression or keyword list to clean. Note that a search expression may +contain boolean operators which need to be preserved. This includes leading +minus signs. =item RETURN -Returns the protein sequence that would be created by the DNA sequence. +Cleaned expression or keyword list. =back =cut -# This is the translation table for protein synthesis. -my $ProteinTable = { AAA => 'K', AAG => 'K', AAT => 'N', AAC => 'N', - AGA => 'R', AGG => 'R', AGT => 'S', AGC => 'S', - ATA => 'I', ATG => 'M', ATT => 'I', ATC => 'I', - ACA => 'T', ACG => 'T', ACT => 'T', ACC => 'T', - GAA => 'E', GAG => 'E', GAT => 'D', GAC => 'D', - GTA => 'V', GTG => 'V', GTT => 'V', GTC => 'V', - GGA => 'G', GGG => 'G', GGT => 'G', GGC => 'G', - GCA => 'A', GCG => 'A', GCT => 'A', GCC => 'A', - CAA => 'Q', CAG => 'Q', CAT => 'H', CAC => 'H', - CTA => 'L', CTG => 'L', CTT => 'L', CTC => 'L', - CGA => 'R', CGG => 'R', CGT => 'R', CGC => 'R', - CCA => 'P', CCG => 'P', CCT => 'P', CCC => 'P', - TAA => '*', TAG => '*', TAT => 'Y', TAC => 'Y', - TGA => '*', TGG => 'W', TGT => 'C', TGC => 'C', - TTA => 'L', TTG => 'L', TTT => 'F', TTC => 'F', - TCA => 'S', TCG => 'S', TCT => 'S', TCC => 'S', - AAR => 'K', AAY => 'N', - AGR => 'R', AGY => 'S', - ATY => 'I', - ACR => 'T', ACY => 'T', 'ACX' => 'T', - GAR => 'E', GAY => 'D', - GTR => 'V', GTY => 'V', GTX => 'V', - GGR => 'G', GGY => 'G', GGX => 'G', - GCR => 'A', GCY => 'A', GCX => 'A', - CAR => 'Q', CAY => 'H', - CTR => 'L', CTY => 'L', CTX => 'L', - CGR => 'R', CGY => 'R', CGX => 'R', - CCR => 'P', CCY => 'P', CCX => 'P', - TAR => '*', TAY => 'Y', - TGY => 'C', - TTR => 'L', TTY => 'F', - TCR => 'S', TCY => 'S', TCX => 'S' - }; - -sub Protein { - # Get the paraeters. - my ($sequence, $table) = @_; - # If no table was specified, use the default. - if (!$table) { - $table = $ProteinTable; - } - # Create the return value. - my $retVal = ""; - # Loop through the input triples. - my $n = length $sequence; - for (my $i = 0; $i < $n; $i += 3) { - # Get the current triple from the sequence. - my $triple = substr($sequence, $i, 3); - # Translate it using the table. - my $protein = "X"; - if (exists $table->{$triple}) { $protein = $table->{$triple}; } - $retVal .= $protein; - } - # Remove the stop codon (if any). - $retVal =~ s/\*$//; - # Return the result. - return $retVal; +sub CleanKeywords { + # Get the parameters. + my ($self, $searchExpression) = @_; + # Perform the standard cleanup. + my $retVal = $self->ERDB::CleanKeywords($searchExpression); + # Fix the periods in EC and TC numbers. + $retVal =~ s/(\d+|\-)\.(\d+|-)\.(\d+|-)\.(\d+|-)/$1_$2_$3_$4/g; + # Fix non-trailing periods. + $retVal =~ s/\.(\w)/_$1/g; + # Fix non-leading minus signs. + $retVal =~ s/(\w)[\-]/$1_/g; + # Fix the vertical bars and colons + $retVal =~ s/(\w)[|:](\w)/$1'$2/g; + # Return the result. + return $retVal; } -=head3 LoadInfo +=head2 Internal Utility Methods -C<< my ($dirName, @relNames) = $sprout->LoadInfo(); >> +=head3 ParseAssignment -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 -to load the entire database. +Parse annotation text to determine whether or not it is a functional assignment. If it is, +the user, function text, and assigning user will be returned as a 3-element list. If it +isn't, an empty list will be returned. -=cut -#: Return Type @; -sub LoadInfo { - # Get the parameters. - my ($self) = @_; - # Create the return list, priming it with the name of the data directory. - my @retVal = ($self->{_options}->{dataDir}); - # Concatenate the table names. - push @retVal, $self->{_erdb}->GetTableNames(); - # Return the result. - return @retVal; -} +A functional assignment is always of the form -=head3 LowBBHs + CIC< function to\n>I -C<< my %bbhMap = $sprout->GoodBBHs($featureID, $cutoff); >> +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 +not always the case. -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 -a greater score. The value returned is a map of feature IDs to scores. +In addition, the functional role may contain extra data that is stripped, such as +terminating spaces or a comment separated from the rest of the text by a tab. + +This is a static method. =over 4 -=item featureID +=item user -ID of the feature whose best hits are desired. +Name of the assigning user. -=item cutoff +=item text -Maximum permissible score for inclusion in the results. +Text of the annotation. =item RETURN -Returns a hash mapping feature IDs to scores. +Returns an empty list if the annotation is not a functional assignment; otherwise, returns +a two-element list containing the user name and the function text. =back =cut -#: Return Type %; -sub LowBBHs { - # Get the parsameters. - my ($self, $featureID, $cutoff) = @_; - # Create the return hash. - my %retVal = (); - # Create a query to get the desired BBHs. - my @bbhList = $self->GetAll(['IsBidirectionalBestHitOf'], - 'IsBidirectionalBestHitOf(sc) <= ? AND IsBidirectionalBestHitOf(from-link) = ?', - [$cutoff, $featureID], - ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(sc)']); - # Form the results into the return hash. - for my $pair (@bbhList) { - $retVal{$pair->[0]} = $pair->[1]; - } - # Return the result. - return %retVal; -} - -=head3 GetGroups - -C<< 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 -shown. Alternatively, if no parameter is supplied, all groups will be -included. Genomes that are not in any group are omitted. -=cut -#: Return Type %@; -sub GetGroups { +sub _ParseAssignment { # Get the parameters. - my ($self, $groupList) = @_; + my ($user, $text) = @_; # Declare the return value. - my %retVal = (); - # Determine whether we are getting all the groups or just some. - if (defined $groupList) { - # Here we have a group list. Loop through them individually, - # getting a list of the relevant genomes. - for my $group (@{$groupList}) { - my @genomeIDs = $self->GetFlat(['Genome'], "Genome(group-name) = ?", - [$group], "Genome(id)"); - $retVal{$group} = \@genomeIDs; - } - } else { - # Here we need all of the groups. In this case, we run through all - # of the genome records, putting each one found into the appropriate - # group. Note that we use a filter clause to insure that only genomes - # in groups are included in the return set. - my @genomes = $self->GetAll(['Genome'], "Genome(group-name) > ' '", [], - ['Genome(id)', 'Genome(group-name)']); - # 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); - } - } + my @retVal = (); + # Check to see if this is a functional assignment. + my ($type, $function) = split(/\n/, $text); + if ($type =~ m/^set function to$/i) { + # Here we have an assignment without a user, so we use the incoming user ID. + @retVal = ($user, $function); + } elsif ($type =~ m/^set (\S+) function to$/i) { + # Here we have an assignment with a user that is passed back to the caller. + @retVal = ($1, $function); } - # Return the hash we just built. - return %retVal; + # If we have an assignment, we need to clean the function text. There may be + # extra junk at the end added as a note from the user. + if (defined( $retVal[1] )) { + $retVal[1] =~ s/(\t\S)?\s*$//; + } + # Return the result list. + return @retVal; } -=head2 Internal Utility Methods - -=head3 ParseAssignment - -Parse annotation text to determine whether or not it is a functional assignment. If it is, -the user, function text, and assigning user will be returned as a 3-element list. If it -isn't, an empty list will be returned. +=head3 _CheckFeature -A functional assignment is always of the form - - IC<\nset >IC< function to\n>I - -where I is the B, I is the B, and I is the -actual functional role. In most cases, the user and the assigning user will be the -same, but that is not always the case. +C<< my $flag = $sprout->_CheckFeature($fid); >> -This is a static method. +Return TRUE if the specified FID is probably an NMPDR feature ID, else FALSE. =over 4 -=item text +=item fid -Text of the annotation. +Feature ID to check. =item RETURN -Returns an empty list if the annotation is not a functional assignment; otherwise, returns -a two-element list containing the user name and the function text. +Returns TRUE if the FID is for one of the NMPDR genomes, else FALSE. =back =cut -sub _ParseAssignment { - # Get the parameters. - my ($text) = @_; - # Declare the return value. - my @retVal = (); - # Check to see if this is a functional assignment. - my ($user, $type, $function) = split(/\n/, $text); - if ($type =~ m/^set ([^ ]+) function to$/i) { - # Here it is, so we return the user name (which is in $1), the functional role text, - # and the assigning user. - @retVal = ($1, $function, $user); - } - # Return the result list. - return @retVal; +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 @@ -2686,8 +3859,9 @@ sub FriendlyTimestamp { my ($timeValue) = @_; - my $retVal = strftime("%a %b %e %H:%M:%S %Y", localtime($timeValue)); + my $retVal = localtime($timeValue); return $retVal; } -1; \ No newline at end of file + +1;