[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.32, Wed Sep 14 13:24:14 2005 UTC revision 1.125, Mon Mar 16 00:24:23 2009 UTC
# Line 2  Line 2 
2    
3      use Data::Dumper;      use Data::Dumper;
4      use strict;      use strict;
     use Carp;  
5      use DBKernel;      use DBKernel;
6      use XML::Simple;      use XML::Simple;
7      use DBQuery;      use ERDBQuery;
8      use DBObject;      use ERDBObject;
     use ERDB;  
9      use Tracer;      use Tracer;
10      use FIGRules;      use FIGRules;
11        use FidCheck;
12      use Stats;      use Stats;
13      use POSIX qw(strftime);      use POSIX qw(strftime);
14        use BasicLocation;
15        use CustomAttributes;
16        use RemoteCustomAttributes;
17        use CGI qw(-nosticky);
18        use WikiTools;
19        use BioWords;
20        use base qw(ERDB);
21    
22  =head1 Sprout Database Manipulation Object  =head1 Sprout Database Manipulation Object
23    
# Line 25  Line 30 
30  on the constructor. For example, the following invocation specifies a PostgreSQL database named I<GenDB>  on the constructor. For example, the following invocation specifies a PostgreSQL database named I<GenDB>
31  whose definition and data files are in a co-directory named F<Data>.  whose definition and data files are in a co-directory named F<Data>.
32    
33  C<< my $sprout = Sprout->new('GenDB', { dbType => 'pg', dataDir => '../Data', xmlFileName => '../Data/SproutDBD.xml' }); >>      my $sprout = Sprout->new('GenDB', { dbType => 'pg', dataDir => '../Data', xmlFileName => '../Data/SproutDBD.xml' });
34    
35  Once you have a sprout object, you may use it to re-create the database, load the tables from  Once you have a sprout object, you may use it to re-create the database, load the tables from
36  tab-delimited flat files and perform queries. Several special methods are provided for common  tab-delimited flat files and perform queries. Several special methods are provided for common
37  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
38  L</dna_seq> returns the DNA sequence for a specified genome location.  L</DNASeq> returns the DNA sequence for a specified genome location.
39    
40  =cut  The Sprout object is a subclass of the ERDB object and inherits all its properties and methods.
41    
42  #: Constructor SFXlate->new_sprout_only();  =cut
43    
44  =head2 Public Methods  =head2 Public Methods
45    
46  =head3 new  =head3 new
47    
48  C<< my $sprout = Sprout->new($dbName, \%options); >>      my $sprout = Sprout->new($dbName, \%options);
49    
50  This is the constructor for a sprout object. It connects to the database and loads the  This is the constructor for a sprout object. It connects to the database and loads the
51  database definition into memory. The positional first parameter specifies the name of the  database definition into memory. The positional first parameter specifies the name of the
# Line 50  Line 55 
55    
56  =item dbName  =item dbName
57    
58  Name of the database.  Name of the database. If omitted, the default Sprout database name is used.
59    
60  =item options  =item options
61    
# Line 62  Line 67 
67    
68  * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)  * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)
69    
70  * B<userData> user name and password, delimited by a slash (default C<root/>)  * B<userData> user name and password, delimited by a slash (default same as SEED)
71    
72  * B<port> connection port (default C<0>)  * B<port> connection port (default C<0>)
73    
74    * B<sock> connection socket (default same as SEED)
75    
76  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)
77    
78  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)
79    
80  * B<noDBOpen> suppresses the connection to the database if TRUE, else FALSE  * B<noDBOpen> suppresses the connection to the database if TRUE, else FALSE
81    
82    * B<host> name of the database host
83    
84  =back  =back
85    
86  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
87  I<fig> with a password of I<admin>. The database load files are in the directory  I<fig> with a password of I<admin>. The database load files are in the directory
88  F</usr/fig/SproutData>.  F</usr/fig/SproutData>.
89    
90  C<< my $sprout = Sprout->new('Sprout', { userData =>; 'fig/admin', dataDir => '/usr/fig/SproutData' }); >>      my $sprout = Sprout->new('Sprout', { userData => 'fig/admin', dataDir => '/usr/fig/SproutData' });
91    
92    In order to work properly with [[ERDBGeneratorPl]], the constructor has an alternate
93    form.
94    
95        my $sprout = Sprout->new(dbd => $filename);
96    
97    Where I<$fileName> is the name of the DBD file. This enables us to specify an alternate
98    DBD for the loader, which is important when the database format changes.
99    
100  =cut  =cut
101    
102  sub new {  sub new {
103      # Get the parameters.      # Get the parameters.
104      my ($class, $dbName, $options) = @_;      my ($class, $dbName, $options) = @_;
105        # Check for the alternate signature, and default the database name if it is missing.
106        if ($dbName eq 'dbd') {
107            $dbName = $FIG_Config::sproutDB;
108            $options = { xmlFileName => $options };
109        } elsif (! defined $dbName) {
110            $dbName = $FIG_Config::sproutDB;
111        } elsif (ref $dbName eq 'HASH') {
112            $options = $dbName;
113            $dbName = $FIG_Config::sproutDB;
114        }
115        # Compute the DBD directory.
116        my $dbd_dir = (defined($FIG_Config::dbd_dir) ? $FIG_Config::dbd_dir :
117                                                      $FIG_Config::fig );
118      # 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
119      # the incoming data.      # the incoming data.
120      my $optionTable = Tracer::GetOptions({      my $optionTable = Tracer::GetOptions({
# Line 92  Line 122 
122                                                          # database type                                                          # database type
123                         dataDir      => $FIG_Config::sproutData,                         dataDir      => $FIG_Config::sproutData,
124                                                          # data file directory                                                          # data file directory
125                         xmlFileName  => "$FIG_Config::sproutData/SproutDBD.xml",                         xmlFileName  => "$dbd_dir/SproutDBD.xml",
126                                                          # database definition file name                                                          # database definition file name
127                         userData     => "$FIG_Config::dbuser/$FIG_Config::dbpass",                         userData     => "$FIG_Config::sproutUser/$FIG_Config::sproutPass",
128                                                          # user name and password                                                          # user name and password
129                         port         => $FIG_Config::dbport,                         port         => $FIG_Config::sproutPort,
130                                                          # database connection port                                                          # database connection port
131                           sock         => $FIG_Config::sproutSock,
132                           host         => $FIG_Config::sprout_host,
133                         maxSegmentLength => 4500,        # maximum feature segment length                         maxSegmentLength => 4500,        # maximum feature segment length
134                         maxSequenceLength => 8000,       # maximum contig sequence length                         maxSequenceLength => 8000,       # maximum contig sequence length
135                         noDBOpen     => 0,               # 1 to suppress the database open                         noDBOpen     => 0,               # 1 to suppress the database open
136                           demandDriven => 0,               # 1 for forward-only queries
137                        }, $options);                        }, $options);
138      # Get the data directory.      # Get the data directory.
139      my $dataDir = $optionTable->{dataDir};      my $dataDir = $optionTable->{dataDir};
# Line 110  Line 143 
143      # Connect to the database.      # Connect to the database.
144      my $dbh;      my $dbh;
145      if (! $optionTable->{noDBOpen}) {      if (! $optionTable->{noDBOpen}) {
146            Trace("Connect data: host = $optionTable->{host}, port = $optionTable->{port}.") if T(3);
147          $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,          $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,
148                                  $password, $optionTable->{port});                                  $password, $optionTable->{port}, $optionTable->{host}, $optionTable->{sock});
149      }      }
150      # Create the ERDB object.      # Create the ERDB object.
151      my $xmlFileName = "$optionTable->{xmlFileName}";      my $xmlFileName = "$optionTable->{xmlFileName}";
152      my $erdb = ERDB->new($dbh, $xmlFileName);      my $retVal = ERDB::new($class, $dbh, $xmlFileName, %$optionTable);
153      # Create this object.      # Add the option table and XML file name.
154      my $self = { _erdb => $erdb, _options => $optionTable, _xmlName => $xmlFileName };      $retVal->{_options} = $optionTable;
155      # Bless and return it.      $retVal->{_xmlName} = $xmlFileName;
156      bless $self;      # Set up space for the group file data.
157      return $self;      $retVal->{groupHash} = undef;
158        # Set up space for the genome hash. We use this to identify NMPDR genomes
159        # and remember genome data.
160        $retVal->{genomeHash} = {};
161        $retVal->{genomeHashFilled} = 0;
162        # Remember the data directory name.
163        $retVal->{dataDir} = $dataDir;
164        # Return it.
165        return $retVal;
166  }  }
167    
168  =head3 MaxSegment  =head3 ca
169    
170  C<< my $length = $sprout->MaxSegment(); >>      my $ca = $sprout->ca():;
171    
172  This method returns the maximum permissible length of a feature segment. The length is important  Return the [[CustomAttributesPm]] object for retrieving object
173  because it enables us to make reasonable guesses at how to find features inside a particular  properties.
 contig region. For example, if the maximum length is 4000 and we're looking for a feature that  
 overlaps the region from 6000 to 7000 we know that the starting position must be between 2001  
 and 10999.  
174    
175  =cut  =cut
 #: Return Type $;  
 sub MaxSegment {  
     my ($self) = @_;  
     return $self->{_options}->{maxSegmentLength};  
 }  
   
 =head3 MaxSequence  
   
 C<< my $length = $sprout->MaxSequence(); >>  
   
 This method returns the maximum permissible length of a contig sequence. A contig is broken  
 into sequences in order to save memory resources. In particular, when manipulating features,  
 we generally only need a few sequences in memory rather than the entire contig.  
176    
177  =cut  sub ca {
178  #: Return Type $;      # Get the parameters.
 sub MaxSequence {  
179      my ($self) = @_;      my ($self) = @_;
180      return $self->{_options}->{maxSequenceLength};      # Do we already have an attribute object?
181        my $retVal = $self->{_ca};
182        if (! defined $retVal) {
183            # No, create one. How we do it depends on the configuration.
184            if ($FIG_Config::attrURL) {
185                Trace("Remote attribute server $FIG_Config::attrURL chosen.") if T(3);
186                $retVal = RemoteCustomAttributes->new($FIG_Config::attrURL);
187            } elsif ($FIG_Config::attrDbName) {
188                Trace("Local attribute database $FIG_Config::attrDbName chosen.") if T(3);
189                my $user = ($FIG_Config::arch eq 'win' ? 'self' : scalar(getpwent()));
190                $retVal = CustomAttributes->new(user => $user);
191            }
192            # Save it for next time.
193            $self->{_ca} = $retVal;
194        }
195        # Return the result.
196        return $retVal;
197  }  }
198    
199  =head3 Get  =head3 CoreGenomes
   
 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]); >>  
   
 It is also permissible to specify I<only> an ORDER BY clause. For example, the following invocation gets  
 all genomes ordered by genus and species.  
   
 C<< $query = $sprout->Get(['Genome'], "ORDER BY Genome(genus), Genome(species)"); >>  
200    
201  Odd things may happen if one of the ORDER BY fields is in a secondary relation. So, for example, an      my @genomes = $sprout->CoreGenomes($scope);
 attempt to order B<Feature>s by alias may (depending on the underlying database engine used) cause  
 a single feature to appear more than once.  
202    
203  If multiple names are specified, then the query processor will automatically determine a  Return the IDs of NMPDR genomes in the specified scope.
 join path between the entities and relationships. The algorithm used is very simplistic.  
 In particular, you can't specify any entity or relationship more than once, and if a  
 relationship is recursive, the path is determined by the order in which the entity  
 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.  
204    
205  =over 4  =over 4
206    
207  =item objectNames  =item scope
   
 List containing the names of the entity and relationship objects to be retrieved.  
208    
209  =item filterClause  Scope of the desired genomes. C<core> covers the original core genomes,
210    C<nmpdr> covers all genomes in NMPDR groups, and C<all> covers all
211  WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can  genomes in the system.
 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.  
212    
213  =item RETURN  =item RETURN
214    
215  Returns a B<DBQuery> that can be used to iterate through all of the results.  Returns a list of the IDs for the genomes in the specified scope.
216    
217  =back  =back
218    
219  =cut  =cut
220    
221  sub Get {  sub CoreGenomes {
222      # Get the parameters.      # Get the parameters.
223      my ($self, $objectNames, $filterClause, $parameterList) = @_;      my ($self, $scope) = @_;
224      # We differ from the ERDB Get method in that the parameter list is passed in as a list reference      # Declare the return variable.
225      # rather than a list of parameters. The next step is to convert the parameters from a reference      my @retVal = ();
226      # to a real list. We can only do this if the parameters have been specified.      # If we want all genomes, then this is easy.
227      my @parameters;      if ($scope eq 'all') {
228      if ($parameterList) { @parameters = @{$parameterList}; }          @retVal = $self->Genomes();
229      return $self->{_erdb}->Get($objectNames, $filterClause, @parameters);      } else {
230            # Here we're dealing with groups. Get the hash of all the
231            # genome groups.
232            my %groups = $self->GetGroups();
233            # Loop through the groups, keeping the ones that we want.
234            for my $group (keys %groups) {
235                # Decide if we want to keep this group.
236                my $keepGroup = 0;
237                if ($scope eq 'nmpdr') {
238                    # NMPDR mode: keep all groups.
239                    $keepGroup = 1;
240                } elsif ($scope eq 'core') {
241                    # CORE mode. Only keep real core groups.
242                    if (grep { $group =~ /$_/ } @{$FIG_Config::realCoreGroups}) {
243                        $keepGroup = 1;
244                    }
245                }
246                # Add this group if we're keeping it.
247                if ($keepGroup) {
248                    push @retVal, @{$groups{$group}};
249                }
250            }
251        }
252        # Return the result.
253        return @retVal;
254  }  }
255    
256  =head3 GetEntity  =head3 SuperGroup
257    
258  C<< my $entityObject = $sprout->GetEntity($entityType, $ID); >>      my $superGroup = $sprout->SuperGroup($groupName);
259    
260  Return an object describing the entity instance with a specified ID.  Return the name of the super-group containing the specified NMPDR genome
261    group. If no appropriate super-group can be found, an error will be
262    thrown.
263    
264  =over 4  =over 4
265    
266  =item entityType  =item groupName
   
 Entity type name.  
267    
268  =item ID  Name of the group whose super-group is desired.
   
 ID of the desired entity.  
269    
270  =item RETURN  =item RETURN
271    
272  Returns a B<DBObject> representing the desired entity instance, or an undefined value if no  Returns the name of the super-group containing the incoming group.
 instance is found with the specified key.  
273    
274  =back  =back
275    
276  =cut  =cut
277    
278  sub GetEntity {  sub SuperGroup {
279      # Get the parameters.      # Get the parameters.
280      my ($self, $entityType, $ID) = @_;      my ($self, $groupName) = @_;
281      # Call the ERDB method.      # Declare the return variable.
282      return $self->{_erdb}->GetEntity($entityType, $ID);      my $retVal;
283        # Get the group hash.
284        my %groupHash = $self->CheckGroupFile();
285        # Find the super-group genus.
286        $groupName =~ /([A-Z]\w+)/;
287        my $nameThing = $1;
288        # See if it's directly in the group hash.
289        if (exists $groupHash{$nameThing}) {
290            # Yes, then it's our result.
291            $retVal = $nameThing;
292        } else {
293            # No, so we have to search.
294            for my $superGroup (keys %groupHash) {
295                # Get this super-group's item list.
296                my $list = $groupHash{$superGroup}->{contents};
297                # Search it.
298                if (grep { $_->[0] eq $nameThing } @{$list}) {
299                    $retVal = $superGroup;
300                }
301            }
302        }
303        # Return the result.
304        return $retVal;
305  }  }
306    
307  =head3 GetEntityValues  =head3 MaxSegment
   
 C<< my @values = GetEntityValues($entityType, $ID, \@fields); >>  
   
 Return a list of values from a specified entity instance.  
   
 =over 4  
   
 =item entityType  
   
 Entity type name.  
   
 =item ID  
   
 ID of the desired entity.  
   
 =item fields  
   
 List of field names, each of the form I<objectName>C<(>I<fieldName>C<)>.  
   
 =item RETURN  
308    
309  Returns a flattened list of the values of the specified fields for the specified entity.      my $length = $sprout->MaxSegment();
310    
311  =back  This method returns the maximum permissible length of a feature segment. The length is important
312    because it enables us to make reasonable guesses at how to find features inside a particular
313    contig region. For example, if the maximum length is 4000 and we're looking for a feature that
314    overlaps the region from 6000 to 7000 we know that the starting position must be between 2001
315    and 10999.
316    
317  =cut  =cut
318  #: Return Type @;  #: Return Type $;
319  sub GetEntityValues {  sub MaxSegment {
320      # Get the parameters.      my ($self) = @_;
321      my ($self, $entityType, $ID, $fields) = @_;      return $self->{_options}->{maxSegmentLength};
     # Call the ERDB method.  
     return $self->{_erdb}->GetEntityValues($entityType, $ID, $fields);  
322  }  }
323    
324  =head3 ShowMetaData  =head3 MaxSequence
   
 C<< $sprout->ShowMetaData($fileName); >>  
   
 This method outputs a description of the database to an HTML file in the data directory.  
   
 =over 4  
   
 =item fileName  
325    
326  Fully-qualified name to give to the output file.      my $length = $sprout->MaxSequence();
327    
328  =back  This method returns the maximum permissible length of a contig sequence. A contig is broken
329    into sequences in order to save memory resources. In particular, when manipulating features,
330    we generally only need a few sequences in memory rather than the entire contig.
331    
332  =cut  =cut
333    #: Return Type $;
334  sub ShowMetaData {  sub MaxSequence {
335      # Get the parameters.      my ($self) = @_;
336      my ($self, $fileName) = @_;      return $self->{_options}->{maxSequenceLength};
     # Compute the file name.  
     my $options = $self->{_options};  
     # Call the show method on the underlying ERDB object.  
     $self->{_erdb}->ShowMetaData($fileName);  
337  }  }
338    
339  =head3 Load  =head3 Load
340    
341  C<< $sprout->Load($rebuild); >>;      $sprout->Load($rebuild);;
342    
343  Load the database from files in the data directory, optionally re-creating the tables.  Load the database from files in the data directory, optionally re-creating the tables.
344    
# Line 359  Line 350 
350  The files are loaded based on the presumption that each line of the file is a record in the  The files are loaded based on the presumption that each line of the file is a record in the
351  relation, and the individual fields are delimited by tabs. Tab and new-line characters inside  relation, and the individual fields are delimited by tabs. Tab and new-line characters inside
352  fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must  fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must
353  be presented in the order given in the relation tables produced by the L</ShowMetaData> method.  be presented in the order given in the relation tables produced by the database documentation.
354    
355  =over 4  =over 4
356    
# Line 379  Line 370 
370  sub Load {  sub Load {
371      # Get the parameters.      # Get the parameters.
372      my ($self, $rebuild) = @_;      my ($self, $rebuild) = @_;
     # Get the database object.  
     my $erdb = $self->{_erdb};  
373      # Load the tables from the data directory.      # Load the tables from the data directory.
374      my $retVal = $erdb->LoadTables($self->{_options}->{dataDir}, $rebuild);      my $retVal = $self->LoadTables($self->{_options}->{dataDir}, $rebuild);
375      # Return the statistics.      # Return the statistics.
376      return $retVal;      return $retVal;
377  }  }
378    
379  =head3 LoadUpdate  =head3 LoadUpdate
380    
381  C<< my %stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>      my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList);
382    
383  Load updates to one or more database tables. This method enables the client to make changes to one  Load updates to one or more database tables. This method enables the client to make changes to one
384  or two tables without reloading the whole database. For each table, there must be a corresponding  or two tables without reloading the whole database. For each table, there must be a corresponding
# Line 422  Line 411 
411  sub LoadUpdate {  sub LoadUpdate {
412      # Get the parameters.      # Get the parameters.
413      my ($self, $truncateFlag, $tableList) = @_;      my ($self, $truncateFlag, $tableList) = @_;
     # Get the database object.  
     my $erdb = $self->{_erdb};  
414      # Declare the return value.      # Declare the return value.
415      my $retVal = Stats->new();      my $retVal = Stats->new();
416      # Get the data directory.      # Get the data directory.
# Line 437  Line 424 
424              Trace("No load file found for $tableName in $dataDir.") if T(0);              Trace("No load file found for $tableName in $dataDir.") if T(0);
425          } else {          } else {
426              # Attempt to load this table.              # Attempt to load this table.
427              my $result = $erdb->LoadTable($fileName, $tableName, $truncateFlag);              my $result = $self->LoadTable($fileName, $tableName, truncate => $truncateFlag);
428              # Accumulate the resulting statistics.              # Accumulate the resulting statistics.
429              $retVal->Accumulate($result);              $retVal->Accumulate($result);
430          }          }
# Line 446  Line 433 
433      return $retVal;      return $retVal;
434  }  }
435    
436    =head3 GenomeCounts
437    
438        my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete);
439    
440    Count the number of genomes in each domain. If I<$complete> is TRUE, only complete
441    genomes will be included in the counts.
442    
443    =over 4
444    
445    =item complete
446    
447    TRUE if only complete genomes are to be counted, FALSE if all genomes are to be
448    counted
449    
450    =item RETURN
451    
452    A six-element list containing the number of genomes in each of six categories--
453    Archaea, Bacteria, Eukaryota, Viral, Environmental, and Unknown, respectively.
454    
455    =back
456    
457    =cut
458    
459    sub GenomeCounts {
460        # Get the parameters.
461        my ($self, $complete) = @_;
462        # Set the filter based on the completeness flag.
463        my $filter = ($complete ? "Genome(complete) = 1" : "");
464        # Get all the genomes and the related taxonomy information.
465        my @genomes = $self->GetAll(['Genome'], $filter, [], ['Genome(id)', 'Genome(taxonomy)']);
466        # Clear the counters.
467        my ($arch, $bact, $euk, $vir, $env, $unk) = (0, 0, 0, 0, 0, 0);
468        # Loop through, counting the domains.
469        for my $genome (@genomes) {
470            if    ($genome->[1] =~ /^archaea/i)  { ++$arch }
471            elsif ($genome->[1] =~ /^bacter/i)   { ++$bact }
472            elsif ($genome->[1] =~ /^eukar/i)    { ++$euk }
473            elsif ($genome->[1] =~ /^vir/i)      { ++$vir }
474            elsif ($genome->[1] =~ /^env/i)      { ++$env }
475            else  { ++$unk }
476        }
477        # Return the counts.
478        return ($arch, $bact, $euk, $vir, $env, $unk);
479    }
480    
481    =head3 ContigCount
482    
483        my $count = $sprout->ContigCount($genomeID);
484    
485    Return the number of contigs for the specified genome ID.
486    
487    =over 4
488    
489    =item genomeID
490    
491    ID of the genome whose contig count is desired.
492    
493    =item RETURN
494    
495    Returns the number of contigs for the specified genome.
496    
497    =back
498    
499    =cut
500    
501    sub ContigCount {
502        # Get the parameters.
503        my ($self, $genomeID) = @_;
504        # Get the contig count.
505        my $retVal = $self->GetCount(['Contig', 'HasContig'], "HasContig(from-link) = ?", [$genomeID]);
506        # Return the result.
507        return $retVal;
508    }
509    
510    =head3 GenomeMenu
511    
512        my $html = $sprout->GenomeMenu(%options);
513    
514    Generate a genome selection control with the specified name and options.
515    This control is almost but not quite the same as the genome control in the
516    B<SearchHelper> class. Eventually, the two will be combined.
517    
518    =over 4
519    
520    =item options
521    
522    Optional parameters for the control (see below).
523    
524    =item RETURN
525    
526    Returns the HTML for a genome selection control on a form (sometimes called a popup menu).
527    
528    =back
529    
530    The valid options are as follows.
531    
532    =over 4
533    
534    =item name
535    
536    Name to give this control for use in passing it to the form. The default is C<myGenomeControl>.
537    Terrible things will happen if you have two controls with the same name on the same page.
538    
539    =item filter
540    
541    If specified, a filter for the list of genomes to display. The filter should be in the form of a
542    list reference, a string, or a hash reference. If it is a list reference, the first element
543    of the list should be the filter string, and the remaining elements the filter parameters. If it is a
544    string, it will be split into a list at each included tab. If it is a hash reference, it should be
545    a hash that maps genomes which should be included to a TRUE value.
546    
547    =item multiSelect
548    
549    If TRUE, then the user can select multiple genomes. If FALSE, the user can only select one genome.
550    
551    =item size
552    
553    Number of rows to display in the control. The default is C<10>
554    
555    =item id
556    
557    ID to give this control. The default is the value of the C<name> option. Nothing will work correctly
558    unless this ID is unique.
559    
560    =item selected
561    
562    A comma-delimited list of selected genomes, or a reference to a list of selected genomes. The
563    default is none.
564    
565    =item class
566    
567    If specified, a style class to assign to the genome control.
568    
569    =back
570    
571    =cut
572    
573    sub GenomeMenu {
574        # Get the parameters.
575        my ($self, %options) = @_;
576        # Get the control's name and ID.
577        my $menuName = $options{name} || $options{id} || 'myGenomeControl';
578        my $menuID = $options{id} || $menuName;
579        Trace("Genome menu name = $menuName with ID $menuID.") if T(3);
580        # Compute the IDs for the status display.
581        my $divID = "${menuID}_status";
582        my $urlID = "${menuID}_url";
583        # Compute the code to show selected genomes in the status area.
584        my $showSelect = "showSelected('$menuID', '$divID', '$urlID', $FIG_Config::genome_control_cap)";
585        # Check for single-select or multi-select.
586        my $multiSelect = $options{multiSelect} || 0;
587        # Get the style data.
588        my $class = $options{class} || '';
589        # Get the list of pre-selected items.
590        my $selections = $options{selected} || [];
591        if (ref $selections ne 'ARRAY') {
592            $selections = [ split /\s*,\s*/, $selections ];
593        }
594        my %selected = map { $_ => 1 } @{$selections};
595        # Extract the filter information. The default is no filtering. It can be passed as a tab-delimited
596        # string, a hash reference, or a list reference.
597        my ($filterHash, $filterString);
598        my $filterParms = $options{filter} || "";
599        if (ref $filterParms eq 'HASH') {
600            $filterHash = $filterParms;
601            $filterParms = [];
602            $filterString = "";
603        } else {
604            if (! ref $filterParms) {
605                $filterParms = [split /\t|\\t/, $filterParms];
606            }
607            $filterString = shift @{$filterParms};
608        }
609        # Check for possible subsystem filtering. If there is one, we will tack the
610        # relationship onto the object name list.
611        my @objectNames = qw(Genome);
612        if ($filterString =~ /ParticipatesIn\(/) {
613            push @objectNames, 'ParticipatesIn';
614        }
615        # Get a list of all the genomes in group order. In fact, we only need them ordered
616        # by name (genus,species,strain), but putting primary-group in front enables us to
617        # take advantage of an existing index.
618        my @genomeList = $self->GetAll(\@objectNames, "$filterString ORDER BY Genome(primary-group), Genome(genus), Genome(species), Genome(unique-characterization)",
619                                       $filterParms,
620                                       [qw(Genome(primary-group) Genome(id) Genome(genus) Genome(species) Genome(unique-characterization) Genome(taxonomy) Genome(contigs))]);
621        # Apply the hash filter (if any).
622        if (defined $filterHash) {
623            @genomeList = grep { $filterHash->{$_->[1]} } @genomeList;
624        }
625        # Create a hash to organize the genomes by group. Each group will contain a list of
626        # 2-tuples, the first element being the genome ID and the second being the genome
627        # name.
628        my %gHash = ();
629        for my $genome (@genomeList) {
630            # Get the genome data.
631            my ($group, $genomeID, $genus, $species, $strain, $taxonomy, $contigs) = @{$genome};
632            # Compute its name. This is the genus, species, strain (if any), and the contig count.
633            my $name = "$genus $species ";
634            $name .= "$strain " if $strain;
635            my $contigCount = ($contigs == 1 ? "" : ", $contigs contigs");
636            # Now we get the domain. The domain tells us the display style of the organism.
637            my ($domain) = split /\s*;\s*/, $taxonomy, 2;
638            # Now compute the display group. This is normally the primary group, but if the
639            # organism is supporting, we blank it out.
640            my $displayGroup = ($group eq $FIG_Config::otherGroup ? "" : $group);
641            # Push the genome into the group's list. Note that we use the real group
642            # name for the hash key here, not the display group name.
643            push @{$gHash{$group}}, [$genomeID, $name, $contigCount, $domain];
644        }
645        # We are almost ready to unroll the menu out of the group hash. The final step is to separate
646        # the supporting genomes by domain. First, we extract the NMPDR groups and sort them. They
647        # are sorted by the first capitalized word. Groups with "other" are sorted after groups
648        # that aren't "other". At some point, we will want to make this less complicated.
649        my %sortGroups = map { $_ =~ /(other)?(.*)([A-Z].+)/; "$3$1$2" => $_ }
650                             grep { $_ ne $FIG_Config::otherGroup } keys %gHash;
651        my @groups = map { $sortGroups{$_} } sort keys %sortGroups;
652        # Remember the number of NMPDR groups.
653        my $nmpdrGroupCount = scalar @groups;
654        # Are there any supporting genomes?
655        if (exists $gHash{$FIG_Config::otherGroup}) {
656            # Loop through the supporting genomes, classifying them by domain. We'll also keep a list
657            # of the domains found.
658            my @otherGenomes = @{$gHash{$FIG_Config::otherGroup}};
659            my @domains = ();
660            for my $genomeData (@otherGenomes) {
661                my ($genomeID, $name, $contigCount, $domain) = @{$genomeData};
662                if (exists $gHash{$domain}) {
663                    push @{$gHash{$domain}}, $genomeData;
664                } else {
665                    $gHash{$domain} = [$genomeData];
666                    push @domains, $domain;
667                }
668            }
669            # Add the domain groups at the end of the main group list. The main group list will now
670            # contain all the categories we need to display the genomes.
671            push @groups, sort @domains;
672            # Delete the supporting group.
673            delete $gHash{$FIG_Config::otherGroup};
674        }
675        # Now it gets complicated. We need a way to mark all the NMPDR genomes. We take advantage
676        # of the fact they come first in the list. We'll accumulate a count of the NMPDR genomes
677        # and use that to make the selections.
678        my $nmpdrCount = 0;
679        # Create the type counters.
680        my $groupCount = 1;
681        # Get the number of rows to display.
682        my $rows = $options{size} || 10;
683        # If we're multi-row, create an onChange event.
684        my $onChangeTag = ( $rows > 1 ? " onChange=\"$showSelect;\" onFocus=\"$showSelect;\"" : "" );
685        # Set up the multiple-select flag.
686        my $multipleTag = ($multiSelect ? " multiple" : "" );
687        # Set up the style class.
688        my $classTag = ($class ? " $class" : "" );
689        # Create the SELECT tag and stuff it into the output array.
690        my @lines = qq(<SELECT name="$menuName" id="$menuID" class="genomeSelect $class" $onChangeTag$multipleTag$classTag size="$rows">);
691        # Loop through the groups.
692        for my $group (@groups) {
693            # Get the genomes in the group.
694            for my $genome (@{$gHash{$group}}) {
695                # If this is an NMPDR organism, we add an extra style and count it.
696                my $nmpdrStyle = "";
697                if ($nmpdrGroupCount > 0) {
698                    $nmpdrCount++;
699                    $nmpdrStyle = " Core";
700                }
701                # Get the organism ID, name, contig count, and domain.
702                my ($genomeID, $name, $contigCount, $domain) = @{$genome};
703                # See if we're pre-selected.
704                my $selectTag = ($selected{$genomeID} ? " SELECTED" : "");
705                # Compute the display name.
706                my $nameString = "$name ($genomeID$contigCount)";
707                # Generate the option tag.
708                my $optionTag = "<OPTION class=\"$domain$nmpdrStyle\" title=\"$group\" value=\"$genomeID\"$selectTag>$nameString</OPTION>";
709                push @lines, "    $optionTag";
710            }
711            # Record this group in the nmpdrGroup count. When that gets to 0, we've finished the NMPDR
712            # groups.
713            $nmpdrGroupCount--;
714        }
715        # Close the SELECT tag.
716        push @lines, "</SELECT>";
717        if ($rows > 1) {
718            # We're in a non-compact mode, so we need to add some selection helpers. First is
719            # the search box. This allows the user to type text and change which genomes are
720            # displayed. For multiple-select mode, we include a button that selects the displayed
721            # genes. For single-select mode, we use a plain label instead.
722            my $searchThingName = "${menuID}_SearchThing";
723            my $searchThingLabel = "Type to narrow selection";
724            my $searchThingButton = "";
725            if ($multiSelect) {
726                $searchThingButton = qq(<INPUT type="button" name="MacroSearch" class="button" value="Go" onClick="selectShowing('$menuID', '$searchThingName'); $showSelect;" />);
727            }
728            push @lines, "<br />$searchThingLabel&nbsp;" .
729                         qq(<INPUT type="text" id="$searchThingName" name="$searchThingName" class="genomeSearchThing" onKeyup="showTyped('$menuID', '$searchThingName');" />) .
730                         $searchThingButton .
731                         Hint("GenomeControl", 28) . "<br />";
732            # For multi-select mode, we also have buttons to set and clear selections.
733            if ($multiSelect) {
734                push @lines, qq(<INPUT type="button" name="ClearAll" class="bigButton genomeButton" value="Clear All" onClick="clearAll(getElementById('$menuID')); $showSelect" />);
735                push @lines, qq(<INPUT type="button" name="SelectAll" class="bigButton genomeButton" value="Select All" onClick="selectAll(getElementById('$menuID')); $showSelect" />);
736                push @lines, qq(<INPUT type="button" name="NMPDROnly" class="bigButton genomeButton" value="Select NMPDR" onClick="selectSome(getElementById('$menuID'), $nmpdrCount, true); $showSelect;" />);
737            }
738            # Add a hidden field we can use to generate organism page hyperlinks.
739            push @lines, qq(<INPUT type="hidden" id="$urlID" value="$FIG_Config::cgi_url/wiki/rest.cgi/NmpdrPlugin/SeedViewer?page=Organism;organism=" />);
740            # Add the status display. This tells the user what's selected no matter where the list is scrolled.
741            push @lines, qq(<DIV id="$divID" class="Panel"></DIV>);
742        }
743        # Assemble all the lines into a string.
744        my $retVal = join("\n", @lines, "");
745        # Return the result.
746        return $retVal;
747    }
748    
749    
750    =head3 Stem
751    
752        my $stem = $sprout->Stem($word);
753    
754    Return the stem of the specified word, or C<undef> if the word is not
755    stemmable. Note that even if the word is stemmable, the stem may be
756    the same as the original word.
757    
758    =over 4
759    
760    =item word
761    
762    Word to convert into a stem.
763    
764    =item RETURN
765    
766    Returns a stem of the word (which may be the word itself), or C<undef> if
767    the word is not stemmable.
768    
769    =back
770    
771    =cut
772    
773    sub Stem {
774        # Get the parameters.
775        my ($self, $word) = @_;
776        # Get the stemmer object.
777        my $stemmer = $self->{stemmer};
778        if (! defined $stemmer) {
779            # We don't have one pre-built, so we build and save it now.
780            $stemmer = BioWords->new(exceptions => "$FIG_Config::sproutData/Exceptions.txt",
781                                     stops => "$FIG_Config::sproutData/StopWords.txt",
782                                     cache => 0);
783            $self->{stemmer} = $stemmer;
784        }
785        # Try to stem the word.
786        my $retVal = $stemmer->Process($word);
787        # Return the result.
788        return $retVal;
789    }
790    
791    
792  =head3 Build  =head3 Build
793    
794  C<< $sprout->Build(); >>      $sprout->Build();
795    
796  Build the database. The database will be cleared and the tables re-created from the metadata.  Build the database. The database will be cleared and the tables re-created from the metadata.
797  This method is useful when a database is brand new or when the database definition has  This method is useful when a database is brand new or when the database definition has
# Line 460  Line 803 
803      # Get the parameters.      # Get the parameters.
804      my ($self) = @_;      my ($self) = @_;
805      # Create the tables.      # Create the tables.
806      $self->{_erdb}->CreateTables;      $self->CreateTables();
807  }  }
808    
809  =head3 Genomes  =head3 Genomes
810    
811  C<< my @genomes = $sprout->Genomes(); >>      my @genomes = $sprout->Genomes();
812    
813  Return a list of all the genome IDs.  Return a list of all the genome IDs.
814    
# Line 482  Line 825 
825    
826  =head3 GenusSpecies  =head3 GenusSpecies
827    
828  C<< my $infoString = $sprout->GenusSpecies($genomeID); >>      my $infoString = $sprout->GenusSpecies($genomeID);
829    
830  Return the genus, species, and unique characterization for a genome.  Return the genus, species, and unique characterization for a genome.
831    
# Line 504  Line 847 
847  sub GenusSpecies {  sub GenusSpecies {
848      # Get the parameters.      # Get the parameters.
849      my ($self, $genomeID) = @_;      my ($self, $genomeID) = @_;
850      # Get the data for the specified genome.      # Declare the return value.
851      my @values = $self->GetEntityValues('Genome', $genomeID, ['Genome(genus)', 'Genome(species)',      my $retVal;
852                                                                'Genome(unique-characterization)']);      # Get the genome data.
853      # Format the result and return it.      my $genomeData = $self->_GenomeData($genomeID);
854      my $retVal = join(' ', @values);      # Only proceed if we found the genome.
855        if (defined $genomeData) {
856            $retVal = $genomeData->PrimaryValue('Genome(scientific-name)');
857        }
858        # Return it.
859      return $retVal;      return $retVal;
860  }  }
861    
862  =head3 FeaturesOf  =head3 FeaturesOf
863    
864  C<< my @features = $sprout->FeaturesOf($genomeID, $ftype); >>      my @features = $sprout->FeaturesOf($genomeID, $ftype);
865    
866  Return a list of the features relevant to a specified genome.  Return a list of the features relevant to a specified genome.
867    
# Line 559  Line 906 
906    
907  =head3 FeatureLocation  =head3 FeatureLocation
908    
909  C<< my @locations = $sprout->FeatureLocation($featureID); >>      my @locations = $sprout->FeatureLocation($featureID);
910    
911  Return the location of a feature in its genome's contig segments. In a list context, this method  Return the location of a feature in its genome's contig segments. In a list context, this method
912  will return a list of the locations. In a scalar context, it will return the locations as a space-  will return a list of the locations. In a scalar context, it will return the locations as a space-
# Line 583  Line 930 
930  =item RETURN  =item RETURN
931    
932  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
933  context and as a comma-delimited string in a scalar context.  context and as a comma-delimited string in a scalar context. An empty list means the feature
934    wasn't found.
935    
936  =back  =back
937    
938  =cut  =cut
939  #: Return Type @;  
 #: Return Type $;  
940  sub FeatureLocation {  sub FeatureLocation {
941      # Get the parameters.      # Get the parameters.
942      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
943      # Create a query for the feature locations.      # Declare the return variable.
     my $query = $self->Get(['IsLocatedIn'], "IsLocatedIn(from-link) = ? ORDER BY IsLocatedIn(locN)",  
                            [$featureID]);  
     # Create the return list.  
944      my @retVal = ();      my @retVal = ();
945      # Set up the variables used to determine if we have adjacent segments. This initial setup will      # Get the feature record.
946      # not match anything.      my $object = $self->GetEntity('Feature', $featureID);
947      my ($prevContig, $prevBeg, $prevDir, $prevLen) = ("", 0, "0", 0);      # Only proceed if we found it.
948      # Loop through the query results, creating location specifiers.      if (defined $object) {
949      while (my $location = $query->Fetch()) {          # Get the location string.
950          # Get the location parameters.          my $locString = $object->PrimaryValue('Feature(location-string)');
951          my ($contigID, $beg, $dir, $len) = $location->Values(['IsLocatedIn(to-link)',          # Create the return list.
952              'IsLocatedIn(beg)', 'IsLocatedIn(dir)', 'IsLocatedIn(len)']);          @retVal = split /\s*,\s*/, $locString;
         # Check to see if we are adjacent to the previous segment.  
         if ($prevContig eq $contigID && $dir eq $prevDir) {  
             # Here the new segment is in the same direction on the same contig. Insure the  
             # new segment's beginning is next to the old segment's end.  
             if (($dir eq "-" && $beg == $prevBeg - $prevLen) ||  
                 ($dir eq "+" && $beg == $prevBeg + $prevLen)) {  
                 # Here we need to merge two segments. Adjust the beginning and length values  
                 # to include both segments.  
                 $beg = $prevBeg;  
                 $len += $prevLen;  
                 # Pop the old segment off. The new one will replace it later.  
                 pop @retVal;  
             }  
         }  
         # Remember this specifier for the adjacent-segment test the next time through.  
         ($prevContig, $prevBeg, $prevDir, $prevLen) = ($contigID, $beg, $dir, $len);  
         # Compute the initial base pair.  
         my $start = ($dir eq "+" ? $beg : $beg + $len - 1);  
         # Add the specifier to the list.  
         push @retVal, "${contigID}_$beg$dir$len";  
953      }      }
954      # Return the list in the format indicated by the context.      # Return the list in the format indicated by the context.
955      return (wantarray ? @retVal : join(',', @retVal));      return (wantarray ? @retVal : join(',', @retVal));
# Line 633  Line 957 
957    
958  =head3 ParseLocation  =head3 ParseLocation
959    
960  C<< my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location); >>      my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location);
961    
962  Split a location specifier into the contig ID, the starting point, the direction, and the  Split a location specifier into the contig ID, the starting point, the direction, and the
963  length.  length.
# Line 652  Line 976 
976  =back  =back
977    
978  =cut  =cut
979  #: Return Type @;  
980  sub ParseLocation {  sub ParseLocation {
981      # Get the parameter. Note that if we're called as an instance method, we ignore      # Get the parameter. Note that if we're called as an instance method, we ignore
982      # the first parameter.      # the first parameter.
983      shift if UNIVERSAL::isa($_[0],__PACKAGE__);      shift if UNIVERSAL::isa($_[0],__PACKAGE__);
984      my ($location) = @_;      my ($location) = @_;
985      # Parse it into segments.      # Parse it into segments.
986      $location =~ /^(.*)_(\d*)([+-_])(\d*)$/;      $location =~ /^(.+)_(\d+)([+\-_])(\d+)$/;
987      my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);      my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);
988      # If the direction is an underscore, convert it to a + or -.      # If the direction is an underscore, convert it to a + or -.
989      if ($dir eq "_") {      if ($dir eq "_") {
# Line 675  Line 999 
999      return ($contigID, $start, $dir, $len);      return ($contigID, $start, $dir, $len);
1000  }  }
1001    
1002    
1003    
1004  =head3 PointLocation  =head3 PointLocation
1005    
1006  C<< my $found = Sprout::PointLocation($location, $point); >>      my $found = Sprout::PointLocation($location, $point);
1007    
1008  Return the offset into the specified location of the specified point on the contig. If  Return the offset into the specified location of the specified point on the contig. If
1009  the specified point is before the location, a negative value will be returned. If it is  the specified point is before the location, a negative value will be returned. If it is
# Line 706  Line 1032 
1032  =back  =back
1033    
1034  =cut  =cut
1035  #: Return Type $;  
1036  sub PointLocation {  sub PointLocation {
1037      # Get the parameter. Note that if we're called as an instance method, we ignore      # Get the parameter. Note that if we're called as an instance method, we ignore
1038      # the first parameter.      # the first parameter.
# Line 729  Line 1055 
1055    
1056  =head3 DNASeq  =head3 DNASeq
1057    
1058  C<< my $sequence = $sprout->DNASeq(\@locationList); >>      my $sequence = $sprout->DNASeq(\@locationList);
1059    
1060  This method returns the DNA sequence represented by a list of locations. The list of locations  This method returns the DNA sequence represented by a list of locations. The list of locations
1061  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,
1062  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>.
1063    
1064    For example, the following would return the DNA sequence for contig C<83333.1:NC_000913>
1065    between positions 1401 and 1532, inclusive.
1066    
1067        my $sequence = $sprout->DNASeq('83333.1:NC_000913_1401_1532');
1068    
1069  =over 4  =over 4
1070    
1071  =item locationList  =item locationList
1072    
1073  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
1074  L</FeatureLocation> for more about this format).  I<contigID>C<_>I<begin>C<_>I<end> (see L</FeatureLocation> for more about this format).
1075    
1076  =item RETURN  =item RETURN
1077    
# Line 767  Line 1098 
1098          # 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
1099          # before putting it in the return value.          # before putting it in the return value.
1100          my ($start, $stop);          my ($start, $stop);
1101          Trace("Parsed location is $beg$dir$len.") if T(SDNA => 4);          Trace("Parse of \"$location\" is $beg$dir$len.") if T(SDNA => 4);
1102          if ($dir eq "+") {          if ($dir eq "+") {
1103              $start = $beg;              $start = $beg;
1104              $stop = $beg + $len - 1;              $stop = $beg + $len - 1;
# Line 790  Line 1121 
1121              Trace("Sequence is from $startPosition to $stopPosition.") if T(SDNA => 4);              Trace("Sequence is from $startPosition to $stopPosition.") if T(SDNA => 4);
1122              # Figure out the start point and length of the relevant section.              # Figure out the start point and length of the relevant section.
1123              my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);              my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);
1124              my $len1 = ($stopPosition <= $stop ? $stopPosition : $stop) - $startPosition - $pos1;              my $len1 = ($stopPosition < $stop ? $stopPosition : $stop) + 1 - $startPosition - $pos1;
1125              Trace("Position is $pos1 for length $len1.") if T(SDNA => 4);              Trace("Position is $pos1 for length $len1.") if T(SDNA => 4);
1126              # Add the relevant data to the location data.              # Add the relevant data to the location data.
1127              $locationDNA .= substr($sequenceData, $pos1, $len1);              $locationDNA .= substr($sequenceData, $pos1, $len1);
# Line 808  Line 1139 
1139    
1140  =head3 AllContigs  =head3 AllContigs
1141    
1142  C<< my @idList = $sprout->AllContigs($genomeID); >>      my @idList = $sprout->AllContigs($genomeID);
1143    
1144  Return a list of all the contigs for a genome.  Return a list of all the contigs for a genome.
1145    
# Line 836  Line 1167 
1167      return @retVal;      return @retVal;
1168  }  }
1169    
1170  =head3 ContigLength  =head3 GenomeLength
1171    
1172  C<< my $length = $sprout->ContigLength($contigID); >>      my $length = $sprout->GenomeLength($genomeID);
1173    
1174  Compute the length of a contig.  Return the length of the specified genome in base pairs.
1175    
1176  =over 4  =over 4
1177    
1178  =item contigID  =item genomeID
1179    
1180  ID of the contig whose length is desired.  ID of the genome whose base pair count is desired.
1181    
1182  =item RETURN  =item RETURN
1183    
1184  Returns the number of positions in the contig.  Returns the number of base pairs in all the contigs of the specified
1185    genome.
1186    
1187  =back  =back
1188    
1189  =cut  =cut
1190  #: Return Type $;  
1191  sub ContigLength {  sub GenomeLength {
1192      # Get the parameters.      # Get the parameters.
1193      my ($self, $contigID) = @_;      my ($self, $genomeID) = @_;
1194      # Get the contig's last sequence.      # Declare the return variable.
1195      my $query = $self->Get(['IsMadeUpOf'],      my $retVal = 0;
1196          "IsMadeUpOf(from-link) = ? ORDER BY IsMadeUpOf(start-position) DESC",      # Get the genome data.
1197        my $genomeData = $self->_GenomeData($genomeID);
1198        # Only proceed if it exists.
1199        if (defined $genomeData) {
1200            $retVal = $genomeData->PrimaryValue('Genome(dna-size)');
1201        }
1202        # Return the result.
1203        return $retVal;
1204    }
1205    
1206    =head3 FeatureCount
1207    
1208        my $count = $sprout->FeatureCount($genomeID, $type);
1209    
1210    Return the number of features of the specified type in the specified genome.
1211    
1212    =over 4
1213    
1214    =item genomeID
1215    
1216    ID of the genome whose feature count is desired.
1217    
1218    =item type
1219    
1220    Type of feature to count (eg. C<peg>, C<rna>, etc.).
1221    
1222    =item RETURN
1223    
1224    Returns the number of features of the specified type for the specified genome.
1225    
1226    =back
1227    
1228    =cut
1229    
1230    sub FeatureCount {
1231        # Get the parameters.
1232        my ($self, $genomeID, $type) = @_;
1233        # Compute the count.
1234        my $retVal = $self->GetCount(['HasFeature', 'Feature'],
1235                                    "HasFeature(from-link) = ? AND Feature(feature-type) = ?",
1236                                    [$genomeID, $type]);
1237        # Return the result.
1238        return $retVal;
1239    }
1240    
1241    =head3 GenomeAssignments
1242    
1243        my $fidHash = $sprout->GenomeAssignments($genomeID);
1244    
1245    Return a list of a genome's assigned features. The return hash will contain each
1246    assigned feature of the genome mapped to the text of its most recent functional
1247    assignment.
1248    
1249    =over 4
1250    
1251    =item genomeID
1252    
1253    ID of the genome whose functional assignments are desired.
1254    
1255    =item RETURN
1256    
1257    Returns a reference to a hash which maps each feature to its most recent
1258    functional assignment.
1259    
1260    =back
1261    
1262    =cut
1263    
1264    sub GenomeAssignments {
1265        # Get the parameters.
1266        my ($self, $genomeID) = @_;
1267        # Declare the return variable.
1268        my $retVal = {};
1269        # Query the genome's features.
1270        my $query = $self->Get(['HasFeature', 'Feature'], "HasFeature(from-link) = ?",
1271                               [$genomeID]);
1272        # Loop through the features.
1273        while (my $data = $query->Fetch) {
1274            # Get the feature ID and assignment.
1275            my ($fid, $assignment) = $data->Values(['Feature(id)', 'Feature(assignment)']);
1276            if ($assignment) {
1277                $retVal->{$fid} = $assignment;
1278            }
1279        }
1280        # Return the result.
1281        return $retVal;
1282    }
1283    
1284    =head3 ContigLength
1285    
1286        my $length = $sprout->ContigLength($contigID);
1287    
1288    Compute the length of a contig.
1289    
1290    =over 4
1291    
1292    =item contigID
1293    
1294    ID of the contig whose length is desired.
1295    
1296    =item RETURN
1297    
1298    Returns the number of positions in the contig.
1299    
1300    =back
1301    
1302    =cut
1303    #: Return Type $;
1304    sub ContigLength {
1305        # Get the parameters.
1306        my ($self, $contigID) = @_;
1307        # Get the contig's last sequence.
1308        my $query = $self->Get(['IsMadeUpOf'],
1309            "IsMadeUpOf(from-link) = ? ORDER BY IsMadeUpOf(start-position) DESC",
1310          [$contigID]);          [$contigID]);
1311      my $sequence = $query->Fetch();      my $sequence = $query->Fetch();
1312      # Declare the return value.      # Declare the return value.
# Line 869  Line 1314 
1314      # Set it from the sequence data, if any.      # Set it from the sequence data, if any.
1315      if ($sequence) {      if ($sequence) {
1316          my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);          my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);
1317          $retVal = $start + $len;          $retVal = $start + $len - 1;
1318        }
1319        # Return the result.
1320        return $retVal;
1321    }
1322    
1323    =head3 ClusterPEGs
1324    
1325        my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs);
1326    
1327    Cluster the PEGs in a list according to the cluster coding scheme of the specified
1328    subsystem. In order for this to work properly, the subsystem object must have
1329    been used recently to retrieve the PEGs using the B<get_pegs_from_cell> or
1330    B<get_row> methods. This causes the cluster numbers to be pulled into the
1331    subsystem's color hash. If a PEG is not found in the color hash, it will not
1332    appear in the output sequence.
1333    
1334    =over 4
1335    
1336    =item sub
1337    
1338    Sprout subsystem object for the relevant subsystem, from the L</get_subsystem>
1339    method.
1340    
1341    =item pegs
1342    
1343    Reference to the list of PEGs to be clustered.
1344    
1345    =item RETURN
1346    
1347    Returns a list of the PEGs, grouped into smaller lists by cluster number.
1348    
1349    =back
1350    
1351    =cut
1352    #: Return Type $@@;
1353    sub ClusterPEGs {
1354        # Get the parameters.
1355        my ($self, $sub, $pegs) = @_;
1356        # Declare the return variable.
1357        my $retVal = [];
1358        # Loop through the PEGs, creating arrays for each cluster.
1359        for my $pegID (@{$pegs}) {
1360            my $clusterNumber = $sub->get_cluster_number($pegID);
1361            # Only proceed if the PEG is in a cluster.
1362            if ($clusterNumber >= 0) {
1363                # Push this PEG onto the sub-list for the specified cluster number.
1364                push @{$retVal->[$clusterNumber]}, $pegID;
1365            }
1366      }      }
1367      # Return the result.      # Return the result.
1368      return $retVal;      return $retVal;
# Line 877  Line 1370 
1370    
1371  =head3 GenesInRegion  =head3 GenesInRegion
1372    
1373  C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>      my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop);
1374    
1375  List the features which overlap a specified region in a contig.  List the features which overlap a specified region in a contig.
1376    
# Line 906  Line 1399 
1399  =back  =back
1400    
1401  =cut  =cut
1402  #: Return Type @@;  
1403  sub GenesInRegion {  sub GenesInRegion {
1404      # Get the parameters.      # Get the parameters.
1405      my ($self, $contigID, $start, $stop) = @_;      my ($self, $contigID, $start, $stop) = @_;
1406      # Get the maximum segment length.      # Get the maximum segment length.
1407      my $maximumSegmentLength = $self->MaxSegment;      my $maximumSegmentLength = $self->MaxSegment;
     # Create a hash to receive the feature list. We use a hash so that we can eliminate  
     # duplicates easily. The hash key will be the feature ID. The value will be a two-element  
     # containing the minimum and maximum offsets. We will use the offsets to sort the results  
     # when we're building the result set.  
     my %featuresFound = ();  
1408      # Prime the values we'll use for the returned beginning and end.      # Prime the values we'll use for the returned beginning and end.
1409      my @initialMinMax = ($self->ContigLength($contigID), 0);      my @initialMinMax = ($self->ContigLength($contigID), 0);
1410      my ($min, $max) = @initialMinMax;      my ($min, $max) = @initialMinMax;
1411      # Create a table of parameters for each query. Each query looks for features travelling in      # Get the overlapping features.
1412        my @featureObjects = $self->GeneDataInRegion($contigID, $start, $stop);
1413        # We'l use this hash to help us track the feature IDs and sort them. The key is the
1414        # feature ID and the value is a [$left,$right] pair indicating the maximum extent
1415        # of the feature's locations.
1416        my %featureMap = ();
1417        # Loop through them to do the begin/end analysis.
1418        for my $featureObject (@featureObjects) {
1419            # Get the feature's location string. This may contain multiple actual locations.
1420            my ($locations, $fid) = $featureObject->Values([qw(Feature(location-string) Feature(id))]);
1421            my @locationSegments = split /\s*,\s*/, $locations;
1422            # Loop through the locations.
1423            for my $locationSegment (@locationSegments) {
1424                # Construct an object for the location.
1425                my $locationObject = BasicLocation->new($locationSegment);
1426                # Merge the current segment's begin and end into the min and max.
1427                my ($left, $right) = ($locationObject->Left, $locationObject->Right);
1428                my ($beg, $end);
1429                if (exists $featureMap{$fid}) {
1430                    ($beg, $end) = @{$featureMap{$fid}};
1431                    $beg = $left if $left < $beg;
1432                    $end = $right if $right > $end;
1433                } else {
1434                    ($beg, $end) = ($left, $right);
1435                }
1436                $min = $beg if $beg < $min;
1437                $max = $end if $end > $max;
1438                # Store the feature's new extent back into the hash table.
1439                $featureMap{$fid} = [$beg, $end];
1440            }
1441        }
1442        # Now we must compute the list of the IDs for the features found. We start with a list
1443        # of midpoints / feature ID pairs. (It's not really a midpoint, it's twice the midpoint,
1444        # but the result of the sort will be the same.)
1445        my @list = map { [$featureMap{$_}->[0] + $featureMap{$_}->[1], $_] } keys %featureMap;
1446        # Now we sort by midpoint and yank out the feature IDs.
1447        my @retVal = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @list;
1448        # Return it along with the min and max.
1449        return (\@retVal, $min, $max);
1450    }
1451    
1452    =head3 GeneDataInRegion
1453    
1454        my @featureList = $sprout->GenesInRegion($contigID, $start, $stop);
1455    
1456    List the features which overlap a specified region in a contig.
1457    
1458    =over 4
1459    
1460    =item contigID
1461    
1462    ID of the contig containing the region of interest.
1463    
1464    =item start
1465    
1466    Offset of the first residue in the region of interest.
1467    
1468    =item stop
1469    
1470    Offset of the last residue in the region of interest.
1471    
1472    =item RETURN
1473    
1474    Returns a list of B<ERDBObjects> for the desired features. Each object will
1475    contain a B<Feature> record.
1476    
1477    =back
1478    
1479    =cut
1480    
1481    sub GeneDataInRegion {
1482        # Get the parameters.
1483        my ($self, $contigID, $start, $stop) = @_;
1484        # Get the maximum segment length.
1485        my $maximumSegmentLength = $self->MaxSegment;
1486        # Create a hash to receive the feature list. We use a hash so that we can eliminate
1487        # duplicates easily. The hash key will be the feature ID. The value will be the feature's
1488        # ERDBObject from the query.
1489        my %featuresFound = ();
1490        # Create a table of parameters for the queries. Each query looks for features travelling in
1491      # a particular direction. The query parameters include the contig ID, the feature direction,      # a particular direction. The query parameters include the contig ID, the feature direction,
1492      # 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
1493      # 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.
# Line 929  Line 1496 
1496      # Loop through the query parameters.      # Loop through the query parameters.
1497      for my $parms (values %queryParms) {      for my $parms (values %queryParms) {
1498          # Create the query.          # Create the query.
1499          my $query = $self->Get(['IsLocatedIn'],          my $query = $self->Get([qw(Feature IsLocatedIn)],
1500              "IsLocatedIn(to-link)= ? AND IsLocatedIn(dir) = ? AND IsLocatedIn(beg) >= ? AND IsLocatedIn(beg) <= ?",              "IsLocatedIn(to-link)= ? AND IsLocatedIn(dir) = ? AND IsLocatedIn(beg) >= ? AND IsLocatedIn(beg) <= ?",
1501              $parms);              $parms);
1502          # Loop through the feature segments found.          # Loop through the feature segments found.
1503          while (my $segment = $query->Fetch) {          while (my $segment = $query->Fetch) {
1504              # Get the data about this segment.              # Get the data about this segment.
1505              my ($featureID, $dir, $beg, $len) = $segment->Values(['IsLocatedIn(from-link)',              my ($featureID, $contig, $dir, $beg, $len) = $segment->Values([qw(IsLocatedIn(from-link)
1506                  'IsLocatedIn(dir)', 'IsLocatedIn(beg)', 'IsLocatedIn(len)']);                  IsLocatedIn(to-link) IsLocatedIn(dir) IsLocatedIn(beg) IsLocatedIn(len))]);
1507              # Determine if this feature actually overlaps the region. The query insures that              # Determine if this feature segment actually overlaps the region. The query insures that
1508              # this will be the case if the segment is the maximum length, so to fine-tune              # this will be the case if the segment is the maximum length, so to fine-tune
1509              # the results we insure that the inequality from the query holds using the actual              # the results we insure that the inequality from the query holds using the actual
1510              # length.              # length.
1511              my ($found, $end) = (0, 0);              my $loc = BasicLocation->new($contig, $beg, $dir, $len);
1512              if ($dir eq '+') {              my $found = $loc->Overlap($start, $stop);
                 $end = $beg + $len;  
                 if ($end >= $start) {  
                     # Denote we found a useful feature.  
                     $found = 1;  
                 }  
             } elsif ($dir eq '-') {  
                 # Note we switch things around so that the beginning is to the left of the  
                 # ending.  
                 ($beg, $end) = ($beg - $len, $beg);  
                 if ($beg <= $stop) {  
                     # Denote we found a useful feature.  
                     $found = 1;  
                 }  
             }  
1513              if ($found) {              if ($found) {
1514                  # Here we need to record the feature and update the minima and maxima. First,                  # Save this feature in the result list.
1515                  # get the current entry for the specified feature.                  $featuresFound{$featureID} = $segment;
                 my ($loc1, $loc2) = (exists $featuresFound{$featureID} ? @{$featuresFound{$featureID}} :  
                                      @initialMinMax);  
                 # Merge the current segment's begin and end into the feature begin and end and the  
                 # global min and max.  
                 if ($beg < $loc1) {  
                     $loc1 = $beg;  
                     $min = $beg if $beg < $min;  
                 }  
                 if ($end > $loc2) {  
                     $loc2 = $end;  
                     $max = $end if $end > $max;  
1516                  }                  }
                 # Store the entry back into the hash table.  
                 $featuresFound{$featureID} = [$loc1, $loc2];  
1517              }              }
1518          }          }
1519      }      # Return the ERDB objects for the features found.
1520      # Now we must compute the list of the IDs for the features found. We start with a list      return values %featuresFound;
     # of midpoints / feature ID pairs. (It's not really a midpoint, it's twice the midpoint,  
     # but the result of the sort will be the same.)  
     my @list = map { [$featuresFound{$_}->[0] + $featuresFound{$_}->[1], $_] } keys %featuresFound;  
     # Now we sort by midpoint and yank out the feature IDs.  
     my @retVal = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @list;  
     # Return it along with the min and max.  
     return (\@retVal, $min, $max);  
1521  }  }
1522    
1523  =head3 FType  =head3 FType
1524    
1525  C<< my $ftype = $sprout->FType($featureID); >>      my $ftype = $sprout->FType($featureID);
1526    
1527  Return the type of a feature.  Return the type of a feature.
1528    
# Line 1019  Line 1552 
1552    
1553  =head3 FeatureAnnotations  =head3 FeatureAnnotations
1554    
1555  C<< my @descriptors = $sprout->FeatureAnnotations($featureID); >>      my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag);
1556    
1557  Return the annotations of a feature.  Return the annotations of a feature.
1558    
# Line 1029  Line 1562 
1562    
1563  ID of the feature whose annotations are desired.  ID of the feature whose annotations are desired.
1564    
1565    =item rawFlag
1566    
1567    If TRUE, the annotation timestamps will be returned in raw form; otherwise, they
1568    will be returned in human-readable form.
1569    
1570  =item RETURN  =item RETURN
1571    
1572  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.
1573    
1574  * B<featureID> ID of the relevant feature.  * B<featureID> ID of the relevant feature.
1575    
1576  * B<timeStamp> time the annotation was made, in user-friendly format.  * B<timeStamp> time the annotation was made.
1577    
1578  * B<user> ID of the user who made the annotation  * B<user> ID of the user who made the annotation
1579    
# Line 1047  Line 1585 
1585  #: Return Type @%;  #: Return Type @%;
1586  sub FeatureAnnotations {  sub FeatureAnnotations {
1587      # Get the parameters.      # Get the parameters.
1588      my ($self, $featureID) = @_;      my ($self, $featureID, $rawFlag) = @_;
1589      # 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.
1590      my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],      my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1591                             "IsTargetOfAnnotation(from-link) = ?", [$featureID]);                             "IsTargetOfAnnotation(from-link) = ?", [$featureID]);
# Line 1060  Line 1598 
1598              $annotation->Values(['IsTargetOfAnnotation(from-link)',              $annotation->Values(['IsTargetOfAnnotation(from-link)',
1599                                   'Annotation(time)', 'MadeAnnotation(from-link)',                                   'Annotation(time)', 'MadeAnnotation(from-link)',
1600                                   'Annotation(annotation)']);                                   'Annotation(annotation)']);
1601            # Convert the time, if necessary.
1602            if (! $rawFlag) {
1603                $timeStamp = FriendlyTimestamp($timeStamp);
1604            }
1605          # Assemble them into a hash.          # Assemble them into a hash.
1606          my $annotationHash = { featureID => $featureID,          my $annotationHash = { featureID => $featureID,
1607                                 timeStamp => FriendlyTimestamp($timeStamp),                                 timeStamp => $timeStamp,
1608                                 user => $user, text => $text };                                 user => $user, text => $text };
1609          # Add it to the return list.          # Add it to the return list.
1610          push @retVal, $annotationHash;          push @retVal, $annotationHash;
# Line 1073  Line 1615 
1615    
1616  =head3 AllFunctionsOf  =head3 AllFunctionsOf
1617    
1618  C<< my %functions = $sprout->AllFunctionsOf($featureID); >>      my %functions = $sprout->AllFunctionsOf($featureID);
1619    
1620  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
1621  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,
# Line 1091  Line 1633 
1633    
1634  =item RETURN  =item RETURN
1635    
1636  Returns a hash mapping the functional assignment IDs to user IDs.  Returns a hash mapping the user IDs to functional assignment IDs.
1637    
1638  =back  =back
1639    
# Line 1101  Line 1643 
1643      # Get the parameters.      # Get the parameters.
1644      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
1645      # Get all of the feature's annotations.      # Get all of the feature's annotations.
1646      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation'],      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1647                              "IsTargetOfAnnotation(from-link) = ?",                              "IsTargetOfAnnotation(from-link) = ?",
1648                              [$featureID], ['Annotation(time)', 'Annotation(annotation)']);                              [$featureID], ['Annotation(time)', 'Annotation(annotation)',
1649                                               'MadeAnnotation(from-link)']);
1650      # Declare the return hash.      # Declare the return hash.
1651      my %retVal;      my %retVal;
     # Declare a hash for insuring we only make one assignment per user.  
     my %timeHash = ();  
1652      # Now we sort the assignments by timestamp in reverse.      # Now we sort the assignments by timestamp in reverse.
1653      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;
1654      # Loop until we run out of annotations.      # Loop until we run out of annotations.
1655      for my $annotation (@sortedQuery) {      for my $annotation (@sortedQuery) {
1656          # Get the annotation fields.          # Get the annotation fields.
1657          my ($timeStamp, $text) = @{$annotation};          my ($timeStamp, $text, $user) = @{$annotation};
1658          # Check to see if this is a functional assignment.          # Check to see if this is a functional assignment.
1659          my ($user, $function) = _ParseAssignment($text);          my ($actualUser, $function) = _ParseAssignment($user, $text);
1660          if ($user && ! exists $timeHash{$user}) {          if ($actualUser && ! exists $retVal{$actualUser}) {
1661              # Here it is a functional assignment and there has been no              # Here it is a functional assignment and there has been no
1662              # previous assignment for this user, so we stuff it in the              # previous assignment for this user, so we stuff it in the
1663              # return hash.              # return hash.
1664              $retVal{$function} = $user;              $retVal{$actualUser} = $function;
             # Insure we don't assign to this user again.  
             $timeHash{$user} = 1;  
1665          }          }
1666      }      }
1667      # Return the hash of assignments found.      # Return the hash of assignments found.
# Line 1131  Line 1670 
1670    
1671  =head3 FunctionOf  =head3 FunctionOf
1672    
1673  C<< my $functionText = $sprout->FunctionOf($featureID, $userID); >>      my $functionText = $sprout->FunctionOf($featureID, $userID);
1674    
1675  Return the most recently-determined functional assignment of a particular feature.  Return the most recently-determined functional assignment of a particular feature.
1676    
1677  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
1678  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
1679  assignment is a type of annotation. The format of an assignment is described in  assignment is taken from the B<Feature> or C<Annotation> table, depending.
 L</ParseLocation>. 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.  
1680    
1681  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
1682  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
1683  the specified user and FIG are considered trusted. If the user ID is omitted, only FIG  the specified user and FIG are considered trusted. If the user ID is omitted, only FIG
1684  is trusted.  is trusted.
1685    
1686  If the feature is B<not> identified by a FIG ID, then the functional assignment  If the feature is B<not> identified by a FIG ID, then we search the aliases for it.
1687  information is taken from the B<ExternalAliasFunc> table. If the table does  If no matching alias is found, we return an undefined value.
 not contain an entry for the feature, an undefined value is returned.  
1688    
1689  =over 4  =over 4
1690    
# Line 1159  Line 1694 
1694    
1695  =item userID (optional)  =item userID (optional)
1696    
1697  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
1698  C<FIG> assignment will be returned.  functional assignment in the B<Feature> table will be returned.
1699    
1700  =item RETURN  =item RETURN
1701    
# Line 1175  Line 1710 
1710      my ($self, $featureID, $userID) = @_;      my ($self, $featureID, $userID) = @_;
1711      # Declare the return value.      # Declare the return value.
1712      my $retVal;      my $retVal;
1713      # Determine the ID type.      # Find a FIG ID for this feature.
1714      if ($featureID =~ m/^fig\|/) {      my ($fid) = $self->FeaturesByAlias($featureID);
1715          # Here we have a FIG feature ID. We must build the list of trusted      # Only proceed if we have an ID.
1716          # users.      if ($fid) {
1717            # Here we have a FIG feature ID.
1718            if (!$userID) {
1719                # Use the primary assignment.
1720                ($retVal) = $self->GetEntityValues('Feature', $fid, ['Feature(assignment)']);
1721            } else {
1722                # We must build the list of trusted users.
1723          my %trusteeTable = ();          my %trusteeTable = ();
1724          # Check the user ID.          # Check the user ID.
1725          if (!$userID) {          if (!$userID) {
# Line 1200  Line 1741 
1741              }              }
1742          }          }
1743          # 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.
1744          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation'],              my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1745                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1746                                 [$featureID]);                                     [$fid]);
1747          my $timeSelected = 0;          my $timeSelected = 0;
1748          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1749          while (my $annotation = $query->Fetch()) {          while (my $annotation = $query->Fetch()) {
1750              # Get the annotation text.              # Get the annotation text.
1751              my ($text, $time) = $annotation->Values(['Annotation(annotation)','Annotation(time)']);                  my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1752                                                             'Annotation(time)', 'MadeAnnotation(from-link)']);
1753              # 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.
1754              my ($user, $function) = _ParseAssignment($text);                  my ($actualUser, $function) = _ParseAssignment($user, $text);
1755              if ($user) {                  Trace("Assignment user is $actualUser, text is $function.") if T(4);
1756                    if ($actualUser) {
1757                  # Here it is a functional assignment. Check the time and the user                  # Here it is a functional assignment. Check the time and the user
1758                  # name. The time must be recent and the user must be trusted.                  # name. The time must be recent and the user must be trusted.
1759                  if ((exists $trusteeTable{$user}) && ($time > $timeSelected)) {                      if ((exists $trusteeTable{$actualUser}) && ($time > $timeSelected)) {
1760                      $retVal = $function;                      $retVal = $function;
1761                      $timeSelected = $time;                      $timeSelected = $time;
1762                  }                  }
1763              }              }
1764          }          }
1765      } else {          }
         # Here we have a non-FIG feature ID. In this case the user ID does not  
         # matter. We simply get the information from the External Alias Function  
         # table.  
         ($retVal) = $self->GetEntityValues('ExternalAliasFunc', $featureID, ['ExternalAliasFunc(func)']);  
1766      }      }
1767      # Return the assignment found.      # Return the assignment found.
1768      return $retVal;      return $retVal;
1769  }  }
1770    
1771    =head3 FunctionsOf
1772    
1773        my @functionList = $sprout->FunctionOf($featureID, $userID);
1774    
1775    Return the functional assignments of a particular feature.
1776    
1777    The functional assignment is handled differently depending on the type of feature. If
1778    the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional
1779    assignment is a type of annotation. The format of an assignment is described in
1780    L</ParseAssignment>. Its worth noting that we cannot filter on the content of the
1781    annotation itself because it's a text field; however, this is not a big problem because
1782    most features only have a small number of annotations.
1783    
1784    =over 4
1785    
1786    =item featureID
1787    
1788    ID of the feature whose functional assignments are desired.
1789    
1790    =item RETURN
1791    
1792    Returns a list of 2-tuples, each consisting of a user ID and the text of an assignment by
1793    that user.
1794    
1795    =back
1796    
1797    =cut
1798    #: Return Type @@;
1799    sub FunctionsOf {
1800        # Get the parameters.
1801        my ($self, $featureID) = @_;
1802        # Declare the return value.
1803        my @retVal = ();
1804        # Convert to a FIG ID.
1805        my ($fid) = $self->FeaturesByAlias($featureID);
1806        # Only proceed if we found one.
1807        if ($fid) {
1808            # Here we have a FIG feature ID. We must build the list of trusted
1809            # users.
1810            my %trusteeTable = ();
1811            # Build a query for all of the feature's annotations, sorted by date.
1812            my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1813                                   "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1814                                   [$fid]);
1815            my $timeSelected = 0;
1816            # Loop until we run out of annotations.
1817            while (my $annotation = $query->Fetch()) {
1818                # Get the annotation text.
1819                my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1820                                                                'Annotation(time)',
1821                                                                'MadeAnnotation(user)']);
1822                # Check to see if this is a functional assignment for a trusted user.
1823                my ($actualUser, $function) = _ParseAssignment($user, $text);
1824                if ($actualUser) {
1825                    # Here it is a functional assignment.
1826                    push @retVal, [$actualUser, $function];
1827                }
1828            }
1829        }
1830        # Return the assignments found.
1831        return @retVal;
1832    }
1833    
1834  =head3 BBHList  =head3 BBHList
1835    
1836  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>      my $bbhHash = $sprout->BBHList($genomeID, \@featureList);
1837    
1838  Return a hash mapping the features in a specified list to their bidirectional best hits  Return a hash mapping the features in a specified list to their bidirectional best hits
1839  on a specified target genome.  on a specified target genome.
# Line 1262  Line 1864 
1864      my %retVal = ();      my %retVal = ();
1865      # Loop through the incoming features.      # Loop through the incoming features.
1866      for my $featureID (@{$featureList}) {      for my $featureID (@{$featureList}) {
1867          # Create a query to get the feature's best hit.          # Ask the server for the feature's best hit.
1868          my $query = $self->Get(['IsBidirectionalBestHitOf'],          my $bbhData = FIGRules::BBHData($featureID);
1869                                 "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?",          # Peel off the BBHs found.
1870                                 [$featureID, $genomeID]);          my @found = ();
1871          # Look for the best hit.          for my $bbh (@$bbhData) {
1872          my $bbh = $query->Fetch;              my $fid = $bbh->[0];
1873          if ($bbh) {              my $bbGenome = $self->GenomeOf($fid);
1874              my ($targetFeature) = $bbh->Value('IsBidirectionalBestHitOf(to-link)');              if ($bbGenome eq $genomeID) {
1875              $retVal{$featureID} = $targetFeature;                  push @found, $fid;
1876          }          }
1877      }      }
1878            $retVal{$featureID} = \@found;
1879        }
1880      # Return the mapping.      # Return the mapping.
1881      return \%retVal;      return \%retVal;
1882  }  }
1883    
1884  =head3 SimList  =head3 SimList
1885    
1886  C<< my %similarities = $sprout->SimList($featureID, $count); >>      my %similarities = $sprout->SimList($featureID, $count);
1887    
1888  Return a list of the similarities to the specified feature.  Return a list of the similarities to the specified feature.
1889    
1890  Sprout does not support real similarities, so this method just returns the bidirectional  This method just returns the bidirectional best hits for performance reasons.
 best hits.  
1891    
1892  =over 4  =over 4
1893    
# Line 1304  Line 1907 
1907      # Get the parameters.      # Get the parameters.
1908      my ($self, $featureID, $count) = @_;      my ($self, $featureID, $count) = @_;
1909      # Ask for the best hits.      # Ask for the best hits.
1910      my @lists = $self->GetAll(['IsBidirectionalBestHitOf'],      my $lists = FIGRules::BBHData($featureID);
                               "IsBidirectionalBestHitOf(from-link) = ? ORDER BY IsBidirectionalBestHitOf(score) DESC",  
                               [$featureID], ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(score)'],  
                               $count);  
1911      # Create the return value.      # Create the return value.
1912      my %retVal = ();      my %retVal = ();
1913      for my $tuple (@lists) {      for my $tuple (@$lists) {
1914          $retVal{$tuple->[0]} = $tuple->[1];          $retVal{$tuple->[0]} = $tuple->[1];
1915      }      }
1916      # Return the result.      # Return the result.
1917      return %retVal;      return %retVal;
1918  }  }
1919    
   
   
1920  =head3 IsComplete  =head3 IsComplete
1921    
1922  C<< my $flag = $sprout->IsComplete($genomeID); >>      my $flag = $sprout->IsComplete($genomeID);
1923    
1924  Return TRUE if the specified genome is complete, else FALSE.  Return TRUE if the specified genome is complete, else FALSE.
1925    
# Line 1346  Line 1944 
1944      # Declare the return variable.      # Declare the return variable.
1945      my $retVal;      my $retVal;
1946      # Get the genome's data.      # Get the genome's data.
1947      my $genomeData = $self->GetEntity('Genome', $genomeID);      my $genomeData = $self->_GenomeData($genomeID);
1948      if ($genomeData) {      # Only proceed if it exists.
1949        if (defined $genomeData) {
1950          # The genome exists, so get the completeness flag.          # The genome exists, so get the completeness flag.
1951          ($retVal) = $genomeData->Value('complete');          $retVal = $genomeData->PrimaryValue('Genome(complete)');
1952      }      }
1953      # Return the result.      # Return the result.
1954      return $retVal;      return $retVal;
# Line 1357  Line 1956 
1956    
1957  =head3 FeatureAliases  =head3 FeatureAliases
1958    
1959  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>      my @aliasList = $sprout->FeatureAliases($featureID);
1960    
1961  Return a list of the aliases for a specified feature.  Return a list of the aliases for a specified feature.
1962    
# Line 1380  Line 1979 
1979      # Get the parameters.      # Get the parameters.
1980      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
1981      # Get the desired feature's aliases      # Get the desired feature's aliases
1982      my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(alias)']);      my @retVal = $self->GetFlat(['IsAliasOf'], "IsAliasOf(to-link) = ?", [$featureID], 'IsAliasOf(from-link)');
1983      # Return the result.      # Return the result.
1984      return @retVal;      return @retVal;
1985  }  }
1986    
1987  =head3 GenomeOf  =head3 GenomeOf
1988    
1989  C<< my $genomeID = $sprout->GenomeOf($featureID); >>      my $genomeID = $sprout->GenomeOf($featureID);
1990    
1991  Return the genome that contains a specified feature.  Return the genome that contains a specified feature or contig.
1992    
1993  =over 4  =over 4
1994    
1995  =item featureID  =item featureID
1996    
1997  ID of the feature whose genome is desired.  ID of the feature or contig whose genome is desired.
1998    
1999  =item RETURN  =item RETURN
2000    
2001  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
2002  an undefined value.  found, returns an undefined value.
2003    
2004  =back  =back
2005    
# Line 1409  Line 2008 
2008  sub GenomeOf {  sub GenomeOf {
2009      # Get the parameters.      # Get the parameters.
2010      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
     # Create a query to find the genome associated with the feature.  
     my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ?", [$featureID]);  
2011      # Declare the return value.      # Declare the return value.
2012      my $retVal;      my $retVal;
2013      # Get the genome ID.      # Parse the genome ID from the feature ID.
2014      if (my $relationship = $query->Fetch()) {      if ($featureID =~ /^fig\|(\d+\.\d+)/) {
2015          ($retVal) = $relationship->Value('HasContig(from-link)');          $retVal = $1;
2016        } else {
2017            # Find the feature by alias.
2018            my ($realFeatureID) = $self->FeaturesByAlias($featureID);
2019            if ($realFeatureID && $realFeatureID =~ /^fig\|(\d+\.\d+)/) {
2020                $retVal = $1;
2021            }
2022      }      }
2023      # Return the value found.      # Return the value found.
2024      return $retVal;      return $retVal;
# Line 1423  Line 2026 
2026    
2027  =head3 CoupledFeatures  =head3 CoupledFeatures
2028    
2029  C<< my %coupleHash = $sprout->CoupledFeatures($featureID); >>      my %coupleHash = $sprout->CoupledFeatures($featureID);
2030    
2031  Return the features functionally coupled with a specified feature. Features are considered  Return the features functionally coupled with a specified feature. Features are considered
2032  functionally coupled if they tend to be clustered on the same chromosome.  functionally coupled if they tend to be clustered on the same chromosome.
# Line 1445  Line 2048 
2048  sub CoupledFeatures {  sub CoupledFeatures {
2049      # Get the parameters.      # Get the parameters.
2050      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
2051      # Create a query to retrieve the functionally-coupled features.      # Ask the coupling server for the data.
2052      my $query = $self->Get(['ParticipatesInCoupling', 'Coupling'],      Trace("Looking for features coupled to $featureID.") if T(coupling => 3);
2053                             "ParticipatesInCoupling(from-link) = ?", [$featureID]);      my @rawPairs = FIGRules::NetCouplingData('coupled_to', id1 => $featureID);
2054      # This value will be set to TRUE if we find at least one coupled feature.      Trace(scalar(@rawPairs) . " couplings returned.") if T(coupling => 3);
2055      my $found = 0;      # Form them into a hash.
     # Create the return hash.  
2056      my %retVal = ();      my %retVal = ();
2057      # Retrieve the relationship records and store them in the hash.      for my $pair (@rawPairs) {
2058      while (my $clustering = $query->Fetch()) {          # Get the feature ID and score.
2059          # Get the ID and score of the coupling.          my ($featureID2, $score) = @{$pair};
2060          my ($couplingID, $score) = $clustering->Values(['Coupling(id)',          # Only proceed if the feature is in NMPDR.
2061                                                          'Coupling(score)']);          if ($self->_CheckFeature($featureID2)) {
2062          # The coupling ID contains the two feature IDs separated by a space. We use              $retVal{$featureID2} = $score;
         # this information to find the ID of the other feature.  
         my ($fid1, $fid2) = split / /, $couplingID;  
         my $otherFeatureID = ($featureID eq $fid1 ? $fid2 : $fid1);  
         # Attach the other feature's score to its ID.  
         $retVal{$otherFeatureID} = $score;  
         $found = 1;  
2063      }      }
     # Functional coupling is reflexive. If we found at least one coupled feature, we must add  
     # the incoming feature as well.  
     if ($found) {  
         $retVal{$featureID} = 9999;  
2064      }      }
2065      # Return the hash.      # Return the hash.
2066      return %retVal;      return %retVal;
# Line 1476  Line 2068 
2068    
2069  =head3 CouplingEvidence  =head3 CouplingEvidence
2070    
2071  C<< my @evidence = $sprout->CouplingEvidence($peg1, $peg2); >>      my @evidence = $sprout->CouplingEvidence($peg1, $peg2);
2072    
2073  Return the evidence for a functional coupling.  Return the evidence for a functional coupling.
2074    
# Line 1524  Line 2116 
2116      my ($self, $peg1, $peg2) = @_;      my ($self, $peg1, $peg2) = @_;
2117      # Declare the return variable.      # Declare the return variable.
2118      my @retVal = ();      my @retVal = ();
2119      # Our first task is to find out the nature of the coupling: whether or not      # Get the coupling and evidence data.
2120      # it exists, its score, and whether the features are stored in the same      my @rawData = FIGRules::NetCouplingData('coupling_evidence', id1 => $peg1, id2 => $peg2);
2121      # order as the ones coming in.      # Loop through the raw data, saving the ones that are in NMPDR genomes.
2122      my ($couplingID, $inverted, $score) = $self->GetCoupling($peg1, $peg2);      for my $rawTuple (@rawData) {
2123      # Only proceed if a coupling exists.          if ($self->_CheckFeature($rawTuple->[0]) && $self->_CheckFeature($rawTuple->[1])) {
2124      if ($couplingID) {              push @retVal, $rawTuple;
2125          # Determine the ordering to place on the evidence items. If we're          }
         # inverted, we want to see feature 2 before feature 1 (descending); otherwise,  
         # we want feature 1 before feature 2 (normal).  
         Trace("Coupling evidence for ($peg1, $peg2) with inversion flag $inverted.") if T(Coupling => 4);  
         my $ordering = ($inverted ? "DESC" : "");  
         # Get the coupling evidence.  
         my @evidenceList = $self->GetAll(['IsEvidencedBy', 'PCH', 'UsesAsEvidence'],  
                                           "IsEvidencedBy(from-link) = ? ORDER BY PCH(id), UsesAsEvidence(pos) $ordering",  
                                           [$couplingID],  
                                           ['PCH(used)', 'UsesAsEvidence(to-link)']);  
         # Loop through the evidence items. Each piece of evidence is represented by two  
         # positions in the evidence list, one for each feature on the other side of the  
         # evidence link. If at some point we want to generalize to couplings with  
         # more than two positions, this section of code will need to be re-done.  
         while (@evidenceList > 0) {  
             my $peg1Data = shift @evidenceList;  
             my $peg2Data = shift @evidenceList;  
             Trace("Peg 1 is " . $peg1Data->[1] . " and Peg 2 is " . $peg2Data->[1] . ".") if T(Coupling => 4);  
             push @retVal, [$peg1Data->[1], $peg2Data->[1], $peg1Data->[0]];  
         }  
         Trace("Last index in evidence result is is $#retVal.") if T(Coupling => 4);  
2126      }      }
2127      # Return the result.      # Return the result.
2128      return @retVal;      return @retVal;
2129  }  }
2130    
2131  =head3 GetCoupling  =head3 GetSynonymGroup
   
 C<< my ($couplingID, $inverted, $score) = $sprout->GetCoupling($peg1, $peg2); >>  
   
 Return the coupling (if any) for the specified pair of PEGs. If a coupling  
 exists, we return the coupling ID along with an indicator of whether the  
 coupling is stored as C<(>I<$peg1>C<, >I<$peg2>C<)> or C<(>I<$peg2>C<, >I<$peg1>C<)>.  
 In the second case, we say the coupling is I<inverted>. The importance of an  
 inverted coupling is that the PEGs in the evidence will appear in reverse order.  
2132    
2133  =over 4      my $id = $sprout->GetSynonymGroup($fid);
2134    
2135  =item peg1  Return the synonym group name for the specified feature.
2136    
2137  ID of the feature of interest.  =over 4
2138    
2139  =item peg2  =item fid
2140    
2141  ID of the potentially coupled feature.  ID of the feature whose synonym group is desired.
2142    
2143  =item RETURN  =item RETURN
2144    
2145  Returns a three-element list. The first element contains the database ID of  The name of the synonym group to which the feature belongs. If the feature does
2146  the coupling. The second element is FALSE if the coupling is stored in the  not belong to a synonym group, the feature ID itself is returned.
 database in the caller specified order and TRUE if it is stored in the  
 inverted order. The third element is the coupling's score. If the coupling  
 does not exist, all three list elements will be C<undef>.  
2147    
2148  =back  =back
2149    
2150  =cut  =cut
2151  #: Return Type $%@;  
2152  sub GetCoupling {  sub GetSynonymGroup {
2153      # Get the parameters.      # Get the parameters.
2154      my ($self, $peg1, $peg2) = @_;      my ($self, $fid) = @_;
2155      # Declare the return values. We'll start with the coupling ID and undefine the      # Declare the return variable.
2156      # flag and score until we have more information.      my $retVal;
2157      my ($retVal, $inverted, $score) = (CouplingID($peg1, $peg2), undef, undef);      # Find the synonym group.
2158      # Find the coupling data.      my @groups = $self->GetFlat(['IsSynonymGroupFor'], "IsSynonymGroupFor(to-link) = ?",
2159      my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'],                                     [$fid], 'IsSynonymGroupFor(from-link)');
                                  "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)",  
                                  [$retVal], ["ParticipatesInCoupling(from-link)", "Coupling(score)"]);  
2160      # Check to see if we found anything.      # Check to see if we found anything.
2161      if (!@pegs) {      if (@groups) {
2162          Trace("No coupling found.") if T(Coupling => 4);          $retVal = $groups[0];
         # No coupling, so undefine the return value.  
         $retVal = undef;  
2163      } else {      } else {
2164          # We have a coupling! Get the score and check for inversion.          $retVal = $fid;
         $score = $pegs[0]->[1];  
         my $firstFound = $pegs[0]->[0];  
         $inverted = ($firstFound ne $peg1);  
         Trace("Coupling score is $score. First peg is $firstFound, peg 1 is $peg1.") if T(Coupling => 4);  
2165      }      }
2166      # Return the result.      # Return the result.
2167      return ($retVal, $inverted, $score);      return $retVal;
2168  }  }
2169    
2170  =head3 CouplingID  =head3 GetBoundaries
   
 C<< my $couplingID = Sprout::CouplingID($peg1, $peg2); >>  
2171    
2172  Return the coupling ID for a pair of feature IDs.      my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList);
2173    
2174  The coupling ID is currently computed by joining the feature IDs in  Determine the begin and end boundaries for the locations in a list. All of the
2175  sorted order with a space. Client modules (that is, modules which  locations must belong to the same contig and have mostly the same direction in
2176  use Sprout) should not, however, count on this always being the  order for this method to produce a meaningful result. The resulting
2177  case. This method provides a way for abstracting the concept of a  begin/end pair will contain all of the bases in any of the locations.
 coupling ID. All that we know for sure about it is that it can be  
 generated easily from the feature IDs and the order of the IDs  
 in the parameter list does not matter (i.e. C<CouplingID("a1", "b1")>  
 will have the same value as C<CouplingID("b1", "a1")>.  
2178    
2179  =over 4  =over 4
2180    
2181  =item peg1  =item locList
   
 First feature of interest.  
2182    
2183  =item peg2  List of locations to process.
   
 Second feature of interest.  
2184    
2185  =item RETURN  =item RETURN
2186    
2187  Returns the ID that would be used to represent a functional coupling of  Returns a 3-tuple consisting of the contig ID, the beginning boundary,
2188  the two specified PEGs.  and the ending boundary. The beginning boundary will be left of the
2189    end for mostly-forward locations and right of the end for mostly-backward
2190    locations.
2191    
2192  =back  =back
2193    
2194  =cut  =cut
 #: Return Type $;  
 sub CouplingID {  
     return join " ", sort @_;  
 }  
2195    
2196  =head3 GetEntityTypes  sub GetBoundaries {
   
 C<< my @entityList = $sprout->GetEntityTypes(); >>  
   
 Return the list of supported entity types.  
   
 =cut  
 #: Return Type @;  
 sub GetEntityTypes {  
2197      # Get the parameters.      # Get the parameters.
2198      my ($self) = @_;      my ($self, @locList) = @_;
2199      # Get the underlying database object.      # Set up the counters used to determine the most popular direction.
2200      my $erdb = $self->{_erdb};      my %counts = ( '+' => 0, '-' => 0 );
2201      # Get its entity type list.      # Get the last location and parse it.
2202      my @retVal = $erdb->GetEntityTypes();      my $locObject = BasicLocation->new(pop @locList);
2203        # Prime the loop with its data.
2204        my ($contig, $beg, $end) = ($locObject->Contig, $locObject->Left, $locObject->Right);
2205        # Count its direction.
2206        $counts{$locObject->Dir}++;
2207        # Loop through the remaining locations. Note that in most situations, this loop
2208        # will not iterate at all, because most of the time we will be dealing with a
2209        # singleton list.
2210        for my $loc (@locList) {
2211            # Create a location object.
2212            my $locObject = BasicLocation->new($loc);
2213            # Count the direction.
2214            $counts{$locObject->Dir}++;
2215            # Get the left end and the right end.
2216            my $left = $locObject->Left;
2217            my $right = $locObject->Right;
2218            # Merge them into the return variables.
2219            if ($left < $beg) {
2220                $beg = $left;
2221            }
2222            if ($right > $end) {
2223                $end = $right;
2224            }
2225        }
2226        # If the most common direction is reverse, flip the begin and end markers.
2227        if ($counts{'-'} > $counts{'+'}) {
2228            ($beg, $end) = ($end, $beg);
2229        }
2230        # Return the result.
2231        return ($contig, $beg, $end);
2232  }  }
2233    
2234  =head3 ReadFasta  =head3 ReadFasta
2235    
2236  C<< my %sequenceData = Sprout::ReadFasta($fileName, $prefix); >>      my %sequenceData = Sprout::ReadFasta($fileName, $prefix);
2237    
2238  Read sequence data from a FASTA-format file. Each sequence in a FASTA file is represented by  Read sequence data from a FASTA-format file. Each sequence in a FASTA file is represented by
2239  one or more lines of data. The first line begins with a > character and contains an ID.  one or more lines of data. The first line begins with a > character and contains an ID.
# Line 1737  Line 2299 
2299    
2300  =head3 FormatLocations  =head3 FormatLocations
2301    
2302  C<< my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat); >>      my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat);
2303    
2304  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
2305  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
# Line 1802  Line 2364 
2364    
2365  =head3 DumpData  =head3 DumpData
2366    
2367  C<< $sprout->DumpData(); >>      $sprout->DumpData();
2368    
2369  Dump all the tables to tab-delimited DTX files. The files will be stored in the data directory.  Dump all the tables to tab-delimited DTX files. The files will be stored in the data directory.
2370    
# Line 1814  Line 2376 
2376      # Get the data directory name.      # Get the data directory name.
2377      my $outputDirectory = $self->{_options}->{dataDir};      my $outputDirectory = $self->{_options}->{dataDir};
2378      # Dump the relations.      # Dump the relations.
2379      $self->{_erdb}->DumpRelations($outputDirectory);      $self->DumpRelations($outputDirectory);
2380  }  }
2381    
2382  =head3 XMLFileName  =head3 XMLFileName
2383    
2384  C<< my $fileName = $sprout->XMLFileName(); >>      my $fileName = $sprout->XMLFileName();
2385    
2386  Return the name of this database's XML definition file.  Return the name of this database's XML definition file.
2387    
# Line 1830  Line 2392 
2392      return $self->{_xmlName};      return $self->{_xmlName};
2393  }  }
2394    
2395    =head3 GetGenomeNameData
2396    
2397        my ($genus, $species, $strain) = $sprout->GenomeNameData($genomeID);
2398    
2399    Return the genus, species, and unique characterization for a genome. This
2400    is similar to L</GenusSpecies>, with the exception that it returns the
2401    values in three seperate fields.
2402    
2403    =over 4
2404    
2405    =item genomeID
2406    
2407    ID of the genome whose name data is desired.
2408    
2409    =item RETURN
2410    
2411    Returns a three-element list, consisting of the genus, species, and strain
2412    of the specified genome. If the genome is not found, an error occurs.
2413    
2414    =back
2415    
2416    =cut
2417    
2418    sub GetGenomeNameData {
2419        # Get the parameters.
2420        my ($self, $genomeID) = @_;
2421        # Declare the return variables.
2422        my ($genus, $species, $strain);
2423        # Get the genome's data.
2424        my $genomeData = $self->_GenomeData($genomeID);
2425        # Only proceed if the genome exists.
2426        if (defined $genomeData) {
2427            # Get the desired values.
2428            ($genus, $species, $strain) = $genomeData->Values(['Genome(genus)',
2429                                                               'Genome(species)',
2430                                                               'Genome(unique-characterization)']);
2431        } else {
2432            # Throw an error because they were not found.
2433            Confess("Genome $genomeID not found in database.");
2434        }
2435        # Return the results.
2436        return ($genus, $species, $strain);
2437    }
2438    
2439    =head3 GetGenomeByNameData
2440    
2441        my @genomes = $sprout->GetGenomeByNameData($genus, $species, $strain);
2442    
2443    Return a list of the IDs of the genomes with the specified genus,
2444    species, and strain. In almost every case, there will be either zero or
2445    one IDs returned; however, two or more IDs could be returned if there are
2446    multiple versions of the genome in the database.
2447    
2448    =over 4
2449    
2450    =item genus
2451    
2452    Genus of the desired genome.
2453    
2454    =item species
2455    
2456    Species of the desired genome.
2457    
2458    =item strain
2459    
2460    Strain (unique characterization) of the desired genome. This may be an empty
2461    string, in which case it is presumed that the desired genome has no strain
2462    specified.
2463    
2464    =item RETURN
2465    
2466    Returns a list of the IDs of the genomes having the specified genus, species, and
2467    strain.
2468    
2469    =back
2470    
2471    =cut
2472    
2473    sub GetGenomeByNameData {
2474        # Get the parameters.
2475        my ($self, $genus, $species, $strain) = @_;
2476        # Try to find the genomes.
2477        my @retVal = $self->GetFlat(['Genome'], "Genome(genus) = ? AND Genome(species) = ? AND Genome(unique-characterization) = ?",
2478                                    [$genus, $species, $strain], 'Genome(id)');
2479        # Return the result.
2480        return @retVal;
2481    }
2482    
2483  =head3 Insert  =head3 Insert
2484    
2485  C<< $sprout->Insert($objectType, \%fieldHash); >>      $sprout->Insert($objectType, \%fieldHash);
2486    
2487  Insert an entity or relationship instance into the database. The entity or relationship of interest  Insert an entity or relationship instance into the database. The entity or relationship of interest
2488  is defined by a type name and then a hash of field names to values. Field values in the primary  is defined by a type name and then a hash of field names to values. Field values in the primary
# Line 1841  Line 2491 
2491  list references. For example, the following line inserts an inactive PEG feature named  list references. For example, the following line inserts an inactive PEG feature named
2492  C<fig|188.1.peg.1> with aliases C<ZP_00210270.1> and C<gi|46206278>.  C<fig|188.1.peg.1> with aliases C<ZP_00210270.1> and C<gi|46206278>.
2493    
2494  C<< $sprout->Insert('Feature', { id => 'fig|188.1.peg.1', active => 0, feature-type => 'peg', alias => ['ZP_00210270.1', 'gi|46206278']}); >>      $sprout->Insert('Feature', { id => 'fig|188.1.peg.1', active => 0, feature-type => 'peg', alias => ['ZP_00210270.1', 'gi|46206278']});
2495    
2496  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
2497  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>.
2498    
2499  C<< $sprout->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence => 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >>      $sprout->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence => 'http://seedu.uchicago.edu/query.cgi?article_id=142'});
2500    
2501  =over 4  =over 4
2502    
# Line 1866  Line 2516 
2516      # Get the parameters.      # Get the parameters.
2517      my ($self, $objectType, $fieldHash) = @_;      my ($self, $objectType, $fieldHash) = @_;
2518      # Call the underlying method.      # Call the underlying method.
2519      $self->{_erdb}->InsertObject($objectType, $fieldHash);      $self->InsertObject($objectType, $fieldHash);
2520  }  }
2521    
2522  =head3 Annotate  =head3 Annotate
2523    
2524  C<< my $ok = $sprout->Annotate($fid, $timestamp, $user, $text); >>      my $ok = $sprout->Annotate($fid, $timestamp, $user, $text);
2525    
2526  Annotate a feature. This inserts an Annotation record into the database and links it to the  Annotate a feature. This inserts an Annotation record into the database and links it to the
2527  specified feature and user.  specified feature and user.
# Line 1925  Line 2575 
2575    
2576  =head3 AssignFunction  =head3 AssignFunction
2577    
2578  C<< my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser); >>      my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser);
2579    
2580  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
2581  format is described in L</ParseAssignment>.  format is described in L</ParseAssignment>.
# Line 1985  Line 2635 
2635    
2636  =head3 FeaturesByAlias  =head3 FeaturesByAlias
2637    
2638  C<< my @features = $sprout->FeaturesByAlias($alias); >>      my @features = $sprout->FeaturesByAlias($alias);
2639    
2640  Returns a list of features with the specified alias. The alias is parsed to determine  Returns a list of features with the specified alias. The alias is parsed to determine
2641  the type of the alias. A string of digits is a GenBack ID and a string of exactly 6  the type of the alias. A string of digits is a GenBack ID and a string of exactly 6
# Line 2019  Line 2669 
2669          push @retVal, $mappedAlias;          push @retVal, $mappedAlias;
2670      } else {      } else {
2671          # Here we have a non-FIG alias. Get the features with the normalized alias.          # Here we have a non-FIG alias. Get the features with the normalized alias.
2672          @retVal = $self->GetFlat(['Feature'], 'Feature(alias) = ?', [$mappedAlias], 'Feature(id)');          @retVal = $self->GetFlat(['IsAliasOf'], 'IsAliasOf(from-link) = ?', [$mappedAlias], 'IsAliasOf(to-link)');
2673      }      }
2674      # Return the result.      # Return the result.
2675      return @retVal;      return @retVal;
2676  }  }
2677    
 =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, $entityName, $entityID) = @_;  
     # Check for the entity instance.  
     Trace("Checking existence of $entityName with ID=$entityID.") if T(4);  
     my $testInstance = $self->GetEntity($entityName, $entityID);  
     # Return an existence indicator.  
     my $retVal = ($testInstance ? 1 : 0);  
     return $retVal;  
 }  
   
2678  =head3 FeatureTranslation  =head3 FeatureTranslation
2679    
2680  C<< my $translation = $sprout->FeatureTranslation($featureID); >>      my $translation = $sprout->FeatureTranslation($featureID);
2681    
2682  Return the translation of a feature.  Return the translation of a feature.
2683    
# Line 2090  Line 2705 
2705    
2706  =head3 Taxonomy  =head3 Taxonomy
2707    
2708  C<< my @taxonomyList = $sprout->Taxonomy($genome); >>      my @taxonomyList = $sprout->Taxonomy($genome);
2709    
2710  Return the taxonomy of the specified genome. This will be in the form of a list  Return the taxonomy of the specified genome. This will be in the form of a list
2711  containing the various classifications in order from domain (eg. C<Bacteria>, C<Archaea>,  containing the various classifications in order from domain (eg. C<Bacteria>, C<Archaea>,
2712  or C<Eukaryote>) to sub-species. For example,  or C<Eukaryote>) to sub-species. For example,
2713    
2714  C<< (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12) >>      (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12)
2715    
2716  =over 4  =over 4
2717    
# Line 2115  Line 2730 
2730  sub Taxonomy {  sub Taxonomy {
2731      # Get the parameters.      # Get the parameters.
2732      my ($self, $genome) = @_;      my ($self, $genome) = @_;
     # Find the specified genome's taxonomy string.  
     my ($list) = $self->GetEntityValues('Genome', $genome, ['Genome(taxonomy)']);  
2733      # Declare the return variable.      # Declare the return variable.
2734      my @retVal = ();      my @retVal = ();
2735      # If we found the genome, return its taxonomy string.      # Get the genome data.
2736      if ($list) {      my $genomeData = $self->_GenomeData($genome);
2737          @retVal = split /\s*;\s*/, $list;      # Only proceed if it exists.
2738        if (defined $genomeData) {
2739            # Create the taxonomy from the taxonomy string.
2740            @retVal = split /\s*;\s*/, $genomeData->PrimaryValue('Genome(taxonomy)');
2741      } else {      } else {
2742            # Genome doesn't exist, so emit a warning.
2743          Trace("Genome \"$genome\" does not have a taxonomy in the database.\n") if T(0);          Trace("Genome \"$genome\" does not have a taxonomy in the database.\n") if T(0);
2744      }      }
2745      # Return the value found.      # Return the value found.
# Line 2131  Line 2748 
2748    
2749  =head3 CrudeDistance  =head3 CrudeDistance
2750    
2751  C<< my $distance = $sprout->CrudeDistance($genome1, $genome2); >>      my $distance = $sprout->CrudeDistance($genome1, $genome2);
2752    
2753  Returns a crude estimate of the distance between two genomes. The distance is construed so  Returns a crude estimate of the distance between two genomes. The distance is construed so
2754  that it will be 0 for genomes with identical taxonomies and 1 for genomes from different domains.  that it will be 0 for genomes with identical taxonomies and 1 for genomes from different domains.
# Line 2167  Line 2784 
2784      }      }
2785      my @taxA = $self->Taxonomy($genomeA);      my @taxA = $self->Taxonomy($genomeA);
2786      my @taxB = $self->Taxonomy($genomeB);      my @taxB = $self->Taxonomy($genomeB);
2787      # Initialize the distance to 1. We'll reduce it each time we find a match between the      # Compute the distance.
2788      # taxonomies.      my $retVal = FIGRules::CrudeDistanceFormula(\@taxA, \@taxB);
     my $retVal = 1.0;  
     # Initialize the subtraction amount. This amount determines the distance reduction caused  
     # by a mismatch at the current level.  
     my $v = 0.5;  
     # Loop through the taxonomies.  
     for (my $i = 0; ($i < @taxA) && ($i < @taxB) && ($taxA[$i] eq $taxB[$i]); $i++) {  
         $retVal -= $v;  
         $v /= 2;  
     }  
2789      return $retVal;      return $retVal;
2790  }  }
2791    
2792  =head3 RoleName  =head3 RoleName
2793    
2794  C<< my $roleName = $sprout->RoleName($roleID); >>      my $roleName = $sprout->RoleName($roleID);
2795    
2796  Return the descriptive name of the role with the specified ID. In general, a role  Return the descriptive name of the role with the specified ID. In general, a role
2797  will only have a descriptive name if it is coded as an EC number.  will only have a descriptive name if it is coded as an EC number.
# Line 2217  Line 2825 
2825    
2826  =head3 RoleDiagrams  =head3 RoleDiagrams
2827    
2828  C<< my @diagrams = $sprout->RoleDiagrams($roleID); >>      my @diagrams = $sprout->RoleDiagrams($roleID);
2829    
2830  Return a list of the diagrams containing a specified functional role.  Return a list of the diagrams containing a specified functional role.
2831    
# Line 2245  Line 2853 
2853      return @retVal;      return @retVal;
2854  }  }
2855    
 =head3 GetProperties  
   
 C<< my @list = $sprout->GetProperties($fid, $key, $value, $url); >>  
   
 Return a list of the properties with the specified characteristics.  
   
 Properties are arbitrary key-value pairs associated with a feature. (At some point they  
 will also be associated with genomes.) A property value is represented by a 4-tuple of  
 the form B<($fid, $key, $value, $url)>. These exactly correspond to the parameter  
   
 =over 4  
   
 =item fid  
   
 ID of the feature possessing the property.  
   
 =item key  
   
 Name or key of the property.  
   
 =item value  
   
 Value of the property.  
   
 =item url  
   
 URL of the document that indicated the property should have this particular value, or an  
 empty string if no such document exists.  
   
 =back  
   
 The parameters act as a filter for the desired data. Any non-null parameter will  
 automatically match all the tuples returned. So, specifying just the I<$fid> will  
 return all the properties of the specified feature; similarly, specifying the I<$key>  
 and I<$value> parameters will return all the features having the specified property  
 value.  
   
 A single property key can have many values, representing different ideas about the  
 feature in question. For example, one paper may declare that a feature C<fig|83333.1.peg.10> is  
 virulent, and another may declare that it is not virulent. A query about the virulence of  
 C<fig|83333.1.peg.10> would be coded as  
   
     my @list = $sprout->GetProperties('fig|83333.1.peg.10', 'virulence', '', '');  
   
 Here the I<$value> and I<$url> fields are left blank, indicating that those fields are  
 not to be filtered. The tuples returned would be  
   
     ('fig|83333.1.peg.10', 'virulence', 'yes', 'http://www.somewhere.edu/first.paper.pdf')  
     ('fig|83333.1.peg.10', 'virulence', 'no', 'http://www.somewhere.edu/second.paper.pdf')  
   
 =cut  
 #: Return Type @@;  
 sub GetProperties {  
     # Get the parameters.  
     my ($self, @parms) = @_;  
     # Declare the return variable.  
     my @retVal = ();  
     # Now we need to create a WHERE clause that will get us the data we want. First,  
     # we create a list of the columns containing the data for each parameter.  
     my @colNames = ('HasProperty(from-link)', 'Property(property-name)',  
                     'Property(property-value)', 'HasProperty(evidence)');  
     # Now we build the WHERE clause and the list of parameter values.  
     my @where = ();  
     my @values = ();  
     for (my $i = 0; $i <= $#colNames; $i++) {  
         my $parm = $parms[$i];  
         if (defined $parm && ($parm ne '')) {  
             push @where, "$colNames[$i] = ?";  
             push @values, $parm;  
         }  
     }  
     # Format the WHERE clause.  
     my $filter = (@values > 0 ? (join " AND ", @where) : undef);  
     # Ask for all the propertie values with the desired characteristics.  
     my $query = $self->Get(['HasProperty', 'Property'], $filter, \@values);  
     while (my $valueObject = $query->Fetch()) {  
         my @tuple = $valueObject->Values(\@colNames);  
         push @retVal, \@tuple;  
     }  
     # Return the result.  
     return @retVal;  
 }  
   
2856  =head3 FeatureProperties  =head3 FeatureProperties
2857    
2858  C<< my @properties = $sprout->FeatureProperties($featureID); >>      my @properties = $sprout->FeatureProperties($featureID);
2859    
2860  Return a list of the properties for the specified feature. Properties are key-value pairs  Return a list of the properties for the specified feature. Properties are key-value pairs
2861  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
2862  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
2863  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
2864  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.  
2865    
2866  =over 4  =over 4
2867    
# Line 2348  Line 2871 
2871    
2872  =item RETURN  =item RETURN
2873    
2874  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.  
2875    
2876  =back  =back
2877    
# Line 2359  Line 2881 
2881      # Get the parameters.      # Get the parameters.
2882      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
2883      # Get the properties.      # Get the properties.
2884      my @retVal = $self->GetAll(['HasProperty', 'Property'], "HasProperty(from-link) = ?", [$featureID],      my @attributes = $self->ca->GetAttributes($featureID);
2885                              ['Property(property-name)', 'Property(property-value)',      # Strip the feature ID off each tuple.
2886                               'HasProperty(evidence)']);      my @retVal = ();
2887        for my $attributeRow (@attributes) {
2888            shift @{$attributeRow};
2889            push @retVal, $attributeRow;
2890        }
2891      # Return the resulting list.      # Return the resulting list.
2892      return @retVal;      return @retVal;
2893  }  }
2894    
2895  =head3 DiagramName  =head3 DiagramName
2896    
2897  C<< my $diagramName = $sprout->DiagramName($diagramID); >>      my $diagramName = $sprout->DiagramName($diagramID);
2898    
2899  Return the descriptive name of a diagram.  Return the descriptive name of a diagram.
2900    
# Line 2394  Line 2920 
2920      return $retVal;      return $retVal;
2921  }  }
2922    
2923    =head3 PropertyID
2924    
2925        my $id = $sprout->PropertyID($propName, $propValue);
2926    
2927    Return the ID of the specified property name and value pair, if the
2928    pair exists. Only a small subset of the FIG attributes are stored as
2929    Sprout properties, mostly for use in search optimization.
2930    
2931    =over 4
2932    
2933    =item propName
2934    
2935    Name of the desired property.
2936    
2937    =item propValue
2938    
2939    Value expected for the desired property.
2940    
2941    =item RETURN
2942    
2943    Returns the ID of the name/value pair, or C<undef> if the pair does not exist.
2944    
2945    =back
2946    
2947    =cut
2948    
2949    sub PropertyID {
2950        # Get the parameters.
2951        my ($self, $propName, $propValue) = @_;
2952        # Try to find the ID.
2953        my ($retVal) = $self->GetFlat(['Property'],
2954                                      "Property(property-name) = ? AND Property(property-value) = ?",
2955                                      [$propName, $propValue], 'Property(id)');
2956        # Return the result.
2957        return $retVal;
2958    }
2959    
2960  =head3 MergedAnnotations  =head3 MergedAnnotations
2961    
2962  C<< my @annotationList = $sprout->MergedAnnotations(\@list); >>      my @annotationList = $sprout->MergedAnnotations(\@list);
2963    
2964  Returns a merged list of the annotations for the features in a list. Each annotation is  Returns a merged list of the annotations for the features in a list. Each annotation is
2965  represented by a 4-tuple of the form C<($fid, $timestamp, $userID, $annotation)>, where  represented by a 4-tuple of the form C<($fid, $timestamp, $userID, $annotation)>, where
# Line 2445  Line 3008 
3008    
3009  =head3 RoleNeighbors  =head3 RoleNeighbors
3010    
3011  C<< my @roleList = $sprout->RoleNeighbors($roleID); >>      my @roleList = $sprout->RoleNeighbors($roleID);
3012    
3013  Returns a list of the roles that occur in the same diagram as the specified role. Because  Returns a list of the roles that occur in the same diagram as the specified role. Because
3014  diagrams and roles are in a many-to-many relationship with each other, the list is  diagrams and roles are in a many-to-many relationship with each other, the list is
# Line 2488  Line 3051 
3051    
3052  =head3 FeatureLinks  =head3 FeatureLinks
3053    
3054  C<< my @links = $sprout->FeatureLinks($featureID); >>      my @links = $sprout->FeatureLinks($featureID);
3055    
3056  Return a list of the web hyperlinks associated with a feature. The web hyperlinks are  Return a list of the web hyperlinks associated with a feature. The web hyperlinks are
3057  to external websites describing either the feature itself or the organism containing it  to external websites describing either the feature itself or the organism containing it
# Line 2519  Line 3082 
3082    
3083  =head3 SubsystemsOf  =head3 SubsystemsOf
3084    
3085  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>      my %subsystems = $sprout->SubsystemsOf($featureID);
3086    
3087  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
3088  to the roles the feature performs.  to the roles the feature performs.
# Line 2547  Line 3110 
3110                                      ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);                                      ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);
3111      # Create the return value.      # Create the return value.
3112      my %retVal = ();      my %retVal = ();
3113        # Build a hash to weed out duplicates. Sometimes the same PEG and role appears
3114        # in two spreadsheet cells.
3115        my %dupHash = ();
3116      # Loop through the results, adding them to the hash.      # Loop through the results, adding them to the hash.
3117      for my $record (@subsystems) {      for my $record (@subsystems) {
3118            # Get this subsystem and role.
3119          my ($subsys, $role) = @{$record};          my ($subsys, $role) = @{$record};
3120          if (exists $retVal{$subsys}) {          # Insure it's the first time for both.
3121            my $dupKey = "$subsys\n$role";
3122            if (! exists $dupHash{"$subsys\n$role"}) {
3123                $dupHash{$dupKey} = 1;
3124              push @{$retVal{$subsys}}, $role;              push @{$retVal{$subsys}}, $role;
         } else {  
             $retVal{$subsys} = [$role];  
3125          }          }
3126      }      }
3127      # Return the hash.      # Return the hash.
# Line 2562  Line 3130 
3130    
3131  =head3 SubsystemList  =head3 SubsystemList
3132    
3133  C<< my @subsystems = $sprout->SubsystemList($featureID); >>      my @subsystems = $sprout->SubsystemList($featureID);
3134    
3135  Return a list containing the names of the subsystems in which the specified  Return a list containing the names of the subsystems in which the specified
3136  feature participates. Unlike L</SubsystemsOf>, this method only returns the  feature participates. Unlike L</SubsystemsOf>, this method only returns the
# Line 2585  Line 3153 
3153  sub SubsystemList {  sub SubsystemList {
3154      # Get the parameters.      # Get the parameters.
3155      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
3156      # Get the list of names.      # Get the list of names. We do a join to the Subsystem table because we have missing subsystems in
3157      my @retVal = $self->GetFlat(['ContainsFeature', 'HasSSCell'], "ContainsFeature(to-link) = ?",      # the Sprout database!
3158                                  [$featureID], 'HasSSCell(from-link)');      my @retVal = $self->GetFlat(['HasRoleInSubsystem', 'Subsystem'], "HasRoleInSubsystem(from-link) = ?",
3159                                    [$featureID], 'HasRoleInSubsystem(to-link)');
3160        # Return the result, sorted.
3161        return sort @retVal;
3162    }
3163    
3164    =head3 GenomeSubsystemData
3165    
3166        my %featureData = $sprout->GenomeSubsystemData($genomeID);
3167    
3168    Return a hash mapping genome features to their subsystem roles.
3169    
3170    =over 4
3171    
3172    =item genomeID
3173    
3174    ID of the genome whose subsystem feature map is desired.
3175    
3176    =item RETURN
3177    
3178    Returns a hash mapping each feature of the genome to a list of 2-tuples. Eacb
3179    2-tuple contains a subsystem name followed by a role ID.
3180    
3181    =back
3182    
3183    =cut
3184    
3185    sub GenomeSubsystemData {
3186        # Get the parameters.
3187        my ($self, $genomeID) = @_;
3188        # Declare the return variable.
3189        my %retVal = ();
3190        # Get a list of the genome features that participate in subsystems. For each
3191        # feature we get its subsystem ID and the corresponding roles.
3192        my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf', 'HasSSCell'],
3193                                     "HasFeature(from-link) = ?", [$genomeID],
3194                                     ['HasFeature(to-link)', 'IsRoleOf(from-link)',  'HasSSCell(from-link)']);
3195        # Now we get a list of valid subsystems. These are the subsystems connected to the genome with
3196        # a non-negative variant code.
3197        my %subs = map { $_ => 1 } $self->GetFlat(['ParticipatesIn'],
3198                                                    "ParticipatesIn(from-link) = ? AND ParticipatesIn(variant-code) >= 0",
3199                                                    [$genomeID], 'ParticipatesIn(to-link)');
3200        # We loop through @roleData to build the hash.
3201        for my $roleEntry (@roleData) {
3202            # Get the data for this feature and cell.
3203            my ($fid, $role, $subsys) = @{$roleEntry};
3204            Trace("Subsystem for $fid is $subsys.") if T(4);
3205            # Check the subsystem;
3206            if ($subs{$subsys}) {
3207                Trace("Subsystem found.") if T(4);
3208                # Insure this feature has an entry in the return hash.
3209                if (! exists $retVal{$fid}) { $retVal{$fid} = []; }
3210                # Merge in this new data.
3211                push @{$retVal{$fid}}, [$subsys, $role];
3212            }
3213        }
3214      # Return the result.      # Return the result.
3215      return @retVal;      return %retVal;
3216  }  }
3217    
3218  =head3 RelatedFeatures  =head3 RelatedFeatures
3219    
3220  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>      my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID);
3221    
3222  Return a list of the features which are bi-directional best hits of the specified feature and  Return a list of the features which are bi-directional best hits of the specified feature and
3223  have been assigned the specified function by the specified user. If no such features exists,  have been assigned the specified function by the specified user. If no such features exists,
# Line 2627  Line 3250 
3250      # Get the parameters.      # Get the parameters.
3251      my ($self, $featureID, $function, $userID) = @_;      my ($self, $featureID, $function, $userID) = @_;
3252      # 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.
3253      my @bbhFeatures = $self->GetFlat(['IsBidirectionalBestHitOf'],      my $bbhData = FIGRules::BBHData($featureID);
3254                                       "IsBidirectionalBestHitOf(from-link) = ?", [$featureID],      my @bbhFeatures = map { $_->[0] } @$bbhData;
                                      'IsBidirectionalBestHitOf(to-link)');  
3255      # 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
3256      # functional assignment.      # functional assignment.
3257      my @retVal = ();      my @retVal = ();
# Line 2647  Line 3269 
3269    
3270  =head3 TaxonomySort  =head3 TaxonomySort
3271    
3272  C<< my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs); >>      my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs);
3273    
3274  Return a list formed by sorting the specified features by the taxonomy of the containing  Return a list formed by sorting the specified features by the taxonomy of the containing
3275  genome. This will cause genomes from similar organisms to float close to each other.  genome. This will cause genomes from similar organisms to float close to each other.
# Line 2682  Line 3304 
3304          my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",          my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",
3305                                          [$fid], 'Genome(taxonomy)');                                          [$fid], 'Genome(taxonomy)');
3306          # Add this feature to the hash buffer.          # Add this feature to the hash buffer.
3307          Tracer::AddToListMap(\%hashBuffer, $taxonomy, $fid);          push @{$hashBuffer{$taxonomy}}, $fid;
3308      }      }
3309      # Sort the keys and get the elements.      # Sort the keys and get the elements.
3310      my @retVal = ();      my @retVal = ();
# Line 2693  Line 3315 
3315      return @retVal;      return @retVal;
3316  }  }
3317    
 =head3 GetAll  
   
 C<< my @list = $sprout->GetAll(\@objectNames, $filterClause, \@parameters, \@fields, $count); >>  
   
 Return a list of values taken from the objects returned by a query. The first three  
 parameters correspond to the parameters of the L</Get> method. The final parameter is  
 a list of the fields desired from each record found by the query. The field name  
 syntax is the standard syntax used for fields in the B<ERDB> system--  
 B<I<objectName>(I<fieldName>)>-- where I<objectName> is the name of the relevant entity  
 or relationship and I<fieldName> is the name of the field.  
   
 The list returned will be a list of lists. Each element of the list will contain  
 the values returned for the fields specified in the fourth parameter. If one of the  
 fields specified returns multiple values, they are flattened in with the rest. For  
 example, the following call will return a list of the features in a particular  
 spreadsheet cell, and each feature will be represented by a list containing the  
 feature ID followed by all of its aliases.  
   
 C<< $query = $sprout->Get(['ContainsFeature', 'Feature'], "ContainsFeature(from-link) = ?", [$ssCellID], ['Feature(id)', 'Feature(alias)']); >>  
   
 =over 4  
   
 =item objectNames  
   
 List containing the names of the entity and relationship objects to be retrieved.  
   
 =item filterClause  
   
 WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can  
 be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form  
 B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the  
 parameter list as additional parameters. The fields in a filter clause can come from primary  
 entity relations, relationship relations, or secondary entity relations; however, all of the  
 entities and relationships involved must be included in the list of object names.  
   
 =item parameterList  
   
 List of the parameters to be substituted in for the parameters marks in the filter clause.  
   
 =item fields  
   
 List of the fields to be returned in each element of the list returned.  
   
 =item count  
   
 Maximum number of records to return. If omitted or 0, all available records will be returned.  
   
 =item RETURN  
   
 Returns a list of list references. Each element of the return list contains the values for the  
 fields specified in the B<fields> parameter.  
   
 =back  
   
 =cut  
 #: Return Type @@;  
 sub GetAll {  
     # Get the parameters.  
     my ($self, $objectNames, $filterClause, $parameterList, $fields, $count) = @_;  
     # Call the ERDB method.  
     my @retVal = $self->{_erdb}->GetAll($objectNames, $filterClause, $parameterList,  
                                         $fields, $count);  
     # Return the resulting list.  
     return @retVal;  
 }  
   
 =head3 GetFlat  
   
 C<< my @list = $sprout->GetFlat(\@objectNames, $filterClause, $parameterList, $field); >>  
   
 This is a variation of L</GetAll> that asks for only a single field per record and  
 returns a single flattened list.  
   
 =over 4  
   
 =item objectNames  
   
 List containing the names of the entity and relationship objects to be retrieved.  
   
 =item filterClause  
   
 WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can  
 be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form  
 B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the  
 parameter list as additional parameters. The fields in a filter clause can come from primary  
 entity relations, relationship relations, or secondary entity relations; however, all of the  
 entities and relationships involved must be included in the list of object names.  
   
 =item parameterList  
   
 List of the parameters to be substituted in for the parameters marks in the filter clause.  
   
 =item field  
   
 Name of the field to be used to get the elements of the list returned.  
   
 =item RETURN  
   
 Returns a list of values.  
   
 =back  
   
 =cut  
 #: Return Type @;  
 sub GetFlat {  
     # Get the parameters.  
     my ($self, $objectNames, $filterClause, $parameterList, $field) = @_;  
     # Construct the query.  
     my $query = $self->Get($objectNames, $filterClause, $parameterList);  
     # Create the result list.  
     my @retVal = ();  
     # Loop through the records, adding the field values found to the result list.  
     while (my $row = $query->Fetch()) {  
         push @retVal, $row->Value($field);  
     }  
     # Return the list created.  
     return @retVal;  
 }  
   
3318  =head3 Protein  =head3 Protein
3319    
3320  C<< my $protein = Sprout::Protein($sequence, $table); >>      my $protein = Sprout::Protein($sequence, $table);
3321    
3322  Translate a DNA sequence into a protein sequence.  Translate a DNA sequence into a protein sequence.
3323    
# Line 2884  Line 3387 
3387      # Loop through the input triples.      # Loop through the input triples.
3388      my $n = length $sequence;      my $n = length $sequence;
3389      for (my $i = 0; $i < $n; $i += 3) {      for (my $i = 0; $i < $n; $i += 3) {
3390          # Get the current triple from the sequence.          # Get the current triple from the sequence. Note we convert to
3391          my $triple = substr($sequence, $i, 3);          # upper case to insure a match.
3392            my $triple = uc substr($sequence, $i, 3);
3393          # Translate it using the table.          # Translate it using the table.
3394          my $protein = "X";          my $protein = "X";
3395          if (exists $table->{$triple}) { $protein = $table->{$triple}; }          if (exists $table->{$triple}) { $protein = $table->{$triple}; }
# Line 2899  Line 3403 
3403    
3404  =head3 LoadInfo  =head3 LoadInfo
3405    
3406  C<< my ($dirName, @relNames) = $sprout->LoadInfo(); >>      my ($dirName, @relNames) = $sprout->LoadInfo();
3407    
3408  Return the name of the directory from which data is to be loaded and a list of the relation  Return the name of the directory from which data is to be loaded and a list of the relation
3409  names. This information is useful when trying to analyze what needs to be put where in order  names. This information is useful when trying to analyze what needs to be put where in order
# Line 2913  Line 3417 
3417      # 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.
3418      my @retVal = ($self->{_options}->{dataDir});      my @retVal = ($self->{_options}->{dataDir});
3419      # Concatenate the table names.      # Concatenate the table names.
3420      push @retVal, $self->{_erdb}->GetTableNames();      push @retVal, $self->GetTableNames();
3421      # Return the result.      # Return the result.
3422      return @retVal;      return @retVal;
3423  }  }
3424    
3425  =head3 LowBBHs  =head3 BBHMatrix
3426    
3427  C<< my %bbhMap = $sprout->GoodBBHs($featureID, $cutoff); >>      my $bbhMap = $sprout->BBHMatrix($genomeID, $cutoff, @targets);
3428    
3429  Return the bidirectional best hits of a feature whose score is no greater than a  Find all the bidirectional best hits for the features of a genome in a
3430  specified cutoff value. A higher cutoff value will allow inclusion of hits with  specified list of target genomes. The return value will be a hash mapping
3431  a greater score. The value returned is a map of feature IDs to scores.  features in the original genome to their bidirectional best hits in the
3432    target genomes.
3433    
3434  =over 4  =over 4
3435    
3436  =item featureID  =item genomeID
3437    
3438  ID of the feature whose best hits are desired.  ID of the genome whose features are to be examined for bidirectional best hits.
3439    
3440  =item cutoff  =item cutoff
3441    
3442  Maximum permissible score for inclusion in the results.  A cutoff value. Only hits with a score lower than the cutoff will be returned.
3443    
3444    =item targets
3445    
3446    List of target genomes. Only pairs originating in the original
3447    genome and landing in one of the target genomes will be returned.
3448    
3449  =item RETURN  =item RETURN
3450    
3451  Returns a hash mapping feature IDs to scores.  Returns a reference to a hash mapping each feature in the original genome
3452    to a sub-hash mapping its BBH pegs in the target genomes to their scores.
3453    
3454  =back  =back
3455    
3456  =cut  =cut
3457  #: Return Type %;  
3458  sub LowBBHs {  sub BBHMatrix {
3459      # Get the parsameters.      # Get the parameters.
3460      my ($self, $featureID, $cutoff) = @_;      my ($self, $genomeID, $cutoff, @targets) = @_;
3461      # Create the return hash.      # Declare the return variable.
3462      my %retVal = ();      my %retVal = ();
3463      # Create a query to get the desired BBHs.      # Ask for the BBHs.
3464      my @bbhList = $self->GetAll(['IsBidirectionalBestHitOf'],      my @bbhList = FIGRules::BatchBBHs("fig|$genomeID.%", $cutoff, @targets);
3465                                  'IsBidirectionalBestHitOf(sc) <= ? AND IsBidirectionalBestHitOf(from-link) = ?',      Trace("Retrieved " . scalar(@bbhList) . " BBH results.") if T(3);
3466                                  [$cutoff, $featureID],      # We now have a set of 4-tuples that we need to convert into a hash of hashes.
3467                                  ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(sc)']);      for my $bbhData (@bbhList) {
3468      # Form the results into the return hash.          my ($peg1, $peg2, $score) = @{$bbhData};
3469      for my $pair (@bbhList) {          if (! exists $retVal{$peg1}) {
3470          $retVal{$pair->[0]} = $pair->[1];              $retVal{$peg1} = { $peg2 => $score };
3471            } else {
3472                $retVal{$peg1}->{$peg2} = $score;
3473            }
3474      }      }
3475      # Return the result.      # Return the result.
3476      return %retVal;      return \%retVal;
3477  }  }
3478    
 =head3 GetGroups  
3479    
3480  C<< my %groups = $sprout->GetGroups(\@groupList); >>  =head3 SimMatrix
3481    
3482  Return a hash mapping each group to the IDs of the genomes in the group.      my %simMap = $sprout->SimMatrix($genomeID, $cutoff, @targets);
 A list of groups may be specified, in which case only those groups will be  
 shown. Alternatively, if no parameter is supplied, all groups will be  
 included. Genomes that are not in any group are omitted.  
3483    
3484  =cut  Find all the similarities for the features of a genome in a
3485  #: Return Type %@;  specified list of target genomes. The return value will be a hash mapping
3486  sub GetGroups {  features in the original genome to their similarites in the
3487      # Get the parameters.  target genomes.
3488      my ($self, $groupList) = @_;  
3489      # Declare the return value.  =over 4
3490      my %retVal = ();  
3491      # Determine whether we are getting all the groups or just some.  =item genomeID
3492      if (defined $groupList) {  
3493          # Here we have a group list. Loop through them individually,  ID of the genome whose features are to be examined for similarities.
3494          # getting a list of the relevant genomes.  
3495          for my $group (@{$groupList}) {  =item cutoff
3496              my @genomeIDs = $self->GetFlat(['Genome'], "Genome(group-name) = ?",  
3497                  [$group], "Genome(id)");  A cutoff value. Only hits with a score lower than the cutoff will be returned.
3498              $retVal{$group} = \@genomeIDs;  
3499    =item targets
3500    
3501    List of target genomes. Only pairs originating in the original
3502    genome and landing in one of the target genomes will be returned.
3503    
3504    =item RETURN
3505    
3506    Returns a hash mapping each feature in the original genome to a hash mapping its
3507    similar pegs in the target genomes to their scores.
3508    
3509    =back
3510    
3511    =cut
3512    
3513    sub SimMatrix {
3514        # Get the parameters.
3515        my ($self, $genomeID, $cutoff, @targets) = @_;
3516        # Declare the return variable.
3517        my %retVal = ();
3518        # Get the list of features in the source organism.
3519        my @fids = $self->FeaturesOf($genomeID);
3520        # Ask for the sims. We only want similarities to fig features.
3521        my $simList = FIGRules::GetNetworkSims($self, \@fids, {}, 1000, $cutoff, "fig");
3522        if (! defined $simList) {
3523            Confess("Unable to retrieve similarities from server.");
3524        } else {
3525            Trace("Processing sims.") if T(3);
3526            # We now have a set of sims that we need to convert into a hash of hashes. First, we
3527            # Create a hash for the target genomes.
3528            my %targetHash = map { $_ => 1 } @targets;
3529            for my $simData (@{$simList}) {
3530                # Get the PEGs and the score.
3531                my ($peg1, $peg2, $score) = ($simData->id1, $simData->id2, $simData->psc);
3532                # Insure the second ID is in the target list.
3533                my ($genome2) = FIGRules::ParseFeatureID($peg2);
3534                if (exists $targetHash{$genome2}) {
3535                    # Here it is. Now we need to add it to the return hash. How we do that depends
3536                    # on whether or not $peg1 is new to us.
3537                    if (! exists $retVal{$peg1}) {
3538                        $retVal{$peg1} = { $peg2 => $score };
3539                    } else {
3540                        $retVal{$peg1}->{$peg2} = $score;
3541                    }
3542                }
3543            }
3544        }
3545        # Return the result.
3546        return %retVal;
3547    }
3548    
3549    
3550    =head3 LowBBHs
3551    
3552        my %bbhMap = $sprout->LowBBHs($featureID, $cutoff);
3553    
3554    Return the bidirectional best hits of a feature whose score is no greater than a
3555    specified cutoff value. A higher cutoff value will allow inclusion of hits with
3556    a greater score. The value returned is a map of feature IDs to scores.
3557    
3558    =over 4
3559    
3560    =item featureID
3561    
3562    ID of the feature whose best hits are desired.
3563    
3564    =item cutoff
3565    
3566    Maximum permissible score for inclusion in the results.
3567    
3568    =item RETURN
3569    
3570    Returns a hash mapping feature IDs to scores.
3571    
3572    =back
3573    
3574    =cut
3575    #: Return Type %;
3576    sub LowBBHs {
3577        # Get the parsameters.
3578        my ($self, $featureID, $cutoff) = @_;
3579        # Create the return hash.
3580        my %retVal = ();
3581        # Query for the desired BBHs.
3582        my $bbhList = FIGRules::BBHData($featureID, $cutoff);
3583        # Form the results into the return hash.
3584        for my $pair (@$bbhList) {
3585            my $fid = $pair->[0];
3586            if ($self->Exists('Feature', $fid)) {
3587                $retVal{$fid} = $pair->[1];
3588            }
3589        }
3590        # Return the result.
3591        return %retVal;
3592    }
3593    
3594    =head3 Sims
3595    
3596        my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters);
3597    
3598    Get a list of similarities for a specified feature. Similarity information is not kept in the
3599    Sprout database; rather, they are retrieved from a network server. The similarities are
3600    returned as B<Sim> objects. A Sim object is actually a list reference that has been blessed
3601    so that its elements can be accessed by name.
3602    
3603    Similarities can be either raw or expanded. The raw similarities are basic
3604    hits between features with similar DNA. Expanding a raw similarity drags in any
3605    features considered substantially identical. So, for example, if features B<A1>,
3606    B<A2>, and B<A3> are all substantially identical to B<A>, then a raw similarity
3607    B<[C,A]> would be expanded to B<[C,A] [C,A1] [C,A2] [C,A3]>.
3608    
3609    =over 4
3610    
3611    =item fid
3612    
3613    ID of the feature whose similarities are desired, or reference to a list of IDs
3614    of features whose similarities are desired.
3615    
3616    =item maxN
3617    
3618    Maximum number of similarities to return.
3619    
3620    =item maxP
3621    
3622    Minumum allowable similarity score.
3623    
3624    =item select
3625    
3626    Selection criterion: C<raw> means only raw similarities are returned; C<fig>
3627    means only similarities to FIG features are returned; C<all> means all expanded
3628    similarities are returned; and C<figx> means similarities are expanded until the
3629    number of FIG features equals the maximum.
3630    
3631    =item max_expand
3632    
3633    The maximum number of features to expand.
3634    
3635    =item filters
3636    
3637    Reference to a hash containing filter information, or a subroutine that can be
3638    used to filter the sims.
3639    
3640    =item RETURN
3641    
3642    Returns a reference to a list of similarity objects, or C<undef> if an error
3643    occurred.
3644    
3645    =back
3646    
3647    =cut
3648    
3649    sub Sims {
3650        # Get the parameters.
3651        my ($self, $fid, $maxN, $maxP, $select, $max_expand, $filters) = @_;
3652        # Create the shim object to test for deleted FIDs.
3653        my $shim = FidCheck->new($self);
3654        # Ask the network for sims.
3655        my $retVal = FIGRules::GetNetworkSims($shim, $fid, {}, $maxN, $maxP, $select, $max_expand, $filters);
3656        # Return the result.
3657        return $retVal;
3658    }
3659    
3660    =head3 IsAllGenomes
3661    
3662        my $flag = $sprout->IsAllGenomes(\@list, \@checkList);
3663    
3664    Return TRUE if all genomes in the second list are represented in the first list at
3665    least one. Otherwise, return FALSE. If the second list is omitted, the first list is
3666    compared to a list of all the genomes.
3667    
3668    =over 4
3669    
3670    =item list
3671    
3672    Reference to the list to be compared to the second list.
3673    
3674    =item checkList (optional)
3675    
3676    Reference to the comparison target list. Every genome ID in this list must occur at
3677    least once in the first list. If this parameter is omitted, a list of all the genomes
3678    is used.
3679    
3680    =item RETURN
3681    
3682    Returns TRUE if every item in the second list appears at least once in the
3683    first list, else FALSE.
3684    
3685    =back
3686    
3687    =cut
3688    
3689    sub IsAllGenomes {
3690        # Get the parameters.
3691        my ($self, $list, $checkList) = @_;
3692        # Supply the checklist if it was omitted.
3693        $checkList = [$self->Genomes()] if ! defined($checkList);
3694        # Create a hash of the original list.
3695        my %testList = map { $_ => 1 } @{$list};
3696        # Declare the return variable. We assume that the representation
3697        # is complete and stop at the first failure.
3698        my $retVal = 1;
3699        my $n = scalar @{$checkList};
3700        for (my $i = 0; $retVal && $i < $n; $i++) {
3701            if (! $testList{$checkList->[$i]}) {
3702                $retVal = 0;
3703            }
3704        }
3705        # Return the result.
3706        return $retVal;
3707    }
3708    
3709    =head3 GetGroups
3710    
3711        my %groups = $sprout->GetGroups(\@groupList);
3712    
3713    Return a hash mapping each group to the IDs of the genomes in the group.
3714    A list of groups may be specified, in which case only those groups will be
3715    shown. Alternatively, if no parameter is supplied, all groups will be
3716    included. Genomes that are not in any group are omitted.
3717    
3718    =cut
3719    #: Return Type %@;
3720    sub GetGroups {
3721        # Get the parameters.
3722        my ($self, $groupList) = @_;
3723        # Declare the return value.
3724        my %retVal = ();
3725        # Determine whether we are getting all the groups or just some.
3726        if (defined $groupList) {
3727            # Here we have a group list. Loop through them individually,
3728            # getting a list of the relevant genomes.
3729            for my $group (@{$groupList}) {
3730                my @genomeIDs = $self->GetFlat(['Genome'], "Genome(primary-group) = ?",
3731                    [$group], "Genome(id)");
3732                $retVal{$group} = \@genomeIDs;
3733          }          }
3734      } else {      } else {
3735          # Here we need all of the groups. In this case, we run through all          # Here we need all of the groups. In this case, we run through all
3736          # of the genome records, putting each one found into the appropriate          # of the genome records, putting each one found into the appropriate
3737          # group. Note that we use a filter clause to insure that only genomes          # group. Note that we use a filter clause to insure that only genomes
3738          # in groups are included in the return set.          # in real NMPDR groups are included in the return set.
3739          my @genomes = $self->GetAll(['Genome'], "Genome(group-name) > ' '", [],          my @genomes = $self->GetAll(['Genome'], "Genome(primary-group) <> ?",
3740                                      ['Genome(id)', 'Genome(group-name)']);                                      [$FIG_Config::otherGroup], ['Genome(id)', 'Genome(primary-group)']);
3741          # Loop through the genomes found.          # Loop through the genomes found.
3742          for my $genome (@genomes) {          for my $genome (@genomes) {
3743              # Pop this genome's ID off the current list.              # Get the genome ID and group, and add this genome to the group's list.
3744              my @groups = @{$genome};              my ($genomeID, $group) = @{$genome};
3745              my $genomeID = shift @groups;              push @{$retVal{$group}}, $genomeID;
             # Loop through the groups, adding the genome ID to each group's  
             # list.  
             for my $group (@groups) {  
                 Tracer::AddToListMap(\%retVal, $group, $genomeID);  
             }  
3746          }          }
3747      }      }
3748      # Return the hash we just built.      # Return the hash we just built.
# Line 3012  Line 3751 
3751    
3752  =head3 MyGenomes  =head3 MyGenomes
3753    
3754  C<< my @genomes = Sprout::MyGenomes($dataDir); >>      my @genomes = Sprout::MyGenomes($dataDir);
3755    
3756  Return a list of the genomes to be included in the Sprout.  Return a list of the genomes to be included in the Sprout.
3757    
# Line 3044  Line 3783 
3783    
3784  =head3 LoadFileName  =head3 LoadFileName
3785    
3786  C<< my $fileName = Sprout::LoadFileName($dataDir, $tableName); >>      my $fileName = Sprout::LoadFileName($dataDir, $tableName);
3787    
3788  Return the name of the load file for the specified table in the specified data  Return the name of the load file for the specified table in the specified data
3789  directory.  directory.
# Line 3083  Line 3822 
3822      return $retVal;      return $retVal;
3823  }  }
3824    
3825    =head3 DeleteGenome
3826    
3827        my $stats = $sprout->DeleteGenome($genomeID, $testFlag);
3828    
3829    Delete a genome from the database.
3830    
3831    =over 4
3832    
3833    =item genomeID
3834    
3835    ID of the genome to delete
3836    
3837    =item testFlag
3838    
3839    If TRUE, then the DELETE statements will be traced, but no deletions will occur.
3840    
3841    =item RETURN
3842    
3843    Returns a statistics object describing the rows deleted.
3844    
3845    =back
3846    
3847    =cut
3848    #: Return Type $%;
3849    sub DeleteGenome {
3850        # Get the parameters.
3851        my ($self, $genomeID, $testFlag) = @_;
3852        # Perform the delete for the genome's features.
3853        my $retVal = $self->Delete('Feature', "fig|$genomeID.%", testMode => $testFlag);
3854        # Perform the delete for the primary genome data.
3855        my $stats = $self->Delete('Genome', $genomeID, testMode => $testFlag);
3856        $retVal->Accumulate($stats);
3857        # Return the result.
3858        return $retVal;
3859    }
3860    
3861    =head3 Fix
3862    
3863        my %fixedHash = $sprout->Fix(%groupHash);
3864    
3865    Prepare a genome group hash (like that returned by L</GetGroups>) for processing.
3866    The groups will be combined into the appropriate super-groups.
3867    
3868    =over 4
3869    
3870    =item groupHash
3871    
3872    Hash to be fixed up.
3873    
3874    =item RETURN
3875    
3876    Returns a fixed-up version of the hash.
3877    
3878    =back
3879    
3880    =cut
3881    
3882    sub Fix {
3883        # Get the parameters.
3884        my ($self, %groupHash) = @_;
3885        # Create the result hash.
3886        my %retVal = ();
3887        # Copy over the genomes.
3888        for my $groupID (keys %groupHash) {
3889            # Get the super-group name.
3890            my $realGroupID = $self->SuperGroup($groupID);
3891            # Append this group's genomes into the result hash
3892            # using the super-group name.
3893            push @{$retVal{$realGroupID}}, @{$groupHash{$groupID}};
3894        }
3895        # Return the result hash.
3896        return %retVal;
3897    }
3898    
3899    =head3 GroupPageName
3900    
3901        my $name = $sprout->GroupPageName($group);
3902    
3903    Return the name of the page for the specified NMPDR group.
3904    
3905    =over 4
3906    
3907    =item group
3908    
3909    Name of the relevant group.
3910    
3911    =item RETURN
3912    
3913    Returns the relative page name (e.g. C<../content/campy.php>). If the group file is not in
3914    memory it will be read in.
3915    
3916    =back
3917    
3918    =cut
3919    
3920    sub GroupPageName {
3921        # Get the parameters.
3922        my ($self, $group) = @_;
3923        # Check for the group file data.
3924        my %superTable = $self->CheckGroupFile();
3925        # Compute the real group name.
3926        my $realGroup = $self->SuperGroup($group);
3927        # Get the associated page name.
3928        my $retVal = "../content/$superTable{$realGroup}->{page}";
3929        # Return the result.
3930        return $retVal;
3931    }
3932    
3933    
3934    =head3 AddProperty
3935    
3936        $sprout->AddProperty($featureID, $key, @values);
3937    
3938    Add a new attribute value (Property) to a feature.
3939    
3940    =over 4
3941    
3942    =item peg
3943    
3944    ID of the feature to which the attribute is to be added.
3945    
3946    =item key
3947    
3948    Name of the attribute (key).
3949    
3950    =item values
3951    
3952    Values of the attribute.
3953    
3954    =back
3955    
3956    =cut
3957    #: Return Type ;
3958    sub AddProperty {
3959        # Get the parameters.
3960        my ($self, $featureID, $key, @values) = @_;
3961        # Add the property using the attached attributes object.
3962        $self->ca->AddAttribute($featureID, $key, @values);
3963    }
3964    
3965    =head3 CheckGroupFile
3966    
3967        my %groupData = $sprout->CheckGroupFile();
3968    
3969    Get the group file hash. The group file hash describes the relationship
3970    between a group and the super-group to which it belongs for purposes of
3971    display. The super-group name is computed from the first capitalized word
3972    in the actual group name. For each super-group, the group file contains
3973    the page name and a list of the species expected to be in the group.
3974    Each species is specified by a genus and a species name. A species name
3975    of C<0> implies an entire genus.
3976    
3977    This method returns a hash from super-group names to a hash reference. Each
3978    resulting hash reference contains the following fields.
3979    
3980    =over 4
3981    
3982    =item specials
3983    
3984    Reference to a hash whose keys are the names of special species.
3985    
3986    =item contents
3987    
3988    A list of 2-tuples, each containing a genus name followed by a species name
3989    (or 0, indicating all species). This list indicates which organisms belong
3990    in the super-group.
3991    
3992    =back
3993    
3994    =cut
3995    
3996    sub CheckGroupFile {
3997        # Get the parameters.
3998        my ($self) = @_;
3999        # Check to see if we already have this hash.
4000        if (! defined $self->{groupHash}) {
4001            # We don't, so we need to read it in.
4002            my %groupHash;
4003            # Read the group file.
4004            my @groupLines = Tracer::GetFile("$FIG_Config::sproutData/groups.tbl");
4005            # Loop through the list of sort-of groups.
4006            for my $groupLine (@groupLines) {
4007                my ($name, $specials, @contents) = split /\t/, $groupLine;
4008                $groupHash{$name} = { specials => { map { $_ => 1 } split /\s*,\s*/, $specials },
4009                                      contents => [ map { [ split /\s*,\s*/, $_ ] } @contents ]
4010                                    };
4011            }
4012            # Save the hash.
4013            $self->{groupHash} = \%groupHash;
4014        }
4015        # Return the result.
4016        return %{$self->{groupHash}};
4017    }
4018    
4019    =head2 Virtual Methods
4020    
4021    =head3 CleanKeywords
4022    
4023        my $cleanedString = $sprout->CleanKeywords($searchExpression);
4024    
4025    Clean up a search expression or keyword list. This involves converting the periods
4026    in EC numbers to underscores, converting non-leading minus signs to underscores,
4027    a vertical bar or colon to an apostrophe, and forcing lower case for all alphabetic
4028    characters. In addition, any extra spaces are removed.
4029    
4030    =over 4
4031    
4032    =item searchExpression
4033    
4034    Search expression or keyword list to clean. Note that a search expression may
4035    contain boolean operators which need to be preserved. This includes leading
4036    minus signs.
4037    
4038    =item RETURN
4039    
4040    Cleaned expression or keyword list.
4041    
4042    =back
4043    
4044    =cut
4045    
4046    sub CleanKeywords {
4047        # Get the parameters.
4048        my ($self, $searchExpression) = @_;
4049        # Get the stemmer.
4050        my $stemmer = $self->GetStemmer();
4051        # Convert the search expression using the stemmer.
4052        my $retVal = $stemmer->PrepareSearchExpression($searchExpression);
4053        Trace("Cleaned keyword list for \"$searchExpression\" is \"$retVal\".") if T(3);
4054        # Return the result.
4055        return $retVal;
4056    }
4057    
4058    =head3 GetSourceObject
4059    
4060        my $source = $erdb->GetSourceObject();
4061    
4062    Return the object to be used in creating load files for this database.
4063    
4064    =cut
4065    
4066    sub GetSourceObject {
4067        # Get the parameters.
4068        my ($self) = @_;
4069        # Check to see if we already have a source object.
4070        my $retVal = $self->{_fig};
4071        if (! defined $retVal) {
4072            # No, so create one.
4073            require FIG;
4074            $retVal = FIG->new();
4075        }
4076        # Return the object.
4077        return $retVal;
4078    }
4079    
4080    =head3 SectionList
4081    
4082        my @sections = $erdb->SectionList();
4083    
4084    Return a list of the names for the different data sections used when loading this database.
4085    The default is a single string, in which case there is only one section representing the
4086    entire database.
4087    
4088    =cut
4089    
4090    sub SectionList {
4091        # Get the parameters.
4092        my ($self, $source) = @_;
4093        # Ask the BaseSproutLoader for a section list.
4094        require BaseSproutLoader;
4095        my @retVal = BaseSproutLoader::GetSectionList($self);
4096        # Return the list.
4097        return @retVal;
4098    }
4099    
4100    =head3 Loader
4101    
4102        my $groupLoader = $erdb->Loader($groupName, $options);
4103    
4104    Return an [[ERDBLoadGroupPm]] object for the specified load group. This method is used
4105    by [[ERDBGeneratorPl]] to create the load group objects. If you are not using
4106    [[ERDBGeneratorPl]], you don't need to override this method.
4107    
4108    =over 4
4109    
4110    =item groupName
4111    
4112    Name of the load group whose object is to be returned. The group name is
4113    guaranteed to be a single word with only the first letter capitalized.
4114    
4115    =item options
4116    
4117    Reference to a hash of command-line options.
4118    
4119    =item RETURN
4120    
4121    Returns an [[ERDBLoadGroupPm]] object that can be used to process the specified load group
4122    for this database.
4123    
4124    =back
4125    
4126    =cut
4127    
4128    sub Loader {
4129        # Get the parameters.
4130        my ($self, $groupName, $options) = @_;
4131        # Compute the loader name.
4132        my $loaderClass = "${groupName}SproutLoader";
4133        # Pull in its definition.
4134        require "$loaderClass.pm";
4135        # Create an object for it.
4136        my $retVal = eval("$loaderClass->new(\$self, \$options)");
4137        # Insure it worked.
4138        Confess("Could not create $loaderClass object: $@") if $@;
4139        # Return it to the caller.
4140        return $retVal;
4141    }
4142    
4143    
4144    =head3 LoadGroupList
4145    
4146        my @groups = $erdb->LoadGroupList();
4147    
4148    Returns a list of the names for this database's load groups. This method is used
4149    by [[ERDBGeneratorPl]] when the user wishes to load all table groups. The default
4150    is a single group called 'All' that loads everything.
4151    
4152    =cut
4153    
4154    sub LoadGroupList {
4155        # Return the list.
4156        return qw(Genome Subsystem Annotation Property Source Reaction Synonym Feature Drug);
4157    }
4158    
4159    =head3 LoadDirectory
4160    
4161        my $dirName = $erdb->LoadDirectory();
4162    
4163    Return the name of the directory in which load files are kept. The default is
4164    the FIG temporary directory, which is a really bad choice, but it's always there.
4165    
4166    =cut
4167    
4168    sub LoadDirectory {
4169        # Get the parameters.
4170        my ($self) = @_;
4171        # Return the directory name.
4172        return $self->{dataDir};
4173    }
4174    
4175  =head2 Internal Utility Methods  =head2 Internal Utility Methods
4176    
4177    =head3 GetStemmer
4178    
4179        my $stermmer = $sprout->GetStemmer();
4180    
4181    Return the stemmer object for this database.
4182    
4183    =cut
4184    
4185    sub GetStemmer {
4186        # Get the parameters.
4187        my ($self) = @_;
4188        # Declare the return variable.
4189        my $retVal = $self->{stemmer};
4190        if (! defined $retVal) {
4191            # We don't have one pre-built, so we build and save it now.
4192            $retVal = BioWords->new(exceptions => "$FIG_Config::sproutData/Exceptions.txt",
4193                                     stops => "$FIG_Config::sproutData/StopWords.txt",
4194                                     cache => 0);
4195            $self->{stemmer} = $retVal;
4196        }
4197        # Return the result.
4198        return $retVal;
4199    }
4200    
4201  =head3 ParseAssignment  =head3 ParseAssignment
4202    
4203  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,
# Line 3093  Line 4206 
4206    
4207  A functional assignment is always of the form  A functional assignment is always of the form
4208    
4209      I<XXXX>C<\nset >I<YYYY>C< function to\n>I<ZZZZZ>      set YYYY function to
4210        ZZZZ
4211    
4212    where I<YYYY> is the B<user>, and I<ZZZZ> is the actual functional role. In most cases,
4213    the user and the assigning user (from MadeAnnotation) will be the same, but that is
4214    not always the case.
4215    
4216  where I<XXXX> is the B<assigning user>, I<YYYY> is the B<user>, and I<ZZZZ> is the  In addition, the functional role may contain extra data that is stripped, such as
4217  actual functional role. In most cases, the user and the assigning user will be the  terminating spaces or a comment separated from the rest of the text by a tab.
 same, but that is not always the case.  
4218    
4219  This is a static method.  This is a static method.
4220    
4221  =over 4  =over 4
4222    
4223    =item user
4224    
4225    Name of the assigning user.
4226    
4227  =item text  =item text
4228    
4229  Text of the annotation.  Text of the annotation.
# Line 3118  Line 4239 
4239    
4240  sub _ParseAssignment {  sub _ParseAssignment {
4241      # Get the parameters.      # Get the parameters.
4242      my ($text) = @_;      my ($user, $text) = @_;
4243      # Declare the return value.      # Declare the return value.
4244      my @retVal = ();      my @retVal = ();
4245      # Check to see if this is a functional assignment.      # Check to see if this is a functional assignment.
4246      my ($user, $type, $function) = split(/\n/, $text);      my ($type, $function) = split(/\n/, $text);
4247      if ($type =~ m/^set ([^ ]+) function to$/i) {      if ($type =~ m/^set function to$/i) {
4248          # Here it is, so we return the user name (which is in $1), the functional role text,          # Here we have an assignment without a user, so we use the incoming user ID.
4249          # and the assigning user.          @retVal = ($user, $function);
4250          @retVal = ($1, $function, $user);      } elsif ($type =~ m/^set (\S+) function to$/i) {
4251            # Here we have an assignment with a user that is passed back to the caller.
4252            @retVal = ($1, $function);
4253        }
4254        # If we have an assignment, we need to clean the function text. There may be
4255        # extra junk at the end added as a note from the user.
4256        if (defined( $retVal[1] )) {
4257            $retVal[1] =~ s/(\t\S)?\s*$//;
4258      }      }
4259      # Return the result list.      # Return the result list.
4260      return @retVal;      return @retVal;
4261  }  }
4262    
4263    =head3 _CheckFeature
4264    
4265        my $flag = $sprout->_CheckFeature($fid);
4266    
4267    Return TRUE if the specified FID is probably an NMPDR feature ID, else FALSE.
4268    
4269    =over 4
4270    
4271    =item fid
4272    
4273    Feature ID to check.
4274    
4275    =item RETURN
4276    
4277    Returns TRUE if the FID is for one of the NMPDR genomes, else FALSE.
4278    
4279    =back
4280    
4281    =cut
4282    
4283    sub _CheckFeature {
4284        # Get the parameters.
4285        my ($self, $fid) = @_;
4286        # Insure we have a genome hash.
4287        my $genomes = $self->_GenomeHash();
4288        # Get the feature's genome ID.
4289        my ($genomeID) = FIGRules::ParseFeatureID($fid);
4290        # Return an indicator of whether or not the genome ID is in the hash.
4291        return ($self->{genomeHash}->{$genomeID} ? 1 : 0);
4292    }
4293    
4294  =head3 FriendlyTimestamp  =head3 FriendlyTimestamp
4295    
4296  Convert a time number to a user-friendly time stamp for display.  Convert a time number to a user-friendly time stamp for display.
# Line 3154  Line 4313 
4313    
4314  sub FriendlyTimestamp {  sub FriendlyTimestamp {
4315      my ($timeValue) = @_;      my ($timeValue) = @_;
4316      my $retVal = strftime("%a %b %e %H:%M:%S %Y", localtime($timeValue));      my $retVal = localtime($timeValue);
4317      return $retVal;      return $retVal;
4318  }  }
4319    
 =head3 AddProperty  
4320    
4321  C<< my  = $sprout->AddProperty($featureID, $key, $value, $url); >>  =head3 Hint
4322    
4323  Add a new attribute value (Property) to a feature. In the SEED system, attributes can      my $htmlText = Sprout::Hint($wikiPage, $hintID);
4324  be added to almost any object. In Sprout, they can only be added to features. In  
4325  Sprout, attributes are implemented using I<properties>. A property represents a key/value  Return the HTML for a help link that displays the specified hint text when it is clicked.
4326  pair. If the particular key/value pair coming in is not already in the database, a new  This HTML can be put in forms to provide a useful hinting mechanism.
 B<Property> record is created to hold it.  
4327    
4328  =over 4  =over 4
4329    
4330  =item peg  =item wikiPage
4331    
4332  ID of the feature to which the attribute is to be replied.  Name of the wiki page to be popped up when the hint mark is clicked.
4333    
4334  =item key  =item hintID
4335    
4336  Name of the attribute (key).  ID of the text to display for the hint. This should correspond to a tip number
4337    in the Wiki.
4338    
4339    =item RETURN
4340    
4341    Returns the html for the hint facility. The resulting html shows the word "help" and
4342    uses the standard FIG popup technology.
4343    
4344    =back
4345    
4346  =item value  =cut
4347    
4348    sub Hint {
4349        # Get the parameters.
4350        my ($wikiPage, $hintID) = @_;
4351        # Declare the return variable.
4352        my $retVal;
4353        # Convert the wiki page name to a URL.
4354        my $wikiURL;
4355        if ($wikiPage =~ m#/#) {
4356            # Here it's a URL of some sort.
4357            $wikiURL = $wikiPage;
4358        } else {
4359            # Here it's a wiki page.
4360            my $page = join("", map { ucfirst $_ } split /\s+/, $wikiPage);
4361            if ($page =~ /^(.+?)\.(.+)$/) {
4362                $page = "$1/$2";
4363            } else {
4364                $page = "FIG/$page";
4365            }
4366            $wikiURL = "$FIG_Config::cgi_url/wiki/view.cgi/$page";
4367        }
4368        # Is there hint text?
4369        if (! $hintID) {
4370            # No. Create a new-page hint.
4371            $retVal = qq(&nbsp;<a class="hint" onclick="doPagePopup(this, '$wikiURL')">(help)</a>);
4372        } else {
4373            # With hint text, we create a popup window hint. We need to compute the hint URL.
4374            my $tipURL = "$FIG_Config::cgi_url/wiki/view.cgi/FIG/TWikiCustomTip" .
4375                Tracer::Pad($hintID, 3, 1, "0");
4376            # Create a hint pop-up link.
4377            $retVal = qq(&nbsp;<a class="hint" onclick="doHintPopup(this, '$wikiURL', '$tipURL')">(help)</a>);
4378        }
4379        # Return the HTML.
4380        return $retVal;
4381    }
4382    
4383    =head3 _GenomeHash
4384    
4385        my $gHash = $sprout->_GenomeHash();
4386    
4387    Return a hash mapping all NMPDR genome IDs to [[ERDBObjectPm]] genome objects.
4388    
4389    =cut
4390    
4391    sub _GenomeHash {
4392        # Get the parameters.
4393        my ($self) = @_;
4394        # Do we already have a filled hash?
4395        if (! $self->{genomeHashFilled}) {
4396            # No, create it.
4397            my %gHash = map { $_->PrimaryValue('id') => $_ } $self->GetList("Genome", "", []);
4398            $self->{genomeHash} = \%gHash;
4399            # Denote we have it.
4400            $self->{genomeHashFilled} = 1;
4401        }
4402        # Return the hash.
4403        return $self->{genomeHash};
4404    }
4405    
4406    =head3 _GenomeData
4407    
4408        my $genomeData = $sprout->_GenomeData($genomeID);
4409    
4410    Return an [[ERDBObjectPm]] object for the specified genome, or an undefined
4411    value if the genome does not exist.
4412    
4413  Value of the attribute.  =over 4
4414    
4415    =item genomeID
4416    
4417  =item url  ID of the desired genome.
4418    
4419  URL or text citation from which the property was obtained.  =item RETURN
4420    
4421    Returns either an [[ERDBObjectPm]] containing the genome, or an undefined value.
4422    If the genome exists, it will have been read into the genome cache.
4423    
4424  =back  =back
4425    
4426  =cut  =cut
4427  #: Return Type ;  
4428  sub AddProperty {  sub _GenomeData {
4429      # Get the parameters.      # Get the parameters.
4430      my ($self, $featureID, $key, $value, $url) = @_;      my ($self, $genomeID) = @_;
4431      # Declare the variable to hold the desired property ID.      # Are we in the genome hash?
4432      my $propID;      if (! exists $self->{genomeHash}->{$genomeID} && ! $self->{genomeHashFilled}) {
4433      # Attempt to find a property record for this key/value pair.          # The genome isn't in the hash, and the hash is not complete, so we try to
4434      my @properties = $self->GetFlat(['Property'],          # read it.
4435                                     "Property(property-name) = ? AND Property(property-value) = ?",          $self->{genomeHash}->{$genomeID} = $self->GetEntity(Genome => $genomeID);
                                    [$key, $value], 'Property(id)');  
     if (@properties) {  
         # Here the property is already in the database. We save its ID.  
         $propID = $properties[0];  
         # Here the property value does not exist. We need to generate an ID. It will be set  
         # to a number one greater than the maximum value in the database. This call to  
         # GetAll will stop after one record.  
         my @maxProperty = $self->GetAll(['Property'], "ORDER BY Property(id) DESC", [], ['Property(id)'],  
                                         1);  
         $propID = $maxProperty[0]->[0] + 1;  
         # Insert the new property value.  
         $self->Insert('Property', { 'property-name' => $key, 'property-value' => $value, id => $propID });  
4436      }      }
4437      # Now we connect the incoming feature to the property.      # Return the result.
4438      $self->Insert('HasProperty', { 'from-link' => $featureID, 'to-link' => $propID, evidence => $url });      return $self->{genomeHash}->{$genomeID};
4439  }  }
4440    
4441    =head3 _CacheGenome
4442    
4443        $sprout->_CacheGenome($genomeID, $genomeData);
4444    
4445    Store the specified genome object in the genome cache if it is already there.
4446    
4447    =over 4
4448    
4449    =item genomeID
4450    
4451    ID of the genome to store in the cache.
4452    
4453    =item genomeData
4454    
4455    An [[ERDBObjectPm]] containing at least the data for the specified genome.
4456    Note that the Genome may not be the primary object in it, so a fully-qualified
4457    field name has to be used to retrieve data from it.
4458    
4459    =back
4460    
4461    =cut
4462    
4463    sub _CacheGenome {
4464        # Get the parameters.
4465        my ($self, $genomeID, $genomeData) = @_;
4466        # Only proceed if we don't already have the genome.
4467        if (! exists $self->{genomeHash}->{$genomeID}) {
4468            $self->{genomeHash}->{$genomeID} = $genomeData;
4469        }
4470    }
4471    
4472  1;  1;

Legend:
Removed from v.1.32  
changed lines
  Added in v.1.125

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3