[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.94, Fri Nov 3 00:42:25 2006 UTC revision 1.105, Wed Jan 30 22:14:02 2008 UTC
# Line 1  Line 1 
1  package Sprout;  package Sprout;
2    
     require Exporter;  
     use ERDB;  
     @ISA = qw(Exporter ERDB);  
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;
9      use Tracer;      use Tracer;
10      use FIGRules;      use FIGRules;
11      use FidCheck;      use FidCheck;
12      use Stats;      use Stats;
13      use POSIX qw(strftime);      use POSIX qw(strftime);
14      use BasicLocation;      use BasicLocation;
15        use CustomAttributes;
16        use RemoteCustomAttributes;
17        use base qw(ERDB);
18    
19  =head1 Sprout Database Manipulation Object  =head1 Sprout Database Manipulation Object
20    
# Line 28  Line 27 
27  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>
28  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>.
29    
30  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' });
31    
32  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
33  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
34  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
35  L</dna_seq> returns the DNA sequence for a specified genome location.  L</DNASeq> returns the DNA sequence for a specified genome location.
36    
37  The Sprout object is a subclass of the ERDB object and inherits all its properties and methods.  The Sprout object is a subclass of the ERDB object and inherits all its properties and methods.
38    
# Line 45  Line 44 
44    
45  =head3 new  =head3 new
46    
47  C<< my $sprout = Sprout->new($dbName, \%options); >>      my $sprout = Sprout->new($dbName, \%options);
48    
49  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
50  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 79  Line 78 
78    
79  * B<noDBOpen> suppresses the connection to the database if TRUE, else FALSE  * B<noDBOpen> suppresses the connection to the database if TRUE, else FALSE
80    
81    * B<host> name of the database host
82    
83  =back  =back
84    
85  For example, the following constructor call specifies a database named I<Sprout> and a user name of  For example, the following constructor call specifies a database named I<Sprout> and a user name of
86  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
87  F</usr/fig/SproutData>.  F</usr/fig/SproutData>.
88    
89  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' });
90    
91  =cut  =cut
92    
# Line 109  Line 110 
110                         port         => $FIG_Config::dbport,                         port         => $FIG_Config::dbport,
111                                                          # database connection port                                                          # database connection port
112                         sock         => $FIG_Config::dbsock,                         sock         => $FIG_Config::dbsock,
113                         host         => $FIG_Config::dbhost,                         host         => $FIG_Config::sprout_host,
114                         maxSegmentLength => 4500,        # maximum feature segment length                         maxSegmentLength => 4500,        # maximum feature segment length
115                         maxSequenceLength => 8000,       # maximum contig sequence length                         maxSequenceLength => 8000,       # maximum contig sequence length
116                         noDBOpen     => 0,               # 1 to suppress the database open                         noDBOpen     => 0,               # 1 to suppress the database open
# Line 122  Line 123 
123      # Connect to the database.      # Connect to the database.
124      my $dbh;      my $dbh;
125      if (! $optionTable->{noDBOpen}) {      if (! $optionTable->{noDBOpen}) {
126            Trace("Connect data: host = $optionTable->{host}, port = $optionTable->{port}.") if T(3);
127          $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,          $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,
128                                  $password, $optionTable->{port}, $optionTable->{host}, $optionTable->{sock});                                  $password, $optionTable->{port}, $optionTable->{host}, $optionTable->{sock});
129      }      }
# Line 133  Line 135 
135      $retVal->{_xmlName} = $xmlFileName;      $retVal->{_xmlName} = $xmlFileName;
136      # Set up space for the group file data.      # Set up space for the group file data.
137      $retVal->{groupHash} = undef;      $retVal->{groupHash} = undef;
138        # Set up space for the genome hash. We use this to identify NMPDR genomes.
139        $retVal->{genomeHash} = undef;
140        # Connect to the attributes.
141        if ($FIG_Config::attrURL) {
142            Trace("Remote attribute server $FIG_Config::attrURL chosen.") if T(3);
143            $retVal->{_ca} = RemoteCustomAttributes->new($FIG_Config::attrURL);
144        } elsif ($FIG_Config::attrDbName) {
145            Trace("Local attribute database $FIG_Config::attrDbName chosen.") if T(3);
146            my $user = ($FIG_Config::arch eq 'win' ? 'self' : scalar(getpwent()));
147            $retVal->{_ca} = CustomAttributes->new(user => $user);
148        }
149      # Return it.      # Return it.
150      return $retVal;      return $retVal;
151  }  }
152    
153  =head3 MaxSegment  =head3 MaxSegment
154    
155  C<< my $length = $sprout->MaxSegment(); >>      my $length = $sprout->MaxSegment();
156    
157  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
158  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 156  Line 169 
169    
170  =head3 MaxSequence  =head3 MaxSequence
171    
172  C<< my $length = $sprout->MaxSequence(); >>      my $length = $sprout->MaxSequence();
173    
174  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
175  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 171  Line 184 
184    
185  =head3 Load  =head3 Load
186    
187  C<< $sprout->Load($rebuild); >>;      $sprout->Load($rebuild);;
188    
189  Load the database from files in the data directory, optionally re-creating the tables.  Load the database from files in the data directory, optionally re-creating the tables.
190    
# Line 211  Line 224 
224    
225  =head3 LoadUpdate  =head3 LoadUpdate
226    
227  C<< my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>      my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList);
228    
229  Load updates to one or more database tables. This method enables the client to make changes to one  Load updates to one or more database tables. This method enables the client to make changes to one
230  or two tables without reloading the whole database. For each table, there must be a corresponding  or two tables without reloading the whole database. For each table, there must be a corresponding
# Line 257  Line 270 
270              Trace("No load file found for $tableName in $dataDir.") if T(0);              Trace("No load file found for $tableName in $dataDir.") if T(0);
271          } else {          } else {
272              # Attempt to load this table.              # Attempt to load this table.
273              my $result = $self->LoadTable($fileName, $tableName, $truncateFlag);              my $result = $self->LoadTable($fileName, $tableName, truncate => $truncateFlag);
274              # Accumulate the resulting statistics.              # Accumulate the resulting statistics.
275              $retVal->Accumulate($result);              $retVal->Accumulate($result);
276          }          }
# Line 268  Line 281 
281    
282  =head3 GenomeCounts  =head3 GenomeCounts
283    
284  C<< my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete); >>      my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete);
285    
286  Count the number of genomes in each domain. If I<$complete> is TRUE, only complete  Count the number of genomes in each domain. If I<$complete> is TRUE, only complete
287  genomes will be included in the counts.  genomes will be included in the counts.
# Line 313  Line 326 
326    
327  =head3 ContigCount  =head3 ContigCount
328    
329  C<< my $count = $sprout->ContigCount($genomeID); >>      my $count = $sprout->ContigCount($genomeID);
330    
331  Return the number of contigs for the specified genome ID.  Return the number of contigs for the specified genome ID.
332    
# Line 342  Line 355 
355    
356  =head3 GeneMenu  =head3 GeneMenu
357    
358  C<< my $selectHtml = $sprout->GeneMenu(\%attributes, $filterString, \@params, $selected); >>      my $selectHtml = $sprout->GeneMenu(\%attributes, $filterString, \@params, $selected);
359    
360  Return an HTML select menu of genomes. Each genome will be an option in the menu,  Return an HTML select menu of genomes. Each genome will be an option in the menu,
361  and will be displayed by name with the ID and a contig count attached. The selection  and will be displayed by name with the ID and a contig count attached. The selection
# Line 423  Line 436 
436    
437  =head3 Build  =head3 Build
438    
439  C<< $sprout->Build(); >>      $sprout->Build();
440    
441  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.
442  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 440  Line 453 
453    
454  =head3 Genomes  =head3 Genomes
455    
456  C<< my @genomes = $sprout->Genomes(); >>      my @genomes = $sprout->Genomes();
457    
458  Return a list of all the genome IDs.  Return a list of all the genome IDs.
459    
# Line 457  Line 470 
470    
471  =head3 GenusSpecies  =head3 GenusSpecies
472    
473  C<< my $infoString = $sprout->GenusSpecies($genomeID); >>      my $infoString = $sprout->GenusSpecies($genomeID);
474    
475  Return the genus, species, and unique characterization for a genome.  Return the genus, species, and unique characterization for a genome.
476    
# Line 489  Line 502 
502    
503  =head3 FeaturesOf  =head3 FeaturesOf
504    
505  C<< my @features = $sprout->FeaturesOf($genomeID, $ftype); >>      my @features = $sprout->FeaturesOf($genomeID, $ftype);
506    
507  Return a list of the features relevant to a specified genome.  Return a list of the features relevant to a specified genome.
508    
# Line 534  Line 547 
547    
548  =head3 FeatureLocation  =head3 FeatureLocation
549    
550  C<< my @locations = $sprout->FeatureLocation($featureID); >>      my @locations = $sprout->FeatureLocation($featureID);
551    
552  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
553  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 563  Line 576 
576  =back  =back
577    
578  =cut  =cut
579  #: Return Type @;  
 #: Return Type $;  
580  sub FeatureLocation {  sub FeatureLocation {
581      # Get the parameters.      # Get the parameters.
582      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
583      # Create a query for the feature locations.      # Get the feature record.
584      my $query = $self->Get(['IsLocatedIn'], "IsLocatedIn(from-link) = ? ORDER BY IsLocatedIn(locN)",      my $object = $self->GetEntity('Feature', $featureID);
585                             [$featureID]);      Confess("Feature $featureID not found.") if ! defined($object);
586        # Get the location string.
587        my $locString = $object->PrimaryValue('Feature(location-string)');
588      # Create the return list.      # Create the return list.
589      my @retVal = ();      my @retVal = split /\s*,\s*/, $locString;
     # Set up the variables used to determine if we have adjacent segments. This initial setup will  
     # not match anything.  
     my ($prevContig, $prevBeg, $prevDir, $prevLen) = ("", 0, "0", 0);  
     # Loop through the query results, creating location specifiers.  
     while (my $location = $query->Fetch()) {  
         # Get the location parameters.  
         my ($contigID, $beg, $dir, $len) = $location->Values(['IsLocatedIn(to-link)',  
             'IsLocatedIn(beg)', 'IsLocatedIn(dir)', 'IsLocatedIn(len)']);  
         # 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 + $len == $prevBeg) {  
                 # Here we're merging two backward blocks, so we keep the new begin point  
                 # and adjust the length.  
                 $len += $prevLen;  
                 # Pop the old segment off. The new one will replace it later.  
                 pop @retVal;  
             } elsif ($dir eq "+" && $beg == $prevBeg + $prevLen) {  
                 # Here we need to merge two forward blocks. 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";  
     }  
590      # Return the list in the format indicated by the context.      # Return the list in the format indicated by the context.
591      return (wantarray ? @retVal : join(',', @retVal));      return (wantarray ? @retVal : join(',', @retVal));
592  }  }
593    
594  =head3 ParseLocation  =head3 ParseLocation
595    
596  C<< my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location); >>      my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location);
597    
598  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
599  length.  length.
# Line 632  Line 612 
612  =back  =back
613    
614  =cut  =cut
615  #: Return Type @;  
616  sub ParseLocation {  sub ParseLocation {
617      # 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
618      # the first parameter.      # the first parameter.
# Line 659  Line 639 
639    
640  =head3 PointLocation  =head3 PointLocation
641    
642  C<< my $found = Sprout::PointLocation($location, $point); >>      my $found = Sprout::PointLocation($location, $point);
643    
644  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
645  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 688  Line 668 
668  =back  =back
669    
670  =cut  =cut
671  #: Return Type $;  
672  sub PointLocation {  sub PointLocation {
673      # 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
674      # the first parameter.      # the first parameter.
# Line 711  Line 691 
691    
692  =head3 DNASeq  =head3 DNASeq
693    
694  C<< my $sequence = $sprout->DNASeq(\@locationList); >>      my $sequence = $sprout->DNASeq(\@locationList);
695    
696  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
697  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,
# Line 795  Line 775 
775    
776  =head3 AllContigs  =head3 AllContigs
777    
778  C<< my @idList = $sprout->AllContigs($genomeID); >>      my @idList = $sprout->AllContigs($genomeID);
779    
780  Return a list of all the contigs for a genome.  Return a list of all the contigs for a genome.
781    
# Line 825  Line 805 
805    
806  =head3 GenomeLength  =head3 GenomeLength
807    
808  C<< my $length = $sprout->GenomeLength($genomeID); >>      my $length = $sprout->GenomeLength($genomeID);
809    
810  Return the length of the specified genome in base pairs.  Return the length of the specified genome in base pairs.
811    
# Line 860  Line 840 
840    
841  =head3 FeatureCount  =head3 FeatureCount
842    
843  C<< my $count = $sprout->FeatureCount($genomeID, $type); >>      my $count = $sprout->FeatureCount($genomeID, $type);
844    
845  Return the number of features of the specified type in the specified genome.  Return the number of features of the specified type in the specified genome.
846    
# Line 895  Line 875 
875    
876  =head3 GenomeAssignments  =head3 GenomeAssignments
877    
878  C<< my $fidHash = $sprout->GenomeAssignments($genomeID); >>      my $fidHash = $sprout->GenomeAssignments($genomeID);
879    
880  Return a list of a genome's assigned features. The return hash will contain each  Return a list of a genome's assigned features. The return hash will contain each
881  assigned feature of the genome mapped to the text of its most recent functional  assigned feature of the genome mapped to the text of its most recent functional
# Line 938  Line 918 
918    
919  =head3 ContigLength  =head3 ContigLength
920    
921  C<< my $length = $sprout->ContigLength($contigID); >>      my $length = $sprout->ContigLength($contigID);
922    
923  Compute the length of a contig.  Compute the length of a contig.
924    
# Line 977  Line 957 
957    
958  =head3 ClusterPEGs  =head3 ClusterPEGs
959    
960  C<< my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs); >>      my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs);
961    
962  Cluster the PEGs in a list according to the cluster coding scheme of the specified  Cluster the PEGs in a list according to the cluster coding scheme of the specified
963  subsystem. In order for this to work properly, the subsystem object must have  subsystem. In order for this to work properly, the subsystem object must have
964  been used recently to retrieve the PEGs using the B<get_pegs_from_cell> method.  been used recently to retrieve the PEGs using the B<get_pegs_from_cell> or
965  This causes the cluster numbers to be pulled into the subsystem's color hash.  B<get_row> methods. This causes the cluster numbers to be pulled into the
966  If a PEG is not found in the color hash, it will not appear in the output  subsystem's color hash. If a PEG is not found in the color hash, it will not
967  sequence.  appear in the output sequence.
968    
969  =over 4  =over 4
970    
# Line 1025  Line 1005 
1005    
1006  =head3 GenesInRegion  =head3 GenesInRegion
1007    
1008  C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>      my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop);
1009    
1010  List the features which overlap a specified region in a contig.  List the features which overlap a specified region in a contig.
1011    
# Line 1054  Line 1034 
1034  =back  =back
1035    
1036  =cut  =cut
1037  #: Return Type @@;  
1038  sub GenesInRegion {  sub GenesInRegion {
1039      # Get the parameters.      # Get the parameters.
1040      my ($self, $contigID, $start, $stop) = @_;      my ($self, $contigID, $start, $stop) = @_;
1041      # Get the maximum segment length.      # Get the maximum segment length.
1042      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 = ();  
1043      # Prime the values we'll use for the returned beginning and end.      # Prime the values we'll use for the returned beginning and end.
1044      my @initialMinMax = ($self->ContigLength($contigID), 0);      my @initialMinMax = ($self->ContigLength($contigID), 0);
1045      my ($min, $max) = @initialMinMax;      my ($min, $max) = @initialMinMax;
1046      # Create a table of parameters for each query. Each query looks for features travelling in      # Get the overlapping features.
1047        my @featureObjects = $self->GeneDataInRegion($contigID, $start, $stop);
1048        # We'l use this hash to help us track the feature IDs and sort them. The key is the
1049        # feature ID and the value is a [$left,$right] pair indicating the maximum extent
1050        # of the feature's locations.
1051        my %featureMap = ();
1052        # Loop through them to do the begin/end analysis.
1053        for my $featureObject (@featureObjects) {
1054            # Get the feature's location string. This may contain multiple actual locations.
1055            my ($locations, $fid) = $featureObject->Values([qw(Feature(location-string) Feature(id))]);
1056            my @locationSegments = split /\s*,\s*/, $locations;
1057            # Loop through the locations.
1058            for my $locationSegment (@locationSegments) {
1059                # Construct an object for the location.
1060                my $locationObject = BasicLocation->new($locationSegment);
1061                # Merge the current segment's begin and end into the min and max.
1062                my ($left, $right) = ($locationObject->Left, $locationObject->Right);
1063                my ($beg, $end);
1064                if (exists $featureMap{$fid}) {
1065                    ($beg, $end) = @{$featureMap{$fid}};
1066                    $beg = $left if $left < $beg;
1067                    $end = $right if $right > $end;
1068                } else {
1069                    ($beg, $end) = ($left, $right);
1070                }
1071                $min = $beg if $beg < $min;
1072                $max = $end if $end > $max;
1073                # Store the feature's new extent back into the hash table.
1074                $featureMap{$fid} = [$beg, $end];
1075            }
1076        }
1077        # Now we must compute the list of the IDs for the features found. We start with a list
1078        # of midpoints / feature ID pairs. (It's not really a midpoint, it's twice the midpoint,
1079        # but the result of the sort will be the same.)
1080        my @list = map { [$featureMap{$_}->[0] + $featureMap{$_}->[1], $_] } keys %featureMap;
1081        # Now we sort by midpoint and yank out the feature IDs.
1082        my @retVal = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @list;
1083        # Return it along with the min and max.
1084        return (\@retVal, $min, $max);
1085    }
1086    
1087    =head3 GeneDataInRegion
1088    
1089        my @featureList = $sprout->GenesInRegion($contigID, $start, $stop);
1090    
1091    List the features which overlap a specified region in a contig.
1092    
1093    =over 4
1094    
1095    =item contigID
1096    
1097    ID of the contig containing the region of interest.
1098    
1099    =item start
1100    
1101    Offset of the first residue in the region of interest.
1102    
1103    =item stop
1104    
1105    Offset of the last residue in the region of interest.
1106    
1107    =item RETURN
1108    
1109    Returns a list of B<ERDBObjects> for the desired features. Each object will
1110    contain a B<Feature> record.
1111    
1112    =back
1113    
1114    =cut
1115    
1116    sub GeneDataInRegion {
1117        # Get the parameters.
1118        my ($self, $contigID, $start, $stop) = @_;
1119        # Get the maximum segment length.
1120        my $maximumSegmentLength = $self->MaxSegment;
1121        # Create a hash to receive the feature list. We use a hash so that we can eliminate
1122        # duplicates easily. The hash key will be the feature ID. The value will be the feature's
1123        # ERDBObject from the query.
1124        my %featuresFound = ();
1125        # Create a table of parameters for the queries. Each query looks for features travelling in
1126      # 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,
1127      # 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
1128      # 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 1077  Line 1131 
1131      # Loop through the query parameters.      # Loop through the query parameters.
1132      for my $parms (values %queryParms) {      for my $parms (values %queryParms) {
1133          # Create the query.          # Create the query.
1134          my $query = $self->Get(['IsLocatedIn'],          my $query = $self->Get([qw(Feature IsLocatedIn)],
1135              "IsLocatedIn(to-link)= ? AND IsLocatedIn(dir) = ? AND IsLocatedIn(beg) >= ? AND IsLocatedIn(beg) <= ?",              "IsLocatedIn(to-link)= ? AND IsLocatedIn(dir) = ? AND IsLocatedIn(beg) >= ? AND IsLocatedIn(beg) <= ?",
1136              $parms);              $parms);
1137          # Loop through the feature segments found.          # Loop through the feature segments found.
1138          while (my $segment = $query->Fetch) {          while (my $segment = $query->Fetch) {
1139              # Get the data about this segment.              # Get the data about this segment.
1140              my ($featureID, $dir, $beg, $len) = $segment->Values(['IsLocatedIn(from-link)',              my ($featureID, $contig, $dir, $beg, $len) = $segment->Values([qw(IsLocatedIn(from-link)
1141                  'IsLocatedIn(dir)', 'IsLocatedIn(beg)', 'IsLocatedIn(len)']);                  IsLocatedIn(to-link) IsLocatedIn(dir) IsLocatedIn(beg) IsLocatedIn(len))]);
1142              # 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
1143              # 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
1144              # 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
1145              # length.              # length.
1146              my ($found, $end) = (0, 0);              my $loc = BasicLocation->new($contig, $beg, $dir, $len);
1147              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;  
                 }  
             }  
1148              if ($found) {              if ($found) {
1149                  # Here we need to record the feature and update the minima and maxima. First,                  # Save this feature in the result list.
1150                  # get the current entry for the specified feature.                  $featuresFound{$featureID} = $segment;
                 my ($loc1, $loc2) = (exists $featuresFound{$featureID} ? @{$featuresFound{$featureID}} :  
                                      @initialMinMax);  
                 # Merge the current segment's begin and end into the feature begin and end and the  
                 # global min and max.  
                 if ($beg < $loc1) {  
                     $loc1 = $beg;  
                     $min = $beg if $beg < $min;  
                 }  
                 if ($end > $loc2) {  
                     $loc2 = $end;  
                     $max = $end if $end > $max;  
1151                  }                  }
                 # Store the entry back into the hash table.  
                 $featuresFound{$featureID} = [$loc1, $loc2];  
1152              }              }
1153          }          }
1154      }      # Return the ERDB objects for the features found.
1155      # 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);  
1156  }  }
1157    
1158  =head3 FType  =head3 FType
1159    
1160  C<< my $ftype = $sprout->FType($featureID); >>      my $ftype = $sprout->FType($featureID);
1161    
1162  Return the type of a feature.  Return the type of a feature.
1163    
# Line 1167  Line 1187 
1187    
1188  =head3 FeatureAnnotations  =head3 FeatureAnnotations
1189    
1190  C<< my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag); >>      my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag);
1191    
1192  Return the annotations of a feature.  Return the annotations of a feature.
1193    
# Line 1230  Line 1250 
1250    
1251  =head3 AllFunctionsOf  =head3 AllFunctionsOf
1252    
1253  C<< my %functions = $sprout->AllFunctionsOf($featureID); >>      my %functions = $sprout->AllFunctionsOf($featureID);
1254    
1255  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
1256  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 1285  Line 1305 
1305    
1306  =head3 FunctionOf  =head3 FunctionOf
1307    
1308  C<< my $functionText = $sprout->FunctionOf($featureID, $userID); >>      my $functionText = $sprout->FunctionOf($featureID, $userID);
1309    
1310  Return the most recently-determined functional assignment of a particular feature.  Return the most recently-determined functional assignment of a particular feature.
1311    
# Line 1389  Line 1409 
1409    
1410  =head3 FunctionsOf  =head3 FunctionsOf
1411    
1412  C<< my @functionList = $sprout->FunctionOf($featureID, $userID); >>      my @functionList = $sprout->FunctionOf($featureID, $userID);
1413    
1414  Return the functional assignments of a particular feature.  Return the functional assignments of a particular feature.
1415    
# Line 1461  Line 1481 
1481    
1482  =head3 BBHList  =head3 BBHList
1483    
1484  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>      my $bbhHash = $sprout->BBHList($genomeID, \@featureList);
1485    
1486  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
1487  on a specified target genome.  on a specified target genome.
# Line 1511  Line 1531 
1531    
1532  =head3 SimList  =head3 SimList
1533    
1534  C<< my %similarities = $sprout->SimList($featureID, $count); >>      my %similarities = $sprout->SimList($featureID, $count);
1535    
1536  Return a list of the similarities to the specified feature.  Return a list of the similarities to the specified feature.
1537    
# Line 1547  Line 1567 
1567    
1568  =head3 IsComplete  =head3 IsComplete
1569    
1570  C<< my $flag = $sprout->IsComplete($genomeID); >>      my $flag = $sprout->IsComplete($genomeID);
1571    
1572  Return TRUE if the specified genome is complete, else FALSE.  Return TRUE if the specified genome is complete, else FALSE.
1573    
# Line 1575  Line 1595 
1595      my $genomeData = $self->GetEntity('Genome', $genomeID);      my $genomeData = $self->GetEntity('Genome', $genomeID);
1596      if ($genomeData) {      if ($genomeData) {
1597          # The genome exists, so get the completeness flag.          # The genome exists, so get the completeness flag.
1598          ($retVal) = $genomeData->Value('Genome(complete)');          $retVal = $genomeData->PrimaryValue('Genome(complete)');
1599      }      }
1600      # Return the result.      # Return the result.
1601      return $retVal;      return $retVal;
# Line 1583  Line 1603 
1603    
1604  =head3 FeatureAliases  =head3 FeatureAliases
1605    
1606  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>      my @aliasList = $sprout->FeatureAliases($featureID);
1607    
1608  Return a list of the aliases for a specified feature.  Return a list of the aliases for a specified feature.
1609    
# Line 1606  Line 1626 
1626      # Get the parameters.      # Get the parameters.
1627      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
1628      # Get the desired feature's aliases      # Get the desired feature's aliases
1629      my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(alias)']);      my @retVal = $self->GetFlat(['IsAliasOf'], "IsAliasOf(to-link) = ?", [$featureID], 'IsAliasOf(from-link)');
1630      # Return the result.      # Return the result.
1631      return @retVal;      return @retVal;
1632  }  }
1633    
1634  =head3 GenomeOf  =head3 GenomeOf
1635    
1636  C<< my $genomeID = $sprout->GenomeOf($featureID); >>      my $genomeID = $sprout->GenomeOf($featureID);
1637    
1638  Return the genome that contains a specified feature or contig.  Return the genome that contains a specified feature or contig.
1639    
# Line 1635  Line 1655 
1655  sub GenomeOf {  sub GenomeOf {
1656      # Get the parameters.      # Get the parameters.
1657      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
     # Create a query to find the genome associated with the incoming ID.  
     my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ? OR HasContig(to-link) = ?",  
                            [$featureID, $featureID]);  
1658      # Declare the return value.      # Declare the return value.
1659      my $retVal;      my $retVal;
1660      # Get the genome ID.      # Parse the genome ID from the feature ID.
1661      if (my $relationship = $query->Fetch()) {      if ($featureID =~ /^fig\|(\d+\.\d+)/) {
1662          ($retVal) = $relationship->Value('HasContig(from-link)');          $retVal = $1;
1663        } else {
1664            Confess("Invalid feature ID $featureID.");
1665      }      }
1666      # Return the value found.      # Return the value found.
1667      return $retVal;      return $retVal;
# Line 1650  Line 1669 
1669    
1670  =head3 CoupledFeatures  =head3 CoupledFeatures
1671    
1672  C<< my %coupleHash = $sprout->CoupledFeatures($featureID); >>      my %coupleHash = $sprout->CoupledFeatures($featureID);
1673    
1674  Return the features functionally coupled with a specified feature. Features are considered  Return the features functionally coupled with a specified feature. Features are considered
1675  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 1672  Line 1691 
1691  sub CoupledFeatures {  sub CoupledFeatures {
1692      # Get the parameters.      # Get the parameters.
1693      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
1694        # Ask the coupling server for the data.
1695      Trace("Looking for features coupled to $featureID.") if T(coupling => 3);      Trace("Looking for features coupled to $featureID.") if T(coupling => 3);
1696      # Create a query to retrieve the functionally-coupled features.      my @rawPairs = FIGRules::NetCouplingData('coupled_to', id1 => $featureID);
1697      my $query = $self->Get(['ParticipatesInCoupling', 'Coupling'],      Trace(scalar(@rawPairs) . " couplings returned.") if T(coupling => 3);
1698                             "ParticipatesInCoupling(from-link) = ?", [$featureID]);      # Form them into a hash.
     # This value will be set to TRUE if we find at least one coupled feature.  
     my $found = 0;  
     # Create the return hash.  
1699      my %retVal = ();      my %retVal = ();
1700      # Retrieve the relationship records and store them in the hash.      for my $pair (@rawPairs) {
1701      while (my $clustering = $query->Fetch()) {          # Get the feature ID and score.
1702          # Get the ID and score of the coupling.          my ($featureID2, $score) = @{$pair};
1703          my ($couplingID, $score) = $clustering->Values(['Coupling(id)',          # Only proceed if the feature is in NMPDR.
1704                                                          'Coupling(score)']);          if ($self->_CheckFeature($featureID2)) {
1705          Trace("$featureID coupled with score $score to ID $couplingID.") if T(coupling => 4);              $retVal{$featureID2} = $score;
1706          # Get the other feature that participates in the coupling.          }
         my ($otherFeatureID) = $self->GetFlat(['ParticipatesInCoupling'],  
                                            "ParticipatesInCoupling(to-link) = ? AND ParticipatesInCoupling(from-link) <> ?",  
                                            [$couplingID, $featureID], 'ParticipatesInCoupling(from-link)');  
         Trace("$couplingID target feature is $otherFeatureID.") if T(coupling => 4);  
         # Attach the other feature's score to its ID.  
         $retVal{$otherFeatureID} = $score;  
         $found = 1;  
1707      }      }
1708      # 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
1709      # the incoming feature as well.      # the incoming feature as well.
1710      if ($found) {      if (keys %retVal) {
1711          $retVal{$featureID} = 9999;          $retVal{$featureID} = 9999;
1712      }      }
1713      # Return the hash.      # Return the hash.
# Line 1706  Line 1716 
1716    
1717  =head3 CouplingEvidence  =head3 CouplingEvidence
1718    
1719  C<< my @evidence = $sprout->CouplingEvidence($peg1, $peg2); >>      my @evidence = $sprout->CouplingEvidence($peg1, $peg2);
1720    
1721  Return the evidence for a functional coupling.  Return the evidence for a functional coupling.
1722    
# Line 1754  Line 1764 
1764      my ($self, $peg1, $peg2) = @_;      my ($self, $peg1, $peg2) = @_;
1765      # Declare the return variable.      # Declare the return variable.
1766      my @retVal = ();      my @retVal = ();
1767      # Our first task is to find out the nature of the coupling: whether or not      # Get the coupling and evidence data.
1768      # it exists, its score, and whether the features are stored in the same      my @rawData = FIGRules::NetCouplingData('coupling_evidence', id1 => $peg1, id2 => $peg2);
1769      # order as the ones coming in.      # Loop through the raw data, saving the ones that are in NMPDR genomes.
1770      my ($couplingID, $inverted, $score) = $self->GetCoupling($peg1, $peg2);      for my $rawTuple (@rawData) {
1771      # Only proceed if a coupling exists.          if ($self->_CheckFeature($rawTuple->[0]) && $self->_CheckFeature($rawTuple->[1])) {
1772      if ($couplingID) {              push @retVal, $rawTuple;
         # 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);  
1773      }      }
     # Return the result.  
     return @retVal;  
 }  
   
 =head3 GetCoupling  
   
 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.  
   
 =over 4  
   
 =item peg1  
   
 ID of the feature of interest.  
   
 =item peg2  
   
 ID of the potentially coupled feature.  
   
 =item RETURN  
   
 Returns a three-element list. The first element contains the database ID of  
 the coupling. The second element is FALSE if the coupling is stored in the  
 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>.  
   
 =back  
   
 =cut  
 #: Return Type $%@;  
 sub GetCoupling {  
     # Get the parameters.  
     my ($self, $peg1, $peg2) = @_;  
     # Declare the return values. We'll start with the coupling ID and undefine the  
     # flag and score until we have more information.  
     my ($retVal, $inverted, $score) = ($self->CouplingID($peg1, $peg2), undef, undef);  
     # Find the coupling data.  
     my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'],  
                                  "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)",  
                                  [$retVal], ["ParticipatesInCoupling(from-link)", "Coupling(score)"]);  
     # Check to see if we found anything.  
     if (!@pegs) {  
         Trace("No coupling found.") if T(Coupling => 4);  
         # No coupling, so undefine the return value.  
         $retVal = undef;  
     } else {  
         # We have a coupling! Get the score and check for inversion.  
         $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);  
1774      }      }
1775      # Return the result.      # Return the result.
1776      return ($retVal, $inverted, $score);      return @retVal;
1777  }  }
1778    
1779  =head3 GetSynonymGroup  =head3 GetSynonymGroup
1780    
1781  C<< my $id = $sprout->GetSynonymGroup($fid); >>      my $id = $sprout->GetSynonymGroup($fid);
1782    
1783  Return the synonym group name for the specified feature.  Return the synonym group name for the specified feature.
1784    
# Line 1885  Line 1817 
1817    
1818  =head3 GetBoundaries  =head3 GetBoundaries
1819    
1820  C<< my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList); >>      my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList);
1821    
1822  Determine the begin and end boundaries for the locations in a list. All of the  Determine the begin and end boundaries for the locations in a list. All of the
1823  locations must belong to the same contig and have mostly the same direction in  locations must belong to the same contig and have mostly the same direction in
# Line 1947  Line 1879 
1879      return ($contig, $beg, $end);      return ($contig, $beg, $end);
1880  }  }
1881    
 =head3 CouplingID  
   
 C<< my $couplingID = $sprout->CouplingID($peg1, $peg2); >>  
   
 Return the coupling ID for a pair of feature IDs.  
   
 The coupling ID is currently computed by joining the feature IDs in  
 sorted order with a space. Client modules (that is, modules which  
 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")>.  
   
 =over 4  
   
 =item peg1  
   
 First feature of interest.  
   
 =item peg2  
   
 Second feature of interest.  
   
 =item RETURN  
   
 Returns the ID that would be used to represent a functional coupling of  
 the two specified PEGs.  
   
 =back  
   
 =cut  
 #: Return Type $;  
 sub CouplingID {  
     my ($self, @pegs) = @_;  
     return $self->DigestKey(join " ", sort @pegs);  
 }  
   
1882  =head3 ReadFasta  =head3 ReadFasta
1883    
1884  C<< my %sequenceData = Sprout::ReadFasta($fileName, $prefix); >>      my %sequenceData = Sprout::ReadFasta($fileName, $prefix);
1885    
1886  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
1887  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 2054  Line 1947 
1947    
1948  =head3 FormatLocations  =head3 FormatLocations
1949    
1950  C<< my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat); >>      my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat);
1951    
1952  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
1953  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 2119  Line 2012 
2012    
2013  =head3 DumpData  =head3 DumpData
2014    
2015  C<< $sprout->DumpData(); >>      $sprout->DumpData();
2016    
2017  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.
2018    
# Line 2136  Line 2029 
2029    
2030  =head3 XMLFileName  =head3 XMLFileName
2031    
2032  C<< my $fileName = $sprout->XMLFileName(); >>      my $fileName = $sprout->XMLFileName();
2033    
2034  Return the name of this database's XML definition file.  Return the name of this database's XML definition file.
2035    
# Line 2147  Line 2040 
2040      return $self->{_xmlName};      return $self->{_xmlName};
2041  }  }
2042    
2043    =head3 GetGenomeNameData
2044    
2045        my ($genus, $species, $strain) = $sprout->GenomeNameData($genomeID);
2046    
2047    Return the genus, species, and unique characterization for a genome. This
2048    is similar to L</GenusSpecies>, with the exception that it returns the
2049    values in three seperate fields.
2050    
2051    =over 4
2052    
2053    =item genomeID
2054    
2055    ID of the genome whose name data is desired.
2056    
2057    =item RETURN
2058    
2059    Returns a three-element list, consisting of the genus, species, and strain
2060    of the specified genome. If the genome is not found, an error occurs.
2061    
2062    =back
2063    
2064    =cut
2065    
2066    sub GetGenomeNameData {
2067        # Get the parameters.
2068        my ($self, $genomeID) = @_;
2069        # Get the desired values.
2070        my ($genus, $species, $strain) = $self->GetEntityValues('Genome', $genomeID =>
2071                                                                [qw(Genome(genus) Genome(species) Genome(unique-characterization))]);
2072        # Throw an error if they were not found.
2073        if (! defined $genus) {
2074            Confess("Genome $genomeID not found in database.");
2075        }
2076        # Return the results.
2077        return ($genus, $species, $strain);
2078    }
2079    
2080    =head3 GetGenomeByNameData
2081    
2082        my @genomes = $sprout->GetGenomeByNameData($genus, $species, $strain);
2083    
2084    Return a list of the IDs of the genomes with the specified genus,
2085    species, and strain. In almost every case, there will be either zero or
2086    one IDs returned; however, two or more IDs could be returned if there are
2087    multiple versions of the genome in the database.
2088    
2089    =over 4
2090    
2091    =item genus
2092    
2093    Genus of the desired genome.
2094    
2095    =item species
2096    
2097    Species of the desired genome.
2098    
2099    =item strain
2100    
2101    Strain (unique characterization) of the desired genome. This may be an empty
2102    string, in which case it is presumed that the desired genome has no strain
2103    specified.
2104    
2105    =item RETURN
2106    
2107    Returns a list of the IDs of the genomes having the specified genus, species, and
2108    strain.
2109    
2110    =back
2111    
2112    =cut
2113    
2114    sub GetGenomeByNameData {
2115        # Get the parameters.
2116        my ($self, $genus, $species, $strain) = @_;
2117        # Try to find the genomes.
2118        my @retVal = $self->GetFlat(['Genome'], "Genome(genus) = ? AND Genome(species) = ? AND Genome(unique-characterization) = ?",
2119                                    [$genus, $species, $strain], 'Genome(id)');
2120        # Return the result.
2121        return @retVal;
2122    }
2123    
2124  =head3 Insert  =head3 Insert
2125    
2126  C<< $sprout->Insert($objectType, \%fieldHash); >>      $sprout->Insert($objectType, \%fieldHash);
2127    
2128  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
2129  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 2158  Line 2132 
2132  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
2133  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>.
2134    
2135  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']});
2136    
2137  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
2138  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>.
2139    
2140  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'});
2141    
2142  =over 4  =over 4
2143    
# Line 2188  Line 2162 
2162    
2163  =head3 Annotate  =head3 Annotate
2164    
2165  C<< my $ok = $sprout->Annotate($fid, $timestamp, $user, $text); >>      my $ok = $sprout->Annotate($fid, $timestamp, $user, $text);
2166    
2167  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
2168  specified feature and user.  specified feature and user.
# Line 2242  Line 2216 
2216    
2217  =head3 AssignFunction  =head3 AssignFunction
2218    
2219  C<< my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser); >>      my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser);
2220    
2221  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
2222  format is described in L</ParseAssignment>.  format is described in L</ParseAssignment>.
# Line 2302  Line 2276 
2276    
2277  =head3 FeaturesByAlias  =head3 FeaturesByAlias
2278    
2279  C<< my @features = $sprout->FeaturesByAlias($alias); >>      my @features = $sprout->FeaturesByAlias($alias);
2280    
2281  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
2282  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 2336  Line 2310 
2310          push @retVal, $mappedAlias;          push @retVal, $mappedAlias;
2311      } else {      } else {
2312          # 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.
2313          @retVal = $self->GetFlat(['Feature'], 'Feature(alias) = ?', [$mappedAlias], 'Feature(id)');          @retVal = $self->GetFlat(['IsAliasOf'], 'IsAliasOf(from-link) = ?', [$mappedAlias], 'IsAliasOf(to-link)');
2314      }      }
2315      # Return the result.      # Return the result.
2316      return @retVal;      return @retVal;
# Line 2344  Line 2318 
2318    
2319  =head3 FeatureTranslation  =head3 FeatureTranslation
2320    
2321  C<< my $translation = $sprout->FeatureTranslation($featureID); >>      my $translation = $sprout->FeatureTranslation($featureID);
2322    
2323  Return the translation of a feature.  Return the translation of a feature.
2324    
# Line 2372  Line 2346 
2346    
2347  =head3 Taxonomy  =head3 Taxonomy
2348    
2349  C<< my @taxonomyList = $sprout->Taxonomy($genome); >>      my @taxonomyList = $sprout->Taxonomy($genome);
2350    
2351  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
2352  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>,
2353  or C<Eukaryote>) to sub-species. For example,  or C<Eukaryote>) to sub-species. For example,
2354    
2355  C<< (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12) >>      (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12)
2356    
2357  =over 4  =over 4
2358    
# Line 2413  Line 2387 
2387    
2388  =head3 CrudeDistance  =head3 CrudeDistance
2389    
2390  C<< my $distance = $sprout->CrudeDistance($genome1, $genome2); >>      my $distance = $sprout->CrudeDistance($genome1, $genome2);
2391    
2392  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
2393  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 2465  Line 2439 
2439    
2440  =head3 RoleName  =head3 RoleName
2441    
2442  C<< my $roleName = $sprout->RoleName($roleID); >>      my $roleName = $sprout->RoleName($roleID);
2443    
2444  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
2445  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 2499  Line 2473 
2473    
2474  =head3 RoleDiagrams  =head3 RoleDiagrams
2475    
2476  C<< my @diagrams = $sprout->RoleDiagrams($roleID); >>      my @diagrams = $sprout->RoleDiagrams($roleID);
2477    
2478  Return a list of the diagrams containing a specified functional role.  Return a list of the diagrams containing a specified functional role.
2479    
# Line 2529  Line 2503 
2503    
2504  =head3 GetProperties  =head3 GetProperties
2505    
2506  C<< my @list = $sprout->GetProperties($fid, $key, $value, $url); >>      my @list = $sprout->GetProperties($fid, $key, $value, $url);
2507    
2508  Return a list of the properties with the specified characteristics.  Return a list of the properties with the specified characteristics.
2509    
2510  Properties are arbitrary key-value pairs associated with a feature. (At some point they  Properties are the Sprout analog of the FIG attributes. The call is
2511  will also be associated with genomes.) A property value is represented by a 4-tuple of  passed directly to the CustomAttributes or RemoteCustomAttributes object
2512  the form B<($fid, $key, $value, $url)>. These exactly correspond to the parameter  contained in this object.
2513    
2514  =over 4  This method returns a series of tuples that match the specified criteria. Each tuple
2515    will contain an object ID, a key, and one or more values. The parameters to this
2516    method therefore correspond structurally to the values expected in each tuple. In
2517    addition, you can ask for a generic search by suffixing a percent sign (C<%>) to any
2518    of the parameters. So, for example,
2519    
2520  =item fid      my @attributeList = $sprout->GetProperties('fig|100226.1.peg.1004', 'structure%', 1, 2);
2521    
2522  ID of the feature possessing the property.  would return something like
2523    
2524  =item key      ['fig}100226.1.peg.1004', 'structure', 1, 2]
2525        ['fig}100226.1.peg.1004', 'structure1', 1, 2]
2526        ['fig}100226.1.peg.1004', 'structure2', 1, 2]
2527        ['fig}100226.1.peg.1004', 'structureA', 1, 2]
2528    
2529  Name or key of the property.  Use of C<undef> in any position acts as a wild card (all values). You can also specify
2530    a list reference in the ID column. Thus,
2531    
2532  =item value      my @attributeList = $sprout->GetProperties(['100226.1', 'fig|100226.1.%'], 'PUBMED');
2533    
2534  Value of the property.  would get the PUBMED attribute data for Streptomyces coelicolor A3(2) and all its
2535    features.
2536    
2537  =item url  In addition to values in multiple sections, a single attribute key can have multiple
2538    values, so even
2539    
2540  URL of the document that indicated the property should have this particular value, or an      my @attributeList = $sprout->GetProperties($peg, 'virulent');
 empty string if no such document exists.  
2541    
2542  =back  which has no wildcard in the key or the object ID, may return multiple tuples.
2543    
2544  The parameters act as a filter for the desired data. Any non-null parameter will  =over 4
 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.  
2545    
2546  A single property key can have many values, representing different ideas about the  =item objectID
 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  
2547    
2548      my @list = $sprout->GetProperties('fig|83333.1.peg.10', 'virulence', '', '');  ID of object whose attributes are desired. If the attributes are desired for multiple
2549    objects, this parameter can be specified as a list reference. If the attributes are
2550    desired for all objects, specify C<undef> or an empty string. Finally, you can specify
2551    attributes for a range of object IDs by putting a percent sign (C<%>) at the end.
2552    
2553  Here the I<$value> and I<$url> fields are left blank, indicating that those fields are  =item key
 not to be filtered. The tuples returned would be  
2554    
2555      ('fig|83333.1.peg.10', 'virulence', 'yes', 'http://www.somewhere.edu/first.paper.pdf')  Attribute key name. A value of C<undef> or an empty string will match all
2556      ('fig|83333.1.peg.10', 'virulence', 'no', 'http://www.somewhere.edu/second.paper.pdf')  attribute keys. If the values are desired for multiple keys, this parameter can be
2557    specified as a list reference. Finally, you can specify attributes for a range of
2558    keys by putting a percent sign (C<%>) at the end.
2559    
2560    =item values
2561    
2562    List of the desired attribute values, section by section. If C<undef>
2563    or an empty string is specified, all values in that section will match. A
2564    generic match can be requested by placing a percent sign (C<%>) at the end.
2565    In that case, all values that match up to and not including the percent sign
2566    will match. You may also specify a regular expression enclosed
2567    in slashes. All values that match the regular expression will be returned. For
2568    performance reasons, only values have this extra capability.
2569    
2570    =item RETURN
2571    
2572    Returns a list of tuples. The first element in the tuple is an object ID, the
2573    second is an attribute key, and the remaining elements are the sections of
2574    the attribute value. All of the tuples will match the criteria set forth in
2575    the parameter list.
2576    
2577    =back
2578    
2579  =cut  =cut
2580  #: Return Type @@;  
2581  sub GetProperties {  sub GetProperties {
2582      # Get the parameters.      # Get the parameters.
2583      my ($self, @parms) = @_;      my ($self, @parms) = @_;
2584      # Declare the return variable.      # Declare the return variable.
2585      my @retVal = ();      my @retVal = $self->{_ca}->GetAttributes(@parms);
     # 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;  
     }  
2586      # Return the result.      # Return the result.
2587      return @retVal;      return @retVal;
2588  }  }
2589    
2590  =head3 FeatureProperties  =head3 FeatureProperties
2591    
2592  C<< my @properties = $sprout->FeatureProperties($featureID); >>      my @properties = $sprout->FeatureProperties($featureID);
2593    
2594  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
2595  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
2596  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
2597  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
2598  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.  
2599    
2600  =over 4  =over 4
2601    
# Line 2630  Line 2605 
2605    
2606  =item RETURN  =item RETURN
2607    
2608  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.  
2609    
2610  =back  =back
2611    
# Line 2641  Line 2615 
2615      # Get the parameters.      # Get the parameters.
2616      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
2617      # Get the properties.      # Get the properties.
2618      my @retVal = $self->GetAll(['HasProperty', 'Property'], "HasProperty(from-link) = ?", [$featureID],      my @attributes = $self->{_ca}->GetAttributes($featureID);
2619                              ['Property(property-name)', 'Property(property-value)',      # Strip the feature ID off each tuple.
2620                               'HasProperty(evidence)']);      my @retVal = ();
2621        for my $attributeRow (@attributes) {
2622            shift @{$attributeRow};
2623            push @retVal, $attributeRow;
2624        }
2625      # Return the resulting list.      # Return the resulting list.
2626      return @retVal;      return @retVal;
2627  }  }
2628    
2629  =head3 DiagramName  =head3 DiagramName
2630    
2631  C<< my $diagramName = $sprout->DiagramName($diagramID); >>      my $diagramName = $sprout->DiagramName($diagramID);
2632    
2633  Return the descriptive name of a diagram.  Return the descriptive name of a diagram.
2634    
# Line 2678  Line 2656 
2656    
2657  =head3 PropertyID  =head3 PropertyID
2658    
2659  C<< my $id = $sprout->PropertyID($propName, $propValue); >>      my $id = $sprout->PropertyID($propName, $propValue);
2660    
2661  Return the ID of the specified property name and value pair, if the  Return the ID of the specified property name and value pair, if the
2662  pair exists.  pair exists. Only a small subset of the FIG attributes are stored as
2663    Sprout properties, mostly for use in search optimization.
2664    
2665  =over 4  =over 4
2666    
# Line 2714  Line 2693 
2693    
2694  =head3 MergedAnnotations  =head3 MergedAnnotations
2695    
2696  C<< my @annotationList = $sprout->MergedAnnotations(\@list); >>      my @annotationList = $sprout->MergedAnnotations(\@list);
2697    
2698  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
2699  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 2763  Line 2742 
2742    
2743  =head3 RoleNeighbors  =head3 RoleNeighbors
2744    
2745  C<< my @roleList = $sprout->RoleNeighbors($roleID); >>      my @roleList = $sprout->RoleNeighbors($roleID);
2746    
2747  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
2748  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 2806  Line 2785 
2785    
2786  =head3 FeatureLinks  =head3 FeatureLinks
2787    
2788  C<< my @links = $sprout->FeatureLinks($featureID); >>      my @links = $sprout->FeatureLinks($featureID);
2789    
2790  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
2791  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 2837  Line 2816 
2816    
2817  =head3 SubsystemsOf  =head3 SubsystemsOf
2818    
2819  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>      my %subsystems = $sprout->SubsystemsOf($featureID);
2820    
2821  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
2822  to the roles the feature performs.  to the roles the feature performs.
# Line 2885  Line 2864 
2864    
2865  =head3 SubsystemList  =head3 SubsystemList
2866    
2867  C<< my @subsystems = $sprout->SubsystemList($featureID); >>      my @subsystems = $sprout->SubsystemList($featureID);
2868    
2869  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
2870  feature participates. Unlike L</SubsystemsOf>, this method only returns the  feature participates. Unlike L</SubsystemsOf>, this method only returns the
# Line 2917  Line 2896 
2896    
2897  =head3 GenomeSubsystemData  =head3 GenomeSubsystemData
2898    
2899  C<< my %featureData = $sprout->GenomeSubsystemData($genomeID); >>      my %featureData = $sprout->GenomeSubsystemData($genomeID);
2900    
2901  Return a hash mapping genome features to their subsystem roles.  Return a hash mapping genome features to their subsystem roles.
2902    
# Line 2977  Line 2956 
2956    
2957  =head3 RelatedFeatures  =head3 RelatedFeatures
2958    
2959  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>      my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID);
2960    
2961  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
2962  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 3028  Line 3007 
3007    
3008  =head3 TaxonomySort  =head3 TaxonomySort
3009    
3010  C<< my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs); >>      my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs);
3011    
3012  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
3013  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 3063  Line 3042 
3042          my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",          my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",
3043                                          [$fid], 'Genome(taxonomy)');                                          [$fid], 'Genome(taxonomy)');
3044          # Add this feature to the hash buffer.          # Add this feature to the hash buffer.
3045          Tracer::AddToListMap(\%hashBuffer, $taxonomy, $fid);          push @{$hashBuffer{$taxonomy}}, $fid;
3046      }      }
3047      # Sort the keys and get the elements.      # Sort the keys and get the elements.
3048      my @retVal = ();      my @retVal = ();
# Line 3076  Line 3055 
3055    
3056  =head3 Protein  =head3 Protein
3057    
3058  C<< my $protein = Sprout::Protein($sequence, $table); >>      my $protein = Sprout::Protein($sequence, $table);
3059    
3060  Translate a DNA sequence into a protein sequence.  Translate a DNA sequence into a protein sequence.
3061    
# Line 3162  Line 3141 
3141    
3142  =head3 LoadInfo  =head3 LoadInfo
3143    
3144  C<< my ($dirName, @relNames) = $sprout->LoadInfo(); >>      my ($dirName, @relNames) = $sprout->LoadInfo();
3145    
3146  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
3147  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 3181  Line 3160 
3160      return @retVal;      return @retVal;
3161  }  }
3162    
3163    =head3 BBHMatrix
3164    
3165        my %bbhMap = $sprout->BBHMatrix($genomeID, $cutoff, @targets);
3166    
3167    Find all the bidirectional best hits for the features of a genome in a
3168    specified list of target genomes. The return value will be a hash mapping
3169    features in the original genome to their bidirectional best hits in the
3170    target genomes.
3171    
3172    =over 4
3173    
3174    =item genomeID
3175    
3176    ID of the genome whose features are to be examined for bidirectional best hits.
3177    
3178    =item cutoff
3179    
3180    A cutoff value. Only hits with a score lower than the cutoff will be returned.
3181    
3182    =item targets
3183    
3184    List of target genomes. Only pairs originating in the original
3185    genome and landing in one of the target genomes will be returned.
3186    
3187    =item RETURN
3188    
3189    Returns a hash mapping each feature in the original genome to a hash mapping its
3190    BBH pegs in the target genomes to their scores.
3191    
3192    =back
3193    
3194    =cut
3195    
3196    sub BBHMatrix {
3197        # Get the parameters.
3198        my ($self, $genomeID, $cutoff, @targets) = @_;
3199        # Declare the return variable.
3200        my %retVal = ();
3201        # Ask for the BBHs.
3202        my @bbhList = FIGRules::BatchBBHs("fig|$genomeID.%", $cutoff, @targets);
3203        # We now have a set of 4-tuples that we need to convert into a hash of hashes.
3204        for my $bbhData (@bbhList) {
3205            my ($peg1, $peg2, $score) = @{$bbhData};
3206            if (! exists $retVal{$peg1}) {
3207                $retVal{$peg1} = { $peg2 => $score };
3208            } else {
3209                $retVal{$peg1}->{$peg2} = $score;
3210            }
3211        }
3212        # Return the result.
3213        return %retVal;
3214    }
3215    
3216    
3217    =head3 SimMatrix
3218    
3219        my %simMap = $sprout->SimMatrix($genomeID, $cutoff, @targets);
3220    
3221    Find all the similarities for the features of a genome in a
3222    specified list of target genomes. The return value will be a hash mapping
3223    features in the original genome to their similarites in the
3224    target genomes.
3225    
3226    =over 4
3227    
3228    =item genomeID
3229    
3230    ID of the genome whose features are to be examined for similarities.
3231    
3232    =item cutoff
3233    
3234    A cutoff value. Only hits with a score lower than the cutoff will be returned.
3235    
3236    =item targets
3237    
3238    List of target genomes. Only pairs originating in the original
3239    genome and landing in one of the target genomes will be returned.
3240    
3241    =item RETURN
3242    
3243    Returns a hash mapping each feature in the original genome to a hash mapping its
3244    similar pegs in the target genomes to their scores.
3245    
3246    =back
3247    
3248    =cut
3249    
3250    sub SimMatrix {
3251        # Get the parameters.
3252        my ($self, $genomeID, $cutoff, @targets) = @_;
3253        # Declare the return variable.
3254        my %retVal = ();
3255        # Get the list of features in the source organism.
3256        my @fids = $self->FeaturesOf($genomeID);
3257        # Ask for the sims. We only want similarities to fig features.
3258        my $simList = FIGRules::GetNetworkSims($self, \@fids, {}, 1000, $cutoff, "fig");
3259        if (! defined $simList) {
3260            Confess("Unable to retrieve similarities from server.");
3261        } else {
3262            Trace("Processing sims.") if T(3);
3263            # We now have a set of sims that we need to convert into a hash of hashes. First, we
3264            # Create a hash for the target genomes.
3265            my %targetHash = map { $_ => 1 } @targets;
3266            for my $simData (@{$simList}) {
3267                # Get the PEGs and the score.
3268                my ($peg1, $peg2, $score) = ($simData->id1, $simData->id2, $simData->psc);
3269                # Insure the second ID is in the target list.
3270                my ($genome2) = FIGRules::ParseFeatureID($peg2);
3271                if (exists $targetHash{$genome2}) {
3272                    # Here it is. Now we need to add it to the return hash. How we do that depends
3273                    # on whether or not $peg1 is new to us.
3274                    if (! exists $retVal{$peg1}) {
3275                        $retVal{$peg1} = { $peg2 => $score };
3276                    } else {
3277                        $retVal{$peg1}->{$peg2} = $score;
3278                    }
3279                }
3280            }
3281        }
3282        # Return the result.
3283        return %retVal;
3284    }
3285    
3286    
3287  =head3 LowBBHs  =head3 LowBBHs
3288    
3289  C<< my %bbhMap = $sprout->LowBBHs($featureID, $cutoff); >>      my %bbhMap = $sprout->LowBBHs($featureID, $cutoff);
3290    
3291  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
3292  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 3227  Line 3330 
3330    
3331  =head3 Sims  =head3 Sims
3332    
3333  C<< my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters); >>      my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters);
3334    
3335  Get a list of similarities for a specified feature. Similarity information is not kept in the  Get a list of similarities for a specified feature. Similarity information is not kept in the
3336  Sprout database; rather, they are retrieved from a network server. The similarities are  Sprout database; rather, they are retrieved from a network server. The similarities are
# Line 3237  Line 3340 
3340  Similarities can be either raw or expanded. The raw similarities are basic  Similarities can be either raw or expanded. The raw similarities are basic
3341  hits between features with similar DNA. Expanding a raw similarity drags in any  hits between features with similar DNA. Expanding a raw similarity drags in any
3342  features considered substantially identical. So, for example, if features B<A1>,  features considered substantially identical. So, for example, if features B<A1>,
3343  B<A2>, and B<A3> are all substatially identical to B<A>, then a raw similarity  B<A2>, and B<A3> are all substantially identical to B<A>, then a raw similarity
3344  B<[C,A]> would be expanded to B<[C,A] [C,A1] [C,A2] [C,A3]>.  B<[C,A]> would be expanded to B<[C,A] [C,A1] [C,A2] [C,A3]>.
3345    
3346  =over 4  =over 4
3347    
3348  =item fid  =item fid
3349    
3350  ID of the feature whose similarities are desired.  ID of the feature whose similarities are desired, or reference to a list of IDs
3351    of features whose similarities are desired.
3352    
3353  =item maxN  =item maxN
3354    
# Line 3292  Line 3396 
3396    
3397  =head3 IsAllGenomes  =head3 IsAllGenomes
3398    
3399  C<< my $flag = $sprout->IsAllGenomes(\@list, \@checkList); >>      my $flag = $sprout->IsAllGenomes(\@list, \@checkList);
3400    
3401  Return TRUE if all genomes in the second list are represented in the first list at  Return TRUE if all genomes in the second list are represented in the first list at
3402  least one. Otherwise, return FALSE. If the second list is omitted, the first list is  least one. Otherwise, return FALSE. If the second list is omitted, the first list is
# Line 3341  Line 3445 
3445    
3446  =head3 GetGroups  =head3 GetGroups
3447    
3448  C<< my %groups = $sprout->GetGroups(\@groupList); >>      my %groups = $sprout->GetGroups(\@groupList);
3449    
3450  Return a hash mapping each group to the IDs of the genomes in the group.  Return a hash mapping each group to the IDs of the genomes in the group.
3451  A list of groups may be specified, in which case only those groups will be  A list of groups may be specified, in which case only those groups will be
# Line 3373  Line 3477 
3477                                      [$FIG_Config::otherGroup], ['Genome(id)', 'Genome(primary-group)']);                                      [$FIG_Config::otherGroup], ['Genome(id)', 'Genome(primary-group)']);
3478          # Loop through the genomes found.          # Loop through the genomes found.
3479          for my $genome (@genomes) {          for my $genome (@genomes) {
3480              # Pop this genome's ID off the current list.              # Get the genome ID and group, and add this genome to the group's list.
3481              my @groups = @{$genome};              my ($genomeID, $group) = @{$genome};
3482              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);  
             }  
3483          }          }
3484      }      }
3485      # Return the hash we just built.      # Return the hash we just built.
# Line 3389  Line 3488 
3488    
3489  =head3 MyGenomes  =head3 MyGenomes
3490    
3491  C<< my @genomes = Sprout::MyGenomes($dataDir); >>      my @genomes = Sprout::MyGenomes($dataDir);
3492    
3493  Return a list of the genomes to be included in the Sprout.  Return a list of the genomes to be included in the Sprout.
3494    
# Line 3421  Line 3520 
3520    
3521  =head3 LoadFileName  =head3 LoadFileName
3522    
3523  C<< my $fileName = Sprout::LoadFileName($dataDir, $tableName); >>      my $fileName = Sprout::LoadFileName($dataDir, $tableName);
3524    
3525  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
3526  directory.  directory.
# Line 3462  Line 3561 
3561    
3562  =head3 DeleteGenome  =head3 DeleteGenome
3563    
3564  C<< my $stats = $sprout->DeleteGenome($genomeID, $testFlag); >>      my $stats = $sprout->DeleteGenome($genomeID, $testFlag);
3565    
3566  Delete a genome from the database.  Delete a genome from the database.
3567    
# Line 3488  Line 3587 
3587      # Get the parameters.      # Get the parameters.
3588      my ($self, $genomeID, $testFlag) = @_;      my ($self, $genomeID, $testFlag) = @_;
3589      # Perform the delete for the genome's features.      # Perform the delete for the genome's features.
3590      my $retVal = $self->Delete('Feature', "fig|$genomeID.%", $testFlag);      my $retVal = $self->Delete('Feature', "fig|$genomeID.%", testMode => $testFlag);
3591      # Perform the delete for the primary genome data.      # Perform the delete for the primary genome data.
3592      my $stats = $self->Delete('Genome', $genomeID, $testFlag);      my $stats = $self->Delete('Genome', $genomeID, testMode => $testFlag);
3593      $retVal->Accumulate($stats);      $retVal->Accumulate($stats);
3594      # Return the result.      # Return the result.
3595      return $retVal;      return $retVal;
# Line 3498  Line 3597 
3597    
3598  =head3 Fix  =head3 Fix
3599    
3600  C<< my %fixedHash = Sprout::Fix(%groupHash); >>      my %fixedHash = $sprout->Fix(%groupHash);
3601    
3602  Prepare a genome group hash (like that returned by L</GetGroups> for processing.  Prepare a genome group hash (like that returned by L</GetGroups>) for processing.
3603  Groups with the same primary name will be combined. The primary name is the  The groups will be combined into the appropriate super-groups.
 first capitalized word in the group name.  
3604    
3605  =over 4  =over 4
3606    
# Line 3520  Line 3618 
3618    
3619  sub Fix {  sub Fix {
3620      # Get the parameters.      # Get the parameters.
3621      my (%groupHash) = @_;      my ($self, %groupHash) = @_;
3622      # Create the result hash.      # Create the result hash.
3623      my %retVal = ();      my %retVal = ();
3624        # Get the super-group table.
3625        my %superTable = $self->CheckGroupFile();
3626      # Copy over the genomes.      # Copy over the genomes.
3627      for my $groupID (keys %groupHash) {      for my $groupID (keys %groupHash) {
3628          # Make a safety copy of the group ID.          # Get the super-group name.
3629          my $realGroupID = $groupID;          my $realGroupID;
         # Yank the primary name.  
3630          if ($groupID =~ /([A-Z]\w+)/) {          if ($groupID =~ /([A-Z]\w+)/) {
3631                if (! defined($superTable{$1})) {
3632                    Confess("Super-group name not found for group $groupID.");
3633                } else {
3634              $realGroupID = $1;              $realGroupID = $1;
3635          }          }
3636            } else {
3637                Confess("Invalid group name $groupID.");
3638            }
3639          # Append this group's genomes into the result hash.          # Append this group's genomes into the result hash.
3640          Tracer::AddToListMap(\%retVal, $realGroupID, @{$groupHash{$groupID}});          push @{$retVal{$realGroupID}}, @{$groupHash{$groupID}};
3641      }      }
3642      # Return the result hash.      # Return the result hash.
3643      return %retVal;      return %retVal;
# Line 3540  Line 3645 
3645    
3646  =head3 GroupPageName  =head3 GroupPageName
3647    
3648  C<< my $name = $sprout->GroupPageName($group); >>      my $name = $sprout->GroupPageName($group);
3649    
3650  Return the name of the page for the specified NMPDR group.  Return the name of the page for the specified NMPDR group.
3651    
# Line 3565  Line 3670 
3670      # Declare the return variable.      # Declare the return variable.
3671      my $retVal;      my $retVal;
3672      # Check for the group file data.      # Check for the group file data.
3673      if (! defined $self->{groupHash}) {      my %superTable = $self->CheckGroupFile();
         # Read the group file.  
         my %groupData = Sprout::ReadGroupFile($self->{_options}->{dataDir} . "/groups.tbl");  
         # Store it in our object.  
         $self->{groupHash} = \%groupData;  
     }  
3674      # Compute the real group name.      # Compute the real group name.
     my $realGroup = $group;  
3675      if ($group =~ /([A-Z]\w+)/) {      if ($group =~ /([A-Z]\w+)/) {
3676          $realGroup = $1;          my $realGroup = $1;
3677            if (! defined($superTable{$1})) {
3678                Confess("No super-group found for \"$group\".");
3679            } else {
3680                $retVal = "../content/$superTable{$1}->{page}";
3681            }
3682        } else {
3683            Confess("\"group\" is not a valid group name.");
3684      }      }
     # Return the page name.  
     $retVal = "../content/" . $self->{groupHash}->{$realGroup}->[1];  
3685      # Return the result.      # Return the result.
3686      return $retVal;      return $retVal;
3687  }  }
3688    
 =head3 ReadGroupFile  
   
 C<< my %groupData = Sprout::ReadGroupFile($groupFileName); >>  
   
 Read in the data from the specified group file. The group file contains information  
 about each of the NMPDR groups.  
   
 =over 4  
   
 =item name  
   
 Name of the group.  
   
 =item page  
   
 Name of the group's page on the web site (e.g. C<campy.php> for  
 Campylobacter)  
3689    
3690  =item genus  =head3 AddProperty
3691    
3692  Genus of the group      $sprout->AddProperty($featureID, $key, @values);
3693    
3694  =item species  Add a new attribute value (Property) to a feature.
3695    
3696  Species of the group, or an empty string if the group is for an entire  =over 4
 genus. If the group contains more than one species, the species names  
 should be separated by commas.  
3697    
3698  =back  =item peg
3699    
3700  The parameters to this method are as follows  ID of the feature to which the attribute is to be added.
3701    
3702  =over 4  =item key
3703    
3704  =item groupFile  Name of the attribute (key).
3705    
3706  Name of the file containing the group data.  =item values
3707    
3708  =item RETURN  Values of the attribute.
   
 Returns a hash keyed on group name. The value of each hash  
3709    
3710  =back  =back
3711    
3712  =cut  =cut
3713    #: Return Type ;
3714  sub ReadGroupFile {  sub AddProperty {
3715      # Get the parameters.      # Get the parameters.
3716      my ($groupFileName) = @_;      my ($self, $featureID, $key, @values) = @_;
3717      # Declare the return variable.      # Add the property using the attached attributes object.
3718      my %retVal;      $self->{_ca}->AddAttribute($featureID, $key, @values);
     # Read the group file.  
     my @groupLines = Tracer::GetFile($groupFileName);  
     for my $groupLine (@groupLines) {  
         my ($name, $page, $genus, $species) = split(/\t/, $groupLine);  
         $retVal{$name} = [$page, $genus, $species];  
3719      }      }
     # Return the result.  
     return %retVal;  
 }  
   
 =head3 AddProperty  
3720    
3721  C<< my  = $sprout->AddProperty($featureID, $key, $value, $url); >>  =head3 CheckGroupFile
3722    
3723  Add a new attribute value (Property) to a feature. In the SEED system, attributes can      my %groupData = $sprout->CheckGroupFile();
 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.  
3724    
3725  =over 4  Get the group file hash. The group file hash describes the relationship
3726    between a group and the super-group to which it belongs for purposes of
3727  =item peg  display. The super-group name is computed from the first capitalized word
3728    in the actual group name. For each super-group, the group file contains
3729    the page name and a list of the species expected to be in the group.
3730    Each species is specified by a genus and a species name. A species name
3731    of C<0> implies an entire genus.
3732    
3733  ID of the feature to which the attribute is to be replied.  This method returns a hash from super-group names to a hash reference. Each
3734    resulting hash reference contains the following fields.
3735    
3736  =item key  =over 4
   
 Name of the attribute (key).  
3737    
3738  =item value  =item page
3739    
3740  Value of the attribute.  The super-group's web page in the NMPDR.
3741    
3742  =item url  =item contents
3743    
3744  URL or text citation from which the property was obtained.  A list of 2-tuples, each containing a genus name followed by a species name
3745    (or 0, indicating all species). This list indicates which organisms belong
3746    in the super-group.
3747    
3748  =back  =back
3749    
3750  =cut  =cut
3751  #: Return Type ;  
3752  sub AddProperty {  sub CheckGroupFile{
3753      # Get the parameters.      # Get the parameters.
3754      my ($self, $featureID, $key, $value, $url) = @_;      my ($self) = @_;
3755      # Declare the variable to hold the desired property ID.      # Check to see if we already have this hash.
3756      my $propID;      if (! defined $self->{groupHash}) {
3757      # Attempt to find a property record for this key/value pair.          # We don't, so we need to read it in.
3758      my @properties = $self->GetFlat(['Property'],          my %groupHash;
3759                                     "Property(property-name) = ? AND Property(property-value) = ?",          # Read the group file.
3760                                     [$key, $value], 'Property(id)');          my @groupLines = Tracer::GetFile("$FIG_Config::sproutData/groups.tbl");
3761      if (@properties) {          # Loop through the list of sort-of groups.
3762          # Here the property is already in the database. We save its ID.          for my $groupLine (@groupLines) {
3763          $propID = $properties[0];              my ($name, $page, @contents) = split /\t/, $groupLine;
3764          # Here the property value does not exist. We need to generate an ID. It will be set              $groupHash{$name} = { page => $page,
3765          # to a number one greater than the maximum value in the database. This call to                                    contents => [ map { [ split /\s*,\s*/, $_ ] } @contents ]
3766          # GetAll will stop after one record.                                  };
         my @maxProperty = $self->GetAll(['Property'], "ORDER BY Property(id) DESC", [], ['Property(id)'],  
                                         1);  
         $propID = $maxProperty[0]->[0] + 1;  
         # Insert the new property value.  
         $self->Insert('Property', { 'property-name' => $key, 'property-value' => $value, id => $propID });  
3767      }      }
3768      # Now we connect the incoming feature to the property.          # Save the hash.
3769      $self->Insert('HasProperty', { 'from-link' => $featureID, 'to-link' => $propID, evidence => $url });          $self->{groupHash} = \%groupHash;
3770        }
3771        # Return the result.
3772        return %{$self->{groupHash}};
3773  }  }
3774    
3775  =head2 Virtual Methods  =head2 Virtual Methods
3776    
3777  =head3 CleanKeywords  =head3 CleanKeywords
3778    
3779  C<< my $cleanedString = $sprout->CleanKeywords($searchExpression); >>      my $cleanedString = $sprout->CleanKeywords($searchExpression);
3780    
3781  Clean up a search expression or keyword list. This involves converting the periods  Clean up a search expression or keyword list. This involves converting the periods
3782  in EC numbers to underscores, converting non-leading minus signs to underscores,  in EC numbers to underscores, converting non-leading minus signs to underscores,
# Line 3754  Line 3826 
3826    
3827  A functional assignment is always of the form  A functional assignment is always of the form
3828    
3829      C<set >I<YYYY>C< function to\n>I<ZZZZZ>      set YYYY function to
3830        ZZZZ
3831    
3832  where I<YYYY> is the B<user>, and I<ZZZZ> is the actual functional role. In most cases,  where I<YYYY> is the B<user>, and I<ZZZZ> is the actual functional role. In most cases,
3833  the user and the assigning user (from MadeAnnotation) will be the same, but that is  the user and the assigning user (from MadeAnnotation) will be the same, but that is
# Line 3807  Line 3880 
3880      return @retVal;      return @retVal;
3881  }  }
3882    
3883    =head3 _CheckFeature
3884    
3885        my $flag = $sprout->_CheckFeature($fid);
3886    
3887    Return TRUE if the specified FID is probably an NMPDR feature ID, else FALSE.
3888    
3889    =over 4
3890    
3891    =item fid
3892    
3893    Feature ID to check.
3894    
3895    =item RETURN
3896    
3897    Returns TRUE if the FID is for one of the NMPDR genomes, else FALSE.
3898    
3899    =back
3900    
3901    =cut
3902    
3903    sub _CheckFeature {
3904        # Get the parameters.
3905        my ($self, $fid) = @_;
3906        # Insure we have a genome hash.
3907        if (! defined $self->{genomeHash}) {
3908            my %genomeHash = map { $_ => 1 } $self->GetFlat(['Genome'], "", [], 'Genome(id)');
3909            $self->{genomeHash} = \%genomeHash;
3910        }
3911        # Get the feature's genome ID.
3912        my ($genomeID) = FIGRules::ParseFeatureID($fid);
3913        # Return an indicator of whether or not the genome ID is in the hash.
3914        return ($self->{genomeHash}->{$genomeID} ? 1 : 0);
3915    }
3916    
3917  =head3 FriendlyTimestamp  =head3 FriendlyTimestamp
3918    
3919  Convert a time number to a user-friendly time stamp for display.  Convert a time number to a user-friendly time stamp for display.

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3