--- Sprout.pm 2005/06/24 21:45:45 1.16 +++ Sprout.pm 2009/01/19 21:46:21 1.122 @@ -2,17 +2,22 @@ use Data::Dumper; use strict; - use Carp; use DBKernel; use XML::Simple; - use DBQuery; - use DBObject; - use ERDB; + use ERDBQuery; + use ERDBObject; use Tracer; use FIGRules; + use FidCheck; use Stats; use POSIX qw(strftime); - + use BasicLocation; + use CustomAttributes; + use RemoteCustomAttributes; + use CGI qw(-nosticky); + use WikiTools; + use BioWords; + use base qw(ERDB); =head1 Sprout Database Manipulation Object @@ -25,22 +30,22 @@ on the constructor. For example, the following invocation specifies a PostgreSQL database named I whose definition and data files are in a co-directory named F. -C<< my $sprout = Sprout->new('GenDB', { dbType => 'pg', dataDir => '../Data', xmlFileName => '../Data/SproutDBD.xml' }); >> + my $sprout = Sprout->new('GenDB', { dbType => 'pg', dataDir => '../Data', xmlFileName => '../Data/SproutDBD.xml' }); Once you have a sprout object, you may use it to re-create the database, load the tables from tab-delimited flat files and perform queries. Several special methods are provided for common -query tasks. For example, L lists the IDs of all the genomes in the database and -L returns the DNA sequence for a specified genome location. +query tasks. For example, L lists the IDs of all the genomes in the database and +L returns the DNA sequence for a specified genome location. -=cut +The Sprout object is a subclass of the ERDB object and inherits all its properties and methods. -#: Constructor SFXlate->new_sprout_only(); +=cut =head2 Public Methods =head3 new -C<< my $sprout = Sprout->new($dbName, \%options); >> + my $sprout = Sprout->new($dbName, \%options); This is the constructor for a sprout object. It connects to the database and loads the database definition into memory. The positional first parameter specifies the name of the @@ -50,7 +55,7 @@ =item dbName -Name of the database. +Name of the database. If omitted, the default Sprout database name is used. =item options @@ -62,37 +67,72 @@ * 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 + +* B name of the database host + =back For example, the following constructor call specifies a database named I and a user name of I with a password of I. The database load files are in the directory F. -C<< my $sprout = Sprout->new('Sprout', { userData =>; 'fig/admin', dataDir => '/usr/fig/SproutData' }); >> + my $sprout = Sprout->new('Sprout', { userData => 'fig/admin', dataDir => '/usr/fig/SproutData' }); + +In order to work properly with [[ERDBGeneratorPl]], the constructor has an alternate +form. + + my $sprout = Sprout->new(dbd => $filename); + +Where I<$fileName> is the name of the DBD file. This enables us to specify an alternate +DBD for the loader, which is important when the database format changes. =cut sub new { # Get the parameters. my ($class, $dbName, $options) = @_; + # Check for the alternate signature, and default the database name if it is missing. + if ($dbName eq 'dbd') { + $dbName = $FIG_Config::sproutDB; + $options = { xmlFileName => $options }; + } elsif (! defined $dbName) { + $dbName = $FIG_Config::sproutDB; + } elsif (ref $dbName eq 'HASH') { + $options = $dbName; + $dbName = $FIG_Config::sproutDB; + } + # 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 => '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 + 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::sproutUser/$FIG_Config::sproutPass", + # user name and password + port => $FIG_Config::sproutPort, + # database connection port + sock => $FIG_Config::sproutSock, + host => $FIG_Config::sprout_host, maxSegmentLength => 4500, # maximum feature segment length maxSequenceLength => 8000, # maximum contig sequence length + noDBOpen => 0, # 1 to suppress the database open }, $options); # Get the data directory. my $dataDir = $optionTable->{dataDir}; @@ -100,20 +140,176 @@ $optionTable->{userData} =~ m!([^/]*)/(.*)$!; my ($userName, $password) = ($1, $2); # Connect to the database. - my $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName, $password, $optionTable->{port}); + my $dbh; + if (! $optionTable->{noDBOpen}) { + Trace("Connect data: host = $optionTable->{host}, port = $optionTable->{port}.") if T(3); + $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName, + $password, $optionTable->{port}, $optionTable->{host}, $optionTable->{sock}); + } # 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; + 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 + # and remember genome data. + $retVal->{genomeHash} = {}; + $retVal->{genomeHashFilled} = 0; + # Remember the data directory name. + $retVal->{dataDir} = $dataDir; + # Return it. + return $retVal; +} + +=head3 ca + + my $ca = $sprout->ca():; + +Return the [[CustomAttributesPm]] object for retrieving object +properties. + +=cut + +sub ca { + # Get the parameters. + my ($self) = @_; + # Do we already have an attribute object? + my $retVal = $self->{_ca}; + if (! defined $retVal) { + # No, create one. How we do it depends on the configuration. + if ($FIG_Config::attrURL) { + Trace("Remote attribute server $FIG_Config::attrURL chosen.") if T(3); + $retVal = 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 = CustomAttributes->new(user => $user); + } + # Save it for next time. + $self->{_ca} = $retVal; + } + # Return the result. + return $retVal; +} + +=head3 CoreGenomes + + my @genomes = $sprout->CoreGenomes($scope); + +Return the IDs of NMPDR genomes in the specified scope. + +=over 4 + +=item scope + +Scope of the desired genomes. C covers the original core genomes, +C covers all genomes in NMPDR groups, and C covers all +genomes in the system. + +=item RETURN + +Returns a list of the IDs for the genomes in the specified scope. + +=back + +=cut + +sub CoreGenomes { + # Get the parameters. + my ($self, $scope) = @_; + # Declare the return variable. + my @retVal = (); + # If we want all genomes, then this is easy. + if ($scope eq 'all') { + @retVal = $self->Genomes(); + } else { + # Here we're dealing with groups. Get the hash of all the + # genome groups. + my %groups = $self->GetGroups(); + # Loop through the groups, keeping the ones that we want. + for my $group (keys %groups) { + # Decide if we want to keep this group. + my $keepGroup = 0; + if ($scope eq 'nmpdr') { + # NMPDR mode: keep all groups. + $keepGroup = 1; + } elsif ($scope eq 'core') { + # CORE mode. Only keep real core groups. + if (grep { $group =~ /$_/ } @{$FIG_Config::realCoreGroups}) { + $keepGroup = 1; + } + } + # Add this group if we're keeping it. + if ($keepGroup) { + push @retVal, @{$groups{$group}}; + } + } + } + # Return the result. + return @retVal; +} + +=head3 SuperGroup + + my $superGroup = $sprout->SuperGroup($groupName); + +Return the name of the super-group containing the specified NMPDR genome +group. If no appropriate super-group can be found, an error will be +thrown. + +=over 4 + +=item groupName + +Name of the group whose super-group is desired. + +=item RETURN + +Returns the name of the super-group containing the incoming group. + +=back + +=cut + +sub SuperGroup { + # Get the parameters. + my ($self, $groupName) = @_; + # Declare the return variable. + my $retVal; + # Get the group hash. + my %groupHash = $self->CheckGroupFile(); + # Find the super-group genus. + $groupName =~ /([A-Z]\w+)/; + my $nameThing = $1; + # See if it's directly in the group hash. + if (exists $groupHash{$nameThing}) { + # Yes, then it's our result. + $retVal = $nameThing; + } else { + # No, so we have to search. + for my $superGroup (keys %groupHash) { + # Get this super-group's item list. + my $list = $groupHash{$superGroup}->{contents}; + # Search it. + if (grep { $_->[0] eq $nameThing } @{$list}) { + $retVal = $superGroup; + } + } + # Make sure we found something. + if (! $retVal) { + Confess("No super-group found for \"$groupName\"."); + } + } + # Return the result. + return $retVal; } =head3 MaxSegment -C<< my $length = $sprout->MaxSegment(); >> + my $length = $sprout->MaxSegment(); This method returns the maximum permissible length of a feature segment. The length is important because it enables us to make reasonable guesses at how to find features inside a particular @@ -130,7 +326,7 @@ =head3 MaxSequence -C<< my $length = $sprout->MaxSequence(); >> + my $length = $sprout->MaxSequence(); This method returns the maximum permissible length of a contig sequence. A contig is broken into sequences in order to save memory resources. In particular, when manipulating features, @@ -143,299 +339,458 @@ 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. +=head3 Load -C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ?", [$genus]); >> + $sprout->Load($rebuild);; -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 +Load the database from files in the data directory, optionally re-creating the tables. -C<< $query = $sprout->Get(['Genome'], "Genome(genus) = \'$genus\'"); >> +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. -however, this version of the call would generate a syntax error if there were any quote -characters inside the variable C<$genus>. +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 database documentation. -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. +=over 4 -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, +=item rebuild -C<< $query = $sprout->Get(['Genome', 'ComesFrom', 'Source'], "Genome(genus) = ?", [$genus]); >> +TRUE if the data tables need to be created or re-created, else FALSE -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. +=item RETURN -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. +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. -C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ? ORDER BY Genome(species)", [$genus]); >> +=back -It is also permissible to specify I an ORDER BY clause. For example, the following invocation gets -all genomes ordered by genus and species. +=cut +#: 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; +} -C<< $query = $sprout->Get(['Genome'], "ORDER BY Genome(genus), Genome(species)"); >> +=head3 LoadUpdate -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. + my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList); -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. +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 objectNames - -List containing the names of the entity and relationship objects to be retrieved. - -=item filterClause +=item truncateFlag -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. +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 parameterList +=item tableList -List of the parameters to be substituted in for the parameters marks in the filter clause. +List of the tables to be updated. =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 encountered, and a list of error messages. =back =cut - -sub Get { +#: Return Type $%; +sub LoadUpdate { # 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); + 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, truncate => $truncateFlag); + # Accumulate the resulting statistics. + $retVal->Accumulate($result); + } + } + # Return the statistics. + return $retVal; } -=head3 GetEntity +=head3 GenomeCounts -C<< my $entityObject = $sprout->GetEntity($entityType, $ID); >> + my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete); -Return an object describing the entity instance with a specified ID. +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 complete -=item ID - -ID of the desired entity. +TRUE if only complete genomes are to be counted, FALSE if all genomes are to be +counted =item RETURN -Returns a B representing the desired entity instance, or an undefined value if no -instance is found with the specified key. +A six-element list containing the number of genomes in each of six categories-- +Archaea, Bacteria, Eukaryota, Viral, Environmental, and Unknown, respectively. =back =cut -sub GetEntity { +sub GenomeCounts { # Get the parameters. - my ($self, $entityType, $ID) = @_; - # Call the ERDB method. - return $self->{_erdb}->GetEntity($entityType, $ID); + 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 GetEntityValues +=head3 ContigCount -C<< my @values = GetEntityValues($entityType, $ID, \@fields); >> + my $count = $sprout->ContigCount($genomeID); -Return a list of values from a specified entity instance. +Return the number of contigs for the specified genome ID. =over 4 -=item entityType - -Entity type name. - -=item ID - -ID of the desired entity. - -=item fields +=item genomeID -List of field names, each of the form IC<(>IC<)>. +ID of the genome whose contig count is desired. =item RETURN -Returns a flattened list of the values of the specified fields for the specified entity. +Returns the number of contigs for the specified genome. =back =cut -#: Return Type @; -sub GetEntityValues { + +sub ContigCount { # Get the parameters. - my ($self, $entityType, $ID, $fields) = @_; - # Call the ERDB method. - return $self->{_erdb}->GetEntityValues($entityType, $ID, $fields); + my ($self, $genomeID) = @_; + # Get the contig count. + my $retVal = $self->GetCount(['Contig', 'HasContig'], "HasContig(from-link) = ?", [$genomeID]); + # Return the result. + return $retVal; } -=head3 ShowMetaData +=head3 GenomeMenu -C<< $sprout->ShowMetaData($fileName); >> + my $html = $sprout->GenomeMenu(%options); -This method outputs a description of the database to an HTML file in the data directory. +Generate a genome selection control with the specified name and options. +This control is almost but not quite the same as the genome control in the +B class. Eventually, the two will be combined. =over 4 -=item fileName +=item options + +Optional parameters for the control (see below). + +=item RETURN -Fully-qualified name to give to the output file. +Returns the HTML for a genome selection control on a form (sometimes called a popup menu). =back -=cut +The valid options are as follows. -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); -} +=over 4 -=head3 Load +=item name -C<< $sprout->Load($rebuild); >>; +Name to give this control for use in passing it to the form. The default is C. +Terrible things will happen if you have two controls with the same name on the same page. -Load the database from files in the data directory, optionally re-creating the tables. +=item filter -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 specified, a filter for the list of genomes to display. The filter should be in the form of a +list reference, a string, or a hash reference. If it is a list reference, the first element +of the list should be the filter string, and the remaining elements the filter parameters. If it is a +string, it will be split into a list at each included tab. If it is a hash reference, it should be +a hash that maps genomes which should be included to a TRUE value. -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. +=item multiSelect -=over 4 +If TRUE, then the user can select multiple genomes. If FALSE, the user can only select one genome. -=item rebuild +=item size -TRUE if the data tables need to be created or re-created, else FALSE +Number of rows to display in the control. The default is C<10> -=item RETURN +=item id -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. +ID to give this control. The default is the value of the C option. Nothing will work correctly +unless this ID is unique. + +=item selected + +A comma-delimited list of selected genomes, or a reference to a list of selected genomes. The +default is none. + +=item class + +If specified, a style class to assign to the genome control. =back =cut -#: Return Type %; -sub Load { + +sub GenomeMenu { # 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. + my ($self, %options) = @_; + # Get the control's name and ID. + my $menuName = $options{name} || $options{id} || 'myGenomeControl'; + my $menuID = $options{id} || $menuName; + Trace("Genome menu name = $menuName with ID $menuID.") if T(3); + # Compute the IDs for the status display. + my $divID = "${menuID}_status"; + my $urlID = "${menuID}_url"; + # Compute the code to show selected genomes in the status area. + my $showSelect = "showSelected('$menuID', '$divID', '$urlID', $FIG_Config::genome_control_cap)"; + # Check for single-select or multi-select. + my $multiSelect = $options{multiSelect} || 0; + # Get the style data. + my $class = $options{class} || ''; + # Get the list of pre-selected items. + my $selections = $options{selected} || []; + if (ref $selections ne 'ARRAY') { + $selections = [ split /\s*,\s*/, $selections ]; + } + my %selected = map { $_ => 1 } @{$selections}; + # Extract the filter information. The default is no filtering. It can be passed as a tab-delimited + # string, a hash reference, or a list reference. + my ($filterHash, $filterString); + my $filterParms = $options{filter} || ""; + if (ref $filterParms eq 'HASH') { + $filterHash = $filterParms; + $filterParms = []; + $filterString = ""; + } else { + if (! ref $filterParms) { + $filterParms = [split /\t|\\t/, $filterParms]; + } + $filterString = shift @{$filterParms}; + } + # Check for possible subsystem filtering. If there is one, we will tack the + # relationship onto the object name list. + my @objectNames = qw(Genome); + if ($filterString =~ /ParticipatesIn\(/) { + push @objectNames, 'ParticipatesIn'; + } + # Get a list of all the genomes in group order. In fact, we only need them ordered + # by name (genus,species,strain), but putting primary-group in front enables us to + # take advantage of an existing index. + my @genomeList = $self->GetAll(\@objectNames, "$filterString ORDER BY Genome(primary-group), Genome(genus), Genome(species), Genome(unique-characterization)", + $filterParms, + [qw(Genome(primary-group) Genome(id) Genome(genus) Genome(species) Genome(unique-characterization) Genome(taxonomy) Genome(contigs))]); + # Apply the hash filter (if any). + if (defined $filterHash) { + @genomeList = grep { $filterHash->{$_->[1]} } @genomeList; + } + # Create a hash to organize the genomes by group. Each group will contain a list of + # 2-tuples, the first element being the genome ID and the second being the genome + # name. + my %gHash = (); + for my $genome (@genomeList) { + # Get the genome data. + my ($group, $genomeID, $genus, $species, $strain, $taxonomy, $contigs) = @{$genome}; + # Compute its name. This is the genus, species, strain (if any), and the contig count. + my $name = "$genus $species "; + $name .= "$strain " if $strain; + my $contigCount = ($contigs == 1 ? "" : ", $contigs contigs"); + # Now we get the domain. The domain tells us the display style of the organism. + my ($domain) = split /\s*;\s*/, $taxonomy, 2; + # Now compute the display group. This is normally the primary group, but if the + # organism is supporting, we blank it out. + my $displayGroup = ($group eq $FIG_Config::otherGroup ? "" : $group); + # Push the genome into the group's list. Note that we use the real group + # name for the hash key here, not the display group name. + push @{$gHash{$group}}, [$genomeID, $name, $contigCount, $domain]; + } + # We are almost ready to unroll the menu out of the group hash. The final step is to separate + # the supporting genomes by domain. First, we extract the NMPDR groups and sort them. They + # are sorted by the first capitalized word. Groups with "other" are sorted after groups + # that aren't "other". At some point, we will want to make this less complicated. + my %sortGroups = map { $_ =~ /(other)?(.*)([A-Z].+)/; "$3$1$2" => $_ } + grep { $_ ne $FIG_Config::otherGroup } keys %gHash; + my @groups = map { $sortGroups{$_} } sort keys %sortGroups; + # Remember the number of NMPDR groups. + my $nmpdrGroupCount = scalar @groups; + # Are there any supporting genomes? + if (exists $gHash{$FIG_Config::otherGroup}) { + # Loop through the supporting genomes, classifying them by domain. We'll also keep a list + # of the domains found. + my @otherGenomes = @{$gHash{$FIG_Config::otherGroup}}; + my @domains = (); + for my $genomeData (@otherGenomes) { + my ($genomeID, $name, $contigCount, $domain) = @{$genomeData}; + if (exists $gHash{$domain}) { + push @{$gHash{$domain}}, $genomeData; + } else { + $gHash{$domain} = [$genomeData]; + push @domains, $domain; + } + } + # Add the domain groups at the end of the main group list. The main group list will now + # contain all the categories we need to display the genomes. + push @groups, sort @domains; + # Delete the supporting group. + delete $gHash{$FIG_Config::otherGroup}; + } + # Now it gets complicated. We need a way to mark all the NMPDR genomes. We take advantage + # of the fact they come first in the list. We'll accumulate a count of the NMPDR genomes + # and use that to make the selections. + my $nmpdrCount = 0; + # Create the type counters. + my $groupCount = 1; + # Get the number of rows to display. + my $rows = $options{size} || 10; + # If we're multi-row, create an onChange event. + my $onChangeTag = ( $rows > 1 ? " onChange=\"$showSelect;\" onFocus=\"$showSelect;\"" : "" ); + # Set up the multiple-select flag. + my $multipleTag = ($multiSelect ? " multiple" : "" ); + # Set up the style class. + my $classTag = ($class ? " class=\"$class\"" : "" ); + # Create the SELECT tag and stuff it into the output array. + my @lines = (""; + if ($rows > 1) { + # We're in a non-compact mode, so we need to add some selection helpers. First is + # the search box. This allows the user to type text and change which genomes are + # displayed. For multiple-select mode, we include a button that selects the displayed + # genes. For single-select mode, we use a plain label instead. + my $searchThingName = "${menuID}_SearchThing"; + my $searchThingLabel = ($multiSelect ? "" + : "Show genomes containing"); + push @lines, "
$searchThingLabel " . + "" . + Hint("GenomeControl", "Type here to filter the genomes displayed.") . "
"; + # For multi-select mode, we also have buttons to set and clear selections. + if ($multiSelect) { + push @lines, ""; + push @lines, ""; + push @lines, ""; + } + # Add a hidden field we can use to generate organism page hyperlinks. + push @lines, ""; + # Add the status display. This tells the user what's selected no matter where the list is scrolled. + push @lines, "
"; + } + # Assemble all the lines into a string. + my $retVal = join("\n", @lines, ""); + # Return the result. return $retVal; } -=head3 LoadUpdate - -C<< my %stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >> -Load updates to one or more database tables. This method enables the client to make changes to one -or two tables without reloading the whole database. For each table, there must be a corresponding -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. +=head3 Stem -=over 4 + my $stem = $sprout->Stem($word); -=item truncateFlag +Return the stem of the specified word, or C if the word is not +stemmable. Note that even if the word is stemmable, the stem may be +the same as the original word. -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. +=over 4 -=item tableList +=item word -List of the tables to be updated. +Word to convert into a stem. =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 a stem of the word (which may be the word itself), or C if +the word is not stemmable. =back =cut -#: Return Type $%; -sub LoadUpdate { + +sub Stem { # 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); + my ($self, $word) = @_; + # Get the stemmer object. + my $stemmer = $self->{stemmer}; + if (! defined $stemmer) { + # We don't have one pre-built, so we build and save it now. + $stemmer = BioWords->new(exceptions => "$FIG_Config::sproutData/Exceptions.txt", + stops => "$FIG_Config::sproutData/StopWords.txt", + cache => 0); + $self->{stemmer} = $stemmer; } - # Return the statistics. + # Try to stem the word. + my $retVal = $stemmer->Process($word); + # Return the result. return $retVal; } + =head3 Build -C<< $sprout->Build(); >> + $sprout->Build(); Build the database. The database will be cleared and the tables re-created from the metadata. This method is useful when a database is brand new or when the database definition has @@ -447,12 +802,12 @@ # Get the parameters. my ($self) = @_; # Create the tables. - $self->{_erdb}->CreateTables; + $self->CreateTables(); } =head3 Genomes -C<< my @genomes = $sprout->Genomes(); >> + my @genomes = $sprout->Genomes(); Return a list of all the genome IDs. @@ -469,7 +824,7 @@ =head3 GenusSpecies -C<< my $infoString = $sprout->GenusSpecies($genomeID); >> + my $infoString = $sprout->GenusSpecies($genomeID); Return the genus, species, and unique characterization for a genome. @@ -491,17 +846,21 @@ 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); + # Declare the return value. + my $retVal; + # Get the genome data. + my $genomeData = $self->_GenomeData($genomeID); + # Only proceed if we found the genome. + if (defined $genomeData) { + $retVal = $genomeData->PrimaryValue('Genome(scientific-name)'); + } + # Return it. return $retVal; } =head3 FeaturesOf -C<< my @features = $sprout->FeaturesOf($genomeID, $ftype); >> + my @features = $sprout->FeaturesOf($genomeID, $ftype); Return a list of the features relevant to a specified genome. @@ -546,7 +905,7 @@ =head3 FeatureLocation -C<< my @locations = $sprout->FeatureLocation($featureID); >> + my @locations = $sprout->FeatureLocation($featureID); Return the location of a feature in its genome's contig segments. In a list context, this method will return a list of the locations. In a scalar context, it will return the locations as a space- @@ -570,55 +929,34 @@ =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. An empty list means the feature +wasn't found. =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. + # Declare the return variable. 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"; + # Get the feature record. + my $object = $self->GetEntity('Feature', $featureID); + # Only proceed if we found it. + if (defined $object) { + # Get the location string. + my $locString = $object->PrimaryValue('Feature(location-string)'); + # Create the return list. + @retVal = split /\s*,\s*/, $locString; } # Return the list in the format indicated by the context. - return (wantarray ? @retVal : join(' ', @retVal)); + return (wantarray ? @retVal : join(',', @retVal)); } =head3 ParseLocation -C<< my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location); >> + my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location); Split a location specifier into the contig ID, the starting point, the direction, and the length. @@ -637,14 +975,14 @@ =back =cut -#: Return Type @; + sub ParseLocation { # 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*)$/; + $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 "_") { @@ -660,14 +998,16 @@ return ($contigID, $start, $dir, $len); } + + =head3 PointLocation -C<< my $found = Sprout::PointLocation($location, $point); >> + my $found = Sprout::PointLocation($location, $point); Return the offset into the specified location of the specified point on the contig. If the specified point is before the location, a negative value will be returned. If it is -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,7 +1031,7 @@ =back =cut -#: Return Type $; + sub PointLocation { # Get the parameter. Note that if we're called as an instance method, we ignore # the first parameter. @@ -714,18 +1054,23 @@ =head3 DNASeq -C<< my $sequence = $sprout->DNASeq(\@locationList); >> + my $sequence = $sprout->DNASeq(\@locationList); This method returns the DNA sequence represented by a list of locations. The list of locations should be of the form returned by L when in a list context. In other words, 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 @@ -752,13 +1097,15 @@ # 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; + $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)", @@ -770,18 +1117,19 @@ $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 $len = ($stopPosition <= $stop ? $stopPosition : $stop) - $startPosition - $pos1; + 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, $len); + $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 { - $locationDNA = join('', reverse split //, $locationDNA); - $retVal .= $locationDNA; + $retVal .= FIG::reverse_comp($locationDNA); } } # Return the result. @@ -790,7 +1138,7 @@ =head3 AllContigs -C<< my @idList = $sprout->AllContigs($genomeID); >> + my @idList = $sprout->AllContigs($genomeID); Return a list of all the contigs for a genome. @@ -818,21 +1166,135 @@ return @retVal; } -=head3 ContigLength +=head3 GenomeLength -C<< my $length = $sprout->ContigLength($contigID); >> + 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 + +sub GenomeLength { + # Get the parameters. + my ($self, $genomeID) = @_; + # Declare the return variable. + my $retVal = 0; + # Get the genome data. + my $genomeData = $self->_GenomeData($genomeID); + # Only proceed if it exists. + if (defined $genomeData) { + $retVal = $genomeData->PrimaryValue('Genome(dna-size)'); + } + # Return the result. + return $retVal; +} + +=head3 FeatureCount + + my $count = $sprout->FeatureCount($genomeID, $type); + +Return the number of features of the specified type in the specified genome. + +=over 4 + +=item genomeID + +ID of the genome whose feature count is desired. + +=item type + +Type of feature to count (eg. C, C, etc.). + +=item RETURN + +Returns the number of features of the specified type for the specified genome. + +=back + +=cut + +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 GenomeAssignments + + my $fidHash = $sprout->GenomeAssignments($genomeID); + +Return a list of a genome's assigned features. The return hash will contain each +assigned feature of the genome mapped to the text of its most recent functional +assignment. + +=over 4 + +=item genomeID + +ID of the genome whose functional assignments are desired. + +=item RETURN + +Returns a reference to a hash which maps each feature to its most recent +functional assignment. + +=back + +=cut + +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 ContigLength + + my $length = $sprout->ContigLength($contigID); + +Compute the length of a contig. + +=over 4 + +=item contigID + +ID of the contig whose length is desired. + +=item RETURN + +Returns the number of positions in the contig. =back @@ -851,7 +1313,55 @@ # Set it from the sequence data, if any. if ($sequence) { my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']); - $retVal = $start + $len; + $retVal = $start + $len - 1; + } + # Return the result. + return $retVal; +} + +=head3 ClusterPEGs + + my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs); + +Cluster the PEGs in a list according to the cluster coding scheme of the specified +subsystem. In order for this to work properly, the subsystem object must have +been used recently to retrieve the PEGs using the B or +B methods. This causes the cluster numbers to be pulled into the +subsystem's color hash. If a PEG is not found in the color hash, it will not +appear in the output sequence. + +=over 4 + +=item sub + +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 list of the PEGs, grouped into smaller lists by cluster number. + +=back + +=cut +#: 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; @@ -859,7 +1369,7 @@ =head3 GenesInRegion -C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >> + my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); List the features which overlap a specified region in a contig. @@ -888,21 +1398,95 @@ =back =cut -#: Return Type @@; + sub GenesInRegion { # Get the parameters. my ($self, $contigID, $start, $stop) = @_; # Get the maximum segment length. my $maximumSegmentLength = $self->MaxSegment; - # Create a hash to receive the feature list. We use a hash so that we can eliminate - # duplicates easily. The hash key will be the feature ID. The value will be a two-element - # containing the minimum and maximum offsets. We will use the offsets to sort the results - # when we're building the result set. - my %featuresFound = (); # Prime the values we'll use for the returned beginning and end. my @initialMinMax = ($self->ContigLength($contigID), 0); my ($min, $max) = @initialMinMax; - # Create a table of parameters for each query. Each query looks for features travelling in + # Get the overlapping features. + my @featureObjects = $self->GeneDataInRegion($contigID, $start, $stop); + # We'l use this hash to help us track the feature IDs and sort them. The key is the + # feature ID and the value is a [$left,$right] pair indicating the maximum extent + # of the feature's locations. + my %featureMap = (); + # Loop through them to do the begin/end analysis. + for my $featureObject (@featureObjects) { + # Get the feature's location string. This may contain multiple actual locations. + my ($locations, $fid) = $featureObject->Values([qw(Feature(location-string) Feature(id))]); + my @locationSegments = split /\s*,\s*/, $locations; + # Loop through the locations. + for my $locationSegment (@locationSegments) { + # Construct an object for the location. + my $locationObject = BasicLocation->new($locationSegment); + # Merge the current segment's begin and end into the min and max. + my ($left, $right) = ($locationObject->Left, $locationObject->Right); + my ($beg, $end); + if (exists $featureMap{$fid}) { + ($beg, $end) = @{$featureMap{$fid}}; + $beg = $left if $left < $beg; + $end = $right if $right > $end; + } else { + ($beg, $end) = ($left, $right); + } + $min = $beg if $beg < $min; + $max = $end if $end > $max; + # Store the feature's new extent back into the hash table. + $featureMap{$fid} = [$beg, $end]; + } + } + # Now we must compute the list of the IDs for the features found. We start with a list + # of midpoints / feature ID pairs. (It's not really a midpoint, it's twice the midpoint, + # but the result of the sort will be the same.) + my @list = map { [$featureMap{$_}->[0] + $featureMap{$_}->[1], $_] } keys %featureMap; + # Now we sort by midpoint and yank out the feature IDs. + my @retVal = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @list; + # Return it along with the min and max. + return (\@retVal, $min, $max); +} + +=head3 GeneDataInRegion + + my @featureList = $sprout->GenesInRegion($contigID, $start, $stop); + +List the features which overlap a specified region in a contig. + +=over 4 + +=item contigID + +ID of the contig containing the region of interest. + +=item start + +Offset of the first residue in the region of interest. + +=item stop + +Offset of the last residue in the region of interest. + +=item RETURN + +Returns a list of B for the desired features. Each object will +contain a B record. + +=back + +=cut + +sub GeneDataInRegion { + # Get the parameters. + my ($self, $contigID, $start, $stop) = @_; + # Get the maximum segment length. + my $maximumSegmentLength = $self->MaxSegment; + # Create a hash to receive the feature list. We use a hash so that we can eliminate + # duplicates easily. The hash key will be the feature ID. The value will be the feature's + # ERDBObject from the query. + my %featuresFound = (); + # Create a table of parameters for the queries. Each query looks for features travelling in # a particular direction. The query parameters include the contig ID, the feature direction, # the lowest possible start position, and the highest possible start position. This works # because each feature segment length must be no greater than the maximum segment length. @@ -911,67 +1495,33 @@ # Loop through the query parameters. for my $parms (values %queryParms) { # Create the query. - my $query = $self->Get(['IsLocatedIn'], + my $query = $self->Get([qw(Feature IsLocatedIn)], "IsLocatedIn(to-link)= ? AND IsLocatedIn(dir) = ? AND IsLocatedIn(beg) >= ? AND IsLocatedIn(beg) <= ?", $parms); # Loop through the feature segments found. while (my $segment = $query->Fetch) { # Get the data about this segment. - my ($featureID, $dir, $beg, $len) = $segment->Values(['IsLocatedIn(from-link)', - 'IsLocatedIn(dir)', 'IsLocatedIn(beg)', 'IsLocatedIn(len)']); - # Determine if this feature actually overlaps the region. The query insures that + my ($featureID, $contig, $dir, $beg, $len) = $segment->Values([qw(IsLocatedIn(from-link) + IsLocatedIn(to-link) IsLocatedIn(dir) IsLocatedIn(beg) IsLocatedIn(len))]); + # Determine if this feature segment actually overlaps the region. The query insures that # this will be the case if the segment is the maximum length, so to fine-tune # the results we insure that the inequality from the query holds using the actual # length. - my ($found, $end) = (0, 0); - if ($dir eq '+') { - $end = $beg + $len; - if ($end >= $start) { - # Denote we found a useful feature. - $found = 1; - } - } elsif ($dir eq '-') { - # Note we switch things around so that the beginning is to the left of the - # ending. - ($beg, $end) = ($beg - $len, $beg); - if ($beg <= $stop) { - # Denote we found a useful feature. - $found = 1; - } - } + my $loc = BasicLocation->new($contig, $beg, $dir, $len); + my $found = $loc->Overlap($start, $stop); if ($found) { - # Here we need to record the feature and update the minima and maxima. First, - # get the current entry for the specified feature. - my ($loc1, $loc2) = (exists $featuresFound{$featureID} ? @{$featuresFound{$featureID}} : - @initialMinMax); - # Merge the current segment's begin and end into the feature begin and end and the - # global min and max. - if ($beg < $loc1) { - $loc1 = $beg; - $min = $beg if $beg < $min; - } - if ($end > $loc2) { - $loc2 = $end; - $max = $end if $end > $max; - } - # Store the entry back into the hash table. - $featuresFound{$featureID} = [$loc1, $loc2]; + # Save this feature in the result list. + $featuresFound{$featureID} = $segment; } } } - # Now we must compute the list of the IDs for the features found. We start with a list - # of midpoints / feature ID pairs. (It's not really a midpoint, it's twice the midpoint, - # but the result of the sort will be the same.) - my @list = map { [$featuresFound{$_}->[0] + $featuresFound{$_}->[1], $_] } keys %featuresFound; - # Now we sort by midpoint and yank out the feature IDs. - my @retVal = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @list; - # Return it along with the min and max. - return (\@retVal, $min, $max); + # Return the ERDB objects for the features found. + return values %featuresFound; } =head3 FType -C<< my $ftype = $sprout->FType($featureID); >> + my $ftype = $sprout->FType($featureID); Return the type of a feature. @@ -1001,7 +1551,7 @@ =head3 FeatureAnnotations -C<< my @descriptors = $sprout->FeatureAnnotations($featureID); >> + my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag); Return the annotations of a feature. @@ -1011,13 +1561,18 @@ 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, in user-friendly format. +* B time the annotation was made. * B ID of the user who made the annotation @@ -1029,7 +1584,7 @@ #: Return Type @%; sub FeatureAnnotations { # Get the parameters. - my ($self, $featureID) = @_; + 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]); @@ -1042,9 +1597,13 @@ $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 => FriendlyTimestamp($timeStamp), + timeStamp => $timeStamp, user => $user, text => $text }; # Add it to the return list. push @retVal, $annotationHash; @@ -1055,14 +1614,14 @@ =head3 AllFunctionsOf -C<< my %functions = $sprout->AllFunctionsOf($featureID); >> + my %functions = $sprout->AllFunctionsOf($featureID); Return all of the functional assignments for a particular feature. The data is returned as a hash of functional assignments to user IDs. A functional assignment is a type of annotation, -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 +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 @@ -1073,7 +1632,7 @@ =item RETURN -Returns a hash mapping the functional assignment IDs to user IDs. +Returns a hash mapping the user IDs to functional assignment IDs. =back @@ -1083,28 +1642,25 @@ # Get the parameters. my ($self, $featureID) = @_; # Get all of the feature's annotations. - my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation'], + my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'], "IsTargetOfAnnotation(from-link) = ?", - [$featureID], ['Annotation(time)', 'Annotation(annotation)']); + [$featureID], ['Annotation(time)', 'Annotation(annotation)', + 'MadeAnnotation(from-link)']); # 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. for my $annotation (@sortedQuery) { # Get the annotation fields. - my ($timeStamp, $text) = @{$annotation}; + my ($timeStamp, $text, $user) = @{$annotation}; # Check to see if this is a functional assignment. - my ($user, $function) = _ParseAssignment($text); - if ($user && ! exists $timeHash{$user}) { + 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; + $retVal{$actualUser} = $function; } } # Return the hash of assignments found. @@ -1113,25 +1669,21 @@ =head3 FunctionOf -C<< my $functionText = $sprout->FunctionOf($featureID, $userID); >> + my $functionText = $sprout->FunctionOf($featureID, $userID); Return the most recently-determined functional assignment of a particular feature. 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 the specified user and FIG are considered trusted. If the user ID is omitted, only FIG is trusted. -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 undefined value is returned. +If the feature is B identified by a FIG ID, then we search the aliases for it. +If no matching alias is found, we return an undefined value. =over 4 @@ -1141,8 +1693,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 @@ -1157,63 +1709,130 @@ 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. + # Find a FIG ID for this feature. + my ($fid) = $self->FeaturesByAlias($featureID); + # Only proceed if we have an ID. + if ($fid) { + # 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', $fid, ['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", + [$fid]); + 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; + } } } } + } + # Return the assignment found. + return $retVal; +} + +=head3 FunctionsOf + + 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. + +=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 = (); + # Convert to a FIG ID. + my ($fid) = $self->FeaturesByAlias($featureID); + # Only proceed if we found one. + if ($fid) { + # 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]); + [$fid]); 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)']); } - # Return the assignment found. - return $retVal; + # Return the assignments found. + return @retVal; } =head3 BBHList -C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >> + my $bbhHash = $sprout->BBHList($genomeID, \@featureList); Return a hash mapping the features in a specified list to their bidirectional best hits on a specified target genome. @@ -1244,16 +1863,18 @@ 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; + # 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 mapping. return \%retVal; @@ -1261,12 +1882,11 @@ =head3 SimList -C<< my %similarities = $sprout->SimList($featureID, $count); >> + my %similarities = $sprout->SimList($featureID, $count); Return a list of the similarities to the specified feature. -Sprout does not support real similarities, so this method just returns the bidirectional -best hits. +This method just returns the bidirectional best hits for performance reasons. =over 4 @@ -1286,24 +1906,19 @@ # Get the parameters. my ($self, $featureID, $count) = @_; # Ask for the best hits. - my @lists = $self->GetAll(['IsBidirectionalBestHitOf'], - "IsBidirectionalBestHitOf(from-link) = ? ORDER BY IsBidirectionalBestHitOf(score) DESC", - [$featureID], ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(score)'], - $count); + my $lists = FIGRules::BBHData($featureID); # Create the return value. my %retVal = (); - for my $tuple (@lists) { + for my $tuple (@$lists) { $retVal{$tuple->[0]} = $tuple->[1]; } # Return the result. return %retVal; } - - =head3 IsComplete -C<< my $flag = $sprout->IsComplete($genomeID); >> + my $flag = $sprout->IsComplete($genomeID); Return TRUE if the specified genome is complete, else FALSE. @@ -1328,10 +1943,11 @@ # Declare the return variable. my $retVal; # Get the genome's data. - my $genomeData = $self->GetEntity('Genome', $genomeID); - if ($genomeData) { + my $genomeData = $self->_GenomeData($genomeID); + # Only proceed if it exists. + if (defined $genomeData) { # The genome exists, so get the completeness flag. - ($retVal) = $genomeData->Value('complete'); + $retVal = $genomeData->PrimaryValue('Genome(complete)'); } # Return the result. return $retVal; @@ -1339,7 +1955,7 @@ =head3 FeatureAliases -C<< my @aliasList = $sprout->FeatureAliases($featureID); >> + my @aliasList = $sprout->FeatureAliases($featureID); Return a list of the aliases for a specified feature. @@ -1362,27 +1978,27 @@ # Get the parameters. my ($self, $featureID) = @_; # Get the desired feature's aliases - my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(alias)']); + my @retVal = $self->GetFlat(['IsAliasOf'], "IsAliasOf(to-link) = ?", [$featureID], 'IsAliasOf(from-link)'); # Return the result. return @retVal; } =head3 GenomeOf -C<< my $genomeID = $sprout->GenomeOf($featureID); >> + my $genomeID = $sprout->GenomeOf($featureID); -Return the genome that contains a specified feature. +Return the genome that contains a specified feature or contig. =over 4 =item featureID -ID of the feature whose genome is desired. +ID of the feature or contig whose genome is desired. =item RETURN -Returns the ID of the genome for the specified feature. If the feature is not found, returns -an undefined value. +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 @@ -1391,13 +2007,17 @@ 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)'); + # Parse the genome ID from the feature ID. + if ($featureID =~ /^fig\|(\d+\.\d+)/) { + $retVal = $1; + } else { + # Find the feature by alias. + my ($realFeatureID) = $self->FeaturesByAlias($featureID); + if ($realFeatureID && $realFeatureID =~ /^fig\|(\d+\.\d+)/) { + $retVal = $1; + } } # Return the value found. return $retVal; @@ -1405,7 +2025,7 @@ =head3 CoupledFeatures -C<< my %coupleHash = $sprout->CoupledFeatures($featureID); >> + my %coupleHash = $sprout->CoupledFeatures($featureID); Return the features functionally coupled with a specified feature. Features are considered functionally coupled if they tend to be clustered on the same chromosome. @@ -1427,30 +2047,19 @@ 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. + # 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 = (); - # 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; + 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; + } } # Return the hash. return %retVal; @@ -1458,7 +2067,7 @@ =head3 CouplingEvidence -C<< my @evidence = $sprout->CouplingEvidence($peg1, $peg2); >> + my @evidence = $sprout->CouplingEvidence($peg1, $peg2); Return the evidence for a functional coupling. @@ -1506,147 +2115,124 @@ my ($self, $peg1, $peg2) = @_; # Declare the return variable. my @retVal = (); - # Our first task is to find out the nature of the coupling. - my ($couplingID, $inverted, $score) = $self->GetCoupling($peg1, $peg2); - # Only proceed if a coupling exists. - if ($couplingID) { - # Determine the ordering to place on the evidence items. If we're - # inverted, we want to see feature 2 before feature 1; otherwise, - # we want the reverse. - my $ordering = ($inverted ? "DESC" : ""); - # Get the coupling evidence. - my @evidenceList = $self->GetAll(['IsEvidencedBy', 'PCH', 'UsesAsEvidence'], - "IsEvidencedBy(from-link) = ? ORDER BY PCH(id), UsesAsEvidence(pos) $ordering", - [$couplingID], - ['PCH(used)', 'UsesAsEvidence(pos)']); - # Loop through the evidence items. Each piece of evidence is represented by two - # positions in the evidence list, one for each feature on the other side of the - # evidence link. If at some point we want to generalize to couplings with - # more than two positions, this section of code will need to be re-done. - while (@evidenceList > 0) { - my $peg1Data = shift @evidenceList; - my $peg2Data = shift @evidenceList; - push @retVal, [$peg1Data->[1], $peg2Data->[1], $peg1Data->[0]]; - } - } - # TODO: code + # Get the coupling and evidence data. + my @rawData = FIGRules::NetCouplingData('coupling_evidence', id1 => $peg1, id2 => $peg2); + # Loop through the raw data, saving the ones that are in NMPDR genomes. + for my $rawTuple (@rawData) { + if ($self->_CheckFeature($rawTuple->[0]) && $self->_CheckFeature($rawTuple->[1])) { + push @retVal, $rawTuple; + } + } # Return the result. return @retVal; } -=head3 GetCoupling - -C<< my ($couplingID, $inverted, $score) = $sprout->GetCoupling($peg1, $peg2); >> - -Return the coupling (if any) for the specified pair of PEGs. If a coupling -exists, we return the coupling ID along with an indicator of whether the -coupling is stored as C<(>I<$peg1>C<, >I<$peg2>C<)> or C<(>I<$peg2>C<, >I<$peg1>C<)>. -In the second case, we say the coupling is I. The importance of an -inverted coupling is that the PEGs in the evidence will appear in reverse order. +=head3 GetSynonymGroup -=over 4 + my $id = $sprout->GetSynonymGroup($fid); -=item peg1 +Return the synonym group name for the specified feature. -ID of the feature of interest. +=over 4 -=item peg2 +=item fid -ID of the potentially coupled feature. +ID of the feature whose synonym group is desired. =item RETURN -Returns a three-element list. The first element contains the database ID of -the coupling. The second element is FALSE if the coupling is stored in the -database in the caller specified order and TRUE if it is stored in the -inverted order. The third element is the coupling's score. If the coupling -does not exist, all three list elements will be C. +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 -#: Return Type $%@; -sub GetCoupling { + +sub GetSynonymGroup { # Get the parameters. - my ($self, $peg1, $peg2) = @_; - # Declare the return values. We'll start with the coupling ID and undefine the - # flag and score until we have more information. - my ($retVal, $inverted, $score) = (CouplingID($peg1, $peg2), undef, undef); - # Find the coupling data. - my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'], - "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)", - [$retVal], "ParticipatesInCoupling(from-link), Coupling(score)"); + 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 (!@pegs) { - # No coupling, so undefine the return value. - $retVal = undef; + if (@groups) { + $retVal = $groups[0]; } else { - # We have a coupling! Get the score and check for inversion. - $score = $pegs[0]->[1]; - $inverted = ($pegs[0]->[0] eq $peg1); + $retVal = $fid; } # Return the result. - return ($retVal, $inverted, $score); + return $retVal; } -=head3 CouplingID +=head3 GetBoundaries -C<< my $couplingID = Sprout::CouplingID($peg1, $peg2); >> + my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList); -Return the coupling ID for a pair of feature IDs. - -The coupling ID is currently computed by joining the feature IDs in -sorted order with a space. Client modules (that is, modules which -use Sprout) should not, however, count on this always being the -case. This method provides a way for abstracting the concept of a -coupling ID. All that we know for sure about it is that it can be -generated easily from the feature IDs and the order of the IDs -in the parameter list does not matter (i.e. C -will have the same value as C. +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 peg1 - -First feature of interest. - -=item peg2 +=item locList -Second feature of interest. +List of locations to process. =item RETURN -Returns the ID that would be used to represent a functional coupling of -the two specified PEGs. +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 -#: Return Type $; -sub CouplingID { - return join " ", sort @_; -} - -=head3 GetEntityTypes -C<< my @entityList = $sprout->GetEntityTypes(); >> - -Return the list of supported entity types. - -=cut -#: Return Type @; -sub GetEntityTypes { +sub GetBoundaries { # Get the parameters. - my ($self) = @_; - # Get the underlying database object. - my $erdb = $self->{_erdb}; - # Get its entity type list. - my @retVal = $erdb->GetEntityTypes(); + 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); >> + 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. @@ -1688,13 +2274,13 @@ 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; + $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 upper + # First, we get the actual data out. Note that we normalize to lower # case. $line =~ /^\s*(.*?)(\s|\n)/; $sequence .= $1; @@ -1702,7 +2288,7 @@ } # Flush out the last sequence (if any). if ($sequence) { - $retVal{$id} = uc $sequence; + $retVal{$id} = lc $sequence; } # Close the file. close FASTAFILE; @@ -1712,12 +2298,12 @@ =head3 FormatLocations -C<< my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat); >> + my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat); Insure that a list of feature locations is in the Sprout format. The Sprout feature location format is I_I where I<*> is C<+> for a forward gene and C<-> for a backward 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 +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 @@ -1777,7 +2363,7 @@ =head3 DumpData -C<< $sprout->DumpData(); >> + $sprout->DumpData(); Dump all the tables to tab-delimited DTX files. The files will be stored in the data directory. @@ -1789,12 +2375,12 @@ # Get the data directory name. my $outputDirectory = $self->{_options}->{dataDir}; # Dump the relations. - $self->{_erdb}->DumpRelations($outputDirectory); + $self->DumpRelations($outputDirectory); } =head3 XMLFileName -C<< my $fileName = $sprout->XMLFileName(); >> + my $fileName = $sprout->XMLFileName(); Return the name of this database's XML definition file. @@ -1805,23 +2391,111 @@ return $self->{_xmlName}; } -=head3 Insert +=head3 GetGenomeNameData -C<< $sprout->Insert($objectType, \%fieldHash); >> + my ($genus, $species, $strain) = $sprout->GenomeNameData($genomeID); -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. +Return the genus, species, and unique characterization for a genome. This +is similar to L, with the exception that it returns the +values in three seperate fields. -C<< $sprout->Insert('Feature', { id => 'fig|188.1.peg.1', active => 0, feature-type => 'peg', alias => ['ZP_00210270.1', 'gi|46206278']}); >> +=over 4 -The next statement inserts a C relationship between feature C and -property C<4> with an evidence URL of C. +=item genomeID + +ID of the genome whose name data is desired. + +=item RETURN + +Returns a three-element list, consisting of the genus, species, and strain +of the specified genome. If the genome is not found, an error occurs. + +=back + +=cut + +sub GetGenomeNameData { + # Get the parameters. + my ($self, $genomeID) = @_; + # Declare the return variables. + my ($genus, $species, $strain); + # Get the genome's data. + my $genomeData = $self->_GenomeData($genomeID); + # Only proceed if the genome exists. + if (defined $genomeData) { + # Get the desired values. + ($genus, $species, $strain) = $genomeData->Values(['Genome(genus)', + 'Genome(species)', + 'Genome(unique-characterization)']); + } else { + # Throw an error because they were not found. + Confess("Genome $genomeID not found in database."); + } + # Return the results. + return ($genus, $species, $strain); +} + +=head3 GetGenomeByNameData + + my @genomes = $sprout->GetGenomeByNameData($genus, $species, $strain); + +Return a list of the IDs of the genomes with the specified genus, +species, and strain. In almost every case, there will be either zero or +one IDs returned; however, two or more IDs could be returned if there are +multiple versions of the genome in the database. + +=over 4 + +=item genus + +Genus of the desired genome. + +=item species + +Species of the desired genome. + +=item strain + +Strain (unique characterization) of the desired genome. This may be an empty +string, in which case it is presumed that the desired genome has no strain +specified. + +=item RETURN + +Returns a list of the IDs of the genomes having the specified genus, species, and +strain. + +=back -C<< $sprout->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence => 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >> +=cut + +sub GetGenomeByNameData { + # Get the parameters. + my ($self, $genus, $species, $strain) = @_; + # Try to find the genomes. + my @retVal = $self->GetFlat(['Genome'], "Genome(genus) = ? AND Genome(species) = ? AND Genome(unique-characterization) = ?", + [$genus, $species, $strain], 'Genome(id)'); + # Return the result. + return @retVal; +} + +=head3 Insert + + $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. + + $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. + + $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 @@ -1841,12 +2515,12 @@ # Get the parameters. my ($self, $objectType, $fieldHash) = @_; # Call the underlying method. - $self->{_erdb}->InsertObject($objectType, $fieldHash); + $self->InsertObject($objectType, $fieldHash); } =head3 Annotate -C<< my $ok = $sprout->Annotate($fid, $timestamp, $user, $text); >> + my $ok = $sprout->Annotate($fid, $timestamp, $user, $text); Annotate a feature. This inserts an Annotation record into the database and links it to the specified feature and user. @@ -1900,7 +2574,7 @@ =head3 AssignFunction -C<< my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser); >> + my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser); This method assigns a function to a feature. Functions are a special type of annotation. The general format is described in L. @@ -1960,7 +2634,7 @@ =head3 FeaturesByAlias -C<< my @features = $sprout->FeaturesByAlias($alias); >> + my @features = $sprout->FeaturesByAlias($alias); Returns a list of features with the specified alias. The alias is parsed to determine the type of the alias. A string of digits is a GenBack ID and a string of exactly 6 @@ -1994,49 +2668,15 @@ push @retVal, $mappedAlias; } else { # Here we have a non-FIG alias. Get the features with the normalized alias. - @retVal = $self->GetFlat(['Feature'], 'Feature(alias) = ?', [$mappedAlias], 'Feature(id)'); + @retVal = $self->GetFlat(['IsAliasOf'], 'IsAliasOf(from-link) = ?', [$mappedAlias], 'IsAliasOf(to-link)'); } # Return the result. return @retVal; } -=head3 Exists - -C<< my $found = $sprout->Exists($entityName, $entityID); >> - -Return TRUE if an entity exists, else FALSE. - -=over 4 - -=item entityName - -Name of the entity type (e.g. C) relevant to the existence check. - -=item entityID - -ID of the entity instance whose existence is to be checked. - -=item RETURN - -Returns TRUE if the entity instance exists, else FALSE. - -=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; -} - =head3 FeatureTranslation -C<< my $translation = $sprout->FeatureTranslation($featureID); >> + my $translation = $sprout->FeatureTranslation($featureID); Return the translation of a feature. @@ -2064,13 +2704,13 @@ =head3 Taxonomy -C<< my @taxonomyList = $sprout->Taxonomy($genome); >> + my @taxonomyList = $sprout->Taxonomy($genome); Return the taxonomy of the specified genome. This will be in the form of a list containing the various classifications in order from domain (eg. C, C, or C) to sub-species. For example, -C<< (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12) >> + (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12) =over 4 @@ -2089,14 +2729,16 @@ 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; + # Get the genome data. + my $genomeData = $self->_GenomeData($genome); + # Only proceed if it exists. + if (defined $genomeData) { + # Create the taxonomy from the taxonomy string. + @retVal = split /\s*;\s*/, $genomeData->PrimaryValue('Genome(taxonomy)'); } else { + # Genome doesn't exist, so emit a warning. Trace("Genome \"$genome\" does not have a taxonomy in the database.\n") if T(0); } # Return the value found. @@ -2105,7 +2747,7 @@ =head3 CrudeDistance -C<< my $distance = $sprout->CrudeDistance($genome1, $genome2); >> + my $distance = $sprout->CrudeDistance($genome1, $genome2); Returns a crude estimate of the distance between two genomes. The distance is construed so that it will be 0 for genomes with identical taxonomies and 1 for genomes from different domains. @@ -2141,23 +2783,14 @@ } 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; - } + # Compute the distance. + my $retVal = FIGRules::CrudeDistanceFormula(\@taxA, \@taxB); return $retVal; } =head3 RoleName -C<< my $roleName = $sprout->RoleName($roleID); >> + my $roleName = $sprout->RoleName($roleID); Return the descriptive name of the role with the specified ID. In general, a role will only have a descriptive name if it is coded as an EC number. @@ -2191,7 +2824,7 @@ =head3 RoleDiagrams -C<< my @diagrams = $sprout->RoleDiagrams($roleID); >> + my @diagrams = $sprout->RoleDiagrams($roleID); Return a list of the diagrams containing a specified functional role. @@ -2221,15 +2854,13 @@ =head3 FeatureProperties -C<< my @properties = $sprout->FeatureProperties($featureID); >> + my @properties = $sprout->FeatureProperties($featureID); Return a list of the properties for the specified feature. Properties are key-value pairs that specify special characteristics of the feature. For example, a property could indicate that a feature is essential to the survival of the organism or that it has benign influence on the activities of a pathogen. Each property is returned as a triple of the form -C<($key,$value,$url)>, where C<$key> is the property name, C<$value> is its value (commonly -a 1 or a 0, but possibly a string or a floating-point value), and C<$url> is a string describing -the web address or citation in which the property's value for the feature was identified. +C<($key,@values)>, where C<$key> is the property name and C<@values> are its values. =over 4 @@ -2239,8 +2870,7 @@ =item RETURN -Returns a list of triples, each triple containing the property name, its value, and a URL or -citation. +Returns a list of tuples, each tuple containing the property name and its values. =back @@ -2250,16 +2880,20 @@ # Get the parameters. my ($self, $featureID) = @_; # Get the properties. - my @retVal = $self->GetAll(['HasProperty', 'Property'], "HasProperty(from-link) = ?", [$featureID], - ['Property(property-name)', 'Property(property-value)', - 'HasProperty(evidence)']); + my @attributes = $self->ca->GetAttributes($featureID); + # Strip the feature ID off each tuple. + my @retVal = (); + for my $attributeRow (@attributes) { + shift @{$attributeRow}; + push @retVal, $attributeRow; + } # Return the resulting list. return @retVal; } =head3 DiagramName -C<< my $diagramName = $sprout->DiagramName($diagramID); >> + my $diagramName = $sprout->DiagramName($diagramID); Return the descriptive name of a diagram. @@ -2285,9 +2919,46 @@ return $retVal; } +=head3 PropertyID + + my $id = $sprout->PropertyID($propName, $propValue); + +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 propName + +Name of the desired property. + +=item propValue + +Value expected for the desired property. + +=item RETURN + +Returns the ID of the name/value pair, or C if the pair does not exist. + +=back + +=cut + +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 MergedAnnotations -C<< my @annotationList = $sprout->MergedAnnotations(\@list); >> + my @annotationList = $sprout->MergedAnnotations(\@list); Returns a merged list of the annotations for the features in a list. Each annotation is represented by a 4-tuple of the form C<($fid, $timestamp, $userID, $annotation)>, where @@ -2336,7 +3007,7 @@ =head3 RoleNeighbors -C<< my @roleList = $sprout->RoleNeighbors($roleID); >> + my @roleList = $sprout->RoleNeighbors($roleID); Returns a list of the roles that occur in the same diagram as the specified role. Because diagrams and roles are in a many-to-many relationship with each other, the list is @@ -2379,7 +3050,7 @@ =head3 FeatureLinks -C<< my @links = $sprout->FeatureLinks($featureID); >> + my @links = $sprout->FeatureLinks($featureID); Return a list of the web hyperlinks associated with a feature. The web hyperlinks are to external websites describing either the feature itself or the organism containing it @@ -2410,10 +3081,10 @@ =head3 SubsystemsOf -C<< my %subsystems = $sprout->SubsystemsOf($featureID); >> + my %subsystems = $sprout->SubsystemsOf($featureID); Return a hash describing all the subsystems in which a feature participates. Each subsystem is mapped -to the role the feature performs. +to the roles the feature performs. =over 4 @@ -2423,12 +3094,12 @@ =item RETURN -Returns a hash mapping all the feature's subsystems to the feature's role. +Returns a hash mapping all the feature's subsystems to a list of the feature's roles. =back =cut -#: Return Type %; +#: Return Type %@; sub SubsystemsOf { # Get the parameters. my ($self, $featureID) = @_; @@ -2438,9 +3109,19 @@ ['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) { - $retVal{$record->[0]} = $record->[1]; + # 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; @@ -2448,7 +3129,7 @@ =head3 SubsystemList -C<< my @subsystems = $sprout->SubsystemList($featureID); >> + my @subsystems = $sprout->SubsystemList($featureID); Return a list containing the names of the subsystems in which the specified feature participates. Unlike L, this method only returns the @@ -2471,16 +3152,71 @@ sub SubsystemList { # Get the parameters. my ($self, $featureID) = @_; - # Get the list of names. - my @retVal = $self->GetFlat(['ContainsFeature', 'HasSSCell'], "ContainsFeature(to-link) = ?", - [$featureID], 'HasSSCell(from-link)'); + # Get the list of names. We do a join to the Subsystem table because we have missing subsystems in + # the Sprout database! + my @retVal = $self->GetFlat(['HasRoleInSubsystem', 'Subsystem'], "HasRoleInSubsystem(from-link) = ?", + [$featureID], 'HasRoleInSubsystem(to-link)'); + # Return the result, sorted. + return sort @retVal; +} + +=head3 GenomeSubsystemData + + 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 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 + +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 subsystem ID and the corresponding roles. + my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf', 'HasSSCell'], + "HasFeature(from-link) = ?", [$genomeID], + ['HasFeature(to-link)', 'IsRoleOf(from-link)', 'HasSSCell(from-link)']); + # Now we get a list of valid subsystems. These are the subsystems connected to the genome with + # a non-negative variant code. + my %subs = map { $_ => 1 } $self->GetFlat(['ParticipatesIn'], + "ParticipatesIn(from-link) = ? AND ParticipatesIn(variant-code) >= 0", + [$genomeID], 'ParticipatesIn(to-link)'); + # We loop through @roleData to build the hash. + for my $roleEntry (@roleData) { + # Get the data for this feature and cell. + my ($fid, $role, $subsys) = @{$roleEntry}; + Trace("Subsystem for $fid is $subsys.") if T(4); + # Check the subsystem; + if ($subs{$subsys}) { + Trace("Subsystem found.") if T(4); + # 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; + return %retVal; } =head3 RelatedFeatures -C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >> + my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); Return a list of the features which are bi-directional best hits of the specified feature and have been assigned the specified function by the specified user. If no such features exists, @@ -2513,9 +3249,8 @@ # 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)'); + my $bbhData = FIGRules::BBHData($featureID); + my @bbhFeatures = map { $_->[0] } @$bbhData; # Now we loop through the features, pulling out the ones that have the correct # functional assignment. my @retVal = (); @@ -2533,7 +3268,7 @@ =head3 TaxonomySort -C<< my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs); >> + my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs); Return a list formed by sorting the specified features by the taxonomy of the containing genome. This will cause genomes from similar organisms to float close to each other. @@ -2568,7 +3303,7 @@ my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?", [$fid], 'Genome(taxonomy)'); # Add this feature to the hash buffer. - Tracer::AddToListMap(\%hashBuffer, $taxonomy, $fid); + push @{$hashBuffer{$taxonomy}}, $fid; } # Sort the keys and get the elements. my @retVal = (); @@ -2579,128 +3314,9 @@ return @retVal; } -=head3 GetAll - -C<< my @list = $sprout->GetAll(\@objectNames, $filterClause, \@parameters, \@fields, $count); >> - -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. - -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. - -C<< $query = $sprout->Get(['ContainsFeature', 'Feature'], "ContainsFeature(from-link) = ?", [$ssCellID], ['Feature(id)', 'Feature(alias)']); >> - -=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 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. - -=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. - -=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; -} - -=head3 GetFlat - -C<< my @list = $sprout->GetFlat(\@objectNames, $filterClause, $parameterList, $field); >> - -This is a variation of L that asks for only a single field per record and -returns a single flattened list. - -=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 field - -Name of the field to be used to get the elements of the list returned. - -=item RETURN - -Returns a list of values. - -=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; -} - =head3 Protein -C<< my $protein = Sprout::Protein($sequence, $table); >> + my $protein = Sprout::Protein($sequence, $table); Translate a DNA sequence into a protein sequence. @@ -2770,8 +3386,9 @@ # 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); + # 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}; } @@ -2785,7 +3402,7 @@ =head3 LoadInfo -C<< my ($dirName, @relNames) = $sprout->LoadInfo(); >> + my ($dirName, @relNames) = $sprout->LoadInfo(); Return the name of the directory from which data is to be loaded and a list of the relation names. This information is useful when trying to analyze what needs to be put where in order @@ -2799,152 +3416,880 @@ # 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(); + push @retVal, $self->GetTableNames(); # Return the result. return @retVal; } -=head3 LowBBHs +=head3 BBHMatrix -C<< my %bbhMap = $sprout->GoodBBHs($featureID, $cutoff); >> + my $bbhMap = $sprout->BBHMatrix($genomeID, $cutoff, @targets); -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. +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 best hits are desired. +ID of the genome whose features are to be examined for bidirectional best hits. =item cutoff -Maximum permissible score for inclusion in the results. +A cutoff value. Only hits with a score lower than the cutoff will be returned. + +=item targets + +List of target genomes. Only pairs originating in the original +genome and landing in one of the target genomes will be returned. =item RETURN -Returns a hash mapping feature IDs to scores. +Returns a reference to a hash mapping each feature in the original genome +to a sub-hash mapping its BBH pegs in the target genomes to their scores. =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 BBHMatrix { # Get the parameters. - my ($self, $groupList) = @_; - # Declare the return value. + my ($self, $genomeID, $cutoff, @targets) = @_; + # Declare the return variable. 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); - } + # Ask for the BBHs. + my @bbhList = FIGRules::BatchBBHs("fig|$genomeID.%", $cutoff, @targets); + Trace("Retrieved " . scalar(@bbhList) . " BBH results.") if T(3); + # 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 hash we just built. - return %retVal; + # Return the result. + return \%retVal; } -=head2 Internal Utility Methods -=head3 ParseAssignment +=head3 SimMatrix -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. + my %simMap = $sprout->SimMatrix($genomeID, $cutoff, @targets); -A functional assignment is always of the form +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. - 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. +=over 4 -This is a static method. +=item genomeID -=over 4 +ID of the genome whose features are to be examined for similarities. -=item text +=item cutoff -Text of the annotation. +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 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 a hash mapping each feature in the original genome to a hash mapping its +similar pegs in the target genomes to their scores. =back =cut -sub _ParseAssignment { +sub SimMatrix { # Get the parameters. - my ($text) = @_; - # Declare the return value. - my @retVal = (); + my ($self, $genomeID, $cutoff, @targets) = @_; + # Declare the return variable. + my %retVal = (); + # Get the list of features in the source organism. + my @fids = $self->FeaturesOf($genomeID); + # Ask for the sims. We only want similarities to fig features. + my $simList = FIGRules::GetNetworkSims($self, \@fids, {}, 1000, $cutoff, "fig"); + if (! defined $simList) { + Confess("Unable to retrieve similarities from server."); + } else { + Trace("Processing sims.") if T(3); + # We now have a set of sims that we need to convert into a hash of hashes. First, we + # Create a hash for the target genomes. + my %targetHash = map { $_ => 1 } @targets; + for my $simData (@{$simList}) { + # Get the PEGs and the score. + my ($peg1, $peg2, $score) = ($simData->id1, $simData->id2, $simData->psc); + # Insure the second ID is in the target list. + my ($genome2) = FIGRules::ParseFeatureID($peg2); + if (exists $targetHash{$genome2}) { + # Here it is. Now we need to add it to the return hash. How we do that depends + # on whether or not $peg1 is new to us. + if (! exists $retVal{$peg1}) { + $retVal{$peg1} = { $peg2 => $score }; + } else { + $retVal{$peg1}->{$peg2} = $score; + } + } + } + } + # Return the result. + return %retVal; +} + + +=head3 LowBBHs + + 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 featureID + +ID of the feature whose best hits are desired. + +=item cutoff + +Maximum permissible score for inclusion in the results. + +=item RETURN + +Returns a hash mapping feature IDs to scores. + +=back + +=cut +#: 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 Sims + + my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters); + +Get a list of similarities for a specified feature. Similarity information is not kept in the +Sprout database; rather, they are retrieved from a network server. The similarities are +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 fid + +ID of the feature whose similarities are desired, or reference to a list of IDs +of features whose similarities are desired. + +=item maxN + +Maximum number of similarities to return. + +=item maxP + +Minumum allowable similarity score. + +=item select + +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. + +=item max_expand + +The maximum number of features to expand. + +=item filters + +Reference to a hash containing filter information, or a subroutine that can be +used to filter the sims. + +=item RETURN + +Returns a reference to a list of similarity objects, or C if an error +occurred. + +=back + +=cut + +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 IsAllGenomes + + my $flag = $sprout->IsAllGenomes(\@list, \@checkList); + +Return TRUE if all genomes in the second list are represented in the first list at +least one. Otherwise, return FALSE. If the second list is omitted, the first list is +compared to a list of all the genomes. + +=over 4 + +=item list + +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 TRUE if every item in the second list appears at least once in the +first list, else FALSE. + +=back + +=cut + +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; +} + +=head3 GetGroups + + 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 { + # 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) { + # Get the genome ID and group, and add this genome to the group's list. + my ($genomeID, $group) = @{$genome}; + push @{$retVal{$group}}, $genomeID; + } + } + # Return the hash we just built. + return %retVal; +} + +=head3 MyGenomes + + my @genomes = Sprout::MyGenomes($dataDir); + +Return a list of the genomes to be included in the Sprout. + +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. + +=over 4 + +=item dataDir + +Directory containing the Sprout load files. + +=back + +=cut +#: Return Type @; +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 LoadFileName + + my $fileName = Sprout::LoadFileName($dataDir, $tableName); + +Return the name of the load file for the specified table in the specified data +directory. + +=over 4 + +=item dataDir + +Directory containing the Sprout load files. + +=item tableName + +Name of the table whose load file is desired. + +=item RETURN + +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 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 DeleteGenome + + my $stats = $sprout->DeleteGenome($genomeID, $testFlag); + +Delete a genome from the database. + +=over 4 + +=item genomeID + +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 statistics object describing the rows deleted. + +=back + +=cut +#: 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 Fix + + my %fixedHash = $sprout->Fix(%groupHash); + +Prepare a genome group hash (like that returned by L) for processing. +The groups will be combined into the appropriate super-groups. + +=over 4 + +=item groupHash + +Hash to be fixed up. + +=item RETURN + +Returns a fixed-up version of the hash. + +=back + +=cut + +sub Fix { + # Get the parameters. + my ($self, %groupHash) = @_; + # Create the result hash. + my %retVal = (); + # Copy over the genomes. + for my $groupID (keys %groupHash) { + # Get the super-group name. + my $realGroupID = $self->SuperGroup($groupID); + # Append this group's genomes into the result hash + # using the super-group name. + push @{$retVal{$realGroupID}}, @{$groupHash{$groupID}}; + } + # Return the result hash. + return %retVal; +} + +=head3 GroupPageName + + my $name = $sprout->GroupPageName($group); + +Return the name of the page for the specified NMPDR group. + +=over 4 + +=item group + +Name of the relevant group. + +=item RETURN + +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 + +sub GroupPageName { + # Get the parameters. + my ($self, $group) = @_; + # Check for the group file data. + my %superTable = $self->CheckGroupFile(); + # Compute the real group name. + my $realGroup = $self->SuperGroup($group); + # Get the associated page name. + my $retVal = "../content/$superTable{$realGroup}->{page}"; + # Return the result. + return $retVal; +} + + +=head3 AddProperty + + $sprout->AddProperty($featureID, $key, @values); + +Add a new attribute value (Property) to a feature. + +=over 4 + +=item peg + +ID of the feature to which the attribute is to be added. + +=item key + +Name of the attribute (key). + +=item values + +Values of the attribute. + +=back + +=cut +#: 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 CheckGroupFile + + my %groupData = $sprout->CheckGroupFile(); + +Get the group file hash. The group file hash describes the relationship +between a group and the super-group to which it belongs for purposes of +display. The super-group name is computed from the first capitalized word +in the actual group name. For each super-group, the group file contains +the page name and a list of the species expected to be in the group. +Each species is specified by a genus and a species name. A species name +of C<0> implies an entire genus. + +This method returns a hash from super-group names to a hash reference. Each +resulting hash reference contains the following fields. + +=over 4 + +=item page + +The super-group's web page in the NMPDR. + +=item contents + +A list of 2-tuples, each containing a genus name followed by a species name +(or 0, indicating all species). This list indicates which organisms belong +in the super-group. + +=back + +=cut + +sub CheckGroupFile { + # Get the parameters. + my ($self) = @_; + # Check to see if we already have this hash. + if (! defined $self->{groupHash}) { + # We don't, so we need to read it in. + my %groupHash; + # Read the group file. + my @groupLines = Tracer::GetFile("$FIG_Config::sproutData/groups.tbl"); + # Loop through the list of sort-of groups. + for my $groupLine (@groupLines) { + my ($name, $page, @contents) = split /\t/, $groupLine; + $groupHash{$name} = { page => $page, + contents => [ map { [ split /\s*,\s*/, $_ ] } @contents ] + }; + } + # Save the hash. + $self->{groupHash} = \%groupHash; + } + # Return the result. + return %{$self->{groupHash}}; +} + +=head2 Virtual Methods + +=head3 CleanKeywords + + my $cleanedString = $sprout->CleanKeywords($searchExpression); + +Clean up a search expression or keyword list. This involves converting the periods +in EC numbers to underscores, converting non-leading minus signs to underscores, +a vertical bar or colon to an apostrophe, and forcing lower case for all alphabetic +characters. In addition, any extra spaces are removed. + +=over 4 + +=item searchExpression + +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 + +Cleaned expression or keyword list. + +=back + +=cut + +sub CleanKeywords { + # Get the parameters. + my ($self, $searchExpression) = @_; + # Get the stemmer. + my $stemmer = $self->GetStemmer(); + # Convert the search expression using the stemmer. + my $retVal = $stemmer->PrepareSearchExpression($searchExpression); + Trace("Cleaned keyword list for \"$searchExpression\" is \"$retVal\".") if T(3); + # Return the result. + return $retVal; +} + +=head3 GetSourceObject + + my $source = $erdb->GetSourceObject(); + +Return the object to be used in creating load files for this database. + +=cut + +sub GetSourceObject { + # Get the parameters. + my ($self) = @_; + # Check to see if we already have a source object. + my $retVal = $self->{_fig}; + if (! defined $retVal) { + # No, so create one. + require FIG; + $retVal = FIG->new(); + } + # Return the object. + return $retVal; +} + +=head3 SectionList + + my @sections = $erdb->SectionList(); + +Return a list of the names for the different data sections used when loading this database. +The default is a single string, in which case there is only one section representing the +entire database. + +=cut + +sub SectionList { + # Get the parameters. + my ($self, $source) = @_; + # Ask the BaseSproutLoader for a section list. + require BaseSproutLoader; + my @retVal = BaseSproutLoader::GetSectionList($self); + # Return the list. + return @retVal; +} + +=head3 Loader + + my $groupLoader = $erdb->Loader($groupName, $options); + +Return an [[ERDBLoadGroupPm]] object for the specified load group. This method is used +by [[ERDBGeneratorPl]] to create the load group objects. If you are not using +[[ERDBGeneratorPl]], you don't need to override this method. + +=over 4 + +=item groupName + +Name of the load group whose object is to be returned. The group name is +guaranteed to be a single word with only the first letter capitalized. + +=item options + +Reference to a hash of command-line options. + +=item RETURN + +Returns an [[ERDBLoadGroupPm]] object that can be used to process the specified load group +for this database. + +=back + +=cut + +sub Loader { + # Get the parameters. + my ($self, $groupName, $options) = @_; + # Compute the loader name. + my $loaderClass = "${groupName}SproutLoader"; + # Pull in its definition. + require "$loaderClass.pm"; + # Create an object for it. + my $retVal = eval("$loaderClass->new(\$self, \$options)"); + # Insure it worked. + Confess("Could not create $loaderClass object: $@") if $@; + # Return it to the caller. + return $retVal; +} + + +=head3 LoadGroupList + + my @groups = $erdb->LoadGroupList(); + +Returns a list of the names for this database's load groups. This method is used +by [[ERDBGeneratorPl]] when the user wishes to load all table groups. The default +is a single group called 'All' that loads everything. + +=cut + +sub LoadGroupList { + # Return the list. + return qw(Genome Subsystem Annotation Property Source Reaction Synonym Feature Drug); +} + +=head3 LoadDirectory + + my $dirName = $erdb->LoadDirectory(); + +Return the name of the directory in which load files are kept. The default is +the FIG temporary directory, which is a really bad choice, but it's always there. + +=cut + +sub LoadDirectory { + # Get the parameters. + my ($self) = @_; + # Return the directory name. + return $self->{dataDir}; +} + +=head2 Internal Utility Methods + +=head3 GetStemmer + + my $stermmer = $sprout->GetStemmer(); + +Return the stemmer object for this database. + +=cut + +sub GetStemmer { + # Get the parameters. + my ($self) = @_; + # Declare the return variable. + my $retVal = $self->{stemmer}; + if (! defined $retVal) { + # We don't have one pre-built, so we build and save it now. + $retVal = BioWords->new(exceptions => "$FIG_Config::sproutData/Exceptions.txt", + stops => "$FIG_Config::sproutData/StopWords.txt", + cache => 0); + $self->{stemmer} = $retVal; + } + # Return the result. + return $retVal; +} + +=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. + +A functional assignment is always of the form + + set YYYY function to + ZZZZ + +where I is the B, and I is the actual functional role. In most cases, +the user and the assigning user (from MadeAnnotation) will be the same, but that is +not always the case. + +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 user + +Name of the assigning user. + +=item text + +Text of the annotation. + +=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. + +=back + +=cut + +sub _ParseAssignment { + # Get the parameters. + my ($user, $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); + 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); + } + # 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; } +=head3 _CheckFeature + + my $flag = $sprout->_CheckFeature($fid); + +Return TRUE if the specified FID is probably an NMPDR feature ID, else FALSE. + +=over 4 + +=item fid + +Feature ID to check. + +=item RETURN + +Returns TRUE if the FID is for one of the NMPDR genomes, else FALSE. + +=back + +=cut + +sub _CheckFeature { + # Get the parameters. + my ($self, $fid) = @_; + # Insure we have a genome hash. + my $genomes = $self->_GenomeHash(); + # Get the feature's genome ID. + my ($genomeID) = FIGRules::ParseFeatureID($fid); + # Return an indicator of whether or not the genome ID is in the hash. + return ($self->{genomeHash}->{$genomeID} ? 1 : 0); +} + =head3 FriendlyTimestamp Convert a time number to a user-friendly time stamp for display. @@ -2967,65 +4312,141 @@ sub FriendlyTimestamp { my ($timeValue) = @_; - my $retVal = strftime("%a %b %e %H:%M:%S %Y", localtime($timeValue)); + my $retVal = localtime($timeValue); return $retVal; } -=head3 AddProperty -C<< my = $sprout->AddProperty($featureID, $key, $value, $url); >> +=head3 Hint + + my $htmlText = SearchHelper::Hint($wikiPage, $hintText); -Add a new attribute value (Property) to a feature. In the SEED system, attributes can -be added to almost any object. In Sprout, they can only be added to features. In -Sprout, attributes are implemented using I. A property represents a key/value -pair. If the particular key/value pair coming in is not already in the database, a new -B record is created to hold it. +Return the HTML for a small question mark that displays the specified hint text when it is clicked. +This HTML can be put in forms to provide a useful hinting mechanism. =over 4 -=item peg +=item wikiPage -ID of the feature to which the attribute is to be replied. +Name of the wiki page to be popped up when the hint mark is clicked. -=item key +=item hintText -Name of the attribute (key). +Text to display for the hint. It is raw html, but may not contain any double quotes. + +=item RETURN -=item value +Returns the html for the hint facility. The resulting html shows a small button-like thing that +uses the standard FIG popup technology. -Value of the attribute. +=back -=item url +=cut -URL or text citation from which the property was obtained. +sub Hint { + # Get the parameters. + my ($wikiPage, $hintText) = @_; + # Escape the single quotes in the hint text. + my $quotedText = $hintText; + $quotedText =~ s/'/\\'/g; + # Convert the wiki page name to a URL. + my $wikiURL = join("", map { ucfirst $_ } split /\s+/, $wikiPage); + $wikiURL = "$FIG_Config::cgi_url/wiki/view.cgi/FIG/$wikiURL"; + # Compute the mouseover script. + my $mouseOver = "doTooltip(this, '$quotedText')"; + # Create the html. + my $retVal = " "; + # Return it. + return $retVal; +} + +=head3 _GenomeHash + + my $gHash = $sprout->_GenomeHash(); + +Return a hash mapping all NMPDR genome IDs to [[ERDBObjectPm]] genome objects. + +=cut + +sub _GenomeHash { + # Get the parameters. + my ($self) = @_; + # Do we already have a filled hash? + if (! $self->{genomeHashFilled}) { + # No, create it. + my %gHash = map { $_->PrimaryValue('id') => $_ } $self->GetList("Genome", "", []); + $self->{genomeHash} = \%gHash; + # Denote we have it. + $self->{genomeHashFilled} = 1; + } + # Return the hash. + return $self->{genomeHash}; +} + +=head3 _GenomeData + + my $genomeData = $sprout->_GenomeData($genomeID); + +Return an [[ERDBObjectPm]] object for the specified genome, or an undefined +value if the genome does not exist. + +=over 4 + +=item genomeID + +ID of the desired genome. + +=item RETURN + +Returns either an [[ERDBObjectPm]] containing the genome, or an undefined value. +If the genome exists, it will have been read into the genome cache. =back =cut -#: Return Type ; -sub AddProperty { + +sub _GenomeData { + # Get the parameters. + my ($self, $genomeID) = @_; + # Are we in the genome hash? + if (! exists $self->{genomeHash}->{$genomeID} && ! $self->{genomeHashFilled}) { + # The genome isn't in the hash, and the hash is not complete, so we try to + # read it. + $self->{genomeHash}->{$genomeID} = $self->GetEntity(Genome => $genomeID); + } + # Return the result. + return $self->{genomeHash}->{$genomeID}; +} + +=head3 _CacheGenome + + $sprout->_CacheGenome($genomeID, $genomeData); + +Store the specified genome object in the genome cache if it is already there. + +=over 4 + +=item genomeID + +ID of the genome to store in the cache. + +=item genomeData + +An [[ERDBObjectPm]] containing at least the data for the specified genome. +Note that the Genome may not be the primary object in it, so a fully-qualified +field name has to be used to retrieve data from it. + +=back + +=cut + +sub _CacheGenome { # Get the parameters. - my ($self, $featureID, $key, $value, $url) = @_; - # Declare the variable to hold the desired property ID. - my $propID; - # Attempt to find a property record for this key/value pair. - my @properties = $self->GetFlat(['Property'], - "Property(property-name) = ? AND Property(property-value) = ?", - [$key, $value], 'Property(id)'); - if (@properties) { - # Here the property is already in the database. We save its ID. - $propID = $properties[0]; - # Here the property value does not exist. We need to generate an ID. It will be set - # to a number one greater than the maximum value in the database. This call to - # GetAll will stop after one record. - my @maxProperty = $self->GetAll(['Property'], "ORDER BY Property(id) DESC", [], ['Property(id)'], - 1); - $propID = $maxProperty[0]->[0] + 1; - # Insert the new property value. - $self->Insert('Property', { 'property-name' => $key, 'property-value' => $value, id => $propID }); + my ($self, $genomeID, $genomeData) = @_; + # Only proceed if we don't already have the genome. + if (! exists $self->{genomeHash}->{$genomeID}) { + $self->{genomeHash}->{$genomeID} = $genomeData; } - # Now we connect the incoming feature to the property. - $self->Insert('HasProperty', { 'from-link' => $featureID, 'to-link' => $propID, evidence => $url }); } 1; \ No newline at end of file