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

Diff of /Sprout/Sprout.pm

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

revision 1.7, Wed Jan 26 22:26:09 2005 UTC revision 1.124, Wed Mar 4 00:09:43 2009 UTC
# Line 2  Line 2 
2    
3          use Data::Dumper;          use Data::Dumper;
4          use strict;          use strict;
         use Carp;  
5          use DBKernel;          use DBKernel;
6          use XML::Simple;          use XML::Simple;
7          use DBQuery;      use ERDBQuery;
8          use DBObject;      use ERDBObject;
         use ERDB;  
9          use Tracer;          use Tracer;
10          use FIGRules;          use FIGRules;
11        use FidCheck;
12          use Stats;          use Stats;
13      use POSIX qw(strftime);      use POSIX qw(strftime);
14        use BasicLocation;
15        use CustomAttributes;
16        use RemoteCustomAttributes;
17        use CGI qw(-nosticky);
18        use WikiTools;
19        use BioWords;
20        use base qw(ERDB);
21    
22  =head1 Sprout Database Manipulation Object  =head1 Sprout Database Manipulation Object
23    
# Line 25  Line 30 
30  on the constructor. For example, the following invocation specifies a PostgreSQL database named I<GenDB>  on the constructor. For example, the following invocation specifies a PostgreSQL database named I<GenDB>
31  whose definition and data files are in a co-directory named F<Data>.  whose definition and data files are in a co-directory named F<Data>.
32    
33  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' });
34    
35  Once you have a sprout object, you may use it to re-create the database, load the tables from  Once you have a sprout object, you may use it to re-create the database, load the tables from
36  tab-delimited flat files and perform queries. Several special methods are provided for common  tab-delimited flat files and perform queries. Several special methods are provided for common
37  query tasks. For example, L</genomes> lists the IDs of all the genomes in the database and  query tasks. For example, L</Genomes> lists the IDs of all the genomes in the database and
38  L</dna_seq> returns the DNA sequence for a specified genome location.  L</DNASeq> returns the DNA sequence for a specified genome location.
39    
40  =cut  The Sprout object is a subclass of the ERDB object and inherits all its properties and methods.
41    
42  #: Constructor SFXlate->new_sprout_only();  =cut
43    
44  =head2 Public Methods  =head2 Public Methods
45    
46  =head3 new  =head3 new
47    
48  C<< my $sprout = Sprout->new($dbName, \%options); >>      my $sprout = Sprout->new($dbName, \%options);
49    
50  This is the constructor for a sprout object. It connects to the database and loads the  This is the constructor for a sprout object. It connects to the database and loads the
51  database definition into memory. The positional first parameter specifies the name of the  database definition into memory. The positional first parameter specifies the name of the
# Line 50  Line 55 
55    
56  =item dbName  =item dbName
57    
58  Name of the database.  Name of the database. If omitted, the default Sprout database name is used.
59    
60  =item options  =item options
61    
# Line 62  Line 67 
67    
68  * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)  * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)
69    
70  * B<userData> user name and password, delimited by a slash (default C<root/>)  * B<userData> user name and password, delimited by a slash (default same as SEED)
71    
72  * B<port> connection port (default C<0>)  * B<port> connection port (default C<0>)
73    
74    * B<sock> connection socket (default same as SEED)
75    
76  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)
77    
78  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)
79    
80    * B<noDBOpen> suppresses the connection to the database if TRUE, else FALSE
81    
82    * B<host> name of the database host
83    
84  =back  =back
85    
86  For example, the following constructor call specifies a database named I<Sprout> and a user name of  For example, the following constructor call specifies a database named I<Sprout> and a user name of
87  I<fig> with a password of I<admin>. The database load files are in the directory  I<fig> with a password of I<admin>. The database load files are in the directory
88  F</usr/fig/SproutData>.  F</usr/fig/SproutData>.
89    
90  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' });
91    
92    In order to work properly with [[ERDBGeneratorPl]], the constructor has an alternate
93    form.
94    
95        my $sprout = Sprout->new(dbd => $filename);
96    
97    Where I<$fileName> is the name of the DBD file. This enables us to specify an alternate
98    DBD for the loader, which is important when the database format changes.
99    
100  =cut  =cut
101    
102  sub new {  sub new {
103          # Get the parameters.          # Get the parameters.
104          my ($class, $dbName, $options) = @_;          my ($class, $dbName, $options) = @_;
105        # Check for the alternate signature, and default the database name if it is missing.
106        if ($dbName eq 'dbd') {
107            $dbName = $FIG_Config::sproutDB;
108            $options = { xmlFileName => $options };
109        } elsif (! defined $dbName) {
110            $dbName = $FIG_Config::sproutDB;
111        } elsif (ref $dbName eq 'HASH') {
112            $options = $dbName;
113            $dbName = $FIG_Config::sproutDB;
114        }
115        # Compute the DBD directory.
116        my $dbd_dir = (defined($FIG_Config::dbd_dir) ? $FIG_Config::dbd_dir :
117                                                      $FIG_Config::fig );
118          # Compute the options. We do this by starting with a table of defaults and overwriting with          # Compute the options. We do this by starting with a table of defaults and overwriting with
119          # the incoming data.          # the incoming data.
120          my $optionTable = Tracer::GetOptions({          my $optionTable = Tracer::GetOptions({
121                                             dbType               => 'mysql',                     # database type                         dbType       => $FIG_Config::dbms,
122                                             dataDir              => 'Data',                      # data file directory                                                          # database type
123                                             xmlFileName  => 'SproutDBD.xml', # database definition file name                         dataDir      => $FIG_Config::sproutData,
124                                             userData             => 'root/',                     # user name and password                                                          # data file directory
125                                             port                 => 0,                           # database connection port                         xmlFileName  => "$dbd_dir/SproutDBD.xml",
126                                                            # database definition file name
127                           userData     => "$FIG_Config::sproutUser/$FIG_Config::sproutPass",
128                                                            # user name and password
129                           port         => $FIG_Config::sproutPort,
130                                                            # database connection port
131                           sock         => $FIG_Config::sproutSock,
132                           host         => $FIG_Config::sprout_host,
133                                             maxSegmentLength => 4500,            # maximum feature segment length                                             maxSegmentLength => 4500,            # maximum feature segment length
134                                             maxSequenceLength => 8000,           # maximum contig sequence length                                             maxSequenceLength => 8000,           # maximum contig sequence length
135                           noDBOpen     => 0,               # 1 to suppress the database open
136                           demandDriven => 0,               # 1 for forward-only queries
137                                            }, $options);                                            }, $options);
138          # Get the data directory.          # Get the data directory.
139          my $dataDir = $optionTable->{dataDir};          my $dataDir = $optionTable->{dataDir};
# Line 100  Line 141 
141          $optionTable->{userData} =~ m!([^/]*)/(.*)$!;          $optionTable->{userData} =~ m!([^/]*)/(.*)$!;
142          my ($userName, $password) = ($1, $2);          my ($userName, $password) = ($1, $2);
143          # Connect to the database.          # Connect to the database.
144          my $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName, $password, $optionTable->{port});      my $dbh;
145        if (! $optionTable->{noDBOpen}) {
146            Trace("Connect data: host = $optionTable->{host}, port = $optionTable->{port}.") if T(3);
147            $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,
148                                    $password, $optionTable->{port}, $optionTable->{host}, $optionTable->{sock});
149        }
150          # Create the ERDB object.          # Create the ERDB object.
151          my $xmlFileName = "$optionTable->{xmlFileName}";          my $xmlFileName = "$optionTable->{xmlFileName}";
152          my $erdb = ERDB->new($dbh, $xmlFileName);      my $retVal = ERDB::new($class, $dbh, $xmlFileName, %$optionTable);
153          # Create this object.      # Add the option table and XML file name.
154          my $self = { _erdb => $erdb, _options => $optionTable, _xmlName => $xmlFileName };      $retVal->{_options} = $optionTable;
155          # Bless and return it.      $retVal->{_xmlName} = $xmlFileName;
156          bless $self;      # Set up space for the group file data.
157          return $self;      $retVal->{groupHash} = undef;
158        # Set up space for the genome hash. We use this to identify NMPDR genomes
159        # and remember genome data.
160        $retVal->{genomeHash} = {};
161        $retVal->{genomeHashFilled} = 0;
162        # Remember the data directory name.
163        $retVal->{dataDir} = $dataDir;
164        # Return it.
165        return $retVal;
166  }  }
167    
168  =head3 MaxSegment  =head3 ca
169    
170  C<< my $length = $sprout->MaxSegment(); >>      my $ca = $sprout->ca():;
171    
172  This method returns the maximum permissible length of a feature segment. The length is important  Return the [[CustomAttributesPm]] object for retrieving object
173  because it enables us to make reasonable guesses at how to find features inside a particular  properties.
 contig region. For example, if the maximum length is 4000 and we're looking for a feature that  
 overlaps the region from 6000 to 7000 we know that the starting position must be between 2001  
 and 10999.  
174    
175  =cut  =cut
 #: Return Type $;  
 sub MaxSegment {  
         my $self = shift @_;  
         return $self->{_options}->{maxSegmentLength};  
 }  
   
 =head3 MaxSequence  
   
 C<< my $length = $sprout->MaxSequence(); >>  
176    
177  This method returns the maximum permissible length of a contig sequence. A contig is broken  sub ca {
178  into sequences in order to save memory resources. In particular, when manipulating features,      # Get the parameters.
179  we generally only need a few sequences in memory rather than the entire contig.      my ($self) = @_;
180        # Do we already have an attribute object?
181  =cut      my $retVal = $self->{_ca};
182  #: Return Type $;      if (! defined $retVal) {
183  sub MaxSequence {          # No, create one. How we do it depends on the configuration.
184          my $self = shift @_;          if ($FIG_Config::attrURL) {
185          return $self->{_options}->{maxSequenceLength};              Trace("Remote attribute server $FIG_Config::attrURL chosen.") if T(3);
186                $retVal = RemoteCustomAttributes->new($FIG_Config::attrURL);
187            } elsif ($FIG_Config::attrDbName) {
188                Trace("Local attribute database $FIG_Config::attrDbName chosen.") if T(3);
189                my $user = ($FIG_Config::arch eq 'win' ? 'self' : scalar(getpwent()));
190                $retVal = CustomAttributes->new(user => $user);
191            }
192            # Save it for next time.
193            $self->{_ca} = $retVal;
194        }
195        # Return the result.
196        return $retVal;
197  }  }
198    
199  =head3 Get  =head3 CoreGenomes
   
 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<objectName>(I<fieldName>)>. For example, the  
 following call requests all B<Genome> objects for the genus specified in the variable  
 $genus.  
   
 C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ?", [$genus]); >>  
   
 The WHERE clause contains a single question mark, so there is a single additional  
 parameter representing the parameter value. It would also be possible to code  
   
 C<< $query = $sprout->Get(['Genome'], "Genome(genus) = \'$genus\'"); >>  
   
 however, this version of the call would generate a syntax error if there were any quote  
 characters inside the variable C<$genus>.  
   
 The use of the strange parenthesized notation for field names enables us to distinguish  
 hyphens contained within field names from minus signs that participate in the computation  
 of the WHERE clause. All of the methods that manipulate fields will use this same notation.  
   
 It is possible to specify multiple entity and relationship names in order to retrieve more than  
 one object's data at the same time, which allows highly complex joined queries. For example,  
   
 C<< $query = $sprout->Get(['Genome', 'ComesFrom', 'Source'], "Genome(genus) = ?", [$genus]); >>  
   
 This query returns all the genomes for a particular genus and allows access to the  
 sources from which they came. The join clauses to go from Genome to Source are generated  
 automatically.  
   
 Finally, the filter clause can contain sort information. To do this, simply put an C<ORDER BY>  
 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<objectName>(I<fieldName>)>.  
 For example, the following filter string gets all genomes for a particular genus and sorts  
 them by species name.  
   
 C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ? ORDER BY Genome(species)", [$genus]); >>  
   
 It is also permissible to specify I<only> an ORDER BY clause. For example, the following invocation gets  
 all genomes ordered by genus and species.  
   
 C<< $query = $sprout->Get(['Genome'], "ORDER BY Genome(genus), Genome(species)"); >>  
200    
201  Odd things may happen if one of the ORDER BY fields is in a secondary relation. So, for example, an      my @genomes = $sprout->CoreGenomes($scope);
 attempt to order B<Feature>s by alias may (depending on the underlying database engine used) cause  
 a single feature to appear more than once.  
202    
203  If multiple names are specified, then the query processor will automatically determine a  Return the IDs of NMPDR genomes in the specified scope.
 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<IsParentOf>  
 which relates B<People> objects to other B<People> 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.  
204    
205  =over 4  =over 4
206    
207  =item objectNames  =item scope
   
 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<objectName>(I<fieldName>)>. 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  
208    
209  List of the parameters to be substituted in for the parameters marks in the filter clause.  Scope of the desired genomes. C<core> covers the original core genomes,
210    C<nmpdr> covers all genomes in NMPDR groups, and C<all> covers all
211    genomes in the system.
212    
213  =item RETURN  =item RETURN
214    
215  Returns a B<DBQuery> that can be used to iterate through all of the results.  Returns a list of the IDs for the genomes in the specified scope.
216    
217  =back  =back
218    
219  =cut  =cut
220    
221  sub Get {  sub CoreGenomes {
222          # Get the parameters.          # Get the parameters.
223          my $self = shift @_;      my ($self, $scope) = @_;
224          my ($objectNames, $filterClause, $parameterList) = @_;      # Declare the return variable.
225          # We differ from the ERDB Get method in that the parameter list is passed in as a list reference      my @retVal = ();
226          # rather than a list of parameters. The next step is to convert the parameters from a reference      # If we want all genomes, then this is easy.
227          # to a real list. We can only do this if the parameters have been specified.      if ($scope eq 'all') {
228          my @parameters;          @retVal = $self->Genomes();
229          if ($parameterList) { @parameters = @{$parameterList}; }      } else {
230          return $self->{_erdb}->Get($objectNames, $filterClause, @parameters);          # Here we're dealing with groups. Get the hash of all the
231            # genome groups.
232            my %groups = $self->GetGroups();
233            # Loop through the groups, keeping the ones that we want.
234            for my $group (keys %groups) {
235                # Decide if we want to keep this group.
236                my $keepGroup = 0;
237                if ($scope eq 'nmpdr') {
238                    # NMPDR mode: keep all groups.
239                    $keepGroup = 1;
240                } elsif ($scope eq 'core') {
241                    # CORE mode. Only keep real core groups.
242                    if (grep { $group =~ /$_/ } @{$FIG_Config::realCoreGroups}) {
243                        $keepGroup = 1;
244                    }
245                }
246                # Add this group if we're keeping it.
247                if ($keepGroup) {
248                    push @retVal, @{$groups{$group}};
249                }
250            }
251        }
252        # Return the result.
253        return @retVal;
254  }  }
255    
256  =head3 GetEntity  =head3 SuperGroup
257    
258  C<< my $entityObject = $sprout->GetEntity($entityType, $ID); >>      my $superGroup = $sprout->SuperGroup($groupName);
259    
260  Return an object describing the entity instance with a specified ID.  Return the name of the super-group containing the specified NMPDR genome
261    group. If no appropriate super-group can be found, an error will be
262    thrown.
263    
264  =over 4  =over 4
265    
266  =item entityType  =item groupName
267    
268  Entity type name.  Name of the group whose super-group is desired.
   
 =item ID  
   
 ID of the desired entity.  
269    
270  =item RETURN  =item RETURN
271    
272  Returns a B<DBObject> representing the desired entity instance, or an undefined value if no  Returns the name of the super-group containing the incoming group.
 instance is found with the specified key.  
273    
274  =back  =back
275    
276  =cut  =cut
277    
278  sub GetEntity {  sub SuperGroup {
279          # Get the parameters.          # Get the parameters.
280          my $self = shift @_;      my ($self, $groupName) = @_;
281          my ($entityType, $ID) = @_;      # Declare the return variable.
282          # Create a query.      my $retVal;
283          my $query = $self->Get([$entityType], "$entityType(id) = ?", [$ID]);      # Get the group hash.
284          # Get the first (and only) object.      my %groupHash = $self->CheckGroupFile();
285          my $retVal = $query->Fetch();      # Find the super-group genus.
286        $groupName =~ /([A-Z]\w+)/;
287        my $nameThing = $1;
288        # See if it's directly in the group hash.
289        if (exists $groupHash{$nameThing}) {
290            # Yes, then it's our result.
291            $retVal = $nameThing;
292        } else {
293            # No, so we have to search.
294            for my $superGroup (keys %groupHash) {
295                # Get this super-group's item list.
296                my $list = $groupHash{$superGroup}->{contents};
297                # Search it.
298                if (grep { $_->[0] eq $nameThing } @{$list}) {
299                    $retVal = $superGroup;
300                }
301            }
302            # Make sure we found something.
303            if (! $retVal) {
304                Confess("No super-group found for \"$groupName\".");
305            }
306        }
307          # Return the result.          # Return the result.
308          return $retVal;          return $retVal;
309  }  }
310    
311  =head3 GetEntityValues  =head3 MaxSegment
   
 C<< my @values = GetEntityValues($entityType, $ID, \@fields); >>  
   
 Return a list of values from a specified entity instance.  
   
 =over 4  
   
 =item entityType  
   
 Entity type name.  
   
 =item ID  
   
 ID of the desired entity.  
   
 =item fields  
   
 List of field names, each of the form I<objectName>C<(>I<fieldName>C<)>.  
   
 =item RETURN  
312    
313  Returns a flattened list of the values of the specified fields for the specified entity.      my $length = $sprout->MaxSegment();
314    
315  =back  This method returns the maximum permissible length of a feature segment. The length is important
316    because it enables us to make reasonable guesses at how to find features inside a particular
317    contig region. For example, if the maximum length is 4000 and we're looking for a feature that
318    overlaps the region from 6000 to 7000 we know that the starting position must be between 2001
319    and 10999.
320    
321  =cut  =cut
322  #: Return Type @;  #: Return Type $;
323  sub GetEntityValues {  sub MaxSegment {
324          # Get the parameters.      my ($self) = @_;
325          my $self = shift @_;      return $self->{_options}->{maxSegmentLength};
         my ($entityType, $ID, $fields) = @_;  
         # Get the specified entity.  
         my $entity = $self->GetEntity($entityType, $ID);  
         # Declare the return list.  
         my @retVal = ();  
         # If we found the entity, push the values into the return list.  
         if ($entity) {  
                 push @retVal, $entity->Values($fields);  
         }  
         # Return the result.  
         return @retVal;  
326  }  }
327    
328  =head3 ShowMetaData  =head3 MaxSequence
   
 C<< $sprout->ShowMetaData($fileName); >>  
   
 This method outputs a description of the database to an HTML file in the data directory.  
   
 =over 4  
   
 =item fileName  
329    
330  Fully-qualified name to give to the output file.      my $length = $sprout->MaxSequence();
331    
332  =back  This method returns the maximum permissible length of a contig sequence. A contig is broken
333    into sequences in order to save memory resources. In particular, when manipulating features,
334    we generally only need a few sequences in memory rather than the entire contig.
335    
336  =cut  =cut
337    #: Return Type $;
338  sub ShowMetaData {  sub MaxSequence {
339          # Get the parameters.      my ($self) = @_;
340          my $self = shift @_;      return $self->{_options}->{maxSequenceLength};
         my ($fileName) = @_;  
         # Compute the file name.  
         my $options = $self->{_options};  
         # Call the show method on the underlying ERDB object.  
         $self->{_erdb}->ShowMetaData($fileName);  
341  }  }
342    
343  =head3 Load  =head3 Load
344    
345  C<< $sprout->Load($rebuild); >>;      $sprout->Load($rebuild);;
346    
347  Load the database from files in the data directory, optionally re-creating the tables.  Load the database from files in the data directory, optionally re-creating the tables.
348    
# Line 363  Line 354 
354  The files are loaded based on the presumption that each line of the file is a record in the  The files are loaded based on the presumption that each line of the file is a record in the
355  relation, and the individual fields are delimited by tabs. Tab and new-line characters inside  relation, and the individual fields are delimited by tabs. Tab and new-line characters inside
356  fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must  fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must
357  be presented in the order given in the relation tables produced by the L</ShowMetaData> method.  be presented in the order given in the relation tables produced by the database documentation.
358    
359  =over 4  =over 4
360    
# Line 382  Line 373 
373  #: Return Type %;  #: Return Type %;
374  sub Load {  sub Load {
375          # Get the parameters.          # Get the parameters.
376          my $self = shift @_;      my ($self, $rebuild) = @_;
         my ($rebuild) = @_;  
         # Get the database object.  
         my $erdb = $self->{_erdb};  
377          # Load the tables from the data directory.          # Load the tables from the data directory.
378          my $retVal = $erdb->LoadTables($self->{_options}->{dataDir}, $rebuild);      my $retVal = $self->LoadTables($self->{_options}->{dataDir}, $rebuild);
379          # Return the statistics.          # Return the statistics.
380          return $retVal;          return $retVal;
381  }  }
382    
383  =head3 LoadUpdate  =head3 LoadUpdate
384    
385  C<< my %stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>      my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList);
386    
387  Load updates to one or more database tables. This method enables the client to make changes to one  Load updates to one or more database tables. This method enables the client to make changes to one
388  or two tables without reloading the whole database. For each table, there must be a corresponding  or two tables without reloading the whole database. For each table, there must be a corresponding
# Line 426  Line 414 
414  #: Return Type $%;  #: Return Type $%;
415  sub LoadUpdate {  sub LoadUpdate {
416          # Get the parameters.          # Get the parameters.
417          my $self = shift @_;      my ($self, $truncateFlag, $tableList) = @_;
         my ($truncateFlag, $tableList) = @_;  
         # Get the database object.  
         my $erdb = $self->{_erdb};  
418          # Declare the return value.          # Declare the return value.
419          my $retVal = Stats->new();          my $retVal = Stats->new();
420          # Get the data directory.          # Get the data directory.
# Line 438  Line 423 
423          # Loop through the incoming table names.          # Loop through the incoming table names.
424          for my $tableName (@{$tableList}) {          for my $tableName (@{$tableList}) {
425                  # Find the table's file.                  # Find the table's file.
426                  my $fileName = "$dataDir/$tableName";          my $fileName = LoadFileName($dataDir, $tableName);
427                  if (! -e $fileName) {          if (! $fileName) {
428                          $fileName = "$fileName.dtx";              Trace("No load file found for $tableName in $dataDir.") if T(0);
429                  }          } else {
430                  # Attempt to load this table.                  # Attempt to load this table.
431                  my $result = $erdb->LoadTable($fileName, $tableName, $truncateFlag);              my $result = $self->LoadTable($fileName, $tableName, truncate => $truncateFlag);
432                  # Accumulate the resulting statistics.                  # Accumulate the resulting statistics.
433                  $retVal->Accumulate($result);                  $retVal->Accumulate($result);
434          }          }
435        }
436          # Return the statistics.          # Return the statistics.
437          return $retVal;          return $retVal;
438  }  }
439    
440    =head3 GenomeCounts
441    
442        my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete);
443    
444    Count the number of genomes in each domain. If I<$complete> is TRUE, only complete
445    genomes will be included in the counts.
446    
447    =over 4
448    
449    =item complete
450    
451    TRUE if only complete genomes are to be counted, FALSE if all genomes are to be
452    counted
453    
454    =item RETURN
455    
456    A six-element list containing the number of genomes in each of six categories--
457    Archaea, Bacteria, Eukaryota, Viral, Environmental, and Unknown, respectively.
458    
459    =back
460    
461    =cut
462    
463    sub GenomeCounts {
464        # Get the parameters.
465        my ($self, $complete) = @_;
466        # Set the filter based on the completeness flag.
467        my $filter = ($complete ? "Genome(complete) = 1" : "");
468        # Get all the genomes and the related taxonomy information.
469        my @genomes = $self->GetAll(['Genome'], $filter, [], ['Genome(id)', 'Genome(taxonomy)']);
470        # Clear the counters.
471        my ($arch, $bact, $euk, $vir, $env, $unk) = (0, 0, 0, 0, 0, 0);
472        # Loop through, counting the domains.
473        for my $genome (@genomes) {
474            if    ($genome->[1] =~ /^archaea/i)  { ++$arch }
475            elsif ($genome->[1] =~ /^bacter/i)   { ++$bact }
476            elsif ($genome->[1] =~ /^eukar/i)    { ++$euk }
477            elsif ($genome->[1] =~ /^vir/i)      { ++$vir }
478            elsif ($genome->[1] =~ /^env/i)      { ++$env }
479            else  { ++$unk }
480        }
481        # Return the counts.
482        return ($arch, $bact, $euk, $vir, $env, $unk);
483    }
484    
485    =head3 ContigCount
486    
487        my $count = $sprout->ContigCount($genomeID);
488    
489    Return the number of contigs for the specified genome ID.
490    
491    =over 4
492    
493    =item genomeID
494    
495    ID of the genome whose contig count is desired.
496    
497    =item RETURN
498    
499    Returns the number of contigs for the specified genome.
500    
501    =back
502    
503    =cut
504    
505    sub ContigCount {
506        # Get the parameters.
507        my ($self, $genomeID) = @_;
508        # Get the contig count.
509        my $retVal = $self->GetCount(['Contig', 'HasContig'], "HasContig(from-link) = ?", [$genomeID]);
510        # Return the result.
511        return $retVal;
512    }
513    
514    =head3 GenomeMenu
515    
516        my $html = $sprout->GenomeMenu(%options);
517    
518    Generate a genome selection control with the specified name and options.
519    This control is almost but not quite the same as the genome control in the
520    B<SearchHelper> class. Eventually, the two will be combined.
521    
522    =over 4
523    
524    =item options
525    
526    Optional parameters for the control (see below).
527    
528    =item RETURN
529    
530    Returns the HTML for a genome selection control on a form (sometimes called a popup menu).
531    
532    =back
533    
534    The valid options are as follows.
535    
536    =over 4
537    
538    =item name
539    
540    Name to give this control for use in passing it to the form. The default is C<myGenomeControl>.
541    Terrible things will happen if you have two controls with the same name on the same page.
542    
543    =item filter
544    
545    If specified, a filter for the list of genomes to display. The filter should be in the form of a
546    list reference, a string, or a hash reference. If it is a list reference, the first element
547    of the list should be the filter string, and the remaining elements the filter parameters. If it is a
548    string, it will be split into a list at each included tab. If it is a hash reference, it should be
549    a hash that maps genomes which should be included to a TRUE value.
550    
551    =item multiSelect
552    
553    If TRUE, then the user can select multiple genomes. If FALSE, the user can only select one genome.
554    
555    =item size
556    
557    Number of rows to display in the control. The default is C<10>
558    
559    =item id
560    
561    ID to give this control. The default is the value of the C<name> option. Nothing will work correctly
562    unless this ID is unique.
563    
564    =item selected
565    
566    A comma-delimited list of selected genomes, or a reference to a list of selected genomes. The
567    default is none.
568    
569    =item class
570    
571    If specified, a style class to assign to the genome control.
572    
573    =back
574    
575    =cut
576    
577    sub GenomeMenu {
578        # Get the parameters.
579        my ($self, %options) = @_;
580        # Get the control's name and ID.
581        my $menuName = $options{name} || $options{id} || 'myGenomeControl';
582        my $menuID = $options{id} || $menuName;
583        Trace("Genome menu name = $menuName with ID $menuID.") if T(3);
584        # Compute the IDs for the status display.
585        my $divID = "${menuID}_status";
586        my $urlID = "${menuID}_url";
587        # Compute the code to show selected genomes in the status area.
588        my $showSelect = "showSelected('$menuID', '$divID', '$urlID', $FIG_Config::genome_control_cap)";
589        # Check for single-select or multi-select.
590        my $multiSelect = $options{multiSelect} || 0;
591        # Get the style data.
592        my $class = $options{class} || '';
593        # Get the list of pre-selected items.
594        my $selections = $options{selected} || [];
595        if (ref $selections ne 'ARRAY') {
596            $selections = [ split /\s*,\s*/, $selections ];
597        }
598        my %selected = map { $_ => 1 } @{$selections};
599        # Extract the filter information. The default is no filtering. It can be passed as a tab-delimited
600        # string, a hash reference, or a list reference.
601        my ($filterHash, $filterString);
602        my $filterParms = $options{filter} || "";
603        if (ref $filterParms eq 'HASH') {
604            $filterHash = $filterParms;
605            $filterParms = [];
606            $filterString = "";
607        } else {
608            if (! ref $filterParms) {
609                $filterParms = [split /\t|\\t/, $filterParms];
610            }
611            $filterString = shift @{$filterParms};
612        }
613        # Check for possible subsystem filtering. If there is one, we will tack the
614        # relationship onto the object name list.
615        my @objectNames = qw(Genome);
616        if ($filterString =~ /ParticipatesIn\(/) {
617            push @objectNames, 'ParticipatesIn';
618        }
619        # Get a list of all the genomes in group order. In fact, we only need them ordered
620        # by name (genus,species,strain), but putting primary-group in front enables us to
621        # take advantage of an existing index.
622        my @genomeList = $self->GetAll(\@objectNames, "$filterString ORDER BY Genome(primary-group), Genome(genus), Genome(species), Genome(unique-characterization)",
623                                       $filterParms,
624                                       [qw(Genome(primary-group) Genome(id) Genome(genus) Genome(species) Genome(unique-characterization) Genome(taxonomy) Genome(contigs))]);
625        # Apply the hash filter (if any).
626        if (defined $filterHash) {
627            @genomeList = grep { $filterHash->{$_->[1]} } @genomeList;
628        }
629        # Create a hash to organize the genomes by group. Each group will contain a list of
630        # 2-tuples, the first element being the genome ID and the second being the genome
631        # name.
632        my %gHash = ();
633        for my $genome (@genomeList) {
634            # Get the genome data.
635            my ($group, $genomeID, $genus, $species, $strain, $taxonomy, $contigs) = @{$genome};
636            # Compute its name. This is the genus, species, strain (if any), and the contig count.
637            my $name = "$genus $species ";
638            $name .= "$strain " if $strain;
639            my $contigCount = ($contigs == 1 ? "" : ", $contigs contigs");
640            # Now we get the domain. The domain tells us the display style of the organism.
641            my ($domain) = split /\s*;\s*/, $taxonomy, 2;
642            # Now compute the display group. This is normally the primary group, but if the
643            # organism is supporting, we blank it out.
644            my $displayGroup = ($group eq $FIG_Config::otherGroup ? "" : $group);
645            # Push the genome into the group's list. Note that we use the real group
646            # name for the hash key here, not the display group name.
647            push @{$gHash{$group}}, [$genomeID, $name, $contigCount, $domain];
648        }
649        # We are almost ready to unroll the menu out of the group hash. The final step is to separate
650        # the supporting genomes by domain. First, we extract the NMPDR groups and sort them. They
651        # are sorted by the first capitalized word. Groups with "other" are sorted after groups
652        # that aren't "other". At some point, we will want to make this less complicated.
653        my %sortGroups = map { $_ =~ /(other)?(.*)([A-Z].+)/; "$3$1$2" => $_ }
654                             grep { $_ ne $FIG_Config::otherGroup } keys %gHash;
655        my @groups = map { $sortGroups{$_} } sort keys %sortGroups;
656        # Remember the number of NMPDR groups.
657        my $nmpdrGroupCount = scalar @groups;
658        # Are there any supporting genomes?
659        if (exists $gHash{$FIG_Config::otherGroup}) {
660            # Loop through the supporting genomes, classifying them by domain. We'll also keep a list
661            # of the domains found.
662            my @otherGenomes = @{$gHash{$FIG_Config::otherGroup}};
663            my @domains = ();
664            for my $genomeData (@otherGenomes) {
665                my ($genomeID, $name, $contigCount, $domain) = @{$genomeData};
666                if (exists $gHash{$domain}) {
667                    push @{$gHash{$domain}}, $genomeData;
668                } else {
669                    $gHash{$domain} = [$genomeData];
670                    push @domains, $domain;
671                }
672            }
673            # Add the domain groups at the end of the main group list. The main group list will now
674            # contain all the categories we need to display the genomes.
675            push @groups, sort @domains;
676            # Delete the supporting group.
677            delete $gHash{$FIG_Config::otherGroup};
678        }
679        # Now it gets complicated. We need a way to mark all the NMPDR genomes. We take advantage
680        # of the fact they come first in the list. We'll accumulate a count of the NMPDR genomes
681        # and use that to make the selections.
682        my $nmpdrCount = 0;
683        # Create the type counters.
684        my $groupCount = 1;
685        # Get the number of rows to display.
686        my $rows = $options{size} || 10;
687        # If we're multi-row, create an onChange event.
688        my $onChangeTag = ( $rows > 1 ? " onChange=\"$showSelect;\" onFocus=\"$showSelect;\"" : "" );
689        # Set up the multiple-select flag.
690        my $multipleTag = ($multiSelect ? " multiple" : "" );
691        # Set up the style class.
692        my $classTag = ($class ? " $class" : "" );
693        # Create the SELECT tag and stuff it into the output array.
694        my @lines = qq(<SELECT name="$menuName" id="$menuID" class="genomeSelect $class" $onChangeTag$multipleTag$classTag size="$rows">);
695        # Loop through the groups.
696        for my $group (@groups) {
697            # Get the genomes in the group.
698            for my $genome (@{$gHash{$group}}) {
699                # If this is an NMPDR organism, we add an extra style and count it.
700                my $nmpdrStyle = "";
701                if ($nmpdrGroupCount > 0) {
702                    $nmpdrCount++;
703                    $nmpdrStyle = " Core";
704                }
705                # Get the organism ID, name, contig count, and domain.
706                my ($genomeID, $name, $contigCount, $domain) = @{$genome};
707                # See if we're pre-selected.
708                my $selectTag = ($selected{$genomeID} ? " SELECTED" : "");
709                # Compute the display name.
710                my $nameString = "$name ($genomeID$contigCount)";
711                # Generate the option tag.
712                my $optionTag = "<OPTION class=\"$domain$nmpdrStyle\" title=\"$group\" value=\"$genomeID\"$selectTag>$nameString</OPTION>";
713                push @lines, "    $optionTag";
714            }
715            # Record this group in the nmpdrGroup count. When that gets to 0, we've finished the NMPDR
716            # groups.
717            $nmpdrGroupCount--;
718        }
719        # Close the SELECT tag.
720        push @lines, "</SELECT>";
721        if ($rows > 1) {
722            # We're in a non-compact mode, so we need to add some selection helpers. First is
723            # the search box. This allows the user to type text and change which genomes are
724            # displayed. For multiple-select mode, we include a button that selects the displayed
725            # genes. For single-select mode, we use a plain label instead.
726            my $searchThingName = "${menuID}_SearchThing";
727            my $searchThingLabel = "Type to narrow selection";
728            my $searchThingButton = "";
729            my $goHint = "";
730            if ($multiSelect) {
731                $searchThingButton = qq(<INPUT type="button" name="MacroSearch" class="button" value="Go" onClick="selectShowing('$menuID', '$searchThingName'); $showSelect;" />);
732                $goHint = " Click <strong>Go</strong> to select them.";
733            }
734            push @lines, "<br />$searchThingLabel&nbsp;" .
735                         qq(<INPUT type="text" id="$searchThingName" name="$searchThingName" class="genomeSearchThing" onKeyup="showTyped('$menuID', '$searchThingName');" />) .
736                         $searchThingButton .
737                         Hint("GenomeControl", "Type a genome ID or part of an organism name to filter the genomes displayed.$goHint") . "<br />";
738            # For multi-select mode, we also have buttons to set and clear selections.
739            if ($multiSelect) {
740                push @lines, qq(<INPUT type="button" name="ClearAll" class="bigButton genomeButton" value="Clear All" onClick="clearAll(getElementById('$menuID')); $showSelect" />);
741                push @lines, qq(<INPUT type="button" name="SelectAll" class="bigButton genomeButton" value="Select All" onClick="selectAll(getElementById('$menuID')); $showSelect" />);
742                push @lines, qq(<INPUT type="button" name="NMPDROnly" class="bigButton genomeButton" value="Select NMPDR" onClick="selectSome(getElementById('$menuID'), $nmpdrCount, true); $showSelect;" />);
743            }
744            # Add a hidden field we can use to generate organism page hyperlinks.
745            push @lines, qq(<INPUT type="hidden" id="$urlID" value="$FIG_Config::cgi_url/wiki/rest.cgi/NmpdrPlugin/SeedViewer?page=Organism;organism=" />);
746            # Add the status display. This tells the user what's selected no matter where the list is scrolled.
747            push @lines, qq(<DIV id="$divID" class="Panel"></DIV>);
748        }
749        # Assemble all the lines into a string.
750        my $retVal = join("\n", @lines, "");
751        # Return the result.
752        return $retVal;
753    }
754    
755    
756    =head3 Stem
757    
758        my $stem = $sprout->Stem($word);
759    
760    Return the stem of the specified word, or C<undef> if the word is not
761    stemmable. Note that even if the word is stemmable, the stem may be
762    the same as the original word.
763    
764    =over 4
765    
766    =item word
767    
768    Word to convert into a stem.
769    
770    =item RETURN
771    
772    Returns a stem of the word (which may be the word itself), or C<undef> if
773    the word is not stemmable.
774    
775    =back
776    
777    =cut
778    
779    sub Stem {
780        # Get the parameters.
781        my ($self, $word) = @_;
782        # Get the stemmer object.
783        my $stemmer = $self->{stemmer};
784        if (! defined $stemmer) {
785            # We don't have one pre-built, so we build and save it now.
786            $stemmer = BioWords->new(exceptions => "$FIG_Config::sproutData/Exceptions.txt",
787                                     stops => "$FIG_Config::sproutData/StopWords.txt",
788                                     cache => 0);
789            $self->{stemmer} = $stemmer;
790        }
791        # Try to stem the word.
792        my $retVal = $stemmer->Process($word);
793        # Return the result.
794        return $retVal;
795    }
796    
797    
798  =head3 Build  =head3 Build
799    
800  C<< $sprout->Build(); >>      $sprout->Build();
801    
802  Build the database. The database will be cleared and the tables re-created from the metadata.  Build the database. The database will be cleared and the tables re-created from the metadata.
803  This method is useful when a database is brand new or when the database definition has  This method is useful when a database is brand new or when the database definition has
# Line 463  Line 807 
807  #: Return Type ;  #: Return Type ;
808  sub Build {  sub Build {
809          # Get the parameters.          # Get the parameters.
810          my $self = shift @_;      my ($self) = @_;
811          # Create the tables.          # Create the tables.
812          $self->{_erdb}->CreateTables;      $self->CreateTables();
813  }  }
814    
815  =head3 Genomes  =head3 Genomes
816    
817  C<< my @genomes = $sprout->Genomes(); >>      my @genomes = $sprout->Genomes();
818    
819  Return a list of all the genome IDs.  Return a list of all the genome IDs.
820    
# Line 478  Line 822 
822  #: Return Type @;  #: Return Type @;
823  sub Genomes {  sub Genomes {
824          # Get the parameters.          # Get the parameters.
825          my $self = shift @_;      my ($self) = @_;
826          # Get all the genomes.          # Get all the genomes.
827          my @retVal = $self->GetFlat(['Genome'], "", [], 'Genome(id)');          my @retVal = $self->GetFlat(['Genome'], "", [], 'Genome(id)');
828          # Return the list of IDs.          # Return the list of IDs.
# Line 487  Line 831 
831    
832  =head3 GenusSpecies  =head3 GenusSpecies
833    
834  C<< my $infoString = $sprout->GenusSpecies($genomeID); >>      my $infoString = $sprout->GenusSpecies($genomeID);
835    
836  Return the genus, species, and unique characterization for a genome.  Return the genus, species, and unique characterization for a genome.
837    
# Line 508  Line 852 
852  #: Return Type $;  #: Return Type $;
853  sub GenusSpecies {  sub GenusSpecies {
854          # Get the parameters.          # Get the parameters.
855          my $self = shift @_;      my ($self, $genomeID) = @_;
856          my ($genomeID) = @_;      # Declare the return value.
857          # Get the data for the specified genome.      my $retVal;
858          my @values = $self->GetEntityValues('Genome', $genomeID, ['Genome(genus)', 'Genome(species)',      # Get the genome data.
859                                                                                                                            'Genome(unique-characterization)']);      my $genomeData = $self->_GenomeData($genomeID);
860          # Format the result and return it.      # Only proceed if we found the genome.
861          my $retVal = join(' ', @values);      if (defined $genomeData) {
862            $retVal = $genomeData->PrimaryValue('Genome(scientific-name)');
863        }
864        # Return it.
865          return $retVal;          return $retVal;
866  }  }
867    
868  =head3 FeaturesOf  =head3 FeaturesOf
869    
870  C<< my @features = $sprout->FeaturesOf($genomeID, $ftype); >>      my @features = $sprout->FeaturesOf($genomeID, $ftype);
871    
872  Return a list of the features relevant to a specified genome.  Return a list of the features relevant to a specified genome.
873    
# Line 545  Line 892 
892  #: Return Type @;  #: Return Type @;
893  sub FeaturesOf {  sub FeaturesOf {
894          # Get the parameters.          # Get the parameters.
895          my $self = shift @_;      my ($self, $genomeID,$ftype) = @_;
         my ($genomeID,$ftype) = @_;  
896          # Get the features we want.          # Get the features we want.
897          my @features;          my @features;
898          if (!$ftype) {          if (!$ftype) {
# Line 566  Line 912 
912    
913  =head3 FeatureLocation  =head3 FeatureLocation
914    
915  C<< my @locations = $sprout->FeatureLocation($featureID); >>      my @locations = $sprout->FeatureLocation($featureID);
916    
917  Return the location of a feature in its genome's contig segments. In a list context, this method  Return the location of a feature in its genome's contig segments. In a list context, this method
918  will return a list of the locations. In a scalar context, it will return the locations as a space-  will return a list of the locations. In a scalar context, it will return the locations as a space-
# Line 590  Line 936 
936  =item RETURN  =item RETURN
937    
938  Returns a list of the feature's contig segments. The locations are returned as a list in a list  Returns a list of the feature's contig segments. The locations are returned as a list in a list
939  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
940    wasn't found.
941    
942  =back  =back
943    
944  =cut  =cut
945  #: Return Type @;  
 #: Return Type $;  
946  sub FeatureLocation {  sub FeatureLocation {
947          # Get the parameters.          # Get the parameters.
948          my $self = shift @_;      my ($self, $featureID) = @_;
949          my ($featureID) = @_;      # Declare the return variable.
         # Create a query for the feature locations.  
         my $query = $self->Get(['IsLocatedIn'], "IsLocatedIn(from-link) = ? ORDER BY IsLocatedIn(locN)",  
                                                    [$featureID]);  
         # Create the return list.  
950          my @retVal = ();          my @retVal = ();
951          # Set up the variables used to determine if we have adjacent segments. This initial setup will      # Get the feature record.
952          # not match anything.      my $object = $self->GetEntity('Feature', $featureID);
953          my ($prevContig, $prevBeg, $prevDir, $prevLen) = ("", 0, "0", 0);      # Only proceed if we found it.
954          # Loop through the query results, creating location specifiers.      if (defined $object) {
955          while (my $location = $query->Fetch()) {          # Get the location string.
956                  # Get the location parameters.          my $locString = $object->PrimaryValue('Feature(location-string)');
957                  my ($contigID, $beg, $dir, $len) = $location->Values(['IsLocatedIn(to-link)',          # Create the return list.
958                          'IsLocatedIn(beg)', 'IsLocatedIn(dir)', 'IsLocatedIn(len)']);          @retVal = split /\s*,\s*/, $locString;
                 # 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";  
959          }          }
960          # Return the list in the format indicated by the context.          # Return the list in the format indicated by the context.
961          return (wantarray ? @retVal : join(' ', @retVal));      return (wantarray ? @retVal : join(',', @retVal));
962  }  }
963    
964  =head3 ParseLocation  =head3 ParseLocation
965    
966  C<< my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location); >>      my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location);
967    
968  Split a location specifier into the contig ID, the starting point, the direction, and the  Split a location specifier into the contig ID, the starting point, the direction, and the
969  length.  length.
# Line 658  Line 982 
982  =back  =back
983    
984  =cut  =cut
985  #: Return Type @;  
986  sub ParseLocation {  sub ParseLocation {
987          # Get the parameter.      # Get the parameter. Note that if we're called as an instance method, we ignore
988        # the first parameter.
989        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
990          my ($location) = @_;          my ($location) = @_;
991          # Parse it into segments.          # Parse it into segments.
992          $location =~ /^(.*)_(\d*)([+-_])(\d*)$/;      $location =~ /^(.+)_(\d+)([+\-_])(\d+)$/;
993          my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);          my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);
994          # If the direction is an underscore, convert it to a + or -.          # If the direction is an underscore, convert it to a + or -.
995          if ($dir eq "_") {          if ($dir eq "_") {
# Line 679  Line 1005 
1005          return ($contigID, $start, $dir, $len);          return ($contigID, $start, $dir, $len);
1006  }  }
1007    
 =head3 DNASeq  
1008    
 C<< my $sequence = $sprout->DNASeq(\@locationList); >>  
1009    
1010  This method returns the DNA sequence represented by a list of locations. The list of locations  =head3 PointLocation
1011  should be of the form returned by L</featureLocation> when in a list context. In other words,  
1012  each location is of the form I<contigID>C<_>I<begin>I<dir>I<end>.      my $found = Sprout::PointLocation($location, $point);
1013    
1014    Return the offset into the specified location of the specified point on the contig. If
1015    the specified point is before the location, a negative value will be returned. If it is
1016    beyond the location, an undefined value will be returned. It is assumed that the offset
1017    is for the location's contig. The location can either be new-style (using a C<+> or C<->
1018    and a length) or old-style (using C<_> and start and end positions.
1019    
1020  =over 4  =over 4
1021    
1022  =item locationList  =item location
1023    
1024  List of location specifiers, each in the form I<contigID>C<_>I<begin>I<dir>I<end> (see  A location specifier (see L</FeatureLocation> for a description).
1025  L</FeatureLocation> for more about this format).  
1026    =item point
1027    
1028    The offset into the contig of the point in which we're interested.
1029    
1030  =item RETURN  =item RETURN
1031    
1032  Returns a string of nucleotides corresponding to the DNA segments in the location list.  Returns the offset inside the specified location of the specified point, a negative
1033    number if the point is before the location, or an undefined value if the point is past
1034    the location. If the length of the location is 0, this method will B<always> denote
1035    that it is outside the location. The offset will always be relative to the left-most
1036    position in the location.
1037    
1038  =back  =back
1039    
1040  =cut  =cut
1041  #: Return Type $;  
1042  sub DNASeq {  sub PointLocation {
1043          # Get the parameters.      # Get the parameter. Note that if we're called as an instance method, we ignore
1044          my $self = shift @_;      # the first parameter.
1045          my ($locationList) = @_;      shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1046          # Create the return string.      my ($location, $point) = @_;
1047        # Parse out the location elements. Note that this works on both old-style and new-style
1048        # locations.
1049        my ($contigID, $start, $dir, $len) = ParseLocation($location);
1050        # Declare the return variable.
1051        my $retVal;
1052        # Compute the offset. The computation is dependent on the direction of the location.
1053        my $offset = (($dir == '+') ? $point - $start : $point - ($start - $len + 1));
1054        # Return the offset if it's valid.
1055        if ($offset < $len) {
1056            $retVal = $offset;
1057        }
1058        # Return the offset found.
1059        return $retVal;
1060    }
1061    
1062    =head3 DNASeq
1063    
1064        my $sequence = $sprout->DNASeq(\@locationList);
1065    
1066    This method returns the DNA sequence represented by a list of locations. The list of locations
1067    should be of the form returned by L</featureLocation> when in a list context. In other words,
1068    each location is of the form I<contigID>C<_>I<begin>I<dir>I<end>.
1069    
1070    For example, the following would return the DNA sequence for contig C<83333.1:NC_000913>
1071    between positions 1401 and 1532, inclusive.
1072    
1073        my $sequence = $sprout->DNASeq('83333.1:NC_000913_1401_1532');
1074    
1075    =over 4
1076    
1077    =item locationList
1078    
1079    List of location specifiers, each in the form I<contigID>C<_>I<begin>I<dir>I<len> or
1080    I<contigID>C<_>I<begin>C<_>I<end> (see L</FeatureLocation> for more about this format).
1081    
1082    =item RETURN
1083    
1084    Returns a string of nucleotides corresponding to the DNA segments in the location list.
1085    
1086    =back
1087    
1088    =cut
1089    #: Return Type $;
1090    sub DNASeq {
1091        # Get the parameters.
1092        my ($self, $locationList) = @_;
1093        # Create the return string.
1094          my $retVal = "";          my $retVal = "";
1095          # Loop through the locations.          # Loop through the locations.
1096          for my $location (@{$locationList}) {          for my $location (@{$locationList}) {
# Line 720  Line 1104 
1104                  # the start point is the ending. Note that in the latter case we must reverse the DNA string                  # the start point is the ending. Note that in the latter case we must reverse the DNA string
1105                  # before putting it in the return value.                  # before putting it in the return value.
1106                  my ($start, $stop);                  my ($start, $stop);
1107            Trace("Parse of \"$location\" is $beg$dir$len.") if T(SDNA => 4);
1108                  if ($dir eq "+") {                  if ($dir eq "+") {
1109                          $start = $beg;                          $start = $beg;
1110                          $stop = $beg + $len - 1;                          $stop = $beg + $len - 1;
1111                  } else {                  } else {
1112                          $start = $beg + $len + 1;              $start = $beg - $len + 1;
1113                          $stop = $beg;                          $stop = $beg;
1114                  }                  }
1115            Trace("Looking for sequences containing $start through $stop.") if T(SDNA => 4);
1116                  my $query = $self->Get(['IsMadeUpOf','Sequence'],                  my $query = $self->Get(['IsMadeUpOf','Sequence'],
1117                          "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .                          "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .
1118                          " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",                          " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",
# Line 738  Line 1124 
1124                                  $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',                                  $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',
1125                                                                     'IsMadeUpOf(len)']);                                                                     'IsMadeUpOf(len)']);
1126                          my $stopPosition = $startPosition + $sequenceLength;                          my $stopPosition = $startPosition + $sequenceLength;
1127                Trace("Sequence is from $startPosition to $stopPosition.") if T(SDNA => 4);
1128                          # Figure out the start point and length of the relevant section.                          # Figure out the start point and length of the relevant section.
1129                          my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);                          my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);
1130                          my $len = ($stopPosition <= $stop ? $stopPosition : $stop) - $startPosition - $pos1;              my $len1 = ($stopPosition < $stop ? $stopPosition : $stop) + 1 - $startPosition - $pos1;
1131                Trace("Position is $pos1 for length $len1.") if T(SDNA => 4);
1132                          # Add the relevant data to the location data.                          # Add the relevant data to the location data.
1133                          $locationDNA .= substr($sequenceData, $pos1, $len);              $locationDNA .= substr($sequenceData, $pos1, $len1);
1134                  }                  }
1135                  # Add this location's data to the return string. Note that we may need to reverse it.                  # Add this location's data to the return string. Note that we may need to reverse it.
1136                  if ($dir eq '+') {                  if ($dir eq '+') {
1137                          $retVal .= $locationDNA;                          $retVal .= $locationDNA;
1138                  } else {                  } else {
1139                          $locationDNA = join('', reverse split //, $locationDNA);              $retVal .= FIG::reverse_comp($locationDNA);
                         $retVal .= $locationDNA;  
1140                  }                  }
1141          }          }
1142          # Return the result.          # Return the result.
# Line 758  Line 1145 
1145    
1146  =head3 AllContigs  =head3 AllContigs
1147    
1148  C<< my @idList = $sprout->AllContigs($genomeID); >>      my @idList = $sprout->AllContigs($genomeID);
1149    
1150  Return a list of all the contigs for a genome.  Return a list of all the contigs for a genome.
1151    
# Line 778  Line 1165 
1165  #: Return Type @;  #: Return Type @;
1166  sub AllContigs {  sub AllContigs {
1167          # Get the parameters.          # Get the parameters.
1168          my $self = shift @_;      my ($self, $genomeID) = @_;
         my ($genomeID) = @_;  
1169          # Ask for the genome's Contigs.          # Ask for the genome's Contigs.
1170          my @retVal = $self->GetFlat(['HasContig'], "HasContig(from-link) = ?", [$genomeID],          my @retVal = $self->GetFlat(['HasContig'], "HasContig(from-link) = ?", [$genomeID],
1171                                                                  'HasContig(to-link)');                                                                  'HasContig(to-link)');
# Line 787  Line 1173 
1173          return @retVal;          return @retVal;
1174  }  }
1175    
1176    =head3 GenomeLength
1177    
1178        my $length = $sprout->GenomeLength($genomeID);
1179    
1180    Return the length of the specified genome in base pairs.
1181    
1182    =over 4
1183    
1184    =item genomeID
1185    
1186    ID of the genome whose base pair count is desired.
1187    
1188    =item RETURN
1189    
1190    Returns the number of base pairs in all the contigs of the specified
1191    genome.
1192    
1193    =back
1194    
1195    =cut
1196    
1197    sub GenomeLength {
1198        # Get the parameters.
1199        my ($self, $genomeID) = @_;
1200        # Declare the return variable.
1201        my $retVal = 0;
1202        # Get the genome data.
1203        my $genomeData = $self->_GenomeData($genomeID);
1204        # Only proceed if it exists.
1205        if (defined $genomeData) {
1206            $retVal = $genomeData->PrimaryValue('Genome(dna-size)');
1207        }
1208        # Return the result.
1209        return $retVal;
1210    }
1211    
1212    =head3 FeatureCount
1213    
1214        my $count = $sprout->FeatureCount($genomeID, $type);
1215    
1216    Return the number of features of the specified type in the specified genome.
1217    
1218    =over 4
1219    
1220    =item genomeID
1221    
1222    ID of the genome whose feature count is desired.
1223    
1224    =item type
1225    
1226    Type of feature to count (eg. C<peg>, C<rna>, etc.).
1227    
1228    =item RETURN
1229    
1230    Returns the number of features of the specified type for the specified genome.
1231    
1232    =back
1233    
1234    =cut
1235    
1236    sub FeatureCount {
1237        # Get the parameters.
1238        my ($self, $genomeID, $type) = @_;
1239        # Compute the count.
1240        my $retVal = $self->GetCount(['HasFeature', 'Feature'],
1241                                    "HasFeature(from-link) = ? AND Feature(feature-type) = ?",
1242                                    [$genomeID, $type]);
1243        # Return the result.
1244        return $retVal;
1245    }
1246    
1247    =head3 GenomeAssignments
1248    
1249        my $fidHash = $sprout->GenomeAssignments($genomeID);
1250    
1251    Return a list of a genome's assigned features. The return hash will contain each
1252    assigned feature of the genome mapped to the text of its most recent functional
1253    assignment.
1254    
1255    =over 4
1256    
1257    =item genomeID
1258    
1259    ID of the genome whose functional assignments are desired.
1260    
1261    =item RETURN
1262    
1263    Returns a reference to a hash which maps each feature to its most recent
1264    functional assignment.
1265    
1266    =back
1267    
1268    =cut
1269    
1270    sub GenomeAssignments {
1271        # Get the parameters.
1272        my ($self, $genomeID) = @_;
1273        # Declare the return variable.
1274        my $retVal = {};
1275        # Query the genome's features.
1276        my $query = $self->Get(['HasFeature', 'Feature'], "HasFeature(from-link) = ?",
1277                               [$genomeID]);
1278        # Loop through the features.
1279        while (my $data = $query->Fetch) {
1280            # Get the feature ID and assignment.
1281            my ($fid, $assignment) = $data->Values(['Feature(id)', 'Feature(assignment)']);
1282            if ($assignment) {
1283                $retVal->{$fid} = $assignment;
1284            }
1285        }
1286        # Return the result.
1287        return $retVal;
1288    }
1289    
1290  =head3 ContigLength  =head3 ContigLength
1291    
1292  C<< my $length = $sprout->ContigLength($contigID); >>      my $length = $sprout->ContigLength($contigID);
1293    
1294  Compute the length of a contig.  Compute the length of a contig.
1295    
# Line 809  Line 1309 
1309  #: Return Type $;  #: Return Type $;
1310  sub ContigLength {  sub ContigLength {
1311          # Get the parameters.          # Get the parameters.
1312          my $self = shift @_;      my ($self, $contigID) = @_;
         my ($contigID) = @_;  
1313          # Get the contig's last sequence.          # Get the contig's last sequence.
1314          my $query = $self->Get(['IsMadeUpOf'],          my $query = $self->Get(['IsMadeUpOf'],
1315                  "IsMadeUpOf(from-link) = ? ORDER BY IsMadeUpOf(start-position) DESC",                  "IsMadeUpOf(from-link) = ? ORDER BY IsMadeUpOf(start-position) DESC",
# Line 821  Line 1320 
1320          # Set it from the sequence data, if any.          # Set it from the sequence data, if any.
1321          if ($sequence) {          if ($sequence) {
1322                  my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);                  my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);
1323                  $retVal = $start + $len;          $retVal = $start + $len - 1;
1324        }
1325        # Return the result.
1326        return $retVal;
1327    }
1328    
1329    =head3 ClusterPEGs
1330    
1331        my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs);
1332    
1333    Cluster the PEGs in a list according to the cluster coding scheme of the specified
1334    subsystem. In order for this to work properly, the subsystem object must have
1335    been used recently to retrieve the PEGs using the B<get_pegs_from_cell> or
1336    B<get_row> methods. This causes the cluster numbers to be pulled into the
1337    subsystem's color hash. If a PEG is not found in the color hash, it will not
1338    appear in the output sequence.
1339    
1340    =over 4
1341    
1342    =item sub
1343    
1344    Sprout subsystem object for the relevant subsystem, from the L</get_subsystem>
1345    method.
1346    
1347    =item pegs
1348    
1349    Reference to the list of PEGs to be clustered.
1350    
1351    =item RETURN
1352    
1353    Returns a list of the PEGs, grouped into smaller lists by cluster number.
1354    
1355    =back
1356    
1357    =cut
1358    #: Return Type $@@;
1359    sub ClusterPEGs {
1360        # Get the parameters.
1361        my ($self, $sub, $pegs) = @_;
1362        # Declare the return variable.
1363        my $retVal = [];
1364        # Loop through the PEGs, creating arrays for each cluster.
1365        for my $pegID (@{$pegs}) {
1366            my $clusterNumber = $sub->get_cluster_number($pegID);
1367            # Only proceed if the PEG is in a cluster.
1368            if ($clusterNumber >= 0) {
1369                # Push this PEG onto the sub-list for the specified cluster number.
1370                push @{$retVal->[$clusterNumber]}, $pegID;
1371            }
1372          }          }
1373          # Return the result.          # Return the result.
1374          return $retVal;          return $retVal;
# Line 829  Line 1376 
1376    
1377  =head3 GenesInRegion  =head3 GenesInRegion
1378    
1379  C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>      my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop);
1380    
1381  List the features which overlap a specified region in a contig.  List the features which overlap a specified region in a contig.
1382    
# Line 852  Line 1399 
1399  Returns a three-element list. The first element is a list of feature IDs for the features that  Returns a three-element list. The first element is a list of feature IDs for the features that
1400  overlap the region of interest. The second and third elements are the minimum and maximum  overlap the region of interest. The second and third elements are the minimum and maximum
1401  locations of the features provided on the specified contig. These may extend outside  locations of the features provided on the specified contig. These may extend outside
1402  the start and stop values.  the start and stop values. The first element (that is, the list of features) is sorted
1403    roughly by location.
1404    
1405  =back  =back
1406    
1407  =cut  =cut
1408  #: Return Type @;  
1409  sub GenesInRegion {  sub GenesInRegion {
1410          # Get the parameters.          # Get the parameters.
1411          my $self = shift @_;      my ($self, $contigID, $start, $stop) = @_;
1412          my ($contigID, $start, $stop) = @_;      # Get the maximum segment length.
1413        my $maximumSegmentLength = $self->MaxSegment;
1414        # Prime the values we'll use for the returned beginning and end.
1415        my @initialMinMax = ($self->ContigLength($contigID), 0);
1416        my ($min, $max) = @initialMinMax;
1417        # Get the overlapping features.
1418        my @featureObjects = $self->GeneDataInRegion($contigID, $start, $stop);
1419        # We'l use this hash to help us track the feature IDs and sort them. The key is the
1420        # feature ID and the value is a [$left,$right] pair indicating the maximum extent
1421        # of the feature's locations.
1422        my %featureMap = ();
1423        # Loop through them to do the begin/end analysis.
1424        for my $featureObject (@featureObjects) {
1425            # Get the feature's location string. This may contain multiple actual locations.
1426            my ($locations, $fid) = $featureObject->Values([qw(Feature(location-string) Feature(id))]);
1427            my @locationSegments = split /\s*,\s*/, $locations;
1428            # Loop through the locations.
1429            for my $locationSegment (@locationSegments) {
1430                # Construct an object for the location.
1431                my $locationObject = BasicLocation->new($locationSegment);
1432                # Merge the current segment's begin and end into the min and max.
1433                my ($left, $right) = ($locationObject->Left, $locationObject->Right);
1434                my ($beg, $end);
1435                if (exists $featureMap{$fid}) {
1436                    ($beg, $end) = @{$featureMap{$fid}};
1437                    $beg = $left if $left < $beg;
1438                    $end = $right if $right > $end;
1439                } else {
1440                    ($beg, $end) = ($left, $right);
1441                }
1442                $min = $beg if $beg < $min;
1443                $max = $end if $end > $max;
1444                # Store the feature's new extent back into the hash table.
1445                $featureMap{$fid} = [$beg, $end];
1446            }
1447        }
1448        # Now we must compute the list of the IDs for the features found. We start with a list
1449        # of midpoints / feature ID pairs. (It's not really a midpoint, it's twice the midpoint,
1450        # but the result of the sort will be the same.)
1451        my @list = map { [$featureMap{$_}->[0] + $featureMap{$_}->[1], $_] } keys %featureMap;
1452        # Now we sort by midpoint and yank out the feature IDs.
1453        my @retVal = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @list;
1454        # Return it along with the min and max.
1455        return (\@retVal, $min, $max);
1456    }
1457    
1458    =head3 GeneDataInRegion
1459    
1460        my @featureList = $sprout->GenesInRegion($contigID, $start, $stop);
1461    
1462    List the features which overlap a specified region in a contig.
1463    
1464    =over 4
1465    
1466    =item contigID
1467    
1468    ID of the contig containing the region of interest.
1469    
1470    =item start
1471    
1472    Offset of the first residue in the region of interest.
1473    
1474    =item stop
1475    
1476    Offset of the last residue in the region of interest.
1477    
1478    =item RETURN
1479    
1480    Returns a list of B<ERDBObjects> for the desired features. Each object will
1481    contain a B<Feature> record.
1482    
1483    =back
1484    
1485    =cut
1486    
1487    sub GeneDataInRegion {
1488        # Get the parameters.
1489        my ($self, $contigID, $start, $stop) = @_;
1490          # Get the maximum segment length.          # Get the maximum segment length.
1491          my $maximumSegmentLength = $self->MaxSegment;          my $maximumSegmentLength = $self->MaxSegment;
1492          # Create a hash to receive the feature list. We use a hash so that we can eliminate          # Create a hash to receive the feature list. We use a hash so that we can eliminate
1493          # duplicates easily.      # duplicates easily. The hash key will be the feature ID. The value will be the feature's
1494        # ERDBObject from the query.
1495          my %featuresFound = ();          my %featuresFound = ();
1496          # Prime the values we'll use for the returned beginning and end.      # Create a table of parameters for the queries. Each query looks for features travelling in
         my ($min, $max) = ($self->ContigLength($contigID), 0);  
         # Create a table of parameters for each query. Each query looks for features travelling in  
1497          # a particular direction. The query parameters include the contig ID, the feature direction,          # a particular direction. The query parameters include the contig ID, the feature direction,
1498          # the lowest possible start position, and the highest possible start position. This works          # the lowest possible start position, and the highest possible start position. This works
1499          # because each feature segment length must be no greater than the maximum segment length.          # because each feature segment length must be no greater than the maximum segment length.
# Line 878  Line 1502 
1502          # Loop through the query parameters.          # Loop through the query parameters.
1503          for my $parms (values %queryParms) {          for my $parms (values %queryParms) {
1504                  # Create the query.                  # Create the query.
1505                  my $query = $self->Get(['IsLocatedIn'],          my $query = $self->Get([qw(Feature IsLocatedIn)],
1506                          "IsLocatedIn(to-link)= ? AND IsLocatedIn(dir) = ? AND IsLocatedIn(beg) >= ? AND IsLocatedIn(beg) <= ?",                          "IsLocatedIn(to-link)= ? AND IsLocatedIn(dir) = ? AND IsLocatedIn(beg) >= ? AND IsLocatedIn(beg) <= ?",
1507                          $parms);                          $parms);
1508                  # Loop through the feature segments found.                  # Loop through the feature segments found.
1509                  while (my $segment = $query->Fetch) {                  while (my $segment = $query->Fetch) {
1510                          # Get the data about this segment.                          # Get the data about this segment.
1511                          my ($featureID, $dir, $beg, $len) = $segment->Values(['IsLocatedIn(from-link)',              my ($featureID, $contig, $dir, $beg, $len) = $segment->Values([qw(IsLocatedIn(from-link)
1512                                  'IsLocatedIn(dir)', 'IsLocatedIn(beg)', 'IsLocatedIn(len)']);                  IsLocatedIn(to-link) IsLocatedIn(dir) IsLocatedIn(beg) IsLocatedIn(len))]);
1513                          # Determine if this feature actually overlaps the region. The query insures that              # Determine if this feature segment actually overlaps the region. The query insures that
1514                          # this will be the case if the segment is the maximum length, so to fine-tune                          # this will be the case if the segment is the maximum length, so to fine-tune
1515                          # the results we insure that the inequality from the query holds using the actual                          # the results we insure that the inequality from the query holds using the actual
1516                          # length.                          # length.
1517                          my ($found, $end) = (0, 0);              my $loc = BasicLocation->new($contig, $beg, $dir, $len);
1518                          if ($dir eq '+') {              my $found = $loc->Overlap($start, $stop);
                                 $end = $beg + $len;  
                                 if ($end >= $start) {  
                                         # Denote we found a useful feature.  
                                         $found = 1;  
                                 }  
                         } elsif ($dir eq '-') {  
                                 $end = $beg - $len;  
                                 if ($end <= $stop) {  
                                         # Denote we found a useful feature.  
                                         $found = 1;  
                                 }  
                         }  
1519                          if ($found) {                          if ($found) {
1520                                  # Here we need to record the feature and update the minimum and maximum.                  # Save this feature in the result list.
1521                                  $featuresFound{$featureID} = 1;                  $featuresFound{$featureID} = $segment;
                                 if ($beg < $min) { $min = $beg; }  
                                 if ($end < $min) { $min = $end; }  
                                 if ($beg > $max) { $max = $beg; }  
                                 if ($end > $max) { $max = $end; }  
1522                          }                          }
1523                  }                  }
1524          }          }
1525          # Compute a list of the IDs for the features found.      # Return the ERDB objects for the features found.
1526          my @list = (sort (keys %featuresFound));      return values %featuresFound;
         # Return it along with the min and max.  
         return (\@list, $min, $max);  
1527  }  }
1528    
1529  =head3 FType  =head3 FType
1530    
1531  C<< my $ftype = $sprout->FType($featureID); >>      my $ftype = $sprout->FType($featureID);
1532    
1533  Return the type of a feature.  Return the type of a feature.
1534    
# Line 943  Line 1549 
1549  #: Return Type $;  #: Return Type $;
1550  sub FType {  sub FType {
1551          # Get the parameters.          # Get the parameters.
1552          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
1553          # Get the specified feature's type.          # Get the specified feature's type.
1554          my ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(feature-type)']);          my ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(feature-type)']);
1555          # Return the result.          # Return the result.
# Line 953  Line 1558 
1558    
1559  =head3 FeatureAnnotations  =head3 FeatureAnnotations
1560    
1561  C<< my @descriptors = $sprout->FeatureAnnotations($featureID); >>      my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag);
1562    
1563  Return the annotations of a feature.  Return the annotations of a feature.
1564    
# Line 963  Line 1568 
1568    
1569  ID of the feature whose annotations are desired.  ID of the feature whose annotations are desired.
1570    
1571    =item rawFlag
1572    
1573    If TRUE, the annotation timestamps will be returned in raw form; otherwise, they
1574    will be returned in human-readable form.
1575    
1576  =item RETURN  =item RETURN
1577    
1578  Returns a list of annotation descriptors. Each descriptor is a hash with the following fields.  Returns a list of annotation descriptors. Each descriptor is a hash with the following fields.
1579    
1580  * B<featureID> ID of the relevant feature.  * B<featureID> ID of the relevant feature.
1581    
1582  * B<timeStamp> time the annotation was made, in user-friendly format.  * B<timeStamp> time the annotation was made.
1583    
1584  * B<user> ID of the user who made the annotation  * B<user> ID of the user who made the annotation
1585    
# Line 981  Line 1591 
1591  #: Return Type @%;  #: Return Type @%;
1592  sub FeatureAnnotations {  sub FeatureAnnotations {
1593          # Get the parameters.          # Get the parameters.
1594          my $self = shift @_;      my ($self, $featureID, $rawFlag) = @_;
         my ($featureID) = @_;  
1595          # Create a query to get the feature's annotations and the associated users.          # Create a query to get the feature's annotations and the associated users.
1596          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1597                                                     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);                                                     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);
# Line 995  Line 1604 
1604                          $annotation->Values(['IsTargetOfAnnotation(from-link)',                          $annotation->Values(['IsTargetOfAnnotation(from-link)',
1605                                                                   'Annotation(time)', 'MadeAnnotation(from-link)',                                                                   'Annotation(time)', 'MadeAnnotation(from-link)',
1606                                                                   'Annotation(annotation)']);                                                                   'Annotation(annotation)']);
1607            # Convert the time, if necessary.
1608            if (! $rawFlag) {
1609                $timeStamp = FriendlyTimestamp($timeStamp);
1610            }
1611                  # Assemble them into a hash.                  # Assemble them into a hash.
1612          my $annotationHash = { featureID => $featureID,          my $annotationHash = { featureID => $featureID,
1613                                 timeStamp => FriendlyTimestamp($timeStamp),                                 timeStamp => $timeStamp,
1614                                                             user => $user, text => $text };                                                             user => $user, text => $text };
1615                  # Add it to the return list.                  # Add it to the return list.
1616                  push @retVal, $annotationHash;                  push @retVal, $annotationHash;
# Line 1008  Line 1621 
1621    
1622  =head3 AllFunctionsOf  =head3 AllFunctionsOf
1623    
1624  C<< my %functions = $sprout->AllFunctionsOf($featureID); >>      my %functions = $sprout->AllFunctionsOf($featureID);
1625    
1626  Return all of the functional assignments for a particular feature. The data is returned as a  Return all of the functional assignments for a particular feature. The data is returned as a
1627  hash of functional assignments to user IDs. A functional assignment is a type of annotation.  hash of functional assignments to user IDs. A functional assignment is a type of annotation,
1628  It has the format "XXXX\nset XXXX function to\nYYYYY". In this instance, XXXX is the user ID  Functional assignments are described in the L</ParseAssignment> function. Its worth noting that
1629  and YYYYY is the functional assignment text. Its worth noting that we cannot filter on the content  we cannot filter on the content of the annotation itself because it's a text field; however,
1630  of the annotation itself because it's a text field; however, this is not a big problem because most  this is not a big problem because most features only have a small number of annotations.
1631  features only have a small number of annotations. Finally, if a single user has multiple  Finally, if a single user has multiple functional assignments, we will only keep the most
1632  functional assignments, we will only keep the most recent one.  recent one.
1633    
1634  =over 4  =over 4
1635    
# Line 1026  Line 1639 
1639    
1640  =item RETURN  =item RETURN
1641    
1642  Returns a hash mapping the functional assignment IDs to user IDs.  Returns a hash mapping the user IDs to functional assignment IDs.
1643    
1644  =back  =back
1645    
# Line 1034  Line 1647 
1647  #: Return Type %;  #: Return Type %;
1648  sub AllFunctionsOf {  sub AllFunctionsOf {
1649          # Get the parameters.          # Get the parameters.
1650          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
1651          # Get all of the feature's annotations.          # Get all of the feature's annotations.
1652      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation'],      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1653                                                      "IsTargetOfAnnotation(from-link) = ?",                                                      "IsTargetOfAnnotation(from-link) = ?",
1654                              [$featureID], ['Annotation(time)', 'Annotation(annotation)']);                              [$featureID], ['Annotation(time)', 'Annotation(annotation)',
1655                                               'MadeAnnotation(from-link)']);
1656          # Declare the return hash.          # Declare the return hash.
1657          my %retVal;          my %retVal;
     # Declare a hash for insuring we only make one assignment per user.  
     my %timeHash = ();  
1658      # Now we sort the assignments by timestamp in reverse.      # Now we sort the assignments by timestamp in reverse.
1659      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;
1660          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1661      for my $annotation (@sortedQuery) {      for my $annotation (@sortedQuery) {
1662          # Get the annotation fields.          # Get the annotation fields.
1663          my ($timeStamp, $text) = @{$annotation};          my ($timeStamp, $text, $user) = @{$annotation};
1664                  # Check to see if this is a functional assignment.                  # Check to see if this is a functional assignment.
1665                  my ($user, $function) = ParseAssignment($text);          my ($actualUser, $function) = _ParseAssignment($user, $text);
1666          if ($user && ! exists $timeHash{$user}) {          if ($actualUser && ! exists $retVal{$actualUser}) {
1667              # Here it is a functional assignment and there has been no              # Here it is a functional assignment and there has been no
1668              # previous assignment for this user, so we stuff it in the              # previous assignment for this user, so we stuff it in the
1669              # return hash.              # return hash.
1670                          $retVal{$function} = $user;              $retVal{$actualUser} = $function;
             # Insure we don't assign to this user again.  
             $timeHash{$user} = 1;  
1671                  }                  }
1672          }          }
1673          # Return the hash of assignments found.          # Return the hash of assignments found.
# Line 1067  Line 1676 
1676    
1677  =head3 FunctionOf  =head3 FunctionOf
1678    
1679  C<< my $functionText = $sprout->FunctionOf($featureID, $userID); >>      my $functionText = $sprout->FunctionOf($featureID, $userID);
1680    
1681  Return the most recently-determined functional assignment of a particular feature.  Return the most recently-determined functional assignment of a particular feature.
1682    
1683  The functional assignment is handled differently depending on the type of feature. If  The functional assignment is handled differently depending on the type of feature. If
1684  the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional  the feature is identified by a FIG ID (begins with the string C<fig|>), then the functional
1685  assignment is a type of annotation. It has the format "XXXX\nset XXXX function to\nYYYYY". In this  assignment is taken from the B<Feature> or C<Annotation> table, depending.
 instance, XXXX is the user ID and YYYYY is the functional assignment text. 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.  
1686    
1687  Each user has an associated list of trusted users. The assignment returned will be the most  Each user has an associated list of trusted users. The assignment returned will be the most
1688  recent one by at least one of the trusted users. If no trusted user list is available, then  recent one by at least one of the trusted users. If no trusted user list is available, then
1689  the specified user and FIG are considered trusted. If the user ID is omitted, only FIG  the specified user and FIG are considered trusted. If the user ID is omitted, only FIG
1690  is trusted.  is trusted.
1691    
1692  If the feature is B<not> identified by a FIG ID, then the functional assignment  If the feature is B<not> identified by a FIG ID, then we search the aliases for it.
1693  information is taken from the B<ExternalAliasFunc> table. If the table does  If no matching alias is found, we return an undefined value.
 not contain an entry for the feature, an undefined value is returned.  
1694    
1695  =over 4  =over 4
1696    
# Line 1095  Line 1700 
1700    
1701  =item userID (optional)  =item userID (optional)
1702    
1703  ID of the user whose function determination is desired. If omitted, only the latest  ID of the user whose function determination is desired. If omitted, the primary
1704  C<FIG> assignment will be returned.  functional assignment in the B<Feature> table will be returned.
1705    
1706  =item RETURN  =item RETURN
1707    
# Line 1108  Line 1713 
1713  #: Return Type $;  #: Return Type $;
1714  sub FunctionOf {  sub FunctionOf {
1715          # Get the parameters.          # Get the parameters.
1716          my $self = shift @_;      my ($self, $featureID, $userID) = @_;
         my ($featureID, $userID) = @_;  
1717      # Declare the return value.      # Declare the return value.
1718      my $retVal;      my $retVal;
1719      # Determine the ID type.      # Find a FIG ID for this feature.
1720      if ($featureID =~ m/^fig\|/) {      my ($fid) = $self->FeaturesByAlias($featureID);
1721          # Here we have a FIG feature ID. We must build the list of trusted      # Only proceed if we have an ID.
1722          # users.      if ($fid) {
1723            # Here we have a FIG feature ID.
1724            if (!$userID) {
1725                # Use the primary assignment.
1726                ($retVal) = $self->GetEntityValues('Feature', $fid, ['Feature(assignment)']);
1727            } else {
1728                # We must build the list of trusted users.
1729          my %trusteeTable = ();          my %trusteeTable = ();
1730          # Check the user ID.          # Check the user ID.
1731          if (!$userID) {          if (!$userID) {
# Line 1137  Line 1747 
1747              }              }
1748          }          }
1749          # Build a query for all of the feature's annotations, sorted by date.          # Build a query for all of the feature's annotations, sorted by date.
1750          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation'],              my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1751                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1752                                 [$featureID]);                                     [$fid]);
1753          my $timeSelected = 0;          my $timeSelected = 0;
1754          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1755          while (my $annotation = $query->Fetch()) {          while (my $annotation = $query->Fetch()) {
1756              # Get the annotation text.              # Get the annotation text.
1757              my ($text, $time) = $annotation->Values(['Annotation(annotation)','Annotation(time)']);                  my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1758                                                             'Annotation(time)', 'MadeAnnotation(from-link)']);
1759              # Check to see if this is a functional assignment for a trusted user.              # Check to see if this is a functional assignment for a trusted user.
1760              my ($user, $type, $function) = split(/\n/, $text);                  my ($actualUser, $function) = _ParseAssignment($user, $text);
1761              if ($type =~ m/^set $user function to$/i) {                  Trace("Assignment user is $actualUser, text is $function.") if T(4);
1762                    if ($actualUser) {
1763                  # Here it is a functional assignment. Check the time and the user                  # Here it is a functional assignment. Check the time and the user
1764                  # name. The time must be recent and the user must be trusted.                  # name. The time must be recent and the user must be trusted.
1765                  if ((exists $trusteeTable{$user}) && ($time > $timeSelected)) {                      if ((exists $trusteeTable{$actualUser}) && ($time > $timeSelected)) {
1766                      $retVal = $function;                      $retVal = $function;
1767                      $timeSelected = $time;                      $timeSelected = $time;
1768                  }                  }
1769              }              }
1770          }          }
1771      } 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)']);  
1772      }      }
1773          # Return the assignment found.          # Return the assignment found.
1774          return $retVal;          return $retVal;
1775  }  }
1776    
1777    =head3 FunctionsOf
1778    
1779        my @functionList = $sprout->FunctionOf($featureID, $userID);
1780    
1781    Return the functional assignments of a particular feature.
1782    
1783    The functional assignment is handled differently depending on the type of feature. If
1784    the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional
1785    assignment is a type of annotation. The format of an assignment is described in
1786    L</ParseAssignment>. Its worth noting that we cannot filter on the content of the
1787    annotation itself because it's a text field; however, this is not a big problem because
1788    most features only have a small number of annotations.
1789    
1790    =over 4
1791    
1792    =item featureID
1793    
1794    ID of the feature whose functional assignments are desired.
1795    
1796    =item RETURN
1797    
1798    Returns a list of 2-tuples, each consisting of a user ID and the text of an assignment by
1799    that user.
1800    
1801    =back
1802    
1803    =cut
1804    #: Return Type @@;
1805    sub FunctionsOf {
1806        # Get the parameters.
1807        my ($self, $featureID) = @_;
1808        # Declare the return value.
1809        my @retVal = ();
1810        # Convert to a FIG ID.
1811        my ($fid) = $self->FeaturesByAlias($featureID);
1812        # Only proceed if we found one.
1813        if ($fid) {
1814            # Here we have a FIG feature ID. We must build the list of trusted
1815            # users.
1816            my %trusteeTable = ();
1817            # Build a query for all of the feature's annotations, sorted by date.
1818            my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1819                                   "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1820                                   [$fid]);
1821            my $timeSelected = 0;
1822            # Loop until we run out of annotations.
1823            while (my $annotation = $query->Fetch()) {
1824                # Get the annotation text.
1825                my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1826                                                                'Annotation(time)',
1827                                                                'MadeAnnotation(user)']);
1828                # Check to see if this is a functional assignment for a trusted user.
1829                my ($actualUser, $function) = _ParseAssignment($user, $text);
1830                if ($actualUser) {
1831                    # Here it is a functional assignment.
1832                    push @retVal, [$actualUser, $function];
1833                }
1834            }
1835        }
1836        # Return the assignments found.
1837        return @retVal;
1838    }
1839    
1840  =head3 BBHList  =head3 BBHList
1841    
1842  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>      my $bbhHash = $sprout->BBHList($genomeID, \@featureList);
1843    
1844  Return a hash mapping the features in a specified list to their bidirectional best hits  Return a hash mapping the features in a specified list to their bidirectional best hits
1845  on a specified target genome.  on a specified target genome.
# Line 1185  Line 1856 
1856    
1857  =item RETURN  =item RETURN
1858    
1859  Returns a reference to a hash that maps the IDs of the incoming features to the IDs of  Returns a reference to a hash that maps the IDs of the incoming features to the best hits
1860  their best hits.  on the target genome.
1861    
1862  =back  =back
1863    
# Line 1194  Line 1865 
1865  #: Return Type %;  #: Return Type %;
1866  sub BBHList {  sub BBHList {
1867          # Get the parameters.          # Get the parameters.
1868          my $self = shift @_;      my ($self, $genomeID, $featureList) = @_;
         my ($genomeID, $featureList) = @_;  
1869          # Create the return structure.          # Create the return structure.
1870          my %retVal = ();          my %retVal = ();
1871          # Loop through the incoming features.          # Loop through the incoming features.
1872          for my $featureID (@{$featureList}) {          for my $featureID (@{$featureList}) {
1873                  # Create a query to get the feature's best hit.          # Ask the server for the feature's best hit.
1874                  my $query = $self->Get(['IsBidirectionalBestHitOf'],          my $bbhData = FIGRules::BBHData($featureID);
1875                                                             "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?",          # Peel off the BBHs found.
1876                                                             [$featureID, $genomeID]);          my @found = ();
1877                  # Look for the best hit.          for my $bbh (@$bbhData) {
1878                  my $bbh = $query->Fetch;              my $fid = $bbh->[0];
1879                  if ($bbh) {              my $bbGenome = $self->GenomeOf($fid);
1880                          my ($targetFeature) = $bbh->Value('IsBidirectionalBestHitOf(to-link)');              if ($bbGenome eq $genomeID) {
1881                          $retVal{$featureID} = $targetFeature;                  push @found, $fid;
1882                }
1883                  }                  }
1884            $retVal{$featureID} = \@found;
1885          }          }
1886          # Return the mapping.          # Return the mapping.
1887          return \%retVal;          return \%retVal;
1888  }  }
1889    
1890  =head3 FeatureAliases  =head3 SimList
1891    
1892  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>      my %similarities = $sprout->SimList($featureID, $count);
1893    
1894  Return a list of the aliases for a specified feature.  Return a list of the similarities to the specified feature.
1895    
1896    This method just returns the bidirectional best hits for performance reasons.
1897    
1898  =over 4  =over 4
1899    
1900  =item featureID  =item featureID
1901    
1902  ID of the feature whose aliases are desired.  ID of the feature whose similarities are desired.
1903    
1904  =item RETURN  =item count
1905    
1906  Returns a list of the feature's aliases. If the feature is not found or has no aliases, it will  Maximum number of similar features to be returned, or C<0> to return them all.
 return an empty list.  
1907    
1908  =back  =back
1909    
1910  =cut  =cut
1911  #: Return Type @;  #: Return Type %;
1912  sub FeatureAliases {  sub SimList {
1913          # Get the parameters.          # Get the parameters.
1914          my $self = shift @_;      my ($self, $featureID, $count) = @_;
1915          my ($featureID) = @_;      # Ask for the best hits.
1916          # Get the desired feature's aliases      my $lists = FIGRules::BBHData($featureID);
1917          my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(alias)']);      # Create the return value.
1918        my %retVal = ();
1919        for my $tuple (@$lists) {
1920            $retVal{$tuple->[0]} = $tuple->[1];
1921        }
1922          # Return the result.          # Return the result.
1923          return @retVal;      return %retVal;
1924  }  }
1925    
1926  =head3 GenomeOf  =head3 IsComplete
1927    
1928  C<< my $genomeID = $sprout->GenomeOf($featureID); >>      my $flag = $sprout->IsComplete($genomeID);
1929    
1930  Return the genome that contains a specified feature.  Return TRUE if the specified genome is complete, else FALSE.
1931    
1932  =over 4  =over 4
1933    
1934  =item featureID  =item genomeID
1935    
1936  ID of the feature whose genome is desired.  ID of the genome whose completeness status is desired.
1937    
1938  =item RETURN  =item RETURN
1939    
1940  Returns the ID of the genome for the specified feature. If the feature is not found, returns  Returns TRUE if the genome is complete, FALSE if it is incomplete, and C<undef> if it is
1941  an undefined value.  not found.
1942    
1943  =back  =back
1944    
1945  =cut  =cut
1946  #: Return Type $;  #: Return Type $;
1947  sub GenomeOf {  sub IsComplete {
1948          # Get the parameters.          # Get the parameters.
1949          my $self = shift @_;      my ($self, $genomeID) = @_;
1950          my ($featureID) = @_;      # Declare the return variable.
         # 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.  
1951          my $retVal;          my $retVal;
1952          # Get the genome ID.      # Get the genome's data.
1953          if (my $relationship = $query->Fetch()) {      my $genomeData = $self->_GenomeData($genomeID);
1954                  ($retVal) = $relationship->Value('HasContig(from-link)');      # Only proceed if it exists.
1955        if (defined $genomeData) {
1956            # The genome exists, so get the completeness flag.
1957            $retVal = $genomeData->PrimaryValue('Genome(complete)');
1958          }          }
1959          # Return the value found.      # Return the result.
1960          return $retVal;          return $retVal;
1961  }  }
1962    
1963  =head3 CoupledFeatures  =head3 FeatureAliases
1964    
1965  C<< my %coupleHash = $sprout->CoupledFeatures($featureID); >>      my @aliasList = $sprout->FeatureAliases($featureID);
1966    
1967  Return the features functionally coupled with a specified feature. Features are considered  Return a list of the aliases for a specified feature.
 functionally coupled if they tend to be clustered on the same chromosome.  
1968    
1969  =over 4  =over 4
1970    
1971  =item featureID  =item featureID
1972    
1973  ID of the feature whose functionally-coupled brethren are desired.  ID of the feature whose aliases are desired.
1974    
1975  =item RETURN  =item RETURN
1976    
1977  A hash mapping the functionally-coupled feature IDs to the coupling score.  Returns a list of the feature's aliases. If the feature is not found or has no aliases, it will
1978    return an empty list.
1979    
1980  =back  =back
1981    
1982  =cut  =cut
1983  #: Return Type %;  #: Return Type @;
1984    sub FeatureAliases {
1985        # Get the parameters.
1986        my ($self, $featureID) = @_;
1987        # Get the desired feature's aliases
1988        my @retVal = $self->GetFlat(['IsAliasOf'], "IsAliasOf(to-link) = ?", [$featureID], 'IsAliasOf(from-link)');
1989        # Return the result.
1990        return @retVal;
1991    }
1992    
1993    =head3 GenomeOf
1994    
1995        my $genomeID = $sprout->GenomeOf($featureID);
1996    
1997    Return the genome that contains a specified feature or contig.
1998    
1999    =over 4
2000    
2001    =item featureID
2002    
2003    ID of the feature or contig whose genome is desired.
2004    
2005    =item RETURN
2006    
2007    Returns the ID of the genome for the specified feature or contig. If the feature or contig is not
2008    found, returns an undefined value.
2009    
2010    =back
2011    
2012    =cut
2013    #: Return Type $;
2014    sub GenomeOf {
2015        # Get the parameters.
2016        my ($self, $featureID) = @_;
2017        # Declare the return value.
2018        my $retVal;
2019        # Parse the genome ID from the feature ID.
2020        if ($featureID =~ /^fig\|(\d+\.\d+)/) {
2021            $retVal = $1;
2022        } else {
2023            # Find the feature by alias.
2024            my ($realFeatureID) = $self->FeaturesByAlias($featureID);
2025            if ($realFeatureID && $realFeatureID =~ /^fig\|(\d+\.\d+)/) {
2026                $retVal = $1;
2027            }
2028        }
2029        # Return the value found.
2030        return $retVal;
2031    }
2032    
2033    =head3 CoupledFeatures
2034    
2035        my %coupleHash = $sprout->CoupledFeatures($featureID);
2036    
2037    Return the features functionally coupled with a specified feature. Features are considered
2038    functionally coupled if they tend to be clustered on the same chromosome.
2039    
2040    =over 4
2041    
2042    =item featureID
2043    
2044    ID of the feature whose functionally-coupled brethren are desired.
2045    
2046    =item RETURN
2047    
2048    A hash mapping the functionally-coupled feature IDs to the coupling score.
2049    
2050    =back
2051    
2052    =cut
2053    #: Return Type %;
2054  sub CoupledFeatures {  sub CoupledFeatures {
2055          # Get the parameters.          # Get the parameters.
2056          my $self = shift @_;      my ($self, $featureID) = @_;
2057          my ($featureID) = @_;      # Ask the coupling server for the data.
2058          # Create a query to retrieve the functionally-coupled features. Note that we depend on the      Trace("Looking for features coupled to $featureID.") if T(coupling => 3);
2059          # fact that the functional coupling is physically paired. If (A,B) is in the database, then      my @rawPairs = FIGRules::NetCouplingData('coupled_to', id1 => $featureID);
2060          # (B,A) will also be found.      Trace(scalar(@rawPairs) . " couplings returned.") if T(coupling => 3);
2061          my $query = $self->Get(['IsClusteredOnChromosomeWith'],      # Form them into a hash.
                                                    "IsClusteredOnChromosomeWith(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.  
2062          my %retVal = ();          my %retVal = ();
2063          # Retrieve the relationship records and store them in the hash.      for my $pair (@rawPairs) {
2064          while (my $clustering = $query->Fetch()) {          # Get the feature ID and score.
2065                  my ($otherFeatureID, $score) = $clustering->Values(['IsClusteredOnChromosomeWith(to-link)',          my ($featureID2, $score) = @{$pair};
2066                                                                      'IsClusteredOnChromosomeWith(score)']);          # Only proceed if the feature is in NMPDR.
2067                  $retVal{$otherFeatureID} = $score;          if ($self->_CheckFeature($featureID2)) {
2068                  $found = 1;              $retVal{$featureID2} = $score;
2069          }          }
         # 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;  
2070      }      }
2071          # Return the hash.          # Return the hash.
2072          return %retVal;          return %retVal;
2073  }  }
2074    
2075  =head3 GetEntityTypes  =head3 CouplingEvidence
2076    
2077        my @evidence = $sprout->CouplingEvidence($peg1, $peg2);
2078    
2079    Return the evidence for a functional coupling.
2080    
2081    A pair of features is considered evidence of a coupling between two other
2082    features if they occur close together on a contig and both are similar to
2083    the coupled features. So, if B<A1> and B<A2> are close together on a contig,
2084    B<B1> and B<B2> are considered evidence for the coupling if (1) B<B1> and
2085    B<B2> are close together, (2) B<B1> is similar to B<A1>, and (3) B<B2> is
2086    similar to B<A2>.
2087    
2088    The score of a coupling is determined by the number of pieces of evidence
2089    that are considered I<representative>. If several evidence items belong to
2090    a group of genomes that are close to each other, only one of those items
2091    is considered representative. The other evidence items are presumed to be
2092    there because of the relationship between the genomes rather than because
2093    the two proteins generated by the features have a related functionality.
2094    
2095    Each evidence item is returned as a three-tuple in the form C<[>I<$peg1a>C<,>
2096    I<$peg2a>C<,> I<$rep>C<]>, where I<$peg1a> is similar to I<$peg1>, I<$peg2a>
2097    is similar to I<$peg2>, and I<$rep> is TRUE if the evidence is representative
2098    and FALSE otherwise.
2099    
2100    =over 4
2101    
2102    =item peg1
2103    
2104    ID of the feature of interest.
2105    
2106    =item peg2
2107    
2108    ID of a feature functionally coupled to the feature of interest.
2109    
2110    =item RETURN
2111    
2112    Returns a list of 3-tuples. Each tuple consists of a feature similar to the feature
2113    of interest, a feature similar to the functionally coupled feature, and a flag
2114    that is TRUE for a representative piece of evidence and FALSE otherwise.
2115    
2116    =back
2117    
2118    =cut
2119    #: Return Type @@;
2120    sub CouplingEvidence {
2121        # Get the parameters.
2122        my ($self, $peg1, $peg2) = @_;
2123        # Declare the return variable.
2124        my @retVal = ();
2125        # Get the coupling and evidence data.
2126        my @rawData = FIGRules::NetCouplingData('coupling_evidence', id1 => $peg1, id2 => $peg2);
2127        # Loop through the raw data, saving the ones that are in NMPDR genomes.
2128        for my $rawTuple (@rawData) {
2129            if ($self->_CheckFeature($rawTuple->[0]) && $self->_CheckFeature($rawTuple->[1])) {
2130                push @retVal, $rawTuple;
2131            }
2132        }
2133        # Return the result.
2134        return @retVal;
2135    }
2136    
2137    =head3 GetSynonymGroup
2138    
2139        my $id = $sprout->GetSynonymGroup($fid);
2140    
2141    Return the synonym group name for the specified feature.
2142    
2143    =over 4
2144    
2145    =item fid
2146    
2147    ID of the feature whose synonym group is desired.
2148    
2149  C<< my @entityList = $sprout->GetEntityTypes(); >>  =item RETURN
2150    
2151    The name of the synonym group to which the feature belongs. If the feature does
2152    not belong to a synonym group, the feature ID itself is returned.
2153    
2154  Return the list of supported entity types.  =back
2155    
2156  =cut  =cut
2157  #: Return Type @;  
2158  sub GetEntityTypes {  sub GetSynonymGroup {
2159        # Get the parameters.
2160        my ($self, $fid) = @_;
2161        # Declare the return variable.
2162        my $retVal;
2163        # Find the synonym group.
2164        my @groups = $self->GetFlat(['IsSynonymGroupFor'], "IsSynonymGroupFor(to-link) = ?",
2165                                       [$fid], 'IsSynonymGroupFor(from-link)');
2166        # Check to see if we found anything.
2167        if (@groups) {
2168            $retVal = $groups[0];
2169        } else {
2170            $retVal = $fid;
2171        }
2172        # Return the result.
2173        return $retVal;
2174    }
2175    
2176    =head3 GetBoundaries
2177    
2178        my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList);
2179    
2180    Determine the begin and end boundaries for the locations in a list. All of the
2181    locations must belong to the same contig and have mostly the same direction in
2182    order for this method to produce a meaningful result. The resulting
2183    begin/end pair will contain all of the bases in any of the locations.
2184    
2185    =over 4
2186    
2187    =item locList
2188    
2189    List of locations to process.
2190    
2191    =item RETURN
2192    
2193    Returns a 3-tuple consisting of the contig ID, the beginning boundary,
2194    and the ending boundary. The beginning boundary will be left of the
2195    end for mostly-forward locations and right of the end for mostly-backward
2196    locations.
2197    
2198    =back
2199    
2200    =cut
2201    
2202    sub GetBoundaries {
2203          # Get the parameters.          # Get the parameters.
2204          my $self = shift @_;      my ($self, @locList) = @_;
2205          # Get the underlying database object.      # Set up the counters used to determine the most popular direction.
2206          my $erdb = $self->{_erdb};      my %counts = ( '+' => 0, '-' => 0 );
2207          # Get its entity type list.      # Get the last location and parse it.
2208          my @retVal = $erdb->GetEntityTypes();      my $locObject = BasicLocation->new(pop @locList);
2209        # Prime the loop with its data.
2210        my ($contig, $beg, $end) = ($locObject->Contig, $locObject->Left, $locObject->Right);
2211        # Count its direction.
2212        $counts{$locObject->Dir}++;
2213        # Loop through the remaining locations. Note that in most situations, this loop
2214        # will not iterate at all, because most of the time we will be dealing with a
2215        # singleton list.
2216        for my $loc (@locList) {
2217            # Create a location object.
2218            my $locObject = BasicLocation->new($loc);
2219            # Count the direction.
2220            $counts{$locObject->Dir}++;
2221            # Get the left end and the right end.
2222            my $left = $locObject->Left;
2223            my $right = $locObject->Right;
2224            # Merge them into the return variables.
2225            if ($left < $beg) {
2226                $beg = $left;
2227            }
2228            if ($right > $end) {
2229                $end = $right;
2230            }
2231        }
2232        # If the most common direction is reverse, flip the begin and end markers.
2233        if ($counts{'-'} > $counts{'+'}) {
2234            ($beg, $end) = ($end, $beg);
2235        }
2236        # Return the result.
2237        return ($contig, $beg, $end);
2238  }  }
2239    
2240  =head3 ReadFasta  =head3 ReadFasta
2241    
2242  C<< my %sequenceData = Sprout::ReadFasta($fileName, $prefix); >>      my %sequenceData = Sprout::ReadFasta($fileName, $prefix);
2243    
2244  Read sequence data from a FASTA-format file. Each sequence in a FASTA file is represented by  Read sequence data from a FASTA-format file. Each sequence in a FASTA file is represented by
2245  one or more lines of data. The first line begins with a > character and contains an ID.  one or more lines of data. The first line begins with a > character and contains an ID.
# Line 1394  Line 2281 
2281                  if ($line =~ m/^>\s*(.+?)(\s|\n)/) {                  if ($line =~ m/^>\s*(.+?)(\s|\n)/) {
2282                          # Here we have a new header. Store the current sequence if we have one.                          # Here we have a new header. Store the current sequence if we have one.
2283                          if ($id) {                          if ($id) {
2284                                  $retVal{$id} = $sequence;                  $retVal{$id} = lc $sequence;
2285                          }                          }
2286                          # Clear the sequence accumulator and save the new ID.                          # Clear the sequence accumulator and save the new ID.
2287                          ($id, $sequence) = ("$prefix$1", "");                          ($id, $sequence) = ("$prefix$1", "");
2288                  } else {                  } else {
2289                          # Here we have a data line, so we add it to the sequence accumulator.                          # Here we have a data line, so we add it to the sequence accumulator.
2290                          # First, we get the actual data out.              # First, we get the actual data out. Note that we normalize to lower
2291                # case.
2292                          $line =~ /^\s*(.*?)(\s|\n)/;                          $line =~ /^\s*(.*?)(\s|\n)/;
2293                          $sequence .= $1;                          $sequence .= $1;
2294                  }                  }
2295          }          }
2296          # Flush out the last sequence (if any).          # Flush out the last sequence (if any).
2297          if ($sequence) {          if ($sequence) {
2298                  $retVal {$id} = $sequence;          $retVal{$id} = lc $sequence;
2299          }          }
2300        # Close the file.
2301        close FASTAFILE;
2302          # Return the hash constructed from the file.          # Return the hash constructed from the file.
2303          return %retVal;          return %retVal;
2304  }  }
2305    
2306  =head3 FormatLocations  =head3 FormatLocations
2307    
2308  C<< my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat); >>      my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat);
2309    
2310  Insure that a list of feature locations is in the Sprout format. The Sprout feature location  Insure that a list of feature locations is in the Sprout format. The Sprout feature location
2311  format is I<contig>_I<beg*len> where I<*> is C<+> for a forward gene and C<-> for a backward  format is I<contig>_I<beg*len> where I<*> is C<+> for a forward gene and C<-> for a backward
2312  gene. The old format is I<contig>_I<beg>_I<end>.  gene. The old format is I<contig>_I<beg>_I<end>. If a feature is in the new format already,
2313    it will not be changed; otherwise, it will be converted. This method can also be used to
2314    perform the reverse task-- insuring that all the locations are in the old format.
2315    
2316  =over 4  =over 4
2317    
# Line 1446  Line 2338 
2338  #: Return Type @;  #: Return Type @;
2339  sub FormatLocations {  sub FormatLocations {
2340          # Get the parameters.          # Get the parameters.
2341          my $self = shift @_;      my ($self, $prefix, $locations, $oldFormat) = @_;
         my ($prefix, $locations, $oldFormat) = @_;  
2342          # Create the return list.          # Create the return list.
2343          my @retVal = ();          my @retVal = ();
2344          # Check to see if any locations were passed in.          # Check to see if any locations were passed in.
2345          if ($locations eq '') {          if ($locations eq '') {
2346              confess "No locations specified.";          Confess("No locations specified.");
2347          } else {          } else {
2348                  # Loop through the locations, converting them to the new format.                  # Loop through the locations, converting them to the new format.
2349                  for my $location (@{$locations}) {                  for my $location (@{$locations}) {
# Line 1479  Line 2370 
2370    
2371  =head3 DumpData  =head3 DumpData
2372    
2373  C<< $sprout->DumpData(); >>      $sprout->DumpData();
2374    
2375  Dump all the tables to tab-delimited DTX files. The files will be stored in the data directory.  Dump all the tables to tab-delimited DTX files. The files will be stored in the data directory.
2376    
# Line 1487  Line 2378 
2378    
2379  sub DumpData {  sub DumpData {
2380          # Get the parameters.          # Get the parameters.
2381          my $self = shift @_;      my ($self) = @_;
2382          # Get the data directory name.          # Get the data directory name.
2383          my $outputDirectory = $self->{_options}->{dataDir};          my $outputDirectory = $self->{_options}->{dataDir};
2384          # Dump the relations.          # Dump the relations.
2385          $self->{_erdb}->DumpRelations($outputDirectory);      $self->DumpRelations($outputDirectory);
2386  }  }
2387    
2388  =head3 XMLFileName  =head3 XMLFileName
2389    
2390  C<< my $fileName = $sprout->XMLFileName(); >>      my $fileName = $sprout->XMLFileName();
2391    
2392  Return the name of this database's XML definition file.  Return the name of this database's XML definition file.
2393    
2394  =cut  =cut
2395  #: Return Type $;  #: Return Type $;
2396  sub XMLFileName {  sub XMLFileName {
2397          my $self = shift @_;      my ($self) = @_;
2398          return $self->{_xmlName};          return $self->{_xmlName};
2399  }  }
2400    
2401    =head3 GetGenomeNameData
2402    
2403        my ($genus, $species, $strain) = $sprout->GenomeNameData($genomeID);
2404    
2405    Return the genus, species, and unique characterization for a genome. This
2406    is similar to L</GenusSpecies>, with the exception that it returns the
2407    values in three seperate fields.
2408    
2409    =over 4
2410    
2411    =item genomeID
2412    
2413    ID of the genome whose name data is desired.
2414    
2415    =item RETURN
2416    
2417    Returns a three-element list, consisting of the genus, species, and strain
2418    of the specified genome. If the genome is not found, an error occurs.
2419    
2420    =back
2421    
2422    =cut
2423    
2424    sub GetGenomeNameData {
2425        # Get the parameters.
2426        my ($self, $genomeID) = @_;
2427        # Declare the return variables.
2428        my ($genus, $species, $strain);
2429        # Get the genome's data.
2430        my $genomeData = $self->_GenomeData($genomeID);
2431        # Only proceed if the genome exists.
2432        if (defined $genomeData) {
2433            # Get the desired values.
2434            ($genus, $species, $strain) = $genomeData->Values(['Genome(genus)',
2435                                                               'Genome(species)',
2436                                                               'Genome(unique-characterization)']);
2437        } else {
2438            # Throw an error because they were not found.
2439            Confess("Genome $genomeID not found in database.");
2440        }
2441        # Return the results.
2442        return ($genus, $species, $strain);
2443    }
2444    
2445    =head3 GetGenomeByNameData
2446    
2447        my @genomes = $sprout->GetGenomeByNameData($genus, $species, $strain);
2448    
2449    Return a list of the IDs of the genomes with the specified genus,
2450    species, and strain. In almost every case, there will be either zero or
2451    one IDs returned; however, two or more IDs could be returned if there are
2452    multiple versions of the genome in the database.
2453    
2454    =over 4
2455    
2456    =item genus
2457    
2458    Genus of the desired genome.
2459    
2460    =item species
2461    
2462    Species of the desired genome.
2463    
2464    =item strain
2465    
2466    Strain (unique characterization) of the desired genome. This may be an empty
2467    string, in which case it is presumed that the desired genome has no strain
2468    specified.
2469    
2470    =item RETURN
2471    
2472    Returns a list of the IDs of the genomes having the specified genus, species, and
2473    strain.
2474    
2475    =back
2476    
2477    =cut
2478    
2479    sub GetGenomeByNameData {
2480        # Get the parameters.
2481        my ($self, $genus, $species, $strain) = @_;
2482        # Try to find the genomes.
2483        my @retVal = $self->GetFlat(['Genome'], "Genome(genus) = ? AND Genome(species) = ? AND Genome(unique-characterization) = ?",
2484                                    [$genus, $species, $strain], 'Genome(id)');
2485        # Return the result.
2486        return @retVal;
2487    }
2488    
2489  =head3 Insert  =head3 Insert
2490    
2491  C<< $sprout->Insert($objectType, \%fieldHash); >>      $sprout->Insert($objectType, \%fieldHash);
2492    
2493  Insert an entity or relationship instance into the database. The entity or relationship of interest  Insert an entity or relationship instance into the database. The entity or relationship of interest
2494  is defined by a type name and then a hash of field names to values. Field values in the primary  is defined by a type name and then a hash of field names to values. Field values in the primary
# Line 1518  Line 2497 
2497  list references. For example, the following line inserts an inactive PEG feature named  list references. For example, the following line inserts an inactive PEG feature named
2498  C<fig|188.1.peg.1> with aliases C<ZP_00210270.1> and C<gi|46206278>.  C<fig|188.1.peg.1> with aliases C<ZP_00210270.1> and C<gi|46206278>.
2499    
2500  C<< $sprout->Insert('Feature', { id => 'fig|188.1.peg.1', active => 0, feature-type => 'peg', alias => ['ZP_00210270.1', 'gi|46206278']}); >>      $sprout->Insert('Feature', { id => 'fig|188.1.peg.1', active => 0, feature-type => 'peg', alias => ['ZP_00210270.1', 'gi|46206278']});
2501    
2502  The next statement inserts a C<HasProperty> relationship between feature C<fig|158879.1.peg.1> and  The next statement inserts a C<HasProperty> relationship between feature C<fig|158879.1.peg.1> and
2503  property C<4> with an evidence URL of C<http://seedu.uchicago.edu/query.cgi?article_id=142>.  property C<4> with an evidence URL of C<http://seedu.uchicago.edu/query.cgi?article_id=142>.
2504    
2505  C<< $sprout->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence = 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >>      $sprout->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence => 'http://seedu.uchicago.edu/query.cgi?article_id=142'});
2506    
2507  =over 4  =over 4
2508    
# Line 1541  Line 2520 
2520  #: Return Type ;  #: Return Type ;
2521  sub Insert {  sub Insert {
2522          # Get the parameters.          # Get the parameters.
2523          my $self = shift @_;      my ($self, $objectType, $fieldHash) = @_;
         my ($objectType, $fieldHash) = @_;  
2524          # Call the underlying method.          # Call the underlying method.
2525          $self->{_erdb}->InsertObject($objectType, $fieldHash);      $self->InsertObject($objectType, $fieldHash);
2526  }  }
2527    
2528  =head3 Annotate  =head3 Annotate
2529    
2530  C<< my $ok = $sprout->Annotate($fid, $timestamp, $user, $text); >>      my $ok = $sprout->Annotate($fid, $timestamp, $user, $text);
2531    
2532  Annotate a feature. This inserts an Annotation record into the database and links it to the  Annotate a feature. This inserts an Annotation record into the database and links it to the
2533  specified feature and user.  specified feature and user.
# Line 1583  Line 2561 
2561  #: Return Type $;  #: Return Type $;
2562  sub Annotate {  sub Annotate {
2563          # Get the parameters.          # Get the parameters.
2564          my $self = shift @_;      my ($self, $fid, $timestamp, $user, $text) = @_;
         my ($fid, $timestamp, $user, $text) = @_;  
2565          # Create the annotation ID.          # Create the annotation ID.
2566          my $aid = "$fid:$timestamp";          my $aid = "$fid:$timestamp";
2567          # Insert the Annotation object.          # Insert the Annotation object.
# Line 1604  Line 2581 
2581    
2582  =head3 AssignFunction  =head3 AssignFunction
2583    
2584  C<< my $ok = $sprout->AssignFunction($featureID, $user, $function); >>      my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser);
2585    
2586  This method assigns a function to a feature. Functions are a special type of annotation. The general  This method assigns a function to a feature. Functions are a special type of annotation. The general
2587  format is "XXXX\nset XXXX function to\nYYYYY" where XXXX is the feature type and YYYY is the functional  format is described in L</ParseAssignment>.
 assignment text.  
2588    
2589  =over 4  =over 4
2590    
# Line 1618  Line 2594 
2594    
2595  =item user  =item user
2596    
2597  Name of the user making the assignment. This is frequently a group name, like C<kegg> or C<fig>.  Name of the user group making the assignment, such as C<kegg> or C<fig>.
2598    
2599  =item function  =item function
2600    
2601  Text of the function being assigned.  Text of the function being assigned.
2602    
2603    =item assigningUser (optional)
2604    
2605    Name of the individual user making the assignment. If omitted, defaults to the user group.
2606    
2607  =item RETURN  =item RETURN
2608    
2609  Returns 1 if successful, 0 if an error occurred.  Returns 1 if successful, 0 if an error occurred.
# Line 1634  Line 2614 
2614  #: Return Type $;  #: Return Type $;
2615  sub AssignFunction {  sub AssignFunction {
2616          # Get the parameters.          # Get the parameters.
2617          my $self = shift @_;      my ($self, $featureID, $user, $function, $assigningUser) = @_;
2618          my ($featureID, $user, $function) = @_;      # Default the assigning user.
2619        if (! $assigningUser) {
2620            $assigningUser = $user;
2621        }
2622          # Create an annotation string from the parameters.          # Create an annotation string from the parameters.
2623          my $annotationText = "$user\nset $user function to\n$function";      my $annotationText = "$assigningUser\nset $user function to\n$function";
2624          # Get the current time.          # Get the current time.
2625          my $now = time;          my $now = time;
2626          # Declare the return variable.          # Declare the return variable.
# Line 1658  Line 2641 
2641    
2642  =head3 FeaturesByAlias  =head3 FeaturesByAlias
2643    
2644  C<< my @features = $sprout->FeaturesByAlias($alias); >>      my @features = $sprout->FeaturesByAlias($alias);
2645    
2646  Returns a list of features with the specified alias. The alias is parsed to determine  Returns a list of features with the specified alias. The alias is parsed to determine
2647  the type of the alias. A string of digits is a GenBack ID and a string of exactly 6  the type of the alias. A string of digits is a GenBack ID and a string of exactly 6
# Line 1682  Line 2665 
2665  #: Return Type @;  #: Return Type @;
2666  sub FeaturesByAlias {  sub FeaturesByAlias {
2667          # Get the parameters.          # Get the parameters.
2668          my $self = shift @_;      my ($self, $alias) = @_;
         my ($alias) = @_;  
2669          # Declare the return variable.          # Declare the return variable.
2670          my @retVal = ();          my @retVal = ();
2671          # Parse the alias.          # Parse the alias.
# Line 1693  Line 2675 
2675                  push @retVal, $mappedAlias;                  push @retVal, $mappedAlias;
2676          } else {          } else {
2677                  # Here we have a non-FIG alias. Get the features with the normalized alias.                  # Here we have a non-FIG alias. Get the features with the normalized alias.
2678                  @retVal = $self->GetFlat(['Feature'], 'Feature(alias) = ?', [$mappedAlias], 'Feature(id)');          @retVal = $self->GetFlat(['IsAliasOf'], 'IsAliasOf(from-link) = ?', [$mappedAlias], 'IsAliasOf(to-link)');
2679          }          }
2680          # Return the result.          # Return the result.
2681          return @retVal;          return @retVal;
2682  }  }
2683    
 =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<Feature>) 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 = shift @_;  
         my ($entityName, $entityID) = @_;  
         # Check for the entity instance.  
         my $testInstance = $self->GetEntity($entityName, $entityID);  
         # Return an existence indicator.  
         my $retVal = ($testInstance ? 1 : 0);  
         return $retVal;  
 }  
   
2684  =head3 FeatureTranslation  =head3 FeatureTranslation
2685    
2686  C<< my $translation = $sprout->FeatureTranslation($featureID); >>      my $translation = $sprout->FeatureTranslation($featureID);
2687    
2688  Return the translation of a feature.  Return the translation of a feature.
2689    
# Line 1756  Line 2703 
2703  #: Return Type $;  #: Return Type $;
2704  sub FeatureTranslation {  sub FeatureTranslation {
2705          # Get the parameters.          # Get the parameters.
2706          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
2707          # Get the specified feature's translation.          # Get the specified feature's translation.
2708          my ($retVal) = $self->GetEntityValues("Feature", $featureID, ['Feature(translation)']);          my ($retVal) = $self->GetEntityValues("Feature", $featureID, ['Feature(translation)']);
2709          return $retVal;          return $retVal;
# Line 1765  Line 2711 
2711    
2712  =head3 Taxonomy  =head3 Taxonomy
2713    
2714  C<< my @taxonomyList = $sprout->Taxonomy($genome); >>      my @taxonomyList = $sprout->Taxonomy($genome);
2715    
2716  Return the taxonomy of the specified genome. This will be in the form of a list  Return the taxonomy of the specified genome. This will be in the form of a list
2717  containing the various classifications in order from domain (eg. C<Bacteria>, C<Archaea>,  containing the various classifications in order from domain (eg. C<Bacteria>, C<Archaea>,
2718  or C<Eukaryote>) to sub-species. For example,  or C<Eukaryote>) to sub-species. For example,
2719    
2720  C<< (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12) >>      (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12)
2721    
2722  =over 4  =over 4
2723    
# Line 1789  Line 2735 
2735  #: Return Type @;  #: Return Type @;
2736  sub Taxonomy {  sub Taxonomy {
2737          # Get the parameters.          # Get the parameters.
2738          my $self = shift @_;      my ($self, $genome) = @_;
         my ($genome) = @_;  
         # Find the specified genome's taxonomy string.  
         my ($list) = $self->GetEntityValues('Genome', $genome, ['Genome(taxonomy)']);  
2739          # Declare the return variable.          # Declare the return variable.
2740          my @retVal = ();          my @retVal = ();
2741          # If we found the genome, return its taxonomy string.      # Get the genome data.
2742          if ($list) {      my $genomeData = $self->_GenomeData($genome);
2743                  @retVal = split /\s*;\s*/, $list;      # Only proceed if it exists.
2744        if (defined $genomeData) {
2745            # Create the taxonomy from the taxonomy string.
2746            @retVal = split /\s*;\s*/, $genomeData->PrimaryValue('Genome(taxonomy)');
2747          } else {          } else {
2748            # Genome doesn't exist, so emit a warning.
2749                  Trace("Genome \"$genome\" does not have a taxonomy in the database.\n") if T(0);                  Trace("Genome \"$genome\" does not have a taxonomy in the database.\n") if T(0);
2750          }          }
2751          # Return the value found.          # Return the value found.
# Line 1807  Line 2754 
2754    
2755  =head3 CrudeDistance  =head3 CrudeDistance
2756    
2757  C<< my $distance = $sprout->CrudeDistance($genome1, $genome2); >>      my $distance = $sprout->CrudeDistance($genome1, $genome2);
2758    
2759  Returns a crude estimate of the distance between two genomes. The distance is construed so  Returns a crude estimate of the distance between two genomes. The distance is construed so
2760  that it will be 0 for genomes with identical taxonomies and 1 for genomes from different domains.  that it will be 0 for genomes with identical taxonomies and 1 for genomes from different domains.
# Line 1833  Line 2780 
2780  #: Return Type $;  #: Return Type $;
2781  sub CrudeDistance {  sub CrudeDistance {
2782          # Get the parameters.          # Get the parameters.
2783          my $self = shift @_;      my ($self, $genome1, $genome2) = @_;
         my ($genome1, $genome2) = @_;  
2784          # Insure that the distance is commutative by sorting the genome IDs.          # Insure that the distance is commutative by sorting the genome IDs.
2785          my ($genomeA, $genomeB);          my ($genomeA, $genomeB);
2786          if ($genome2 < $genome2) {          if ($genome2 < $genome2) {
# Line 1844  Line 2790 
2790          }          }
2791          my @taxA = $self->Taxonomy($genomeA);          my @taxA = $self->Taxonomy($genomeA);
2792          my @taxB = $self->Taxonomy($genomeB);          my @taxB = $self->Taxonomy($genomeB);
2793          # Initialize the distance to 1. We'll reduce it each time we find a match between the      # Compute the distance.
2794          # taxonomies.      my $retVal = FIGRules::CrudeDistanceFormula(\@taxA, \@taxB);
         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;  
         }  
2795      return $retVal;      return $retVal;
2796  }  }
2797    
2798  =head3 RoleName  =head3 RoleName
2799    
2800  C<< my $roleName = $sprout->RoleName($roleID); >>      my $roleName = $sprout->RoleName($roleID);
2801    
2802  Return the descriptive name of the role with the specified ID. In general, a role  Return the descriptive name of the role with the specified ID. In general, a role
2803  will only have a descriptive name if it is coded as an EC number.  will only have a descriptive name if it is coded as an EC number.
# Line 1881  Line 2818 
2818  #: Return Type $;  #: Return Type $;
2819  sub RoleName {  sub RoleName {
2820          # Get the parameters.          # Get the parameters.
2821          my $self = shift @_;      my ($self, $roleID) = @_;
         my ($roleID) = @_;  
2822          # Get the specified role's name.          # Get the specified role's name.
2823          my ($retVal) = $self->GetEntityValues('Role', $roleID, ['Role(name)']);          my ($retVal) = $self->GetEntityValues('Role', $roleID, ['Role(name)']);
2824          # Use the ID if the role has no name.          # Use the ID if the role has no name.
# Line 1895  Line 2831 
2831    
2832  =head3 RoleDiagrams  =head3 RoleDiagrams
2833    
2834  C<< my @diagrams = $sprout->RoleDiagrams($roleID); >>      my @diagrams = $sprout->RoleDiagrams($roleID);
2835    
2836  Return a list of the diagrams containing a specified functional role.  Return a list of the diagrams containing a specified functional role.
2837    
# Line 1915  Line 2851 
2851  #: Return Type @;  #: Return Type @;
2852  sub RoleDiagrams {  sub RoleDiagrams {
2853          # Get the parameters.          # Get the parameters.
2854          my $self = shift @_;      my ($self, $roleID) = @_;
         my ($roleID) = @_;  
2855          # Query for the diagrams.          # Query for the diagrams.
2856          my @retVal = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],          my @retVal = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],
2857                                                                  'RoleOccursIn(to-link)');                                                                  'RoleOccursIn(to-link)');
# Line 1926  Line 2861 
2861    
2862  =head3 FeatureProperties  =head3 FeatureProperties
2863    
2864  C<< my @properties = $sprout->FeatureProperties($featureID); >>      my @properties = $sprout->FeatureProperties($featureID);
2865    
2866  Return a list of the properties for the specified feature. Properties are key-value pairs  Return a list of the properties for the specified feature. Properties are key-value pairs
2867  that specify special characteristics of the feature. For example, a property could indicate  that specify special characteristics of the feature. For example, a property could indicate
2868  that a feature is essential to the survival of the organism or that it has benign influence  that a feature is essential to the survival of the organism or that it has benign influence
2869  on the activities of a pathogen. Each property is returned as a triple of the form  on the activities of a pathogen. Each property is returned as a triple of the form
2870  C<($key,$value,$url)>, where C<$key> is the property name, C<$value> is its value (commonly  C<($key,@values)>, where C<$key> is the property name and  C<@values> are its values.
 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.  
2871    
2872  =over 4  =over 4
2873    
# Line 1944  Line 2877 
2877    
2878  =item RETURN  =item RETURN
2879    
2880  Returns a list of triples, each triple containing the property name, its value, and a URL or  Returns a list of tuples, each tuple containing the property name and its values.
 citation.  
2881    
2882  =back  =back
2883    
# Line 1953  Line 2885 
2885  #: Return Type @@;  #: Return Type @@;
2886  sub FeatureProperties {  sub FeatureProperties {
2887          # Get the parameters.          # Get the parameters.
2888          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
2889          # Get the properties.          # Get the properties.
2890          my @retVal = $self->GetAll(['HasProperty', 'Property'], "HasProperty(from-link) = ?", [$featureID],      my @attributes = $self->ca->GetAttributes($featureID);
2891                                                          ['Property(property-name)', 'Property(property-value)',      # Strip the feature ID off each tuple.
2892                                                           'HasProperty(evidence)']);      my @retVal = ();
2893        for my $attributeRow (@attributes) {
2894            shift @{$attributeRow};
2895            push @retVal, $attributeRow;
2896        }
2897          # Return the resulting list.          # Return the resulting list.
2898          return @retVal;          return @retVal;
2899  }  }
2900    
2901  =head3 DiagramName  =head3 DiagramName
2902    
2903  C<< my $diagramName = $sprout->DiagramName($diagramID); >>      my $diagramName = $sprout->DiagramName($diagramID);
2904    
2905  Return the descriptive name of a diagram.  Return the descriptive name of a diagram.
2906    
# Line 1985  Line 2920 
2920  #: Return Type $;  #: Return Type $;
2921  sub DiagramName {  sub DiagramName {
2922          # Get the parameters.          # Get the parameters.
2923          my $self = shift @_;      my ($self, $diagramID) = @_;
         my ($diagramID) = @_;  
2924          # Get the specified diagram's name and return it.          # Get the specified diagram's name and return it.
2925          my ($retVal) = $self->GetEntityValues('Diagram', $diagramID, ['Diagram(name)']);          my ($retVal) = $self->GetEntityValues('Diagram', $diagramID, ['Diagram(name)']);
2926          return $retVal;          return $retVal;
2927  }  }
2928    
2929    =head3 PropertyID
2930    
2931        my $id = $sprout->PropertyID($propName, $propValue);
2932    
2933    Return the ID of the specified property name and value pair, if the
2934    pair exists. Only a small subset of the FIG attributes are stored as
2935    Sprout properties, mostly for use in search optimization.
2936    
2937    =over 4
2938    
2939    =item propName
2940    
2941    Name of the desired property.
2942    
2943    =item propValue
2944    
2945    Value expected for the desired property.
2946    
2947    =item RETURN
2948    
2949    Returns the ID of the name/value pair, or C<undef> if the pair does not exist.
2950    
2951    =back
2952    
2953    =cut
2954    
2955    sub PropertyID {
2956        # Get the parameters.
2957        my ($self, $propName, $propValue) = @_;
2958        # Try to find the ID.
2959        my ($retVal) = $self->GetFlat(['Property'],
2960                                      "Property(property-name) = ? AND Property(property-value) = ?",
2961                                      [$propName, $propValue], 'Property(id)');
2962        # Return the result.
2963        return $retVal;
2964    }
2965    
2966  =head3 MergedAnnotations  =head3 MergedAnnotations
2967    
2968  C<< my @annotationList = $sprout->MergedAnnotations(\@list); >>      my @annotationList = $sprout->MergedAnnotations(\@list);
2969    
2970  Returns a merged list of the annotations for the features in a list. Each annotation is  Returns a merged list of the annotations for the features in a list. Each annotation is
2971  represented by a 4-tuple of the form C<($fid, $timestamp, $userID, $annotation)>, where  represented by a 4-tuple of the form C<($fid, $timestamp, $userID, $annotation)>, where
# Line 2018  Line 2989 
2989  #: Return Type @;  #: Return Type @;
2990  sub MergedAnnotations {  sub MergedAnnotations {
2991          # Get the parameters.          # Get the parameters.
2992          my $self = shift @_;      my ($self, $list) = @_;
         my ($list) = @_;  
2993          # Create a list to hold the annotation tuples found.          # Create a list to hold the annotation tuples found.
2994          my @tuples = ();          my @tuples = ();
2995          # Loop through the features in the input list.          # Loop through the features in the input list.
# Line 2044  Line 3014 
3014    
3015  =head3 RoleNeighbors  =head3 RoleNeighbors
3016    
3017  C<< my @roleList = $sprout->RoleNeighbors($roleID); >>      my @roleList = $sprout->RoleNeighbors($roleID);
3018    
3019  Returns a list of the roles that occur in the same diagram as the specified role. Because  Returns a list of the roles that occur in the same diagram as the specified role. Because
3020  diagrams and roles are in a many-to-many relationship with each other, the list is  diagrams and roles are in a many-to-many relationship with each other, the list is
# Line 2067  Line 3037 
3037  #: Return Type @;  #: Return Type @;
3038  sub RoleNeighbors {  sub RoleNeighbors {
3039          # Get the parameters.          # Get the parameters.
3040          my $self = shift @_;      my ($self, $roleID) = @_;
         my ($roleID) = @_;  
3041          # Get all the diagrams containing this role.          # Get all the diagrams containing this role.
3042          my @diagrams = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],          my @diagrams = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],
3043                                                                    'RoleOccursIn(to-link)');                                                                    'RoleOccursIn(to-link)');
# Line 2088  Line 3057 
3057    
3058  =head3 FeatureLinks  =head3 FeatureLinks
3059    
3060  C<< my @links = $sprout->FeatureLinks($featureID); >>      my @links = $sprout->FeatureLinks($featureID);
3061    
3062  Return a list of the web hyperlinks associated with a feature. The web hyperlinks are  Return a list of the web hyperlinks associated with a feature. The web hyperlinks are
3063  to external websites describing either the feature itself or the organism containing it  to external websites describing either the feature itself or the organism containing it
# Line 2110  Line 3079 
3079  #: Return Type @;  #: Return Type @;
3080  sub FeatureLinks {  sub FeatureLinks {
3081          # Get the parameters.          # Get the parameters.
3082          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
3083          # Get the feature's links.          # Get the feature's links.
3084          my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(link)']);          my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(link)']);
3085          # Return the feature's links.          # Return the feature's links.
# Line 2120  Line 3088 
3088    
3089  =head3 SubsystemsOf  =head3 SubsystemsOf
3090    
3091  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>      my %subsystems = $sprout->SubsystemsOf($featureID);
3092    
3093  Return a hash describing all the subsystems in which a feature participates. Each subsystem is mapped  Return a hash describing all the subsystems in which a feature participates. Each subsystem is mapped
3094  to the role the feature performs.  to the roles the feature performs.
3095    
3096  =over 4  =over 4
3097    
# Line 2133  Line 3101 
3101    
3102  =item RETURN  =item RETURN
3103    
3104  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.
3105    
3106  =back  =back
3107    
3108  =cut  =cut
3109  #: Return Type %;  #: Return Type %@;
3110  sub SubsystemsOf {  sub SubsystemsOf {
3111          # Get the parameters.          # Get the parameters.
3112          my $self = shift @_;      my ($self, $featureID) = @_;
3113          my ($featureID) = @_;      # Get the subsystem list.
         # Use the SSCell to connect features to subsystems.  
3114          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],
3115                                                                          "ContainsFeature(to-link) = ?", [$featureID],                                                                          "ContainsFeature(to-link) = ?", [$featureID],
3116                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);
3117          # Create the return value.          # Create the return value.
3118          my %retVal = ();          my %retVal = ();
3119        # Build a hash to weed out duplicates. Sometimes the same PEG and role appears
3120        # in two spreadsheet cells.
3121        my %dupHash = ();
3122          # Loop through the results, adding them to the hash.          # Loop through the results, adding them to the hash.
3123          for my $record (@subsystems) {          for my $record (@subsystems) {
3124                  $retVal{$record->[0]} = $record->[1];          # Get this subsystem and role.
3125            my ($subsys, $role) = @{$record};
3126            # Insure it's the first time for both.
3127            my $dupKey = "$subsys\n$role";
3128            if (! exists $dupHash{"$subsys\n$role"}) {
3129                $dupHash{$dupKey} = 1;
3130                push @{$retVal{$subsys}}, $role;
3131            }
3132          }          }
3133          # Return the hash.          # Return the hash.
3134          return %retVal;          return %retVal;
3135  }  }
3136    
3137  =head3 RelatedFeatures  =head3 SubsystemList
3138    
3139  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>      my @subsystems = $sprout->SubsystemList($featureID);
3140    
3141  Return a list of the features which are bi-directional best hits of the specified feature and  Return a list containing the names of the subsystems in which the specified
3142  have been assigned the specified function by the specified user. If no such features exists,  feature participates. Unlike L</SubsystemsOf>, this method only returns the
3143  an empty list will be returned.  subsystem names, not the roles.
3144    
3145  =over 4  =over 4
3146    
3147  =item featureID  =item featureID
3148    
3149  ID of the feature to whom the desired features are related.  ID of the feature whose subsystem names are desired.
   
 =item function  
   
 Functional assignment (as returned by C</FunctionOf>) that is used to determine which related  
 features should be selected.  
   
 =item userID  
   
 ID of the user whose functional assignments are to be used. If omitted, C<FIG> is assumed.  
3150    
3151  =item RETURN  =item RETURN
3152    
3153  Returns a list of the related features with the specified function.  Returns a list of the names of the subsystems in which the feature participates.
3154    
3155  =back  =back
3156    
3157  =cut  =cut
3158  #: Return Type @;  #: Return Type @;
3159  sub RelatedFeatures {  sub SubsystemList {
3160          # Get the parameters.          # Get the parameters.
3161          my $self = shift @_;      my ($self, $featureID) = @_;
3162          my ($featureID, $function, $userID) = @_;      # Get the list of names. We do a join to the Subsystem table because we have missing subsystems in
3163          # Get a list of the features that are BBHs of the incoming feature.      # the Sprout database!
3164          my @bbhFeatures = $self->GetFlat(['IsBidirectionalBestHitOf'],      my @retVal = $self->GetFlat(['HasRoleInSubsystem', 'Subsystem'], "HasRoleInSubsystem(from-link) = ?",
3165                                                                           "IsBidirectionalBestHitOf(from-link) = ?", [$featureID],                                  [$featureID], 'HasRoleInSubsystem(to-link)');
3166                                                                           'IsBidirectionalBestHitOf(to-link)');      # Return the result, sorted.
3167          # Now we loop through the features, pulling out the ones that have the correct      return sort @retVal;
         # functional assignment.  
         my @retVal = ();  
         for my $bbhFeature (@bbhFeatures) {  
                 # Get this feature's functional assignment.  
                 my $newFunction = $self->FunctionOf($bbhFeature, $userID);  
                 # If it matches, add it to the result list.  
                 if ($newFunction eq $function) {  
                         push @retVal, $bbhFeature;  
                 }  
         }  
         # Return the result list.  
         return @retVal;  
3168  }  }
3169    
3170  =head3 TaxonomySort  =head3 GenomeSubsystemData
3171    
3172  C<< my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs); >>      my %featureData = $sprout->GenomeSubsystemData($genomeID);
3173    
3174  Return a list formed by sorting the specified features by the taxonomy of the containing  Return a hash mapping genome features to their subsystem roles.
 genome. This will cause genomes from similar organisms to float close to each other.  
   
 This task could almost be handled by the database; however, the taxonomy string in the  
 database is a text field and can't be indexed. Instead, we create a hash table that maps  
 taxonomy strings to lists of features. We then process the hash table using a key sort  
 and merge the feature lists together to create the output.  
3175    
3176  =over 4  =over 4
3177    
3178  =item $featureIDs  =item genomeID
3179    
3180  List of features to be taxonomically sorted.  ID of the genome whose subsystem feature map is desired.
3181    
3182  =item RETURN  =item RETURN
3183    
3184  Returns the list of features sorted by the taxonomies of the containing genomes.  Returns a hash mapping each feature of the genome to a list of 2-tuples. Eacb
3185    2-tuple contains a subsystem name followed by a role ID.
3186    
3187  =back  =back
3188    
3189  =cut  =cut
3190  #: Return Type @;  
3191  sub TaxonomySort {  sub GenomeSubsystemData {
3192          # Get the parameters.          # Get the parameters.
3193          my $self = shift @_;      my ($self, $genomeID) = @_;
3194          my ($featureIDs) = @_;      # Declare the return variable.
3195          # Create the working hash table.      my %retVal = ();
3196          my %hashBuffer = ();      # Get a list of the genome features that participate in subsystems. For each
3197          # Loop through the features.      # feature we get its subsystem ID and the corresponding roles.
3198          for my $fid (@{$featureIDs}) {      my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf', 'HasSSCell'],
3199                  # Get the taxonomy of the feature's genome.                                   "HasFeature(from-link) = ?", [$genomeID],
3200                  my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",                                   ['HasFeature(to-link)', 'IsRoleOf(from-link)',  'HasSSCell(from-link)']);
3201                                                                                  [$fid], 'Genome(taxonomy)');      # Now we get a list of valid subsystems. These are the subsystems connected to the genome with
3202                  # Add this feature to the hash buffer.      # a non-negative variant code.
3203                  if (exists $hashBuffer{$taxonomy}) {      my %subs = map { $_ => 1 } $self->GetFlat(['ParticipatesIn'],
3204                          push @{$hashBuffer{$taxonomy}}, $fid;                                                  "ParticipatesIn(from-link) = ? AND ParticipatesIn(variant-code) >= 0",
3205                  } else {                                                  [$genomeID], 'ParticipatesIn(to-link)');
3206                          $hashBuffer{$taxonomy} = [$fid];      # We loop through @roleData to build the hash.
3207                  }      for my $roleEntry (@roleData) {
3208            # Get the data for this feature and cell.
3209            my ($fid, $role, $subsys) = @{$roleEntry};
3210            Trace("Subsystem for $fid is $subsys.") if T(4);
3211            # Check the subsystem;
3212            if ($subs{$subsys}) {
3213                Trace("Subsystem found.") if T(4);
3214                # Insure this feature has an entry in the return hash.
3215                if (! exists $retVal{$fid}) { $retVal{$fid} = []; }
3216                # Merge in this new data.
3217                push @{$retVal{$fid}}, [$subsys, $role];
3218          }          }
         # Sort the keys and get the elements.  
         my @retVal = ();  
         for my $taxon (sort keys %hashBuffer) {  
                 push @retVal, @{$hashBuffer{$taxon}};  
3219          }          }
3220          # Return the result.          # Return the result.
3221          return @retVal;      return %retVal;
3222  }  }
3223    
3224  =head3 GetAll  =head3 RelatedFeatures
   
 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</Get> 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<ERDB> system--  
 B<I<objectName>(I<fieldName>)>-- where I<objectName> is the name of the relevant entity  
 or relationship and I<fieldName> is the name of the field.  
3225    
3226  The list returned will be a list of lists. Each element of the list will contain      my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID);
 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.  
3227    
3228  C<< $query = $sprout->Get(['ContainsFeature', 'Feature'], "ContainsFeature(from-link) = ?", [$ssCellID], ['Feature(id)', 'Feature(alias)']); >>  Return a list of the features which are bi-directional best hits of the specified feature and
3229    have been assigned the specified function by the specified user. If no such features exists,
3230    an empty list will be returned.
3231    
3232  =over 4  =over 4
3233    
3234  =item objectNames  =item featureID
   
 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<objectName>(I<fieldName>)>. 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  
3235    
3236  List of the parameters to be substituted in for the parameters marks in the filter clause.  ID of the feature to whom the desired features are related.
3237    
3238  =item fields  =item function
3239    
3240  List of the fields to be returned in each element of the list returned.  Functional assignment (as returned by C</FunctionOf>) that is used to determine which related
3241    features should be selected.
3242    
3243  =item count  =item userID
3244    
3245  Maximum number of records to return. If omitted or 0, all available records will be returned.  ID of the user whose functional assignments are to be used. If omitted, C<FIG> is assumed.
3246    
3247  =item RETURN  =item RETURN
3248    
3249  Returns a list of list references. Each element of the return list contains the values for the  Returns a list of the related features with the specified function.
 fields specified in the B<fields> parameter.  
3250    
3251  =back  =back
3252    
3253  =cut  =cut
3254  #: Return Type @@;  #: Return Type @;
3255  sub GetAll {  sub RelatedFeatures {
3256          # Get the parameters.          # Get the parameters.
3257          my $self = shift @_;      my ($self, $featureID, $function, $userID) = @_;
3258          my ($objectNames, $filterClause, $parameterList, $fields, $count) = @_;      # Get a list of the features that are BBHs of the incoming feature.
3259          # Create the query.      my $bbhData = FIGRules::BBHData($featureID);
3260          my $query = $self->Get($objectNames, $filterClause, $parameterList);      my @bbhFeatures = map { $_->[0] } @$bbhData;
3261          # Set up a counter of the number of records read.      # Now we loop through the features, pulling out the ones that have the correct
3262          my $fetched = 0;      # functional assignment.
         # Insure the counter has a value.  
         if (!defined $count) {  
                 $count = 0;  
         }  
         # Loop through the records returned, extracting the fields. Note that if the  
         # counter is non-zero, we stop when the number of records read hits the count.  
3263          my @retVal = ();          my @retVal = ();
3264          while (($count == 0 || $fetched < $count) && (my $row = $query->Fetch())) {      for my $bbhFeature (@bbhFeatures) {
3265                  my @rowData = $row->Values($fields);          # Get this feature's functional assignment.
3266                  push @retVal, \@rowData;          my $newFunction = $self->FunctionOf($bbhFeature, $userID);
3267                  $fetched++;          # If it matches, add it to the result list.
3268            if ($newFunction eq $function) {
3269                push @retVal, $bbhFeature;
3270          }          }
3271          # Return the resulting list.      }
3272        # Return the result list.
3273          return @retVal;          return @retVal;
3274  }  }
3275    
3276  =head3 GetFlat  =head3 TaxonomySort
   
 C<< my @list = $sprout->GetFlat(\@objectNames, $filterClause, $parameterList, $field); >>  
   
 This is a variation of L</GetAll> 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.  
3277    
3278  =item filterClause      my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs);
3279    
3280  WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can  Return a list formed by sorting the specified features by the taxonomy of the containing
3281  be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form  genome. This will cause genomes from similar organisms to float close to each other.
 B<I<objectName>(I<fieldName>)>. 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.  
3282    
3283  =item parameterList  This task could almost be handled by the database; however, the taxonomy string in the
3284    database is a text field and can't be indexed. Instead, we create a hash table that maps
3285    taxonomy strings to lists of features. We then process the hash table using a key sort
3286    and merge the feature lists together to create the output.
3287    
3288  List of the parameters to be substituted in for the parameters marks in the filter clause.  =over 4
3289    
3290  =item field  =item $featureIDs
3291    
3292  Name of the field to be used to get the elements of the list returned.  List of features to be taxonomically sorted.
3293    
3294  =item RETURN  =item RETURN
3295    
3296  Returns a list of values.  Returns the list of features sorted by the taxonomies of the containing genomes.
3297    
3298  =back  =back
3299    
3300  =cut  =cut
3301  #: Return Type @;  #: Return Type @;
3302  sub GetFlat {  sub TaxonomySort {
3303          # Get the parameters.          # Get the parameters.
3304          my $self = shift @_;      my ($self, $featureIDs) = @_;
3305          my ($objectNames, $filterClause, $parameterList, $field) = @_;      # Create the working hash table.
3306          # Construct the query.      my %hashBuffer = ();
3307          my $query = $self->Get($objectNames, $filterClause, $parameterList);      # Loop through the features.
3308          # Create the result list.      for my $fid (@{$featureIDs}) {
3309            # Get the taxonomy of the feature's genome.
3310            my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",
3311                                            [$fid], 'Genome(taxonomy)');
3312            # Add this feature to the hash buffer.
3313            push @{$hashBuffer{$taxonomy}}, $fid;
3314        }
3315        # Sort the keys and get the elements.
3316          my @retVal = ();          my @retVal = ();
3317          # Loop through the records, adding the field values found to the result list.      for my $taxon (sort keys %hashBuffer) {
3318          while (my $row = $query->Fetch()) {          push @retVal, @{$hashBuffer{$taxon}};
                 push @retVal, $row->Value($field);  
3319          }          }
3320          # Return the list created.      # Return the result.
3321          return @retVal;          return @retVal;
3322  }  }
3323    
3324  =head3 Protein  =head3 Protein
3325    
3326  C<< my $protein = Sprout::Protein($sequence, $table); >>      my $protein = Sprout::Protein($sequence, $table);
3327    
3328  Translate a DNA sequence into a protein sequence.  Translate a DNA sequence into a protein sequence.
3329    
# Line 2470  Line 3393 
3393          # Loop through the input triples.          # Loop through the input triples.
3394          my $n = length $sequence;          my $n = length $sequence;
3395          for (my $i = 0; $i < $n; $i += 3) {          for (my $i = 0; $i < $n; $i += 3) {
3396                  # Get the current triple from the sequence.          # Get the current triple from the sequence. Note we convert to
3397                  my $triple = substr($sequence, $i, 3);          # upper case to insure a match.
3398            my $triple = uc substr($sequence, $i, 3);
3399                  # Translate it using the table.                  # Translate it using the table.
3400                  my $protein = "X";                  my $protein = "X";
3401                  if (exists $table->{$triple}) { $protein = $table->{$triple}; }                  if (exists $table->{$triple}) { $protein = $table->{$triple}; }
# Line 2485  Line 3409 
3409    
3410  =head3 LoadInfo  =head3 LoadInfo
3411    
3412  C<< my ($dirName, @relNames) = $sprout->LoadInfo(); >>      my ($dirName, @relNames) = $sprout->LoadInfo();
3413    
3414  Return the name of the directory from which data is to be loaded and a list of the relation  Return the name of the directory from which data is to be loaded and a list of the relation
3415  names. This information is useful when trying to analyze what needs to be put where in order  names. This information is useful when trying to analyze what needs to be put where in order
# Line 2495  Line 3419 
3419  #: Return Type @;  #: Return Type @;
3420  sub LoadInfo {  sub LoadInfo {
3421          # Get the parameters.          # Get the parameters.
3422          my $self = shift @_;      my ($self) = @_;
3423          # Create the return list, priming it with the name of the data directory.          # Create the return list, priming it with the name of the data directory.
3424          my @retVal = ($self->{_options}->{dataDir});          my @retVal = ($self->{_options}->{dataDir});
3425          # Concatenate the table names.          # Concatenate the table names.
3426          push @retVal, $self->{_erdb}->GetTableNames();      push @retVal, $self->GetTableNames();
3427          # Return the result.          # Return the result.
3428          return @retVal;          return @retVal;
3429  }  }
3430    
3431    =head3 BBHMatrix
3432    
3433        my $bbhMap = $sprout->BBHMatrix($genomeID, $cutoff, @targets);
3434    
3435    Find all the bidirectional best hits for the features of a genome in a
3436    specified list of target genomes. The return value will be a hash mapping
3437    features in the original genome to their bidirectional best hits in the
3438    target genomes.
3439    
3440    =over 4
3441    
3442    =item genomeID
3443    
3444    ID of the genome whose features are to be examined for bidirectional best hits.
3445    
3446    =item cutoff
3447    
3448    A cutoff value. Only hits with a score lower than the cutoff will be returned.
3449    
3450    =item targets
3451    
3452    List of target genomes. Only pairs originating in the original
3453    genome and landing in one of the target genomes will be returned.
3454    
3455    =item RETURN
3456    
3457    Returns a reference to a hash mapping each feature in the original genome
3458    to a sub-hash mapping its BBH pegs in the target genomes to their scores.
3459    
3460    =back
3461    
3462    =cut
3463    
3464    sub BBHMatrix {
3465        # Get the parameters.
3466        my ($self, $genomeID, $cutoff, @targets) = @_;
3467        # Declare the return variable.
3468        my %retVal = ();
3469        # Ask for the BBHs.
3470        my @bbhList = FIGRules::BatchBBHs("fig|$genomeID.%", $cutoff, @targets);
3471        Trace("Retrieved " . scalar(@bbhList) . " BBH results.") if T(3);
3472        # We now have a set of 4-tuples that we need to convert into a hash of hashes.
3473        for my $bbhData (@bbhList) {
3474            my ($peg1, $peg2, $score) = @{$bbhData};
3475            if (! exists $retVal{$peg1}) {
3476                $retVal{$peg1} = { $peg2 => $score };
3477            } else {
3478                $retVal{$peg1}->{$peg2} = $score;
3479            }
3480        }
3481        # Return the result.
3482        return \%retVal;
3483    }
3484    
3485    
3486    =head3 SimMatrix
3487    
3488        my %simMap = $sprout->SimMatrix($genomeID, $cutoff, @targets);
3489    
3490    Find all the similarities for the features of a genome in a
3491    specified list of target genomes. The return value will be a hash mapping
3492    features in the original genome to their similarites in the
3493    target genomes.
3494    
3495    =over 4
3496    
3497    =item genomeID
3498    
3499    ID of the genome whose features are to be examined for similarities.
3500    
3501    =item cutoff
3502    
3503    A cutoff value. Only hits with a score lower than the cutoff will be returned.
3504    
3505    =item targets
3506    
3507    List of target genomes. Only pairs originating in the original
3508    genome and landing in one of the target genomes will be returned.
3509    
3510    =item RETURN
3511    
3512    Returns a hash mapping each feature in the original genome to a hash mapping its
3513    similar pegs in the target genomes to their scores.
3514    
3515    =back
3516    
3517    =cut
3518    
3519    sub SimMatrix {
3520        # Get the parameters.
3521        my ($self, $genomeID, $cutoff, @targets) = @_;
3522        # Declare the return variable.
3523        my %retVal = ();
3524        # Get the list of features in the source organism.
3525        my @fids = $self->FeaturesOf($genomeID);
3526        # Ask for the sims. We only want similarities to fig features.
3527        my $simList = FIGRules::GetNetworkSims($self, \@fids, {}, 1000, $cutoff, "fig");
3528        if (! defined $simList) {
3529            Confess("Unable to retrieve similarities from server.");
3530        } else {
3531            Trace("Processing sims.") if T(3);
3532            # We now have a set of sims that we need to convert into a hash of hashes. First, we
3533            # Create a hash for the target genomes.
3534            my %targetHash = map { $_ => 1 } @targets;
3535            for my $simData (@{$simList}) {
3536                # Get the PEGs and the score.
3537                my ($peg1, $peg2, $score) = ($simData->id1, $simData->id2, $simData->psc);
3538                # Insure the second ID is in the target list.
3539                my ($genome2) = FIGRules::ParseFeatureID($peg2);
3540                if (exists $targetHash{$genome2}) {
3541                    # Here it is. Now we need to add it to the return hash. How we do that depends
3542                    # on whether or not $peg1 is new to us.
3543                    if (! exists $retVal{$peg1}) {
3544                        $retVal{$peg1} = { $peg2 => $score };
3545                    } else {
3546                        $retVal{$peg1}->{$peg2} = $score;
3547                    }
3548                }
3549            }
3550        }
3551        # Return the result.
3552        return %retVal;
3553    }
3554    
3555    
3556  =head3 LowBBHs  =head3 LowBBHs
3557    
3558  C<< my %bbhMap = $sprout->GoodBBHs($featureID, $cutoff); >>      my %bbhMap = $sprout->LowBBHs($featureID, $cutoff);
3559    
3560  Return the bidirectional best hits of a feature whose score is no greater than a  Return the bidirectional best hits of a feature whose score is no greater than a
3561  specified cutoff value. A higher cutoff value will allow inclusion of hits with  specified cutoff value. A higher cutoff value will allow inclusion of hits with
# Line 2532  Line 3581 
3581  #: Return Type %;  #: Return Type %;
3582  sub LowBBHs {  sub LowBBHs {
3583          # Get the parsameters.          # Get the parsameters.
3584          my $self = shift @_;      my ($self, $featureID, $cutoff) = @_;
         my ($featureID, $cutoff) = @_;  
3585          # Create the return hash.          # Create the return hash.
3586          my %retVal = ();          my %retVal = ();
3587          # Create a query to get the desired BBHs.      # Query for the desired BBHs.
3588          my @bbhList = $self->GetAll(['IsBidirectionalBestHitOf'],      my $bbhList = FIGRules::BBHData($featureID, $cutoff);
                                                                 'IsBidirectionalBestHitOf(sc) <= ? AND IsBidirectionalBestHitOf(from-link) = ?',  
                                                                 [$cutoff, $featureID],  
                                                                 ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(sc)']);  
3589          # Form the results into the return hash.          # Form the results into the return hash.
3590          for my $pair (@bbhList) {      for my $pair (@$bbhList) {
3591                  $retVal{$pair->[0]} = $pair->[1];          my $fid = $pair->[0];
3592            if ($self->Exists('Feature', $fid)) {
3593                $retVal{$fid} = $pair->[1];
3594            }
3595          }          }
3596          # Return the result.          # Return the result.
3597          return %retVal;          return %retVal;
3598  }  }
3599    
3600  =head3 GetGroups  =head3 Sims
3601    
3602        my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters);
3603    
3604    Get a list of similarities for a specified feature. Similarity information is not kept in the
3605    Sprout database; rather, they are retrieved from a network server. The similarities are
3606    returned as B<Sim> objects. A Sim object is actually a list reference that has been blessed
3607    so that its elements can be accessed by name.
3608    
3609    Similarities can be either raw or expanded. The raw similarities are basic
3610    hits between features with similar DNA. Expanding a raw similarity drags in any
3611    features considered substantially identical. So, for example, if features B<A1>,
3612    B<A2>, and B<A3> are all substantially identical to B<A>, then a raw similarity
3613    B<[C,A]> would be expanded to B<[C,A] [C,A1] [C,A2] [C,A3]>.
3614    
3615    =over 4
3616    
3617    =item fid
3618    
3619    ID of the feature whose similarities are desired, or reference to a list of IDs
3620    of features whose similarities are desired.
3621    
3622    =item maxN
3623    
3624    Maximum number of similarities to return.
3625    
3626    =item maxP
3627    
3628    Minumum allowable similarity score.
3629    
3630    =item select
3631    
3632    Selection criterion: C<raw> means only raw similarities are returned; C<fig>
3633    means only similarities to FIG features are returned; C<all> means all expanded
3634    similarities are returned; and C<figx> means similarities are expanded until the
3635    number of FIG features equals the maximum.
3636    
3637    =item max_expand
3638    
3639    The maximum number of features to expand.
3640    
3641    =item filters
3642    
3643    Reference to a hash containing filter information, or a subroutine that can be
3644    used to filter the sims.
3645    
3646    =item RETURN
3647    
3648    Returns a reference to a list of similarity objects, or C<undef> if an error
3649    occurred.
3650    
3651    =back
3652    
3653  =cut  =cut
 #: Return Type %@;  
 sub GetGroups {  
 }  
3654    
3655  =head2 Internal Utility Methods  sub Sims {
3656        # Get the parameters.
3657        my ($self, $fid, $maxN, $maxP, $select, $max_expand, $filters) = @_;
3658        # Create the shim object to test for deleted FIDs.
3659        my $shim = FidCheck->new($self);
3660        # Ask the network for sims.
3661        my $retVal = FIGRules::GetNetworkSims($shim, $fid, {}, $maxN, $maxP, $select, $max_expand, $filters);
3662        # Return the result.
3663        return $retVal;
3664    }
3665    
3666  =head3 ParseAssignment  =head3 IsAllGenomes
3667    
3668  Parse annotation text to determine whether or not it is a functional assignment. If it is,      my $flag = $sprout->IsAllGenomes(\@list, \@checkList);
 the user and function text will be returned as a 2-element list. If it isn't, an empty list  
 will be returned.  
3669    
3670  This is a static method.  Return TRUE if all genomes in the second list are represented in the first list at
3671    least one. Otherwise, return FALSE. If the second list is omitted, the first list is
3672    compared to a list of all the genomes.
3673    
3674  =over 4  =over 4
3675    
3676  =item text  =item list
3677    
3678  Text of the annotation.  Reference to the list to be compared to the second list.
3679    
3680    =item checkList (optional)
3681    
3682    Reference to the comparison target list. Every genome ID in this list must occur at
3683    least once in the first list. If this parameter is omitted, a list of all the genomes
3684    is used.
3685    
3686  =item RETURN  =item RETURN
3687    
3688  Returns an empty list if the annotation is not a functional assignment; otherwise, returns  Returns TRUE if every item in the second list appears at least once in the
3689  a two-element list containing the user name and the function text.  first list, else FALSE.
3690    
3691  =back  =back
3692    
3693  =cut  =cut
3694    
3695  sub ParseAssignment {  sub IsAllGenomes {
3696        # Get the parameters.
3697        my ($self, $list, $checkList) = @_;
3698        # Supply the checklist if it was omitted.
3699        $checkList = [$self->Genomes()] if ! defined($checkList);
3700        # Create a hash of the original list.
3701        my %testList = map { $_ => 1 } @{$list};
3702        # Declare the return variable. We assume that the representation
3703        # is complete and stop at the first failure.
3704        my $retVal = 1;
3705        my $n = scalar @{$checkList};
3706        for (my $i = 0; $retVal && $i < $n; $i++) {
3707            if (! $testList{$checkList->[$i]}) {
3708                $retVal = 0;
3709            }
3710        }
3711        # Return the result.
3712        return $retVal;
3713    }
3714    
3715    =head3 GetGroups
3716    
3717        my %groups = $sprout->GetGroups(\@groupList);
3718    
3719    Return a hash mapping each group to the IDs of the genomes in the group.
3720    A list of groups may be specified, in which case only those groups will be
3721    shown. Alternatively, if no parameter is supplied, all groups will be
3722    included. Genomes that are not in any group are omitted.
3723    
3724    =cut
3725    #: Return Type %@;
3726    sub GetGroups {
3727          # Get the parameters.          # Get the parameters.
3728          my ($text) = @_;      my ($self, $groupList) = @_;
3729          # Declare the return value.          # Declare the return value.
3730          my @retVal = ();      my %retVal = ();
3731          # Check to see if this is a functional assignment.      # Determine whether we are getting all the groups or just some.
3732          my ($user, $type, $function) = split(/\n/, $text);      if (defined $groupList) {
3733          if ($type =~ m/^set $user function to$/i) {          # Here we have a group list. Loop through them individually,
3734                  # Here it is, so we return the user name and function text.          # getting a list of the relevant genomes.
3735                  @retVal = ($user, $function);          for my $group (@{$groupList}) {
3736                my @genomeIDs = $self->GetFlat(['Genome'], "Genome(primary-group) = ?",
3737                    [$group], "Genome(id)");
3738                $retVal{$group} = \@genomeIDs;
3739          }          }
3740          # Return the result list.      } else {
3741            # Here we need all of the groups. In this case, we run through all
3742            # of the genome records, putting each one found into the appropriate
3743            # group. Note that we use a filter clause to insure that only genomes
3744            # in real NMPDR groups are included in the return set.
3745            my @genomes = $self->GetAll(['Genome'], "Genome(primary-group) <> ?",
3746                                        [$FIG_Config::otherGroup], ['Genome(id)', 'Genome(primary-group)']);
3747            # Loop through the genomes found.
3748            for my $genome (@genomes) {
3749                # Get the genome ID and group, and add this genome to the group's list.
3750                my ($genomeID, $group) = @{$genome};
3751                push @{$retVal{$group}}, $genomeID;
3752            }
3753        }
3754        # Return the hash we just built.
3755        return %retVal;
3756    }
3757    
3758    =head3 MyGenomes
3759    
3760        my @genomes = Sprout::MyGenomes($dataDir);
3761    
3762    Return a list of the genomes to be included in the Sprout.
3763    
3764    This method is provided for use during the Sprout load. It presumes the Genome load file has
3765    already been created. (It will be in the Sprout data directory and called either C<Genome>
3766    or C<Genome.dtx>.) Essentially, it reads in the Genome load file and strips out the genome
3767    IDs.
3768    
3769    =over 4
3770    
3771    =item dataDir
3772    
3773    Directory containing the Sprout load files.
3774    
3775    =back
3776    
3777    =cut
3778    #: Return Type @;
3779    sub MyGenomes {
3780        # Get the parameters.
3781        my ($dataDir) = @_;
3782        # Compute the genome file name.
3783        my $genomeFileName = LoadFileName($dataDir, "Genome");
3784        # Extract the genome IDs from the files.
3785        my @retVal = map { $_ =~ /^(\S+)/; $1 } Tracer::GetFile($genomeFileName);
3786        # Return the result.
3787          return @retVal;          return @retVal;
3788  }  }
3789    
3790  =head3 FriendlyTimestamp  =head3 LoadFileName
3791    
3792  Convert a time number to a user-friendly time stamp for display.      my $fileName = Sprout::LoadFileName($dataDir, $tableName);
3793    
3794  This is a static method.  Return the name of the load file for the specified table in the specified data
3795    directory.
3796    
3797  =over 4  =over 4
3798    
3799  =item timeValue  =item dataDir
3800    
3801  Numeric time value.  Directory containing the Sprout load files.
3802    
3803    =item tableName
3804    
3805    Name of the table whose load file is desired.
3806    
3807  =item RETURN  =item RETURN
3808    
3809  Returns a string containing the same time in user-readable format.  Returns the name of the file containing the load data for the specified table, or
3810    C<undef> if no load file is present.
3811    
3812  =back  =back
3813    
3814  =cut  =cut
3815    #: Return Type $;
3816  sub FriendlyTimestamp {  sub LoadFileName {
3817      my ($timeValue) = @_;      # Get the parameters.
3818      my $retVal = strftime("%a %b %e %H:%M:%S %Y", localtime($timeValue));      my ($dataDir, $tableName) = @_;
3819      return $retVal;      # Declare the return variable.
3820        my $retVal;
3821        # Check for the various file names.
3822        if (-e "$dataDir/$tableName") {
3823            $retVal = "$dataDir/$tableName";
3824        } elsif (-e "$dataDir/$tableName.dtx") {
3825            $retVal = "$dataDir/$tableName.dtx";
3826        }
3827        # Return the result.
3828        return $retVal;
3829    }
3830    
3831    =head3 DeleteGenome
3832    
3833        my $stats = $sprout->DeleteGenome($genomeID, $testFlag);
3834    
3835    Delete a genome from the database.
3836    
3837    =over 4
3838    
3839    =item genomeID
3840    
3841    ID of the genome to delete
3842    
3843    =item testFlag
3844    
3845    If TRUE, then the DELETE statements will be traced, but no deletions will occur.
3846    
3847    =item RETURN
3848    
3849    Returns a statistics object describing the rows deleted.
3850    
3851    =back
3852    
3853    =cut
3854    #: Return Type $%;
3855    sub DeleteGenome {
3856        # Get the parameters.
3857        my ($self, $genomeID, $testFlag) = @_;
3858        # Perform the delete for the genome's features.
3859        my $retVal = $self->Delete('Feature', "fig|$genomeID.%", testMode => $testFlag);
3860        # Perform the delete for the primary genome data.
3861        my $stats = $self->Delete('Genome', $genomeID, testMode => $testFlag);
3862        $retVal->Accumulate($stats);
3863        # Return the result.
3864        return $retVal;
3865    }
3866    
3867    =head3 Fix
3868    
3869        my %fixedHash = $sprout->Fix(%groupHash);
3870    
3871    Prepare a genome group hash (like that returned by L</GetGroups>) for processing.
3872    The groups will be combined into the appropriate super-groups.
3873    
3874    =over 4
3875    
3876    =item groupHash
3877    
3878    Hash to be fixed up.
3879    
3880    =item RETURN
3881    
3882    Returns a fixed-up version of the hash.
3883    
3884    =back
3885    
3886    =cut
3887    
3888    sub Fix {
3889        # Get the parameters.
3890        my ($self, %groupHash) = @_;
3891        # Create the result hash.
3892        my %retVal = ();
3893        # Copy over the genomes.
3894        for my $groupID (keys %groupHash) {
3895            # Get the super-group name.
3896            my $realGroupID = $self->SuperGroup($groupID);
3897            # Append this group's genomes into the result hash
3898            # using the super-group name.
3899            push @{$retVal{$realGroupID}}, @{$groupHash{$groupID}};
3900        }
3901        # Return the result hash.
3902        return %retVal;
3903    }
3904    
3905    =head3 GroupPageName
3906    
3907        my $name = $sprout->GroupPageName($group);
3908    
3909    Return the name of the page for the specified NMPDR group.
3910    
3911    =over 4
3912    
3913    =item group
3914    
3915    Name of the relevant group.
3916    
3917    =item RETURN
3918    
3919    Returns the relative page name (e.g. C<../content/campy.php>). If the group file is not in
3920    memory it will be read in.
3921    
3922    =back
3923    
3924    =cut
3925    
3926    sub GroupPageName {
3927        # Get the parameters.
3928        my ($self, $group) = @_;
3929        # Check for the group file data.
3930        my %superTable = $self->CheckGroupFile();
3931        # Compute the real group name.
3932        my $realGroup = $self->SuperGroup($group);
3933        # Get the associated page name.
3934        my $retVal = "../content/$superTable{$realGroup}->{page}";
3935        # Return the result.
3936        return $retVal;
3937    }
3938    
3939    
3940    =head3 AddProperty
3941    
3942        $sprout->AddProperty($featureID, $key, @values);
3943    
3944    Add a new attribute value (Property) to a feature.
3945    
3946    =over 4
3947    
3948    =item peg
3949    
3950    ID of the feature to which the attribute is to be added.
3951    
3952    =item key
3953    
3954    Name of the attribute (key).
3955    
3956    =item values
3957    
3958    Values of the attribute.
3959    
3960    =back
3961    
3962    =cut
3963    #: Return Type ;
3964    sub AddProperty {
3965        # Get the parameters.
3966        my ($self, $featureID, $key, @values) = @_;
3967        # Add the property using the attached attributes object.
3968        $self->ca->AddAttribute($featureID, $key, @values);
3969    }
3970    
3971    =head3 CheckGroupFile
3972    
3973        my %groupData = $sprout->CheckGroupFile();
3974    
3975    Get the group file hash. The group file hash describes the relationship
3976    between a group and the super-group to which it belongs for purposes of
3977    display. The super-group name is computed from the first capitalized word
3978    in the actual group name. For each super-group, the group file contains
3979    the page name and a list of the species expected to be in the group.
3980    Each species is specified by a genus and a species name. A species name
3981    of C<0> implies an entire genus.
3982    
3983    This method returns a hash from super-group names to a hash reference. Each
3984    resulting hash reference contains the following fields.
3985    
3986    =over 4
3987    
3988    =item page
3989    
3990    The super-group's web page in the NMPDR.
3991    
3992    =item contents
3993    
3994    A list of 2-tuples, each containing a genus name followed by a species name
3995    (or 0, indicating all species). This list indicates which organisms belong
3996    in the super-group.
3997    
3998    =back
3999    
4000    =cut
4001    
4002    sub CheckGroupFile {
4003        # Get the parameters.
4004        my ($self) = @_;
4005        # Check to see if we already have this hash.
4006        if (! defined $self->{groupHash}) {
4007            # We don't, so we need to read it in.
4008            my %groupHash;
4009            # Read the group file.
4010            my @groupLines = Tracer::GetFile("$FIG_Config::sproutData/groups.tbl");
4011            # Loop through the list of sort-of groups.
4012            for my $groupLine (@groupLines) {
4013                my ($name, $page, @contents) = split /\t/, $groupLine;
4014                $groupHash{$name} = { page => $page,
4015                                      contents => [ map { [ split /\s*,\s*/, $_ ] } @contents ]
4016                                    };
4017            }
4018            # Save the hash.
4019            $self->{groupHash} = \%groupHash;
4020        }
4021        # Return the result.
4022        return %{$self->{groupHash}};
4023    }
4024    
4025    =head2 Virtual Methods
4026    
4027    =head3 CleanKeywords
4028    
4029        my $cleanedString = $sprout->CleanKeywords($searchExpression);
4030    
4031    Clean up a search expression or keyword list. This involves converting the periods
4032    in EC numbers to underscores, converting non-leading minus signs to underscores,
4033    a vertical bar or colon to an apostrophe, and forcing lower case for all alphabetic
4034    characters. In addition, any extra spaces are removed.
4035    
<