[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.4, Tue Jan 25 01:36:09 2005 UTC revision 1.122, Mon Jan 19 21:46:21 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.
   
 =cut  
39    
40  #: Constructor SFXlate->new_sprout_only();  The Sprout object is a subclass of the ERDB object and inherits all its properties and methods.
41    
42    =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 51  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 63  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                                            }, $options);                                            }, $options);
137          # Get the data directory.          # Get the data directory.
138          my $dataDir = $optionTable->{dataDir};          my $dataDir = $optionTable->{dataDir};
# Line 101  Line 140 
140          $optionTable->{userData} =~ m!([^/]*)/(.*)$!;          $optionTable->{userData} =~ m!([^/]*)/(.*)$!;
141          my ($userName, $password) = ($1, $2);          my ($userName, $password) = ($1, $2);
142          # Connect to the database.          # Connect to the database.
143          my $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName, $password, $optionTable->{port});      my $dbh;
144        if (! $optionTable->{noDBOpen}) {
145            Trace("Connect data: host = $optionTable->{host}, port = $optionTable->{port}.") if T(3);
146            $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,
147                                    $password, $optionTable->{port}, $optionTable->{host}, $optionTable->{sock});
148        }
149          # Create the ERDB object.          # Create the ERDB object.
150          my $xmlFileName = "$optionTable->{xmlFileName}";          my $xmlFileName = "$optionTable->{xmlFileName}";
151          my $erdb = ERDB->new($dbh, $xmlFileName);      my $retVal = ERDB::new($class, $dbh, $xmlFileName);
152          # Create this object.      # Add the option table and XML file name.
153          my $self = { _erdb => $erdb, _options => $optionTable, _xmlName => $xmlFileName };      $retVal->{_options} = $optionTable;
154          # Bless and return it.      $retVal->{_xmlName} = $xmlFileName;
155          bless $self;      # Set up space for the group file data.
156          return $self;      $retVal->{groupHash} = undef;
157        # Set up space for the genome hash. We use this to identify NMPDR genomes
158        # and remember genome data.
159        $retVal->{genomeHash} = {};
160        $retVal->{genomeHashFilled} = 0;
161        # Remember the data directory name.
162        $retVal->{dataDir} = $dataDir;
163        # Return it.
164        return $retVal;
165  }  }
166    
167  =head3 MaxSegment  =head3 ca
168    
169  C<< my $length = $sprout->MaxSegment(); >>      my $ca = $sprout->ca():;
170    
171  This method returns the maximum permissible length of a feature segment. The length is important  Return the [[CustomAttributesPm]] object for retrieving object
172  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.  
173    
174  =cut  =cut
 #: Return Type $;  
 sub MaxSegment {  
         my $self = shift @_;  
         return $self->{_options}->{maxSegmentLength};  
 }  
   
 =head3 MaxSequence  
175    
176  C<< my $length = $sprout->MaxSequence(); >>  sub ca {
177        # Get the parameters.
178  This method returns the maximum permissible length of a contig sequence. A contig is broken      my ($self) = @_;
179  into sequences in order to save memory resources. In particular, when manipulating features,      # Do we already have an attribute object?
180  we generally only need a few sequences in memory rather than the entire contig.      my $retVal = $self->{_ca};
181        if (! defined $retVal) {
182  =cut          # No, create one. How we do it depends on the configuration.
183  #: Return Type $;          if ($FIG_Config::attrURL) {
184  sub MaxSequence {              Trace("Remote attribute server $FIG_Config::attrURL chosen.") if T(3);
185          my $self = shift @_;              $retVal = RemoteCustomAttributes->new($FIG_Config::attrURL);
186          return $self->{_options}->{maxSequenceLength};          } elsif ($FIG_Config::attrDbName) {
187                Trace("Local attribute database $FIG_Config::attrDbName chosen.") if T(3);
188                my $user = ($FIG_Config::arch eq 'win' ? 'self' : scalar(getpwent()));
189                $retVal = CustomAttributes->new(user => $user);
190            }
191            # Save it for next time.
192            $self->{_ca} = $retVal;
193        }
194        # Return the result.
195        return $retVal;
196  }  }
197    
198  =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)"); >>  
199    
200  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.  
201    
202  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.  
203    
204  =over 4  =over 4
205    
206  =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.  
207    
208  =item parameterList  Scope of the desired genomes. C<core> covers the original core genomes,
209    C<nmpdr> covers all genomes in NMPDR groups, and C<all> covers all
210  List of the parameters to be substituted in for the parameters marks in the filter clause.  genomes in the system.
211    
212  =item RETURN  =item RETURN
213    
214  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.
215    
216  =back  =back
217    
218  =cut  =cut
219    
220  sub Get {  sub CoreGenomes {
221          # Get the parameters.          # Get the parameters.
222          my $self = shift @_;      my ($self, $scope) = @_;
223          my ($objectNames, $filterClause, $parameterList) = @_;      # Declare the return variable.
224          # We differ from the ERDB Get method in that the parameter list is passed in as a list reference      my @retVal = ();
225          # 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.
226          # to a real list. We can only do this if the parameters have been specified.      if ($scope eq 'all') {
227          my @parameters;          @retVal = $self->Genomes();
228          if ($parameterList) { @parameters = @{$parameterList}; }      } else {
229          return $self->{_erdb}->Get($objectNames, $filterClause, @parameters);          # Here we're dealing with groups. Get the hash of all the
230            # genome groups.
231            my %groups = $self->GetGroups();
232            # Loop through the groups, keeping the ones that we want.
233            for my $group (keys %groups) {
234                # Decide if we want to keep this group.
235                my $keepGroup = 0;
236                if ($scope eq 'nmpdr') {
237                    # NMPDR mode: keep all groups.
238                    $keepGroup = 1;
239                } elsif ($scope eq 'core') {
240                    # CORE mode. Only keep real core groups.
241                    if (grep { $group =~ /$_/ } @{$FIG_Config::realCoreGroups}) {
242                        $keepGroup = 1;
243                    }
244                }
245                # Add this group if we're keeping it.
246                if ($keepGroup) {
247                    push @retVal, @{$groups{$group}};
248                }
249            }
250        }
251        # Return the result.
252        return @retVal;
253  }  }
254    
255  =head3 GetEntity  =head3 SuperGroup
256    
257  C<< my $entityObject = $sprout->GetEntity($entityType, $ID); >>      my $superGroup = $sprout->SuperGroup($groupName);
258    
259  Return an object describing the entity instance with a specified ID.  Return the name of the super-group containing the specified NMPDR genome
260    group. If no appropriate super-group can be found, an error will be
261    thrown.
262    
263  =over 4  =over 4
264    
265  =item entityType  =item groupName
   
 Entity type name.  
266    
267  =item ID  Name of the group whose super-group is desired.
   
 ID of the desired entity.  
268    
269  =item RETURN  =item RETURN
270    
271  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.  
272    
273  =back  =back
274    
275  =cut  =cut
276    
277  sub GetEntity {  sub SuperGroup {
278          # Get the parameters.          # Get the parameters.
279          my $self = shift @_;      my ($self, $groupName) = @_;
280          my ($entityType, $ID) = @_;      # Declare the return variable.
281          # Create a query.      my $retVal;
282          my $query = $self->Get([$entityType], "$entityType(id) = ?", [$ID]);      # Get the group hash.
283          # Get the first (and only) object.      my %groupHash = $self->CheckGroupFile();
284          my $retVal = $query->Fetch();      # Find the super-group genus.
285        $groupName =~ /([A-Z]\w+)/;
286        my $nameThing = $1;
287        # See if it's directly in the group hash.
288        if (exists $groupHash{$nameThing}) {
289            # Yes, then it's our result.
290            $retVal = $nameThing;
291        } else {
292            # No, so we have to search.
293            for my $superGroup (keys %groupHash) {
294                # Get this super-group's item list.
295                my $list = $groupHash{$superGroup}->{contents};
296                # Search it.
297                if (grep { $_->[0] eq $nameThing } @{$list}) {
298                    $retVal = $superGroup;
299                }
300            }
301            # Make sure we found something.
302            if (! $retVal) {
303                Confess("No super-group found for \"$groupName\".");
304            }
305        }
306          # Return the result.          # Return the result.
307          return $retVal;          return $retVal;
308  }  }
309    
310  =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  
311    
312  Returns a flattened list of the values of the specified fields for the specified entity.      my $length = $sprout->MaxSegment();
313    
314  =back  This method returns the maximum permissible length of a feature segment. The length is important
315    because it enables us to make reasonable guesses at how to find features inside a particular
316    contig region. For example, if the maximum length is 4000 and we're looking for a feature that
317    overlaps the region from 6000 to 7000 we know that the starting position must be between 2001
318    and 10999.
319    
320  =cut  =cut
321  #: Return Type @;  #: Return Type $;
322  sub GetEntityValues {  sub MaxSegment {
323          # Get the parameters.      my ($self) = @_;
324          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;  
325  }  }
326    
327  =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  
328    
329  Fully-qualified name to give to the output file.      my $length = $sprout->MaxSequence();
330    
331  =back  This method returns the maximum permissible length of a contig sequence. A contig is broken
332    into sequences in order to save memory resources. In particular, when manipulating features,
333    we generally only need a few sequences in memory rather than the entire contig.
334    
335  =cut  =cut
336    #: Return Type $;
337  sub ShowMetaData {  sub MaxSequence {
338          # Get the parameters.      my ($self) = @_;
339          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);  
340  }  }
341    
342  =head3 Load  =head3 Load
343    
344  C<< $sprout->Load($rebuild); >>;      $sprout->Load($rebuild);;
345    
346  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.
347    
# Line 364  Line 353 
353  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
354  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
355  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
356  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.
357    
358  =over 4  =over 4
359    
# Line 383  Line 372 
372  #: Return Type %;  #: Return Type %;
373  sub Load {  sub Load {
374          # Get the parameters.          # Get the parameters.
375          my $self = shift @_;      my ($self, $rebuild) = @_;
         my ($rebuild) = @_;  
         # Get the database object.  
         my $erdb = $self->{_erdb};  
376          # Load the tables from the data directory.          # Load the tables from the data directory.
377          my $retVal = $erdb->LoadTables($self->{_options}->{dataDir}, $rebuild);      my $retVal = $self->LoadTables($self->{_options}->{dataDir}, $rebuild);
378          # Return the statistics.          # Return the statistics.
379          return $retVal;          return $retVal;
380  }  }
381    
382  =head3 LoadUpdate  =head3 LoadUpdate
383    
384  C<< my %stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>      my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList);
385    
386  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
387  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 424  Line 410 
410  =back  =back
411    
412  =cut  =cut
413  #: Return Type %;  #: Return Type $%;
414  sub LoadUpdate {  sub LoadUpdate {
415          # Get the parameters.          # Get the parameters.
416          my $self = shift @_;      my ($self, $truncateFlag, $tableList) = @_;
         my ($truncateFlag, $tableList) = @_;  
         # Get the database object.  
         my $erdb = $self->{_erdb};  
417          # Declare the return value.          # Declare the return value.
418          my $retVal = Stats->new();          my $retVal = Stats->new();
419          # Get the data directory.          # Get the data directory.
# Line 439  Line 422 
422          # Loop through the incoming table names.          # Loop through the incoming table names.
423          for my $tableName (@{$tableList}) {          for my $tableName (@{$tableList}) {
424                  # Find the table's file.                  # Find the table's file.
425                  my $fileName = "$dataDir/$tableName";          my $fileName = LoadFileName($dataDir, $tableName);
426                  if (! -e $fileName) {          if (! $fileName) {
427                          $fileName = "$fileName.dtx";              Trace("No load file found for $tableName in $dataDir.") if T(0);
428                  }          } else {
429                  # Attempt to load this table.                  # Attempt to load this table.
430                  my $result = $erdb->LoadTable($fileName, $tableName, $truncateFlag);              my $result = $self->LoadTable($fileName, $tableName, truncate => $truncateFlag);
431                  # Accumulate the resulting statistics.                  # Accumulate the resulting statistics.
432                  $retVal->Accumulate($result);                  $retVal->Accumulate($result);
433          }          }
434        }
435          # Return the statistics.          # Return the statistics.
436          return $retVal;          return $retVal;
437  }  }
438    
439    =head3 GenomeCounts
440    
441        my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete);
442    
443    Count the number of genomes in each domain. If I<$complete> is TRUE, only complete
444    genomes will be included in the counts.
445    
446    =over 4
447    
448    =item complete
449    
450    TRUE if only complete genomes are to be counted, FALSE if all genomes are to be
451    counted
452    
453    =item RETURN
454    
455    A six-element list containing the number of genomes in each of six categories--
456    Archaea, Bacteria, Eukaryota, Viral, Environmental, and Unknown, respectively.
457    
458    =back
459    
460    =cut
461    
462    sub GenomeCounts {
463        # Get the parameters.
464        my ($self, $complete) = @_;
465        # Set the filter based on the completeness flag.
466        my $filter = ($complete ? "Genome(complete) = 1" : "");
467        # Get all the genomes and the related taxonomy information.
468        my @genomes = $self->GetAll(['Genome'], $filter, [], ['Genome(id)', 'Genome(taxonomy)']);
469        # Clear the counters.
470        my ($arch, $bact, $euk, $vir, $env, $unk) = (0, 0, 0, 0, 0, 0);
471        # Loop through, counting the domains.
472        for my $genome (@genomes) {
473            if    ($genome->[1] =~ /^archaea/i)  { ++$arch }
474            elsif ($genome->[1] =~ /^bacter/i)   { ++$bact }
475            elsif ($genome->[1] =~ /^eukar/i)    { ++$euk }
476            elsif ($genome->[1] =~ /^vir/i)      { ++$vir }
477            elsif ($genome->[1] =~ /^env/i)      { ++$env }
478            else  { ++$unk }
479        }
480        # Return the counts.
481        return ($arch, $bact, $euk, $vir, $env, $unk);
482    }
483    
484    =head3 ContigCount
485    
486        my $count = $sprout->ContigCount($genomeID);
487    
488    Return the number of contigs for the specified genome ID.
489    
490    =over 4
491    
492    =item genomeID
493    
494    ID of the genome whose contig count is desired.
495    
496    =item RETURN
497    
498    Returns the number of contigs for the specified genome.
499    
500    =back
501    
502    =cut
503    
504    sub ContigCount {
505        # Get the parameters.
506        my ($self, $genomeID) = @_;
507        # Get the contig count.
508        my $retVal = $self->GetCount(['Contig', 'HasContig'], "HasContig(from-link) = ?", [$genomeID]);
509        # Return the result.
510        return $retVal;
511    }
512    
513    =head3 GenomeMenu
514    
515        my $html = $sprout->GenomeMenu(%options);
516    
517    Generate a genome selection control with the specified name and options.
518    This control is almost but not quite the same as the genome control in the
519    B<SearchHelper> class. Eventually, the two will be combined.
520    
521    =over 4
522    
523    =item options
524    
525    Optional parameters for the control (see below).
526    
527    =item RETURN
528    
529    Returns the HTML for a genome selection control on a form (sometimes called a popup menu).
530    
531    =back
532    
533    The valid options are as follows.
534    
535    =over 4
536    
537    =item name
538    
539    Name to give this control for use in passing it to the form. The default is C<myGenomeControl>.
540    Terrible things will happen if you have two controls with the same name on the same page.
541    
542    =item filter
543    
544    If specified, a filter for the list of genomes to display. The filter should be in the form of a
545    list reference, a string, or a hash reference. If it is a list reference, the first element
546    of the list should be the filter string, and the remaining elements the filter parameters. If it is a
547    string, it will be split into a list at each included tab. If it is a hash reference, it should be
548    a hash that maps genomes which should be included to a TRUE value.
549    
550    =item multiSelect
551    
552    If TRUE, then the user can select multiple genomes. If FALSE, the user can only select one genome.
553    
554    =item size
555    
556    Number of rows to display in the control. The default is C<10>
557    
558    =item id
559    
560    ID to give this control. The default is the value of the C<name> option. Nothing will work correctly
561    unless this ID is unique.
562    
563    =item selected
564    
565    A comma-delimited list of selected genomes, or a reference to a list of selected genomes. The
566    default is none.
567    
568    =item class
569    
570    If specified, a style class to assign to the genome control.
571    
572    =back
573    
574    =cut
575    
576    sub GenomeMenu {
577        # Get the parameters.
578        my ($self, %options) = @_;
579        # Get the control's name and ID.
580        my $menuName = $options{name} || $options{id} || 'myGenomeControl';
581        my $menuID = $options{id} || $menuName;
582        Trace("Genome menu name = $menuName with ID $menuID.") if T(3);
583        # Compute the IDs for the status display.
584        my $divID = "${menuID}_status";
585        my $urlID = "${menuID}_url";
586        # Compute the code to show selected genomes in the status area.
587        my $showSelect = "showSelected('$menuID', '$divID', '$urlID', $FIG_Config::genome_control_cap)";
588        # Check for single-select or multi-select.
589        my $multiSelect = $options{multiSelect} || 0;
590        # Get the style data.
591        my $class = $options{class} || '';
592        # Get the list of pre-selected items.
593        my $selections = $options{selected} || [];
594        if (ref $selections ne 'ARRAY') {
595            $selections = [ split /\s*,\s*/, $selections ];
596        }
597        my %selected = map { $_ => 1 } @{$selections};
598        # Extract the filter information. The default is no filtering. It can be passed as a tab-delimited
599        # string, a hash reference, or a list reference.
600        my ($filterHash, $filterString);
601        my $filterParms = $options{filter} || "";
602        if (ref $filterParms eq 'HASH') {
603            $filterHash = $filterParms;
604            $filterParms = [];
605            $filterString = "";
606        } else {
607            if (! ref $filterParms) {
608                $filterParms = [split /\t|\\t/, $filterParms];
609            }
610            $filterString = shift @{$filterParms};
611        }
612        # Check for possible subsystem filtering. If there is one, we will tack the
613        # relationship onto the object name list.
614        my @objectNames = qw(Genome);
615        if ($filterString =~ /ParticipatesIn\(/) {
616            push @objectNames, 'ParticipatesIn';
617        }
618        # Get a list of all the genomes in group order. In fact, we only need them ordered
619        # by name (genus,species,strain), but putting primary-group in front enables us to
620        # take advantage of an existing index.
621        my @genomeList = $self->GetAll(\@objectNames, "$filterString ORDER BY Genome(primary-group), Genome(genus), Genome(species), Genome(unique-characterization)",
622                                       $filterParms,
623                                       [qw(Genome(primary-group) Genome(id) Genome(genus) Genome(species) Genome(unique-characterization) Genome(taxonomy) Genome(contigs))]);
624        # Apply the hash filter (if any).
625        if (defined $filterHash) {
626            @genomeList = grep { $filterHash->{$_->[1]} } @genomeList;
627        }
628        # Create a hash to organize the genomes by group. Each group will contain a list of
629        # 2-tuples, the first element being the genome ID and the second being the genome
630        # name.
631        my %gHash = ();
632        for my $genome (@genomeList) {
633            # Get the genome data.
634            my ($group, $genomeID, $genus, $species, $strain, $taxonomy, $contigs) = @{$genome};
635            # Compute its name. This is the genus, species, strain (if any), and the contig count.
636            my $name = "$genus $species ";
637            $name .= "$strain " if $strain;
638            my $contigCount = ($contigs == 1 ? "" : ", $contigs contigs");
639            # Now we get the domain. The domain tells us the display style of the organism.
640            my ($domain) = split /\s*;\s*/, $taxonomy, 2;
641            # Now compute the display group. This is normally the primary group, but if the
642            # organism is supporting, we blank it out.
643            my $displayGroup = ($group eq $FIG_Config::otherGroup ? "" : $group);
644            # Push the genome into the group's list. Note that we use the real group
645            # name for the hash key here, not the display group name.
646            push @{$gHash{$group}}, [$genomeID, $name, $contigCount, $domain];
647        }
648        # We are almost ready to unroll the menu out of the group hash. The final step is to separate
649        # the supporting genomes by domain. First, we extract the NMPDR groups and sort them. They
650        # are sorted by the first capitalized word. Groups with "other" are sorted after groups
651        # that aren't "other". At some point, we will want to make this less complicated.
652        my %sortGroups = map { $_ =~ /(other)?(.*)([A-Z].+)/; "$3$1$2" => $_ }
653                             grep { $_ ne $FIG_Config::otherGroup } keys %gHash;
654        my @groups = map { $sortGroups{$_} } sort keys %sortGroups;
655        # Remember the number of NMPDR groups.
656        my $nmpdrGroupCount = scalar @groups;
657        # Are there any supporting genomes?
658        if (exists $gHash{$FIG_Config::otherGroup}) {
659            # Loop through the supporting genomes, classifying them by domain. We'll also keep a list
660            # of the domains found.
661            my @otherGenomes = @{$gHash{$FIG_Config::otherGroup}};
662            my @domains = ();
663            for my $genomeData (@otherGenomes) {
664                my ($genomeID, $name, $contigCount, $domain) = @{$genomeData};
665                if (exists $gHash{$domain}) {
666                    push @{$gHash{$domain}}, $genomeData;
667                } else {
668                    $gHash{$domain} = [$genomeData];
669                    push @domains, $domain;
670                }
671            }
672            # Add the domain groups at the end of the main group list. The main group list will now
673            # contain all the categories we need to display the genomes.
674            push @groups, sort @domains;
675            # Delete the supporting group.
676            delete $gHash{$FIG_Config::otherGroup};
677        }
678        # Now it gets complicated. We need a way to mark all the NMPDR genomes. We take advantage
679        # of the fact they come first in the list. We'll accumulate a count of the NMPDR genomes
680        # and use that to make the selections.
681        my $nmpdrCount = 0;
682        # Create the type counters.
683        my $groupCount = 1;
684        # Get the number of rows to display.
685        my $rows = $options{size} || 10;
686        # If we're multi-row, create an onChange event.
687        my $onChangeTag = ( $rows > 1 ? " onChange=\"$showSelect;\" onFocus=\"$showSelect;\"" : "" );
688        # Set up the multiple-select flag.
689        my $multipleTag = ($multiSelect ? " multiple" : "" );
690        # Set up the style class.
691        my $classTag = ($class ? " class=\"$class\"" : "" );
692        # Create the SELECT tag and stuff it into the output array.
693        my @lines = ("<SELECT name=\"$menuName\" id=\"$menuID\" $onChangeTag$multipleTag$classTag size=\"$rows\">");
694        # Loop through the groups.
695        for my $group (@groups) {
696            # Get the genomes in the group.
697            for my $genome (@{$gHash{$group}}) {
698                # If this is an NMPDR organism, we add an extra style and count it.
699                my $nmpdrStyle = "";
700                if ($nmpdrGroupCount > 0) {
701                    $nmpdrCount++;
702                    $nmpdrStyle = " Core";
703                }
704                # Get the organism ID, name, contig count, and domain.
705                my ($genomeID, $name, $contigCount, $domain) = @{$genome};
706                # See if we're pre-selected.
707                my $selectTag = ($selected{$genomeID} ? " SELECTED" : "");
708                # Compute the display name.
709                my $nameString = "$name ($genomeID$contigCount)";
710                # Generate the option tag.
711                my $optionTag = "<OPTION class=\"$domain$nmpdrStyle\" title=\"$group\" value=\"$genomeID\"$selectTag>$nameString</OPTION>";
712                push @lines, "    $optionTag";
713            }
714            # Record this group in the nmpdrGroup count. When that gets to 0, we've finished the NMPDR
715            # groups.
716            $nmpdrGroupCount--;
717        }
718        # Close the SELECT tag.
719        push @lines, "</SELECT>";
720        if ($rows > 1) {
721            # We're in a non-compact mode, so we need to add some selection helpers. First is
722            # the search box. This allows the user to type text and change which genomes are
723            # displayed. For multiple-select mode, we include a button that selects the displayed
724            # genes. For single-select mode, we use a plain label instead.
725            my $searchThingName = "${menuID}_SearchThing";
726            my $searchThingLabel = ($multiSelect ? "<INPUT type=\"button\" name=\"MacroSearch\" class=\"button\" value=\"Select genomes containing\" onClick=\"selectShowing('$menuID', '$searchThingName'); $showSelect;\" />"
727                                                 : "Show genomes containing");
728            push @lines, "<br />$searchThingLabel&nbsp;" .
729                         "<INPUT type=\"text\" id=\"$searchThingName\" name=\"$searchThingName\" size=\"30\" onKeyup=\"showTyped('$menuID', '$searchThingName');\" />" .
730                         Hint("GenomeControl", "Type here to filter the genomes displayed.") . "<br />";
731            # For multi-select mode, we also have buttons to set and clear selections.
732            if ($multiSelect) {
733                push @lines, "<INPUT type=\"button\" name=\"ClearAll\" class=\"bigButton\"  value=\"Clear All\" onClick=\"clearAll(getElementById('$menuID')); $showSelect\" />";
734                push @lines, "<INPUT type=\"button\" name=\"SelectAll\" class=\"bigButton\" value=\"Select All\" onClick=\"selectAll(getElementById('$menuID')); $showSelect\" />";
735                push @lines, "<INPUT type=\"button\" name=\"NMPDROnly\" class=\"bigButton\"  value=\"Select NMPDR\" onClick=\"selectSome(getElementById('$menuID'), $nmpdrCount, true); $showSelect;\" />";
736            }
737            # Add a hidden field we can use to generate organism page hyperlinks.
738            push @lines, "<INPUT type=\"hidden\" id=\"$urlID\" value=\"$FIG_Config::cgi_url/wiki/rest.cgi/NmpdrPlugin/SeedViewer?page=Organism;organism=\" />";
739            # Add the status display. This tells the user what's selected no matter where the list is scrolled.
740            push @lines, "<DIV id=\"$divID\" class=\"Panel\"></DIV>";
741        }
742        # Assemble all the lines into a string.
743        my $retVal = join("\n", @lines, "");
744        # Return the result.
745        return $retVal;
746    }
747    
748    
749    =head3 Stem
750    
751        my $stem = $sprout->Stem($word);
752    
753    Return the stem of the specified word, or C<undef> if the word is not
754    stemmable. Note that even if the word is stemmable, the stem may be
755    the same as the original word.
756    
757    =over 4
758    
759    =item word
760    
761    Word to convert into a stem.
762    
763    =item RETURN
764    
765    Returns a stem of the word (which may be the word itself), or C<undef> if
766    the word is not stemmable.
767    
768    =back
769    
770    =cut
771    
772    sub Stem {
773        # Get the parameters.
774        my ($self, $word) = @_;
775        # Get the stemmer object.
776        my $stemmer = $self->{stemmer};
777        if (! defined $stemmer) {
778            # We don't have one pre-built, so we build and save it now.
779            $stemmer = BioWords->new(exceptions => "$FIG_Config::sproutData/Exceptions.txt",
780                                     stops => "$FIG_Config::sproutData/StopWords.txt",
781                                     cache => 0);
782            $self->{stemmer} = $stemmer;
783        }
784        # Try to stem the word.
785        my $retVal = $stemmer->Process($word);
786        # Return the result.
787        return $retVal;
788    }
789    
790    
791  =head3 Build  =head3 Build
792    
793  C<< $sprout->Build(); >>      $sprout->Build();
794    
795  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.
796  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 464  Line 800 
800  #: Return Type ;  #: Return Type ;
801  sub Build {  sub Build {
802          # Get the parameters.          # Get the parameters.
803          my $self = shift @_;      my ($self) = @_;
804          # Create the tables.          # Create the tables.
805          $self->{_erdb}->CreateTables;      $self->CreateTables();
806  }  }
807    
808  =head3 Genomes  =head3 Genomes
809    
810  C<< my @genomes = $sprout->Genomes(); >>      my @genomes = $sprout->Genomes();
811    
812  Return a list of all the genome IDs.  Return a list of all the genome IDs.
813    
# Line 479  Line 815 
815  #: Return Type @;  #: Return Type @;
816  sub Genomes {  sub Genomes {
817          # Get the parameters.          # Get the parameters.
818          my $self = shift @_;      my ($self) = @_;
819          # Get all the genomes.          # Get all the genomes.
820          my @retVal = $self->GetFlat(['Genome'], "", [], 'Genome(id)');          my @retVal = $self->GetFlat(['Genome'], "", [], 'Genome(id)');
821          # Return the list of IDs.          # Return the list of IDs.
# Line 488  Line 824 
824    
825  =head3 GenusSpecies  =head3 GenusSpecies
826    
827  C<< my $infoString = $sprout->GenusSpecies($genomeID); >>      my $infoString = $sprout->GenusSpecies($genomeID);
828    
829  Return the genus, species, and unique characterization for a genome.  Return the genus, species, and unique characterization for a genome.
830    
# Line 509  Line 845 
845  #: Return Type $;  #: Return Type $;
846  sub GenusSpecies {  sub GenusSpecies {
847          # Get the parameters.          # Get the parameters.
848          my $self = shift @_;      my ($self, $genomeID) = @_;
849          my ($genomeID) = @_;      # Declare the return value.
850          # Get the data for the specified genome.      my $retVal;
851          my @values = $self->GetEntityValues('Genome', $genomeID, ['Genome(genus)', 'Genome(species)',      # Get the genome data.
852                                                                                                                            'Genome(unique-characterization)']);      my $genomeData = $self->_GenomeData($genomeID);
853          # Format the result and return it.      # Only proceed if we found the genome.
854          my $retVal = join(' ', @values);      if (defined $genomeData) {
855            $retVal = $genomeData->PrimaryValue('Genome(scientific-name)');
856        }
857        # Return it.
858          return $retVal;          return $retVal;
859  }  }
860    
861  =head3 FeaturesOf  =head3 FeaturesOf
862    
863  C<< my @features = $sprout->FeaturesOf($genomeID, $ftype); >>      my @features = $sprout->FeaturesOf($genomeID, $ftype);
864    
865  Return a list of the features relevant to a specified genome.  Return a list of the features relevant to a specified genome.
866    
# Line 546  Line 885 
885  #: Return Type @;  #: Return Type @;
886  sub FeaturesOf {  sub FeaturesOf {
887          # Get the parameters.          # Get the parameters.
888          my $self = shift @_;      my ($self, $genomeID,$ftype) = @_;
         my ($genomeID,$ftype) = @_;  
889          # Get the features we want.          # Get the features we want.
890          my @features;          my @features;
891          if (!$ftype) {          if (!$ftype) {
# Line 567  Line 905 
905    
906  =head3 FeatureLocation  =head3 FeatureLocation
907    
908  C<< my @locations = $sprout->FeatureLocation($featureID); >>      my @locations = $sprout->FeatureLocation($featureID);
909    
910  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
911  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 591  Line 929 
929  =item RETURN  =item RETURN
930    
931  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
932  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
933    wasn't found.
934    
935  =back  =back
936    
937  =cut  =cut
938  #: Return Type @;  
 #: Return Type $;  
939  sub FeatureLocation {  sub FeatureLocation {
940          # Get the parameters.          # Get the parameters.
941          my $self = shift @_;      my ($self, $featureID) = @_;
942          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.  
943          my @retVal = ();          my @retVal = ();
944          # Set up the variables used to determine if we have adjacent segments. This initial setup will      # Get the feature record.
945          # not match anything.      my $object = $self->GetEntity('Feature', $featureID);
946          my ($prevContig, $prevBeg, $prevDir, $prevLen) = ("", 0, "0", 0);      # Only proceed if we found it.
947          # Loop through the query results, creating location specifiers.      if (defined $object) {
948          while (my $location = $query->Fetch()) {          # Get the location string.
949                  # Get the location parameters.          my $locString = $object->PrimaryValue('Feature(location-string)');
950                  my ($contigID, $beg, $dir, $len) = $location->Values(['IsLocatedIn(to-link)',          # Create the return list.
951                          '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";  
952          }          }
953          # Return the list in the format indicated by the context.          # Return the list in the format indicated by the context.
954          return (wantarray ? @retVal : join(' ', @retVal));      return (wantarray ? @retVal : join(',', @retVal));
955  }  }
956    
957  =head3 ParseLocation  =head3 ParseLocation
958    
959  C<< my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location); >>      my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location);
960    
961  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
962  length.  length.
# Line 659  Line 975 
975  =back  =back
976    
977  =cut  =cut
978  #: Return Type @;  
979  sub ParseLocation {  sub ParseLocation {
980          # Get the parameter.      # Get the parameter. Note that if we're called as an instance method, we ignore
981        # the first parameter.
982        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
983          my ($location) = @_;          my ($location) = @_;
984          # Parse it into segments.          # Parse it into segments.
985          $location =~ /^(.*)_(\d*)([+-_])(\d*)$/;      $location =~ /^(.+)_(\d+)([+\-_])(\d+)$/;
986          my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);          my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);
987          # If the direction is an underscore, convert it to a + or -.          # If the direction is an underscore, convert it to a + or -.
988          if ($dir eq "_") {          if ($dir eq "_") {
# Line 680  Line 998 
998          return ($contigID, $start, $dir, $len);          return ($contigID, $start, $dir, $len);
999  }  }
1000    
 =head3 DNASeq  
1001    
 C<< my $sequence = $sprout->DNASeq(\@locationList); >>  
1002    
1003  This method returns the DNA sequence represented by a list of locations. The list of locations  =head3 PointLocation
1004  should be of the form returned by L</featureLocation> when in a list context. In other words,  
1005  each location is of the form I<contigID>C<_>I<begin>I<dir>I<end>.      my $found = Sprout::PointLocation($location, $point);
1006    
1007    Return the offset into the specified location of the specified point on the contig. If
1008    the specified point is before the location, a negative value will be returned. If it is
1009    beyond the location, an undefined value will be returned. It is assumed that the offset
1010    is for the location's contig. The location can either be new-style (using a C<+> or C<->
1011    and a length) or old-style (using C<_> and start and end positions.
1012    
1013  =over 4  =over 4
1014    
1015  =item locationList  =item location
1016    
1017    A location specifier (see L</FeatureLocation> for a description).
1018    
1019  List of location specifiers, each in the form I<contigID>C<_>I<begin>I<dir>I<end> (see  =item point
1020  L</FeatureLocation> for more about this format).  
1021    The offset into the contig of the point in which we're interested.
1022    
1023  =item RETURN  =item RETURN
1024    
1025  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
1026    number if the point is before the location, or an undefined value if the point is past
1027    the location. If the length of the location is 0, this method will B<always> denote
1028    that it is outside the location. The offset will always be relative to the left-most
1029    position in the location.
1030    
1031  =back  =back
1032    
1033  =cut  =cut
1034  #: Return Type $;  
1035  sub DNASeq {  sub PointLocation {
1036        # Get the parameter. Note that if we're called as an instance method, we ignore
1037        # the first parameter.
1038        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1039        my ($location, $point) = @_;
1040        # Parse out the location elements. Note that this works on both old-style and new-style
1041        # locations.
1042        my ($contigID, $start, $dir, $len) = ParseLocation($location);
1043        # Declare the return variable.
1044        my $retVal;
1045        # Compute the offset. The computation is dependent on the direction of the location.
1046        my $offset = (($dir == '+') ? $point - $start : $point - ($start - $len + 1));
1047        # Return the offset if it's valid.
1048        if ($offset < $len) {
1049            $retVal = $offset;
1050        }
1051        # Return the offset found.
1052        return $retVal;
1053    }
1054    
1055    =head3 DNASeq
1056    
1057        my $sequence = $sprout->DNASeq(\@locationList);
1058    
1059    This method returns the DNA sequence represented by a list of locations. The list of locations
1060    should be of the form returned by L</featureLocation> when in a list context. In other words,
1061    each location is of the form I<contigID>C<_>I<begin>I<dir>I<end>.
1062    
1063    For example, the following would return the DNA sequence for contig C<83333.1:NC_000913>
1064    between positions 1401 and 1532, inclusive.
1065    
1066        my $sequence = $sprout->DNASeq('83333.1:NC_000913_1401_1532');
1067    
1068    =over 4
1069    
1070    =item locationList
1071    
1072    List of location specifiers, each in the form I<contigID>C<_>I<begin>I<dir>I<len> or
1073    I<contigID>C<_>I<begin>C<_>I<end> (see L</FeatureLocation> for more about this format).
1074    
1075    =item RETURN
1076    
1077    Returns a string of nucleotides corresponding to the DNA segments in the location list.
1078    
1079    =back
1080    
1081    =cut
1082    #: Return Type $;
1083    sub DNASeq {
1084          # Get the parameters.          # Get the parameters.
1085          my $self = shift @_;      my ($self, $locationList) = @_;
         my ($locationList) = @_;  
1086          # Create the return string.          # Create the return string.
1087          my $retVal = "";          my $retVal = "";
1088          # Loop through the locations.          # Loop through the locations.
# Line 721  Line 1097 
1097                  # 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
1098                  # before putting it in the return value.                  # before putting it in the return value.
1099                  my ($start, $stop);                  my ($start, $stop);
1100            Trace("Parse of \"$location\" is $beg$dir$len.") if T(SDNA => 4);
1101                  if ($dir eq "+") {                  if ($dir eq "+") {
1102                          $start = $beg;                          $start = $beg;
1103                          $stop = $beg + $len - 1;                          $stop = $beg + $len - 1;
1104                  } else {                  } else {
1105                          $start = $beg + $len + 1;              $start = $beg - $len + 1;
1106                          $stop = $beg;                          $stop = $beg;
1107                  }                  }
1108            Trace("Looking for sequences containing $start through $stop.") if T(SDNA => 4);
1109                  my $query = $self->Get(['IsMadeUpOf','Sequence'],                  my $query = $self->Get(['IsMadeUpOf','Sequence'],
1110                          "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .                          "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .
1111                          " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",                          " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",
# Line 739  Line 1117 
1117                                  $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',                                  $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',
1118                                                                     'IsMadeUpOf(len)']);                                                                     'IsMadeUpOf(len)']);
1119                          my $stopPosition = $startPosition + $sequenceLength;                          my $stopPosition = $startPosition + $sequenceLength;
1120                Trace("Sequence is from $startPosition to $stopPosition.") if T(SDNA => 4);
1121                          # Figure out the start point and length of the relevant section.                          # Figure out the start point and length of the relevant section.
1122                          my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);                          my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);
1123                          my $len = ($stopPosition <= $stop ? $stopPosition : $stop) - $startPosition - $pos1;              my $len1 = ($stopPosition < $stop ? $stopPosition : $stop) + 1 - $startPosition - $pos1;
1124                Trace("Position is $pos1 for length $len1.") if T(SDNA => 4);
1125                          # Add the relevant data to the location data.                          # Add the relevant data to the location data.
1126                          $locationDNA .= substr($sequenceData, $pos1, $len);              $locationDNA .= substr($sequenceData, $pos1, $len1);
1127                  }                  }
1128                  # 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.
1129                  if ($dir eq '+') {                  if ($dir eq '+') {
1130                          $retVal .= $locationDNA;                          $retVal .= $locationDNA;
1131                  } else {                  } else {
1132                          $locationDNA = join('', reverse split //, $locationDNA);              $retVal .= FIG::reverse_comp($locationDNA);
                         $retVal .= $locationDNA;  
1133                  }                  }
1134          }          }
1135          # Return the result.          # Return the result.
# Line 759  Line 1138 
1138    
1139  =head3 AllContigs  =head3 AllContigs
1140    
1141  C<< my @idList = $sprout->AllContigs($genomeID); >>      my @idList = $sprout->AllContigs($genomeID);
1142    
1143  Return a list of all the contigs for a genome.  Return a list of all the contigs for a genome.
1144    
# Line 779  Line 1158 
1158  #: Return Type @;  #: Return Type @;
1159  sub AllContigs {  sub AllContigs {
1160          # Get the parameters.          # Get the parameters.
1161          my $self = shift @_;      my ($self, $genomeID) = @_;
         my ($genomeID) = @_;  
1162          # Ask for the genome's Contigs.          # Ask for the genome's Contigs.
1163          my @retVal = $self->GetFlat(['HasContig'], "HasContig(from-link) = ?", [$genomeID],          my @retVal = $self->GetFlat(['HasContig'], "HasContig(from-link) = ?", [$genomeID],
1164                                                                  'HasContig(to-link)');                                                                  'HasContig(to-link)');
# Line 788  Line 1166 
1166          return @retVal;          return @retVal;
1167  }  }
1168    
1169    =head3 GenomeLength
1170    
1171        my $length = $sprout->GenomeLength($genomeID);
1172    
1173    Return the length of the specified genome in base pairs.
1174    
1175    =over 4
1176    
1177    =item genomeID
1178    
1179    ID of the genome whose base pair count is desired.
1180    
1181    =item RETURN
1182    
1183    Returns the number of base pairs in all the contigs of the specified
1184    genome.
1185    
1186    =back
1187    
1188    =cut
1189    
1190    sub GenomeLength {
1191        # Get the parameters.
1192        my ($self, $genomeID) = @_;
1193        # Declare the return variable.
1194        my $retVal = 0;
1195        # Get the genome data.
1196        my $genomeData = $self->_GenomeData($genomeID);
1197        # Only proceed if it exists.
1198        if (defined $genomeData) {
1199            $retVal = $genomeData->PrimaryValue('Genome(dna-size)');
1200        }
1201        # Return the result.
1202        return $retVal;
1203    }
1204    
1205    =head3 FeatureCount
1206    
1207        my $count = $sprout->FeatureCount($genomeID, $type);
1208    
1209    Return the number of features of the specified type in the specified genome.
1210    
1211    =over 4
1212    
1213    =item genomeID
1214    
1215    ID of the genome whose feature count is desired.
1216    
1217    =item type
1218    
1219    Type of feature to count (eg. C<peg>, C<rna>, etc.).
1220    
1221    =item RETURN
1222    
1223    Returns the number of features of the specified type for the specified genome.
1224    
1225    =back
1226    
1227    =cut
1228    
1229    sub FeatureCount {
1230        # Get the parameters.
1231        my ($self, $genomeID, $type) = @_;
1232        # Compute the count.
1233        my $retVal = $self->GetCount(['HasFeature', 'Feature'],
1234                                    "HasFeature(from-link) = ? AND Feature(feature-type) = ?",
1235                                    [$genomeID, $type]);
1236        # Return the result.
1237        return $retVal;
1238    }
1239    
1240    =head3 GenomeAssignments
1241    
1242        my $fidHash = $sprout->GenomeAssignments($genomeID);
1243    
1244    Return a list of a genome's assigned features. The return hash will contain each
1245    assigned feature of the genome mapped to the text of its most recent functional
1246    assignment.
1247    
1248    =over 4
1249    
1250    =item genomeID
1251    
1252    ID of the genome whose functional assignments are desired.
1253    
1254    =item RETURN
1255    
1256    Returns a reference to a hash which maps each feature to its most recent
1257    functional assignment.
1258    
1259    =back
1260    
1261    =cut
1262    
1263    sub GenomeAssignments {
1264        # Get the parameters.
1265        my ($self, $genomeID) = @_;
1266        # Declare the return variable.
1267        my $retVal = {};
1268        # Query the genome's features.
1269        my $query = $self->Get(['HasFeature', 'Feature'], "HasFeature(from-link) = ?",
1270                               [$genomeID]);
1271        # Loop through the features.
1272        while (my $data = $query->Fetch) {
1273            # Get the feature ID and assignment.
1274            my ($fid, $assignment) = $data->Values(['Feature(id)', 'Feature(assignment)']);
1275            if ($assignment) {
1276                $retVal->{$fid} = $assignment;
1277            }
1278        }
1279        # Return the result.
1280        return $retVal;
1281    }
1282    
1283  =head3 ContigLength  =head3 ContigLength
1284    
1285  C<< my $length = $sprout->ContigLength($contigID); >>      my $length = $sprout->ContigLength($contigID);
1286    
1287  Compute the length of a contig.  Compute the length of a contig.
1288    
# Line 810  Line 1302 
1302  #: Return Type $;  #: Return Type $;
1303  sub ContigLength {  sub ContigLength {
1304          # Get the parameters.          # Get the parameters.
1305          my $self = shift @_;      my ($self, $contigID) = @_;
         my ($contigID) = @_;  
1306          # Get the contig's last sequence.          # Get the contig's last sequence.
1307          my $query = $self->Get(['IsMadeUpOf'],          my $query = $self->Get(['IsMadeUpOf'],
1308                  "IsMadeUpOf(from-link) = ? ORDER BY IsMadeUpOf(start-position) DESC",                  "IsMadeUpOf(from-link) = ? ORDER BY IsMadeUpOf(start-position) DESC",
# Line 822  Line 1313 
1313          # Set it from the sequence data, if any.          # Set it from the sequence data, if any.
1314          if ($sequence) {          if ($sequence) {
1315                  my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);                  my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);
1316                  $retVal = $start + $len;          $retVal = $start + $len - 1;
1317        }
1318        # Return the result.
1319        return $retVal;
1320    }
1321    
1322    =head3 ClusterPEGs
1323    
1324        my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs);
1325    
1326    Cluster the PEGs in a list according to the cluster coding scheme of the specified
1327    subsystem. In order for this to work properly, the subsystem object must have
1328    been used recently to retrieve the PEGs using the B<get_pegs_from_cell> or
1329    B<get_row> methods. This causes the cluster numbers to be pulled into the
1330    subsystem's color hash. If a PEG is not found in the color hash, it will not
1331    appear in the output sequence.
1332    
1333    =over 4
1334    
1335    =item sub
1336    
1337    Sprout subsystem object for the relevant subsystem, from the L</get_subsystem>
1338    method.
1339    
1340    =item pegs
1341    
1342    Reference to the list of PEGs to be clustered.
1343    
1344    =item RETURN
1345    
1346    Returns a list of the PEGs, grouped into smaller lists by cluster number.
1347    
1348    =back
1349    
1350    =cut
1351    #: Return Type $@@;
1352    sub ClusterPEGs {
1353        # Get the parameters.
1354        my ($self, $sub, $pegs) = @_;
1355        # Declare the return variable.
1356        my $retVal = [];
1357        # Loop through the PEGs, creating arrays for each cluster.
1358        for my $pegID (@{$pegs}) {
1359            my $clusterNumber = $sub->get_cluster_number($pegID);
1360            # Only proceed if the PEG is in a cluster.
1361            if ($clusterNumber >= 0) {
1362                # Push this PEG onto the sub-list for the specified cluster number.
1363                push @{$retVal->[$clusterNumber]}, $pegID;
1364            }
1365          }          }
1366          # Return the result.          # Return the result.
1367          return $retVal;          return $retVal;
# Line 830  Line 1369 
1369    
1370  =head3 GenesInRegion  =head3 GenesInRegion
1371    
1372  C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>      my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop);
1373    
1374  List the features which overlap a specified region in a contig.  List the features which overlap a specified region in a contig.
1375    
# Line 853  Line 1392 
1392  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
1393  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
1394  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
1395  the start and stop values.  the start and stop values. The first element (that is, the list of features) is sorted
1396    roughly by location.
1397    
1398  =back  =back
1399    
1400  =cut  =cut
1401  #: Return Type @;  
1402  sub GenesInRegion {  sub GenesInRegion {
1403          # Get the parameters.          # Get the parameters.
1404          my $self = shift @_;      my ($self, $contigID, $start, $stop) = @_;
1405          my ($contigID, $start, $stop) = @_;      # Get the maximum segment length.
1406        my $maximumSegmentLength = $self->MaxSegment;
1407        # Prime the values we'll use for the returned beginning and end.
1408        my @initialMinMax = ($self->ContigLength($contigID), 0);
1409        my ($min, $max) = @initialMinMax;
1410        # Get the overlapping features.
1411        my @featureObjects = $self->GeneDataInRegion($contigID, $start, $stop);
1412        # We'l use this hash to help us track the feature IDs and sort them. The key is the
1413        # feature ID and the value is a [$left,$right] pair indicating the maximum extent
1414        # of the feature's locations.
1415        my %featureMap = ();
1416        # Loop through them to do the begin/end analysis.
1417        for my $featureObject (@featureObjects) {
1418            # Get the feature's location string. This may contain multiple actual locations.
1419            my ($locations, $fid) = $featureObject->Values([qw(Feature(location-string) Feature(id))]);
1420            my @locationSegments = split /\s*,\s*/, $locations;
1421            # Loop through the locations.
1422            for my $locationSegment (@locationSegments) {
1423                # Construct an object for the location.
1424                my $locationObject = BasicLocation->new($locationSegment);
1425                # Merge the current segment's begin and end into the min and max.
1426                my ($left, $right) = ($locationObject->Left, $locationObject->Right);
1427                my ($beg, $end);
1428                if (exists $featureMap{$fid}) {
1429                    ($beg, $end) = @{$featureMap{$fid}};
1430                    $beg = $left if $left < $beg;
1431                    $end = $right if $right > $end;
1432                } else {
1433                    ($beg, $end) = ($left, $right);
1434                }
1435                $min = $beg if $beg < $min;
1436                $max = $end if $end > $max;
1437                # Store the feature's new extent back into the hash table.
1438                $featureMap{$fid} = [$beg, $end];
1439            }
1440        }
1441        # Now we must compute the list of the IDs for the features found. We start with a list
1442        # of midpoints / feature ID pairs. (It's not really a midpoint, it's twice the midpoint,
1443        # but the result of the sort will be the same.)
1444        my @list = map { [$featureMap{$_}->[0] + $featureMap{$_}->[1], $_] } keys %featureMap;
1445        # Now we sort by midpoint and yank out the feature IDs.
1446        my @retVal = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @list;
1447        # Return it along with the min and max.
1448        return (\@retVal, $min, $max);
1449    }
1450    
1451    =head3 GeneDataInRegion
1452    
1453        my @featureList = $sprout->GenesInRegion($contigID, $start, $stop);
1454    
1455    List the features which overlap a specified region in a contig.
1456    
1457    =over 4
1458    
1459    =item contigID
1460    
1461    ID of the contig containing the region of interest.
1462    
1463    =item start
1464    
1465    Offset of the first residue in the region of interest.
1466    
1467    =item stop
1468    
1469    Offset of the last residue in the region of interest.
1470    
1471    =item RETURN
1472    
1473    Returns a list of B<ERDBObjects> for the desired features. Each object will
1474    contain a B<Feature> record.
1475    
1476    =back
1477    
1478    =cut
1479    
1480    sub GeneDataInRegion {
1481        # Get the parameters.
1482        my ($self, $contigID, $start, $stop) = @_;
1483          # Get the maximum segment length.          # Get the maximum segment length.
1484          my $maximumSegmentLength = $self->MaxSegment;          my $maximumSegmentLength = $self->MaxSegment;
1485          # 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
1486          # duplicates easily.      # duplicates easily. The hash key will be the feature ID. The value will be the feature's
1487        # ERDBObject from the query.
1488          my %featuresFound = ();          my %featuresFound = ();
1489          # 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  
1490          # 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,
1491          # 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
1492          # 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 879  Line 1495 
1495          # Loop through the query parameters.          # Loop through the query parameters.
1496          for my $parms (values %queryParms) {          for my $parms (values %queryParms) {
1497                  # Create the query.                  # Create the query.
1498                  my $query = $self->Get(['IsLocatedIn'],          my $query = $self->Get([qw(Feature IsLocatedIn)],
1499                          "IsLocatedIn(to-link)= ? AND IsLocatedIn(dir) = ? AND IsLocatedIn(beg) >= ? AND IsLocatedIn(beg) <= ?",                          "IsLocatedIn(to-link)= ? AND IsLocatedIn(dir) = ? AND IsLocatedIn(beg) >= ? AND IsLocatedIn(beg) <= ?",
1500                          $parms);                          $parms);
1501                  # Loop through the feature segments found.                  # Loop through the feature segments found.
1502                  while (my $segment = $query->Fetch) {                  while (my $segment = $query->Fetch) {
1503                          # Get the data about this segment.                          # Get the data about this segment.
1504                          my ($featureID, $dir, $beg, $len) = $segment->Values(['IsLocatedIn(from-link)',              my ($featureID, $contig, $dir, $beg, $len) = $segment->Values([qw(IsLocatedIn(from-link)
1505                                  'IsLocatedIn(dir)', 'IsLocatedIn(beg)', 'IsLocatedIn(len)']);                  IsLocatedIn(to-link) IsLocatedIn(dir) IsLocatedIn(beg) IsLocatedIn(len))]);
1506                          # 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
1507                          # 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
1508                          # 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
1509                          # length.                          # length.
1510                          my ($found, $end) = (0, 0);              my $loc = BasicLocation->new($contig, $beg, $dir, $len);
1511                          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;  
                                 }  
                         }  
1512                          if ($found) {                          if ($found) {
1513                                  # Here we need to record the feature and update the minimum and maximum.                  # Save this feature in the result list.
1514                                  $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; }  
1515                          }                          }
1516                  }                  }
1517          }          }
1518          # Compute a list of the IDs for the features found.      # Return the ERDB objects for the features found.
1519          my @list = (sort (keys %featuresFound));      return values %featuresFound;
         # Return it along with the min and max.  
         return (\@list, $min, $max);  
1520  }  }
1521    
1522  =head3 FType  =head3 FType
1523    
1524  C<< my $ftype = $sprout->FType($featureID); >>      my $ftype = $sprout->FType($featureID);
1525    
1526  Return the type of a feature.  Return the type of a feature.
1527    
# Line 944  Line 1542 
1542  #: Return Type $;  #: Return Type $;
1543  sub FType {  sub FType {
1544          # Get the parameters.          # Get the parameters.
1545          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
1546          # Get the specified feature's type.          # Get the specified feature's type.
1547          my ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(feature-type)']);          my ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(feature-type)']);
1548          # Return the result.          # Return the result.
# Line 954  Line 1551 
1551    
1552  =head3 FeatureAnnotations  =head3 FeatureAnnotations
1553    
1554  C<< my @descriptors = $sprout->FeatureAnnotations($featureID); >>      my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag);
1555    
1556  Return the annotations of a feature.  Return the annotations of a feature.
1557    
# Line 964  Line 1561 
1561    
1562  ID of the feature whose annotations are desired.  ID of the feature whose annotations are desired.
1563    
1564    =item rawFlag
1565    
1566    If TRUE, the annotation timestamps will be returned in raw form; otherwise, they
1567    will be returned in human-readable form.
1568    
1569  =item RETURN  =item RETURN
1570    
1571  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.
1572    
1573  * B<featureID> ID of the relevant feature.  * B<featureID> ID of the relevant feature.
1574    
1575  * B<timeStamp> time the annotation was made, in user-friendly format.  * B<timeStamp> time the annotation was made.
1576    
1577  * B<user> ID of the user who made the annotation  * B<user> ID of the user who made the annotation
1578    
# Line 982  Line 1584 
1584  #: Return Type @%;  #: Return Type @%;
1585  sub FeatureAnnotations {  sub FeatureAnnotations {
1586          # Get the parameters.          # Get the parameters.
1587          my $self = shift @_;      my ($self, $featureID, $rawFlag) = @_;
         my ($featureID) = @_;  
1588          # 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.
1589          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1590                                                     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);                                                     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);
# Line 996  Line 1597 
1597                          $annotation->Values(['IsTargetOfAnnotation(from-link)',                          $annotation->Values(['IsTargetOfAnnotation(from-link)',
1598                                                                   'Annotation(time)', 'MadeAnnotation(from-link)',                                                                   'Annotation(time)', 'MadeAnnotation(from-link)',
1599                                                                   'Annotation(annotation)']);                                                                   'Annotation(annotation)']);
1600            # Convert the time, if necessary.
1601            if (! $rawFlag) {
1602                $timeStamp = FriendlyTimestamp($timeStamp);
1603            }
1604                  # Assemble them into a hash.                  # Assemble them into a hash.
1605          my $annotationHash = { featureID => $featureID,          my $annotationHash = { featureID => $featureID,
1606                                 timeStamp => FriendlyTimestamp($timeStamp),                                 timeStamp => $timeStamp,
1607                                                             user => $user, text => $text };                                                             user => $user, text => $text };
1608                  # Add it to the return list.                  # Add it to the return list.
1609                  push @retVal, $annotationHash;                  push @retVal, $annotationHash;
# Line 1009  Line 1614 
1614    
1615  =head3 AllFunctionsOf  =head3 AllFunctionsOf
1616    
1617  C<< my %functions = $sprout->AllFunctionsOf($featureID); >>      my %functions = $sprout->AllFunctionsOf($featureID);
1618    
1619  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
1620  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,
1621  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
1622  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,
1623  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.
1624  features only have a small number of annotations.  Finally, if a single user has multiple functional assignments, we will only keep the most
1625    recent one.
1626    
1627  =over 4  =over 4
1628    
# Line 1026  Line 1632 
1632    
1633  =item RETURN  =item RETURN
1634    
1635  Returns a hash mapping the functional assignment IDs to user IDs.  Returns a hash mapping the user IDs to functional assignment IDs.
1636    
1637  =back  =back
1638    
# Line 1034  Line 1640 
1640  #: Return Type %;  #: Return Type %;
1641  sub AllFunctionsOf {  sub AllFunctionsOf {
1642          # Get the parameters.          # Get the parameters.
1643          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
1644          # Get all of the feature's annotations.          # Get all of the feature's annotations.
1645          my @query = $self->GetFlat(['IsTargetOfAnnotation', 'Annotation'],      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1646                                                      "IsTargetOfAnnotation(from-link) = ?",                                                      "IsTargetOfAnnotation(from-link) = ?",
1647                                                          [$featureID], 'Annotation(annotation)');                              [$featureID], ['Annotation(time)', 'Annotation(annotation)',
1648                                               'MadeAnnotation(from-link)']);
1649          # Declare the return hash.          # Declare the return hash.
1650          my %retVal;          my %retVal;
1651        # Now we sort the assignments by timestamp in reverse.
1652        my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;
1653          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1654          for my $text (@query) {      for my $annotation (@sortedQuery) {
1655            # Get the annotation fields.
1656            my ($timeStamp, $text, $user) = @{$annotation};
1657                  # Check to see if this is a functional assignment.                  # Check to see if this is a functional assignment.
1658                  my ($user, $function) = ParseAssignment($text);          my ($actualUser, $function) = _ParseAssignment($user, $text);
1659                  if ($user) {          if ($actualUser && ! exists $retVal{$actualUser}) {
1660                          # Here it is, so stuff it in the return hash.              # Here it is a functional assignment and there has been no
1661                          $retVal{$function} = $user;              # previous assignment for this user, so we stuff it in the
1662                # return hash.
1663                $retVal{$actualUser} = $function;
1664                  }                  }
1665          }          }
1666          # Return the hash of assignments found.          # Return the hash of assignments found.
# Line 1057  Line 1669 
1669    
1670  =head3 FunctionOf  =head3 FunctionOf
1671    
1672  C<< my $functionText = $sprout->FunctionOf($featureID, $userID); >>      my $functionText = $sprout->FunctionOf($featureID, $userID);
1673    
1674  Return the most recently-determined functional assignment of a particular feature.  Return the most recently-determined functional assignment of a particular feature.
1675    
1676  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
1677  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
1678  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.  
1679    
1680  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
1681  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
1682  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
1683  is trusted.  is trusted.
1684    
1685  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.
1686  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.  
1687    
1688  =over 4  =over 4
1689    
# Line 1085  Line 1693 
1693    
1694  =item userID (optional)  =item userID (optional)
1695    
1696  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
1697  C<FIG> assignment will be returned.  functional assignment in the B<Feature> table will be returned.
1698    
1699  =item RETURN  =item RETURN
1700    
# Line 1098  Line 1706 
1706  #: Return Type $;  #: Return Type $;
1707  sub FunctionOf {  sub FunctionOf {
1708          # Get the parameters.          # Get the parameters.
1709          my $self = shift @_;      my ($self, $featureID, $userID) = @_;
         my ($featureID, $userID) = @_;  
1710      # Declare the return value.      # Declare the return value.
1711      my $retVal;      my $retVal;
1712      # Determine the ID type.      # Find a FIG ID for this feature.
1713      if ($featureID =~ m/^fig\|/) {      my ($fid) = $self->FeaturesByAlias($featureID);
1714          # Here we have a FIG feature ID. We must build the list of trusted      # Only proceed if we have an ID.
1715          # users.      if ($fid) {
1716            # Here we have a FIG feature ID.
1717            if (!$userID) {
1718                # Use the primary assignment.
1719                ($retVal) = $self->GetEntityValues('Feature', $fid, ['Feature(assignment)']);
1720            } else {
1721                # We must build the list of trusted users.
1722          my %trusteeTable = ();          my %trusteeTable = ();
1723          # Check the user ID.          # Check the user ID.
1724          if (!$userID) {          if (!$userID) {
# Line 1127  Line 1740 
1740              }              }
1741          }          }
1742          # 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.
1743          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation'],              my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1744                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1745                                 [$featureID]);                                     [$fid]);
1746          my $timeSelected = 0;          my $timeSelected = 0;
1747          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1748          while (my $annotation = $query->Fetch()) {          while (my $annotation = $query->Fetch()) {
1749              # Get the annotation text.              # Get the annotation text.
1750              my ($text, $time) = $annotation->Values(['Annotation(annotation)','Annotation(time)']);                  my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1751                                                             'Annotation(time)', 'MadeAnnotation(from-link)']);
1752              # 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.
1753              my ($user, $type, $function) = split(/\n/, $text);                  my ($actualUser, $function) = _ParseAssignment($user, $text);
1754              if ($type =~ m/^set $user function to$/i) {                  Trace("Assignment user is $actualUser, text is $function.") if T(4);
1755                    if ($actualUser) {
1756                  # Here it is a functional assignment. Check the time and the user                  # Here it is a functional assignment. Check the time and the user
1757                  # name. The time must be recent and the user must be trusted.                  # name. The time must be recent and the user must be trusted.
1758                  if ((exists $trusteeTable{$user}) && ($time > $timeSelected)) {                      if ((exists $trusteeTable{$actualUser}) && ($time > $timeSelected)) {
1759                      $retVal = $function;                      $retVal = $function;
1760                      $timeSelected = $time;                      $timeSelected = $time;
1761                  }                  }
1762              }              }
1763          }          }
1764      } 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)']);  
1765      }      }
1766          # Return the assignment found.          # Return the assignment found.
1767          return $retVal;          return $retVal;
1768  }  }
1769    
1770    =head3 FunctionsOf
1771    
1772        my @functionList = $sprout->FunctionOf($featureID, $userID);
1773    
1774    Return the functional assignments of a particular feature.
1775    
1776    The functional assignment is handled differently depending on the type of feature. If
1777    the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional
1778    assignment is a type of annotation. The format of an assignment is described in
1779    L</ParseAssignment>. Its worth noting that we cannot filter on the content of the
1780    annotation itself because it's a text field; however, this is not a big problem because
1781    most features only have a small number of annotations.
1782    
1783    =over 4
1784    
1785    =item featureID
1786    
1787    ID of the feature whose functional assignments are desired.
1788    
1789    =item RETURN
1790    
1791    Returns a list of 2-tuples, each consisting of a user ID and the text of an assignment by
1792    that user.
1793    
1794    =back
1795    
1796    =cut
1797    #: Return Type @@;
1798    sub FunctionsOf {
1799        # Get the parameters.
1800        my ($self, $featureID) = @_;
1801        # Declare the return value.
1802        my @retVal = ();
1803        # Convert to a FIG ID.
1804        my ($fid) = $self->FeaturesByAlias($featureID);
1805        # Only proceed if we found one.
1806        if ($fid) {
1807            # Here we have a FIG feature ID. We must build the list of trusted
1808            # users.
1809            my %trusteeTable = ();
1810            # Build a query for all of the feature's annotations, sorted by date.
1811            my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1812                                   "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1813                                   [$fid]);
1814            my $timeSelected = 0;
1815            # Loop until we run out of annotations.
1816            while (my $annotation = $query->Fetch()) {
1817                # Get the annotation text.
1818                my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1819                                                                'Annotation(time)',
1820                                                                'MadeAnnotation(user)']);
1821                # Check to see if this is a functional assignment for a trusted user.
1822                my ($actualUser, $function) = _ParseAssignment($user, $text);
1823                if ($actualUser) {
1824                    # Here it is a functional assignment.
1825                    push @retVal, [$actualUser, $function];
1826                }
1827            }
1828        }
1829        # Return the assignments found.
1830        return @retVal;
1831    }
1832    
1833  =head3 BBHList  =head3 BBHList
1834    
1835  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>      my $bbhHash = $sprout->BBHList($genomeID, \@featureList);
1836    
1837  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
1838  on a specified target genome.  on a specified target genome.
# Line 1175  Line 1849 
1849    
1850  =item RETURN  =item RETURN
1851    
1852  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
1853  their best hits.  on the target genome.
1854    
1855  =back  =back
1856    
# Line 1184  Line 1858 
1858  #: Return Type %;  #: Return Type %;
1859  sub BBHList {  sub BBHList {
1860          # Get the parameters.          # Get the parameters.
1861          my $self = shift @_;      my ($self, $genomeID, $featureList) = @_;
         my ($genomeID, $featureList) = @_;  
1862          # Create the return structure.          # Create the return structure.
1863          my %retVal = ();          my %retVal = ();
1864          # Loop through the incoming features.          # Loop through the incoming features.
1865          for my $featureID (@{$featureList}) {          for my $featureID (@{$featureList}) {
1866                  # Create a query to get the feature's best hit.          # Ask the server for the feature's best hit.
1867                  my $query = $self->Get(['IsBidirectionalBestHitOf'],          my $bbhData = FIGRules::BBHData($featureID);
1868                                                             "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?",          # Peel off the BBHs found.
1869                                                             [$featureID, $genomeID]);          my @found = ();
1870                  # Look for the best hit.          for my $bbh (@$bbhData) {
1871                  my $bbh = $query->Fetch;              my $fid = $bbh->[0];
1872                  if ($bbh) {              my $bbGenome = $self->GenomeOf($fid);
1873                          my ($targetFeature) = $bbh->Value('IsBidirectionalBestHitOf(to-link)');              if ($bbGenome eq $genomeID) {
1874                          $retVal{$featureID} = $targetFeature;                  push @found, $fid;
1875                }
1876                  }                  }
1877            $retVal{$featureID} = \@found;
1878          }          }
1879          # Return the mapping.          # Return the mapping.
1880          return \%retVal;          return \%retVal;
1881  }  }
1882    
1883  =head3 FeatureAliases  =head3 SimList
1884    
1885  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>      my %similarities = $sprout->SimList($featureID, $count);
1886    
1887  Return a list of the aliases for a specified feature.  Return a list of the similarities to the specified feature.
1888    
1889    This method just returns the bidirectional best hits for performance reasons.
1890    
1891  =over 4  =over 4
1892    
1893  =item featureID  =item featureID
1894    
1895  ID of the feature whose aliases are desired.  ID of the feature whose similarities are desired.
1896    
1897  =item RETURN  =item count
1898    
1899  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.  
1900    
1901  =back  =back
1902    
1903  =cut  =cut
1904  #: Return Type @;  #: Return Type %;
1905  sub FeatureAliases {  sub SimList {
1906          # Get the parameters.          # Get the parameters.
1907          my $self = shift @_;      my ($self, $featureID, $count) = @_;
1908          my ($featureID) = @_;      # Ask for the best hits.
1909          # Get the desired feature's aliases      my $lists = FIGRules::BBHData($featureID);
1910          my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(alias)']);      # Create the return value.
1911        my %retVal = ();
1912        for my $tuple (@$lists) {
1913            $retVal{$tuple->[0]} = $tuple->[1];
1914        }
1915          # Return the result.          # Return the result.
1916          return @retVal;      return %retVal;
1917  }  }
1918    
1919  =head3 GenomeOf  =head3 IsComplete
1920    
1921  C<< my $genomeID = $sprout->GenomeOf($featureID); >>      my $flag = $sprout->IsComplete($genomeID);
1922    
1923  Return the genome that contains a specified feature.  Return TRUE if the specified genome is complete, else FALSE.
1924    
1925  =over 4  =over 4
1926    
1927  =item featureID  =item genomeID
1928    
1929  ID of the feature whose genome is desired.  ID of the genome whose completeness status is desired.
1930    
1931  =item RETURN  =item RETURN
1932    
1933  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
1934  an undefined value.  not found.
1935    
1936  =back  =back
1937    
1938  =cut  =cut
1939  #: Return Type $;  #: Return Type $;
1940  sub GenomeOf {  sub IsComplete {
1941          # Get the parameters.          # Get the parameters.
1942          my $self = shift @_;      my ($self, $genomeID) = @_;
1943          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.  
1944          my $retVal;          my $retVal;
1945          # Get the genome ID.      # Get the genome's data.
1946          if (my $relationship = $query->Fetch()) {      my $genomeData = $self->_GenomeData($genomeID);
1947                  ($retVal) = $relationship->Value('HasContig(from-link)');      # Only proceed if it exists.
1948        if (defined $genomeData) {
1949            # The genome exists, so get the completeness flag.
1950            $retVal = $genomeData->PrimaryValue('Genome(complete)');
1951          }          }
1952          # Return the value found.      # Return the result.
1953          return $retVal;          return $retVal;
1954  }  }
1955    
1956  =head3 CoupledFeatures  =head3 FeatureAliases
1957    
1958  C<< my %coupleHash = $sprout->CoupledFeatures($featureID); >>      my @aliasList = $sprout->FeatureAliases($featureID);
1959    
1960  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.  
1961    
1962  =over 4  =over 4
1963    
1964  =item featureID  =item featureID
1965    
1966  ID of the feature whose functionally-coupled brethren are desired.  ID of the feature whose aliases are desired.
1967    
1968  =item RETURN  =item RETURN
1969    
1970  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
1971    return an empty list.
1972    
1973  =back  =back
1974    
1975  =cut  =cut
1976  #: Return Type %;  #: Return Type @;
1977  sub CoupledFeatures {  sub FeatureAliases {
1978          # Get the parameters.      # Get the parameters.
1979          my $self = shift @_;      my ($self, $featureID) = @_;
1980          my ($featureID) = @_;      # Get the desired feature's aliases
1981          # Create a query to retrieve the functionally-coupled features. Note that we depend on the      my @retVal = $self->GetFlat(['IsAliasOf'], "IsAliasOf(to-link) = ?", [$featureID], 'IsAliasOf(from-link)');
1982          # fact that the functional coupling is physically paired. If (A,B) is in the database, then      # Return the result.
1983          # (B,A) will also be found.      return @retVal;
1984          my $query = $self->Get(['IsClusteredOnChromosomeWith'],  }
1985                                                     "IsClusteredOnChromosomeWith(from-link) = ?", [$featureID]);  
1986          # This value will be set to TRUE if we find at least one coupled feature.  =head3 GenomeOf
1987          my $found = 0;  
1988          # Create the return hash.      my $genomeID = $sprout->GenomeOf($featureID);
1989    
1990    Return the genome that contains a specified feature or contig.
1991    
1992    =over 4
1993    
1994    =item featureID
1995    
1996    ID of the feature or contig whose genome is desired.
1997    
1998    =item RETURN
1999    
2000    Returns the ID of the genome for the specified feature or contig. If the feature or contig is not
2001    found, returns an undefined value.
2002    
2003    =back
2004    
2005    =cut
2006    #: Return Type $;
2007    sub GenomeOf {
2008        # Get the parameters.
2009        my ($self, $featureID) = @_;
2010        # Declare the return value.
2011        my $retVal;
2012        # Parse the genome ID from the feature ID.
2013        if ($featureID =~ /^fig\|(\d+\.\d+)/) {
2014            $retVal = $1;
2015        } else {
2016            # Find the feature by alias.
2017            my ($realFeatureID) = $self->FeaturesByAlias($featureID);
2018            if ($realFeatureID && $realFeatureID =~ /^fig\|(\d+\.\d+)/) {
2019                $retVal = $1;
2020            }
2021        }
2022        # Return the value found.
2023        return $retVal;
2024    }
2025    
2026    =head3 CoupledFeatures
2027    
2028        my %coupleHash = $sprout->CoupledFeatures($featureID);
2029    
2030    Return the features functionally coupled with a specified feature. Features are considered
2031    functionally coupled if they tend to be clustered on the same chromosome.
2032    
2033    =over 4
2034    
2035    =item featureID
2036    
2037    ID of the feature whose functionally-coupled brethren are desired.
2038    
2039    =item RETURN
2040    
2041    A hash mapping the functionally-coupled feature IDs to the coupling score.
2042    
2043    =back
2044    
2045    =cut
2046    #: Return Type %;
2047    sub CoupledFeatures {
2048        # Get the parameters.
2049        my ($self, $featureID) = @_;
2050        # Ask the coupling server for the data.
2051        Trace("Looking for features coupled to $featureID.") if T(coupling => 3);
2052        my @rawPairs = FIGRules::NetCouplingData('coupled_to', id1 => $featureID);
2053        Trace(scalar(@rawPairs) . " couplings returned.") if T(coupling => 3);
2054        # Form them into a hash.
2055          my %retVal = ();          my %retVal = ();
2056          # Retrieve the relationship records and store them in the hash.      for my $pair (@rawPairs) {
2057          while (my $clustering = $query->Fetch()) {          # Get the feature ID and score.
2058                  my ($otherFeatureID, $score) = $clustering->Values(['IsClusteredOnChromosomeWith(to-link)',          my ($featureID2, $score) = @{$pair};
2059                                                                      'IsClusteredOnChromosomeWith(score)']);          # Only proceed if the feature is in NMPDR.
2060                  $retVal{$otherFeatureID} = $score;          if ($self->_CheckFeature($featureID2)) {
2061                  $found = 1;              $retVal{$featureID2} = $score;
2062          }          }
         # 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;  
2063      }      }
2064          # Return the hash.          # Return the hash.
2065          return %retVal;          return %retVal;
2066  }  }
2067    
2068  =head3 GetEntityTypes  =head3 CouplingEvidence
2069    
2070        my @evidence = $sprout->CouplingEvidence($peg1, $peg2);
2071    
2072    Return the evidence for a functional coupling.
2073    
2074    A pair of features is considered evidence of a coupling between two other
2075    features if they occur close together on a contig and both are similar to
2076    the coupled features. So, if B<A1> and B<A2> are close together on a contig,
2077    B<B1> and B<B2> are considered evidence for the coupling if (1) B<B1> and
2078    B<B2> are close together, (2) B<B1> is similar to B<A1>, and (3) B<B2> is
2079    similar to B<A2>.
2080    
2081    The score of a coupling is determined by the number of pieces of evidence
2082    that are considered I<representative>. If several evidence items belong to
2083    a group of genomes that are close to each other, only one of those items
2084    is considered representative. The other evidence items are presumed to be
2085    there because of the relationship between the genomes rather than because
2086    the two proteins generated by the features have a related functionality.
2087    
2088    Each evidence item is returned as a three-tuple in the form C<[>I<$peg1a>C<,>
2089    I<$peg2a>C<,> I<$rep>C<]>, where I<$peg1a> is similar to I<$peg1>, I<$peg2a>
2090    is similar to I<$peg2>, and I<$rep> is TRUE if the evidence is representative
2091    and FALSE otherwise.
2092    
2093    =over 4
2094    
2095    =item peg1
2096    
2097    ID of the feature of interest.
2098    
2099    =item peg2
2100    
2101    ID of a feature functionally coupled to the feature of interest.
2102    
2103    =item RETURN
2104    
2105    Returns a list of 3-tuples. Each tuple consists of a feature similar to the feature
2106    of interest, a feature similar to the functionally coupled feature, and a flag
2107    that is TRUE for a representative piece of evidence and FALSE otherwise.
2108    
2109    =back
2110    
2111    =cut
2112    #: Return Type @@;
2113    sub CouplingEvidence {
2114        # Get the parameters.
2115        my ($self, $peg1, $peg2) = @_;
2116        # Declare the return variable.
2117        my @retVal = ();
2118        # Get the coupling and evidence data.
2119        my @rawData = FIGRules::NetCouplingData('coupling_evidence', id1 => $peg1, id2 => $peg2);
2120        # Loop through the raw data, saving the ones that are in NMPDR genomes.
2121        for my $rawTuple (@rawData) {
2122            if ($self->_CheckFeature($rawTuple->[0]) && $self->_CheckFeature($rawTuple->[1])) {
2123                push @retVal, $rawTuple;
2124            }
2125        }
2126        # Return the result.
2127        return @retVal;
2128    }
2129    
2130    =head3 GetSynonymGroup
2131    
2132        my $id = $sprout->GetSynonymGroup($fid);
2133    
2134    Return the synonym group name for the specified feature.
2135    
2136    =over 4
2137    
2138    =item fid
2139    
2140    ID of the feature whose synonym group is desired.
2141    
2142    =item RETURN
2143    
2144    The name of the synonym group to which the feature belongs. If the feature does
2145    not belong to a synonym group, the feature ID itself is returned.
2146    
2147    =back
2148    
2149    =cut
2150    
2151    sub GetSynonymGroup {
2152        # Get the parameters.
2153        my ($self, $fid) = @_;
2154        # Declare the return variable.
2155        my $retVal;
2156        # Find the synonym group.
2157        my @groups = $self->GetFlat(['IsSynonymGroupFor'], "IsSynonymGroupFor(to-link) = ?",
2158                                       [$fid], 'IsSynonymGroupFor(from-link)');
2159        # Check to see if we found anything.
2160        if (@groups) {
2161            $retVal = $groups[0];
2162        } else {
2163            $retVal = $fid;
2164        }
2165        # Return the result.
2166        return $retVal;
2167    }
2168    
2169    =head3 GetBoundaries
2170    
2171        my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList);
2172    
2173  C<< my @entityList = $sprout->GetEntityTypes(); >>  Determine the begin and end boundaries for the locations in a list. All of the
2174    locations must belong to the same contig and have mostly the same direction in
2175    order for this method to produce a meaningful result. The resulting
2176    begin/end pair will contain all of the bases in any of the locations.
2177    
2178  Return the list of supported entity types.  =over 4
2179    
2180    =item locList
2181    
2182    List of locations to process.
2183    
2184    =item RETURN
2185    
2186    Returns a 3-tuple consisting of the contig ID, the beginning boundary,
2187    and the ending boundary. The beginning boundary will be left of the
2188    end for mostly-forward locations and right of the end for mostly-backward
2189    locations.
2190    
2191    =back
2192    
2193  =cut  =cut
2194  #: Return Type @;  
2195  sub GetEntityTypes {  sub GetBoundaries {
2196          # Get the parameters.          # Get the parameters.
2197          my $self = shift @_;      my ($self, @locList) = @_;
2198          # Get the underlying database object.      # Set up the counters used to determine the most popular direction.
2199          my $erdb = $self->{_erdb};      my %counts = ( '+' => 0, '-' => 0 );
2200          # Get its entity type list.      # Get the last location and parse it.
2201          my @retVal = $erdb->GetEntityTypes();      my $locObject = BasicLocation->new(pop @locList);
2202        # Prime the loop with its data.
2203        my ($contig, $beg, $end) = ($locObject->Contig, $locObject->Left, $locObject->Right);
2204        # Count its direction.
2205        $counts{$locObject->Dir}++;
2206        # Loop through the remaining locations. Note that in most situations, this loop
2207        # will not iterate at all, because most of the time we will be dealing with a
2208        # singleton list.
2209        for my $loc (@locList) {
2210            # Create a location object.
2211            my $locObject = BasicLocation->new($loc);
2212            # Count the direction.
2213            $counts{$locObject->Dir}++;
2214            # Get the left end and the right end.
2215            my $left = $locObject->Left;
2216            my $right = $locObject->Right;
2217            # Merge them into the return variables.
2218            if ($left < $beg) {
2219                $beg = $left;
2220            }
2221            if ($right > $end) {
2222                $end = $right;
2223            }
2224        }
2225        # If the most common direction is reverse, flip the begin and end markers.
2226        if ($counts{'-'} > $counts{'+'}) {
2227            ($beg, $end) = ($end, $beg);
2228        }
2229        # Return the result.
2230        return ($contig, $beg, $end);
2231  }  }
2232    
2233  =head3 ReadFasta  =head3 ReadFasta
2234    
2235  C<< my %sequenceData = Sprout::ReadFasta($fileName, $prefix); >>      my %sequenceData = Sprout::ReadFasta($fileName, $prefix);
2236    
2237  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
2238  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 1384  Line 2274 
2274                  if ($line =~ m/^>\s*(.+?)(\s|\n)/) {                  if ($line =~ m/^>\s*(.+?)(\s|\n)/) {
2275                          # 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.
2276                          if ($id) {                          if ($id) {
2277                                  $retVal{$id} = $sequence;                  $retVal{$id} = lc $sequence;
2278                          }                          }
2279                          # Clear the sequence accumulator and save the new ID.                          # Clear the sequence accumulator and save the new ID.
2280                          ($id, $sequence) = ("$prefix$1", "");                          ($id, $sequence) = ("$prefix$1", "");
2281                  } else {                  } else {
2282                          # 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.
2283                          # First, we get the actual data out.              # First, we get the actual data out. Note that we normalize to lower
2284                # case.
2285                          $line =~ /^\s*(.*?)(\s|\n)/;                          $line =~ /^\s*(.*?)(\s|\n)/;
2286                          $sequence .= $1;                          $sequence .= $1;
2287                  }                  }
2288          }          }
2289          # Flush out the last sequence (if any).          # Flush out the last sequence (if any).
2290          if ($sequence) {          if ($sequence) {
2291                  $retVal {$id} = $sequence;          $retVal{$id} = lc $sequence;
2292          }          }
2293        # Close the file.
2294        close FASTAFILE;
2295          # Return the hash constructed from the file.          # Return the hash constructed from the file.
2296          return %retVal;          return %retVal;
2297  }  }
2298    
2299  =head3 FormatLocations  =head3 FormatLocations
2300    
2301  C<< my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat); >>      my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat);
2302    
2303  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
2304  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
2305  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,
2306    it will not be changed; otherwise, it will be converted. This method can also be used to
2307    perform the reverse task-- insuring that all the locations are in the old format.
2308    
2309  =over 4  =over 4
2310    
# Line 1436  Line 2331 
2331  #: Return Type @;  #: Return Type @;
2332  sub FormatLocations {  sub FormatLocations {
2333          # Get the parameters.          # Get the parameters.
2334          my $self = shift @_;      my ($self, $prefix, $locations, $oldFormat) = @_;
         my ($prefix, $locations, $oldFormat) = @_;  
2335          # Create the return list.          # Create the return list.
2336          my @retVal = ();          my @retVal = ();
2337          # Check to see if any locations were passed in.          # Check to see if any locations were passed in.
2338          if ($locations eq '') {          if ($locations eq '') {
2339              confess "No locations specified.";          Confess("No locations specified.");
2340          } else {          } else {
2341                  # Loop through the locations, converting them to the new format.                  # Loop through the locations, converting them to the new format.
2342                  for my $location (@{$locations}) {                  for my $location (@{$locations}) {
# Line 1469  Line 2363 
2363    
2364  =head3 DumpData  =head3 DumpData
2365    
2366  C<< $sprout->DumpData(); >>      $sprout->DumpData();
2367    
2368  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.
2369    
# Line 1477  Line 2371 
2371    
2372  sub DumpData {  sub DumpData {
2373          # Get the parameters.          # Get the parameters.
2374          my $self = shift @_;      my ($self) = @_;
2375          # Get the data directory name.          # Get the data directory name.
2376          my $outputDirectory = $self->{_options}->{dataDir};          my $outputDirectory = $self->{_options}->{dataDir};
2377          # Dump the relations.          # Dump the relations.
2378          $self->{_erdb}->DumpRelations($outputDirectory);      $self->DumpRelations($outputDirectory);
2379  }  }
2380    
2381  =head3 XMLFileName  =head3 XMLFileName
2382    
2383  C<< my $fileName = $sprout->XMLFileName(); >>      my $fileName = $sprout->XMLFileName();
2384    
2385  Return the name of this database's XML definition file.  Return the name of this database's XML definition file.
2386    
2387  =cut  =cut
2388  #: Return Type $;  #: Return Type $;
2389  sub XMLFileName {  sub XMLFileName {
2390          my $self = shift @_;      my ($self) = @_;
2391          return $self->{_xmlName};          return $self->{_xmlName};
2392  }  }
2393    
2394    =head3 GetGenomeNameData
2395    
2396        my ($genus, $species, $strain) = $sprout->GenomeNameData($genomeID);
2397    
2398    Return the genus, species, and unique characterization for a genome. This
2399    is similar to L</GenusSpecies>, with the exception that it returns the
2400    values in three seperate fields.
2401    
2402    =over 4
2403    
2404    =item genomeID
2405    
2406    ID of the genome whose name data is desired.
2407    
2408    =item RETURN
2409    
2410    Returns a three-element list, consisting of the genus, species, and strain
2411    of the specified genome. If the genome is not found, an error occurs.
2412    
2413    =back
2414    
2415    =cut
2416    
2417    sub GetGenomeNameData {
2418        # Get the parameters.
2419        my ($self, $genomeID) = @_;
2420        # Declare the return variables.
2421        my ($genus, $species, $strain);
2422        # Get the genome's data.
2423        my $genomeData = $self->_GenomeData($genomeID);
2424        # Only proceed if the genome exists.
2425        if (defined $genomeData) {
2426            # Get the desired values.
2427            ($genus, $species, $strain) = $genomeData->Values(['Genome(genus)',
2428                                                               'Genome(species)',
2429                                                               'Genome(unique-characterization)']);
2430        } else {
2431            # Throw an error because they were not found.
2432            Confess("Genome $genomeID not found in database.");
2433        }
2434        # Return the results.
2435        return ($genus, $species, $strain);
2436    }
2437    
2438    =head3 GetGenomeByNameData
2439    
2440        my @genomes = $sprout->GetGenomeByNameData($genus, $species, $strain);
2441    
2442    Return a list of the IDs of the genomes with the specified genus,
2443    species, and strain. In almost every case, there will be either zero or
2444    one IDs returned; however, two or more IDs could be returned if there are
2445    multiple versions of the genome in the database.
2446    
2447    =over 4
2448    
2449    =item genus
2450    
2451    Genus of the desired genome.
2452    
2453    =item species
2454    
2455    Species of the desired genome.
2456    
2457    =item strain
2458    
2459    Strain (unique characterization) of the desired genome. This may be an empty
2460    string, in which case it is presumed that the desired genome has no strain
2461    specified.
2462    
2463    =item RETURN
2464    
2465    Returns a list of the IDs of the genomes having the specified genus, species, and
2466    strain.
2467    
2468    =back
2469    
2470    =cut
2471    
2472    sub GetGenomeByNameData {
2473        # Get the parameters.
2474        my ($self, $genus, $species, $strain) = @_;
2475        # Try to find the genomes.
2476        my @retVal = $self->GetFlat(['Genome'], "Genome(genus) = ? AND Genome(species) = ? AND Genome(unique-characterization) = ?",
2477                                    [$genus, $species, $strain], 'Genome(id)');
2478        # Return the result.
2479        return @retVal;
2480    }
2481    
2482  =head3 Insert  =head3 Insert
2483    
2484  C<< $sprout->Insert($objectType, \%fieldHash); >>      $sprout->Insert($objectType, \%fieldHash);
2485    
2486  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
2487  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 1508  Line 2490 
2490  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
2491  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>.
2492    
2493  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']});
2494    
2495  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
2496  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>.
2497    
2498  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'});
2499    
2500  =over 4  =over 4
2501    
# Line 1531  Line 2513 
2513  #: Return Type ;  #: Return Type ;
2514  sub Insert {  sub Insert {
2515          # Get the parameters.          # Get the parameters.
2516          my $self = shift @_;      my ($self, $objectType, $fieldHash) = @_;
         my ($objectType, $fieldHash) = @_;  
2517          # Call the underlying method.          # Call the underlying method.
2518          $self->{_erdb}->InsertObject($objectType, $fieldHash);      $self->InsertObject($objectType, $fieldHash);
2519  }  }
2520    
2521  =head3 Annotate  =head3 Annotate
2522    
2523  C<< my $ok = $sprout->Annotate($fid, $timestamp, $user, $text); >>      my $ok = $sprout->Annotate($fid, $timestamp, $user, $text);
2524    
2525  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
2526  specified feature and user.  specified feature and user.
# Line 1573  Line 2554 
2554  #: Return Type $;  #: Return Type $;
2555  sub Annotate {  sub Annotate {
2556          # Get the parameters.          # Get the parameters.
2557          my $self = shift @_;      my ($self, $fid, $timestamp, $user, $text) = @_;
         my ($fid, $timestamp, $user, $text) = @_;  
2558          # Create the annotation ID.          # Create the annotation ID.
2559          my $aid = "$fid:$timestamp";          my $aid = "$fid:$timestamp";
2560          # Insert the Annotation object.          # Insert the Annotation object.
# Line 1594  Line 2574 
2574    
2575  =head3 AssignFunction  =head3 AssignFunction
2576    
2577  C<< my $ok = $sprout->AssignFunction($featureID, $user, $function); >>      my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser);
2578    
2579  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
2580  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.  
2581    
2582  =over 4  =over 4
2583    
# Line 1608  Line 2587 
2587    
2588  =item user  =item user
2589    
2590  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>.
2591    
2592  =item function  =item function
2593    
2594  Text of the function being assigned.  Text of the function being assigned.
2595    
2596    =item assigningUser (optional)
2597    
2598    Name of the individual user making the assignment. If omitted, defaults to the user group.
2599    
2600  =item RETURN  =item RETURN
2601    
2602  Returns 1 if successful, 0 if an error occurred.  Returns 1 if successful, 0 if an error occurred.
# Line 1624  Line 2607 
2607  #: Return Type $;  #: Return Type $;
2608  sub AssignFunction {  sub AssignFunction {
2609          # Get the parameters.          # Get the parameters.
2610          my $self = shift @_;      my ($self, $featureID, $user, $function, $assigningUser) = @_;
2611          my ($featureID, $user, $function) = @_;      # Default the assigning user.
2612        if (! $assigningUser) {
2613            $assigningUser = $user;
2614        }
2615          # Create an annotation string from the parameters.          # Create an annotation string from the parameters.
2616          my $annotationText = "$user\nset $user function to\n$function";      my $annotationText = "$assigningUser\nset $user function to\n$function";
2617          # Get the current time.          # Get the current time.
2618          my $now = time;          my $now = time;
2619          # Declare the return variable.          # Declare the return variable.
# Line 1648  Line 2634 
2634    
2635  =head3 FeaturesByAlias  =head3 FeaturesByAlias
2636    
2637  C<< my @features = $sprout->FeaturesByAlias($alias); >>      my @features = $sprout->FeaturesByAlias($alias);
2638    
2639  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
2640  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 1672  Line 2658 
2658  #: Return Type @;  #: Return Type @;
2659  sub FeaturesByAlias {  sub FeaturesByAlias {
2660          # Get the parameters.          # Get the parameters.
2661          my $self = shift @_;      my ($self, $alias) = @_;
         my ($alias) = @_;  
2662          # Declare the return variable.          # Declare the return variable.
2663          my @retVal = ();          my @retVal = ();
2664          # Parse the alias.          # Parse the alias.
# Line 1683  Line 2668 
2668                  push @retVal, $mappedAlias;                  push @retVal, $mappedAlias;
2669          } else {          } else {
2670                  # 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.
2671                  @retVal = $self->GetFlat(['Feature'], 'Feature(alias) = ?', [$mappedAlias], 'Feature(id)');          @retVal = $self->GetFlat(['IsAliasOf'], 'IsAliasOf(from-link) = ?', [$mappedAlias], 'IsAliasOf(to-link)');
2672          }          }
2673          # Return the result.          # Return the result.
2674          return @retVal;          return @retVal;
2675  }  }
2676    
 =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;  
 }  
   
2677  =head3 FeatureTranslation  =head3 FeatureTranslation
2678    
2679  C<< my $translation = $sprout->FeatureTranslation($featureID); >>      my $translation = $sprout->FeatureTranslation($featureID);
2680    
2681  Return the translation of a feature.  Return the translation of a feature.
2682    
# Line 1746  Line 2696 
2696  #: Return Type $;  #: Return Type $;
2697  sub FeatureTranslation {  sub FeatureTranslation {
2698          # Get the parameters.          # Get the parameters.
2699          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
2700          # Get the specified feature's translation.          # Get the specified feature's translation.
2701          my ($retVal) = $self->GetEntityValues("Feature", $featureID, ['Feature(translation)']);          my ($retVal) = $self->GetEntityValues("Feature", $featureID, ['Feature(translation)']);
2702          return $retVal;          return $retVal;
# Line 1755  Line 2704 
2704    
2705  =head3 Taxonomy  =head3 Taxonomy
2706    
2707  C<< my @taxonomyList = $sprout->Taxonomy($genome); >>      my @taxonomyList = $sprout->Taxonomy($genome);
2708    
2709  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
2710  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>,
2711  or C<Eukaryote>) to sub-species. For example,  or C<Eukaryote>) to sub-species. For example,
2712    
2713  C<< (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12) >>      (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12)
2714    
2715  =over 4  =over 4
2716    
# Line 1779  Line 2728 
2728  #: Return Type @;  #: Return Type @;
2729  sub Taxonomy {  sub Taxonomy {
2730          # Get the parameters.          # Get the parameters.
2731          my $self = shift @_;      my ($self, $genome) = @_;
         my ($genome) = @_;  
         # Find the specified genome's taxonomy string.  
         my ($list) = $self->GetEntityValues('Genome', $genome, ['Genome(taxonomy)']);  
2732          # Declare the return variable.          # Declare the return variable.
2733          my @retVal = ();          my @retVal = ();
2734          # If we found the genome, return its taxonomy string.      # Get the genome data.
2735          if ($list) {      my $genomeData = $self->_GenomeData($genome);
2736                  @retVal = split /\s*;\s*/, $list;      # Only proceed if it exists.
2737        if (defined $genomeData) {
2738            # Create the taxonomy from the taxonomy string.
2739            @retVal = split /\s*;\s*/, $genomeData->PrimaryValue('Genome(taxonomy)');
2740          } else {          } else {
2741            # Genome doesn't exist, so emit a warning.
2742                  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);
2743          }          }
2744          # Return the value found.          # Return the value found.
# Line 1797  Line 2747 
2747    
2748  =head3 CrudeDistance  =head3 CrudeDistance
2749    
2750  C<< my $distance = $sprout->CrudeDistance($genome1, $genome2); >>      my $distance = $sprout->CrudeDistance($genome1, $genome2);
2751    
2752  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
2753  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 1823  Line 2773 
2773  #: Return Type $;  #: Return Type $;
2774  sub CrudeDistance {  sub CrudeDistance {
2775          # Get the parameters.          # Get the parameters.
2776          my $self = shift @_;      my ($self, $genome1, $genome2) = @_;
         my ($genome1, $genome2) = @_;  
2777          # Insure that the distance is commutative by sorting the genome IDs.          # Insure that the distance is commutative by sorting the genome IDs.
2778          my ($genomeA, $genomeB);          my ($genomeA, $genomeB);
2779          if ($genome2 < $genome2) {          if ($genome2 < $genome2) {
# Line 1834  Line 2783 
2783          }          }
2784          my @taxA = $self->Taxonomy($genomeA);          my @taxA = $self->Taxonomy($genomeA);
2785          my @taxB = $self->Taxonomy($genomeB);          my @taxB = $self->Taxonomy($genomeB);
2786          # Initialize the distance to 1. We'll reduce it each time we find a match between the      # Compute the distance.
2787          # 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;  
         }  
2788      return $retVal;      return $retVal;
2789  }  }
2790    
2791  =head3 RoleName  =head3 RoleName
2792    
2793  C<< my $roleName = $sprout->RoleName($roleID); >>      my $roleName = $sprout->RoleName($roleID);
2794    
2795  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
2796  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 1871  Line 2811 
2811  #: Return Type $;  #: Return Type $;
2812  sub RoleName {  sub RoleName {
2813          # Get the parameters.          # Get the parameters.
2814          my $self = shift @_;      my ($self, $roleID) = @_;
         my ($roleID) = @_;  
2815          # Get the specified role's name.          # Get the specified role's name.
2816          my ($retVal) = $self->GetEntityValues('Role', $roleID, ['Role(name)']);          my ($retVal) = $self->GetEntityValues('Role', $roleID, ['Role(name)']);
2817          # Use the ID if the role has no name.          # Use the ID if the role has no name.
# Line 1885  Line 2824 
2824    
2825  =head3 RoleDiagrams  =head3 RoleDiagrams
2826    
2827  C<< my @diagrams = $sprout->RoleDiagrams($roleID); >>      my @diagrams = $sprout->RoleDiagrams($roleID);
2828    
2829  Return a list of the diagrams containing a specified functional role.  Return a list of the diagrams containing a specified functional role.
2830    
# Line 1905  Line 2844 
2844  #: Return Type @;  #: Return Type @;
2845  sub RoleDiagrams {  sub RoleDiagrams {
2846          # Get the parameters.          # Get the parameters.
2847          my $self = shift @_;      my ($self, $roleID) = @_;
         my ($roleID) = @_;  
2848          # Query for the diagrams.          # Query for the diagrams.
2849          my @retVal = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],          my @retVal = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],
2850                                                                  'RoleOccursIn(to-link)');                                                                  'RoleOccursIn(to-link)');
# Line 1916  Line 2854 
2854    
2855  =head3 FeatureProperties  =head3 FeatureProperties
2856    
2857  C<< my @properties = $sprout->FeatureProperties($featureID); >>      my @properties = $sprout->FeatureProperties($featureID);
2858    
2859  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
2860  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
2861  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
2862  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
2863  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.  
2864    
2865  =over 4  =over 4
2866    
# Line 1934  Line 2870 
2870    
2871  =item RETURN  =item RETURN
2872    
2873  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.  
2874    
2875  =back  =back
2876    
# Line 1943  Line 2878 
2878  #: Return Type @@;  #: Return Type @@;
2879  sub FeatureProperties {  sub FeatureProperties {
2880          # Get the parameters.          # Get the parameters.
2881          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
2882          # Get the properties.          # Get the properties.
2883          my @retVal = $self->GetAll(['HasProperty', 'Property'], "HasProperty(from-link) = ?", [$featureID],      my @attributes = $self->ca->GetAttributes($featureID);
2884                                                          ['Property(property-name)', 'Property(property-value)',      # Strip the feature ID off each tuple.
2885                                                           'HasProperty(evidence)']);      my @retVal = ();
2886        for my $attributeRow (@attributes) {
2887            shift @{$attributeRow};
2888            push @retVal, $attributeRow;
2889        }
2890          # Return the resulting list.          # Return the resulting list.
2891          return @retVal;          return @retVal;
2892  }  }
2893    
2894  =head3 DiagramName  =head3 DiagramName
2895    
2896  C<< my $diagramName = $sprout->DiagramName($diagramID); >>      my $diagramName = $sprout->DiagramName($diagramID);
2897    
2898  Return the descriptive name of a diagram.  Return the descriptive name of a diagram.
2899    
# Line 1975  Line 2913 
2913  #: Return Type $;  #: Return Type $;
2914  sub DiagramName {  sub DiagramName {
2915          # Get the parameters.          # Get the parameters.
2916          my $self = shift @_;      my ($self, $diagramID) = @_;
         my ($diagramID) = @_;  
2917          # Get the specified diagram's name and return it.          # Get the specified diagram's name and return it.
2918          my ($retVal) = $self->GetEntityValues('Diagram', $diagramID, ['Diagram(name)']);          my ($retVal) = $self->GetEntityValues('Diagram', $diagramID, ['Diagram(name)']);
2919          return $retVal;          return $retVal;
2920  }  }
2921    
2922    =head3 PropertyID
2923    
2924        my $id = $sprout->PropertyID($propName, $propValue);
2925    
2926    Return the ID of the specified property name and value pair, if the
2927    pair exists. Only a small subset of the FIG attributes are stored as
2928    Sprout properties, mostly for use in search optimization.
2929    
2930    =over 4
2931    
2932    =item propName
2933    
2934    Name of the desired property.
2935    
2936    =item propValue
2937    
2938    Value expected for the desired property.
2939    
2940    =item RETURN
2941    
2942    Returns the ID of the name/value pair, or C<undef> if the pair does not exist.
2943    
2944    =back
2945    
2946    =cut
2947    
2948    sub PropertyID {
2949        # Get the parameters.
2950        my ($self, $propName, $propValue) = @_;
2951        # Try to find the ID.
2952        my ($retVal) = $self->GetFlat(['Property'],
2953                                      "Property(property-name) = ? AND Property(property-value) = ?",
2954                                      [$propName, $propValue], 'Property(id)');
2955        # Return the result.
2956        return $retVal;
2957    }
2958    
2959  =head3 MergedAnnotations  =head3 MergedAnnotations
2960    
2961  C<< my @annotationList = $sprout->MergedAnnotations(\@list); >>      my @annotationList = $sprout->MergedAnnotations(\@list);
2962    
2963  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
2964  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 2008  Line 2982 
2982  #: Return Type @;  #: Return Type @;
2983  sub MergedAnnotations {  sub MergedAnnotations {
2984          # Get the parameters.          # Get the parameters.
2985          my $self = shift @_;      my ($self, $list) = @_;
         my ($list) = @_;  
2986          # Create a list to hold the annotation tuples found.          # Create a list to hold the annotation tuples found.
2987          my @tuples = ();          my @tuples = ();
2988          # Loop through the features in the input list.          # Loop through the features in the input list.
# Line 2034  Line 3007 
3007    
3008  =head3 RoleNeighbors  =head3 RoleNeighbors
3009    
3010  C<< my @roleList = $sprout->RoleNeighbors($roleID); >>      my @roleList = $sprout->RoleNeighbors($roleID);
3011    
3012  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
3013  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 2057  Line 3030 
3030  #: Return Type @;  #: Return Type @;
3031  sub RoleNeighbors {  sub RoleNeighbors {
3032          # Get the parameters.          # Get the parameters.
3033          my $self = shift @_;      my ($self, $roleID) = @_;
         my ($roleID) = @_;  
3034          # Get all the diagrams containing this role.          # Get all the diagrams containing this role.
3035          my @diagrams = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],          my @diagrams = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],
3036                                                                    'RoleOccursIn(to-link)');                                                                    'RoleOccursIn(to-link)');
# Line 2078  Line 3050 
3050    
3051  =head3 FeatureLinks  =head3 FeatureLinks
3052    
3053  C<< my @links = $sprout->FeatureLinks($featureID); >>      my @links = $sprout->FeatureLinks($featureID);
3054    
3055  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
3056  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 2100  Line 3072 
3072  #: Return Type @;  #: Return Type @;
3073  sub FeatureLinks {  sub FeatureLinks {
3074          # Get the parameters.          # Get the parameters.
3075          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
3076          # Get the feature's links.          # Get the feature's links.
3077          my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(link)']);          my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(link)']);
3078          # Return the feature's links.          # Return the feature's links.
# Line 2110  Line 3081 
3081    
3082  =head3 SubsystemsOf  =head3 SubsystemsOf
3083    
3084  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>      my %subsystems = $sprout->SubsystemsOf($featureID);
3085    
3086  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
3087  to the role the feature performs.  to the roles the feature performs.
3088    
3089  =over 4  =over 4
3090    
# Line 2123  Line 3094 
3094    
3095  =item RETURN  =item RETURN
3096    
3097  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.
3098    
3099  =back  =back
3100    
3101  =cut  =cut
3102  #: Return Type %;  #: Return Type %@;
3103  sub SubsystemsOf {  sub SubsystemsOf {
3104          # Get the parameters.          # Get the parameters.
3105          my $self = shift @_;      my ($self, $featureID) = @_;
3106          my ($featureID) = @_;      # Get the subsystem list.
         # Use the SSCell to connect features to subsystems.  
3107          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],
3108                                                                          "ContainsFeature(to-link) = ?", [$featureID],                                                                          "ContainsFeature(to-link) = ?", [$featureID],
3109                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);
3110          # Create the return value.          # Create the return value.
3111          my %retVal = ();          my %retVal = ();
3112        # Build a hash to weed out duplicates. Sometimes the same PEG and role appears
3113        # in two spreadsheet cells.
3114        my %dupHash = ();
3115          # Loop through the results, adding them to the hash.          # Loop through the results, adding them to the hash.
3116          for my $record (@subsystems) {          for my $record (@subsystems) {
3117                  $retVal{$record->[0]} = $record->[1];          # Get this subsystem and role.
3118            my ($subsys, $role) = @{$record};
3119            # Insure it's the first time for both.
3120            my $dupKey = "$subsys\n$role";
3121            if (! exists $dupHash{"$subsys\n$role"}) {
3122                $dupHash{$dupKey} = 1;
3123                push @{$retVal{$subsys}}, $role;
3124            }
3125          }          }
3126          # Return the hash.          # Return the hash.
3127          return %retVal;          return %retVal;
3128  }  }
3129    
3130  =head3 RelatedFeatures  =head3 SubsystemList
3131    
3132  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>      my @subsystems = $sprout->SubsystemList($featureID);
3133    
3134  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
3135  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
3136  an empty list will be returned.  subsystem names, not the roles.
3137    
3138  =over 4  =over 4
3139    
3140  =item featureID  =item featureID
3141    
3142  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.  
3143    
3144  =item RETURN  =item RETURN
3145    
3146  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.
3147    
3148  =back  =back
3149    
3150  =cut  =cut
3151  #: Return Type @;  #: Return Type @;
3152  sub RelatedFeatures {  sub SubsystemList {
3153          # Get the parameters.          # Get the parameters.
3154          my $self = shift @_;      my ($self, $featureID) = @_;
3155          my ($featureID, $function, $userID) = @_;      # Get the list of names. We do a join to the Subsystem table because we have missing subsystems in
3156          # Get a list of the features that are BBHs of the incoming feature.      # the Sprout database!
3157          my @bbhFeatures = $self->GetFlat(['IsBidirectionalBestHitOf'],      my @retVal = $self->GetFlat(['HasRoleInSubsystem', 'Subsystem'], "HasRoleInSubsystem(from-link) = ?",
3158                                                                           "IsBidirectionalBestHitOf(from-link) = ?", [$featureID],                                  [$featureID], 'HasRoleInSubsystem(to-link)');
3159                                                                           'IsBidirectionalBestHitOf(to-link)');      # Return the result, sorted.
3160          # 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;  
3161  }  }
3162    
3163  =head3 TaxonomySort  =head3 GenomeSubsystemData
3164    
3165  C<< my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs); >>      my %featureData = $sprout->GenomeSubsystemData($genomeID);
3166    
3167  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.  
3168    
3169  =over 4  =over 4
3170    
3171  =item $featureIDs  =item genomeID
3172    
3173  List of features to be taxonomically sorted.  ID of the genome whose subsystem feature map is desired.
3174    
3175  =item RETURN  =item RETURN
3176    
3177  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
3178    2-tuple contains a subsystem name followed by a role ID.
3179    
3180  =back  =back
3181    
3182  =cut  =cut
3183  #: Return Type @;  
3184  sub TaxonomySort {  sub GenomeSubsystemData {
3185          # Get the parameters.          # Get the parameters.
3186          my $self = shift @_;      my ($self, $genomeID) = @_;
3187          my ($featureIDs) = @_;      # Declare the return variable.
3188          # Create the working hash table.      my %retVal = ();
3189          my %hashBuffer = ();      # Get a list of the genome features that participate in subsystems. For each
3190          # Loop through the features.      # feature we get its subsystem ID and the corresponding roles.
3191          for my $fid (@{$featureIDs}) {      my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf', 'HasSSCell'],
3192                  # Get the taxonomy of the feature's genome.                                   "HasFeature(from-link) = ?", [$genomeID],
3193                  my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",                                   ['HasFeature(to-link)', 'IsRoleOf(from-link)',  'HasSSCell(from-link)']);
3194                                                                                  [$fid], 'Genome(taxonomy)');      # Now we get a list of valid subsystems. These are the subsystems connected to the genome with
3195                  # Add this feature to the hash buffer.      # a non-negative variant code.
3196                  if (exists $hashBuffer{$taxonomy}) {      my %subs = map { $_ => 1 } $self->GetFlat(['ParticipatesIn'],
3197                          push @{$hashBuffer{$taxonomy}}, $fid;                                                  "ParticipatesIn(from-link) = ? AND ParticipatesIn(variant-code) >= 0",
3198                  } else {                                                  [$genomeID], 'ParticipatesIn(to-link)');
3199                          $hashBuffer{$taxonomy} = [$fid];      # We loop through @roleData to build the hash.
3200                  }      for my $roleEntry (@roleData) {
3201            # Get the data for this feature and cell.
3202            my ($fid, $role, $subsys) = @{$roleEntry};
3203            Trace("Subsystem for $fid is $subsys.") if T(4);
3204            # Check the subsystem;
3205            if ($subs{$subsys}) {
3206                Trace("Subsystem found.") if T(4);
3207                # Insure this feature has an entry in the return hash.
3208                if (! exists $retVal{$fid}) { $retVal{$fid} = []; }
3209                # Merge in this new data.
3210                push @{$retVal{$fid}}, [$subsys, $role];
3211          }          }
         # Sort the keys and get the elements.  
         my @retVal = ();  
         for my $taxon (sort keys %hashBuffer) {  
                 push @retVal, @{$hashBuffer{$taxon}};  
3212          }          }
3213          # Return the result.          # Return the result.
3214          return @retVal;      return %retVal;
3215  }  }
3216    
3217  =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.  
3218    
3219  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.  
3220    
3221  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
3222    have been assigned the specified function by the specified user. If no such features exists,
3223    an empty list will be returned.
3224    
3225  =over 4  =over 4
3226    
3227  =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  
3228    
3229  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.
3230    
3231  =item fields  =item function
3232    
3233  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
3234    features should be selected.
3235    
3236  =item count  =item userID
3237    
3238  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.
3239    
3240  =item RETURN  =item RETURN
3241    
3242  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.  
3243    
3244  =back  =back
3245    
3246  =cut  =cut
3247  #: Return Type @@;  #: Return Type @;
3248  sub GetAll {  sub RelatedFeatures {
3249          # Get the parameters.          # Get the parameters.
3250          my $self = shift @_;      my ($self, $featureID, $function, $userID) = @_;
3251          my ($objectNames, $filterClause, $parameterList, $fields, $count) = @_;      # Get a list of the features that are BBHs of the incoming feature.
3252          # Create the query.      my $bbhData = FIGRules::BBHData($featureID);
3253          my $query = $self->Get($objectNames, $filterClause, $parameterList);      my @bbhFeatures = map { $_->[0] } @$bbhData;
3254          # Set up a counter of the number of records read.      # Now we loop through the features, pulling out the ones that have the correct
3255          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.  
3256          my @retVal = ();          my @retVal = ();
3257          while (($count == 0 || $fetched < $count) && (my $row = $query->Fetch())) {      for my $bbhFeature (@bbhFeatures) {
3258                  my @rowData = $row->Values($fields);          # Get this feature's functional assignment.
3259                  push @retVal, \@rowData;          my $newFunction = $self->FunctionOf($bbhFeature, $userID);
3260                  $fetched++;          # If it matches, add it to the result list.
3261            if ($newFunction eq $function) {
3262                push @retVal, $bbhFeature;
3263          }          }
3264          # Return the resulting list.      }
3265        # Return the result list.
3266          return @retVal;          return @retVal;
3267  }  }
3268    
3269  =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.  
3270    
3271  =item filterClause      my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs);
3272    
3273  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
3274  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.  
3275    
3276  =item parameterList  This task could almost be handled by the database; however, the taxonomy string in the
3277    database is a text field and can't be indexed. Instead, we create a hash table that maps
3278    taxonomy strings to lists of features. We then process the hash table using a key sort
3279    and merge the feature lists together to create the output.
3280    
3281  List of the parameters to be substituted in for the parameters marks in the filter clause.  =over 4
3282    
3283  =item field  =item $featureIDs
3284    
3285  Name of the field to be used to get the elements of the list returned.  List of features to be taxonomically sorted.
3286    
3287  =item RETURN  =item RETURN
3288    
3289  Returns a list of values.  Returns the list of features sorted by the taxonomies of the containing genomes.
3290    
3291  =back  =back
3292    
3293  =cut  =cut
3294  #: Return Type @;  #: Return Type @;
3295  sub GetFlat {  sub TaxonomySort {
3296          # Get the parameters.          # Get the parameters.
3297          my $self = shift @_;      my ($self, $featureIDs) = @_;
3298          my ($objectNames, $filterClause, $parameterList, $field) = @_;      # Create the working hash table.
3299          # Construct the query.      my %hashBuffer = ();
3300          my $query = $self->Get($objectNames, $filterClause, $parameterList);      # Loop through the features.
3301          # Create the result list.      for my $fid (@{$featureIDs}) {
3302            # Get the taxonomy of the feature's genome.
3303            my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",
3304                                            [$fid], 'Genome(taxonomy)');
3305            # Add this feature to the hash buffer.
3306            push @{$hashBuffer{$taxonomy}}, $fid;
3307        }
3308        # Sort the keys and get the elements.
3309          my @retVal = ();          my @retVal = ();
3310          # Loop through the records, adding the field values found to the result list.      for my $taxon (sort keys %hashBuffer) {
3311          while (my $row = $query->Fetch()) {          push @retVal, @{$hashBuffer{$taxon}};
                 push @retVal, $row->Value($field);  
3312          }          }
3313          # Return the list created.      # Return the result.
3314          return @retVal;          return @retVal;
3315  }  }
3316    
3317  =head3 Protein  =head3 Protein
3318    
3319  C<< my $protein = Sprout::Protein($sequence, $table); >>      my $protein = Sprout::Protein($sequence, $table);
3320    
3321  Translate a DNA sequence into a protein sequence.  Translate a DNA sequence into a protein sequence.
3322    
# Line 2460  Line 3386 
3386          # Loop through the input triples.          # Loop through the input triples.
3387          my $n = length $sequence;          my $n = length $sequence;
3388          for (my $i = 0; $i < $n; $i += 3) {          for (my $i = 0; $i < $n; $i += 3) {
3389                  # Get the current triple from the sequence.          # Get the current triple from the sequence. Note we convert to
3390                  my $triple = substr($sequence, $i, 3);          # upper case to insure a match.
3391            my $triple = uc substr($sequence, $i, 3);
3392                  # Translate it using the table.                  # Translate it using the table.
3393                  my $protein = "X";                  my $protein = "X";
3394                  if (exists $table->{$triple}) { $protein = $table->{$triple}; }                  if (exists $table->{$triple}) { $protein = $table->{$triple}; }
# Line 2475  Line 3402 
3402    
3403  =head3 LoadInfo  =head3 LoadInfo
3404    
3405  C<< my ($dirName, @relNames) = $sprout->LoadInfo(); >>      my ($dirName, @relNames) = $sprout->LoadInfo();
3406    
3407  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
3408  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 2485  Line 3412 
3412  #: Return Type @;  #: Return Type @;
3413  sub LoadInfo {  sub LoadInfo {
3414          # Get the parameters.          # Get the parameters.
3415          my $self = shift @_;      my ($self) = @_;
3416          # 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.
3417          my @retVal = ($self->{_options}->{dataDir});          my @retVal = ($self->{_options}->{dataDir});
3418          # Concatenate the table names.          # Concatenate the table names.
3419          push @retVal, $self->{_erdb}->GetTableNames();      push @retVal, $self->GetTableNames();
3420          # Return the result.          # Return the result.
3421          return @retVal;          return @retVal;
3422  }  }
3423    
3424    =head3 BBHMatrix
3425    
3426        my $bbhMap = $sprout->BBHMatrix($genomeID, $cutoff, @targets);
3427    
3428    Find all the bidirectional best hits for the features of a genome in a
3429    specified list of target genomes. The return value will be a hash mapping
3430    features in the original genome to their bidirectional best hits in the
3431    target genomes.
3432    
3433    =over 4
3434    
3435    =item genomeID
3436    
3437    ID of the genome whose features are to be examined for bidirectional best hits.
3438    
3439    =item cutoff
3440    
3441    A cutoff value. Only hits with a score lower than the cutoff will be returned.
3442    
3443    =item targets
3444    
3445    List of target genomes. Only pairs originating in the original
3446    genome and landing in one of the target genomes will be returned.
3447    
3448    =item RETURN
3449    
3450    Returns a reference to a hash mapping each feature in the original genome
3451    to a sub-hash mapping its BBH pegs in the target genomes to their scores.
3452    
3453    =back
3454    
3455    =cut
3456    
3457    sub BBHMatrix {
3458        # Get the parameters.
3459        my ($self, $genomeID, $cutoff, @targets) = @_;
3460        # Declare the return variable.
3461        my %retVal = ();
3462        # Ask for the BBHs.
3463        my @bbhList = FIGRules::BatchBBHs("fig|$genomeID.%", $cutoff, @targets);
3464        Trace("Retrieved " . scalar(@bbhList) . " BBH results.") if T(3);
3465        # We now have a set of 4-tuples that we need to convert into a hash of hashes.
3466        for my $bbhData (@bbhList) {
3467            my ($peg1, $peg2, $score) = @{$bbhData};
3468            if (! exists $retVal{$peg1}) {
3469                $retVal{$peg1} = { $peg2 => $score };
3470            } else {
3471                $retVal{$peg1}->{$peg2} = $score;
3472            }
3473        }
3474        # Return the result.
3475        return \%retVal;
3476    }
3477    
3478    
3479    =head3 SimMatrix
3480    
3481        my %simMap = $sprout->SimMatrix($genomeID, $cutoff, @targets);
3482    
3483    Find all the similarities for the features of a genome in a
3484    specified list of target genomes. The return value will be a hash mapping
3485    features in the original genome to their similarites in the
3486    target genomes.
3487    
3488    =over 4
3489    
3490    =item genomeID
3491    
3492    ID of the genome whose features are to be examined for similarities.
3493    
3494    =item cutoff
3495    
3496    A cutoff value. Only hits with a score lower than the cutoff will be returned.
3497    
3498    =item targets
3499    
3500    List of target genomes. Only pairs originating in the original
3501    genome and landing in one of the target genomes will be returned.
3502    
3503    =item RETURN
3504    
3505    Returns a hash mapping each feature in the original genome to a hash mapping its
3506    similar pegs in the target genomes to their scores.
3507    
3508    =back
3509    
3510    =cut
3511    
3512    sub SimMatrix {
3513        # Get the parameters.
3514        my ($self, $genomeID, $cutoff, @targets) = @_;
3515        # Declare the return variable.
3516        my %retVal = ();
3517        # Get the list of features in the source organism.
3518        my @fids = $self->FeaturesOf($genomeID);
3519        # Ask for the sims. We only want similarities to fig features.
3520        my $simList = FIGRules::GetNetworkSims($self, \@fids, {}, 1000, $cutoff, "fig");
3521        if (! defined $simList) {
3522            Confess("Unable to retrieve similarities from server.");
3523        } else {
3524            Trace("Processing sims.") if T(3);
3525            # We now have a set of sims that we need to convert into a hash of hashes. First, we
3526            # Create a hash for the target genomes.
3527            my %targetHash = map { $_ => 1 } @targets;
3528            for my $simData (@{$simList}) {
3529                # Get the PEGs and the score.
3530                my ($peg1, $peg2, $score) = ($simData->id1, $simData->id2, $simData->psc);
3531                # Insure the second ID is in the target list.
3532                my ($genome2) = FIGRules::ParseFeatureID($peg2);
3533                if (exists $targetHash{$genome2}) {
3534                    # Here it is. Now we need to add it to the return hash. How we do that depends
3535                    # on whether or not $peg1 is new to us.
3536                    if (! exists $retVal{$peg1}) {
3537                        $retVal{$peg1} = { $peg2 => $score };
3538                    } else {
3539                        $retVal{$peg1}->{$peg2} = $score;
3540                    }
3541                }
3542            }
3543        }
3544        # Return the result.
3545        return %retVal;
3546    }
3547    
3548    
3549  =head3 LowBBHs  =head3 LowBBHs
3550    
3551  C<< my %bbhMap = $sprout->GoodBBHs($featureID, $cutoff); >>      my %bbhMap = $sprout->LowBBHs($featureID, $cutoff);
3552    
3553  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
3554  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 2522  Line 3574 
3574  #: Return Type %;  #: Return Type %;
3575  sub LowBBHs {  sub LowBBHs {
3576          # Get the parsameters.          # Get the parsameters.
3577          my $self = shift @_;      my ($self, $featureID, $cutoff) = @_;
         my ($featureID, $cutoff) = @_;  
3578          # Create the return hash.          # Create the return hash.
3579          my %retVal = ();          my %retVal = ();
3580          # Create a query to get the desired BBHs.      # Query for the desired BBHs.
3581          my @bbhList = $self->GetAll(['IsBidirectionalBestHitOf'],      my $bbhList = FIGRules::BBHData($featureID, $cutoff);
                                                                 'IsBidirectionalBestHitOf(sc) <= ? AND IsBidirectionalBestHitOf(from-link) = ?',  
                                                                 [$cutoff, $featureID],  
                                                                 ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(sc)']);  
3582          # Form the results into the return hash.          # Form the results into the return hash.
3583          for my $pair (@bbhList) {      for my $pair (@$bbhList) {
3584                  $retVal{$pair->[0]} = $pair->[1];          my $fid = $pair->[0];
3585            if ($self->Exists('Feature', $fid)) {
3586                $retVal{$fid} = $pair->[1];
3587            }
3588          }          }
3589          # Return the result.          # Return the result.
3590          return %retVal;          return %retVal;
3591  }  }
3592    
3593  =head2 Internal Utility Methods  =head3 Sims
3594    
3595  =head3 ParseAssignment      my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters);
3596    
3597  Parse annotation text to determine whether or not it is a functional assignment. If it is,  Get a list of similarities for a specified feature. Similarity information is not kept in the
3598  the user and function text will be returned as a 2-element list. If it isn't, an empty list  Sprout database; rather, they are retrieved from a network server. The similarities are
3599  will be returned.  returned as B<Sim> objects. A Sim object is actually a list reference that has been blessed
3600    so that its elements can be accessed by name.
3601    
3602    Similarities can be either raw or expanded. The raw similarities are basic
3603    hits between features with similar DNA. Expanding a raw similarity drags in any
3604    features considered substantially identical. So, for example, if features B<A1>,
3605    B<A2>, and B<A3> are all substantially identical to B<A>, then a raw similarity
3606    B<[C,A]> would be expanded to B<[C,A] [C,A1] [C,A2] [C,A3]>.
3607    
3608  This is a static method.  =over 4
3609    
3610    =item fid
3611    
3612    ID of the feature whose similarities are desired, or reference to a list of IDs
3613    of features whose similarities are desired.
3614    
3615    =item maxN
3616    
3617    Maximum number of similarities to return.
3618    
3619    =item maxP
3620    
3621    Minumum allowable similarity score.
3622    
3623    =item select
3624    
3625    Selection criterion: C<raw> means only raw similarities are returned; C<fig>
3626    means only similarities to FIG features are returned; C<all> means all expanded
3627    similarities are returned; and C<figx> means similarities are expanded until the
3628    number of FIG features equals the maximum.
3629    
3630    =item max_expand
3631    
3632    The maximum number of features to expand.
3633    
3634    =item filters
3635    
3636    Reference to a hash containing filter information, or a subroutine that can be
3637    used to filter the sims.
3638    
3639    =item RETURN
3640    
3641    Returns a reference to a list of similarity objects, or C<undef> if an error
3642    occurred.
3643    
3644    =back
3645    
3646    =cut
3647    
3648    sub Sims {
3649        # Get the parameters.
3650        my ($self, $fid, $maxN, $maxP, $select, $max_expand, $filters) = @_;
3651        # Create the shim object to test for deleted FIDs.
3652        my $shim = FidCheck->new($self);
3653        # Ask the network for sims.
3654        my $retVal = FIGRules::GetNetworkSims($shim, $fid, {}, $maxN, $maxP, $select, $max_expand, $filters);
3655        # Return the result.
3656        return $retVal;
3657    }
3658    
3659    =head3 IsAllGenomes
3660    
3661        my $flag = $sprout->IsAllGenomes(\@list, \@checkList);
3662    
3663    Return TRUE if all genomes in the second list are represented in the first list at
3664    least one. Otherwise, return FALSE. If the second list is omitted, the first list is
3665    compared to a list of all the genomes.
3666    
3667  =over 4  =over 4
3668    
3669  =item text  =item list
3670    
3671  Text of the annotation.  Reference to the list to be compared to the second list.
3672    
3673    =item checkList (optional)
3674    
3675    Reference to the comparison target list. Every genome ID in this list must occur at
3676    least once in the first list. If this parameter is omitted, a list of all the genomes
3677    is used.
3678    
3679  =item RETURN  =item RETURN
3680    
3681  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
3682  a two-element list containing the user name and the function text.  first list, else FALSE.
3683    
3684  =back  =back
3685    
3686  =cut  =cut
3687    
3688  sub ParseAssignment {  sub IsAllGenomes {
3689        # Get the parameters.
3690        my ($self, $list, $checkList) = @_;
3691        # Supply the checklist if it was omitted.
3692        $checkList = [$self->Genomes()] if ! defined($checkList);
3693        # Create a hash of the original list.
3694        my %testList = map { $_ => 1 } @{$list};
3695        # Declare the return variable. We assume that the representation
3696        # is complete and stop at the first failure.
3697        my $retVal = 1;
3698        my $n = scalar @{$checkList};
3699        for (my $i = 0; $retVal && $i < $n; $i++) {
3700            if (! $testList{$checkList->[$i]}) {
3701                $retVal = 0;
3702            }
3703        }
3704        # Return the result.
3705        return $retVal;
3706    }
3707    
3708    =head3 GetGroups
3709    
3710        my %groups = $sprout->GetGroups(\@groupList);
3711    
3712    Return a hash mapping each group to the IDs of the genomes in the group.
3713    A list of groups may be specified, in which case only those groups will be
3714    shown. Alternatively, if no parameter is supplied, all groups will be
3715    included. Genomes that are not in any group are omitted.
3716    
3717    =cut
3718    #: Return Type %@;
3719    sub GetGroups {
3720          # Get the parameters.          # Get the parameters.
3721          my ($text) = @_;      my ($self, $groupList) = @_;
3722          # Declare the return value.          # Declare the return value.
3723          my @retVal = ();      my %retVal = ();
3724          # Check to see if this is a functional assignment.      # Determine whether we are getting all the groups or just some.
3725          my ($user, $type, $function) = split(/\n/, $text);      if (defined $groupList) {
3726          if ($type =~ m/^set $user function to$/i) {          # Here we have a group list. Loop through them individually,
3727                  # Here it is, so we return the user name and function text.          # getting a list of the relevant genomes.
3728                  @retVal = ($user, $function);          for my $group (@{$groupList}) {
3729                my @genomeIDs = $self->GetFlat(['Genome'], "Genome(primary-group) = ?",
3730                    [$group], "Genome(id)");
3731                $retVal{$group} = \@genomeIDs;
3732          }          }
3733          # Return the result list.      } else {
3734            # Here we need all of the groups. In this case, we run through all
3735            # of the genome records, putting each one found into the appropriate
3736            # group. Note that we use a filter clause to insure that only genomes
3737            # in real NMPDR groups are included in the return set.
3738            my @genomes = $self->GetAll(['Genome'], "Genome(primary-group) <> ?",
3739                                        [$FIG_Config::otherGroup], ['Genome(id)', 'Genome(primary-group)']);
3740            # Loop through the genomes found.
3741            for my $genome (@genomes) {
3742                # Get the genome ID and group, and add this genome to the group's list.
3743                my ($genomeID, $group) = @{$genome};
3744                push @{$retVal{$group}}, $genomeID;
3745            }
3746        }
3747        # Return the hash we just built.
3748        return %retVal;
3749    }
3750    
3751    =head3 MyGenomes
3752    
3753        my @genomes = Sprout::MyGenomes($dataDir);
3754    
3755    Return a list of the genomes to be included in the Sprout.
3756    
3757    This method is provided for use during the Sprout load. It presumes the Genome load file has
3758    already been created. (It will be in the Sprout data directory and called either C<Genome>
3759    or C<Genome.dtx>.) Essentially, it reads in the Genome load file and strips out the genome
3760    IDs.
3761    
3762    =over 4
3763    
3764    =item dataDir
3765    
3766    Directory containing the Sprout load files.
3767    
3768    =back
3769    
3770    =cut
3771    #: Return Type @;
3772    sub MyGenomes {
3773        # Get the parameters.
3774        my ($dataDir) = @_;
3775        # Compute the genome file name.
3776        my $genomeFileName = LoadFileName($dataDir, "Genome");
3777        # Extract the genome IDs from the files.
3778        my @retVal = map { $_ =~ /^(\S+)/; $1 } Tracer::GetFile($genomeFileName);
3779        # Return the result.
3780          return @retVal;          return @retVal;
3781  }  }
3782    
3783  =head3 FriendlyTimestamp  =head3 LoadFileName
3784    
3785  Convert a time number to a user-friendly time stamp for display.      my $fileName = Sprout::LoadFileName($dataDir, $tableName);
3786    
3787  This is a static method.  Return the name of the load file for the specified table in the specified data
3788    directory.
3789    
3790  =over 4  =over 4
3791    
3792  =item timeValue  =item dataDir
3793    
3794  Numeric time value.  Directory containing the Sprout load files.
3795    
3796    =item tableName
3797    
3798    Name of the table whose load file is desired.
3799    
3800  =item RETURN  =item RETURN
3801    
3802  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
3803    C<undef> if no load file is present.
3804    
3805    =back
3806    
3807    =cut
3808    #: Return Type $;
3809    sub LoadFileName {
3810        # Get the parameters.
3811        my ($dataDir, $tableName) = @_;
3812        # Declare the return variable.
3813        my $retVal;
3814        # Check for the various file names.
3815        if (-e "$dataDir/$tableName") {
3816            $retVal = "$dataDir/$tableName";
3817        } elsif (-e "$dataDir/$tableName.dtx") {
3818            $retVal = "$dataDir/$tableName.dtx";
3819        }
3820        # Return the result.
3821        return $retVal;
3822    }
3823    
3824    =head3 DeleteGenome
3825    
3826        my $stats = $sprout->DeleteGenome($genomeID, $testFlag);
3827    
3828    Delete a genome from the database.
3829    
3830    =over 4
3831    
3832    =item genomeID
3833    
3834    ID of the genome to delete
3835    
3836    =item testFlag
3837    
3838    If TRUE, then the DELETE statements will be traced, but no deletions will occur.
3839    
3840    =item RETURN
3841    
3842    Returns a statistics object describing the rows deleted.
3843    
3844    =back
3845    
3846    =cut
3847    #: Return Type $%;
3848    sub DeleteGenome {
3849        # Get the parameters.
3850        my ($self, $genomeID, $testFlag) = @_;
3851        # Perform the delete for the genome's features.
3852        my $retVal = $self->Delete('Feature', "fig|$genomeID.%", testMode => $testFlag);
3853        # Perform the delete for the primary genome data.
3854        my $stats = $self->Delete('Genome', $genomeID, testMode => $testFlag);
3855        $retVal->Accumulate($stats);
3856        # Return the result.
3857        return $retVal;
3858    }
3859    
3860    =head3 Fix
3861    
3862        my %fixedHash = $sprout->Fix(%groupHash);
3863    
3864    Prepare a genome group hash (like that returned by L</GetGroups>) for processing.
3865    The groups will be combined into the appropriate super-groups.
3866    
3867    =over 4
3868    
3869    =item groupHash
3870    
3871    Hash to be fixed up.
3872    
3873    =item RETURN
3874    
3875    Returns a fixed-up version of the hash.
3876    
3877    =back
3878    
3879    =cut
3880    
3881    sub Fix {
3882        # Get the parameters.
3883        my ($self, %groupHash) = @_;
3884        # Create the result hash.
3885        my %retVal = ();
3886        # Copy over the genomes.
3887        for my $groupID (keys %groupHash) {
3888            # Get the super-group name.
3889            my $realGroupID = $self->SuperGroup($groupID);
3890            # Append this group's genomes into the result hash
3891            # using the super-group name.
3892            push @{$retVal{$realGroupID}}, @{$groupHash{$groupID}};
3893        }
3894        # Return the result hash.
3895        return %retVal;
3896    }
3897    
3898    =head3 GroupPageName
3899    
3900        my $name = $sprout->GroupPageName($group);
3901    
3902    Return the name of the page for the specified NMPDR group.
3903    
3904    =over 4
3905    
3906    =item group
3907    
3908    Name of the relevant group.
3909    
3910    =item RETURN
3911    
3912    Returns the relative page name (e.g. C<../content/campy.php>). If the group file is not in
3913    memory it will be read in.
3914    
3915    =back
3916    
3917    =cut
3918    
3919    sub GroupPageName {
3920        # Get the parameters.
3921        my ($self, $group) = @_;
3922        # Check for the group file data.
3923        my %superTable = $self->CheckGroupFile();
3924        # Compute the real group name.
3925        my $realGroup = $self->SuperGroup($group);
3926        # Get the associated page name.
3927        my $retVal = "../content/$superTable{$realGroup}->{page}";
3928        # Return the result.
3929        return $retVal;
3930    }
3931    
3932    
3933    =head3 AddProperty
3934    
3935        $sprout->AddProperty($featureID, $key, @values);
3936    
3937    Add a new attribute value (Property) to a feature.
3938    
3939    =over 4
3940    
3941    =item peg
3942    
3943    ID of the feature to which the attribute is to be added.
3944    
3945    =item key
3946    
3947    Name of the attribute (key).
3948    
3949    =item values
3950    
3951    Values of the attribute.
3952    
3953    =back
3954    
3955    =cut
3956    #: Return Type ;
3957    sub AddProperty {
3958        # Get the parameters.
3959        my ($self, $featureID, $key, @values) = @_;
3960        # Add the property using the attached attributes object.
3961        $self->ca->AddAttribute($featureID, $key, @values);
3962    }
3963    
3964    =head3 CheckGroupFile
3965    
3966        my %groupData = $sprout->CheckGroupFile();
3967    
3968    Get the group file hash. The group file hash describes the relationship
3969    between a group and the super-group to which it belongs for purposes of
3970    display. The super-group name is computed from the first capitalized word
3971    in the actual group name. For each super-group, the group file contains
3972    the page name and a list of the species expected to be in the group.
3973    Each species is specified by a genus and a species name. A species name
3974    of C<0> implies an entire genus.
3975    
3976    This method returns a hash from super-group names to a hash reference. Each
3977    resulting hash reference contains the following fields.
3978    
3979    =over 4
3980    
3981    =item page
3982    
3983    The super-group's web page in the NMPDR.
3984    
3985    =item contents
3986    
3987    A list of 2-tuples, each containing a genus name followed by a species name
3988    (or 0, indicating all species). This list indicates which organisms belong
3989    in the super-group.
3990    
3991    =back
3992    
3993    =cut
3994    
3995    sub CheckGroupFile {
3996        # Get the parameters.
3997        my ($self) = @_;
3998        # Check to see if we already have this hash.
3999        if (! defined $self->{groupHash}) {
4000            # We don't, so we need to read it in.
4001            my %groupHash;
4002            # Read the group file.
4003            my @groupLines = Tracer::GetFile("$FIG_Config::sproutData/groups.tbl");
4004            # Loop through the list of sort-of groups.
4005            for my $groupLine (@groupLines) {
4006                my ($name, $page, @contents) = split /\t/, $groupLine;
4007                $groupHash{$name} = { page => $page,
4008                                      contents => [ map { [ split /\s*,\s*/, $_ ] } @contents ]
4009                                    };
4010            }
4011            # Save the hash.
4012            $self->{groupHash} = \%groupHash;
4013        }
4014        # Return the result.
4015        return %{$self->{groupHash}};
4016    }
4017    
4018    =head2 Virtual Methods
4019    
4020    =head3 CleanKeywords
4021    
4022        my $cleanedString = $sprout->CleanKeywords($searchExpression);
4023    
4024    Clean up a search expression or keyword list. This involves converting the periods
4025    in EC numbers to underscores, converting non-leading minus signs to underscores,
4026    a vertical bar or colon to an apostrophe, and forcing lower case for all alphabetic
4027    characters. In addition, any extra spaces are removed.
4028    
4029    =over 4
4030    
4031    =item searchExpression
4032    
4033    Search expression or keyword list to clean. Note that a search expression may
4034    contain boolean operators which need to be preserved. This includes leading
4035    minus signs.
4036    
4037    =item RETURN
4038    
4039    Cleaned expression or keyword list.
4040    
4041    =back
4042    
4043    =cut
4044    
4045    sub CleanKeywords {
4046        # Get the parameters.
4047        my ($self, $searchExpression) = @_;
4048        # Get the stemmer.
4049        my $stemmer = $self->GetStemmer();
4050        # Convert the search expression using the stemmer.
4051        my $retVal = $stemmer->PrepareSearchExpression($searchExpression);
4052        Trace("Cleaned keyword list for \"$searchExpression\" is \"$retVal\".") if T(3);
4053        # Return the result.
4054        return $retVal;
4055    }
4056    
4057    =head3 GetSourceObject
4058    
4059        my $source = $erdb->GetSourceObject();
4060    
4061    Return the object to be used in creating load files for this database.
4062    
4063    =cut
4064    
4065    sub GetSourceObject {
4066        # Get the parameters.
4067        my ($self) = @_;
4068        # Check to see if we already have a source object.
4069        my $retVal = $self->{_fig};
4070