[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.61, Wed Jun 14 20:15:48 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 Stats;          use Stats;
# Line 32  Line 34 
34  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
35  L</dna_seq> returns the DNA sequence for a specified genome location.  L</dna_seq> returns the DNA sequence for a specified genome location.
36    
37    The Sprout object is a subclass of the ERDB object and inherits all its properties and methods.
38    
39  =cut  =cut
40    
41  #: Constructor SFXlate->new_sprout_only();  #: Constructor SFXlate->new_sprout_only();
# Line 62  Line 66 
66    
67  * 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>)
68    
69  * 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)
70    
71  * B<port> connection port (default C<0>)  * B<port> connection port (default C<0>)
72    
73    * B<sock> connection socket (default same as SEED)
74    
75  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)
76    
77  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)
78    
79    * B<noDBOpen> suppresses the connection to the database if TRUE, else FALSE
80    
81  =back  =back
82    
83  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 86  Line 94 
94          # 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
95          # the incoming data.          # the incoming data.
96          my $optionTable = Tracer::GetOptions({          my $optionTable = Tracer::GetOptions({
97                                             dbType               => 'mysql',                     # database type                         dbType       => $FIG_Config::dbms,
98                                             dataDir              => 'Data',                      # data file directory                                                          # database type
99                                             xmlFileName  => 'SproutDBD.xml', # database definition file name                         dataDir      => $FIG_Config::sproutData,
100                                             userData             => 'root/',                     # user name and password                                                          # data file directory
101                                             port                 => 0,                           # database connection port                         xmlFileName  => "$FIG_Config::fig/SproutDBD.xml",
102                                                            # database definition file name
103                           userData     => "$FIG_Config::dbuser/$FIG_Config::dbpass",
104                                                            # user name and password
105                           port         => $FIG_Config::dbport,
106                                                            # database connection port
107                           sock         => $FIG_Config::dbsock,
108                                             maxSegmentLength => 4500,            # maximum feature segment length                                             maxSegmentLength => 4500,            # maximum feature segment length
109                                             maxSequenceLength => 8000,           # maximum contig sequence length                                             maxSequenceLength => 8000,           # maximum contig sequence length
110                           noDBOpen     => 0,               # 1 to suppress the database open
111                                            }, $options);                                            }, $options);
112          # Get the data directory.          # Get the data directory.
113          my $dataDir = $optionTable->{dataDir};          my $dataDir = $optionTable->{dataDir};
# Line 100  Line 115 
115          $optionTable->{userData} =~ m!([^/]*)/(.*)$!;          $optionTable->{userData} =~ m!([^/]*)/(.*)$!;
116          my ($userName, $password) = ($1, $2);          my ($userName, $password) = ($1, $2);
117          # Connect to the database.          # Connect to the database.
118          my $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName, $password, $optionTable->{port});      my $dbh;
119        if (! $optionTable->{noDBOpen}) {
120            $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,
121                                    $password, $optionTable->{port}, undef, $optionTable->{sock});
122        }
123          # Create the ERDB object.          # Create the ERDB object.
124          my $xmlFileName = "$optionTable->{xmlFileName}";          my $xmlFileName = "$optionTable->{xmlFileName}";
125          my $erdb = ERDB->new($dbh, $xmlFileName);      my $retVal = ERDB::new($class, $dbh, $xmlFileName);
126          # Create this object.      # Add the option table and XML file name.
127          my $self = { _erdb => $erdb, _options => $optionTable, _xmlName => $xmlFileName };      $retVal->{_options} = $optionTable;
128          # Bless and return it.      $retVal->{_xmlName} = $xmlFileName;
129          bless $self;      # Return it.
130          return $self;      return $retVal;
131  }  }
132    
133  =head3 MaxSegment  =head3 MaxSegment
# Line 143  Line 162 
162          return $self->{_options}->{maxSequenceLength};          return $self->{_options}->{maxSequenceLength};
163  }  }
164    
165  =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]); >>  
166    
167  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.  
168    
169  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.
170    
171  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
172  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
173  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
174    extension are used in preference to the files with an extension.
175    
176  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
177  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
178  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
179  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.  
180    
181  =over 4  =over 4
182    
183  =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  
184    
185  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
186    
187  =item RETURN  =item RETURN
188    
189  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,
190    the number of errors, and a list of the error messages.
191    
192  =back  =back
193    
194  =cut  =cut
195    #: Return Type %;
196  sub Get {  sub Load {
197          # Get the parameters.          # Get the parameters.
198          my ($self, $objectNames, $filterClause, $parameterList) = @_;      my ($self, $rebuild) = @_;
199          # 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.
200          # 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);
201          # to a real list. We can only do this if the parameters have been specified.      # Return the statistics.
202          my @parameters;      return $retVal;
         if ($parameterList) { @parameters = @{$parameterList}; }  
         return $self->{_erdb}->Get($objectNames, $filterClause, @parameters);  
203  }  }
204    
205  =head3 GetEntity  =head3 LoadUpdate
206    
207  C<< my $entityObject = $sprout->GetEntity($entityType, $ID); >>  C<< my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>
208    
209  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
210    or two tables without reloading the whole database. For each table, there must be a corresponding
211    file in the data directory, either with the same name as the table, or with a C<.dtx> suffix. So,
212    for example, to make updates to the B<FeatureTranslation> relation, there must be a
213    C<FeatureTranslation.dtx> file in the data directory. Unlike a full load, files without an extension
214    are not examined. This allows update files to co-exist with files from an original load.
215    
216  =over 4  =over 4
217    
218  =item entityType  =item truncateFlag
219    
220  Entity type name.  TRUE if the tables should be rebuilt before loading, else FALSE. A value of TRUE therefore causes
221    current data and schema of the tables to be replaced, while a value of FALSE means the new data
222    is added to the existing data in the various relations.
223    
224  =item ID  =item tableList
225    
226  ID of the desired entity.  List of the tables to be updated.
227    
228  =item RETURN  =item RETURN
229    
230  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,
231  instance is found with the specified key.  the number of errors encountered, and a list of error messages.
232    
233  =back  =back
234    
235  =cut  =cut
236    #: Return Type $%;
237  sub GetEntity {  sub LoadUpdate {
238          # Get the parameters.          # Get the parameters.
239          my ($self, $entityType, $ID) = @_;      my ($self, $truncateFlag, $tableList) = @_;
240          # Create a query.      # Declare the return value.
241          my $query = $self->Get([$entityType], "$entityType(id) = ?", [$ID]);      my $retVal = Stats->new();
242          # Get the first (and only) object.      # Get the data directory.
243          my $retVal = $query->Fetch();      my $optionTable = $self->{_options};
244          # Return the result.      my $dataDir = $optionTable->{dataDir};
245        # Loop through the incoming table names.
246        for my $tableName (@{$tableList}) {
247            # Find the table's file.
248            my $fileName = LoadFileName($dataDir, $tableName);
249            if (! $fileName) {
250                Trace("No load file found for $tableName in $dataDir.") if T(0);
251            } else {
252                # Attempt to load this table.
253                my $result = $self->LoadTable($fileName, $tableName, $truncateFlag);
254                # Accumulate the resulting statistics.
255                $retVal->Accumulate($result);
256            }
257        }
258        # Return the statistics.
259          return $retVal;          return $retVal;
260  }  }
261    
262  =head3 GetEntityValues  =head3 GenomeCounts
263    
264  C<< my @values = GetEntityValues($entityType, $ID, \@fields); >>  C<< my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete); >>
265    
266  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
267    genomes will be included in the counts.
268    
269  =over 4  =over 4
270    
271  =item entityType  =item complete
   
 Entity type name.  
272    
273  =item ID  TRUE if only complete genomes are to be counted, FALSE if all genomes are to be
274    counted
 ID of the desired entity.  
   
 =item fields  
   
 List of field names, each of the form I<objectName>C<(>I<fieldName>C<)>.  
275    
276  =item RETURN  =item RETURN
277    
278  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--
279    Archaea, Bacteria, Eukaryota, Viral, Environmental, and Unknown, respectively.
280    
281  =back  =back
282    
283  =cut  =cut
 #: Return Type @;  
 sub GetEntityValues {  
         # Get the parameters.  
         my ($self, $entityType, $ID, $fields) = @_;  
         # Get the specified entity.  
         my $entity = $self->GetEntity($entityType, $ID);  
         # Declare the return list.  
         my @retVal = ();  
         # If we found the entity, push the values into the return list.  
         if ($entity) {  
                 push @retVal, $entity->Values($fields);  
         }  
         # Return the result.  
         return @retVal;  
 }  
   
 =head3 ShowMetaData  
284    
285  C<< $sprout->ShowMetaData($fileName); >>  sub GenomeCounts {
   
 This method outputs a description of the database to an HTML file in the data directory.  
   
 =over 4  
   
 =item fileName  
   
 Fully-qualified name to give to the output file.  
   
 =back  
   
 =cut  
   
 sub ShowMetaData {  
286          # Get the parameters.          # Get the parameters.
287          my ($self, $fileName) = @_;      my ($self, $complete) = @_;
288          # Compute the file name.      # Set the filter based on the completeness flag.
289          my $options = $self->{_options};      my $filter = ($complete ? "Genome(complete) = 1" : "");
290          # Call the show method on the underlying ERDB object.      # Get all the genomes and the related taxonomy information.
291          $self->{_erdb}->ShowMetaData($fileName);      my @genomes = $self->GetAll(['Genome'], $filter, [], ['Genome(id)', 'Genome(taxonomy)']);
292        # Clear the counters.
293        my ($arch, $bact, $euk, $vir, $env, $unk) = (0, 0, 0, 0, 0, 0);
294        # Loop through, counting the domains.
295        for my $genome (@genomes) {
296            if    ($genome->[1] =~ /^archaea/i)  { ++$arch }
297            elsif ($genome->[1] =~ /^bacter/i)   { ++$bact }
298            elsif ($genome->[1] =~ /^eukar/i)    { ++$euk }
299            elsif ($genome->[1] =~ /^vir/i)      { ++$vir }
300            elsif ($genome->[1] =~ /^env/i)      { ++$env }
301            else  { ++$unk }
302        }
303        # Return the counts.
304        return ($arch, $bact, $euk, $vir, $env, $unk);
305  }  }
306    
307  =head3 Load  =head3 ContigCount
   
 C<< $sprout->Load($rebuild); >>;  
308    
309  Load the database from files in the data directory, optionally re-creating the tables.  C<< my $count = $sprout->ContigCount($genomeID); >>
310    
311  This method always deletes the data from the database before loading, even if the tables are not  Return the number of contigs for the specified genome ID.
 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.  
   
 The files are loaded based on the presumption that each line of the file is a record in the  
 relation, and the individual fields are delimited by tabs. Tab and new-line characters inside  
 fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must  
 be presented in the order given in the relation tables produced by the L</ShowMetaData> method.  
312    
313  =over 4  =over 4
314    
315  =item rebuild  =item genomeID
316    
317  TRUE if the data tables need to be created or re-created, else FALSE  ID of the genome whose contig count is desired.
318    
319  =item RETURN  =item RETURN
320    
321  Returns a statistical object containing the number of records read, the number of duplicates found,  Returns the number of contigs for the specified genome.
 the number of errors, and a list of the error messages.  
322    
323  =back  =back
324    
325  =cut  =cut
326  #: Return Type %;  
327  sub Load {  sub ContigCount {
328          # Get the parameters.          # Get the parameters.
329          my ($self, $rebuild) = @_;      my ($self, $genomeID) = @_;
330          # Get the database object.      # Get the contig count.
331          my $erdb = $self->{_erdb};      my $retVal = $self->GetCount(['Contig', 'HasContig'], "HasContig(from-link) = ?", [$genomeID]);
332          # Load the tables from the data directory.      # Return the result.
         my $retVal = $erdb->LoadTables($self->{_options}->{dataDir}, $rebuild);  
         # Return the statistics.  
333          return $retVal;          return $retVal;
334  }  }
335    
336  =head3 LoadUpdate  =head3 GeneMenu
337    
338  C<< my %stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>  C<< my $selectHtml = $sprout->GeneMenu(\%attributes, $filterString, \@params); >>
339    
340  Load updates to one or more database tables. This method enables the client to make changes to one  Return an HTML select menu of genomes. Each genome will be an option in the menu,
341  or two tables without reloading the whole database. For each table, there must be a corresponding  and will be displayed by name with the ID and a contig count attached. The selection
342  file in the data directory, either with the same name as the table, or with a C<.dtx> suffix. So,  value will be the genome ID. The genomes will be sorted by genus/species name.
 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.  
343    
344  =over 4  =over 4
345    
346  =item truncateFlag  =item attributes
347    
348  TRUE if the tables should be rebuilt before loading, else FALSE. A value of TRUE therefore causes  Reference to a hash mapping attributes to values for the SELECT tag generated.
 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.  
349    
350  =item tableList  =item filterString
351    
352  List of the tables to be updated.  A filter string for use in selecting the genomes. The filter string must conform
353    to the rules for the C<< ERDB->Get >> method.
354    
355    =item params
356    
357    Reference to a list of values to be substituted in for the parameter marks in
358    the filter string.
359    
360  =item RETURN  =item RETURN
361    
362  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.  
363    
364  =back  =back
365    
366  =cut  =cut
367  #: Return Type $%;  
368  sub LoadUpdate {  sub GeneMenu {
369          # Get the parameters.          # Get the parameters.
370          my ($self, $truncateFlag, $tableList) = @_;      my ($self, $attributes, $filterString, $params) = @_;
371          # Get the database object.      # Start the menu.
372          my $erdb = $self->{_erdb};      my $retVal = "<select " .
373          # Declare the return value.          join(" ", map { "$_=\"$attributes->{$_}\"" } keys %{$attributes}) .
374          my $retVal = Stats->new();          ">\n";
375          # Get the data directory.      # Get the genomes.
376          my $optionTable = $self->{_options};      my @genomes = $self->GetAll(['Genome'], $filterString, $params, ['Genome(id)',
377          my $dataDir = $optionTable->{dataDir};                                                                       'Genome(genus)',
378          # Loop through the incoming table names.                                                                       'Genome(species)']);
379          for my $tableName (@{$tableList}) {      # Sort them by name.
380                  # Find the table's file.      my @sorted = sort { lc("$a->[1] $a->[2]") cmp lc("$b->[1] $b->[2]") } @genomes;
381                  my $fileName = "$dataDir/$tableName";      # Loop through the genomes, creating the option tags.
382                  if (! -e $fileName) {      for my $genomeData (@sorted) {
383                          $fileName = "$fileName.dtx";          # Get the data for this genome.
384                  }          my ($genomeID, $genus, $species) = @{$genomeData};
385                  # Attempt to load this table.          # Get the contig count.
386                  my $result = $erdb->LoadTable($fileName, $tableName, $truncateFlag);          my $count = $self->ContigCount($genomeID);
387                  # Accumulate the resulting statistics.          my $counting = ($count == 1 ? "contig" : "contigs");
388                  $retVal->Accumulate($result);          # Build the option tag.
389            $retVal .= "<option value=\"$genomeID\">$genus $species ($genomeID) [$count $counting]</option>\n";
390          }          }
391          # Return the statistics.      # Close the SELECT tag.
392        $retVal .= "</select>\n";
393        # Return the result.
394          return $retVal;          return $retVal;
395  }  }
   
396  =head3 Build  =head3 Build
397    
398  C<< $sprout->Build(); >>  C<< $sprout->Build(); >>
# Line 459  Line 407 
407          # Get the parameters.          # Get the parameters.
408          my ($self) = @_;          my ($self) = @_;
409          # Create the tables.          # Create the tables.
410          $self->{_erdb}->CreateTables;      $self->CreateTables();
411  }  }
412    
413  =head3 Genomes  =head3 Genomes
# Line 582  Line 530 
530  =item RETURN  =item RETURN
531    
532  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
533  context and as a space-delimited string in a scalar context.  context and as a comma-delimited string in a scalar context.
534    
535  =back  =back
536    
# Line 609  Line 557 
557                  if ($prevContig eq $contigID && $dir eq $prevDir) {                  if ($prevContig eq $contigID && $dir eq $prevDir) {
558                          # 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
559                          # new segment's beginning is next to the old segment's end.                          # new segment's beginning is next to the old segment's end.
560                          if (($dir eq "-" && $beg == $prevBeg - $prevLen) ||              if ($dir eq "-" && $beg + $len == $prevBeg) {
561                                  ($dir eq "+" && $beg == $prevBeg + $prevLen)) {                  # Here we're merging two backward blocks, so we keep the new begin point
562                                  # Here we need to merge two segments. Adjust the beginning and length values                  # and adjust the length.
563                                  # to include both segments.                  $len += $prevLen;
564                    # Pop the old segment off. The new one will replace it later.
565                    pop @retVal;
566                } elsif ($dir eq "+" && $beg == $prevBeg + $prevLen) {
567                    # Here we need to merge two forward blocks. Adjust the beginning and
568                    # length values to include both segments.
569                                  $beg = $prevBeg;                                  $beg = $prevBeg;
570                                  $len += $prevLen;                                  $len += $prevLen;
571                                  # 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 574 
574                  }                  }
575                  # Remember this specifier for the adjacent-segment test the next time through.                  # Remember this specifier for the adjacent-segment test the next time through.
576                  ($prevContig, $prevBeg, $prevDir, $prevLen) = ($contigID, $beg, $dir, $len);                  ($prevContig, $prevBeg, $prevDir, $prevLen) = ($contigID, $beg, $dir, $len);
577            # Compute the initial base pair.
578            my $start = ($dir eq "+" ? $beg : $beg + $len - 1);
579                  # Add the specifier to the list.                  # Add the specifier to the list.
580                  push @retVal, "${contigID}_$beg$dir$len";          push @retVal, "${contigID}_$start$dir$len";
581          }          }
582          # Return the list in the format indicated by the context.          # Return the list in the format indicated by the context.
583          return (wantarray ? @retVal : join(' ', @retVal));      return (wantarray ? @retVal : join(',', @retVal));
584  }  }
585    
586  =head3 ParseLocation  =head3 ParseLocation
# Line 656  Line 611 
611      shift if UNIVERSAL::isa($_[0],__PACKAGE__);      shift if UNIVERSAL::isa($_[0],__PACKAGE__);
612          my ($location) = @_;          my ($location) = @_;
613          # Parse it into segments.          # Parse it into segments.
614          $location =~ /^(.*)_(\d*)([+-_])(\d*)$/;      $location =~ /^(.+)_(\d+)([+\-_])(\d+)$/;
615          my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);          my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);
616          # If the direction is an underscore, convert it to a + or -.          # If the direction is an underscore, convert it to a + or -.
617          if ($dir eq "_") {          if ($dir eq "_") {
# Line 732  Line 687 
687  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,
688  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>.
689    
690    For example, the following would return the DNA sequence for contig C<83333.1:NC_000913>
691    between positions 1401 and 1532, inclusive.
692    
693        my $sequence = $sprout->DNASeq('83333.1:NC_000913_1401_1532');
694    
695  =over 4  =over 4
696    
697  =item locationList  =item locationList
698    
699  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
700  L</FeatureLocation> for more about this format).  I<contigID>C<_>I<begin>C<_>I<end> (see L</FeatureLocation> for more about this format).
701    
702  =item RETURN  =item RETURN
703    
# Line 764  Line 724 
724                  # 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
725                  # before putting it in the return value.                  # before putting it in the return value.
726                  my ($start, $stop);                  my ($start, $stop);
727            Trace("Parse of \"$location\" is $beg$dir$len.") if T(SDNA => 4);
728                  if ($dir eq "+") {                  if ($dir eq "+") {
729                          $start = $beg;                          $start = $beg;
730                          $stop = $beg + $len - 1;                          $stop = $beg + $len - 1;
731                  } else {                  } else {
732                          $start = $beg + $len + 1;              $start = $beg - $len + 1;
733                          $stop = $beg;                          $stop = $beg;
734                  }                  }
735            Trace("Looking for sequences containing $start through $stop.") if T(SDNA => 4);
736                  my $query = $self->Get(['IsMadeUpOf','Sequence'],                  my $query = $self->Get(['IsMadeUpOf','Sequence'],
737                          "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .                          "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .
738                          " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",                          " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",
# Line 782  Line 744 
744                                  $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',                                  $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',
745                                                                     'IsMadeUpOf(len)']);                                                                     'IsMadeUpOf(len)']);
746                          my $stopPosition = $startPosition + $sequenceLength;                          my $stopPosition = $startPosition + $sequenceLength;
747                Trace("Sequence is from $startPosition to $stopPosition.") if T(SDNA => 4);
748                          # Figure out the start point and length of the relevant section.                          # Figure out the start point and length of the relevant section.
749                          my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);                          my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);
750                          my $len = ($stopPosition <= $stop ? $stopPosition : $stop) - $startPosition - $pos1;              my $len1 = ($stopPosition < $stop ? $stopPosition : $stop) + 1 - $startPosition - $pos1;
751                Trace("Position is $pos1 for length $len1.") if T(SDNA => 4);
752                          # Add the relevant data to the location data.                          # Add the relevant data to the location data.
753                          $locationDNA .= substr($sequenceData, $pos1, $len);              $locationDNA .= substr($sequenceData, $pos1, $len1);
754                  }                  }
755                  # 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.
756                  if ($dir eq '+') {                  if ($dir eq '+') {
757                          $retVal .= $locationDNA;                          $retVal .= $locationDNA;
758                  } else {                  } else {
759                          $locationDNA = join('', reverse split //, $locationDNA);              $retVal .= FIG::reverse_comp($locationDNA);
                         $retVal .= $locationDNA;  
760                  }                  }
761          }          }
762          # Return the result.          # Return the result.
# Line 863  Line 826 
826          # Set it from the sequence data, if any.          # Set it from the sequence data, if any.
827          if ($sequence) {          if ($sequence) {
828                  my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);                  my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);
829                  $retVal = $start + $len;          $retVal = $start + $len - 1;
830        }
831        # Return the result.
832        return $retVal;
833    }
834    
835    =head3 ClusterPEGs
836    
837    C<< my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs); >>
838    
839    Cluster the PEGs in a list according to the cluster coding scheme of the specified
840    subsystem. In order for this to work properly, the subsystem object must have
841    been used recently to retrieve the PEGs using the B<get_pegs_from_cell> method.
842    This causes the cluster numbers to be pulled into the subsystem's color hash.
843    If a PEG is not found in the color hash, it will not appear in the output
844    sequence.
845    
846    =over 4
847    
848    =item sub
849    
850    Sprout subsystem object for the relevant subsystem, from the L</get_subsystem>
851    method.
852    
853    =item pegs
854    
855    Reference to the list of PEGs to be clustered.
856    
857    =item RETURN
858    
859    Returns a list of the PEGs, grouped into smaller lists by cluster number.
860    
861    =back
862    
863    =cut
864    #: Return Type $@@;
865    sub ClusterPEGs {
866        # Get the parameters.
867        my ($self, $sub, $pegs) = @_;
868        # Declare the return variable.
869        my $retVal = [];
870        # Loop through the PEGs, creating arrays for each cluster.
871        for my $pegID (@{$pegs}) {
872            my $clusterNumber = $sub->get_cluster_number($pegID);
873            # Only proceed if the PEG is in a cluster.
874            if ($clusterNumber >= 0) {
875                # Push this PEG onto the sub-list for the specified cluster number.
876                push @{$retVal->[$clusterNumber]}, $pegID;
877            }
878          }          }
879          # Return the result.          # Return the result.
880          return $retVal;          return $retVal;
# Line 1013  Line 1024 
1024    
1025  =head3 FeatureAnnotations  =head3 FeatureAnnotations
1026    
1027  C<< my @descriptors = $sprout->FeatureAnnotations($featureID); >>  C<< my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag); >>
1028    
1029  Return the annotations of a feature.  Return the annotations of a feature.
1030    
# Line 1023  Line 1034 
1034    
1035  ID of the feature whose annotations are desired.  ID of the feature whose annotations are desired.
1036    
1037    =item rawFlag
1038    
1039    If TRUE, the annotation timestamps will be returned in raw form; otherwise, they
1040    will be returned in human-readable form.
1041    
1042  =item RETURN  =item RETURN
1043    
1044  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.
1045    
1046  * B<featureID> ID of the relevant feature.  * B<featureID> ID of the relevant feature.
1047    
1048  * B<timeStamp> time the annotation was made, in user-friendly format.  * B<timeStamp> time the annotation was made.
1049    
1050  * B<user> ID of the user who made the annotation  * B<user> ID of the user who made the annotation
1051    
# Line 1041  Line 1057 
1057  #: Return Type @%;  #: Return Type @%;
1058  sub FeatureAnnotations {  sub FeatureAnnotations {
1059          # Get the parameters.          # Get the parameters.
1060          my ($self, $featureID) = @_;      my ($self, $featureID, $rawFlag) = @_;
1061          # 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.
1062          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1063                                                     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);                                                     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);
# Line 1054  Line 1070 
1070                          $annotation->Values(['IsTargetOfAnnotation(from-link)',                          $annotation->Values(['IsTargetOfAnnotation(from-link)',
1071                                                                   'Annotation(time)', 'MadeAnnotation(from-link)',                                                                   'Annotation(time)', 'MadeAnnotation(from-link)',
1072                                                                   'Annotation(annotation)']);                                                                   'Annotation(annotation)']);
1073            # Convert the time, if necessary.
1074            if (! $rawFlag) {
1075                $timeStamp = FriendlyTimestamp($timeStamp);
1076            }
1077                  # Assemble them into a hash.                  # Assemble them into a hash.
1078          my $annotationHash = { featureID => $featureID,          my $annotationHash = { featureID => $featureID,
1079                                 timeStamp => FriendlyTimestamp($timeStamp),                                 timeStamp => $timeStamp,
1080                                                             user => $user, text => $text };                                                             user => $user, text => $text };
1081                  # Add it to the return list.                  # Add it to the return list.
1082                  push @retVal, $annotationHash;                  push @retVal, $annotationHash;
# Line 1070  Line 1090 
1090  C<< my %functions = $sprout->AllFunctionsOf($featureID); >>  C<< my %functions = $sprout->AllFunctionsOf($featureID); >>
1091    
1092  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
1093  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,
1094  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
1095  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,
1096  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.
1097  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
1098  functional assignments, we will only keep the most recent one.  recent one.
1099    
1100  =over 4  =over 4
1101    
# Line 1085  Line 1105 
1105    
1106  =item RETURN  =item RETURN
1107    
1108  Returns a hash mapping the functional assignment IDs to user IDs.  Returns a hash mapping the user IDs to functional assignment IDs.
1109    
1110  =back  =back
1111    
# Line 1095  Line 1115 
1115          # Get the parameters.          # Get the parameters.
1116          my ($self, $featureID) = @_;          my ($self, $featureID) = @_;
1117          # Get all of the feature's annotations.          # Get all of the feature's annotations.
1118      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation'],      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1119                                                      "IsTargetOfAnnotation(from-link) = ?",                                                      "IsTargetOfAnnotation(from-link) = ?",
1120                              [$featureID], ['Annotation(time)', 'Annotation(annotation)']);                              [$featureID], ['Annotation(time)', 'Annotation(annotation)',
1121                                               'MadeAnnotation(from-link)']);
1122          # Declare the return hash.          # Declare the return hash.
1123          my %retVal;          my %retVal;
     # Declare a hash for insuring we only make one assignment per user.  
     my %timeHash = ();  
1124      # Now we sort the assignments by timestamp in reverse.      # Now we sort the assignments by timestamp in reverse.
1125      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;
1126          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1127      for my $annotation (@sortedQuery) {      for my $annotation (@sortedQuery) {
1128          # Get the annotation fields.          # Get the annotation fields.
1129          my ($timeStamp, $text) = @{$annotation};          my ($timeStamp, $text, $user) = @{$annotation};
1130                  # Check to see if this is a functional assignment.                  # Check to see if this is a functional assignment.
1131                  my ($user, $function) = ParseAssignment($text);          my ($actualUser, $function) = _ParseAssignment($user, $text);
1132          if ($user && ! exists $timeHash{$user}) {          if ($actualUser && ! exists $retVal{$actualUser}) {
1133              # Here it is a functional assignment and there has been no              # Here it is a functional assignment and there has been no
1134              # previous assignment for this user, so we stuff it in the              # previous assignment for this user, so we stuff it in the
1135              # return hash.              # return hash.
1136                          $retVal{$function} = $user;              $retVal{$actualUser} = $function;
             # Insure we don't assign to this user again.  
             $timeHash{$user} = 1;  
1137                  }                  }
1138          }          }
1139          # Return the hash of assignments found.          # Return the hash of assignments found.
# Line 1131  Line 1148 
1148    
1149  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
1150  the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional  the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional
1151  assignment is a type of annotation. It has the format "XXXX\nset XXXX function to\nYYYYY". In this  assignment is a type of annotation. The format of an assignment is described in
1152  instance, XXXX is the user ID and YYYYY is the functional assignment text. Its worth noting that  L</ParseAssignment>. Its worth noting that we cannot filter on the content of the
1153  we cannot filter on the content of the annotation itself because it's a text field; however, this  annotation itself because it's a text field; however, this is not a big problem because
1154  is not a big problem because most features only have a small number of annotations.  most features only have a small number of annotations.
1155    
1156  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
1157  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 1194  Line 1211 
1211              }              }
1212          }          }
1213          # 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.
1214          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation'],          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1215                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1216                                 [$featureID]);                                 [$featureID]);
1217          my $timeSelected = 0;          my $timeSelected = 0;
1218          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1219          while (my $annotation = $query->Fetch()) {          while (my $annotation = $query->Fetch()) {
1220              # Get the annotation text.              # Get the annotation text.
1221              my ($text, $time) = $annotation->Values(['Annotation(annotation)','Annotation(time)']);              my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1222                                                         'Annotation(time)', 'MadeAnnotation(from-link)']);
1223              # 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.
1224              my ($user, $type, $function) = split(/\n/, $text);              my ($actualUser, $function) = _ParseAssignment($user, $text);
1225              if ($type =~ m/^set $user function to$/i) {              Trace("Assignment user is $actualUser, text is $function.") if T(4);
1226                if ($actualUser) {
1227                  # Here it is a functional assignment. Check the time and the user                  # Here it is a functional assignment. Check the time and the user
1228                  # name. The time must be recent and the user must be trusted.                  # name. The time must be recent and the user must be trusted.
1229                  if ((exists $trusteeTable{$user}) && ($time > $timeSelected)) {                  if ((exists $trusteeTable{$actualUser}) && ($time > $timeSelected)) {
1230                      $retVal = $function;                      $retVal = $function;
1231                      $timeSelected = $time;                      $timeSelected = $time;
1232                  }                  }
# Line 1223  Line 1242 
1242          return $retVal;          return $retVal;
1243  }  }
1244    
1245    =head3 FunctionsOf
1246    
1247    C<< my @functionList = $sprout->FunctionOf($featureID, $userID); >>
1248    
1249    Return the functional assignments of a particular feature.
1250    
1251    The functional assignment is handled differently depending on the type of feature. If
1252    the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional
1253    assignment is a type of annotation. The format of an assignment is described in
1254    L</ParseAssignment>. Its worth noting that we cannot filter on the content of the
1255    annotation itself because it's a text field; however, this is not a big problem because
1256    most features only have a small number of annotations.
1257    
1258    If the feature is B<not> identified by a FIG ID, then the functional assignment
1259    information is taken from the B<ExternalAliasFunc> table. If the table does
1260    not contain an entry for the feature, an empty list is returned.
1261    
1262    =over 4
1263    
1264    =item featureID
1265    
1266    ID of the feature whose functional assignments are desired.
1267    
1268    =item RETURN
1269    
1270    Returns a list of 2-tuples, each consisting of a user ID and the text of an assignment by
1271    that user.
1272    
1273    =back
1274    
1275    =cut
1276    #: Return Type @@;
1277    sub FunctionsOf {
1278        # Get the parameters.
1279        my ($self, $featureID) = @_;
1280        # Declare the return value.
1281        my @retVal = ();
1282        # Determine the ID type.
1283        if ($featureID =~ m/^fig\|/) {
1284            # Here we have a FIG feature ID. We must build the list of trusted
1285            # users.
1286            my %trusteeTable = ();
1287            # Build a query for all of the feature's annotations, sorted by date.
1288            my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1289                                   "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1290                                   [$featureID]);
1291            my $timeSelected = 0;
1292            # Loop until we run out of annotations.
1293            while (my $annotation = $query->Fetch()) {
1294                # Get the annotation text.
1295                my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1296                                                                'Annotation(time)',
1297                                                                'MadeAnnotation(user)']);
1298                # Check to see if this is a functional assignment for a trusted user.
1299                my ($actualUser, $function) = _ParseAssignment($user, $text);
1300                if ($actualUser) {
1301                    # Here it is a functional assignment.
1302                    push @retVal, [$actualUser, $function];
1303                }
1304            }
1305        } else {
1306            # Here we have a non-FIG feature ID. In this case the user ID does not
1307            # matter. We simply get the information from the External Alias Function
1308            # table.
1309            my @assignments = $self->GetEntityValues('ExternalAliasFunc', $featureID,
1310                                                     ['ExternalAliasFunc(func)']);
1311            push @retVal, map { ['master', $_] } @assignments;
1312        }
1313        # Return the assignments found.
1314        return @retVal;
1315    }
1316    
1317  =head3 BBHList  =head3 BBHList
1318    
1319  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>
# Line 1242  Line 1333 
1333    
1334  =item RETURN  =item RETURN
1335    
1336  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
1337  their best hits.  on the target genome.
1338    
1339  =back  =back
1340    
# Line 1260  Line 1351 
1351                  my $query = $self->Get(['IsBidirectionalBestHitOf'],                  my $query = $self->Get(['IsBidirectionalBestHitOf'],
1352                                                             "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?",                                                             "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?",
1353                                                             [$featureID, $genomeID]);                                                             [$featureID, $genomeID]);
1354                  # Look for the best hit.          # Peel off the BBHs found.
1355                  my $bbh = $query->Fetch;          my @found = ();
1356                  if ($bbh) {          while (my $bbh = $query->Fetch) {
1357                          my ($targetFeature) = $bbh->Value('IsBidirectionalBestHitOf(to-link)');              push @found, $bbh->Value('IsBidirectionalBestHitOf(to-link)');
                         $retVal{$featureID} = $targetFeature;  
1358                  }                  }
1359            $retVal{$featureID} = \@found;
1360          }          }
1361          # Return the mapping.          # Return the mapping.
1362          return \%retVal;          return \%retVal;
1363  }  }
1364    
1365    =head3 SimList
1366    
1367    C<< my %similarities = $sprout->SimList($featureID, $count); >>
1368    
1369    Return a list of the similarities to the specified feature.
1370    
1371    Sprout does not support real similarities, so this method just returns the bidirectional
1372    best hits.
1373    
1374    =over 4
1375    
1376    =item featureID
1377    
1378    ID of the feature whose similarities are desired.
1379    
1380    =item count
1381    
1382    Maximum number of similar features to be returned, or C<0> to return them all.
1383    
1384    =back
1385    
1386    =cut
1387    #: Return Type %;
1388    sub SimList {
1389        # Get the parameters.
1390        my ($self, $featureID, $count) = @_;
1391        # Ask for the best hits.
1392        my @lists = $self->GetAll(['IsBidirectionalBestHitOf'],
1393                                  "IsBidirectionalBestHitOf(from-link) = ? ORDER BY IsBidirectionalBestHitOf(score) DESC",
1394                                  [$featureID], ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(score)'],
1395                                  $count);
1396        # Create the return value.
1397        my %retVal = ();
1398        for my $tuple (@lists) {
1399            $retVal{$tuple->[0]} = $tuple->[1];
1400        }
1401        # Return the result.
1402        return %retVal;
1403    }
1404    
1405    
1406    
1407    =head3 IsComplete
1408    
1409    C<< my $flag = $sprout->IsComplete($genomeID); >>
1410    
1411    Return TRUE if the specified genome is complete, else FALSE.
1412    
1413    =over 4
1414    
1415    =item genomeID
1416    
1417    ID of the genome whose completeness status is desired.
1418    
1419    =item RETURN
1420    
1421    Returns TRUE if the genome is complete, FALSE if it is incomplete, and C<undef> if it is
1422    not found.
1423    
1424    =back
1425    
1426    =cut
1427    #: Return Type $;
1428    sub IsComplete {
1429        # Get the parameters.
1430        my ($self, $genomeID) = @_;
1431        # Declare the return variable.
1432        my $retVal;
1433        # Get the genome's data.
1434        my $genomeData = $self->GetEntity('Genome', $genomeID);
1435        if ($genomeData) {
1436            # The genome exists, so get the completeness flag.
1437            ($retVal) = $genomeData->Value('Genome(complete)');
1438        }
1439        # Return the result.
1440        return $retVal;
1441    }
1442    
1443  =head3 FeatureAliases  =head3 FeatureAliases
1444    
1445  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>
# Line 1305  Line 1474 
1474    
1475  C<< my $genomeID = $sprout->GenomeOf($featureID); >>  C<< my $genomeID = $sprout->GenomeOf($featureID); >>
1476    
1477  Return the genome that contains a specified feature.  Return the genome that contains a specified feature or contig.
1478    
1479  =over 4  =over 4
1480    
1481  =item featureID  =item featureID
1482    
1483  ID of the feature whose genome is desired.  ID of the feature or contig whose genome is desired.
1484    
1485  =item RETURN  =item RETURN
1486    
1487  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
1488  an undefined value.  found, returns an undefined value.
1489    
1490  =back  =back
1491    
# Line 1325  Line 1494 
1494  sub GenomeOf {  sub GenomeOf {
1495          # Get the parameters.          # Get the parameters.
1496          my ($self, $featureID) = @_;          my ($self, $featureID) = @_;
1497          # Create a query to find the genome associated with the feature.      # Create a query to find the genome associated with the incoming ID.
1498          my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ?", [$featureID]);      my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ? OR HasContig(to-link) = ?",
1499                               [$featureID, $featureID]);
1500          # Declare the return value.          # Declare the return value.
1501          my $retVal;          my $retVal;
1502          # Get the genome ID.          # Get the genome ID.
# Line 1341  Line 1511 
1511    
1512  C<< my %coupleHash = $sprout->CoupledFeatures($featureID); >>  C<< my %coupleHash = $sprout->CoupledFeatures($featureID); >>
1513    
1514  Return the features functionally coupled with a specified feature. Features are considered  Return the features functionally coupled with a specified feature. Features are considered
1515  functionally coupled if they tend to be clustered on the same chromosome.  functionally coupled if they tend to be clustered on the same chromosome.
1516    
1517    =over 4
1518    
1519    =item featureID
1520    
1521    ID of the feature whose functionally-coupled brethren are desired.
1522    
1523    =item RETURN
1524    
1525    A hash mapping the functionally-coupled feature IDs to the coupling score.
1526    
1527    =back
1528    
1529    =cut
1530    #: Return Type %;
1531    sub CoupledFeatures {
1532        # Get the parameters.
1533        my ($self, $featureID) = @_;
1534        # Create a query to retrieve the functionally-coupled features.
1535        my $query = $self->Get(['ParticipatesInCoupling', 'Coupling'],
1536                               "ParticipatesInCoupling(from-link) = ?", [$featureID]);
1537        # This value will be set to TRUE if we find at least one coupled feature.
1538        my $found = 0;
1539        # Create the return hash.
1540        my %retVal = ();
1541        # Retrieve the relationship records and store them in the hash.
1542        while (my $clustering = $query->Fetch()) {
1543            # Get the ID and score of the coupling.
1544            my ($couplingID, $score) = $clustering->Values(['Coupling(id)',
1545                                                            'Coupling(score)']);
1546            # Get the other feature that participates in the coupling.
1547            my ($otherFeatureID) = $self->GetFlat(['ParticipatesInCoupling'],
1548                                               "ParticipatesInCoupling(to-link) = ? AND ParticipatesInCoupling(from-link) <> ?",
1549                                               [$couplingID, $featureID], 'ParticipatesInCoupling(from-link)');
1550            # Attach the other feature's score to its ID.
1551            $retVal{$otherFeatureID} = $score;
1552            $found = 1;
1553        }
1554        # Functional coupling is reflexive. If we found at least one coupled feature, we must add
1555        # the incoming feature as well.
1556        if ($found) {
1557            $retVal{$featureID} = 9999;
1558        }
1559        # Return the hash.
1560        return %retVal;
1561    }
1562    
1563    =head3 CouplingEvidence
1564    
1565    C<< my @evidence = $sprout->CouplingEvidence($peg1, $peg2); >>
1566    
1567    Return the evidence for a functional coupling.
1568    
1569    A pair of features is considered evidence of a coupling between two other
1570    features if they occur close together on a contig and both are similar to
1571    the coupled features. So, if B<A1> and B<A2> are close together on a contig,
1572    B<B1> and B<B2> are considered evidence for the coupling if (1) B<B1> and
1573    B<B2> are close together, (2) B<B1> is similar to B<A1>, and (3) B<B2> is
1574    similar to B<A2>.
1575    
1576    The score of a coupling is determined by the number of pieces of evidence
1577    that are considered I<representative>. If several evidence items belong to
1578    a group of genomes that are close to each other, only one of those items
1579    is considered representative. The other evidence items are presumed to be
1580    there because of the relationship between the genomes rather than because
1581    the two proteins generated by the features have a related functionality.
1582    
1583    Each evidence item is returned as a three-tuple in the form C<[>I<$peg1a>C<,>
1584    I<$peg2a>C<,> I<$rep>C<]>, where I<$peg1a> is similar to I<$peg1>, I<$peg2a>
1585    is similar to I<$peg2>, and I<$rep> is TRUE if the evidence is representative
1586    and FALSE otherwise.
1587    
1588    =over 4
1589    
1590    =item peg1
1591    
1592    ID of the feature of interest.
1593    
1594    =item peg2
1595    
1596    ID of a feature functionally coupled to the feature of interest.
1597    
1598    =item RETURN
1599    
1600    Returns a list of 3-tuples. Each tuple consists of a feature similar to the feature
1601    of interest, a feature similar to the functionally coupled feature, and a flag
1602    that is TRUE for a representative piece of evidence and FALSE otherwise.
1603    
1604    =back
1605    
1606    =cut
1607    #: Return Type @@;
1608    sub CouplingEvidence {
1609        # Get the parameters.
1610        my ($self, $peg1, $peg2) = @_;
1611        # Declare the return variable.
1612        my @retVal = ();
1613        # Our first task is to find out the nature of the coupling: whether or not
1614        # it exists, its score, and whether the features are stored in the same
1615        # order as the ones coming in.
1616        my ($couplingID, $inverted, $score) = $self->GetCoupling($peg1, $peg2);
1617        # Only proceed if a coupling exists.
1618        if ($couplingID) {
1619            # Determine the ordering to place on the evidence items. If we're
1620            # inverted, we want to see feature 2 before feature 1 (descending); otherwise,
1621            # we want feature 1 before feature 2 (normal).
1622            Trace("Coupling evidence for ($peg1, $peg2) with inversion flag $inverted.") if T(Coupling => 4);
1623            my $ordering = ($inverted ? "DESC" : "");
1624            # Get the coupling evidence.
1625            my @evidenceList = $self->GetAll(['IsEvidencedBy', 'PCH', 'UsesAsEvidence'],
1626                                              "IsEvidencedBy(from-link) = ? ORDER BY PCH(id), UsesAsEvidence(pos) $ordering",
1627                                              [$couplingID],
1628                                              ['PCH(used)', 'UsesAsEvidence(to-link)']);
1629            # Loop through the evidence items. Each piece of evidence is represented by two
1630            # positions in the evidence list, one for each feature on the other side of the
1631            # evidence link. If at some point we want to generalize to couplings with
1632            # more than two positions, this section of code will need to be re-done.
1633            while (@evidenceList > 0) {
1634                my $peg1Data = shift @evidenceList;
1635                my $peg2Data = shift @evidenceList;
1636                Trace("Peg 1 is " . $peg1Data->[1] . " and Peg 2 is " . $peg2Data->[1] . ".") if T(Coupling => 4);
1637                push @retVal, [$peg1Data->[1], $peg2Data->[1], $peg1Data->[0]];
1638            }
1639            Trace("Last index in evidence result is is $#retVal.") if T(Coupling => 4);
1640        }
1641        # Return the result.
1642        return @retVal;
1643    }
1644    
1645    =head3 GetCoupling
1646    
1647    C<< my ($couplingID, $inverted, $score) = $sprout->GetCoupling($peg1, $peg2); >>
1648    
1649    Return the coupling (if any) for the specified pair of PEGs. If a coupling
1650    exists, we return the coupling ID along with an indicator of whether the
1651    coupling is stored as C<(>I<$peg1>C<, >I<$peg2>C<)> or C<(>I<$peg2>C<, >I<$peg1>C<)>.
1652    In the second case, we say the coupling is I<inverted>. The importance of an
1653    inverted coupling is that the PEGs in the evidence will appear in reverse order.
1654    
1655  =over 4  =over 4
1656    
1657  =item featureID  =item peg1
1658    
1659  ID of the feature whose functionally-coupled brethren are desired.  ID of the feature of interest.
1660    
1661    =item peg2
1662    
1663    ID of the potentially coupled feature.
1664    
1665  =item RETURN  =item RETURN
1666    
1667  A hash mapping the functionally-coupled feature IDs to the coupling score.  Returns a three-element list. The first element contains the database ID of
1668    the coupling. The second element is FALSE if the coupling is stored in the
1669    database in the caller specified order and TRUE if it is stored in the
1670    inverted order. The third element is the coupling's score. If the coupling
1671    does not exist, all three list elements will be C<undef>.
1672    
1673  =back  =back
1674    
1675  =cut  =cut
1676  #: Return Type %;  #: Return Type $%@;
1677  sub CoupledFeatures {  sub GetCoupling {
1678          # Get the parameters.          # Get the parameters.
1679          my ($self, $featureID) = @_;      my ($self, $peg1, $peg2) = @_;
1680          # Create a query to retrieve the functionally-coupled features. Note that we depend on the      # Declare the return values. We'll start with the coupling ID and undefine the
1681          # fact that the functional coupling is physically paired. If (A,B) is in the database, then      # flag and score until we have more information.
1682          # (B,A) will also be found.      my ($retVal, $inverted, $score) = (CouplingID($peg1, $peg2), undef, undef);
1683          my $query = $self->Get(['IsClusteredOnChromosomeWith'],      # Find the coupling data.
1684                                                     "IsClusteredOnChromosomeWith(from-link) = ?", [$featureID]);      my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'],
1685          # This value will be set to TRUE if we find at least one coupled feature.                                   "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)",
1686          my $found = 0;                                   [$retVal], ["ParticipatesInCoupling(from-link)", "Coupling(score)"]);
1687          # Create the return hash.      # Check to see if we found anything.
1688          my %retVal = ();      if (!@pegs) {
1689          # Retrieve the relationship records and store them in the hash.          Trace("No coupling found.") if T(Coupling => 4);
1690          while (my $clustering = $query->Fetch()) {          # No coupling, so undefine the return value.
1691                  my ($otherFeatureID, $score) = $clustering->Values(['IsClusteredOnChromosomeWith(to-link)',          $retVal = undef;
1692                                                                      'IsClusteredOnChromosomeWith(score)']);      } else {
1693                  $retVal{$otherFeatureID} = $score;          # We have a coupling! Get the score and check for inversion.
1694                  $found = 1;          $score = $pegs[0]->[1];
1695          }          my $firstFound = $pegs[0]->[0];
1696          # Functional coupling is reflexive. If we found at least one coupled feature, we must add          $inverted = ($firstFound ne $peg1);
1697          # the incoming feature as well.          Trace("Coupling score is $score. First peg is $firstFound, peg 1 is $peg1.") if T(Coupling => 4);
         if ($found) {  
                 $retVal{$featureID} = 9999;  
1698      }      }
1699          # Return the hash.      # Return the result.
1700          return %retVal;      return ($retVal, $inverted, $score);
1701  }  }
1702    
1703  =head3 GetEntityTypes  =head3 CouplingID
1704    
1705    C<< my $couplingID = Sprout::CouplingID($peg1, $peg2); >>
1706    
1707    Return the coupling ID for a pair of feature IDs.
1708    
1709    The coupling ID is currently computed by joining the feature IDs in
1710    sorted order with a space. Client modules (that is, modules which
1711    use Sprout) should not, however, count on this always being the
1712    case. This method provides a way for abstracting the concept of a
1713    coupling ID. All that we know for sure about it is that it can be
1714    generated easily from the feature IDs and the order of the IDs
1715    in the parameter list does not matter (i.e. C<CouplingID("a1", "b1")>
1716    will have the same value as C<CouplingID("b1", "a1")>.
1717    
1718    =over 4
1719    
1720    =item peg1
1721    
1722    First feature of interest.
1723    
1724    =item peg2
1725    
1726    Second feature of interest.
1727    
1728  C<< my @entityList = $sprout->GetEntityTypes(); >>  =item RETURN
1729    
1730    Returns the ID that would be used to represent a functional coupling of
1731    the two specified PEGs.
1732    
1733  Return the list of supported entity types.  =back
1734    
1735  =cut  =cut
1736  #: Return Type @;  #: Return Type $;
1737  sub GetEntityTypes {  sub CouplingID {
1738          # Get the parameters.      return join " ", sort @_;
         my ($self) = @_;  
         # Get the underlying database object.  
         my $erdb = $self->{_erdb};  
         # Get its entity type list.  
         my @retVal = $erdb->GetEntityTypes();  
1739  }  }
1740    
1741  =head3 ReadFasta  =head3 ReadFasta
# Line 1447  Line 1782 
1782                  if ($line =~ m/^>\s*(.+?)(\s|\n)/) {                  if ($line =~ m/^>\s*(.+?)(\s|\n)/) {
1783                          # 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.
1784                          if ($id) {                          if ($id) {
1785                                  $retVal{$id} = $sequence;                  $retVal{$id} = lc $sequence;
1786                          }                          }
1787                          # Clear the sequence accumulator and save the new ID.                          # Clear the sequence accumulator and save the new ID.
1788                          ($id, $sequence) = ("$prefix$1", "");                          ($id, $sequence) = ("$prefix$1", "");
1789                  } else {                  } else {
1790                          # 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.
1791                          # First, we get the actual data out.              # First, we get the actual data out. Note that we normalize to lower
1792                # case.
1793                          $line =~ /^\s*(.*?)(\s|\n)/;                          $line =~ /^\s*(.*?)(\s|\n)/;
1794                          $sequence .= $1;                          $sequence .= $1;
1795                  }                  }
1796          }          }
1797          # Flush out the last sequence (if any).          # Flush out the last sequence (if any).
1798          if ($sequence) {          if ($sequence) {
1799                  $retVal {$id} = $sequence;          $retVal{$id} = lc $sequence;
1800          }          }
1801        # Close the file.
1802        close FASTAFILE;
1803          # Return the hash constructed from the file.          # Return the hash constructed from the file.
1804          return %retVal;          return %retVal;
1805  }  }
# Line 1545  Line 1883 
1883          # Get the data directory name.          # Get the data directory name.
1884          my $outputDirectory = $self->{_options}->{dataDir};          my $outputDirectory = $self->{_options}->{dataDir};
1885          # Dump the relations.          # Dump the relations.
1886          $self->{_erdb}->DumpRelations($outputDirectory);      $self->DumpRelations($outputDirectory);
1887  }  }
1888    
1889  =head3 XMLFileName  =head3 XMLFileName
# Line 1577  Line 1915 
1915  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
1916  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>.
1917    
1918  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'}); >>
1919    
1920  =over 4  =over 4
1921    
# Line 1597  Line 1935 
1935          # Get the parameters.          # Get the parameters.
1936          my ($self, $objectType, $fieldHash) = @_;          my ($self, $objectType, $fieldHash) = @_;
1937          # Call the underlying method.          # Call the underlying method.
1938          $self->{_erdb}->InsertObject($objectType, $fieldHash);      $self->InsertObject($objectType, $fieldHash);
1939  }  }
1940    
1941  =head3 Annotate  =head3 Annotate
# Line 1656  Line 1994 
1994    
1995  =head3 AssignFunction  =head3 AssignFunction
1996    
1997  C<< my $ok = $sprout->AssignFunction($featureID, $user, $function); >>  C<< my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser); >>
1998    
1999  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
2000  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.  
2001    
2002  =over 4  =over 4
2003    
# Line 1670  Line 2007 
2007    
2008  =item user  =item user
2009    
2010  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>.
2011    
2012  =item function  =item function
2013    
2014  Text of the function being assigned.  Text of the function being assigned.
2015    
2016    =item assigningUser (optional)
2017    
2018    Name of the individual user making the assignment. If omitted, defaults to the user group.
2019    
2020  =item RETURN  =item RETURN
2021    
2022  Returns 1 if successful, 0 if an error occurred.  Returns 1 if successful, 0 if an error occurred.
# Line 1686  Line 2027 
2027  #: Return Type $;  #: Return Type $;
2028  sub AssignFunction {  sub AssignFunction {
2029          # Get the parameters.          # Get the parameters.
2030          my ($self, $featureID, $user, $function) = @_;      my ($self, $featureID, $user, $function, $assigningUser) = @_;
2031        # Default the assigning user.
2032        if (! $assigningUser) {
2033            $assigningUser = $user;
2034        }
2035          # Create an annotation string from the parameters.          # Create an annotation string from the parameters.
2036          my $annotationText = "$user\nset $user function to\n$function";      my $annotationText = "$assigningUser\nset $user function to\n$function";
2037          # Get the current time.          # Get the current time.
2038          my $now = time;          my $now = time;
2039          # Declare the return variable.          # Declare the return variable.
# Line 1777  Line 2122 
2122          # Get the parameters.          # Get the parameters.
2123          my ($self, $entityName, $entityID) = @_;          my ($self, $entityName, $entityID) = @_;
2124          # Check for the entity instance.          # Check for the entity instance.
2125        Trace("Checking existence of $entityName with ID=$entityID.") if T(4);
2126          my $testInstance = $self->GetEntity($entityName, $entityID);          my $testInstance = $self->GetEntity($entityName, $entityID);
2127          # Return an existence indicator.          # Return an existence indicator.
2128          my $retVal = ($testInstance ? 1 : 0);          my $retVal = ($testInstance ? 1 : 0);
# Line 1968  Line 2314 
2314          return @retVal;          return @retVal;
2315  }  }
2316    
2317    =head3 GetProperties
2318    
2319    C<< my @list = $sprout->GetProperties($fid, $key, $value, $url); >>
2320    
2321    Return a list of the properties with the specified characteristics.
2322    
2323    Properties are arbitrary key-value pairs associated with a feature. (At some point they
2324    will also be associated with genomes.) A property value is represented by a 4-tuple of
2325    the form B<($fid, $key, $value, $url)>. These exactly correspond to the parameter
2326    
2327    =over 4
2328    
2329    =item fid
2330    
2331    ID of the feature possessing the property.
2332    
2333    =item key
2334    
2335    Name or key of the property.
2336    
2337    =item value
2338    
2339    Value of the property.
2340    
2341    =item url
2342    
2343    URL of the document that indicated the property should have this particular value, or an
2344    empty string if no such document exists.
2345    
2346    =back
2347    
2348    The parameters act as a filter for the desired data. Any non-null parameter will
2349    automatically match all the tuples returned. So, specifying just the I<$fid> will
2350    return all the properties of the specified feature; similarly, specifying the I<$key>
2351    and I<$value> parameters will return all the features having the specified property
2352    value.
2353    
2354    A single property key can have many values, representing different ideas about the
2355    feature in question. For example, one paper may declare that a feature C<fig|83333.1.peg.10> is
2356    virulent, and another may declare that it is not virulent. A query about the virulence of
2357    C<fig|83333.1.peg.10> would be coded as
2358    
2359        my @list = $sprout->GetProperties('fig|83333.1.peg.10', 'virulence', '', '');
2360    
2361    Here the I<$value> and I<$url> fields are left blank, indicating that those fields are
2362    not to be filtered. The tuples returned would be
2363    
2364        ('fig|83333.1.peg.10', 'virulence', 'yes', 'http://www.somewhere.edu/first.paper.pdf')
2365        ('fig|83333.1.peg.10', 'virulence', 'no', 'http://www.somewhere.edu/second.paper.pdf')
2366    
2367    =cut
2368    #: Return Type @@;
2369    sub GetProperties {
2370        # Get the parameters.
2371        my ($self, @parms) = @_;
2372        # Declare the return variable.
2373        my @retVal = ();
2374        # Now we need to create a WHERE clause that will get us the data we want. First,
2375        # we create a list of the columns containing the data for each parameter.
2376        my @colNames = ('HasProperty(from-link)', 'Property(property-name)',
2377                        'Property(property-value)', 'HasProperty(evidence)');
2378        # Now we build the WHERE clause and the list of parameter values.
2379        my @where = ();
2380        my @values = ();
2381        for (my $i = 0; $i <= $#colNames; $i++) {
2382            my $parm = $parms[$i];
2383            if (defined $parm && ($parm ne '')) {
2384                push @where, "$colNames[$i] = ?";
2385                push @values, $parm;
2386            }
2387        }
2388        # Format the WHERE clause.
2389        my $filter = (@values > 0 ? (join " AND ", @where) : undef);
2390        # Ask for all the propertie values with the desired characteristics.
2391        my $query = $self->Get(['HasProperty', 'Property'], $filter, \@values);
2392        while (my $valueObject = $query->Fetch()) {
2393            my @tuple = $valueObject->Values(\@colNames);
2394            push @retVal, \@tuple;
2395        }
2396        # Return the result.
2397        return @retVal;
2398    }
2399    
2400  =head3 FeatureProperties  =head3 FeatureProperties
2401    
2402  C<< my @properties = $sprout->FeatureProperties($featureID); >>  C<< my @properties = $sprout->FeatureProperties($featureID); >>
# Line 2162  Line 2591 
2591  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>
2592    
2593  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
2594  to the role the feature performs.  to the roles the feature performs.
2595    
2596  =over 4  =over 4
2597    
# Line 2172  Line 2601 
2601    
2602  =item RETURN  =item RETURN
2603    
2604  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.
2605    
2606  =back  =back
2607    
2608  =cut  =cut
2609  #: Return Type %;  #: Return Type %@;
2610  sub SubsystemsOf {  sub SubsystemsOf {
2611          # Get the parameters.          # Get the parameters.
2612          my ($self, $featureID) = @_;          my ($self, $featureID) = @_;
2613          # Use the SSCell to connect features to subsystems.      # Get the subsystem list.
2614          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],
2615                                                                          "ContainsFeature(to-link) = ?", [$featureID],                                                                          "ContainsFeature(to-link) = ?", [$featureID],
2616                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);
2617          # Create the return value.          # Create the return value.
2618          my %retVal = ();          my %retVal = ();
2619        # Build a hash to weed out duplicates. Sometimes the same PEG and role appears
2620        # in two spreadsheet cells.
2621        my %dupHash = ();
2622          # Loop through the results, adding them to the hash.          # Loop through the results, adding them to the hash.
2623          for my $record (@subsystems) {          for my $record (@subsystems) {
2624                  $retVal{$record->[0]} = $record->[1];          # Get this subsystem and role.
2625            my ($subsys, $role) = @{$record};
2626            # Insure it's the first time for both.
2627            my $dupKey = "$subsys\n$role";
2628            if (! exists $dupHash{"$subsys\n$role"}) {
2629                $dupHash{$dupKey} = 1;
2630                push @{$retVal{$subsys}}, $role;
2631            }
2632          }          }
2633          # Return the hash.          # Return the hash.
2634          return %retVal;          return %retVal;
2635  }  }
2636    
2637    =head3 SubsystemList
2638    
2639    C<< my @subsystems = $sprout->SubsystemList($featureID); >>
2640    
2641    Return a list containing the names of the subsystems in which the specified
2642    feature participates. Unlike L</SubsystemsOf>, this method only returns the
2643    subsystem names, not the roles.
2644    
2645    =over 4
2646    
2647    =item featureID
2648    
2649    ID of the feature whose subsystem names are desired.
2650    
2651    =item RETURN
2652    
2653    Returns a list of the names of the subsystems in which the feature participates.
2654    
2655    =back
2656    
2657    =cut
2658    #: Return Type @;
2659    sub SubsystemList {
2660        # Get the parameters.
2661        my ($self, $featureID) = @_;
2662        # Get the list of names.
2663        my @retVal = $self->GetFlat(['ContainsFeature', 'HasSSCell'], "ContainsFeature(to-link) = ?",
2664                                    [$featureID], 'HasSSCell(from-link)');
2665        # Return the result.
2666        return @retVal;
2667    }
2668    
2669    
2670    
2671  =head3 RelatedFeatures  =head3 RelatedFeatures
2672    
2673  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>
# Line 2296  Line 2769 
2769          return @retVal;          return @retVal;
2770  }  }
2771    
 =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;  
 }  
   
2772  =head3 Protein  =head3 Protein
2773    
2774  C<< my $protein = Sprout::Protein($sequence, $table); >>  C<< my $protein = Sprout::Protein($sequence, $table); >>
# Line 2529  Line 2870 
2870          # 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.
2871          my @retVal = ($self->{_options}->{dataDir});          my @retVal = ($self->{_options}->{dataDir});
2872          # Concatenate the table names.          # Concatenate the table names.
2873          push @retVal, $self->{_erdb}->GetTableNames();      push @retVal, $self->GetTableNames();
2874          # Return the result.          # Return the result.
2875          return @retVal;          return @retVal;
2876  }  }
2877    
2878  =head3 LowBBHs  =head3 LowBBHs
2879    
2880  C<< my %bbhMap = $sprout->GoodBBHs($featureID, $cutoff); >>  C<< my %bbhMap = $sprout->LowBBHs($featureID, $cutoff); >>
2881    
2882  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
2883  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 2626  Line 2967 
2967      return %retVal;      return %retVal;
2968  }  }
2969    
2970    =head3 MyGenomes
2971    
2972    C<< my @genomes = Sprout::MyGenomes($dataDir); >>
2973    
2974    Return a list of the genomes to be included in the Sprout.
2975    
2976    This method is provided for use during the Sprout load. It presumes the Genome load file has
2977    already been created. (It will be in the Sprout data directory and called either C<Genome>
2978    or C<Genome.dtx>.) Essentially, it reads in the Genome load file and strips out the genome
2979    IDs.
2980    
2981    =over 4
2982    
2983    =item dataDir
2984    
2985    Directory containing the Sprout load files.
2986    
2987    =back
2988    
2989    =cut
2990    #: Return Type @;
2991    sub MyGenomes {
2992        # Get the parameters.
2993        my ($dataDir) = @_;
2994        # Compute the genome file name.
2995        my $genomeFileName = LoadFileName($dataDir, "Genome");
2996        # Extract the genome IDs from the files.
2997        my @retVal = map { $_ =~ /^(\S+)/; $1 } Tracer::GetFile($genomeFileName);
2998        # Return the result.
2999        return @retVal;
3000    }
3001    
3002    =head3 LoadFileName
3003    
3004    C<< my $fileName = Sprout::LoadFileName($dataDir, $tableName); >>
3005    
3006    Return the name of the load file for the specified table in the specified data
3007    directory.
3008    
3009    =over 4
3010    
3011    =item dataDir
3012    
3013    Directory containing the Sprout load files.
3014    
3015    =item tableName
3016    
3017    Name of the table whose load file is desired.
3018    
3019    =item RETURN
3020    
3021    Returns the name of the file containing the load data for the specified table, or
3022    C<undef> if no load file is present.
3023    
3024    =back
3025    
3026    =cut
3027    #: Return Type $;
3028    sub LoadFileName {
3029        # Get the parameters.
3030        my ($dataDir, $tableName) = @_;
3031        # Declare the return variable.
3032        my $retVal;
3033        # Check for the various file names.
3034        if (-e "$dataDir/$tableName") {
3035            $retVal = "$dataDir/$tableName";
3036        } elsif (-e "$dataDir/$tableName.dtx") {
3037            $retVal = "$dataDir/$tableName.dtx";
3038        }
3039        # Return the result.
3040        return $retVal;
3041    }
3042    
3043    =head3 DeleteGenome
3044    
3045    C<< my $stats = $sprout->DeleteGenome($genomeID, $testFlag); >>
3046    
3047    Delete a genome from the database.
3048    
3049    =over 4
3050    
3051    =item genomeID
3052    
3053    ID of the genome to delete
3054    
3055    =item testFlag
3056    
3057    If TRUE, then the DELETE statements will be traced, but no deletions will occur.
3058    
3059    =item RETURN
3060    
3061    Returns a statistics object describing the rows deleted.
3062    
3063    =back
3064    
3065    =cut
3066    #: Return Type $%;
3067    sub DeleteGenome {
3068        # Get the parameters.
3069        my ($self, $genomeID, $testFlag) = @_;
3070        # Perform the delete for the genome's features.
3071        my $retVal = $self->Delete('Feature', "fig|$genomeID.%", $testFlag);
3072        # Perform the delete for the primary genome data.
3073        my $stats = $self->Delete('Genome', $genomeID, $testFlag);
3074        $retVal->Accumulate($stats);
3075        # Return the result.
3076        return $retVal;
3077    }
3078    
3079  =head2 Internal Utility Methods  =head2 Internal Utility Methods
3080    
3081  =head3 ParseAssignment  =head3 ParseAssignment
3082    
3083  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,
3084  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
3085  will be returned.  isn't, an empty list will be returned.
3086    
3087    A functional assignment is always of the form
3088    
3089        C<set >I<YYYY>C< function to\n>I<ZZZZZ>
3090    
3091    where I<YYYY> is the B<user>, and I<ZZZZ> is the actual functional role. In most cases,
3092    the user and the assigning user (from MadeAnnotation) will be the same, but that is
3093    not always the case.
3094    
3095    In addition, the functional role may contain extra data that is stripped, such as
3096    terminating spaces or a comment separated from the rest of the text by a tab.
3097    
3098  This is a static method.  This is a static method.
3099    
3100  =over 4  =over 4
3101    
3102    =item user
3103    
3104    Name of the assigning user.
3105    
3106  =item text  =item text
3107    
3108  Text of the annotation.  Text of the annotation.
# Line 2651  Line 3116 
3116    
3117  =cut  =cut
3118    
3119  sub ParseAssignment {  sub _ParseAssignment {
3120          # Get the parameters.          # Get the parameters.
3121          my ($text) = @_;      my ($user, $text) = @_;
3122          # Declare the return value.          # Declare the return value.
3123          my @retVal = ();          my @retVal = ();
3124          # Check to see if this is a functional assignment.          # Check to see if this is a functional assignment.
3125          my ($user, $type, $function) = split(/\n/, $text);      my ($type, $function) = split(/\n/, $text);
3126          if ($type =~ m/^set $user function to$/i) {      if ($type =~ m/^set function to$/i) {
3127                  # 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.
3128                  @retVal = ($user, $function);                  @retVal = ($user, $function);
3129        } elsif ($type =~ m/^set (\S+) function to$/i) {
3130            # Here we have an assignment with a user that is passed back to the caller.
3131            @retVal = ($1, $function);
3132        }
3133        # If we have an assignment, we need to clean the function text. There may be
3134        # extra junk at the end added as a note from the user.
3135        if (@retVal) {
3136            $retVal[1] =~ s/(\t\S)?\s*$//;
3137          }          }
3138          # Return the result list.          # Return the result list.
3139          return @retVal;          return @retVal;
# Line 2688  Line 3161 
3161    
3162  sub FriendlyTimestamp {  sub FriendlyTimestamp {
3163      my ($timeValue) = @_;      my ($timeValue) = @_;
3164      my $retVal = strftime("%a %b %e %H:%M:%S %Y", localtime($timeValue));      my $retVal = localtime($timeValue);
3165      return $retVal;      return $retVal;
3166  }  }
3167    
3168    =head3 AddProperty
3169    
3170    C<< my  = $sprout->AddProperty($featureID, $key, $value, $url); >>
3171    
3172    Add a new attribute value (Property) to a feature. In the SEED system, attributes can
3173    be added to almost any object. In Sprout, they can only be added to features. In
3174    Sprout, attributes are implemented using I<properties>. A property represents a key/value
3175    pair. If the particular key/value pair coming in is not already in the database, a new
3176    B<Property> record is created to hold it.
3177    
3178    =over 4
3179    
3180    =item peg
3181    
3182    ID of the feature to which the attribute is to be replied.
3183    
3184    =item key
3185    
3186    Name of the attribute (key).
3187    
3188    =item value
3189    
3190    Value of the attribute.
3191    
3192    =item url
3193    
3194    URL or text citation from which the property was obtained.
3195    
3196    =back
3197    
3198    =cut
3199    #: Return Type ;
3200    sub AddProperty {
3201        # Get the parameters.
3202        my ($self, $featureID, $key, $value, $url) = @_;
3203        # Declare the variable to hold the desired property ID.
3204        my $propID;
3205        # Attempt to find a property record for this key/value pair.
3206        my @properties = $self->GetFlat(['Property'],
3207                                       "Property(property-name) = ? AND Property(property-value) = ?",
3208                                       [$key, $value], 'Property(id)');
3209        if (@properties) {
3210            # Here the property is already in the database. We save its ID.
3211            $propID = $properties[0];
3212            # Here the property value does not exist. We need to generate an ID. It will be set
3213            # to a number one greater than the maximum value in the database. This call to
3214            # GetAll will stop after one record.
3215            my @maxProperty = $self->GetAll(['Property'], "ORDER BY Property(id) DESC", [], ['Property(id)'],
3216                                            1);
3217            $propID = $maxProperty[0]->[0] + 1;
3218            # Insert the new property value.
3219            $self->Insert('Property', { 'property-name' => $key, 'property-value' => $value, id => $propID });
3220        }
3221        # Now we connect the incoming feature to the property.
3222        $self->Insert('HasProperty', { 'from-link' => $featureID, 'to-link' => $propID, evidence => $url });
3223    }
3224    
3225    
3226  1;  1;

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3