[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.10, Fri Feb 25 18:41:45 2005 UTC revision 1.93, Sun Oct 22 05:15:56 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 143  Line 169 
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, $objectNames, $filterClause, $parameterList) = @_;      my ($self, $rebuild) = @_;
206          # We differ from the ERDB Get method in that the parameter list is passed in as a list reference      # Load the tables from the data directory.
207          # rather than a list of parameters. The next step is to convert the parameters from a reference      my $retVal = $self->LoadTables($self->{_options}->{dataDir}, $rebuild);
208          # to a real list. We can only do this if the parameters have been specified.      # Return the statistics.
209          my @parameters;      return $retVal;
         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, $entityType, $ID) = @_;      my ($self, $truncateFlag, $tableList) = @_;
247          # Create a query.      # Declare the return value.
248          my $query = $self->Get([$entityType], "$entityType(id) = ?", [$ID]);      my $retVal = Stats->new();
249          # Get the first (and only) object.      # Get the data directory.
250          my $retVal = $query->Fetch();      my $optionTable = $self->{_options};
251          # Return the result.      my $dataDir = $optionTable->{dataDir};
252        # 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
   
 Entity type name.  
   
 =item ID  
279    
280  ID of the desired entity.  TRUE if only complete genomes are to be counted, FALSE if all genomes are to be
281    counted
 =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, $entityType, $ID, $fields) = @_;      my ($self, $complete) = @_;
295          # Get the specified entity.      # Set the filter based on the completeness flag.
296          my $entity = $self->GetEntity($entityType, $ID);      my $filter = ($complete ? "Genome(complete) = 1" : "");
297          # Declare the return list.      # Get all the genomes and the related taxonomy information.
298          my @retVal = ();      my @genomes = $self->GetAll(['Genome'], $filter, [], ['Genome(id)', 'Genome(taxonomy)']);
299          # If we found the entity, push the values into the return list.      # Clear the counters.
300          if ($entity) {      my ($arch, $bact, $euk, $vir, $env, $unk) = (0, 0, 0, 0, 0, 0);
301                  push @retVal, $entity->Values($fields);      # Loop through, counting the domains.
302        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    =item RETURN
327    
328  Fully-qualified name to give to the output file.  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, $fileName) = @_;      my ($self, $genomeID) = @_;
337          # Compute the file name.      # Get the contig count.
338          my $options = $self->{_options};      my $retVal = $self->GetCount(['Contig', 'HasContig'], "HasContig(from-link) = ?", [$genomeID]);
339          # Call the show method on the underlying ERDB object.      # Return the result.
340          $self->{_erdb}->ShowMetaData($fileName);      return $retVal;
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, $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, $truncateFlag, $tableList) = @_;      my ($self, $attributes, $filterString, $params, $selected, $fast) = @_;
386          # Get the database object.      my $slowMode = ! $fast;
387          my $erdb = $self->{_erdb};      # Default to nothing selected. This prevents an execution warning if "$selected"
388          # Declare the return value.      # is undefined.
389          my $retVal = Stats->new();      $selected = "" unless defined $selected;
390          # Get the data directory.      Trace("Gene Menu called with slow mode \"$slowMode\" and selection \"$selected\".") if T(3);
391          my $optionTable = $self->{_options};      # Start the menu.
392          my $dataDir = $optionTable->{dataDir};      my $retVal = "<select " .
393          # Loop through the incoming table names.          join(" ", map { "$_=\"$attributes->{$_}\"" } keys %{$attributes}) .
394          for my $tableName (@{$tableList}) {          ">\n";
395                  # Find the table's file.      # Get the genomes.
396                  my $fileName = "$dataDir/$tableName";      my @genomes = $self->GetAll(['Genome'], $filterString, $params, ['Genome(id)',
397                  if (! -e $fileName) {                                                                       'Genome(genus)',
398                          $fileName = "$fileName.dtx";                                                                       'Genome(species)',
399                  }                                                                       'Genome(unique-characterization)']);
400                  # Attempt to load this table.      # Sort them by name.
401                  my $result = $erdb->LoadTable($fileName, $tableName, $truncateFlag);      my @sorted = sort { lc("$a->[1] $a->[2]") cmp lc("$b->[1] $b->[2]") } @genomes;
402                  # Accumulate the resulting statistics.      # Loop through the genomes, creating the option tags.
403                  $retVal->Accumulate($result);      for my $genomeData (@sorted) {
404            # 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 459  Line 435 
435          # Get the parameters.          # Get the parameters.
436          my ($self) = @_;          my ($self) = @_;
437          # Create the tables.          # Create the tables.
438          $self->{_erdb}->CreateTables;      $self->CreateTables();
439  }  }
440    
441  =head3 Genomes  =head3 Genomes
# Line 582  Line 558 
558  =item RETURN  =item RETURN
559    
560  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
561  context and as a space-delimited string in a scalar context.  context and as a comma-delimited string in a scalar context.
562    
563  =back  =back
564    
# Line 609  Line 585 
585                  if ($prevContig eq $contigID && $dir eq $prevDir) {                  if ($prevContig eq $contigID && $dir eq $prevDir) {
586                          # 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
587                          # new segment's beginning is next to the old segment's end.                          # new segment's beginning is next to the old segment's end.
588                          if (($dir eq "-" && $beg == $prevBeg - $prevLen) ||              if ($dir eq "-" && $beg + $len == $prevBeg) {
589                                  ($dir eq "+" && $beg == $prevBeg + $prevLen)) {                  # Here we're merging two backward blocks, so we keep the new begin point
590                                  # Here we need to merge two segments. Adjust the beginning and length values                  # and adjust the length.
591                                  # to include both segments.                  $len += $prevLen;
592                    # Pop the old segment off. The new one will replace it later.
593                    pop @retVal;
594                } elsif ($dir eq "+" && $beg == $prevBeg + $prevLen) {
595                    # Here we need to merge two forward blocks. Adjust the beginning and
596                    # length values to include both segments.
597                                  $beg = $prevBeg;                                  $beg = $prevBeg;
598                                  $len += $prevLen;                                  $len += $prevLen;
599                                  # 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 621  Line 602 
602                  }                  }
603                  # Remember this specifier for the adjacent-segment test the next time through.                  # Remember this specifier for the adjacent-segment test the next time through.
604                  ($prevContig, $prevBeg, $prevDir, $prevLen) = ($contigID, $beg, $dir, $len);                  ($prevContig, $prevBeg, $prevDir, $prevLen) = ($contigID, $beg, $dir, $len);
605            # Compute the initial base pair.
606            my $start = ($dir eq "+" ? $beg : $beg + $len - 1);
607                  # Add the specifier to the list.                  # Add the specifier to the list.
608                  push @retVal, "${contigID}_$beg$dir$len";          push @retVal, "${contigID}_$start$dir$len";
609          }          }
610          # Return the list in the format indicated by the context.          # Return the list in the format indicated by the context.
611          return (wantarray ? @retVal : join(' ', @retVal));      return (wantarray ? @retVal : join(',', @retVal));
612  }  }
613    
614  =head3 ParseLocation  =head3 ParseLocation
# Line 656  Line 639 
639      shift if UNIVERSAL::isa($_[0],__PACKAGE__);      shift if UNIVERSAL::isa($_[0],__PACKAGE__);
640          my ($location) = @_;          my ($location) = @_;
641          # Parse it into segments.          # Parse it into segments.
642          $location =~ /^(.*)_(\d*)([+-_])(\d*)$/;      $location =~ /^(.+)_(\d+)([+\-_])(\d+)$/;
643          my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);          my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);
644          # If the direction is an underscore, convert it to a + or -.          # If the direction is an underscore, convert it to a + or -.
645          if ($dir eq "_") {          if ($dir eq "_") {
# Line 672  Line 655 
655          return ($contigID, $start, $dir, $len);          return ($contigID, $start, $dir, $len);
656  }  }
657    
658    
659    
660  =head3 PointLocation  =head3 PointLocation
661    
662  C<< my $found = Sprout::PointLocation($location, $point); >>  C<< my $found = Sprout::PointLocation($location, $point); >>
# Line 732  Line 717 
717  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,
718  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>.
719    
720    For example, the following would return the DNA sequence for contig C<83333.1:NC_000913>
721    between positions 1401 and 1532, inclusive.
722    
723        my $sequence = $sprout->DNASeq('83333.1:NC_000913_1401_1532');
724    
725  =over 4  =over 4
726    
727  =item locationList  =item locationList
728    
729  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
730  L</FeatureLocation> for more about this format).  I<contigID>C<_>I<begin>C<_>I<end> (see L</FeatureLocation> for more about this format).
731    
732  =item RETURN  =item RETURN
733    
# Line 764  Line 754 
754                  # 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
755                  # before putting it in the return value.                  # before putting it in the return value.
756                  my ($start, $stop);                  my ($start, $stop);
757            Trace("Parse of \"$location\" is $beg$dir$len.") if T(SDNA => 4);
758                  if ($dir eq "+") {                  if ($dir eq "+") {
759                          $start = $beg;                          $start = $beg;
760                          $stop = $beg + $len - 1;                          $stop = $beg + $len - 1;
761                  } else {                  } else {
762                          $start = $beg + $len + 1;              $start = $beg - $len + 1;
763                          $stop = $beg;                          $stop = $beg;
764                  }                  }
765            Trace("Looking for sequences containing $start through $stop.") if T(SDNA => 4);
766                  my $query = $self->Get(['IsMadeUpOf','Sequence'],                  my $query = $self->Get(['IsMadeUpOf','Sequence'],
767                          "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .                          "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .
768                          " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",                          " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",
# Line 782  Line 774 
774                                  $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',                                  $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',
775                                                                     'IsMadeUpOf(len)']);                                                                     'IsMadeUpOf(len)']);
776                          my $stopPosition = $startPosition + $sequenceLength;                          my $stopPosition = $startPosition + $sequenceLength;
777                Trace("Sequence is from $startPosition to $stopPosition.") if T(SDNA => 4);
778                          # Figure out the start point and length of the relevant section.                          # Figure out the start point and length of the relevant section.
779                          my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);                          my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);
780                          my $len = ($stopPosition <= $stop ? $stopPosition : $stop) - $startPosition - $pos1;              my $len1 = ($stopPosition < $stop ? $stopPosition : $stop) + 1 - $startPosition - $pos1;
781                Trace("Position is $pos1 for length $len1.") if T(SDNA => 4);
782                          # Add the relevant data to the location data.                          # Add the relevant data to the location data.
783                          $locationDNA .= substr($sequenceData, $pos1, $len);              $locationDNA .= substr($sequenceData, $pos1, $len1);
784                  }                  }
785                  # 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.
786                  if ($dir eq '+') {                  if ($dir eq '+') {
787                          $retVal .= $locationDNA;                          $retVal .= $locationDNA;
788                  } else {                  } else {
789                          $locationDNA = join('', reverse split //, $locationDNA);              $retVal .= FIG::reverse_comp($locationDNA);
                         $retVal .= $locationDNA;  
790                  }                  }
791          }          }
792          # Return the result.          # Return the result.
# Line 830  Line 823 
823          return @retVal;          return @retVal;
824  }  }
825    
826    =head3 GenomeLength
827    
828    C<< my $length = $sprout->GenomeLength($genomeID); >>
829    
830    Return the length of the specified genome in base pairs.
831    
832    =over 4
833    
834    =item genomeID
835    
836    ID of the genome whose base pair count is desired.
837    
838    =item RETURN
839    
840    Returns the number of base pairs in all the contigs of the specified
841    genome.
842    
843    =back
844    
845    =cut
846    
847    sub GenomeLength {
848        # Get the parameters.
849        my ($self, $genomeID) = @_;
850        # Declare the return variable.
851        my $retVal = 0;
852        # Get the genome's contig sequence lengths.
853        my @lens = $self->GetFlat(['HasContig', 'IsMadeUpOf'], 'HasContig(from-link) = ?',
854                           [$genomeID], 'IsMadeUpOf(len)');
855        # Sum the lengths.
856        map { $retVal += $_ } @lens;
857        # Return the result.
858        return $retVal;
859    }
860    
861    =head3 FeatureCount
862    
863    C<< my $count = $sprout->FeatureCount($genomeID, $type); >>
864    
865    Return the number of features of the specified type in the specified genome.
866    
867    =over 4
868    
869    =item genomeID
870    
871    ID of the genome whose feature count is desired.
872    
873    =item type
874    
875    Type of feature to count (eg. C<peg>, C<rna>, etc.).
876    
877    =item RETURN
878    
879    Returns the number of features of the specified type for the specified genome.
880    
881    =back
882    
883    =cut
884    
885    sub FeatureCount {
886        # Get the parameters.
887        my ($self, $genomeID, $type) = @_;
888        # Compute the count.
889        my $retVal = $self->GetCount(['HasFeature', 'Feature'],
890                                    "HasFeature(from-link) = ? AND Feature(feature-type) = ?",
891                                    [$genomeID, $type]);
892        # Return the result.
893        return $retVal;
894    }
895    
896    =head3 GenomeAssignments
897    
898    C<< my $fidHash = $sprout->GenomeAssignments($genomeID); >>
899    
900    Return a list of a genome's assigned features. The return hash will contain each
901    assigned feature of the genome mapped to the text of its most recent functional
902    assignment.
903    
904    =over 4
905    
906    =item genomeID
907    
908    ID of the genome whose functional assignments are desired.
909    
910    =item RETURN
911    
912    Returns a reference to a hash which maps each feature to its most recent
913    functional assignment.
914    
915    =back
916    
917    =cut
918    
919    sub GenomeAssignments {
920        # Get the parameters.
921        my ($self, $genomeID) = @_;
922        # Declare the return variable.
923        my $retVal = {};
924        # Query the genome's features.
925        my $query = $self->Get(['HasFeature', 'Feature'], "HasFeature(from-link) = ?",
926                               [$genomeID]);
927        # Loop through the features.
928        while (my $data = $query->Fetch) {
929            # Get the feature ID and assignment.
930            my ($fid, $assignment) = $data->Values(['Feature(id)', 'Feature(assignment)']);
931            if ($assignment) {
932                $retVal->{$fid} = $assignment;
933            }
934        }
935        # Return the result.
936        return $retVal;
937    }
938    
939  =head3 ContigLength  =head3 ContigLength
940    
941  C<< my $length = $sprout->ContigLength($contigID); >>  C<< my $length = $sprout->ContigLength($contigID); >>
# Line 863  Line 969 
969          # Set it from the sequence data, if any.          # Set it from the sequence data, if any.
970          if ($sequence) {          if ($sequence) {
971                  my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);                  my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);
972                  $retVal = $start + $len;          $retVal = $start + $len - 1;
973          }          }
974          # Return the result.          # Return the result.
975          return $retVal;          return $retVal;
976  }  }
977    
978  =head3 GenesInRegion  =head3 ClusterPEGs
979    
980  C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>  C<< my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs); >>
981    
982  List the features which overlap a specified region in a contig.  Cluster the PEGs in a list according to the cluster coding scheme of the specified
983    subsystem. In order for this to work properly, the subsystem object must have
984    been used recently to retrieve the PEGs using the B<get_pegs_from_cell> method.
985    This causes the cluster numbers to be pulled into the subsystem's color hash.
986    If a PEG is not found in the color hash, it will not appear in the output
987    sequence.
988    
989  =over 4  =over 4
990    
991  =item contigID  =item sub
   
 ID of the contig containing the region of interest.  
   
 =item start  
992    
993  Offset of the first residue in the region of interest.  Sprout subsystem object for the relevant subsystem, from the L</get_subsystem>
994    method.
995    
996  =item stop  =item pegs
997    
998  Offset of the last residue in the region of interest.  Reference to the list of PEGs to be clustered.
999    
1000    =item RETURN
1001    
1002    Returns a list of the PEGs, grouped into smaller lists by cluster number.
1003    
1004    =back
1005    
1006    =cut
1007    #: Return Type $@@;
1008    sub ClusterPEGs {
1009        # Get the parameters.
1010        my ($self, $sub, $pegs) = @_;
1011        # Declare the return variable.
1012        my $retVal = [];
1013        # Loop through the PEGs, creating arrays for each cluster.
1014        for my $pegID (@{$pegs}) {
1015            my $clusterNumber = $sub->get_cluster_number($pegID);
1016            # Only proceed if the PEG is in a cluster.
1017            if ($clusterNumber >= 0) {
1018                # Push this PEG onto the sub-list for the specified cluster number.
1019                push @{$retVal->[$clusterNumber]}, $pegID;
1020            }
1021        }
1022        # Return the result.
1023        return $retVal;
1024    }
1025    
1026    =head3 GenesInRegion
1027    
1028    C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>
1029    
1030    List the features which overlap a specified region in a contig.
1031    
1032    =over 4
1033    
1034    =item contigID
1035    
1036    ID of the contig containing the region of interest.
1037    
1038    =item start
1039    
1040    Offset of the first residue in the region of interest.
1041    
1042    =item stop
1043    
1044    Offset of the last residue in the region of interest.
1045    
1046  =item RETURN  =item RETURN
1047    
# Line 1013  Line 1167 
1167    
1168  =head3 FeatureAnnotations  =head3 FeatureAnnotations
1169    
1170  C<< my @descriptors = $sprout->FeatureAnnotations($featureID); >>  C<< my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag); >>
1171    
1172  Return the annotations of a feature.  Return the annotations of a feature.
1173    
# Line 1023  Line 1177 
1177    
1178  ID of the feature whose annotations are desired.  ID of the feature whose annotations are desired.
1179    
1180    =item rawFlag
1181    
1182    If TRUE, the annotation timestamps will be returned in raw form; otherwise, they
1183    will be returned in human-readable form.
1184    
1185  =item RETURN  =item RETURN
1186    
1187  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.
1188    
1189  * B<featureID> ID of the relevant feature.  * B<featureID> ID of the relevant feature.
1190    
1191  * B<timeStamp> time the annotation was made, in user-friendly format.  * B<timeStamp> time the annotation was made.
1192    
1193  * B<user> ID of the user who made the annotation  * B<user> ID of the user who made the annotation
1194    
# Line 1041  Line 1200 
1200  #: Return Type @%;  #: Return Type @%;
1201  sub FeatureAnnotations {  sub FeatureAnnotations {
1202          # Get the parameters.          # Get the parameters.
1203          my ($self, $featureID) = @_;      my ($self, $featureID, $rawFlag) = @_;
1204          # 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.
1205          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1206                                                     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);                                                     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);
# Line 1054  Line 1213 
1213                          $annotation->Values(['IsTargetOfAnnotation(from-link)',                          $annotation->Values(['IsTargetOfAnnotation(from-link)',
1214                                                                   'Annotation(time)', 'MadeAnnotation(from-link)',                                                                   'Annotation(time)', 'MadeAnnotation(from-link)',
1215                                                                   'Annotation(annotation)']);                                                                   'Annotation(annotation)']);
1216            # Convert the time, if necessary.
1217            if (! $rawFlag) {
1218                $timeStamp = FriendlyTimestamp($timeStamp);
1219            }
1220                  # Assemble them into a hash.                  # Assemble them into a hash.
1221          my $annotationHash = { featureID => $featureID,          my $annotationHash = { featureID => $featureID,
1222                                 timeStamp => FriendlyTimestamp($timeStamp),                                 timeStamp => $timeStamp,
1223                                                             user => $user, text => $text };                                                             user => $user, text => $text };
1224                  # Add it to the return list.                  # Add it to the return list.
1225                  push @retVal, $annotationHash;                  push @retVal, $annotationHash;
# Line 1070  Line 1233 
1233  C<< my %functions = $sprout->AllFunctionsOf($featureID); >>  C<< my %functions = $sprout->AllFunctionsOf($featureID); >>
1234    
1235  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
1236  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,
1237  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
1238  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,
1239  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.
1240  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
1241  functional assignments, we will only keep the most recent one.  recent one.
1242    
1243  =over 4  =over 4
1244    
# Line 1085  Line 1248 
1248    
1249  =item RETURN  =item RETURN
1250    
1251  Returns a hash mapping the functional assignment IDs to user IDs.  Returns a hash mapping the user IDs to functional assignment IDs.
1252    
1253  =back  =back
1254    
# Line 1095  Line 1258 
1258          # Get the parameters.          # Get the parameters.
1259          my ($self, $featureID) = @_;          my ($self, $featureID) = @_;
1260          # Get all of the feature's annotations.          # Get all of the feature's annotations.
1261      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation'],      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1262                                                      "IsTargetOfAnnotation(from-link) = ?",                                                      "IsTargetOfAnnotation(from-link) = ?",
1263                              [$featureID], ['Annotation(time)', 'Annotation(annotation)']);                              [$featureID], ['Annotation(time)', 'Annotation(annotation)',
1264                                               'MadeAnnotation(from-link)']);
1265          # Declare the return hash.          # Declare the return hash.
1266          my %retVal;          my %retVal;
     # Declare a hash for insuring we only make one assignment per user.  
     my %timeHash = ();  
1267      # Now we sort the assignments by timestamp in reverse.      # Now we sort the assignments by timestamp in reverse.
1268      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;
1269          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1270      for my $annotation (@sortedQuery) {      for my $annotation (@sortedQuery) {
1271          # Get the annotation fields.          # Get the annotation fields.
1272          my ($timeStamp, $text) = @{$annotation};          my ($timeStamp, $text, $user) = @{$annotation};
1273                  # Check to see if this is a functional assignment.                  # Check to see if this is a functional assignment.
1274                  my ($user, $function) = ParseAssignment($text);          my ($actualUser, $function) = _ParseAssignment($user, $text);
1275          if ($user && ! exists $timeHash{$user}) {          if ($actualUser && ! exists $retVal{$actualUser}) {
1276              # Here it is a functional assignment and there has been no              # Here it is a functional assignment and there has been no
1277              # previous assignment for this user, so we stuff it in the              # previous assignment for this user, so we stuff it in the
1278              # return hash.              # return hash.
1279                          $retVal{$function} = $user;              $retVal{$actualUser} = $function;
             # Insure we don't assign to this user again.  
             $timeHash{$user} = 1;  
1280                  }                  }
1281          }          }
1282          # Return the hash of assignments found.          # Return the hash of assignments found.
# Line 1130  Line 1290 
1290  Return the most recently-determined functional assignment of a particular feature.  Return the most recently-determined functional assignment of a particular feature.
1291    
1292  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
1293  the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional  the feature is identified by a FIG ID (begins with the string C<fig|>), then the functional
1294  assignment is a type of annotation. It has the format "XXXX\nset XXXX function to\nYYYYY". In this  assignment is taken from the B<Feature> or C<Annotation> table, depending.
 instance, XXXX is the user ID and YYYYY is the functional assignment text. Its worth noting that  
 we cannot filter on the content of the annotation itself because it's a text field; however, this  
 is not a big problem because most features only have a small number of annotations.  
1295    
1296  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
1297  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 1153  Line 1310 
1310    
1311  =item userID (optional)  =item userID (optional)
1312    
1313  ID of the user whose function determination is desired. If omitted, only the latest  ID of the user whose function determination is desired. If omitted, the primary
1314  C<FIG> assignment will be returned.  functional assignment in the B<Feature> table will be returned.
1315    
1316  =item RETURN  =item RETURN
1317    
# Line 1171  Line 1328 
1328      my $retVal;      my $retVal;
1329      # Determine the ID type.      # Determine the ID type.
1330      if ($featureID =~ m/^fig\|/) {      if ($featureID =~ m/^fig\|/) {
1331          # Here we have a FIG feature ID. We must build the list of trusted          # Here we have a FIG feature ID.
1332          # users.          if (!$userID) {
1333                # Use the primary assignment.
1334                ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(assignment)']);
1335            } else {
1336                # We must build the list of trusted users.
1337          my %trusteeTable = ();          my %trusteeTable = ();
1338          # Check the user ID.          # Check the user ID.
1339          if (!$userID) {          if (!$userID) {
# Line 1194  Line 1355 
1355              }              }
1356          }          }
1357          # 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.
1358          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation'],              my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1359                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1360                                 [$featureID]);                                 [$featureID]);
1361          my $timeSelected = 0;          my $timeSelected = 0;
1362          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1363          while (my $annotation = $query->Fetch()) {          while (my $annotation = $query->Fetch()) {
1364              # Get the annotation text.              # Get the annotation text.
1365              my ($text, $time) = $annotation->Values(['Annotation(annotation)','Annotation(time)']);                  my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1366                                                             'Annotation(time)', 'MadeAnnotation(from-link)']);
1367              # 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.
1368              my ($user, $type, $function) = split(/\n/, $text);                  my ($actualUser, $function) = _ParseAssignment($user, $text);
1369              if ($type =~ m/^set $user function to$/i) {                  Trace("Assignment user is $actualUser, text is $function.") if T(4);
1370                    if ($actualUser) {
1371                  # Here it is a functional assignment. Check the time and the user                  # Here it is a functional assignment. Check the time and the user
1372                  # name. The time must be recent and the user must be trusted.                  # name. The time must be recent and the user must be trusted.
1373                  if ((exists $trusteeTable{$user}) && ($time > $timeSelected)) {                      if ((exists $trusteeTable{$actualUser}) && ($time > $timeSelected)) {
1374                      $retVal = $function;                      $retVal = $function;
1375                      $timeSelected = $time;                      $timeSelected = $time;
1376                  }                  }
1377              }              }
1378          }          }
1379            }
1380      } else {      } else {
1381          # Here we have a non-FIG feature ID. In this case the user ID does not          # Here we have a non-FIG feature ID. In this case the user ID does not
1382          # matter. We simply get the information from the External Alias Function          # matter. We simply get the information from the External Alias Function
# Line 1223  Line 1387 
1387          return $retVal;          return $retVal;
1388  }  }
1389    
1390    =head3 FunctionsOf
1391    
1392    C<< my @functionList = $sprout->FunctionOf($featureID, $userID); >>
1393    
1394    Return the functional assignments of a particular feature.
1395    
1396    The functional assignment is handled differently depending on the type of feature. If
1397    the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional
1398    assignment is a type of annotation. The format of an assignment is described in
1399    L</ParseAssignment>. Its worth noting that we cannot filter on the content of the
1400    annotation itself because it's a text field; however, this is not a big problem because
1401    most features only have a small number of annotations.
1402    
1403    If the feature is B<not> identified by a FIG ID, then the functional assignment
1404    information is taken from the B<ExternalAliasFunc> table. If the table does
1405    not contain an entry for the feature, an empty list is returned.
1406    
1407    =over 4
1408    
1409    =item featureID
1410    
1411    ID of the feature whose functional assignments are desired.
1412    
1413    =item RETURN
1414    
1415    Returns a list of 2-tuples, each consisting of a user ID and the text of an assignment by
1416    that user.
1417    
1418    =back
1419    
1420    =cut
1421    #: Return Type @@;
1422    sub FunctionsOf {
1423        # Get the parameters.
1424        my ($self, $featureID) = @_;
1425        # Declare the return value.
1426        my @retVal = ();
1427        # Determine the ID type.
1428        if ($featureID =~ m/^fig\|/) {
1429            # Here we have a FIG feature ID. We must build the list of trusted
1430            # users.
1431            my %trusteeTable = ();
1432            # Build a query for all of the feature's annotations, sorted by date.
1433            my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1434                                   "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1435                                   [$featureID]);
1436            my $timeSelected = 0;
1437            # Loop until we run out of annotations.
1438            while (my $annotation = $query->Fetch()) {
1439                # Get the annotation text.
1440                my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1441                                                                'Annotation(time)',
1442                                                                'MadeAnnotation(user)']);
1443                # Check to see if this is a functional assignment for a trusted user.
1444                my ($actualUser, $function) = _ParseAssignment($user, $text);
1445                if ($actualUser) {
1446                    # Here it is a functional assignment.
1447                    push @retVal, [$actualUser, $function];
1448                }
1449            }
1450        } else {
1451            # Here we have a non-FIG feature ID. In this case the user ID does not
1452            # matter. We simply get the information from the External Alias Function
1453            # table.
1454            my @assignments = $self->GetEntityValues('ExternalAliasFunc', $featureID,
1455                                                     ['ExternalAliasFunc(func)']);
1456            push @retVal, map { ['master', $_] } @assignments;
1457        }
1458        # Return the assignments found.
1459        return @retVal;
1460    }
1461    
1462  =head3 BBHList  =head3 BBHList
1463    
1464  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>
# Line 1242  Line 1478 
1478    
1479  =item RETURN  =item RETURN
1480    
1481  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
1482  their best hits.  on the target genome.
1483    
1484  =back  =back
1485    
# Line 1256  Line 1492 
1492          my %retVal = ();          my %retVal = ();
1493          # Loop through the incoming features.          # Loop through the incoming features.
1494          for my $featureID (@{$featureList}) {          for my $featureID (@{$featureList}) {
1495                  # Create a query to get the feature's best hit.          # Ask the server for the feature's best hit.
1496                  my $query = $self->Get(['IsBidirectionalBestHitOf'],          my @bbhData = FIGRules::BBHData($featureID);
1497                                                             "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?",          # Peel off the BBHs found.
1498                                                             [$featureID, $genomeID]);          my @found = ();
1499                  # Look for the best hit.          for my $bbh (@bbhData) {
1500                  my $bbh = $query->Fetch;              push @found, $bbh->[0];
                 if ($bbh) {  
                         my ($targetFeature) = $bbh->Value('IsBidirectionalBestHitOf(to-link)');  
                         $retVal{$featureID} = $targetFeature;  
1501                  }                  }
1502            $retVal{$featureID} = \@found;
1503          }          }
1504          # Return the mapping.          # Return the mapping.
1505          return \%retVal;          return \%retVal;
1506  }  }
1507    
1508    =head3 SimList
1509    
1510    C<< my %similarities = $sprout->SimList($featureID, $count); >>
1511    
1512    Return a list of the similarities to the specified feature.
1513    
1514    This method just returns the bidirectional best hits for performance reasons.
1515    
1516    =over 4
1517    
1518    =item featureID
1519    
1520    ID of the feature whose similarities are desired.
1521    
1522    =item count
1523    
1524    Maximum number of similar features to be returned, or C<0> to return them all.
1525    
1526    =back
1527    
1528    =cut
1529    #: Return Type %;
1530    sub SimList {
1531        # Get the parameters.
1532        my ($self, $featureID, $count) = @_;
1533        # Ask for the best hits.
1534        my @lists = FIGRules::BBHData($featureID);
1535        # Create the return value.
1536        my %retVal = ();
1537        for my $tuple (@lists) {
1538            $retVal{$tuple->[0]} = $tuple->[1];
1539        }
1540        # Return the result.
1541        return %retVal;
1542    }
1543    
1544    =head3 IsComplete
1545    
1546    C<< my $flag = $sprout->IsComplete($genomeID); >>
1547    
1548    Return TRUE if the specified genome is complete, else FALSE.
1549    
1550    =over 4
1551    
1552    =item genomeID
1553    
1554    ID of the genome whose completeness status is desired.
1555    
1556    =item RETURN
1557    
1558    Returns TRUE if the genome is complete, FALSE if it is incomplete, and C<undef> if it is
1559    not found.
1560    
1561    =back
1562    
1563    =cut
1564    #: Return Type $;
1565    sub IsComplete {
1566        # Get the parameters.
1567        my ($self, $genomeID) = @_;
1568        # Declare the return variable.
1569        my $retVal;
1570        # Get the genome's data.
1571        my $genomeData = $self->GetEntity('Genome', $genomeID);
1572        if ($genomeData) {
1573            # The genome exists, so get the completeness flag.
1574            ($retVal) = $genomeData->Value('Genome(complete)');
1575        }
1576        # Return the result.
1577        return $retVal;
1578    }
1579    
1580  =head3 FeatureAliases  =head3 FeatureAliases
1581    
1582  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>
# Line 1305  Line 1611 
1611    
1612  C<< my $genomeID = $sprout->GenomeOf($featureID); >>  C<< my $genomeID = $sprout->GenomeOf($featureID); >>
1613    
1614  Return the genome that contains a specified feature.  Return the genome that contains a specified feature or contig.
1615    
1616  =over 4  =over 4
1617    
1618  =item featureID  =item featureID
1619    
1620  ID of the feature whose genome is desired.  ID of the feature or contig whose genome is desired.
1621    
1622  =item RETURN  =item RETURN
1623    
1624  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
1625  an undefined value.  found, returns an undefined value.
1626    
1627  =back  =back
1628    
# Line 1325  Line 1631 
1631  sub GenomeOf {  sub GenomeOf {
1632          # Get the parameters.          # Get the parameters.
1633          my ($self, $featureID) = @_;          my ($self, $featureID) = @_;
1634          # Create a query to find the genome associated with the feature.      # Create a query to find the genome associated with the incoming ID.
1635          my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ?", [$featureID]);      my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ? OR HasContig(to-link) = ?",
1636                               [$featureID, $featureID]);
1637          # Declare the return value.          # Declare the return value.
1638          my $retVal;          my $retVal;
1639          # Get the genome ID.          # Get the genome ID.
# Line 1361  Line 1668 
1668  sub CoupledFeatures {  sub CoupledFeatures {
1669          # Get the parameters.          # Get the parameters.
1670          my ($self, $featureID) = @_;          my ($self, $featureID) = @_;
1671          # Create a query to retrieve the functionally-coupled features. Note that we depend on the      Trace("Looking for features coupled to $featureID.") if T(coupling => 3);
1672          # fact that the functional coupling is physically paired. If (A,B) is in the database, then      # Create a query to retrieve the functionally-coupled features.
1673          # (B,A) will also be found.      my $query = $self->Get(['ParticipatesInCoupling', 'Coupling'],
1674          my $query = $self->Get(['IsClusteredOnChromosomeWith'],                             "ParticipatesInCoupling(from-link) = ?", [$featureID]);
                                                    "IsClusteredOnChromosomeWith(from-link) = ?", [$featureID]);  
1675          # 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.
1676          my $found = 0;          my $found = 0;
1677          # Create the return hash.          # Create the return hash.
1678          my %retVal = ();          my %retVal = ();
1679          # Retrieve the relationship records and store them in the hash.          # Retrieve the relationship records and store them in the hash.
1680          while (my $clustering = $query->Fetch()) {          while (my $clustering = $query->Fetch()) {
1681                  my ($otherFeatureID, $score) = $clustering->Values(['IsClusteredOnChromosomeWith(to-link)',          # Get the ID and score of the coupling.
1682                                                                      'IsClusteredOnChromosomeWith(score)']);          my ($couplingID, $score) = $clustering->Values(['Coupling(id)',
1683                                                            'Coupling(score)']);
1684            Trace("$featureID coupled with score $score to ID $couplingID.") if T(coupling => 4);
1685            # Get the other feature that participates in the coupling.
1686            my ($otherFeatureID) = $self->GetFlat(['ParticipatesInCoupling'],
1687                                               "ParticipatesInCoupling(to-link) = ? AND ParticipatesInCoupling(from-link) <> ?",
1688                                               [$couplingID, $featureID], 'ParticipatesInCoupling(from-link)');
1689            Trace("$couplingID target feature is $otherFeatureID.") if T(coupling => 4);
1690            # Attach the other feature's score to its ID.
1691                  $retVal{$otherFeatureID} = $score;                  $retVal{$otherFeatureID} = $score;
1692                  $found = 1;                  $found = 1;
1693          }          }
# Line 1386  Line 1700 
1700          return %retVal;          return %retVal;
1701  }  }
1702    
1703  =head3 GetEntityTypes  =head3 CouplingEvidence
1704    
1705    C<< my @evidence = $sprout->CouplingEvidence($peg1, $peg2); >>
1706    
1707  C<< my @entityList = $sprout->GetEntityTypes(); >>  Return the evidence for a functional coupling.
1708    
1709  Return the list of supported entity types.  A pair of features is considered evidence of a coupling between two other
1710    features if they occur close together on a contig and both are similar to
1711    the coupled features. So, if B<A1> and B<A2> are close together on a contig,
1712    B<B1> and B<B2> are considered evidence for the coupling if (1) B<B1> and
1713    B<B2> are close together, (2) B<B1> is similar to B<A1>, and (3) B<B2> is
1714    similar to B<A2>.
1715    
1716    The score of a coupling is determined by the number of pieces of evidence
1717    that are considered I<representative>. If several evidence items belong to
1718    a group of genomes that are close to each other, only one of those items
1719    is considered representative. The other evidence items are presumed to be
1720    there because of the relationship between the genomes rather than because
1721    the two proteins generated by the features have a related functionality.
1722    
1723    Each evidence item is returned as a three-tuple in the form C<[>I<$peg1a>C<,>
1724    I<$peg2a>C<,> I<$rep>C<]>, where I<$peg1a> is similar to I<$peg1>, I<$peg2a>
1725    is similar to I<$peg2>, and I<$rep> is TRUE if the evidence is representative
1726    and FALSE otherwise.
1727    
1728    =over 4
1729    
1730    =item peg1
1731    
1732    ID of the feature of interest.
1733    
1734    =item peg2
1735    
1736    ID of a feature functionally coupled to the feature of interest.
1737    
1738    =item RETURN
1739    
1740    Returns a list of 3-tuples. Each tuple consists of a feature similar to the feature
1741    of interest, a feature similar to the functionally coupled feature, and a flag
1742    that is TRUE for a representative piece of evidence and FALSE otherwise.
1743    
1744    =back
1745    
1746  =cut  =cut
1747  #: Return Type @;  #: Return Type @@;
1748  sub GetEntityTypes {  sub CouplingEvidence {
1749          # Get the parameters.          # Get the parameters.
1750          my ($self) = @_;      my ($self, $peg1, $peg2) = @_;
1751          # Get the underlying database object.      # Declare the return variable.
1752          my $erdb = $self->{_erdb};      my @retVal = ();
1753          # Get its entity type list.      # Our first task is to find out the nature of the coupling: whether or not
1754          my @retVal = $erdb->GetEntityTypes();      # it exists, its score, and whether the features are stored in the same
1755        # order as the ones coming in.
1756        my ($couplingID, $inverted, $score) = $self->GetCoupling($peg1, $peg2);
1757        # Only proceed if a coupling exists.
1758        if ($couplingID) {
1759            # Determine the ordering to place on the evidence items. If we're
1760            # inverted, we want to see feature 2 before feature 1 (descending); otherwise,
1761            # we want feature 1 before feature 2 (normal).
1762            Trace("Coupling evidence for ($peg1, $peg2) with inversion flag $inverted.") if T(Coupling => 4);
1763            my $ordering = ($inverted ? "DESC" : "");
1764            # Get the coupling evidence.
1765            my @evidenceList = $self->GetAll(['IsEvidencedBy', 'PCH', 'UsesAsEvidence'],
1766                                              "IsEvidencedBy(from-link) = ? ORDER BY PCH(id), UsesAsEvidence(pos) $ordering",
1767                                              [$couplingID],
1768                                              ['PCH(used)', 'UsesAsEvidence(to-link)']);
1769            # Loop through the evidence items. Each piece of evidence is represented by two
1770            # positions in the evidence list, one for each feature on the other side of the
1771            # evidence link. If at some point we want to generalize to couplings with
1772            # more than two positions, this section of code will need to be re-done.
1773            while (@evidenceList > 0) {
1774                my $peg1Data = shift @evidenceList;
1775                my $peg2Data = shift @evidenceList;
1776                Trace("Peg 1 is " . $peg1Data->[1] . " and Peg 2 is " . $peg2Data->[1] . ".") if T(Coupling => 4);
1777                push @retVal, [$peg1Data->[1], $peg2Data->[1], $peg1Data->[0]];
1778            }
1779            Trace("Last index in evidence result is is $#retVal.") if T(Coupling => 4);
1780        }
1781        # Return the result.
1782        return @retVal;
1783    }
1784    
1785    =head3 GetCoupling
1786    
1787    C<< my ($couplingID, $inverted, $score) = $sprout->GetCoupling($peg1, $peg2); >>
1788    
1789    Return the coupling (if any) for the specified pair of PEGs. If a coupling
1790    exists, we return the coupling ID along with an indicator of whether the
1791    coupling is stored as C<(>I<$peg1>C<, >I<$peg2>C<)> or C<(>I<$peg2>C<, >I<$peg1>C<)>.
1792    In the second case, we say the coupling is I<inverted>. The importance of an
1793    inverted coupling is that the PEGs in the evidence will appear in reverse order.
1794    
1795    =over 4
1796    
1797    =item peg1
1798    
1799    ID of the feature of interest.
1800    
1801    =item peg2
1802    
1803    ID of the potentially coupled feature.
1804    
1805    =item RETURN
1806    
1807    Returns a three-element list. The first element contains the database ID of
1808    the coupling. The second element is FALSE if the coupling is stored in the
1809    database in the caller specified order and TRUE if it is stored in the
1810    inverted order. The third element is the coupling's score. If the coupling
1811    does not exist, all three list elements will be C<undef>.
1812    
1813    =back
1814    
1815    =cut
1816    #: Return Type $%@;
1817    sub GetCoupling {
1818        # Get the parameters.
1819        my ($self, $peg1, $peg2) = @_;
1820        # Declare the return values. We'll start with the coupling ID and undefine the
1821        # flag and score until we have more information.
1822        my ($retVal, $inverted, $score) = ($self->CouplingID($peg1, $peg2), undef, undef);
1823        # Find the coupling data.
1824        my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'],
1825                                     "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)",
1826                                     [$retVal], ["ParticipatesInCoupling(from-link)", "Coupling(score)"]);
1827        # Check to see if we found anything.
1828        if (!@pegs) {
1829            Trace("No coupling found.") if T(Coupling => 4);
1830            # No coupling, so undefine the return value.
1831            $retVal = undef;
1832        } else {
1833            # We have a coupling! Get the score and check for inversion.
1834            $score = $pegs[0]->[1];
1835            my $firstFound = $pegs[0]->[0];
1836            $inverted = ($firstFound ne $peg1);
1837            Trace("Coupling score is $score. First peg is $firstFound, peg 1 is $peg1.") if T(Coupling => 4);
1838        }
1839        # Return the result.
1840        return ($retVal, $inverted, $score);
1841    }
1842    
1843    =head3 GetSynonymGroup
1844    
1845    C<< my $id = $sprout->GetSynonymGroup($fid); >>
1846    
1847    Return the synonym group name for the specified feature.
1848    
1849    =over 4
1850    
1851    =item fid
1852    
1853    ID of the feature whose synonym group is desired.
1854    
1855    =item RETURN
1856    
1857    The name of the synonym group to which the feature belongs. If the feature does
1858    not belong to a synonym group, the feature ID itself is returned.
1859    
1860    =back
1861    
1862    =cut
1863    
1864    sub GetSynonymGroup {
1865        # Get the parameters.
1866        my ($self, $fid) = @_;
1867        # Declare the return variable.
1868        my $retVal;
1869        # Find the synonym group.
1870        my @groups = $self->GetFlat(['IsSynonymGroupFor'], "IsSynonymGroupFor(to-link) = ?",
1871                                       [$fid], 'IsSynonymGroupFor(from-link)');
1872        # Check to see if we found anything.
1873        if (@groups) {
1874            $retVal = $groups[0];
1875        } else {
1876            $retVal = $fid;
1877        }
1878        # Return the result.
1879        return $retVal;
1880    }
1881    
1882    =head3 GetBoundaries
1883    
1884    C<< my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList); >>
1885    
1886    Determine the begin and end boundaries for the locations in a list. All of the
1887    locations must belong to the same contig and have mostly the same direction in
1888    order for this method to produce a meaningful result. The resulting
1889    begin/end pair will contain all of the bases in any of the locations.
1890    
1891    =over 4
1892    
1893    =item locList
1894    
1895    List of locations to process.
1896    
1897    =item RETURN
1898    
1899    Returns a 3-tuple consisting of the contig ID, the beginning boundary,
1900    and the ending boundary. The beginning boundary will be left of the
1901    end for mostly-forward locations and right of the end for mostly-backward
1902    locations.
1903    
1904    =back
1905    
1906    =cut
1907    
1908    sub GetBoundaries {
1909        # Get the parameters.
1910        my ($self, @locList) = @_;
1911        # Set up the counters used to determine the most popular direction.
1912        my %counts = ( '+' => 0, '-' => 0 );
1913        # Get the last location and parse it.
1914        my $locObject = BasicLocation->new(pop @locList);
1915        # Prime the loop with its data.
1916        my ($contig, $beg, $end) = ($locObject->Contig, $locObject->Left, $locObject->Right);
1917        # Count its direction.
1918        $counts{$locObject->Dir}++;
1919        # Loop through the remaining locations. Note that in most situations, this loop
1920        # will not iterate at all, because most of the time we will be dealing with a
1921        # singleton list.
1922        for my $loc (@locList) {
1923            # Create a location object.
1924            my $locObject = BasicLocation->new($loc);
1925            # Count the direction.
1926            $counts{$locObject->Dir}++;
1927            # Get the left end and the right end.
1928            my $left = $locObject->Left;
1929            my $right = $locObject->Right;
1930            # Merge them into the return variables.
1931            if ($left < $beg) {
1932                $beg = $left;
1933            }
1934            if ($right > $end) {
1935                $end = $right;
1936            }
1937        }
1938        # If the most common direction is reverse, flip the begin and end markers.
1939        if ($counts{'-'} > $counts{'+'}) {
1940            ($beg, $end) = ($end, $beg);
1941        }
1942        # Return the result.
1943        return ($contig, $beg, $end);
1944    }
1945    
1946    =head3 CouplingID
1947    
1948    C<< my $couplingID = $sprout->CouplingID($peg1, $peg2); >>
1949    
1950    Return the coupling ID for a pair of feature IDs.
1951    
1952    The coupling ID is currently computed by joining the feature IDs in
1953    sorted order with a space. Client modules (that is, modules which
1954    use Sprout) should not, however, count on this always being the
1955    case. This method provides a way for abstracting the concept of a
1956    coupling ID. All that we know for sure about it is that it can be
1957    generated easily from the feature IDs and the order of the IDs
1958    in the parameter list does not matter (i.e. C<CouplingID("a1", "b1")>
1959    will have the same value as C<CouplingID("b1", "a1")>.
1960    
1961    =over 4
1962    
1963    =item peg1
1964    
1965    First feature of interest.
1966    
1967    =item peg2
1968    
1969    Second feature of interest.
1970    
1971    =item RETURN
1972    
1973    Returns the ID that would be used to represent a functional coupling of
1974    the two specified PEGs.
1975    
1976    =back
1977    
1978    =cut
1979    #: Return Type $;
1980    sub CouplingID {
1981        my ($self, @pegs) = @_;
1982        return $self->DigestKey(join " ", sort @pegs);
1983  }  }
1984    
1985  =head3 ReadFasta  =head3 ReadFasta
# Line 1447  Line 2026 
2026                  if ($line =~ m/^>\s*(.+?)(\s|\n)/) {                  if ($line =~ m/^>\s*(.+?)(\s|\n)/) {
2027                          # 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.
2028                          if ($id) {                          if ($id) {
2029                                  $retVal{$id} = $sequence;                  $retVal{$id} = lc $sequence;
2030                          }                          }
2031                          # Clear the sequence accumulator and save the new ID.                          # Clear the sequence accumulator and save the new ID.
2032                          ($id, $sequence) = ("$prefix$1", "");                          ($id, $sequence) = ("$prefix$1", "");
2033                  } else {                  } else {
2034                          # 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.
2035                          # First, we get the actual data out.              # First, we get the actual data out. Note that we normalize to lower
2036                # case.
2037                          $line =~ /^\s*(.*?)(\s|\n)/;                          $line =~ /^\s*(.*?)(\s|\n)/;
2038                          $sequence .= $1;                          $sequence .= $1;
2039                  }                  }
2040          }          }
2041          # Flush out the last sequence (if any).          # Flush out the last sequence (if any).
2042          if ($sequence) {          if ($sequence) {
2043                  $retVal {$id} = $sequence;          $retVal{$id} = lc $sequence;
2044          }          }
2045        # Close the file.
2046        close FASTAFILE;
2047          # Return the hash constructed from the file.          # Return the hash constructed from the file.
2048          return %retVal;          return %retVal;
2049  }  }
# Line 1545  Line 2127 
2127          # Get the data directory name.          # Get the data directory name.
2128          my $outputDirectory = $self->{_options}->{dataDir};          my $outputDirectory = $self->{_options}->{dataDir};
2129          # Dump the relations.          # Dump the relations.
2130          $self->{_erdb}->DumpRelations($outputDirectory);      $self->DumpRelations($outputDirectory);
2131  }  }
2132    
2133  =head3 XMLFileName  =head3 XMLFileName
# Line 1577  Line 2159 
2159  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
2160  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>.
2161    
2162  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'}); >>
2163    
2164  =over 4  =over 4
2165    
# Line 1597  Line 2179 
2179          # Get the parameters.          # Get the parameters.
2180          my ($self, $objectType, $fieldHash) = @_;          my ($self, $objectType, $fieldHash) = @_;
2181          # Call the underlying method.          # Call the underlying method.
2182          $self->{_erdb}->InsertObject($objectType, $fieldHash);      $self->InsertObject($objectType, $fieldHash);
2183  }  }
2184    
2185  =head3 Annotate  =head3 Annotate
# Line 1656  Line 2238 
2238    
2239  =head3 AssignFunction  =head3 AssignFunction
2240    
2241  C<< my $ok = $sprout->AssignFunction($featureID, $user, $function); >>  C<< my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser); >>
2242    
2243  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
2244  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.  
2245    
2246  =over 4  =over 4
2247    
# Line 1670  Line 2251 
2251    
2252  =item user  =item user
2253    
2254  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>.
2255    
2256  =item function  =item function
2257    
2258  Text of the function being assigned.  Text of the function being assigned.
2259    
2260    =item assigningUser (optional)
2261    
2262    Name of the individual user making the assignment. If omitted, defaults to the user group.
2263    
2264  =item RETURN  =item RETURN
2265    
2266  Returns 1 if successful, 0 if an error occurred.  Returns 1 if successful, 0 if an error occurred.
# Line 1686  Line 2271 
2271  #: Return Type $;  #: Return Type $;
2272  sub AssignFunction {  sub AssignFunction {
2273          # Get the parameters.          # Get the parameters.
2274          my ($self, $featureID, $user, $function) = @_;      my ($self, $featureID, $user, $function, $assigningUser) = @_;
2275        # Default the assigning user.
2276        if (! $assigningUser) {
2277            $assigningUser = $user;
2278        }
2279          # Create an annotation string from the parameters.          # Create an annotation string from the parameters.
2280          my $annotationText = "$user\nset $user function to\n$function";      my $annotationText = "$assigningUser\nset $user function to\n$function";
2281          # Get the current time.          # Get the current time.
2282          my $now = time;          my $now = time;
2283          # Declare the return variable.          # Declare the return variable.
# Line 1749  Line 2338 
2338          return @retVal;          return @retVal;
2339  }  }
2340    
2341  =head3 Exists  =head3 FeatureTranslation
   
 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, $entityName, $entityID) = @_;  
         # Check for the entity instance.  
         my $testInstance = $self->GetEntity($entityName, $entityID);  
         # Return an existence indicator.  
         my $retVal = ($testInstance ? 1 : 0);  
         return $retVal;  
 }  
   
 =head3 FeatureTranslation  
2342    
2343  C<< my $translation = $sprout->FeatureTranslation($featureID); >>  C<< my $translation = $sprout->FeatureTranslation($featureID); >>
2344    
# Line 1968  Line 2523 
2523          return @retVal;          return @retVal;
2524  }  }
2525    
2526    =head3 GetProperties
2527    
2528    C<< my @list = $sprout->GetProperties($fid, $key, $value, $url); >>
2529    
2530    Return a list of the properties with the specified characteristics.
2531    
2532    Properties are arbitrary key-value pairs associated with a feature. (At some point they
2533    will also be associated with genomes.) A property value is represented by a 4-tuple of
2534    the form B<($fid, $key, $value, $url)>. These exactly correspond to the parameter
2535    
2536    =over 4
2537    
2538    =item fid
2539    
2540    ID of the feature possessing the property.
2541    
2542    =item key
2543    
2544    Name or key of the property.
2545    
2546    =item value
2547    
2548    Value of the property.
2549    
2550    =item url
2551    
2552    URL of the document that indicated the property should have this particular value, or an
2553    empty string if no such document exists.
2554    
2555    =back
2556    
2557    The parameters act as a filter for the desired data. Any non-null parameter will
2558    automatically match all the tuples returned. So, specifying just the I<$fid> will
2559    return all the properties of the specified feature; similarly, specifying the I<$key>
2560    and I<$value> parameters will return all the features having the specified property
2561    value.
2562    
2563    A single property key can have many values, representing different ideas about the
2564    feature in question. For example, one paper may declare that a feature C<fig|83333.1.peg.10> is
2565    virulent, and another may declare that it is not virulent. A query about the virulence of
2566    C<fig|83333.1.peg.10> would be coded as
2567    
2568        my @list = $sprout->GetProperties('fig|83333.1.peg.10', 'virulence', '', '');
2569    
2570    Here the I<$value> and I<$url> fields are left blank, indicating that those fields are
2571    not to be filtered. The tuples returned would be
2572    
2573        ('fig|83333.1.peg.10', 'virulence', 'yes', 'http://www.somewhere.edu/first.paper.pdf')
2574        ('fig|83333.1.peg.10', 'virulence', 'no', 'http://www.somewhere.edu/second.paper.pdf')
2575    
2576    =cut
2577    #: Return Type @@;
2578    sub GetProperties {
2579        # Get the parameters.
2580        my ($self, @parms) = @_;
2581        # Declare the return variable.
2582        my @retVal = ();
2583        # Now we need to create a WHERE clause that will get us the data we want. First,
2584        # we create a list of the columns containing the data for each parameter.
2585        my @colNames = ('HasProperty(from-link)', 'Property(property-name)',
2586                        'Property(property-value)', 'HasProperty(evidence)');
2587        # Now we build the WHERE clause and the list of parameter values.
2588        my @where = ();
2589        my @values = ();
2590        for (my $i = 0; $i <= $#colNames; $i++) {
2591            my $parm = $parms[$i];
2592            if (defined $parm && ($parm ne '')) {
2593                push @where, "$colNames[$i] = ?";
2594                push @values, $parm;
2595            }
2596        }
2597        # Format the WHERE clause.
2598        my $filter = (@values > 0 ? (join " AND ", @where) : undef);
2599        # Ask for all the propertie values with the desired characteristics.
2600        my $query = $self->Get(['HasProperty', 'Property'], $filter, \@values);
2601        while (my $valueObject = $query->Fetch()) {
2602            my @tuple = $valueObject->Values(\@colNames);
2603            push @retVal, \@tuple;
2604        }
2605        # Return the result.
2606        return @retVal;
2607    }
2608    
2609  =head3 FeatureProperties  =head3 FeatureProperties
2610    
2611  C<< my @properties = $sprout->FeatureProperties($featureID); >>  C<< my @properties = $sprout->FeatureProperties($featureID); >>
# Line 2034  Line 2672 
2672          return $retVal;          return $retVal;
2673  }  }
2674    
2675    =head3 PropertyID
2676    
2677    C<< my $id = $sprout->PropertyID($propName, $propValue); >>
2678    
2679    Return the ID of the specified property name and value pair, if the
2680    pair exists.
2681    
2682    =over 4
2683    
2684    =item propName
2685    
2686    Name of the desired property.
2687    
2688    =item propValue
2689    
2690    Value expected for the desired property.
2691    
2692    =item RETURN
2693    
2694    Returns the ID of the name/value pair, or C<undef> if the pair does not exist.
2695    
2696    =back
2697    
2698    =cut
2699    
2700    sub PropertyID {
2701        # Get the parameters.
2702        my ($self, $propName, $propValue) = @_;
2703        # Try to find the ID.
2704        my ($retVal) = $self->GetFlat(['Property'],
2705                                      "Property(property-name) = ? AND Property(property-value) = ?",
2706                                      [$propName, $propValue], 'Property(id)');
2707        # Return the result.
2708        return $retVal;
2709    }
2710    
2711  =head3 MergedAnnotations  =head3 MergedAnnotations
2712    
2713  C<< my @annotationList = $sprout->MergedAnnotations(\@list); >>  C<< my @annotationList = $sprout->MergedAnnotations(\@list); >>
# Line 2162  Line 2836 
2836  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>
2837    
2838  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
2839  to the role the feature performs.  to the roles the feature performs.
2840    
2841  =over 4  =over 4
2842    
# Line 2172  Line 2846 
2846    
2847  =item RETURN  =item RETURN
2848    
2849  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.
2850    
2851  =back  =back
2852    
2853  =cut  =cut
2854  #: Return Type %;  #: Return Type %@;
2855  sub SubsystemsOf {  sub SubsystemsOf {
2856          # Get the parameters.          # Get the parameters.
2857          my ($self, $featureID) = @_;          my ($self, $featureID) = @_;
2858          # Use the SSCell to connect features to subsystems.      # Get the subsystem list.
2859          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],
2860                                                                          "ContainsFeature(to-link) = ?", [$featureID],                                                                          "ContainsFeature(to-link) = ?", [$featureID],
2861                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);
2862          # Create the return value.          # Create the return value.
2863          my %retVal = ();          my %retVal = ();
2864        # Build a hash to weed out duplicates. Sometimes the same PEG and role appears
2865        # in two spreadsheet cells.
2866        my %dupHash = ();
2867          # Loop through the results, adding them to the hash.          # Loop through the results, adding them to the hash.
2868          for my $record (@subsystems) {          for my $record (@subsystems) {
2869                  $retVal{$record->[0]} = $record->[1];          # Get this subsystem and role.
2870            my ($subsys, $role) = @{$record};
2871            # Insure it's the first time for both.
2872            my $dupKey = "$subsys\n$role";
2873            if (! exists $dupHash{"$subsys\n$role"}) {
2874                $dupHash{$dupKey} = 1;
2875                push @{$retVal{$subsys}}, $role;
2876            }
2877          }          }
2878          # Return the hash.          # Return the hash.
2879          return %retVal;          return %retVal;
2880  }  }
2881    
2882    =head3 SubsystemList
2883    
2884    C<< my @subsystems = $sprout->SubsystemList($featureID); >>
2885    
2886    Return a list containing the names of the subsystems in which the specified
2887    feature participates. Unlike L</SubsystemsOf>, this method only returns the
2888    subsystem names, not the roles.
2889    
2890    =over 4
2891    
2892    =item featureID
2893    
2894    ID of the feature whose subsystem names are desired.
2895    
2896    =item RETURN
2897    
2898    Returns a list of the names of the subsystems in which the feature participates.
2899    
2900    =back
2901    
2902    =cut
2903    #: Return Type @;
2904    sub SubsystemList {
2905        # Get the parameters.
2906        my ($self, $featureID) = @_;
2907        # Get the list of names.
2908        my @retVal = $self->GetFlat(['HasRoleInSubsystem'], "HasRoleInSubsystem(from-link) = ?",
2909                                    [$featureID], 'HasRoleInSubsystem(to-link)');
2910        # Return the result, sorted.
2911        return sort @retVal;
2912    }
2913    
2914    =head3 GenomeSubsystemData
2915    
2916    C<< my %featureData = $sprout->GenomeSubsystemData($genomeID); >>
2917    
2918    Return a hash mapping genome features to their subsystem roles.
2919    
2920    =over 4
2921    
2922    =item genomeID
2923    
2924    ID of the genome whose subsystem feature map is desired.
2925    
2926    =item RETURN
2927    
2928    Returns a hash mapping each feature of the genome to a list of 2-tuples. Eacb
2929    2-tuple contains a subsystem name followed by a role ID.
2930    
2931    =back
2932    
2933    =cut
2934    
2935    sub GenomeSubsystemData {
2936        # Get the parameters.
2937        my ($self, $genomeID) = @_;
2938        # Declare the return variable.
2939        my %retVal = ();
2940        # Get a list of the genome features that participate in subsystems. For each
2941        # feature we get its spreadsheet cells and the corresponding roles.
2942        my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf'],
2943                                 "HasFeature(from-link) = ?", [$genomeID],
2944                                 ['HasFeature(to-link)', 'IsRoleOf(to-link)', 'IsRoleOf(from-link)']);
2945        # Now we get a list of the spreadsheet cells and their associated subsystems. Subsystems
2946        # with an unknown variant code (-1) are skipped. Note the genome ID is at both ends of the
2947        # list. We use it at the beginning to get all the spreadsheet cells for the genome and
2948        # again at the end to filter out participation in subsystems with a negative variant code.
2949        my @cellData = $self->GetAll(['IsGenomeOf', 'HasSSCell', 'ParticipatesIn'],
2950                                     "IsGenomeOf(from-link) = ? AND ParticipatesIn(variant-code) >= 0 AND ParticipatesIn(from-link) = ?",
2951                                     [$genomeID, $genomeID], ['HasSSCell(to-link)', 'HasSSCell(from-link)']);
2952        # Now "@roleData" lists the spreadsheet cell and role for each of the genome's features.
2953        # "@cellData" lists the subsystem name for each of the genome's spreadsheet cells. We
2954        # link these two lists together to create the result. First, we want a hash mapping
2955        # spreadsheet cells to subsystem names.
2956        my %subHash = map { $_->[0] => $_->[1] } @cellData;
2957        # We loop through @cellData to build the hash.
2958        for my $roleEntry (@roleData) {
2959            # Get the data for this feature and cell.
2960            my ($fid, $cellID, $role) = @{$roleEntry};
2961            # Check for a subsystem name.
2962            my $subsys = $subHash{$cellID};
2963            if ($subsys) {
2964                # Insure this feature has an entry in the return hash.
2965                if (! exists $retVal{$fid}) { $retVal{$fid} = []; }
2966                # Merge in this new data.
2967                push @{$retVal{$fid}}, [$subsys, $role];
2968            }
2969        }
2970        # Return the result.
2971        return %retVal;
2972    }
2973    
2974  =head3 RelatedFeatures  =head3 RelatedFeatures
2975    
2976  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>
# Line 2230  Line 3006 
3006          # Get the parameters.          # Get the parameters.
3007          my ($self, $featureID, $function, $userID) = @_;          my ($self, $featureID, $function, $userID) = @_;
3008          # 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.
3009          my @bbhFeatures = $self->GetFlat(['IsBidirectionalBestHitOf'],      my @bbhFeatures = map { $_->[0] } FIGRules::BBHData($featureID);
                                                                          "IsBidirectionalBestHitOf(from-link) = ?", [$featureID],  
                                                                          'IsBidirectionalBestHitOf(to-link)');  
3010          # 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
3011          # functional assignment.          # functional assignment.
3012          my @retVal = ();          my @retVal = ();
# Line 2296  Line 3070 
3070          return @retVal;          return @retVal;
3071  }  }
3072    
 =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, $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, $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;  
 }  
   
3073  =head3 Protein  =head3 Protein
3074    
3075  C<< my $protein = Sprout::Protein($sequence, $table); >>  C<< my $protein = Sprout::Protein($sequence, $table); >>
# Line 2500  Line 3142 
3142          # Loop through the input triples.          # Loop through the input triples.
3143          my $n = length $sequence;          my $n = length $sequence;
3144          for (my $i = 0; $i < $n; $i += 3) {          for (my $i = 0; $i < $n; $i += 3) {
3145                  # Get the current triple from the sequence.          # Get the current triple from the sequence. Note we convert to
3146                  my $triple = substr($sequence, $i, 3);          # upper case to insure a match.
3147            my $triple = uc substr($sequence, $i, 3);
3148                  # Translate it using the table.                  # Translate it using the table.
3149                  my $protein = "X";                  my $protein = "X";
3150                  if (exists $table->{$triple}) { $protein = $table->{$triple}; }                  if (exists $table->{$triple}) { $protein = $table->{$triple}; }
# Line 2529  Line 3172 
3172          # 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.
3173          my @retVal = ($self->{_options}->{dataDir});          my @retVal = ($self->{_options}->{dataDir});
3174          # Concatenate the table names.          # Concatenate the table names.
3175          push @retVal, $self->{_erdb}->GetTableNames();      push @retVal, $self->GetTableNames();
3176          # Return the result.          # Return the result.
3177          return @retVal;          return @retVal;
3178  }  }
3179    
3180  =head3 LowBBHs  =head3 LowBBHs
3181    
3182  C<< my %bbhMap = $sprout->GoodBBHs($featureID, $cutoff); >>  C<< my %bbhMap = $sprout->LowBBHs($featureID, $cutoff); >>
3183    
3184  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
3185  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 2565  Line 3208 
3208          my ($self, $featureID, $cutoff) = @_;          my ($self, $featureID, $cutoff) = @_;
3209          # Create the return hash.          # Create the return hash.
3210          my %retVal = ();          my %retVal = ();
3211          # Create a query to get the desired BBHs.      # Query for the desired BBHs.
3212          my @bbhList = $self->GetAll(['IsBidirectionalBestHitOf'],      my @bbhList = FIGRules::BBHData($featureID, $cutoff);
                                                                 'IsBidirectionalBestHitOf(sc) <= ? AND IsBidirectionalBestHitOf(from-link) = ?',  
                                                                 [$cutoff, $featureID],  
                                                                 ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(sc)']);  
3213          # Form the results into the return hash.          # Form the results into the return hash.
3214          for my $pair (@bbhList) {          for my $pair (@bbhList) {
3215                  $retVal{$pair->[0]} = $pair->[1];                  $retVal{$pair->[0]} = $pair->[1];
# Line 2578  Line 3218 
3218          return %retVal;          return %retVal;
3219  }  }
3220    
3221    =head3 Sims
3222    
3223    C<< my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters); >>
3224    
3225    Get a list of similarities for a specified feature. Similarity information is not kept in the
3226    Sprout database; rather, they are retrieved from a network server. The similarities are
3227    returned as B<Sim> objects. A Sim object is actually a list reference that has been blessed
3228    so that its elements can be accessed by name.
3229    
3230    Similarities can be either raw or expanded. The raw similarities are basic
3231    hits between features with similar DNA. Expanding a raw similarity drags in any
3232    features considered substantially identical. So, for example, if features B<A1>,
3233    B<A2>, and B<A3> are all substatially identical to B<A>, then a raw similarity
3234    B<[C,A]> would be expanded to B<[C,A] [C,A1] [C,A2] [C,A3]>.
3235    
3236    =over 4
3237    
3238    =item fid
3239    
3240    ID of the feature whose similarities are desired.
3241    
3242    =item maxN
3243    
3244    Maximum number of similarities to return.
3245    
3246    =item maxP
3247    
3248    Minumum allowable similarity score.
3249    
3250    =item select
3251    
3252    Selection criterion: C<raw> means only raw similarities are returned; C<fig>
3253    means only similarities to FIG features are returned; C<all> means all expanded
3254    similarities are returned; and C<figx> means similarities are expanded until the
3255    number of FIG features equals the maximum.
3256    
3257    =item max_expand
3258    
3259    The maximum number of features to expand.
3260    
3261    =item filters
3262    
3263    Reference to a hash containing filter information, or a subroutine that can be
3264    used to filter the sims.
3265    
3266    =item RETURN
3267    
3268    Returns a reference to a list of similarity objects, or C<undef> if an error
3269    occurred.
3270    
3271    =back
3272    
3273    =cut
3274    
3275    sub Sims {
3276        # Get the parameters.
3277        my ($self, $fid, $maxN, $maxP, $select, $max_expand, $filters) = @_;
3278        # Create the shim object to test for deleted FIDs.
3279        my $shim = FidCheck->new($self);
3280        # Ask the network for sims.
3281        my $retVal = FIGRules::GetNetworkSims($shim, $fid, {}, $maxN, $maxP, $select, $max_expand, $filters);
3282        # Return the result.
3283        return $retVal;
3284    }
3285    
3286    =head3 IsAllGenomes
3287    
3288    C<< my $flag = $sprout->IsAllGenomes(\@list, \@checkList); >>
3289    
3290    Return TRUE if all genomes in the second list are represented in the first list at
3291    least one. Otherwise, return FALSE. If the second list is omitted, the first list is
3292    compared to a list of all the genomes.
3293    
3294    =over 4
3295    
3296    =item list
3297    
3298    Reference to the list to be compared to the second list.
3299    
3300    =item checkList (optional)
3301    
3302    Reference to the comparison target list. Every genome ID in this list must occur at
3303    least once in the first list. If this parameter is omitted, a list of all the genomes
3304    is used.
3305    
3306    =item RETURN
3307    
3308    Returns TRUE if every item in the second list appears at least once in the
3309    first list, else FALSE.
3310    
3311    =back
3312    
3313    =cut
3314    
3315    sub IsAllGenomes {
3316        # Get the parameters.
3317        my ($self, $list, $checkList) = @_;
3318        # Supply the checklist if it was omitted.
3319        $checkList = [$self->Genomes()] if ! defined($checkList);
3320        # Create a hash of the original list.
3321        my %testList = map { $_ => 1 } @{$list};
3322        # Declare the return variable. We assume that the representation
3323        # is complete and stop at the first failure.
3324        my $retVal = 1;
3325        my $n = scalar @{$checkList};
3326        for (my $i = 0; $retVal && $i < $n; $i++) {
3327            if (! $testList{$checkList->[$i]}) {
3328                $retVal = 0;
3329            }
3330        }
3331        # Return the result.
3332        return $retVal;
3333    }
3334    
3335  =head3 GetGroups  =head3 GetGroups
3336    
3337  C<< my %groups = $sprout->GetGroups(\@groupList); >>  C<< my %groups = $sprout->GetGroups(\@groupList); >>
# Line 2599  Line 3353 
3353          # Here we have a group list. Loop through them individually,          # Here we have a group list. Loop through them individually,
3354          # getting a list of the relevant genomes.          # getting a list of the relevant genomes.
3355          for my $group (@{$groupList}) {          for my $group (@{$groupList}) {
3356              my @genomeIDs = $self->GetFlat(['Genome'], "Genome(group-name) = ?",              my @genomeIDs = $self->GetFlat(['Genome'], "Genome(primary-group) = ?",
3357                  [$group], "Genome(id)");                  [$group], "Genome(id)");
3358              $retVal{$group} = \@genomeIDs;              $retVal{$group} = \@genomeIDs;
3359          }          }
# Line 2607  Line 3361 
3361          # Here we need all of the groups. In this case, we run through all          # Here we need all of the groups. In this case, we run through all
3362          # of the genome records, putting each one found into the appropriate          # of the genome records, putting each one found into the appropriate
3363          # group. Note that we use a filter clause to insure that only genomes          # group. Note that we use a filter clause to insure that only genomes
3364          # in groups are included in the return set.          # in real NMPDR groups are included in the return set.
3365          my @genomes = $self->GetAll(['Genome'], "Genome(group-name) > ' '", [],          my @genomes = $self->GetAll(['Genome'], "Genome(primary-group) <> ?",
3366                                      ['Genome(id)', 'Genome(group-name)']);                                      [$FIG_Config::otherGroup], ['Genome(id)', 'Genome(primary-group)']);
3367          # Loop through the genomes found.          # Loop through the genomes found.
3368          for my $genome (@genomes) {          for my $genome (@genomes) {
3369              # Pop this genome's ID off the current list.              # Pop this genome's ID off the current list.
# Line 2626  Line 3380 
3380      return %retVal;      return %retVal;
3381  }  }
3382    
3383    =head3 MyGenomes
3384    
3385    C<< my @genomes = Sprout::MyGenomes($dataDir); >>
3386    
3387    Return a list of the genomes to be included in the Sprout.
3388    
3389    This method is provided for use during the Sprout load. It presumes the Genome load file has
3390    already been created. (It will be in the Sprout data directory and called either C<Genome>
3391    or C<Genome.dtx>.) Essentially, it reads in the Genome load file and strips out the genome
3392    IDs.
3393    
3394    =over 4
3395    
3396    =item dataDir
3397    
3398    Directory containing the Sprout load files.
3399    
3400    =back
3401    
3402    =cut
3403    #: Return Type @;
3404    sub MyGenomes {
3405        # Get the parameters.
3406        my ($dataDir) = @_;
3407        # Compute the genome file name.
3408        my $genomeFileName = LoadFileName($dataDir, "Genome");
3409        # Extract the genome IDs from the files.
3410        my @retVal = map { $_ =~ /^(\S+)/; $1 } Tracer::GetFile($genomeFileName);
3411        # Return the result.
3412        return @retVal;
3413    }
3414    
3415    =head3 LoadFileName
3416    
3417    C<< my $fileName = Sprout::LoadFileName($dataDir, $tableName); >>
3418    
3419    Return the name of the load file for the specified table in the specified data
3420    directory.
3421    
3422    =over 4
3423    
3424    =item dataDir
3425    
3426    Directory containing the Sprout load files.
3427    
3428    =item tableName
3429    
3430    Name of the table whose load file is desired.
3431    
3432    =item RETURN
3433    
3434    Returns the name of the file containing the load data for the specified table, or
3435    C<undef> if no load file is present.
3436    
3437    =back
3438    
3439    =cut
3440    #: Return Type $;
3441    sub LoadFileName {
3442        # Get the parameters.
3443        my ($dataDir, $tableName) = @_;
3444        # Declare the return variable.
3445        my $retVal;
3446        # Check for the various file names.
3447        if (-e "$dataDir/$tableName") {
3448            $retVal = "$dataDir/$tableName";
3449        } elsif (-e "$dataDir/$tableName.dtx") {
3450            $retVal = "$dataDir/$tableName.dtx";
3451        }
3452        # Return the result.
3453        return $retVal;
3454    }
3455    
3456    =head3 DeleteGenome
3457    
3458    C<< my $stats = $sprout->DeleteGenome($genomeID, $testFlag); >>
3459    
3460    Delete a genome from the database.
3461    
3462    =over 4
3463    
3464    =item genomeID
3465    
3466    ID of the genome to delete
3467    
3468    =item testFlag
3469    
3470    If TRUE, then the DELETE statements will be traced, but no deletions will occur.
3471    
3472    =item RETURN
3473    
3474    Returns a statistics object describing the rows deleted.
3475    
3476    =back
3477    
3478    =cut
3479    #: Return Type $%;
3480    sub DeleteGenome {
3481        # Get the parameters.
3482        my ($self, $genomeID, $testFlag) = @_;
3483        # Perform the delete for the genome's features.
3484        my $retVal = $self->Delete('Feature', "fig|$genomeID.%", $testFlag);
3485        # Perform the delete for the primary genome data.
3486        my $stats = $self->Delete('Genome', $genomeID, $testFlag);
3487        $retVal->Accumulate($stats);
3488        # Return the result.
3489        return $retVal;
3490    }
3491    
3492    =head3 Fix
3493    
3494    C<< my %fixedHash = Sprout::Fix(%groupHash); >>
3495    
3496    Prepare a genome group hash (like that returned by L</GetGroups> for processing.
3497    Groups with the same primary name will be combined. The primary name is the
3498    first capitalized word in the group name.
3499    
3500    =over 4
3501    
3502    =item groupHash
3503    
3504    Hash to be fixed up.
3505    
3506    =item RETURN
3507    
3508    Returns a fixed-up version of the hash.
3509    
3510    =back
3511    
3512    =cut
3513    
3514    sub Fix {
3515        # Get the parameters.
3516        my (%groupHash) = @_;
3517        # Create the result hash.
3518        my %retVal = ();
3519        # Copy over the genomes.
3520        for my $groupID (keys %groupHash) {
3521            # Make a safety copy of the group ID.
3522            my $realGroupID = $groupID;
3523            # Yank the primary name.
3524            if ($groupID =~ /([A-Z]\w+)/) {
3525                $realGroupID = $1;
3526            }
3527            # Append this group's genomes into the result hash.
3528            Tracer::AddToListMap(\%retVal, $realGroupID, @{$groupHash{$groupID}});
3529        }
3530        # Return the result hash.
3531        return %retVal;
3532    }
3533    
3534    =head3 GroupPageName
3535    
3536    C<< my $name = $sprout->GroupPageName($group); >>
3537    
3538    Return the name of the page for the specified NMPDR group.
3539    
3540    =over 4
3541    
3542    =item group
3543    
3544    Name of the relevant group.
3545    
3546    =item RETURN
3547    
3548    Returns the relative page name (e.g. C<../content/campy.php>). If the group file is not in
3549    memory it will be read in.
3550    
3551    =back
3552    
3553    =cut
3554    
3555    sub GroupPageName {
3556        # Get the parameters.
3557        my ($self, $group) = @_;
3558        # Declare the return variable.
3559        my $retVal;
3560        # Check for the group file data.
3561        if (! defined $self->{groupHash}) {
3562            # Read the group file.
3563            my %groupData = Sprout::ReadGroupFile($self->{_options}->{dataDir} . "/groups.tbl");
3564            # Store it in our object.
3565            $self->{groupHash} = \%groupData;
3566        }
3567        # Compute the real group name.
3568        my $realGroup = $group;
3569        if ($group =~ /([A-Z]\w+)/) {
3570            $realGroup = $1;
3571        }
3572        # Return the page name.
3573        $retVal = "../content/" . $self->{groupHash}->{$realGroup}->[1];
3574        # Return the result.
3575        return $retVal;
3576    }
3577    
3578    =head3 ReadGroupFile
3579    
3580    C<< my %groupData = Sprout::ReadGroupFile($groupFileName); >>
3581    
3582    Read in the data from the specified group file. The group file contains information
3583    about each of the NMPDR groups.
3584    
3585    =over 4
3586    
3587    =item name
3588    
3589    Name of the group.
3590    
3591    =item page
3592    
3593    Name of the group's page on the web site (e.g. C<campy.php> for
3594    Campylobacter)
3595    
3596    =item genus
3597    
3598    Genus of the group
3599    
3600    =item species
3601    
3602    Species of the group, or an empty string if the group is for an entire
3603    genus. If the group contains more than one species, the species names
3604    should be separated by commas.
3605    
3606    =back
3607    
3608    The parameters to this method are as follows
3609    
3610    =over 4
3611    
3612    =item groupFile
3613    
3614    Name of the file containing the group data.
3615    
3616    =item RETURN
3617    
3618    Returns a hash keyed on group name. The value of each hash
3619    
3620    =back
3621    
3622    =cut
3623    
3624    sub ReadGroupFile {
3625        # Get the parameters.
3626        my ($groupFileName) = @_;
3627        # Declare the return variable.
3628        my %retVal;
3629        # Read the group file.
3630        my @groupLines = Tracer::GetFile($groupFileName);
3631        for my $groupLine (@groupLines) {
3632            my ($name, $page, $genus, $species) = split(/\t/, $groupLine);
3633            $retVal{$name} = [$page, $genus, $species];
3634        }
3635        # Return the result.
3636        return %retVal;
3637    }
3638    
3639    =head3 AddProperty
3640    
3641    C<< my  = $sprout->AddProperty($featureID, $key, $value, $url); >>
3642    
3643    Add a new attribute value (Property) to a feature. In the SEED system, attributes can
3644    be added to almost any object. In Sprout, they can only be added to features. In
3645    Sprout, attributes are implemented using I<properties>. A property represents a key/value
3646    pair. If the particular key/value pair coming in is not already in the database, a new
3647    B<Property> record is created to hold it.
3648    
3649    =over 4
3650    
3651    =item peg
3652    
3653    ID of the feature to which the attribute is to be replied.
3654    
3655    =item key
3656    
3657    Name of the attribute (key).
3658    
3659    =item value
3660    
3661    Value of the attribute.
3662    
3663    =item url
3664    
3665    URL or text citation from which the property was obtained.
3666    
3667    =back
3668    
3669    =cut
3670    #: Return Type ;
3671    sub AddProperty {
3672        # Get the parameters.
3673        my ($self, $featureID, $key, $value, $url) = @_;
3674        # Declare the variable to hold the desired property ID.
3675        my $propID;
3676        # Attempt to find a property record for this key/value pair.
3677        my @properties = $self->GetFlat(['Property'],
3678                                       "Property(property-name) = ? AND Property(property-value) = ?",
3679                                       [$key, $value], 'Property(id)');
3680        if (@properties) {
3681            # Here the property is already in the database. We save its ID.
3682            $propID = $properties[0];
3683            # Here the property value does not exist. We need to generate an ID. It will be set
3684            # to a number one greater than the maximum value in the database. This call to
3685            # GetAll will stop after one record.
3686            my @maxProperty = $self->GetAll(['Property'], "ORDER BY Property(id) DESC", [], ['Property(id)'],
3687                                            1);
3688            $propID = $maxProperty[0]->[0] + 1;
3689            # Insert the new property value.
3690            $self->Insert('Property', { 'property-name' => $key, 'property-value' => $value, id => $propID });
3691        }
3692        # Now we connect the incoming feature to the property.
3693        $self->Insert('HasProperty', { 'from-link' => $featureID, 'to-link' => $propID, evidence => $url });
3694    }
3695    
3696    =head2 Virtual Methods
3697    
3698    =head3 CleanKeywords
3699    
3700    C<< my $cleanedString = $sprout->CleanKeywords($searchExpression); >>
3701    
3702    Clean up a search expression or keyword list. This involves converting the periods
3703    in EC numbers to underscores, converting non-leading minus signs to underscores,
3704    a vertical bar or colon to an apostrophe, and forcing lower case for all alphabetic
3705    characters. In addition, any extra spaces are removed.
3706    
3707    =over 4
3708    
3709    =item searchExpression
3710    
3711    Search expression or keyword list to clean. Note that a search expression may
3712    contain boolean operators which need to be preserved. This includes leading
3713    minus signs.
3714    
3715    =item RETURN
3716    
3717    Cleaned expression or keyword list.
3718    
3719    =back
3720    
3721    =cut
3722    
3723    sub CleanKeywords {
3724        # Get the parameters.
3725        my ($self, $searchExpression) = @_;
3726        # Perform the standard cleanup.
3727        my $retVal = $self->ERDB::CleanKeywords($searchExpression);
3728        # Fix the periods in EC and TC numbers.
3729        $retVal =~ s/(\d+|\-)\.(\d+|-)\.(\d+|-)\.(\d+|-)/$1_$2_$3_$4/g;
3730        # Fix non-trailing periods.
3731        $retVal =~ s/\.(\w)/_$1/g;
3732        # Fix non-leading minus signs.
3733        $retVal =~ s/(\w)[\-]/$1_/g;
3734        # Fix the vertical bars and colons
3735        $retVal =~ s/(\w)[|:](\w)/$1'$2/g;
3736        # Return the result.
3737        return $retVal;
3738    }
3739    
3740  =head2 Internal Utility Methods  =head2 Internal Utility Methods
3741    
3742  =head3 ParseAssignment  =head3 ParseAssignment
3743    
3744  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,
3745  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
3746  will be returned.  isn't, an empty list will be returned.
3747    
3748    A functional assignment is always of the form
3749    
3750        C<set >I<YYYY>C< function to\n>I<ZZZZZ>
3751    
3752    where I<YYYY> is the B<user>, and I<ZZZZ> is the actual functional role. In most cases,
3753    the user and the assigning user (from MadeAnnotation) will be the same, but that is
3754    not always the case.
3755    
3756    In addition, the functional role may contain extra data that is stripped, such as
3757    terminating spaces or a comment separated from the rest of the text by a tab.
3758    
3759  This is a static method.  This is a static method.
3760    
3761  =over 4  =over 4
3762    
3763    =item user
3764    
3765    Name of the assigning user.
3766    
3767  =item text  =item text
3768    
3769  Text of the annotation.  Text of the annotation.
# Line 2651  Line 3777 
3777    
3778  =cut  =cut
3779    
3780  sub ParseAssignment {  sub _ParseAssignment {
3781          # Get the parameters.          # Get the parameters.
3782          my ($text) = @_;      my ($user, $text) = @_;
3783          # Declare the return value.          # Declare the return value.
3784          my @retVal = ();          my @retVal = ();
3785          # Check to see if this is a functional assignment.          # Check to see if this is a functional assignment.
3786          my ($user, $type, $function) = split(/\n/, $text);      my ($type, $function) = split(/\n/, $text);
3787          if ($type =~ m/^set $user function to$/i) {      if ($type =~ m/^set function to$/i) {
3788                  # 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.
3789                  @retVal = ($user, $function);                  @retVal = ($user, $function);
3790        } elsif ($type =~ m/^set (\S+) function to$/i) {
3791            # Here we have an assignment with a user that is passed back to the caller.
3792            @retVal = ($1, $function);
3793        }
3794        # If we have an assignment, we need to clean the function text. There may be
3795        # extra junk at the end added as a note from the user.
3796        if (defined( $retVal[1] )) {
3797            $retVal[1] =~ s/(\t\S)?\s*$//;
3798          }          }
3799          # Return the result list.          # Return the result list.
3800          return @retVal;          return @retVal;
# Line 2688  Line 3822 
3822    
3823  sub FriendlyTimestamp {  sub FriendlyTimestamp {
3824      my ($timeValue) = @_;      my ($timeValue) = @_;
3825      my $retVal = strftime("%a %b %e %H:%M:%S %Y", localtime($timeValue));      my $retVal = localtime($timeValue);
3826      return $retVal;      return $retVal;
3827  }  }
3828    
3829    
3830  1;  1;

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.93

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3