[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.36, Wed Sep 14 13:52:34 2005 UTC revision 1.115, Sun Sep 7 03:13:32 2008 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 DBQuery;
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;
18        use WikiTools;
19        use base qw(ERDB);
20    
21  =head1 Sprout Database Manipulation Object  =head1 Sprout Database Manipulation Object
22    
# Line 25  Line 29 
29  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>
30  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>.
31    
32  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' });
33    
34  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
35  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
36  query tasks. For example, L</genomes> lists the IDs of all the genomes in the database and  query tasks. For example, L</Genomes> lists the IDs of all the genomes in the database and
37  L</dna_seq> returns the DNA sequence for a specified genome location.  L</DNASeq> returns the DNA sequence for a specified genome location.
38    
39    The Sprout object is a subclass of the ERDB object and inherits all its properties and methods.
40    
41  =cut  =cut
42    
# Line 40  Line 46 
46    
47  =head3 new  =head3 new
48    
49  C<< my $sprout = Sprout->new($dbName, \%options); >>      my $sprout = Sprout->new($dbName, \%options);
50    
51  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
52  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 62  Line 68 
68    
69  * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)  * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)
70    
71  * B<userData> user name and password, delimited by a slash (default C<root/>)  * B<userData> user name and password, delimited by a slash (default same as SEED)
72    
73  * B<port> connection port (default C<0>)  * B<port> connection port (default C<0>)
74    
75    * B<sock> connection socket (default same as SEED)
76    
77  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)
78    
79  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)
80    
81  * B<noDBOpen> suppresses the connection to the database if TRUE, else FALSE  * B<noDBOpen> suppresses the connection to the database if TRUE, else FALSE
82    
83    * B<host> name of the database host
84    
85  =back  =back
86    
87  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
88  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
89  F</usr/fig/SproutData>.  F</usr/fig/SproutData>.
90    
91  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' });
92    
93  =cut  =cut
94    
95  sub new {  sub new {
96      # Get the parameters.      # Get the parameters.
97      my ($class, $dbName, $options) = @_;      my ($class, $dbName, $options) = @_;
98        # Compute the DBD directory.
99        my $dbd_dir = (defined($FIG_Config::dbd_dir) ? $FIG_Config::dbd_dir :
100                                                      $FIG_Config::fig );
101      # 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
102      # the incoming data.      # the incoming data.
103      my $optionTable = Tracer::GetOptions({      my $optionTable = Tracer::GetOptions({
# Line 92  Line 105 
105                                                          # database type                                                          # database type
106                         dataDir      => $FIG_Config::sproutData,                         dataDir      => $FIG_Config::sproutData,
107                                                          # data file directory                                                          # data file directory
108                         xmlFileName  => "$FIG_Config::sproutData/SproutDBD.xml",                         xmlFileName  => "$dbd_dir/SproutDBD.xml",
109                                                          # database definition file name                                                          # database definition file name
110                         userData     => "$FIG_Config::dbuser/$FIG_Config::dbpass",                         userData     => "$FIG_Config::dbuser/$FIG_Config::dbpass",
111                                                          # user name and password                                                          # user name and password
112                         port         => $FIG_Config::dbport,                         port         => $FIG_Config::dbport,
113                                                          # database connection port                                                          # database connection port
114                           sock         => $FIG_Config::dbsock,
115                           host         => $FIG_Config::sprout_host,
116                         maxSegmentLength => 4500,        # maximum feature segment length                         maxSegmentLength => 4500,        # maximum feature segment length
117                         maxSequenceLength => 8000,       # maximum contig sequence length                         maxSequenceLength => 8000,       # maximum contig sequence length
118                         noDBOpen     => 0,               # 1 to suppress the database open                         noDBOpen     => 0,               # 1 to suppress the database open
# Line 110  Line 125 
125      # Connect to the database.      # Connect to the database.
126      my $dbh;      my $dbh;
127      if (! $optionTable->{noDBOpen}) {      if (! $optionTable->{noDBOpen}) {
128            Trace("Connect data: host = $optionTable->{host}, port = $optionTable->{port}.") if T(3);
129          $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,          $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,
130                                  $password, $optionTable->{port});                                  $password, $optionTable->{port}, $optionTable->{host}, $optionTable->{sock});
131      }      }
132      # Create the ERDB object.      # Create the ERDB object.
133      my $xmlFileName = "$optionTable->{xmlFileName}";      my $xmlFileName = "$optionTable->{xmlFileName}";
134      my $erdb = ERDB->new($dbh, $xmlFileName);      my $retVal = ERDB::new($class, $dbh, $xmlFileName);
135      # Create this object.      # Add the option table and XML file name.
136      my $self = { _erdb => $erdb, _options => $optionTable, _xmlName => $xmlFileName };      $retVal->{_options} = $optionTable;
137      # Bless and return it.      $retVal->{_xmlName} = $xmlFileName;
138      bless $self;      # Set up space for the group file data.
139      return $self;      $retVal->{groupHash} = undef;
140        # Set up space for the genome hash. We use this to identify NMPDR genomes.
141        $retVal->{genomeHash} = undef;
142        # Connect to the attributes.
143        if ($FIG_Config::attrURL) {
144            Trace("Remote attribute server $FIG_Config::attrURL chosen.") if T(3);
145            $retVal->{_ca} = RemoteCustomAttributes->new($FIG_Config::attrURL);
146        } elsif ($FIG_Config::attrDbName) {
147            Trace("Local attribute database $FIG_Config::attrDbName chosen.") if T(3);
148            my $user = ($FIG_Config::arch eq 'win' ? 'self' : scalar(getpwent()));
149            $retVal->{_ca} = CustomAttributes->new(user => $user);
150        }
151        # Insure we have access to the stem module.
152        WikiUse('Lingua::Stem');
153        $retVal->{stemmer} = Lingua::Stem->new();
154        $retVal->{stemmer}->stem_caching({ -level => 2 });
155        # Return it.
156        return $retVal;
157    }
158    
159    =head3 CoreGenomes
160    
161        my @genomes = $sprout->CoreGenomes($scope);
162    
163    Return the IDs of NMPDR genomes in the specified scope.
164    
165    =over 4
166    
167    =item scope
168    
169    Scope of the desired genomes. C<core> covers the original core genomes,
170    C<nmpdr> covers all genomes in NMPDR groups, and C<all> covers all
171    genomes in the system.
172    
173    =item RETURN
174    
175    Returns a list of the IDs for the genomes in the specified scope.
176    
177    =back
178    
179    =cut
180    
181    sub CoreGenomes {
182        # Get the parameters.
183        my ($self, $scope) = @_;
184        # Declare the return variable.
185        my @retVal = ();
186        # If we want all genomes, then this is easy.
187        if ($scope eq 'all') {
188            @retVal = $self->Genomes();
189        } else {
190            # Here we're dealing with groups. Get the hash of all the
191            # genome groups.
192            my %groups = $self->GetGroups();
193            # Loop through the groups, keeping the ones that we want.
194            for my $group (keys %groups) {
195                # Decide if we want to keep this group.
196                my $keepGroup = 0;
197                if ($scope eq 'nmpdr') {
198                    # NMPDR mode: keep all groups.
199                    $keepGroup = 1;
200                } elsif ($scope eq 'core') {
201                    # CORE mode. Only keep real core groups.
202                    if (grep { $group =~ /$_/ } @{$FIG_Config::realCoreGroups}) {
203                        $keepGroup = 1;
204                    }
205                }
206                # Add this group if we're keeping it.
207                if ($keepGroup) {
208                    push @retVal, @{$groups{$group}};
209                }
210            }
211        }
212        # Return the result.
213        return @retVal;
214    }
215    
216    =head3 SuperGroup
217    
218        my $superGroup = $sprout->SuperGroup($groupName);
219    
220    Return the name of the super-group containing the specified NMPDR genome
221    group. If no appropriate super-group can be found, an error will be
222    thrown.
223    
224    =over 4
225    
226    =item groupName
227    
228    Name of the group whose super-group is desired.
229    
230    =item RETURN
231    
232    Returns the name of the super-group containing the incoming group.
233    
234    =back
235    
236    =cut
237    
238    sub SuperGroup {
239        # Get the parameters.
240        my ($self, $groupName) = @_;
241        # Declare the return variable.
242        my $retVal;
243        # Get the group hash.
244        my %groupHash = $self->CheckGroupFile();
245        # Find the super-group genus.
246        $groupName =~ /([A-Z]\w+)/;
247        my $nameThing = $1;
248        # See if it's directly in the group hash.
249        if (exists $groupHash{$nameThing}) {
250            # Yes, then it's our result.
251            $retVal = $nameThing;
252        } else {
253            # No, so we have to search.
254            for my $superGroup (keys %groupHash) {
255                # Get this super-group's item list.
256                my $list = $groupHash{$superGroup}->{contents};
257                # Search it.
258                if (grep { $_->[0] eq $nameThing } @{$list}) {
259                    $retVal = $superGroup;
260                }
261            }
262            # Make sure we found something.
263            if (! $retVal) {
264                Confess("No super-group found for \"$groupName\".");
265            }
266        }
267        # Return the result.
268        return $retVal;
269  }  }
270    
271  =head3 MaxSegment  =head3 MaxSegment
272    
273  C<< my $length = $sprout->MaxSegment(); >>      my $length = $sprout->MaxSegment();
274    
275  This method returns the maximum permissible length of a feature segment. The length is important  This method returns the maximum permissible length of a feature segment. The length is important
276  because it enables us to make reasonable guesses at how to find features inside a particular  because it enables us to make reasonable guesses at how to find features inside a particular
# Line 142  Line 287 
287    
288  =head3 MaxSequence  =head3 MaxSequence
289    
290  C<< my $length = $sprout->MaxSequence(); >>      my $length = $sprout->MaxSequence();
291    
292  This method returns the maximum permissible length of a contig sequence. A contig is broken  This method returns the maximum permissible length of a contig sequence. A contig is broken
293  into sequences in order to save memory resources. In particular, when manipulating features,  into sequences in order to save memory resources. In particular, when manipulating features,
# Line 155  Line 300 
300      return $self->{_options}->{maxSequenceLength};      return $self->{_options}->{maxSequenceLength};
301  }  }
302    
303  =head3 Get  =head3 Load
   
 C<< my $query = $sprout->Get(\@objectNames, $filterClause, \@parameterList); >>  
   
 This method allows a general query against the Sprout data using a specified filter clause.  
   
 The filter is a standard WHERE/ORDER BY clause with question marks as parameter markers and each  
 field name represented in the form B<I<objectName>(I<fieldName>)>. For example, the  
 following call requests all B<Genome> objects for the genus specified in the variable  
 $genus.  
304    
305  C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ?", [$genus]); >>      $sprout->Load($rebuild);;
306    
307  The WHERE clause contains a single question mark, so there is a single additional  Load the database from files in the data directory, optionally re-creating the tables.
 parameter representing the parameter value. It would also be possible to code  
308    
309  C<< $query = $sprout->Get(['Genome'], "Genome(genus) = \'$genus\'"); >>  This method always deletes the data from the database before loading, even if the tables are not
310    re-created. The data is loaded into the relations from files in the data directory either having the
311    same name as the target relation with no extension or with an extension of C<.dtx>. Files without an
312    extension are used in preference to the files with an extension.
313    
314  however, this version of the call would generate a syntax error if there were any quote  The files are loaded based on the presumption that each line of the file is a record in the
315  characters inside the variable C<$genus>.  relation, and the individual fields are delimited by tabs. Tab and new-line characters inside
316    fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must
317    be presented in the order given in the relation tables produced by the L</ShowMetaData> method.
318    
319  The use of the strange parenthesized notation for field names enables us to distinguish  =over 4
 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.  
320    
321  It is possible to specify multiple entity and relationship names in order to retrieve more than  =item rebuild
 one object's data at the same time, which allows highly complex joined queries. For example,  
322    
323  C<< $query = $sprout->Get(['Genome', 'ComesFrom', 'Source'], "Genome(genus) = ?", [$genus]); >>  TRUE if the data tables need to be created or re-created, else FALSE
324    
325  This query returns all the genomes for a particular genus and allows access to the  =item RETURN
 sources from which they came. The join clauses to go from Genome to Source are generated  
 automatically.  
326    
327  Finally, the filter clause can contain sort information. To do this, simply put an C<ORDER BY>  Returns a statistical object containing the number of records read, the number of duplicates found,
328  clause at the end of the filter. Field references in the ORDER BY section follow the same rules  the number of errors, and a list of the error messages.
 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.  
329    
330  C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ? ORDER BY Genome(species)", [$genus]); >>  =back
331    
332  It is also permissible to specify I<only> an ORDER BY clause. For example, the following invocation gets  =cut
333  all genomes ordered by genus and species.  #: Return Type %;
334    sub Load {
335        # Get the parameters.
336        my ($self, $rebuild) = @_;
337        # Load the tables from the data directory.
338        my $retVal = $self->LoadTables($self->{_options}->{dataDir}, $rebuild);
339        # Return the statistics.
340        return $retVal;
341    }
342    
343  C<< $query = $sprout->Get(['Genome'], "ORDER BY Genome(genus), Genome(species)"); >>  =head3 LoadUpdate
344    
345  Odd things may happen if one of the ORDER BY fields is in a secondary relation. So, for example, an      my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList);
 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.  
346    
347  If multiple names are specified, then the query processor will automatically determine a  Load updates to one or more database tables. This method enables the client to make changes to one
348  join path between the entities and relationships. The algorithm used is very simplistic.  or two tables without reloading the whole database. For each table, there must be a corresponding
349  In particular, you can't specify any entity or relationship more than once, and if a  file in the data directory, either with the same name as the table, or with a C<.dtx> suffix. So,
350  relationship is recursive, the path is determined by the order in which the entity  for example, to make updates to the B<FeatureTranslation> relation, there must be a
351  and the relationship appear. For example, consider a recursive relationship B<IsParentOf>  C<FeatureTranslation.dtx> file in the data directory. Unlike a full load, files without an extension
352  which relates B<People> objects to other B<People> objects. If the join path is  are not examined. This allows update files to co-exist with files from an original load.
 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.  
353    
354  =over 4  =over 4
355    
356  =item objectNames  =item truncateFlag
   
 List containing the names of the entity and relationship objects to be retrieved.  
   
 =item filterClause  
357    
358  WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can  TRUE if the tables should be rebuilt before loading, else FALSE. A value of TRUE therefore causes
359  be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form  current data and schema of the tables to be replaced, while a value of FALSE means the new data
360  B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the  is added to the existing data in the various relations.
 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.  
361    
362  =item parameterList  =item tableList
363    
364  List of the parameters to be substituted in for the parameters marks in the filter clause.  List of the tables to be updated.
365    
366  =item RETURN  =item RETURN
367    
368  Returns a B<DBQuery> that can be used to iterate through all of the results.  Returns a statistical object containing the number of records read, the number of duplicates found,
369    the number of errors encountered, and a list of error messages.
370    
371  =back  =back
372    
373  =cut  =cut
374    #: Return Type $%;
375  sub Get {  sub LoadUpdate {
376      # Get the parameters.      # Get the parameters.
377      my ($self, $objectNames, $filterClause, $parameterList) = @_;      my ($self, $truncateFlag, $tableList) = @_;
378      # We differ from the ERDB Get method in that the parameter list is passed in as a list reference      # Declare the return value.
379      # rather than a list of parameters. The next step is to convert the parameters from a reference      my $retVal = Stats->new();
380      # to a real list. We can only do this if the parameters have been specified.      # Get the data directory.
381      my @parameters;      my $optionTable = $self->{_options};
382      if ($parameterList) { @parameters = @{$parameterList}; }      my $dataDir = $optionTable->{dataDir};
383      return $self->{_erdb}->Get($objectNames, $filterClause, @parameters);      # Loop through the incoming table names.
384        for my $tableName (@{$tableList}) {
385            # Find the table's file.
386            my $fileName = LoadFileName($dataDir, $tableName);
387            if (! $fileName) {
388                Trace("No load file found for $tableName in $dataDir.") if T(0);
389            } else {
390                # Attempt to load this table.
391                my $result = $self->LoadTable($fileName, $tableName, truncate => $truncateFlag);
392                # Accumulate the resulting statistics.
393                $retVal->Accumulate($result);
394            }
395        }
396        # Return the statistics.
397        return $retVal;
398  }  }
399    
400  =head3 GetEntity  =head3 GenomeCounts
401    
402  C<< my $entityObject = $sprout->GetEntity($entityType, $ID); >>      my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete);
403    
404  Return an object describing the entity instance with a specified ID.  Count the number of genomes in each domain. If I<$complete> is TRUE, only complete
405    genomes will be included in the counts.
406    
407  =over 4  =over 4
408    
409  =item entityType  =item complete
   
 Entity type name.  
   
 =item ID  
410    
411  ID of the desired entity.  TRUE if only complete genomes are to be counted, FALSE if all genomes are to be
412    counted
413    
414  =item RETURN  =item RETURN
415    
416  Returns a B<DBObject> representing the desired entity instance, or an undefined value if no  A six-element list containing the number of genomes in each of six categories--
417  instance is found with the specified key.  Archaea, Bacteria, Eukaryota, Viral, Environmental, and Unknown, respectively.
418    
419  =back  =back
420    
421  =cut  =cut
422    
423  sub GetEntity {  sub GenomeCounts {
424      # Get the parameters.      # Get the parameters.
425      my ($self, $entityType, $ID) = @_;      my ($self, $complete) = @_;
426      # Call the ERDB method.      # Set the filter based on the completeness flag.
427      return $self->{_erdb}->GetEntity($entityType, $ID);      my $filter = ($complete ? "Genome(complete) = 1" : "");
428        # Get all the genomes and the related taxonomy information.
429        my @genomes = $self->GetAll(['Genome'], $filter, [], ['Genome(id)', 'Genome(taxonomy)']);
430        # Clear the counters.
431        my ($arch, $bact, $euk, $vir, $env, $unk) = (0, 0, 0, 0, 0, 0);
432        # Loop through, counting the domains.
433        for my $genome (@genomes) {
434            if    ($genome->[1] =~ /^archaea/i)  { ++$arch }
435            elsif ($genome->[1] =~ /^bacter/i)   { ++$bact }
436            elsif ($genome->[1] =~ /^eukar/i)    { ++$euk }
437            elsif ($genome->[1] =~ /^vir/i)      { ++$vir }
438            elsif ($genome->[1] =~ /^env/i)      { ++$env }
439            else  { ++$unk }
440        }
441        # Return the counts.
442        return ($arch, $bact, $euk, $vir, $env, $unk);
443  }  }
444    
445  =head3 GetEntityValues  =head3 ContigCount
446    
447  C<< my @values = GetEntityValues($entityType, $ID, \@fields); >>      my $count = $sprout->ContigCount($genomeID);
448    
449  Return a list of values from a specified entity instance.  Return the number of contigs for the specified genome ID.
450    
451  =over 4  =over 4
452    
453  =item entityType  =item genomeID
   
 Entity type name.  
   
 =item ID  
   
 ID of the desired entity.  
   
 =item fields  
454    
455  List of field names, each of the form I<objectName>C<(>I<fieldName>C<)>.  ID of the genome whose contig count is desired.
456    
457  =item RETURN  =item RETURN
458    
459  Returns a flattened list of the values of the specified fields for the specified entity.  Returns the number of contigs for the specified genome.
460    
461  =back  =back
462    
463  =cut  =cut
464  #: Return Type @;  
465  sub GetEntityValues {  sub ContigCount {
466      # Get the parameters.      # Get the parameters.
467      my ($self, $entityType, $ID, $fields) = @_;      my ($self, $genomeID) = @_;
468      # Call the ERDB method.      # Get the contig count.
469      return $self->{_erdb}->GetEntityValues($entityType, $ID, $fields);      my $retVal = $self->GetCount(['Contig', 'HasContig'], "HasContig(from-link) = ?", [$genomeID]);
470        # Return the result.
471        return $retVal;
472  }  }
473    
474  =head3 ShowMetaData  =head3 GenomeMenu
475    
476  C<< $sprout->ShowMetaData($fileName); >>      my $html = $sprout->GenomeMenu(%options);
477    
478  This method outputs a description of the database to an HTML file in the data directory.  Generate a genome selection control with the specified name and options.
479    This control is almost but not quite the same as the genome control in the
480    B<SearchHelper> class. Eventually, the two will be combined.
481    
482  =over 4  =over 4
483    
484  =item fileName  =item options
485    
486    Optional parameters for the control (see below).
487    
488    =item RETURN
489    
490  Fully-qualified name to give to the output file.  Returns the HTML for a genome selection control on a form (sometimes called a popup menu).
491    
492  =back  =back
493    
494  =cut  The valid options are as follows.
495    
496  sub ShowMetaData {  =over 4
     # Get the parameters.  
     my ($self, $fileName) = @_;  
     # Compute the file name.  
     my $options = $self->{_options};  
     # Call the show method on the underlying ERDB object.  
     $self->{_erdb}->ShowMetaData($fileName);  
 }  
497    
498  =head3 Load  =item name
499    
500  C<< $sprout->Load($rebuild); >>;  Name to give this control for use in passing it to the form. The default is C<myGenomeControl>.
501    Terrible things will happen if you have two controls with the same name on the same page.
502    
503  Load the database from files in the data directory, optionally re-creating the tables.  =item filter
504    
505  This method always deletes the data from the database before loading, even if the tables are not  If specified, a filter for the list of genomes to display. The filter should be in the form of a
506  re-created. The data is loaded into the relations from files in the data directory either having the  list reference. The first element of the list should be the filter string, and the remaining elements
507  same name as the target relation with no extension or with an extension of C<.dtx>. Files without an  the filter parameters.
 extension are used in preference to the files with an extension.  
508    
509  The files are loaded based on the presumption that each line of the file is a record in the  =item multiSelect
 relation, and the individual fields are delimited by tabs. Tab and new-line characters inside  
 fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must  
 be presented in the order given in the relation tables produced by the L</ShowMetaData> method.  
510    
511  =over 4  If TRUE, then the user can select multiple genomes. If FALSE, the user can only select one genome.
512    
513  =item rebuild  =item size
514    
515  TRUE if the data tables need to be created or re-created, else FALSE  Number of rows to display in the control. The default is C<10>
516    
517  =item RETURN  =item id
518    
519  Returns a statistical object containing the number of records read, the number of duplicates found,  ID to give this control. The default is the value of the C<name> option. Nothing will work correctly
520  the number of errors, and a list of the error messages.  unless this ID is unique.
521    
522    =item selected
523    
524    A comma-delimited list of selected genomes, or a reference to a list of selected genomes. The
525    default is none.
526    
527    =item class
528    
529    If specified, a style class to assign to the genome control.
530    
531  =back  =back
532    
533  =cut  =cut
534  #: Return Type %;  
535  sub Load {  sub GenomeMenu {
536      # Get the parameters.      # Get the parameters.
537      my ($self, $rebuild) = @_;      my ($self, %options) = @_;
538      # Get the database object.      # Get the control's name and ID.
539      my $erdb = $self->{_erdb};      my $menuName = $options{name} || $options{id} || 'myGenomeControl';
540      # Load the tables from the data directory.      my $menuID = $options{id} || $menuName;
541      my $retVal = $erdb->LoadTables($self->{_options}->{dataDir}, $rebuild);      Trace("Genome menu name = $menuName with ID $menuID.") if T(3);
542      # Return the statistics.      # Compute the IDs for the status display.
543        my $divID = "${menuID}_status";
544        my $urlID = "${menuID}_url";
545        # Compute the code to show selected genomes in the status area.
546        my $showSelect = "showSelected('$menuID', '$divID', '$urlID', 1000)";
547        # Check for single-select or multi-select.
548        my $multiSelect = $options{multiSelect} || 0;
549        # Get the style data.
550        my $class = $options{class} || '';
551        # Get the list of pre-selected items.
552        my $selections = $options{selected} || [];
553        if (ref $selections ne 'ARRAY') {
554            $selections = [ split /\s*,\s*/, $selections ];
555        }
556        my %selected = map { $_ => 1 } @{$selections};
557        # Extract the filter information. The default is no filtering. It can be passed as a tab-delimited
558        # string or a list reference.
559        my $filterParms = $options{filter} || "";
560        if (! ref $filterParms) {
561            $filterParms = [split /\t|\\t/, $filterParms];
562        }
563        my $filterString = shift @{$filterParms};
564        # Get a list of all the genomes in group order. In fact, we only need them ordered
565        # by name (genus,species,strain), but putting primary-group in front enables us to
566        # take advantage of an existing index.
567        my @genomeList = $self->GetAll(['Genome'], "$filterString ORDER BY Genome(primary-group), Genome(genus), Genome(species), Genome(unique-characterization)",
568                                       $filterParms,
569                                       [qw(Genome(primary-group) Genome(id) Genome(genus) Genome(species) Genome(unique-characterization) Genome(taxonomy) Genome(contigs))]);
570        # Create a hash to organize the genomes by group. Each group will contain a list of
571        # 2-tuples, the first element being the genome ID and the second being the genome
572        # name.
573        my %gHash = ();
574        for my $genome (@genomeList) {
575            # Get the genome data.
576            my ($group, $genomeID, $genus, $species, $strain, $taxonomy, $contigs) = @{$genome};
577            # Compute its name. This is the genus, species, strain (if any), and the contig count.
578            my $name = "$genus $species ";
579            $name .= "$strain " if $strain;
580            my $contigCount = ($contigs == 1 ? "" : ", $contigs contigs");
581            # Now we get the domain. The domain tells us the display style of the organism.
582            my ($domain) = split /\s*;\s*/, $taxonomy, 2;
583            # Now compute the display group. This is normally the primary group, but if the
584            # organism is supporting, we blank it out.
585            my $displayGroup = ($group eq $FIG_Config::otherGroup ? "" : $group);
586            # Push the genome into the group's list. Note that we use the real group
587            # name for the hash key here, not the display group name.
588            push @{$gHash{$group}}, [$genomeID, $name, $contigCount, $domain];
589        }
590        # We are almost ready to unroll the menu out of the group hash. The final step is to separate
591        # the supporting genomes by domain. First, we extract the NMPDR groups and sort them. They
592        # are sorted by the first capitalized word. Groups with "other" are sorted after groups
593        # that aren't "other". At some point, we will want to make this less complicated.
594        my %sortGroups = map { $_ =~ /(other)?(.*)([A-Z].+)/; "$3$1$2" => $_ }
595                             grep { $_ ne $FIG_Config::otherGroup } keys %gHash;
596        my @groups = map { $sortGroups{$_} } sort keys %sortGroups;
597        # Remember the number of NMPDR groups.
598        my $nmpdrGroupCount = scalar @groups;
599        # Loop through the supporting genomes, classifying them by domain. We'll also keep a list
600        # of the domains found.
601        my @otherGenomes = @{$gHash{$FIG_Config::otherGroup}};
602        my @domains = ();
603        for my $genomeData (@otherGenomes) {
604            my ($genomeID, $name, $contigCount, $domain) = @{$genomeData};
605            if (exists $gHash{$domain}) {
606                push @{$gHash{$domain}}, $genomeData;
607            } else {
608                $gHash{$domain} = [$genomeData];
609                push @domains, $domain;
610            }
611        }
612        # Add the domain groups at the end of the main group list. The main group list will now
613        # contain all the categories we need to display the genomes.
614        push @groups, sort @domains;
615        # Delete the supporting group.
616        delete $gHash{$FIG_Config::otherGroup};
617        # Now it gets complicated. We need a way to mark all the NMPDR genomes. We take advantage
618        # of the fact they come first in the list. We'll accumulate a count of the NMPDR genomes
619        # and use that to make the selections.
620        my $nmpdrCount = 0;
621        # Create the type counters.
622        my $groupCount = 1;
623        # Get the number of rows to display.
624        my $rows = $options{size} || 10;
625        # If we're multi-row, create an onChange event.
626        my $onChangeTag = ( $rows > 1 ? " onChange=\"$showSelect;\" onFocus=\"$showSelect;\"" : "" );
627        # Set up the multiple-select flag.
628        my $multipleTag = ($multiSelect ? " multiple" : "" );
629        # Set up the style class.
630        my $classTag = ($class ? " class=\"$class\"" : "" );
631        # Create the SELECT tag and stuff it into the output array.
632        my @lines = ("<SELECT name=\"$menuName\" id=\"$menuID\" $onChangeTag$multipleTag$classTag size=\"$rows\">");
633        # Loop through the groups.
634        for my $group (@groups) {
635            # Get the genomes in the group.
636            for my $genome (@{$gHash{$group}}) {
637                # If this is an NMPDR organism, we add an extra style and count it.
638                my $nmpdrStyle = "";
639                if ($nmpdrGroupCount > 0) {
640                    $nmpdrCount++;
641                    $nmpdrStyle = " Core";
642                }
643                # Get the organism ID, name, contig count, and domain.
644                my ($genomeID, $name, $contigCount, $domain) = @{$genome};
645                # See if we're pre-selected.
646                my $selectTag = ($selected{$genomeID} ? " SELECTED" : "");
647                # Compute the display name.
648                my $nameString = "$name ($genomeID$contigCount)";
649                # Generate the option tag.
650                my $optionTag = "<OPTION class=\"$domain$nmpdrStyle\" title=\"$group\" value=\"$genomeID\"$selectTag>$nameString</OPTION>";
651                push @lines, "    $optionTag";
652            }
653            # Record this group in the nmpdrGroup count. When that gets to 0, we've finished the NMPDR
654            # groups.
655            $nmpdrGroupCount--;
656        }
657        # Close the SELECT tag.
658        push @lines, "</SELECT>";
659        if ($rows > 1) {
660            # We're in a non-compact mode, so we need to add some selection helpers. First is
661            # the search box. This allows the user to type text and change which genomes are
662            # displayed. For multiple-select mode, we include a button that selects the displayed
663            # genes. For single-select mode, we use a plain label instead.
664            my $searchThingName = "${menuID}_SearchThing";
665            my $searchThingLabel = ($multiSelect ? "<INPUT type=\"button\" name=\"MacroSearch\" class=\"button\" value=\"Select genomes containing\" onClick=\"selectShowing('$menuID', '$searchThingName'); $showSelect;\" />"
666                                                 : "Show genomes containing");
667            push @lines, "<br />$searchThingLabel&nbsp;" .
668                         "<INPUT type=\"text\" id=\"$searchThingName\" name=\"$searchThingName\" size=\"30\" onKeyup=\"showTyped('$menuID', '$searchThingName');\" />" .
669                         Hint("GenomeControl", "Type here to filter the genomes displayed.") . "<br />";
670            # For multi-select mode, we also have buttons to set and clear selections.
671            if ($multiSelect) {
672                push @lines, "<INPUT type=\"button\" name=\"ClearAll\" class=\"bigButton\"  value=\"Clear All\" onClick=\"clearAll('$menuID'); $showSelect\" />";
673                push @lines, "<INPUT type=\"button\" name=\"SelectAll\" class=\"bigButton\" value=\"Select All\" onClick=\"selectAll('$menuID'); $showSelect\" />";
674                push @lines, "<INPUT type=\"button\" name=\"NMPDROnly\" class=\"bigButton\"  value=\"Select NMPDR\" onClick=\"selectSome('$menuID', $nmpdrCount, true); $showSelect;\" />";
675            }
676            # Add a hidden field we can use to generate organism page hyperlinks.
677            push @lines, "<INPUT type=\"hidden\" id=\"$urlID\" value=\"$FIG_Config::cgi_url/wiki/rest.cgi/NmpdrPlugin/SeedViewer?page=Organism;organism=\" />";
678            # Add the status display. This tells the user what's selected no matter where the list is scrolled.
679            push @lines, "<DIV id=\"$divID\" class=\"Panel\"></DIV>";
680        }
681        # Assemble all the lines into a string.
682        my $retVal = join("\n", @lines, "");
683        # Return the result.
684      return $retVal;      return $retVal;
685  }  }
686    
 =head3 LoadUpdate  
   
 C<< my %stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>  
687    
688  Load updates to one or more database tables. This method enables the client to make changes to one  =head3 Stem
 or two tables without reloading the whole database. For each table, there must be a corresponding  
 file in the data directory, either with the same name as the table, or with a C<.dtx> suffix. So,  
 for example, to make updates to the B<FeatureTranslation> relation, there must be a  
 C<FeatureTranslation.dtx> file in the data directory. Unlike a full load, files without an extension  
 are not examined. This allows update files to co-exist with files from an original load.  
689    
690  =over 4      my $stem = $sprout->Stem($word);
691    
692  =item truncateFlag  Return the stem of the specified word, or C<undef> if the word is not
693    stemmable. Note that even if the word is stemmable, the stem may be
694    the same as the original word.
695    
696  TRUE if the tables should be rebuilt before loading, else FALSE. A value of TRUE therefore causes  =over 4
 current data and schema of the tables to be replaced, while a value of FALSE means the new data  
 is added to the existing data in the various relations.  
697    
698  =item tableList  =item word
699    
700  List of the tables to be updated.  Word to convert into a stem.
701    
702  =item RETURN  =item RETURN
703    
704  Returns a statistical object containing the number of records read, the number of duplicates found,  Returns a stem of the word (which may be the word itself), or C<undef> if
705  the number of errors encountered, and a list of error messages.  the word is not stemmable.
706    
707  =back  =back
708    
709  =cut  =cut
710  #: Return Type $%;  
711  sub LoadUpdate {  sub Stem {
712      # Get the parameters.      # Get the parameters.
713      my ($self, $truncateFlag, $tableList) = @_;      my ($self, $word) = @_;
714      # Get the database object.      # Declare the return variable.
715      my $erdb = $self->{_erdb};      my $retVal;
716      # Declare the return value.      # See if it's stemmable.
717      my $retVal = Stats->new();      if ($word =~ /^[A-Za-z]+$/) {
718      # Get the data directory.          # Compute the stem.
719      my $optionTable = $self->{_options};          my $stemList = $self->{stemmer}->stem($word);
720      my $dataDir = $optionTable->{dataDir};          my $stem = $stemList->[0];
721      # Loop through the incoming table names.          # Check to see if it's long enough.
722      for my $tableName (@{$tableList}) {          if (length $stem >= 3) {
723          # Find the table's file.              # Yes, keep it.
724          my $fileName = LoadFileName($dataDir, $tableName);              $retVal = $stem;
         if (! $fileName) {  
             Trace("No load file found for $tableName in $dataDir.") if T(0);  
725          } else {          } else {
726              # Attempt to load this table.              # No, use the original word.
727              my $result = $erdb->LoadTable($fileName, $tableName, $truncateFlag);              $retVal = $word;
             # Accumulate the resulting statistics.  
             $retVal->Accumulate($result);  
728          }          }
729      }      }
730      # Return the statistics.      # Return the result.
731      return $retVal;      return $retVal;
732  }  }
733    
734    
735  =head3 Build  =head3 Build
736    
737  C<< $sprout->Build(); >>      $sprout->Build();
738    
739  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.
740  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 746 
746      # Get the parameters.      # Get the parameters.
747      my ($self) = @_;      my ($self) = @_;
748      # Create the tables.      # Create the tables.
749      $self->{_erdb}->CreateTables;      $self->CreateTables();
750  }  }
751    
752  =head3 Genomes  =head3 Genomes
753    
754  C<< my @genomes = $sprout->Genomes(); >>      my @genomes = $sprout->Genomes();
755    
756  Return a list of all the genome IDs.  Return a list of all the genome IDs.
757    
# Line 482  Line 768 
768    
769  =head3 GenusSpecies  =head3 GenusSpecies
770    
771  C<< my $infoString = $sprout->GenusSpecies($genomeID); >>      my $infoString = $sprout->GenusSpecies($genomeID);
772    
773  Return the genus, species, and unique characterization for a genome.  Return the genus, species, and unique characterization for a genome.
774    
# Line 514  Line 800 
800    
801  =head3 FeaturesOf  =head3 FeaturesOf
802    
803  C<< my @features = $sprout->FeaturesOf($genomeID, $ftype); >>      my @features = $sprout->FeaturesOf($genomeID, $ftype);
804    
805  Return a list of the features relevant to a specified genome.  Return a list of the features relevant to a specified genome.
806    
# Line 559  Line 845 
845    
846  =head3 FeatureLocation  =head3 FeatureLocation
847    
848  C<< my @locations = $sprout->FeatureLocation($featureID); >>      my @locations = $sprout->FeatureLocation($featureID);
849    
850  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
851  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 869 
869  =item RETURN  =item RETURN
870    
871  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
872  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
873    wasn't found.
874    
875  =back  =back
876    
877  =cut  =cut
878  #: Return Type @;  
 #: Return Type $;  
879  sub FeatureLocation {  sub FeatureLocation {
880      # Get the parameters.      # Get the parameters.
881      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
882      # 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.  
883      my @retVal = ();      my @retVal = ();
884      # Set up the variables used to determine if we have adjacent segments. This initial setup will      # Get the feature record.
885      # not match anything.      my $object = $self->GetEntity('Feature', $featureID);
886      my ($prevContig, $prevBeg, $prevDir, $prevLen) = ("", 0, "0", 0);      # Only proceed if we found it.
887      # Loop through the query results, creating location specifiers.      if (defined $object) {
888      while (my $location = $query->Fetch()) {          # Get the location string.
889          # Get the location parameters.          my $locString = $object->PrimaryValue('Feature(location-string)');
890          my ($contigID, $beg, $dir, $len) = $location->Values(['IsLocatedIn(to-link)',          # Create the return list.
891              '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}_$start$dir$len";  
892      }      }
893      # Return the list in the format indicated by the context.      # Return the list in the format indicated by the context.
894      return (wantarray ? @retVal : join(',', @retVal));      return (wantarray ? @retVal : join(',', @retVal));
# Line 633  Line 896 
896    
897  =head3 ParseLocation  =head3 ParseLocation
898    
899  C<< my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location); >>      my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location);
900    
901  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
902  length.  length.
# Line 652  Line 915 
915  =back  =back
916    
917  =cut  =cut
918  #: Return Type @;  
919  sub ParseLocation {  sub ParseLocation {
920      # 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
921      # the first parameter.      # the first parameter.
922      shift if UNIVERSAL::isa($_[0],__PACKAGE__);      shift if UNIVERSAL::isa($_[0],__PACKAGE__);
923      my ($location) = @_;      my ($location) = @_;
924      # Parse it into segments.      # Parse it into segments.
925      $location =~ /^(.*)_(\d*)([+-_])(\d*)$/;      $location =~ /^(.+)_(\d+)([+\-_])(\d+)$/;
926      my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);      my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);
927      # If the direction is an underscore, convert it to a + or -.      # If the direction is an underscore, convert it to a + or -.
928      if ($dir eq "_") {      if ($dir eq "_") {
# Line 675  Line 938 
938      return ($contigID, $start, $dir, $len);      return ($contigID, $start, $dir, $len);
939  }  }
940    
941    
942    
943  =head3 PointLocation  =head3 PointLocation
944    
945  C<< my $found = Sprout::PointLocation($location, $point); >>      my $found = Sprout::PointLocation($location, $point);
946    
947  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
948  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 971 
971  =back  =back
972    
973  =cut  =cut
974  #: Return Type $;  
975  sub PointLocation {  sub PointLocation {
976      # 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
977      # the first parameter.      # the first parameter.
# Line 729  Line 994 
994    
995  =head3 DNASeq  =head3 DNASeq
996    
997  C<< my $sequence = $sprout->DNASeq(\@locationList); >>      my $sequence = $sprout->DNASeq(\@locationList);
998    
999  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
1000  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,
1001  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>.
1002    
1003    For example, the following would return the DNA sequence for contig C<83333.1:NC_000913>
1004    between positions 1401 and 1532, inclusive.
1005    
1006        my $sequence = $sprout->DNASeq('83333.1:NC_000913_1401_1532');
1007    
1008  =over 4  =over 4
1009    
1010  =item locationList  =item locationList
1011    
1012  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
1013  L</FeatureLocation> for more about this format).  I<contigID>C<_>I<begin>C<_>I<end> (see L</FeatureLocation> for more about this format).
1014    
1015  =item RETURN  =item RETURN
1016    
# Line 770  Line 1040 
1040          Trace("Parse of \"$location\" is $beg$dir$len.") if T(SDNA => 4);          Trace("Parse of \"$location\" is $beg$dir$len.") if T(SDNA => 4);
1041          if ($dir eq "+") {          if ($dir eq "+") {
1042              $start = $beg;              $start = $beg;
1043              $stop = $beg + $len;              $stop = $beg + $len - 1;
1044          } else {          } else {
1045              $start = $beg - $len;              $start = $beg - $len + 1;
1046              $stop = $beg + 1;              $stop = $beg;
1047          }          }
1048          Trace("Looking for sequences containing $start to $stop.") if T(SDNA => 4);          Trace("Looking for sequences containing $start through $stop.") if T(SDNA => 4);
1049          my $query = $self->Get(['IsMadeUpOf','Sequence'],          my $query = $self->Get(['IsMadeUpOf','Sequence'],
1050              "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .              "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .
1051              " IsMadeUpOf(start-position) < ? ORDER BY IsMadeUpOf(start-position)",              " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",
1052              [$contigID, $start, $stop]);              [$contigID, $start, $stop]);
1053          # Loop through the sequences.          # Loop through the sequences.
1054          while (my $sequence = $query->Fetch()) {          while (my $sequence = $query->Fetch()) {
# Line 790  Line 1060 
1060              Trace("Sequence is from $startPosition to $stopPosition.") if T(SDNA => 4);              Trace("Sequence is from $startPosition to $stopPosition.") if T(SDNA => 4);
1061              # Figure out the start point and length of the relevant section.              # Figure out the start point and length of the relevant section.
1062              my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);              my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);
1063              my $len1 = ($stopPosition <= $stop ? $stopPosition : $stop) - $startPosition - $pos1;              my $len1 = ($stopPosition < $stop ? $stopPosition : $stop) + 1 - $startPosition - $pos1;
1064              Trace("Position is $pos1 for length $len1.") if T(SDNA => 4);              Trace("Position is $pos1 for length $len1.") if T(SDNA => 4);
1065              # Add the relevant data to the location data.              # Add the relevant data to the location data.
1066              $locationDNA .= substr($sequenceData, $pos1, $len1);              $locationDNA .= substr($sequenceData, $pos1, $len1);
# Line 808  Line 1078 
1078    
1079  =head3 AllContigs  =head3 AllContigs
1080    
1081  C<< my @idList = $sprout->AllContigs($genomeID); >>      my @idList = $sprout->AllContigs($genomeID);
1082    
1083  Return a list of all the contigs for a genome.  Return a list of all the contigs for a genome.
1084    
# Line 836  Line 1106 
1106      return @retVal;      return @retVal;
1107  }  }
1108    
1109  =head3 ContigLength  =head3 GenomeLength
1110    
1111  C<< my $length = $sprout->ContigLength($contigID); >>      my $length = $sprout->GenomeLength($genomeID);
1112    
1113  Compute the length of a contig.  Return the length of the specified genome in base pairs.
1114    
1115  =over 4  =over 4
1116    
1117  =item contigID  =item genomeID
1118    
1119  ID of the contig whose length is desired.  ID of the genome whose base pair count is desired.
1120    
1121  =item RETURN  =item RETURN
1122    
1123  Returns the number of positions in the contig.  Returns the number of base pairs in all the contigs of the specified
1124    genome.
1125    
1126  =back  =back
1127    
1128  =cut  =cut
1129  #: Return Type $;  
1130  sub ContigLength {  sub GenomeLength {
1131      # Get the parameters.      # Get the parameters.
1132      my ($self, $contigID) = @_;      my ($self, $genomeID) = @_;
1133      # Get the contig's last sequence.      # Declare the return variable.
1134      my $query = $self->Get(['IsMadeUpOf'],      my $retVal = 0;
1135          "IsMadeUpOf(from-link) = ? ORDER BY IsMadeUpOf(start-position) DESC",      # Get the genome's contig sequence lengths.
1136          [$contigID]);      my @lens = $self->GetFlat(['HasContig', 'IsMadeUpOf'], 'HasContig(from-link) = ?',
1137      my $sequence = $query->Fetch();                         [$genomeID], 'IsMadeUpOf(len)');
1138      # Declare the return value.      # Sum the lengths.
1139        map { $retVal += $_ } @lens;
1140        # Return the result.
1141        return $retVal;
1142    }
1143    
1144    =head3 FeatureCount
1145    
1146        my $count = $sprout->FeatureCount($genomeID, $type);
1147    
1148    Return the number of features of the specified type in the specified genome.
1149    
1150    =over 4
1151    
1152    =item genomeID
1153    
1154    ID of the genome whose feature count is desired.
1155    
1156    =item type
1157    
1158    Type of feature to count (eg. C<peg>, C<rna>, etc.).
1159    
1160    =item RETURN
1161    
1162    Returns the number of features of the specified type for the specified genome.
1163    
1164    =back
1165    
1166    =cut
1167    
1168    sub FeatureCount {
1169        # Get the parameters.
1170        my ($self, $genomeID, $type) = @_;
1171        # Compute the count.
1172        my $retVal = $self->GetCount(['HasFeature', 'Feature'],
1173                                    "HasFeature(from-link) = ? AND Feature(feature-type) = ?",
1174                                    [$genomeID, $type]);
1175        # Return the result.
1176        return $retVal;
1177    }
1178    
1179    =head3 GenomeAssignments
1180    
1181        my $fidHash = $sprout->GenomeAssignments($genomeID);
1182    
1183    Return a list of a genome's assigned features. The return hash will contain each
1184    assigned feature of the genome mapped to the text of its most recent functional
1185    assignment.
1186    
1187    =over 4
1188    
1189    =item genomeID
1190    
1191    ID of the genome whose functional assignments are desired.
1192    
1193    =item RETURN
1194    
1195    Returns a reference to a hash which maps each feature to its most recent
1196    functional assignment.
1197    
1198    =back
1199    
1200    =cut
1201    
1202    sub GenomeAssignments {
1203        # Get the parameters.
1204        my ($self, $genomeID) = @_;
1205        # Declare the return variable.
1206        my $retVal = {};
1207        # Query the genome's features.
1208        my $query = $self->Get(['HasFeature', 'Feature'], "HasFeature(from-link) = ?",
1209                               [$genomeID]);
1210        # Loop through the features.
1211        while (my $data = $query->Fetch) {
1212            # Get the feature ID and assignment.
1213            my ($fid, $assignment) = $data->Values(['Feature(id)', 'Feature(assignment)']);
1214            if ($assignment) {
1215                $retVal->{$fid} = $assignment;
1216            }
1217        }
1218        # Return the result.
1219        return $retVal;
1220    }
1221    
1222    =head3 ContigLength
1223    
1224        my $length = $sprout->ContigLength($contigID);
1225    
1226    Compute the length of a contig.
1227    
1228    =over 4
1229    
1230    =item contigID
1231    
1232    ID of the contig whose length is desired.
1233    
1234    =item RETURN
1235    
1236    Returns the number of positions in the contig.
1237    
1238    =back
1239    
1240    =cut
1241    #: Return Type $;
1242    sub ContigLength {
1243        # Get the parameters.
1244        my ($self, $contigID) = @_;
1245        # Get the contig's last sequence.
1246        my $query = $self->Get(['IsMadeUpOf'],
1247            "IsMadeUpOf(from-link) = ? ORDER BY IsMadeUpOf(start-position) DESC",
1248            [$contigID]);
1249        my $sequence = $query->Fetch();
1250        # Declare the return value.
1251      my $retVal = 0;      my $retVal = 0;
1252      # Set it from the sequence data, if any.      # Set it from the sequence data, if any.
1253      if ($sequence) {      if ($sequence) {
1254          my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);          my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);
1255          $retVal = $start + $len;          $retVal = $start + $len - 1;
1256        }
1257        # Return the result.
1258        return $retVal;
1259    }
1260    
1261    =head3 ClusterPEGs
1262    
1263        my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs);
1264    
1265    Cluster the PEGs in a list according to the cluster coding scheme of the specified
1266    subsystem. In order for this to work properly, the subsystem object must have
1267    been used recently to retrieve the PEGs using the B<get_pegs_from_cell> or
1268    B<get_row> methods. This causes the cluster numbers to be pulled into the
1269    subsystem's color hash. If a PEG is not found in the color hash, it will not
1270    appear in the output sequence.
1271    
1272    =over 4
1273    
1274    =item sub
1275    
1276    Sprout subsystem object for the relevant subsystem, from the L</get_subsystem>
1277    method.
1278    
1279    =item pegs
1280    
1281    Reference to the list of PEGs to be clustered.
1282    
1283    =item RETURN
1284    
1285    Returns a list of the PEGs, grouped into smaller lists by cluster number.
1286    
1287    =back
1288    
1289    =cut
1290    #: Return Type $@@;
1291    sub ClusterPEGs {
1292        # Get the parameters.
1293        my ($self, $sub, $pegs) = @_;
1294        # Declare the return variable.
1295        my $retVal = [];
1296        # Loop through the PEGs, creating arrays for each cluster.
1297        for my $pegID (@{$pegs}) {
1298            my $clusterNumber = $sub->get_cluster_number($pegID);
1299            # Only proceed if the PEG is in a cluster.
1300            if ($clusterNumber >= 0) {
1301                # Push this PEG onto the sub-list for the specified cluster number.
1302                push @{$retVal->[$clusterNumber]}, $pegID;
1303            }
1304      }      }
1305      # Return the result.      # Return the result.
1306      return $retVal;      return $retVal;
# Line 877  Line 1308 
1308    
1309  =head3 GenesInRegion  =head3 GenesInRegion
1310    
1311  C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>      my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop);
1312    
1313  List the features which overlap a specified region in a contig.  List the features which overlap a specified region in a contig.
1314    
# Line 906  Line 1337 
1337  =back  =back
1338    
1339  =cut  =cut
1340  #: Return Type @@;  
1341  sub GenesInRegion {  sub GenesInRegion {
1342      # Get the parameters.      # Get the parameters.
1343      my ($self, $contigID, $start, $stop) = @_;      my ($self, $contigID, $start, $stop) = @_;
1344      # Get the maximum segment length.      # Get the maximum segment length.
1345      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 = ();  
1346      # Prime the values we'll use for the returned beginning and end.      # Prime the values we'll use for the returned beginning and end.
1347      my @initialMinMax = ($self->ContigLength($contigID), 0);      my @initialMinMax = ($self->ContigLength($contigID), 0);
1348      my ($min, $max) = @initialMinMax;      my ($min, $max) = @initialMinMax;
1349      # Create a table of parameters for each query. Each query looks for features travelling in      # Get the overlapping features.
1350        my @featureObjects = $self->GeneDataInRegion($contigID, $start, $stop);
1351        # We'l use this hash to help us track the feature IDs and sort them. The key is the
1352        # feature ID and the value is a [$left,$right] pair indicating the maximum extent
1353        # of the feature's locations.
1354        my %featureMap = ();
1355        # Loop through them to do the begin/end analysis.
1356        for my $featureObject (@featureObjects) {
1357            # Get the feature's location string. This may contain multiple actual locations.
1358            my ($locations, $fid) = $featureObject->Values([qw(Feature(location-string) Feature(id))]);
1359            my @locationSegments = split /\s*,\s*/, $locations;
1360            # Loop through the locations.
1361            for my $locationSegment (@locationSegments) {
1362                # Construct an object for the location.
1363                my $locationObject = BasicLocation->new($locationSegment);
1364                # Merge the current segment's begin and end into the min and max.
1365                my ($left, $right) = ($locationObject->Left, $locationObject->Right);
1366                my ($beg, $end);
1367                if (exists $featureMap{$fid}) {
1368                    ($beg, $end) = @{$featureMap{$fid}};
1369                    $beg = $left if $left < $beg;
1370                    $end = $right if $right > $end;
1371                } else {
1372                    ($beg, $end) = ($left, $right);
1373                }
1374                $min = $beg if $beg < $min;
1375                $max = $end if $end > $max;
1376                # Store the feature's new extent back into the hash table.
1377                $featureMap{$fid} = [$beg, $end];
1378            }
1379        }
1380        # Now we must compute the list of the IDs for the features found. We start with a list
1381        # of midpoints / feature ID pairs. (It's not really a midpoint, it's twice the midpoint,
1382        # but the result of the sort will be the same.)
1383        my @list = map { [$featureMap{$_}->[0] + $featureMap{$_}->[1], $_] } keys %featureMap;
1384        # Now we sort by midpoint and yank out the feature IDs.
1385        my @retVal = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @list;
1386        # Return it along with the min and max.
1387        return (\@retVal, $min, $max);
1388    }
1389    
1390    =head3 GeneDataInRegion
1391    
1392        my @featureList = $sprout->GenesInRegion($contigID, $start, $stop);
1393    
1394    List the features which overlap a specified region in a contig.
1395    
1396    =over 4
1397    
1398    =item contigID
1399    
1400    ID of the contig containing the region of interest.
1401    
1402    =item start
1403    
1404    Offset of the first residue in the region of interest.
1405    
1406    =item stop
1407    
1408    Offset of the last residue in the region of interest.
1409    
1410    =item RETURN
1411    
1412    Returns a list of B<ERDBObjects> for the desired features. Each object will
1413    contain a B<Feature> record.
1414    
1415    =back
1416    
1417    =cut
1418    
1419    sub GeneDataInRegion {
1420        # Get the parameters.
1421        my ($self, $contigID, $start, $stop) = @_;
1422        # Get the maximum segment length.
1423        my $maximumSegmentLength = $self->MaxSegment;
1424        # Create a hash to receive the feature list. We use a hash so that we can eliminate
1425        # duplicates easily. The hash key will be the feature ID. The value will be the feature's
1426        # ERDBObject from the query.
1427        my %featuresFound = ();
1428        # Create a table of parameters for the queries. Each query looks for features travelling in
1429      # 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,
1430      # 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
1431      # 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 1434 
1434      # Loop through the query parameters.      # Loop through the query parameters.
1435      for my $parms (values %queryParms) {      for my $parms (values %queryParms) {
1436          # Create the query.          # Create the query.
1437          my $query = $self->Get(['IsLocatedIn'],          my $query = $self->Get([qw(Feature IsLocatedIn)],
1438              "IsLocatedIn(to-link)= ? AND IsLocatedIn(dir) = ? AND IsLocatedIn(beg) >= ? AND IsLocatedIn(beg) <= ?",              "IsLocatedIn(to-link)= ? AND IsLocatedIn(dir) = ? AND IsLocatedIn(beg) >= ? AND IsLocatedIn(beg) <= ?",
1439              $parms);              $parms);
1440          # Loop through the feature segments found.          # Loop through the feature segments found.
1441          while (my $segment = $query->Fetch) {          while (my $segment = $query->Fetch) {
1442              # Get the data about this segment.              # Get the data about this segment.
1443              my ($featureID, $dir, $beg, $len) = $segment->Values(['IsLocatedIn(from-link)',              my ($featureID, $contig, $dir, $beg, $len) = $segment->Values([qw(IsLocatedIn(from-link)
1444                  'IsLocatedIn(dir)', 'IsLocatedIn(beg)', 'IsLocatedIn(len)']);                  IsLocatedIn(to-link) IsLocatedIn(dir) IsLocatedIn(beg) IsLocatedIn(len))]);
1445              # 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
1446              # 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
1447              # 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
1448              # length.              # length.
1449              my ($found, $end) = (0, 0);              my $loc = BasicLocation->new($contig, $beg, $dir, $len);
1450              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;  
                 }  
             }  
1451              if ($found) {              if ($found) {
1452                  # Here we need to record the feature and update the minima and maxima. First,                  # Save this feature in the result list.
1453                  # 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;  
1454                  }                  }
                 if ($end > $loc2) {  
                     $loc2 = $end;  
                     $max = $end if $end > $max;  
                 }  
                 # Store the entry back into the hash table.  
                 $featuresFound{$featureID} = [$loc1, $loc2];  
1455              }              }
1456          }          }
1457      }      # Return the ERDB objects for the features found.
1458      # 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);  
1459  }  }
1460    
1461  =head3 FType  =head3 FType
1462    
1463  C<< my $ftype = $sprout->FType($featureID); >>      my $ftype = $sprout->FType($featureID);
1464    
1465  Return the type of a feature.  Return the type of a feature.
1466    
# Line 1019  Line 1490 
1490    
1491  =head3 FeatureAnnotations  =head3 FeatureAnnotations
1492    
1493  C<< my @descriptors = $sprout->FeatureAnnotations($featureID); >>      my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag);
1494    
1495  Return the annotations of a feature.  Return the annotations of a feature.
1496    
# Line 1029  Line 1500 
1500    
1501  ID of the feature whose annotations are desired.  ID of the feature whose annotations are desired.
1502    
1503    =item rawFlag
1504    
1505    If TRUE, the annotation timestamps will be returned in raw form; otherwise, they
1506    will be returned in human-readable form.
1507    
1508  =item RETURN  =item RETURN
1509    
1510  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.
1511    
1512  * B<featureID> ID of the relevant feature.  * B<featureID> ID of the relevant feature.
1513    
1514  * B<timeStamp> time the annotation was made, in user-friendly format.  * B<timeStamp> time the annotation was made.
1515    
1516  * B<user> ID of the user who made the annotation  * B<user> ID of the user who made the annotation
1517    
# Line 1047  Line 1523 
1523  #: Return Type @%;  #: Return Type @%;
1524  sub FeatureAnnotations {  sub FeatureAnnotations {
1525      # Get the parameters.      # Get the parameters.
1526      my ($self, $featureID) = @_;      my ($self, $featureID, $rawFlag) = @_;
1527      # 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.
1528      my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],      my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1529                             "IsTargetOfAnnotation(from-link) = ?", [$featureID]);                             "IsTargetOfAnnotation(from-link) = ?", [$featureID]);
# Line 1060  Line 1536 
1536              $annotation->Values(['IsTargetOfAnnotation(from-link)',              $annotation->Values(['IsTargetOfAnnotation(from-link)',
1537                                   'Annotation(time)', 'MadeAnnotation(from-link)',                                   'Annotation(time)', 'MadeAnnotation(from-link)',
1538                                   'Annotation(annotation)']);                                   'Annotation(annotation)']);
1539            # Convert the time, if necessary.
1540            if (! $rawFlag) {
1541                $timeStamp = FriendlyTimestamp($timeStamp);
1542            }
1543          # Assemble them into a hash.          # Assemble them into a hash.
1544          my $annotationHash = { featureID => $featureID,          my $annotationHash = { featureID => $featureID,
1545                                 timeStamp => FriendlyTimestamp($timeStamp),                                 timeStamp => $timeStamp,
1546                                 user => $user, text => $text };                                 user => $user, text => $text };
1547          # Add it to the return list.          # Add it to the return list.
1548          push @retVal, $annotationHash;          push @retVal, $annotationHash;
# Line 1073  Line 1553 
1553    
1554  =head3 AllFunctionsOf  =head3 AllFunctionsOf
1555    
1556  C<< my %functions = $sprout->AllFunctionsOf($featureID); >>      my %functions = $sprout->AllFunctionsOf($featureID);
1557    
1558  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
1559  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 1571 
1571    
1572  =item RETURN  =item RETURN
1573    
1574  Returns a hash mapping the functional assignment IDs to user IDs.  Returns a hash mapping the user IDs to functional assignment IDs.
1575    
1576  =back  =back
1577    
# Line 1101  Line 1581 
1581      # Get the parameters.      # Get the parameters.
1582      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
1583      # Get all of the feature's annotations.      # Get all of the feature's annotations.
1584      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation'],      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1585                              "IsTargetOfAnnotation(from-link) = ?",                              "IsTargetOfAnnotation(from-link) = ?",
1586                              [$featureID], ['Annotation(time)', 'Annotation(annotation)']);                              [$featureID], ['Annotation(time)', 'Annotation(annotation)',
1587                                               'MadeAnnotation(from-link)']);
1588      # Declare the return hash.      # Declare the return hash.
1589      my %retVal;      my %retVal;
     # Declare a hash for insuring we only make one assignment per user.  
     my %timeHash = ();  
1590      # Now we sort the assignments by timestamp in reverse.      # Now we sort the assignments by timestamp in reverse.
1591      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;
1592      # Loop until we run out of annotations.      # Loop until we run out of annotations.
1593      for my $annotation (@sortedQuery) {      for my $annotation (@sortedQuery) {
1594          # Get the annotation fields.          # Get the annotation fields.
1595          my ($timeStamp, $text) = @{$annotation};          my ($timeStamp, $text, $user) = @{$annotation};
1596          # Check to see if this is a functional assignment.          # Check to see if this is a functional assignment.
1597          my ($user, $function) = _ParseAssignment($text);          my ($actualUser, $function) = _ParseAssignment($user, $text);
1598          if ($user && ! exists $timeHash{$user}) {          if ($actualUser && ! exists $retVal{$actualUser}) {
1599              # Here it is a functional assignment and there has been no              # Here it is a functional assignment and there has been no
1600              # previous assignment for this user, so we stuff it in the              # previous assignment for this user, so we stuff it in the
1601              # return hash.              # return hash.
1602              $retVal{$function} = $user;              $retVal{$actualUser} = $function;
             # Insure we don't assign to this user again.  
             $timeHash{$user} = 1;  
1603          }          }
1604      }      }
1605      # Return the hash of assignments found.      # Return the hash of assignments found.
# Line 1131  Line 1608 
1608    
1609  =head3 FunctionOf  =head3 FunctionOf
1610    
1611  C<< my $functionText = $sprout->FunctionOf($featureID, $userID); >>      my $functionText = $sprout->FunctionOf($featureID, $userID);
1612    
1613  Return the most recently-determined functional assignment of a particular feature.  Return the most recently-determined functional assignment of a particular feature.
1614    
1615  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
1616  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
1617  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.  
1618    
1619  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
1620  recent one by at least one of the trusted users. If no trusted user list is available, then  recent one by at least one of the trusted users. If no trusted user list is available, then
# Line 1159  Line 1633 
1633    
1634  =item userID (optional)  =item userID (optional)
1635    
1636  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
1637  C<FIG> assignment will be returned.  functional assignment in the B<Feature> table will be returned.
1638    
1639  =item RETURN  =item RETURN
1640    
# Line 1177  Line 1651 
1651      my $retVal;      my $retVal;
1652      # Determine the ID type.      # Determine the ID type.
1653      if ($featureID =~ m/^fig\|/) {      if ($featureID =~ m/^fig\|/) {
1654          # Here we have a FIG feature ID. We must build the list of trusted          # Here we have a FIG feature ID.
1655          # users.          if (!$userID) {
1656                # Use the primary assignment.
1657                ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(assignment)']);
1658            } else {
1659                # We must build the list of trusted users.
1660          my %trusteeTable = ();          my %trusteeTable = ();
1661          # Check the user ID.          # Check the user ID.
1662          if (!$userID) {          if (!$userID) {
# Line 1200  Line 1678 
1678              }              }
1679          }          }
1680          # 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.
1681          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation'],              my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1682                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1683                                 [$featureID]);                                 [$featureID]);
1684          my $timeSelected = 0;          my $timeSelected = 0;
1685          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1686          while (my $annotation = $query->Fetch()) {          while (my $annotation = $query->Fetch()) {
1687              # Get the annotation text.              # Get the annotation text.
1688              my ($text, $time) = $annotation->Values(['Annotation(annotation)','Annotation(time)']);                  my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1689                                                             'Annotation(time)', 'MadeAnnotation(from-link)']);
1690              # 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.
1691              my ($user, $function) = _ParseAssignment($text);                  my ($actualUser, $function) = _ParseAssignment($user, $text);
1692              if ($user) {                  Trace("Assignment user is $actualUser, text is $function.") if T(4);
1693                    if ($actualUser) {
1694                  # Here it is a functional assignment. Check the time and the user                  # Here it is a functional assignment. Check the time and the user
1695                  # name. The time must be recent and the user must be trusted.                  # name. The time must be recent and the user must be trusted.
1696                  if ((exists $trusteeTable{$user}) && ($time > $timeSelected)) {                      if ((exists $trusteeTable{$actualUser}) && ($time > $timeSelected)) {
1697                      $retVal = $function;                      $retVal = $function;
1698                      $timeSelected = $time;                      $timeSelected = $time;
1699                  }                  }
1700              }              }
1701          }          }
1702            }
1703      } else {      } else {
1704          # Here we have a non-FIG feature ID. In this case the user ID does not          # Here we have a non-FIG feature ID. In this case the user ID does not
1705          # matter. We simply get the information from the External Alias Function          # matter. We simply get the information from the External Alias Function
# Line 1229  Line 1710 
1710      return $retVal;      return $retVal;
1711  }  }
1712    
1713    =head3 FunctionsOf
1714    
1715        my @functionList = $sprout->FunctionOf($featureID, $userID);
1716    
1717    Return the functional assignments of a particular feature.
1718    
1719    The functional assignment is handled differently depending on the type of feature. If
1720    the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional
1721    assignment is a type of annotation. The format of an assignment is described in
1722    L</ParseAssignment>. Its worth noting that we cannot filter on the content of the
1723    annotation itself because it's a text field; however, this is not a big problem because
1724    most features only have a small number of annotations.
1725    
1726    If the feature is B<not> identified by a FIG ID, then the functional assignment
1727    information is taken from the B<ExternalAliasFunc> table. If the table does
1728    not contain an entry for the feature, an empty list is returned.
1729    
1730    =over 4
1731    
1732    =item featureID
1733    
1734    ID of the feature whose functional assignments are desired.
1735    
1736    =item RETURN
1737    
1738    Returns a list of 2-tuples, each consisting of a user ID and the text of an assignment by
1739    that user.
1740    
1741    =back
1742    
1743    =cut
1744    #: Return Type @@;
1745    sub FunctionsOf {
1746        # Get the parameters.
1747        my ($self, $featureID) = @_;
1748        # Declare the return value.
1749        my @retVal = ();
1750        # Determine the ID type.
1751        if ($featureID =~ m/^fig\|/) {
1752            # Here we have a FIG feature ID. We must build the list of trusted
1753            # users.
1754            my %trusteeTable = ();
1755            # Build a query for all of the feature's annotations, sorted by date.
1756            my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1757                                   "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1758                                   [$featureID]);
1759            my $timeSelected = 0;
1760            # Loop until we run out of annotations.
1761            while (my $annotation = $query->Fetch()) {
1762                # Get the annotation text.
1763                my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1764                                                                'Annotation(time)',
1765                                                                'MadeAnnotation(user)']);
1766                # Check to see if this is a functional assignment for a trusted user.
1767                my ($actualUser, $function) = _ParseAssignment($user, $text);
1768                if ($actualUser) {
1769                    # Here it is a functional assignment.
1770                    push @retVal, [$actualUser, $function];
1771                }
1772            }
1773        } else {
1774            # Here we have a non-FIG feature ID. In this case the user ID does not
1775            # matter. We simply get the information from the External Alias Function
1776            # table.
1777            my @assignments = $self->GetEntityValues('ExternalAliasFunc', $featureID,
1778                                                     ['ExternalAliasFunc(func)']);
1779            push @retVal, map { ['master', $_] } @assignments;
1780        }
1781        # Return the assignments found.
1782        return @retVal;
1783    }
1784    
1785  =head3 BBHList  =head3 BBHList
1786    
1787  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>      my $bbhHash = $sprout->BBHList($genomeID, \@featureList);
1788    
1789  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
1790  on a specified target genome.  on a specified target genome.
# Line 1262  Line 1815 
1815      my %retVal = ();      my %retVal = ();
1816      # Loop through the incoming features.      # Loop through the incoming features.
1817      for my $featureID (@{$featureList}) {      for my $featureID (@{$featureList}) {
1818          # Create a query to get the feature's best hit.          # Ask the server for the feature's best hit.
1819          my $query = $self->Get(['IsBidirectionalBestHitOf'],          my @bbhData = FIGRules::BBHData($featureID);
1820                                 "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?",          # Peel off the BBHs found.
1821                                 [$featureID, $genomeID]);          my @found = ();
1822          # Look for the best hit.          for my $bbh (@bbhData) {
1823          my $bbh = $query->Fetch;              my $fid = $bbh->[0];
1824          if ($bbh) {              my $bbGenome = $self->GenomeOf($fid);
1825              my ($targetFeature) = $bbh->Value('IsBidirectionalBestHitOf(to-link)');              if ($bbGenome eq $genomeID) {
1826              $retVal{$featureID} = $targetFeature;                  push @found, $fid;
1827                }
1828          }          }
1829            $retVal{$featureID} = \@found;
1830      }      }
1831      # Return the mapping.      # Return the mapping.
1832      return \%retVal;      return \%retVal;
# Line 1279  Line 1834 
1834    
1835  =head3 SimList  =head3 SimList
1836    
1837  C<< my %similarities = $sprout->SimList($featureID, $count); >>      my %similarities = $sprout->SimList($featureID, $count);
1838    
1839  Return a list of the similarities to the specified feature.  Return a list of the similarities to the specified feature.
1840    
1841  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.  
1842    
1843  =over 4  =over 4
1844    
# Line 1304  Line 1858 
1858      # Get the parameters.      # Get the parameters.
1859      my ($self, $featureID, $count) = @_;      my ($self, $featureID, $count) = @_;
1860      # Ask for the best hits.      # Ask for the best hits.
1861      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);  
1862      # Create the return value.      # Create the return value.
1863      my %retVal = ();      my %retVal = ();
1864      for my $tuple (@lists) {      for my $tuple (@lists) {
# Line 1317  Line 1868 
1868      return %retVal;      return %retVal;
1869  }  }
1870    
   
   
1871  =head3 IsComplete  =head3 IsComplete
1872    
1873  C<< my $flag = $sprout->IsComplete($genomeID); >>      my $flag = $sprout->IsComplete($genomeID);
1874    
1875  Return TRUE if the specified genome is complete, else FALSE.  Return TRUE if the specified genome is complete, else FALSE.
1876    
# Line 1349  Line 1898 
1898      my $genomeData = $self->GetEntity('Genome', $genomeID);      my $genomeData = $self->GetEntity('Genome', $genomeID);
1899      if ($genomeData) {      if ($genomeData) {
1900          # The genome exists, so get the completeness flag.          # The genome exists, so get the completeness flag.
1901          ($retVal) = $genomeData->Value('complete');          $retVal = $genomeData->PrimaryValue('Genome(complete)');
1902      }      }
1903      # Return the result.      # Return the result.
1904      return $retVal;      return $retVal;
# Line 1357  Line 1906 
1906    
1907  =head3 FeatureAliases  =head3 FeatureAliases
1908    
1909  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>      my @aliasList = $sprout->FeatureAliases($featureID);
1910    
1911  Return a list of the aliases for a specified feature.  Return a list of the aliases for a specified feature.
1912    
# Line 1380  Line 1929 
1929      # Get the parameters.      # Get the parameters.
1930      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
1931      # Get the desired feature's aliases      # Get the desired feature's aliases
1932      my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(alias)']);      my @retVal = $self->GetFlat(['IsAliasOf'], "IsAliasOf(to-link) = ?", [$featureID], 'IsAliasOf(from-link)');
1933      # Return the result.      # Return the result.
1934      return @retVal;      return @retVal;
1935  }  }
1936    
1937  =head3 GenomeOf  =head3 GenomeOf
1938    
1939  C<< my $genomeID = $sprout->GenomeOf($featureID); >>      my $genomeID = $sprout->GenomeOf($featureID);
1940    
1941  Return the genome that contains a specified feature.  Return the genome that contains a specified feature or contig.
1942    
1943  =over 4  =over 4
1944    
1945  =item featureID  =item featureID
1946    
1947  ID of the feature whose genome is desired.  ID of the feature or contig whose genome is desired.
1948    
1949  =item RETURN  =item RETURN
1950    
1951  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
1952  an undefined value.  found, returns an undefined value.
1953    
1954  =back  =back
1955    
# Line 1409  Line 1958 
1958  sub GenomeOf {  sub GenomeOf {
1959      # Get the parameters.      # Get the parameters.
1960      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]);  
1961      # Declare the return value.      # Declare the return value.
1962      my $retVal;      my $retVal;
1963      # Get the genome ID.      # Parse the genome ID from the feature ID.
1964      if (my $relationship = $query->Fetch()) {      if ($featureID =~ /^fig\|(\d+\.\d+)/) {
1965          ($retVal) = $relationship->Value('HasContig(from-link)');          $retVal = $1;
1966        } else {
1967            # Find the feature by alias.
1968            my ($realFeatureID) = $self->FeaturesByAlias($featureID);
1969            if ($realFeatureID && $realFeatureID =~ /^fig\|(\d+\.\d+)/) {
1970                $retVal = $1;
1971            } else {
1972                # Use the external table.
1973                my ($org) = $self->GetFlat(['ExternalAliasOrg'], "ExternalAliasOrg(id) = ?",
1974                                           [$featureID], "ExternalAliasOrg(org)");
1975                if ($org) {
1976                    $retVal = $org;
1977                } else {
1978                    Confess("Invalid feature ID $featureID.");
1979                }
1980            }
1981      }      }
1982      # Return the value found.      # Return the value found.
1983      return $retVal;      return $retVal;
# Line 1423  Line 1985 
1985    
1986  =head3 CoupledFeatures  =head3 CoupledFeatures
1987    
1988  C<< my %coupleHash = $sprout->CoupledFeatures($featureID); >>      my %coupleHash = $sprout->CoupledFeatures($featureID);
1989    
1990  Return the features functionally coupled with a specified feature. Features are considered  Return the features functionally coupled with a specified feature. Features are considered
1991  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 2007 
2007  sub CoupledFeatures {  sub CoupledFeatures {
2008      # Get the parameters.      # Get the parameters.
2009      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
2010      # Create a query to retrieve the functionally-coupled features.      # Ask the coupling server for the data.
2011      my $query = $self->Get(['ParticipatesInCoupling', 'Coupling'],      Trace("Looking for features coupled to $featureID.") if T(coupling => 3);
2012                             "ParticipatesInCoupling(from-link) = ?", [$featureID]);      my @rawPairs = FIGRules::NetCouplingData('coupled_to', id1 => $featureID);
2013      # This value will be set to TRUE if we find at least one coupled feature.      Trace(scalar(@rawPairs) . " couplings returned.") if T(coupling => 3);
2014      my $found = 0;      # Form them into a hash.
     # Create the return hash.  
2015      my %retVal = ();      my %retVal = ();
2016      # Retrieve the relationship records and store them in the hash.      for my $pair (@rawPairs) {
2017      while (my $clustering = $query->Fetch()) {          # Get the feature ID and score.
2018          # Get the ID and score of the coupling.          my ($featureID2, $score) = @{$pair};
2019          my ($couplingID, $score) = $clustering->Values(['Coupling(id)',          # Only proceed if the feature is in NMPDR.
2020                                                          'Coupling(score)']);          if ($self->_CheckFeature($featureID2)) {
2021          # The coupling ID contains the two feature IDs separated by a space. We use              $retVal{$featureID2} = $score;
2022          # 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;  
2023      }      }
2024      # Functional coupling is reflexive. If we found at least one coupled feature, we must add      # Functional coupling is reflexive. If we found at least one coupled feature, we must add
2025      # the incoming feature as well.      # the incoming feature as well.
2026      if ($found) {      if (keys %retVal) {
2027          $retVal{$featureID} = 9999;          $retVal{$featureID} = 9999;
2028      }      }
2029      # Return the hash.      # Return the hash.
# Line 1476  Line 2032 
2032    
2033  =head3 CouplingEvidence  =head3 CouplingEvidence
2034    
2035  C<< my @evidence = $sprout->CouplingEvidence($peg1, $peg2); >>      my @evidence = $sprout->CouplingEvidence($peg1, $peg2);
2036    
2037  Return the evidence for a functional coupling.  Return the evidence for a functional coupling.
2038    
# Line 1524  Line 2080 
2080      my ($self, $peg1, $peg2) = @_;      my ($self, $peg1, $peg2) = @_;
2081      # Declare the return variable.      # Declare the return variable.
2082      my @retVal = ();      my @retVal = ();
2083      # Our first task is to find out the nature of the coupling: whether or not      # Get the coupling and evidence data.
2084      # it exists, its score, and whether the features are stored in the same      my @rawData = FIGRules::NetCouplingData('coupling_evidence', id1 => $peg1, id2 => $peg2);
2085      # order as the ones coming in.      # Loop through the raw data, saving the ones that are in NMPDR genomes.
2086      my ($couplingID, $inverted, $score) = $self->GetCoupling($peg1, $peg2);      for my $rawTuple (@rawData) {
2087      # Only proceed if a coupling exists.          if ($self->_CheckFeature($rawTuple->[0]) && $self->_CheckFeature($rawTuple->[1])) {
2088      if ($couplingID) {              push @retVal, $rawTuple;
2089          # 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);  
2090      }      }
2091      # Return the result.      # Return the result.
2092      return @retVal;      return @retVal;
2093  }  }
2094    
2095  =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.  
2096    
2097  =over 4      my $id = $sprout->GetSynonymGroup($fid);
2098    
2099  =item peg1  Return the synonym group name for the specified feature.
2100    
2101  ID of the feature of interest.  =over 4
2102    
2103  =item peg2  =item fid
2104    
2105  ID of the potentially coupled feature.  ID of the feature whose synonym group is desired.
2106    
2107  =item RETURN  =item RETURN
2108    
2109  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
2110  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>.  
2111    
2112  =back  =back
2113    
2114  =cut  =cut
2115  #: Return Type $%@;  
2116  sub GetCoupling {  sub GetSynonymGroup {
2117      # Get the parameters.      # Get the parameters.
2118      my ($self, $peg1, $peg2) = @_;      my ($self, $fid) = @_;
2119      # Declare the return values. We'll start with the coupling ID and undefine the      # Declare the return variable.
2120      # flag and score until we have more information.      my $retVal;
2121      my ($retVal, $inverted, $score) = (CouplingID($peg1, $peg2), undef, undef);      # Find the synonym group.
2122      # Find the coupling data.      my @groups = $self->GetFlat(['IsSynonymGroupFor'], "IsSynonymGroupFor(to-link) = ?",
2123      my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'],                                     [$fid], 'IsSynonymGroupFor(from-link)');
                                  "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)",  
                                  [$retVal], ["ParticipatesInCoupling(from-link)", "Coupling(score)"]);  
2124      # Check to see if we found anything.      # Check to see if we found anything.
2125      if (!@pegs) {      if (@groups) {
2126          Trace("No coupling found.") if T(Coupling => 4);          $retVal = $groups[0];
         # No coupling, so undefine the return value.  
         $retVal = undef;  
2127      } else {      } else {
2128          # 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);  
2129      }      }
2130      # Return the result.      # Return the result.
2131      return ($retVal, $inverted, $score);      return $retVal;
2132  }  }
2133    
2134  =head3 CouplingID  =head3 GetBoundaries
2135    
2136  C<< my $couplingID = Sprout::CouplingID($peg1, $peg2); >>      my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList);
2137    
2138  Return the coupling ID for a pair of feature IDs.  Determine the begin and end boundaries for the locations in a list. All of the
2139    locations must belong to the same contig and have mostly the same direction in
2140  The coupling ID is currently computed by joining the feature IDs in  order for this method to produce a meaningful result. The resulting
2141  sorted order with a space. Client modules (that is, modules which  begin/end pair will contain all of the bases in any of the locations.
 use Sprout) should not, however, count on this always being the  
 case. This method provides a way for abstracting the concept of a  
 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")>.  
2142    
2143  =over 4  =over 4
2144    
2145  =item peg1  =item locList
   
 First feature of interest.  
   
 =item peg2  
2146    
2147  Second feature of interest.  List of locations to process.
2148    
2149  =item RETURN  =item RETURN
2150    
2151  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,
2152  the two specified PEGs.  and the ending boundary. The beginning boundary will be left of the
2153    end for mostly-forward locations and right of the end for mostly-backward
2154    locations.
2155    
2156  =back  =back
2157    
2158  =cut  =cut
 #: Return Type $;  
 sub CouplingID {  
     return join " ", sort @_;  
 }  
   
 =head3 GetEntityTypes  
2159    
2160  C<< my @entityList = $sprout->GetEntityTypes(); >>  sub GetBoundaries {
   
 Return the list of supported entity types.  
   
 =cut  
 #: Return Type @;  
 sub GetEntityTypes {  
2161      # Get the parameters.      # Get the parameters.
2162      my ($self) = @_;      my ($self, @locList) = @_;
2163      # Get the underlying database object.      # Set up the counters used to determine the most popular direction.
2164      my $erdb = $self->{_erdb};      my %counts = ( '+' => 0, '-' => 0 );
2165      # Get its entity type list.      # Get the last location and parse it.
2166      my @retVal = $erdb->GetEntityTypes();      my $locObject = BasicLocation->new(pop @locList);
2167        # Prime the loop with its data.
2168        my ($contig, $beg, $end) = ($locObject->Contig, $locObject->Left, $locObject->Right);
2169        # Count its direction.
2170        $counts{$locObject->Dir}++;
2171        # Loop through the remaining locations. Note that in most situations, this loop
2172        # will not iterate at all, because most of the time we will be dealing with a
2173        # singleton list.
2174        for my $loc (@locList) {
2175            # Create a location object.
2176            my $locObject = BasicLocation->new($loc);
2177            # Count the direction.
2178            $counts{$locObject->Dir}++;
2179            # Get the left end and the right end.
2180            my $left = $locObject->Left;
2181            my $right = $locObject->Right;
2182            # Merge them into the return variables.
2183            if ($left < $beg) {
2184                $beg = $left;
2185            }
2186            if ($right > $end) {
2187                $end = $right;
2188            }
2189        }
2190        # If the most common direction is reverse, flip the begin and end markers.
2191        if ($counts{'-'} > $counts{'+'}) {
2192            ($beg, $end) = ($end, $beg);
2193        }
2194        # Return the result.
2195        return ($contig, $beg, $end);
2196  }  }
2197    
2198  =head3 ReadFasta  =head3 ReadFasta
2199    
2200  C<< my %sequenceData = Sprout::ReadFasta($fileName, $prefix); >>      my %sequenceData = Sprout::ReadFasta($fileName, $prefix);
2201    
2202  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
2203  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 2263 
2263    
2264  =head3 FormatLocations  =head3 FormatLocations
2265    
2266  C<< my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat); >>      my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat);
2267    
2268  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
2269  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 2328 
2328    
2329  =head3 DumpData  =head3 DumpData
2330    
2331  C<< $sprout->DumpData(); >>      $sprout->DumpData();
2332    
2333  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.
2334    
# Line 1814  Line 2340 
2340      # Get the data directory name.      # Get the data directory name.
2341      my $outputDirectory = $self->{_options}->{dataDir};      my $outputDirectory = $self->{_options}->{dataDir};
2342      # Dump the relations.      # Dump the relations.
2343      $self->{_erdb}->DumpRelations($outputDirectory);      $self->DumpRelations($outputDirectory);
2344  }  }
2345    
2346  =head3 XMLFileName  =head3 XMLFileName
2347    
2348  C<< my $fileName = $sprout->XMLFileName(); >>      my $fileName = $sprout->XMLFileName();
2349    
2350  Return the name of this database's XML definition file.  Return the name of this database's XML definition file.
2351    
# Line 1830  Line 2356 
2356      return $self->{_xmlName};      return $self->{_xmlName};
2357  }  }
2358    
2359    =head3 GetGenomeNameData
2360    
2361        my ($genus, $species, $strain) = $sprout->GenomeNameData($genomeID);
2362    
2363    Return the genus, species, and unique characterization for a genome. This
2364    is similar to L</GenusSpecies>, with the exception that it returns the
2365    values in three seperate fields.
2366    
2367    =over 4
2368    
2369    =item genomeID
2370    
2371    ID of the genome whose name data is desired.
2372    
2373    =item RETURN
2374    
2375    Returns a three-element list, consisting of the genus, species, and strain
2376    of the specified genome. If the genome is not found, an error occurs.
2377    
2378    =back
2379    
2380    =cut
2381    
2382    sub GetGenomeNameData {
2383        # Get the parameters.
2384        my ($self, $genomeID) = @_;
2385        # Get the desired values.
2386        my ($genus, $species, $strain) = $self->GetEntityValues('Genome', $genomeID =>
2387                                                                [qw(Genome(genus) Genome(species) Genome(unique-characterization))]);
2388        # Throw an error if they were not found.
2389        if (! defined $genus) {
2390            Confess("Genome $genomeID not found in database.");
2391        }
2392        # Return the results.
2393        return ($genus, $species, $strain);
2394    }
2395    
2396    =head3 GetGenomeByNameData
2397    
2398        my @genomes = $sprout->GetGenomeByNameData($genus, $species, $strain);
2399    
2400    Return a list of the IDs of the genomes with the specified genus,
2401    species, and strain. In almost every case, there will be either zero or
2402    one IDs returned; however, two or more IDs could be returned if there are
2403    multiple versions of the genome in the database.
2404    
2405    =over 4
2406    
2407    =item genus
2408    
2409    Genus of the desired genome.
2410    
2411    =item species
2412    
2413    Species of the desired genome.
2414    
2415    =item strain
2416    
2417    Strain (unique characterization) of the desired genome. This may be an empty
2418    string, in which case it is presumed that the desired genome has no strain
2419    specified.
2420    
2421    =item RETURN
2422    
2423    Returns a list of the IDs of the genomes having the specified genus, species, and
2424    strain.
2425    
2426    =back
2427    
2428    =cut
2429    
2430    sub GetGenomeByNameData {
2431        # Get the parameters.
2432        my ($self, $genus, $species, $strain) = @_;
2433        # Try to find the genomes.
2434        my @retVal = $self->GetFlat(['Genome'], "Genome(genus) = ? AND Genome(species) = ? AND Genome(unique-characterization) = ?",
2435                                    [$genus, $species, $strain], 'Genome(id)');
2436        # Return the result.
2437        return @retVal;
2438    }
2439    
2440  =head3 Insert  =head3 Insert
2441    
2442  C<< $sprout->Insert($objectType, \%fieldHash); >>      $sprout->Insert($objectType, \%fieldHash);
2443    
2444  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
2445  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 2448 
2448  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
2449  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>.
2450    
2451  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']});
2452    
2453  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
2454  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>.
2455    
2456  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'});
2457    
2458  =over 4  =over 4
2459    
# Line 1866  Line 2473 
2473      # Get the parameters.      # Get the parameters.
2474      my ($self, $objectType, $fieldHash) = @_;      my ($self, $objectType, $fieldHash) = @_;
2475      # Call the underlying method.      # Call the underlying method.
2476      $self->{_erdb}->InsertObject($objectType, $fieldHash);      $self->InsertObject($objectType, $fieldHash);
2477  }  }
2478    
2479  =head3 Annotate  =head3 Annotate
2480    
2481  C<< my $ok = $sprout->Annotate($fid, $timestamp, $user, $text); >>      my $ok = $sprout->Annotate($fid, $timestamp, $user, $text);
2482    
2483  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
2484  specified feature and user.  specified feature and user.
# Line 1925  Line 2532 
2532    
2533  =head3 AssignFunction  =head3 AssignFunction
2534    
2535  C<< my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser); >>      my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser);
2536    
2537  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
2538  format is described in L</ParseAssignment>.  format is described in L</ParseAssignment>.
# Line 1985  Line 2592 
2592    
2593  =head3 FeaturesByAlias  =head3 FeaturesByAlias
2594    
2595  C<< my @features = $sprout->FeaturesByAlias($alias); >>      my @features = $sprout->FeaturesByAlias($alias);
2596    
2597  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
2598  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 2626 
2626          push @retVal, $mappedAlias;          push @retVal, $mappedAlias;
2627      } else {      } else {
2628          # 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.
2629          @retVal = $self->GetFlat(['Feature'], 'Feature(alias) = ?', [$mappedAlias], 'Feature(id)');          @retVal = $self->GetFlat(['IsAliasOf'], 'IsAliasOf(from-link) = ?', [$mappedAlias], 'IsAliasOf(to-link)');
2630      }      }
2631      # Return the result.      # Return the result.
2632      return @retVal;      return @retVal;
2633  }  }
2634    
 =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;  
 }  
   
2635  =head3 FeatureTranslation  =head3 FeatureTranslation
2636    
2637  C<< my $translation = $sprout->FeatureTranslation($featureID); >>      my $translation = $sprout->FeatureTranslation($featureID);
2638    
2639  Return the translation of a feature.  Return the translation of a feature.
2640    
# Line 2090  Line 2662 
2662    
2663  =head3 Taxonomy  =head3 Taxonomy
2664    
2665  C<< my @taxonomyList = $sprout->Taxonomy($genome); >>      my @taxonomyList = $sprout->Taxonomy($genome);
2666    
2667  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
2668  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>,
2669  or C<Eukaryote>) to sub-species. For example,  or C<Eukaryote>) to sub-species. For example,
2670    
2671  C<< (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12) >>      (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12)
2672    
2673  =over 4  =over 4
2674    
# Line 2131  Line 2703 
2703    
2704  =head3 CrudeDistance  =head3 CrudeDistance
2705    
2706  C<< my $distance = $sprout->CrudeDistance($genome1, $genome2); >>      my $distance = $sprout->CrudeDistance($genome1, $genome2);
2707    
2708  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
2709  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 2183  Line 2755 
2755    
2756  =head3 RoleName  =head3 RoleName
2757    
2758  C<< my $roleName = $sprout->RoleName($roleID); >>      my $roleName = $sprout->RoleName($roleID);
2759    
2760  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
2761  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 2789 
2789    
2790  =head3 RoleDiagrams  =head3 RoleDiagrams
2791    
2792  C<< my @diagrams = $sprout->RoleDiagrams($roleID); >>      my @diagrams = $sprout->RoleDiagrams($roleID);
2793    
2794  Return a list of the diagrams containing a specified functional role.  Return a list of the diagrams containing a specified functional role.
2795    
# Line 2245  Line 2817 
2817      return @retVal;      return @retVal;
2818  }  }
2819    
 =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;  
 }  
   
2820  =head3 FeatureProperties  =head3 FeatureProperties
2821    
2822  C<< my @properties = $sprout->FeatureProperties($featureID); >>      my @properties = $sprout->FeatureProperties($featureID);
2823    
2824  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
2825  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
2826  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
2827  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
2828  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.  
2829    
2830  =over 4  =over 4
2831    
# Line 2348  Line 2835 
2835    
2836  =item RETURN  =item RETURN
2837    
2838  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.  
2839    
2840  =back  =back
2841    
# Line 2359  Line 2845 
2845      # Get the parameters.      # Get the parameters.
2846      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
2847      # Get the properties.      # Get the properties.
2848      my @retVal = $self->GetAll(['HasProperty', 'Property'], "HasProperty(from-link) = ?", [$featureID],      my @attributes = $self->{_ca}->GetAttributes($featureID);
2849                              ['Property(property-name)', 'Property(property-value)',      # Strip the feature ID off each tuple.
2850                               'HasProperty(evidence)']);      my @retVal = ();
2851        for my $attributeRow (@attributes) {
2852            shift @{$attributeRow};
2853            push @retVal, $attributeRow;
2854        }
2855      # Return the resulting list.      # Return the resulting list.
2856      return @retVal;      return @retVal;
2857  }  }
2858    
2859  =head3 DiagramName  =head3 DiagramName
2860    
2861  C<< my $diagramName = $sprout->DiagramName($diagramID); >>      my $diagramName = $sprout->DiagramName($diagramID);
2862    
2863  Return the descriptive name of a diagram.  Return the descriptive name of a diagram.
2864    
# Line 2394  Line 2884 
2884      return $retVal;      return $retVal;
2885  }  }
2886    
2887    =head3 PropertyID
2888    
2889        my $id = $sprout->PropertyID($propName, $propValue);
2890    
2891    Return the ID of the specified property name and value pair, if the
2892    pair exists. Only a small subset of the FIG attributes are stored as
2893    Sprout properties, mostly for use in search optimization.
2894    
2895    =over 4
2896    
2897    =item propName
2898    
2899    Name of the desired property.
2900    
2901    =item propValue
2902    
2903    Value expected for the desired property.
2904    
2905    =item RETURN
2906    
2907    Returns the ID of the name/value pair, or C<undef> if the pair does not exist.
2908    
2909    =back
2910    
2911    =cut
2912    
2913    sub PropertyID {
2914        # Get the parameters.
2915        my ($self, $propName, $propValue) = @_;
2916        # Try to find the ID.
2917        my ($retVal) = $self->GetFlat(['Property'],
2918                                      "Property(property-name) = ? AND Property(property-value) = ?",
2919                                      [$propName, $propValue], 'Property(id)');
2920        # Return the result.
2921        return $retVal;
2922    }
2923    
2924  =head3 MergedAnnotations  =head3 MergedAnnotations
2925    
2926  C<< my @annotationList = $sprout->MergedAnnotations(\@list); >>      my @annotationList = $sprout->MergedAnnotations(\@list);
2927    
2928  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
2929  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 2972 
2972    
2973  =head3 RoleNeighbors  =head3 RoleNeighbors
2974    
2975  C<< my @roleList = $sprout->RoleNeighbors($roleID); >>      my @roleList = $sprout->RoleNeighbors($roleID);
2976    
2977  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
2978  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 3015 
3015    
3016  =head3 FeatureLinks  =head3 FeatureLinks
3017    
3018  C<< my @links = $sprout->FeatureLinks($featureID); >>      my @links = $sprout->FeatureLinks($featureID);
3019    
3020  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
3021  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 3046 
3046    
3047  =head3 SubsystemsOf  =head3 SubsystemsOf
3048    
3049  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>      my %subsystems = $sprout->SubsystemsOf($featureID);
3050    
3051  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
3052  to the roles the feature performs.  to the roles the feature performs.
# Line 2547  Line 3074 
3074                                      ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);                                      ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);
3075      # Create the return value.      # Create the return value.
3076      my %retVal = ();      my %retVal = ();
3077        # Build a hash to weed out duplicates. Sometimes the same PEG and role appears
3078        # in two spreadsheet cells.
3079        my %dupHash = ();
3080      # Loop through the results, adding them to the hash.      # Loop through the results, adding them to the hash.
3081      for my $record (@subsystems) {      for my $record (@subsystems) {
3082            # Get this subsystem and role.
3083          my ($subsys, $role) = @{$record};          my ($subsys, $role) = @{$record};
3084          if (exists $retVal{$subsys}) {          # Insure it's the first time for both.
3085            my $dupKey = "$subsys\n$role";
3086            if (! exists $dupHash{"$subsys\n$role"}) {
3087                $dupHash{$dupKey} = 1;
3088              push @{$retVal{$subsys}}, $role;              push @{$retVal{$subsys}}, $role;
         } else {  
             $retVal{$subsys} = [$role];  
3089          }          }
3090      }      }
3091      # Return the hash.      # Return the hash.
# Line 2562  Line 3094 
3094    
3095  =head3 SubsystemList  =head3 SubsystemList
3096    
3097  C<< my @subsystems = $sprout->SubsystemList($featureID); >>      my @subsystems = $sprout->SubsystemList($featureID);
3098    
3099  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
3100  feature participates. Unlike L</SubsystemsOf>, this method only returns the  feature participates. Unlike L</SubsystemsOf>, this method only returns the
# Line 2586  Line 3118 
3118      # Get the parameters.      # Get the parameters.
3119      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
3120      # Get the list of names.      # Get the list of names.
3121      my @retVal = $self->GetFlat(['ContainsFeature', 'HasSSCell'], "ContainsFeature(to-link) = ?",      ##HACK: we do a join to the Subsystem table because we have missing subsystems in
3122                                  [$featureID], 'HasSSCell(from-link)');      ## the Sprout database!
3123        my @retVal = $self->GetFlat(['HasRoleInSubsystem', 'Subsystem'], "HasRoleInSubsystem(from-link) = ?",
3124                                    [$featureID], 'HasRoleInSubsystem(to-link)');
3125        # Return the result, sorted.
3126        return sort @retVal;
3127    }
3128    
3129    =head3 GenomeSubsystemData
3130    
3131        my %featureData = $sprout->GenomeSubsystemData($genomeID);
3132    
3133    Return a hash mapping genome features to their subsystem roles.
3134    
3135    =over 4
3136    
3137    =item genomeID
3138    
3139    ID of the genome whose subsystem feature map is desired.
3140    
3141    =item RETURN
3142    
3143    Returns a hash mapping each feature of the genome to a list of 2-tuples. Eacb
3144    2-tuple contains a subsystem name followed by a role ID.
3145    
3146    =back
3147    
3148    =cut
3149    
3150    sub GenomeSubsystemData {
3151        # Get the parameters.
3152        my ($self, $genomeID) = @_;
3153        # Declare the return variable.
3154        my %retVal = ();
3155        # Get a list of the genome features that participate in subsystems. For each
3156        # feature we get its spreadsheet cells and the corresponding roles.
3157        my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf'],
3158                                 "HasFeature(from-link) = ?", [$genomeID],
3159                                 ['HasFeature(to-link)', 'IsRoleOf(to-link)', 'IsRoleOf(from-link)']);
3160        # Now we get a list of the spreadsheet cells and their associated subsystems. Subsystems
3161        # with an unknown variant code (-1) are skipped. Note the genome ID is at both ends of the
3162        # list. We use it at the beginning to get all the spreadsheet cells for the genome and
3163        # again at the end to filter out participation in subsystems with a negative variant code.
3164        my @cellData = $self->GetAll(['IsGenomeOf', 'HasSSCell', 'ParticipatesIn'],
3165                                     "IsGenomeOf(from-link) = ? AND ParticipatesIn(variant-code) >= 0 AND ParticipatesIn(from-link) = ?",
3166                                     [$genomeID, $genomeID], ['HasSSCell(to-link)', 'HasSSCell(from-link)']);
3167        # Now "@roleData" lists the spreadsheet cell and role for each of the genome's features.
3168        # "@cellData" lists the subsystem name for each of the genome's spreadsheet cells. We
3169        # link these two lists together to create the result. First, we want a hash mapping
3170        # spreadsheet cells to subsystem names.
3171        my %subHash = map { $_->[0] => $_->[1] } @cellData;
3172        # We loop through @cellData to build the hash.
3173        for my $roleEntry (@roleData) {
3174            # Get the data for this feature and cell.
3175            my ($fid, $cellID, $role) = @{$roleEntry};
3176            # Check for a subsystem name.
3177            my $subsys = $subHash{$cellID};
3178            if ($subsys) {
3179                # Insure this feature has an entry in the return hash.
3180                if (! exists $retVal{$fid}) { $retVal{$fid} = []; }
3181                # Merge in this new data.
3182                push @{$retVal{$fid}}, [$subsys, $role];
3183            }
3184        }
3185      # Return the result.      # Return the result.
3186      return @retVal;      return %retVal;
3187  }  }
3188    
3189  =head3 RelatedFeatures  =head3 RelatedFeatures
3190    
3191  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>      my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID);
3192    
3193  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
3194  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 3221 
3221      # Get the parameters.      # Get the parameters.
3222      my ($self, $featureID, $function, $userID) = @_;      my ($self, $featureID, $function, $userID) = @_;
3223      # 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.
3224      my @bbhFeatures = $self->GetFlat(['IsBidirectionalBestHitOf'],      my @bbhFeatures = map { $_->[0] } FIGRules::BBHData($featureID);
                                      "IsBidirectionalBestHitOf(from-link) = ?", [$featureID],  
                                      'IsBidirectionalBestHitOf(to-link)');  
3225      # 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
3226      # functional assignment.      # functional assignment.
3227      my @retVal = ();      my @retVal = ();
# Line 2647  Line 3239 
3239    
3240  =head3 TaxonomySort  =head3 TaxonomySort
3241    
3242  C<< my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs); >>      my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs);
3243    
3244  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
3245  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 3274 
3274          my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",          my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",
3275                                          [$fid], 'Genome(taxonomy)');                                          [$fid], 'Genome(taxonomy)');
3276          # Add this feature to the hash buffer.          # Add this feature to the hash buffer.
3277          Tracer::AddToListMap(\%hashBuffer, $taxonomy, $fid);          push @{$hashBuffer{$taxonomy}}, $fid;
3278      }      }
3279      # Sort the keys and get the elements.      # Sort the keys and get the elements.
3280      my @retVal = ();      my @retVal = ();
# Line 2693  Line 3285 
3285      return @retVal;      return @retVal;
3286  }  }
3287    
 =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;  
 }  
   
3288  =head3 Protein  =head3 Protein
3289    
3290  C<< my $protein = Sprout::Protein($sequence, $table); >>      my $protein = Sprout::Protein($sequence, $table);
3291    
3292  Translate a DNA sequence into a protein sequence.  Translate a DNA sequence into a protein sequence.
3293    
# Line 2884  Line 3357 
3357      # Loop through the input triples.      # Loop through the input triples.
3358      my $n = length $sequence;      my $n = length $sequence;
3359      for (my $i = 0; $i < $n; $i += 3) {      for (my $i = 0; $i < $n; $i += 3) {
3360          # Get the current triple from the sequence.          # Get the current triple from the sequence. Note we convert to
3361          my $triple = substr($sequence, $i, 3);          # upper case to insure a match.
3362            my $triple = uc substr($sequence, $i, 3);
3363          # Translate it using the table.          # Translate it using the table.
3364          my $protein = "X";          my $protein = "X";
3365          if (exists $table->{$triple}) { $protein = $table->{$triple}; }          if (exists $table->{$triple}) { $protein = $table->{$triple}; }
# Line 2899  Line 3373 
3373    
3374  =head3 LoadInfo  =head3 LoadInfo
3375    
3376  C<< my ($dirName, @relNames) = $sprout->LoadInfo(); >>      my ($dirName, @relNames) = $sprout->LoadInfo();
3377    
3378  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
3379  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 3387 
3387      # 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.
3388      my @retVal = ($self->{_options}->{dataDir});      my @retVal = ($self->{_options}->{dataDir});
3389      # Concatenate the table names.      # Concatenate the table names.
3390      push @retVal, $self->{_erdb}->GetTableNames();      push @retVal, $self->GetTableNames();
3391      # Return the result.      # Return the result.
3392      return @retVal;      return @retVal;
3393  }  }
3394    
3395  =head3 LowBBHs  =head3 BBHMatrix
3396    
3397  C<< my %bbhMap = $sprout->GoodBBHs($featureID, $cutoff); >>      my %bbhMap = $sprout->BBHMatrix($genomeID, $cutoff, @targets);
3398    
3399  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
3400  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
3401  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
3402    target genomes.
3403    
3404  =over 4  =over 4
3405    
3406  =item featureID  =item genomeID
3407    
3408  ID of the feature whose best hits are desired.  ID of the genome whose features are to be examined for bidirectional best hits.
3409    
3410  =item cutoff  =item cutoff
3411    
3412  Maximum permissible score for inclusion in the results.  A cutoff value. Only hits with a score lower than the cutoff will be returned.
3413    
3414    =item targets
3415    
3416    List of target genomes. Only pairs originating in the original
3417    genome and landing in one of the target genomes will be returned.
3418    
3419  =item RETURN  =item RETURN
3420    
3421  Returns a hash mapping feature IDs to scores.  Returns a hash mapping each feature in the original genome to a hash mapping its
3422    BBH pegs in the target genomes to their scores.
3423    
3424  =back  =back
3425    
3426  =cut  =cut
3427  #: Return Type %;  
3428  sub LowBBHs {  sub BBHMatrix {
3429      # Get the parsameters.      # Get the parameters.
3430      my ($self, $featureID, $cutoff) = @_;      my ($self, $genomeID, $cutoff, @targets) = @_;
3431      # Create the return hash.      # Declare the return variable.
3432      my %retVal = ();      my %retVal = ();
3433      # Create a query to get the desired BBHs.      # Ask for the BBHs.
3434      my @bbhList = $self->GetAll(['IsBidirectionalBestHitOf'],      my @bbhList = FIGRules::BatchBBHs("fig|$genomeID.%", $cutoff, @targets);
3435                                  'IsBidirectionalBestHitOf(sc) <= ? AND IsBidirectionalBestHitOf(from-link) = ?',      # We now have a set of 4-tuples that we need to convert into a hash of hashes.
3436                                  [$cutoff, $featureID],      for my $bbhData (@bbhList) {
3437                                  ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(sc)']);          my ($peg1, $peg2, $score) = @{$bbhData};
3438      # Form the results into the return hash.          if (! exists $retVal{$peg1}) {
3439      for my $pair (@bbhList) {              $retVal{$peg1} = { $peg2 => $score };
3440          $retVal{$pair->[0]} = $pair->[1];          } else {
3441                $retVal{$peg1}->{$peg2} = $score;
3442            }
3443      }      }
3444      # Return the result.      # Return the result.
3445      return %retVal;      return %retVal;
3446  }  }
3447    
 =head3 GetGroups  
3448    
3449  C<< my %groups = $sprout->GetGroups(\@groupList); >>  =head3 SimMatrix
3450    
3451  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.  
3452    
3453  =cut  Find all the similarities for the features of a genome in a
3454  #: Return Type %@;  specified list of target genomes. The return value will be a hash mapping
3455  sub GetGroups {  features in the original genome to their similarites in the
3456      # Get the parameters.  target genomes.
3457    
3458    =over 4
3459    
3460    =item genomeID
3461    
3462    ID of the genome whose features are to be examined for similarities.
3463    
3464    =item cutoff
3465    
3466    A cutoff value. Only hits with a score lower than the cutoff will be returned.
3467    
3468    =item targets
3469    
3470    List of target genomes. Only pairs originating in the original
3471    genome and landing in one of the target genomes will be returned.
3472    
3473    =item RETURN
3474    
3475    Returns a hash mapping each feature in the original genome to a hash mapping its
3476    similar pegs in the target genomes to their scores.
3477    
3478    =back
3479    
3480    =cut
3481    
3482    sub SimMatrix {
3483        # Get the parameters.
3484        my ($self, $genomeID, $cutoff, @targets) = @_;
3485        # Declare the return variable.
3486        my %retVal = ();
3487        # Get the list of features in the source organism.
3488        my @fids = $self->FeaturesOf($genomeID);
3489        # Ask for the sims. We only want similarities to fig features.
3490        my $simList = FIGRules::GetNetworkSims($self, \@fids, {}, 1000, $cutoff, "fig");
3491        if (! defined $simList) {
3492            Confess("Unable to retrieve similarities from server.");
3493        } else {
3494            Trace("Processing sims.") if T(3);
3495            # We now have a set of sims that we need to convert into a hash of hashes. First, we
3496            # Create a hash for the target genomes.
3497            my %targetHash = map { $_ => 1 } @targets;
3498            for my $simData (@{$simList}) {
3499                # Get the PEGs and the score.
3500                my ($peg1, $peg2, $score) = ($simData->id1, $simData->id2, $simData->psc);
3501                # Insure the second ID is in the target list.
3502                my ($genome2) = FIGRules::ParseFeatureID($peg2);
3503                if (exists $targetHash{$genome2}) {
3504                    # Here it is. Now we need to add it to the return hash. How we do that depends
3505                    # on whether or not $peg1 is new to us.
3506                    if (! exists $retVal{$peg1}) {
3507                        $retVal{$peg1} = { $peg2 => $score };
3508                    } else {
3509                        $retVal{$peg1}->{$peg2} = $score;
3510                    }
3511                }
3512            }
3513        }
3514        # Return the result.
3515        return %retVal;
3516    }
3517    
3518    
3519    =head3 LowBBHs
3520    
3521        my %bbhMap = $sprout->LowBBHs($featureID, $cutoff);
3522    
3523    Return the bidirectional best hits of a feature whose score is no greater than a
3524    specified cutoff value. A higher cutoff value will allow inclusion of hits with
3525    a greater score. The value returned is a map of feature IDs to scores.
3526    
3527    =over 4
3528    
3529    =item featureID
3530    
3531    ID of the feature whose best hits are desired.
3532    
3533    =item cutoff
3534    
3535    Maximum permissible score for inclusion in the results.
3536    
3537    =item RETURN
3538    
3539    Returns a hash mapping feature IDs to scores.
3540    
3541    =back
3542    
3543    =cut
3544    #: Return Type %;
3545    sub LowBBHs {
3546        # Get the parsameters.
3547        my ($self, $featureID, $cutoff) = @_;
3548        # Create the return hash.
3549        my %retVal = ();
3550        # Query for the desired BBHs.
3551        my @bbhList = FIGRules::BBHData($featureID, $cutoff);
3552        # Form the results into the return hash.
3553        for my $pair (@bbhList) {
3554            my $fid = $pair->[0];
3555            if ($self->Exists('Feature', $fid)) {
3556                $retVal{$fid} = $pair->[1];
3557            }
3558        }
3559        # Return the result.
3560        return %retVal;
3561    }
3562    
3563    =head3 Sims
3564    
3565        my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters);
3566    
3567    Get a list of similarities for a specified feature. Similarity information is not kept in the
3568    Sprout database; rather, they are retrieved from a network server. The similarities are
3569    returned as B<Sim> objects. A Sim object is actually a list reference that has been blessed
3570    so that its elements can be accessed by name.
3571    
3572    Similarities can be either raw or expanded. The raw similarities are basic
3573    hits between features with similar DNA. Expanding a raw similarity drags in any
3574    features considered substantially identical. So, for example, if features B<A1>,
3575    B<A2>, and B<A3> are all substantially identical to B<A>, then a raw similarity
3576    B<[C,A]> would be expanded to B<[C,A] [C,A1] [C,A2] [C,A3]>.
3577    
3578    =over 4
3579    
3580    =item fid
3581    
3582    ID of the feature whose similarities are desired, or reference to a list of IDs
3583    of features whose similarities are desired.
3584    
3585    =item maxN
3586    
3587    Maximum number of similarities to return.
3588    
3589    =item maxP
3590    
3591    Minumum allowable similarity score.
3592    
3593    =item select
3594    
3595    Selection criterion: C<raw> means only raw similarities are returned; C<fig>
3596    means only similarities to FIG features are returned; C<all> means all expanded
3597    similarities are returned; and C<figx> means similarities are expanded until the
3598    number of FIG features equals the maximum.
3599    
3600    =item max_expand
3601    
3602    The maximum number of features to expand.
3603    
3604    =item filters
3605    
3606    Reference to a hash containing filter information, or a subroutine that can be
3607    used to filter the sims.
3608    
3609    =item RETURN
3610    
3611    Returns a reference to a list of similarity objects, or C<undef> if an error
3612    occurred.
3613    
3614    =back
3615    
3616    =cut
3617    
3618    sub Sims {
3619        # Get the parameters.
3620        my ($self, $fid, $maxN, $maxP, $select, $max_expand, $filters) = @_;
3621        # Create the shim object to test for deleted FIDs.
3622        my $shim = FidCheck->new($self);
3623        # Ask the network for sims.
3624        my $retVal = FIGRules::GetNetworkSims($shim, $fid, {}, $maxN, $maxP, $select, $max_expand, $filters);
3625        # Return the result.
3626        return $retVal;
3627    }
3628    
3629    =head3 IsAllGenomes
3630    
3631        my $flag = $sprout->IsAllGenomes(\@list, \@checkList);
3632    
3633    Return TRUE if all genomes in the second list are represented in the first list at
3634    least one. Otherwise, return FALSE. If the second list is omitted, the first list is
3635    compared to a list of all the genomes.
3636    
3637    =over 4
3638    
3639    =item list
3640    
3641    Reference to the list to be compared to the second list.
3642    
3643    =item checkList (optional)
3644    
3645    Reference to the comparison target list. Every genome ID in this list must occur at
3646    least once in the first list. If this parameter is omitted, a list of all the genomes
3647    is used.
3648    
3649    =item RETURN
3650    
3651    Returns TRUE if every item in the second list appears at least once in the
3652    first list, else FALSE.
3653    
3654    =back
3655    
3656    =cut
3657    
3658    sub IsAllGenomes {
3659        # Get the parameters.
3660        my ($self, $list, $checkList) = @_;
3661        # Supply the checklist if it was omitted.
3662        $checkList = [$self->Genomes()] if ! defined($checkList);
3663        # Create a hash of the original list.
3664        my %testList = map { $_ => 1 } @{$list};
3665        # Declare the return variable. We assume that the representation
3666        # is complete and stop at the first failure.
3667        my $retVal = 1;
3668        my $n = scalar @{$checkList};
3669        for (my $i = 0; $retVal && $i < $n; $i++) {
3670            if (! $testList{$checkList->[$i]}) {
3671                $retVal = 0;
3672            }
3673        }
3674        # Return the result.
3675        return $retVal;
3676    }
3677    
3678    =head3 GetGroups
3679    
3680        my %groups = $sprout->GetGroups(\@groupList);
3681    
3682    Return a hash mapping each group to the IDs of the genomes in the group.
3683    A list of groups may be specified, in which case only those groups will be
3684    shown. Alternatively, if no parameter is supplied, all groups will be
3685    included. Genomes that are not in any group are omitted.
3686    
3687    =cut
3688    #: Return Type %@;
3689    sub GetGroups {
3690        # Get the parameters.
3691      my ($self, $groupList) = @_;      my ($self, $groupList) = @_;
3692      # Declare the return value.      # Declare the return value.
3693      my %retVal = ();      my %retVal = ();
# Line 2983  Line 3696 
3696          # Here we have a group list. Loop through them individually,          # Here we have a group list. Loop through them individually,
3697          # getting a list of the relevant genomes.          # getting a list of the relevant genomes.
3698          for my $group (@{$groupList}) {          for my $group (@{$groupList}) {
3699              my @genomeIDs = $self->GetFlat(['Genome'], "Genome(group-name) = ?",              my @genomeIDs = $self->GetFlat(['Genome'], "Genome(primary-group) = ?",
3700                  [$group], "Genome(id)");                  [$group], "Genome(id)");
3701              $retVal{$group} = \@genomeIDs;              $retVal{$group} = \@genomeIDs;
3702          }          }
# Line 2991  Line 3704 
3704          # 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
3705          # of the genome records, putting each one found into the appropriate          # of the genome records, putting each one found into the appropriate
3706          # 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
3707          # in groups are included in the return set.          # in real NMPDR groups are included in the return set.
3708          my @genomes = $self->GetAll(['Genome'], "Genome(group-name) > ' '", [],          my @genomes = $self->GetAll(['Genome'], "Genome(primary-group) <> ?",
3709                                      ['Genome(id)', 'Genome(group-name)']);                                      [$FIG_Config::otherGroup], ['Genome(id)', 'Genome(primary-group)']);
3710          # Loop through the genomes found.          # Loop through the genomes found.
3711          for my $genome (@genomes) {          for my $genome (@genomes) {
3712              # Pop this genome's ID off the current list.              # Get the genome ID and group, and add this genome to the group's list.
3713              my @groups = @{$genome};              my ($genomeID, $group) = @{$genome};
3714              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);  
             }  
3715          }          }
3716      }      }
3717      # Return the hash we just built.      # Return the hash we just built.
# Line 3012  Line 3720 
3720    
3721  =head3 MyGenomes  =head3 MyGenomes
3722    
3723  C<< my @genomes = Sprout::MyGenomes($dataDir); >>      my @genomes = Sprout::MyGenomes($dataDir);
3724    
3725  Return a list of the genomes to be included in the Sprout.  Return a list of the genomes to be included in the Sprout.
3726    
# Line 3044  Line 3752 
3752    
3753  =head3 LoadFileName  =head3 LoadFileName
3754    
3755  C<< my $fileName = Sprout::LoadFileName($dataDir, $tableName); >>      my $fileName = Sprout::LoadFileName($dataDir, $tableName);
3756    
3757  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
3758  directory.  directory.
# Line 3083  Line 3791 
3791      return $retVal;      return $retVal;
3792  }  }
3793    
3794    =head3 DeleteGenome
3795    
3796        my $stats = $sprout->DeleteGenome($genomeID, $testFlag);
3797    
3798    Delete a genome from the database.
3799    
3800    =over 4
3801    
3802    =item genomeID
3803    
3804    ID of the genome to delete
3805    
3806    =item testFlag
3807    
3808    If TRUE, then the DELETE statements will be traced, but no deletions will occur.
3809    
3810    =item RETURN
3811    
3812    Returns a statistics object describing the rows deleted.
3813    
3814    =back
3815    
3816    =cut
3817    #: Return Type $%;
3818    sub DeleteGenome {
3819        # Get the parameters.
3820        my ($self, $genomeID, $testFlag) = @_;
3821        # Perform the delete for the genome's features.
3822        my $retVal = $self->Delete('Feature', "fig|$genomeID.%", testMode => $testFlag);
3823        # Perform the delete for the primary genome data.
3824        my $stats = $self->Delete('Genome', $genomeID, testMode => $testFlag);
3825        $retVal->Accumulate($stats);
3826        # Return the result.
3827        return $retVal;
3828    }
3829    
3830    =head3 Fix
3831    
3832        my %fixedHash = $sprout->Fix(%groupHash);
3833    
3834    Prepare a genome group hash (like that returned by L</GetGroups>) for processing.
3835    The groups will be combined into the appropriate super-groups.
3836    
3837    =over 4
3838    
3839    =item groupHash
3840    
3841    Hash to be fixed up.
3842    
3843    =item RETURN
3844    
3845    Returns a fixed-up version of the hash.
3846    
3847    =back
3848    
3849    =cut
3850    
3851    sub Fix {
3852        # Get the parameters.
3853        my ($self, %groupHash) = @_;
3854        # Create the result hash.
3855        my %retVal = ();
3856        # Copy over the genomes.
3857        for my $groupID (keys %groupHash) {
3858            # Get the super-group name.
3859            my $realGroupID = $self->SuperGroup($groupID);
3860            # Append this group's genomes into the result hash
3861            # using the super-group name.
3862            push @{$retVal{$realGroupID}}, @{$groupHash{$groupID}};
3863        }
3864        # Return the result hash.
3865        return %retVal;
3866    }
3867    
3868    =head3 GroupPageName
3869    
3870        my $name = $sprout->GroupPageName($group);
3871    
3872    Return the name of the page for the specified NMPDR group.
3873    
3874    =over 4
3875    
3876    =item group
3877    
3878    Name of the relevant group.
3879    
3880    =item RETURN
3881    
3882    Returns the relative page name (e.g. C<../content/campy.php>). If the group file is not in
3883    memory it will be read in.
3884    
3885    =back
3886    
3887    =cut
3888    
3889    sub GroupPageName {
3890        # Get the parameters.
3891        my ($self, $group) = @_;
3892        # Check for the group file data.
3893        my %superTable = $self->CheckGroupFile();
3894        # Compute the real group name.
3895        my $realGroup = $self->SuperGroup($group);
3896        # Get the associated page name.
3897        my $retVal = "../content/$superTable{$realGroup}->{page}";
3898        # Return the result.
3899        return $retVal;
3900    }
3901    
3902    
3903    =head3 AddProperty
3904    
3905        $sprout->AddProperty($featureID, $key, @values);
3906    
3907    Add a new attribute value (Property) to a feature.
3908    
3909    =over 4
3910    
3911    =item peg
3912    
3913    ID of the feature to which the attribute is to be added.
3914    
3915    =item key
3916    
3917    Name of the attribute (key).
3918    
3919    =item values
3920    
3921    Values of the attribute.
3922    
3923    =back
3924    
3925    =cut
3926    #: Return Type ;
3927    sub AddProperty {
3928        # Get the parameters.
3929        my ($self, $featureID, $key, @values) = @_;
3930        # Add the property using the attached attributes object.
3931        $self->{_ca}->AddAttribute($featureID, $key, @values);
3932    }
3933    
3934    =head3 CheckGroupFile
3935    
3936        my %groupData = $sprout->CheckGroupFile();
3937    
3938    Get the group file hash. The group file hash describes the relationship
3939    between a group and the super-group to which it belongs for purposes of
3940    display. The super-group name is computed from the first capitalized word
3941    in the actual group name. For each super-group, the group file contains
3942    the page name and a list of the species expected to be in the group.
3943    Each species is specified by a genus and a species name. A species name
3944    of C<0> implies an entire genus.
3945    
3946    This method returns a hash from super-group names to a hash reference. Each
3947    resulting hash reference contains the following fields.
3948    
3949    =over 4
3950    
3951    =item page
3952    
3953    The super-group's web page in the NMPDR.
3954    
3955    =item contents
3956    
3957    A list of 2-tuples, each containing a genus name followed by a species name
3958    (or 0, indicating all species). This list indicates which organisms belong
3959    in the super-group.
3960    
3961    =back
3962    
3963    =cut
3964    
3965    sub CheckGroupFile {
3966        # Get the parameters.
3967        my ($self) = @_;
3968        # Check to see if we already have this hash.
3969        if (! defined $self->{groupHash}) {
3970            # We don't, so we need to read it in.
3971            my %groupHash;
3972            # Read the group file.
3973            my @groupLines = Tracer::GetFile("$FIG_Config::sproutData/groups.tbl");
3974            # Loop through the list of sort-of groups.
3975            for my $groupLine (@groupLines) {
3976                my ($name, $page, @contents) = split /\t/, $groupLine;
3977                $groupHash{$name} = { page => $page,
3978                                      contents => [ map { [ split /\s*,\s*/, $_ ] } @contents ]
3979                                    };
3980            }
3981            # Save the hash.
3982            $self->{groupHash} = \%groupHash;
3983        }
3984        # Return the result.
3985        return %{$self->{groupHash}};
3986    }
3987    
3988    =head2 Virtual Methods
3989    
3990    =head3 CleanKeywords
3991    
3992        my $cleanedString = $sprout->CleanKeywords($searchExpression);
3993    
3994    Clean up a search expression or keyword list. This involves converting the periods
3995    in EC numbers to underscores, converting non-leading minus signs to underscores,
3996    a vertical bar or colon to an apostrophe, and forcing lower case for all alphabetic
3997    characters. In addition, any extra spaces are removed.
3998    
3999    =over 4
4000    
4001    =item searchExpression
4002    
4003    Search expression or keyword list to clean. Note that a search expression may
4004    contain boolean operators which need to be preserved. This includes leading
4005    minus signs.
4006    
4007    =item RETURN
4008    
4009    Cleaned expression or keyword list.
4010    
4011    =back
4012    
4013    =cut
4014    
4015    sub CleanKeywords {
4016        # Get the parameters.
4017        my ($self, $searchExpression) = @_;
4018        # Perform the standard cleanup.
4019        my $words = $self->ERDB::CleanKeywords($searchExpression);
4020        # Fix the periods in EC and TC numbers.
4021        $words =~ s/(\d+|\-)\.(\d+|-)\.(\d+|-)\.(\d+|-)/$1_$2_$3_$4/g;
4022        # Fix non-trailing periods.
4023        $words =~ s/\.(\w)/_$1/g;
4024        # Fix non-leading minus signs.
4025        $words =~ s/(\w)[\-]/$1_/g;
4026        # Fix the vertical bars and colons
4027        $words =~ s/(\w)[|:](\w)/$1'$2/g;
4028        # Now split up the list so that each keyword is in its own string. We keep the delimiters
4029        # because they may contain boolean expression data.
4030        my @words = split /([^A-Za-z'0-9_]+)/, $words;
4031        # We'll convert the stemmable words into stems and re-assemble the result.
4032        my $retVal = "";
4033        for my $word (@words) {
4034            my $stem = $self->Stem($word);
4035            if (defined $stem) {
4036                $retVal .= $stem;
4037            } else {
4038                $retVal .= $word;
4039            }
4040        }
4041        Trace("Cleaned keyword list for \"$searchExpression\" is \"$retVal\".") if T(3);
4042        # Return the result.
4043        return $retVal;
4044    }
4045    
4046  =head2 Internal Utility Methods  =head2 Internal Utility Methods
4047    
4048  =head3 ParseAssignment  =head3 ParseAssignment
# Line 3093  Line 4053 
4053    
4054  A functional assignment is always of the form  A functional assignment is always of the form
4055    
4056      I<XXXX>C<\nset >I<YYYY>C< function to\n>I<ZZZZZ>      set YYYY function to
4057        ZZZZ
4058    
4059    where I<YYYY> is the B<user>, and I<ZZZZ> is the actual functional role. In most cases,
4060    the user and the assigning user (from MadeAnnotation) will be the same, but that is
4061    not always the case.
4062    
4063  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
4064  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.  
4065    
4066  This is a static method.  This is a static method.
4067    
4068  =over 4  =over 4
4069    
4070    =item user
4071    
4072    Name of the assigning user.
4073    
4074  =item text  =item text
4075    
4076  Text of the annotation.  Text of the annotation.
# Line 3118  Line 4086 
4086    
4087  sub _ParseAssignment {  sub _ParseAssignment {
4088      # Get the parameters.      # Get the parameters.
4089      my ($text) = @_;      my ($user, $text) = @_;
4090      # Declare the return value.      # Declare the return value.
4091      my @retVal = ();      my @retVal = ();
4092      # Check to see if this is a functional assignment.      # Check to see if this is a functional assignment.
4093      my ($user, $type, $function) = split(/\n/, $text);      my ($type, $function) = split(/\n/, $text);
4094      if ($type =~ m/^set ([^ ]+) function to$/i) {      if ($type =~ m/^set function to$/i) {
4095          # 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.
4096          # and the assigning user.          @retVal = ($user, $function);
4097          @retVal = ($1, $function, $user);      } elsif ($type =~ m/^set (\S+) function to$/i) {
4098            # Here we have an assignment with a user that is passed back to the caller.
4099            @retVal = ($1, $function);
4100        }
4101        # If we have an assignment, we need to clean the function text. There may be
4102        # extra junk at the end added as a note from the user.
4103        if (defined( $retVal[1] )) {
4104            $retVal[1] =~ s/(\t\S)?\s*$//;
4105      }      }
4106      # Return the result list.      # Return the result list.
4107      return @retVal;      return @retVal;
4108  }  }
4109    
4110    =head3 _CheckFeature
4111    
4112        my $flag = $sprout->_CheckFeature($fid);
4113    
4114    Return TRUE if the specified FID is probably an NMPDR feature ID, else FALSE.
4115    
4116    =over 4
4117    
4118    =item fid
4119    
4120    Feature ID to check.
4121    
4122    =item RETURN
4123    
4124    Returns TRUE if the FID is for one of the NMPDR genomes, else FALSE.
4125    
4126    =back
4127    
4128    =cut
4129    
4130    sub _CheckFeature {
4131        # Get the parameters.
4132        my ($self, $fid) = @_;
4133        # Insure we have a genome hash.
4134        if (! defined $self->{genomeHash}) {
4135            my %genomeHash = map { $_ => 1 } $self->GetFlat(['Genome'], "", [], 'Genome(id)');
4136            $self->{genomeHash} = \%genomeHash;
4137        }
4138        # Get the feature's genome ID.
4139        my ($genomeID) = FIGRules::ParseFeatureID($fid);
4140        # Return an indicator of whether or not the genome ID is in the hash.
4141        return ($self->{genomeHash}->{$genomeID} ? 1 : 0);
4142    }
4143    
4144  =head3 FriendlyTimestamp  =head3 FriendlyTimestamp
4145    
4146  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 4163 
4163    
4164  sub FriendlyTimestamp {  sub FriendlyTimestamp {
4165      my ($timeValue) = @_;      my ($timeValue) = @_;
4166      my $retVal = strftime("%a %b %e %H:%M:%S %Y", localtime($timeValue));      my $retVal = localtime($timeValue);
4167      return $retVal;      return $retVal;
4168  }  }
4169    
 =head3 AddProperty  
   
 C<< my  = $sprout->AddProperty($featureID, $key, $value, $url); >>  
4170    
4171  Add a new attribute value (Property) to a feature. In the SEED system, attributes can  =head3 Hint
 be added to almost any object. In Sprout, they can only be added to features. In  
 Sprout, attributes are implemented using I<properties>. A property represents a key/value  
 pair. If the particular key/value pair coming in is not already in the database, a new  
 B<Property> record is created to hold it.  
4172    
4173  =over 4      my $htmlText = SearchHelper::Hint($wikiPage, $hintText);
4174    
4175  =item peg  Return the HTML for a small question mark that displays the specified hint text when it is clicked.
4176    This HTML can be put in forms to provide a useful hinting mechanism.
4177    
4178  ID of the feature to which the attribute is to be replied.  =over 4
4179    
4180  =item key  =item wikiPage
4181    
4182  Name of the attribute (key).  Name of the wiki page to be popped up when the hint mark is clicked.
4183    
4184  =item value  =item hintText
4185    
4186  Value of the attribute.  Text to display for the hint. It is raw html, but may not contain any double quotes.
4187    
4188  =item url  =item RETURN
4189    
4190  URL or text citation from which the property was obtained.  Returns the html for the hint facility. The resulting html shows a small button-like thing that
4191    uses the standard FIG popup technology.
4192    
4193  =back  =back
4194    
4195  =cut  =cut
4196  #: Return Type ;  
4197  sub AddProperty {  sub Hint {
4198      # Get the parameters.      # Get the parameters.
4199      my ($self, $featureID, $key, $value, $url) = @_;      my ($wikiPage, $hintText) = @_;
4200      # Declare the variable to hold the desired property ID.      # Escape the single quotes in the hint text.
4201      my $propID;      my $quotedText = $hintText;
4202      # Attempt to find a property record for this key/value pair.      $quotedText =~ s/'/\\'/g;
4203      my @properties = $self->GetFlat(['Property'],      # Convert the wiki page name to a URL.
4204                                     "Property(property-name) = ? AND Property(property-value) = ?",      my $wikiURL = join("", map { ucfirst $_ } split /\s+/, $wikiPage);
4205                                     [$key, $value], 'Property(id)');      $wikiURL = "$FIG_Config::cgi_url/wiki/view.cgi/FIG/$wikiURL";
4206      if (@properties) {      # Compute the mouseover script.
4207          # Here the property is already in the database. We save its ID.      my $mouseOver = "doTooltip(this, '$quotedText')";
4208          $propID = $properties[0];      # Create the html.
4209          # Here the property value does not exist. We need to generate an ID. It will be set      my $retVal = "&nbsp;<a href=\"$wikiURL\"><img src=\"$FIG_Config::cgi_url/Html/button-h.png\" class=\"helpicon\" onmouseover=\"$mouseOver\"/></a>";
4210          # to a number one greater than the maximum value in the database. This call to      # Return it.
4211          # GetAll will stop after one record.      return $retVal;
         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 });  
     }  
     # Now we connect the incoming feature to the property.  
     $self->Insert('HasProperty', { 'from-link' => $featureID, 'to-link' => $propID, evidence => $url });  
4212  }  }
4213    
   
   
4214  1;  1;

Legend:
Removed from v.1.36  
changed lines
  Added in v.1.115

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3