[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.54, Thu Feb 16 04:10:07 2006 UTC revision 1.94, Fri Nov 3 00:42:25 2006 UTC
# Line 1  Line 1 
1  package Sprout;  package Sprout;
2    
3        require Exporter;
4        use ERDB;
5        @ISA = qw(Exporter ERDB);
6      use Data::Dumper;      use Data::Dumper;
7      use strict;      use strict;
8      use Carp;      use Carp;
# Line 7  Line 10 
10      use XML::Simple;      use XML::Simple;
11      use DBQuery;      use DBQuery;
12      use DBObject;      use DBObject;
     use ERDB;  
13      use Tracer;      use Tracer;
14      use FIGRules;      use FIGRules;
15        use FidCheck;
16      use Stats;      use Stats;
17      use POSIX qw(strftime);      use POSIX qw(strftime);
18        use BasicLocation;
19    
20  =head1 Sprout Database Manipulation Object  =head1 Sprout Database Manipulation Object
21    
# Line 32  Line 35 
35  query tasks. For example, L</genomes> lists the IDs of all the genomes in the database and  query tasks. For example, L</genomes> lists the IDs of all the genomes in the database and
36  L</dna_seq> returns the DNA sequence for a specified genome location.  L</dna_seq> returns the DNA sequence for a specified genome location.
37    
38    The Sprout object is a subclass of the ERDB object and inherits all its properties and methods.
39    
40  =cut  =cut
41    
42  #: Constructor SFXlate->new_sprout_only();  #: Constructor SFXlate->new_sprout_only();
# Line 62  Line 67 
67    
68  * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)  * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)
69    
70  * B<userData> user name and password, delimited by a slash (default C<root/>)  * B<userData> user name and password, delimited by a slash (default same as SEED)
71    
72  * B<port> connection port (default C<0>)  * B<port> connection port (default C<0>)
73    
74    * B<sock> connection socket (default same as SEED)
75    
76  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)
77    
78  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)
# Line 85  Line 92 
92  sub new {  sub new {
93      # Get the parameters.      # Get the parameters.
94      my ($class, $dbName, $options) = @_;      my ($class, $dbName, $options) = @_;
95        # Compute the DBD directory.
96        my $dbd_dir = (defined($FIG_Config::dbd_dir) ? $FIG_Config::dbd_dir :
97                                                      $FIG_Config::fig );
98      # Compute the options. We do this by starting with a table of defaults and overwriting with      # Compute the options. We do this by starting with a table of defaults and overwriting with
99      # the incoming data.      # the incoming data.
100      my $optionTable = Tracer::GetOptions({      my $optionTable = Tracer::GetOptions({
# Line 92  Line 102 
102                                                          # database type                                                          # database type
103                         dataDir      => $FIG_Config::sproutData,                         dataDir      => $FIG_Config::sproutData,
104                                                          # data file directory                                                          # data file directory
105                         xmlFileName  => "$FIG_Config::fig/SproutDBD.xml",                         xmlFileName  => "$dbd_dir/SproutDBD.xml",
106                                                          # database definition file name                                                          # database definition file name
107                         userData     => "$FIG_Config::dbuser/$FIG_Config::dbpass",                         userData     => "$FIG_Config::dbuser/$FIG_Config::dbpass",
108                                                          # user name and password                                                          # user name and password
109                         port         => $FIG_Config::dbport,                         port         => $FIG_Config::dbport,
110                                                          # database connection port                                                          # database connection port
111                           sock         => $FIG_Config::dbsock,
112                           host         => $FIG_Config::dbhost,
113                         maxSegmentLength => 4500,        # maximum feature segment length                         maxSegmentLength => 4500,        # maximum feature segment length
114                         maxSequenceLength => 8000,       # maximum contig sequence length                         maxSequenceLength => 8000,       # maximum contig sequence length
115                         noDBOpen     => 0,               # 1 to suppress the database open                         noDBOpen     => 0,               # 1 to suppress the database open
# Line 111  Line 123 
123      my $dbh;      my $dbh;
124      if (! $optionTable->{noDBOpen}) {      if (! $optionTable->{noDBOpen}) {
125          $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,          $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,
126                                  $password, $optionTable->{port});                                  $password, $optionTable->{port}, $optionTable->{host}, $optionTable->{sock});
127      }      }
128      # Create the ERDB object.      # Create the ERDB object.
129      my $xmlFileName = "$optionTable->{xmlFileName}";      my $xmlFileName = "$optionTable->{xmlFileName}";
130      my $erdb = ERDB->new($dbh, $xmlFileName);      my $retVal = ERDB::new($class, $dbh, $xmlFileName);
131      # Create this object.      # Add the option table and XML file name.
132      my $self = { _erdb => $erdb, _options => $optionTable, _xmlName => $xmlFileName };      $retVal->{_options} = $optionTable;
133      # Bless and return it.      $retVal->{_xmlName} = $xmlFileName;
134      bless $self;      # Set up space for the group file data.
135      return $self;      $retVal->{groupHash} = undef;
136        # Return it.
137        return $retVal;
138  }  }
139    
140  =head3 MaxSegment  =head3 MaxSegment
# Line 155  Line 169 
169      return $self->{_options}->{maxSequenceLength};      return $self->{_options}->{maxSequenceLength};
170  }  }
171    
 =head3 Get  
   
 C<< my $query = $sprout->Get(\@objectNames, $filterClause, \@parameterList); >>  
   
 This method allows a general query against the Sprout data using a specified filter clause.  
   
 The filter is a standard WHERE/ORDER BY clause with question marks as parameter markers and each  
 field name represented in the form B<I<objectName>(I<fieldName>)>. For example, the  
 following call requests all B<Genome> objects for the genus specified in the variable  
 $genus.  
   
 C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ?", [$genus]); >>  
   
 The WHERE clause contains a single question mark, so there is a single additional  
 parameter representing the parameter value. It would also be possible to code  
   
 C<< $query = $sprout->Get(['Genome'], "Genome(genus) = \'$genus\'"); >>  
   
 however, this version of the call would generate a syntax error if there were any quote  
 characters inside the variable C<$genus>.  
   
 The use of the strange parenthesized notation for field names enables us to distinguish  
 hyphens contained within field names from minus signs that participate in the computation  
 of the WHERE clause. All of the methods that manipulate fields will use this same notation.  
   
 It is possible to specify multiple entity and relationship names in order to retrieve more than  
 one object's data at the same time, which allows highly complex joined queries. For example,  
   
 C<< $query = $sprout->Get(['Genome', 'ComesFrom', 'Source'], "Genome(genus) = ?", [$genus]); >>  
   
 This query returns all the genomes for a particular genus and allows access to the  
 sources from which they came. The join clauses to go from Genome to Source are generated  
 automatically.  
   
 Finally, the filter clause can contain sort information. To do this, simply put an C<ORDER BY>  
 clause at the end of the filter. Field references in the ORDER BY section follow the same rules  
 as they do in the filter itself; in other words, each one must be of the form B<I<objectName>(I<fieldName>)>.  
 For example, the following filter string gets all genomes for a particular genus and sorts  
 them by species name.  
   
 C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ? ORDER BY Genome(species)", [$genus]); >>  
   
 It is also permissible to specify I<only> an ORDER BY clause. For example, the following invocation gets  
 all genomes ordered by genus and species.  
   
 C<< $query = $sprout->Get(['Genome'], "ORDER BY Genome(genus), Genome(species)"); >>  
   
 Odd things may happen if one of the ORDER BY fields is in a secondary relation. So, for example, an  
 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.  
   
 If multiple names are specified, then the query processor will automatically determine a  
 join path between the entities and relationships. The algorithm used is very simplistic.  
 In particular, you can't specify any entity or relationship more than once, and if a  
 relationship is recursive, the path is determined by the order in which the entity  
 and the relationship appear. For example, consider a recursive relationship B<IsParentOf>  
 which relates B<People> objects to other B<People> objects. If the join path is  
 coded as C<['People', 'IsParentOf']>, then the people returned will be parents. If, however,  
 the join path is C<['IsParentOf', 'People']>, then the people returned will be children.  
   
 =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 RETURN  
   
 Returns a B<DBQuery> that can be used to iterate through all of the results.  
   
 =back  
   
 =cut  
   
 sub Get {  
     # Get the parameters.  
     my ($self, $objectNames, $filterClause, $parameterList) = @_;  
     # We differ from the ERDB Get method in that the parameter list is passed in as a list reference  
     # rather than a list of parameters. The next step is to convert the parameters from a reference  
     # to a real list. We can only do this if the parameters have been specified.  
     my @parameters;  
     if ($parameterList) { @parameters = @{$parameterList}; }  
     return $self->{_erdb}->Get($objectNames, $filterClause, @parameters);  
 }  
   
 =head3 GetEntity  
   
 C<< my $entityObject = $sprout->GetEntity($entityType, $ID); >>  
   
 Return an object describing the entity instance with a specified ID.  
   
 =over 4  
   
 =item entityType  
   
 Entity type name.  
   
 =item ID  
   
 ID of the desired entity.  
   
 =item RETURN  
   
 Returns a B<DBObject> representing the desired entity instance, or an undefined value if no  
 instance is found with the specified key.  
   
 =back  
   
 =cut  
   
 sub GetEntity {  
     # Get the parameters.  
     my ($self, $entityType, $ID) = @_;  
     # Call the ERDB method.  
     return $self->{_erdb}->GetEntity($entityType, $ID);  
 }  
   
 =head3 GetEntityValues  
   
 C<< my @values = GetEntityValues($entityType, $ID, \@fields); >>  
   
 Return a list of values from a specified entity instance.  
   
 =over 4  
   
 =item entityType  
   
 Entity type name.  
   
 =item ID  
   
 ID of the desired entity.  
   
 =item fields  
   
 List of field names, each of the form I<objectName>C<(>I<fieldName>C<)>.  
   
 =item RETURN  
   
 Returns a flattened list of the values of the specified fields for the specified entity.  
   
 =back  
   
 =cut  
 #: Return Type @;  
 sub GetEntityValues {  
     # Get the parameters.  
     my ($self, $entityType, $ID, $fields) = @_;  
     # Call the ERDB method.  
     return $self->{_erdb}->GetEntityValues($entityType, $ID, $fields);  
 }  
   
 =head3 ShowMetaData  
   
 C<< $sprout->ShowMetaData($fileName); >>  
   
 This method outputs a description of the database to an HTML file in the data directory.  
   
 =over 4  
   
 =item fileName  
   
 Fully-qualified name to give to the output file.  
   
 =back  
   
 =cut  
   
 sub ShowMetaData {  
     # 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);  
 }  
   
172  =head3 Load  =head3 Load
173    
174  C<< $sprout->Load($rebuild); >>;  C<< $sprout->Load($rebuild); >>;
# Line 379  Line 203 
203  sub Load {  sub Load {
204      # Get the parameters.      # Get the parameters.
205      my ($self, $rebuild) = @_;      my ($self, $rebuild) = @_;
     # Get the database object.  
     my $erdb = $self->{_erdb};  
206      # Load the tables from the data directory.      # Load the tables from the data directory.
207      my $retVal = $erdb->LoadTables($self->{_options}->{dataDir}, $rebuild);      my $retVal = $self->LoadTables($self->{_options}->{dataDir}, $rebuild);
208      # Return the statistics.      # Return the statistics.
209      return $retVal;      return $retVal;
210  }  }
# Line 422  Line 244 
244  sub LoadUpdate {  sub LoadUpdate {
245      # Get the parameters.      # Get the parameters.
246      my ($self, $truncateFlag, $tableList) = @_;      my ($self, $truncateFlag, $tableList) = @_;
     # Get the database object.  
     my $erdb = $self->{_erdb};  
247      # Declare the return value.      # Declare the return value.
248      my $retVal = Stats->new();      my $retVal = Stats->new();
249      # Get the data directory.      # Get the data directory.
# Line 437  Line 257 
257              Trace("No load file found for $tableName in $dataDir.") if T(0);              Trace("No load file found for $tableName in $dataDir.") if T(0);
258          } else {          } else {
259              # Attempt to load this table.              # Attempt to load this table.
260              my $result = $erdb->LoadTable($fileName, $tableName, $truncateFlag);              my $result = $self->LoadTable($fileName, $tableName, $truncateFlag);
261              # Accumulate the resulting statistics.              # Accumulate the resulting statistics.
262              $retVal->Accumulate($result);              $retVal->Accumulate($result);
263          }          }
# Line 446  Line 266 
266      return $retVal;      return $retVal;
267  }  }
268    
269    =head3 GenomeCounts
270    
271    C<< my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete); >>
272    
273    Count the number of genomes in each domain. If I<$complete> is TRUE, only complete
274    genomes will be included in the counts.
275    
276    =over 4
277    
278    =item complete
279    
280    TRUE if only complete genomes are to be counted, FALSE if all genomes are to be
281    counted
282    
283    =item RETURN
284    
285    A six-element list containing the number of genomes in each of six categories--
286    Archaea, Bacteria, Eukaryota, Viral, Environmental, and Unknown, respectively.
287    
288    =back
289    
290    =cut
291    
292    sub GenomeCounts {
293        # Get the parameters.
294        my ($self, $complete) = @_;
295        # Set the filter based on the completeness flag.
296        my $filter = ($complete ? "Genome(complete) = 1" : "");
297        # Get all the genomes and the related taxonomy information.
298        my @genomes = $self->GetAll(['Genome'], $filter, [], ['Genome(id)', 'Genome(taxonomy)']);
299        # Clear the counters.
300        my ($arch, $bact, $euk, $vir, $env, $unk) = (0, 0, 0, 0, 0, 0);
301        # Loop through, counting the domains.
302        for my $genome (@genomes) {
303            if    ($genome->[1] =~ /^archaea/i)  { ++$arch }
304            elsif ($genome->[1] =~ /^bacter/i)   { ++$bact }
305            elsif ($genome->[1] =~ /^eukar/i)    { ++$euk }
306            elsif ($genome->[1] =~ /^vir/i)      { ++$vir }
307            elsif ($genome->[1] =~ /^env/i)      { ++$env }
308            else  { ++$unk }
309        }
310        # Return the counts.
311        return ($arch, $bact, $euk, $vir, $env, $unk);
312    }
313    
314    =head3 ContigCount
315    
316    C<< my $count = $sprout->ContigCount($genomeID); >>
317    
318    Return the number of contigs for the specified genome ID.
319    
320    =over 4
321    
322    =item genomeID
323    
324    ID of the genome whose contig count is desired.
325    
326    =item RETURN
327    
328    Returns the number of contigs for the specified genome.
329    
330    =back
331    
332    =cut
333    
334    sub ContigCount {
335        # Get the parameters.
336        my ($self, $genomeID) = @_;
337        # Get the contig count.
338        my $retVal = $self->GetCount(['Contig', 'HasContig'], "HasContig(from-link) = ?", [$genomeID]);
339        # Return the result.
340        return $retVal;
341    }
342    
343    =head3 GeneMenu
344    
345    C<< my $selectHtml = $sprout->GeneMenu(\%attributes, $filterString, \@params, $selected); >>
346    
347    Return an HTML select menu of genomes. Each genome will be an option in the menu,
348    and will be displayed by name with the ID and a contig count attached. The selection
349    value will be the genome ID. The genomes will be sorted by genus/species name.
350    
351    =over 4
352    
353    =item attributes
354    
355    Reference to a hash mapping attributes to values for the SELECT tag generated.
356    
357    =item filterString
358    
359    A filter string for use in selecting the genomes. The filter string must conform
360    to the rules for the C<< ERDB->Get >> method.
361    
362    =item params
363    
364    Reference to a list of values to be substituted in for the parameter marks in
365    the filter string.
366    
367    =item selected (optional)
368    
369    ID of the genome to be initially selected.
370    
371    =item fast (optional)
372    
373    If specified and TRUE, the contig counts will be omitted to improve performance.
374    
375    =item RETURN
376    
377    Returns an HTML select menu with the specified genomes as selectable options.
378    
379    =back
380    
381    =cut
382    
383    sub GeneMenu {
384        # Get the parameters.
385        my ($self, $attributes, $filterString, $params, $selected, $fast) = @_;
386        my $slowMode = ! $fast;
387        # Default to nothing selected. This prevents an execution warning if "$selected"
388        # is undefined.
389        $selected = "" unless defined $selected;
390        Trace("Gene Menu called with slow mode \"$slowMode\" and selection \"$selected\".") if T(3);
391        # Start the menu.
392        my $retVal = "<select " .
393            join(" ", map { "$_=\"$attributes->{$_}\"" } keys %{$attributes}) .
394            ">\n";
395        # Get the genomes.
396        my @genomes = $self->GetAll(['Genome'], $filterString, $params, ['Genome(id)',
397                                                                         'Genome(genus)',
398                                                                         'Genome(species)',
399                                                                         'Genome(unique-characterization)']);
400        # Sort them by name.
401        my @sorted = sort { lc("$a->[1] $a->[2]") cmp lc("$b->[1] $b->[2]") } @genomes;
402        # Loop through the genomes, creating the option tags.
403        for my $genomeData (@sorted) {
404            # Get the data for this genome.
405            my ($genomeID, $genus, $species, $strain) = @{$genomeData};
406            # Get the contig count.
407            my $contigInfo = "";
408            if ($slowMode) {
409                my $count = $self->ContigCount($genomeID);
410                my $counting = ($count == 1 ? "contig" : "contigs");
411                $contigInfo = "[$count $counting]";
412            }
413            # Find out if we're selected.
414            my $selectOption = ($selected eq $genomeID ? " selected" : "");
415            # Build the option tag.
416            $retVal .= "<option value=\"$genomeID\"$selectOption>$genus $species $strain ($genomeID)$contigInfo</option>\n";
417        }
418        # Close the SELECT tag.
419        $retVal .= "</select>\n";
420        # Return the result.
421        return $retVal;
422    }
423    
424  =head3 Build  =head3 Build
425    
426  C<< $sprout->Build(); >>  C<< $sprout->Build(); >>
# Line 460  Line 435 
435      # Get the parameters.      # Get the parameters.
436      my ($self) = @_;      my ($self) = @_;
437      # Create the tables.      # Create the tables.
438      $self->{_erdb}->CreateTables;      $self->CreateTables();
439  }  }
440    
441  =head3 Genomes  =head3 Genomes
# Line 680  Line 655 
655      return ($contigID, $start, $dir, $len);      return ($contigID, $start, $dir, $len);
656  }  }
657    
658    
659    
660  =head3 PointLocation  =head3 PointLocation
661    
662  C<< my $found = Sprout::PointLocation($location, $point); >>  C<< my $found = Sprout::PointLocation($location, $point); >>
# Line 740  Line 717 
717  should be of the form returned by L</featureLocation> when in a list context. In other words,  should be of the form returned by L</featureLocation> when in a list context. In other words,
718  each location is of the form I<contigID>C<_>I<begin>I<dir>I<end>.  each location is of the form I<contigID>C<_>I<begin>I<dir>I<end>.
719    
720    For example, the following would return the DNA sequence for contig C<83333.1:NC_000913>
721    between positions 1401 and 1532, inclusive.
722    
723        my $sequence = $sprout->DNASeq('83333.1:NC_000913_1401_1532');
724    
725  =over 4  =over 4
726    
727  =item locationList  =item locationList
728    
729  List of location specifiers, each in the form I<contigID>C<_>I<begin>I<dir>I<end> (see  List of location specifiers, each in the form I<contigID>C<_>I<begin>I<dir>I<len> or
730  L</FeatureLocation> for more about this format).  I<contigID>C<_>I<begin>C<_>I<end> (see L</FeatureLocation> for more about this format).
731    
732  =item RETURN  =item RETURN
733    
# Line 841  Line 823 
823      return @retVal;      return @retVal;
824  }  }
825    
826    =head3 GenomeLength
827    
828    C<< my $length = $sprout->GenomeLength($genomeID); >>
829    
830    Return the length of the specified genome in base pairs.
831    
832    =over 4
833    
834    =item genomeID
835    
836    ID of the genome whose base pair count is desired.
837    
838    =item RETURN
839    
840    Returns the number of base pairs in all the contigs of the specified
841    genome.
842    
843    =back
844    
845    =cut
846    
847    sub GenomeLength {
848        # Get the parameters.
849        my ($self, $genomeID) = @_;
850        # Declare the return variable.
851        my $retVal = 0;
852        # Get the genome's contig sequence lengths.
853        my @lens = $self->GetFlat(['HasContig', 'IsMadeUpOf'], 'HasContig(from-link) = ?',
854                           [$genomeID], 'IsMadeUpOf(len)');
855        # Sum the lengths.
856        map { $retVal += $_ } @lens;
857        # Return the result.
858        return $retVal;
859    }
860    
861    =head3 FeatureCount
862    
863    C<< my $count = $sprout->FeatureCount($genomeID, $type); >>
864    
865    Return the number of features of the specified type in the specified genome.
866    
867    =over 4
868    
869    =item genomeID
870    
871    ID of the genome whose feature count is desired.
872    
873    =item type
874    
875    Type of feature to count (eg. C<peg>, C<rna>, etc.).
876    
877    =item RETURN
878    
879    Returns the number of features of the specified type for the specified genome.
880    
881    =back
882    
883    =cut
884    
885    sub FeatureCount {
886        # Get the parameters.
887        my ($self, $genomeID, $type) = @_;
888        # Compute the count.
889        my $retVal = $self->GetCount(['HasFeature', 'Feature'],
890                                    "HasFeature(from-link) = ? AND Feature(feature-type) = ?",
891                                    [$genomeID, $type]);
892        # Return the result.
893        return $retVal;
894    }
895    
896    =head3 GenomeAssignments
897    
898    C<< my $fidHash = $sprout->GenomeAssignments($genomeID); >>
899    
900    Return a list of a genome's assigned features. The return hash will contain each
901    assigned feature of the genome mapped to the text of its most recent functional
902    assignment.
903    
904    =over 4
905    
906    =item genomeID
907    
908    ID of the genome whose functional assignments are desired.
909    
910    =item RETURN
911    
912    Returns a reference to a hash which maps each feature to its most recent
913    functional assignment.
914    
915    =back
916    
917    =cut
918    
919    sub GenomeAssignments {
920        # Get the parameters.
921        my ($self, $genomeID) = @_;
922        # Declare the return variable.
923        my $retVal = {};
924        # Query the genome's features.
925        my $query = $self->Get(['HasFeature', 'Feature'], "HasFeature(from-link) = ?",
926                               [$genomeID]);
927        # Loop through the features.
928        while (my $data = $query->Fetch) {
929            # Get the feature ID and assignment.
930            my ($fid, $assignment) = $data->Values(['Feature(id)', 'Feature(assignment)']);
931            if ($assignment) {
932                $retVal->{$fid} = $assignment;
933            }
934        }
935        # Return the result.
936        return $retVal;
937    }
938    
939  =head3 ContigLength  =head3 ContigLength
940    
941  C<< my $length = $sprout->ContigLength($contigID); >>  C<< my $length = $sprout->ContigLength($contigID); >>
# Line 1195  Line 1290 
1290  Return the most recently-determined functional assignment of a particular feature.  Return the most recently-determined functional assignment of a particular feature.
1291    
1292  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
1293  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
1294  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</ParseAssignment>. 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.  
1295    
1296  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
1297  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 1218  Line 1310 
1310    
1311  =item userID (optional)  =item userID (optional)
1312    
1313  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
1314  C<FIG> assignment will be returned.  functional assignment in the B<Feature> table will be returned.
1315    
1316  =item RETURN  =item RETURN
1317    
# Line 1236  Line 1328 
1328      my $retVal;      my $retVal;
1329      # Determine the ID type.      # Determine the ID type.
1330      if ($featureID =~ m/^fig\|/) {      if ($featureID =~ m/^fig\|/) {
1331          # Here we have a FIG feature ID. We must build the list of trusted          # Here we have a FIG feature ID.
1332          # users.          if (!$userID) {
1333                # Use the primary assignment.
1334                ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(assignment)']);
1335            } else {
1336                # We must build the list of trusted users.
1337          my %trusteeTable = ();          my %trusteeTable = ();
1338          # Check the user ID.          # Check the user ID.
1339          if (!$userID) {          if (!$userID) {
# Line 1280  Line 1376 
1376                  }                  }
1377              }              }
1378          }          }
1379            }
1380      } else {      } else {
1381          # 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
1382          # matter. We simply get the information from the External Alias Function          # matter. We simply get the information from the External Alias Function
# Line 1395  Line 1492 
1492      my %retVal = ();      my %retVal = ();
1493      # Loop through the incoming features.      # Loop through the incoming features.
1494      for my $featureID (@{$featureList}) {      for my $featureID (@{$featureList}) {
1495          # Create a query to get the feature's best hit.          # Ask the server for the feature's best hit.
1496          my $query = $self->Get(['IsBidirectionalBestHitOf'],          my @bbhData = FIGRules::BBHData($featureID);
                                "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?",  
                                [$featureID, $genomeID]);  
1497          # Peel off the BBHs found.          # Peel off the BBHs found.
1498          my @found = ();          my @found = ();
1499          while (my $bbh = $query->Fetch) {          for my $bbh (@bbhData) {
1500              push @found, $bbh->Value('IsBidirectionalBestHitOf(to-link)');              my $fid = $bbh->[0];
1501                my $bbGenome = $self->GenomeOf($fid);
1502                if ($bbGenome eq $genomeID) {
1503                    push @found, $fid;
1504                }
1505          }          }
1506          $retVal{$featureID} = \@found;          $retVal{$featureID} = \@found;
1507      }      }
# Line 1416  Line 1515 
1515    
1516  Return a list of the similarities to the specified feature.  Return a list of the similarities to the specified feature.
1517    
1518  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.  
1519    
1520  =over 4  =over 4
1521    
# Line 1437  Line 1535 
1535      # Get the parameters.      # Get the parameters.
1536      my ($self, $featureID, $count) = @_;      my ($self, $featureID, $count) = @_;
1537      # Ask for the best hits.      # Ask for the best hits.
1538      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);  
1539      # Create the return value.      # Create the return value.
1540      my %retVal = ();      my %retVal = ();
1541      for my $tuple (@lists) {      for my $tuple (@lists) {
# Line 1450  Line 1545 
1545      return %retVal;      return %retVal;
1546  }  }
1547    
   
   
1548  =head3 IsComplete  =head3 IsComplete
1549    
1550  C<< my $flag = $sprout->IsComplete($genomeID); >>  C<< my $flag = $sprout->IsComplete($genomeID); >>
# Line 1522  Line 1615 
1615    
1616  C<< my $genomeID = $sprout->GenomeOf($featureID); >>  C<< my $genomeID = $sprout->GenomeOf($featureID); >>
1617    
1618  Return the genome that contains a specified feature.  Return the genome that contains a specified feature or contig.
1619    
1620  =over 4  =over 4
1621    
1622  =item featureID  =item featureID
1623    
1624  ID of the feature whose genome is desired.  ID of the feature or contig whose genome is desired.
1625    
1626  =item RETURN  =item RETURN
1627    
1628  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
1629  an undefined value.  found, returns an undefined value.
1630    
1631  =back  =back
1632    
# Line 1542  Line 1635 
1635  sub GenomeOf {  sub GenomeOf {
1636      # Get the parameters.      # Get the parameters.
1637      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
1638      # Create a query to find the genome associated with the feature.      # Create a query to find the genome associated with the incoming ID.
1639      my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ?", [$featureID]);      my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ? OR HasContig(to-link) = ?",
1640                               [$featureID, $featureID]);
1641      # Declare the return value.      # Declare the return value.
1642      my $retVal;      my $retVal;
1643      # Get the genome ID.      # Get the genome ID.
# Line 1578  Line 1672 
1672  sub CoupledFeatures {  sub CoupledFeatures {
1673      # Get the parameters.      # Get the parameters.
1674      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
1675        Trace("Looking for features coupled to $featureID.") if T(coupling => 3);
1676      # Create a query to retrieve the functionally-coupled features.      # Create a query to retrieve the functionally-coupled features.
1677      my $query = $self->Get(['ParticipatesInCoupling', 'Coupling'],      my $query = $self->Get(['ParticipatesInCoupling', 'Coupling'],
1678                             "ParticipatesInCoupling(from-link) = ?", [$featureID]);                             "ParticipatesInCoupling(from-link) = ?", [$featureID]);
# Line 1590  Line 1685 
1685          # Get the ID and score of the coupling.          # Get the ID and score of the coupling.
1686          my ($couplingID, $score) = $clustering->Values(['Coupling(id)',          my ($couplingID, $score) = $clustering->Values(['Coupling(id)',
1687                                                          'Coupling(score)']);                                                          'Coupling(score)']);
1688          # The coupling ID contains the two feature IDs separated by a space. We use          Trace("$featureID coupled with score $score to ID $couplingID.") if T(coupling => 4);
1689          # this information to find the ID of the other feature.          # Get the other feature that participates in the coupling.
1690          my ($fid1, $fid2) = split / /, $couplingID;          my ($otherFeatureID) = $self->GetFlat(['ParticipatesInCoupling'],
1691          my $otherFeatureID = ($featureID eq $fid1 ? $fid2 : $fid1);                                             "ParticipatesInCoupling(to-link) = ? AND ParticipatesInCoupling(from-link) <> ?",
1692                                               [$couplingID, $featureID], 'ParticipatesInCoupling(from-link)');
1693            Trace("$couplingID target feature is $otherFeatureID.") if T(coupling => 4);
1694          # Attach the other feature's score to its ID.          # Attach the other feature's score to its ID.
1695          $retVal{$otherFeatureID} = $score;          $retVal{$otherFeatureID} = $score;
1696          $found = 1;          $found = 1;
# Line 1726  Line 1823 
1823      my ($self, $peg1, $peg2) = @_;      my ($self, $peg1, $peg2) = @_;
1824      # Declare the return values. We'll start with the coupling ID and undefine the      # Declare the return values. We'll start with the coupling ID and undefine the
1825      # flag and score until we have more information.      # flag and score until we have more information.
1826      my ($retVal, $inverted, $score) = (CouplingID($peg1, $peg2), undef, undef);      my ($retVal, $inverted, $score) = ($self->CouplingID($peg1, $peg2), undef, undef);
1827      # Find the coupling data.      # Find the coupling data.
1828      my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'],      my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'],
1829                                   "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)",                                   "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)",
# Line 1747  Line 1844 
1844      return ($retVal, $inverted, $score);      return ($retVal, $inverted, $score);
1845  }  }
1846    
1847  =head3 CouplingID  =head3 GetSynonymGroup
1848    
1849  C<< my $couplingID = Sprout::CouplingID($peg1, $peg2); >>  C<< my $id = $sprout->GetSynonymGroup($fid); >>
1850    
1851  Return the coupling ID for a pair of feature IDs.  Return the synonym group name for the specified feature.
1852    
1853  The coupling ID is currently computed by joining the feature IDs in  =over 4
1854    
1855    =item fid
1856    
1857    ID of the feature whose synonym group is desired.
1858    
1859    =item RETURN
1860    
1861    The name of the synonym group to which the feature belongs. If the feature does
1862    not belong to a synonym group, the feature ID itself is returned.
1863    
1864    =back
1865    
1866    =cut
1867    
1868    sub GetSynonymGroup {
1869        # Get the parameters.
1870        my ($self, $fid) = @_;
1871        # Declare the return variable.
1872        my $retVal;
1873        # Find the synonym group.
1874        my @groups = $self->GetFlat(['IsSynonymGroupFor'], "IsSynonymGroupFor(to-link) = ?",
1875                                       [$fid], 'IsSynonymGroupFor(from-link)');
1876        # Check to see if we found anything.
1877        if (@groups) {
1878            $retVal = $groups[0];
1879        } else {
1880            $retVal = $fid;
1881        }
1882        # Return the result.
1883        return $retVal;
1884    }
1885    
1886    =head3 GetBoundaries
1887    
1888    C<< my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList); >>
1889    
1890    Determine the begin and end boundaries for the locations in a list. All of the
1891    locations must belong to the same contig and have mostly the same direction in
1892    order for this method to produce a meaningful result. The resulting
1893    begin/end pair will contain all of the bases in any of the locations.
1894    
1895    =over 4
1896    
1897    =item locList
1898    
1899    List of locations to process.
1900    
1901    =item RETURN
1902    
1903    Returns a 3-tuple consisting of the contig ID, the beginning boundary,
1904    and the ending boundary. The beginning boundary will be left of the
1905    end for mostly-forward locations and right of the end for mostly-backward
1906    locations.
1907    
1908    =back
1909    
1910    =cut
1911    
1912    sub GetBoundaries {
1913        # Get the parameters.
1914        my ($self, @locList) = @_;
1915        # Set up the counters used to determine the most popular direction.
1916        my %counts = ( '+' => 0, '-' => 0 );
1917        # Get the last location and parse it.
1918        my $locObject = BasicLocation->new(pop @locList);
1919        # Prime the loop with its data.
1920        my ($contig, $beg, $end) = ($locObject->Contig, $locObject->Left, $locObject->Right);
1921        # Count its direction.
1922        $counts{$locObject->Dir}++;
1923        # Loop through the remaining locations. Note that in most situations, this loop
1924        # will not iterate at all, because most of the time we will be dealing with a
1925        # singleton list.
1926        for my $loc (@locList) {
1927            # Create a location object.
1928            my $locObject = BasicLocation->new($loc);
1929            # Count the direction.
1930            $counts{$locObject->Dir}++;
1931            # Get the left end and the right end.
1932            my $left = $locObject->Left;
1933            my $right = $locObject->Right;
1934            # Merge them into the return variables.
1935            if ($left < $beg) {
1936                $beg = $left;
1937            }
1938            if ($right > $end) {
1939                $end = $right;
1940            }
1941        }
1942        # If the most common direction is reverse, flip the begin and end markers.
1943        if ($counts{'-'} > $counts{'+'}) {
1944            ($beg, $end) = ($end, $beg);
1945        }
1946        # Return the result.
1947        return ($contig, $beg, $end);
1948    }
1949    
1950    =head3 CouplingID
1951    
1952    C<< my $couplingID = $sprout->CouplingID($peg1, $peg2); >>
1953    
1954    Return the coupling ID for a pair of feature IDs.
1955    
1956    The coupling ID is currently computed by joining the feature IDs in
1957  sorted order with a space. Client modules (that is, modules which  sorted order with a space. Client modules (that is, modules which
1958  use Sprout) should not, however, count on this always being the  use Sprout) should not, however, count on this always being the
1959  case. This method provides a way for abstracting the concept of a  case. This method provides a way for abstracting the concept of a
# Line 1782  Line 1982 
1982  =cut  =cut
1983  #: Return Type $;  #: Return Type $;
1984  sub CouplingID {  sub CouplingID {
1985      return join " ", sort @_;      my ($self, @pegs) = @_;
1986  }      return $self->DigestKey(join " ", sort @pegs);
   
 =head3 GetEntityTypes  
   
 C<< my @entityList = $sprout->GetEntityTypes(); >>  
   
 Return the list of supported entity types.  
   
 =cut  
 #: Return Type @;  
 sub GetEntityTypes {  
     # Get the parameters.  
     my ($self) = @_;  
     # Get the underlying database object.  
     my $erdb = $self->{_erdb};  
     # Get its entity type list.  
     my @retVal = $erdb->GetEntityTypes();  
1987  }  }
1988    
1989  =head3 ReadFasta  =head3 ReadFasta
# Line 1947  Line 2131 
2131      # Get the data directory name.      # Get the data directory name.
2132      my $outputDirectory = $self->{_options}->{dataDir};      my $outputDirectory = $self->{_options}->{dataDir};
2133      # Dump the relations.      # Dump the relations.
2134      $self->{_erdb}->DumpRelations($outputDirectory);      $self->DumpRelations($outputDirectory);
2135  }  }
2136    
2137  =head3 XMLFileName  =head3 XMLFileName
# Line 1999  Line 2183 
2183      # Get the parameters.      # Get the parameters.
2184      my ($self, $objectType, $fieldHash) = @_;      my ($self, $objectType, $fieldHash) = @_;
2185      # Call the underlying method.      # Call the underlying method.
2186      $self->{_erdb}->InsertObject($objectType, $fieldHash);      $self->InsertObject($objectType, $fieldHash);
2187  }  }
2188    
2189  =head3 Annotate  =head3 Annotate
# Line 2158  Line 2342 
2342      return @retVal;      return @retVal;
2343  }  }
2344    
 =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;  
 }  
   
2345  =head3 FeatureTranslation  =head3 FeatureTranslation
2346    
2347  C<< my $translation = $sprout->FeatureTranslation($featureID); >>  C<< my $translation = $sprout->FeatureTranslation($featureID); >>
# Line 2527  Line 2676 
2676      return $retVal;      return $retVal;
2677  }  }
2678    
2679    =head3 PropertyID
2680    
2681    C<< my $id = $sprout->PropertyID($propName, $propValue); >>
2682    
2683    Return the ID of the specified property name and value pair, if the
2684    pair exists.
2685    
2686    =over 4
2687    
2688    =item propName
2689    
2690    Name of the desired property.
2691    
2692    =item propValue
2693    
2694    Value expected for the desired property.
2695    
2696    =item RETURN
2697    
2698    Returns the ID of the name/value pair, or C<undef> if the pair does not exist.
2699    
2700    =back
2701    
2702    =cut
2703    
2704    sub PropertyID {
2705        # Get the parameters.
2706        my ($self, $propName, $propValue) = @_;
2707        # Try to find the ID.
2708        my ($retVal) = $self->GetFlat(['Property'],
2709                                      "Property(property-name) = ? AND Property(property-value) = ?",
2710                                      [$propName, $propValue], 'Property(id)');
2711        # Return the result.
2712        return $retVal;
2713    }
2714    
2715  =head3 MergedAnnotations  =head3 MergedAnnotations
2716    
2717  C<< my @annotationList = $sprout->MergedAnnotations(\@list); >>  C<< my @annotationList = $sprout->MergedAnnotations(\@list); >>
# Line 2724  Line 2909 
2909      # Get the parameters.      # Get the parameters.
2910      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
2911      # Get the list of names.      # Get the list of names.
2912      my @retVal = $self->GetFlat(['ContainsFeature', 'HasSSCell'], "ContainsFeature(to-link) = ?",      my @retVal = $self->GetFlat(['HasRoleInSubsystem'], "HasRoleInSubsystem(from-link) = ?",
2913                                  [$featureID], 'HasSSCell(from-link)');                                  [$featureID], 'HasRoleInSubsystem(to-link)');
2914        # Return the result, sorted.
2915        return sort @retVal;
2916    }
2917    
2918    =head3 GenomeSubsystemData
2919    
2920    C<< my %featureData = $sprout->GenomeSubsystemData($genomeID); >>
2921    
2922    Return a hash mapping genome features to their subsystem roles.
2923    
2924    =over 4
2925    
2926    =item genomeID
2927    
2928    ID of the genome whose subsystem feature map is desired.
2929    
2930    =item RETURN
2931    
2932    Returns a hash mapping each feature of the genome to a list of 2-tuples. Eacb
2933    2-tuple contains a subsystem name followed by a role ID.
2934    
2935    =back
2936    
2937    =cut
2938    
2939    sub GenomeSubsystemData {
2940        # Get the parameters.
2941        my ($self, $genomeID) = @_;
2942        # Declare the return variable.
2943        my %retVal = ();
2944        # Get a list of the genome features that participate in subsystems. For each
2945        # feature we get its spreadsheet cells and the corresponding roles.
2946        my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf'],
2947                                 "HasFeature(from-link) = ?", [$genomeID],
2948                                 ['HasFeature(to-link)', 'IsRoleOf(to-link)', 'IsRoleOf(from-link)']);
2949        # Now we get a list of the spreadsheet cells and their associated subsystems. Subsystems
2950        # with an unknown variant code (-1) are skipped. Note the genome ID is at both ends of the
2951        # list. We use it at the beginning to get all the spreadsheet cells for the genome and
2952        # again at the end to filter out participation in subsystems with a negative variant code.
2953        my @cellData = $self->GetAll(['IsGenomeOf', 'HasSSCell', 'ParticipatesIn'],
2954                                     "IsGenomeOf(from-link) = ? AND ParticipatesIn(variant-code) >= 0 AND ParticipatesIn(from-link) = ?",
2955                                     [$genomeID, $genomeID], ['HasSSCell(to-link)', 'HasSSCell(from-link)']);
2956        # Now "@roleData" lists the spreadsheet cell and role for each of the genome's features.
2957        # "@cellData" lists the subsystem name for each of the genome's spreadsheet cells. We
2958        # link these two lists together to create the result. First, we want a hash mapping
2959        # spreadsheet cells to subsystem names.
2960        my %subHash = map { $_->[0] => $_->[1] } @cellData;
2961        # We loop through @cellData to build the hash.
2962        for my $roleEntry (@roleData) {
2963            # Get the data for this feature and cell.
2964            my ($fid, $cellID, $role) = @{$roleEntry};
2965            # Check for a subsystem name.
2966            my $subsys = $subHash{$cellID};
2967            if ($subsys) {
2968                # Insure this feature has an entry in the return hash.
2969                if (! exists $retVal{$fid}) { $retVal{$fid} = []; }
2970                # Merge in this new data.
2971                push @{$retVal{$fid}}, [$subsys, $role];
2972            }
2973        }
2974      # Return the result.      # Return the result.
2975      return @retVal;      return %retVal;
2976  }  }
2977    
2978  =head3 RelatedFeatures  =head3 RelatedFeatures
# Line 2765  Line 3010 
3010      # Get the parameters.      # Get the parameters.
3011      my ($self, $featureID, $function, $userID) = @_;      my ($self, $featureID, $function, $userID) = @_;
3012      # 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.
3013      my @bbhFeatures = $self->GetFlat(['IsBidirectionalBestHitOf'],      my @bbhFeatures = map { $_->[0] } FIGRules::BBHData($featureID);
                                      "IsBidirectionalBestHitOf(from-link) = ?", [$featureID],  
                                      'IsBidirectionalBestHitOf(to-link)');  
3014      # 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
3015      # functional assignment.      # functional assignment.
3016      my @retVal = ();      my @retVal = ();
# Line 2831  Line 3074 
3074      return @retVal;      return @retVal;
3075  }  }
3076    
 =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;  
 }  
   
3077  =head3 Protein  =head3 Protein
3078    
3079  C<< my $protein = Sprout::Protein($sequence, $table); >>  C<< my $protein = Sprout::Protein($sequence, $table); >>
# Line 3022  Line 3146 
3146      # Loop through the input triples.      # Loop through the input triples.
3147      my $n = length $sequence;      my $n = length $sequence;
3148      for (my $i = 0; $i < $n; $i += 3) {      for (my $i = 0; $i < $n; $i += 3) {
3149          # Get the current triple from the sequence.          # Get the current triple from the sequence. Note we convert to
3150          my $triple = substr($sequence, $i, 3);          # upper case to insure a match.
3151            my $triple = uc substr($sequence, $i, 3);
3152          # Translate it using the table.          # Translate it using the table.
3153          my $protein = "X";          my $protein = "X";
3154          if (exists $table->{$triple}) { $protein = $table->{$triple}; }          if (exists $table->{$triple}) { $protein = $table->{$triple}; }
# Line 3051  Line 3176 
3176      # 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.
3177      my @retVal = ($self->{_options}->{dataDir});      my @retVal = ($self->{_options}->{dataDir});
3178      # Concatenate the table names.      # Concatenate the table names.
3179      push @retVal, $self->{_erdb}->GetTableNames();      push @retVal, $self->GetTableNames();
3180      # Return the result.      # Return the result.
3181      return @retVal;      return @retVal;
3182  }  }
3183    
3184  =head3 LowBBHs  =head3 LowBBHs
3185    
3186  C<< my %bbhMap = $sprout->GoodBBHs($featureID, $cutoff); >>  C<< my %bbhMap = $sprout->LowBBHs($featureID, $cutoff); >>
3187    
3188  Return the bidirectional best hits of a feature whose score is no greater than a  Return the bidirectional best hits of a feature whose score is no greater than a
3189  specified cutoff value. A higher cutoff value will allow inclusion of hits with  specified cutoff value. A higher cutoff value will allow inclusion of hits with
# Line 3087  Line 3212 
3212      my ($self, $featureID, $cutoff) = @_;      my ($self, $featureID, $cutoff) = @_;
3213      # Create the return hash.      # Create the return hash.
3214      my %retVal = ();      my %retVal = ();
3215      # Create a query to get the desired BBHs.      # Query for the desired BBHs.
3216      my @bbhList = $self->GetAll(['IsBidirectionalBestHitOf'],      my @bbhList = FIGRules::BBHData($featureID, $cutoff);
                                 'IsBidirectionalBestHitOf(sc) <= ? AND IsBidirectionalBestHitOf(from-link) = ?',  
                                 [$cutoff, $featureID],  
                                 ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(sc)']);  
3217      # Form the results into the return hash.      # Form the results into the return hash.
3218      for my $pair (@bbhList) {      for my $pair (@bbhList) {
3219          $retVal{$pair->[0]} = $pair->[1];          my $fid = $pair->[0];
3220            if ($self->Exists('Feature', $fid)) {
3221                $retVal{$fid} = $pair->[1];
3222            }
3223      }      }
3224      # Return the result.      # Return the result.
3225      return %retVal;      return %retVal;
3226  }  }
3227    
3228    =head3 Sims
3229    
3230    C<< my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters); >>
3231    
3232    Get a list of similarities for a specified feature. Similarity information is not kept in the
3233    Sprout database; rather, they are retrieved from a network server. The similarities are
3234    returned as B<Sim> objects. A Sim object is actually a list reference that has been blessed
3235    so that its elements can be accessed by name.
3236    
3237    Similarities can be either raw or expanded. The raw similarities are basic
3238    hits between features with similar DNA. Expanding a raw similarity drags in any
3239    features considered substantially identical. So, for example, if features B<A1>,
3240    B<A2>, and B<A3> are all substatially identical to B<A>, then a raw similarity
3241    B<[C,A]> would be expanded to B<[C,A] [C,A1] [C,A2] [C,A3]>.
3242    
3243    =over 4
3244    
3245    =item fid
3246    
3247    ID of the feature whose similarities are desired.
3248    
3249    =item maxN
3250    
3251    Maximum number of similarities to return.
3252    
3253    =item maxP
3254    
3255    Minumum allowable similarity score.
3256    
3257    =item select
3258    
3259    Selection criterion: C<raw> means only raw similarities are returned; C<fig>
3260    means only similarities to FIG features are returned; C<all> means all expanded
3261    similarities are returned; and C<figx> means similarities are expanded until the
3262    number of FIG features equals the maximum.
3263    
3264    =item max_expand
3265    
3266    The maximum number of features to expand.
3267    
3268    =item filters
3269    
3270    Reference to a hash containing filter information, or a subroutine that can be
3271    used to filter the sims.
3272    
3273    =item RETURN
3274    
3275    Returns a reference to a list of similarity objects, or C<undef> if an error
3276    occurred.
3277    
3278    =back
3279    
3280    =cut
3281    
3282    sub Sims {
3283        # Get the parameters.
3284        my ($self, $fid, $maxN, $maxP, $select, $max_expand, $filters) = @_;
3285        # Create the shim object to test for deleted FIDs.
3286        my $shim = FidCheck->new($self);
3287        # Ask the network for sims.
3288        my $retVal = FIGRules::GetNetworkSims($shim, $fid, {}, $maxN, $maxP, $select, $max_expand, $filters);
3289        # Return the result.
3290        return $retVal;
3291    }
3292    
3293    =head3 IsAllGenomes
3294    
3295    C<< my $flag = $sprout->IsAllGenomes(\@list, \@checkList); >>
3296    
3297    Return TRUE if all genomes in the second list are represented in the first list at
3298    least one. Otherwise, return FALSE. If the second list is omitted, the first list is
3299    compared to a list of all the genomes.
3300    
3301    =over 4
3302    
3303    =item list
3304    
3305    Reference to the list to be compared to the second list.
3306    
3307    =item checkList (optional)
3308    
3309    Reference to the comparison target list. Every genome ID in this list must occur at
3310    least once in the first list. If this parameter is omitted, a list of all the genomes
3311    is used.
3312    
3313    =item RETURN
3314    
3315    Returns TRUE if every item in the second list appears at least once in the
3316    first list, else FALSE.
3317    
3318    =back
3319    
3320    =cut
3321    
3322    sub IsAllGenomes {
3323        # Get the parameters.
3324        my ($self, $list, $checkList) = @_;
3325        # Supply the checklist if it was omitted.
3326        $checkList = [$self->Genomes()] if ! defined($checkList);
3327        # Create a hash of the original list.
3328        my %testList = map { $_ => 1 } @{$list};
3329        # Declare the return variable. We assume that the representation
3330        # is complete and stop at the first failure.
3331        my $retVal = 1;
3332        my $n = scalar @{$checkList};
3333        for (my $i = 0; $retVal && $i < $n; $i++) {
3334            if (! $testList{$checkList->[$i]}) {
3335                $retVal = 0;
3336            }
3337        }
3338        # Return the result.
3339        return $retVal;
3340    }
3341    
3342  =head3 GetGroups  =head3 GetGroups
3343    
3344  C<< my %groups = $sprout->GetGroups(\@groupList); >>  C<< my %groups = $sprout->GetGroups(\@groupList); >>
# Line 3121  Line 3360 
3360          # Here we have a group list. Loop through them individually,          # Here we have a group list. Loop through them individually,
3361          # getting a list of the relevant genomes.          # getting a list of the relevant genomes.
3362          for my $group (@{$groupList}) {          for my $group (@{$groupList}) {
3363              my @genomeIDs = $self->GetFlat(['Genome'], "Genome(group-name) = ?",              my @genomeIDs = $self->GetFlat(['Genome'], "Genome(primary-group) = ?",
3364                  [$group], "Genome(id)");                  [$group], "Genome(id)");
3365              $retVal{$group} = \@genomeIDs;              $retVal{$group} = \@genomeIDs;
3366          }          }
# Line 3129  Line 3368 
3368          # 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
3369          # of the genome records, putting each one found into the appropriate          # of the genome records, putting each one found into the appropriate
3370          # 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
3371          # in groups are included in the return set.          # in real NMPDR groups are included in the return set.
3372          my @genomes = $self->GetAll(['Genome'], "Genome(group-name) > ' '", [],          my @genomes = $self->GetAll(['Genome'], "Genome(primary-group) <> ?",
3373                                      ['Genome(id)', 'Genome(group-name)']);                                      [$FIG_Config::otherGroup], ['Genome(id)', 'Genome(primary-group)']);
3374          # Loop through the genomes found.          # Loop through the genomes found.
3375          for my $genome (@genomes) {          for my $genome (@genomes) {
3376              # Pop this genome's ID off the current list.              # Pop this genome's ID off the current list.
# Line 3249  Line 3488 
3488      # Get the parameters.      # Get the parameters.
3489      my ($self, $genomeID, $testFlag) = @_;      my ($self, $genomeID, $testFlag) = @_;
3490      # Perform the delete for the genome's features.      # Perform the delete for the genome's features.
3491      my $retVal = $self->{_erdb}->Delete('Feature', "fig|$genomeID.%", $testFlag);      my $retVal = $self->Delete('Feature', "fig|$genomeID.%", $testFlag);
3492      # Perform the delete for the primary genome data.      # Perform the delete for the primary genome data.
3493      my $stats = $self->{_erdb}->Delete('Genome', $genomeID, $testFlag);      my $stats = $self->Delete('Genome', $genomeID, $testFlag);
3494      $retVal->Accumulate($stats);      $retVal->Accumulate($stats);
3495      # Return the result.      # Return the result.
3496      return $retVal;      return $retVal;
3497  }  }
3498    
3499  =head2 Internal Utility Methods  =head3 Fix
3500    
3501  =head3 ParseAssignment  C<< my %fixedHash = Sprout::Fix(%groupHash); >>
3502    
3503  Parse annotation text to determine whether or not it is a functional assignment. If it is,  Prepare a genome group hash (like that returned by L</GetGroups> for processing.
3504  the user, function text, and assigning user will be returned as a 3-element list. If it  Groups with the same primary name will be combined. The primary name is the
3505  isn't, an empty list will be returned.  first capitalized word in the group name.
3506    
3507  A functional assignment is always of the form  =over 4
3508    
3509      C<set >I<YYYY>C< function to\n>I<ZZZZZ>  =item groupHash
3510    
3511  where I<YYYY> is the B<user>, and I<ZZZZ> is the actual functional role. In most cases,  Hash to be fixed up.
 the user and the assigning user (from MadeAnnotation) will be the same, but that is  
 not always the case.  
3512    
3513  In addition, the functional role may contain extra data that is stripped, such as  =item RETURN
 terminating spaces or a comment separated from the rest of the text by a tab.  
3514    
3515  This is a static method.  Returns a fixed-up version of the hash.
3516    
3517  =over 4  =back
3518    
3519  =item user  =cut
3520    
3521  Name of the assigning user.  sub Fix {
3522        # Get the parameters.
3523        my (%groupHash) = @_;
3524        # Create the result hash.
3525        my %retVal = ();
3526        # Copy over the genomes.
3527        for my $groupID (keys %groupHash) {
3528            # Make a safety copy of the group ID.
3529            my $realGroupID = $groupID;
3530            # Yank the primary name.
3531            if ($groupID =~ /([A-Z]\w+)/) {
3532                $realGroupID = $1;
3533            }
3534            # Append this group's genomes into the result hash.
3535            Tracer::AddToListMap(\%retVal, $realGroupID, @{$groupHash{$groupID}});
3536        }
3537        # Return the result hash.
3538        return %retVal;
3539    }
3540    
3541  =item text  =head3 GroupPageName
3542    
3543  Text of the annotation.  C<< my $name = $sprout->GroupPageName($group); >>
3544    
3545    Return the name of the page for the specified NMPDR group.
3546    
3547    =over 4
3548    
3549    =item group
3550    
3551    Name of the relevant group.
3552    
3553  =item RETURN  =item RETURN
3554    
3555  Returns an empty list if the annotation is not a functional assignment; otherwise, returns  Returns the relative page name (e.g. C<../content/campy.php>). If the group file is not in
3556  a two-element list containing the user name and the function text.  memory it will be read in.
3557    
3558  =back  =back
3559    
3560  =cut  =cut
3561    
3562  sub _ParseAssignment {  sub GroupPageName {
3563      # Get the parameters.      # Get the parameters.
3564      my ($user, $text) = @_;      my ($self, $group) = @_;
3565      # Declare the return value.      # Declare the return variable.
3566      my @retVal = ();      my $retVal;
3567      # Check to see if this is a functional assignment.      # Check for the group file data.
3568      my ($type, $function) = split(/\n/, $text);      if (! defined $self->{groupHash}) {
3569      if ($type =~ m/^set function to$/i) {          # Read the group file.
3570          # Here we have an assignment without a user, so we use the incoming user ID.          my %groupData = Sprout::ReadGroupFile($self->{_options}->{dataDir} . "/groups.tbl");
3571          @retVal = ($user, $function);          # Store it in our object.
3572      } elsif ($type =~ m/^set (\S+) function to$/i) {          $self->{groupHash} = \%groupData;
         # Here we have an assignment with a user that is passed back to the caller.  
         @retVal = ($1, $function);  
3573      }      }
3574      # If we have an assignment, we need to clean the function text. There may be      # Compute the real group name.
3575      # extra junk at the end added as a note from the user.      my $realGroup = $group;
3576      if (@retVal) {      if ($group =~ /([A-Z]\w+)/) {
3577          $retVal[1] =~ s/(\t\S)?\s*$//;          $realGroup = $1;
3578      }      }
3579      # Return the result list.      # Return the page name.
3580      return @retVal;      $retVal = "../content/" . $self->{groupHash}->{$realGroup}->[1];
3581        # Return the result.
3582        return $retVal;
3583  }  }
3584    
3585  =head3 FriendlyTimestamp  =head3 ReadGroupFile
3586    
3587  Convert a time number to a user-friendly time stamp for display.  C<< my %groupData = Sprout::ReadGroupFile($groupFileName); >>
3588    
3589  This is a static method.  Read in the data from the specified group file. The group file contains information
3590    about each of the NMPDR groups.
3591    
3592  =over 4  =over 4
3593    
3594  =item timeValue  =item name
3595    
3596  Numeric time value.  Name of the group.
3597    
3598    =item page
3599    
3600    Name of the group's page on the web site (e.g. C<campy.php> for
3601    Campylobacter)
3602    
3603    =item genus
3604    
3605    Genus of the group
3606    
3607    =item species
3608    
3609    Species of the group, or an empty string if the group is for an entire
3610    genus. If the group contains more than one species, the species names
3611    should be separated by commas.
3612    
3613    =back
3614    
3615    The parameters to this method are as follows
3616    
3617    =over 4
3618    
3619    =item groupFile
3620    
3621    Name of the file containing the group data.
3622    
3623  =item RETURN  =item RETURN
3624    
3625  Returns a string containing the same time in user-readable format.  Returns a hash keyed on group name. The value of each hash
3626    
3627  =back  =back
3628    
3629  =cut  =cut
3630    
3631  sub FriendlyTimestamp {  sub ReadGroupFile {
3632      my ($timeValue) = @_;      # Get the parameters.
3633      my $retVal = localtime($timeValue);      my ($groupFileName) = @_;
3634      return $retVal;      # Declare the return variable.
3635        my %retVal;
3636        # Read the group file.
3637        my @groupLines = Tracer::GetFile($groupFileName);
3638        for my $groupLine (@groupLines) {
3639            my ($name, $page, $genus, $species) = split(/\t/, $groupLine);
3640            $retVal{$name} = [$page, $genus, $species];
3641        }
3642        # Return the result.
3643        return %retVal;
3644  }  }
3645    
3646  =head3 AddProperty  =head3 AddProperty
# Line 3403  Line 3700 
3700      $self->Insert('HasProperty', { 'from-link' => $featureID, 'to-link' => $propID, evidence => $url });      $self->Insert('HasProperty', { 'from-link' => $featureID, 'to-link' => $propID, evidence => $url });
3701  }  }
3702    
3703    =head2 Virtual Methods
3704    
3705    =head3 CleanKeywords
3706    
3707    C<< my $cleanedString = $sprout->CleanKeywords($searchExpression); >>
3708    
3709    Clean up a search expression or keyword list. This involves converting the periods
3710    in EC numbers to underscores, converting non-leading minus signs to underscores,
3711    a vertical bar or colon to an apostrophe, and forcing lower case for all alphabetic
3712    characters. In addition, any extra spaces are removed.
3713    
3714    =over 4
3715    
3716    =item searchExpression
3717    
3718    Search expression or keyword list to clean. Note that a search expression may
3719    contain boolean operators which need to be preserved. This includes leading
3720    minus signs.
3721    
3722    =item RETURN
3723    
3724    Cleaned expression or keyword list.
3725    
3726    =back
3727    
3728    =cut
3729    
3730    sub CleanKeywords {
3731        # Get the parameters.
3732        my ($self, $searchExpression) = @_;
3733        # Perform the standard cleanup.
3734        my $retVal = $self->ERDB::CleanKeywords($searchExpression);
3735        # Fix the periods in EC and TC numbers.
3736        $retVal =~ s/(\d+|\-)\.(\d+|-)\.(\d+|-)\.(\d+|-)/$1_$2_$3_$4/g;
3737        # Fix non-trailing periods.
3738        $retVal =~ s/\.(\w)/_$1/g;
3739        # Fix non-leading minus signs.
3740        $retVal =~ s/(\w)[\-]/$1_/g;
3741        # Fix the vertical bars and colons
3742        $retVal =~ s/(\w)[|:](\w)/$1'$2/g;
3743        # Return the result.
3744        return $retVal;
3745    }
3746    
3747    =head2 Internal Utility Methods
3748    
3749    =head3 ParseAssignment
3750    
3751    Parse annotation text to determine whether or not it is a functional assignment. If it is,
3752    the user, function text, and assigning user will be returned as a 3-element list. If it
3753    isn't, an empty list will be returned.
3754    
3755    A functional assignment is always of the form
3756    
3757        C<set >I<YYYY>C< function to\n>I<ZZZZZ>
3758    
3759    where I<YYYY> is the B<user>, and I<ZZZZ> is the actual functional role. In most cases,
3760    the user and the assigning user (from MadeAnnotation) will be the same, but that is
3761    not always the case.
3762    
3763    In addition, the functional role may contain extra data that is stripped, such as
3764    terminating spaces or a comment separated from the rest of the text by a tab.
3765    
3766    This is a static method.
3767    
3768    =over 4
3769    
3770    =item user
3771    
3772    Name of the assigning user.
3773    
3774    =item text
3775    
3776    Text of the annotation.
3777    
3778    =item RETURN
3779    
3780    Returns an empty list if the annotation is not a functional assignment; otherwise, returns
3781    a two-element list containing the user name and the function text.
3782    
3783    =back
3784    
3785    =cut
3786    
3787    sub _ParseAssignment {
3788        # Get the parameters.
3789        my ($user, $text) = @_;
3790        # Declare the return value.
3791        my @retVal = ();
3792        # Check to see if this is a functional assignment.
3793        my ($type, $function) = split(/\n/, $text);
3794        if ($type =~ m/^set function to$/i) {
3795            # Here we have an assignment without a user, so we use the incoming user ID.
3796            @retVal = ($user, $function);
3797        } elsif ($type =~ m/^set (\S+) function to$/i) {
3798            # Here we have an assignment with a user that is passed back to the caller.
3799            @retVal = ($1, $function);
3800        }
3801        # If we have an assignment, we need to clean the function text. There may be
3802        # extra junk at the end added as a note from the user.
3803        if (defined( $retVal[1] )) {
3804            $retVal[1] =~ s/(\t\S)?\s*$//;
3805        }
3806        # Return the result list.
3807        return @retVal;
3808    }
3809    
3810    =head3 FriendlyTimestamp
3811    
3812    Convert a time number to a user-friendly time stamp for display.
3813    
3814    This is a static method.
3815    
3816    =over 4
3817    
3818    =item timeValue
3819    
3820    Numeric time value.
3821    
3822    =item RETURN
3823    
3824    Returns a string containing the same time in user-readable format.
3825    
3826    =back
3827    
3828    =cut
3829    
3830    sub FriendlyTimestamp {
3831        my ($timeValue) = @_;
3832        my $retVal = localtime($timeValue);
3833        return $retVal;
3834    }
3835    
3836    
3837  1;  1;

Legend:
Removed from v.1.54  
changed lines
  Added in v.1.94

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3