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

Diff of /Sprout/Sprout.pm

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

revision 1.7, Wed Jan 26 22:26:09 2005 UTC revision 1.86, Sun Sep 24 17:14:16 2006 UTC
# Line 1  Line 1 
1  package Sprout;  package Sprout;
2    
3        require Exporter;
4        use ERDB;
5        @ISA = qw(Exporter ERDB);
6          use Data::Dumper;          use Data::Dumper;
7          use strict;          use strict;
8          use Carp;          use Carp;
# Line 7  Line 10 
10          use XML::Simple;          use XML::Simple;
11          use DBQuery;          use DBQuery;
12          use DBObject;          use DBObject;
         use ERDB;  
13          use Tracer;          use Tracer;
14          use FIGRules;          use FIGRules;
15        use FidCheck;
16          use Stats;          use Stats;
17      use POSIX qw(strftime);      use POSIX qw(strftime);
18        use BasicLocation;
19    
20  =head1 Sprout Database Manipulation Object  =head1 Sprout Database Manipulation Object
21    
# Line 32  Line 35 
35  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
36  L</dna_seq> returns the DNA sequence for a specified genome location.  L</dna_seq> returns the DNA sequence for a specified genome location.
37    
38    The Sprout object is a subclass of the ERDB object and inherits all its properties and methods.
39    
40  =cut  =cut
41    
42  #: Constructor SFXlate->new_sprout_only();  #: Constructor SFXlate->new_sprout_only();
# Line 62  Line 67 
67    
68  * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)  * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)
69    
70  * B<userData> user name and password, delimited by a slash (default C<root/>)  * B<userData> user name and password, delimited by a slash (default same as SEED)
71    
72  * B<port> connection port (default C<0>)  * B<port> connection port (default C<0>)
73    
74    * B<sock> connection socket (default same as SEED)
75    
76  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)
77    
78  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)
79    
80    * B<noDBOpen> suppresses the connection to the database if TRUE, else FALSE
81    
82  =back  =back
83    
84  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
# Line 83  Line 92 
92  sub new {  sub new {
93          # Get the parameters.          # Get the parameters.
94          my ($class, $dbName, $options) = @_;          my ($class, $dbName, $options) = @_;
95        # Compute the DBD directory.
96        my $dbd_dir = (defined($FIG_Config::dbd_dir) ? $FIG_Config::dbd_dir :
97                                                      $FIG_Config::fig );
98          # 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
99          # the incoming data.          # the incoming data.
100          my $optionTable = Tracer::GetOptions({          my $optionTable = Tracer::GetOptions({
101                                             dbType               => 'mysql',                     # database type                         dbType       => $FIG_Config::dbms,
102                                             dataDir              => 'Data',                      # data file directory                                                          # database type
103                                             xmlFileName  => 'SproutDBD.xml', # database definition file name                         dataDir      => $FIG_Config::sproutData,
104                                             userData             => 'root/',                     # user name and password                                                          # data file directory
105                                             port                 => 0,                           # database connection port                         xmlFileName  => "$dbd_dir/SproutDBD.xml",
106                                                            # database definition file name
107                           userData     => "$FIG_Config::dbuser/$FIG_Config::dbpass",
108                                                            # user name and password
109                           port         => $FIG_Config::dbport,
110                                                            # database connection port
111                           sock         => $FIG_Config::dbsock,
112                           host         => $FIG_Config::dbhost,
113                                             maxSegmentLength => 4500,            # maximum feature segment length                                             maxSegmentLength => 4500,            # maximum feature segment length
114                                             maxSequenceLength => 8000,           # maximum contig sequence length                                             maxSequenceLength => 8000,           # maximum contig sequence length
115                           noDBOpen     => 0,               # 1 to suppress the database open
116                                            }, $options);                                            }, $options);
117          # Get the data directory.          # Get the data directory.
118          my $dataDir = $optionTable->{dataDir};          my $dataDir = $optionTable->{dataDir};
# Line 100  Line 120 
120          $optionTable->{userData} =~ m!([^/]*)/(.*)$!;          $optionTable->{userData} =~ m!([^/]*)/(.*)$!;
121          my ($userName, $password) = ($1, $2);          my ($userName, $password) = ($1, $2);
122          # Connect to the database.          # Connect to the database.
123          my $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName, $password, $optionTable->{port});      my $dbh;
124        if (! $optionTable->{noDBOpen}) {
125            $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,
126                                    $password, $optionTable->{port}, $optionTable->{host}, $optionTable->{sock});
127        }
128          # Create the ERDB object.          # Create the ERDB object.
129          my $xmlFileName = "$optionTable->{xmlFileName}";          my $xmlFileName = "$optionTable->{xmlFileName}";
130          my $erdb = ERDB->new($dbh, $xmlFileName);      my $retVal = ERDB::new($class, $dbh, $xmlFileName);
131          # Create this object.      # Add the option table and XML file name.
132          my $self = { _erdb => $erdb, _options => $optionTable, _xmlName => $xmlFileName };      $retVal->{_options} = $optionTable;
133          # Bless and return it.      $retVal->{_xmlName} = $xmlFileName;
134          bless $self;      # Set up space for the group file data.
135          return $self;      $retVal->{groupHash} = undef;
136        # Return it.
137        return $retVal;
138  }  }
139    
140  =head3 MaxSegment  =head3 MaxSegment
# Line 124  Line 150 
150  =cut  =cut
151  #: Return Type $;  #: Return Type $;
152  sub MaxSegment {  sub MaxSegment {
153          my $self = shift @_;      my ($self) = @_;
154          return $self->{_options}->{maxSegmentLength};          return $self->{_options}->{maxSegmentLength};
155  }  }
156    
# Line 139  Line 165 
165  =cut  =cut
166  #: Return Type $;  #: Return Type $;
167  sub MaxSequence {  sub MaxSequence {
168          my $self = shift @_;      my ($self) = @_;
169          return $self->{_options}->{maxSequenceLength};          return $self->{_options}->{maxSequenceLength};
170  }  }
171    
172  =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.  
   
 C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ?", [$genus]); >>  
   
 The WHERE clause contains a single question mark, so there is a single additional  
 parameter representing the parameter value. It would also be possible to code  
   
 C<< $query = $sprout->Get(['Genome'], "Genome(genus) = \'$genus\'"); >>  
   
 however, this version of the call would generate a syntax error if there were any quote  
 characters inside the variable C<$genus>.  
   
 The use of the strange parenthesized notation for field names enables us to distinguish  
 hyphens contained within field names from minus signs that participate in the computation  
 of the WHERE clause. All of the methods that manipulate fields will use this same notation.  
   
 It is possible to specify multiple entity and relationship names in order to retrieve more than  
 one object's data at the same time, which allows highly complex joined queries. For example,  
   
 C<< $query = $sprout->Get(['Genome', 'ComesFrom', 'Source'], "Genome(genus) = ?", [$genus]); >>  
   
 This query returns all the genomes for a particular genus and allows access to the  
 sources from which they came. The join clauses to go from Genome to Source are generated  
 automatically.  
   
 Finally, the filter clause can contain sort information. To do this, simply put an C<ORDER BY>  
 clause at the end of the filter. Field references in the ORDER BY section follow the same rules  
 as they do in the filter itself; in other words, each one must be of the form B<I<objectName>(I<fieldName>)>.  
 For example, the following filter string gets all genomes for a particular genus and sorts  
 them by species name.  
   
 C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ? ORDER BY Genome(species)", [$genus]); >>  
173    
174  It is also permissible to specify I<only> an ORDER BY clause. For example, the following invocation gets  C<< $sprout->Load($rebuild); >>;
 all genomes ordered by genus and species.  
175    
176  C<< $query = $sprout->Get(['Genome'], "ORDER BY Genome(genus), Genome(species)"); >>  Load the database from files in the data directory, optionally re-creating the tables.
177    
178  Odd things may happen if one of the ORDER BY fields is in a secondary relation. So, for example, an  This method always deletes the data from the database before loading, even if the tables are not
179  attempt to order B<Feature>s by alias may (depending on the underlying database engine used) cause  re-created. The data is loaded into the relations from files in the data directory either having the
180  a single feature to appear more than once.  same name as the target relation with no extension or with an extension of C<.dtx>. Files without an
181    extension are used in preference to the files with an extension.
182    
183  If multiple names are specified, then the query processor will automatically determine a  The files are loaded based on the presumption that each line of the file is a record in the
184  join path between the entities and relationships. The algorithm used is very simplistic.  relation, and the individual fields are delimited by tabs. Tab and new-line characters inside
185  In particular, you can't specify any entity or relationship more than once, and if a  fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must
186  relationship is recursive, the path is determined by the order in which the entity  be presented in the order given in the relation tables produced by the L</ShowMetaData> method.
 and the relationship appear. For example, consider a recursive relationship B<IsParentOf>  
 which relates B<People> objects to other B<People> objects. If the join path is  
 coded as C<['People', 'IsParentOf']>, then the people returned will be parents. If, however,  
 the join path is C<['IsParentOf', 'People']>, then the people returned will be children.  
187    
188  =over 4  =over 4
189    
190  =item objectNames  =item rebuild
   
 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  
191    
192  List of the parameters to be substituted in for the parameters marks in the filter clause.  TRUE if the data tables need to be created or re-created, else FALSE
193    
194  =item RETURN  =item RETURN
195    
196  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,
197    the number of errors, and a list of the error messages.
198    
199  =back  =back
200    
201  =cut  =cut
202    #: Return Type %;
203  sub Get {  sub Load {
204          # Get the parameters.          # Get the parameters.
205          my $self = shift @_;      my ($self, $rebuild) = @_;
206          my ($objectNames, $filterClause, $parameterList) = @_;      # Load the tables from the data directory.
207          # We differ from the ERDB Get method in that the parameter list is passed in as a list reference      my $retVal = $self->LoadTables($self->{_options}->{dataDir}, $rebuild);
208          # rather than a list of parameters. The next step is to convert the parameters from a reference      # Return the statistics.
209          # to a real list. We can only do this if the parameters have been specified.      return $retVal;
         my @parameters;  
         if ($parameterList) { @parameters = @{$parameterList}; }  
         return $self->{_erdb}->Get($objectNames, $filterClause, @parameters);  
210  }  }
211    
212  =head3 GetEntity  =head3 LoadUpdate
213    
214  C<< my $entityObject = $sprout->GetEntity($entityType, $ID); >>  C<< my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>
215    
216  Return an object describing the entity instance with a specified ID.  Load updates to one or more database tables. This method enables the client to make changes to one
217    or two tables without reloading the whole database. For each table, there must be a corresponding
218    file in the data directory, either with the same name as the table, or with a C<.dtx> suffix. So,
219    for example, to make updates to the B<FeatureTranslation> relation, there must be a
220    C<FeatureTranslation.dtx> file in the data directory. Unlike a full load, files without an extension
221    are not examined. This allows update files to co-exist with files from an original load.
222    
223  =over 4  =over 4
224    
225  =item entityType  =item truncateFlag
226    
227  Entity type name.  TRUE if the tables should be rebuilt before loading, else FALSE. A value of TRUE therefore causes
228    current data and schema of the tables to be replaced, while a value of FALSE means the new data
229    is added to the existing data in the various relations.
230    
231  =item ID  =item tableList
232    
233  ID of the desired entity.  List of the tables to be updated.
234    
235  =item RETURN  =item RETURN
236    
237  Returns a B<DBObject> representing the desired entity instance, or an undefined value if no  Returns a statistical object containing the number of records read, the number of duplicates found,
238  instance is found with the specified key.  the number of errors encountered, and a list of error messages.
239    
240  =back  =back
241    
242  =cut  =cut
243    #: Return Type $%;
244  sub GetEntity {  sub LoadUpdate {
245          # Get the parameters.          # Get the parameters.
246          my $self = shift @_;      my ($self, $truncateFlag, $tableList) = @_;
247          my ($entityType, $ID) = @_;      # Declare the return value.
248          # Create a query.      my $retVal = Stats->new();
249          my $query = $self->Get([$entityType], "$entityType(id) = ?", [$ID]);      # Get the data directory.
250          # Get the first (and only) object.      my $optionTable = $self->{_options};
251          my $retVal = $query->Fetch();      my $dataDir = $optionTable->{dataDir};
252          # Return the result.      # Loop through the incoming table names.
253        for my $tableName (@{$tableList}) {
254            # Find the table's file.
255            my $fileName = LoadFileName($dataDir, $tableName);
256            if (! $fileName) {
257                Trace("No load file found for $tableName in $dataDir.") if T(0);
258            } else {
259                # Attempt to load this table.
260                my $result = $self->LoadTable($fileName, $tableName, $truncateFlag);
261                # Accumulate the resulting statistics.
262                $retVal->Accumulate($result);
263            }
264        }
265        # Return the statistics.
266          return $retVal;          return $retVal;
267  }  }
268    
269  =head3 GetEntityValues  =head3 GenomeCounts
270    
271  C<< my @values = GetEntityValues($entityType, $ID, \@fields); >>  C<< my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete); >>
272    
273  Return a list of values from a specified entity instance.  Count the number of genomes in each domain. If I<$complete> is TRUE, only complete
274    genomes will be included in the counts.
275    
276  =over 4  =over 4
277    
278  =item entityType  =item complete
279    
280  Entity type name.  TRUE if only complete genomes are to be counted, FALSE if all genomes are to be
281    counted
 =item ID  
   
 ID of the desired entity.  
   
 =item fields  
   
 List of field names, each of the form I<objectName>C<(>I<fieldName>C<)>.  
282    
283  =item RETURN  =item RETURN
284    
285  Returns a flattened list of the values of the specified fields for the specified entity.  A six-element list containing the number of genomes in each of six categories--
286    Archaea, Bacteria, Eukaryota, Viral, Environmental, and Unknown, respectively.
287    
288  =back  =back
289    
290  =cut  =cut
291  #: Return Type @;  
292  sub GetEntityValues {  sub GenomeCounts {
293          # Get the parameters.          # Get the parameters.
294          my $self = shift @_;      my ($self, $complete) = @_;
295          my ($entityType, $ID, $fields) = @_;      # Set the filter based on the completeness flag.
296          # Get the specified entity.      my $filter = ($complete ? "Genome(complete) = 1" : "");
297          my $entity = $self->GetEntity($entityType, $ID);      # Get all the genomes and the related taxonomy information.
298          # Declare the return list.      my @genomes = $self->GetAll(['Genome'], $filter, [], ['Genome(id)', 'Genome(taxonomy)']);
299          my @retVal = ();      # Clear the counters.
300          # If we found the entity, push the values into the return list.      my ($arch, $bact, $euk, $vir, $env, $unk) = (0, 0, 0, 0, 0, 0);
301          if ($entity) {      # Loop through, counting the domains.
302                  push @retVal, $entity->Values($fields);      for my $genome (@genomes) {
303            if    ($genome->[1] =~ /^archaea/i)  { ++$arch }
304            elsif ($genome->[1] =~ /^bacter/i)   { ++$bact }
305            elsif ($genome->[1] =~ /^eukar/i)    { ++$euk }
306            elsif ($genome->[1] =~ /^vir/i)      { ++$vir }
307            elsif ($genome->[1] =~ /^env/i)      { ++$env }
308            else  { ++$unk }
309          }          }
310          # Return the result.      # Return the counts.
311          return @retVal;      return ($arch, $bact, $euk, $vir, $env, $unk);
312  }  }
313    
314  =head3 ShowMetaData  =head3 ContigCount
315    
316  C<< $sprout->ShowMetaData($fileName); >>  C<< my $count = $sprout->ContigCount($genomeID); >>
317    
318  This method outputs a description of the database to an HTML file in the data directory.  Return the number of contigs for the specified genome ID.
319    
320  =over 4  =over 4
321    
322  =item fileName  =item genomeID
323    
324    ID of the genome whose contig count is desired.
325    
326  Fully-qualified name to give to the output file.  =item RETURN
327    
328    Returns the number of contigs for the specified genome.
329    
330  =back  =back
331    
332  =cut  =cut
333    
334  sub ShowMetaData {  sub ContigCount {
335          # Get the parameters.          # Get the parameters.
336          my $self = shift @_;      my ($self, $genomeID) = @_;
337          my ($fileName) = @_;      # Get the contig count.
338          # Compute the file name.      my $retVal = $self->GetCount(['Contig', 'HasContig'], "HasContig(from-link) = ?", [$genomeID]);
339          my $options = $self->{_options};      # Return the result.
340          # Call the show method on the underlying ERDB object.      return $retVal;
         $self->{_erdb}->ShowMetaData($fileName);  
341  }  }
342    
343  =head3 Load  =head3 GeneMenu
   
 C<< $sprout->Load($rebuild); >>;  
   
 Load the database from files in the data directory, optionally re-creating the tables.  
344    
345  This method always deletes the data from the database before loading, even if the tables are not  C<< my $selectHtml = $sprout->GeneMenu(\%attributes, $filterString, \@params, $selected); >>
 re-created. The data is loaded into the relations from files in the data directory either having the  
 same name as the target relation with no extension or with an extension of C<.dtx>. Files without an  
 extension are used in preference to the files with an extension.  
346    
347  The files are loaded based on the presumption that each line of the file is a record in the  Return an HTML select menu of genomes. Each genome will be an option in the menu,
348  relation, and the individual fields are delimited by tabs. Tab and new-line characters inside  and will be displayed by name with the ID and a contig count attached. The selection
349  fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must  value will be the genome ID. The genomes will be sorted by genus/species name.
 be presented in the order given in the relation tables produced by the L</ShowMetaData> method.  
350    
351  =over 4  =over 4
352    
353  =item rebuild  =item attributes
   
 TRUE if the data tables need to be created or re-created, else FALSE  
   
 =item RETURN  
   
 Returns a statistical object containing the number of records read, the number of duplicates found,  
 the number of errors, and a list of the error messages.  
   
 =back  
354    
355  =cut  Reference to a hash mapping attributes to values for the SELECT tag generated.
 #: Return Type %;  
 sub Load {  
         # Get the parameters.  
         my $self = shift @_;  
         my ($rebuild) = @_;  
         # Get the database object.  
         my $erdb = $self->{_erdb};  
         # Load the tables from the data directory.  
         my $retVal = $erdb->LoadTables($self->{_options}->{dataDir}, $rebuild);  
         # Return the statistics.  
         return $retVal;  
 }  
356    
357  =head3 LoadUpdate  =item filterString
358    
359  C<< my %stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>  A filter string for use in selecting the genomes. The filter string must conform
360    to the rules for the C<< ERDB->Get >> method.
361    
362  Load updates to one or more database tables. This method enables the client to make changes to one  =item params
 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.  
363    
364  =over 4  Reference to a list of values to be substituted in for the parameter marks in
365    the filter string.
366    
367  =item truncateFlag  =item selected (optional)
368    
369  TRUE if the tables should be rebuilt before loading, else FALSE. A value of TRUE therefore causes  ID of the genome to be initially selected.
 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.  
370    
371  =item tableList  =item fast (optional)
372    
373  List of the tables to be updated.  If specified and TRUE, the contig counts will be omitted to improve performance.
374    
375  =item RETURN  =item RETURN
376    
377  Returns a statistical object containing the number of records read, the number of duplicates found,  Returns an HTML select menu with the specified genomes as selectable options.
 the number of errors encountered, and a list of error messages.  
378    
379  =back  =back
380    
381  =cut  =cut
382  #: Return Type $%;  
383  sub LoadUpdate {  sub GeneMenu {
384          # Get the parameters.          # Get the parameters.
385          my $self = shift @_;      my ($self, $attributes, $filterString, $params, $selected, $fast) = @_;
386          my ($truncateFlag, $tableList) = @_;      my $slowMode = ! $fast;
387          # Get the database object.      # Default to nothing selected. This prevents an execution warning if "$selected"
388          my $erdb = $self->{_erdb};      # is undefined.
389          # Declare the return value.      $selected = "" unless defined $selected;
390          my $retVal = Stats->new();      Trace("Gene Menu called with slow mode \"$slowMode\" and selection \"$selected\".") if T(3);
391          # Get the data directory.      # Start the menu.
392          my $optionTable = $self->{_options};      my $retVal = "<select " .
393          my $dataDir = $optionTable->{dataDir};          join(" ", map { "$_=\"$attributes->{$_}\"" } keys %{$attributes}) .
394          # Loop through the incoming table names.          ">\n";
395          for my $tableName (@{$tableList}) {      # Get the genomes.
396                  # Find the table's file.      my @genomes = $self->GetAll(['Genome'], $filterString, $params, ['Genome(id)',
397                  my $fileName = "$dataDir/$tableName";                                                                       'Genome(genus)',
398                  if (! -e $fileName) {                                                                       'Genome(species)',
399                          $fileName = "$fileName.dtx";                                                                       'Genome(unique-characterization)']);
400                  }      # Sort them by name.
401                  # Attempt to load this table.      my @sorted = sort { lc("$a->[1] $a->[2]") cmp lc("$b->[1] $b->[2]") } @genomes;
402                  my $result = $erdb->LoadTable($fileName, $tableName, $truncateFlag);      # Loop through the genomes, creating the option tags.
403                  # Accumulate the resulting statistics.      for my $genomeData (@sorted) {
404                  $retVal->Accumulate($result);          # Get the data for this genome.
405            my ($genomeID, $genus, $species, $strain) = @{$genomeData};
406            # Get the contig count.
407            my $contigInfo = "";
408            if ($slowMode) {
409                my $count = $self->ContigCount($genomeID);
410                my $counting = ($count == 1 ? "contig" : "contigs");
411                $contigInfo = "[$count $counting]";
412            }
413            # Find out if we're selected.
414            my $selectOption = ($selected eq $genomeID ? " selected" : "");
415            # Build the option tag.
416            $retVal .= "<option value=\"$genomeID\"$selectOption>$genus $species $strain ($genomeID)$contigInfo</option>\n";
417          }          }
418          # Return the statistics.      # Close the SELECT tag.
419        $retVal .= "</select>\n";
420        # Return the result.
421          return $retVal;          return $retVal;
422  }  }
423    
# Line 463  Line 433 
433  #: Return Type ;  #: Return Type ;
434  sub Build {  sub Build {
435          # Get the parameters.          # Get the parameters.
436          my $self = shift @_;      my ($self) = @_;
437          # Create the tables.          # Create the tables.
438          $self->{_erdb}->CreateTables;      $self->CreateTables();
439    }
440    
441    =head3 NmpdrGenomeMenu
442    
443    C<< my $htmlText = $sprout->NmpdrGenomeMenu(\%options, \@selected); >>
444    
445    This method creates a hierarchical HTML menu for NMPDR genomes organized by category. The
446    category indicates the low-level NMPDR group. Organizing the genomes in this way makes it
447    easier to select all genomes from a particular category.
448    
449    =over 4
450    
451    =item options
452    
453    Reference to a hash containing the options to be applied to the C<SELECT> tag form the menu.
454    Typical options would include C<name> to specify the field name, C<multiple> to specify
455    that multiple selections are allowed, and C<size> to set the number of rows to display
456    in the menu.
457    
458    =item selected
459    
460    Reference to a list containing the IDs of the genomes to be pre-selected. If the menu
461    is not intended to allow multiple selections, the list should be a singleton. If the
462    list is empty, nothing will be pre-selected.
463    
464    =item RETURN
465    
466    Returns the HTML text to generate a C<SELECT> menu inside a form.
467    
468    =back
469    
470    =cut
471    
472    sub NmpdrGenomeMenu {
473        # Get the parameters.
474        my ($self, $options, $selected) = @_;
475        # Get a list of all the genomes in group order. In fact, we only need them ordered
476        # by name (genus,species,strain), but putting primary-group in front enables us to
477        # take advantage of an existing index.
478        my @genomeList = $self->GetAll(['Genome'],
479                                       "ORDER BY Genome(primary-group), Genome(genus), Genome(species), Genome(unique-characterization)",
480                                       [], ['Genome(primary-group)', 'Genome(id)',
481                                            'Genome(genus)', 'Genome(species)',
482                                            'Genome(unique-characterization)']);
483        # Create a hash to organize the genomes by group. Each group will contain a list of
484        # 2-tuples, the first element being the genome ID and the second being the genome
485        # name.
486        my %groupHash = ();
487        for my $genome (@genomeList) {
488            # Get the genome data.
489            my ($group, $genomeID, $genus, $species, $strain) = @{$genome};
490            # Form the genome name.
491            my $name = "$genus $species";
492            if ($strain) {
493                $name .= " $strain";
494            }
495            # Push the genome into the group's list.
496            push @{$groupHash{$group}}, [$genomeID, $name];
497        }
498        # Now we are ready to unroll the menu out of the group hash. First, we sort the groups, putting
499        # the supporting-genome group last.
500        my @groups = sort grep { $_ ne $FIG_Config::otherGroup } keys %groupHash;
501        push @groups, $FIG_Config::otherGroup;
502        # Next, create a hash that specifies the pre-selected entries.
503        my %selectedHash = map { $_ => 1 } @{$selected};
504        # Create the SELECT tag and stuff it into the output array.
505        my $select = "<" . join(" ", 'SELECT', map { "$_=\"$options->{$_}\"" } keys %{$options}) . ">";
506        my @lines = ($select);
507        # Loop through the groups.
508        for my $group (@groups) {
509            # Create the option group tag.
510            my $tag = "<OPTGROUP name=\"$group\">";
511            push @lines, "  $tag";
512            # Get the genomes in the group.
513            for my $genome (@{$groupHash{$group}}) {
514                my ($genomeID, $name) = @{$genome};
515                # See if it's selected.
516                my $select = ($selectedHash{$genomeID} ? " selected" : "");
517                # Generate the option tag.
518                my $optionTag = "<OPTION value=\"$genomeID\"$select>$name</OPTION>";
519                push @lines, "    $optionTag";
520            }
521            # Close the option group.
522            push @lines, "  </OPTGROUP>";
523        }
524        # Close the SELECT tag.
525        push @lines, "</SELECT>";
526        # Assemble the lines into a string.
527        my $retVal = join("\n", @lines, "");
528        # Return the result.
529        return $retVal;
530  }  }
531    
532  =head3 Genomes  =head3 Genomes
# Line 478  Line 539 
539  #: Return Type @;  #: Return Type @;
540  sub Genomes {  sub Genomes {
541          # Get the parameters.          # Get the parameters.
542          my $self = shift @_;      my ($self) = @_;
543          # Get all the genomes.          # Get all the genomes.
544          my @retVal = $self->GetFlat(['Genome'], "", [], 'Genome(id)');          my @retVal = $self->GetFlat(['Genome'], "", [], 'Genome(id)');
545          # Return the list of IDs.          # Return the list of IDs.
# Line 508  Line 569 
569  #: Return Type $;  #: Return Type $;
570  sub GenusSpecies {  sub GenusSpecies {
571          # Get the parameters.          # Get the parameters.
572          my $self = shift @_;      my ($self, $genomeID) = @_;
         my ($genomeID) = @_;  
573          # Get the data for the specified genome.          # Get the data for the specified genome.
574          my @values = $self->GetEntityValues('Genome', $genomeID, ['Genome(genus)', 'Genome(species)',          my @values = $self->GetEntityValues('Genome', $genomeID, ['Genome(genus)', 'Genome(species)',
575                                                                                                                            'Genome(unique-characterization)']);                                                                                                                            'Genome(unique-characterization)']);
# Line 545  Line 605 
605  #: Return Type @;  #: Return Type @;
606  sub FeaturesOf {  sub FeaturesOf {
607          # Get the parameters.          # Get the parameters.
608          my $self = shift @_;      my ($self, $genomeID,$ftype) = @_;
         my ($genomeID,$ftype) = @_;  
609          # Get the features we want.          # Get the features we want.
610          my @features;          my @features;
611          if (!$ftype) {          if (!$ftype) {
# Line 590  Line 649 
649  =item RETURN  =item RETURN
650    
651  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
652  context and as a space-delimited string in a scalar context.  context and as a comma-delimited string in a scalar context.
653    
654  =back  =back
655    
# Line 599  Line 658 
658  #: Return Type $;  #: Return Type $;
659  sub FeatureLocation {  sub FeatureLocation {
660          # Get the parameters.          # Get the parameters.
661          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
662          # Create a query for the feature locations.          # Create a query for the feature locations.
663          my $query = $self->Get(['IsLocatedIn'], "IsLocatedIn(from-link) = ? ORDER BY IsLocatedIn(locN)",          my $query = $self->Get(['IsLocatedIn'], "IsLocatedIn(from-link) = ? ORDER BY IsLocatedIn(locN)",
664                                                     [$featureID]);                                                     [$featureID]);
# Line 618  Line 676 
676                  if ($prevContig eq $contigID && $dir eq $prevDir) {                  if ($prevContig eq $contigID && $dir eq $prevDir) {
677                          # Here the new segment is in the same direction on the same contig. Insure the                          # Here the new segment is in the same direction on the same contig. Insure the
678                          # new segment's beginning is next to the old segment's end.                          # new segment's beginning is next to the old segment's end.
679                          if (($dir eq "-" && $beg == $prevBeg - $prevLen) ||              if ($dir eq "-" && $beg + $len == $prevBeg) {
680                                  ($dir eq "+" && $beg == $prevBeg + $prevLen)) {                  # Here we're merging two backward blocks, so we keep the new begin point
681                                  # Here we need to merge two segments. Adjust the beginning and length values                  # and adjust the length.
682                                  # to include both segments.                  $len += $prevLen;
683                    # Pop the old segment off. The new one will replace it later.
684                    pop @retVal;
685                } elsif ($dir eq "+" && $beg == $prevBeg + $prevLen) {
686                    # Here we need to merge two forward blocks. Adjust the beginning and
687                    # length values to include both segments.
688                                  $beg = $prevBeg;                                  $beg = $prevBeg;
689                                  $len += $prevLen;                                  $len += $prevLen;
690                                  # Pop the old segment off. The new one will replace it later.                                  # Pop the old segment off. The new one will replace it later.
# Line 630  Line 693 
693                  }                  }
694                  # Remember this specifier for the adjacent-segment test the next time through.                  # Remember this specifier for the adjacent-segment test the next time through.
695                  ($prevContig, $prevBeg, $prevDir, $prevLen) = ($contigID, $beg, $dir, $len);                  ($prevContig, $prevBeg, $prevDir, $prevLen) = ($contigID, $beg, $dir, $len);
696            # Compute the initial base pair.
697            my $start = ($dir eq "+" ? $beg : $beg + $len - 1);
698                  # Add the specifier to the list.                  # Add the specifier to the list.
699                  push @retVal, "${contigID}_$beg$dir$len";          push @retVal, "${contigID}_$start$dir$len";
700          }          }
701          # Return the list in the format indicated by the context.          # Return the list in the format indicated by the context.
702          return (wantarray ? @retVal : join(' ', @retVal));      return (wantarray ? @retVal : join(',', @retVal));
703  }  }
704    
705  =head3 ParseLocation  =head3 ParseLocation
# Line 660  Line 725 
725  =cut  =cut
726  #: Return Type @;  #: Return Type @;
727  sub ParseLocation {  sub ParseLocation {
728          # Get the parameter.      # Get the parameter. Note that if we're called as an instance method, we ignore
729        # the first parameter.
730        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
731          my ($location) = @_;          my ($location) = @_;
732          # Parse it into segments.          # Parse it into segments.
733          $location =~ /^(.*)_(\d*)([+-_])(\d*)$/;      $location =~ /^(.+)_(\d+)([+\-_])(\d+)$/;
734          my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);          my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);
735          # If the direction is an underscore, convert it to a + or -.          # If the direction is an underscore, convert it to a + or -.
736          if ($dir eq "_") {          if ($dir eq "_") {
# Line 679  Line 746 
746          return ($contigID, $start, $dir, $len);          return ($contigID, $start, $dir, $len);
747  }  }
748    
749    
750    
751    =head3 PointLocation
752    
753    C<< my $found = Sprout::PointLocation($location, $point); >>
754    
755    Return the offset into the specified location of the specified point on the contig. If
756    the specified point is before the location, a negative value will be returned. If it is
757    beyond the location, an undefined value will be returned. It is assumed that the offset
758    is for the location's contig. The location can either be new-style (using a C<+> or C<->
759    and a length) or old-style (using C<_> and start and end positions.
760    
761    =over 4
762    
763    =item location
764    
765    A location specifier (see L</FeatureLocation> for a description).
766    
767    =item point
768    
769    The offset into the contig of the point in which we're interested.
770    
771    =item RETURN
772    
773    Returns the offset inside the specified location of the specified point, a negative
774    number if the point is before the location, or an undefined value if the point is past
775    the location. If the length of the location is 0, this method will B<always> denote
776    that it is outside the location. The offset will always be relative to the left-most
777    position in the location.
778    
779    =back
780    
781    =cut
782    #: Return Type $;
783    sub PointLocation {
784        # Get the parameter. Note that if we're called as an instance method, we ignore
785        # the first parameter.
786        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
787        my ($location, $point) = @_;
788        # Parse out the location elements. Note that this works on both old-style and new-style
789        # locations.
790        my ($contigID, $start, $dir, $len) = ParseLocation($location);
791        # Declare the return variable.
792        my $retVal;
793        # Compute the offset. The computation is dependent on the direction of the location.
794        my $offset = (($dir == '+') ? $point - $start : $point - ($start - $len + 1));
795        # Return the offset if it's valid.
796        if ($offset < $len) {
797            $retVal = $offset;
798        }
799        # Return the offset found.
800        return $retVal;
801    }
802    
803  =head3 DNASeq  =head3 DNASeq
804    
805  C<< my $sequence = $sprout->DNASeq(\@locationList); >>  C<< my $sequence = $sprout->DNASeq(\@locationList); >>
# Line 687  Line 808 
808  should be of the form returned by L</featureLocation> when in a list context. In other words,  should be of the form returned by L</featureLocation> when in a list context. In other words,
809  each location is of the form I<contigID>C<_>I<begin>I<dir>I<end>.  each location is of the form I<contigID>C<_>I<begin>I<dir>I<end>.
810    
811    For example, the following would return the DNA sequence for contig C<83333.1:NC_000913>
812    between positions 1401 and 1532, inclusive.
813    
814        my $sequence = $sprout->DNASeq('83333.1:NC_000913_1401_1532');
815    
816  =over 4  =over 4
817    
818  =item locationList  =item locationList
819    
820  List of location specifiers, each in the form I<contigID>C<_>I<begin>I<dir>I<end> (see  List of location specifiers, each in the form I<contigID>C<_>I<begin>I<dir>I<len> or
821  L</FeatureLocation> for more about this format).  I<contigID>C<_>I<begin>C<_>I<end> (see L</FeatureLocation> for more about this format).
822    
823  =item RETURN  =item RETURN
824    
# Line 704  Line 830 
830  #: Return Type $;  #: Return Type $;
831  sub DNASeq {  sub DNASeq {
832          # Get the parameters.          # Get the parameters.
833          my $self = shift @_;      my ($self, $locationList) = @_;
         my ($locationList) = @_;  
834          # Create the return string.          # Create the return string.
835          my $retVal = "";          my $retVal = "";
836          # Loop through the locations.          # Loop through the locations.
# Line 720  Line 845 
845                  # 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
846                  # before putting it in the return value.                  # before putting it in the return value.
847                  my ($start, $stop);                  my ($start, $stop);
848            Trace("Parse of \"$location\" is $beg$dir$len.") if T(SDNA => 4);
849                  if ($dir eq "+") {                  if ($dir eq "+") {
850                          $start = $beg;                          $start = $beg;
851                          $stop = $beg + $len - 1;                          $stop = $beg + $len - 1;
852                  } else {                  } else {
853                          $start = $beg + $len + 1;              $start = $beg - $len + 1;
854                          $stop = $beg;                          $stop = $beg;
855                  }                  }
856            Trace("Looking for sequences containing $start through $stop.") if T(SDNA => 4);
857                  my $query = $self->Get(['IsMadeUpOf','Sequence'],                  my $query = $self->Get(['IsMadeUpOf','Sequence'],
858                          "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .                          "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .
859                          " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",                          " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",
# Line 738  Line 865 
865                                  $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',                                  $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',
866                                                                     'IsMadeUpOf(len)']);                                                                     'IsMadeUpOf(len)']);
867                          my $stopPosition = $startPosition + $sequenceLength;                          my $stopPosition = $startPosition + $sequenceLength;
868                Trace("Sequence is from $startPosition to $stopPosition.") if T(SDNA => 4);
869                          # Figure out the start point and length of the relevant section.                          # Figure out the start point and length of the relevant section.
870                          my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);                          my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);
871                          my $len = ($stopPosition <= $stop ? $stopPosition : $stop) - $startPosition - $pos1;              my $len1 = ($stopPosition < $stop ? $stopPosition : $stop) + 1 - $startPosition - $pos1;
872                Trace("Position is $pos1 for length $len1.") if T(SDNA => 4);
873                          # Add the relevant data to the location data.                          # Add the relevant data to the location data.
874                          $locationDNA .= substr($sequenceData, $pos1, $len);              $locationDNA .= substr($sequenceData, $pos1, $len1);
875                  }                  }
876                  # 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.
877                  if ($dir eq '+') {                  if ($dir eq '+') {
878                          $retVal .= $locationDNA;                          $retVal .= $locationDNA;
879                  } else {                  } else {
880                          $locationDNA = join('', reverse split //, $locationDNA);              $retVal .= FIG::reverse_comp($locationDNA);
                         $retVal .= $locationDNA;  
881                  }                  }
882          }          }
883          # Return the result.          # Return the result.
# Line 778  Line 906 
906  #: Return Type @;  #: Return Type @;
907  sub AllContigs {  sub AllContigs {
908          # Get the parameters.          # Get the parameters.
909          my $self = shift @_;      my ($self, $genomeID) = @_;
         my ($genomeID) = @_;  
910          # Ask for the genome's Contigs.          # Ask for the genome's Contigs.
911          my @retVal = $self->GetFlat(['HasContig'], "HasContig(from-link) = ?", [$genomeID],          my @retVal = $self->GetFlat(['HasContig'], "HasContig(from-link) = ?", [$genomeID],
912                                                                  'HasContig(to-link)');                                                                  'HasContig(to-link)');
# Line 787  Line 914 
914          return @retVal;          return @retVal;
915  }  }
916    
917  =head3 ContigLength  =head3 GenomeLength
918    
919  C<< my $length = $sprout->ContigLength($contigID); >>  C<< my $length = $sprout->GenomeLength($genomeID); >>
920    
921  Compute the length of a contig.  Return the length of the specified genome in base pairs.
922    
923  =over 4  =over 4
924    
925  =item contigID  =item genomeID
926    
927  ID of the contig whose length is desired.  ID of the genome whose base pair count is desired.
928    
929  =item RETURN  =item RETURN
930    
931  Returns the number of positions in the contig.  Returns the number of base pairs in all the contigs of the specified
932    genome.
933    
934  =back  =back
935    
936  =cut  =cut
937  #: Return Type $;  
938  sub ContigLength {  sub GenomeLength {
939          # Get the parameters.          # Get the parameters.
940          my $self = shift @_;      my ($self, $genomeID) = @_;
941          my ($contigID) = @_;      # Declare the return variable.
         # Get the contig's last sequence.  
         my $query = $self->Get(['IsMadeUpOf'],  
                 "IsMadeUpOf(from-link) = ? ORDER BY IsMadeUpOf(start-position) DESC",  
                 [$contigID]);  
         my $sequence = $query->Fetch();  
         # Declare the return value.  
942          my $retVal = 0;          my $retVal = 0;
943          # Set it from the sequence data, if any.      # Get the genome's contig sequence lengths.
944          if ($sequence) {      my @lens = $self->GetFlat(['HasContig', 'IsMadeUpOf'], 'HasContig(from-link) = ?',
945                  my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);                         [$genomeID], 'IsMadeUpOf(len)');
946                  $retVal = $start + $len;      # Sum the lengths.
947          }      map { $retVal += $_ } @lens;
948          # Return the result.          # Return the result.
949          return $retVal;          return $retVal;
950  }  }
951    
952  =head3 GenesInRegion  =head3 FeatureCount
953    
954  C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>  C<< my $count = $sprout->FeatureCount($genomeID, $type); >>
955    
956  List the features which overlap a specified region in a contig.  Return the number of features of the specified type in the specified genome.
957    
958  =over 4  =over 4
959    
960  =item contigID  =item genomeID
   
 ID of the contig containing the region of interest.  
   
 =item start  
961    
962  Offset of the first residue in the region of interest.  ID of the genome whose feature count is desired.
963    
964  =item stop  =item type
965    
966  Offset of the last residue in the region of interest.  Type of feature to count (eg. C<peg>, C<rna>, etc.).
967    
968  =item RETURN  =item RETURN
969    
970  Returns a three-element list. The first element is a list of feature IDs for the features that  Returns the number of features of the specified type for the specified genome.
 overlap the region of interest. The second and third elements are the minimum and maximum  
 locations of the features provided on the specified contig. These may extend outside  
 the start and stop values.  
971    
972  =back  =back
973    
974  =cut  =cut
975  #: Return Type @;  
976  sub GenesInRegion {  sub FeatureCount {
977          # Get the parameters.          # Get the parameters.
978          my $self = shift @_;      my ($self, $genomeID, $type) = @_;
979          my ($contigID, $start, $stop) = @_;      # Compute the count.
980          # Get the maximum segment length.      my $retVal = $self->GetCount(['HasFeature', 'Feature'],
981                                    "HasFeature(from-link) = ? AND Feature(feature-type) = ?",
982                                    [$genomeID, $type]);
983        # Return the result.
984        return $retVal;
985    }
986    
987    =head3 GenomeAssignments
988    
989    C<< my $fidHash = $sprout->GenomeAssignments($genomeID); >>
990    
991    Return a list of a genome's assigned features. The return hash will contain each
992    assigned feature of the genome mapped to the text of its most recent functional
993    assignment.
994    
995    =over 4
996    
997    =item genomeID
998    
999    ID of the genome whose functional assignments are desired.
1000    
1001    =item RETURN
1002    
1003    Returns a reference to a hash which maps each feature to its most recent
1004    functional assignment.
1005    
1006    =back
1007    
1008    =cut
1009    
1010    sub GenomeAssignments {
1011        # Get the parameters.
1012        my ($self, $genomeID) = @_;
1013        # Declare the return variable.
1014        my $retVal = {};
1015        # Query the genome's features and annotations. We'll put the oldest annotations
1016        # first so that the last assignment to go into the hash will be the correct one.
1017        my $query = $self->Get(['HasFeature', 'IsTargetOfAnnotation', 'Annotation'],
1018                               "HasFeature(from-link) = ? ORDER BY Annotation(time)",
1019                               [$genomeID]);
1020        # Loop through the annotations.
1021        while (my $data = $query->Fetch) {
1022            # Get the feature ID and annotation text.
1023            my ($fid, $annotation) = $data->Values(['HasFeature(to-link)',
1024                                                    'Annotation(annotation)']);
1025            # Check to see if this is an assignment. Note that the user really
1026            # doesn't matter to us, other than we use it to determine whether or
1027            # not this is an assignment.
1028            my ($user, $assignment) = _ParseAssignment('fig', $annotation);
1029            if ($user) {
1030                # Here it's an assignment. We put it in the return hash, overwriting
1031                # any older assignment that might be present.
1032                $retVal->{$fid} = $assignment;
1033            }
1034        }
1035        # Return the result.
1036        return $retVal;
1037    }
1038    
1039    =head3 ContigLength
1040    
1041    C<< my $length = $sprout->ContigLength($contigID); >>
1042    
1043    Compute the length of a contig.
1044    
1045    =over 4
1046    
1047    =item contigID
1048    
1049    ID of the contig whose length is desired.
1050    
1051    =item RETURN
1052    
1053    Returns the number of positions in the contig.
1054    
1055    =back
1056    
1057    =cut
1058    #: Return Type $;
1059    sub ContigLength {
1060        # Get the parameters.
1061        my ($self, $contigID) = @_;
1062        # Get the contig's last sequence.
1063        my $query = $self->Get(['IsMadeUpOf'],
1064            "IsMadeUpOf(from-link) = ? ORDER BY IsMadeUpOf(start-position) DESC",
1065            [$contigID]);
1066        my $sequence = $query->Fetch();
1067        # Declare the return value.
1068        my $retVal = 0;
1069        # Set it from the sequence data, if any.
1070        if ($sequence) {
1071            my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);
1072            $retVal = $start + $len - 1;
1073        }
1074        # Return the result.
1075        return $retVal;
1076    }
1077    
1078    =head3 ClusterPEGs
1079    
1080    C<< my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs); >>
1081    
1082    Cluster the PEGs in a list according to the cluster coding scheme of the specified
1083    subsystem. In order for this to work properly, the subsystem object must have
1084    been used recently to retrieve the PEGs using the B<get_pegs_from_cell> method.
1085    This causes the cluster numbers to be pulled into the subsystem's color hash.
1086    If a PEG is not found in the color hash, it will not appear in the output
1087    sequence.
1088    
1089    =over 4
1090    
1091    =item sub
1092    
1093    Sprout subsystem object for the relevant subsystem, from the L</get_subsystem>
1094    method.
1095    
1096    =item pegs
1097    
1098    Reference to the list of PEGs to be clustered.
1099    
1100    =item RETURN
1101    
1102    Returns a list of the PEGs, grouped into smaller lists by cluster number.
1103    
1104    =back
1105    
1106    =cut
1107    #: Return Type $@@;
1108    sub ClusterPEGs {
1109        # Get the parameters.
1110        my ($self, $sub, $pegs) = @_;
1111        # Declare the return variable.
1112        my $retVal = [];
1113        # Loop through the PEGs, creating arrays for each cluster.
1114        for my $pegID (@{$pegs}) {
1115            my $clusterNumber = $sub->get_cluster_number($pegID);
1116            # Only proceed if the PEG is in a cluster.
1117            if ($clusterNumber >= 0) {
1118                # Push this PEG onto the sub-list for the specified cluster number.
1119                push @{$retVal->[$clusterNumber]}, $pegID;
1120            }
1121        }
1122        # Return the result.
1123        return $retVal;
1124    }
1125    
1126    =head3 GenesInRegion
1127    
1128    C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>
1129    
1130    List the features which overlap a specified region in a contig.
1131    
1132    =over 4
1133    
1134    =item contigID
1135    
1136    ID of the contig containing the region of interest.
1137    
1138    =item start
1139    
1140    Offset of the first residue in the region of interest.
1141    
1142    =item stop
1143    
1144    Offset of the last residue in the region of interest.
1145    
1146    =item RETURN
1147    
1148    Returns a three-element list. The first element is a list of feature IDs for the features that
1149    overlap the region of interest. The second and third elements are the minimum and maximum
1150    locations of the features provided on the specified contig. These may extend outside
1151    the start and stop values. The first element (that is, the list of features) is sorted
1152    roughly by location.
1153    
1154    =back
1155    
1156    =cut
1157    #: Return Type @@;
1158    sub GenesInRegion {
1159        # Get the parameters.
1160        my ($self, $contigID, $start, $stop) = @_;
1161        # Get the maximum segment length.
1162          my $maximumSegmentLength = $self->MaxSegment;          my $maximumSegmentLength = $self->MaxSegment;
1163          # 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
1164          # duplicates easily.      # duplicates easily. The hash key will be the feature ID. The value will be a two-element
1165        # containing the minimum and maximum offsets. We will use the offsets to sort the results
1166        # when we're building the result set.
1167          my %featuresFound = ();          my %featuresFound = ();
1168          # Prime the values we'll use for the returned beginning and end.          # Prime the values we'll use for the returned beginning and end.
1169          my ($min, $max) = ($self->ContigLength($contigID), 0);      my @initialMinMax = ($self->ContigLength($contigID), 0);
1170        my ($min, $max) = @initialMinMax;
1171          # Create a table of parameters for each query. Each query looks for features travelling in          # Create a table of parameters for each query. Each query looks for features travelling in
1172          # 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,
1173          # 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
# Line 898  Line 1197 
1197                                          $found = 1;                                          $found = 1;
1198                                  }                                  }
1199                          } elsif ($dir eq '-') {                          } elsif ($dir eq '-') {
1200                                  $end = $beg - $len;                  # Note we switch things around so that the beginning is to the left of the
1201                                  if ($end <= $stop) {                  # ending.
1202                    ($beg, $end) = ($beg - $len, $beg);
1203                    if ($beg <= $stop) {
1204                                          # Denote we found a useful feature.                                          # Denote we found a useful feature.
1205                                          $found = 1;                                          $found = 1;
1206                                  }                                  }
1207                          }                          }
1208                          if ($found) {                          if ($found) {
1209                                  # Here we need to record the feature and update the minimum and maximum.                  # Here we need to record the feature and update the minima and maxima. First,
1210                                  $featuresFound{$featureID} = 1;                  # get the current entry for the specified feature.
1211                                  if ($beg < $min) { $min = $beg; }                  my ($loc1, $loc2) = (exists $featuresFound{$featureID} ? @{$featuresFound{$featureID}} :
1212                                  if ($end < $min) { $min = $end; }                                       @initialMinMax);
1213                                  if ($beg > $max) { $max = $beg; }                  # Merge the current segment's begin and end into the feature begin and end and the
1214                                  if ($end > $max) { $max = $end; }                  # global min and max.
1215                    if ($beg < $loc1) {
1216                        $loc1 = $beg;
1217                        $min = $beg if $beg < $min;
1218                    }
1219                    if ($end > $loc2) {
1220                        $loc2 = $end;
1221                        $max = $end if $end > $max;
1222                    }
1223                    # Store the entry back into the hash table.
1224                    $featuresFound{$featureID} = [$loc1, $loc2];
1225                          }                          }
1226                  }                  }
1227          }          }
1228          # Compute a list of the IDs for the features found.      # Now we must compute the list of the IDs for the features found. We start with a list
1229          my @list = (sort (keys %featuresFound));      # of midpoints / feature ID pairs. (It's not really a midpoint, it's twice the midpoint,
1230        # but the result of the sort will be the same.)
1231        my @list = map { [$featuresFound{$_}->[0] + $featuresFound{$_}->[1], $_] } keys %featuresFound;
1232        # Now we sort by midpoint and yank out the feature IDs.
1233        my @retVal = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @list;
1234          # Return it along with the min and max.          # Return it along with the min and max.
1235          return (\@list, $min, $max);      return (\@retVal, $min, $max);
1236  }  }
1237    
1238  =head3 FType  =head3 FType
# Line 943  Line 1258 
1258  #: Return Type $;  #: Return Type $;
1259  sub FType {  sub FType {
1260          # Get the parameters.          # Get the parameters.
1261          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
1262          # Get the specified feature's type.          # Get the specified feature's type.
1263          my ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(feature-type)']);          my ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(feature-type)']);
1264          # Return the result.          # Return the result.
# Line 953  Line 1267 
1267    
1268  =head3 FeatureAnnotations  =head3 FeatureAnnotations
1269    
1270  C<< my @descriptors = $sprout->FeatureAnnotations($featureID); >>  C<< my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag); >>
1271    
1272  Return the annotations of a feature.  Return the annotations of a feature.
1273    
# Line 963  Line 1277 
1277    
1278  ID of the feature whose annotations are desired.  ID of the feature whose annotations are desired.
1279    
1280    =item rawFlag
1281    
1282    If TRUE, the annotation timestamps will be returned in raw form; otherwise, they
1283    will be returned in human-readable form.
1284    
1285  =item RETURN  =item RETURN
1286    
1287  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.
1288    
1289  * B<featureID> ID of the relevant feature.  * B<featureID> ID of the relevant feature.
1290    
1291  * B<timeStamp> time the annotation was made, in user-friendly format.  * B<timeStamp> time the annotation was made.
1292    
1293  * B<user> ID of the user who made the annotation  * B<user> ID of the user who made the annotation
1294    
# Line 981  Line 1300 
1300  #: Return Type @%;  #: Return Type @%;
1301  sub FeatureAnnotations {  sub FeatureAnnotations {
1302          # Get the parameters.          # Get the parameters.
1303          my $self = shift @_;      my ($self, $featureID, $rawFlag) = @_;
         my ($featureID) = @_;  
1304          # 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.
1305          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1306                                                     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);                                                     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);
# Line 995  Line 1313 
1313                          $annotation->Values(['IsTargetOfAnnotation(from-link)',                          $annotation->Values(['IsTargetOfAnnotation(from-link)',
1314                                                                   'Annotation(time)', 'MadeAnnotation(from-link)',                                                                   'Annotation(time)', 'MadeAnnotation(from-link)',
1315                                                                   'Annotation(annotation)']);                                                                   'Annotation(annotation)']);
1316            # Convert the time, if necessary.
1317            if (! $rawFlag) {
1318                $timeStamp = FriendlyTimestamp($timeStamp);
1319            }
1320                  # Assemble them into a hash.                  # Assemble them into a hash.
1321          my $annotationHash = { featureID => $featureID,          my $annotationHash = { featureID => $featureID,
1322                                 timeStamp => FriendlyTimestamp($timeStamp),                                 timeStamp => $timeStamp,
1323                                                             user => $user, text => $text };                                                             user => $user, text => $text };
1324                  # Add it to the return list.                  # Add it to the return list.
1325                  push @retVal, $annotationHash;                  push @retVal, $annotationHash;
# Line 1011  Line 1333 
1333  C<< my %functions = $sprout->AllFunctionsOf($featureID); >>  C<< my %functions = $sprout->AllFunctionsOf($featureID); >>
1334    
1335  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
1336  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,
1337  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
1338  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,
1339  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.
1340  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
1341  functional assignments, we will only keep the most recent one.  recent one.
1342    
1343  =over 4  =over 4
1344    
# Line 1026  Line 1348 
1348    
1349  =item RETURN  =item RETURN
1350    
1351  Returns a hash mapping the functional assignment IDs to user IDs.  Returns a hash mapping the user IDs to functional assignment IDs.
1352    
1353  =back  =back
1354    
# Line 1034  Line 1356 
1356  #: Return Type %;  #: Return Type %;
1357  sub AllFunctionsOf {  sub AllFunctionsOf {
1358          # Get the parameters.          # Get the parameters.
1359          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
1360          # Get all of the feature's annotations.          # Get all of the feature's annotations.
1361      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation'],      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1362                                                      "IsTargetOfAnnotation(from-link) = ?",                                                      "IsTargetOfAnnotation(from-link) = ?",
1363                              [$featureID], ['Annotation(time)', 'Annotation(annotation)']);                              [$featureID], ['Annotation(time)', 'Annotation(annotation)',
1364                                               'MadeAnnotation(from-link)']);
1365          # Declare the return hash.          # Declare the return hash.
1366          my %retVal;          my %retVal;
     # Declare a hash for insuring we only make one assignment per user.  
     my %timeHash = ();  
1367      # Now we sort the assignments by timestamp in reverse.      # Now we sort the assignments by timestamp in reverse.
1368      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;
1369          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1370      for my $annotation (@sortedQuery) {      for my $annotation (@sortedQuery) {
1371          # Get the annotation fields.          # Get the annotation fields.
1372          my ($timeStamp, $text) = @{$annotation};          my ($timeStamp, $text, $user) = @{$annotation};
1373                  # Check to see if this is a functional assignment.                  # Check to see if this is a functional assignment.
1374                  my ($user, $function) = ParseAssignment($text);          my ($actualUser, $function) = _ParseAssignment($user, $text);
1375          if ($user && ! exists $timeHash{$user}) {          if ($actualUser && ! exists $retVal{$actualUser}) {
1376              # Here it is a functional assignment and there has been no              # Here it is a functional assignment and there has been no
1377              # previous assignment for this user, so we stuff it in the              # previous assignment for this user, so we stuff it in the
1378              # return hash.              # return hash.
1379                          $retVal{$function} = $user;              $retVal{$actualUser} = $function;
             # Insure we don't assign to this user again.  
             $timeHash{$user} = 1;  
1380                  }                  }
1381          }          }
1382          # Return the hash of assignments found.          # Return the hash of assignments found.
# Line 1073  Line 1391 
1391    
1392  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
1393  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 a functional
1394  assignment is a type of annotation. It has the format "XXXX\nset XXXX function to\nYYYYY". In this  assignment is a type of annotation. The format of an assignment is described in
1395  instance, XXXX is the user ID and YYYYY is the functional assignment text. Its worth noting that  L</ParseAssignment>. Its worth noting that we cannot filter on the content of the
1396  we cannot filter on the content of the annotation itself because it's a text field; however, this  annotation itself because it's a text field; however, this is not a big problem because
1397  is not a big problem because most features only have a small number of annotations.  most features only have a small number of annotations.
1398    
1399  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
1400  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 1108  Line 1426 
1426  #: Return Type $;  #: Return Type $;
1427  sub FunctionOf {  sub FunctionOf {
1428          # Get the parameters.          # Get the parameters.
1429          my $self = shift @_;      my ($self, $featureID, $userID) = @_;
         my ($featureID, $userID) = @_;  
1430      # Declare the return value.      # Declare the return value.
1431      my $retVal;      my $retVal;
1432      # Determine the ID type.      # Determine the ID type.
# Line 1137  Line 1454 
1454              }              }
1455          }          }
1456          # 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.
1457          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation'],          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1458                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1459                                 [$featureID]);                                 [$featureID]);
1460          my $timeSelected = 0;          my $timeSelected = 0;
1461          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1462          while (my $annotation = $query->Fetch()) {          while (my $annotation = $query->Fetch()) {
1463              # Get the annotation text.              # Get the annotation text.
1464              my ($text, $time) = $annotation->Values(['Annotation(annotation)','Annotation(time)']);              my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1465                                                         'Annotation(time)', 'MadeAnnotation(from-link)']);
1466              # 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.
1467              my ($user, $type, $function) = split(/\n/, $text);              my ($actualUser, $function) = _ParseAssignment($user, $text);
1468              if ($type =~ m/^set $user function to$/i) {              Trace("Assignment user is $actualUser, text is $function.") if T(4);
1469                if ($actualUser) {
1470                  # Here it is a functional assignment. Check the time and the user                  # Here it is a functional assignment. Check the time and the user
1471                  # name. The time must be recent and the user must be trusted.                  # name. The time must be recent and the user must be trusted.
1472                  if ((exists $trusteeTable{$user}) && ($time > $timeSelected)) {                  if ((exists $trusteeTable{$actualUser}) && ($time > $timeSelected)) {
1473                      $retVal = $function;                      $retVal = $function;
1474                      $timeSelected = $time;                      $timeSelected = $time;
1475                  }                  }
# Line 1166  Line 1485 
1485          return $retVal;          return $retVal;
1486  }  }
1487    
1488    =head3 FunctionsOf
1489    
1490    C<< my @functionList = $sprout->FunctionOf($featureID, $userID); >>
1491    
1492    Return the functional assignments of a particular feature.
1493    
1494    The functional assignment is handled differently depending on the type of feature. If
1495    the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional
1496    assignment is a type of annotation. The format of an assignment is described in
1497    L</ParseAssignment>. Its worth noting that we cannot filter on the content of the
1498    annotation itself because it's a text field; however, this is not a big problem because
1499    most features only have a small number of annotations.
1500    
1501    If the feature is B<not> identified by a FIG ID, then the functional assignment
1502    information is taken from the B<ExternalAliasFunc> table. If the table does
1503    not contain an entry for the feature, an empty list is returned.
1504    
1505    =over 4
1506    
1507    =item featureID
1508    
1509    ID of the feature whose functional assignments are desired.
1510    
1511    =item RETURN
1512    
1513    Returns a list of 2-tuples, each consisting of a user ID and the text of an assignment by
1514    that user.
1515    
1516    =back
1517    
1518    =cut
1519    #: Return Type @@;
1520    sub FunctionsOf {
1521        # Get the parameters.
1522        my ($self, $featureID) = @_;
1523        # Declare the return value.
1524        my @retVal = ();
1525        # Determine the ID type.
1526        if ($featureID =~ m/^fig\|/) {
1527            # Here we have a FIG feature ID. We must build the list of trusted
1528            # users.
1529            my %trusteeTable = ();
1530            # Build a query for all of the feature's annotations, sorted by date.
1531            my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1532                                   "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1533                                   [$featureID]);
1534            my $timeSelected = 0;
1535            # Loop until we run out of annotations.
1536            while (my $annotation = $query->Fetch()) {
1537                # Get the annotation text.
1538                my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1539                                                                'Annotation(time)',
1540                                                                'MadeAnnotation(user)']);
1541                # Check to see if this is a functional assignment for a trusted user.
1542                my ($actualUser, $function) = _ParseAssignment($user, $text);
1543                if ($actualUser) {
1544                    # Here it is a functional assignment.
1545                    push @retVal, [$actualUser, $function];
1546                }
1547            }
1548        } else {
1549            # Here we have a non-FIG feature ID. In this case the user ID does not
1550            # matter. We simply get the information from the External Alias Function
1551            # table.
1552            my @assignments = $self->GetEntityValues('ExternalAliasFunc', $featureID,
1553                                                     ['ExternalAliasFunc(func)']);
1554            push @retVal, map { ['master', $_] } @assignments;
1555        }
1556        # Return the assignments found.
1557        return @retVal;
1558    }
1559    
1560  =head3 BBHList  =head3 BBHList
1561    
1562  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>
# Line 1185  Line 1576 
1576    
1577  =item RETURN  =item RETURN
1578    
1579  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
1580  their best hits.  on the target genome.
1581    
1582  =back  =back
1583    
# Line 1194  Line 1585 
1585  #: Return Type %;  #: Return Type %;
1586  sub BBHList {  sub BBHList {
1587          # Get the parameters.          # Get the parameters.
1588          my $self = shift @_;      my ($self, $genomeID, $featureList) = @_;
         my ($genomeID, $featureList) = @_;  
1589          # Create the return structure.          # Create the return structure.
1590          my %retVal = ();          my %retVal = ();
1591          # Loop through the incoming features.          # Loop through the incoming features.
1592          for my $featureID (@{$featureList}) {          for my $featureID (@{$featureList}) {
1593                  # Create a query to get the feature's best hit.          # Ask the server for the feature's best hit.
1594                  my $query = $self->Get(['IsBidirectionalBestHitOf'],          my @bbhData = FIGRules::BBHData($featureID);
1595                                                             "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?",          # Peel off the BBHs found.
1596                                                             [$featureID, $genomeID]);          my @found = ();
1597                  # Look for the best hit.          for my $bbh (@bbhData) {
1598                  my $bbh = $query->Fetch;              push @found, $bbh->[0];
                 if ($bbh) {  
                         my ($targetFeature) = $bbh->Value('IsBidirectionalBestHitOf(to-link)');  
                         $retVal{$featureID} = $targetFeature;  
1599                  }                  }
1600            $retVal{$featureID} = \@found;
1601          }          }
1602          # Return the mapping.          # Return the mapping.
1603          return \%retVal;          return \%retVal;
1604  }  }
1605    
1606    =head3 SimList
1607    
1608    C<< my %similarities = $sprout->SimList($featureID, $count); >>
1609    
1610    Return a list of the similarities to the specified feature.
1611    
1612    This method just returns the bidirectional best hits for performance reasons.
1613    
1614    =over 4
1615    
1616    =item featureID
1617    
1618    ID of the feature whose similarities are desired.
1619    
1620    =item count
1621    
1622    Maximum number of similar features to be returned, or C<0> to return them all.
1623    
1624    =back
1625    
1626    =cut
1627    #: Return Type %;
1628    sub SimList {
1629        # Get the parameters.
1630        my ($self, $featureID, $count) = @_;
1631        # Ask for the best hits.
1632        my @lists = FIGRules::BBHData($featureID);
1633        # Create the return value.
1634        my %retVal = ();
1635        for my $tuple (@lists) {
1636            $retVal{$tuple->[0]} = $tuple->[1];
1637        }
1638        # Return the result.
1639        return %retVal;
1640    }
1641    
1642    =head3 IsComplete
1643    
1644    C<< my $flag = $sprout->IsComplete($genomeID); >>
1645    
1646    Return TRUE if the specified genome is complete, else FALSE.
1647    
1648    =over 4
1649    
1650    =item genomeID
1651    
1652    ID of the genome whose completeness status is desired.
1653    
1654    =item RETURN
1655    
1656    Returns TRUE if the genome is complete, FALSE if it is incomplete, and C<undef> if it is
1657    not found.
1658    
1659    =back
1660    
1661    =cut
1662    #: Return Type $;
1663    sub IsComplete {
1664        # Get the parameters.
1665        my ($self, $genomeID) = @_;
1666        # Declare the return variable.
1667        my $retVal;
1668        # Get the genome's data.
1669        my $genomeData = $self->GetEntity('Genome', $genomeID);
1670        if ($genomeData) {
1671            # The genome exists, so get the completeness flag.
1672            ($retVal) = $genomeData->Value('Genome(complete)');
1673        }
1674        # Return the result.
1675        return $retVal;
1676    }
1677    
1678  =head3 FeatureAliases  =head3 FeatureAliases
1679    
1680  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>
# Line 1238  Line 1698 
1698  #: Return Type @;  #: Return Type @;
1699  sub FeatureAliases {  sub FeatureAliases {
1700          # Get the parameters.          # Get the parameters.
1701          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
1702          # Get the desired feature's aliases          # Get the desired feature's aliases
1703          my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(alias)']);          my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(alias)']);
1704          # Return the result.          # Return the result.
# Line 1250  Line 1709 
1709    
1710  C<< my $genomeID = $sprout->GenomeOf($featureID); >>  C<< my $genomeID = $sprout->GenomeOf($featureID); >>
1711    
1712  Return the genome that contains a specified feature.  Return the genome that contains a specified feature or contig.
1713    
1714  =over 4  =over 4
1715    
1716  =item featureID  =item featureID
1717    
1718  ID of the feature whose genome is desired.  ID of the feature or contig whose genome is desired.
1719    
1720  =item RETURN  =item RETURN
1721    
1722  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
1723  an undefined value.  found, returns an undefined value.
1724    
1725  =back  =back
1726    
# Line 1269  Line 1728 
1728  #: Return Type $;  #: Return Type $;
1729  sub GenomeOf {  sub GenomeOf {
1730          # Get the parameters.          # Get the parameters.
1731          my $self = shift @_;      my ($self, $featureID) = @_;
1732          my ($featureID) = @_;      # Create a query to find the genome associated with the incoming ID.
1733          # Create a query to find the genome associated with the feature.      my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ? OR HasContig(to-link) = ?",
1734          my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ?", [$featureID]);                             [$featureID, $featureID]);
1735          # Declare the return value.          # Declare the return value.
1736          my $retVal;          my $retVal;
1737          # Get the genome ID.          # Get the genome ID.
# Line 1306  Line 1765 
1765  #: Return Type %;  #: Return Type %;
1766  sub CoupledFeatures {  sub CoupledFeatures {
1767          # Get the parameters.          # Get the parameters.
1768          my $self = shift @_;      my ($self, $featureID) = @_;
1769          my ($featureID) = @_;      Trace("Looking for features coupled to $featureID.") if T(coupling => 3);
1770          # Create a query to retrieve the functionally-coupled features. Note that we depend on the      # Create a query to retrieve the functionally-coupled features.
1771          # fact that the functional coupling is physically paired. If (A,B) is in the database, then      my $query = $self->Get(['ParticipatesInCoupling', 'Coupling'],
1772          # (B,A) will also be found.                             "ParticipatesInCoupling(from-link) = ?", [$featureID]);
         my $query = $self->Get(['IsClusteredOnChromosomeWith'],  
                                                    "IsClusteredOnChromosomeWith(from-link) = ?", [$featureID]);  
1773          # This value will be set to TRUE if we find at least one coupled feature.          # This value will be set to TRUE if we find at least one coupled feature.
1774          my $found = 0;          my $found = 0;
1775          # Create the return hash.          # Create the return hash.
1776          my %retVal = ();          my %retVal = ();
1777          # Retrieve the relationship records and store them in the hash.          # Retrieve the relationship records and store them in the hash.
1778          while (my $clustering = $query->Fetch()) {          while (my $clustering = $query->Fetch()) {
1779                  my ($otherFeatureID, $score) = $clustering->Values(['IsClusteredOnChromosomeWith(to-link)',          # Get the ID and score of the coupling.
1780                                                                      'IsClusteredOnChromosomeWith(score)']);          my ($couplingID, $score) = $clustering->Values(['Coupling(id)',
1781                                                            'Coupling(score)']);
1782            Trace("$featureID coupled with score $score to ID $couplingID.") if T(coupling => 4);
1783            # Get the other feature that participates in the coupling.
1784            my ($otherFeatureID) = $self->GetFlat(['ParticipatesInCoupling'],
1785                                               "ParticipatesInCoupling(to-link) = ? AND ParticipatesInCoupling(from-link) <> ?",
1786                                               [$couplingID, $featureID], 'ParticipatesInCoupling(from-link)');
1787            Trace("$couplingID target feature is $otherFeatureID.") if T(coupling => 4);
1788            # Attach the other feature's score to its ID.
1789                  $retVal{$otherFeatureID} = $score;                  $retVal{$otherFeatureID} = $score;
1790                  $found = 1;                  $found = 1;
1791          }          }
# Line 1333  Line 1798 
1798          return %retVal;          return %retVal;
1799  }  }
1800    
1801  =head3 GetEntityTypes  =head3 CouplingEvidence
1802    
1803    C<< my @evidence = $sprout->CouplingEvidence($peg1, $peg2); >>
1804    
1805    Return the evidence for a functional coupling.
1806    
1807  C<< my @entityList = $sprout->GetEntityTypes(); >>  A pair of features is considered evidence of a coupling between two other
1808    features if they occur close together on a contig and both are similar to
1809    the coupled features. So, if B<A1> and B<A2> are close together on a contig,
1810    B<B1> and B<B2> are considered evidence for the coupling if (1) B<B1> and
1811    B<B2> are close together, (2) B<B1> is similar to B<A1>, and (3) B<B2> is
1812    similar to B<A2>.
1813    
1814  Return the list of supported entity types.  The score of a coupling is determined by the number of pieces of evidence
1815    that are considered I<representative>. If several evidence items belong to
1816    a group of genomes that are close to each other, only one of those items
1817    is considered representative. The other evidence items are presumed to be
1818    there because of the relationship between the genomes rather than because
1819    the two proteins generated by the features have a related functionality.
1820    
1821    Each evidence item is returned as a three-tuple in the form C<[>I<$peg1a>C<,>
1822    I<$peg2a>C<,> I<$rep>C<]>, where I<$peg1a> is similar to I<$peg1>, I<$peg2a>
1823    is similar to I<$peg2>, and I<$rep> is TRUE if the evidence is representative
1824    and FALSE otherwise.
1825    
1826    =over 4
1827    
1828    =item peg1
1829    
1830    ID of the feature of interest.
1831    
1832    =item peg2
1833    
1834    ID of a feature functionally coupled to the feature of interest.
1835    
1836    =item RETURN
1837    
1838    Returns a list of 3-tuples. Each tuple consists of a feature similar to the feature
1839    of interest, a feature similar to the functionally coupled feature, and a flag
1840    that is TRUE for a representative piece of evidence and FALSE otherwise.
1841    
1842    =back
1843    
1844  =cut  =cut
1845  #: Return Type @;  #: Return Type @@;
1846  sub GetEntityTypes {  sub CouplingEvidence {
1847          # Get the parameters.          # Get the parameters.
1848          my $self = shift @_;      my ($self, $peg1, $peg2) = @_;
1849          # Get the underlying database object.      # Declare the return variable.
1850          my $erdb = $self->{_erdb};      my @retVal = ();
1851          # Get its entity type list.      # Our first task is to find out the nature of the coupling: whether or not
1852          my @retVal = $erdb->GetEntityTypes();      # it exists, its score, and whether the features are stored in the same
1853        # order as the ones coming in.
1854        my ($couplingID, $inverted, $score) = $self->GetCoupling($peg1, $peg2);
1855        # Only proceed if a coupling exists.
1856        if ($couplingID) {
1857            # Determine the ordering to place on the evidence items. If we're
1858            # inverted, we want to see feature 2 before feature 1 (descending); otherwise,
1859            # we want feature 1 before feature 2 (normal).
1860            Trace("Coupling evidence for ($peg1, $peg2) with inversion flag $inverted.") if T(Coupling => 4);
1861            my $ordering = ($inverted ? "DESC" : "");
1862            # Get the coupling evidence.
1863            my @evidenceList = $self->GetAll(['IsEvidencedBy', 'PCH', 'UsesAsEvidence'],
1864                                              "IsEvidencedBy(from-link) = ? ORDER BY PCH(id), UsesAsEvidence(pos) $ordering",
1865                                              [$couplingID],
1866                                              ['PCH(used)', 'UsesAsEvidence(to-link)']);
1867            # Loop through the evidence items. Each piece of evidence is represented by two
1868            # positions in the evidence list, one for each feature on the other side of the
1869            # evidence link. If at some point we want to generalize to couplings with
1870            # more than two positions, this section of code will need to be re-done.
1871            while (@evidenceList > 0) {
1872                my $peg1Data = shift @evidenceList;
1873                my $peg2Data = shift @evidenceList;
1874                Trace("Peg 1 is " . $peg1Data->[1] . " and Peg 2 is " . $peg2Data->[1] . ".") if T(Coupling => 4);
1875                push @retVal, [$peg1Data->[1], $peg2Data->[1], $peg1Data->[0]];
1876            }
1877            Trace("Last index in evidence result is is $#retVal.") if T(Coupling => 4);
1878        }
1879        # Return the result.
1880        return @retVal;
1881  }  }
1882    
1883  =head3 ReadFasta  =head3 GetCoupling
1884    
1885  C<< my %sequenceData = Sprout::ReadFasta($fileName, $prefix); >>  C<< my ($couplingID, $inverted, $score) = $sprout->GetCoupling($peg1, $peg2); >>
1886    
1887  Read sequence data from a FASTA-format file. Each sequence in a FASTA file is represented by  Return the coupling (if any) for the specified pair of PEGs. If a coupling
1888  one or more lines of data. The first line begins with a > character and contains an ID.  exists, we return the coupling ID along with an indicator of whether the
1889  The remaining lines contain the sequence data in order.  coupling is stored as C<(>I<$peg1>C<, >I<$peg2>C<)> or C<(>I<$peg2>C<, >I<$peg1>C<)>.
1890    In the second case, we say the coupling is I<inverted>. The importance of an
1891    inverted coupling is that the PEGs in the evidence will appear in reverse order.
1892    
1893  =over 4  =over 4
1894    
1895  =item fileName  =item peg1
1896    
1897  Name of the FASTA file.  ID of the feature of interest.
1898    
1899  =item prefix (optional)  =item peg2
1900    
1901  Prefix to be put in front of each ID found.  ID of the potentially coupled feature.
1902    
1903  =item RETURN  =item RETURN
1904    
1905  Returns a hash that maps each ID to its sequence.  Returns a three-element list. The first element contains the database ID of
1906    the coupling. The second element is FALSE if the coupling is stored in the
1907    database in the caller specified order and TRUE if it is stored in the
1908    inverted order. The third element is the coupling's score. If the coupling
1909    does not exist, all three list elements will be C<undef>.
1910    
1911  =back  =back
1912    
1913  =cut  =cut
1914  #: Return Type %;  #: Return Type $%@;
1915  sub ReadFasta {  sub GetCoupling {
1916        # Get the parameters.
1917        my ($self, $peg1, $peg2) = @_;
1918        # Declare the return values. We'll start with the coupling ID and undefine the
1919        # flag and score until we have more information.
1920        my ($retVal, $inverted, $score) = ($self->CouplingID($peg1, $peg2), undef, undef);
1921        # Find the coupling data.
1922        my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'],
1923                                     "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)",
1924                                     [$retVal], ["ParticipatesInCoupling(from-link)", "Coupling(score)"]);
1925        # Check to see if we found anything.
1926        if (!@pegs) {
1927            Trace("No coupling found.") if T(Coupling => 4);
1928            # No coupling, so undefine the return value.
1929            $retVal = undef;
1930        } else {
1931            # We have a coupling! Get the score and check for inversion.
1932            $score = $pegs[0]->[1];
1933            my $firstFound = $pegs[0]->[0];
1934            $inverted = ($firstFound ne $peg1);
1935            Trace("Coupling score is $score. First peg is $firstFound, peg 1 is $peg1.") if T(Coupling => 4);
1936        }
1937        # Return the result.
1938        return ($retVal, $inverted, $score);
1939    }
1940    
1941    =head3 GetSynonymGroup
1942    
1943    C<< my $id = $sprout->GetSynonymGroup($fid); >>
1944    
1945    Return the synonym group name for the specified feature.
1946    
1947    =over 4
1948    
1949    =item fid
1950    
1951    ID of the feature whose synonym group is desired.
1952    
1953    =item RETURN
1954    
1955    The name of the synonym group to which the feature belongs. If the feature does
1956    not belong to a synonym group, the feature ID itself is returned.
1957    
1958    =back
1959    
1960    =cut
1961    
1962    sub GetSynonymGroup {
1963        # Get the parameters.
1964        my ($self, $fid) = @_;
1965        # Declare the return variable.
1966        my $retVal;
1967        # Find the synonym group.
1968        my @groups = $self->GetFlat(['IsSynonymGroupFor'], "IsSynonymGroupFor(to-link) = ?",
1969                                       [$fid], 'IsSynonymGroupFor(from-link)');
1970        # Check to see if we found anything.
1971        if (@groups) {
1972            $retVal = $groups[0];
1973        } else {
1974            $retVal = $fid;
1975        }
1976        # Return the result.
1977        return $retVal;
1978    }
1979    
1980    =head3 GetBoundaries
1981    
1982    C<< my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList); >>
1983    
1984    Determine the begin and end boundaries for the locations in a list. All of the
1985    locations must belong to the same contig and have mostly the same direction in
1986    order for this method to produce a meaningful result. The resulting
1987    begin/end pair will contain all of the bases in any of the locations.
1988    
1989    =over 4
1990    
1991    =item locList
1992    
1993    List of locations to process.
1994    
1995    =item RETURN
1996    
1997    Returns a 3-tuple consisting of the contig ID, the beginning boundary,
1998    and the ending boundary. The beginning boundary will be left of the
1999    end for mostly-forward locations and right of the end for mostly-backward
2000    locations.
2001    
2002    =back
2003    
2004    =cut
2005    
2006    sub GetBoundaries {
2007        # Get the parameters.
2008        my ($self, @locList) = @_;
2009        # Set up the counters used to determine the most popular direction.
2010        my %counts = ( '+' => 0, '-' => 0 );
2011        # Get the last location and parse it.
2012        my $locObject = BasicLocation->new(pop @locList);
2013        # Prime the loop with its data.
2014        my ($contig, $beg, $end) = ($locObject->Contig, $locObject->Left, $locObject->Right);
2015        # Count its direction.
2016        $counts{$locObject->Dir}++;
2017        # Loop through the remaining locations. Note that in most situations, this loop
2018        # will not iterate at all, because most of the time we will be dealing with a
2019        # singleton list.
2020        for my $loc (@locList) {
2021            # Create a location object.
2022            my $locObject = BasicLocation->new($loc);
2023            # Count the direction.
2024            $counts{$locObject->Dir}++;
2025            # Get the left end and the right end.
2026            my $left = $locObject->Left;
2027            my $right = $locObject->Right;
2028            # Merge them into the return variables.
2029            if ($left < $beg) {
2030                $beg = $left;
2031            }
2032            if ($right > $end) {
2033                $end = $right;
2034            }
2035        }
2036        # If the most common direction is reverse, flip the begin and end markers.
2037        if ($counts{'-'} > $counts{'+'}) {
2038            ($beg, $end) = ($end, $beg);
2039        }
2040        # Return the result.
2041        return ($contig, $beg, $end);
2042    }
2043    
2044    =head3 CouplingID
2045    
2046    C<< my $couplingID = $sprout->CouplingID($peg1, $peg2); >>
2047    
2048    Return the coupling ID for a pair of feature IDs.
2049    
2050    The coupling ID is currently computed by joining the feature IDs in
2051    sorted order with a space. Client modules (that is, modules which
2052    use Sprout) should not, however, count on this always being the
2053    case. This method provides a way for abstracting the concept of a
2054    coupling ID. All that we know for sure about it is that it can be
2055    generated easily from the feature IDs and the order of the IDs
2056    in the parameter list does not matter (i.e. C<CouplingID("a1", "b1")>
2057    will have the same value as C<CouplingID("b1", "a1")>.
2058    
2059    =over 4
2060    
2061    =item peg1
2062    
2063    First feature of interest.
2064    
2065    =item peg2
2066    
2067    Second feature of interest.
2068    
2069    =item RETURN
2070    
2071    Returns the ID that would be used to represent a functional coupling of
2072    the two specified PEGs.
2073    
2074    =back
2075    
2076    =cut
2077    #: Return Type $;
2078    sub CouplingID {
2079        my ($self, @pegs) = @_;
2080        return $self->DigestKey(join " ", sort @pegs);
2081    }
2082    
2083    =head3 ReadFasta
2084    
2085    C<< my %sequenceData = Sprout::ReadFasta($fileName, $prefix); >>
2086    
2087    Read sequence data from a FASTA-format file. Each sequence in a FASTA file is represented by
2088    one or more lines of data. The first line begins with a > character and contains an ID.
2089    The remaining lines contain the sequence data in order.
2090    
2091    =over 4
2092    
2093    =item fileName
2094    
2095    Name of the FASTA file.
2096    
2097    =item prefix (optional)
2098    
2099    Prefix to be put in front of each ID found.
2100    
2101    =item RETURN
2102    
2103    Returns a hash that maps each ID to its sequence.
2104    
2105    =back
2106    
2107    =cut
2108    #: Return Type %;
2109    sub ReadFasta {
2110          # Get the parameters.          # Get the parameters.
2111          my ($fileName, $prefix) = @_;          my ($fileName, $prefix) = @_;
2112          # Create the return hash.          # Create the return hash.
# Line 1394  Line 2124 
2124                  if ($line =~ m/^>\s*(.+?)(\s|\n)/) {                  if ($line =~ m/^>\s*(.+?)(\s|\n)/) {
2125                          # 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.
2126                          if ($id) {                          if ($id) {
2127                                  $retVal{$id} = $sequence;                  $retVal{$id} = lc $sequence;
2128                          }                          }
2129                          # Clear the sequence accumulator and save the new ID.                          # Clear the sequence accumulator and save the new ID.
2130                          ($id, $sequence) = ("$prefix$1", "");                          ($id, $sequence) = ("$prefix$1", "");
2131                  } else {                  } else {
2132                          # 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.
2133                          # First, we get the actual data out.              # First, we get the actual data out. Note that we normalize to lower
2134                # case.
2135                          $line =~ /^\s*(.*?)(\s|\n)/;                          $line =~ /^\s*(.*?)(\s|\n)/;
2136                          $sequence .= $1;                          $sequence .= $1;
2137                  }                  }
2138          }          }
2139          # Flush out the last sequence (if any).          # Flush out the last sequence (if any).
2140          if ($sequence) {          if ($sequence) {
2141                  $retVal {$id} = $sequence;          $retVal{$id} = lc $sequence;
2142          }          }
2143        # Close the file.
2144        close FASTAFILE;
2145          # Return the hash constructed from the file.          # Return the hash constructed from the file.
2146          return %retVal;          return %retVal;
2147  }  }
# Line 1419  Line 2152 
2152    
2153  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
2154  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
2155  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,
2156    it will not be changed; otherwise, it will be converted. This method can also be used to
2157    perform the reverse task-- insuring that all the locations are in the old format.
2158    
2159  =over 4  =over 4
2160    
# Line 1446  Line 2181 
2181  #: Return Type @;  #: Return Type @;
2182  sub FormatLocations {  sub FormatLocations {
2183          # Get the parameters.          # Get the parameters.
2184          my $self = shift @_;      my ($self, $prefix, $locations, $oldFormat) = @_;
         my ($prefix, $locations, $oldFormat) = @_;  
2185          # Create the return list.          # Create the return list.
2186          my @retVal = ();          my @retVal = ();
2187          # Check to see if any locations were passed in.          # Check to see if any locations were passed in.
2188          if ($locations eq '') {          if ($locations eq '') {
2189              confess "No locations specified.";          Confess("No locations specified.");
2190          } else {          } else {
2191                  # Loop through the locations, converting them to the new format.                  # Loop through the locations, converting them to the new format.
2192                  for my $location (@{$locations}) {                  for my $location (@{$locations}) {
# Line 1487  Line 2221 
2221    
2222  sub DumpData {  sub DumpData {
2223          # Get the parameters.          # Get the parameters.
2224          my $self = shift @_;      my ($self) = @_;
2225          # Get the data directory name.          # Get the data directory name.
2226          my $outputDirectory = $self->{_options}->{dataDir};          my $outputDirectory = $self->{_options}->{dataDir};
2227          # Dump the relations.          # Dump the relations.
2228          $self->{_erdb}->DumpRelations($outputDirectory);      $self->DumpRelations($outputDirectory);
2229  }  }
2230    
2231  =head3 XMLFileName  =head3 XMLFileName
# Line 1503  Line 2237 
2237  =cut  =cut
2238  #: Return Type $;  #: Return Type $;
2239  sub XMLFileName {  sub XMLFileName {
2240          my $self = shift @_;      my ($self) = @_;
2241          return $self->{_xmlName};          return $self->{_xmlName};
2242  }  }
2243    
# Line 1523  Line 2257 
2257  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
2258  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>.
2259    
2260  C<< $sprout->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence = 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >>  C<< $sprout->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence => 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >>
2261    
2262  =over 4  =over 4
2263    
# Line 1541  Line 2275 
2275  #: Return Type ;  #: Return Type ;
2276  sub Insert {  sub Insert {
2277          # Get the parameters.          # Get the parameters.
2278          my $self = shift @_;      my ($self, $objectType, $fieldHash) = @_;
         my ($objectType, $fieldHash) = @_;  
2279          # Call the underlying method.          # Call the underlying method.
2280          $self->{_erdb}->InsertObject($objectType, $fieldHash);      $self->InsertObject($objectType, $fieldHash);
2281  }  }
2282    
2283  =head3 Annotate  =head3 Annotate
# Line 1583  Line 2316 
2316  #: Return Type $;  #: Return Type $;
2317  sub Annotate {  sub Annotate {
2318          # Get the parameters.          # Get the parameters.
2319          my $self = shift @_;      my ($self, $fid, $timestamp, $user, $text) = @_;
         my ($fid, $timestamp, $user, $text) = @_;  
2320          # Create the annotation ID.          # Create the annotation ID.
2321          my $aid = "$fid:$timestamp";          my $aid = "$fid:$timestamp";
2322          # Insert the Annotation object.          # Insert the Annotation object.
# Line 1604  Line 2336 
2336    
2337  =head3 AssignFunction  =head3 AssignFunction
2338    
2339  C<< my $ok = $sprout->AssignFunction($featureID, $user, $function); >>  C<< my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser); >>
2340    
2341  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
2342  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.  
2343    
2344  =over 4  =over 4
2345    
# Line 1618  Line 2349 
2349    
2350  =item user  =item user
2351    
2352  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>.
2353    
2354  =item function  =item function
2355    
2356  Text of the function being assigned.  Text of the function being assigned.
2357    
2358    =item assigningUser (optional)
2359    
2360    Name of the individual user making the assignment. If omitted, defaults to the user group.
2361    
2362  =item RETURN  =item RETURN
2363    
2364  Returns 1 if successful, 0 if an error occurred.  Returns 1 if successful, 0 if an error occurred.
# Line 1634  Line 2369 
2369  #: Return Type $;  #: Return Type $;
2370  sub AssignFunction {  sub AssignFunction {
2371          # Get the parameters.          # Get the parameters.
2372          my $self = shift @_;      my ($self, $featureID, $user, $function, $assigningUser) = @_;
2373          my ($featureID, $user, $function) = @_;      # Default the assigning user.
2374        if (! $assigningUser) {
2375            $assigningUser = $user;
2376        }
2377          # Create an annotation string from the parameters.          # Create an annotation string from the parameters.
2378          my $annotationText = "$user\nset $user function to\n$function";      my $annotationText = "$assigningUser\nset $user function to\n$function";
2379          # Get the current time.          # Get the current time.
2380          my $now = time;          my $now = time;
2381          # Declare the return variable.          # Declare the return variable.
# Line 1682  Line 2420 
2420  #: Return Type @;  #: Return Type @;
2421  sub FeaturesByAlias {  sub FeaturesByAlias {
2422          # Get the parameters.          # Get the parameters.
2423          my $self = shift @_;      my ($self, $alias) = @_;
         my ($alias) = @_;  
2424          # Declare the return variable.          # Declare the return variable.
2425          my @retVal = ();          my @retVal = ();
2426          # Parse the alias.          # Parse the alias.
# Line 1699  Line 2436 
2436          return @retVal;          return @retVal;
2437  }  }
2438    
 =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;  
 }  
   
2439  =head3 FeatureTranslation  =head3 FeatureTranslation
2440    
2441  C<< my $translation = $sprout->FeatureTranslation($featureID); >>  C<< my $translation = $sprout->FeatureTranslation($featureID); >>
# Line 1756  Line 2458 
2458  #: Return Type $;  #: Return Type $;
2459  sub FeatureTranslation {  sub FeatureTranslation {
2460          # Get the parameters.          # Get the parameters.
2461          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
2462          # Get the specified feature's translation.          # Get the specified feature's translation.
2463          my ($retVal) = $self->GetEntityValues("Feature", $featureID, ['Feature(translation)']);          my ($retVal) = $self->GetEntityValues("Feature", $featureID, ['Feature(translation)']);
2464          return $retVal;          return $retVal;
# Line 1789  Line 2490 
2490  #: Return Type @;  #: Return Type @;
2491  sub Taxonomy {  sub Taxonomy {
2492          # Get the parameters.          # Get the parameters.
2493          my $self = shift @_;      my ($self, $genome) = @_;
         my ($genome) = @_;  
2494          # Find the specified genome's taxonomy string.          # Find the specified genome's taxonomy string.
2495          my ($list) = $self->GetEntityValues('Genome', $genome, ['Genome(taxonomy)']);          my ($list) = $self->GetEntityValues('Genome', $genome, ['Genome(taxonomy)']);
2496          # Declare the return variable.          # Declare the return variable.
# Line 1833  Line 2533 
2533  #: Return Type $;  #: Return Type $;
2534  sub CrudeDistance {  sub CrudeDistance {
2535          # Get the parameters.          # Get the parameters.
2536          my $self = shift @_;      my ($self, $genome1, $genome2) = @_;
         my ($genome1, $genome2) = @_;  
2537          # Insure that the distance is commutative by sorting the genome IDs.          # Insure that the distance is commutative by sorting the genome IDs.
2538          my ($genomeA, $genomeB);          my ($genomeA, $genomeB);
2539          if ($genome2 < $genome2) {          if ($genome2 < $genome2) {
# Line 1881  Line 2580 
2580  #: Return Type $;  #: Return Type $;
2581  sub RoleName {  sub RoleName {
2582          # Get the parameters.          # Get the parameters.
2583          my $self = shift @_;      my ($self, $roleID) = @_;
         my ($roleID) = @_;  
2584          # Get the specified role's name.          # Get the specified role's name.
2585          my ($retVal) = $self->GetEntityValues('Role', $roleID, ['Role(name)']);          my ($retVal) = $self->GetEntityValues('Role', $roleID, ['Role(name)']);
2586          # Use the ID if the role has no name.          # Use the ID if the role has no name.
# Line 1915  Line 2613 
2613  #: Return Type @;  #: Return Type @;
2614  sub RoleDiagrams {  sub RoleDiagrams {
2615          # Get the parameters.          # Get the parameters.
2616          my $self = shift @_;      my ($self, $roleID) = @_;
         my ($roleID) = @_;  
2617          # Query for the diagrams.          # Query for the diagrams.
2618          my @retVal = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],          my @retVal = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],
2619                                                                  'RoleOccursIn(to-link)');                                                                  'RoleOccursIn(to-link)');
# Line 1924  Line 2621 
2621          return @retVal;          return @retVal;
2622  }  }
2623    
2624    =head3 GetProperties
2625    
2626    C<< my @list = $sprout->GetProperties($fid, $key, $value, $url); >>
2627    
2628    Return a list of the properties with the specified characteristics.
2629    
2630    Properties are arbitrary key-value pairs associated with a feature. (At some point they
2631    will also be associated with genomes.) A property value is represented by a 4-tuple of
2632    the form B<($fid, $key, $value, $url)>. These exactly correspond to the parameter
2633    
2634    =over 4
2635    
2636    =item fid
2637    
2638    ID of the feature possessing the property.
2639    
2640    =item key
2641    
2642    Name or key of the property.
2643    
2644    =item value
2645    
2646    Value of the property.
2647    
2648    =item url
2649    
2650    URL of the document that indicated the property should have this particular value, or an
2651    empty string if no such document exists.
2652    
2653    =back
2654    
2655    The parameters act as a filter for the desired data. Any non-null parameter will
2656    automatically match all the tuples returned. So, specifying just the I<$fid> will
2657    return all the properties of the specified feature; similarly, specifying the I<$key>
2658    and I<$value> parameters will return all the features having the specified property
2659    value.
2660    
2661    A single property key can have many values, representing different ideas about the
2662    feature in question. For example, one paper may declare that a feature C<fig|83333.1.peg.10> is
2663    virulent, and another may declare that it is not virulent. A query about the virulence of
2664    C<fig|83333.1.peg.10> would be coded as
2665    
2666        my @list = $sprout->GetProperties('fig|83333.1.peg.10', 'virulence', '', '');
2667    
2668    Here the I<$value> and I<$url> fields are left blank, indicating that those fields are
2669    not to be filtered. The tuples returned would be
2670    
2671        ('fig|83333.1.peg.10', 'virulence', 'yes', 'http://www.somewhere.edu/first.paper.pdf')
2672        ('fig|83333.1.peg.10', 'virulence', 'no', 'http://www.somewhere.edu/second.paper.pdf')
2673    
2674    =cut
2675    #: Return Type @@;
2676    sub GetProperties {
2677        # Get the parameters.
2678        my ($self, @parms) = @_;
2679        # Declare the return variable.
2680        my @retVal = ();
2681        # Now we need to create a WHERE clause that will get us the data we want. First,
2682        # we create a list of the columns containing the data for each parameter.
2683        my @colNames = ('HasProperty(from-link)', 'Property(property-name)',
2684                        'Property(property-value)', 'HasProperty(evidence)');
2685        # Now we build the WHERE clause and the list of parameter values.
2686        my @where = ();
2687        my @values = ();
2688        for (my $i = 0; $i <= $#colNames; $i++) {
2689            my $parm = $parms[$i];
2690            if (defined $parm && ($parm ne '')) {
2691                push @where, "$colNames[$i] = ?";
2692                push @values, $parm;
2693            }
2694        }
2695        # Format the WHERE clause.
2696        my $filter = (@values > 0 ? (join " AND ", @where) : undef);
2697        # Ask for all the propertie values with the desired characteristics.
2698        my $query = $self->Get(['HasProperty', 'Property'], $filter, \@values);
2699        while (my $valueObject = $query->Fetch()) {
2700            my @tuple = $valueObject->Values(\@colNames);
2701            push @retVal, \@tuple;
2702        }
2703        # Return the result.
2704        return @retVal;
2705    }
2706    
2707  =head3 FeatureProperties  =head3 FeatureProperties
2708    
2709  C<< my @properties = $sprout->FeatureProperties($featureID); >>  C<< my @properties = $sprout->FeatureProperties($featureID); >>
# Line 1953  Line 2733 
2733  #: Return Type @@;  #: Return Type @@;
2734  sub FeatureProperties {  sub FeatureProperties {
2735          # Get the parameters.          # Get the parameters.
2736          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
2737          # Get the properties.          # Get the properties.
2738          my @retVal = $self->GetAll(['HasProperty', 'Property'], "HasProperty(from-link) = ?", [$featureID],          my @retVal = $self->GetAll(['HasProperty', 'Property'], "HasProperty(from-link) = ?", [$featureID],
2739                                                          ['Property(property-name)', 'Property(property-value)',                                                          ['Property(property-name)', 'Property(property-value)',
# Line 1985  Line 2764 
2764  #: Return Type $;  #: Return Type $;
2765  sub DiagramName {  sub DiagramName {
2766          # Get the parameters.          # Get the parameters.
2767          my $self = shift @_;      my ($self, $diagramID) = @_;
         my ($diagramID) = @_;  
2768          # Get the specified diagram's name and return it.          # Get the specified diagram's name and return it.
2769          my ($retVal) = $self->GetEntityValues('Diagram', $diagramID, ['Diagram(name)']);          my ($retVal) = $self->GetEntityValues('Diagram', $diagramID, ['Diagram(name)']);
2770          return $retVal;          return $retVal;
# Line 2018  Line 2796 
2796  #: Return Type @;  #: Return Type @;
2797  sub MergedAnnotations {  sub MergedAnnotations {
2798          # Get the parameters.          # Get the parameters.
2799          my $self = shift @_;      my ($self, $list) = @_;
         my ($list) = @_;  
2800          # Create a list to hold the annotation tuples found.          # Create a list to hold the annotation tuples found.
2801          my @tuples = ();          my @tuples = ();
2802          # Loop through the features in the input list.          # Loop through the features in the input list.
# Line 2067  Line 2844 
2844  #: Return Type @;  #: Return Type @;
2845  sub RoleNeighbors {  sub RoleNeighbors {
2846          # Get the parameters.          # Get the parameters.
2847          my $self = shift @_;      my ($self, $roleID) = @_;
         my ($roleID) = @_;  
2848          # Get all the diagrams containing this role.          # Get all the diagrams containing this role.
2849          my @diagrams = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],          my @diagrams = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],
2850                                                                    'RoleOccursIn(to-link)');                                                                    'RoleOccursIn(to-link)');
# Line 2110  Line 2886 
2886  #: Return Type @;  #: Return Type @;
2887  sub FeatureLinks {  sub FeatureLinks {
2888          # Get the parameters.          # Get the parameters.
2889          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
2890          # Get the feature's links.          # Get the feature's links.
2891          my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(link)']);          my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(link)']);
2892          # Return the feature's links.          # Return the feature's links.
# Line 2123  Line 2898 
2898  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>
2899    
2900  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
2901  to the role the feature performs.  to the roles the feature performs.
2902    
2903  =over 4  =over 4
2904    
# Line 2133  Line 2908 
2908    
2909  =item RETURN  =item RETURN
2910    
2911  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.
2912    
2913  =back  =back
2914    
2915  =cut  =cut
2916  #: Return Type %;  #: Return Type %@;
2917  sub SubsystemsOf {  sub SubsystemsOf {
2918          # Get the parameters.          # Get the parameters.
2919          my $self = shift @_;      my ($self, $featureID) = @_;
2920          my ($featureID) = @_;      # Get the subsystem list.
         # Use the SSCell to connect features to subsystems.  
2921          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],
2922                                                                          "ContainsFeature(to-link) = ?", [$featureID],                                                                          "ContainsFeature(to-link) = ?", [$featureID],
2923                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);
2924          # Create the return value.          # Create the return value.
2925          my %retVal = ();          my %retVal = ();
2926        # Build a hash to weed out duplicates. Sometimes the same PEG and role appears
2927        # in two spreadsheet cells.
2928        my %dupHash = ();
2929          # Loop through the results, adding them to the hash.          # Loop through the results, adding them to the hash.
2930          for my $record (@subsystems) {          for my $record (@subsystems) {
2931                  $retVal{$record->[0]} = $record->[1];          # Get this subsystem and role.
2932            my ($subsys, $role) = @{$record};
2933            # Insure it's the first time for both.
2934            my $dupKey = "$subsys\n$role";
2935            if (! exists $dupHash{"$subsys\n$role"}) {
2936                $dupHash{$dupKey} = 1;
2937                push @{$retVal{$subsys}}, $role;
2938            }
2939          }          }
2940          # Return the hash.          # Return the hash.
2941          return %retVal;          return %retVal;
2942  }  }
2943    
2944    =head3 SubsystemList
2945    
2946    C<< my @subsystems = $sprout->SubsystemList($featureID); >>
2947    
2948    Return a list containing the names of the subsystems in which the specified
2949    feature participates. Unlike L</SubsystemsOf>, this method only returns the
2950    subsystem names, not the roles.
2951    
2952    =over 4
2953    
2954    =item featureID
2955    
2956    ID of the feature whose subsystem names are desired.
2957    
2958    =item RETURN
2959    
2960    Returns a list of the names of the subsystems in which the feature participates.
2961    
2962    =back
2963    
2964    =cut
2965    #: Return Type @;
2966    sub SubsystemList {
2967        # Get the parameters.
2968        my ($self, $featureID) = @_;
2969        # Get the list of names.
2970        my @retVal = $self->GetFlat(['ContainsFeature', 'HasSSCell'], "ContainsFeature(to-link) = ?",
2971                                    [$featureID], 'HasSSCell(from-link)');
2972        # Return the result.
2973        return @retVal;
2974    }
2975    
2976    =head3 GenomeSubsystemData
2977    
2978    C<< my %featureData = $sprout->GenomeSubsystemData($genomeID); >>
2979    
2980    Return a hash mapping genome features to their subsystem roles.
2981    
2982    =over 4
2983    
2984    =item genomeID
2985    
2986    ID of the genome whose subsystem feature map is desired.
2987    
2988    =item RETURN
2989    
2990    Returns a hash mapping each feature of the genome to a list of 2-tuples. Eacb
2991    2-tuple contains a subsystem name followed by a role ID.
2992    
2993    =back
2994    
2995    =cut
2996    
2997    sub GenomeSubsystemData {
2998        # Get the parameters.
2999        my ($self, $genomeID) = @_;
3000        # Declare the return variable.
3001        my %retVal = ();
3002        # Get a list of the genome features that participate in subsystems. For each
3003        # feature we get its spreadsheet cells and the corresponding roles.
3004        my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf'],
3005                                 "HasFeature(from-link) = ?", [$genomeID],
3006                                 ['HasFeature(to-link)', 'IsRoleOf(to-link)', 'IsRoleOf(from-link)']);
3007        # Now we get a list of the spreadsheet cells and their associated subsystems. Subsystems
3008        # with an unknown variant code (-1) are skipped. Note the genome ID is at both ends of the
3009        # list. We use it at the beginning to get all the spreadsheet cells for the genome and
3010        # again at the end to filter out participation in subsystems with a negative variant code.
3011        my @cellData = $self->GetAll(['IsGenomeOf', 'HasSSCell', 'ParticipatesIn'],
3012                                     "IsGenomeOf(from-link) = ? AND ParticipatesIn(variant-code) >= 0 AND ParticipatesIn(from-link) = ?",
3013                                     [$genomeID, $genomeID], ['HasSSCell(to-link)', 'HasSSCell(from-link)']);
3014        # Now "@roleData" lists the spreadsheet cell and role for each of the genome's features.
3015        # "@cellData" lists the subsystem name for each of the genome's spreadsheet cells. We
3016        # link these two lists together to create the result. First, we want a hash mapping
3017        # spreadsheet cells to subsystem names.
3018        my %subHash = map { $_->[0] => $_->[1] } @cellData;
3019        # We loop through @cellData to build the hash.
3020        for my $roleEntry (@roleData) {
3021            # Get the data for this feature and cell.
3022            my ($fid, $cellID, $role) = @{$roleEntry};
3023            # Check for a subsystem name.
3024            my $subsys = $subHash{$cellID};
3025            if ($subsys) {
3026                # Insure this feature has an entry in the return hash.
3027                if (! exists $retVal{$fid}) { $retVal{$fid} = []; }
3028                # Merge in this new data.
3029                push @{$retVal{$fid}}, [$subsys, $role];
3030            }
3031        }
3032        # Return the result.
3033        return %retVal;
3034    }
3035    
3036  =head3 RelatedFeatures  =head3 RelatedFeatures
3037    
3038  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>
# Line 2190  Line 3066 
3066  #: Return Type @;  #: Return Type @;
3067  sub RelatedFeatures {  sub RelatedFeatures {
3068          # Get the parameters.          # Get the parameters.
3069          my $self = shift @_;      my ($self, $featureID, $function, $userID) = @_;
         my ($featureID, $function, $userID) = @_;  
3070          # Get a list of the features that are BBHs of the incoming feature.          # Get a list of the features that are BBHs of the incoming feature.
3071          my @bbhFeatures = $self->GetFlat(['IsBidirectionalBestHitOf'],      my @bbhFeatures = map { $_->[0] } FIGRules::BBHData($featureID);
                                                                          "IsBidirectionalBestHitOf(from-link) = ?", [$featureID],  
                                                                          'IsBidirectionalBestHitOf(to-link)');  
3072          # Now we loop through the features, pulling out the ones that have the correct          # Now we loop through the features, pulling out the ones that have the correct
3073          # functional assignment.          # functional assignment.
3074          my @retVal = ();          my @retVal = ();
# Line 2239  Line 3112 
3112  #: Return Type @;  #: Return Type @;
3113  sub TaxonomySort {  sub TaxonomySort {
3114          # Get the parameters.          # Get the parameters.
3115          my $self = shift @_;      my ($self, $featureIDs) = @_;
         my ($featureIDs) = @_;  
3116          # Create the working hash table.          # Create the working hash table.
3117          my %hashBuffer = ();          my %hashBuffer = ();
3118          # Loop through the features.          # Loop through the features.
# Line 2249  Line 3121 
3121                  my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",                  my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",
3122                                                                                  [$fid], 'Genome(taxonomy)');                                                                                  [$fid], 'Genome(taxonomy)');
3123                  # Add this feature to the hash buffer.                  # Add this feature to the hash buffer.
3124                  if (exists $hashBuffer{$taxonomy}) {          Tracer::AddToListMap(\%hashBuffer, $taxonomy, $fid);
                         push @{$hashBuffer{$taxonomy}}, $fid;  
                 } else {  
                         $hashBuffer{$taxonomy} = [$fid];  
                 }  
3125          }          }
3126          # Sort the keys and get the elements.          # Sort the keys and get the elements.
3127          my @retVal = ();          my @retVal = ();
# Line 2264  Line 3132 
3132          return @retVal;          return @retVal;
3133  }  }
3134    
 =head3 GetAll  
   
 C<< my @list = $sprout->GetAll(\@objectNames, $filterClause, \@parameters, \@fields, $count); >>  
   
 Return a list of values taken from the objects returned by a query. The first three  
 parameters correspond to the parameters of the L</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.  
   
 The list returned will be a list of lists. Each element of the list will contain  
 the values returned for the fields specified in the fourth parameter. If one of the  
 fields specified returns multiple values, they are flattened in with the rest. For  
 example, the following call will return a list of the features in a particular  
 spreadsheet cell, and each feature will be represented by a list containing the  
 feature ID followed by all of its aliases.  
   
 C<< $query = $sprout->Get(['ContainsFeature', 'Feature'], "ContainsFeature(from-link) = ?", [$ssCellID], ['Feature(id)', 'Feature(alias)']); >>  
   
 =over 4  
   
 =item objectNames  
   
 List containing the names of the entity and relationship objects to be retrieved.  
   
 =item filterClause  
   
 WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can  
 be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form  
 B<I<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  
   
 List of the parameters to be substituted in for the parameters marks in the filter clause.  
   
 =item fields  
   
 List of the fields to be returned in each element of the list returned.  
   
 =item count  
   
 Maximum number of records to return. If omitted or 0, all available records will be returned.  
   
 =item RETURN  
   
 Returns a list of list references. Each element of the return list contains the values for the  
 fields specified in the B<fields> parameter.  
   
 =back  
   
 =cut  
 #: Return Type @@;  
 sub GetAll {  
         # Get the parameters.  
         my $self = shift @_;  
         my ($objectNames, $filterClause, $parameterList, $fields, $count) = @_;  
         # Create the query.  
         my $query = $self->Get($objectNames, $filterClause, $parameterList);  
         # Set up a counter of the number of records read.  
         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.  
         my @retVal = ();  
         while (($count == 0 || $fetched < $count) && (my $row = $query->Fetch())) {  
                 my @rowData = $row->Values($fields);  
                 push @retVal, \@rowData;  
                 $fetched++;  
         }  
         # Return the resulting list.  
         return @retVal;  
 }  
   
 =head3 GetFlat  
   
 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.  
   
 =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  
   
 List of the parameters to be substituted in for the parameters marks in the filter clause.  
   
 =item field  
   
 Name of the field to be used to get the elements of the list returned.  
   
 =item RETURN  
   
 Returns a list of values.  
   
 =back  
   
 =cut  
 #: Return Type @;  
 sub GetFlat {  
         # Get the parameters.  
         my $self = shift @_;  
         my ($objectNames, $filterClause, $parameterList, $field) = @_;  
         # Construct the query.  
         my $query = $self->Get($objectNames, $filterClause, $parameterList);  
         # Create the result list.  
         my @retVal = ();  
         # Loop through the records, adding the field values found to the result list.  
         while (my $row = $query->Fetch()) {  
                 push @retVal, $row->Value($field);  
         }  
         # Return the list created.  
         return @retVal;  
 }  
   
3135  =head3 Protein  =head3 Protein
3136    
3137  C<< my $protein = Sprout::Protein($sequence, $table); >>  C<< my $protein = Sprout::Protein($sequence, $table); >>
# Line 2495  Line 3229 
3229  #: Return Type @;  #: Return Type @;
3230  sub LoadInfo {  sub LoadInfo {
3231          # Get the parameters.          # Get the parameters.
3232          my $self = shift @_;      my ($self) = @_;
3233          # 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.
3234          my @retVal = ($self->{_options}->{dataDir});          my @retVal = ($self->{_options}->{dataDir});
3235          # Concatenate the table names.          # Concatenate the table names.
3236          push @retVal, $self->{_erdb}->GetTableNames();      push @retVal, $self->GetTableNames();
3237          # Return the result.          # Return the result.
3238          return @retVal;          return @retVal;
3239  }  }
3240    
3241  =head3 LowBBHs  =head3 LowBBHs
3242    
3243  C<< my %bbhMap = $sprout->GoodBBHs($featureID, $cutoff); >>  C<< my %bbhMap = $sprout->LowBBHs($featureID, $cutoff); >>
3244    
3245  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
3246  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 3266 
3266  #: Return Type %;  #: Return Type %;
3267  sub LowBBHs {  sub LowBBHs {
3268          # Get the parsameters.          # Get the parsameters.
3269          my $self = shift @_;      my ($self, $featureID, $cutoff) = @_;
         my ($featureID, $cutoff) = @_;  
3270          # Create the return hash.          # Create the return hash.
3271          my %retVal = ();          my %retVal = ();
3272          # Create a query to get the desired BBHs.      # Query for the desired BBHs.
3273          my @bbhList = $self->GetAll(['IsBidirectionalBestHitOf'],      my @bbhList = FIGRules::BBHData($featureID, $cutoff);
                                                                 'IsBidirectionalBestHitOf(sc) <= ? AND IsBidirectionalBestHitOf(from-link) = ?',  
                                                                 [$cutoff, $featureID],  
                                                                 ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(sc)']);  
3274          # Form the results into the return hash.          # Form the results into the return hash.
3275          for my $pair (@bbhList) {          for my $pair (@bbhList) {
3276                  $retVal{$pair->[0]} = $pair->[1];                  $retVal{$pair->[0]} = $pair->[1];
# Line 2549  Line 3279 
3279          return %retVal;          return %retVal;
3280  }  }
3281    
3282    =head3 Sims
3283    
3284    C<< my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters); >>
3285    
3286    Get a list of similarities for a specified feature. Similarity information is not kept in the
3287    Sprout database; rather, they are retrieved from a network server. The similarities are
3288    returned as B<Sim> objects. A Sim object is actually a list reference that has been blessed
3289    so that its elements can be accessed by name.
3290    
3291    Similarities can be either raw or expanded. The raw similarities are basic
3292    hits between features with similar DNA. Expanding a raw similarity drags in any
3293    features considered substantially identical. So, for example, if features B<A1>,
3294    B<A2>, and B<A3> are all substatially identical to B<A>, then a raw similarity
3295    B<[C,A]> would be expanded to B<[C,A] [C,A1] [C,A2] [C,A3]>.
3296    
3297    =over 4
3298    
3299    =item fid
3300    
3301    ID of the feature whose similarities are desired.
3302    
3303    =item maxN
3304    
3305    Maximum number of similarities to return.
3306    
3307    =item maxP
3308    
3309    Minumum allowable similarity score.
3310    
3311    =item select
3312    
3313    Selection criterion: C<raw> means only raw similarities are returned; C<fig>
3314    means only similarities to FIG features are returned; C<all> means all expanded
3315    similarities are returned; and C<figx> means similarities are expanded until the
3316    number of FIG features equals the maximum.
3317    
3318    =item max_expand
3319    
3320    The maximum number of features to expand.
3321    
3322    =item filters
3323    
3324    Reference to a hash containing filter information, or a subroutine that can be
3325    used to filter the sims.
3326    
3327    =item RETURN
3328    
3329    Returns a reference to a list of similarity objects, or C<undef> if an error
3330    occurred.
3331    
3332    =back
3333    
3334    =cut
3335    
3336    sub Sims {
3337        # Get the parameters.
3338        my ($self, $fid, $maxN, $maxP, $select, $max_expand, $filters) = @_;
3339        # Create the shim object to test for deleted FIDs.
3340        my $shim = FidCheck->new($self);
3341        # Ask the network for sims.
3342        my $retVal = FIGRules::GetNetworkSims($shim, $fid, {}, $maxN, $maxP, $select, $max_expand, $filters);
3343        # Return the result.
3344        return $retVal;
3345    }
3346    
3347  =head3 GetGroups  =head3 GetGroups
3348    
3349    C<< my %groups = $sprout->GetGroups(\@groupList); >>
3350    
3351    Return a hash mapping each group to the IDs of the genomes in the group.
3352    A list of groups may be specified, in which case only those groups will be
3353    shown. Alternatively, if no parameter is supplied, all groups will be
3354    included. Genomes that are not in any group are omitted.
3355    
3356  =cut  =cut
3357  #: Return Type %@;  #: Return Type %@;
3358  sub GetGroups {  sub GetGroups {
3359        # Get the parameters.
3360        my ($self, $groupList) = @_;
3361        # Declare the return value.
3362        my %retVal = ();
3363        # Determine whether we are getting all the groups or just some.
3364        if (defined $groupList) {
3365            # Here we have a group list. Loop through them individually,
3366            # getting a list of the relevant genomes.
3367            for my $group (@{$groupList}) {
3368                my @genomeIDs = $self->GetFlat(['Genome'], "Genome(primary-group) = ?",
3369                    [$group], "Genome(id)");
3370                $retVal{$group} = \@genomeIDs;
3371            }
3372        } else {
3373            # Here we need all of the groups. In this case, we run through all
3374            # of the genome records, putting each one found into the appropriate
3375            # group. Note that we use a filter clause to insure that only genomes
3376            # in real NMPDR groups are included in the return set.
3377            my @genomes = $self->GetAll(['Genome'], "Genome(primary-group) <> ?",
3378                                        [$FIG_Config::otherGroup], ['Genome(id)', 'Genome(primary-group)']);
3379            # Loop through the genomes found.
3380            for my $genome (@genomes) {
3381                # Pop this genome's ID off the current list.
3382                my @groups = @{$genome};
3383                my $genomeID = shift @groups;
3384                # Loop through the groups, adding the genome ID to each group's
3385                # list.
3386                for my $group (@groups) {
3387                    Tracer::AddToListMap(\%retVal, $group, $genomeID);
3388                }
3389            }
3390        }
3391        # Return the hash we just built.
3392        return %retVal;
3393    }
3394    
3395    =head3 MyGenomes
3396    
3397    C<< my @genomes = Sprout::MyGenomes($dataDir); >>
3398    
3399    Return a list of the genomes to be included in the Sprout.
3400    
3401    This method is provided for use during the Sprout load. It presumes the Genome load file has
3402    already been created. (It will be in the Sprout data directory and called either C<Genome>
3403    or C<Genome.dtx>.) Essentially, it reads in the Genome load file and strips out the genome
3404    IDs.
3405    
3406    =over 4
3407    
3408    =item dataDir
3409    
3410    Directory containing the Sprout load files.
3411    
3412    =back
3413    
3414    =cut
3415    #: Return Type @;
3416    sub MyGenomes {
3417        # Get the parameters.
3418        my ($dataDir) = @_;
3419        # Compute the genome file name.
3420        my $genomeFileName = LoadFileName($dataDir, "Genome");
3421        # Extract the genome IDs from the files.
3422        my @retVal = map { $_ =~ /^(\S+)/; $1 } Tracer::GetFile($genomeFileName);
3423        # Return the result.
3424        return @retVal;
3425    }
3426    
3427    =head3 LoadFileName
3428    
3429    C<< my $fileName = Sprout::LoadFileName($dataDir, $tableName); >>
3430    
3431    Return the name of the load file for the specified table in the specified data
3432    directory.
3433    
3434    =over 4
3435    
3436    =item dataDir
3437    
3438    Directory containing the Sprout load files.
3439    
3440    =item tableName
3441    
3442    Name of the table whose load file is desired.
3443    
3444    =item RETURN
3445    
3446    Returns the name of the file containing the load data for the specified table, or
3447    C<undef> if no load file is present.
3448    
3449    =back
3450    
3451    =cut
3452    #: Return Type $;
3453    sub LoadFileName {
3454        # Get the parameters.
3455        my ($dataDir, $tableName) = @_;
3456        # Declare the return variable.
3457        my $retVal;
3458        # Check for the various file names.
3459        if (-e "$dataDir/$tableName") {
3460            $retVal = "$dataDir/$tableName";
3461        } elsif (-e "$dataDir/$tableName.dtx") {
3462            $retVal = "$dataDir/$tableName.dtx";
3463        }
3464        # Return the result.
3465        return $retVal;
3466    }
3467    
3468    =head3 DeleteGenome
3469    
3470    C<< my $stats = $sprout->DeleteGenome($genomeID, $testFlag); >>
3471    
3472    Delete a genome from the database.
3473    
3474    =over 4
3475    
3476    =item genomeID
3477    
3478    ID of the genome to delete
3479    
3480    =item testFlag
3481    
3482    If TRUE, then the DELETE statements will be traced, but no deletions will occur.
3483    
3484    =item RETURN
3485    
3486    Returns a statistics object describing the rows deleted.
3487    
3488    =back
3489    
3490    =cut
3491    #: Return Type $%;
3492    sub DeleteGenome {
3493        # Get the parameters.
3494        my ($self, $genomeID, $testFlag) = @_;
3495        # Perform the delete for the genome's features.
3496        my $retVal = $self->Delete('Feature', "fig|$genomeID.%", $testFlag);
3497        # Perform the delete for the primary genome data.
3498        my $stats = $self->Delete('Genome', $genomeID, $testFlag);
3499        $retVal->Accumulate($stats);
3500        # Return the result.
3501        return $retVal;
3502    }
3503    
3504    =head3 Fix
3505    
3506    C<< my %fixedHash = Sprout::Fix(%groupHash); >>
3507    
3508    Prepare a genome group hash (like that returned by L</GetGroups> for processing.
3509    Groups with the same primary name will be combined. The primary name is the
3510    first capitalized word in the group name.
3511    
3512    =over 4
3513    
3514    =item groupHash
3515    
3516    Hash to be fixed up.
3517    
3518    =item RETURN
3519    
3520    Returns a fixed-up version of the hash.
3521    
3522    =back
3523    
3524    =cut
3525    
3526    sub Fix {
3527        # Get the parameters.
3528        my (%groupHash) = @_;
3529        # Create the result hash.
3530        my %retVal = ();
3531        # Copy over the genomes.
3532        for my $groupID (keys %groupHash) {
3533            # Make a safety copy of the group ID.
3534            my $realGroupID = $groupID;
3535            # Yank the primary name.
3536            if ($groupID =~ /([A-Z]\w+)/) {
3537                $realGroupID = $1;
3538            }
3539            # Append this group's genomes into the result hash.
3540            Tracer::AddToListMap(\%retVal, $realGroupID, @{$groupHash{$groupID}});
3541        }
3542        # Return the result hash.
3543        return %retVal;
3544    }
3545    
3546    =head3 GroupPageName
3547    
3548    C<< my $name = $sprout->GroupPageName($group); >>
3549    
3550    Return the name of the page for the specified NMPDR group.
3551    
3552    =over 4
3553    
3554    =item group
3555    
3556    Name of the relevant group.
3557    
3558    =item RETURN
3559    
3560    Returns the relative page name (e.g. C<../content/campy.php>). If the group file is not in
3561    memory it will be read in.
3562    
3563    =back
3564    
3565    =cut
3566    
3567    sub GroupPageName {
3568        # Get the parameters.
3569        my ($self, $group) = @_;
3570        # Declare the return variable.
3571        my $retVal;
3572        # Check for the group file data.
3573        if (! defined $self->{groupHash}) {
3574            # Read the group file.
3575            my %groupData = Sprout::ReadGroupFile($self->{_options}->{dataDir} . "/groups.tbl");
3576            # Store it in our object.
3577            $self->{groupHash} = \%groupData;
3578        }
3579        # Compute the real group name.
3580        my $realGroup = $group;
3581        if ($group =~ /([A-Z]\w+)/) {
3582            $realGroup = $1;
3583        }
3584        # Return the page name.
3585        $retVal = "../content/" . $self->{groupHash}->{$realGroup}->[1];
3586        # Return the result.
3587        return $retVal;
3588    }
3589    
3590    =head3 ReadGroupFile
3591    
3592    C<< my %groupData = Sprout::ReadGroupFile($groupFileName); >>
3593    
3594    Read in the data from the specified group file. The group file contains information
3595    about each of the NMPDR groups.
3596    
3597    =over 4
3598    
3599    =item name
3600    
3601    Name of the group.
3602    
3603    =item page
3604    
3605    Name of the group's page on the web site (e.g. C<campy.php> for
3606    Campylobacter)
3607    
3608    =item genus
3609    
3610    Genus of the group
3611    
3612    =item species
3613    
3614    Species of the group, or an empty string if the group is for an entire
3615    genus. If the group contains more than one species, the species names
3616    should be separated by commas.
3617    
3618    =back
3619    
3620    The parameters to this method are as follows
3621    
3622    =over 4
3623    
3624    =item groupFile
3625    
3626    Name of the file containing the group data.
3627    
3628    =item RETURN
3629    
3630    Returns a hash keyed on group name. The value of each hash
3631    
3632    =back
3633    
3634    =cut
3635    
3636    sub ReadGroupFile {
3637        # Get the parameters.
3638        my ($groupFileName) = @_;
3639        # Declare the return variable.
3640        my %retVal;
3641        # Read the group file.
3642        my @groupLines = Tracer::GetFile($groupFileName);
3643        for my $groupLine (@groupLines) {
3644            my ($name, $page, $genus, $species) = split(/\t/, $groupLine);
3645            $retVal{$name} = [$page, $genus, $species];
3646        }
3647        # Return the result.
3648        return %retVal;
3649  }  }
3650    
3651  =head2 Internal Utility Methods  =head2 Internal Utility Methods
# Line 2561  Line 3653 
3653  =head3 ParseAssignment  =head3 ParseAssignment
3654    
3655  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,
3656  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
3657  will be returned.  isn't, an empty list will be returned.
3658    
3659    A functional assignment is always of the form
3660    
3661        C<set >I<YYYY>C< function to\n>I<ZZZZZ>
3662    
3663    where I<YYYY> is the B<user>, and I<ZZZZ> is the actual functional role. In most cases,
3664    the user and the assigning user (from MadeAnnotation) will be the same, but that is
3665    not always the case.
3666    
3667    In addition, the functional role may contain extra data that is stripped, such as
3668    terminating spaces or a comment separated from the rest of the text by a tab.
3669    
3670  This is a static method.  This is a static method.
3671    
3672  =over 4  =over 4
3673    
3674    =item user
3675    
3676    Name of the assigning user.
3677    
3678  =item text  =item text
3679    
3680  Text of the annotation.  Text of the annotation.
# Line 2581  Line 3688 
3688    
3689  =cut  =cut
3690    
3691  sub ParseAssignment {  sub _ParseAssignment {
3692          # Get the parameters.          # Get the parameters.
3693          my ($text) = @_;      my ($user, $text) = @_;
3694          # Declare the return value.          # Declare the return value.
3695          my @retVal = ();          my @retVal = ();
3696          # Check to see if this is a functional assignment.          # Check to see if this is a functional assignment.
3697          my ($user, $type, $function) = split(/\n/, $text);      my ($type, $function) = split(/\n/, $text);
3698          if ($type =~ m/^set $user function to$/i) {      if ($type =~ m/^set function to$/i) {
3699                  # Here it is, so we return the user name and function text.          # Here we have an assignment without a user, so we use the incoming user ID.
3700                  @retVal = ($user, $function);                  @retVal = ($user, $function);
3701        } elsif ($type =~ m/^set (\S+) function to$/i) {
3702            # Here we have an assignment with a user that is passed back to the caller.
3703            @retVal = ($1, $function);
3704        }
3705        # If we have an assignment, we need to clean the function text. There may be
3706        # extra junk at the end added as a note from the user.
3707        if (defined( $retVal[1] )) {
3708            $retVal[1] =~ s/(\t\S)?\s*$//;
3709          }          }
3710          # Return the result list.          # Return the result list.
3711          return @retVal;          return @retVal;
# Line 2618  Line 3733 
3733    
3734  sub FriendlyTimestamp {  sub FriendlyTimestamp {
3735      my ($timeValue) = @_;      my ($timeValue) = @_;
3736      my $retVal = strftime("%a %b %e %H:%M:%S %Y", localtime($timeValue));      my $retVal = localtime($timeValue);
3737      return $retVal;      return $retVal;
3738  }  }
3739    
3740    =head3 AddProperty
3741    
3742    C<< my  = $sprout->AddProperty($featureID, $key, $value, $url); >>
3743    
3744    Add a new attribute value (Property) to a feature. In the SEED system, attributes can
3745    be added to almost any object. In Sprout, they can only be added to features. In
3746    Sprout, attributes are implemented using I<properties>. A property represents a key/value
3747    pair. If the particular key/value pair coming in is not already in the database, a new
3748    B<Property> record is created to hold it.
3749    
3750    =over 4
3751    
3752    =item peg
3753    
3754    ID of the feature to which the attribute is to be replied.
3755    
3756    =item key
3757    
3758    Name of the attribute (key).
3759    
3760    =item value
3761    
3762    Value of the attribute.
3763    
3764    =item url
3765    
3766    URL or text citation from which the property was obtained.
3767    
3768    =back
3769    
3770    =cut
3771    #: Return Type ;
3772    sub AddProperty {
3773        # Get the parameters.
3774        my ($self, $featureID, $key, $value, $url) = @_;
3775        # Declare the variable to hold the desired property ID.
3776        my $propID;
3777        # Attempt to find a property record for this key/value pair.
3778        my @properties = $self->GetFlat(['Property'],
3779                                       "Property(property-name) = ? AND Property(property-value) = ?",
3780                                       [$key, $value], 'Property(id)');
3781        if (@properties) {
3782            # Here the property is already in the database. We save its ID.
3783            $propID = $properties[0];
3784            # Here the property value does not exist. We need to generate an ID. It will be set
3785            # to a number one greater than the maximum value in the database. This call to
3786            # GetAll will stop after one record.
3787            my @maxProperty = $self->GetAll(['Property'], "ORDER BY Property(id) DESC", [], ['Property(id)'],
3788                                            1);
3789            $propID = $maxProperty[0]->[0] + 1;
3790            # Insert the new property value.
3791            $self->Insert('Property', { 'property-name' => $key, 'property-value' => $value, id => $propID });
3792        }
3793        # Now we connect the incoming feature to the property.
3794        $self->Insert('HasProperty', { 'from-link' => $featureID, 'to-link' => $propID, evidence => $url });
3795    }
3796    
3797    
3798  1;  1;

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.86

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3