[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.8, Thu Jan 27 00:30:20 2005 UTC revision 1.116, Tue Sep 9 21:02:10 2008 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 DBQuery;
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;
18        use WikiTools;
19        use base qw(ERDB);
20    
21  =head1 Sprout Database Manipulation Object  =head1 Sprout Database Manipulation Object
22    
# Line 25  Line 29 
29  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>
30  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>.
31    
32  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' });
33    
34  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
35  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
36  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
37  L</dna_seq> returns the DNA sequence for a specified genome location.  L</DNASeq> returns the DNA sequence for a specified genome location.
38    
39    The Sprout object is a subclass of the ERDB object and inherits all its properties and methods.
40    
41  =cut  =cut
42    
# Line 40  Line 46 
46    
47  =head3 new  =head3 new
48    
49  C<< my $sprout = Sprout->new($dbName, \%options); >>      my $sprout = Sprout->new($dbName, \%options);
50    
51  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
52  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 62  Line 68 
68    
69  * 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>)
70    
71  * 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)
72    
73  * B<port> connection port (default C<0>)  * B<port> connection port (default C<0>)
74    
75    * B<sock> connection socket (default same as SEED)
76    
77  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)
78    
79  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)
80    
81    * B<noDBOpen> suppresses the connection to the database if TRUE, else FALSE
82    
83    * B<host> name of the database host
84    
85  =back  =back
86    
87  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
88  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
89  F</usr/fig/SproutData>.  F</usr/fig/SproutData>.
90    
91  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' });
92    
93  =cut  =cut
94    
95  sub new {  sub new {
96          # Get the parameters.          # Get the parameters.
97          my ($class, $dbName, $options) = @_;          my ($class, $dbName, $options) = @_;
98        # Compute the DBD directory.
99        my $dbd_dir = (defined($FIG_Config::dbd_dir) ? $FIG_Config::dbd_dir :
100                                                      $FIG_Config::fig );
101          # 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
102          # the incoming data.          # the incoming data.
103          my $optionTable = Tracer::GetOptions({          my $optionTable = Tracer::GetOptions({
104                                             dbType               => 'mysql',                     # database type                         dbType       => $FIG_Config::dbms,
105                                             dataDir              => 'Data',                      # data file directory                                                          # database type
106                                             xmlFileName  => 'SproutDBD.xml', # database definition file name                         dataDir      => $FIG_Config::sproutData,
107                                             userData             => 'root/',                     # user name and password                                                          # data file directory
108                                             port                 => 0,                           # database connection port                         xmlFileName  => "$dbd_dir/SproutDBD.xml",
109                                                            # database definition file name
110                           userData     => "$FIG_Config::sproutUser/$FIG_Config::sproutPass",
111                                                            # user name and password
112                           port         => $FIG_Config::sproutPort,
113                                                            # database connection port
114                           sock         => $FIG_Config::sproutSock,
115                           host         => $FIG_Config::sprout_host,
116                                             maxSegmentLength => 4500,            # maximum feature segment length                                             maxSegmentLength => 4500,            # maximum feature segment length
117                                             maxSequenceLength => 8000,           # maximum contig sequence length                                             maxSequenceLength => 8000,           # maximum contig sequence length
118                           noDBOpen     => 0,               # 1 to suppress the database open
119                                            }, $options);                                            }, $options);
120          # Get the data directory.          # Get the data directory.
121          my $dataDir = $optionTable->{dataDir};          my $dataDir = $optionTable->{dataDir};
# Line 100  Line 123 
123          $optionTable->{userData} =~ m!([^/]*)/(.*)$!;          $optionTable->{userData} =~ m!([^/]*)/(.*)$!;
124          my ($userName, $password) = ($1, $2);          my ($userName, $password) = ($1, $2);
125          # Connect to the database.          # Connect to the database.
126          my $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName, $password, $optionTable->{port});      my $dbh;
127        if (! $optionTable->{noDBOpen}) {
128            Trace("Connect data: host = $optionTable->{host}, port = $optionTable->{port}.") if T(3);
129            $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,
130                                    $password, $optionTable->{port}, $optionTable->{host}, $optionTable->{sock});
131        }
132          # Create the ERDB object.          # Create the ERDB object.
133          my $xmlFileName = "$optionTable->{xmlFileName}";          my $xmlFileName = "$optionTable->{xmlFileName}";
134          my $erdb = ERDB->new($dbh, $xmlFileName);      my $retVal = ERDB::new($class, $dbh, $xmlFileName);
135          # Create this object.      # Add the option table and XML file name.
136          my $self = { _erdb => $erdb, _options => $optionTable, _xmlName => $xmlFileName };      $retVal->{_options} = $optionTable;
137          # Bless and return it.      $retVal->{_xmlName} = $xmlFileName;
138          bless $self;      # Set up space for the group file data.
139          return $self;      $retVal->{groupHash} = undef;
140        # Set up space for the genome hash. We use this to identify NMPDR genomes.
141        $retVal->{genomeHash} = undef;
142        # Connect to the attributes.
143        if ($FIG_Config::attrURL) {
144            Trace("Remote attribute server $FIG_Config::attrURL chosen.") if T(3);
145            $retVal->{_ca} = RemoteCustomAttributes->new($FIG_Config::attrURL);
146        } elsif ($FIG_Config::attrDbName) {
147            Trace("Local attribute database $FIG_Config::attrDbName chosen.") if T(3);
148            my $user = ($FIG_Config::arch eq 'win' ? 'self' : scalar(getpwent()));
149            $retVal->{_ca} = CustomAttributes->new(user => $user);
150        }
151        # Return it.
152        return $retVal;
153    }
154    
155    =head3 CoreGenomes
156    
157        my @genomes = $sprout->CoreGenomes($scope);
158    
159    Return the IDs of NMPDR genomes in the specified scope.
160    
161    =over 4
162    
163    =item scope
164    
165    Scope of the desired genomes. C<core> covers the original core genomes,
166    C<nmpdr> covers all genomes in NMPDR groups, and C<all> covers all
167    genomes in the system.
168    
169    =item RETURN
170    
171    Returns a list of the IDs for the genomes in the specified scope.
172    
173    =back
174    
175    =cut
176    
177    sub CoreGenomes {
178        # Get the parameters.
179        my ($self, $scope) = @_;
180        # Declare the return variable.
181        my @retVal = ();
182        # If we want all genomes, then this is easy.
183        if ($scope eq 'all') {
184            @retVal = $self->Genomes();
185        } else {
186            # Here we're dealing with groups. Get the hash of all the
187            # genome groups.
188            my %groups = $self->GetGroups();
189            # Loop through the groups, keeping the ones that we want.
190            for my $group (keys %groups) {
191                # Decide if we want to keep this group.
192                my $keepGroup = 0;
193                if ($scope eq 'nmpdr') {
194                    # NMPDR mode: keep all groups.
195                    $keepGroup = 1;
196                } elsif ($scope eq 'core') {
197                    # CORE mode. Only keep real core groups.
198                    if (grep { $group =~ /$_/ } @{$FIG_Config::realCoreGroups}) {
199                        $keepGroup = 1;
200                    }
201                }
202                # Add this group if we're keeping it.
203                if ($keepGroup) {
204                    push @retVal, @{$groups{$group}};
205                }
206            }
207        }
208        # Return the result.
209        return @retVal;
210    }
211    
212    =head3 SuperGroup
213    
214        my $superGroup = $sprout->SuperGroup($groupName);
215    
216    Return the name of the super-group containing the specified NMPDR genome
217    group. If no appropriate super-group can be found, an error will be
218    thrown.
219    
220    =over 4
221    
222    =item groupName
223    
224    Name of the group whose super-group is desired.
225    
226    =item RETURN
227    
228    Returns the name of the super-group containing the incoming group.
229    
230    =back
231    
232    =cut
233    
234    sub SuperGroup {
235        # Get the parameters.
236        my ($self, $groupName) = @_;
237        # Declare the return variable.
238        my $retVal;
239        # Get the group hash.
240        my %groupHash = $self->CheckGroupFile();
241        # Find the super-group genus.
242        $groupName =~ /([A-Z]\w+)/;
243        my $nameThing = $1;
244        # See if it's directly in the group hash.
245        if (exists $groupHash{$nameThing}) {
246            # Yes, then it's our result.
247            $retVal = $nameThing;
248        } else {
249            # No, so we have to search.
250            for my $superGroup (keys %groupHash) {
251                # Get this super-group's item list.
252                my $list = $groupHash{$superGroup}->{contents};
253                # Search it.
254                if (grep { $_->[0] eq $nameThing } @{$list}) {
255                    $retVal = $superGroup;
256                }
257            }
258            # Make sure we found something.
259            if (! $retVal) {
260                Confess("No super-group found for \"$groupName\".");
261            }
262        }
263        # Return the result.
264        return $retVal;
265  }  }
266    
267  =head3 MaxSegment  =head3 MaxSegment
268    
269  C<< my $length = $sprout->MaxSegment(); >>      my $length = $sprout->MaxSegment();
270    
271  This method returns the maximum permissible length of a feature segment. The length is important  This method returns the maximum permissible length of a feature segment. The length is important
272  because it enables us to make reasonable guesses at how to find features inside a particular  because it enables us to make reasonable guesses at how to find features inside a particular
# Line 124  Line 277 
277  =cut  =cut
278  #: Return Type $;  #: Return Type $;
279  sub MaxSegment {  sub MaxSegment {
280          my $self = shift @_;      my ($self) = @_;
281          return $self->{_options}->{maxSegmentLength};          return $self->{_options}->{maxSegmentLength};
282  }  }
283    
284  =head3 MaxSequence  =head3 MaxSequence
285    
286  C<< my $length = $sprout->MaxSequence(); >>      my $length = $sprout->MaxSequence();
287    
288  This method returns the maximum permissible length of a contig sequence. A contig is broken  This method returns the maximum permissible length of a contig sequence. A contig is broken
289  into sequences in order to save memory resources. In particular, when manipulating features,  into sequences in order to save memory resources. In particular, when manipulating features,
# Line 139  Line 292 
292  =cut  =cut
293  #: Return Type $;  #: Return Type $;
294  sub MaxSequence {  sub MaxSequence {
295          my $self = shift @_;      my ($self) = @_;
296          return $self->{_options}->{maxSequenceLength};          return $self->{_options}->{maxSequenceLength};
297  }  }
298    
299  =head3 Get  =head3 Load
   
 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.  
300    
301  C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ?", [$genus]); >>      $sprout->Load($rebuild);;
302    
303  The WHERE clause contains a single question mark, so there is a single additional  Load the database from files in the data directory, optionally re-creating the tables.
 parameter representing the parameter value. It would also be possible to code  
304    
305  C<< $query = $sprout->Get(['Genome'], "Genome(genus) = \'$genus\'"); >>  This method always deletes the data from the database before loading, even if the tables are not
306    re-created. The data is loaded into the relations from files in the data directory either having the
307    same name as the target relation with no extension or with an extension of C<.dtx>. Files without an
308    extension are used in preference to the files with an extension.
309    
310  however, this version of the call would generate a syntax error if there were any quote  The files are loaded based on the presumption that each line of the file is a record in the
311  characters inside the variable C<$genus>.  relation, and the individual fields are delimited by tabs. Tab and new-line characters inside
312    fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must
313    be presented in the order given in the relation tables produced by the L</ShowMetaData> method.
314    
315  The use of the strange parenthesized notation for field names enables us to distinguish  =over 4
 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.  
316    
317  It is possible to specify multiple entity and relationship names in order to retrieve more than  =item rebuild
 one object's data at the same time, which allows highly complex joined queries. For example,  
318    
319  C<< $query = $sprout->Get(['Genome', 'ComesFrom', 'Source'], "Genome(genus) = ?", [$genus]); >>  TRUE if the data tables need to be created or re-created, else FALSE
320    
321  This query returns all the genomes for a particular genus and allows access to the  =item RETURN
 sources from which they came. The join clauses to go from Genome to Source are generated  
 automatically.  
322    
323  Finally, the filter clause can contain sort information. To do this, simply put an C<ORDER BY>  Returns a statistical object containing the number of records read, the number of duplicates found,
324  clause at the end of the filter. Field references in the ORDER BY section follow the same rules  the number of errors, and a list of the error messages.
 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.  
325    
326  C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ? ORDER BY Genome(species)", [$genus]); >>  =back
327    
328  It is also permissible to specify I<only> an ORDER BY clause. For example, the following invocation gets  =cut
329  all genomes ordered by genus and species.  #: Return Type %;
330    sub Load {
331        # Get the parameters.
332        my ($self, $rebuild) = @_;
333        # Load the tables from the data directory.
334        my $retVal = $self->LoadTables($self->{_options}->{dataDir}, $rebuild);
335        # Return the statistics.
336        return $retVal;
337    }
338    
339  C<< $query = $sprout->Get(['Genome'], "ORDER BY Genome(genus), Genome(species)"); >>  =head3 LoadUpdate
340    
341  Odd things may happen if one of the ORDER BY fields is in a secondary relation. So, for example, an      my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList);
 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.  
342    
343  If multiple names are specified, then the query processor will automatically determine a  Load updates to one or more database tables. This method enables the client to make changes to one
344  join path between the entities and relationships. The algorithm used is very simplistic.  or two tables without reloading the whole database. For each table, there must be a corresponding
345  In particular, you can't specify any entity or relationship more than once, and if a  file in the data directory, either with the same name as the table, or with a C<.dtx> suffix. So,
346  relationship is recursive, the path is determined by the order in which the entity  for example, to make updates to the B<FeatureTranslation> relation, there must be a
347  and the relationship appear. For example, consider a recursive relationship B<IsParentOf>  C<FeatureTranslation.dtx> file in the data directory. Unlike a full load, files without an extension
348  which relates B<People> objects to other B<People> objects. If the join path is  are not examined. This allows update files to co-exist with files from an original load.
 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.  
349    
350  =over 4  =over 4
351    
352  =item objectNames  =item truncateFlag
   
 List containing the names of the entity and relationship objects to be retrieved.  
   
 =item filterClause  
353    
354  WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can  TRUE if the tables should be rebuilt before loading, else FALSE. A value of TRUE therefore causes
355  be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form  current data and schema of the tables to be replaced, while a value of FALSE means the new data
356  B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the  is added to the existing data in the various relations.
 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.  
357    
358  =item parameterList  =item tableList
359    
360  List of the parameters to be substituted in for the parameters marks in the filter clause.  List of the tables to be updated.
361    
362  =item RETURN  =item RETURN
363    
364  Returns a B<DBQuery> that can be used to iterate through all of the results.  Returns a statistical object containing the number of records read, the number of duplicates found,
365    the number of errors encountered, and a list of error messages.
366    
367  =back  =back
368    
369  =cut  =cut
370    #: Return Type $%;
371  sub Get {  sub LoadUpdate {
372          # Get the parameters.          # Get the parameters.
373          my $self = shift @_;      my ($self, $truncateFlag, $tableList) = @_;
374          my ($objectNames, $filterClause, $parameterList) = @_;      # Declare the return value.
375          # We differ from the ERDB Get method in that the parameter list is passed in as a list reference      my $retVal = Stats->new();
376          # rather than a list of parameters. The next step is to convert the parameters from a reference      # Get the data directory.
377          # to a real list. We can only do this if the parameters have been specified.      my $optionTable = $self->{_options};
378          my @parameters;      my $dataDir = $optionTable->{dataDir};
379          if ($parameterList) { @parameters = @{$parameterList}; }      # Loop through the incoming table names.
380          return $self->{_erdb}->Get($objectNames, $filterClause, @parameters);      for my $tableName (@{$tableList}) {
381            # Find the table's file.
382            my $fileName = LoadFileName($dataDir, $tableName);
383            if (! $fileName) {
384                Trace("No load file found for $tableName in $dataDir.") if T(0);
385            } else {
386                # Attempt to load this table.
387                my $result = $self->LoadTable($fileName, $tableName, truncate => $truncateFlag);
388                # Accumulate the resulting statistics.
389                $retVal->Accumulate($result);
390            }
391        }
392        # Return the statistics.
393        return $retVal;
394  }  }
395    
396  =head3 GetEntity  =head3 GenomeCounts
397    
398  C<< my $entityObject = $sprout->GetEntity($entityType, $ID); >>      my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete);
399    
400  Return an object describing the entity instance with a specified ID.  Count the number of genomes in each domain. If I<$complete> is TRUE, only complete
401    genomes will be included in the counts.
402    
403  =over 4  =over 4
404    
405  =item entityType  =item complete
406    
407  Entity type name.  TRUE if only complete genomes are to be counted, FALSE if all genomes are to be
408    counted
 =item ID  
   
 ID of the desired entity.  
409    
410  =item RETURN  =item RETURN
411    
412  Returns a B<DBObject> representing the desired entity instance, or an undefined value if no  A six-element list containing the number of genomes in each of six categories--
413  instance is found with the specified key.  Archaea, Bacteria, Eukaryota, Viral, Environmental, and Unknown, respectively.
414    
415  =back  =back
416    
417  =cut  =cut
418    
419  sub GetEntity {  sub GenomeCounts {
420          # Get the parameters.          # Get the parameters.
421          my $self = shift @_;      my ($self, $complete) = @_;
422          my ($entityType, $ID) = @_;      # Set the filter based on the completeness flag.
423          # Create a query.      my $filter = ($complete ? "Genome(complete) = 1" : "");
424          my $query = $self->Get([$entityType], "$entityType(id) = ?", [$ID]);      # Get all the genomes and the related taxonomy information.
425          # Get the first (and only) object.      my @genomes = $self->GetAll(['Genome'], $filter, [], ['Genome(id)', 'Genome(taxonomy)']);
426          my $retVal = $query->Fetch();      # Clear the counters.
427          # Return the result.      my ($arch, $bact, $euk, $vir, $env, $unk) = (0, 0, 0, 0, 0, 0);
428          return $retVal;      # Loop through, counting the domains.
429        for my $genome (@genomes) {
430            if    ($genome->[1] =~ /^archaea/i)  { ++$arch }
431            elsif ($genome->[1] =~ /^bacter/i)   { ++$bact }
432            elsif ($genome->[1] =~ /^eukar/i)    { ++$euk }
433            elsif ($genome->[1] =~ /^vir/i)      { ++$vir }
434            elsif ($genome->[1] =~ /^env/i)      { ++$env }
435            else  { ++$unk }
436        }
437        # Return the counts.
438        return ($arch, $bact, $euk, $vir, $env, $unk);
439  }  }
440    
441  =head3 GetEntityValues  =head3 ContigCount
442    
443  C<< my @values = GetEntityValues($entityType, $ID, \@fields); >>      my $count = $sprout->ContigCount($genomeID);
444    
445  Return a list of values from a specified entity instance.  Return the number of contigs for the specified genome ID.
446    
447  =over 4  =over 4
448    
449  =item entityType  =item genomeID
   
 Entity type name.  
   
 =item ID  
   
 ID of the desired entity.  
   
 =item fields  
450    
451  List of field names, each of the form I<objectName>C<(>I<fieldName>C<)>.  ID of the genome whose contig count is desired.
452    
453  =item RETURN  =item RETURN
454    
455  Returns a flattened list of the values of the specified fields for the specified entity.  Returns the number of contigs for the specified genome.
456    
457  =back  =back
458    
459  =cut  =cut
460  #: Return Type @;  
461  sub GetEntityValues {  sub ContigCount {
462          # Get the parameters.          # Get the parameters.
463          my $self = shift @_;      my ($self, $genomeID) = @_;
464          my ($entityType, $ID, $fields) = @_;      # Get the contig count.
465          # Get the specified entity.      my $retVal = $self->GetCount(['Contig', 'HasContig'], "HasContig(from-link) = ?", [$genomeID]);
         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);  
         }  
466          # Return the result.          # Return the result.
467          return @retVal;      return $retVal;
468  }  }
469    
470  =head3 ShowMetaData  =head3 GenomeMenu
471    
472  C<< $sprout->ShowMetaData($fileName); >>      my $html = $sprout->GenomeMenu(%options);
473    
474  This method outputs a description of the database to an HTML file in the data directory.  Generate a genome selection control with the specified name and options.
475    This control is almost but not quite the same as the genome control in the
476    B<SearchHelper> class. Eventually, the two will be combined.
477    
478  =over 4  =over 4
479    
480  =item fileName  =item options
481    
482    Optional parameters for the control (see below).
483    
484  Fully-qualified name to give to the output file.  =item RETURN
485    
486    Returns the HTML for a genome selection control on a form (sometimes called a popup menu).
487    
488  =back  =back
489    
490  =cut  The valid options are as follows.
491    
492  sub ShowMetaData {  =over 4
         # Get the parameters.  
         my $self = shift @_;  
         my ($fileName) = @_;  
         # Compute the file name.  
         my $options = $self->{_options};  
         # Call the show method on the underlying ERDB object.  
         $self->{_erdb}->ShowMetaData($fileName);  
 }  
493    
494  =head3 Load  =item name
495    
496  C<< $sprout->Load($rebuild); >>;  Name to give this control for use in passing it to the form. The default is C<myGenomeControl>.
497    Terrible things will happen if you have two controls with the same name on the same page.
498    
499  Load the database from files in the data directory, optionally re-creating the tables.  =item filter
500    
501  This method always deletes the data from the database before loading, even if the tables are not  If specified, a filter for the list of genomes to display. The filter should be in the form of a
502  re-created. The data is loaded into the relations from files in the data directory either having the  list reference. The first element of the list should be the filter string, and the remaining elements
503  same name as the target relation with no extension or with an extension of C<.dtx>. Files without an  the filter parameters.
 extension are used in preference to the files with an extension.  
504    
505  The files are loaded based on the presumption that each line of the file is a record in the  =item multiSelect
 relation, and the individual fields are delimited by tabs. Tab and new-line characters inside  
 fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must  
 be presented in the order given in the relation tables produced by the L</ShowMetaData> method.  
506    
507  =over 4  If TRUE, then the user can select multiple genomes. If FALSE, the user can only select one genome.
508    
509  =item rebuild  =item size
510    
511  TRUE if the data tables need to be created or re-created, else FALSE  Number of rows to display in the control. The default is C<10>
512    
513  =item RETURN  =item id
514    
515  Returns a statistical object containing the number of records read, the number of duplicates found,  ID to give this control. The default is the value of the C<name> option. Nothing will work correctly
516  the number of errors, and a list of the error messages.  unless this ID is unique.
517    
518    =item selected
519    
520    A comma-delimited list of selected genomes, or a reference to a list of selected genomes. The
521    default is none.
522    
523    =item class
524    
525    If specified, a style class to assign to the genome control.
526    
527  =back  =back
528    
529  =cut  =cut
530  #: Return Type %;  
531  sub Load {  sub GenomeMenu {
532          # Get the parameters.          # Get the parameters.
533          my $self = shift @_;      my ($self, %options) = @_;
534          my ($rebuild) = @_;      # Get the control's name and ID.
535          # Get the database object.      my $menuName = $options{name} || $options{id} || 'myGenomeControl';
536          my $erdb = $self->{_erdb};      my $menuID = $options{id} || $menuName;
537          # Load the tables from the data directory.      Trace("Genome menu name = $menuName with ID $menuID.") if T(3);
538          my $retVal = $erdb->LoadTables($self->{_options}->{dataDir}, $rebuild);      # Compute the IDs for the status display.
539          # Return the statistics.      my $divID = "${menuID}_status";
540        my $urlID = "${menuID}_url";
541        # Compute the code to show selected genomes in the status area.
542        my $showSelect = "showSelected('$menuID', '$divID', '$urlID', 1000)";
543        # Check for single-select or multi-select.
544        my $multiSelect = $options{multiSelect} || 0;
545        # Get the style data.
546        my $class = $options{class} || '';
547        # Get the list of pre-selected items.
548        my $selections = $options{selected} || [];
549        if (ref $selections ne 'ARRAY') {
550            $selections = [ split /\s*,\s*/, $selections ];
551        }
552        my %selected = map { $_ => 1 } @{$selections};
553        # Extract the filter information. The default is no filtering. It can be passed as a tab-delimited
554        # string or a list reference.
555        my $filterParms = $options{filter} || "";
556        if (! ref $filterParms) {
557            $filterParms = [split /\t|\\t/, $filterParms];
558        }
559        my $filterString = shift @{$filterParms};
560        # Get a list of all the genomes in group order. In fact, we only need them ordered
561        # by name (genus,species,strain), but putting primary-group in front enables us to
562        # take advantage of an existing index.
563        my @genomeList = $self->GetAll(['Genome'], "$filterString ORDER BY Genome(primary-group), Genome(genus), Genome(species), Genome(unique-characterization)",
564                                       $filterParms,
565                                       [qw(Genome(primary-group) Genome(id) Genome(genus) Genome(species) Genome(unique-characterization) Genome(taxonomy) Genome(contigs))]);
566        # Create a hash to organize the genomes by group. Each group will contain a list of
567        # 2-tuples, the first element being the genome ID and the second being the genome
568        # name.
569        my %gHash = ();
570        for my $genome (@genomeList) {
571            # Get the genome data.
572            my ($group, $genomeID, $genus, $species, $strain, $taxonomy, $contigs) = @{$genome};
573            # Compute its name. This is the genus, species, strain (if any), and the contig count.
574            my $name = "$genus $species ";
575            $name .= "$strain " if $strain;
576            my $contigCount = ($contigs == 1 ? "" : ", $contigs contigs");
577            # Now we get the domain. The domain tells us the display style of the organism.
578            my ($domain) = split /\s*;\s*/, $taxonomy, 2;
579            # Now compute the display group. This is normally the primary group, but if the
580            # organism is supporting, we blank it out.
581            my $displayGroup = ($group eq $FIG_Config::otherGroup ? "" : $group);
582            # Push the genome into the group's list. Note that we use the real group
583            # name for the hash key here, not the display group name.
584            push @{$gHash{$group}}, [$genomeID, $name, $contigCount, $domain];
585        }
586        # We are almost ready to unroll the menu out of the group hash. The final step is to separate
587        # the supporting genomes by domain. First, we extract the NMPDR groups and sort them. They
588        # are sorted by the first capitalized word. Groups with "other" are sorted after groups
589        # that aren't "other". At some point, we will want to make this less complicated.
590        my %sortGroups = map { $_ =~ /(other)?(.*)([A-Z].+)/; "$3$1$2" => $_ }
591                             grep { $_ ne $FIG_Config::otherGroup } keys %gHash;
592        my @groups = map { $sortGroups{$_} } sort keys %sortGroups;
593        # Remember the number of NMPDR groups.
594        my $nmpdrGroupCount = scalar @groups;
595        # Loop through the supporting genomes, classifying them by domain. We'll also keep a list
596        # of the domains found.
597        my @otherGenomes = @{$gHash{$FIG_Config::otherGroup}};
598        my @domains = ();
599        for my $genomeData (@otherGenomes) {
600            my ($genomeID, $name, $contigCount, $domain) = @{$genomeData};
601            if (exists $gHash{$domain}) {
602                push @{$gHash{$domain}}, $genomeData;
603            } else {
604                $gHash{$domain} = [$genomeData];
605                push @domains, $domain;
606            }
607        }
608        # Add the domain groups at the end of the main group list. The main group list will now
609        # contain all the categories we need to display the genomes.
610        push @groups, sort @domains;
611        # Delete the supporting group.
612        delete $gHash{$FIG_Config::otherGroup};
613        # Now it gets complicated. We need a way to mark all the NMPDR genomes. We take advantage
614        # of the fact they come first in the list. We'll accumulate a count of the NMPDR genomes
615        # and use that to make the selections.
616        my $nmpdrCount = 0;
617        # Create the type counters.
618        my $groupCount = 1;
619        # Get the number of rows to display.
620        my $rows = $options{size} || 10;
621        # If we're multi-row, create an onChange event.
622        my $onChangeTag = ( $rows > 1 ? " onChange=\"$showSelect;\" onFocus=\"$showSelect;\"" : "" );
623        # Set up the multiple-select flag.
624        my $multipleTag = ($multiSelect ? " multiple" : "" );
625        # Set up the style class.
626        my $classTag = ($class ? " class=\"$class\"" : "" );
627        # Create the SELECT tag and stuff it into the output array.
628        my @lines = ("<SELECT name=\"$menuName\" id=\"$menuID\" $onChangeTag$multipleTag$classTag size=\"$rows\">");
629        # Loop through the groups.
630        for my $group (@groups) {
631            # Get the genomes in the group.
632            for my $genome (@{$gHash{$group}}) {
633                # If this is an NMPDR organism, we add an extra style and count it.
634                my $nmpdrStyle = "";
635                if ($nmpdrGroupCount > 0) {
636                    $nmpdrCount++;
637                    $nmpdrStyle = " Core";
638                }
639                # Get the organism ID, name, contig count, and domain.
640                my ($genomeID, $name, $contigCount, $domain) = @{$genome};
641                # See if we're pre-selected.
642                my $selectTag = ($selected{$genomeID} ? " SELECTED" : "");
643                # Compute the display name.
644                my $nameString = "$name ($genomeID$contigCount)";
645                # Generate the option tag.
646                my $optionTag = "<OPTION class=\"$domain$nmpdrStyle\" title=\"$group\" value=\"$genomeID\"$selectTag>$nameString</OPTION>";
647                push @lines, "    $optionTag";
648            }
649            # Record this group in the nmpdrGroup count. When that gets to 0, we've finished the NMPDR
650            # groups.
651            $nmpdrGroupCount--;
652        }
653        # Close the SELECT tag.
654        push @lines, "</SELECT>";
655        if ($rows > 1) {
656            # We're in a non-compact mode, so we need to add some selection helpers. First is
657            # the search box. This allows the user to type text and change which genomes are
658            # displayed. For multiple-select mode, we include a button that selects the displayed
659            # genes. For single-select mode, we use a plain label instead.
660            my $searchThingName = "${menuID}_SearchThing";
661            my $searchThingLabel = ($multiSelect ? "<INPUT type=\"button\" name=\"MacroSearch\" class=\"button\" value=\"Select genomes containing\" onClick=\"selectShowing('$menuID', '$searchThingName'); $showSelect;\" />"
662                                                 : "Show genomes containing");
663            push @lines, "<br />$searchThingLabel&nbsp;" .
664                         "<INPUT type=\"text\" id=\"$searchThingName\" name=\"$searchThingName\" size=\"30\" onKeyup=\"showTyped('$menuID', '$searchThingName');\" />" .
665                         Hint("GenomeControl", "Type here to filter the genomes displayed.") . "<br />";
666            # For multi-select mode, we also have buttons to set and clear selections.
667            if ($multiSelect) {
668                push @lines, "<INPUT type=\"button\" name=\"ClearAll\" class=\"bigButton\"  value=\"Clear All\" onClick=\"clearAll('$menuID'); $showSelect\" />";
669                push @lines, "<INPUT type=\"button\" name=\"SelectAll\" class=\"bigButton\" value=\"Select All\" onClick=\"selectAll('$menuID'); $showSelect\" />";
670                push @lines, "<INPUT type=\"button\" name=\"NMPDROnly\" class=\"bigButton\"  value=\"Select NMPDR\" onClick=\"selectSome('$menuID', $nmpdrCount, true); $showSelect;\" />";
671            }
672            # Add a hidden field we can use to generate organism page hyperlinks.
673            push @lines, "<INPUT type=\"hidden\" id=\"$urlID\" value=\"$FIG_Config::cgi_url/wiki/rest.cgi/NmpdrPlugin/SeedViewer?page=Organism;organism=\" />";
674            # Add the status display. This tells the user what's selected no matter where the list is scrolled.
675            push @lines, "<DIV id=\"$divID\" class=\"Panel\"></DIV>";
676        }
677        # Assemble all the lines into a string.
678        my $retVal = join("\n", @lines, "");
679        # Return the result.
680          return $retVal;          return $retVal;
681  }  }
682    
 =head3 LoadUpdate  
   
 C<< my %stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>  
683    
684  Load updates to one or more database tables. This method enables the client to make changes to one  =head3 Stem
 or two tables without reloading the whole database. For each table, there must be a corresponding  
 file in the data directory, either with the same name as the table, or with a C<.dtx> suffix. So,  
 for example, to make updates to the B<FeatureTranslation> relation, there must be a  
 C<FeatureTranslation.dtx> file in the data directory. Unlike a full load, files without an extension  
 are not examined. This allows update files to co-exist with files from an original load.  
685    
686  =over 4      my $stem = $sprout->Stem($word);
687    
688  =item truncateFlag  Return the stem of the specified word, or C<undef> if the word is not
689    stemmable. Note that even if the word is stemmable, the stem may be
690    the same as the original word.
691    
692  TRUE if the tables should be rebuilt before loading, else FALSE. A value of TRUE therefore causes  =over 4
 current data and schema of the tables to be replaced, while a value of FALSE means the new data  
 is added to the existing data in the various relations.  
693    
694  =item tableList  =item word
695    
696  List of the tables to be updated.  Word to convert into a stem.
697    
698  =item RETURN  =item RETURN
699    
700  Returns a statistical object containing the number of records read, the number of duplicates found,  Returns a stem of the word (which may be the word itself), or C<undef> if
701  the number of errors encountered, and a list of error messages.  the word is not stemmable.
702    
703  =back  =back
704    
705  =cut  =cut
706  #: Return Type $%;  
707  sub LoadUpdate {  sub Stem {
708          # Get the parameters.          # Get the parameters.
709          my $self = shift @_;      my ($self, $word) = @_;
710          my ($truncateFlag, $tableList) = @_;      # Declare the return variable.
711          # Get the database object.      my $retVal;
712          my $erdb = $self->{_erdb};      # See if it's stemmable.
713          # Declare the return value.      if ($word =~ /^[A-Za-z]+$/) {
714          my $retVal = Stats->new();          # Compute the stem. Currently, it's just the word.
715          # Get the data directory.          my $stem = $word;
716          my $optionTable = $self->{_options};          # Check to see if it's long enough.
717          my $dataDir = $optionTable->{dataDir};          if (length $stem >= 3) {
718          # Loop through the incoming table names.              # Yes, keep it.
719          for my $tableName (@{$tableList}) {              $retVal = $stem;
720                  # Find the table's file.          } else {
721                  my $fileName = "$dataDir/$tableName";              # No, use the original word.
722                  if (! -e $fileName) {              $retVal = $word;
                         $fileName = "$fileName.dtx";  
723                  }                  }
                 # Attempt to load this table.  
                 my $result = $erdb->LoadTable($fileName, $tableName, $truncateFlag);  
                 # Accumulate the resulting statistics.  
                 $retVal->Accumulate($result);  
724          }          }
725          # Return the statistics.      # Return the result.
726          return $retVal;          return $retVal;
727  }  }
728    
729    
730  =head3 Build  =head3 Build
731    
732  C<< $sprout->Build(); >>      $sprout->Build();
733    
734  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.
735  This method is useful when a database is brand new or when the database definition has  This method is useful when a database is brand new or when the database definition has
# Line 463  Line 739 
739  #: Return Type ;  #: Return Type ;
740  sub Build {  sub Build {
741          # Get the parameters.          # Get the parameters.
742          my $self = shift @_;      my ($self) = @_;
743          # Create the tables.          # Create the tables.
744          $self->{_erdb}->CreateTables;      $self->CreateTables();
745  }  }
746    
747  =head3 Genomes  =head3 Genomes
748    
749  C<< my @genomes = $sprout->Genomes(); >>      my @genomes = $sprout->Genomes();
750    
751  Return a list of all the genome IDs.  Return a list of all the genome IDs.
752    
# Line 478  Line 754 
754  #: Return Type @;  #: Return Type @;
755  sub Genomes {  sub Genomes {
756          # Get the parameters.          # Get the parameters.
757          my $self = shift @_;      my ($self) = @_;
758          # Get all the genomes.          # Get all the genomes.
759          my @retVal = $self->GetFlat(['Genome'], "", [], 'Genome(id)');          my @retVal = $self->GetFlat(['Genome'], "", [], 'Genome(id)');
760          # Return the list of IDs.          # Return the list of IDs.
# Line 487  Line 763 
763    
764  =head3 GenusSpecies  =head3 GenusSpecies
765    
766  C<< my $infoString = $sprout->GenusSpecies($genomeID); >>      my $infoString = $sprout->GenusSpecies($genomeID);
767    
768  Return the genus, species, and unique characterization for a genome.  Return the genus, species, and unique characterization for a genome.
769    
# Line 508  Line 784 
784  #: Return Type $;  #: Return Type $;
785  sub GenusSpecies {  sub GenusSpecies {
786          # Get the parameters.          # Get the parameters.
787          my $self = shift @_;      my ($self, $genomeID) = @_;
         my ($genomeID) = @_;  
788          # Get the data for the specified genome.          # Get the data for the specified genome.
789          my @values = $self->GetEntityValues('Genome', $genomeID, ['Genome(genus)', 'Genome(species)',          my @values = $self->GetEntityValues('Genome', $genomeID, ['Genome(genus)', 'Genome(species)',
790                                                                                                                            'Genome(unique-characterization)']);                                                                                                                            'Genome(unique-characterization)']);
# Line 520  Line 795 
795    
796  =head3 FeaturesOf  =head3 FeaturesOf
797    
798  C<< my @features = $sprout->FeaturesOf($genomeID, $ftype); >>      my @features = $sprout->FeaturesOf($genomeID, $ftype);
799    
800  Return a list of the features relevant to a specified genome.  Return a list of the features relevant to a specified genome.
801    
# Line 545  Line 820 
820  #: Return Type @;  #: Return Type @;
821  sub FeaturesOf {  sub FeaturesOf {
822          # Get the parameters.          # Get the parameters.
823          my $self = shift @_;      my ($self, $genomeID,$ftype) = @_;
         my ($genomeID,$ftype) = @_;  
824          # Get the features we want.          # Get the features we want.
825          my @features;          my @features;
826          if (!$ftype) {          if (!$ftype) {
# Line 566  Line 840 
840    
841  =head3 FeatureLocation  =head3 FeatureLocation
842    
843  C<< my @locations = $sprout->FeatureLocation($featureID); >>      my @locations = $sprout->FeatureLocation($featureID);
844    
845  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
846  will return a list of the locations. In a scalar context, it will return the locations as a space-  will return a list of the locations. In a scalar context, it will return the locations as a space-
# Line 590  Line 864 
864  =item RETURN  =item RETURN
865    
866  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
867  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
868    wasn't found.
869    
870  =back  =back
871    
872  =cut  =cut
873  #: Return Type @;  
 #: Return Type $;  
874  sub FeatureLocation {  sub FeatureLocation {
875          # Get the parameters.          # Get the parameters.
876          my $self = shift @_;      my ($self, $featureID) = @_;
877          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.  
878          my @retVal = ();          my @retVal = ();
879          # Set up the variables used to determine if we have adjacent segments. This initial setup will      # Get the feature record.
880          # not match anything.      my $object = $self->GetEntity('Feature', $featureID);
881          my ($prevContig, $prevBeg, $prevDir, $prevLen) = ("", 0, "0", 0);      # Only proceed if we found it.
882          # Loop through the query results, creating location specifiers.      if (defined $object) {
883          while (my $location = $query->Fetch()) {          # Get the location string.
884                  # Get the location parameters.          my $locString = $object->PrimaryValue('Feature(location-string)');
885                  my ($contigID, $beg, $dir, $len) = $location->Values(['IsLocatedIn(to-link)',          # Create the return list.
886                          '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";  
887          }          }
888          # Return the list in the format indicated by the context.          # Return the list in the format indicated by the context.
889          return (wantarray ? @retVal : join(' ', @retVal));      return (wantarray ? @retVal : join(',', @retVal));
890  }  }
891    
892  =head3 ParseLocation  =head3 ParseLocation
893    
894  C<< my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location); >>      my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location);
895    
896  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
897  length.  length.
# Line 658  Line 910 
910  =back  =back
911    
912  =cut  =cut
913  #: Return Type @;  
914  sub ParseLocation {  sub ParseLocation {
915          # Get the parameter.      # Get the parameter. Note that if we're called as an instance method, we ignore
916        # the first parameter.
917        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
918          my ($location) = @_;          my ($location) = @_;
919          # Parse it into segments.          # Parse it into segments.
920          $location =~ /^(.*)_(\d*)([+-_])(\d*)$/;      $location =~ /^(.+)_(\d+)([+\-_])(\d+)$/;
921          my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);          my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);
922          # If the direction is an underscore, convert it to a + or -.          # If the direction is an underscore, convert it to a + or -.
923          if ($dir eq "_") {          if ($dir eq "_") {
# Line 679  Line 933 
933          return ($contigID, $start, $dir, $len);          return ($contigID, $start, $dir, $len);
934  }  }
935    
 =head3 DNASeq  
936    
 C<< my $sequence = $sprout->DNASeq(\@locationList); >>  
937    
938  This method returns the DNA sequence represented by a list of locations. The list of locations  =head3 PointLocation
939  should be of the form returned by L</featureLocation> when in a list context. In other words,  
940  each location is of the form I<contigID>C<_>I<begin>I<dir>I<end>.      my $found = Sprout::PointLocation($location, $point);
941    
942    Return the offset into the specified location of the specified point on the contig. If
943    the specified point is before the location, a negative value will be returned. If it is
944    beyond the location, an undefined value will be returned. It is assumed that the offset
945    is for the location's contig. The location can either be new-style (using a C<+> or C<->
946    and a length) or old-style (using C<_> and start and end positions.
947    
948  =over 4  =over 4
949    
950  =item locationList  =item location
951    
952    A location specifier (see L</FeatureLocation> for a description).
953    
954    =item point
955    
956  List of location specifiers, each in the form I<contigID>C<_>I<begin>I<dir>I<end> (see  The offset into the contig of the point in which we're interested.
 L</FeatureLocation> for more about this format).  
957    
958  =item RETURN  =item RETURN
959    
960  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
961    number if the point is before the location, or an undefined value if the point is past
962    the location. If the length of the location is 0, this method will B<always> denote
963    that it is outside the location. The offset will always be relative to the left-most
964    position in the location.
965    
966  =back  =back
967    
968  =cut  =cut
969  #: Return Type $;  
970  sub DNASeq {  sub PointLocation {
971          # Get the parameters.      # Get the parameter. Note that if we're called as an instance method, we ignore
972          my $self = shift @_;      # the first parameter.
973          my ($locationList) = @_;      shift if UNIVERSAL::isa($_[0],__PACKAGE__);
974          # Create the return string.      my ($location, $point) = @_;
975          my $retVal = "";      # Parse out the location elements. Note that this works on both old-style and new-style
976          # Loop through the locations.      # locations.
977          for my $location (@{$locationList}) {      my ($contigID, $start, $dir, $len) = ParseLocation($location);
978                  # Set up a variable to contain the DNA at this location.      # Declare the return variable.
979                  my $locationDNA = "";      my $retVal;
980                  # Parse out the contig ID, the beginning point, the direction, and the end point.      # Compute the offset. The computation is dependent on the direction of the location.
981                  my ($contigID, $beg, $dir, $len) = ParseLocation($location);      my $offset = (($dir == '+') ? $point - $start : $point - ($start - $len + 1));
982                  # Now we must create a query to return all the sequences in the contig relevant to the region      # Return the offset if it's valid.
983                  # specified. First, we compute the start and stop points when reading through the sequences.      if ($offset < $len) {
984            $retVal = $offset;
985        }
986        # Return the offset found.
987        return $retVal;
988    }
989    
990    =head3 DNASeq
991    
992        my $sequence = $sprout->DNASeq(\@locationList);
993    
994    This method returns the DNA sequence represented by a list of locations. The list of locations
995    should be of the form returned by L</featureLocation> when in a list context. In other words,
996    each location is of the form I<contigID>C<_>I<begin>I<dir>I<end>.
997    
998    For example, the following would return the DNA sequence for contig C<83333.1:NC_000913>
999    between positions 1401 and 1532, inclusive.
1000    
1001        my $sequence = $sprout->DNASeq('83333.1:NC_000913_1401_1532');
1002    
1003    =over 4
1004    
1005    =item locationList
1006    
1007    List of location specifiers, each in the form I<contigID>C<_>I<begin>I<dir>I<len> or
1008    I<contigID>C<_>I<begin>C<_>I<end> (see L</FeatureLocation> for more about this format).
1009    
1010    =item RETURN
1011    
1012    Returns a string of nucleotides corresponding to the DNA segments in the location list.
1013    
1014    =back
1015    
1016    =cut
1017    #: Return Type $;
1018    sub DNASeq {
1019        # Get the parameters.
1020        my ($self, $locationList) = @_;
1021        # Create the return string.
1022        my $retVal = "";
1023        # Loop through the locations.
1024        for my $location (@{$locationList}) {
1025            # Set up a variable to contain the DNA at this location.
1026            my $locationDNA = "";
1027            # Parse out the contig ID, the beginning point, the direction, and the end point.
1028            my ($contigID, $beg, $dir, $len) = ParseLocation($location);
1029            # Now we must create a query to return all the sequences in the contig relevant to the region
1030            # specified. First, we compute the start and stop points when reading through the sequences.
1031                  # For a forward transcription, the start point is the beginning; for a backward transcription,                  # For a forward transcription, the start point is the beginning; for a backward transcription,
1032                  # 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
1033                  # before putting it in the return value.                  # before putting it in the return value.
1034                  my ($start, $stop);                  my ($start, $stop);
1035            Trace("Parse of \"$location\" is $beg$dir$len.") if T(SDNA => 4);
1036                  if ($dir eq "+") {                  if ($dir eq "+") {
1037                          $start = $beg;                          $start = $beg;
1038                          $stop = $beg + $len - 1;                          $stop = $beg + $len - 1;
1039                  } else {                  } else {
1040                          $start = $beg + $len + 1;              $start = $beg - $len + 1;
1041                          $stop = $beg;                          $stop = $beg;
1042                  }                  }
1043            Trace("Looking for sequences containing $start through $stop.") if T(SDNA => 4);
1044                  my $query = $self->Get(['IsMadeUpOf','Sequence'],                  my $query = $self->Get(['IsMadeUpOf','Sequence'],
1045                          "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .                          "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .
1046                          " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",                          " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",
# Line 738  Line 1052 
1052                                  $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',                                  $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',
1053                                                                     'IsMadeUpOf(len)']);                                                                     'IsMadeUpOf(len)']);
1054                          my $stopPosition = $startPosition + $sequenceLength;                          my $stopPosition = $startPosition + $sequenceLength;
1055                Trace("Sequence is from $startPosition to $stopPosition.") if T(SDNA => 4);
1056                          # Figure out the start point and length of the relevant section.                          # Figure out the start point and length of the relevant section.
1057                          my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);                          my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);
1058                          my $len = ($stopPosition <= $stop ? $stopPosition : $stop) - $startPosition - $pos1;              my $len1 = ($stopPosition < $stop ? $stopPosition : $stop) + 1 - $startPosition - $pos1;
1059                Trace("Position is $pos1 for length $len1.") if T(SDNA => 4);
1060                          # Add the relevant data to the location data.                          # Add the relevant data to the location data.
1061                          $locationDNA .= substr($sequenceData, $pos1, $len);              $locationDNA .= substr($sequenceData, $pos1, $len1);
1062                  }                  }
1063                  # 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.
1064                  if ($dir eq '+') {                  if ($dir eq '+') {
1065                          $retVal .= $locationDNA;                          $retVal .= $locationDNA;
1066                  } else {                  } else {
1067                          $locationDNA = join('', reverse split //, $locationDNA);              $retVal .= FIG::reverse_comp($locationDNA);
                         $retVal .= $locationDNA;  
1068                  }                  }
1069          }          }
1070          # Return the result.          # Return the result.
# Line 758  Line 1073 
1073    
1074  =head3 AllContigs  =head3 AllContigs
1075    
1076  C<< my @idList = $sprout->AllContigs($genomeID); >>      my @idList = $sprout->AllContigs($genomeID);
1077    
1078  Return a list of all the contigs for a genome.  Return a list of all the contigs for a genome.
1079    
# Line 778  Line 1093 
1093  #: Return Type @;  #: Return Type @;
1094  sub AllContigs {  sub AllContigs {
1095          # Get the parameters.          # Get the parameters.
1096          my $self = shift @_;      my ($self, $genomeID) = @_;
         my ($genomeID) = @_;  
1097          # Ask for the genome's Contigs.          # Ask for the genome's Contigs.
1098          my @retVal = $self->GetFlat(['HasContig'], "HasContig(from-link) = ?", [$genomeID],          my @retVal = $self->GetFlat(['HasContig'], "HasContig(from-link) = ?", [$genomeID],
1099                                                                  'HasContig(to-link)');                                                                  'HasContig(to-link)');
# Line 787  Line 1101 
1101          return @retVal;          return @retVal;
1102  }  }
1103    
1104    =head3 GenomeLength
1105    
1106        my $length = $sprout->GenomeLength($genomeID);
1107    
1108    Return the length of the specified genome in base pairs.
1109    
1110    =over 4
1111    
1112    =item genomeID
1113    
1114    ID of the genome whose base pair count is desired.
1115    
1116    =item RETURN
1117    
1118    Returns the number of base pairs in all the contigs of the specified
1119    genome.
1120    
1121    =back
1122    
1123    =cut
1124    
1125    sub GenomeLength {
1126        # Get the parameters.
1127        my ($self, $genomeID) = @_;
1128        # Declare the return variable.
1129        my $retVal = 0;
1130        # Get the genome's contig sequence lengths.
1131        my @lens = $self->GetFlat(['HasContig', 'IsMadeUpOf'], 'HasContig(from-link) = ?',
1132                           [$genomeID], 'IsMadeUpOf(len)');
1133        # Sum the lengths.
1134        map { $retVal += $_ } @lens;
1135        # Return the result.
1136        return $retVal;
1137    }
1138    
1139    =head3 FeatureCount
1140    
1141        my $count = $sprout->FeatureCount($genomeID, $type);
1142    
1143    Return the number of features of the specified type in the specified genome.
1144    
1145    =over 4
1146    
1147    =item genomeID
1148    
1149    ID of the genome whose feature count is desired.
1150    
1151    =item type
1152    
1153    Type of feature to count (eg. C<peg>, C<rna>, etc.).
1154    
1155    =item RETURN
1156    
1157    Returns the number of features of the specified type for the specified genome.
1158    
1159    =back
1160    
1161    =cut
1162    
1163    sub FeatureCount {
1164        # Get the parameters.
1165        my ($self, $genomeID, $type) = @_;
1166        # Compute the count.
1167        my $retVal = $self->GetCount(['HasFeature', 'Feature'],
1168                                    "HasFeature(from-link) = ? AND Feature(feature-type) = ?",
1169                                    [$genomeID, $type]);
1170        # Return the result.
1171        return $retVal;
1172    }
1173    
1174    =head3 GenomeAssignments
1175    
1176        my $fidHash = $sprout->GenomeAssignments($genomeID);
1177    
1178    Return a list of a genome's assigned features. The return hash will contain each
1179    assigned feature of the genome mapped to the text of its most recent functional
1180    assignment.
1181    
1182    =over 4
1183    
1184    =item genomeID
1185    
1186    ID of the genome whose functional assignments are desired.
1187    
1188    =item RETURN
1189    
1190    Returns a reference to a hash which maps each feature to its most recent
1191    functional assignment.
1192    
1193    =back
1194    
1195    =cut
1196    
1197    sub GenomeAssignments {
1198        # Get the parameters.
1199        my ($self, $genomeID) = @_;
1200        # Declare the return variable.
1201        my $retVal = {};
1202        # Query the genome's features.
1203        my $query = $self->Get(['HasFeature', 'Feature'], "HasFeature(from-link) = ?",
1204                               [$genomeID]);
1205        # Loop through the features.
1206        while (my $data = $query->Fetch) {
1207            # Get the feature ID and assignment.
1208            my ($fid, $assignment) = $data->Values(['Feature(id)', 'Feature(assignment)']);
1209            if ($assignment) {
1210                $retVal->{$fid} = $assignment;
1211            }
1212        }
1213        # Return the result.
1214        return $retVal;
1215    }
1216    
1217  =head3 ContigLength  =head3 ContigLength
1218    
1219  C<< my $length = $sprout->ContigLength($contigID); >>      my $length = $sprout->ContigLength($contigID);
1220    
1221  Compute the length of a contig.  Compute the length of a contig.
1222    
# Line 809  Line 1236 
1236  #: Return Type $;  #: Return Type $;
1237  sub ContigLength {  sub ContigLength {
1238          # Get the parameters.          # Get the parameters.
1239          my $self = shift @_;      my ($self, $contigID) = @_;
         my ($contigID) = @_;  
1240          # Get the contig's last sequence.          # Get the contig's last sequence.
1241          my $query = $self->Get(['IsMadeUpOf'],          my $query = $self->Get(['IsMadeUpOf'],
1242                  "IsMadeUpOf(from-link) = ? ORDER BY IsMadeUpOf(start-position) DESC",                  "IsMadeUpOf(from-link) = ? ORDER BY IsMadeUpOf(start-position) DESC",
# Line 821  Line 1247 
1247          # Set it from the sequence data, if any.          # Set it from the sequence data, if any.
1248          if ($sequence) {          if ($sequence) {
1249                  my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);                  my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);
1250                  $retVal = $start + $len;          $retVal = $start + $len - 1;
1251        }
1252        # Return the result.
1253        return $retVal;
1254    }
1255    
1256    =head3 ClusterPEGs
1257    
1258        my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs);
1259    
1260    Cluster the PEGs in a list according to the cluster coding scheme of the specified
1261    subsystem. In order for this to work properly, the subsystem object must have
1262    been used recently to retrieve the PEGs using the B<get_pegs_from_cell> or
1263    B<get_row> methods. This causes the cluster numbers to be pulled into the
1264    subsystem's color hash. If a PEG is not found in the color hash, it will not
1265    appear in the output sequence.
1266    
1267    =over 4
1268    
1269    =item sub
1270    
1271    Sprout subsystem object for the relevant subsystem, from the L</get_subsystem>
1272    method.
1273    
1274    =item pegs
1275    
1276    Reference to the list of PEGs to be clustered.
1277    
1278    =item RETURN
1279    
1280    Returns a list of the PEGs, grouped into smaller lists by cluster number.
1281    
1282    =back
1283    
1284    =cut
1285    #: Return Type $@@;
1286    sub ClusterPEGs {
1287        # Get the parameters.
1288        my ($self, $sub, $pegs) = @_;
1289        # Declare the return variable.
1290        my $retVal = [];
1291        # Loop through the PEGs, creating arrays for each cluster.
1292        for my $pegID (@{$pegs}) {
1293            my $clusterNumber = $sub->get_cluster_number($pegID);
1294            # Only proceed if the PEG is in a cluster.
1295            if ($clusterNumber >= 0) {
1296                # Push this PEG onto the sub-list for the specified cluster number.
1297                push @{$retVal->[$clusterNumber]}, $pegID;
1298            }
1299          }          }
1300          # Return the result.          # Return the result.
1301          return $retVal;          return $retVal;
# Line 829  Line 1303 
1303    
1304  =head3 GenesInRegion  =head3 GenesInRegion
1305    
1306  C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>      my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop);
1307    
1308  List the features which overlap a specified region in a contig.  List the features which overlap a specified region in a contig.
1309    
# Line 852  Line 1326 
1326  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
1327  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
1328  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
1329  the start and stop values.  the start and stop values. The first element (that is, the list of features) is sorted
1330    roughly by location.
1331    
1332  =back  =back
1333    
1334  =cut  =cut
1335  #: Return Type @;  
1336  sub GenesInRegion {  sub GenesInRegion {
1337          # Get the parameters.          # Get the parameters.
1338          my $self = shift @_;      my ($self, $contigID, $start, $stop) = @_;
1339          my ($contigID, $start, $stop) = @_;      # Get the maximum segment length.
1340        my $maximumSegmentLength = $self->MaxSegment;
1341        # Prime the values we'll use for the returned beginning and end.
1342        my @initialMinMax = ($self->ContigLength($contigID), 0);
1343        my ($min, $max) = @initialMinMax;
1344        # Get the overlapping features.
1345        my @featureObjects = $self->GeneDataInRegion($contigID, $start, $stop);
1346        # We'l use this hash to help us track the feature IDs and sort them. The key is the
1347        # feature ID and the value is a [$left,$right] pair indicating the maximum extent
1348        # of the feature's locations.
1349        my %featureMap = ();
1350        # Loop through them to do the begin/end analysis.
1351        for my $featureObject (@featureObjects) {
1352            # Get the feature's location string. This may contain multiple actual locations.
1353            my ($locations, $fid) = $featureObject->Values([qw(Feature(location-string) Feature(id))]);
1354            my @locationSegments = split /\s*,\s*/, $locations;
1355            # Loop through the locations.
1356            for my $locationSegment (@locationSegments) {
1357                # Construct an object for the location.
1358                my $locationObject = BasicLocation->new($locationSegment);
1359                # Merge the current segment's begin and end into the min and max.
1360                my ($left, $right) = ($locationObject->Left, $locationObject->Right);
1361                my ($beg, $end);
1362                if (exists $featureMap{$fid}) {
1363                    ($beg, $end) = @{$featureMap{$fid}};
1364                    $beg = $left if $left < $beg;
1365                    $end = $right if $right > $end;
1366                } else {
1367                    ($beg, $end) = ($left, $right);
1368                }
1369                $min = $beg if $beg < $min;
1370                $max = $end if $end > $max;
1371                # Store the feature's new extent back into the hash table.
1372                $featureMap{$fid} = [$beg, $end];
1373            }
1374        }
1375        # Now we must compute the list of the IDs for the features found. We start with a list
1376        # of midpoints / feature ID pairs. (It's not really a midpoint, it's twice the midpoint,
1377        # but the result of the sort will be the same.)
1378        my @list = map { [$featureMap{$_}->[0] + $featureMap{$_}->[1], $_] } keys %featureMap;
1379        # Now we sort by midpoint and yank out the feature IDs.
1380        my @retVal = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @list;
1381        # Return it along with the min and max.
1382        return (\@retVal, $min, $max);
1383    }
1384    
1385    =head3 GeneDataInRegion
1386    
1387        my @featureList = $sprout->GenesInRegion($contigID, $start, $stop);
1388    
1389    List the features which overlap a specified region in a contig.
1390    
1391    =over 4
1392    
1393    =item contigID
1394    
1395    ID of the contig containing the region of interest.
1396    
1397    =item start
1398    
1399    Offset of the first residue in the region of interest.
1400    
1401    =item stop
1402    
1403    Offset of the last residue in the region of interest.
1404    
1405    =item RETURN
1406    
1407    Returns a list of B<ERDBObjects> for the desired features. Each object will
1408    contain a B<Feature> record.
1409    
1410    =back
1411    
1412    =cut
1413    
1414    sub GeneDataInRegion {
1415        # Get the parameters.
1416        my ($self, $contigID, $start, $stop) = @_;
1417          # Get the maximum segment length.          # Get the maximum segment length.
1418          my $maximumSegmentLength = $self->MaxSegment;          my $maximumSegmentLength = $self->MaxSegment;
1419          # 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
1420          # duplicates easily.      # duplicates easily. The hash key will be the feature ID. The value will be the feature's
1421        # ERDBObject from the query.
1422          my %featuresFound = ();          my %featuresFound = ();
1423          # 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  
1424          # 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,
1425          # 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
1426          # because each feature segment length must be no greater than the maximum segment length.          # because each feature segment length must be no greater than the maximum segment length.
# Line 878  Line 1429 
1429          # Loop through the query parameters.          # Loop through the query parameters.
1430          for my $parms (values %queryParms) {          for my $parms (values %queryParms) {
1431                  # Create the query.                  # Create the query.
1432                  my $query = $self->Get(['IsLocatedIn'],          my $query = $self->Get([qw(Feature IsLocatedIn)],
1433                          "IsLocatedIn(to-link)= ? AND IsLocatedIn(dir) = ? AND IsLocatedIn(beg) >= ? AND IsLocatedIn(beg) <= ?",                          "IsLocatedIn(to-link)= ? AND IsLocatedIn(dir) = ? AND IsLocatedIn(beg) >= ? AND IsLocatedIn(beg) <= ?",
1434                          $parms);                          $parms);
1435                  # Loop through the feature segments found.                  # Loop through the feature segments found.
1436                  while (my $segment = $query->Fetch) {                  while (my $segment = $query->Fetch) {
1437                          # Get the data about this segment.                          # Get the data about this segment.
1438                          my ($featureID, $dir, $beg, $len) = $segment->Values(['IsLocatedIn(from-link)',              my ($featureID, $contig, $dir, $beg, $len) = $segment->Values([qw(IsLocatedIn(from-link)
1439                                  'IsLocatedIn(dir)', 'IsLocatedIn(beg)', 'IsLocatedIn(len)']);                  IsLocatedIn(to-link) IsLocatedIn(dir) IsLocatedIn(beg) IsLocatedIn(len))]);
1440                          # 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
1441                          # 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
1442                          # 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
1443                          # length.                          # length.
1444                          my ($found, $end) = (0, 0);              my $loc = BasicLocation->new($contig, $beg, $dir, $len);
1445                          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;  
                                 }  
                         }  
1446                          if ($found) {                          if ($found) {
1447                                  # Here we need to record the feature and update the minimum and maximum.                  # Save this feature in the result list.
1448                                  $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; }  
1449                          }                          }
1450                  }                  }
1451          }          }
1452          # Compute a list of the IDs for the features found.      # Return the ERDB objects for the features found.
1453          my @list = (sort (keys %featuresFound));      return values %featuresFound;
         # Return it along with the min and max.  
         return (\@list, $min, $max);  
1454  }  }
1455    
1456  =head3 FType  =head3 FType
1457    
1458  C<< my $ftype = $sprout->FType($featureID); >>      my $ftype = $sprout->FType($featureID);
1459    
1460  Return the type of a feature.  Return the type of a feature.
1461    
# Line 943  Line 1476 
1476  #: Return Type $;  #: Return Type $;
1477  sub FType {  sub FType {
1478          # Get the parameters.          # Get the parameters.
1479          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
1480          # Get the specified feature's type.          # Get the specified feature's type.
1481          my ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(feature-type)']);          my ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(feature-type)']);
1482          # Return the result.          # Return the result.
# Line 953  Line 1485 
1485    
1486  =head3 FeatureAnnotations  =head3 FeatureAnnotations
1487    
1488  C<< my @descriptors = $sprout->FeatureAnnotations($featureID); >>      my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag);
1489    
1490  Return the annotations of a feature.  Return the annotations of a feature.
1491    
# Line 963  Line 1495 
1495    
1496  ID of the feature whose annotations are desired.  ID of the feature whose annotations are desired.
1497    
1498    =item rawFlag
1499    
1500    If TRUE, the annotation timestamps will be returned in raw form; otherwise, they
1501    will be returned in human-readable form.
1502    
1503  =item RETURN  =item RETURN
1504    
1505  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.
1506    
1507  * B<featureID> ID of the relevant feature.  * B<featureID> ID of the relevant feature.
1508    
1509  * B<timeStamp> time the annotation was made, in user-friendly format.  * B<timeStamp> time the annotation was made.
1510    
1511  * B<user> ID of the user who made the annotation  * B<user> ID of the user who made the annotation
1512    
# Line 981  Line 1518 
1518  #: Return Type @%;  #: Return Type @%;
1519  sub FeatureAnnotations {  sub FeatureAnnotations {
1520          # Get the parameters.          # Get the parameters.
1521          my $self = shift @_;      my ($self, $featureID, $rawFlag) = @_;
         my ($featureID) = @_;  
1522          # 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.
1523          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1524                                                     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);                                                     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);
# Line 995  Line 1531 
1531                          $annotation->Values(['IsTargetOfAnnotation(from-link)',                          $annotation->Values(['IsTargetOfAnnotation(from-link)',
1532                                                                   'Annotation(time)', 'MadeAnnotation(from-link)',                                                                   'Annotation(time)', 'MadeAnnotation(from-link)',
1533                                                                   'Annotation(annotation)']);                                                                   'Annotation(annotation)']);
1534            # Convert the time, if necessary.
1535            if (! $rawFlag) {
1536                $timeStamp = FriendlyTimestamp($timeStamp);
1537            }
1538                  # Assemble them into a hash.                  # Assemble them into a hash.
1539          my $annotationHash = { featureID => $featureID,          my $annotationHash = { featureID => $featureID,
1540                                 timeStamp => FriendlyTimestamp($timeStamp),                                 timeStamp => $timeStamp,
1541                                                             user => $user, text => $text };                                                             user => $user, text => $text };
1542                  # Add it to the return list.                  # Add it to the return list.
1543                  push @retVal, $annotationHash;                  push @retVal, $annotationHash;
# Line 1008  Line 1548 
1548    
1549  =head3 AllFunctionsOf  =head3 AllFunctionsOf
1550    
1551  C<< my %functions = $sprout->AllFunctionsOf($featureID); >>      my %functions = $sprout->AllFunctionsOf($featureID);
1552    
1553  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
1554  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,
1555  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
1556  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,
1557  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.
1558  features only have a small number of annotations. Finally, if a single user has multiple  Finally, if a single user has multiple functional assignments, we will only keep the most
1559  functional assignments, we will only keep the most recent one.  recent one.
1560    
1561  =over 4  =over 4
1562    
# Line 1026  Line 1566 
1566    
1567  =item RETURN  =item RETURN
1568    
1569  Returns a hash mapping the functional assignment IDs to user IDs.  Returns a hash mapping the user IDs to functional assignment IDs.
1570    
1571  =back  =back
1572    
# Line 1034  Line 1574 
1574  #: Return Type %;  #: Return Type %;
1575  sub AllFunctionsOf {  sub AllFunctionsOf {
1576          # Get the parameters.          # Get the parameters.
1577          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
1578          # Get all of the feature's annotations.          # Get all of the feature's annotations.
1579      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation'],      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1580                                                      "IsTargetOfAnnotation(from-link) = ?",                                                      "IsTargetOfAnnotation(from-link) = ?",
1581                              [$featureID], ['Annotation(time)', 'Annotation(annotation)']);                              [$featureID], ['Annotation(time)', 'Annotation(annotation)',
1582                                               'MadeAnnotation(from-link)']);
1583          # Declare the return hash.          # Declare the return hash.
1584          my %retVal;          my %retVal;
     # Declare a hash for insuring we only make one assignment per user.  
     my %timeHash = ();  
1585      # Now we sort the assignments by timestamp in reverse.      # Now we sort the assignments by timestamp in reverse.
1586      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;
1587          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1588      for my $annotation (@sortedQuery) {      for my $annotation (@sortedQuery) {
1589          # Get the annotation fields.          # Get the annotation fields.
1590          my ($timeStamp, $text) = @{$annotation};          my ($timeStamp, $text, $user) = @{$annotation};
1591                  # Check to see if this is a functional assignment.                  # Check to see if this is a functional assignment.
1592                  my ($user, $function) = ParseAssignment($text);          my ($actualUser, $function) = _ParseAssignment($user, $text);
1593          if ($user && ! exists $timeHash{$user}) {          if ($actualUser && ! exists $retVal{$actualUser}) {
1594              # Here it is a functional assignment and there has been no              # Here it is a functional assignment and there has been no
1595              # previous assignment for this user, so we stuff it in the              # previous assignment for this user, so we stuff it in the
1596              # return hash.              # return hash.
1597                          $retVal{$function} = $user;              $retVal{$actualUser} = $function;
             # Insure we don't assign to this user again.  
             $timeHash{$user} = 1;  
1598                  }                  }
1599          }          }
1600          # Return the hash of assignments found.          # Return the hash of assignments found.
# Line 1067  Line 1603 
1603    
1604  =head3 FunctionOf  =head3 FunctionOf
1605    
1606  C<< my $functionText = $sprout->FunctionOf($featureID, $userID); >>      my $functionText = $sprout->FunctionOf($featureID, $userID);
1607    
1608  Return the most recently-determined functional assignment of a particular feature.  Return the most recently-determined functional assignment of a particular feature.
1609    
1610  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
1611  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
1612  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.  
1613    
1614  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
1615  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
# Line 1095  Line 1628 
1628    
1629  =item userID (optional)  =item userID (optional)
1630    
1631  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
1632  C<FIG> assignment will be returned.  functional assignment in the B<Feature> table will be returned.
1633    
1634  =item RETURN  =item RETURN
1635    
# Line 1108  Line 1641 
1641  #: Return Type $;  #: Return Type $;
1642  sub FunctionOf {  sub FunctionOf {
1643          # Get the parameters.          # Get the parameters.
1644          my $self = shift @_;      my ($self, $featureID, $userID) = @_;
         my ($featureID, $userID) = @_;  
1645      # Declare the return value.      # Declare the return value.
1646      my $retVal;      my $retVal;
1647      # Determine the ID type.      # Determine the ID type.
1648      if ($featureID =~ m/^fig\|/) {      if ($featureID =~ m/^fig\|/) {
1649          # Here we have a FIG feature ID. We must build the list of trusted          # Here we have a FIG feature ID.
1650          # users.          if (!$userID) {
1651                # Use the primary assignment.
1652                ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(assignment)']);
1653            } else {
1654                # We must build the list of trusted users.
1655          my %trusteeTable = ();          my %trusteeTable = ();
1656          # Check the user ID.          # Check the user ID.
1657          if (!$userID) {          if (!$userID) {
# Line 1137  Line 1673 
1673              }              }
1674          }          }
1675          # 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.
1676          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation'],              my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1677                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1678                                 [$featureID]);                                 [$featureID]);
1679          my $timeSelected = 0;          my $timeSelected = 0;
1680          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1681          while (my $annotation = $query->Fetch()) {          while (my $annotation = $query->Fetch()) {
1682              # Get the annotation text.              # Get the annotation text.
1683              my ($text, $time) = $annotation->Values(['Annotation(annotation)','Annotation(time)']);                  my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1684                                                             'Annotation(time)', 'MadeAnnotation(from-link)']);
1685              # 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.
1686              my ($user, $type, $function) = split(/\n/, $text);                  my ($actualUser, $function) = _ParseAssignment($user, $text);
1687              if ($type =~ m/^set $user function to$/i) {                  Trace("Assignment user is $actualUser, text is $function.") if T(4);
1688                    if ($actualUser) {
1689                  # Here it is a functional assignment. Check the time and the user                  # Here it is a functional assignment. Check the time and the user
1690                  # name. The time must be recent and the user must be trusted.                  # name. The time must be recent and the user must be trusted.
1691                  if ((exists $trusteeTable{$user}) && ($time > $timeSelected)) {                      if ((exists $trusteeTable{$actualUser}) && ($time > $timeSelected)) {
1692                      $retVal = $function;                      $retVal = $function;
1693                      $timeSelected = $time;                      $timeSelected = $time;
1694                  }                  }
1695              }              }
1696          }          }
1697            }
1698      } else {      } else {
1699          # Here we have a non-FIG feature ID. In this case the user ID does not          # Here we have a non-FIG feature ID. In this case the user ID does not
1700          # matter. We simply get the information from the External Alias Function          # matter. We simply get the information from the External Alias Function
# Line 1166  Line 1705 
1705          return $retVal;          return $retVal;
1706  }  }
1707    
1708    =head3 FunctionsOf
1709    
1710        my @functionList = $sprout->FunctionOf($featureID, $userID);
1711    
1712    Return the functional assignments of a particular feature.
1713    
1714    The functional assignment is handled differently depending on the type of feature. If
1715    the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional
1716    assignment is a type of annotation. The format of an assignment is described in
1717    L</ParseAssignment>. Its worth noting that we cannot filter on the content of the
1718    annotation itself because it's a text field; however, this is not a big problem because
1719    most features only have a small number of annotations.
1720    
1721    If the feature is B<not> identified by a FIG ID, then the functional assignment
1722    information is taken from the B<ExternalAliasFunc> table. If the table does
1723    not contain an entry for the feature, an empty list is returned.
1724    
1725    =over 4
1726    
1727    =item featureID
1728    
1729    ID of the feature whose functional assignments are desired.
1730    
1731    =item RETURN
1732    
1733    Returns a list of 2-tuples, each consisting of a user ID and the text of an assignment by
1734    that user.
1735    
1736    =back
1737    
1738    =cut
1739    #: Return Type @@;
1740    sub FunctionsOf {
1741        # Get the parameters.
1742        my ($self, $featureID) = @_;
1743        # Declare the return value.
1744        my @retVal = ();
1745        # Determine the ID type.
1746        if ($featureID =~ m/^fig\|/) {
1747            # Here we have a FIG feature ID. We must build the list of trusted
1748            # users.
1749            my %trusteeTable = ();
1750            # Build a query for all of the feature's annotations, sorted by date.
1751            my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1752                                   "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1753                                   [$featureID]);
1754            my $timeSelected = 0;
1755            # Loop until we run out of annotations.
1756            while (my $annotation = $query->Fetch()) {
1757                # Get the annotation text.
1758                my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1759                                                                'Annotation(time)',
1760                                                                'MadeAnnotation(user)']);
1761                # Check to see if this is a functional assignment for a trusted user.
1762                my ($actualUser, $function) = _ParseAssignment($user, $text);
1763                if ($actualUser) {
1764                    # Here it is a functional assignment.
1765                    push @retVal, [$actualUser, $function];
1766                }
1767            }
1768        } else {
1769            # Here we have a non-FIG feature ID. In this case the user ID does not
1770            # matter. We simply get the information from the External Alias Function
1771            # table.
1772            my @assignments = $self->GetEntityValues('ExternalAliasFunc', $featureID,
1773                                                     ['ExternalAliasFunc(func)']);
1774            push @retVal, map { ['master', $_] } @assignments;
1775        }
1776        # Return the assignments found.
1777        return @retVal;
1778    }
1779    
1780  =head3 BBHList  =head3 BBHList
1781    
1782  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>      my $bbhHash = $sprout->BBHList($genomeID, \@featureList);
1783    
1784  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
1785  on a specified target genome.  on a specified target genome.
# Line 1185  Line 1796 
1796    
1797  =item RETURN  =item RETURN
1798    
1799  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
1800  their best hits.  on the target genome.
1801    
1802  =back  =back
1803    
# Line 1194  Line 1805 
1805  #: Return Type %;  #: Return Type %;
1806  sub BBHList {  sub BBHList {
1807          # Get the parameters.          # Get the parameters.
1808          my $self = shift @_;      my ($self, $genomeID, $featureList) = @_;
         my ($genomeID, $featureList) = @_;  
1809          # Create the return structure.          # Create the return structure.
1810          my %retVal = ();          my %retVal = ();
1811          # Loop through the incoming features.          # Loop through the incoming features.
1812          for my $featureID (@{$featureList}) {          for my $featureID (@{$featureList}) {
1813                  # Create a query to get the feature's best hit.          # Ask the server for the feature's best hit.
1814                  my $query = $self->Get(['IsBidirectionalBestHitOf'],          my @bbhData = FIGRules::BBHData($featureID);
1815                                                             "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?",          # Peel off the BBHs found.
1816                                                             [$featureID, $genomeID]);          my @found = ();
1817                  # Look for the best hit.          for my $bbh (@bbhData) {
1818                  my $bbh = $query->Fetch;              my $fid = $bbh->[0];
1819                  if ($bbh) {              my $bbGenome = $self->GenomeOf($fid);
1820                          my ($targetFeature) = $bbh->Value('IsBidirectionalBestHitOf(to-link)');              if ($bbGenome eq $genomeID) {
1821                          $retVal{$featureID} = $targetFeature;                  push @found, $fid;
1822                  }                  }
1823          }          }
1824            $retVal{$featureID} = \@found;
1825        }
1826          # Return the mapping.          # Return the mapping.
1827          return \%retVal;          return \%retVal;
1828  }  }
1829    
1830  =head3 FeatureAliases  =head3 SimList
1831    
1832  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>      my %similarities = $sprout->SimList($featureID, $count);
1833    
1834  Return a list of the aliases for a specified feature.  Return a list of the similarities to the specified feature.
1835    
1836    This method just returns the bidirectional best hits for performance reasons.
1837    
1838  =over 4  =over 4
1839    
1840  =item featureID  =item featureID
1841    
1842  ID of the feature whose aliases are desired.  ID of the feature whose similarities are desired.
1843    
1844  =item RETURN  =item count
1845    
1846  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.  
1847    
1848  =back  =back
1849    
1850  =cut  =cut
1851  #: Return Type @;  #: Return Type %;
1852  sub FeatureAliases {  sub SimList {
1853          # Get the parameters.          # Get the parameters.
1854          my $self = shift @_;      my ($self, $featureID, $count) = @_;
1855          my ($featureID) = @_;      # Ask for the best hits.
1856          # Get the desired feature's aliases      my @lists = FIGRules::BBHData($featureID);
1857          my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(alias)']);      # Create the return value.
1858        my %retVal = ();
1859        for my $tuple (@lists) {
1860            $retVal{$tuple->[0]} = $tuple->[1];
1861        }
1862          # Return the result.          # Return the result.
1863          return @retVal;      return %retVal;
1864  }  }
1865    
1866  =head3 GenomeOf  =head3 IsComplete
1867    
1868  C<< my $genomeID = $sprout->GenomeOf($featureID); >>      my $flag = $sprout->IsComplete($genomeID);
1869    
1870  Return the genome that contains a specified feature.  Return TRUE if the specified genome is complete, else FALSE.
1871    
1872  =over 4  =over 4
1873    
1874  =item featureID  =item genomeID
1875    
1876    ID of the genome whose completeness status is desired.
1877    
1878    =item RETURN
1879    
1880    Returns TRUE if the genome is complete, FALSE if it is incomplete, and C<undef> if it is
1881    not found.
1882    
1883    =back
1884    
1885    =cut
1886    #: Return Type $;
1887    sub IsComplete {
1888        # Get the parameters.
1889        my ($self, $genomeID) = @_;
1890        # Declare the return variable.
1891        my $retVal;
1892        # Get the genome's data.
1893        my $genomeData = $self->GetEntity('Genome', $genomeID);
1894        if ($genomeData) {
1895            # The genome exists, so get the completeness flag.
1896            $retVal = $genomeData->PrimaryValue('Genome(complete)');
1897        }
1898        # Return the result.
1899        return $retVal;
1900    }
1901    
1902    =head3 FeatureAliases
1903    
1904        my @aliasList = $sprout->FeatureAliases($featureID);
1905    
1906    Return a list of the aliases for a specified feature.
1907    
1908    =over 4
1909    
1910    =item featureID
1911    
1912    ID of the feature whose aliases are desired.
1913    
1914    =item RETURN
1915    
1916    Returns a list of the feature's aliases. If the feature is not found or has no aliases, it will
1917    return an empty list.
1918    
1919    =back
1920    
1921    =cut
1922    #: Return Type @;
1923    sub FeatureAliases {
1924        # Get the parameters.
1925        my ($self, $featureID) = @_;
1926        # Get the desired feature's aliases
1927        my @retVal = $self->GetFlat(['IsAliasOf'], "IsAliasOf(to-link) = ?", [$featureID], 'IsAliasOf(from-link)');
1928        # Return the result.
1929        return @retVal;
1930    }
1931    
1932    =head3 GenomeOf
1933    
1934        my $genomeID = $sprout->GenomeOf($featureID);
1935    
1936    Return the genome that contains a specified feature or contig.
1937    
1938  ID of the feature whose genome is desired.  =over 4
1939    
1940    =item featureID
1941    
1942    ID of the feature or contig whose genome is desired.
1943    
1944  =item RETURN  =item RETURN
1945    
1946  Returns the ID of the genome for the specified feature. If the feature is not found, returns  Returns the ID of the genome for the specified feature or contig. If the feature or contig is not
1947  an undefined value.  found, returns an undefined value.
1948    
1949  =back  =back
1950    
# Line 1269  Line 1952 
1952  #: Return Type $;  #: Return Type $;
1953  sub GenomeOf {  sub GenomeOf {
1954          # Get the parameters.          # Get the parameters.
1955          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
         # Create a query to find the genome associated with the feature.  
         my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ?", [$featureID]);  
1956          # Declare the return value.          # Declare the return value.
1957          my $retVal;          my $retVal;
1958          # Get the genome ID.      # Parse the genome ID from the feature ID.
1959          if (my $relationship = $query->Fetch()) {      if ($featureID =~ /^fig\|(\d+\.\d+)/) {
1960                  ($retVal) = $relationship->Value('HasContig(from-link)');          $retVal = $1;
1961        } else {
1962            # Find the feature by alias.
1963            my ($realFeatureID) = $self->FeaturesByAlias($featureID);
1964            if ($realFeatureID && $realFeatureID =~ /^fig\|(\d+\.\d+)/) {
1965                $retVal = $1;
1966            } else {
1967                # Use the external table.
1968                my ($org) = $self->GetFlat(['ExternalAliasOrg'], "ExternalAliasOrg(id) = ?",
1969                                           [$featureID], "ExternalAliasOrg(org)");
1970                if ($org) {
1971                    $retVal = $org;
1972                } else {
1973                    Confess("Invalid feature ID $featureID.");
1974                }
1975            }
1976          }          }
1977          # Return the value found.          # Return the value found.
1978          return $retVal;          return $retVal;
# Line 1285  Line 1980 
1980    
1981  =head3 CoupledFeatures  =head3 CoupledFeatures
1982    
1983  C<< my %coupleHash = $sprout->CoupledFeatures($featureID); >>      my %coupleHash = $sprout->CoupledFeatures($featureID);
1984    
1985  Return the features functionally coupled with a specified feature. Features are considered  Return the features functionally coupled with a specified feature. Features are considered
1986  functionally coupled if they tend to be clustered on the same chromosome.  functionally coupled if they tend to be clustered on the same chromosome.
# Line 1306  Line 2001 
2001  #: Return Type %;  #: Return Type %;
2002  sub CoupledFeatures {  sub CoupledFeatures {
2003          # Get the parameters.          # Get the parameters.
2004          my $self = shift @_;      my ($self, $featureID) = @_;
2005          my ($featureID) = @_;      # Ask the coupling server for the data.
2006          # Create a query to retrieve the functionally-coupled features. Note that we depend on the      Trace("Looking for features coupled to $featureID.") if T(coupling => 3);
2007          # fact that the functional coupling is physically paired. If (A,B) is in the database, then      my @rawPairs = FIGRules::NetCouplingData('coupled_to', id1 => $featureID);
2008          # (B,A) will also be found.      Trace(scalar(@rawPairs) . " couplings returned.") if T(coupling => 3);
2009          my $query = $self->Get(['IsClusteredOnChromosomeWith'],      # Form them into a hash.
                                                    "IsClusteredOnChromosomeWith(from-link) = ?", [$featureID]);  
         # This value will be set to TRUE if we find at least one coupled feature.  
         my $found = 0;  
         # Create the return hash.  
2010          my %retVal = ();          my %retVal = ();
2011          # Retrieve the relationship records and store them in the hash.      for my $pair (@rawPairs) {
2012          while (my $clustering = $query->Fetch()) {          # Get the feature ID and score.
2013                  my ($otherFeatureID, $score) = $clustering->Values(['IsClusteredOnChromosomeWith(to-link)',          my ($featureID2, $score) = @{$pair};
2014                                                                      'IsClusteredOnChromosomeWith(score)']);          # Only proceed if the feature is in NMPDR.
2015                  $retVal{$otherFeatureID} = $score;          if ($self->_CheckFeature($featureID2)) {
2016                  $found = 1;              $retVal{$featureID2} = $score;
2017            }
2018          }          }
2019          # Functional coupling is reflexive. If we found at least one coupled feature, we must add          # Functional coupling is reflexive. If we found at least one coupled feature, we must add
2020          # the incoming feature as well.          # the incoming feature as well.
2021          if ($found) {      if (keys %retVal) {
2022                  $retVal{$featureID} = 9999;                  $retVal{$featureID} = 9999;
2023      }      }
2024          # Return the hash.          # Return the hash.
2025          return %retVal;          return %retVal;
2026  }  }
2027    
2028  =head3 GetEntityTypes  =head3 CouplingEvidence
2029    
2030        my @evidence = $sprout->CouplingEvidence($peg1, $peg2);
2031    
2032    Return the evidence for a functional coupling.
2033    
2034    A pair of features is considered evidence of a coupling between two other
2035    features if they occur close together on a contig and both are similar to
2036    the coupled features. So, if B<A1> and B<A2> are close together on a contig,
2037    B<B1> and B<B2> are considered evidence for the coupling if (1) B<B1> and
2038    B<B2> are close together, (2) B<B1> is similar to B<A1>, and (3) B<B2> is
2039    similar to B<A2>.
2040    
2041    The score of a coupling is determined by the number of pieces of evidence
2042    that are considered I<representative>. If several evidence items belong to
2043    a group of genomes that are close to each other, only one of those items
2044    is considered representative. The other evidence items are presumed to be
2045    there because of the relationship between the genomes rather than because
2046    the two proteins generated by the features have a related functionality.
2047    
2048    Each evidence item is returned as a three-tuple in the form C<[>I<$peg1a>C<,>
2049    I<$peg2a>C<,> I<$rep>C<]>, where I<$peg1a> is similar to I<$peg1>, I<$peg2a>
2050    is similar to I<$peg2>, and I<$rep> is TRUE if the evidence is representative
2051    and FALSE otherwise.
2052    
2053    =over 4
2054    
2055    =item peg1
2056    
2057    ID of the feature of interest.
2058    
2059    =item peg2
2060    
2061    ID of a feature functionally coupled to the feature of interest.
2062    
2063    =item RETURN
2064    
2065    Returns a list of 3-tuples. Each tuple consists of a feature similar to the feature
2066    of interest, a feature similar to the functionally coupled feature, and a flag
2067    that is TRUE for a representative piece of evidence and FALSE otherwise.
2068    
2069    =back
2070    
2071    =cut
2072    #: Return Type @@;
2073    sub CouplingEvidence {
2074        # Get the parameters.
2075        my ($self, $peg1, $peg2) = @_;
2076        # Declare the return variable.
2077        my @retVal = ();
2078        # Get the coupling and evidence data.
2079        my @rawData = FIGRules::NetCouplingData('coupling_evidence', id1 => $peg1, id2 => $peg2);
2080        # Loop through the raw data, saving the ones that are in NMPDR genomes.
2081        for my $rawTuple (@rawData) {
2082            if ($self->_CheckFeature($rawTuple->[0]) && $self->_CheckFeature($rawTuple->[1])) {
2083                push @retVal, $rawTuple;
2084            }
2085        }
2086        # Return the result.
2087        return @retVal;
2088    }
2089    
2090    =head3 GetSynonymGroup
2091    
2092        my $id = $sprout->GetSynonymGroup($fid);
2093    
2094    Return the synonym group name for the specified feature.
2095    
2096    =over 4
2097    
2098    =item fid
2099    
2100    ID of the feature whose synonym group is desired.
2101    
2102    =item RETURN
2103    
2104    The name of the synonym group to which the feature belongs. If the feature does
2105    not belong to a synonym group, the feature ID itself is returned.
2106    
2107    =back
2108    
2109    =cut
2110    
2111    sub GetSynonymGroup {
2112        # Get the parameters.
2113        my ($self, $fid) = @_;
2114        # Declare the return variable.
2115        my $retVal;
2116        # Find the synonym group.
2117        my @groups = $self->GetFlat(['IsSynonymGroupFor'], "IsSynonymGroupFor(to-link) = ?",
2118                                       [$fid], 'IsSynonymGroupFor(from-link)');
2119        # Check to see if we found anything.
2120        if (@groups) {
2121            $retVal = $groups[0];
2122        } else {
2123            $retVal = $fid;
2124        }
2125        # Return the result.
2126        return $retVal;
2127    }
2128    
2129    =head3 GetBoundaries
2130    
2131  C<< my @entityList = $sprout->GetEntityTypes(); >>      my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList);
2132    
2133  Return the list of supported entity types.  Determine the begin and end boundaries for the locations in a list. All of the
2134    locations must belong to the same contig and have mostly the same direction in
2135    order for this method to produce a meaningful result. The resulting
2136    begin/end pair will contain all of the bases in any of the locations.
2137    
2138    =over 4
2139    
2140    =item locList
2141    
2142    List of locations to process.
2143    
2144    =item RETURN
2145    
2146    Returns a 3-tuple consisting of the contig ID, the beginning boundary,
2147    and the ending boundary. The beginning boundary will be left of the
2148    end for mostly-forward locations and right of the end for mostly-backward
2149    locations.
2150    
2151    =back
2152    
2153  =cut  =cut
2154  #: Return Type @;  
2155  sub GetEntityTypes {  sub GetBoundaries {
2156          # Get the parameters.          # Get the parameters.
2157          my $self = shift @_;      my ($self, @locList) = @_;
2158          # Get the underlying database object.      # Set up the counters used to determine the most popular direction.
2159          my $erdb = $self->{_erdb};      my %counts = ( '+' => 0, '-' => 0 );
2160          # Get its entity type list.      # Get the last location and parse it.
2161          my @retVal = $erdb->GetEntityTypes();      my $locObject = BasicLocation->new(pop @locList);
2162        # Prime the loop with its data.
2163        my ($contig, $beg, $end) = ($locObject->Contig, $locObject->Left, $locObject->Right);
2164        # Count its direction.
2165        $counts{$locObject->Dir}++;
2166        # Loop through the remaining locations. Note that in most situations, this loop
2167        # will not iterate at all, because most of the time we will be dealing with a
2168        # singleton list.
2169        for my $loc (@locList) {
2170            # Create a location object.
2171            my $locObject = BasicLocation->new($loc);
2172            # Count the direction.
2173            $counts{$locObject->Dir}++;
2174            # Get the left end and the right end.
2175            my $left = $locObject->Left;
2176            my $right = $locObject->Right;
2177            # Merge them into the return variables.
2178            if ($left < $beg) {
2179                $beg = $left;
2180            }
2181            if ($right > $end) {
2182                $end = $right;
2183            }
2184        }
2185        # If the most common direction is reverse, flip the begin and end markers.
2186        if ($counts{'-'} > $counts{'+'}) {
2187            ($beg, $end) = ($end, $beg);
2188        }
2189        # Return the result.
2190        return ($contig, $beg, $end);
2191  }  }
2192    
2193  =head3 ReadFasta  =head3 ReadFasta
2194    
2195  C<< my %sequenceData = Sprout::ReadFasta($fileName, $prefix); >>      my %sequenceData = Sprout::ReadFasta($fileName, $prefix);
2196    
2197  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
2198  one or more lines of data. The first line begins with a > character and contains an ID.  one or more lines of data. The first line begins with a > character and contains an ID.
# Line 1394  Line 2234 
2234                  if ($line =~ m/^>\s*(.+?)(\s|\n)/) {                  if ($line =~ m/^>\s*(.+?)(\s|\n)/) {
2235                          # 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.
2236                          if ($id) {                          if ($id) {
2237                                  $retVal{$id} = $sequence;                  $retVal{$id} = lc $sequence;
2238                          }                          }
2239                          # Clear the sequence accumulator and save the new ID.                          # Clear the sequence accumulator and save the new ID.
2240                          ($id, $sequence) = ("$prefix$1", "");                          ($id, $sequence) = ("$prefix$1", "");
2241                  } else {                  } else {
2242                          # 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.
2243                          # First, we get the actual data out.              # First, we get the actual data out. Note that we normalize to lower
2244                # case.
2245                          $line =~ /^\s*(.*?)(\s|\n)/;                          $line =~ /^\s*(.*?)(\s|\n)/;
2246                          $sequence .= $1;                          $sequence .= $1;
2247                  }                  }
2248          }          }
2249          # Flush out the last sequence (if any).          # Flush out the last sequence (if any).
2250          if ($sequence) {          if ($sequence) {
2251                  $retVal {$id} = $sequence;          $retVal{$id} = lc $sequence;
2252          }          }
2253        # Close the file.
2254        close FASTAFILE;
2255          # Return the hash constructed from the file.          # Return the hash constructed from the file.
2256          return %retVal;          return %retVal;
2257  }  }
2258    
2259  =head3 FormatLocations  =head3 FormatLocations
2260    
2261  C<< my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat); >>      my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat);
2262    
2263  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
2264  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
2265  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,
2266    it will not be changed; otherwise, it will be converted. This method can also be used to
2267    perform the reverse task-- insuring that all the locations are in the old format.
2268    
2269  =over 4  =over 4
2270    
# Line 1446  Line 2291 
2291  #: Return Type @;  #: Return Type @;
2292  sub FormatLocations {  sub FormatLocations {
2293          # Get the parameters.          # Get the parameters.
2294          my $self = shift @_;      my ($self, $prefix, $locations, $oldFormat) = @_;
         my ($prefix, $locations, $oldFormat) = @_;  
2295          # Create the return list.          # Create the return list.
2296          my @retVal = ();          my @retVal = ();
2297          # Check to see if any locations were passed in.          # Check to see if any locations were passed in.
2298          if ($locations eq '') {          if ($locations eq '') {
2299              confess "No locations specified.";          Confess("No locations specified.");
2300          } else {          } else {
2301                  # Loop through the locations, converting them to the new format.                  # Loop through the locations, converting them to the new format.
2302                  for my $location (@{$locations}) {                  for my $location (@{$locations}) {
# Line 1479  Line 2323 
2323    
2324  =head3 DumpData  =head3 DumpData
2325    
2326  C<< $sprout->DumpData(); >>      $sprout->DumpData();
2327    
2328  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.
2329    
# Line 1487  Line 2331 
2331    
2332  sub DumpData {  sub DumpData {
2333          # Get the parameters.          # Get the parameters.
2334          my $self = shift @_;      my ($self) = @_;
2335          # Get the data directory name.          # Get the data directory name.
2336          my $outputDirectory = $self->{_options}->{dataDir};          my $outputDirectory = $self->{_options}->{dataDir};
2337          # Dump the relations.          # Dump the relations.
2338          $self->{_erdb}->DumpRelations($outputDirectory);      $self->DumpRelations($outputDirectory);
2339  }  }
2340    
2341  =head3 XMLFileName  =head3 XMLFileName
2342    
2343  C<< my $fileName = $sprout->XMLFileName(); >>      my $fileName = $sprout->XMLFileName();
2344    
2345  Return the name of this database's XML definition file.  Return the name of this database's XML definition file.
2346    
2347  =cut  =cut
2348  #: Return Type $;  #: Return Type $;
2349  sub XMLFileName {  sub XMLFileName {
2350          my $self = shift @_;      my ($self) = @_;
2351          return $self->{_xmlName};          return $self->{_xmlName};
2352  }  }
2353    
2354    =head3 GetGenomeNameData
2355    
2356        my ($genus, $species, $strain) = $sprout->GenomeNameData($genomeID);
2357    
2358    Return the genus, species, and unique characterization for a genome. This
2359    is similar to L</GenusSpecies>, with the exception that it returns the
2360    values in three seperate fields.
2361    
2362    =over 4
2363    
2364    =item genomeID
2365    
2366    ID of the genome whose name data is desired.
2367    
2368    =item RETURN
2369    
2370    Returns a three-element list, consisting of the genus, species, and strain
2371    of the specified genome. If the genome is not found, an error occurs.
2372    
2373    =back
2374    
2375    =cut
2376    
2377    sub GetGenomeNameData {
2378        # Get the parameters.
2379        my ($self, $genomeID) = @_;
2380        # Get the desired values.
2381        my ($genus, $species, $strain) = $self->GetEntityValues('Genome', $genomeID =>
2382                                                                [qw(Genome(genus) Genome(species) Genome(unique-characterization))]);
2383        # Throw an error if they were not found.
2384        if (! defined $genus) {
2385            Confess("Genome $genomeID not found in database.");
2386        }
2387        # Return the results.
2388        return ($genus, $species, $strain);
2389    }
2390    
2391    =head3 GetGenomeByNameData
2392    
2393        my @genomes = $sprout->GetGenomeByNameData($genus, $species, $strain);
2394    
2395    Return a list of the IDs of the genomes with the specified genus,
2396    species, and strain. In almost every case, there will be either zero or
2397    one IDs returned; however, two or more IDs could be returned if there are
2398    multiple versions of the genome in the database.
2399    
2400    =over 4
2401    
2402    =item genus
2403    
2404    Genus of the desired genome.
2405    
2406    =item species
2407    
2408    Species of the desired genome.
2409    
2410    =item strain
2411    
2412    Strain (unique characterization) of the desired genome. This may be an empty
2413    string, in which case it is presumed that the desired genome has no strain
2414    specified.
2415    
2416    =item RETURN
2417    
2418    Returns a list of the IDs of the genomes having the specified genus, species, and
2419    strain.
2420    
2421    =back
2422    
2423    =cut
2424    
2425    sub GetGenomeByNameData {
2426        # Get the parameters.
2427        my ($self, $genus, $species, $strain) = @_;
2428        # Try to find the genomes.
2429        my @retVal = $self->GetFlat(['Genome'], "Genome(genus) = ? AND Genome(species) = ? AND Genome(unique-characterization) = ?",
2430                                    [$genus, $species, $strain], 'Genome(id)');
2431        # Return the result.
2432        return @retVal;
2433    }
2434    
2435  =head3 Insert  =head3 Insert
2436    
2437  C<< $sprout->Insert($objectType, \%fieldHash); >>      $sprout->Insert($objectType, \%fieldHash);
2438    
2439  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
2440  is defined by a type name and then a hash of field names to values. Field values in the primary  is defined by a type name and then a hash of field names to values. Field values in the primary
# Line 1518  Line 2443 
2443  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
2444  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>.
2445    
2446  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']});
2447    
2448  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
2449  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>.
2450    
2451  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'});
2452    
2453  =over 4  =over 4
2454    
# Line 1541  Line 2466 
2466  #: Return Type ;  #: Return Type ;
2467  sub Insert {  sub Insert {
2468          # Get the parameters.          # Get the parameters.
2469          my $self = shift @_;      my ($self, $objectType, $fieldHash) = @_;
         my ($objectType, $fieldHash) = @_;  
2470          # Call the underlying method.          # Call the underlying method.
2471          $self->{_erdb}->InsertObject($objectType, $fieldHash);      $self->InsertObject($objectType, $fieldHash);
2472  }  }
2473    
2474  =head3 Annotate  =head3 Annotate
2475    
2476  C<< my $ok = $sprout->Annotate($fid, $timestamp, $user, $text); >>      my $ok = $sprout->Annotate($fid, $timestamp, $user, $text);
2477    
2478  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
2479  specified feature and user.  specified feature and user.
# Line 1583  Line 2507 
2507  #: Return Type $;  #: Return Type $;
2508  sub Annotate {  sub Annotate {
2509          # Get the parameters.          # Get the parameters.
2510          my $self = shift @_;      my ($self, $fid, $timestamp, $user, $text) = @_;
         my ($fid, $timestamp, $user, $text) = @_;  
2511          # Create the annotation ID.          # Create the annotation ID.
2512          my $aid = "$fid:$timestamp";          my $aid = "$fid:$timestamp";
2513          # Insert the Annotation object.          # Insert the Annotation object.
# Line 1604  Line 2527 
2527    
2528  =head3 AssignFunction  =head3 AssignFunction
2529    
2530  C<< my $ok = $sprout->AssignFunction($featureID, $user, $function); >>      my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser);
2531    
2532  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
2533  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.  
2534    
2535  =over 4  =over 4
2536    
# Line 1618  Line 2540 
2540    
2541  =item user  =item user
2542    
2543  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>.
2544    
2545  =item function  =item function
2546    
2547  Text of the function being assigned.  Text of the function being assigned.
2548    
2549    =item assigningUser (optional)
2550    
2551    Name of the individual user making the assignment. If omitted, defaults to the user group.
2552    
2553  =item RETURN  =item RETURN
2554    
2555  Returns 1 if successful, 0 if an error occurred.  Returns 1 if successful, 0 if an error occurred.
# Line 1634  Line 2560 
2560  #: Return Type $;  #: Return Type $;
2561  sub AssignFunction {  sub AssignFunction {
2562          # Get the parameters.          # Get the parameters.
2563          my $self = shift @_;      my ($self, $featureID, $user, $function, $assigningUser) = @_;
2564          my ($featureID, $user, $function) = @_;      # Default the assigning user.
2565        if (! $assigningUser) {
2566            $assigningUser = $user;
2567        }
2568          # Create an annotation string from the parameters.          # Create an annotation string from the parameters.
2569          my $annotationText = "$user\nset $user function to\n$function";      my $annotationText = "$assigningUser\nset $user function to\n$function";
2570          # Get the current time.          # Get the current time.
2571          my $now = time;          my $now = time;
2572          # Declare the return variable.          # Declare the return variable.
# Line 1658  Line 2587 
2587    
2588  =head3 FeaturesByAlias  =head3 FeaturesByAlias
2589    
2590  C<< my @features = $sprout->FeaturesByAlias($alias); >>      my @features = $sprout->FeaturesByAlias($alias);
2591    
2592  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
2593  the type of the alias. A string of digits is a GenBack ID and a string of exactly 6  the type of the alias. A string of digits is a GenBack ID and a string of exactly 6
# Line 1682  Line 2611 
2611  #: Return Type @;  #: Return Type @;
2612  sub FeaturesByAlias {  sub FeaturesByAlias {
2613          # Get the parameters.          # Get the parameters.
2614          my $self = shift @_;      my ($self, $alias) = @_;
         my ($alias) = @_;  
2615          # Declare the return variable.          # Declare the return variable.
2616          my @retVal = ();          my @retVal = ();
2617          # Parse the alias.          # Parse the alias.
# Line 1693  Line 2621 
2621                  push @retVal, $mappedAlias;                  push @retVal, $mappedAlias;
2622          } else {          } else {
2623                  # 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.
2624                  @retVal = $self->GetFlat(['Feature'], 'Feature(alias) = ?', [$mappedAlias], 'Feature(id)');          @retVal = $self->GetFlat(['IsAliasOf'], 'IsAliasOf(from-link) = ?', [$mappedAlias], 'IsAliasOf(to-link)');
2625          }          }
2626          # Return the result.          # Return the result.
2627          return @retVal;          return @retVal;
2628  }  }
2629    
 =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;  
 }  
   
2630  =head3 FeatureTranslation  =head3 FeatureTranslation
2631    
2632  C<< my $translation = $sprout->FeatureTranslation($featureID); >>      my $translation = $sprout->FeatureTranslation($featureID);
2633    
2634  Return the translation of a feature.  Return the translation of a feature.
2635    
# Line 1756  Line 2649 
2649  #: Return Type $;  #: Return Type $;
2650  sub FeatureTranslation {  sub FeatureTranslation {
2651          # Get the parameters.          # Get the parameters.
2652          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
2653          # Get the specified feature's translation.          # Get the specified feature's translation.
2654          my ($retVal) = $self->GetEntityValues("Feature", $featureID, ['Feature(translation)']);          my ($retVal) = $self->GetEntityValues("Feature", $featureID, ['Feature(translation)']);
2655          return $retVal;          return $retVal;
# Line 1765  Line 2657 
2657    
2658  =head3 Taxonomy  =head3 Taxonomy
2659    
2660  C<< my @taxonomyList = $sprout->Taxonomy($genome); >>      my @taxonomyList = $sprout->Taxonomy($genome);
2661    
2662  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
2663  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>,
2664  or C<Eukaryote>) to sub-species. For example,  or C<Eukaryote>) to sub-species. For example,
2665    
2666  C<< (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12) >>      (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12)
2667    
2668  =over 4  =over 4
2669    
# Line 1789  Line 2681 
2681  #: Return Type @;  #: Return Type @;
2682  sub Taxonomy {  sub Taxonomy {
2683          # Get the parameters.          # Get the parameters.
2684          my $self = shift @_;      my ($self, $genome) = @_;
         my ($genome) = @_;  
2685          # Find the specified genome's taxonomy string.          # Find the specified genome's taxonomy string.
2686          my ($list) = $self->GetEntityValues('Genome', $genome, ['Genome(taxonomy)']);          my ($list) = $self->GetEntityValues('Genome', $genome, ['Genome(taxonomy)']);
2687          # Declare the return variable.          # Declare the return variable.
# Line 1807  Line 2698 
2698    
2699  =head3 CrudeDistance  =head3 CrudeDistance
2700    
2701  C<< my $distance = $sprout->CrudeDistance($genome1, $genome2); >>      my $distance = $sprout->CrudeDistance($genome1, $genome2);
2702    
2703  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
2704  that it will be 0 for genomes with identical taxonomies and 1 for genomes from different domains.  that it will be 0 for genomes with identical taxonomies and 1 for genomes from different domains.
# Line 1833  Line 2724 
2724  #: Return Type $;  #: Return Type $;
2725  sub CrudeDistance {  sub CrudeDistance {
2726          # Get the parameters.          # Get the parameters.
2727          my $self = shift @_;      my ($self, $genome1, $genome2) = @_;
         my ($genome1, $genome2) = @_;  
2728          # Insure that the distance is commutative by sorting the genome IDs.          # Insure that the distance is commutative by sorting the genome IDs.
2729          my ($genomeA, $genomeB);          my ($genomeA, $genomeB);
2730          if ($genome2 < $genome2) {          if ($genome2 < $genome2) {
# Line 1860  Line 2750 
2750    
2751  =head3 RoleName  =head3 RoleName
2752    
2753  C<< my $roleName = $sprout->RoleName($roleID); >>      my $roleName = $sprout->RoleName($roleID);
2754    
2755  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
2756  will only have a descriptive name if it is coded as an EC number.  will only have a descriptive name if it is coded as an EC number.
# Line 1881  Line 2771 
2771  #: Return Type $;  #: Return Type $;
2772  sub RoleName {  sub RoleName {
2773          # Get the parameters.          # Get the parameters.
2774          my $self = shift @_;      my ($self, $roleID) = @_;
         my ($roleID) = @_;  
2775          # Get the specified role's name.          # Get the specified role's name.
2776          my ($retVal) = $self->GetEntityValues('Role', $roleID, ['Role(name)']);          my ($retVal) = $self->GetEntityValues('Role', $roleID, ['Role(name)']);
2777          # Use the ID if the role has no name.          # Use the ID if the role has no name.
# Line 1895  Line 2784 
2784    
2785  =head3 RoleDiagrams  =head3 RoleDiagrams
2786    
2787  C<< my @diagrams = $sprout->RoleDiagrams($roleID); >>      my @diagrams = $sprout->RoleDiagrams($roleID);
2788    
2789  Return a list of the diagrams containing a specified functional role.  Return a list of the diagrams containing a specified functional role.
2790    
# Line 1915  Line 2804 
2804  #: Return Type @;  #: Return Type @;
2805  sub RoleDiagrams {  sub RoleDiagrams {
2806          # Get the parameters.          # Get the parameters.
2807          my $self = shift @_;      my ($self, $roleID) = @_;
         my ($roleID) = @_;  
2808          # Query for the diagrams.          # Query for the diagrams.
2809          my @retVal = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],          my @retVal = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],
2810                                                                  'RoleOccursIn(to-link)');                                                                  'RoleOccursIn(to-link)');
# Line 1926  Line 2814 
2814    
2815  =head3 FeatureProperties  =head3 FeatureProperties
2816    
2817  C<< my @properties = $sprout->FeatureProperties($featureID); >>      my @properties = $sprout->FeatureProperties($featureID);
2818    
2819  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
2820  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
2821  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
2822  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
2823  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.  
2824    
2825  =over 4  =over 4
2826    
# Line 1944  Line 2830 
2830    
2831  =item RETURN  =item RETURN
2832    
2833  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.  
2834    
2835  =back  =back
2836    
# Line 1953  Line 2838 
2838  #: Return Type @@;  #: Return Type @@;
2839  sub FeatureProperties {  sub FeatureProperties {
2840          # Get the parameters.          # Get the parameters.
2841          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
2842          # Get the properties.          # Get the properties.
2843          my @retVal = $self->GetAll(['HasProperty', 'Property'], "HasProperty(from-link) = ?", [$featureID],      my @attributes = $self->{_ca}->GetAttributes($featureID);
2844                                                          ['Property(property-name)', 'Property(property-value)',      # Strip the feature ID off each tuple.
2845                                                           'HasProperty(evidence)']);      my @retVal = ();
2846        for my $attributeRow (@attributes) {
2847            shift @{$attributeRow};
2848            push @retVal, $attributeRow;
2849        }
2850          # Return the resulting list.          # Return the resulting list.
2851          return @retVal;          return @retVal;
2852  }  }
2853    
2854  =head3 DiagramName  =head3 DiagramName
2855    
2856  C<< my $diagramName = $sprout->DiagramName($diagramID); >>      my $diagramName = $sprout->DiagramName($diagramID);
2857    
2858  Return the descriptive name of a diagram.  Return the descriptive name of a diagram.
2859    
# Line 1985  Line 2873 
2873  #: Return Type $;  #: Return Type $;
2874  sub DiagramName {  sub DiagramName {
2875          # Get the parameters.          # Get the parameters.
2876          my $self = shift @_;      my ($self, $diagramID) = @_;
         my ($diagramID) = @_;  
2877          # Get the specified diagram's name and return it.          # Get the specified diagram's name and return it.
2878          my ($retVal) = $self->GetEntityValues('Diagram', $diagramID, ['Diagram(name)']);          my ($retVal) = $self->GetEntityValues('Diagram', $diagramID, ['Diagram(name)']);
2879          return $retVal;          return $retVal;
2880  }  }
2881    
2882    =head3 PropertyID
2883    
2884        my $id = $sprout->PropertyID($propName, $propValue);
2885    
2886    Return the ID of the specified property name and value pair, if the
2887    pair exists. Only a small subset of the FIG attributes are stored as
2888    Sprout properties, mostly for use in search optimization.
2889    
2890    =over 4
2891    
2892    =item propName
2893    
2894    Name of the desired property.
2895    
2896    =item propValue
2897    
2898    Value expected for the desired property.
2899    
2900    =item RETURN
2901    
2902    Returns the ID of the name/value pair, or C<undef> if the pair does not exist.
2903    
2904    =back
2905    
2906    =cut
2907    
2908    sub PropertyID {
2909        # Get the parameters.
2910        my ($self, $propName, $propValue) = @_;
2911        # Try to find the ID.
2912        my ($retVal) = $self->GetFlat(['Property'],
2913                                      "Property(property-name) = ? AND Property(property-value) = ?",
2914                                      [$propName, $propValue], 'Property(id)');
2915        # Return the result.
2916        return $retVal;
2917    }
2918    
2919  =head3 MergedAnnotations  =head3 MergedAnnotations
2920    
2921  C<< my @annotationList = $sprout->MergedAnnotations(\@list); >>      my @annotationList = $sprout->MergedAnnotations(\@list);
2922    
2923  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
2924  represented by a 4-tuple of the form C<($fid, $timestamp, $userID, $annotation)>, where  represented by a 4-tuple of the form C<($fid, $timestamp, $userID, $annotation)>, where
# Line 2018  Line 2942 
2942  #: Return Type @;  #: Return Type @;
2943  sub MergedAnnotations {  sub MergedAnnotations {
2944          # Get the parameters.          # Get the parameters.
2945          my $self = shift @_;      my ($self, $list) = @_;
         my ($list) = @_;  
2946          # Create a list to hold the annotation tuples found.          # Create a list to hold the annotation tuples found.
2947          my @tuples = ();          my @tuples = ();
2948          # Loop through the features in the input list.          # Loop through the features in the input list.
# Line 2044  Line 2967 
2967    
2968  =head3 RoleNeighbors  =head3 RoleNeighbors
2969    
2970  C<< my @roleList = $sprout->RoleNeighbors($roleID); >>      my @roleList = $sprout->RoleNeighbors($roleID);
2971    
2972  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
2973  diagrams and roles are in a many-to-many relationship with each other, the list is  diagrams and roles are in a many-to-many relationship with each other, the list is
# Line 2067  Line 2990 
2990  #: Return Type @;  #: Return Type @;
2991  sub RoleNeighbors {  sub RoleNeighbors {
2992          # Get the parameters.          # Get the parameters.
2993          my $self = shift @_;      my ($self, $roleID) = @_;
         my ($roleID) = @_;  
2994          # Get all the diagrams containing this role.          # Get all the diagrams containing this role.
2995          my @diagrams = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],          my @diagrams = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],
2996                                                                    'RoleOccursIn(to-link)');                                                                    'RoleOccursIn(to-link)');
# Line 2088  Line 3010 
3010    
3011  =head3 FeatureLinks  =head3 FeatureLinks
3012    
3013  C<< my @links = $sprout->FeatureLinks($featureID); >>      my @links = $sprout->FeatureLinks($featureID);
3014    
3015  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
3016  to external websites describing either the feature itself or the organism containing it  to external websites describing either the feature itself or the organism containing it
# Line 2110  Line 3032 
3032  #: Return Type @;  #: Return Type @;
3033  sub FeatureLinks {  sub FeatureLinks {
3034          # Get the parameters.          # Get the parameters.
3035          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
3036          # Get the feature's links.          # Get the feature's links.
3037          my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(link)']);          my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(link)']);
3038          # Return the feature's links.          # Return the feature's links.
# Line 2120  Line 3041 
3041    
3042  =head3 SubsystemsOf  =head3 SubsystemsOf
3043    
3044  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>      my %subsystems = $sprout->SubsystemsOf($featureID);
3045    
3046  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
3047  to the role the feature performs.  to the roles the feature performs.
3048    
3049  =over 4  =over 4
3050    
# Line 2133  Line 3054 
3054    
3055  =item RETURN  =item RETURN
3056    
3057  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.
3058    
3059  =back  =back
3060    
3061  =cut  =cut
3062  #: Return Type %;  #: Return Type %@;
3063  sub SubsystemsOf {  sub SubsystemsOf {
3064          # Get the parameters.          # Get the parameters.
3065          my $self = shift @_;      my ($self, $featureID) = @_;
3066          my ($featureID) = @_;      # Get the subsystem list.
         # Use the SSCell to connect features to subsystems.  
3067          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],
3068                                                                          "ContainsFeature(to-link) = ?", [$featureID],                                                                          "ContainsFeature(to-link) = ?", [$featureID],
3069                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);
3070          # Create the return value.          # Create the return value.
3071          my %retVal = ();          my %retVal = ();
3072        # Build a hash to weed out duplicates. Sometimes the same PEG and role appears
3073        # in two spreadsheet cells.
3074        my %dupHash = ();
3075          # Loop through the results, adding them to the hash.          # Loop through the results, adding them to the hash.
3076          for my $record (@subsystems) {          for my $record (@subsystems) {
3077                  $retVal{$record->[0]} = $record->[1];          # Get this subsystem and role.
3078            my ($subsys, $role) = @{$record};
3079            # Insure it's the first time for both.
3080            my $dupKey = "$subsys\n$role";
3081            if (! exists $dupHash{"$subsys\n$role"}) {
3082                $dupHash{$dupKey} = 1;
3083                push @{$retVal{$subsys}}, $role;
3084            }
3085          }          }
3086          # Return the hash.          # Return the hash.
3087          return %retVal;          return %retVal;
3088  }  }
3089    
3090  =head3 RelatedFeatures  =head3 SubsystemList
3091    
3092  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>      my @subsystems = $sprout->SubsystemList($featureID);
3093    
3094  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
3095  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
3096  an empty list will be returned.  subsystem names, not the roles.
3097    
3098  =over 4  =over 4
3099    
3100  =item featureID  =item featureID
3101    
3102  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.  
3103    
3104  =item RETURN  =item RETURN
3105    
3106  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.
3107    
3108  =back  =back
3109    
3110  =cut  =cut
3111  #: Return Type @;  #: Return Type @;
3112  sub RelatedFeatures {  sub SubsystemList {
3113          # Get the parameters.          # Get the parameters.
3114          my $self = shift @_;      my ($self, $featureID) = @_;
3115          my ($featureID, $function, $userID) = @_;      # Get the list of names.
3116          # Get a list of the features that are BBHs of the incoming feature.      ##HACK: we do a join to the Subsystem table because we have missing subsystems in
3117          my @bbhFeatures = $self->GetFlat(['IsBidirectionalBestHitOf'],      ## the Sprout database!
3118                                                                           "IsBidirectionalBestHitOf(from-link) = ?", [$featureID],      my @retVal = $self->GetFlat(['HasRoleInSubsystem', 'Subsystem'], "HasRoleInSubsystem(from-link) = ?",
3119                                                                           'IsBidirectionalBestHitOf(to-link)');                                  [$featureID], 'HasRoleInSubsystem(to-link)');
3120          # Now we loop through the features, pulling out the ones that have the correct      # Return the result, sorted.
3121          # functional assignment.      return sort @retVal;
         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;  
3122  }  }
3123    
3124  =head3 TaxonomySort  =head3 GenomeSubsystemData
3125    
3126  C<< my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs); >>      my %featureData = $sprout->GenomeSubsystemData($genomeID);
3127    
3128  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.  
3129    
3130  =over 4  =over 4
3131    
3132  =item $featureIDs  =item genomeID
3133    
3134  List of features to be taxonomically sorted.  ID of the genome whose subsystem feature map is desired.
3135    
3136  =item RETURN  =item RETURN
3137    
3138  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
3139    2-tuple contains a subsystem name followed by a role ID.
3140    
3141  =back  =back
3142    
3143  =cut  =cut
3144  #: Return Type @;  
3145  sub TaxonomySort {  sub GenomeSubsystemData {
3146          # Get the parameters.          # Get the parameters.
3147          my $self = shift @_;      my ($self, $genomeID) = @_;
3148          my ($featureIDs) = @_;      # Declare the return variable.
3149          # Create the working hash table.      my %retVal = ();
3150          my %hashBuffer = ();      # Get a list of the genome features that participate in subsystems. For each
3151          # Loop through the features.      # feature we get its spreadsheet cells and the corresponding roles.
3152          for my $fid (@{$featureIDs}) {      my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf'],
3153                  # Get the taxonomy of the feature's genome.                               "HasFeature(from-link) = ?", [$genomeID],
3154                  my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",                               ['HasFeature(to-link)', 'IsRoleOf(to-link)', 'IsRoleOf(from-link)']);
3155                                                                                  [$fid], 'Genome(taxonomy)');      # Now we get a list of the spreadsheet cells and their associated subsystems. Subsystems
3156                  # Add this feature to the hash buffer.      # with an unknown variant code (-1) are skipped. Note the genome ID is at both ends of the
3157                  if (exists $hashBuffer{$taxonomy}) {      # list. We use it at the beginning to get all the spreadsheet cells for the genome and
3158                          push @{$hashBuffer{$taxonomy}}, $fid;      # again at the end to filter out participation in subsystems with a negative variant code.
3159                  } else {      my @cellData = $self->GetAll(['IsGenomeOf', 'HasSSCell', 'ParticipatesIn'],
3160                          $hashBuffer{$taxonomy} = [$fid];                                   "IsGenomeOf(from-link) = ? AND ParticipatesIn(variant-code) >= 0 AND ParticipatesIn(from-link) = ?",
3161                                     [$genomeID, $genomeID], ['HasSSCell(to-link)', 'HasSSCell(from-link)']);
3162        # Now "@roleData" lists the spreadsheet cell and role for each of the genome's features.
3163        # "@cellData" lists the subsystem name for each of the genome's spreadsheet cells. We
3164        # link these two lists together to create the result. First, we want a hash mapping
3165        # spreadsheet cells to subsystem names.
3166        my %subHash = map { $_->[0] => $_->[1] } @cellData;
3167        # We loop through @cellData to build the hash.
3168        for my $roleEntry (@roleData) {
3169            # Get the data for this feature and cell.
3170            my ($fid, $cellID, $role) = @{$roleEntry};
3171            # Check for a subsystem name.
3172            my $subsys = $subHash{$cellID};
3173            if ($subsys) {
3174                # Insure this feature has an entry in the return hash.
3175                if (! exists $retVal{$fid}) { $retVal{$fid} = []; }
3176                # Merge in this new data.
3177                push @{$retVal{$fid}}, [$subsys, $role];
3178                  }                  }
3179          }          }
         # Sort the keys and get the elements.  
         my @retVal = ();  
         for my $taxon (sort keys %hashBuffer) {  
                 push @retVal, @{$hashBuffer{$taxon}};  
         }  
3180          # Return the result.          # Return the result.
3181          return @retVal;      return %retVal;
3182  }  }
3183    
3184  =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.  
3185    
3186  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.  
3187    
3188  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
3189    have been assigned the specified function by the specified user. If no such features exists,
3190    an empty list will be returned.
3191    
3192  =over 4  =over 4
3193    
3194  =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  
3195    
3196  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.
3197    
3198  =item fields  =item function
3199    
3200  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
3201    features should be selected.
3202    
3203  =item count  =item userID
3204    
3205  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.
3206    
3207  =item RETURN  =item RETURN
3208    
3209  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.  
3210    
3211  =back  =back
3212    
3213  =cut  =cut
3214  #: Return Type @@;  #: Return Type @;
3215  sub GetAll {  sub RelatedFeatures {
3216          # Get the parameters.          # Get the parameters.
3217          my $self = shift @_;      my ($self, $featureID, $function, $userID) = @_;
3218          my ($objectNames, $filterClause, $parameterList, $fields, $count) = @_;      # Get a list of the features that are BBHs of the incoming feature.
3219          # Create the query.      my @bbhFeatures = map { $_->[0] } FIGRules::BBHData($featureID);
3220          my $query = $self->Get($objectNames, $filterClause, $parameterList);      # Now we loop through the features, pulling out the ones that have the correct
3221          # Set up a counter of the number of records read.      # functional assignment.
         my $fetched = 0;  
         # 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.  
3222          my @retVal = ();          my @retVal = ();
3223          while (($count == 0 || $fetched < $count) && (my $row = $query->Fetch())) {      for my $bbhFeature (@bbhFeatures) {
3224                  my @rowData = $row->Values($fields);          # Get this feature's functional assignment.
3225                  push @retVal, \@rowData;          my $newFunction = $self->FunctionOf($bbhFeature, $userID);
3226                  $fetched++;          # If it matches, add it to the result list.
3227            if ($newFunction eq $function) {
3228                push @retVal, $bbhFeature;
3229          }          }
3230          # Return the resulting list.      }
3231        # Return the result list.
3232          return @retVal;          return @retVal;
3233  }  }
3234    
3235  =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.  
3236    
3237  =item filterClause      my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs);
3238    
3239  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
3240  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.  
3241    
3242  =item parameterList  This task could almost be handled by the database; however, the taxonomy string in the
3243    database is a text field and can't be indexed. Instead, we create a hash table that maps
3244    taxonomy strings to lists of features. We then process the hash table using a key sort
3245    and merge the feature lists together to create the output.
3246    
3247  List of the parameters to be substituted in for the parameters marks in the filter clause.  =over 4
3248    
3249  =item field  =item $featureIDs
3250    
3251  Name of the field to be used to get the elements of the list returned.  List of features to be taxonomically sorted.
3252    
3253  =item RETURN  =item RETURN
3254    
3255  Returns a list of values.  Returns the list of features sorted by the taxonomies of the containing genomes.
3256    
3257  =back  =back
3258    
3259  =cut  =cut
3260  #: Return Type @;  #: Return Type @;
3261  sub GetFlat {  sub TaxonomySort {
3262          # Get the parameters.          # Get the parameters.
3263          my $self = shift @_;      my ($self, $featureIDs) = @_;
3264          my ($objectNames, $filterClause, $parameterList, $field) = @_;      # Create the working hash table.
3265          # Construct the query.      my %hashBuffer = ();
3266          my $query = $self->Get($objectNames, $filterClause, $parameterList);      # Loop through the features.
3267          # Create the result list.      for my $fid (@{$featureIDs}) {
3268            # Get the taxonomy of the feature's genome.
3269            my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",
3270                                            [$fid], 'Genome(taxonomy)');
3271            # Add this feature to the hash buffer.
3272            push @{$hashBuffer{$taxonomy}}, $fid;
3273        }
3274        # Sort the keys and get the elements.
3275          my @retVal = ();          my @retVal = ();
3276          # Loop through the records, adding the field values found to the result list.      for my $taxon (sort keys %hashBuffer) {
3277          while (my $row = $query->Fetch()) {          push @retVal, @{$hashBuffer{$taxon}};
                 push @retVal, $row->Value($field);  
3278          }          }
3279          # Return the list created.      # Return the result.
3280          return @retVal;          return @retVal;
3281  }  }
3282    
3283  =head3 Protein  =head3 Protein
3284    
3285  C<< my $protein = Sprout::Protein($sequence, $table); >>      my $protein = Sprout::Protein($sequence, $table);
3286    
3287  Translate a DNA sequence into a protein sequence.  Translate a DNA sequence into a protein sequence.
3288    
# Line 2470  Line 3352 
3352          # Loop through the input triples.          # Loop through the input triples.
3353          my $n = length $sequence;          my $n = length $sequence;
3354          for (my $i = 0; $i < $n; $i += 3) {          for (my $i = 0; $i < $n; $i += 3) {
3355                  # Get the current triple from the sequence.          # Get the current triple from the sequence. Note we convert to
3356                  my $triple = substr($sequence, $i, 3);          # upper case to insure a match.
3357            my $triple = uc substr($sequence, $i, 3);
3358                  # Translate it using the table.                  # Translate it using the table.
3359                  my $protein = "X";                  my $protein = "X";
3360                  if (exists $table->{$triple}) { $protein = $table->{$triple}; }                  if (exists $table->{$triple}) { $protein = $table->{$triple}; }
# Line 2485  Line 3368 
3368    
3369  =head3 LoadInfo  =head3 LoadInfo
3370    
3371  C<< my ($dirName, @relNames) = $sprout->LoadInfo(); >>      my ($dirName, @relNames) = $sprout->LoadInfo();
3372    
3373  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
3374  names. This information is useful when trying to analyze what needs to be put where in order  names. This information is useful when trying to analyze what needs to be put where in order
# Line 2495  Line 3378 
3378  #: Return Type @;  #: Return Type @;
3379  sub LoadInfo {  sub LoadInfo {
3380          # Get the parameters.          # Get the parameters.
3381          my $self = shift @_;      my ($self) = @_;
3382          # 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.
3383          my @retVal = ($self->{_options}->{dataDir});          my @retVal = ($self->{_options}->{dataDir});
3384          # Concatenate the table names.          # Concatenate the table names.
3385          push @retVal, $self->{_erdb}->GetTableNames();      push @retVal, $self->GetTableNames();
3386          # Return the result.          # Return the result.
3387          return @retVal;          return @retVal;
3388  }  }
3389    
3390    =head3 BBHMatrix
3391    
3392        my %bbhMap = $sprout->BBHMatrix($genomeID, $cutoff, @targets);
3393    
3394    Find all the bidirectional best hits for the features of a genome in a
3395    specified list of target genomes. The return value will be a hash mapping
3396    features in the original genome to their bidirectional best hits in the
3397    target genomes.
3398    
3399    =over 4
3400    
3401    =item genomeID
3402    
3403    ID of the genome whose features are to be examined for bidirectional best hits.
3404    
3405    =item cutoff
3406    
3407    A cutoff value. Only hits with a score lower than the cutoff will be returned.
3408    
3409    =item targets
3410    
3411    List of target genomes. Only pairs originating in the original
3412    genome and landing in one of the target genomes will be returned.
3413    
3414    =item RETURN
3415    
3416    Returns a hash mapping each feature in the original genome to a hash mapping its
3417    BBH pegs in the target genomes to their scores.
3418    
3419    =back
3420    
3421    =cut
3422    
3423    sub BBHMatrix {
3424        # Get the parameters.
3425        my ($self, $genomeID, $cutoff, @targets) = @_;
3426        # Declare the return variable.
3427        my %retVal = ();
3428        # Ask for the BBHs.
3429        my @bbhList = FIGRules::BatchBBHs("fig|$genomeID.%", $cutoff, @targets);
3430        # We now have a set of 4-tuples that we need to convert into a hash of hashes.
3431        for my $bbhData (@bbhList) {
3432            my ($peg1, $peg2, $score) = @{$bbhData};
3433            if (! exists $retVal{$peg1}) {
3434                $retVal{$peg1} = { $peg2 => $score };
3435            } else {
3436                $retVal{$peg1}->{$peg2} = $score;
3437            }
3438        }
3439        # Return the result.
3440        return %retVal;
3441    }
3442    
3443    
3444    =head3 SimMatrix
3445    
3446        my %simMap = $sprout->SimMatrix($genomeID, $cutoff, @targets);
3447    
3448    Find all the similarities for the features of a genome in a
3449    specified list of target genomes. The return value will be a hash mapping
3450    features in the original genome to their similarites in the
3451    target genomes.
3452    
3453    =over 4
3454    
3455    =item genomeID
3456    
3457    ID of the genome whose features are to be examined for similarities.
3458    
3459    =item cutoff
3460    
3461    A cutoff value. Only hits with a score lower than the cutoff will be returned.
3462    
3463    =item targets
3464    
3465    List of target genomes. Only pairs originating in the original
3466    genome and landing in one of the target genomes will be returned.
3467    
3468    =item RETURN
3469    
3470    Returns a hash mapping each feature in the original genome to a hash mapping its
3471    similar pegs in the target genomes to their scores.
3472    
3473    =back
3474    
3475    =cut
3476    
3477    sub SimMatrix {
3478        # Get the parameters.
3479        my ($self, $genomeID, $cutoff, @targets) = @_;
3480        # Declare the return variable.
3481        my %retVal = ();
3482        # Get the list of features in the source organism.
3483        my @fids = $self->FeaturesOf($genomeID);
3484        # Ask for the sims. We only want similarities to fig features.
3485        my $simList = FIGRules::GetNetworkSims($self, \@fids, {}, 1000, $cutoff, "fig");
3486        if (! defined $simList) {
3487            Confess("Unable to retrieve similarities from server.");
3488        } else {
3489            Trace("Processing sims.") if T(3);
3490            # We now have a set of sims that we need to convert into a hash of hashes. First, we
3491            # Create a hash for the target genomes.
3492            my %targetHash = map { $_ => 1 } @targets;
3493            for my $simData (@{$simList}) {
3494                # Get the PEGs and the score.
3495                my ($peg1, $peg2, $score) = ($simData->id1, $simData->id2, $simData->psc);
3496                # Insure the second ID is in the target list.
3497                my ($genome2) = FIGRules::ParseFeatureID($peg2);
3498                if (exists $targetHash{$genome2}) {
3499                    # Here it is. Now we need to add it to the return hash. How we do that depends
3500                    # on whether or not $peg1 is new to us.
3501                    if (! exists $retVal{$peg1}) {
3502                        $retVal{$peg1} = { $peg2 => $score };
3503                    } else {
3504                        $retVal{$peg1}->{$peg2} = $score;
3505                    }
3506                }
3507            }
3508        }
3509        # Return the result.
3510        return %retVal;
3511    }
3512    
3513    
3514  =head3 LowBBHs  =head3 LowBBHs
3515    
3516  C<< my %bbhMap = $sprout->GoodBBHs($featureID, $cutoff); >>      my %bbhMap = $sprout->LowBBHs($featureID, $cutoff);
3517    
3518  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
3519  specified cutoff value. A higher cutoff value will allow inclusion of hits with  specified cutoff value. A higher cutoff value will allow inclusion of hits with
# Line 2532  Line 3539 
3539  #: Return Type %;  #: Return Type %;
3540  sub LowBBHs {  sub LowBBHs {
3541          # Get the parsameters.          # Get the parsameters.
3542          my $self = shift @_;      my ($self, $featureID, $cutoff) = @_;
         my ($featureID, $cutoff) = @_;  
3543          # Create the return hash.          # Create the return hash.
3544          my %retVal = ();          my %retVal = ();
3545          # Create a query to get the desired BBHs.      # Query for the desired BBHs.
3546          my @bbhList = $self->GetAll(['IsBidirectionalBestHitOf'],      my @bbhList = FIGRules::BBHData($featureID, $cutoff);
                                                                 'IsBidirectionalBestHitOf(sc) <= ? AND IsBidirectionalBestHitOf(from-link) = ?',  
                                                                 [$cutoff, $featureID],  
                                                                 ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(sc)']);  
3547          # Form the results into the return hash.          # Form the results into the return hash.
3548          for my $pair (@bbhList) {          for my $pair (@bbhList) {
3549                  $retVal{$pair->[0]} = $pair->[1];          my $fid = $pair->[0];
3550            if ($self->Exists('Feature', $fid)) {
3551                $retVal{$fid} = $pair->[1];
3552            }
3553          }          }
3554          # Return the result.          # Return the result.
3555          return %retVal;          return %retVal;
3556  }  }
3557    
3558    =head3 Sims
3559    
3560        my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters);
3561    
3562    Get a list of similarities for a specified feature. Similarity information is not kept in the
3563    Sprout database; rather, they are retrieved from a network server. The similarities are
3564    returned as B<Sim> objects. A Sim object is actually a list reference that has been blessed
3565    so that its elements can be accessed by name.
3566    
3567    Similarities can be either raw or expanded. The raw similarities are basic
3568    hits between features with similar DNA. Expanding a raw similarity drags in any
3569    features considered substantially identical. So, for example, if features B<A1>,
3570    B<A2>, and B<A3> are all substantially identical to B<A>, then a raw similarity
3571    B<[C,A]> would be expanded to B<[C,A] [C,A1] [C,A2] [C,A3]>.
3572    
3573    =over 4
3574    
3575    =item fid
3576    
3577    ID of the feature whose similarities are desired, or reference to a list of IDs
3578    of features whose similarities are desired.
3579    
3580    =item maxN
3581    
3582    Maximum number of similarities to return.
3583    
3584    =item maxP
3585    
3586    Minumum allowable similarity score.
3587    
3588    =item select
3589    
3590    Selection criterion: C<raw> means only raw similarities are returned; C<fig>
3591    means only similarities to FIG features are returned; C<all> means all expanded
3592    similarities are returned; and C<figx> means similarities are expanded until the
3593    number of FIG features equals the maximum.
3594    
3595    =item max_expand
3596    
3597    The maximum number of features to expand.
3598    
3599    =item filters
3600    
3601    Reference to a hash containing filter information, or a subroutine that can be
3602    used to filter the sims.
3603    
3604    =item RETURN
3605    
3606    Returns a reference to a list of similarity objects, or C<undef> if an error
3607    occurred.
3608    
3609    =back
3610    
3611    =cut
3612    
3613    sub Sims {
3614        # Get the parameters.
3615        my ($self, $fid, $maxN, $maxP, $select, $max_expand, $filters) = @_;
3616        # Create the shim object to test for deleted FIDs.
3617        my $shim = FidCheck->new($self);
3618        # Ask the network for sims.
3619        my $retVal = FIGRules::GetNetworkSims($shim, $fid, {}, $maxN, $maxP, $select, $max_expand, $filters);
3620        # Return the result.
3621        return $retVal;
3622    }
3623    
3624    =head3 IsAllGenomes
3625    
3626        my $flag = $sprout->IsAllGenomes(\@list, \@checkList);
3627    
3628    Return TRUE if all genomes in the second list are represented in the first list at
3629    least one. Otherwise, return FALSE. If the second list is omitted, the first list is
3630    compared to a list of all the genomes.
3631    
3632    =over 4
3633    
3634    =item list
3635    
3636    Reference to the list to be compared to the second list.
3637    
3638    =item checkList (optional)
3639    
3640    Reference to the comparison target list. Every genome ID in this list must occur at
3641    least once in the first list. If this parameter is omitted, a list of all the genomes
3642    is used.
3643    
3644    =item RETURN
3645    
3646    Returns TRUE if every item in the second list appears at least once in the
3647    first list, else FALSE.
3648    
3649    =back
3650    
3651    =cut
3652    
3653    sub IsAllGenomes {
3654        # Get the parameters.
3655        my ($self, $list, $checkList) = @_;
3656        # Supply the checklist if it was omitted.
3657        $checkList = [$self->Genomes()] if ! defined($checkList);
3658        # Create a hash of the original list.
3659        my %testList = map { $_ => 1 } @{$list};
3660        # Declare the return variable. We assume that the representation
3661        # is complete and stop at the first failure.
3662        my $retVal = 1;
3663        my $n = scalar @{$checkList};
3664        for (my $i = 0; $retVal && $i < $n; $i++) {
3665            if (! $testList{$checkList->[$i]}) {
3666                $retVal = 0;
3667            }
3668        }
3669        # Return the result.
3670        return $retVal;
3671    }
3672    
3673  =head3 GetGroups  =head3 GetGroups
3674    
3675  C<< my %groups = $sprout->GetGroups(\@groupList); >>      my %groups = $sprout->GetGroups(\@groupList);
3676    
3677  Return a hash mapping each group to the IDs of the genomes in the group.  Return a hash mapping each group to the IDs of the genomes in the group.
3678  A list of groups may be specified, in which case only those groups will be  A list of groups may be specified, in which case only those groups will be
# Line 2562  Line 3683 
3683  #: Return Type %@;  #: Return Type %@;
3684  sub GetGroups {  sub GetGroups {
3685      # Get the parameters.      # Get the parameters.
3686      my $self = shift @_;      my ($self, $groupList) = @_;
     my ($groupList) = @_;  
3687      # Declare the return value.      # Declare the return value.
3688      my %retVal = ();      my %retVal = ();
3689      # Determine whether we are getting all the groups or just some.      # Determine whether we are getting all the groups or just some.
# Line 2571  Line 3691 
3691          # Here we have a group list. Loop through them individually,          # Here we have a group list. Loop through them individually,
3692          # getting a list of the relevant genomes.          # getting a list of the relevant genomes.
3693          for my $group (@{$groupList}) {          for my $group (@{$groupList}) {
3694              my @genomeIDs = $self->GetFlat(['Genome'], "Genome(group-name) = ?",              my @genomeIDs = $self->GetFlat(['Genome'], "Genome(primary-group) = ?",
3695                  [$group], "Genome(id)");                  [$group], "Genome(id)");
3696              $retVal{$group} = \@genomeIDs;              $retVal{$group} = \@genomeIDs;
3697          }          }
# Line 2579  Line 3699 
3699          # Here we need all of the groups. In this case, we run through all          # Here we need all of the groups. In this case, we run through all
3700          # of the genome records, putting each one found into the appropriate          # of the genome records, putting each one found into the appropriate
3701          # group. Note that we use a filter clause to insure that only genomes          # group. Note that we use a filter clause to insure that only genomes
3702          # in groups are included in the return set.          # in real NMPDR groups are included in the return set.
3703          my @genomes = $self->GetAll(['Genome'], "Genome(group-name) > ' '", [],          my @genomes = $self->GetAll(['Genome'], "Genome(primary-group) <> ?",
3704                                      ['Genome(id)', 'Genome(group-name)']);                                      [$FIG_Config::otherGroup], ['Genome(id)', 'Genome(primary-group)']);
3705          # Loop through the genomes found.          # Loop through the genomes found.
3706          for my $genome (@genomes) {          for my $genome (@genomes) {
3707              # Pop this genome's ID off the current list.              # Get the genome ID and group, and add this genome to the group's list.
3708              my @groups = @{$genome};              my ($genomeID, $group) = @{$genome};
             my $genomeID = shift @groups;  
             # Loop through the groups, adding the genome ID to each group's  
             # list.  
             for my $group (@groups) {  
                 if (exists $retVal{$group}) {  
3709                      push @{$retVal{$group}}, $genomeID;                      push @{$retVal{$group}}, $genomeID;
                 } else {  
                     $retVal{$group} = [$genomeID];  
3710                  }                  }
3711              }              }
3712        # Return the hash we just built.
3713        return %retVal;
3714          }          }
3715    
3716    =head3 MyGenomes
3717    
3718        my @genomes = Sprout::MyGenomes($dataDir);
3719    
3720    Return a list of the genomes to be included in the Sprout.
3721    
3722    This method is provided for use during the Sprout load. It presumes the Genome load file has
3723    already been created. (It will be in the Sprout data directory and called either C<Genome>
3724    or C<Genome.dtx>.) Essentially, it reads in the Genome load file and strips out the genome
3725    IDs.
3726    
3727    =over 4
3728    
3729    =item dataDir
3730    
3731    Directory containing the Sprout load files.
3732    
3733    =back
3734    
3735    =cut
3736    #: Return Type @;
3737    sub MyGenomes {
3738        # Get the parameters.
3739        my ($dataDir) = @_;
3740        # Compute the genome file name.
3741        my $genomeFileName = LoadFileName($dataDir, "Genome");
3742        # Extract the genome IDs from the files.
3743        my @retVal = map { $_ =~ /^(\S+)/; $1 } Tracer::GetFile($genomeFileName);
3744        # Return the result.
3745        return @retVal;
3746      }      }
3747      # Return the hash we just built.  
3748    =head3 LoadFileName
3749    
3750        my $fileName = Sprout::LoadFileName($dataDir, $tableName);
3751    
3752    Return the name of the load file for the specified table in the specified data
3753    directory.
3754    
3755    =over 4
3756    
3757    =item dataDir
3758    
3759    Directory containing the Sprout load files.
3760    
3761    =item tableName
3762    
3763    Name of the table whose load file is desired.
3764    
3765    =item RETURN
3766    
3767    Returns the name of the file containing the load data for the specified table, or
3768    C<undef> if no load file is present.
3769    
3770    =back
3771    
3772    =cut
3773    #: Return Type $;
3774    sub LoadFileName {
3775        # Get the parameters.
3776        my ($dataDir, $tableName) = @_;
3777        # Declare the return variable.
3778        my $retVal;
3779        # Check for the various file names.
3780        if (-e "$dataDir/$tableName") {
3781            $retVal = "$dataDir/$tableName";
3782        } elsif (-e "$dataDir/$tableName.dtx") {
3783            $retVal = "$dataDir/$tableName.dtx";
3784        }
3785        # Return the result.
3786        return $retVal;
3787    }
3788    
3789    =head3 DeleteGenome
3790    
3791        my $stats = $sprout->DeleteGenome($genomeID, $testFlag);
3792    
3793    Delete a genome from the database.
3794    
3795    =over 4
3796    
3797    =item genomeID
3798    
3799    ID of the genome to delete
3800    
3801    =item testFlag
3802    
3803    If TRUE, then the DELETE statements will be traced, but no deletions will occur.
3804    
3805    =item RETURN
3806    
3807    Returns a statistics object describing the rows deleted.
3808    
3809    =back
3810    
3811    =cut
3812    #: Return Type $%;
3813    sub DeleteGenome {
3814        # Get the parameters.
3815        my ($self, $genomeID, $testFlag) = @_;
3816        # Perform the delete for the genome's features.
3817        my $retVal = $self->Delete('Feature', "fig|$genomeID.%", testMode => $testFlag);
3818        # Perform the delete for the primary genome data.
3819        my $stats = $self->Delete('Genome', $genomeID, testMode => $testFlag);
3820        $retVal->Accumulate($stats);
3821        # Return the result.
3822        return $retVal;
3823    }
3824    
3825    =head3 Fix
3826    
3827        my %fixedHash = $sprout->Fix(%groupHash);
3828    
3829    Prepare a genome group hash (like that returned by L</GetGroups>) for processing.
3830    The groups will be combined into the appropriate super-groups.
3831    
3832    =over 4
3833    
3834    =item groupHash
3835    
3836    Hash to be fixed up.
3837    
3838    =item RETURN
3839    
3840    Returns a fixed-up version of the hash.
3841    
3842    =back
3843    
3844    =cut
3845    
3846    sub Fix {
3847        # Get the parameters.
3848        my ($self, %groupHash) = @_;
3849        # Create the result hash.
3850        my %retVal = ();
3851        # Copy over the genomes.
3852        for my $groupID (keys %groupHash) {
3853            # Get the super-group name.
3854            my $realGroupID = $self->SuperGroup($groupID);
3855            # Append this group's genomes into the result hash
3856            # using the super-group name.
3857            push @{$retVal{$realGroupID}}, @{$groupHash{$groupID}};
3858        }
3859        # Return the result hash.
3860      return %retVal;      return %retVal;
3861  }  }
3862    
3863    =head3 GroupPageName
3864    
3865        my $name = $sprout->GroupPageName($group);
3866    
3867    Return the name of the page for the specified NMPDR group.
3868    
3869    =over 4
3870    
3871    =item group
3872    
3873    Name of the relevant group.
3874    
3875    =item RETURN
3876    
3877    Returns the relative page name (e.g. C<../content/campy.php>). If the group file is not in
3878    memory it will be read in.
3879    
3880    =back
3881    
3882    =cut
3883    
3884    sub GroupPageName {
3885        # Get the parameters.
3886        my ($self, $group) = @_;
3887        # Check for the group file data.
3888        my %superTable = $self->CheckGroupFile();
3889        # Compute the real group name.
3890        my $realGroup = $self->SuperGroup($group);
3891        # Get the associated page name.
3892        my $retVal = "../content/$superTable{$realGroup}->{page}";
3893        # Return the result.
3894        return $retVal;
3895    }
3896    
3897    
3898    =head3 AddProperty
3899    
3900        $sprout->AddProperty($featureID, $key, @values);
3901    
3902    Add a new attribute value (Property) to a feature.
3903    
3904    =over 4
3905    
3906    =item peg
3907    
3908    ID of the feature to which the attribute is to be added.
3909    
3910    =item key
3911    
3912    Name of the attribute (key).
3913    
3914    =item values
3915    
3916    Values of the attribute.
3917    
3918    =back
3919    
3920    =cut
3921    #: Return Type ;
3922    sub AddProperty {
3923        # Get the parameters.
3924        my ($self, $featureID, $key, @values) = @_;
3925        # Add the property using the attached attributes object.
3926        $self->{_ca}->AddAttribute($featureID, $key, @values);
3927    }
3928    
3929    =head3 CheckGroupFile
3930    
3931        my %groupData = $sprout->CheckGroupFile();
3932    
3933    Get the group file hash. The group file hash describes the relationship
3934    between a group and the super-group to which it belongs for purposes of
3935    display. The super-group name is computed from the first capitalized word
3936    in the actual group name. For each super-group, the group file contains
3937    the page name and a list of the species expected to be in the group.
3938    Each species is specified by a genus and a species name. A species name
3939    of C<0> implies an entire genus.
3940    
3941    This method returns a hash from super-group names to a hash reference. Each
3942    resulting hash reference contains the following fields.
3943    
3944    =over 4
3945    
3946    =item page
3947    
3948    The super-group's web page in the NMPDR.
3949    
3950    =item contents
3951    
3952    A list of 2-tuples, each containing a genus name followed by a species name
3953    (or 0, indicating all species). This list indicates which organisms belong
3954    in the super-group.
3955    
3956    =back
3957    
3958    =cut
3959    
3960    sub CheckGroupFile {
3961        # Get the parameters.
3962        my ($self) = @_;
3963        # Check to see if we already have this hash.
3964        if (! defined $self->{groupHash}) {
3965            # We don't, so we need to read it in.
3966            my %groupHash;
3967            # Read the group file.
3968            my @groupLines = Tracer::GetFile("$FIG_Config::sproutData/groups.tbl");
3969            # Loop through the list of sort-of groups.
3970            for my $groupLine (@groupLines) {
3971                my ($name, $page, @contents) = split /\t/, $groupLine;
3972                $groupHash{$name} = { page => $page,
3973                                      contents => [ map { [ split /\s*,\s*/, $_ ] } @contents ]
3974                                    };
3975            }
3976            # Save the hash.
3977            $self->{groupHash} = \%groupHash;
3978        }
3979        # Return the result.
3980        return %{$self->{groupHash}};
3981    }
3982    
3983    =head2 Virtual Methods
3984    
3985    =head3 CleanKeywords
3986    
3987        my $cleanedString = $sprout->CleanKeywords($searchExpression);
3988    
3989    Clean up a search expression or keyword list. This involves converting the periods
3990    in EC numbers to underscores, converting non-leading minus signs to underscores,
3991    a vertical bar or colon to an apostrophe, and forcing lower case for all alphabetic
3992    characters. In addition, any extra spaces are removed.
3993    
3994    =over 4
3995    
3996    =item searchExpression
3997    
3998    Search expression or keyword list to clean. Note that a search expression may
3999    contain boolean operators which need to be preserved. This includes leading
4000    minus signs.
4001    
4002    =item RETURN
4003    
4004    Cleaned expression or keyword list.
4005    
4006    =back
4007    
4008    =cut
4009    
4010    sub CleanKeywords {
4011        # Get the parameters.
4012        my ($self, $searchExpression) = @_;
4013        # Perform the standard cleanup.
4014        my $words = $self->ERDB::CleanKeywords($searchExpression);
4015        # Fix the periods in EC and TC numbers.
4016        $words =~ s/(\d+|\-)\.(\d+|-)\.(\d+|-)\.(\d+|-)/$1_$2_$3_$4/g;
4017        # Fix non-trailing periods.
4018        $words =~ s/\.(\w)/_$1/g;
4019        # Fix non-leading minus signs.
4020        $words =~ s/(\w)[\-]/$1_/g;
4021        # Fix the vertical bars and colons
4022        $words =~ s/(\w)[|:](\w)/$1'$2/g;
4023        # Now split up the list so that each keyword is in its own string. We keep the delimiters
4024        # because they may contain boolean expression data.
4025        my @words = split /([^A-Za-z'0-9_]+)/, $words;
4026        # We'll convert the stemmable words into stems and re-assemble the result.
4027        my $retVal = "";
4028        for my $word (@words) {
4029            my $stem = $self->Stem($word);
4030            if (defined $stem) {
4031                $retVal .= $stem;
4032            } else {
4033                $retVal .= $word;
4034            }
4035        }
4036        Trace("Cleaned keyword list for \"$searchExpression\" is \"$retVal\".") if T(3);
4037        # Return the result.
4038        return $retVal;
4039    }
4040    
4041  =head2 Internal Utility Methods  =head2 Internal Utility Methods
4042    
4043  =head3 ParseAssignment  =head3 ParseAssignment
4044    
4045  Parse annotation text to determine whether or not it is a functional assignment. If it is,  Parse annotation text to determine whether or not it is a functional assignment. If it is,
4046  the user and function text will be returned as a 2-element list. If it isn't, an empty list  the user, function text, and assigning user will be returned as a 3-element list. If it
4047  will be returned.  isn't, an empty list will be returned.
4048    
4049    A functional assignment is always of the form
4050    
4051        set YYYY function to
4052        ZZZZ
4053    
4054    where I<YYYY> is the B<user>, and I<ZZZZ> is the actual functional role. In most cases,
4055    the user and the assigning user (from MadeAnnotation) will be the same, but that is
4056    not always the case.
4057    
4058    In addition, the functional role may contain extra data that is stripped, such as
4059    terminating spaces or a comment separated from the rest of the text by a tab.
4060    
4061  This is a static method.  This is a static method.
4062    
4063  =over 4  =over 4
4064    
4065    =item user
4066    
4067    Name of the assigning user.
4068    
4069  =item text  =item text
4070    
4071  Text of the annotation.  Text of the annotation.
# Line 2627  Line 4079 
4079    
4080  =cut  =cut
4081    
4082  sub ParseAssignment {  sub _ParseAssignment {
4083          # Get the parameters.          # Get the parameters.
4084          my ($text) = @_;      my ($user, $text) = @_;
4085          # Declare the return value.          # Declare the return value.
4086          my @retVal = ();          my @retVal = ();