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

Diff of /Sprout/Sprout.pm

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

revision 1.7, Wed Jan 26 22:26:09 2005 UTC revision 1.99, Fri Apr 27 22:21:46 2007 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;
         use Carp;  
8          use DBKernel;          use DBKernel;
9          use XML::Simple;          use XML::Simple;
10          use DBQuery;          use DBQuery;
11          use DBObject;      use ERDBObject;
         use ERDB;  
12          use Tracer;          use Tracer;
13          use FIGRules;          use FIGRules;
14        use FidCheck;
15          use Stats;          use Stats;
16      use POSIX qw(strftime);      use POSIX qw(strftime);
17        use BasicLocation;
18        use CustomAttributes;
19        use RemoteCustomAttributes;
20    
21  =head1 Sprout Database Manipulation Object  =head1 Sprout Database Manipulation Object
22    
# Line 32  Line 36 
36  query tasks. For example, L</genomes> lists the IDs of all the genomes in the database and  query tasks. For example, L</genomes> lists the IDs of all the genomes in the database and
37  L</dna_seq> returns the DNA sequence for a specified genome location.  L</dna_seq> returns the DNA sequence for a specified genome location.
38    
39    The Sprout object is a subclass of the ERDB object and inherits all its properties and methods.
40    
41  =cut  =cut
42    
43  #: Constructor SFXlate->new_sprout_only();  #: Constructor SFXlate->new_sprout_only();
# Line 62  Line 68 
68    
69  * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)  * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)
70    
71  * B<userData> user name and password, delimited by a slash (default C<root/>)  * B<userData> user name and password, delimited by a slash (default same as SEED)
72    
73  * B<port> connection port (default C<0>)  * B<port> connection port (default C<0>)
74    
75    * B<sock> connection socket (default same as SEED)
76    
77  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)
78    
79  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)
80    
81    * B<noDBOpen> suppresses the connection to the database if TRUE, else FALSE
82    
83  =back  =back
84    
85  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 93 
93  sub new {  sub new {
94          # Get the parameters.          # Get the parameters.
95          my ($class, $dbName, $options) = @_;          my ($class, $dbName, $options) = @_;
96        # Compute the DBD directory.
97        my $dbd_dir = (defined($FIG_Config::dbd_dir) ? $FIG_Config::dbd_dir :
98                                                      $FIG_Config::fig );
99          # 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
100          # the incoming data.          # the incoming data.
101          my $optionTable = Tracer::GetOptions({          my $optionTable = Tracer::GetOptions({
102                                             dbType               => 'mysql',                     # database type                         dbType       => $FIG_Config::dbms,
103                                             dataDir              => 'Data',                      # data file directory                                                          # database type
104                                             xmlFileName  => 'SproutDBD.xml', # database definition file name                         dataDir      => $FIG_Config::sproutData,
105                                             userData             => 'root/',                     # user name and password                                                          # data file directory
106                                             port                 => 0,                           # database connection port                         xmlFileName  => "$dbd_dir/SproutDBD.xml",
107                                                            # database definition file name
108                           userData     => "$FIG_Config::dbuser/$FIG_Config::dbpass",
109                                                            # user name and password
110                           port         => $FIG_Config::dbport,
111                                                            # database connection port
112                           sock         => $FIG_Config::dbsock,
113                           host         => $FIG_Config::dbhost,
114                                             maxSegmentLength => 4500,            # maximum feature segment length                                             maxSegmentLength => 4500,            # maximum feature segment length
115                                             maxSequenceLength => 8000,           # maximum contig sequence length                                             maxSequenceLength => 8000,           # maximum contig sequence length
116                           noDBOpen     => 0,               # 1 to suppress the database open
117                                            }, $options);                                            }, $options);
118          # Get the data directory.          # Get the data directory.
119          my $dataDir = $optionTable->{dataDir};          my $dataDir = $optionTable->{dataDir};
# Line 100  Line 121 
121          $optionTable->{userData} =~ m!([^/]*)/(.*)$!;          $optionTable->{userData} =~ m!([^/]*)/(.*)$!;
122          my ($userName, $password) = ($1, $2);          my ($userName, $password) = ($1, $2);
123          # Connect to the database.          # Connect to the database.
124          my $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName, $password, $optionTable->{port});      my $dbh;
125        if (! $optionTable->{noDBOpen}) {
126            $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,
127                                    $password, $optionTable->{port}, $optionTable->{host}, $optionTable->{sock});
128        }
129          # Create the ERDB object.          # Create the ERDB object.
130          my $xmlFileName = "$optionTable->{xmlFileName}";          my $xmlFileName = "$optionTable->{xmlFileName}";
131          my $erdb = ERDB->new($dbh, $xmlFileName);      my $retVal = ERDB::new($class, $dbh, $xmlFileName);
132          # Create this object.      # Add the option table and XML file name.
133          my $self = { _erdb => $erdb, _options => $optionTable, _xmlName => $xmlFileName };      $retVal->{_options} = $optionTable;
134          # Bless and return it.      $retVal->{_xmlName} = $xmlFileName;
135          bless $self;      # Set up space for the group file data.
136          return $self;      $retVal->{groupHash} = undef;
137        # Connect to the attributes.
138        if ($FIG_Config::attrURL) {
139            Trace("Remote attribute server $FIG_Config::attrURL chosen.") if T(3);
140            $retVal->{_ca} = RemoteCustomAttributes->new($FIG_Config::attrURL);
141        } elsif ($FIG_Config::attrDbName) {
142            Trace("Local attribute database $FIG_Config::attrDbName chosen.") if T(3);
143            my $user = ($FIG_Config::arch eq 'win' ? 'self' : scalar(getpwent()));
144            $retVal->{_ca} = CustomAttributes->new(user => $user);
145        }
146        # Return it.
147        return $retVal;
148  }  }
149    
150  =head3 MaxSegment  =head3 MaxSegment
# Line 124  Line 160 
160  =cut  =cut
161  #: Return Type $;  #: Return Type $;
162  sub MaxSegment {  sub MaxSegment {
163          my $self = shift @_;      my ($self) = @_;
164          return $self->{_options}->{maxSegmentLength};          return $self->{_options}->{maxSegmentLength};
165  }  }
166    
# Line 139  Line 175 
175  =cut  =cut
176  #: Return Type $;  #: Return Type $;
177  sub MaxSequence {  sub MaxSequence {
178          my $self = shift @_;      my ($self) = @_;
179          return $self->{_options}->{maxSequenceLength};          return $self->{_options}->{maxSequenceLength};
180  }  }
181    
182  =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]); >>  
183    
184  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.  
185    
186  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.
187    
188  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
189  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
190  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
191    extension are used in preference to the files with an extension.
192    
193  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
194  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
195  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
196  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.  
197    
198  =over 4  =over 4
199    
200  =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  
201    
202  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
203    
204  =item RETURN  =item RETURN
205    
206  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,
207    the number of errors, and a list of the error messages.
208    
209  =back  =back
210    
211  =cut  =cut
212    #: Return Type %;
213  sub Get {  sub Load {
214          # Get the parameters.          # Get the parameters.
215          my $self = shift @_;      my ($self, $rebuild) = @_;
216          my ($objectNames, $filterClause, $parameterList) = @_;      # Load the tables from the data directory.
217          # We differ from the ERDB Get method in that the parameter list is passed in as a list reference      my $retVal = $self->LoadTables($self->{_options}->{dataDir}, $rebuild);
218          # rather than a list of parameters. The next step is to convert the parameters from a reference      # Return the statistics.
219          # to a real list. We can only do this if the parameters have been specified.      return $retVal;
         my @parameters;  
         if ($parameterList) { @parameters = @{$parameterList}; }  
         return $self->{_erdb}->Get($objectNames, $filterClause, @parameters);  
220  }  }
221    
222  =head3 GetEntity  =head3 LoadUpdate
223    
224  C<< my $entityObject = $sprout->GetEntity($entityType, $ID); >>  C<< my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>
225    
226  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
227    or two tables without reloading the whole database. For each table, there must be a corresponding
228    file in the data directory, either with the same name as the table, or with a C<.dtx> suffix. So,
229    for example, to make updates to the B<FeatureTranslation> relation, there must be a
230    C<FeatureTranslation.dtx> file in the data directory. Unlike a full load, files without an extension
231    are not examined. This allows update files to co-exist with files from an original load.
232    
233  =over 4  =over 4
234    
235  =item entityType  =item truncateFlag
236    
237  Entity type name.  TRUE if the tables should be rebuilt before loading, else FALSE. A value of TRUE therefore causes
238    current data and schema of the tables to be replaced, while a value of FALSE means the new data
239    is added to the existing data in the various relations.
240    
241  =item ID  =item tableList
242    
243  ID of the desired entity.  List of the tables to be updated.
244    
245  =item RETURN  =item RETURN
246    
247  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,
248  instance is found with the specified key.  the number of errors encountered, and a list of error messages.
249    
250  =back  =back
251    
252  =cut  =cut
253    #: Return Type $%;
254  sub GetEntity {  sub LoadUpdate {
255          # Get the parameters.          # Get the parameters.
256          my $self = shift @_;      my ($self, $truncateFlag, $tableList) = @_;
257          my ($entityType, $ID) = @_;      # Declare the return value.
258          # Create a query.      my $retVal = Stats->new();
259          my $query = $self->Get([$entityType], "$entityType(id) = ?", [$ID]);      # Get the data directory.
260          # Get the first (and only) object.      my $optionTable = $self->{_options};
261          my $retVal = $query->Fetch();      my $dataDir = $optionTable->{dataDir};
262          # Return the result.      # Loop through the incoming table names.
263        for my $tableName (@{$tableList}) {
264            # Find the table's file.
265            my $fileName = LoadFileName($dataDir, $tableName);
266            if (! $fileName) {
267                Trace("No load file found for $tableName in $dataDir.") if T(0);
268            } else {
269                # Attempt to load this table.
270                my $result = $self->LoadTable($fileName, $tableName, $truncateFlag);
271                # Accumulate the resulting statistics.
272                $retVal->Accumulate($result);
273            }
274        }
275        # Return the statistics.
276          return $retVal;          return $retVal;
277  }  }
278    
279  =head3 GetEntityValues  =head3 GenomeCounts
280    
281  C<< my @values = GetEntityValues($entityType, $ID, \@fields); >>  C<< my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete); >>
282    
283  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
284    genomes will be included in the counts.
285    
286  =over 4  =over 4
287    
288  =item entityType  =item complete
   
 Entity type name.  
   
 =item ID  
   
 ID of the desired entity.  
289    
290  =item fields  TRUE if only complete genomes are to be counted, FALSE if all genomes are to be
291    counted
 List of field names, each of the form I<objectName>C<(>I<fieldName>C<)>.  
292    
293  =item RETURN  =item RETURN
294    
295  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--
296    Archaea, Bacteria, Eukaryota, Viral, Environmental, and Unknown, respectively.
297    
298  =back  =back
299    
300  =cut  =cut
301  #: Return Type @;  
302  sub GetEntityValues {  sub GenomeCounts {
303          # Get the parameters.          # Get the parameters.
304          my $self = shift @_;      my ($self, $complete) = @_;
305          my ($entityType, $ID, $fields) = @_;      # Set the filter based on the completeness flag.
306          # Get the specified entity.      my $filter = ($complete ? "Genome(complete) = 1" : "");
307          my $entity = $self->GetEntity($entityType, $ID);      # Get all the genomes and the related taxonomy information.
308          # Declare the return list.      my @genomes = $self->GetAll(['Genome'], $filter, [], ['Genome(id)', 'Genome(taxonomy)']);
309          my @retVal = ();      # Clear the counters.
310          # If we found the entity, push the values into the return list.      my ($arch, $bact, $euk, $vir, $env, $unk) = (0, 0, 0, 0, 0, 0);
311          if ($entity) {      # Loop through, counting the domains.
312                  push @retVal, $entity->Values($fields);      for my $genome (@genomes) {
313            if    ($genome->[1] =~ /^archaea/i)  { ++$arch }
314            elsif ($genome->[1] =~ /^bacter/i)   { ++$bact }
315            elsif ($genome->[1] =~ /^eukar/i)    { ++$euk }
316            elsif ($genome->[1] =~ /^vir/i)      { ++$vir }
317            elsif ($genome->[1] =~ /^env/i)      { ++$env }
318            else  { ++$unk }
319          }          }
320          # Return the result.      # Return the counts.
321          return @retVal;      return ($arch, $bact, $euk, $vir, $env, $unk);
322  }  }
323    
324  =head3 ShowMetaData  =head3 ContigCount
325    
326  C<< $sprout->ShowMetaData($fileName); >>  C<< my $count = $sprout->ContigCount($genomeID); >>
327    
328  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.
329    
330  =over 4  =over 4
331    
332  =item fileName  =item genomeID
333    
334  Fully-qualified name to give to the output file.  ID of the genome whose contig count is desired.
335    
336    =item RETURN
337    
338    Returns the number of contigs for the specified genome.
339    
340  =back  =back
341    
342  =cut  =cut
343    
344  sub ShowMetaData {  sub ContigCount {
345          # Get the parameters.          # Get the parameters.
346          my $self = shift @_;      my ($self, $genomeID) = @_;
347          my ($fileName) = @_;      # Get the contig count.
348          # Compute the file name.      my $retVal = $self->GetCount(['Contig', 'HasContig'], "HasContig(from-link) = ?", [$genomeID]);
349          my $options = $self->{_options};      # Return the result.
350          # Call the show method on the underlying ERDB object.      return $retVal;
         $self->{_erdb}->ShowMetaData($fileName);  
351  }  }
352    
353  =head3 Load  =head3 GeneMenu
   
 C<< $sprout->Load($rebuild); >>;  
   
 Load the database from files in the data directory, optionally re-creating the tables.  
354    
355  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.  
356    
357  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,
358  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
359  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.  
360    
361  =over 4  =over 4
362    
363  =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  
364    
365  =cut  Reference to a hash mapping attributes to values for the SELECT tag generated.
 #: Return Type %;  
 sub Load {  
         # Get the parameters.  
         my $self = shift @_;  
         my ($rebuild) = @_;  
         # Get the database object.  
         my $erdb = $self->{_erdb};  
         # Load the tables from the data directory.  
         my $retVal = $erdb->LoadTables($self->{_options}->{dataDir}, $rebuild);  
         # Return the statistics.  
         return $retVal;  
 }  
366    
367  =head3 LoadUpdate  =item filterString
368    
369  C<< my %stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>  A filter string for use in selecting the genomes. The filter string must conform
370    to the rules for the C<< ERDB->Get >> method.
371    
372  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.  
373    
374  =over 4  Reference to a list of values to be substituted in for the parameter marks in
375    the filter string.
376    
377  =item truncateFlag  =item selected (optional)
378    
379  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.  
380    
381  =item tableList  =item fast (optional)
382    
383  List of the tables to be updated.  If specified and TRUE, the contig counts will be omitted to improve performance.
384    
385  =item RETURN  =item RETURN
386    
387  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.  
388    
389  =back  =back
390    
391  =cut  =cut
392  #: Return Type $%;  
393  sub LoadUpdate {  sub GeneMenu {
394          # Get the parameters.          # Get the parameters.
395          my $self = shift @_;      my ($self, $attributes, $filterString, $params, $selected, $fast) = @_;
396          my ($truncateFlag, $tableList) = @_;      my $slowMode = ! $fast;
397          # Get the database object.      # Default to nothing selected. This prevents an execution warning if "$selected"
398          my $erdb = $self->{_erdb};      # is undefined.
399          # Declare the return value.      $selected = "" unless defined $selected;
400          my $retVal = Stats->new();      Trace("Gene Menu called with slow mode \"$slowMode\" and selection \"$selected\".") if T(3);
401          # Get the data directory.      # Start the menu.
402          my $optionTable = $self->{_options};      my $retVal = "<select " .
403          my $dataDir = $optionTable->{dataDir};          join(" ", map { "$_=\"$attributes->{$_}\"" } keys %{$attributes}) .
404          # Loop through the incoming table names.          ">\n";
405          for my $tableName (@{$tableList}) {      # Get the genomes.
406                  # Find the table's file.      my @genomes = $self->GetAll(['Genome'], $filterString, $params, ['Genome(id)',
407                  my $fileName = "$dataDir/$tableName";                                                                       'Genome(genus)',
408                  if (! -e $fileName) {                                                                       'Genome(species)',
409                          $fileName = "$fileName.dtx";                                                                       'Genome(unique-characterization)']);
410                  }      # Sort them by name.
411                  # Attempt to load this table.      my @sorted = sort { lc("$a->[1] $a->[2]") cmp lc("$b->[1] $b->[2]") } @genomes;
412                  my $result = $erdb->LoadTable($fileName, $tableName, $truncateFlag);      # Loop through the genomes, creating the option tags.
413                  # Accumulate the resulting statistics.      for my $genomeData (@sorted) {
414                  $retVal->Accumulate($result);          # Get the data for this genome.
415            my ($genomeID, $genus, $species, $strain) = @{$genomeData};
416            # Get the contig count.
417            my $contigInfo = "";
418            if ($slowMode) {
419                my $count = $self->ContigCount($genomeID);
420                my $counting = ($count == 1 ? "contig" : "contigs");
421                $contigInfo = "[$count $counting]";
422            }
423            # Find out if we're selected.
424            my $selectOption = ($selected eq $genomeID ? " selected" : "");
425            # Build the option tag.
426            $retVal .= "<option value=\"$genomeID\"$selectOption>$genus $species $strain ($genomeID)$contigInfo</option>\n";
427          }          }
428          # Return the statistics.      # Close the SELECT tag.
429        $retVal .= "</select>\n";
430        # Return the result.
431          return $retVal;          return $retVal;
432  }  }
433    
# Line 463  Line 443 
443  #: Return Type ;  #: Return Type ;
444  sub Build {  sub Build {
445          # Get the parameters.          # Get the parameters.
446          my $self = shift @_;      my ($self) = @_;
447          # Create the tables.          # Create the tables.
448          $self->{_erdb}->CreateTables;      $self->CreateTables();
449  }  }
450    
451  =head3 Genomes  =head3 Genomes
# Line 478  Line 458 
458  #: Return Type @;  #: Return Type @;
459  sub Genomes {  sub Genomes {
460          # Get the parameters.          # Get the parameters.
461          my $self = shift @_;      my ($self) = @_;
462          # Get all the genomes.          # Get all the genomes.
463          my @retVal = $self->GetFlat(['Genome'], "", [], 'Genome(id)');          my @retVal = $self->GetFlat(['Genome'], "", [], 'Genome(id)');
464          # Return the list of IDs.          # Return the list of IDs.
# Line 508  Line 488 
488  #: Return Type $;  #: Return Type $;
489  sub GenusSpecies {  sub GenusSpecies {
490          # Get the parameters.          # Get the parameters.
491          my $self = shift @_;      my ($self, $genomeID) = @_;
         my ($genomeID) = @_;  
492          # Get the data for the specified genome.          # Get the data for the specified genome.
493          my @values = $self->GetEntityValues('Genome', $genomeID, ['Genome(genus)', 'Genome(species)',          my @values = $self->GetEntityValues('Genome', $genomeID, ['Genome(genus)', 'Genome(species)',
494                                                                                                                            'Genome(unique-characterization)']);                                                                                                                            'Genome(unique-characterization)']);
# Line 545  Line 524 
524  #: Return Type @;  #: Return Type @;
525  sub FeaturesOf {  sub FeaturesOf {
526          # Get the parameters.          # Get the parameters.
527          my $self = shift @_;      my ($self, $genomeID,$ftype) = @_;
         my ($genomeID,$ftype) = @_;  
528          # Get the features we want.          # Get the features we want.
529          my @features;          my @features;
530          if (!$ftype) {          if (!$ftype) {
# Line 590  Line 568 
568  =item RETURN  =item RETURN
569    
570  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
571  context and as a space-delimited string in a scalar context.  context and as a comma-delimited string in a scalar context.
572    
573  =back  =back
574    
# Line 599  Line 577 
577  #: Return Type $;  #: Return Type $;
578  sub FeatureLocation {  sub FeatureLocation {
579          # Get the parameters.          # Get the parameters.
580          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
581          # Create a query for the feature locations.          # Create a query for the feature locations.
582          my $query = $self->Get(['IsLocatedIn'], "IsLocatedIn(from-link) = ? ORDER BY IsLocatedIn(locN)",          my $query = $self->Get(['IsLocatedIn'], "IsLocatedIn(from-link) = ? ORDER BY IsLocatedIn(locN)",
583                                                     [$featureID]);                                                     [$featureID]);
# Line 618  Line 595 
595                  if ($prevContig eq $contigID && $dir eq $prevDir) {                  if ($prevContig eq $contigID && $dir eq $prevDir) {
596                          # 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
597                          # new segment's beginning is next to the old segment's end.                          # new segment's beginning is next to the old segment's end.
598                          if (($dir eq "-" && $beg == $prevBeg - $prevLen) ||              if ($dir eq "-" && $beg + $len == $prevBeg) {
599                                  ($dir eq "+" && $beg == $prevBeg + $prevLen)) {                  # Here we're merging two backward blocks, so we keep the new begin point
600                                  # Here we need to merge two segments. Adjust the beginning and length values                  # and adjust the length.
601                                  # to include both segments.                  $len += $prevLen;
602                    # Pop the old segment off. The new one will replace it later.
603                    pop @retVal;
604                } elsif ($dir eq "+" && $beg == $prevBeg + $prevLen) {
605                    # Here we need to merge two forward blocks. Adjust the beginning and
606                    # length values to include both segments.
607                                  $beg = $prevBeg;                                  $beg = $prevBeg;
608                                  $len += $prevLen;                                  $len += $prevLen;
609                                  # Pop the old segment off. The new one will replace it later.                                  # Pop the old segment off. The new one will replace it later.
# Line 630  Line 612 
612                  }                  }
613                  # Remember this specifier for the adjacent-segment test the next time through.                  # Remember this specifier for the adjacent-segment test the next time through.
614                  ($prevContig, $prevBeg, $prevDir, $prevLen) = ($contigID, $beg, $dir, $len);                  ($prevContig, $prevBeg, $prevDir, $prevLen) = ($contigID, $beg, $dir, $len);
615            # Compute the initial base pair.
616            my $start = ($dir eq "+" ? $beg : $beg + $len - 1);
617                  # Add the specifier to the list.                  # Add the specifier to the list.
618                  push @retVal, "${contigID}_$beg$dir$len";          push @retVal, "${contigID}_$start$dir$len";
619          }          }
620          # Return the list in the format indicated by the context.          # Return the list in the format indicated by the context.
621          return (wantarray ? @retVal : join(' ', @retVal));      return (wantarray ? @retVal : join(',', @retVal));
622  }  }
623    
624  =head3 ParseLocation  =head3 ParseLocation
# Line 660  Line 644 
644  =cut  =cut
645  #: Return Type @;  #: Return Type @;
646  sub ParseLocation {  sub ParseLocation {
647          # Get the parameter.      # Get the parameter. Note that if we're called as an instance method, we ignore
648        # the first parameter.
649        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
650          my ($location) = @_;          my ($location) = @_;
651          # Parse it into segments.          # Parse it into segments.
652          $location =~ /^(.*)_(\d*)([+-_])(\d*)$/;      $location =~ /^(.+)_(\d+)([+\-_])(\d+)$/;
653          my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);          my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);
654          # If the direction is an underscore, convert it to a + or -.          # If the direction is an underscore, convert it to a + or -.
655          if ($dir eq "_") {          if ($dir eq "_") {
# Line 679  Line 665 
665          return ($contigID, $start, $dir, $len);          return ($contigID, $start, $dir, $len);
666  }  }
667    
668    
669    
670    =head3 PointLocation
671    
672    C<< my $found = Sprout::PointLocation($location, $point); >>
673    
674    Return the offset into the specified location of the specified point on the contig. If
675    the specified point is before the location, a negative value will be returned. If it is
676    beyond the location, an undefined value will be returned. It is assumed that the offset
677    is for the location's contig. The location can either be new-style (using a C<+> or C<->
678    and a length) or old-style (using C<_> and start and end positions.
679    
680    =over 4
681    
682    =item location
683    
684    A location specifier (see L</FeatureLocation> for a description).
685    
686    =item point
687    
688    The offset into the contig of the point in which we're interested.
689    
690    =item RETURN
691    
692    Returns the offset inside the specified location of the specified point, a negative
693    number if the point is before the location, or an undefined value if the point is past
694    the location. If the length of the location is 0, this method will B<always> denote
695    that it is outside the location. The offset will always be relative to the left-most
696    position in the location.
697    
698    =back
699    
700    =cut
701    #: Return Type $;
702    sub PointLocation {
703        # Get the parameter. Note that if we're called as an instance method, we ignore
704        # the first parameter.
705        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
706        my ($location, $point) = @_;
707        # Parse out the location elements. Note that this works on both old-style and new-style
708        # locations.
709        my ($contigID, $start, $dir, $len) = ParseLocation($location);
710        # Declare the return variable.
711        my $retVal;
712        # Compute the offset. The computation is dependent on the direction of the location.
713        my $offset = (($dir == '+') ? $point - $start : $point - ($start - $len + 1));
714        # Return the offset if it's valid.
715        if ($offset < $len) {
716            $retVal = $offset;
717        }
718        # Return the offset found.
719        return $retVal;
720    }
721    
722  =head3 DNASeq  =head3 DNASeq
723    
724  C<< my $sequence = $sprout->DNASeq(\@locationList); >>  C<< my $sequence = $sprout->DNASeq(\@locationList); >>
# Line 687  Line 727 
727  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,
728  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>.
729    
730    For example, the following would return the DNA sequence for contig C<83333.1:NC_000913>
731    between positions 1401 and 1532, inclusive.
732    
733        my $sequence = $sprout->DNASeq('83333.1:NC_000913_1401_1532');
734    
735  =over 4  =over 4
736    
737  =item locationList  =item locationList
738    
739  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
740  L</FeatureLocation> for more about this format).  I<contigID>C<_>I<begin>C<_>I<end> (see L</FeatureLocation> for more about this format).
741    
742  =item RETURN  =item RETURN
743    
# Line 704  Line 749 
749  #: Return Type $;  #: Return Type $;
750  sub DNASeq {  sub DNASeq {
751          # Get the parameters.          # Get the parameters.
752          my $self = shift @_;      my ($self, $locationList) = @_;
         my ($locationList) = @_;  
753          # Create the return string.          # Create the return string.
754          my $retVal = "";          my $retVal = "";
755          # Loop through the locations.          # Loop through the locations.
# Line 720  Line 764 
764                  # 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
765                  # before putting it in the return value.                  # before putting it in the return value.
766                  my ($start, $stop);                  my ($start, $stop);
767            Trace("Parse of \"$location\" is $beg$dir$len.") if T(SDNA => 4);
768                  if ($dir eq "+") {                  if ($dir eq "+") {
769                          $start = $beg;                          $start = $beg;
770                          $stop = $beg + $len - 1;                          $stop = $beg + $len - 1;
771                  } else {                  } else {
772                          $start = $beg + $len + 1;              $start = $beg - $len + 1;
773                          $stop = $beg;                          $stop = $beg;
774                  }                  }
775            Trace("Looking for sequences containing $start through $stop.") if T(SDNA => 4);
776                  my $query = $self->Get(['IsMadeUpOf','Sequence'],                  my $query = $self->Get(['IsMadeUpOf','Sequence'],
777                          "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .                          "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .
778                          " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",                          " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",
# Line 738  Line 784 
784                                  $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',                                  $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',
785                                                                     'IsMadeUpOf(len)']);                                                                     'IsMadeUpOf(len)']);
786                          my $stopPosition = $startPosition + $sequenceLength;                          my $stopPosition = $startPosition + $sequenceLength;
787                Trace("Sequence is from $startPosition to $stopPosition.") if T(SDNA => 4);
788                          # Figure out the start point and length of the relevant section.                          # Figure out the start point and length of the relevant section.
789                          my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);                          my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);
790                          my $len = ($stopPosition <= $stop ? $stopPosition : $stop) - $startPosition - $pos1;              my $len1 = ($stopPosition < $stop ? $stopPosition : $stop) + 1 - $startPosition - $pos1;
791                Trace("Position is $pos1 for length $len1.") if T(SDNA => 4);
792                          # Add the relevant data to the location data.                          # Add the relevant data to the location data.
793                          $locationDNA .= substr($sequenceData, $pos1, $len);              $locationDNA .= substr($sequenceData, $pos1, $len1);
794                  }                  }
795                  # 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.
796                  if ($dir eq '+') {                  if ($dir eq '+') {
797                          $retVal .= $locationDNA;                          $retVal .= $locationDNA;
798                  } else {                  } else {
799                          $locationDNA = join('', reverse split //, $locationDNA);              $retVal .= FIG::reverse_comp($locationDNA);
                         $retVal .= $locationDNA;  
800                  }                  }
801          }          }
802          # Return the result.          # Return the result.
# Line 778  Line 825 
825  #: Return Type @;  #: Return Type @;
826  sub AllContigs {  sub AllContigs {
827          # Get the parameters.          # Get the parameters.
828          my $self = shift @_;      my ($self, $genomeID) = @_;
         my ($genomeID) = @_;  
829          # Ask for the genome's Contigs.          # Ask for the genome's Contigs.
830          my @retVal = $self->GetFlat(['HasContig'], "HasContig(from-link) = ?", [$genomeID],          my @retVal = $self->GetFlat(['HasContig'], "HasContig(from-link) = ?", [$genomeID],
831                                                                  'HasContig(to-link)');                                                                  'HasContig(to-link)');
# Line 787  Line 833 
833          return @retVal;          return @retVal;
834  }  }
835    
836  =head3 ContigLength  =head3 GenomeLength
837    
838  C<< my $length = $sprout->ContigLength($contigID); >>  C<< my $length = $sprout->GenomeLength($genomeID); >>
839    
840  Compute the length of a contig.  Return the length of the specified genome in base pairs.
841    
842  =over 4  =over 4
843    
844  =item contigID  =item genomeID
845    
846  ID of the contig whose length is desired.  ID of the genome whose base pair count is desired.
847    
848  =item RETURN  =item RETURN
849    
850  Returns the number of positions in the contig.  Returns the number of base pairs in all the contigs of the specified
851    genome.
852    
853  =back  =back
854    
855  =cut  =cut
856  #: Return Type $;  
857  sub ContigLength {  sub GenomeLength {
858          # Get the parameters.          # Get the parameters.
859          my $self = shift @_;      my ($self, $genomeID) = @_;
860          my ($contigID) = @_;      # Declare the return variable.
         # Get the contig's last sequence.  
         my $query = $self->Get(['IsMadeUpOf'],  
                 "IsMadeUpOf(from-link) = ? ORDER BY IsMadeUpOf(start-position) DESC",  
                 [$contigID]);  
         my $sequence = $query->Fetch();  
         # Declare the return value.  
861          my $retVal = 0;          my $retVal = 0;
862          # Set it from the sequence data, if any.      # Get the genome's contig sequence lengths.
863          if ($sequence) {      my @lens = $self->GetFlat(['HasContig', 'IsMadeUpOf'], 'HasContig(from-link) = ?',
864                  my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);                         [$genomeID], 'IsMadeUpOf(len)');
865                  $retVal = $start + $len;      # Sum the lengths.
866          }      map { $retVal += $_ } @lens;
867          # Return the result.          # Return the result.
868          return $retVal;          return $retVal;
869  }  }
870    
871  =head3 GenesInRegion  =head3 FeatureCount
872    
873  C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>  C<< my $count = $sprout->FeatureCount($genomeID, $type); >>
874    
875  List the features which overlap a specified region in a contig.  Return the number of features of the specified type in the specified genome.
876    
877  =over 4  =over 4
878    
879  =item contigID  =item genomeID
880    
881  ID of the contig containing the region of interest.  ID of the genome whose feature count is desired.
882    
883  =item start  =item type
884    
885  Offset of the first residue in the region of interest.  Type of feature to count (eg. C<peg>, C<rna>, etc.).
886    
887  =item stop  =item RETURN
888    
889  Offset of the last residue in the region of interest.  Returns the number of features of the specified type for the specified genome.
890    
891    =back
892    
893    =cut
894    
895    sub FeatureCount {
896        # Get the parameters.
897        my ($self, $genomeID, $type) = @_;
898        # Compute the count.
899        my $retVal = $self->GetCount(['HasFeature', 'Feature'],
900                                    "HasFeature(from-link) = ? AND Feature(feature-type) = ?",
901                                    [$genomeID, $type]);
902        # Return the result.
903        return $retVal;
904    }
905    
906    =head3 GenomeAssignments
907    
908    C<< my $fidHash = $sprout->GenomeAssignments($genomeID); >>
909    
910    Return a list of a genome's assigned features. The return hash will contain each
911    assigned feature of the genome mapped to the text of its most recent functional
912    assignment.
913    
914    =over 4
915    
916    =item genomeID
917    
918    ID of the genome whose functional assignments are desired.
919    
920  =item RETURN  =item RETURN
921    
922  Returns a three-element list. The first element is a list of feature IDs for the features that  Returns a reference to a hash which maps each feature to its most recent
923  overlap the region of interest. The second and third elements are the minimum and maximum  functional assignment.
 locations of the features provided on the specified contig. These may extend outside  
 the start and stop values.  
924    
925  =back  =back
926    
927  =cut  =cut
928  #: Return Type @;  
929  sub GenesInRegion {  sub GenomeAssignments {
930          # Get the parameters.          # Get the parameters.
931          my $self = shift @_;      my ($self, $genomeID) = @_;
932          my ($contigID, $start, $stop) = @_;      # Declare the return variable.
933          # Get the maximum segment length.      my $retVal = {};
934          my $maximumSegmentLength = $self->MaxSegment;      # Query the genome's features.
935          # Create a hash to receive the feature list. We use a hash so that we can eliminate      my $query = $self->Get(['HasFeature', 'Feature'], "HasFeature(from-link) = ?",
936          # duplicates easily.                             [$genomeID]);
937          my %featuresFound = ();      # Loop through the features.
938          # Prime the values we'll use for the returned beginning and end.      while (my $data = $query->Fetch) {
939          my ($min, $max) = ($self->ContigLength($contigID), 0);          # Get the feature ID and assignment.
940          # Create a table of parameters for each query. Each query looks for features travelling in          my ($fid, $assignment) = $data->Values(['Feature(id)', 'Feature(assignment)']);
941          # a particular direction. The query parameters include the contig ID, the feature direction,          if ($assignment) {
942                $retVal->{$fid} = $assignment;
943            }
944        }
945        # Return the result.
946        return $retVal;
947    }
948    
949    =head3 ContigLength
950    
951    C<< my $length = $sprout->ContigLength($contigID); >>
952    
953    Compute the length of a contig.
954    
955    =over 4
956    
957    =item contigID
958    
959    ID of the contig whose length is desired.
960    
961    =item RETURN
962    
963    Returns the number of positions in the contig.
964    
965    =back
966    
967    =cut
968    #: Return Type $;
969    sub ContigLength {
970        # Get the parameters.
971        my ($self, $contigID) = @_;
972        # Get the contig's last sequence.
973        my $query = $self->Get(['IsMadeUpOf'],
974            "IsMadeUpOf(from-link) = ? ORDER BY IsMadeUpOf(start-position) DESC",
975            [$contigID]);
976        my $sequence = $query->Fetch();
977        # Declare the return value.
978        my $retVal = 0;
979        # Set it from the sequence data, if any.
980        if ($sequence) {
981            my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);
982            $retVal = $start + $len - 1;
983        }
984        # Return the result.
985        return $retVal;
986    }
987    
988    =head3 ClusterPEGs
989    
990    C<< my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs); >>
991    
992    Cluster the PEGs in a list according to the cluster coding scheme of the specified
993    subsystem. In order for this to work properly, the subsystem object must have
994    been used recently to retrieve the PEGs using the B<get_pegs_from_cell> method.
995    This causes the cluster numbers to be pulled into the subsystem's color hash.
996    If a PEG is not found in the color hash, it will not appear in the output
997    sequence.
998    
999    =over 4
1000    
1001    =item sub
1002    
1003    Sprout subsystem object for the relevant subsystem, from the L</get_subsystem>
1004    method.
1005    
1006    =item pegs
1007    
1008    Reference to the list of PEGs to be clustered.
1009    
1010    =item RETURN
1011    
1012    Returns a list of the PEGs, grouped into smaller lists by cluster number.
1013    
1014    =back
1015    
1016    =cut
1017    #: Return Type $@@;
1018    sub ClusterPEGs {
1019        # Get the parameters.
1020        my ($self, $sub, $pegs) = @_;
1021        # Declare the return variable.
1022        my $retVal = [];
1023        # Loop through the PEGs, creating arrays for each cluster.
1024        for my $pegID (@{$pegs}) {
1025            my $clusterNumber = $sub->get_cluster_number($pegID);
1026            # Only proceed if the PEG is in a cluster.
1027            if ($clusterNumber >= 0) {
1028                # Push this PEG onto the sub-list for the specified cluster number.
1029                push @{$retVal->[$clusterNumber]}, $pegID;
1030            }
1031        }
1032        # Return the result.
1033        return $retVal;
1034    }
1035    
1036    =head3 GenesInRegion
1037    
1038    C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>
1039    
1040    List the features which overlap a specified region in a contig.
1041    
1042    =over 4
1043    
1044    =item contigID
1045    
1046    ID of the contig containing the region of interest.
1047    
1048    =item start
1049    
1050    Offset of the first residue in the region of interest.
1051    
1052    =item stop
1053    
1054    Offset of the last residue in the region of interest.
1055    
1056    =item RETURN
1057    
1058    Returns a three-element list. The first element is a list of feature IDs for the features that
1059    overlap the region of interest. The second and third elements are the minimum and maximum
1060    locations of the features provided on the specified contig. These may extend outside
1061    the start and stop values. The first element (that is, the list of features) is sorted
1062    roughly by location.
1063    
1064    =back
1065    
1066    =cut
1067    #: Return Type @@;
1068    sub GenesInRegion {
1069        # Get the parameters.
1070        my ($self, $contigID, $start, $stop) = @_;
1071        # Get the maximum segment length.
1072        my $maximumSegmentLength = $self->MaxSegment;
1073        # Create a hash to receive the feature list. We use a hash so that we can eliminate
1074        # duplicates easily. The hash key will be the feature ID. The value will be a two-element
1075        # containing the minimum and maximum offsets. We will use the offsets to sort the results
1076        # when we're building the result set.
1077        my %featuresFound = ();
1078        # Prime the values we'll use for the returned beginning and end.
1079        my @initialMinMax = ($self->ContigLength($contigID), 0);
1080        my ($min, $max) = @initialMinMax;
1081        # Create a table of parameters for each query. Each query looks for features travelling in
1082        # a particular direction. The query parameters include the contig ID, the feature direction,
1083          # the lowest possible start position, and the highest possible start position. This works          # the lowest possible start position, and the highest possible start position. This works
1084          # because each feature segment length must be no greater than the maximum segment length.          # because each feature segment length must be no greater than the maximum segment length.
1085          my %queryParms = (forward => [$contigID, '+', $start - $maximumSegmentLength + 1, $stop],          my %queryParms = (forward => [$contigID, '+', $start - $maximumSegmentLength + 1, $stop],
# Line 898  Line 1107 
1107                                          $found = 1;                                          $found = 1;
1108                                  }                                  }
1109                          } elsif ($dir eq '-') {                          } elsif ($dir eq '-') {
1110                                  $end = $beg - $len;                  # Note we switch things around so that the beginning is to the left of the
1111                                  if ($end <= $stop) {                  # ending.
1112                    ($beg, $end) = ($beg - $len, $beg);
1113                    if ($beg <= $stop) {
1114                                          # Denote we found a useful feature.                                          # Denote we found a useful feature.
1115                                          $found = 1;                                          $found = 1;
1116                                  }                                  }
1117                          }                          }
1118                          if ($found) {                          if ($found) {
1119                                  # Here we need to record the feature and update the minimum and maximum.                  # Here we need to record the feature and update the minima and maxima. First,
1120                                  $featuresFound{$featureID} = 1;                  # get the current entry for the specified feature.
1121                                  if ($beg < $min) { $min = $beg; }                  my ($loc1, $loc2) = (exists $featuresFound{$featureID} ? @{$featuresFound{$featureID}} :
1122                                  if ($end < $min) { $min = $end; }                                       @initialMinMax);
1123                                  if ($beg > $max) { $max = $beg; }                  # Merge the current segment's begin and end into the feature begin and end and the
1124                                  if ($end > $max) { $max = $end; }                  # global min and max.
1125                    if ($beg < $loc1) {
1126                        $loc1 = $beg;
1127                        $min = $beg if $beg < $min;
1128                    }
1129                    if ($end > $loc2) {
1130                        $loc2 = $end;
1131                        $max = $end if $end > $max;
1132                    }
1133                    # Store the entry back into the hash table.
1134                    $featuresFound{$featureID} = [$loc1, $loc2];
1135                          }                          }
1136                  }                  }
1137          }          }
1138          # Compute a list of the IDs for the features found.      # Now we must compute the list of the IDs for the features found. We start with a list
1139          my @list = (sort (keys %featuresFound));      # of midpoints / feature ID pairs. (It's not really a midpoint, it's twice the midpoint,
1140        # but the result of the sort will be the same.)
1141        my @list = map { [$featuresFound{$_}->[0] + $featuresFound{$_}->[1], $_] } keys %featuresFound;
1142        # Now we sort by midpoint and yank out the feature IDs.
1143        my @retVal = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @list;
1144          # Return it along with the min and max.          # Return it along with the min and max.
1145          return (\@list, $min, $max);      return (\@retVal, $min, $max);
1146  }  }
1147    
1148  =head3 FType  =head3 FType
# Line 943  Line 1168 
1168  #: Return Type $;  #: Return Type $;
1169  sub FType {  sub FType {
1170          # Get the parameters.          # Get the parameters.
1171          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
1172          # Get the specified feature's type.          # Get the specified feature's type.
1173          my ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(feature-type)']);          my ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(feature-type)']);
1174          # Return the result.          # Return the result.
# Line 953  Line 1177 
1177    
1178  =head3 FeatureAnnotations  =head3 FeatureAnnotations
1179    
1180  C<< my @descriptors = $sprout->FeatureAnnotations($featureID); >>  C<< my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag); >>
1181    
1182  Return the annotations of a feature.  Return the annotations of a feature.
1183    
# Line 963  Line 1187 
1187    
1188  ID of the feature whose annotations are desired.  ID of the feature whose annotations are desired.
1189    
1190    =item rawFlag
1191    
1192    If TRUE, the annotation timestamps will be returned in raw form; otherwise, they
1193    will be returned in human-readable form.
1194    
1195  =item RETURN  =item RETURN
1196    
1197  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.
1198    
1199  * B<featureID> ID of the relevant feature.  * B<featureID> ID of the relevant feature.
1200    
1201  * B<timeStamp> time the annotation was made, in user-friendly format.  * B<timeStamp> time the annotation was made.
1202    
1203  * B<user> ID of the user who made the annotation  * B<user> ID of the user who made the annotation
1204    
# Line 981  Line 1210 
1210  #: Return Type @%;  #: Return Type @%;
1211  sub FeatureAnnotations {  sub FeatureAnnotations {
1212          # Get the parameters.          # Get the parameters.
1213          my $self = shift @_;      my ($self, $featureID, $rawFlag) = @_;
         my ($featureID) = @_;  
1214          # 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.
1215          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1216                                                     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);                                                     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);
# Line 995  Line 1223 
1223                          $annotation->Values(['IsTargetOfAnnotation(from-link)',                          $annotation->Values(['IsTargetOfAnnotation(from-link)',
1224                                                                   'Annotation(time)', 'MadeAnnotation(from-link)',                                                                   'Annotation(time)', 'MadeAnnotation(from-link)',
1225                                                                   'Annotation(annotation)']);                                                                   'Annotation(annotation)']);
1226            # Convert the time, if necessary.
1227            if (! $rawFlag) {
1228                $timeStamp = FriendlyTimestamp($timeStamp);
1229            }
1230                  # Assemble them into a hash.                  # Assemble them into a hash.
1231          my $annotationHash = { featureID => $featureID,          my $annotationHash = { featureID => $featureID,
1232                                 timeStamp => FriendlyTimestamp($timeStamp),                                 timeStamp => $timeStamp,
1233                                                             user => $user, text => $text };                                                             user => $user, text => $text };
1234                  # Add it to the return list.                  # Add it to the return list.
1235                  push @retVal, $annotationHash;                  push @retVal, $annotationHash;
# Line 1011  Line 1243 
1243  C<< my %functions = $sprout->AllFunctionsOf($featureID); >>  C<< my %functions = $sprout->AllFunctionsOf($featureID); >>
1244    
1245  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
1246  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,
1247  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
1248  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,
1249  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.
1250  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
1251  functional assignments, we will only keep the most recent one.  recent one.
1252    
1253  =over 4  =over 4
1254    
# Line 1026  Line 1258 
1258    
1259  =item RETURN  =item RETURN
1260    
1261  Returns a hash mapping the functional assignment IDs to user IDs.  Returns a hash mapping the user IDs to functional assignment IDs.
1262    
1263  =back  =back
1264    
# Line 1034  Line 1266 
1266  #: Return Type %;  #: Return Type %;
1267  sub AllFunctionsOf {  sub AllFunctionsOf {
1268          # Get the parameters.          # Get the parameters.
1269          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
1270          # Get all of the feature's annotations.          # Get all of the feature's annotations.
1271      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation'],      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1272                                                      "IsTargetOfAnnotation(from-link) = ?",                                                      "IsTargetOfAnnotation(from-link) = ?",
1273                              [$featureID], ['Annotation(time)', 'Annotation(annotation)']);                              [$featureID], ['Annotation(time)', 'Annotation(annotation)',
1274                                               'MadeAnnotation(from-link)']);
1275          # Declare the return hash.          # Declare the return hash.
1276          my %retVal;          my %retVal;
     # Declare a hash for insuring we only make one assignment per user.  
     my %timeHash = ();  
1277      # Now we sort the assignments by timestamp in reverse.      # Now we sort the assignments by timestamp in reverse.
1278      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;
1279          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1280      for my $annotation (@sortedQuery) {      for my $annotation (@sortedQuery) {
1281          # Get the annotation fields.          # Get the annotation fields.
1282          my ($timeStamp, $text) = @{$annotation};          my ($timeStamp, $text, $user) = @{$annotation};
1283                  # Check to see if this is a functional assignment.                  # Check to see if this is a functional assignment.
1284                  my ($user, $function) = ParseAssignment($text);          my ($actualUser, $function) = _ParseAssignment($user, $text);
1285          if ($user && ! exists $timeHash{$user}) {          if ($actualUser && ! exists $retVal{$actualUser}) {
1286              # Here it is a functional assignment and there has been no              # Here it is a functional assignment and there has been no
1287              # previous assignment for this user, so we stuff it in the              # previous assignment for this user, so we stuff it in the
1288              # return hash.              # return hash.
1289                          $retVal{$function} = $user;              $retVal{$actualUser} = $function;
             # Insure we don't assign to this user again.  
             $timeHash{$user} = 1;  
1290                  }                  }
1291          }          }
1292          # Return the hash of assignments found.          # Return the hash of assignments found.
# Line 1072  Line 1300 
1300  Return the most recently-determined functional assignment of a particular feature.  Return the most recently-determined functional assignment of a particular feature.
1301    
1302  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
1303  the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional  the feature is identified by a FIG ID (begins with the string C<fig|>), then the functional
1304  assignment is a type of annotation. It has the format "XXXX\nset XXXX function to\nYYYYY". In this  assignment is taken from the B<Feature> or C<Annotation> table, depending.
 instance, XXXX is the user ID and YYYYY is the functional assignment text. Its worth noting that  
 we cannot filter on the content of the annotation itself because it's a text field; however, this  
 is not a big problem because most features only have a small number of annotations.  
1305    
1306  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
1307  recent one by at least one of the trusted users. If no trusted user list is available, then  recent one by at least one of the trusted users. If no trusted user list is available, then
# Line 1095  Line 1320 
1320    
1321  =item userID (optional)  =item userID (optional)
1322    
1323  ID of the user whose function determination is desired. If omitted, only the latest  ID of the user whose function determination is desired. If omitted, the primary
1324  C<FIG> assignment will be returned.  functional assignment in the B<Feature> table will be returned.
1325    
1326  =item RETURN  =item RETURN
1327    
# Line 1108  Line 1333 
1333  #: Return Type $;  #: Return Type $;
1334  sub FunctionOf {  sub FunctionOf {
1335          # Get the parameters.          # Get the parameters.
1336          my $self = shift @_;      my ($self, $featureID, $userID) = @_;
         my ($featureID, $userID) = @_;  
1337      # Declare the return value.      # Declare the return value.
1338      my $retVal;      my $retVal;
1339      # Determine the ID type.      # Determine the ID type.
1340      if ($featureID =~ m/^fig\|/) {      if ($featureID =~ m/^fig\|/) {
1341          # Here we have a FIG feature ID. We must build the list of trusted          # Here we have a FIG feature ID.
1342          # users.          if (!$userID) {
1343                # Use the primary assignment.
1344                ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(assignment)']);
1345            } else {
1346                # We must build the list of trusted users.
1347          my %trusteeTable = ();          my %trusteeTable = ();
1348          # Check the user ID.          # Check the user ID.
1349          if (!$userID) {          if (!$userID) {
# Line 1137  Line 1365 
1365              }              }
1366          }          }
1367          # 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.
1368          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation'],              my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1369                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1370                                 [$featureID]);                                 [$featureID]);
1371          my $timeSelected = 0;          my $timeSelected = 0;
1372          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1373          while (my $annotation = $query->Fetch()) {          while (my $annotation = $query->Fetch()) {
1374              # Get the annotation text.              # Get the annotation text.
1375              my ($text, $time) = $annotation->Values(['Annotation(annotation)','Annotation(time)']);                  my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1376                                                             'Annotation(time)', 'MadeAnnotation(from-link)']);
1377              # 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.
1378              my ($user, $type, $function) = split(/\n/, $text);                  my ($actualUser, $function) = _ParseAssignment($user, $text);
1379              if ($type =~ m/^set $user function to$/i) {                  Trace("Assignment user is $actualUser, text is $function.") if T(4);
1380                    if ($actualUser) {
1381                  # Here it is a functional assignment. Check the time and the user                  # Here it is a functional assignment. Check the time and the user
1382                  # name. The time must be recent and the user must be trusted.                  # name. The time must be recent and the user must be trusted.
1383                  if ((exists $trusteeTable{$user}) && ($time > $timeSelected)) {                      if ((exists $trusteeTable{$actualUser}) && ($time > $timeSelected)) {
1384                      $retVal = $function;                      $retVal = $function;
1385                      $timeSelected = $time;                      $timeSelected = $time;
1386                  }                  }
1387              }              }
1388          }          }
1389            }
1390      } else {      } else {
1391          # Here we have a non-FIG feature ID. In this case the user ID does not          # Here we have a non-FIG feature ID. In this case the user ID does not
1392          # matter. We simply get the information from the External Alias Function          # matter. We simply get the information from the External Alias Function
# Line 1166  Line 1397 
1397          return $retVal;          return $retVal;
1398  }  }
1399    
1400    =head3 FunctionsOf
1401    
1402    C<< my @functionList = $sprout->FunctionOf($featureID, $userID); >>
1403    
1404    Return the functional assignments of a particular feature.
1405    
1406    The functional assignment is handled differently depending on the type of feature. If
1407    the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional
1408    assignment is a type of annotation. The format of an assignment is described in
1409    L</ParseAssignment>. Its worth noting that we cannot filter on the content of the
1410    annotation itself because it's a text field; however, this is not a big problem because
1411    most features only have a small number of annotations.
1412    
1413    If the feature is B<not> identified by a FIG ID, then the functional assignment
1414    information is taken from the B<ExternalAliasFunc> table. If the table does
1415    not contain an entry for the feature, an empty list is returned.
1416    
1417    =over 4
1418    
1419    =item featureID
1420    
1421    ID of the feature whose functional assignments are desired.
1422    
1423    =item RETURN
1424    
1425    Returns a list of 2-tuples, each consisting of a user ID and the text of an assignment by
1426    that user.
1427    
1428    =back
1429    
1430    =cut
1431    #: Return Type @@;
1432    sub FunctionsOf {
1433        # Get the parameters.
1434        my ($self, $featureID) = @_;
1435        # Declare the return value.
1436        my @retVal = ();
1437        # Determine the ID type.
1438        if ($featureID =~ m/^fig\|/) {
1439            # Here we have a FIG feature ID. We must build the list of trusted
1440            # users.
1441            my %trusteeTable = ();
1442            # Build a query for all of the feature's annotations, sorted by date.
1443            my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1444                                   "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1445                                   [$featureID]);
1446            my $timeSelected = 0;
1447            # Loop until we run out of annotations.
1448            while (my $annotation = $query->Fetch()) {
1449                # Get the annotation text.
1450                my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1451                                                                'Annotation(time)',
1452                                                                'MadeAnnotation(user)']);
1453                # Check to see if this is a functional assignment for a trusted user.
1454                my ($actualUser, $function) = _ParseAssignment($user, $text);
1455                if ($actualUser) {
1456                    # Here it is a functional assignment.
1457                    push @retVal, [$actualUser, $function];
1458                }
1459            }
1460        } else {
1461            # Here we have a non-FIG feature ID. In this case the user ID does not
1462            # matter. We simply get the information from the External Alias Function
1463            # table.
1464            my @assignments = $self->GetEntityValues('ExternalAliasFunc', $featureID,
1465                                                     ['ExternalAliasFunc(func)']);
1466            push @retVal, map { ['master', $_] } @assignments;
1467        }
1468        # Return the assignments found.
1469        return @retVal;
1470    }
1471    
1472  =head3 BBHList  =head3 BBHList
1473    
1474  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>
# Line 1185  Line 1488 
1488    
1489  =item RETURN  =item RETURN
1490    
1491  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
1492  their best hits.  on the target genome.
1493    
1494  =back  =back
1495    
# Line 1194  Line 1497 
1497  #: Return Type %;  #: Return Type %;
1498  sub BBHList {  sub BBHList {
1499          # Get the parameters.          # Get the parameters.
1500          my $self = shift @_;      my ($self, $genomeID, $featureList) = @_;
         my ($genomeID, $featureList) = @_;  
1501          # Create the return structure.          # Create the return structure.
1502          my %retVal = ();          my %retVal = ();
1503          # Loop through the incoming features.          # Loop through the incoming features.
1504          for my $featureID (@{$featureList}) {          for my $featureID (@{$featureList}) {
1505                  # Create a query to get the feature's best hit.          # Ask the server for the feature's best hit.
1506                  my $query = $self->Get(['IsBidirectionalBestHitOf'],          my @bbhData = FIGRules::BBHData($featureID);
1507                                                             "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?",          # Peel off the BBHs found.
1508                                                             [$featureID, $genomeID]);          my @found = ();
1509                  # Look for the best hit.          for my $bbh (@bbhData) {
1510                  my $bbh = $query->Fetch;              my $fid = $bbh->[0];
1511                  if ($bbh) {              my $bbGenome = $self->GenomeOf($fid);
1512                          my ($targetFeature) = $bbh->Value('IsBidirectionalBestHitOf(to-link)');              if ($bbGenome eq $genomeID) {
1513                          $retVal{$featureID} = $targetFeature;                  push @found, $fid;
1514                  }                  }
1515          }          }
1516            $retVal{$featureID} = \@found;
1517        }
1518          # Return the mapping.          # Return the mapping.
1519          return \%retVal;          return \%retVal;
1520  }  }
1521    
1522    =head3 SimList
1523    
1524    C<< my %similarities = $sprout->SimList($featureID, $count); >>
1525    
1526    Return a list of the similarities to the specified feature.
1527    
1528    This method just returns the bidirectional best hits for performance reasons.
1529    
1530    =over 4
1531    
1532    =item featureID
1533    
1534    ID of the feature whose similarities are desired.
1535    
1536    =item count
1537    
1538    Maximum number of similar features to be returned, or C<0> to return them all.
1539    
1540    =back
1541    
1542    =cut
1543    #: Return Type %;
1544    sub SimList {
1545        # Get the parameters.
1546        my ($self, $featureID, $count) = @_;
1547        # Ask for the best hits.
1548        my @lists = FIGRules::BBHData($featureID);
1549        # Create the return value.
1550        my %retVal = ();
1551        for my $tuple (@lists) {
1552            $retVal{$tuple->[0]} = $tuple->[1];
1553        }
1554        # Return the result.
1555        return %retVal;
1556    }
1557    
1558    =head3 IsComplete
1559    
1560    C<< my $flag = $sprout->IsComplete($genomeID); >>
1561    
1562    Return TRUE if the specified genome is complete, else FALSE.
1563    
1564    =over 4
1565    
1566    =item genomeID
1567    
1568    ID of the genome whose completeness status is desired.
1569    
1570    =item RETURN
1571    
1572    Returns TRUE if the genome is complete, FALSE if it is incomplete, and C<undef> if it is
1573    not found.
1574    
1575    =back
1576    
1577    =cut
1578    #: Return Type $;
1579    sub IsComplete {
1580        # Get the parameters.
1581        my ($self, $genomeID) = @_;
1582        # Declare the return variable.
1583        my $retVal;
1584        # Get the genome's data.
1585        my $genomeData = $self->GetEntity('Genome', $genomeID);
1586        if ($genomeData) {
1587            # The genome exists, so get the completeness flag.
1588            ($retVal) = $genomeData->Value('Genome(complete)');
1589        }
1590        # Return the result.
1591        return $retVal;
1592    }
1593    
1594  =head3 FeatureAliases  =head3 FeatureAliases
1595    
1596  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>
# Line 1238  Line 1614 
1614  #: Return Type @;  #: Return Type @;
1615  sub FeatureAliases {  sub FeatureAliases {
1616          # Get the parameters.          # Get the parameters.
1617          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
1618          # Get the desired feature's aliases          # Get the desired feature's aliases
1619          my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(alias)']);          my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(alias)']);
1620          # Return the result.          # Return the result.
# Line 1250  Line 1625 
1625    
1626  C<< my $genomeID = $sprout->GenomeOf($featureID); >>  C<< my $genomeID = $sprout->GenomeOf($featureID); >>
1627    
1628  Return the genome that contains a specified feature.  Return the genome that contains a specified feature or contig.
1629    
1630  =over 4  =over 4
1631    
1632  =item featureID  =item featureID
1633    
1634  ID of the feature whose genome is desired.  ID of the feature or contig whose genome is desired.
1635    
1636  =item RETURN  =item RETURN
1637    
1638  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
1639  an undefined value.  found, returns an undefined value.
1640    
1641  =back  =back
1642    
# Line 1269  Line 1644 
1644  #: Return Type $;  #: Return Type $;
1645  sub GenomeOf {  sub GenomeOf {
1646          # Get the parameters.          # Get the parameters.
1647          my $self = shift @_;      my ($self, $featureID) = @_;
1648          my ($featureID) = @_;      # Create a query to find the genome associated with the incoming ID.
1649          # Create a query to find the genome associated with the feature.      my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ? OR HasContig(to-link) = ?",
1650          my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ?", [$featureID]);                             [$featureID, $featureID]);
1651          # Declare the return value.          # Declare the return value.
1652          my $retVal;          my $retVal;
1653          # Get the genome ID.          # Get the genome ID.
# Line 1306  Line 1681 
1681  #: Return Type %;  #: Return Type %;
1682  sub CoupledFeatures {  sub CoupledFeatures {
1683          # Get the parameters.          # Get the parameters.
1684          my $self = shift @_;      my ($self, $featureID) = @_;
1685          my ($featureID) = @_;      Trace("Looking for features coupled to $featureID.") if T(coupling => 3);
1686          # Create a query to retrieve the functionally-coupled features. Note that we depend on the      # Create a query to retrieve the functionally-coupled features.
1687          # fact that the functional coupling is physically paired. If (A,B) is in the database, then      my $query = $self->Get(['ParticipatesInCoupling', 'Coupling'],
1688          # (B,A) will also be found.                             "ParticipatesInCoupling(from-link) = ?", [$featureID]);
         my $query = $self->Get(['IsClusteredOnChromosomeWith'],  
                                                    "IsClusteredOnChromosomeWith(from-link) = ?", [$featureID]);  
1689          # 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.
1690          my $found = 0;          my $found = 0;
1691          # Create the return hash.          # Create the return hash.
1692          my %retVal = ();          my %retVal = ();
1693          # Retrieve the relationship records and store them in the hash.          # Retrieve the relationship records and store them in the hash.
1694          while (my $clustering = $query->Fetch()) {          while (my $clustering = $query->Fetch()) {
1695                  my ($otherFeatureID, $score) = $clustering->Values(['IsClusteredOnChromosomeWith(to-link)',          # Get the ID and score of the coupling.
1696                                                                      'IsClusteredOnChromosomeWith(score)']);          my ($couplingID, $score) = $clustering->Values(['Coupling(id)',
1697                                                            'Coupling(score)']);
1698            Trace("$featureID coupled with score $score to ID $couplingID.") if T(coupling => 4);
1699            # Get the other feature that participates in the coupling.
1700            my ($otherFeatureID) = $self->GetFlat(['ParticipatesInCoupling'],
1701                                               "ParticipatesInCoupling(to-link) = ? AND ParticipatesInCoupling(from-link) <> ?",
1702                                               [$couplingID, $featureID], 'ParticipatesInCoupling(from-link)');
1703            Trace("$couplingID target feature is $otherFeatureID.") if T(coupling => 4);
1704            # Attach the other feature's score to its ID.
1705                  $retVal{$otherFeatureID} = $score;                  $retVal{$otherFeatureID} = $score;
1706                  $found = 1;                  $found = 1;
1707          }          }
# Line 1333  Line 1714 
1714          return %retVal;          return %retVal;
1715  }  }
1716    
1717  =head3 GetEntityTypes  =head3 CouplingEvidence
1718    
1719    C<< my @evidence = $sprout->CouplingEvidence($peg1, $peg2); >>
1720    
1721    Return the evidence for a functional coupling.
1722    
1723    A pair of features is considered evidence of a coupling between two other
1724    features if they occur close together on a contig and both are similar to
1725    the coupled features. So, if B<A1> and B<A2> are close together on a contig,
1726    B<B1> and B<B2> are considered evidence for the coupling if (1) B<B1> and
1727    B<B2> are close together, (2) B<B1> is similar to B<A1>, and (3) B<B2> is
1728    similar to B<A2>.
1729    
1730    The score of a coupling is determined by the number of pieces of evidence
1731    that are considered I<representative>. If several evidence items belong to
1732    a group of genomes that are close to each other, only one of those items
1733    is considered representative. The other evidence items are presumed to be
1734    there because of the relationship between the genomes rather than because
1735    the two proteins generated by the features have a related functionality.
1736    
1737    Each evidence item is returned as a three-tuple in the form C<[>I<$peg1a>C<,>
1738    I<$peg2a>C<,> I<$rep>C<]>, where I<$peg1a> is similar to I<$peg1>, I<$peg2a>
1739    is similar to I<$peg2>, and I<$rep> is TRUE if the evidence is representative
1740    and FALSE otherwise.
1741    
1742    =over 4
1743    
1744    =item peg1
1745    
1746    ID of the feature of interest.
1747    
1748    =item peg2
1749    
1750  C<< my @entityList = $sprout->GetEntityTypes(); >>  ID of a feature functionally coupled to the feature of interest.
1751    
1752  Return the list of supported entity types.  =item RETURN
1753    
1754    Returns a list of 3-tuples. Each tuple consists of a feature similar to the feature
1755    of interest, a feature similar to the functionally coupled feature, and a flag
1756    that is TRUE for a representative piece of evidence and FALSE otherwise.
1757    
1758    =back
1759    
1760  =cut  =cut
1761  #: Return Type @;  #: Return Type @@;
1762  sub GetEntityTypes {  sub CouplingEvidence {
1763          # Get the parameters.          # Get the parameters.
1764          my $self = shift @_;      my ($self, $peg1, $peg2) = @_;
1765          # Get the underlying database object.      # Declare the return variable.
1766          my $erdb = $self->{_erdb};      my @retVal = ();
1767          # Get its entity type list.      # Our first task is to find out the nature of the coupling: whether or not
1768          my @retVal = $erdb->GetEntityTypes();      # it exists, its score, and whether the features are stored in the same
1769        # order as the ones coming in.
1770        my ($couplingID, $inverted, $score) = $self->GetCoupling($peg1, $peg2);
1771        # Only proceed if a coupling exists.
1772        if ($couplingID) {
1773            # Determine the ordering to place on the evidence items. If we're
1774            # inverted, we want to see feature 2 before feature 1 (descending); otherwise,
1775            # we want feature 1 before feature 2 (normal).
1776            Trace("Coupling evidence for ($peg1, $peg2) with inversion flag $inverted.") if T(Coupling => 4);
1777            my $ordering = ($inverted ? "DESC" : "");
1778            # Get the coupling evidence.
1779            my @evidenceList = $self->GetAll(['IsEvidencedBy', 'PCH', 'UsesAsEvidence'],
1780                                              "IsEvidencedBy(from-link) = ? ORDER BY PCH(id), UsesAsEvidence(pos) $ordering",
1781                                              [$couplingID],
1782                                              ['PCH(used)', 'UsesAsEvidence(to-link)']);
1783            # Loop through the evidence items. Each piece of evidence is represented by two
1784            # positions in the evidence list, one for each feature on the other side of the
1785            # evidence link. If at some point we want to generalize to couplings with
1786            # more than two positions, this section of code will need to be re-done.
1787            while (@evidenceList > 0) {
1788                my $peg1Data = shift @evidenceList;
1789                my $peg2Data = shift @evidenceList;
1790                Trace("Peg 1 is " . $peg1Data->[1] . " and Peg 2 is " . $peg2Data->[1] . ".") if T(Coupling => 4);
1791                push @retVal, [$peg1Data->[1], $peg2Data->[1], $peg1Data->[0]];
1792            }
1793            Trace("Last index in evidence result is is $#retVal.") if T(Coupling => 4);
1794        }
1795        # Return the result.
1796        return @retVal;
1797  }  }
1798    
1799  =head3 ReadFasta  =head3 GetCoupling
1800    
1801  C<< my %sequenceData = Sprout::ReadFasta($fileName, $prefix); >>  C<< my ($couplingID, $inverted, $score) = $sprout->GetCoupling($peg1, $peg2); >>
1802    
1803  Read sequence data from a FASTA-format file. Each sequence in a FASTA file is represented by  Return the coupling (if any) for the specified pair of PEGs. If a coupling
1804  one or more lines of data. The first line begins with a > character and contains an ID.  exists, we return the coupling ID along with an indicator of whether the
1805  The remaining lines contain the sequence data in order.  coupling is stored as C<(>I<$peg1>C<, >I<$peg2>C<)> or C<(>I<$peg2>C<, >I<$peg1>C<)>.
1806    In the second case, we say the coupling is I<inverted>. The importance of an
1807    inverted coupling is that the PEGs in the evidence will appear in reverse order.
1808    
1809  =over 4  =over 4
1810    
1811  =item fileName  =item peg1
1812    
1813  Name of the FASTA file.  ID of the feature of interest.
1814    
1815  =item prefix (optional)  =item peg2
1816    
1817  Prefix to be put in front of each ID found.  ID of the potentially coupled feature.
1818    
1819  =item RETURN  =item RETURN
1820    
1821  Returns a hash that maps each ID to its sequence.  Returns a three-element list. The first element contains the database ID of
1822    the coupling. The second element is FALSE if the coupling is stored in the
1823    database in the caller specified order and TRUE if it is stored in the
1824    inverted order. The third element is the coupling's score. If the coupling
1825    does not exist, all three list elements will be C<undef>.
1826    
1827  =back  =back
1828    
1829  =cut  =cut
1830  #: Return Type %;  #: Return Type $%@;
1831  sub ReadFasta {  sub GetCoupling {
1832          # Get the parameters.          # Get the parameters.
1833          my ($fileName, $prefix) = @_;      my ($self, $peg1, $peg2) = @_;
1834          # Create the return hash.      # Declare the return values. We'll start with the coupling ID and undefine the
1835          my %retVal = ();      # flag and score until we have more information.
1836          # Open the file for input.      my ($retVal, $inverted, $score) = ($self->CouplingID($peg1, $peg2), undef, undef);
1837          open FASTAFILE, '<', $fileName;      # Find the coupling data.
1838          # Declare the ID variable and clear the sequence accumulator.      my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'],
1839          my $sequence = "";                                   "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)",
1840          my $id = "";                                   [$retVal], ["ParticipatesInCoupling(from-link)", "Coupling(score)"]);
1841        # Check to see if we found anything.
1842        if (!@pegs) {
1843            Trace("No coupling found.") if T(Coupling => 4);
1844            # No coupling, so undefine the return value.
1845            $retVal = undef;
1846        } else {
1847            # We have a coupling! Get the score and check for inversion.
1848            $score = $pegs[0]->[1];
1849            my $firstFound = $pegs[0]->[0];
1850            $inverted = ($firstFound ne $peg1);
1851            Trace("Coupling score is $score. First peg is $firstFound, peg 1 is $peg1.") if T(Coupling => 4);
1852        }
1853        # Return the result.
1854        return ($retVal, $inverted, $score);
1855    }
1856    
1857    =head3 GetSynonymGroup
1858    
1859    C<< my $id = $sprout->GetSynonymGroup($fid); >>
1860    
1861    Return the synonym group name for the specified feature.
1862    
1863    =over 4
1864    
1865    =item fid
1866    
1867    ID of the feature whose synonym group is desired.
1868    
1869    =item RETURN
1870    
1871    The name of the synonym group to which the feature belongs. If the feature does
1872    not belong to a synonym group, the feature ID itself is returned.
1873    
1874    =back
1875    
1876    =cut
1877    
1878    sub GetSynonymGroup {
1879        # Get the parameters.
1880        my ($self, $fid) = @_;
1881        # Declare the return variable.
1882        my $retVal;
1883        # Find the synonym group.
1884        my @groups = $self->GetFlat(['IsSynonymGroupFor'], "IsSynonymGroupFor(to-link) = ?",
1885                                       [$fid], 'IsSynonymGroupFor(from-link)');
1886        # Check to see if we found anything.
1887        if (@groups) {
1888            $retVal = $groups[0];
1889        } else {
1890            $retVal = $fid;
1891        }
1892        # Return the result.
1893        return $retVal;
1894    }
1895    
1896    =head3 GetBoundaries
1897    
1898    C<< my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList); >>
1899    
1900    Determine the begin and end boundaries for the locations in a list. All of the
1901    locations must belong to the same contig and have mostly the same direction in
1902    order for this method to produce a meaningful result. The resulting
1903    begin/end pair will contain all of the bases in any of the locations.
1904    
1905    =over 4
1906    
1907    =item locList
1908    
1909    List of locations to process.
1910    
1911    =item RETURN
1912    
1913    Returns a 3-tuple consisting of the contig ID, the beginning boundary,
1914    and the ending boundary. The beginning boundary will be left of the
1915    end for mostly-forward locations and right of the end for mostly-backward
1916    locations.
1917    
1918    =back
1919    
1920    =cut
1921    
1922    sub GetBoundaries {
1923        # Get the parameters.
1924        my ($self, @locList) = @_;
1925        # Set up the counters used to determine the most popular direction.
1926        my %counts = ( '+' => 0, '-' => 0 );
1927        # Get the last location and parse it.
1928        my $locObject = BasicLocation->new(pop @locList);
1929        # Prime the loop with its data.
1930        my ($contig, $beg, $end) = ($locObject->Contig, $locObject->Left, $locObject->Right);
1931        # Count its direction.
1932        $counts{$locObject->Dir}++;
1933        # Loop through the remaining locations. Note that in most situations, this loop
1934        # will not iterate at all, because most of the time we will be dealing with a
1935        # singleton list.
1936        for my $loc (@locList) {
1937            # Create a location object.
1938            my $locObject = BasicLocation->new($loc);
1939            # Count the direction.
1940            $counts{$locObject->Dir}++;
1941            # Get the left end and the right end.
1942            my $left = $locObject->Left;
1943            my $right = $locObject->Right;
1944            # Merge them into the return variables.
1945            if ($left < $beg) {
1946                $beg = $left;
1947            }
1948            if ($right > $end) {
1949                $end = $right;
1950            }
1951        }
1952        # If the most common direction is reverse, flip the begin and end markers.
1953        if ($counts{'-'} > $counts{'+'}) {
1954            ($beg, $end) = ($end, $beg);
1955        }
1956        # Return the result.
1957        return ($contig, $beg, $end);
1958    }
1959    
1960    =head3 CouplingID
1961    
1962    C<< my $couplingID = $sprout->CouplingID($peg1, $peg2); >>
1963    
1964    Return the coupling ID for a pair of feature IDs.
1965    
1966    The coupling ID is currently computed by joining the feature IDs in
1967    sorted order with a space. Client modules (that is, modules which
1968    use Sprout) should not, however, count on this always being the
1969    case. This method provides a way for abstracting the concept of a
1970    coupling ID. All that we know for sure about it is that it can be
1971    generated easily from the feature IDs and the order of the IDs
1972    in the parameter list does not matter (i.e. C<CouplingID("a1", "b1")>
1973    will have the same value as C<CouplingID("b1", "a1")>.
1974    
1975    =over 4
1976    
1977    =item peg1
1978    
1979    First feature of interest.
1980    
1981    =item peg2
1982    
1983    Second feature of interest.
1984    
1985    =item RETURN
1986    
1987    Returns the ID that would be used to represent a functional coupling of
1988    the two specified PEGs.
1989    
1990    =back
1991    
1992    =cut
1993    #: Return Type $;
1994    sub CouplingID {
1995        my ($self, @pegs) = @_;
1996        return $self->DigestKey(join " ", sort @pegs);
1997    }
1998    
1999    =head3 ReadFasta
2000    
2001    C<< my %sequenceData = Sprout::ReadFasta($fileName, $prefix); >>
2002    
2003    Read sequence data from a FASTA-format file. Each sequence in a FASTA file is represented by
2004    one or more lines of data. The first line begins with a > character and contains an ID.
2005    The remaining lines contain the sequence data in order.
2006    
2007    =over 4
2008    
2009    =item fileName
2010    
2011    Name of the FASTA file.
2012    
2013    =item prefix (optional)
2014    
2015    Prefix to be put in front of each ID found.
2016    
2017    =item RETURN
2018    
2019    Returns a hash that maps each ID to its sequence.
2020    
2021    =back
2022    
2023    =cut
2024    #: Return Type %;
2025    sub ReadFasta {
2026        # Get the parameters.
2027        my ($fileName, $prefix) = @_;
2028        # Create the return hash.
2029        my %retVal = ();
2030        # Open the file for input.
2031        open FASTAFILE, '<', $fileName;
2032        # Declare the ID variable and clear the sequence accumulator.
2033        my $sequence = "";
2034        my $id = "";
2035          # Loop through the file.          # Loop through the file.
2036          while (<FASTAFILE>) {          while (<FASTAFILE>) {
2037                  # Get the current line.                  # Get the current line.
# Line 1394  Line 2040 
2040                  if ($line =~ m/^>\s*(.+?)(\s|\n)/) {                  if ($line =~ m/^>\s*(.+?)(\s|\n)/) {
2041                          # 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.
2042                          if ($id) {                          if ($id) {
2043                                  $retVal{$id} = $sequence;                  $retVal{$id} = lc $sequence;
2044                          }                          }
2045                          # Clear the sequence accumulator and save the new ID.                          # Clear the sequence accumulator and save the new ID.
2046                          ($id, $sequence) = ("$prefix$1", "");                          ($id, $sequence) = ("$prefix$1", "");
2047                  } else {                  } else {
2048                          # 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.
2049                          # First, we get the actual data out.              # First, we get the actual data out. Note that we normalize to lower
2050                # case.
2051                          $line =~ /^\s*(.*?)(\s|\n)/;                          $line =~ /^\s*(.*?)(\s|\n)/;
2052                          $sequence .= $1;                          $sequence .= $1;
2053                  }                  }
2054          }          }
2055          # Flush out the last sequence (if any).          # Flush out the last sequence (if any).
2056          if ($sequence) {          if ($sequence) {
2057                  $retVal {$id} = $sequence;          $retVal{$id} = lc $sequence;
2058          }          }
2059        # Close the file.
2060        close FASTAFILE;
2061          # Return the hash constructed from the file.          # Return the hash constructed from the file.
2062          return %retVal;          return %retVal;
2063  }  }
# Line 1419  Line 2068 
2068    
2069  Insure that a list of feature locations is in the Sprout format. The Sprout feature location  Insure that a list of feature locations is in the Sprout format. The Sprout feature location
2070  format is I<contig>_I<beg*len> where I<*> is C<+> for a forward gene and C<-> for a backward  format is I<contig>_I<beg*len> where I<*> is C<+> for a forward gene and C<-> for a backward
2071  gene. The old format is I<contig>_I<beg>_I<end>.  gene. The old format is I<contig>_I<beg>_I<end>. If a feature is in the new format already,
2072    it will not be changed; otherwise, it will be converted. This method can also be used to
2073    perform the reverse task-- insuring that all the locations are in the old format.
2074    
2075  =over 4  =over 4
2076    
# Line 1446  Line 2097 
2097  #: Return Type @;  #: Return Type @;
2098  sub FormatLocations {  sub FormatLocations {
2099          # Get the parameters.          # Get the parameters.
2100          my $self = shift @_;      my ($self, $prefix, $locations, $oldFormat) = @_;
         my ($prefix, $locations, $oldFormat) = @_;  
2101          # Create the return list.          # Create the return list.
2102          my @retVal = ();          my @retVal = ();
2103          # Check to see if any locations were passed in.          # Check to see if any locations were passed in.
2104          if ($locations eq '') {          if ($locations eq '') {
2105              confess "No locations specified.";          Confess("No locations specified.");
2106          } else {          } else {
2107                  # Loop through the locations, converting them to the new format.                  # Loop through the locations, converting them to the new format.
2108                  for my $location (@{$locations}) {                  for my $location (@{$locations}) {
# Line 1487  Line 2137 
2137    
2138  sub DumpData {  sub DumpData {
2139          # Get the parameters.          # Get the parameters.
2140          my $self = shift @_;      my ($self) = @_;
2141          # Get the data directory name.          # Get the data directory name.
2142          my $outputDirectory = $self->{_options}->{dataDir};          my $outputDirectory = $self->{_options}->{dataDir};
2143          # Dump the relations.          # Dump the relations.
2144          $self->{_erdb}->DumpRelations($outputDirectory);      $self->DumpRelations($outputDirectory);
2145  }  }
2146    
2147  =head3 XMLFileName  =head3 XMLFileName
# Line 1503  Line 2153 
2153  =cut  =cut
2154  #: Return Type $;  #: Return Type $;
2155  sub XMLFileName {  sub XMLFileName {
2156          my $self = shift @_;      my ($self) = @_;
2157          return $self->{_xmlName};          return $self->{_xmlName};
2158  }  }
2159    
# Line 1523  Line 2173 
2173  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
2174  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>.
2175    
2176  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'}); >>
2177    
2178  =over 4  =over 4
2179    
# Line 1541  Line 2191 
2191  #: Return Type ;  #: Return Type ;
2192  sub Insert {  sub Insert {
2193          # Get the parameters.          # Get the parameters.
2194          my $self = shift @_;      my ($self, $objectType, $fieldHash) = @_;
         my ($objectType, $fieldHash) = @_;  
2195          # Call the underlying method.          # Call the underlying method.
2196          $self->{_erdb}->InsertObject($objectType, $fieldHash);      $self->InsertObject($objectType, $fieldHash);
2197  }  }
2198    
2199  =head3 Annotate  =head3 Annotate
# Line 1583  Line 2232 
2232  #: Return Type $;  #: Return Type $;
2233  sub Annotate {  sub Annotate {
2234          # Get the parameters.          # Get the parameters.
2235          my $self = shift @_;      my ($self, $fid, $timestamp, $user, $text) = @_;
         my ($fid, $timestamp, $user, $text) = @_;  
2236          # Create the annotation ID.          # Create the annotation ID.
2237          my $aid = "$fid:$timestamp";          my $aid = "$fid:$timestamp";
2238          # Insert the Annotation object.          # Insert the Annotation object.
# Line 1604  Line 2252 
2252    
2253  =head3 AssignFunction  =head3 AssignFunction
2254    
2255  C<< my $ok = $sprout->AssignFunction($featureID, $user, $function); >>  C<< my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser); >>
2256    
2257  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
2258  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.  
2259    
2260  =over 4  =over 4
2261    
# Line 1618  Line 2265 
2265    
2266  =item user  =item user
2267    
2268  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>.
2269    
2270  =item function  =item function
2271    
2272  Text of the function being assigned.  Text of the function being assigned.
2273    
2274    =item assigningUser (optional)
2275    
2276    Name of the individual user making the assignment. If omitted, defaults to the user group.
2277    
2278  =item RETURN  =item RETURN
2279    
2280  Returns 1 if successful, 0 if an error occurred.  Returns 1 if successful, 0 if an error occurred.
# Line 1634  Line 2285 
2285  #: Return Type $;  #: Return Type $;
2286  sub AssignFunction {  sub AssignFunction {
2287          # Get the parameters.          # Get the parameters.
2288          my $self = shift @_;      my ($self, $featureID, $user, $function, $assigningUser) = @_;
2289          my ($featureID, $user, $function) = @_;      # Default the assigning user.
2290        if (! $assigningUser) {
2291            $assigningUser = $user;
2292        }
2293          # Create an annotation string from the parameters.          # Create an annotation string from the parameters.
2294          my $annotationText = "$user\nset $user function to\n$function";      my $annotationText = "$assigningUser\nset $user function to\n$function";
2295          # Get the current time.          # Get the current time.
2296          my $now = time;          my $now = time;
2297          # Declare the return variable.          # Declare the return variable.
# Line 1682  Line 2336 
2336  #: Return Type @;  #: Return Type @;
2337  sub FeaturesByAlias {  sub FeaturesByAlias {
2338          # Get the parameters.          # Get the parameters.
2339          my $self = shift @_;      my ($self, $alias) = @_;
         my ($alias) = @_;  
2340          # Declare the return variable.          # Declare the return variable.
2341          my @retVal = ();          my @retVal = ();
2342          # Parse the alias.          # Parse the alias.
# Line 1699  Line 2352 
2352          return @retVal;          return @retVal;
2353  }  }
2354    
 =head3 Exists  
   
 C<< my $found = $sprout->Exists($entityName, $entityID); >>  
   
 Return TRUE if an entity exists, else FALSE.  
   
 =over 4  
   
 =item entityName  
   
 Name of the entity type (e.g. C<Feature>) relevant to the existence check.  
   
 =item entityID  
   
 ID of the entity instance whose existence is to be checked.  
   
 =item RETURN  
   
 Returns TRUE if the entity instance exists, else FALSE.  
   
 =back  
   
 =cut  
 #: Return Type $;  
 sub Exists {  
         # Get the parameters.  
         my $self = shift @_;  
         my ($entityName, $entityID) = @_;  
         # Check for the entity instance.  
         my $testInstance = $self->GetEntity($entityName, $entityID);  
         # Return an existence indicator.  
         my $retVal = ($testInstance ? 1 : 0);  
         return $retVal;  
 }  
   
2355  =head3 FeatureTranslation  =head3 FeatureTranslation
2356    
2357  C<< my $translation = $sprout->FeatureTranslation($featureID); >>  C<< my $translation = $sprout->FeatureTranslation($featureID); >>
# Line 1756  Line 2374 
2374  #: Return Type $;  #: Return Type $;
2375  sub FeatureTranslation {  sub FeatureTranslation {
2376          # Get the parameters.          # Get the parameters.
2377          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
2378          # Get the specified feature's translation.          # Get the specified feature's translation.
2379          my ($retVal) = $self->GetEntityValues("Feature", $featureID, ['Feature(translation)']);          my ($retVal) = $self->GetEntityValues("Feature", $featureID, ['Feature(translation)']);
2380          return $retVal;          return $retVal;
# Line 1789  Line 2406 
2406  #: Return Type @;  #: Return Type @;
2407  sub Taxonomy {  sub Taxonomy {
2408          # Get the parameters.          # Get the parameters.
2409          my $self = shift @_;      my ($self, $genome) = @_;
         my ($genome) = @_;  
2410          # Find the specified genome's taxonomy string.          # Find the specified genome's taxonomy string.
2411          my ($list) = $self->GetEntityValues('Genome', $genome, ['Genome(taxonomy)']);          my ($list) = $self->GetEntityValues('Genome', $genome, ['Genome(taxonomy)']);
2412          # Declare the return variable.          # Declare the return variable.
# Line 1833  Line 2449 
2449  #: Return Type $;  #: Return Type $;
2450  sub CrudeDistance {  sub CrudeDistance {
2451          # Get the parameters.          # Get the parameters.
2452          my $self = shift @_;      my ($self, $genome1, $genome2) = @_;
         my ($genome1, $genome2) = @_;  
2453          # Insure that the distance is commutative by sorting the genome IDs.          # Insure that the distance is commutative by sorting the genome IDs.
2454          my ($genomeA, $genomeB);          my ($genomeA, $genomeB);
2455          if ($genome2 < $genome2) {          if ($genome2 < $genome2) {
# Line 1881  Line 2496 
2496  #: Return Type $;  #: Return Type $;
2497  sub RoleName {  sub RoleName {
2498          # Get the parameters.          # Get the parameters.
2499          my $self = shift @_;      my ($self, $roleID) = @_;
         my ($roleID) = @_;  
2500          # Get the specified role's name.          # Get the specified role's name.
2501          my ($retVal) = $self->GetEntityValues('Role', $roleID, ['Role(name)']);          my ($retVal) = $self->GetEntityValues('Role', $roleID, ['Role(name)']);
2502          # Use the ID if the role has no name.          # Use the ID if the role has no name.
# Line 1915  Line 2529 
2529  #: Return Type @;  #: Return Type @;
2530  sub RoleDiagrams {  sub RoleDiagrams {
2531          # Get the parameters.          # Get the parameters.
2532          my $self = shift @_;      my ($self, $roleID) = @_;
         my ($roleID) = @_;  
2533          # Query for the diagrams.          # Query for the diagrams.
2534          my @retVal = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],          my @retVal = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],
2535                                                                  'RoleOccursIn(to-link)');                                                                  'RoleOccursIn(to-link)');
# Line 1924  Line 2537 
2537          return @retVal;          return @retVal;
2538  }  }
2539    
2540    =head3 GetProperties
2541    
2542    C<< my @list = $sprout->GetProperties($fid, $key, $value, $url); >>
2543    
2544    Return a list of the properties with the specified characteristics.
2545    
2546    Properties are the Sprout analog of the FIG attributes. The call is
2547    passed directly to the CustomAttributes or RemoteCustomAttributes object
2548    contained in this object.
2549    
2550    This method returns a series of tuples that match the specified criteria. Each tuple
2551    will contain an object ID, a key, and one or more values. The parameters to this
2552    method therefore correspond structurally to the values expected in each tuple. In
2553    addition, you can ask for a generic search by suffixing a percent sign (C<%>) to any
2554    of the parameters. So, for example,
2555    
2556        my @attributeList = $sprout->GetProperties('fig|100226.1.peg.1004', 'structure%', 1, 2);
2557    
2558    would return something like
2559    
2560        ['fig}100226.1.peg.1004', 'structure', 1, 2]
2561        ['fig}100226.1.peg.1004', 'structure1', 1, 2]
2562        ['fig}100226.1.peg.1004', 'structure2', 1, 2]
2563        ['fig}100226.1.peg.1004', 'structureA', 1, 2]
2564    
2565    Use of C<undef> in any position acts as a wild card (all values). You can also specify
2566    a list reference in the ID column. Thus,
2567    
2568        my @attributeList = $sprout->GetProperties(['100226.1', 'fig|100226.1.%'], 'PUBMED');
2569    
2570    would get the PUBMED attribute data for Streptomyces coelicolor A3(2) and all its
2571    features.
2572    
2573    In addition to values in multiple sections, a single attribute key can have multiple
2574    values, so even
2575    
2576        my @attributeList = $sprout->GetProperties($peg, 'virulent');
2577    
2578    which has no wildcard in the key or the object ID, may return multiple tuples.
2579    
2580    =over 4
2581    
2582    =item objectID
2583    
2584    ID of object whose attributes are desired. If the attributes are desired for multiple
2585    objects, this parameter can be specified as a list reference. If the attributes are
2586    desired for all objects, specify C<undef> or an empty string. Finally, you can specify
2587    attributes for a range of object IDs by putting a percent sign (C<%>) at the end.
2588    
2589    =item key
2590    
2591    Attribute key name. A value of C<undef> or an empty string will match all
2592    attribute keys. If the values are desired for multiple keys, this parameter can be
2593    specified as a list reference. Finally, you can specify attributes for a range of
2594    keys by putting a percent sign (C<%>) at the end.
2595    
2596    =item values
2597    
2598    List of the desired attribute values, section by section. If C<undef>
2599    or an empty string is specified, all values in that section will match. A
2600    generic match can be requested by placing a percent sign (C<%>) at the end.
2601    In that case, all values that match up to and not including the percent sign
2602    will match. You may also specify a regular expression enclosed
2603    in slashes. All values that match the regular expression will be returned. For
2604    performance reasons, only values have this extra capability.
2605    
2606    =item RETURN
2607    
2608    Returns a list of tuples. The first element in the tuple is an object ID, the
2609    second is an attribute key, and the remaining elements are the sections of
2610    the attribute value. All of the tuples will match the criteria set forth in
2611    the parameter list.
2612    
2613    =back
2614    
2615    =cut
2616    
2617    sub GetProperties {
2618        # Get the parameters.
2619        my ($self, @parms) = @_;
2620        # Declare the return variable.
2621        my @retVal = $self->{_ca}->GetAttributes(@parms);
2622        # Return the result.
2623        return @retVal;
2624    }
2625    
2626  =head3 FeatureProperties  =head3 FeatureProperties
2627    
2628  C<< my @properties = $sprout->FeatureProperties($featureID); >>  C<< my @properties = $sprout->FeatureProperties($featureID); >>
# Line 1932  Line 2631 
2631  that specify special characteristics of the feature. For example, a property could indicate  that specify special characteristics of the feature. For example, a property could indicate
2632  that a feature is essential to the survival of the organism or that it has benign influence  that a feature is essential to the survival of the organism or that it has benign influence
2633  on the activities of a pathogen. Each property is returned as a triple of the form  on the activities of a pathogen. Each property is returned as a triple of the form
2634  C<($key,$value,$url)>, where C<$key> is the property name, C<$value> is its value (commonly  C<($key,@values)>, where C<$key> is the property name and  C<@values> are its values.
 a 1 or a 0, but possibly a string or a floating-point value), and C<$url> is a string describing  
 the web address or citation in which the property's value for the feature was identified.  
2635    
2636  =over 4  =over 4
2637    
# Line 1944  Line 2641 
2641    
2642  =item RETURN  =item RETURN
2643    
2644  Returns a list of triples, each triple containing the property name, its value, and a URL or  Returns a list of tuples, each tuple containing the property name and its values.
 citation.  
2645    
2646  =back  =back
2647    
# Line 1953  Line 2649 
2649  #: Return Type @@;  #: Return Type @@;
2650  sub FeatureProperties {  sub FeatureProperties {
2651          # Get the parameters.          # Get the parameters.
2652          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
2653          # Get the properties.          # Get the properties.
2654          my @retVal = $self->GetAll(['HasProperty', 'Property'], "HasProperty(from-link) = ?", [$featureID],      my @attributes = $self->{_ca}->GetAttributes($featureID);
2655                                                          ['Property(property-name)', 'Property(property-value)',      # Strip the feature ID off each tuple.
2656                                                           'HasProperty(evidence)']);      my @retVal = ();
2657        for my $attributeRow (@attributes) {
2658            shift @{$attributeRow};
2659            push @retVal, $attributeRow;
2660        }
2661          # Return the resulting list.          # Return the resulting list.
2662          return @retVal;          return @retVal;
2663  }  }
# Line 1985  Line 2684 
2684  #: Return Type $;  #: Return Type $;
2685  sub DiagramName {  sub DiagramName {
2686          # Get the parameters.          # Get the parameters.
2687          my $self = shift @_;      my ($self, $diagramID) = @_;
         my ($diagramID) = @_;  
2688          # Get the specified diagram's name and return it.          # Get the specified diagram's name and return it.
2689          my ($retVal) = $self->GetEntityValues('Diagram', $diagramID, ['Diagram(name)']);          my ($retVal) = $self->GetEntityValues('Diagram', $diagramID, ['Diagram(name)']);
2690          return $retVal;          return $retVal;
2691  }  }
2692    
2693    =head3 PropertyID
2694    
2695    C<< my $id = $sprout->PropertyID($propName, $propValue); >>
2696    
2697    Return the ID of the specified property name and value pair, if the
2698    pair exists. Only a small subset of the FIG attributes are stored as
2699    Sprout properties, mostly for use in search optimization.
2700    
2701    =over 4
2702    
2703    =item propName
2704    
2705    Name of the desired property.
2706    
2707    =item propValue
2708    
2709    Value expected for the desired property.
2710    
2711    =item RETURN
2712    
2713    Returns the ID of the name/value pair, or C<undef> if the pair does not exist.
2714    
2715    =back
2716    
2717    =cut
2718    
2719    sub PropertyID {
2720        # Get the parameters.
2721        my ($self, $propName, $propValue) = @_;
2722        # Try to find the ID.
2723        my ($retVal) = $self->GetFlat(['Property'],
2724                                      "Property(property-name) = ? AND Property(property-value) = ?",
2725                                      [$propName, $propValue], 'Property(id)');
2726        # Return the result.
2727        return $retVal;
2728    }
2729    
2730  =head3 MergedAnnotations  =head3 MergedAnnotations
2731    
2732  C<< my @annotationList = $sprout->MergedAnnotations(\@list); >>  C<< my @annotationList = $sprout->MergedAnnotations(\@list); >>
# Line 2018  Line 2753 
2753  #: Return Type @;  #: Return Type @;
2754  sub MergedAnnotations {  sub MergedAnnotations {
2755          # Get the parameters.          # Get the parameters.
2756          my $self = shift @_;      my ($self, $list) = @_;
         my ($list) = @_;  
2757          # Create a list to hold the annotation tuples found.          # Create a list to hold the annotation tuples found.
2758          my @tuples = ();          my @tuples = ();
2759          # Loop through the features in the input list.          # Loop through the features in the input list.
# Line 2067  Line 2801 
2801  #: Return Type @;  #: Return Type @;
2802  sub RoleNeighbors {  sub RoleNeighbors {
2803          # Get the parameters.          # Get the parameters.
2804          my $self = shift @_;      my ($self, $roleID) = @_;
         my ($roleID) = @_;  
2805          # Get all the diagrams containing this role.          # Get all the diagrams containing this role.
2806          my @diagrams = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],          my @diagrams = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],
2807                                                                    'RoleOccursIn(to-link)');                                                                    'RoleOccursIn(to-link)');
# Line 2110  Line 2843 
2843  #: Return Type @;  #: Return Type @;
2844  sub FeatureLinks {  sub FeatureLinks {
2845          # Get the parameters.          # Get the parameters.
2846          my $self = shift @_;      my ($self, $featureID) = @_;
         my ($featureID) = @_;  
2847          # Get the feature's links.          # Get the feature's links.
2848          my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(link)']);          my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(link)']);
2849          # Return the feature's links.          # Return the feature's links.
# Line 2123  Line 2855 
2855  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>
2856    
2857  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
2858  to the role the feature performs.  to the roles the feature performs.
2859    
2860  =over 4  =over 4
2861    
# Line 2133  Line 2865 
2865    
2866  =item RETURN  =item RETURN
2867    
2868  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.
2869    
2870  =back  =back
2871    
2872  =cut  =cut
2873  #: Return Type %;  #: Return Type %@;
2874  sub SubsystemsOf {  sub SubsystemsOf {
2875          # Get the parameters.          # Get the parameters.
2876          my $self = shift @_;      my ($self, $featureID) = @_;
2877          my ($featureID) = @_;      # Get the subsystem list.
         # Use the SSCell to connect features to subsystems.  
2878          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],
2879                                                                          "ContainsFeature(to-link) = ?", [$featureID],                                                                          "ContainsFeature(to-link) = ?", [$featureID],
2880                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);
2881          # Create the return value.          # Create the return value.
2882          my %retVal = ();          my %retVal = ();
2883        # Build a hash to weed out duplicates. Sometimes the same PEG and role appears
2884        # in two spreadsheet cells.
2885        my %dupHash = ();
2886          # Loop through the results, adding them to the hash.          # Loop through the results, adding them to the hash.
2887          for my $record (@subsystems) {          for my $record (@subsystems) {
2888                  $retVal{$record->[0]} = $record->[1];          # Get this subsystem and role.
2889            my ($subsys, $role) = @{$record};
2890            # Insure it's the first time for both.
2891            my $dupKey = "$subsys\n$role";
2892            if (! exists $dupHash{"$subsys\n$role"}) {
2893                $dupHash{$dupKey} = 1;
2894                push @{$retVal{$subsys}}, $role;
2895            }
2896          }          }
2897          # Return the hash.          # Return the hash.
2898          return %retVal;          return %retVal;
2899  }  }
2900    
2901    =head3 SubsystemList
2902    
2903    C<< my @subsystems = $sprout->SubsystemList($featureID); >>
2904    
2905    Return a list containing the names of the subsystems in which the specified
2906    feature participates. Unlike L</SubsystemsOf>, this method only returns the
2907    subsystem names, not the roles.
2908    
2909    =over 4
2910    
2911    =item featureID
2912    
2913    ID of the feature whose subsystem names are desired.
2914    
2915    =item RETURN
2916    
2917    Returns a list of the names of the subsystems in which the feature participates.
2918    
2919    =back
2920    
2921    =cut
2922    #: Return Type @;
2923    sub SubsystemList {
2924        # Get the parameters.
2925        my ($self, $featureID) = @_;
2926        # Get the list of names.
2927        my @retVal = $self->GetFlat(['HasRoleInSubsystem'], "HasRoleInSubsystem(from-link) = ?",
2928                                    [$featureID], 'HasRoleInSubsystem(to-link)');
2929        # Return the result, sorted.
2930        return sort @retVal;
2931    }
2932    
2933    =head3 GenomeSubsystemData
2934    
2935    C<< my %featureData = $sprout->GenomeSubsystemData($genomeID); >>
2936    
2937    Return a hash mapping genome features to their subsystem roles.
2938    
2939    =over 4
2940    
2941    =item genomeID
2942    
2943    ID of the genome whose subsystem feature map is desired.
2944    
2945    =item RETURN
2946    
2947    Returns a hash mapping each feature of the genome to a list of 2-tuples. Eacb
2948    2-tuple contains a subsystem name followed by a role ID.
2949    
2950    =back
2951    
2952    =cut
2953    
2954    sub GenomeSubsystemData {
2955        # Get the parameters.
2956        my ($self, $genomeID) = @_;
2957        # Declare the return variable.
2958        my %retVal = ();
2959        # Get a list of the genome features that participate in subsystems. For each
2960        # feature we get its spreadsheet cells and the corresponding roles.
2961        my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf'],
2962                                 "HasFeature(from-link) = ?", [$genomeID],
2963                                 ['HasFeature(to-link)', 'IsRoleOf(to-link)', 'IsRoleOf(from-link)']);
2964        # Now we get a list of the spreadsheet cells and their associated subsystems. Subsystems
2965        # with an unknown variant code (-1) are skipped. Note the genome ID is at both ends of the
2966        # list. We use it at the beginning to get all the spreadsheet cells for the genome and
2967        # again at the end to filter out participation in subsystems with a negative variant code.
2968        my @cellData = $self->GetAll(['IsGenomeOf', 'HasSSCell', 'ParticipatesIn'],
2969                                     "IsGenomeOf(from-link) = ? AND ParticipatesIn(variant-code) >= 0 AND ParticipatesIn(from-link) = ?",
2970                                     [$genomeID, $genomeID], ['HasSSCell(to-link)', 'HasSSCell(from-link)']);
2971        # Now "@roleData" lists the spreadsheet cell and role for each of the genome's features.
2972        # "@cellData" lists the subsystem name for each of the genome's spreadsheet cells. We
2973        # link these two lists together to create the result. First, we want a hash mapping
2974        # spreadsheet cells to subsystem names.
2975        my %subHash = map { $_->[0] => $_->[1] } @cellData;
2976        # We loop through @cellData to build the hash.
2977        for my $roleEntry (@roleData) {
2978            # Get the data for this feature and cell.
2979            my ($fid, $cellID, $role) = @{$roleEntry};
2980            # Check for a subsystem name.
2981            my $subsys = $subHash{$cellID};
2982            if ($subsys) {
2983                # Insure this feature has an entry in the return hash.
2984                if (! exists $retVal{$fid}) { $retVal{$fid} = []; }
2985                # Merge in this new data.
2986                push @{$retVal{$fid}}, [$subsys, $role];
2987            }
2988        }
2989        # Return the result.
2990        return %retVal;
2991    }
2992    
2993  =head3 RelatedFeatures  =head3 RelatedFeatures
2994    
2995  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>
# Line 2190  Line 3023 
3023  #: Return Type @;  #: Return Type @;
3024  sub RelatedFeatures {  sub RelatedFeatures {
3025          # Get the parameters.          # Get the parameters.
3026          my $self = shift @_;      my ($self, $featureID, $function, $userID) = @_;
         my ($featureID, $function, $userID) = @_;  
3027          # 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.
3028          my @bbhFeatures = $self->GetFlat(['IsBidirectionalBestHitOf'],      my @bbhFeatures = map { $_->[0] } FIGRules::BBHData($featureID);
                                                                          "IsBidirectionalBestHitOf(from-link) = ?", [$featureID],  
                                                                          'IsBidirectionalBestHitOf(to-link)');  
3029          # 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
3030          # functional assignment.          # functional assignment.
3031          my @retVal = ();          my @retVal = ();
# Line 2239  Line 3069 
3069  #: Return Type @;  #: Return Type @;
3070  sub TaxonomySort {  sub TaxonomySort {
3071          # Get the parameters.          # Get the parameters.
3072          my $self = shift @_;      my ($self, $featureIDs) = @_;
         my ($featureIDs) = @_;  
3073          # Create the working hash table.          # Create the working hash table.
3074          my %hashBuffer = ();          my %hashBuffer = ();
3075          # Loop through the features.          # Loop through the features.
# Line 2249  Line 3078 
3078                  my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",                  my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",
3079                                                                                  [$fid], 'Genome(taxonomy)');                                                                                  [$fid], 'Genome(taxonomy)');
3080                  # Add this feature to the hash buffer.                  # Add this feature to the hash buffer.
3081                  if (exists $hashBuffer{$taxonomy}) {          Tracer::AddToListMap(\%hashBuffer, $taxonomy, $fid);
                         push @{$hashBuffer{$taxonomy}}, $fid;  
                 } else {  
                         $hashBuffer{$taxonomy} = [$fid];  
                 }  
3082          }          }
3083          # Sort the keys and get the elements.          # Sort the keys and get the elements.
3084          my @retVal = ();          my @retVal = ();
# Line 2264  Line 3089 
3089          return @retVal;          return @retVal;
3090  }  }
3091    
3092  =head3 GetAll  =head3 Protein
   
 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.  
3093    
3094  The list returned will be a list of lists. Each element of the list will contain  C<< my $protein = Sprout::Protein($sequence, $table); >>
 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.  
3095    
3096  C<< $query = $sprout->Get(['ContainsFeature', 'Feature'], "ContainsFeature(from-link) = ?", [$ssCellID], ['Feature(id)', 'Feature(alias)']); >>  Translate a DNA sequence into a protein sequence.
3097    
3098  =over 4  =over 4
3099    
3100  =item objectNames  =item sequence
   
 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  
3101    
3102  List of the fields to be returned in each element of the list returned.  DNA sequence to translate.
3103    
3104  =item count  =item table (optional)
3105    
3106  Maximum number of records to return. If omitted or 0, all available records will be returned.  Reference to a Hash that translates DNA triples to proteins. A triple that does not
3107    appear in the hash will be translated automatically to C<X>.
3108    
3109  =item RETURN  =item RETURN
3110    
3111  Returns a list of list references. Each element of the return list contains the values for the  Returns the protein sequence that would be created by the DNA sequence.
 fields specified in the B<fields> parameter.  
   
 =back  
   
 =cut  
 #: Return Type @@;  
 sub GetAll {  
         # Get the parameters.  
         my $self = shift @_;  
         my ($objectNames, $filterClause, $parameterList, $fields, $count) = @_;  
         # Create the query.  
         my $query = $self->Get($objectNames, $filterClause, $parameterList);  
         # Set up a counter of the number of records read.  
         my $fetched = 0;  
         # Insure the counter has a value.  
         if (!defined $count) {  
                 $count = 0;  
         }  
         # Loop through the records returned, extracting the fields. Note that if the  
         # counter is non-zero, we stop when the number of records read hits the count.  
         my @retVal = ();  
         while (($count == 0 || $fetched < $count) && (my $row = $query->Fetch())) {  
                 my @rowData = $row->Values($fields);  
                 push @retVal, \@rowData;  
                 $fetched++;  
         }  
         # Return the resulting list.  
         return @retVal;  
 }  
   
 =head3 GetFlat  
   
 C<< my @list = $sprout->GetFlat(\@objectNames, $filterClause, $parameterList, $field); >>  
   
 This is a variation of L</GetAll> that asks for only a single field per record and  
 returns a single flattened list.  
   
 =over 4  
   
 =item objectNames  
   
 List containing the names of the entity and relationship objects to be retrieved.  
   
 =item filterClause  
   
 WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can  
 be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form  
 B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the  
 parameter list as additional parameters. The fields in a filter clause can come from primary  
 entity relations, relationship relations, or secondary entity relations; however, all of the  
 entities and relationships involved must be included in the list of object names.  
   
 =item parameterList  
   
 List of the parameters to be substituted in for the parameters marks in the filter clause.  
   
 =item field  
   
 Name of the field to be used to get the elements of the list returned.  
   
 =item RETURN  
   
 Returns a list of values.  
   
 =back  
   
 =cut  
 #: Return Type @;  
 sub GetFlat {  
         # Get the parameters.  
         my $self = shift @_;  
         my ($objectNames, $filterClause, $parameterList, $field) = @_;  
         # Construct the query.  
         my $query = $self->Get($objectNames, $filterClause, $parameterList);  
         # Create the result list.  
         my @retVal = ();  
         # Loop through the records, adding the field values found to the result list.  
         while (my $row = $query->Fetch()) {  
                 push @retVal, $row->Value($field);  
         }  
         # Return the list created.  
         return @retVal;  
 }  
   
 =head3 Protein  
   
 C<< my $protein = Sprout::Protein($sequence, $table); >>  
   
 Translate a DNA sequence into a protein sequence.  
   
 =over 4  
   
 =item sequence  
   
 DNA sequence to translate.  
   
 =item table (optional)  
   
 Reference to a Hash that translates DNA triples to proteins. A triple that does not  
 appear in the hash will be translated automatically to C<X>.  
   
 =item RETURN  
   
 Returns the protein sequence that would be created by the DNA sequence.  
3112    
3113  =back  =back
3114    
# Line 2470  Line 3161 
3161          # Loop through the input triples.          # Loop through the input triples.
3162          my $n = length $sequence;          my $n = length $sequence;
3163          for (my $i = 0; $i < $n; $i += 3) {          for (my $i = 0; $i < $n; $i += 3) {
3164                  # Get the current triple from the sequence.          # Get the current triple from the sequence. Note we convert to
3165                  my $triple = substr($sequence, $i, 3);          # upper case to insure a match.
3166            my $triple = uc substr($sequence, $i, 3);
3167                  # Translate it using the table.                  # Translate it using the table.
3168                  my $protein = "X";                  my $protein = "X";
3169                  if (exists $table->{$triple}) { $protein = $table->{$triple}; }                  if (exists $table->{$triple}) { $protein = $table->{$triple}; }
# Line 2495  Line 3187 
3187  #: Return Type @;  #: Return Type @;
3188  sub LoadInfo {  sub LoadInfo {
3189          # Get the parameters.          # Get the parameters.
3190          my $self = shift @_;      my ($self) = @_;
3191          # 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.
3192          my @retVal = ($self->{_options}->{dataDir});          my @retVal = ($self->{_options}->{dataDir});
3193          # Concatenate the table names.          # Concatenate the table names.
3194          push @retVal, $self->{_erdb}->GetTableNames();      push @retVal, $self->GetTableNames();
3195          # Return the result.          # Return the result.
3196          return @retVal;          return @retVal;
3197  }  }
3198    
3199    =head3 BBHMatrix
3200    
3201    C<< my %bbhMap = $sprout->BBHMatrix($genomeID, $cutoff, @targets); >>
3202    
3203    Find all the bidirectional best hits for the features of a genome in a
3204    specified list of target genomes. The return value will be a hash mapping
3205    features in the original genome to their bidirectional best hits in the
3206    target genomes.
3207    
3208    =over 4
3209    
3210    =item genomeID
3211    
3212    ID of the genome whose features are to be examined for bidirectional best hits.
3213    
3214    =item cutoff
3215    
3216    A cutoff value. Only hits with a score lower than the cutoff will be returned.
3217    
3218    =item targets
3219    
3220    List of target genomes. Only pairs originating in the original
3221    genome and landing in one of the target genomes will be returned.
3222    
3223    =item RETURN
3224    
3225    Returns a hash mapping each feature in the original genome to a hash mapping its
3226    BBH pegs in the target genomes to their scores.
3227    
3228    =back
3229    
3230    =cut
3231    
3232    sub BBHMatrix {
3233        # Get the parameters.
3234        my ($self, $genomeID, $cutoff, @targets) = @_;
3235        # Declare the return variable.
3236        my %retVal = ();
3237        # Ask for the BBHs.
3238        my @bbhList = FIGRules::BatchBBHs("fig|$genomeID.%", $cutoff, @targets);
3239        # We now have a set of 4-tuples that we need to convert into a hash of hashes.
3240        for my $bbhData (@bbhList) {
3241            my ($peg1, $peg2, $score) = @{$bbhData};
3242            if (! exists $retVal{$peg1}) {
3243                $retVal{$peg1} = { $peg2 => $score };
3244            } else {
3245                $retVal{$peg1}->{$peg2} = $score;
3246            }
3247        }
3248        # Return the result.
3249        return %retVal;
3250    }
3251    
3252    
3253    =head3 SimMatrix
3254    
3255    C<< my %simMap = $sprout->SimMatrix($genomeID, $cutoff, @targets); >>
3256    
3257    Find all the similarities for the features of a genome in a
3258    specified list of target genomes. The return value will be a hash mapping
3259    features in the original genome to their similarites in the
3260    target genomes.
3261    
3262    =over 4
3263    
3264    =item genomeID
3265    
3266    ID of the genome whose features are to be examined for similarities.
3267    
3268    =item cutoff
3269    
3270    A cutoff value. Only hits with a score lower than the cutoff will be returned.
3271    
3272    =item targets
3273    
3274    List of target genomes. Only pairs originating in the original
3275    genome and landing in one of the target genomes will be returned.
3276    
3277    =item RETURN
3278    
3279    Returns a hash mapping each feature in the original genome to a hash mapping its
3280    similar pegs in the target genomes to their scores.
3281    
3282    =back
3283    
3284    =cut
3285    
3286    sub SimMatrix {
3287        # Get the parameters.
3288        my ($self, $genomeID, $cutoff, @targets) = @_;
3289        # Declare the return variable.
3290        my %retVal = ();
3291        # Get the list of features in the source organism.
3292        my @fids = $self->FeaturesOf($genomeID);
3293        # Ask for the sims. We only want similarities to fig features.
3294        my $simList = FIGRules::GetNetworkSims($self, \@fids, {}, 1000, $cutoff, "fig");
3295        if (! defined $simList) {
3296            Confess("Unable to retrieve similarities from server.");
3297        } else {
3298            Trace("Processing sims.") if T(3);
3299            # We now have a set of sims that we need to convert into a hash of hashes. First, we
3300            # Create a hash for the target genomes.
3301            my %targetHash = map { $_ => 1 } @targets;
3302            for my $simData (@{$simList}) {
3303                # Get the PEGs and the score.
3304                my ($peg1, $peg2, $score) = ($simData->id1, $simData->id2, $simData->psc);
3305                # Insure the second ID is in the target list.
3306                my ($genome2) = FIGRules::ParseFeatureID($peg2);
3307                if (exists $targetHash{$genome2}) {
3308                    # Here it is. Now we need to add it to the return hash. How we do that depends
3309                    # on whether or not $peg1 is new to us.
3310                    if (! exists $retVal{$peg1}) {
3311                        $retVal{$peg1} = { $peg2 => $score };
3312                    } else {
3313                        $retVal{$peg1}->{$peg2} = $score;
3314                    }
3315                }
3316            }
3317        }
3318        # Return the result.
3319        return %retVal;
3320    }
3321    
3322    
3323  =head3 LowBBHs  =head3 LowBBHs
3324    
3325  C<< my %bbhMap = $sprout->GoodBBHs($featureID, $cutoff); >>  C<< my %bbhMap = $sprout->LowBBHs($featureID, $cutoff); >>
3326    
3327  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
3328  specified cutoff value. A higher cutoff value will allow inclusion of hits with  specified cutoff value. A higher cutoff value will allow inclusion of hits with
# Line 2532  Line 3348 
3348  #: Return Type %;  #: Return Type %;
3349  sub LowBBHs {  sub LowBBHs {
3350          # Get the parsameters.          # Get the parsameters.
3351          my $self = shift @_;      my ($self, $featureID, $cutoff) = @_;
         my ($featureID, $cutoff) = @_;  
3352          # Create the return hash.          # Create the return hash.
3353          my %retVal = ();          my %retVal = ();
3354          # Create a query to get the desired BBHs.      # Query for the desired BBHs.
3355          my @bbhList = $self->GetAll(['IsBidirectionalBestHitOf'],      my @bbhList = FIGRules::BBHData($featureID, $cutoff);
                                                                 'IsBidirectionalBestHitOf(sc) <= ? AND IsBidirectionalBestHitOf(from-link) = ?',  
                                                                 [$cutoff, $featureID],  
                                                                 ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(sc)']);  
3356          # Form the results into the return hash.          # Form the results into the return hash.
3357          for my $pair (@bbhList) {          for my $pair (@bbhList) {
3358                  $retVal{$pair->[0]} = $pair->[1];          my $fid = $pair->[0];
3359            if ($self->Exists('Feature', $fid)) {
3360                $retVal{$fid} = $pair->[1];
3361            }
3362          }          }
3363          # Return the result.          # Return the result.
3364          return %retVal;          return %retVal;
3365  }  }
3366    
3367    =head3 Sims
3368    
3369    C<< my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters); >>
3370    
3371    Get a list of similarities for a specified feature. Similarity information is not kept in the
3372    Sprout database; rather, they are retrieved from a network server. The similarities are
3373    returned as B<Sim> objects. A Sim object is actually a list reference that has been blessed
3374    so that its elements can be accessed by name.
3375    
3376    Similarities can be either raw or expanded. The raw similarities are basic
3377    hits between features with similar DNA. Expanding a raw similarity drags in any
3378    features considered substantially identical. So, for example, if features B<A1>,
3379    B<A2>, and B<A3> are all substantially identical to B<A>, then a raw similarity
3380    B<[C,A]> would be expanded to B<[C,A] [C,A1] [C,A2] [C,A3]>.
3381    
3382    =over 4
3383    
3384    =item fid
3385    
3386    ID of the feature whose similarities are desired.
3387    
3388    =item maxN
3389    
3390    Maximum number of similarities to return.
3391    
3392    =item maxP
3393    
3394    Minumum allowable similarity score.
3395    
3396    =item select
3397    
3398    Selection criterion: C<raw> means only raw similarities are returned; C<fig>
3399    means only similarities to FIG features are returned; C<all> means all expanded
3400    similarities are returned; and C<figx> means similarities are expanded until the
3401    number of FIG features equals the maximum.
3402    
3403    =item max_expand
3404    
3405    The maximum number of features to expand.
3406    
3407    =item filters
3408    
3409    Reference to a hash containing filter information, or a subroutine that can be
3410    used to filter the sims.
3411    
3412    =item RETURN
3413    
3414    Returns a reference to a list of similarity objects, or C<undef> if an error
3415    occurred.
3416    
3417    =back
3418    
3419    =cut
3420    
3421    sub Sims {
3422        # Get the parameters.
3423        my ($self, $fid, $maxN, $maxP, $select, $max_expand, $filters) = @_;
3424        # Create the shim object to test for deleted FIDs.
3425        my $shim = FidCheck->new($self);
3426        # Ask the network for sims.
3427        my $retVal = FIGRules::GetNetworkSims($shim, $fid, {}, $maxN, $maxP, $select, $max_expand, $filters);
3428        # Return the result.
3429        return $retVal;
3430    }
3431    
3432    =head3 IsAllGenomes
3433    
3434    C<< my $flag = $sprout->IsAllGenomes(\@list, \@checkList); >>
3435    
3436    Return TRUE if all genomes in the second list are represented in the first list at
3437    least one. Otherwise, return FALSE. If the second list is omitted, the first list is
3438    compared to a list of all the genomes.
3439    
3440    =over 4
3441    
3442    =item list
3443    
3444    Reference to the list to be compared to the second list.
3445    
3446    =item checkList (optional)
3447    
3448    Reference to the comparison target list. Every genome ID in this list must occur at
3449    least once in the first list. If this parameter is omitted, a list of all the genomes
3450    is used.
3451    
3452    =item RETURN
3453    
3454    Returns TRUE if every item in the second list appears at least once in the
3455    first list, else FALSE.
3456    
3457    =back
3458    
3459    =cut
3460    
3461    sub IsAllGenomes {
3462        # Get the parameters.
3463        my ($self, $list, $checkList) = @_;
3464        # Supply the checklist if it was omitted.
3465        $checkList = [$self->Genomes()] if ! defined($checkList);
3466        # Create a hash of the original list.
3467        my %testList = map { $_ => 1 } @{$list};
3468        # Declare the return variable. We assume that the representation
3469        # is complete and stop at the first failure.
3470        my $retVal = 1;
3471        my $n = scalar @{$checkList};
3472        for (my $i = 0; $retVal && $i < $n; $i++) {
3473            if (! $testList{$checkList->[$i]}) {
3474                $retVal = 0;
3475            }
3476        }
3477        # Return the result.
3478        return $retVal;
3479    }
3480    
3481  =head3 GetGroups  =head3 GetGroups
3482    
3483    C<< my %groups = $sprout->GetGroups(\@groupList); >>
3484    
3485    Return a hash mapping each group to the IDs of the genomes in the group.
3486    A list of groups may be specified, in which case only those groups will be
3487    shown. Alternatively, if no parameter is supplied, all groups will be
3488    included. Genomes that are not in any group are omitted.
3489    
3490  =cut  =cut
3491  #: Return Type %@;  #: Return Type %@;
3492  sub GetGroups {  sub GetGroups {
3493        # Get the parameters.
3494        my ($self, $groupList) = @_;
3495        # Declare the return value.
3496        my %retVal = ();
3497        # Determine whether we are getting all the groups or just some.
3498        if (defined $groupList) {
3499            # Here we have a group list. Loop through them individually,
3500            # getting a list of the relevant genomes.
3501            for my $group (@{$groupList}) {
3502                my @genomeIDs = $self->GetFlat(['Genome'], "Genome(primary-group) = ?",
3503                    [$group], "Genome(id)");
3504                $retVal{$group} = \@genomeIDs;
3505            }
3506        } else {
3507            # Here we need all of the groups. In this case, we run through all
3508            # of the genome records, putting each one found into the appropriate
3509            # group. Note that we use a filter clause to insure that only genomes
3510            # in real NMPDR groups are included in the return set.
3511            my @genomes = $self->GetAll(['Genome'], "Genome(primary-group) <> ?",
3512                                        [$FIG_Config::otherGroup], ['Genome(id)', 'Genome(primary-group)']);
3513            # Loop through the genomes found.
3514            for my $genome (@genomes) {
3515                # Pop this genome's ID off the current list.
3516                my @groups = @{$genome};
3517                my $genomeID = shift @groups;
3518                # Loop through the groups, adding the genome ID to each group's
3519                # list.
3520                for my $group (@groups) {
3521                    Tracer::AddToListMap(\%retVal, $group, $genomeID);
3522                }
3523            }
3524        }
3525        # Return the hash we just built.
3526        return %retVal;
3527    }
3528    
3529    =head3 MyGenomes
3530    
3531    C<< my @genomes = Sprout::MyGenomes($dataDir); >>
3532    
3533    Return a list of the genomes to be included in the Sprout.
3534    
3535    This method is provided for use during the Sprout load. It presumes the Genome load file has
3536    already been created. (It will be in the Sprout data directory and called either C<Genome>
3537    or C<Genome.dtx>.) Essentially, it reads in the Genome load file and strips out the genome
3538    IDs.
3539    
3540    =over 4
3541    
3542    =item dataDir
3543    
3544    Directory containing the Sprout load files.
3545    
3546    =back
3547    
3548    =cut
3549    #: Return Type @;
3550    sub MyGenomes {
3551        # Get the parameters.
3552        my ($dataDir) = @_;
3553        # Compute the genome file name.
3554        my $genomeFileName = LoadFileName($dataDir, "Genome");
3555        # Extract the genome IDs from the files.
3556        my @retVal = map { $_ =~ /^(\S+)/; $1 } Tracer::GetFile($genomeFileName);
3557        # Return the result.
3558        return @retVal;
3559    }
3560    
3561    =head3 LoadFileName
3562    
3563    C<< my $fileName = Sprout::LoadFileName($dataDir, $tableName); >>
3564    
3565    Return the name of the load file for the specified table in the specified data
3566    directory.
3567    
3568    =over 4
3569    
3570    =item dataDir
3571    
3572    Directory containing the Sprout load files.
3573    
3574    =item tableName
3575    
3576    Name of the table whose load file is desired.
3577    
3578    =item RETURN
3579    
3580    Returns the name of the file containing the load data for the specified table, or
3581    C<undef> if no load file is present.
3582    
3583    =back
3584    
3585    =cut
3586    #: Return Type $;
3587    sub LoadFileName {
3588        # Get the parameters.
3589        my ($dataDir, $tableName) = @_;
3590        # Declare the return variable.
3591        my $retVal;
3592        # Check for the various file names.
3593        if (-e "$dataDir/$tableName") {
3594            $retVal = "$dataDir/$tableName";
3595        } elsif (-e "$dataDir/$tableName.dtx") {
3596            $retVal = "$dataDir/$tableName.dtx";
3597        }
3598        # Return the result.
3599        return $retVal;
3600    }
3601    
3602    =head3 DeleteGenome
3603    
3604    C<< my $stats = $sprout->DeleteGenome($genomeID, $testFlag); >>
3605    
3606    Delete a genome from the database.
3607    
3608    =over 4
3609    
3610    =item genomeID
3611    
3612    ID of the genome to delete
3613    
3614    =item testFlag
3615    
3616    If TRUE, then the DELETE statements will be traced, but no deletions will occur.
3617    
3618    =item RETURN
3619    
3620    Returns a statistics object describing the rows deleted.
3621    
3622    =back
3623    
3624    =cut
3625    #: Return Type $%;
3626    sub DeleteGenome {
3627        # Get the parameters.
3628        my ($self, $genomeID, $testFlag) = @_;
3629        # Perform the delete for the genome's features.
3630        my $retVal = $self->Delete('Feature', "fig|$genomeID.%", testMode => $testFlag);
3631        # Perform the delete for the primary genome data.
3632        my $stats = $self->Delete('Genome', $genomeID, testMode => $testFlag);
3633        $retVal->Accumulate($stats);
3634        # Return the result.
3635        return $retVal;
3636    }
3637    
3638    =head3 Fix
3639    
3640    C<< my %fixedHash = Sprout::Fix(%groupHash); >>
3641    
3642    Prepare a genome group hash (like that returned by L</GetGroups> for processing.
3643    Groups with the same primary name will be combined. The primary name is the
3644    first capitalized word in the group name.
3645    
3646    =over 4
3647    
3648    =item groupHash
3649    
3650    Hash to be fixed up.
3651    
3652    =item RETURN
3653    
3654    Returns a fixed-up version of the hash.
3655    
3656    =back
3657    
3658    =cut
3659    
3660    sub Fix {
3661        # Get the parameters.
3662        my (%groupHash) = @_;
3663        # Create the result hash.
3664        my %retVal = ();
3665        # Copy over the genomes.
3666        for my $groupID (keys %groupHash) {
3667            # Make a safety copy of the group ID.
3668            my $realGroupID = $groupID;
3669            # Yank the primary name.
3670            if ($groupID =~ /([A-Z]\w+)/) {
3671                $realGroupID = $1;
3672            }
3673            # Append this group's genomes into the result hash.
3674            Tracer::AddToListMap(\%retVal, $realGroupID, @{$groupHash{$groupID}});
3675        }
3676        # Return the result hash.
3677        return %retVal;
3678    }
3679    
3680    =head3 GroupPageName
3681    
3682    C<< my $name = $sprout->GroupPageName($group); >>
3683    
3684    Return the name of the page for the specified NMPDR group.
3685    
3686    =over 4
3687    
3688    =item group
3689    
3690    Name of the relevant group.
3691    
3692    =item RETURN
3693    
3694    Returns the relative page name (e.g. C<../content/campy.php>). If the group file is not in
3695    memory it will be read in.
3696    
3697    =back
3698    
3699    =cut
3700    
3701    sub GroupPageName {
3702        # Get the parameters.
3703        my ($self, $group) = @_;
3704        # Declare the return variable.
3705        my $retVal;
3706        # Check for the group file data.
3707        if (! defined $self->{groupHash}) {
3708            # Read the group file.
3709            my %groupData = Sprout::ReadGroupFile($self->{_options}->{dataDir} . "/groups.tbl");
3710            # Store it in our object.
3711            $self->{groupHash} = \%groupData;
3712        }
3713        # Compute the real group name.
3714        my $realGroup = $group;
3715        if ($group =~ /([A-Z]\w+)/) {
3716            $realGroup = $1;
3717        }
3718        # Return the page name.
3719        $retVal = "../content/" . $self->{groupHash}->{$realGroup}->[1];
3720        # Return the result.
3721        return $retVal;
3722    }
3723    
3724    =head3 ReadGroupFile
3725    
3726    C<< my %groupData = Sprout::ReadGroupFile($groupFileName); >>
3727    
3728    Read in the data from the specified group file. The group file contains information
3729    about each of the NMPDR groups.
3730    
3731    =over 4
3732    
3733    =item name
3734    
3735    Name of the group.
3736    
3737    =item page
3738    
3739    Name of the group's page on the web site (e.g. C<campy.php> for
3740    Campylobacter)
3741    
3742    =item genus
3743    
3744    Genus of the group
3745    
3746    =item species
3747    
3748    Species of the group, or an empty string if the group is for an entire
3749    genus. If the group contains more than one species, the species names
3750    should be separated by commas.
3751    
3752    =back
3753    
3754    The parameters to this method are as follows
3755    
3756    =over 4
3757    
3758    =item groupFile
3759    
3760    Name of the file containing the group data.
3761    
3762    =item RETURN
3763    
3764    Returns a hash keyed on group name. The value of each hash
3765    
3766    =back
3767    
3768    =cut
3769    
3770    sub ReadGroupFile {
3771        # Get the parameters.
3772        my ($groupFileName) = @_;
3773        # Declare the return variable.
3774        my %retVal;
3775        # Read the group file.
3776        my @groupLines = Tracer::GetFile($groupFileName);
3777        for my $groupLine (@groupLines) {
3778            my ($name, $page, $genus, $species) = split(/\t/, $groupLine);
3779            $retVal{$name} = [$page, $genus, $species];
3780        }
3781        # Return the result.
3782        return %retVal;
3783    }
3784    
3785    =head3 AddProperty
3786    
3787    C<< my  = $sprout->AddProperty($featureID, $key, @values); >>
3788    
3789    Add a new attribute value (Property) to a feature.
3790    
3791    =over 4
3792    
3793    =item peg
3794    
3795    ID of the feature to which the attribute is to be added.
3796    
3797    =item key
3798    
3799    Name of the attribute (key).
3800    
3801    =item values
3802    
3803    Values of the attribute.
3804    
3805    =back
3806    
3807    =cut
3808    #: Return Type ;
3809    sub AddProperty {
3810        # Get the parameters.
3811        my ($self, $featureID, $key, @values) = @_;
3812        # Add the property using the attached attributes object.
3813        $self->{_ca}->AddAttribute($featureID, $key, @values);
3814    }
3815    
3816    =head2 Virtual Methods
3817    
3818    =head3 CleanKeywords
3819    
3820    C<< my $cleanedString = $sprout->CleanKeywords($searchExpression); >>
3821    
3822    Clean up a search expression or keyword list. This involves converting the periods
3823    in EC numbers to underscores, converting non-leading minus signs to underscores,
3824    a vertical bar or colon to an apostrophe, and forcing lower case for all alphabetic
3825    characters. In addition, any extra spaces are removed.
3826    
3827    =over 4
3828    
3829    =item searchExpression
3830    
3831    Search expression or keyword list to clean. Note that a search expression may
3832    contain boolean operators which need to be preserved. This includes leading
3833    minus signs.
3834    
3835    =item RETURN
3836    
3837    Cleaned expression or keyword list.
3838    
3839    =back
3840    
3841    =cut
3842    
3843    sub CleanKeywords {
3844        # Get the parameters.
3845        my ($self, $searchExpression) = @_;
3846        # Perform the standard cleanup.
3847        my $retVal = $self->ERDB::CleanKeywords($searchExpression);
3848        # Fix the periods in EC and TC numbers.
3849        $retVal =~ s/(\d+|\-)\.(\d+|-)\.(\d+|-)\.(\d+|-)/$1_$2_$3_$4/g;
3850        # Fix non-trailing periods.
3851        $retVal =~ s/\.(\w)/_$1/g;
3852        # Fix non-leading minus signs.
3853        $retVal =~ s/(\w)[\-]/$1_/g;
3854        # Fix the vertical bars and colons
3855        $retVal =~ s/(\w)[|:](\w)/$1'$2/g;
3856        # Return the result.
3857        return $retVal;
3858  }  }
3859    
3860  =head2 Internal Utility Methods  =head2 Internal Utility Methods
# Line 2561  Line 3862 
3862  =head3 ParseAssignment  =head3 ParseAssignment
3863    
3864  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,
3865  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
3866  will be returned.  isn't, an empty list will be returned.
3867    
3868    A functional assignment is always of the form
3869    
3870        C<set >I<YYYY>C< function to\n>I<ZZZZZ>
3871    
3872    where I<YYYY> is the B<user>, and I<ZZZZ> is the actual functional role. In most cases,
3873    the user and the assigning user (from MadeAnnotation) will be the same, but that is
3874    not always the case.
3875    
3876    In addition, the functional role may contain extra data that is stripped, such as
3877    terminating spaces or a comment separated from the rest of the text by a tab.
3878    
3879  This is a static method.  This is a static method.
3880    
3881  =over 4  =over 4
3882    
3883    =item user
3884    
3885    Name of the assigning user.
3886    
3887  =item text  =item text
3888    
3889  Text of the annotation.  Text of the annotation.
# Line 2581  Line 3897 
3897    
3898  =cut  =cut
3899    
3900  sub ParseAssignment {  sub _ParseAssignment {
3901          # Get the parameters.          # Get the parameters.
3902          my ($text) = @_;      my ($user, $text) = @_;
3903          # Declare the return value.          # Declare the return value.
3904          my @retVal = ();          my @retVal = ();
3905          # Check to see if this is a functional assignment.          # Check to see if this is a functional assignment.
3906          my ($user, $type, $function) = split(/\n/, $text);      my ($type, $function) = split(/\n/, $text);
3907          if ($type =~ m/^set $user function to$/i) {      if ($type =~ m/^set function to$/i) {
3908                  # 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.
3909                  @retVal = ($user, $function);                  @retVal = ($user, $function);
3910        } elsif ($type =~ m/^set (\S+) function to$/i) {
3911            # Here we have an assignment with a user that is passed back to the caller.
3912            @retVal = ($1, $function);
3913        }
3914        # If we have an assignment, we need to clean the function text. There may be
3915        # extra junk at the end added as a note from the user.
3916        if (defined( $retVal[1] )) {
3917            $retVal[1] =~ s/(\t\S)?\s*$//;
3918          }          }
3919          # Return the result list.          # Return the result list.
3920          return @retVal;          return @retVal;
# Line 2618  Line 3942 
3942    
3943  sub FriendlyTimestamp {  sub FriendlyTimestamp {
3944      my ($timeValue) = @_;      my ($timeValue) = @_;
3945      my $retVal = strftime("%a %b %e %H:%M:%S %Y", localtime($timeValue));      my $retVal = localtime($timeValue);
3946      return $retVal;      return $retVal;
3947  }  }
3948    
3949    
3950  1;  1;

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3