[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.87, Tue Sep 26 13:46:03 2006 UTC
# Line 1  Line 1 
1  package Sprout;  package Sprout;
2    
3        require Exporter;
4        use ERDB;
5        @ISA = qw(Exporter ERDB);
6          use Data::Dumper;          use Data::Dumper;
7          use strict;          use strict;
8          use Carp;          use Carp;
# Line 7  Line 10 
10          use XML::Simple;          use XML::Simple;
11          use DBQuery;          use DBQuery;
12          use DBObject;          use DBObject;
         use ERDB;  
13          use Tracer;          use Tracer;
14          use FIGRules;          use FIGRules;
15        use FidCheck;
16          use Stats;          use Stats;
17      use POSIX qw(strftime);      use POSIX qw(strftime);
18        use BasicLocation;
19    
20  =head1 Sprout Database Manipulation Object  =head1 Sprout Database Manipulation Object
21    
# Line 32  Line 35 
35  query tasks. For example, L</genomes> lists the IDs of all the genomes in the database and  query tasks. For example, L</genomes> lists the IDs of all the genomes in the database and
36  L</dna_seq> returns the DNA sequence for a specified genome location.  L</dna_seq> returns the DNA sequence for a specified genome location.
37    
38    The Sprout object is a subclass of the ERDB object and inherits all its properties and methods.
39    
40  =cut  =cut
41    
42  #: Constructor SFXlate->new_sprout_only();  #: Constructor SFXlate->new_sprout_only();
# Line 62  Line 67 
67    
68  * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)  * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)
69    
70  * B<userData> user name and password, delimited by a slash (default C<root/>)  * B<userData> user name and password, delimited by a slash (default same as SEED)
71    
72  * B<port> connection port (default C<0>)  * B<port> connection port (default C<0>)
73    
74    * B<sock> connection socket (default same as SEED)
75    
76  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)
77    
78  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)
79    
80    * B<noDBOpen> suppresses the connection to the database if TRUE, else FALSE
81    
82  =back  =back
83    
84  For example, the following constructor call specifies a database named I<Sprout> and a user name of  For example, the following constructor call specifies a database named I<Sprout> and a user name of
# Line 83  Line 92 
92  sub new {  sub new {
93          # Get the parameters.          # Get the parameters.
94          my ($class, $dbName, $options) = @_;          my ($class, $dbName, $options) = @_;
95        # Compute the DBD directory.
96        my $dbd_dir = (defined($FIG_Config::dbd_dir) ? $FIG_Config::dbd_dir :
97                                                      $FIG_Config::fig );
98          # Compute the options. We do this by starting with a table of defaults and overwriting with          # Compute the options. We do this by starting with a table of defaults and overwriting with
99          # the incoming data.          # the incoming data.
100          my $optionTable = Tracer::GetOptions({          my $optionTable = Tracer::GetOptions({
101                                             dbType               => 'mysql',                     # database type                         dbType       => $FIG_Config::dbms,
102                                             dataDir              => 'Data',                      # data file directory                                                          # database type
103                                             xmlFileName  => 'SproutDBD.xml', # database definition file name                         dataDir      => $FIG_Config::sproutData,
104                                             userData             => 'root/',                     # user name and password                                                          # data file directory
105                                             port                 => 0,                           # database connection port                         xmlFileName  => "$dbd_dir/SproutDBD.xml",
106                                                            # database definition file name
107                           userData     => "$FIG_Config::dbuser/$FIG_Config::dbpass",
108                                                            # user name and password
109                           port         => $FIG_Config::dbport,
110                                                            # database connection port
111                           sock         => $FIG_Config::dbsock,
112                           host         => $FIG_Config::dbhost,
113                                             maxSegmentLength => 4500,            # maximum feature segment length                                             maxSegmentLength => 4500,            # maximum feature segment length
114                                             maxSequenceLength => 8000,           # maximum contig sequence length                                             maxSequenceLength => 8000,           # maximum contig sequence length
115                           noDBOpen     => 0,               # 1 to suppress the database open
116                                            }, $options);                                            }, $options);
117          # Get the data directory.          # Get the data directory.
118          my $dataDir = $optionTable->{dataDir};          my $dataDir = $optionTable->{dataDir};
# Line 100  Line 120 
120          $optionTable->{userData} =~ m!([^/]*)/(.*)$!;          $optionTable->{userData} =~ m!([^/]*)/(.*)$!;
121          my ($userName, $password) = ($1, $2);          my ($userName, $password) = ($1, $2);
122          # Connect to the database.          # Connect to the database.
123          my $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName, $password, $optionTable->{port});      my $dbh;
124        if (! $optionTable->{noDBOpen}) {
125            $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,
126                                    $password, $optionTable->{port}, $optionTable->{host}, $optionTable->{sock});
127        }
128          # Create the ERDB object.          # Create the ERDB object.
129          my $xmlFileName = "$optionTable->{xmlFileName}";          my $xmlFileName = "$optionTable->{xmlFileName}";
130          my $erdb = ERDB->new($dbh, $xmlFileName);      my $retVal = ERDB::new($class, $dbh, $xmlFileName);
131          # Create this object.      # Add the option table and XML file name.
132          my $self = { _erdb => $erdb, _options => $optionTable, _xmlName => $xmlFileName };      $retVal->{_options} = $optionTable;
133          # Bless and return it.      $retVal->{_xmlName} = $xmlFileName;
134          bless $self;      # Set up space for the group file data.
135          return $self;      $retVal->{groupHash} = undef;
136        # Return it.
137        return $retVal;
138  }  }
139    
140  =head3 MaxSegment  =head3 MaxSegment
# Line 143  Line 169 
169          return $self->{_options}->{maxSequenceLength};          return $self->{_options}->{maxSequenceLength};
170  }  }
171    
172  =head3 Get  =head3 Load
   
 C<< my $query = $sprout->Get(\@objectNames, $filterClause, \@parameterList); >>  
   
 This method allows a general query against the Sprout data using a specified filter clause.  
   
 The filter is a standard WHERE/ORDER BY clause with question marks as parameter markers and each  
 field name represented in the form B<I<objectName>(I<fieldName>)>. For example, the  
 following call requests all B<Genome> objects for the genus specified in the variable  
 $genus.  
   
 C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ?", [$genus]); >>  
   
 The WHERE clause contains a single question mark, so there is a single additional  
 parameter representing the parameter value. It would also be possible to code  
   
 C<< $query = $sprout->Get(['Genome'], "Genome(genus) = \'$genus\'"); >>  
   
 however, this version of the call would generate a syntax error if there were any quote  
 characters inside the variable C<$genus>.  
   
 The use of the strange parenthesized notation for field names enables us to distinguish  
 hyphens contained within field names from minus signs that participate in the computation  
 of the WHERE clause. All of the methods that manipulate fields will use this same notation.  
   
 It is possible to specify multiple entity and relationship names in order to retrieve more than  
 one object's data at the same time, which allows highly complex joined queries. For example,  
   
 C<< $query = $sprout->Get(['Genome', 'ComesFrom', 'Source'], "Genome(genus) = ?", [$genus]); >>  
   
 This query returns all the genomes for a particular genus and allows access to the  
 sources from which they came. The join clauses to go from Genome to Source are generated  
 automatically.  
   
 Finally, the filter clause can contain sort information. To do this, simply put an C<ORDER BY>  
 clause at the end of the filter. Field references in the ORDER BY section follow the same rules  
 as they do in the filter itself; in other words, each one must be of the form B<I<objectName>(I<fieldName>)>.  
 For example, the following filter string gets all genomes for a particular genus and sorts  
 them by species name.  
   
 C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ? ORDER BY Genome(species)", [$genus]); >>  
173    
174  It is also permissible to specify I<only> an ORDER BY clause. For example, the following invocation gets  C<< $sprout->Load($rebuild); >>;
 all genomes ordered by genus and species.  
175    
176  C<< $query = $sprout->Get(['Genome'], "ORDER BY Genome(genus), Genome(species)"); >>  Load the database from files in the data directory, optionally re-creating the tables.
177    
178  Odd things may happen if one of the ORDER BY fields is in a secondary relation. So, for example, an  This method always deletes the data from the database before loading, even if the tables are not
179  attempt to order B<Feature>s by alias may (depending on the underlying database engine used) cause  re-created. The data is loaded into the relations from files in the data directory either having the
180  a single feature to appear more than once.  same name as the target relation with no extension or with an extension of C<.dtx>. Files without an
181    extension are used in preference to the files with an extension.
182    
183  If multiple names are specified, then the query processor will automatically determine a  The files are loaded based on the presumption that each line of the file is a record in the
184  join path between the entities and relationships. The algorithm used is very simplistic.  relation, and the individual fields are delimited by tabs. Tab and new-line characters inside
185  In particular, you can't specify any entity or relationship more than once, and if a  fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must
186  relationship is recursive, the path is determined by the order in which the entity  be presented in the order given in the relation tables produced by the L</ShowMetaData> method.
 and the relationship appear. For example, consider a recursive relationship B<IsParentOf>  
 which relates B<People> objects to other B<People> objects. If the join path is  
 coded as C<['People', 'IsParentOf']>, then the people returned will be parents. If, however,  
 the join path is C<['IsParentOf', 'People']>, then the people returned will be children.  
187    
188  =over 4  =over 4
189    
190  =item objectNames  =item rebuild
   
 List containing the names of the entity and relationship objects to be retrieved.  
   
 =item filterClause  
   
 WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can  
 be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form  
 B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the  
 parameter list as additional parameters. The fields in a filter clause can come from primary  
 entity relations, relationship relations, or secondary entity relations; however, all of the  
 entities and relationships involved must be included in the list of object names.  
   
 =item parameterList  
191    
192  List of the parameters to be substituted in for the parameters marks in the filter clause.  TRUE if the data tables need to be created or re-created, else FALSE
193    
194  =item RETURN  =item RETURN
195    
196  Returns a B<DBQuery> that can be used to iterate through all of the results.  Returns a statistical object containing the number of records read, the number of duplicates found,
197    the number of errors, and a list of the error messages.
198    
199  =back  =back
200    
201  =cut  =cut
202    #: Return Type %;
203  sub Get {  sub Load {
204          # Get the parameters.          # Get the parameters.
205          my ($self, $objectNames, $filterClause, $parameterList) = @_;      my ($self, $rebuild) = @_;
206          # We differ from the ERDB Get method in that the parameter list is passed in as a list reference      # Load the tables from the data directory.
207          # rather than a list of parameters. The next step is to convert the parameters from a reference      my $retVal = $self->LoadTables($self->{_options}->{dataDir}, $rebuild);
208          # to a real list. We can only do this if the parameters have been specified.      # Return the statistics.
209          my @parameters;      return $retVal;
         if ($parameterList) { @parameters = @{$parameterList}; }  
         return $self->{_erdb}->Get($objectNames, $filterClause, @parameters);  
210  }  }
211    
212  =head3 GetEntity  =head3 LoadUpdate
213    
214  C<< my $entityObject = $sprout->GetEntity($entityType, $ID); >>  C<< my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>
215    
216  Return an object describing the entity instance with a specified ID.  Load updates to one or more database tables. This method enables the client to make changes to one
217    or two tables without reloading the whole database. For each table, there must be a corresponding
218    file in the data directory, either with the same name as the table, or with a C<.dtx> suffix. So,
219    for example, to make updates to the B<FeatureTranslation> relation, there must be a
220    C<FeatureTranslation.dtx> file in the data directory. Unlike a full load, files without an extension
221    are not examined. This allows update files to co-exist with files from an original load.
222    
223  =over 4  =over 4
224    
225  =item entityType  =item truncateFlag
226    
227  Entity type name.  TRUE if the tables should be rebuilt before loading, else FALSE. A value of TRUE therefore causes
228    current data and schema of the tables to be replaced, while a value of FALSE means the new data
229    is added to the existing data in the various relations.
230    
231  =item ID  =item tableList
232    
233  ID of the desired entity.  List of the tables to be updated.
234    
235  =item RETURN  =item RETURN
236    
237  Returns a B<DBObject> representing the desired entity instance, or an undefined value if no  Returns a statistical object containing the number of records read, the number of duplicates found,
238  instance is found with the specified key.  the number of errors encountered, and a list of error messages.
239    
240  =back  =back
241    
242  =cut  =cut
243    #: Return Type $%;
244  sub GetEntity {  sub LoadUpdate {
245          # Get the parameters.          # Get the parameters.
246          my ($self, $entityType, $ID) = @_;      my ($self, $truncateFlag, $tableList) = @_;
247          # Create a query.      # Declare the return value.
248          my $query = $self->Get([$entityType], "$entityType(id) = ?", [$ID]);      my $retVal = Stats->new();
249          # Get the first (and only) object.      # Get the data directory.
250          my $retVal = $query->Fetch();      my $optionTable = $self->{_options};
251          # Return the result.      my $dataDir = $optionTable->{dataDir};
252        # Loop through the incoming table names.
253        for my $tableName (@{$tableList}) {
254            # Find the table's file.
255            my $fileName = LoadFileName($dataDir, $tableName);
256            if (! $fileName) {
257                Trace("No load file found for $tableName in $dataDir.") if T(0);
258            } else {
259                # Attempt to load this table.
260                my $result = $self->LoadTable($fileName, $tableName, $truncateFlag);
261                # Accumulate the resulting statistics.
262                $retVal->Accumulate($result);
263            }
264        }
265        # Return the statistics.
266          return $retVal;          return $retVal;
267  }  }
268    
269  =head3 GetEntityValues  =head3 GenomeCounts
270    
271  C<< my @values = GetEntityValues($entityType, $ID, \@fields); >>  C<< my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete); >>
272    
273  Return a list of values from a specified entity instance.  Count the number of genomes in each domain. If I<$complete> is TRUE, only complete
274    genomes will be included in the counts.
275    
276  =over 4  =over 4
277    
278  =item entityType  =item complete
   
 Entity type name.  
   
 =item ID  
279    
280  ID of the desired entity.  TRUE if only complete genomes are to be counted, FALSE if all genomes are to be
281    counted
 =item fields  
   
 List of field names, each of the form I<objectName>C<(>I<fieldName>C<)>.  
282    
283  =item RETURN  =item RETURN
284    
285  Returns a flattened list of the values of the specified fields for the specified entity.  A six-element list containing the number of genomes in each of six categories--
286    Archaea, Bacteria, Eukaryota, Viral, Environmental, and Unknown, respectively.
287    
288  =back  =back
289    
290  =cut  =cut
291  #: Return Type @;  
292  sub GetEntityValues {  sub GenomeCounts {
293          # Get the parameters.          # Get the parameters.
294          my ($self, $entityType, $ID, $fields) = @_;      my ($self, $complete) = @_;
295          # Get the specified entity.      # Set the filter based on the completeness flag.
296          my $entity = $self->GetEntity($entityType, $ID);      my $filter = ($complete ? "Genome(complete) = 1" : "");
297          # Declare the return list.      # Get all the genomes and the related taxonomy information.
298          my @retVal = ();      my @genomes = $self->GetAll(['Genome'], $filter, [], ['Genome(id)', 'Genome(taxonomy)']);
299          # If we found the entity, push the values into the return list.      # Clear the counters.
300          if ($entity) {      my ($arch, $bact, $euk, $vir, $env, $unk) = (0, 0, 0, 0, 0, 0);
301                  push @retVal, $entity->Values($fields);      # Loop through, counting the domains.
302        for my $genome (@genomes) {
303            if    ($genome->[1] =~ /^archaea/i)  { ++$arch }
304            elsif ($genome->[1] =~ /^bacter/i)   { ++$bact }
305            elsif ($genome->[1] =~ /^eukar/i)    { ++$euk }
306            elsif ($genome->[1] =~ /^vir/i)      { ++$vir }
307            elsif ($genome->[1] =~ /^env/i)      { ++$env }
308            else  { ++$unk }
309          }          }
310          # Return the result.      # Return the counts.
311          return @retVal;      return ($arch, $bact, $euk, $vir, $env, $unk);
312  }  }
313    
314  =head3 ShowMetaData  =head3 ContigCount
315    
316  C<< $sprout->ShowMetaData($fileName); >>  C<< my $count = $sprout->ContigCount($genomeID); >>
317    
318  This method outputs a description of the database to an HTML file in the data directory.  Return the number of contigs for the specified genome ID.
319    
320  =over 4  =over 4
321    
322  =item fileName  =item genomeID
323    
324    ID of the genome whose contig count is desired.
325    
326    =item RETURN
327    
328  Fully-qualified name to give to the output file.  Returns the number of contigs for the specified genome.
329    
330  =back  =back
331    
332  =cut  =cut
333    
334  sub ShowMetaData {  sub ContigCount {
335          # Get the parameters.          # Get the parameters.
336          my ($self, $fileName) = @_;      my ($self, $genomeID) = @_;
337          # Compute the file name.      # Get the contig count.
338          my $options = $self->{_options};      my $retVal = $self->GetCount(['Contig', 'HasContig'], "HasContig(from-link) = ?", [$genomeID]);
339          # Call the show method on the underlying ERDB object.      # Return the result.
340          $self->{_erdb}->ShowMetaData($fileName);      return $retVal;
341  }  }
342    
343  =head3 Load  =head3 GeneMenu
   
 C<< $sprout->Load($rebuild); >>;  
   
 Load the database from files in the data directory, optionally re-creating the tables.  
344    
345  This method always deletes the data from the database before loading, even if the tables are not  C<< my $selectHtml = $sprout->GeneMenu(\%attributes, $filterString, \@params, $selected); >>
 re-created. The data is loaded into the relations from files in the data directory either having the  
 same name as the target relation with no extension or with an extension of C<.dtx>. Files without an  
 extension are used in preference to the files with an extension.  
346    
347  The files are loaded based on the presumption that each line of the file is a record in the  Return an HTML select menu of genomes. Each genome will be an option in the menu,
348  relation, and the individual fields are delimited by tabs. Tab and new-line characters inside  and will be displayed by name with the ID and a contig count attached. The selection
349  fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must  value will be the genome ID. The genomes will be sorted by genus/species name.
 be presented in the order given in the relation tables produced by the L</ShowMetaData> method.  
350    
351  =over 4  =over 4
352    
353  =item rebuild  =item attributes
   
 TRUE if the data tables need to be created or re-created, else FALSE  
   
 =item RETURN  
   
 Returns a statistical object containing the number of records read, the number of duplicates found,  
 the number of errors, and a list of the error messages.  
   
 =back  
354    
355  =cut  Reference to a hash mapping attributes to values for the SELECT tag generated.
 #: Return Type %;  
 sub Load {  
         # Get the parameters.  
         my ($self, $rebuild) = @_;  
         # Get the database object.  
         my $erdb = $self->{_erdb};  
         # Load the tables from the data directory.  
         my $retVal = $erdb->LoadTables($self->{_options}->{dataDir}, $rebuild);  
         # Return the statistics.  
         return $retVal;  
 }  
356    
357  =head3 LoadUpdate  =item filterString
358    
359  C<< my %stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>  A filter string for use in selecting the genomes. The filter string must conform
360    to the rules for the C<< ERDB->Get >> method.
361    
362  Load updates to one or more database tables. This method enables the client to make changes to one  =item params
 or two tables without reloading the whole database. For each table, there must be a corresponding  
 file in the data directory, either with the same name as the table, or with a C<.dtx> suffix. So,  
 for example, to make updates to the B<FeatureTranslation> relation, there must be a  
 C<FeatureTranslation.dtx> file in the data directory. Unlike a full load, files without an extension  
 are not examined. This allows update files to co-exist with files from an original load.  
363    
364  =over 4  Reference to a list of values to be substituted in for the parameter marks in
365    the filter string.
366    
367  =item truncateFlag  =item selected (optional)
368    
369  TRUE if the tables should be rebuilt before loading, else FALSE. A value of TRUE therefore causes  ID of the genome to be initially selected.
 current data and schema of the tables to be replaced, while a value of FALSE means the new data  
 is added to the existing data in the various relations.  
370    
371  =item tableList  =item fast (optional)
372    
373  List of the tables to be updated.  If specified and TRUE, the contig counts will be omitted to improve performance.
374    
375  =item RETURN  =item RETURN
376    
377  Returns a statistical object containing the number of records read, the number of duplicates found,  Returns an HTML select menu with the specified genomes as selectable options.
 the number of errors encountered, and a list of error messages.  
378    
379  =back  =back
380    
381  =cut  =cut
382  #: Return Type $%;  
383  sub LoadUpdate {  sub GeneMenu {
384          # Get the parameters.          # Get the parameters.
385          my ($self, $truncateFlag, $tableList) = @_;      my ($self, $attributes, $filterString, $params, $selected, $fast) = @_;
386          # Get the database object.      my $slowMode = ! $fast;
387          my $erdb = $self->{_erdb};      # Default to nothing selected. This prevents an execution warning if "$selected"
388          # Declare the return value.      # is undefined.
389          my $retVal = Stats->new();      $selected = "" unless defined $selected;
390          # Get the data directory.      Trace("Gene Menu called with slow mode \"$slowMode\" and selection \"$selected\".") if T(3);
391          my $optionTable = $self->{_options};      # Start the menu.
392          my $dataDir = $optionTable->{dataDir};      my $retVal = "<select " .
393          # Loop through the incoming table names.          join(" ", map { "$_=\"$attributes->{$_}\"" } keys %{$attributes}) .
394          for my $tableName (@{$tableList}) {          ">\n";
395                  # Find the table's file.      # Get the genomes.
396                  my $fileName = "$dataDir/$tableName";      my @genomes = $self->GetAll(['Genome'], $filterString, $params, ['Genome(id)',
397                  if (! -e $fileName) {                                                                       'Genome(genus)',
398                          $fileName = "$fileName.dtx";                                                                       'Genome(species)',
399                  }                                                                       'Genome(unique-characterization)']);
400                  # Attempt to load this table.      # Sort them by name.
401                  my $result = $erdb->LoadTable($fileName, $tableName, $truncateFlag);      my @sorted = sort { lc("$a->[1] $a->[2]") cmp lc("$b->[1] $b->[2]") } @genomes;
402                  # Accumulate the resulting statistics.      # Loop through the genomes, creating the option tags.
403                  $retVal->Accumulate($result);      for my $genomeData (@sorted) {
404            # Get the data for this genome.
405            my ($genomeID, $genus, $species, $strain) = @{$genomeData};
406            # Get the contig count.
407            my $contigInfo = "";
408            if ($slowMode) {
409                my $count = $self->ContigCount($genomeID);
410                my $counting = ($count == 1 ? "contig" : "contigs");
411                $contigInfo = "[$count $counting]";
412            }
413            # Find out if we're selected.
414            my $selectOption = ($selected eq $genomeID ? " selected" : "");
415            # Build the option tag.
416            $retVal .= "<option value=\"$genomeID\"$selectOption>$genus $species $strain ($genomeID)$contigInfo</option>\n";
417          }          }
418          # Return the statistics.      # Close the SELECT tag.
419        $retVal .= "</select>\n";
420        # Return the result.
421          return $retVal;          return $retVal;
422  }  }
423    
# Line 459  Line 435 
435          # Get the parameters.          # Get the parameters.
436          my ($self) = @_;          my ($self) = @_;
437          # Create the tables.          # Create the tables.
438          $self->{_erdb}->CreateTables;      $self->CreateTables();
439  }  }
440    
441  =head3 Genomes  =head3 Genomes
# Line 582  Line 558 
558  =item RETURN  =item RETURN
559    
560  Returns a list of the feature's contig segments. The locations are returned as a list in a list  Returns a list of the feature's contig segments. The locations are returned as a list in a list
561  context and as a space-delimited string in a scalar context.  context and as a comma-delimited string in a scalar context.
562    
563  =back  =back
564    
# Line 609  Line 585 
585                  if ($prevContig eq $contigID && $dir eq $prevDir) {                  if ($prevContig eq $contigID && $dir eq $prevDir) {
586                          # Here the new segment is in the same direction on the same contig. Insure the                          # Here the new segment is in the same direction on the same contig. Insure the
587                          # new segment's beginning is next to the old segment's end.                          # new segment's beginning is next to the old segment's end.
588                          if (($dir eq "-" && $beg == $prevBeg - $prevLen) ||              if ($dir eq "-" && $beg + $len == $prevBeg) {
589                                  ($dir eq "+" && $beg == $prevBeg + $prevLen)) {                  # Here we're merging two backward blocks, so we keep the new begin point
590                                  # Here we need to merge two segments. Adjust the beginning and length values                  # and adjust the length.
591                                  # to include both segments.                  $len += $prevLen;
592                    # Pop the old segment off. The new one will replace it later.
593                    pop @retVal;
594                } elsif ($dir eq "+" && $beg == $prevBeg + $prevLen) {
595                    # Here we need to merge two forward blocks. Adjust the beginning and
596                    # length values to include both segments.
597                                  $beg = $prevBeg;                                  $beg = $prevBeg;
598                                  $len += $prevLen;                                  $len += $prevLen;
599                                  # Pop the old segment off. The new one will replace it later.                                  # Pop the old segment off. The new one will replace it later.
# Line 621  Line 602 
602                  }                  }
603                  # Remember this specifier for the adjacent-segment test the next time through.                  # Remember this specifier for the adjacent-segment test the next time through.
604                  ($prevContig, $prevBeg, $prevDir, $prevLen) = ($contigID, $beg, $dir, $len);                  ($prevContig, $prevBeg, $prevDir, $prevLen) = ($contigID, $beg, $dir, $len);
605            # Compute the initial base pair.
606            my $start = ($dir eq "+" ? $beg : $beg + $len - 1);
607                  # Add the specifier to the list.                  # Add the specifier to the list.
608                  push @retVal, "${contigID}_$beg$dir$len";          push @retVal, "${contigID}_$start$dir$len";
609          }          }
610          # Return the list in the format indicated by the context.          # Return the list in the format indicated by the context.
611          return (wantarray ? @retVal : join(' ', @retVal));      return (wantarray ? @retVal : join(',', @retVal));
612  }  }
613    
614  =head3 ParseLocation  =head3 ParseLocation
# Line 656  Line 639 
639      shift if UNIVERSAL::isa($_[0],__PACKAGE__);      shift if UNIVERSAL::isa($_[0],__PACKAGE__);
640          my ($location) = @_;          my ($location) = @_;
641          # Parse it into segments.          # Parse it into segments.
642          $location =~ /^(.*)_(\d*)([+-_])(\d*)$/;      $location =~ /^(.+)_(\d+)([+\-_])(\d+)$/;
643          my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);          my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);
644          # If the direction is an underscore, convert it to a + or -.          # If the direction is an underscore, convert it to a + or -.
645          if ($dir eq "_") {          if ($dir eq "_") {
# Line 672  Line 655 
655          return ($contigID, $start, $dir, $len);          return ($contigID, $start, $dir, $len);
656  }  }
657    
658    
659    
660  =head3 PointLocation  =head3 PointLocation
661    
662  C<< my $found = Sprout::PointLocation($location, $point); >>  C<< my $found = Sprout::PointLocation($location, $point); >>
# Line 732  Line 717 
717  should be of the form returned by L</featureLocation> when in a list context. In other words,  should be of the form returned by L</featureLocation> when in a list context. In other words,
718  each location is of the form I<contigID>C<_>I<begin>I<dir>I<end>.  each location is of the form I<contigID>C<_>I<begin>I<dir>I<end>.
719    
720    For example, the following would return the DNA sequence for contig C<83333.1:NC_000913>
721    between positions 1401 and 1532, inclusive.
722    
723        my $sequence = $sprout->DNASeq('83333.1:NC_000913_1401_1532');
724    
725  =over 4  =over 4
726    
727  =item locationList  =item locationList
728    
729  List of location specifiers, each in the form I<contigID>C<_>I<begin>I<dir>I<end> (see  List of location specifiers, each in the form I<contigID>C<_>I<begin>I<dir>I<len> or
730  L</FeatureLocation> for more about this format).  I<contigID>C<_>I<begin>C<_>I<end> (see L</FeatureLocation> for more about this format).
731    
732  =item RETURN  =item RETURN
733    
# Line 764  Line 754 
754                  # the start point is the ending. Note that in the latter case we must reverse the DNA string                  # the start point is the ending. Note that in the latter case we must reverse the DNA string
755                  # before putting it in the return value.                  # before putting it in the return value.
756                  my ($start, $stop);                  my ($start, $stop);
757            Trace("Parse of \"$location\" is $beg$dir$len.") if T(SDNA => 4);
758                  if ($dir eq "+") {                  if ($dir eq "+") {
759                          $start = $beg;                          $start = $beg;
760                          $stop = $beg + $len - 1;                          $stop = $beg + $len - 1;
761                  } else {                  } else {
762                          $start = $beg + $len + 1;              $start = $beg - $len + 1;
763                          $stop = $beg;                          $stop = $beg;
764                  }                  }
765            Trace("Looking for sequences containing $start through $stop.") if T(SDNA => 4);
766                  my $query = $self->Get(['IsMadeUpOf','Sequence'],                  my $query = $self->Get(['IsMadeUpOf','Sequence'],
767                          "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .                          "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .
768                          " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",                          " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",
# Line 782  Line 774 
774                                  $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',                                  $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',
775                                                                     'IsMadeUpOf(len)']);                                                                     'IsMadeUpOf(len)']);
776                          my $stopPosition = $startPosition + $sequenceLength;                          my $stopPosition = $startPosition + $sequenceLength;
777                Trace("Sequence is from $startPosition to $stopPosition.") if T(SDNA => 4);
778                          # Figure out the start point and length of the relevant section.                          # Figure out the start point and length of the relevant section.
779                          my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);                          my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);
780                          my $len = ($stopPosition <= $stop ? $stopPosition : $stop) - $startPosition - $pos1;              my $len1 = ($stopPosition < $stop ? $stopPosition : $stop) + 1 - $startPosition - $pos1;
781                Trace("Position is $pos1 for length $len1.") if T(SDNA => 4);
782                          # Add the relevant data to the location data.                          # Add the relevant data to the location data.
783                          $locationDNA .= substr($sequenceData, $pos1, $len);              $locationDNA .= substr($sequenceData, $pos1, $len1);
784                  }                  }
785                  # Add this location's data to the return string. Note that we may need to reverse it.                  # Add this location's data to the return string. Note that we may need to reverse it.
786                  if ($dir eq '+') {                  if ($dir eq '+') {
787                          $retVal .= $locationDNA;                          $retVal .= $locationDNA;
788                  } else {                  } else {
789                          $locationDNA = join('', reverse split //, $locationDNA);              $retVal .= FIG::reverse_comp($locationDNA);
                         $retVal .= $locationDNA;  
790                  }                  }
791          }          }
792          # Return the result.          # Return the result.
# Line 830  Line 823 
823          return @retVal;          return @retVal;
824  }  }
825    
826    =head3 GenomeLength
827    
828    C<< my $length = $sprout->GenomeLength($genomeID); >>
829    
830    Return the length of the specified genome in base pairs.
831    
832    =over 4
833    
834    =item genomeID
835    
836    ID of the genome whose base pair count is desired.
837    
838    =item RETURN
839    
840    Returns the number of base pairs in all the contigs of the specified
841    genome.
842    
843    =back
844    
845    =cut
846    
847    sub GenomeLength {
848        # Get the parameters.
849        my ($self, $genomeID) = @_;
850        # Declare the return variable.
851        my $retVal = 0;
852        # Get the genome's contig sequence lengths.
853        my @lens = $self->GetFlat(['HasContig', 'IsMadeUpOf'], 'HasContig(from-link) = ?',
854                           [$genomeID], 'IsMadeUpOf(len)');
855        # Sum the lengths.
856        map { $retVal += $_ } @lens;
857        # Return the result.
858        return $retVal;
859    }
860    
861    =head3 FeatureCount
862    
863    C<< my $count = $sprout->FeatureCount($genomeID, $type); >>
864    
865    Return the number of features of the specified type in the specified genome.
866    
867    =over 4
868    
869    =item genomeID
870    
871    ID of the genome whose feature count is desired.
872    
873    =item type
874    
875    Type of feature to count (eg. C<peg>, C<rna>, etc.).
876    
877    =item RETURN
878    
879    Returns the number of features of the specified type for the specified genome.
880    
881    =back
882    
883    =cut
884    
885    sub FeatureCount {
886        # Get the parameters.
887        my ($self, $genomeID, $type) = @_;
888        # Compute the count.
889        my $retVal = $self->GetCount(['HasFeature', 'Feature'],
890                                    "HasFeature(from-link) = ? AND Feature(feature-type) = ?",
891                                    [$genomeID, $type]);
892        # Return the result.
893        return $retVal;
894    }
895    
896    =head3 GenomeAssignments
897    
898    C<< my $fidHash = $sprout->GenomeAssignments($genomeID); >>
899    
900    Return a list of a genome's assigned features. The return hash will contain each
901    assigned feature of the genome mapped to the text of its most recent functional
902    assignment.
903    
904    =over 4
905    
906    =item genomeID
907    
908    ID of the genome whose functional assignments are desired.
909    
910    =item RETURN
911    
912    Returns a reference to a hash which maps each feature to its most recent
913    functional assignment.
914    
915    =back
916    
917    =cut
918    
919    sub GenomeAssignments {
920        # Get the parameters.
921        my ($self, $genomeID) = @_;
922        # Declare the return variable.
923        my $retVal = {};
924        # Query the genome's features and annotations. We'll put the oldest annotations
925        # first so that the last assignment to go into the hash will be the correct one.
926        my $query = $self->Get(['HasFeature', 'IsTargetOfAnnotation', 'Annotation'],
927                               "HasFeature(from-link) = ? ORDER BY Annotation(time)",
928                               [$genomeID]);
929        # Loop through the annotations.
930        while (my $data = $query->Fetch) {
931            # Get the feature ID and annotation text.
932            my ($fid, $annotation) = $data->Values(['HasFeature(to-link)',
933                                                    'Annotation(annotation)']);
934            # Check to see if this is an assignment. Note that the user really
935            # doesn't matter to us, other than we use it to determine whether or
936            # not this is an assignment.
937            my ($user, $assignment) = _ParseAssignment('fig', $annotation);
938            if ($user) {
939                # Here it's an assignment. We put it in the return hash, overwriting
940                # any older assignment that might be present.
941                $retVal->{$fid} = $assignment;
942            }
943        }
944        # Return the result.
945        return $retVal;
946    }
947    
948  =head3 ContigLength  =head3 ContigLength
949    
950  C<< my $length = $sprout->ContigLength($contigID); >>  C<< my $length = $sprout->ContigLength($contigID); >>
# Line 863  Line 978 
978          # Set it from the sequence data, if any.          # Set it from the sequence data, if any.
979          if ($sequence) {          if ($sequence) {
980                  my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);                  my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);
981                  $retVal = $start + $len;          $retVal = $start + $len - 1;
982          }          }
983          # Return the result.          # Return the result.
984          return $retVal;          return $retVal;
985  }  }
986    
987  =head3 GenesInRegion  =head3 ClusterPEGs
988    
989  C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>  C<< my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs); >>
990    
991  List the features which overlap a specified region in a contig.  Cluster the PEGs in a list according to the cluster coding scheme of the specified
992    subsystem. In order for this to work properly, the subsystem object must have
993    been used recently to retrieve the PEGs using the B<get_pegs_from_cell> method.
994    This causes the cluster numbers to be pulled into the subsystem's color hash.
995    If a PEG is not found in the color hash, it will not appear in the output
996    sequence.
997    
998  =over 4  =over 4
999    
1000  =item contigID  =item sub
1001    
1002  ID of the contig containing the region of interest.  Sprout subsystem object for the relevant subsystem, from the L</get_subsystem>
1003    method.
1004    
1005  =item start  =item pegs
1006    
1007  Offset of the first residue in the region of interest.  Reference to the list of PEGs to be clustered.
1008    
1009  =item stop  =item RETURN
1010    
1011  Offset of the last residue in the region of interest.  Returns a list of the PEGs, grouped into smaller lists by cluster number.
1012    
1013    =back
1014    
1015    =cut
1016    #: Return Type $@@;
1017    sub ClusterPEGs {
1018        # Get the parameters.
1019        my ($self, $sub, $pegs) = @_;
1020        # Declare the return variable.
1021        my $retVal = [];
1022        # Loop through the PEGs, creating arrays for each cluster.
1023        for my $pegID (@{$pegs}) {
1024            my $clusterNumber = $sub->get_cluster_number($pegID);
1025            # Only proceed if the PEG is in a cluster.
1026            if ($clusterNumber >= 0) {
1027                # Push this PEG onto the sub-list for the specified cluster number.
1028                push @{$retVal->[$clusterNumber]}, $pegID;
1029            }
1030        }
1031        # Return the result.
1032        return $retVal;
1033    }
1034    
1035    =head3 GenesInRegion
1036    
1037    C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>
1038    
1039    List the features which overlap a specified region in a contig.
1040    
1041    =over 4
1042    
1043    =item contigID
1044    
1045    ID of the contig containing the region of interest.
1046    
1047    =item start
1048    
1049    Offset of the first residue in the region of interest.
1050    
1051    =item stop
1052    
1053    Offset of the last residue in the region of interest.
1054    
1055  =item RETURN  =item RETURN
1056    
# Line 1013  Line 1176 
1176    
1177  =head3 FeatureAnnotations  =head3 FeatureAnnotations
1178    
1179  C<< my @descriptors = $sprout->FeatureAnnotations($featureID); >>  C<< my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag); >>
1180    
1181  Return the annotations of a feature.  Return the annotations of a feature.
1182    
# Line 1023  Line 1186 
1186    
1187  ID of the feature whose annotations are desired.  ID of the feature whose annotations are desired.
1188    
1189    =item rawFlag
1190    
1191    If TRUE, the annotation timestamps will be returned in raw form; otherwise, they
1192    will be returned in human-readable form.
1193    
1194  =item RETURN  =item RETURN
1195    
1196  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.
1197    
1198  * B<featureID> ID of the relevant feature.  * B<featureID> ID of the relevant feature.
1199    
1200  * B<timeStamp> time the annotation was made, in user-friendly format.  * B<timeStamp> time the annotation was made.
1201    
1202  * B<user> ID of the user who made the annotation  * B<user> ID of the user who made the annotation
1203    
# Line 1041  Line 1209 
1209  #: Return Type @%;  #: Return Type @%;
1210  sub FeatureAnnotations {  sub FeatureAnnotations {
1211          # Get the parameters.          # Get the parameters.
1212          my ($self, $featureID) = @_;      my ($self, $featureID, $rawFlag) = @_;
1213          # 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.
1214          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1215                                                     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);                                                     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);
# Line 1054  Line 1222 
1222                          $annotation->Values(['IsTargetOfAnnotation(from-link)',                          $annotation->Values(['IsTargetOfAnnotation(from-link)',
1223                                                                   'Annotation(time)', 'MadeAnnotation(from-link)',                                                                   'Annotation(time)', 'MadeAnnotation(from-link)',
1224                                                                   'Annotation(annotation)']);                                                                   'Annotation(annotation)']);
1225            # Convert the time, if necessary.
1226            if (! $rawFlag) {
1227                $timeStamp = FriendlyTimestamp($timeStamp);
1228            }
1229                  # Assemble them into a hash.                  # Assemble them into a hash.
1230          my $annotationHash = { featureID => $featureID,          my $annotationHash = { featureID => $featureID,
1231                                 timeStamp => FriendlyTimestamp($timeStamp),                                 timeStamp => $timeStamp,
1232                                                             user => $user, text => $text };                                                             user => $user, text => $text };
1233                  # Add it to the return list.                  # Add it to the return list.
1234                  push @retVal, $annotationHash;                  push @retVal, $annotationHash;
# Line 1070  Line 1242 
1242  C<< my %functions = $sprout->AllFunctionsOf($featureID); >>  C<< my %functions = $sprout->AllFunctionsOf($featureID); >>
1243    
1244  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
1245  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,
1246  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
1247  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,
1248  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.
1249  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
1250  functional assignments, we will only keep the most recent one.  recent one.
1251    
1252  =over 4  =over 4
1253    
# Line 1085  Line 1257 
1257    
1258  =item RETURN  =item RETURN
1259    
1260  Returns a hash mapping the functional assignment IDs to user IDs.  Returns a hash mapping the user IDs to functional assignment IDs.
1261    
1262  =back  =back
1263    
# Line 1095  Line 1267 
1267          # Get the parameters.          # Get the parameters.
1268          my ($self, $featureID) = @_;          my ($self, $featureID) = @_;
1269          # Get all of the feature's annotations.          # Get all of the feature's annotations.
1270      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation'],      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1271                                                      "IsTargetOfAnnotation(from-link) = ?",                                                      "IsTargetOfAnnotation(from-link) = ?",
1272                              [$featureID], ['Annotation(time)', 'Annotation(annotation)']);                              [$featureID], ['Annotation(time)', 'Annotation(annotation)',
1273                                               'MadeAnnotation(from-link)']);
1274          # Declare the return hash.          # Declare the return hash.
1275          my %retVal;          my %retVal;
     # Declare a hash for insuring we only make one assignment per user.  
     my %timeHash = ();  
1276      # Now we sort the assignments by timestamp in reverse.      # Now we sort the assignments by timestamp in reverse.
1277      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;
1278          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1279      for my $annotation (@sortedQuery) {      for my $annotation (@sortedQuery) {
1280          # Get the annotation fields.          # Get the annotation fields.
1281          my ($timeStamp, $text) = @{$annotation};          my ($timeStamp, $text, $user) = @{$annotation};
1282                  # Check to see if this is a functional assignment.                  # Check to see if this is a functional assignment.
1283                  my ($user, $function) = ParseAssignment($text);          my ($actualUser, $function) = _ParseAssignment($user, $text);
1284          if ($user && ! exists $timeHash{$user}) {          if ($actualUser && ! exists $retVal{$actualUser}) {
1285              # Here it is a functional assignment and there has been no              # Here it is a functional assignment and there has been no
1286              # previous assignment for this user, so we stuff it in the              # previous assignment for this user, so we stuff it in the
1287              # return hash.              # return hash.
1288                          $retVal{$function} = $user;              $retVal{$actualUser} = $function;
             # Insure we don't assign to this user again.  
             $timeHash{$user} = 1;  
1289                  }                  }
1290          }          }
1291          # Return the hash of assignments found.          # Return the hash of assignments found.
# Line 1131  Line 1300 
1300    
1301  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
1302  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
1303  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
1304  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
1305  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
1306  is not a big problem because most features only have a small number of annotations.  most features only have a small number of annotations.
1307    
1308  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
1309  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 1363 
1363              }              }
1364          }          }
1365          # 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.
1366          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation'],          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1367                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1368                                 [$featureID]);                                 [$featureID]);
1369          my $timeSelected = 0;          my $timeSelected = 0;
1370          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1371          while (my $annotation = $query->Fetch()) {          while (my $annotation = $query->Fetch()) {
1372              # Get the annotation text.              # Get the annotation text.
1373              my ($text, $time) = $annotation->Values(['Annotation(annotation)','Annotation(time)']);              my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1374                                                         'Annotation(time)', 'MadeAnnotation(from-link)']);
1375              # 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.
1376              my ($user, $type, $function) = split(/\n/, $text);              my ($actualUser, $function) = _ParseAssignment($user, $text);
1377              if ($type =~ m/^set $user function to$/i) {              Trace("Assignment user is $actualUser, text is $function.") if T(4);
1378                if ($actualUser) {
1379                  # Here it is a functional assignment. Check the time and the user                  # Here it is a functional assignment. Check the time and the user
1380                  # name. The time must be recent and the user must be trusted.                  # name. The time must be recent and the user must be trusted.
1381                  if ((exists $trusteeTable{$user}) && ($time > $timeSelected)) {                  if ((exists $trusteeTable{$actualUser}) && ($time > $timeSelected)) {
1382                      $retVal = $function;                      $retVal = $function;
1383                      $timeSelected = $time;                      $timeSelected = $time;
1384                  }                  }
# Line 1223  Line 1394 
1394          return $retVal;          return $retVal;
1395  }  }
1396    
1397    =head3 FunctionsOf
1398    
1399    C<< my @functionList = $sprout->FunctionOf($featureID, $userID); >>
1400    
1401    Return the functional assignments of a particular feature.
1402    
1403    The functional assignment is handled differently depending on the type of feature. If
1404    the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional
1405    assignment is a type of annotation. The format of an assignment is described in
1406    L</ParseAssignment>. Its worth noting that we cannot filter on the content of the
1407    annotation itself because it's a text field; however, this is not a big problem because
1408    most features only have a small number of annotations.
1409    
1410    If the feature is B<not> identified by a FIG ID, then the functional assignment
1411    information is taken from the B<ExternalAliasFunc> table. If the table does
1412    not contain an entry for the feature, an empty list is returned.
1413    
1414    =over 4
1415    
1416    =item featureID
1417    
1418    ID of the feature whose functional assignments are desired.
1419    
1420    =item RETURN
1421    
1422    Returns a list of 2-tuples, each consisting of a user ID and the text of an assignment by
1423    that user.
1424    
1425    =back
1426    
1427    =cut
1428    #: Return Type @@;
1429    sub FunctionsOf {
1430        # Get the parameters.
1431        my ($self, $featureID) = @_;
1432        # Declare the return value.
1433        my @retVal = ();
1434        # Determine the ID type.
1435        if ($featureID =~ m/^fig\|/) {
1436            # Here we have a FIG feature ID. We must build the list of trusted
1437            # users.
1438            my %trusteeTable = ();
1439            # Build a query for all of the feature's annotations, sorted by date.
1440            my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1441                                   "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1442                                   [$featureID]);
1443            my $timeSelected = 0;
1444            # Loop until we run out of annotations.
1445            while (my $annotation = $query->Fetch()) {
1446                # Get the annotation text.
1447                my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1448                                                                'Annotation(time)',
1449                                                                'MadeAnnotation(user)']);
1450                # Check to see if this is a functional assignment for a trusted user.
1451                my ($actualUser, $function) = _ParseAssignment($user, $text);
1452                if ($actualUser) {
1453                    # Here it is a functional assignment.
1454                    push @retVal, [$actualUser, $function];
1455                }
1456            }
1457        } else {
1458            # Here we have a non-FIG feature ID. In this case the user ID does not
1459            # matter. We simply get the information from the External Alias Function
1460            # table.
1461            my @assignments = $self->GetEntityValues('ExternalAliasFunc', $featureID,
1462                                                     ['ExternalAliasFunc(func)']);
1463            push @retVal, map { ['master', $_] } @assignments;
1464        }
1465        # Return the assignments found.
1466        return @retVal;
1467    }
1468    
1469  =head3 BBHList  =head3 BBHList
1470    
1471  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>
# Line 1242  Line 1485 
1485    
1486  =item RETURN  =item RETURN
1487    
1488  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
1489  their best hits.  on the target genome.
1490    
1491  =back  =back
1492    
# Line 1256  Line 1499 
1499          my %retVal = ();          my %retVal = ();
1500          # Loop through the incoming features.          # Loop through the incoming features.
1501          for my $featureID (@{$featureList}) {          for my $featureID (@{$featureList}) {
1502                  # Create a query to get the feature's best hit.          # Ask the server for the feature's best hit.
1503                  my $query = $self->Get(['IsBidirectionalBestHitOf'],          my @bbhData = FIGRules::BBHData($featureID);
1504                                                             "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?",          # Peel off the BBHs found.
1505                                                             [$featureID, $genomeID]);          my @found = ();
1506                  # Look for the best hit.          for my $bbh (@bbhData) {
1507                  my $bbh = $query->Fetch;              push @found, $bbh->[0];
                 if ($bbh) {  
                         my ($targetFeature) = $bbh->Value('IsBidirectionalBestHitOf(to-link)');  
                         $retVal{$featureID} = $targetFeature;  
1508                  }                  }
1509            $retVal{$featureID} = \@found;
1510          }          }
1511          # Return the mapping.          # Return the mapping.
1512          return \%retVal;          return \%retVal;
1513  }  }
1514    
1515    =head3 SimList
1516    
1517    C<< my %similarities = $sprout->SimList($featureID, $count); >>
1518    
1519    Return a list of the similarities to the specified feature.
1520    
1521    This method just returns the bidirectional best hits for performance reasons.
1522    
1523    =over 4
1524    
1525    =item featureID
1526    
1527    ID of the feature whose similarities are desired.
1528    
1529    =item count
1530    
1531    Maximum number of similar features to be returned, or C<0> to return them all.
1532    
1533    =back
1534    
1535    =cut
1536    #: Return Type %;
1537    sub SimList {
1538        # Get the parameters.
1539        my ($self, $featureID, $count) = @_;
1540        # Ask for the best hits.
1541        my @lists = FIGRules::BBHData($featureID);
1542        # Create the return value.
1543        my %retVal = ();
1544        for my $tuple (@lists) {
1545            $retVal{$tuple->[0]} = $tuple->[1];
1546        }
1547        # Return the result.
1548        return %retVal;
1549    }
1550    
1551    =head3 IsComplete
1552    
1553    C<< my $flag = $sprout->IsComplete($genomeID); >>
1554    
1555    Return TRUE if the specified genome is complete, else FALSE.
1556    
1557    =over 4
1558    
1559    =item genomeID
1560    
1561    ID of the genome whose completeness status is desired.
1562    
1563    =item RETURN
1564    
1565    Returns TRUE if the genome is complete, FALSE if it is incomplete, and C<undef> if it is
1566    not found.
1567    
1568    =back
1569    
1570    =cut
1571    #: Return Type $;
1572    sub IsComplete {
1573        # Get the parameters.
1574        my ($self, $genomeID) = @_;
1575        # Declare the return variable.
1576        my $retVal;
1577        # Get the genome's data.
1578        my $genomeData = $self->GetEntity('Genome', $genomeID);
1579        if ($genomeData) {
1580            # The genome exists, so get the completeness flag.
1581            ($retVal) = $genomeData->Value('Genome(complete)');
1582        }
1583        # Return the result.
1584        return $retVal;
1585    }
1586    
1587  =head3 FeatureAliases  =head3 FeatureAliases
1588    
1589  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>
# Line 1305  Line 1618 
1618    
1619  C<< my $genomeID = $sprout->GenomeOf($featureID); >>  C<< my $genomeID = $sprout->GenomeOf($featureID); >>
1620    
1621  Return the genome that contains a specified feature.  Return the genome that contains a specified feature or contig.
1622    
1623  =over 4  =over 4
1624    
1625  =item featureID  =item featureID
1626    
1627  ID of the feature whose genome is desired.  ID of the feature or contig whose genome is desired.
1628    
1629  =item RETURN  =item RETURN
1630    
1631  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
1632  an undefined value.  found, returns an undefined value.
1633    
1634  =back  =back
1635    
# Line 1325  Line 1638 
1638  sub GenomeOf {  sub GenomeOf {
1639          # Get the parameters.          # Get the parameters.
1640          my ($self, $featureID) = @_;          my ($self, $featureID) = @_;
1641          # Create a query to find the genome associated with the feature.      # Create a query to find the genome associated with the incoming ID.
1642          my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ?", [$featureID]);      my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ? OR HasContig(to-link) = ?",
1643                               [$featureID, $featureID]);
1644          # Declare the return value.          # Declare the return value.
1645          my $retVal;          my $retVal;
1646          # Get the genome ID.          # Get the genome ID.
# Line 1361  Line 1675 
1675  sub CoupledFeatures {  sub CoupledFeatures {
1676          # Get the parameters.          # Get the parameters.
1677          my ($self, $featureID) = @_;          my ($self, $featureID) = @_;
1678          # Create a query to retrieve the functionally-coupled features. Note that we depend on the      Trace("Looking for features coupled to $featureID.") if T(coupling => 3);
1679          # fact that the functional coupling is physically paired. If (A,B) is in the database, then      # Create a query to retrieve the functionally-coupled features.
1680          # (B,A) will also be found.      my $query = $self->Get(['ParticipatesInCoupling', 'Coupling'],
1681          my $query = $self->Get(['IsClusteredOnChromosomeWith'],                             "ParticipatesInCoupling(from-link) = ?", [$featureID]);
                                                    "IsClusteredOnChromosomeWith(from-link) = ?", [$featureID]);  
1682          # This value will be set to TRUE if we find at least one coupled feature.          # This value will be set to TRUE if we find at least one coupled feature.
1683          my $found = 0;          my $found = 0;
1684          # Create the return hash.          # Create the return hash.
1685          my %retVal = ();          my %retVal = ();
1686          # Retrieve the relationship records and store them in the hash.          # Retrieve the relationship records and store them in the hash.
1687          while (my $clustering = $query->Fetch()) {          while (my $clustering = $query->Fetch()) {
1688                  my ($otherFeatureID, $score) = $clustering->Values(['IsClusteredOnChromosomeWith(to-link)',          # Get the ID and score of the coupling.
1689                                                                      'IsClusteredOnChromosomeWith(score)']);          my ($couplingID, $score) = $clustering->Values(['Coupling(id)',
1690                                                            'Coupling(score)']);
1691            Trace("$featureID coupled with score $score to ID $couplingID.") if T(coupling => 4);
1692            # Get the other feature that participates in the coupling.
1693            my ($otherFeatureID) = $self->GetFlat(['ParticipatesInCoupling'],
1694                                               "ParticipatesInCoupling(to-link) = ? AND ParticipatesInCoupling(from-link) <> ?",
1695                                               [$couplingID, $featureID], 'ParticipatesInCoupling(from-link)');
1696            Trace("$couplingID target feature is $otherFeatureID.") if T(coupling => 4);
1697            # Attach the other feature's score to its ID.
1698                  $retVal{$otherFeatureID} = $score;                  $retVal{$otherFeatureID} = $score;
1699                  $found = 1;                  $found = 1;
1700          }          }
# Line 1386  Line 1707 
1707          return %retVal;          return %retVal;
1708  }  }
1709    
1710  =head3 GetEntityTypes  =head3 CouplingEvidence
1711    
1712    C<< my @evidence = $sprout->CouplingEvidence($peg1, $peg2); >>
1713    
1714    Return the evidence for a functional coupling.
1715    
1716    A pair of features is considered evidence of a coupling between two other
1717    features if they occur close together on a contig and both are similar to
1718    the coupled features. So, if B<A1> and B<A2> are close together on a contig,
1719    B<B1> and B<B2> are considered evidence for the coupling if (1) B<B1> and
1720    B<B2> are close together, (2) B<B1> is similar to B<A1>, and (3) B<B2> is
1721    similar to B<A2>.
1722    
1723    The score of a coupling is determined by the number of pieces of evidence
1724    that are considered I<representative>. If several evidence items belong to
1725    a group of genomes that are close to each other, only one of those items
1726    is considered representative. The other evidence items are presumed to be
1727    there because of the relationship between the genomes rather than because
1728    the two proteins generated by the features have a related functionality.
1729    
1730    Each evidence item is returned as a three-tuple in the form C<[>I<$peg1a>C<,>
1731    I<$peg2a>C<,> I<$rep>C<]>, where I<$peg1a> is similar to I<$peg1>, I<$peg2a>
1732    is similar to I<$peg2>, and I<$rep> is TRUE if the evidence is representative
1733    and FALSE otherwise.
1734    
1735    =over 4
1736    
1737    =item peg1
1738    
1739    ID of the feature of interest.
1740    
1741    =item peg2
1742    
1743  C<< my @entityList = $sprout->GetEntityTypes(); >>  ID of a feature functionally coupled to the feature of interest.
1744    
1745  Return the list of supported entity types.  =item RETURN
1746    
1747    Returns a list of 3-tuples. Each tuple consists of a feature similar to the feature
1748    of interest, a feature similar to the functionally coupled feature, and a flag
1749    that is TRUE for a representative piece of evidence and FALSE otherwise.
1750    
1751    =back
1752    
1753  =cut  =cut
1754  #: Return Type @;  #: Return Type @@;
1755  sub GetEntityTypes {  sub CouplingEvidence {
1756          # Get the parameters.          # Get the parameters.
1757          my ($self) = @_;      my ($self, $peg1, $peg2) = @_;
1758          # Get the underlying database object.      # Declare the return variable.
1759          my $erdb = $self->{_erdb};      my @retVal = ();
1760          # Get its entity type list.      # Our first task is to find out the nature of the coupling: whether or not
1761          my @retVal = $erdb->GetEntityTypes();      # it exists, its score, and whether the features are stored in the same
1762        # order as the ones coming in.
1763        my ($couplingID, $inverted, $score) = $self->GetCoupling($peg1, $peg2);
1764        # Only proceed if a coupling exists.
1765        if ($couplingID) {
1766            # Determine the ordering to place on the evidence items. If we're
1767            # inverted, we want to see feature 2 before feature 1 (descending); otherwise,
1768            # we want feature 1 before feature 2 (normal).
1769            Trace("Coupling evidence for ($peg1, $peg2) with inversion flag $inverted.") if T(Coupling => 4);
1770            my $ordering = ($inverted ? "DESC" : "");
1771            # Get the coupling evidence.
1772            my @evidenceList = $self->GetAll(['IsEvidencedBy', 'PCH', 'UsesAsEvidence'],
1773                                              "IsEvidencedBy(from-link) = ? ORDER BY PCH(id), UsesAsEvidence(pos) $ordering",
1774                                              [$couplingID],
1775                                              ['PCH(used)', 'UsesAsEvidence(to-link)']);
1776            # Loop through the evidence items. Each piece of evidence is represented by two
1777            # positions in the evidence list, one for each feature on the other side of the
1778            # evidence link. If at some point we want to generalize to couplings with
1779            # more than two positions, this section of code will need to be re-done.
1780            while (@evidenceList > 0) {
1781                my $peg1Data = shift @evidenceList;
1782                my $peg2Data = shift @evidenceList;
1783                Trace("Peg 1 is " . $peg1Data->[1] . " and Peg 2 is " . $peg2Data->[1] . ".") if T(Coupling => 4);
1784                push @retVal, [$peg1Data->[1], $peg2Data->[1], $peg1Data->[0]];
1785            }
1786            Trace("Last index in evidence result is is $#retVal.") if T(Coupling => 4);
1787        }
1788        # Return the result.
1789        return @retVal;
1790    }
1791    
1792    =head3 GetCoupling
1793    
1794    C<< my ($couplingID, $inverted, $score) = $sprout->GetCoupling($peg1, $peg2); >>
1795    
1796    Return the coupling (if any) for the specified pair of PEGs. If a coupling
1797    exists, we return the coupling ID along with an indicator of whether the
1798    coupling is stored as C<(>I<$peg1>C<, >I<$peg2>C<)> or C<(>I<$peg2>C<, >I<$peg1>C<)>.
1799    In the second case, we say the coupling is I<inverted>. The importance of an
1800    inverted coupling is that the PEGs in the evidence will appear in reverse order.
1801    
1802    =over 4
1803    
1804    =item peg1
1805    
1806    ID of the feature of interest.
1807    
1808    =item peg2
1809    
1810    ID of the potentially coupled feature.
1811    
1812    =item RETURN
1813    
1814    Returns a three-element list. The first element contains the database ID of
1815    the coupling. The second element is FALSE if the coupling is stored in the
1816    database in the caller specified order and TRUE if it is stored in the
1817    inverted order. The third element is the coupling's score. If the coupling
1818    does not exist, all three list elements will be C<undef>.
1819    
1820    =back
1821    
1822    =cut
1823    #: Return Type $%@;
1824    sub GetCoupling {
1825        # Get the parameters.
1826        my ($self, $peg1, $peg2) = @_;
1827        # Declare the return values. We'll start with the coupling ID and undefine the
1828        # flag and score until we have more information.
1829        my ($retVal, $inverted, $score) = ($self->CouplingID($peg1, $peg2), undef, undef);
1830        # Find the coupling data.
1831        my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'],
1832                                     "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)",
1833                                     [$retVal], ["ParticipatesInCoupling(from-link)", "Coupling(score)"]);
1834        # Check to see if we found anything.
1835        if (!@pegs) {
1836            Trace("No coupling found.") if T(Coupling => 4);
1837            # No coupling, so undefine the return value.
1838            $retVal = undef;
1839        } else {
1840            # We have a coupling! Get the score and check for inversion.
1841            $score = $pegs[0]->[1];
1842            my $firstFound = $pegs[0]->[0];
1843            $inverted = ($firstFound ne $peg1);
1844            Trace("Coupling score is $score. First peg is $firstFound, peg 1 is $peg1.") if T(Coupling => 4);
1845        }
1846        # Return the result.
1847        return ($retVal, $inverted, $score);
1848    }
1849    
1850    =head3 GetSynonymGroup
1851    
1852    C<< my $id = $sprout->GetSynonymGroup($fid); >>
1853    
1854    Return the synonym group name for the specified feature.
1855    
1856    =over 4
1857    
1858    =item fid
1859    
1860    ID of the feature whose synonym group is desired.
1861    
1862    =item RETURN
1863    
1864    The name of the synonym group to which the feature belongs. If the feature does
1865    not belong to a synonym group, the feature ID itself is returned.
1866    
1867    =back
1868    
1869    =cut
1870    
1871    sub GetSynonymGroup {
1872        # Get the parameters.
1873        my ($self, $fid) = @_;
1874        # Declare the return variable.
1875        my $retVal;
1876        # Find the synonym group.
1877        my @groups = $self->GetFlat(['IsSynonymGroupFor'], "IsSynonymGroupFor(to-link) = ?",
1878                                       [$fid], 'IsSynonymGroupFor(from-link)');
1879        # Check to see if we found anything.
1880        if (@groups) {
1881            $retVal = $groups[0];
1882        } else {
1883            $retVal = $fid;
1884        }
1885        # Return the result.
1886        return $retVal;
1887    }
1888    
1889    =head3 GetBoundaries
1890    
1891    C<< my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList); >>
1892    
1893    Determine the begin and end boundaries for the locations in a list. All of the
1894    locations must belong to the same contig and have mostly the same direction in
1895    order for this method to produce a meaningful result. The resulting
1896    begin/end pair will contain all of the bases in any of the locations.
1897    
1898    =over 4
1899    
1900    =item locList
1901    
1902    List of locations to process.
1903    
1904    =item RETURN
1905    
1906    Returns a 3-tuple consisting of the contig ID, the beginning boundary,
1907    and the ending boundary. The beginning boundary will be left of the
1908    end for mostly-forward locations and right of the end for mostly-backward
1909    locations.
1910    
1911    =back
1912    
1913    =cut
1914    
1915    sub GetBoundaries {
1916        # Get the parameters.
1917        my ($self, @locList) = @_;
1918        # Set up the counters used to determine the most popular direction.
1919        my %counts = ( '+' => 0, '-' => 0 );
1920        # Get the last location and parse it.
1921        my $locObject = BasicLocation->new(pop @locList);
1922        # Prime the loop with its data.
1923        my ($contig, $beg, $end) = ($locObject->Contig, $locObject->Left, $locObject->Right);
1924        # Count its direction.
1925        $counts{$locObject->Dir}++;
1926        # Loop through the remaining locations. Note that in most situations, this loop
1927        # will not iterate at all, because most of the time we will be dealing with a
1928        # singleton list.
1929        for my $loc (@locList) {
1930            # Create a location object.
1931            my $locObject = BasicLocation->new($loc);
1932            # Count the direction.
1933            $counts{$locObject->Dir}++;
1934            # Get the left end and the right end.
1935            my $left = $locObject->Left;
1936            my $right = $locObject->Right;
1937            # Merge them into the return variables.
1938            if ($left < $beg) {
1939                $beg = $left;
1940            }
1941            if ($right > $end) {
1942                $end = $right;
1943            }
1944        }
1945        # If the most common direction is reverse, flip the begin and end markers.
1946        if ($counts{'-'} > $counts{'+'}) {
1947            ($beg, $end) = ($end, $beg);
1948        }
1949        # Return the result.
1950        return ($contig, $beg, $end);
1951    }
1952    
1953    =head3 CouplingID
1954    
1955    C<< my $couplingID = $sprout->CouplingID($peg1, $peg2); >>
1956    
1957    Return the coupling ID for a pair of feature IDs.
1958    
1959    The coupling ID is currently computed by joining the feature IDs in
1960    sorted order with a space. Client modules (that is, modules which
1961    use Sprout) should not, however, count on this always being the
1962    case. This method provides a way for abstracting the concept of a
1963    coupling ID. All that we know for sure about it is that it can be
1964    generated easily from the feature IDs and the order of the IDs
1965    in the parameter list does not matter (i.e. C<CouplingID("a1", "b1")>
1966    will have the same value as C<CouplingID("b1", "a1")>.
1967    
1968    =over 4
1969    
1970    =item peg1
1971    
1972    First feature of interest.
1973    
1974    =item peg2
1975    
1976    Second feature of interest.
1977    
1978    =item RETURN
1979    
1980    Returns the ID that would be used to represent a functional coupling of
1981    the two specified PEGs.
1982    
1983    =back
1984    
1985    =cut
1986    #: Return Type $;
1987    sub CouplingID {
1988        my ($self, @pegs) = @_;
1989        return $self->DigestKey(join " ", sort @pegs);
1990  }  }
1991    
1992  =head3 ReadFasta  =head3 ReadFasta
# Line 1447  Line 2033 
2033                  if ($line =~ m/^>\s*(.+?)(\s|\n)/) {                  if ($line =~ m/^>\s*(.+?)(\s|\n)/) {
2034                          # 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.
2035                          if ($id) {                          if ($id) {
2036                                  $retVal{$id} = $sequence;                  $retVal{$id} = lc $sequence;
2037                          }                          }
2038                          # Clear the sequence accumulator and save the new ID.                          # Clear the sequence accumulator and save the new ID.
2039                          ($id, $sequence) = ("$prefix$1", "");                          ($id, $sequence) = ("$prefix$1", "");
2040                  } else {                  } else {
2041                          # 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.
2042                          # First, we get the actual data out.              # First, we get the actual data out. Note that we normalize to lower
2043                # case.
2044                          $line =~ /^\s*(.*?)(\s|\n)/;                          $line =~ /^\s*(.*?)(\s|\n)/;
2045                          $sequence .= $1;                          $sequence .= $1;
2046                  }                  }
2047          }          }
2048          # Flush out the last sequence (if any).          # Flush out the last sequence (if any).
2049          if ($sequence) {          if ($sequence) {
2050                  $retVal {$id} = $sequence;          $retVal{$id} = lc $sequence;
2051          }          }
2052        # Close the file.
2053        close FASTAFILE;
2054          # Return the hash constructed from the file.          # Return the hash constructed from the file.
2055          return %retVal;          return %retVal;
2056  }  }
# Line 1545  Line 2134 
2134          # Get the data directory name.          # Get the data directory name.
2135          my $outputDirectory = $self->{_options}->{dataDir};          my $outputDirectory = $self->{_options}->{dataDir};
2136          # Dump the relations.          # Dump the relations.
2137          $self->{_erdb}->DumpRelations($outputDirectory);      $self->DumpRelations($outputDirectory);
2138  }  }
2139    
2140  =head3 XMLFileName  =head3 XMLFileName
# Line 1577  Line 2166 
2166  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
2167  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>.
2168    
2169  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'}); >>
2170    
2171  =over 4  =over 4
2172    
# Line 1597  Line 2186 
2186          # Get the parameters.          # Get the parameters.
2187          my ($self, $objectType, $fieldHash) = @_;          my ($self, $objectType, $fieldHash) = @_;
2188          # Call the underlying method.          # Call the underlying method.
2189          $self->{_erdb}->InsertObject($objectType, $fieldHash);      $self->InsertObject($objectType, $fieldHash);
2190  }  }
2191    
2192  =head3 Annotate  =head3 Annotate
# Line 1656  Line 2245 
2245    
2246  =head3 AssignFunction  =head3 AssignFunction
2247    
2248  C<< my $ok = $sprout->AssignFunction($featureID, $user, $function); >>  C<< my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser); >>
2249    
2250  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
2251  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.  
2252    
2253  =over 4  =over 4
2254    
# Line 1670  Line 2258 
2258    
2259  =item user  =item user
2260    
2261  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>.
2262    
2263  =item function  =item function
2264    
2265  Text of the function being assigned.  Text of the function being assigned.
2266    
2267    =item assigningUser (optional)
2268    
2269    Name of the individual user making the assignment. If omitted, defaults to the user group.
2270    
2271  =item RETURN  =item RETURN
2272    
2273  Returns 1 if successful, 0 if an error occurred.  Returns 1 if successful, 0 if an error occurred.
# Line 1686  Line 2278 
2278  #: Return Type $;  #: Return Type $;
2279  sub AssignFunction {  sub AssignFunction {
2280          # Get the parameters.          # Get the parameters.
2281          my ($self, $featureID, $user, $function) = @_;      my ($self, $featureID, $user, $function, $assigningUser) = @_;
2282        # Default the assigning user.
2283        if (! $assigningUser) {
2284            $assigningUser = $user;
2285        }
2286          # Create an annotation string from the parameters.          # Create an annotation string from the parameters.
2287          my $annotationText = "$user\nset $user function to\n$function";      my $annotationText = "$assigningUser\nset $user function to\n$function";
2288          # Get the current time.          # Get the current time.
2289          my $now = time;          my $now = time;
2290          # Declare the return variable.          # Declare the return variable.
# Line 1749  Line 2345 
2345          return @retVal;          return @retVal;
2346  }  }
2347    
2348  =head3 Exists  =head3 FeatureTranslation
2349    
2350  C<< my $found = $sprout->Exists($entityName, $entityID); >>  C<< my $translation = $sprout->FeatureTranslation($featureID); >>
2351    
2352  Return TRUE if an entity exists, else FALSE.  Return the translation of a feature.
2353    
2354  =over 4  =over 4
2355    
2356  =item entityName  =item featureID
   
 Name of the entity type (e.g. C<Feature>) relevant to the existence check.  
   
 =item entityID  
2357    
2358  ID of the entity instance whose existence is to be checked.  ID of the feature whose translation is desired
2359    
2360  =item RETURN  =item RETURN
2361    
2362  Returns TRUE if the entity instance exists, else FALSE.  Returns the translation of the specified feature.
2363    
2364  =back  =back
2365    
2366  =cut  =cut
2367  #: Return Type $;  #: Return Type $;
2368  sub Exists {  sub FeatureTranslation {
         # Get the parameters.  
         my ($self, $entityName, $entityID) = @_;  
         # Check for the entity instance.  
         my $testInstance = $self->GetEntity($entityName, $entityID);  
         # Return an existence indicator.  
         my $retVal = ($testInstance ? 1 : 0);  
         return $retVal;  
 }  
   
 =head3 FeatureTranslation  
   
 C<< my $translation = $sprout->FeatureTranslation($featureID); >>  
   
 Return the translation of a feature.  
   
 =over 4  
   
 =item featureID  
   
 ID of the feature whose translation is desired  
   
 =item RETURN  
   
 Returns the translation of the specified feature.  
   
 =back  
   
 =cut  
 #: Return Type $;  
 sub FeatureTranslation {  
2369          # Get the parameters.          # Get the parameters.
2370          my ($self, $featureID) = @_;          my ($self, $featureID) = @_;
2371          # Get the specified feature's translation.          # Get the specified feature's translation.
# Line 1968  Line 2530 
2530          return @retVal;          return @retVal;
2531  }  }
2532    
2533    =head3 GetProperties
2534    
2535    C<< my @list = $sprout->GetProperties($fid, $key, $value, $url); >>
2536    
2537    Return a list of the properties with the specified characteristics.
2538    
2539    Properties are arbitrary key-value pairs associated with a feature. (At some point they
2540    will also be associated with genomes.) A property value is represented by a 4-tuple of
2541    the form B<($fid, $key, $value, $url)>. These exactly correspond to the parameter
2542    
2543    =over 4
2544    
2545    =item fid
2546    
2547    ID of the feature possessing the property.
2548    
2549    =item key
2550    
2551    Name or key of the property.
2552    
2553    =item value
2554    
2555    Value of the property.
2556    
2557    =item url
2558    
2559    URL of the document that indicated the property should have this particular value, or an
2560    empty string if no such document exists.
2561    
2562    =back
2563    
2564    The parameters act as a filter for the desired data. Any non-null parameter will
2565    automatically match all the tuples returned. So, specifying just the I<$fid> will
2566    return all the properties of the specified feature; similarly, specifying the I<$key>
2567    and I<$value> parameters will return all the features having the specified property
2568    value.
2569    
2570    A single property key can have many values, representing different ideas about the
2571    feature in question. For example, one paper may declare that a feature C<fig|83333.1.peg.10> is
2572    virulent, and another may declare that it is not virulent. A query about the virulence of
2573    C<fig|83333.1.peg.10> would be coded as
2574    
2575        my @list = $sprout->GetProperties('fig|83333.1.peg.10', 'virulence', '', '');
2576    
2577    Here the I<$value> and I<$url> fields are left blank, indicating that those fields are
2578    not to be filtered. The tuples returned would be
2579    
2580        ('fig|83333.1.peg.10', 'virulence', 'yes', 'http://www.somewhere.edu/first.paper.pdf')
2581        ('fig|83333.1.peg.10', 'virulence', 'no', 'http://www.somewhere.edu/second.paper.pdf')
2582    
2583    =cut
2584    #: Return Type @@;
2585    sub GetProperties {
2586        # Get the parameters.
2587        my ($self, @parms) = @_;
2588        # Declare the return variable.
2589        my @retVal = ();
2590        # Now we need to create a WHERE clause that will get us the data we want. First,
2591        # we create a list of the columns containing the data for each parameter.
2592        my @colNames = ('HasProperty(from-link)', 'Property(property-name)',
2593                        'Property(property-value)', 'HasProperty(evidence)');
2594        # Now we build the WHERE clause and the list of parameter values.
2595        my @where = ();
2596        my @values = ();
2597        for (my $i = 0; $i <= $#colNames; $i++) {
2598            my $parm = $parms[$i];
2599            if (defined $parm && ($parm ne '')) {
2600                push @where, "$colNames[$i] = ?";
2601                push @values, $parm;
2602            }
2603        }
2604        # Format the WHERE clause.
2605        my $filter = (@values > 0 ? (join " AND ", @where) : undef);
2606        # Ask for all the propertie values with the desired characteristics.
2607        my $query = $self->Get(['HasProperty', 'Property'], $filter, \@values);
2608        while (my $valueObject = $query->Fetch()) {
2609            my @tuple = $valueObject->Values(\@colNames);
2610            push @retVal, \@tuple;
2611        }
2612        # Return the result.
2613        return @retVal;
2614    }
2615    
2616  =head3 FeatureProperties  =head3 FeatureProperties
2617    
2618  C<< my @properties = $sprout->FeatureProperties($featureID); >>  C<< my @properties = $sprout->FeatureProperties($featureID); >>
# Line 2162  Line 2807 
2807  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>
2808    
2809  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
2810  to the role the feature performs.  to the roles the feature performs.
2811    
2812  =over 4  =over 4
2813    
# Line 2172  Line 2817 
2817    
2818  =item RETURN  =item RETURN
2819    
2820  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.
2821    
2822  =back  =back
2823    
2824  =cut  =cut
2825  #: Return Type %;  #: Return Type %@;
2826  sub SubsystemsOf {  sub SubsystemsOf {
2827          # Get the parameters.          # Get the parameters.
2828          my ($self, $featureID) = @_;          my ($self, $featureID) = @_;
2829          # Use the SSCell to connect features to subsystems.      # Get the subsystem list.
2830          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],
2831                                                                          "ContainsFeature(to-link) = ?", [$featureID],                                                                          "ContainsFeature(to-link) = ?", [$featureID],
2832                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);
2833          # Create the return value.          # Create the return value.
2834          my %retVal = ();          my %retVal = ();
2835        # Build a hash to weed out duplicates. Sometimes the same PEG and role appears
2836        # in two spreadsheet cells.
2837        my %dupHash = ();
2838          # Loop through the results, adding them to the hash.          # Loop through the results, adding them to the hash.
2839          for my $record (@subsystems) {          for my $record (@subsystems) {
2840                  $retVal{$record->[0]} = $record->[1];          # Get this subsystem and role.
2841            my ($subsys, $role) = @{$record};
2842            # Insure it's the first time for both.
2843            my $dupKey = "$subsys\n$role";
2844            if (! exists $dupHash{"$subsys\n$role"}) {
2845                $dupHash{$dupKey} = 1;
2846                push @{$retVal{$subsys}}, $role;
2847            }
2848          }          }
2849          # Return the hash.          # Return the hash.
2850          return %retVal;          return %retVal;
2851  }  }
2852    
2853    =head3 SubsystemList
2854    
2855    C<< my @subsystems = $sprout->SubsystemList($featureID); >>
2856    
2857    Return a list containing the names of the subsystems in which the specified
2858    feature participates. Unlike L</SubsystemsOf>, this method only returns the
2859    subsystem names, not the roles.
2860    
2861    =over 4
2862    
2863    =item featureID
2864    
2865    ID of the feature whose subsystem names are desired.
2866    
2867    =item RETURN
2868    
2869    Returns a list of the names of the subsystems in which the feature participates.
2870    
2871    =back
2872    
2873    =cut
2874    #: Return Type @;
2875    sub SubsystemList {
2876        # Get the parameters.
2877        my ($self, $featureID) = @_;
2878        # Get the list of names.
2879        my @retVal = $self->GetFlat(['ContainsFeature', 'HasSSCell'], "ContainsFeature(to-link) = ?",
2880                                    [$featureID], 'HasSSCell(from-link)');
2881        # Return the result.
2882        return @retVal;
2883    }
2884    
2885    =head3 GenomeSubsystemData
2886    
2887    C<< my %featureData = $sprout->GenomeSubsystemData($genomeID); >>
2888    
2889    Return a hash mapping genome features to their subsystem roles.
2890    
2891    =over 4
2892    
2893    =item genomeID
2894    
2895    ID of the genome whose subsystem feature map is desired.
2896    
2897    =item RETURN
2898    
2899    Returns a hash mapping each feature of the genome to a list of 2-tuples. Eacb
2900    2-tuple contains a subsystem name followed by a role ID.
2901    
2902    =back
2903    
2904    =cut
2905    
2906    sub GenomeSubsystemData {
2907        # Get the parameters.
2908        my ($self, $genomeID) = @_;
2909        # Declare the return variable.
2910        my %retVal = ();
2911        # Get a list of the genome features that participate in subsystems. For each
2912        # feature we get its spreadsheet cells and the corresponding roles.
2913        my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf'],
2914                                 "HasFeature(from-link) = ?", [$genomeID],
2915                                 ['HasFeature(to-link)', 'IsRoleOf(to-link)', 'IsRoleOf(from-link)']);
2916        # Now we get a list of the spreadsheet cells and their associated subsystems. Subsystems
2917        # with an unknown variant code (-1) are skipped. Note the genome ID is at both ends of the
2918        # list. We use it at the beginning to get all the spreadsheet cells for the genome and
2919        # again at the end to filter out participation in subsystems with a negative variant code.
2920        my @cellData = $self->GetAll(['IsGenomeOf', 'HasSSCell', 'ParticipatesIn'],
2921                                     "IsGenomeOf(from-link) = ? AND ParticipatesIn(variant-code) >= 0 AND ParticipatesIn(from-link) = ?",
2922                                     [$genomeID, $genomeID], ['HasSSCell(to-link)', 'HasSSCell(from-link)']);
2923        # Now "@roleData" lists the spreadsheet cell and role for each of the genome's features.
2924        # "@cellData" lists the subsystem name for each of the genome's spreadsheet cells. We
2925        # link these two lists together to create the result. First, we want a hash mapping
2926        # spreadsheet cells to subsystem names.
2927        my %subHash = map { $_->[0] => $_->[1] } @cellData;
2928        # We loop through @cellData to build the hash.
2929        for my $roleEntry (@roleData) {
2930            # Get the data for this feature and cell.
2931            my ($fid, $cellID, $role) = @{$roleEntry};
2932            # Check for a subsystem name.
2933            my $subsys = $subHash{$cellID};
2934            if ($subsys) {
2935                # Insure this feature has an entry in the return hash.
2936                if (! exists $retVal{$fid}) { $retVal{$fid} = []; }
2937                # Merge in this new data.
2938                push @{$retVal{$fid}}, [$subsys, $role];
2939            }
2940        }
2941        # Return the result.
2942        return %retVal;
2943    }
2944    
2945  =head3 RelatedFeatures  =head3 RelatedFeatures
2946    
2947  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>
# Line 2230  Line 2977 
2977          # Get the parameters.          # Get the parameters.
2978          my ($self, $featureID, $function, $userID) = @_;          my ($self, $featureID, $function, $userID) = @_;
2979          # Get a list of the features that are BBHs of the incoming feature.          # Get a list of the features that are BBHs of the incoming feature.
2980          my @bbhFeatures = $self->GetFlat(['IsBidirectionalBestHitOf'],      my @bbhFeatures = map { $_->[0] } FIGRules::BBHData($featureID);
                                                                          "IsBidirectionalBestHitOf(from-link) = ?", [$featureID],  
                                                                          'IsBidirectionalBestHitOf(to-link)');  
2981          # Now we loop through the features, pulling out the ones that have the correct          # Now we loop through the features, pulling out the ones that have the correct
2982          # functional assignment.          # functional assignment.
2983          my @retVal = ();          my @retVal = ();
# Line 2296  Line 3041 
3041          return @retVal;          return @retVal;
3042  }  }
3043    
 =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;  
 }  
   
3044  =head3 Protein  =head3 Protein
3045    
3046  C<< my $protein = Sprout::Protein($sequence, $table); >>  C<< my $protein = Sprout::Protein($sequence, $table); >>
# Line 2529  Line 3142 
3142          # 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.
3143          my @retVal = ($self->{_options}->{dataDir});          my @retVal = ($self->{_options}->{dataDir});
3144          # Concatenate the table names.          # Concatenate the table names.
3145          push @retVal, $self->{_erdb}->GetTableNames();      push @retVal, $self->GetTableNames();
3146          # Return the result.          # Return the result.
3147          return @retVal;          return @retVal;
3148  }  }
3149    
3150  =head3 LowBBHs  =head3 LowBBHs
3151    
3152  C<< my %bbhMap = $sprout->GoodBBHs($featureID, $cutoff); >>  C<< my %bbhMap = $sprout->LowBBHs($featureID, $cutoff); >>
3153    
3154  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
3155  specified cutoff value. A higher cutoff value will allow inclusion of hits with  specified cutoff value. A higher cutoff value will allow inclusion of hits with
# Line 2565  Line 3178 
3178          my ($self, $featureID, $cutoff) = @_;          my ($self, $featureID, $cutoff) = @_;
3179          # Create the return hash.          # Create the return hash.
3180          my %retVal = ();          my %retVal = ();
3181          # Create a query to get the desired BBHs.      # Query for the desired BBHs.
3182          my @bbhList = $self->GetAll(['IsBidirectionalBestHitOf'],      my @bbhList = FIGRules::BBHData($featureID, $cutoff);
                                                                 'IsBidirectionalBestHitOf(sc) <= ? AND IsBidirectionalBestHitOf(from-link) = ?',  
                                                                 [$cutoff, $featureID],  
                                                                 ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(sc)']);  
3183          # Form the results into the return hash.          # Form the results into the return hash.
3184          for my $pair (@bbhList) {          for my $pair (@bbhList) {
3185                  $retVal{$pair->[0]} = $pair->[1];                  $retVal{$pair->[0]} = $pair->[1];
# Line 2578  Line 3188 
3188          return %retVal;          return %retVal;
3189  }  }
3190    
3191    =head3 Sims
3192    
3193    C<< my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters); >>
3194    
3195    Get a list of similarities for a specified feature. Similarity information is not kept in the
3196    Sprout database; rather, they are retrieved from a network server. The similarities are
3197    returned as B<Sim> objects. A Sim object is actually a list reference that has been blessed
3198    so that its elements can be accessed by name.
3199    
3200    Similarities can be either raw or expanded. The raw similarities are basic
3201    hits between features with similar DNA. Expanding a raw similarity drags in any
3202    features considered substantially identical. So, for example, if features B<A1>,
3203    B<A2>, and B<A3> are all substatially identical to B<A>, then a raw similarity
3204    B<[C,A]> would be expanded to B<[C,A] [C,A1] [C,A2] [C,A3]>.
3205    
3206    =over 4
3207    
3208    =item fid
3209    
3210    ID of the feature whose similarities are desired.
3211    
3212    =item maxN
3213    
3214    Maximum number of similarities to return.
3215    
3216    =item maxP
3217    
3218    Minumum allowable similarity score.
3219    
3220    =item select
3221    
3222    Selection criterion: C<raw> means only raw similarities are returned; C<fig>
3223    means only similarities to FIG features are returned; C<all> means all expanded
3224    similarities are returned; and C<figx> means similarities are expanded until the
3225    number of FIG features equals the maximum.
3226    
3227    =item max_expand
3228    
3229    The maximum number of features to expand.
3230    
3231    =item filters
3232    
3233    Reference to a hash containing filter information, or a subroutine that can be
3234    used to filter the sims.
3235    
3236    =item RETURN
3237    
3238    Returns a reference to a list of similarity objects, or C<undef> if an error
3239    occurred.
3240    
3241    =back
3242    
3243    =cut
3244    
3245    sub Sims {
3246        # Get the parameters.
3247        my ($self, $fid, $maxN, $maxP, $select, $max_expand, $filters) = @_;
3248        # Create the shim object to test for deleted FIDs.
3249        my $shim = FidCheck->new($self);
3250        # Ask the network for sims.
3251        my $retVal = FIGRules::GetNetworkSims($shim, $fid, {}, $maxN, $maxP, $select, $max_expand, $filters);
3252        # Return the result.
3253        return $retVal;
3254    }
3255    
3256  =head3 GetGroups  =head3 GetGroups
3257    
3258  C<< my %groups = $sprout->GetGroups(\@groupList); >>  C<< my %groups = $sprout->GetGroups(\@groupList); >>
# Line 2599  Line 3274 
3274          # Here we have a group list. Loop through them individually,          # Here we have a group list. Loop through them individually,
3275          # getting a list of the relevant genomes.          # getting a list of the relevant genomes.
3276          for my $group (@{$groupList}) {          for my $group (@{$groupList}) {
3277              my @genomeIDs = $self->GetFlat(['Genome'], "Genome(group-name) = ?",              my @genomeIDs = $self->GetFlat(['Genome'], "Genome(primary-group) = ?",
3278                  [$group], "Genome(id)");                  [$group], "Genome(id)");
3279              $retVal{$group} = \@genomeIDs;              $retVal{$group} = \@genomeIDs;
3280          }          }
# Line 2607  Line 3282 
3282          # Here we need all of the groups. In this case, we run through all          # Here we need all of the groups. In this case, we run through all
3283          # of the genome records, putting each one found into the appropriate          # of the genome records, putting each one found into the appropriate
3284          # group. Note that we use a filter clause to insure that only genomes          # group. Note that we use a filter clause to insure that only genomes
3285          # in groups are included in the return set.          # in real NMPDR groups are included in the return set.
3286          my @genomes = $self->GetAll(['Genome'], "Genome(group-name) > ' '", [],          my @genomes = $self->GetAll(['Genome'], "Genome(primary-group) <> ?",
3287                                      ['Genome(id)', 'Genome(group-name)']);                                      [$FIG_Config::otherGroup], ['Genome(id)', 'Genome(primary-group)']);
3288          # Loop through the genomes found.          # Loop through the genomes found.
3289          for my $genome (@genomes) {          for my $genome (@genomes) {
3290              # Pop this genome's ID off the current list.              # Pop this genome's ID off the current list.
# Line 2626  Line 3301 
3301      return %retVal;      return %retVal;
3302  }  }
3303    
3304    =head3 MyGenomes
3305    
3306    C<< my @genomes = Sprout::MyGenomes($dataDir); >>
3307    
3308    Return a list of the genomes to be included in the Sprout.
3309    
3310    This method is provided for use during the Sprout load. It presumes the Genome load file has
3311    already been created. (It will be in the Sprout data directory and called either C<Genome>
3312    or C<Genome.dtx>.) Essentially, it reads in the Genome load file and strips out the genome
3313    IDs.
3314    
3315    =over 4
3316    
3317    =item dataDir
3318    
3319    Directory containing the Sprout load files.
3320    
3321    =back
3322    
3323    =cut
3324    #: Return Type @;
3325    sub MyGenomes {
3326        # Get the parameters.
3327        my ($dataDir) = @_;
3328        # Compute the genome file name.
3329        my $genomeFileName = LoadFileName($dataDir, "Genome");
3330        # Extract the genome IDs from the files.
3331        my @retVal = map { $_ =~ /^(\S+)/; $1 } Tracer::GetFile($genomeFileName);
3332        # Return the result.
3333        return @retVal;
3334    }
3335    
3336    =head3 LoadFileName
3337    
3338    C<< my $fileName = Sprout::LoadFileName($dataDir, $tableName); >>
3339    
3340    Return the name of the load file for the specified table in the specified data
3341    directory.
3342    
3343    =over 4
3344    
3345    =item dataDir
3346    
3347    Directory containing the Sprout load files.
3348    
3349    =item tableName
3350    
3351    Name of the table whose load file is desired.
3352    
3353    =item RETURN
3354    
3355    Returns the name of the file containing the load data for the specified table, or
3356    C<undef> if no load file is present.
3357    
3358    =back
3359    
3360    =cut
3361    #: Return Type $;
3362    sub LoadFileName {
3363        # Get the parameters.
3364        my ($dataDir, $tableName) = @_;
3365        # Declare the return variable.
3366        my $retVal;
3367        # Check for the various file names.
3368        if (-e "$dataDir/$tableName") {
3369            $retVal = "$dataDir/$tableName";
3370        } elsif (-e "$dataDir/$tableName.dtx") {
3371            $retVal = "$dataDir/$tableName.dtx";
3372        }
3373        # Return the result.
3374        return $retVal;
3375    }
3376    
3377    =head3 DeleteGenome
3378    
3379    C<< my $stats = $sprout->DeleteGenome($genomeID, $testFlag); >>
3380    
3381    Delete a genome from the database.
3382    
3383    =over 4
3384    
3385    =item genomeID
3386    
3387    ID of the genome to delete
3388    
3389    =item testFlag
3390    
3391    If TRUE, then the DELETE statements will be traced, but no deletions will occur.
3392    
3393    =item RETURN
3394    
3395    Returns a statistics object describing the rows deleted.
3396    
3397    =back
3398    
3399    =cut
3400    #: Return Type $%;
3401    sub DeleteGenome {
3402        # Get the parameters.
3403        my ($self, $genomeID, $testFlag) = @_;
3404        # Perform the delete for the genome's features.
3405        my $retVal = $self->Delete('Feature', "fig|$genomeID.%", $testFlag);
3406        # Perform the delete for the primary genome data.
3407        my $stats = $self->Delete('Genome', $genomeID, $testFlag);
3408        $retVal->Accumulate($stats);
3409        # Return the result.
3410        return $retVal;
3411    }
3412    
3413    =head3 Fix
3414    
3415    C<< my %fixedHash = Sprout::Fix(%groupHash); >>
3416    
3417    Prepare a genome group hash (like that returned by L</GetGroups> for processing.
3418    Groups with the same primary name will be combined. The primary name is the
3419    first capitalized word in the group name.
3420    
3421    =over 4
3422    
3423    =item groupHash
3424    
3425    Hash to be fixed up.
3426    
3427    =item RETURN
3428    
3429    Returns a fixed-up version of the hash.
3430    
3431    =back
3432    
3433    =cut
3434    
3435    sub Fix {
3436        # Get the parameters.
3437        my (%groupHash) = @_;
3438        # Create the result hash.
3439        my %retVal = ();
3440        # Copy over the genomes.
3441        for my $groupID (keys %groupHash) {
3442            # Make a safety copy of the group ID.
3443            my $realGroupID = $groupID;
3444            # Yank the primary name.
3445            if ($groupID =~ /([A-Z]\w+)/) {
3446                $realGroupID = $1;
3447            }
3448            # Append this group's genomes into the result hash.
3449            Tracer::AddToListMap(\%retVal, $realGroupID, @{$groupHash{$groupID}});
3450        }
3451        # Return the result hash.
3452        return %retVal;
3453    }
3454    
3455    =head3 GroupPageName
3456    
3457    C<< my $name = $sprout->GroupPageName($group); >>
3458    
3459    Return the name of the page for the specified NMPDR group.
3460    
3461    =over 4
3462    
3463    =item group
3464    
3465    Name of the relevant group.
3466    
3467    =item RETURN
3468    
3469    Returns the relative page name (e.g. C<../content/campy.php>). If the group file is not in
3470    memory it will be read in.
3471    
3472    =back
3473    
3474    =cut
3475    
3476    sub GroupPageName {
3477        # Get the parameters.
3478        my ($self, $group) = @_;
3479        # Declare the return variable.
3480        my $retVal;
3481        # Check for the group file data.
3482        if (! defined $self->{groupHash}) {
3483            # Read the group file.
3484            my %groupData = Sprout::ReadGroupFile($self->{_options}->{dataDir} . "/groups.tbl");
3485            # Store it in our object.
3486            $self->{groupHash} = \%groupData;
3487        }
3488        # Compute the real group name.
3489        my $realGroup = $group;
3490        if ($group =~ /([A-Z]\w+)/) {
3491            $realGroup = $1;
3492        }
3493        # Return the page name.
3494        $retVal = "../content/" . $self->{groupHash}->{$realGroup}->[1];
3495        # Return the result.
3496        return $retVal;
3497    }
3498    
3499    =head3 ReadGroupFile
3500    
3501    C<< my %groupData = Sprout::ReadGroupFile($groupFileName); >>
3502    
3503    Read in the data from the specified group file. The group file contains information
3504    about each of the NMPDR groups.
3505    
3506    =over 4
3507    
3508    =item name
3509    
3510    Name of the group.
3511    
3512    =item page
3513    
3514    Name of the group's page on the web site (e.g. C<campy.php> for
3515    Campylobacter)
3516    
3517    =item genus
3518    
3519    Genus of the group
3520    
3521    =item species
3522    
3523    Species of the group, or an empty string if the group is for an entire
3524    genus. If the group contains more than one species, the species names
3525    should be separated by commas.
3526    
3527    =back
3528    
3529    The parameters to this method are as follows
3530    
3531    =over 4
3532    
3533    =item groupFile
3534    
3535    Name of the file containing the group data.
3536    
3537    =item RETURN
3538    
3539    Returns a hash keyed on group name. The value of each hash
3540    
3541    =back
3542    
3543    =cut
3544    
3545    sub ReadGroupFile {
3546        # Get the parameters.
3547        my ($groupFileName) = @_;
3548        # Declare the return variable.
3549        my %retVal;
3550        # Read the group file.
3551        my @groupLines = Tracer::GetFile($groupFileName);
3552        for my $groupLine (@groupLines) {
3553            my ($name, $page, $genus, $species) = split(/\t/, $groupLine);
3554            $retVal{$name} = [$page, $genus, $species];
3555        }
3556        # Return the result.
3557        return %retVal;
3558    }
3559    
3560  =head2 Internal Utility Methods  =head2 Internal Utility Methods
3561    
3562  =head3 ParseAssignment  =head3 ParseAssignment
3563    
3564  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,
3565  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
3566  will be returned.  isn't, an empty list will be returned.
3567    
3568    A functional assignment is always of the form
3569    
3570        C<set >I<YYYY>C< function to\n>I<ZZZZZ>
3571    
3572    where I<YYYY> is the B<user>, and I<ZZZZ> is the actual functional role. In most cases,
3573    the user and the assigning user (from MadeAnnotation) will be the same, but that is
3574    not always the case.
3575    
3576    In addition, the functional role may contain extra data that is stripped, such as
3577    terminating spaces or a comment separated from the rest of the text by a tab.
3578    
3579  This is a static method.  This is a static method.
3580    
3581  =over 4  =over 4
3582    
3583    =item user
3584    
3585    Name of the assigning user.
3586    
3587  =item text  =item text
3588    
3589  Text of the annotation.  Text of the annotation.
# Line 2651  Line 3597 
3597    
3598  =cut  =cut
3599    
3600  sub ParseAssignment {  sub _ParseAssignment {
3601          # Get the parameters.          # Get the parameters.
3602          my ($text) = @_;      my ($user, $text) = @_;
3603          # Declare the return value.          # Declare the return value.
3604          my @retVal = ();          my @retVal = ();
3605          # Check to see if this is a functional assignment.          # Check to see if this is a functional assignment.
3606          my ($user, $type, $function) = split(/\n/, $text);      my ($type, $function) = split(/\n/, $text);
3607          if ($type =~ m/^set $user function to$/i) {      if ($type =~ m/^set function to$/i) {
3608                  # 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.
3609                  @retVal = ($user, $function);                  @retVal = ($user, $function);
3610        } elsif ($type =~ m/^set (\S+) function to$/i) {
3611            # Here we have an assignment with a user that is passed back to the caller.
3612            @retVal = ($1, $function);
3613        }
3614        # If we have an assignment, we need to clean the function text. There may be
3615        # extra junk at the end added as a note from the user.
3616        if (defined( $retVal[1] )) {
3617            $retVal[1] =~ s/(\t\S)?\s*$//;
3618          }          }
3619          # Return the result list.          # Return the result list.
3620          return @retVal;          return @retVal;
# Line 2688  Line 3642 
3642    
3643  sub FriendlyTimestamp {  sub FriendlyTimestamp {
3644      my ($timeValue) = @_;      my ($timeValue) = @_;
3645      my $retVal = strftime("%a %b %e %H:%M:%S %Y", localtime($timeValue));      my $retVal = localtime($timeValue);
3646      return $retVal;      return $retVal;
3647  }  }
3648    
3649    =head3 AddProperty
3650    
3651    C<< my  = $sprout->AddProperty($featureID, $key, $value, $url); >>
3652    
3653    Add a new attribute value (Property) to a feature. In the SEED system, attributes can
3654    be added to almost any object. In Sprout, they can only be added to features. In
3655    Sprout, attributes are implemented using I<properties>. A property represents a key/value
3656    pair. If the particular key/value pair coming in is not already in the database, a new
3657    B<Property> record is created to hold it.
3658    
3659    =over 4
3660    
3661    =item peg
3662    
3663    ID of the feature to which the attribute is to be replied.
3664    
3665    =item key
3666    
3667    Name of the attribute (key).
3668    
3669    =item value
3670    
3671    Value of the attribute.
3672    
3673    =item url
3674    
3675    URL or text citation from which the property was obtained.
3676    
3677    =back
3678    
3679    =cut
3680    #: Return Type ;
3681    sub AddProperty {
3682        # Get the parameters.
3683        my ($self, $featureID, $key, $value, $url) = @_;
3684        # Declare the variable to hold the desired property ID.
3685        my $propID;
3686        # Attempt to find a property record for this key/value pair.
3687        my @properties = $self->GetFlat(['Property'],
3688                                       "Property(property-name) = ? AND Property(property-value) = ?",
3689                                       [$key, $value], 'Property(id)');
3690        if (@properties) {
3691            # Here the property is already in the database. We save its ID.
3692            $propID = $properties[0];
3693            # Here the property value does not exist. We need to generate an ID. It will be set
3694            # to a number one greater than the maximum value in the database. This call to
3695            # GetAll will stop after one record.
3696            my @maxProperty = $self->GetAll(['Property'], "ORDER BY Property(id) DESC", [], ['Property(id)'],
3697                                            1);
3698            $propID = $maxProperty[0]->[0] + 1;
3699            # Insert the new property value.
3700            $self->Insert('Property', { 'property-name' => $key, 'property-value' => $value, id => $propID });
3701        }
3702        # Now we connect the incoming feature to the property.
3703        $self->Insert('HasProperty', { 'from-link' => $featureID, 'to-link' => $propID, evidence => $url });
3704    }
3705    
3706    
3707  1;  1;

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3