[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.61, Wed Jun 14 20:15:48 2006 UTC revision 1.111, Wed May 7 23:11:51 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;
12      use Stats;      use Stats;
13      use POSIX qw(strftime);      use POSIX qw(strftime);
14        use BasicLocation;
15        use CustomAttributes;
16        use RemoteCustomAttributes;
17        use CGI;
18        use WikiTools;
19        use base qw(ERDB);
20    
21  =head1 Sprout Database Manipulation Object  =head1 Sprout Database Manipulation Object
22    
# Line 27  Line 29 
29  on the constructor. For example, the following invocation specifies a PostgreSQL database named I<GenDB>  on the constructor. For example, the following invocation specifies a PostgreSQL database named I<GenDB>
30  whose definition and data files are in a co-directory named F<Data>.  whose definition and data files are in a co-directory named F<Data>.
31    
32  C<< my $sprout = Sprout->new('GenDB', { dbType => 'pg', dataDir => '../Data', xmlFileName => '../Data/SproutDBD.xml' }); >>      my $sprout = Sprout->new('GenDB', { dbType => 'pg', dataDir => '../Data', xmlFileName => '../Data/SproutDBD.xml' });
33    
34  Once you have a sprout object, you may use it to re-create the database, load the tables from  Once you have a sprout object, you may use it to re-create the database, load the tables from
35  tab-delimited flat files and perform queries. Several special methods are provided for common  tab-delimited flat files and perform queries. Several special methods are provided for common
36  query tasks. For example, L</genomes> lists the IDs of all the genomes in the database and  query tasks. For example, L</Genomes> lists the IDs of all the genomes in the database and
37  L</dna_seq> returns the DNA sequence for a specified genome location.  L</DNASeq> returns the DNA sequence for a specified genome location.
38    
39  The Sprout object is a subclass of the ERDB object and inherits all its properties and methods.  The Sprout object is a subclass of the ERDB object and inherits all its properties and methods.
40    
# Line 44  Line 46 
46    
47  =head3 new  =head3 new
48    
49  C<< my $sprout = Sprout->new($dbName, \%options); >>      my $sprout = Sprout->new($dbName, \%options);
50    
51  This is the constructor for a sprout object. It connects to the database and loads the  This is the constructor for a sprout object. It connects to the database and loads the
52  database definition into memory. The positional first parameter specifies the name of the  database definition into memory. The positional first parameter specifies the name of the
# Line 78  Line 80 
80    
81  * B<noDBOpen> suppresses the connection to the database if TRUE, else FALSE  * B<noDBOpen> suppresses the connection to the database if TRUE, else FALSE
82    
83    * B<host> name of the database host
84    
85  =back  =back
86    
87  For example, the following constructor call specifies a database named I<Sprout> and a user name of  For example, the following constructor call specifies a database named I<Sprout> and a user name of
88  I<fig> with a password of I<admin>. The database load files are in the directory  I<fig> with a password of I<admin>. The database load files are in the directory
89  F</usr/fig/SproutData>.  F</usr/fig/SproutData>.
90    
91  C<< my $sprout = Sprout->new('Sprout', { userData =>; 'fig/admin', dataDir => '/usr/fig/SproutData' }); >>      my $sprout = Sprout->new('Sprout', { userData => 'fig/admin', dataDir => '/usr/fig/SproutData' });
92    
93  =cut  =cut
94    
95  sub new {  sub new {
96      # Get the parameters.      # Get the parameters.
97      my ($class, $dbName, $options) = @_;      my ($class, $dbName, $options) = @_;
98        # Compute the DBD directory.
99        my $dbd_dir = (defined($FIG_Config::dbd_dir) ? $FIG_Config::dbd_dir :
100                                                      $FIG_Config::fig );
101      # Compute the options. We do this by starting with a table of defaults and overwriting with      # Compute the options. We do this by starting with a table of defaults and overwriting with
102      # the incoming data.      # the incoming data.
103      my $optionTable = Tracer::GetOptions({      my $optionTable = Tracer::GetOptions({
# Line 98  Line 105 
105                                                          # database type                                                          # database type
106                         dataDir      => $FIG_Config::sproutData,                         dataDir      => $FIG_Config::sproutData,
107                                                          # data file directory                                                          # data file directory
108                         xmlFileName  => "$FIG_Config::fig/SproutDBD.xml",                         xmlFileName  => "$dbd_dir/SproutDBD.xml",
109                                                          # database definition file name                                                          # database definition file name
110                         userData     => "$FIG_Config::dbuser/$FIG_Config::dbpass",                         userData     => "$FIG_Config::dbuser/$FIG_Config::dbpass",
111                                                          # user name and password                                                          # user name and password
112                         port         => $FIG_Config::dbport,                         port         => $FIG_Config::dbport,
113                                                          # database connection port                                                          # database connection port
114                         sock         => $FIG_Config::dbsock,                         sock         => $FIG_Config::dbsock,
115                           host         => $FIG_Config::sprout_host,
116                         maxSegmentLength => 4500,        # maximum feature segment length                         maxSegmentLength => 4500,        # maximum feature segment length
117                         maxSequenceLength => 8000,       # maximum contig sequence length                         maxSequenceLength => 8000,       # maximum contig sequence length
118                         noDBOpen     => 0,               # 1 to suppress the database open                         noDBOpen     => 0,               # 1 to suppress the database open
# Line 117  Line 125 
125      # Connect to the database.      # Connect to the database.
126      my $dbh;      my $dbh;
127      if (! $optionTable->{noDBOpen}) {      if (! $optionTable->{noDBOpen}) {
128            Trace("Connect data: host = $optionTable->{host}, port = $optionTable->{port}.") if T(3);
129          $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,          $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,
130                                  $password, $optionTable->{port}, undef, $optionTable->{sock});                                  $password, $optionTable->{port}, $optionTable->{host}, $optionTable->{sock});
131      }      }
132      # Create the ERDB object.      # Create the ERDB object.
133      my $xmlFileName = "$optionTable->{xmlFileName}";      my $xmlFileName = "$optionTable->{xmlFileName}";
# Line 126  Line 135 
135      # Add the option table and XML file name.      # Add the option table and XML file name.
136      $retVal->{_options} = $optionTable;      $retVal->{_options} = $optionTable;
137      $retVal->{_xmlName} = $xmlFileName;      $retVal->{_xmlName} = $xmlFileName;
138        # Set up space for the group file data.
139        $retVal->{groupHash} = undef;
140        # Set up space for the genome hash. We use this to identify NMPDR genomes.
141        $retVal->{genomeHash} = undef;
142        # Connect to the attributes.
143        if ($FIG_Config::attrURL) {
144            Trace("Remote attribute server $FIG_Config::attrURL chosen.") if T(3);
145            $retVal->{_ca} = RemoteCustomAttributes->new($FIG_Config::attrURL);
146        } elsif ($FIG_Config::attrDbName) {
147            Trace("Local attribute database $FIG_Config::attrDbName chosen.") if T(3);
148            my $user = ($FIG_Config::arch eq 'win' ? 'self' : scalar(getpwent()));
149            $retVal->{_ca} = CustomAttributes->new(user => $user);
150        }
151        # Insure we have access to the stem module.
152        WikiUse('Lingua::Stem');
153        $retVal->{stemmer} = Lingua::Stem->new();
154        $retVal->{stemmer}->stem_caching({ -level => 2 });
155      # Return it.      # Return it.
156      return $retVal;      return $retVal;
157  }  }
158    
159    =head3 CoreGenomes
160    
161        my @genomes = $sprout->CoreGenomes($scope);
162    
163    Return the IDs of NMPDR genomes in the specified scope.
164    
165    =over 4
166    
167    =item scope
168    
169    Scope of the desired genomes. C<core> covers the original core genomes,
170    C<nmpdr> covers all genomes in NMPDR groups, and C<all> covers all
171    genomes in the system.
172    
173    =item RETURN
174    
175    Returns a list of the IDs for the genomes in the specified scope.
176    
177    =back
178    
179    =cut
180    
181    sub CoreGenomes {
182        # Get the parameters.
183        my ($self, $scope) = @_;
184        # Declare the return variable.
185        my @retVal = ();
186        # If we want all genomes, then this is easy.
187        if ($scope eq 'all') {
188            @retVal = $self->Genomes();
189        } else {
190            # Here we're dealing with groups. Get the hash of all the
191            # genome groups.
192            my %groups = $self->GetGroups();
193            # Loop through the groups, keeping the ones that we want.
194            for my $group (keys %groups) {
195                # Decide if we want to keep this group.
196                my $keepGroup = 0;
197                if ($scope eq 'nmpdr') {
198                    # NMPDR mode: keep all groups.
199                    $keepGroup = 1;
200                } elsif ($scope eq 'core') {
201                    # CORE mode. Only keep real core groups.
202                    if (grep { $group =~ /$_/ } @{$FIG_Config::realCoreGroups}) {
203                        $keepGroup = 1;
204                    }
205                }
206                # Add this group if we're keeping it.
207                if ($keepGroup) {
208                    push @retVal, @{$groups{$group}};
209                }
210            }
211        }
212        # Return the result.
213        return @retVal;
214    }
215    
216    =head3 SuperGroup
217    
218        my $superGroup = $sprout->SuperGroup($groupName);
219    
220    Return the name of the super-group containing the specified NMPDR genome
221    group. If no appropriate super-group can be found, an error will be
222    thrown.
223    
224    =over 4
225    
226    =item groupName
227    
228    Name of the group whose super-group is desired.
229    
230    =item RETURN
231    
232    Returns the name of the super-group containing the incoming group.
233    
234    =back
235    
236    =cut
237    
238    sub SuperGroup {
239        # Get the parameters.
240        my ($self, $groupName) = @_;
241        # Declare the return variable.
242        my $retVal;
243        # Get the group hash.
244        my %groupHash = $self->CheckGroupFile();
245        # Find the super-group genus.
246        $groupName =~ /([A-Z]\w+)/;
247        my $nameThing = $1;
248        # See if it's directly in the group hash.
249        if (exists $groupHash{$nameThing}) {
250            # Yes, then it's our result.
251            $retVal = $nameThing;
252        } else {
253            # No, so we have to search.
254            for my $superGroup (keys %groupHash) {
255                # Get this super-group's item list.
256                my $list = $groupHash{$superGroup}->{contents};
257                # Search it.
258                if (grep { $_->[0] eq $nameThing } @{$list}) {
259                    $retVal = $superGroup;
260                }
261            }
262            # Make sure we found something.
263            if (! $retVal) {
264                Confess("No super-group found for \"$groupName\".");
265            }
266        }
267        # Return the result.
268        return $retVal;
269    }
270    
271  =head3 MaxSegment  =head3 MaxSegment
272    
273  C<< my $length = $sprout->MaxSegment(); >>      my $length = $sprout->MaxSegment();
274    
275  This method returns the maximum permissible length of a feature segment. The length is important  This method returns the maximum permissible length of a feature segment. The length is important
276  because it enables us to make reasonable guesses at how to find features inside a particular  because it enables us to make reasonable guesses at how to find features inside a particular
# Line 149  Line 287 
287    
288  =head3 MaxSequence  =head3 MaxSequence
289    
290  C<< my $length = $sprout->MaxSequence(); >>      my $length = $sprout->MaxSequence();
291    
292  This method returns the maximum permissible length of a contig sequence. A contig is broken  This method returns the maximum permissible length of a contig sequence. A contig is broken
293  into sequences in order to save memory resources. In particular, when manipulating features,  into sequences in order to save memory resources. In particular, when manipulating features,
# Line 164  Line 302 
302    
303  =head3 Load  =head3 Load
304    
305  C<< $sprout->Load($rebuild); >>;      $sprout->Load($rebuild);;
306    
307  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.
308    
# Line 204  Line 342 
342    
343  =head3 LoadUpdate  =head3 LoadUpdate
344    
345  C<< my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>      my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList);
346    
347  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
348  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 250  Line 388 
388              Trace("No load file found for $tableName in $dataDir.") if T(0);              Trace("No load file found for $tableName in $dataDir.") if T(0);
389          } else {          } else {
390              # Attempt to load this table.              # Attempt to load this table.
391              my $result = $self->LoadTable($fileName, $tableName, $truncateFlag);              my $result = $self->LoadTable($fileName, $tableName, truncate => $truncateFlag);
392              # Accumulate the resulting statistics.              # Accumulate the resulting statistics.
393              $retVal->Accumulate($result);              $retVal->Accumulate($result);
394          }          }
# Line 261  Line 399 
399    
400  =head3 GenomeCounts  =head3 GenomeCounts
401    
402  C<< my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete); >>      my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete);
403    
404  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
405  genomes will be included in the counts.  genomes will be included in the counts.
# Line 306  Line 444 
444    
445  =head3 ContigCount  =head3 ContigCount
446    
447  C<< my $count = $sprout->ContigCount($genomeID); >>      my $count = $sprout->ContigCount($genomeID);
448    
449  Return the number of contigs for the specified genome ID.  Return the number of contigs for the specified genome ID.
450    
# Line 333  Line 471 
471      return $retVal;      return $retVal;
472  }  }
473    
474  =head3 GeneMenu  =head3 GenomeMenu
475    
476  C<< my $selectHtml = $sprout->GeneMenu(\%attributes, $filterString, \@params); >>      my $html = $sprout->GenomeMenu(%options);
477    
478  Return an HTML select menu of genomes. Each genome will be an option in the menu,  Generate a genome selection control with the specified name and options.
479  and will be displayed by name with the ID and a contig count attached. The selection  This control is almost but not quite the same as the genome control in the
480  value will be the genome ID. The genomes will be sorted by genus/species name.  B<SearchHelper> class. Eventually, the two will be combined.
481    
482  =over 4  =over 4
483    
484  =item attributes  =item options
485    
486    Optional parameters for the control (see below).
487    
488  Reference to a hash mapping attributes to values for the SELECT tag generated.  =item RETURN
489    
490  =item filterString  Returns the HTML for a genome selection control on a form (sometimes called a popup menu).
491    
492  A filter string for use in selecting the genomes. The filter string must conform  =back
 to the rules for the C<< ERDB->Get >> method.  
493    
494  =item params  The valid options are as follows.
495    
496  Reference to a list of values to be substituted in for the parameter marks in  =over 4
 the filter string.  
497    
498  =item RETURN  =item name
499    
500    Name to give this control for use in passing it to the form. The default is C<myGenomeControl>.
501    Terrible things will happen if you have two controls with the same name on the same page.
502    
503    =item filter
504    
505    If specified, a filter for the list of genomes to display. The filter should be in the form of a
506    list reference. The first element of the list should be the filter string, and the remaining elements
507    the filter parameters.
508    
509    =item multiSelect
510    
511  Returns an HTML select menu with the specified genomes as selectable options.  If TRUE, then the user can select multiple genomes. If FALSE, the user can only select one genome.
512    
513    =item size
514    
515    Number of rows to display in the control. The default is C<10>
516    
517    =item id
518    
519    ID to give this control. The default is the value of the C<name> option. Nothing will work correctly
520    unless this ID is unique.
521    
522    =item selected
523    
524    A comma-delimited list of selected genomes, or a reference to a list of selected genomes. The
525    default is none.
526    
527    =item class
528    
529    If specified, a style class to assign to the genome control.
530    
531  =back  =back
532    
533  =cut  =cut
534    
535  sub GeneMenu {  sub GenomeMenu {
536      # Get the parameters.      # Get the parameters.
537      my ($self, $attributes, $filterString, $params) = @_;      my ($self, %options) = @_;
538      # Start the menu.      # Get the control's name and ID.
539      my $retVal = "<select " .      my $menuName = $options{name} || 'myGenomeControl';
540          join(" ", map { "$_=\"$attributes->{$_}\"" } keys %{$attributes}) .      my $menuID = $options{id} || $menuName;
541          ">\n";      # Compute the IDs for the status display.
542      # Get the genomes.      my $divID = "${menuID}_status";
543      my @genomes = $self->GetAll(['Genome'], $filterString, $params, ['Genome(id)',      my $urlID = "${menuID}_url";
544                                                                       'Genome(genus)',      # Compute the code to show selected genomes in the status area.
545                                                                       'Genome(species)']);      my $showSelect = "showSelected('$menuID', '$divID', '$urlID', 1000)";
546      # Sort them by name.      # Check for single-select or multi-select.
547      my @sorted = sort { lc("$a->[1] $a->[2]") cmp lc("$b->[1] $b->[2]") } @genomes;      my $multiSelect = $options{multiSelect} || 0;
548      # Loop through the genomes, creating the option tags.      # Get the style data.
549      for my $genomeData (@sorted) {      my $class = $options{class} || '';
550          # Get the data for this genome.      # Get the list of pre-selected items.
551          my ($genomeID, $genus, $species) = @{$genomeData};      my $selections = $options{selected} || [];
552          # Get the contig count.      if (ref $selections ne 'ARRAY') {
553          my $count = $self->ContigCount($genomeID);          $selections = [ split /\s*,\s*/, $selections ];
554          my $counting = ($count == 1 ? "contig" : "contigs");      }
555          # Build the option tag.      my %selected = map { $_ => } @{$selections};
556          $retVal .= "<option value=\"$genomeID\">$genus $species ($genomeID) [$count $counting]</option>\n";      # Extract the filter information. The default is no filtering. It can be passed as a tab-delimited
557        # string or a list reference.
558        my $filterParms = $options{filter} || "";
559        if (! ref $filterParms) {
560            $filterParms = [split /\t|\\t/, $filterParms];
561        }
562        my $filterString = shift @{$filterParms};
563        # Get a list of all the genomes in group order. In fact, we only need them ordered
564        # by name (genus,species,strain), but putting primary-group in front enables us to
565        # take advantage of an existing index.
566        my @genomeList = $self->GetAll(['Genome'], "$filterString ORDER BY Genome(primary-group), Genome(genus), Genome(species), Genome(unique-characterization)",
567                                       $filterParms,
568                                       [qw(Genome(primary-group) Genome(id) Genome(genus) Genome(species) Genome(unique-characterization) Genome(taxonomy) Genome(contigs))]);
569        # Create a hash to organize the genomes by group. Each group will contain a list of
570        # 2-tuples, the first element being the genome ID and the second being the genome
571        # name.
572        my %gHash = ();
573        for my $genome (@genomeList) {
574            # Get the genome data.
575            my ($group, $genomeID, $genus, $species, $strain, $taxonomy, $contigs) = @{$genome};
576            # Compute its name. This is the genus, species, strain (if any), and the contig count.
577            my $name = "$genus $species ";
578            $name .= "$strain " if $strain;
579            my $contigCount = ($contigs == 1 ? "" : ", $contigs contigs");
580            # Now we get the domain. The domain tells us the display style of the organism.
581            my ($domain) = split /\s*;\s*/, $taxonomy, 2;
582            # Now compute the display group. This is normally the primary group, but if the
583            # organism is supporting, we blank it out.
584            my $displayGroup = ($group eq $FIG_Config::otherGroup ? "" : $group);
585            # Push the genome into the group's list. Note that we use the real group
586            # name for the hash key here, not the display group name.
587            push @{$gHash{$group}}, [$genomeID, $name, $contigCount, $domain];
588        }
589        # We are almost ready to unroll the menu out of the group hash. The final step is to separate
590        # the supporting genomes by domain. First, we extract the NMPDR groups and sort them. They
591        # are sorted by the first capitalized word. Groups with "other" are sorted after groups
592        # that aren't "other". At some point, we will want to make this less complicated.
593        my %sortGroups = map { $_ =~ /(other)?(.*)([A-Z].+)/; "$3$1$2" => $_ }
594                             grep { $_ ne $FIG_Config::otherGroup } keys %gHash;
595        my @groups = map { $sortGroups{$_} } sort keys %sortGroups;
596        # Remember the number of NMPDR groups.
597        my $nmpdrGroupCount = scalar @groups;
598        # Loop through the supporting genomes, classifying them by domain. We'll also keep a list
599        # of the domains found.
600        my @otherGenomes = @{$gHash{$FIG_Config::otherGroup}};
601        my @domains = ();
602        for my $genomeData (@otherGenomes) {
603            my ($genomeID, $name, $contigCount, $domain) = @{$genomeData};
604            if (exists $gHash{$domain}) {
605                push @{$gHash{$domain}}, $genomeData;
606            } else {
607                $gHash{$domain} = [$genomeData];
608                push @domains, $domain;
609            }
610        }
611        # Add the domain groups at the end of the main group list. The main group list will now
612        # contain all the categories we need to display the genomes.
613        push @groups, sort @domains;
614        # Delete the supporting group.
615        delete $gHash{$FIG_Config::otherGroup};
616        # Now it gets complicated. We need a way to mark all the NMPDR genomes. We take advantage
617        # of the fact they come first in the list. We'll accumulate a count of the NMPDR genomes
618        # and use that to make the selections.
619        my $nmpdrCount = 0;
620        # Create the type counters.
621        my $groupCount = 1;
622        # Get the number of rows to display.
623        my $rows = $options{size} || 10;
624        # If we're multi-row, create an onChange event.
625        my $onChangeTag = ( $rows > 1 ? " onChange=\"$showSelect;\" onFocus=\"$showSelect;\"" : "" );
626        # Set up the multiple-select flag.
627        my $multipleTag = ($multiSelect ? " multiple" : "" );
628        # Set up the style class.
629        my $classTag = ($class ? " class=\"$class\"" : "" );
630        # Create the SELECT tag and stuff it into the output array.
631        my @lines = ("<SELECT name=\"$menuID\" id=\"$menuID\" $onChangeTag$multipleTag$classTag size=\"$rows\">");
632        # Loop through the groups.
633        for my $group (@groups) {
634            # Get the genomes in the group.
635            for my $genome (@{$gHash{$group}}) {
636                # If this is an NMPDR organism, we add an extra style and count it.
637                my $nmpdrStyle = "";
638                if ($nmpdrGroupCount > 0) {
639                    $nmpdrCount++;
640                    $nmpdrStyle = " Core";
641                }
642                # Get the organism ID, name, contig count, and domain.
643                my ($genomeID, $name, $contigCount, $domain) = @{$genome};
644                # See if we're pre-selected.
645                my $selectTag = ($selected{$genomeID} ? " SELECTED" : "");
646                # Compute the display name.
647                my $nameString = "$name ($genomeID$contigCount)";
648                # Generate the option tag.
649                my $optionTag = "<OPTION class=\"$domain$nmpdrStyle\" title=\"$group\" value=\"$genomeID\"$selectTag>$nameString</OPTION>";
650                push @lines, "    $optionTag";
651            }
652            # Record this group in the nmpdrGroup count. When that gets to 0, we've finished the NMPDR
653            # groups.
654            $nmpdrGroupCount--;
655      }      }
656      # Close the SELECT tag.      # Close the SELECT tag.
657      $retVal .= "</select>\n";      push @lines, "</SELECT>";
658        if ($rows > 1) {
659            # We're in a non-compact mode, so we need to add some selection helpers. First is
660            # the search box. This allows the user to type text and change which genomes are
661            # displayed. For multiple-select mode, we include a button that selects the displayed
662            # genes. For single-select mode, we use a plain label instead.
663            my $searchThingName = "${menuID}_SearchThing";
664            my $searchThingLabel = ($multiSelect ? "<INPUT type=\"button\" name=\"MacroSearch\" class=\"button\" value=\"Select genomes containing\" onClick=\"selectShowing('$menuID', '$searchThingName'); $showSelect;\" />"
665                                                 : "Show genomes containing");
666            push @lines, "<br />$searchThingLabel&nbsp;" .
667                         "<INPUT type=\"text\" id=\"$searchThingName\" name=\"$searchThingName\" size=\"30\" onKeyup=\"showTyped('$menuID', '$searchThingName');\" />";
668            # For multi-select mode, we also have buttons to set and clear selections.
669            if ($multiSelect) {
670                push @lines, "<INPUT type=\"button\" name=\"ClearAll\" class=\"bigButton\"  value=\"Clear All\" onClick=\"clearAll('$menuID'); $showSelect\" />";
671                push @lines, "<INPUT type=\"button\" name=\"SelectAll\" class=\"bigButton\" value=\"Select All\" onClick=\"selectAll('$menuID'); $showSelect\" />";
672                push @lines, "<INPUT type=\"button\" name=\"NMPDROnly\" class=\"bigButton\"  value=\"Select NMPDR\" onClick=\"selectSome('$menuID', $nmpdrCount, true); $showSelect;\" />";
673            }
674            # Add a hidden field we can use to generate organism page hyperlinks.
675            push @lines, "<INPUT type=\"hidden\" id=\"$urlID\" value=\"$FIG_Config::cgi_url/seedviewer.cgi?page=Organism;organism=\" />";
676            # Add the status display. This tells the user what's selected no matter where the list is scrolled.
677            push @lines, "<DIV id=\"$divID\" class=\"Panel\"></DIV>";
678        }
679        # Assemble all the lines into a string.
680        my $retVal = join("\n", @lines, "");
681        # Return the result.
682        return $retVal;
683    }
684    
685    
686    =head3 Stem
687    
688        my $stem = $sprout->Stem($word);
689    
690    Return the stem of the specified word, or C<undef> if the word is not
691    stemmable. Note that even if the word is stemmable, the stem may be
692    the same as the original word.
693    
694    =over 4
695    
696    =item word
697    
698    Word to convert into a stem.
699    
700    =item RETURN
701    
702    Returns a stem of the word (which may be the word itself), or C<undef> if
703    the word is not stemmable.
704    
705    =back
706    
707    =cut
708    
709    sub Stem {
710        # Get the parameters.
711        my ($self, $word) = @_;
712        # Declare the return variable.
713        my $retVal;
714        # See if it's stemmable.
715        if ($word =~ /^[A-Za-z]+$/) {
716            # Compute the stem.
717            my $stemList = $self->{stemmer}->stem($word);
718            my $stem = $stemList->[0];
719            # Check to see if it's long enough.
720            if (length $stem >= 3) {
721                # Yes, keep it.
722                $retVal = $stem;
723            } else {
724                # No, use the original word.
725                $retVal = $word;
726            }
727        }
728      # Return the result.      # Return the result.
729      return $retVal;      return $retVal;
730  }  }
731    
732    
733  =head3 Build  =head3 Build
734    
735  C<< $sprout->Build(); >>      $sprout->Build();
736    
737  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.
738  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 412  Line 749 
749    
750  =head3 Genomes  =head3 Genomes
751    
752  C<< my @genomes = $sprout->Genomes(); >>      my @genomes = $sprout->Genomes();
753    
754  Return a list of all the genome IDs.  Return a list of all the genome IDs.
755    
# Line 429  Line 766 
766    
767  =head3 GenusSpecies  =head3 GenusSpecies
768    
769  C<< my $infoString = $sprout->GenusSpecies($genomeID); >>      my $infoString = $sprout->GenusSpecies($genomeID);
770    
771  Return the genus, species, and unique characterization for a genome.  Return the genus, species, and unique characterization for a genome.
772    
# Line 461  Line 798 
798    
799  =head3 FeaturesOf  =head3 FeaturesOf
800    
801  C<< my @features = $sprout->FeaturesOf($genomeID, $ftype); >>      my @features = $sprout->FeaturesOf($genomeID, $ftype);
802    
803  Return a list of the features relevant to a specified genome.  Return a list of the features relevant to a specified genome.
804    
# Line 506  Line 843 
843    
844  =head3 FeatureLocation  =head3 FeatureLocation
845    
846  C<< my @locations = $sprout->FeatureLocation($featureID); >>      my @locations = $sprout->FeatureLocation($featureID);
847    
848  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
849  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 530  Line 867 
867  =item RETURN  =item RETURN
868    
869  Returns a list of the feature's contig segments. The locations are returned as a list in a list  Returns a list of the feature's contig segments. The locations are returned as a list in a list
870  context and as a comma-delimited string in a scalar context.  context and as a comma-delimited string in a scalar context. An empty list means the feature
871    wasn't found.
872    
873  =back  =back
874    
875  =cut  =cut
876  #: Return Type @;  
 #: Return Type $;  
877  sub FeatureLocation {  sub FeatureLocation {
878      # Get the parameters.      # Get the parameters.
879      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
880      # Create a query for the feature locations.      # Declare the return variable.
     my $query = $self->Get(['IsLocatedIn'], "IsLocatedIn(from-link) = ? ORDER BY IsLocatedIn(locN)",  
                            [$featureID]);  
     # Create the return list.  
881      my @retVal = ();      my @retVal = ();
882      # Set up the variables used to determine if we have adjacent segments. This initial setup will      # Get the feature record.
883      # not match anything.      my $object = $self->GetEntity('Feature', $featureID);
884      my ($prevContig, $prevBeg, $prevDir, $prevLen) = ("", 0, "0", 0);      # Only proceed if we found it.
885      # Loop through the query results, creating location specifiers.      if (defined $object) {
886      while (my $location = $query->Fetch()) {          # Get the location string.
887          # Get the location parameters.          my $locString = $object->PrimaryValue('Feature(location-string)');
888          my ($contigID, $beg, $dir, $len) = $location->Values(['IsLocatedIn(to-link)',          # Create the return list.
889              'IsLocatedIn(beg)', 'IsLocatedIn(dir)', 'IsLocatedIn(len)']);          @retVal = split /\s*,\s*/, $locString;
         # Check to see if we are adjacent to the previous segment.  
         if ($prevContig eq $contigID && $dir eq $prevDir) {  
             # Here the new segment is in the same direction on the same contig. Insure the  
             # new segment's beginning is next to the old segment's end.  
             if ($dir eq "-" && $beg + $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";  
890      }      }
891      # Return the list in the format indicated by the context.      # Return the list in the format indicated by the context.
892      return (wantarray ? @retVal : join(',', @retVal));      return (wantarray ? @retVal : join(',', @retVal));
# Line 585  Line 894 
894    
895  =head3 ParseLocation  =head3 ParseLocation
896    
897  C<< my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location); >>      my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location);
898    
899  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
900  length.  length.
# Line 604  Line 913 
913  =back  =back
914    
915  =cut  =cut
916  #: Return Type @;  
917  sub ParseLocation {  sub ParseLocation {
918      # 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
919      # the first parameter.      # the first parameter.
# Line 627  Line 936 
936      return ($contigID, $start, $dir, $len);      return ($contigID, $start, $dir, $len);
937  }  }
938    
939    
940    
941  =head3 PointLocation  =head3 PointLocation
942    
943  C<< my $found = Sprout::PointLocation($location, $point); >>      my $found = Sprout::PointLocation($location, $point);
944    
945  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
946  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 658  Line 969 
969  =back  =back
970    
971  =cut  =cut
972  #: Return Type $;  
973  sub PointLocation {  sub PointLocation {
974      # 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
975      # the first parameter.      # the first parameter.
# Line 681  Line 992 
992    
993  =head3 DNASeq  =head3 DNASeq
994    
995  C<< my $sequence = $sprout->DNASeq(\@locationList); >>      my $sequence = $sprout->DNASeq(\@locationList);
996    
997  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
998  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 765  Line 1076 
1076    
1077  =head3 AllContigs  =head3 AllContigs
1078    
1079  C<< my @idList = $sprout->AllContigs($genomeID); >>      my @idList = $sprout->AllContigs($genomeID);
1080    
1081  Return a list of all the contigs for a genome.  Return a list of all the contigs for a genome.
1082    
# Line 793  Line 1104 
1104      return @retVal;      return @retVal;
1105  }  }
1106    
1107    =head3 GenomeLength
1108    
1109        my $length = $sprout->GenomeLength($genomeID);
1110    
1111    Return the length of the specified genome in base pairs.
1112    
1113    =over 4
1114    
1115    =item genomeID
1116    
1117    ID of the genome whose base pair count is desired.
1118    
1119    =item RETURN
1120    
1121    Returns the number of base pairs in all the contigs of the specified
1122    genome.
1123    
1124    =back
1125    
1126    =cut
1127    
1128    sub GenomeLength {
1129        # Get the parameters.
1130        my ($self, $genomeID) = @_;
1131        # Declare the return variable.
1132        my $retVal = 0;
1133        # Get the genome's contig sequence lengths.
1134        my @lens = $self->GetFlat(['HasContig', 'IsMadeUpOf'], 'HasContig(from-link) = ?',
1135                           [$genomeID], 'IsMadeUpOf(len)');
1136        # Sum the lengths.
1137        map { $retVal += $_ } @lens;
1138        # Return the result.
1139        return $retVal;
1140    }
1141    
1142    =head3 FeatureCount
1143    
1144        my $count = $sprout->FeatureCount($genomeID, $type);
1145    
1146    Return the number of features of the specified type in the specified genome.
1147    
1148    =over 4
1149    
1150    =item genomeID
1151    
1152    ID of the genome whose feature count is desired.
1153    
1154    =item type
1155    
1156    Type of feature to count (eg. C<peg>, C<rna>, etc.).
1157    
1158    =item RETURN
1159    
1160    Returns the number of features of the specified type for the specified genome.
1161    
1162    =back
1163    
1164    =cut
1165    
1166    sub FeatureCount {
1167        # Get the parameters.
1168        my ($self, $genomeID, $type) = @_;
1169        # Compute the count.
1170        my $retVal = $self->GetCount(['HasFeature', 'Feature'],
1171                                    "HasFeature(from-link) = ? AND Feature(feature-type) = ?",
1172                                    [$genomeID, $type]);
1173        # Return the result.
1174        return $retVal;
1175    }
1176    
1177    =head3 GenomeAssignments
1178    
1179        my $fidHash = $sprout->GenomeAssignments($genomeID);
1180    
1181    Return a list of a genome's assigned features. The return hash will contain each
1182    assigned feature of the genome mapped to the text of its most recent functional
1183    assignment.
1184    
1185    =over 4
1186    
1187    =item genomeID
1188    
1189    ID of the genome whose functional assignments are desired.
1190    
1191    =item RETURN
1192    
1193    Returns a reference to a hash which maps each feature to its most recent
1194    functional assignment.
1195    
1196    =back
1197    
1198    =cut
1199    
1200    sub GenomeAssignments {
1201        # Get the parameters.
1202        my ($self, $genomeID) = @_;
1203        # Declare the return variable.
1204        my $retVal = {};
1205        # Query the genome's features.
1206        my $query = $self->Get(['HasFeature', 'Feature'], "HasFeature(from-link) = ?",
1207                               [$genomeID]);
1208        # Loop through the features.
1209        while (my $data = $query->Fetch) {
1210            # Get the feature ID and assignment.
1211            my ($fid, $assignment) = $data->Values(['Feature(id)', 'Feature(assignment)']);
1212            if ($assignment) {
1213                $retVal->{$fid} = $assignment;
1214            }
1215        }
1216        # Return the result.
1217        return $retVal;
1218    }
1219    
1220  =head3 ContigLength  =head3 ContigLength
1221    
1222  C<< my $length = $sprout->ContigLength($contigID); >>      my $length = $sprout->ContigLength($contigID);
1223    
1224  Compute the length of a contig.  Compute the length of a contig.
1225    
# Line 834  Line 1258 
1258    
1259  =head3 ClusterPEGs  =head3 ClusterPEGs
1260    
1261  C<< my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs); >>      my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs);
1262    
1263  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
1264  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
1265  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
1266  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
1267  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
1268  sequence.  appear in the output sequence.
1269    
1270  =over 4  =over 4
1271    
# Line 882  Line 1306 
1306    
1307  =head3 GenesInRegion  =head3 GenesInRegion
1308    
1309  C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>      my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop);
1310    
1311  List the features which overlap a specified region in a contig.  List the features which overlap a specified region in a contig.
1312    
# Line 911  Line 1335 
1335  =back  =back
1336    
1337  =cut  =cut
1338  #: Return Type @@;  
1339  sub GenesInRegion {  sub GenesInRegion {
1340      # Get the parameters.      # Get the parameters.
1341      my ($self, $contigID, $start, $stop) = @_;      my ($self, $contigID, $start, $stop) = @_;
1342      # Get the maximum segment length.      # Get the maximum segment length.
1343      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 = ();  
1344      # Prime the values we'll use for the returned beginning and end.      # Prime the values we'll use for the returned beginning and end.
1345      my @initialMinMax = ($self->ContigLength($contigID), 0);      my @initialMinMax = ($self->ContigLength($contigID), 0);
1346      my ($min, $max) = @initialMinMax;      my ($min, $max) = @initialMinMax;
1347      # Create a table of parameters for each query. Each query looks for features travelling in      # Get the overlapping features.
1348        my @featureObjects = $self->GeneDataInRegion($contigID, $start, $stop);
1349        # We'l use this hash to help us track the feature IDs and sort them. The key is the
1350        # feature ID and the value is a [$left,$right] pair indicating the maximum extent
1351        # of the feature's locations.
1352        my %featureMap = ();
1353        # Loop through them to do the begin/end analysis.
1354        for my $featureObject (@featureObjects) {
1355            # Get the feature's location string. This may contain multiple actual locations.
1356            my ($locations, $fid) = $featureObject->Values([qw(Feature(location-string) Feature(id))]);
1357            my @locationSegments = split /\s*,\s*/, $locations;
1358            # Loop through the locations.
1359            for my $locationSegment (@locationSegments) {
1360                # Construct an object for the location.
1361                my $locationObject = BasicLocation->new($locationSegment);
1362                # Merge the current segment's begin and end into the min and max.
1363                my ($left, $right) = ($locationObject->Left, $locationObject->Right);
1364                my ($beg, $end);
1365                if (exists $featureMap{$fid}) {
1366                    ($beg, $end) = @{$featureMap{$fid}};
1367                    $beg = $left if $left < $beg;
1368                    $end = $right if $right > $end;
1369                } else {
1370                    ($beg, $end) = ($left, $right);
1371                }
1372                $min = $beg if $beg < $min;
1373                $max = $end if $end > $max;
1374                # Store the feature's new extent back into the hash table.
1375                $featureMap{$fid} = [$beg, $end];
1376            }
1377        }
1378        # Now we must compute the list of the IDs for the features found. We start with a list
1379        # of midpoints / feature ID pairs. (It's not really a midpoint, it's twice the midpoint,
1380        # but the result of the sort will be the same.)
1381        my @list = map { [$featureMap{$_}->[0] + $featureMap{$_}->[1], $_] } keys %featureMap;
1382        # Now we sort by midpoint and yank out the feature IDs.
1383        my @retVal = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @list;
1384        # Return it along with the min and max.
1385        return (\@retVal, $min, $max);
1386    }
1387    
1388    =head3 GeneDataInRegion
1389    
1390        my @featureList = $sprout->GenesInRegion($contigID, $start, $stop);
1391    
1392    List the features which overlap a specified region in a contig.
1393    
1394    =over 4
1395    
1396    =item contigID
1397    
1398    ID of the contig containing the region of interest.
1399    
1400    =item start
1401    
1402    Offset of the first residue in the region of interest.
1403    
1404    =item stop
1405    
1406    Offset of the last residue in the region of interest.
1407    
1408    =item RETURN
1409    
1410    Returns a list of B<ERDBObjects> for the desired features. Each object will
1411    contain a B<Feature> record.
1412    
1413    =back
1414    
1415    =cut
1416    
1417    sub GeneDataInRegion {
1418        # Get the parameters.
1419        my ($self, $contigID, $start, $stop) = @_;
1420        # Get the maximum segment length.
1421        my $maximumSegmentLength = $self->MaxSegment;
1422        # Create a hash to receive the feature list. We use a hash so that we can eliminate
1423        # duplicates easily. The hash key will be the feature ID. The value will be the feature's
1424        # ERDBObject from the query.
1425        my %featuresFound = ();
1426        # Create a table of parameters for the queries. Each query looks for features travelling in
1427      # 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,
1428      # 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
1429      # 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 934  Line 1432 
1432      # Loop through the query parameters.      # Loop through the query parameters.
1433      for my $parms (values %queryParms) {      for my $parms (values %queryParms) {
1434          # Create the query.          # Create the query.
1435          my $query = $self->Get(['IsLocatedIn'],          my $query = $self->Get([qw(Feature IsLocatedIn)],
1436              "IsLocatedIn(to-link)= ? AND IsLocatedIn(dir) = ? AND IsLocatedIn(beg) >= ? AND IsLocatedIn(beg) <= ?",              "IsLocatedIn(to-link)= ? AND IsLocatedIn(dir) = ? AND IsLocatedIn(beg) >= ? AND IsLocatedIn(beg) <= ?",
1437              $parms);              $parms);
1438          # Loop through the feature segments found.          # Loop through the feature segments found.
1439          while (my $segment = $query->Fetch) {          while (my $segment = $query->Fetch) {
1440              # Get the data about this segment.              # Get the data about this segment.
1441              my ($featureID, $dir, $beg, $len) = $segment->Values(['IsLocatedIn(from-link)',              my ($featureID, $contig, $dir, $beg, $len) = $segment->Values([qw(IsLocatedIn(from-link)
1442                  'IsLocatedIn(dir)', 'IsLocatedIn(beg)', 'IsLocatedIn(len)']);                  IsLocatedIn(to-link) IsLocatedIn(dir) IsLocatedIn(beg) IsLocatedIn(len))]);
1443              # 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
1444              # 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
1445              # 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
1446              # length.              # length.
1447              my ($found, $end) = (0, 0);              my $loc = BasicLocation->new($contig, $beg, $dir, $len);
1448              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;  
                 }  
             }  
1449              if ($found) {              if ($found) {
1450                  # Here we need to record the feature and update the minima and maxima. First,                  # Save this feature in the result list.
1451                  # 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;  
1452                  }                  }
                 # Store the entry back into the hash table.  
                 $featuresFound{$featureID} = [$loc1, $loc2];  
1453              }              }
1454          }          }
1455      }      # Return the ERDB objects for the features found.
1456      # 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);  
1457  }  }
1458    
1459  =head3 FType  =head3 FType
1460    
1461  C<< my $ftype = $sprout->FType($featureID); >>      my $ftype = $sprout->FType($featureID);
1462    
1463  Return the type of a feature.  Return the type of a feature.
1464    
# Line 1024  Line 1488 
1488    
1489  =head3 FeatureAnnotations  =head3 FeatureAnnotations
1490    
1491  C<< my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag); >>      my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag);
1492    
1493  Return the annotations of a feature.  Return the annotations of a feature.
1494    
# Line 1087  Line 1551 
1551    
1552  =head3 AllFunctionsOf  =head3 AllFunctionsOf
1553    
1554  C<< my %functions = $sprout->AllFunctionsOf($featureID); >>      my %functions = $sprout->AllFunctionsOf($featureID);
1555    
1556  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
1557  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 1142  Line 1606 
1606    
1607  =head3 FunctionOf  =head3 FunctionOf
1608    
1609  C<< my $functionText = $sprout->FunctionOf($featureID, $userID); >>      my $functionText = $sprout->FunctionOf($featureID, $userID);
1610    
1611  Return the most recently-determined functional assignment of a particular feature.  Return the most recently-determined functional assignment of a particular feature.
1612    
1613  The functional assignment is handled differently depending on the type of feature. If  The functional assignment is handled differently depending on the type of feature. If
1614  the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional  the feature is identified by a FIG ID (begins with the string C<fig|>), then the functional
1615  assignment is a type of annotation. The format of an assignment is described in  assignment is taken from the B<Feature> or C<Annotation> table, depending.
 L</ParseAssignment>. Its worth noting that we cannot filter on the content of the  
 annotation itself because it's a text field; however, this is not a big problem because  
 most features only have a small number of annotations.  
1616    
1617  Each user has an associated list of trusted users. The assignment returned will be the most  Each user has an associated list of trusted users. The assignment returned will be the most
1618  recent one by at least one of the trusted users. If no trusted user list is available, then  recent one by at least one of the trusted users. If no trusted user list is available, then
# Line 1170  Line 1631 
1631    
1632  =item userID (optional)  =item userID (optional)
1633    
1634  ID of the user whose function determination is desired. If omitted, only the latest  ID of the user whose function determination is desired. If omitted, the primary
1635  C<FIG> assignment will be returned.  functional assignment in the B<Feature> table will be returned.
1636    
1637  =item RETURN  =item RETURN
1638    
# Line 1188  Line 1649 
1649      my $retVal;      my $retVal;
1650      # Determine the ID type.      # Determine the ID type.
1651      if ($featureID =~ m/^fig\|/) {      if ($featureID =~ m/^fig\|/) {
1652          # Here we have a FIG feature ID. We must build the list of trusted          # Here we have a FIG feature ID.
1653          # users.          if (!$userID) {
1654                # Use the primary assignment.
1655                ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(assignment)']);
1656            } else {
1657                # We must build the list of trusted users.
1658          my %trusteeTable = ();          my %trusteeTable = ();
1659          # Check the user ID.          # Check the user ID.
1660          if (!$userID) {          if (!$userID) {
# Line 1232  Line 1697 
1697                  }                  }
1698              }              }
1699          }          }
1700            }
1701      } else {      } else {
1702          # Here we have a non-FIG feature ID. In this case the user ID does not          # Here we have a non-FIG feature ID. In this case the user ID does not
1703          # matter. We simply get the information from the External Alias Function          # matter. We simply get the information from the External Alias Function
# Line 1244  Line 1710 
1710    
1711  =head3 FunctionsOf  =head3 FunctionsOf
1712    
1713  C<< my @functionList = $sprout->FunctionOf($featureID, $userID); >>      my @functionList = $sprout->FunctionOf($featureID, $userID);
1714    
1715  Return the functional assignments of a particular feature.  Return the functional assignments of a particular feature.
1716    
# Line 1316  Line 1782 
1782    
1783  =head3 BBHList  =head3 BBHList
1784    
1785  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>      my $bbhHash = $sprout->BBHList($genomeID, \@featureList);
1786    
1787  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
1788  on a specified target genome.  on a specified target genome.
# Line 1347  Line 1813 
1813      my %retVal = ();      my %retVal = ();
1814      # Loop through the incoming features.      # Loop through the incoming features.
1815      for my $featureID (@{$featureList}) {      for my $featureID (@{$featureList}) {
1816          # Create a query to get the feature's best hit.          # Ask the server for the feature's best hit.
1817          my $query = $self->Get(['IsBidirectionalBestHitOf'],          my @bbhData = FIGRules::BBHData($featureID);
                                "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?",  
                                [$featureID, $genomeID]);  
1818          # Peel off the BBHs found.          # Peel off the BBHs found.
1819          my @found = ();          my @found = ();
1820          while (my $bbh = $query->Fetch) {          for my $bbh (@bbhData) {
1821              push @found, $bbh->Value('IsBidirectionalBestHitOf(to-link)');              my $fid = $bbh->[0];
1822                my $bbGenome = $self->GenomeOf($fid);
1823                if ($bbGenome eq $genomeID) {
1824                    push @found, $fid;
1825                }
1826          }          }
1827          $retVal{$featureID} = \@found;          $retVal{$featureID} = \@found;
1828      }      }
# Line 1364  Line 1832 
1832    
1833  =head3 SimList  =head3 SimList
1834    
1835  C<< my %similarities = $sprout->SimList($featureID, $count); >>      my %similarities = $sprout->SimList($featureID, $count);
1836    
1837  Return a list of the similarities to the specified feature.  Return a list of the similarities to the specified feature.
1838    
1839  Sprout does not support real similarities, so this method just returns the bidirectional  This method just returns the bidirectional best hits for performance reasons.
 best hits.  
1840    
1841  =over 4  =over 4
1842    
# Line 1389  Line 1856 
1856      # Get the parameters.      # Get the parameters.
1857      my ($self, $featureID, $count) = @_;      my ($self, $featureID, $count) = @_;
1858      # Ask for the best hits.      # Ask for the best hits.
1859      my @lists = $self->GetAll(['IsBidirectionalBestHitOf'],      my @lists = FIGRules::BBHData($featureID);
                               "IsBidirectionalBestHitOf(from-link) = ? ORDER BY IsBidirectionalBestHitOf(score) DESC",  
                               [$featureID], ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(score)'],  
                               $count);  
1860      # Create the return value.      # Create the return value.
1861      my %retVal = ();      my %retVal = ();
1862      for my $tuple (@lists) {      for my $tuple (@lists) {
# Line 1402  Line 1866 
1866      return %retVal;      return %retVal;
1867  }  }
1868    
   
   
1869  =head3 IsComplete  =head3 IsComplete
1870    
1871  C<< my $flag = $sprout->IsComplete($genomeID); >>      my $flag = $sprout->IsComplete($genomeID);
1872    
1873  Return TRUE if the specified genome is complete, else FALSE.  Return TRUE if the specified genome is complete, else FALSE.
1874    
# Line 1434  Line 1896 
1896      my $genomeData = $self->GetEntity('Genome', $genomeID);      my $genomeData = $self->GetEntity('Genome', $genomeID);
1897      if ($genomeData) {      if ($genomeData) {
1898          # The genome exists, so get the completeness flag.          # The genome exists, so get the completeness flag.
1899          ($retVal) = $genomeData->Value('Genome(complete)');          $retVal = $genomeData->PrimaryValue('Genome(complete)');
1900      }      }
1901      # Return the result.      # Return the result.
1902      return $retVal;      return $retVal;
# Line 1442  Line 1904 
1904    
1905  =head3 FeatureAliases  =head3 FeatureAliases
1906    
1907  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>      my @aliasList = $sprout->FeatureAliases($featureID);
1908    
1909  Return a list of the aliases for a specified feature.  Return a list of the aliases for a specified feature.
1910    
# Line 1465  Line 1927 
1927      # Get the parameters.      # Get the parameters.
1928      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
1929      # Get the desired feature's aliases      # Get the desired feature's aliases
1930      my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(alias)']);      my @retVal = $self->GetFlat(['IsAliasOf'], "IsAliasOf(to-link) = ?", [$featureID], 'IsAliasOf(from-link)');
1931      # Return the result.      # Return the result.
1932      return @retVal;      return @retVal;
1933  }  }
1934    
1935  =head3 GenomeOf  =head3 GenomeOf
1936    
1937  C<< my $genomeID = $sprout->GenomeOf($featureID); >>      my $genomeID = $sprout->GenomeOf($featureID);
1938    
1939  Return the genome that contains a specified feature or contig.  Return the genome that contains a specified feature or contig.
1940    
# Line 1494  Line 1956 
1956  sub GenomeOf {  sub GenomeOf {
1957      # Get the parameters.      # Get the parameters.
1958      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]);  
1959      # Declare the return value.      # Declare the return value.
1960      my $retVal;      my $retVal;
1961      # Get the genome ID.      # Parse the genome ID from the feature ID.
1962      if (my $relationship = $query->Fetch()) {      if ($featureID =~ /^fig\|(\d+\.\d+)/) {
1963          ($retVal) = $relationship->Value('HasContig(from-link)');          $retVal = $1;
1964        } else {
1965            Confess("Invalid feature ID $featureID.");
1966      }      }
1967      # Return the value found.      # Return the value found.
1968      return $retVal;      return $retVal;
# Line 1509  Line 1970 
1970    
1971  =head3 CoupledFeatures  =head3 CoupledFeatures
1972    
1973  C<< my %coupleHash = $sprout->CoupledFeatures($featureID); >>      my %coupleHash = $sprout->CoupledFeatures($featureID);
1974    
1975  Return the features functionally coupled with a specified feature. Features are considered  Return the features functionally coupled with a specified feature. Features are considered
1976  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 1531  Line 1992 
1992  sub CoupledFeatures {  sub CoupledFeatures {
1993      # Get the parameters.      # Get the parameters.
1994      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
1995      # Create a query to retrieve the functionally-coupled features.      # Ask the coupling server for the data.
1996      my $query = $self->Get(['ParticipatesInCoupling', 'Coupling'],      Trace("Looking for features coupled to $featureID.") if T(coupling => 3);
1997                             "ParticipatesInCoupling(from-link) = ?", [$featureID]);      my @rawPairs = FIGRules::NetCouplingData('coupled_to', id1 => $featureID);
1998      # This value will be set to TRUE if we find at least one coupled feature.      Trace(scalar(@rawPairs) . " couplings returned.") if T(coupling => 3);
1999      my $found = 0;      # Form them into a hash.
     # Create the return hash.  
2000      my %retVal = ();      my %retVal = ();
2001      # Retrieve the relationship records and store them in the hash.      for my $pair (@rawPairs) {
2002      while (my $clustering = $query->Fetch()) {          # Get the feature ID and score.
2003          # Get the ID and score of the coupling.          my ($featureID2, $score) = @{$pair};
2004          my ($couplingID, $score) = $clustering->Values(['Coupling(id)',          # Only proceed if the feature is in NMPDR.
2005                                                          'Coupling(score)']);          if ($self->_CheckFeature($featureID2)) {
2006          # Get the other feature that participates in the coupling.              $retVal{$featureID2} = $score;
2007          my ($otherFeatureID) = $self->GetFlat(['ParticipatesInCoupling'],          }
                                            "ParticipatesInCoupling(to-link) = ? AND ParticipatesInCoupling(from-link) <> ?",  
                                            [$couplingID, $featureID], 'ParticipatesInCoupling(from-link)');  
         # Attach the other feature's score to its ID.  
         $retVal{$otherFeatureID} = $score;  
         $found = 1;  
2008      }      }
2009      # 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
2010      # the incoming feature as well.      # the incoming feature as well.
2011      if ($found) {      if (keys %retVal) {
2012          $retVal{$featureID} = 9999;          $retVal{$featureID} = 9999;
2013      }      }
2014      # Return the hash.      # Return the hash.
# Line 1562  Line 2017 
2017    
2018  =head3 CouplingEvidence  =head3 CouplingEvidence
2019    
2020  C<< my @evidence = $sprout->CouplingEvidence($peg1, $peg2); >>      my @evidence = $sprout->CouplingEvidence($peg1, $peg2);
2021    
2022  Return the evidence for a functional coupling.  Return the evidence for a functional coupling.
2023    
# Line 1610  Line 2065 
2065      my ($self, $peg1, $peg2) = @_;      my ($self, $peg1, $peg2) = @_;
2066      # Declare the return variable.      # Declare the return variable.
2067      my @retVal = ();      my @retVal = ();
2068      # Our first task is to find out the nature of the coupling: whether or not      # Get the coupling and evidence data.
2069      # it exists, its score, and whether the features are stored in the same      my @rawData = FIGRules::NetCouplingData('coupling_evidence', id1 => $peg1, id2 => $peg2);
2070      # order as the ones coming in.      # Loop through the raw data, saving the ones that are in NMPDR genomes.
2071      my ($couplingID, $inverted, $score) = $self->GetCoupling($peg1, $peg2);      for my $rawTuple (@rawData) {
2072      # Only proceed if a coupling exists.          if ($self->_CheckFeature($rawTuple->[0]) && $self->_CheckFeature($rawTuple->[1])) {
2073      if ($couplingID) {              push @retVal, $rawTuple;
2074          # 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);  
2075      }      }
2076      # Return the result.      # Return the result.
2077      return @retVal;      return @retVal;
2078  }  }
2079    
2080  =head3 GetCoupling  =head3 GetSynonymGroup
   
 C<< my ($couplingID, $inverted, $score) = $sprout->GetCoupling($peg1, $peg2); >>  
   
 Return the coupling (if any) for the specified pair of PEGs. If a coupling  
 exists, we return the coupling ID along with an indicator of whether the  
 coupling is stored as C<(>I<$peg1>C<, >I<$peg2>C<)> or C<(>I<$peg2>C<, >I<$peg1>C<)>.  
 In the second case, we say the coupling is I<inverted>. The importance of an  
 inverted coupling is that the PEGs in the evidence will appear in reverse order.  
2081    
2082  =over 4      my $id = $sprout->GetSynonymGroup($fid);
2083    
2084  =item peg1  Return the synonym group name for the specified feature.
2085    
2086  ID of the feature of interest.  =over 4
2087    
2088  =item peg2  =item fid
2089    
2090  ID of the potentially coupled feature.  ID of the feature whose synonym group is desired.
2091    
2092  =item RETURN  =item RETURN
2093    
2094  Returns a three-element list. The first element contains the database ID of  The name of the synonym group to which the feature belongs. If the feature does
2095  the coupling. The second element is FALSE if the coupling is stored in the  not belong to a synonym group, the feature ID itself is returned.
 database in the caller specified order and TRUE if it is stored in the  
 inverted order. The third element is the coupling's score. If the coupling  
 does not exist, all three list elements will be C<undef>.  
2096    
2097  =back  =back
2098    
2099  =cut  =cut
2100  #: Return Type $%@;  
2101  sub GetCoupling {  sub GetSynonymGroup {
2102      # Get the parameters.      # Get the parameters.
2103      my ($self, $peg1, $peg2) = @_;      my ($self, $fid) = @_;
2104      # Declare the return values. We'll start with the coupling ID and undefine the      # Declare the return variable.
2105      # flag and score until we have more information.      my $retVal;
2106      my ($retVal, $inverted, $score) = (CouplingID($peg1, $peg2), undef, undef);      # Find the synonym group.
2107      # Find the coupling data.      my @groups = $self->GetFlat(['IsSynonymGroupFor'], "IsSynonymGroupFor(to-link) = ?",
2108      my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'],                                     [$fid], 'IsSynonymGroupFor(from-link)');
                                  "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)",  
                                  [$retVal], ["ParticipatesInCoupling(from-link)", "Coupling(score)"]);  
2109      # Check to see if we found anything.      # Check to see if we found anything.
2110      if (!@pegs) {      if (@groups) {
2111          Trace("No coupling found.") if T(Coupling => 4);          $retVal = $groups[0];
         # No coupling, so undefine the return value.  
         $retVal = undef;  
2112      } else {      } else {
2113          # We have a coupling! Get the score and check for inversion.          $retVal = $fid;
         $score = $pegs[0]->[1];  
         my $firstFound = $pegs[0]->[0];  
         $inverted = ($firstFound ne $peg1);  
         Trace("Coupling score is $score. First peg is $firstFound, peg 1 is $peg1.") if T(Coupling => 4);  
2114      }      }
2115      # Return the result.      # Return the result.
2116      return ($retVal, $inverted, $score);      return $retVal;
2117  }  }
2118    
2119  =head3 CouplingID  =head3 GetBoundaries
   
 C<< my $couplingID = Sprout::CouplingID($peg1, $peg2); >>  
2120    
2121  Return the coupling ID for a pair of feature IDs.      my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList);
2122    
2123  The coupling ID is currently computed by joining the feature IDs in  Determine the begin and end boundaries for the locations in a list. All of the
2124  sorted order with a space. Client modules (that is, modules which  locations must belong to the same contig and have mostly the same direction in
2125  use Sprout) should not, however, count on this always being the  order for this method to produce a meaningful result. The resulting
2126  case. This method provides a way for abstracting the concept of a  begin/end pair will contain all of the bases in any of the locations.
 coupling ID. All that we know for sure about it is that it can be  
 generated easily from the feature IDs and the order of the IDs  
 in the parameter list does not matter (i.e. C<CouplingID("a1", "b1")>  
 will have the same value as C<CouplingID("b1", "a1")>.  
2127    
2128  =over 4  =over 4
2129    
2130  =item peg1  =item locList
2131    
2132  First feature of interest.  List of locations to process.
   
 =item peg2  
   
 Second feature of interest.  
2133    
2134  =item RETURN  =item RETURN
2135    
2136  Returns the ID that would be used to represent a functional coupling of  Returns a 3-tuple consisting of the contig ID, the beginning boundary,
2137  the two specified PEGs.  and the ending boundary. The beginning boundary will be left of the
2138    end for mostly-forward locations and right of the end for mostly-backward
2139    locations.
2140    
2141  =back  =back
2142    
2143  =cut  =cut
2144  #: Return Type $;  
2145  sub CouplingID {  sub GetBoundaries {
2146      return join " ", sort @_;      # Get the parameters.
2147        my ($self, @locList) = @_;
2148        # Set up the counters used to determine the most popular direction.
2149        my %counts = ( '+' => 0, '-' => 0 );
2150        # Get the last location and parse it.
2151        my $locObject = BasicLocation->new(pop @locList);
2152        # Prime the loop with its data.
2153        my ($contig, $beg, $end) = ($locObject->Contig, $locObject->Left, $locObject->Right);
2154        # Count its direction.
2155        $counts{$locObject->Dir}++;
2156        # Loop through the remaining locations. Note that in most situations, this loop
2157        # will not iterate at all, because most of the time we will be dealing with a
2158        # singleton list.
2159        for my $loc (@locList) {
2160            # Create a location object.
2161            my $locObject = BasicLocation->new($loc);
2162            # Count the direction.
2163            $counts{$locObject->Dir}++;
2164            # Get the left end and the right end.
2165            my $left = $locObject->Left;
2166            my $right = $locObject->Right;
2167            # Merge them into the return variables.
2168            if ($left < $beg) {
2169                $beg = $left;
2170            }
2171            if ($right > $end) {
2172                $end = $right;
2173            }
2174        }
2175        # If the most common direction is reverse, flip the begin and end markers.
2176        if ($counts{'-'} > $counts{'+'}) {
2177            ($beg, $end) = ($end, $beg);
2178        }
2179        # Return the result.
2180        return ($contig, $beg, $end);
2181  }  }
2182    
2183  =head3 ReadFasta  =head3 ReadFasta
2184    
2185  C<< my %sequenceData = Sprout::ReadFasta($fileName, $prefix); >>      my %sequenceData = Sprout::ReadFasta($fileName, $prefix);
2186    
2187  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
2188  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 1806  Line 2248 
2248    
2249  =head3 FormatLocations  =head3 FormatLocations
2250    
2251  C<< my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat); >>      my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat);
2252    
2253  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
2254  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 1871  Line 2313 
2313    
2314  =head3 DumpData  =head3 DumpData
2315    
2316  C<< $sprout->DumpData(); >>      $sprout->DumpData();
2317    
2318  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.
2319    
# Line 1888  Line 2330 
2330    
2331  =head3 XMLFileName  =head3 XMLFileName
2332    
2333  C<< my $fileName = $sprout->XMLFileName(); >>      my $fileName = $sprout->XMLFileName();
2334    
2335  Return the name of this database's XML definition file.  Return the name of this database's XML definition file.
2336    
# Line 1899  Line 2341 
2341      return $self->{_xmlName};      return $self->{_xmlName};
2342  }  }
2343    
2344    =head3 GetGenomeNameData
2345    
2346        my ($genus, $species, $strain) = $sprout->GenomeNameData($genomeID);
2347    
2348    Return the genus, species, and unique characterization for a genome. This
2349    is similar to L</GenusSpecies>, with the exception that it returns the
2350    values in three seperate fields.
2351    
2352    =over 4
2353    
2354    =item genomeID
2355    
2356    ID of the genome whose name data is desired.
2357    
2358    =item RETURN
2359    
2360    Returns a three-element list, consisting of the genus, species, and strain
2361    of the specified genome. If the genome is not found, an error occurs.
2362    
2363    =back
2364    
2365    =cut
2366    
2367    sub GetGenomeNameData {
2368        # Get the parameters.
2369        my ($self, $genomeID) = @_;
2370        # Get the desired values.
2371        my ($genus, $species, $strain) = $self->GetEntityValues('Genome', $genomeID =>
2372                                                                [qw(Genome(genus) Genome(species) Genome(unique-characterization))]);
2373        # Throw an error if they were not found.
2374        if (! defined $genus) {
2375            Confess("Genome $genomeID not found in database.");
2376        }
2377        # Return the results.
2378        return ($genus, $species, $strain);
2379    }
2380    
2381    =head3 GetGenomeByNameData
2382    
2383        my @genomes = $sprout->GetGenomeByNameData($genus, $species, $strain);
2384    
2385    Return a list of the IDs of the genomes with the specified genus,
2386    species, and strain. In almost every case, there will be either zero or
2387    one IDs returned; however, two or more IDs could be returned if there are
2388    multiple versions of the genome in the database.
2389    
2390    =over 4
2391    
2392    =item genus
2393    
2394    Genus of the desired genome.
2395    
2396    =item species
2397    
2398    Species of the desired genome.
2399    
2400    =item strain
2401    
2402    Strain (unique characterization) of the desired genome. This may be an empty
2403    string, in which case it is presumed that the desired genome has no strain
2404    specified.
2405    
2406    =item RETURN
2407    
2408    Returns a list of the IDs of the genomes having the specified genus, species, and
2409    strain.
2410    
2411    =back
2412    
2413    =cut
2414    
2415    sub GetGenomeByNameData {
2416        # Get the parameters.
2417        my ($self, $genus, $species, $strain) = @_;
2418        # Try to find the genomes.
2419        my @retVal = $self->GetFlat(['Genome'], "Genome(genus) = ? AND Genome(species) = ? AND Genome(unique-characterization) = ?",
2420                                    [$genus, $species, $strain], 'Genome(id)');
2421        # Return the result.
2422        return @retVal;
2423    }
2424    
2425  =head3 Insert  =head3 Insert
2426    
2427  C<< $sprout->Insert($objectType, \%fieldHash); >>      $sprout->Insert($objectType, \%fieldHash);
2428    
2429  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
2430  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 1910  Line 2433 
2433  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
2434  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>.
2435    
2436  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']});
2437    
2438  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
2439  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>.
2440    
2441  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'});
2442    
2443  =over 4  =over 4
2444    
# Line 1940  Line 2463 
2463    
2464  =head3 Annotate  =head3 Annotate
2465    
2466  C<< my $ok = $sprout->Annotate($fid, $timestamp, $user, $text); >>      my $ok = $sprout->Annotate($fid, $timestamp, $user, $text);
2467    
2468  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
2469  specified feature and user.  specified feature and user.
# Line 1994  Line 2517 
2517    
2518  =head3 AssignFunction  =head3 AssignFunction
2519    
2520  C<< my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser); >>      my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser);
2521    
2522  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
2523  format is described in L</ParseAssignment>.  format is described in L</ParseAssignment>.
# Line 2054  Line 2577 
2577    
2578  =head3 FeaturesByAlias  =head3 FeaturesByAlias
2579    
2580  C<< my @features = $sprout->FeaturesByAlias($alias); >>      my @features = $sprout->FeaturesByAlias($alias);
2581    
2582  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
2583  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 2088  Line 2611 
2611          push @retVal, $mappedAlias;          push @retVal, $mappedAlias;
2612      } else {      } else {
2613          # 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.
2614          @retVal = $self->GetFlat(['Feature'], 'Feature(alias) = ?', [$mappedAlias], 'Feature(id)');          @retVal = $self->GetFlat(['IsAliasOf'], 'IsAliasOf(from-link) = ?', [$mappedAlias], 'IsAliasOf(to-link)');
2615      }      }
2616      # Return the result.      # Return the result.
2617      return @retVal;      return @retVal;
2618  }  }
2619    
 =head3 Exists  
   
 C<< my $found = $sprout->Exists($entityName, $entityID); >>  
   
 Return TRUE if an entity exists, else FALSE.  
   
 =over 4  
   
 =item entityName  
   
 Name of the entity type (e.g. C<Feature>) relevant to the existence check.  
   
 =item entityID  
   
 ID of the entity instance whose existence is to be checked.  
   
 =item RETURN  
   
 Returns TRUE if the entity instance exists, else FALSE.  
   
 =back  
   
 =cut  
 #: Return Type $;  
 sub Exists {  
     # Get the parameters.  
     my ($self, $entityName, $entityID) = @_;  
     # Check for the entity instance.  
     Trace("Checking existence of $entityName with ID=$entityID.") if T(4);  
     my $testInstance = $self->GetEntity($entityName, $entityID);  
     # Return an existence indicator.  
     my $retVal = ($testInstance ? 1 : 0);  
     return $retVal;  
 }  
   
2620  =head3 FeatureTranslation  =head3 FeatureTranslation
2621    
2622  C<< my $translation = $sprout->FeatureTranslation($featureID); >>      my $translation = $sprout->FeatureTranslation($featureID);
2623    
2624  Return the translation of a feature.  Return the translation of a feature.
2625    
# Line 2159  Line 2647 
2647    
2648  =head3 Taxonomy  =head3 Taxonomy
2649    
2650  C<< my @taxonomyList = $sprout->Taxonomy($genome); >>      my @taxonomyList = $sprout->Taxonomy($genome);
2651    
2652  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
2653  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>,
2654  or C<Eukaryote>) to sub-species. For example,  or C<Eukaryote>) to sub-species. For example,
2655    
2656  C<< (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12) >>      (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12)
2657    
2658  =over 4  =over 4
2659    
# Line 2200  Line 2688 
2688    
2689  =head3 CrudeDistance  =head3 CrudeDistance
2690    
2691  C<< my $distance = $sprout->CrudeDistance($genome1, $genome2); >>      my $distance = $sprout->CrudeDistance($genome1, $genome2);
2692    
2693  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
2694  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 2252  Line 2740 
2740    
2741  =head3 RoleName  =head3 RoleName
2742    
2743  C<< my $roleName = $sprout->RoleName($roleID); >>      my $roleName = $sprout->RoleName($roleID);
2744    
2745  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
2746  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 2286  Line 2774 
2774    
2775  =head3 RoleDiagrams  =head3 RoleDiagrams
2776    
2777  C<< my @diagrams = $sprout->RoleDiagrams($roleID); >>      my @diagrams = $sprout->RoleDiagrams($roleID);
2778    
2779  Return a list of the diagrams containing a specified functional role.  Return a list of the diagrams containing a specified functional role.
2780    
# Line 2316  Line 2804 
2804    
2805  =head3 GetProperties  =head3 GetProperties
2806    
2807  C<< my @list = $sprout->GetProperties($fid, $key, $value, $url); >>      my @list = $sprout->GetProperties($fid, $key, $value, $url);
2808    
2809  Return a list of the properties with the specified characteristics.  Return a list of the properties with the specified characteristics.
2810    
2811  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
2812  will also be associated with genomes.) A property value is represented by a 4-tuple of  passed directly to the CustomAttributes or RemoteCustomAttributes object
2813  the form B<($fid, $key, $value, $url)>. These exactly correspond to the parameter  contained in this object.
2814    
2815  =over 4  This method returns a series of tuples that match the specified criteria. Each tuple
2816    will contain an object ID, a key, and one or more values. The parameters to this
2817    method therefore correspond structurally to the values expected in each tuple. In
2818    addition, you can ask for a generic search by suffixing a percent sign (C<%>) to any
2819    of the parameters. So, for example,
2820    
2821  =item fid      my @attributeList = $sprout->GetProperties('fig|100226.1.peg.1004', 'structure%', 1, 2);
2822    
2823  ID of the feature possessing the property.  would return something like
2824    
2825  =item key      ['fig}100226.1.peg.1004', 'structure', 1, 2]
2826        ['fig}100226.1.peg.1004', 'structure1', 1, 2]
2827        ['fig}100226.1.peg.1004', 'structure2', 1, 2]
2828        ['fig}100226.1.peg.1004', 'structureA', 1, 2]
2829    
2830  Name or key of the property.  Use of C<undef> in any position acts as a wild card (all values). You can also specify
2831    a list reference in the ID column. Thus,
2832    
2833  =item value      my @attributeList = $sprout->GetProperties(['100226.1', 'fig|100226.1.%'], 'PUBMED');
2834    
2835  Value of the property.  would get the PUBMED attribute data for Streptomyces coelicolor A3(2) and all its
2836    features.
2837    
2838  =item url  In addition to values in multiple sections, a single attribute key can have multiple
2839    values, so even
2840    
2841  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.  
2842    
2843  =back  which has no wildcard in the key or the object ID, may return multiple tuples.
2844    
2845    =over 4
2846    
2847    =item objectID
2848    
2849  The parameters act as a filter for the desired data. Any non-null parameter will  ID of object whose attributes are desired. If the attributes are desired for multiple
2850  automatically match all the tuples returned. So, specifying just the I<$fid> will  objects, this parameter can be specified as a list reference. If the attributes are
2851  return all the properties of the specified feature; similarly, specifying the I<$key>  desired for all objects, specify C<undef> or an empty string. Finally, you can specify
2852  and I<$value> parameters will return all the features having the specified property  attributes for a range of object IDs by putting a percent sign (C<%>) at the end.
 value.  
2853    
2854  A single property key can have many values, representing different ideas about the  =item key
 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  
2855    
2856      my @list = $sprout->GetProperties('fig|83333.1.peg.10', 'virulence', '', '');  Attribute key name. A value of C<undef> or an empty string will match all
2857    attribute keys. If the values are desired for multiple keys, this parameter can be
2858    specified as a list reference. Finally, you can specify attributes for a range of
2859    keys by putting a percent sign (C<%>) at the end.
2860    
2861    =item values
2862    
2863    List of the desired attribute values, section by section. If C<undef>
2864    or an empty string is specified, all values in that section will match. A
2865    generic match can be requested by placing a percent sign (C<%>) at the end.
2866    In that case, all values that match up to and not including the percent sign
2867    will match. You may also specify a regular expression enclosed
2868    in slashes. All values that match the regular expression will be returned. For
2869    performance reasons, only values have this extra capability.
2870    
2871  Here the I<$value> and I<$url> fields are left blank, indicating that those fields are  =item RETURN
 not to be filtered. The tuples returned would be  
2872    
2873      ('fig|83333.1.peg.10', 'virulence', 'yes', 'http://www.somewhere.edu/first.paper.pdf')  Returns a list of tuples. The first element in the tuple is an object ID, the
2874      ('fig|83333.1.peg.10', 'virulence', 'no', 'http://www.somewhere.edu/second.paper.pdf')  second is an attribute key, and the remaining elements are the sections of
2875    the attribute value. All of the tuples will match the criteria set forth in
2876    the parameter list.
2877    
2878    =back
2879    
2880  =cut  =cut
2881  #: Return Type @@;  
2882  sub GetProperties {  sub GetProperties {
2883      # Get the parameters.      # Get the parameters.
2884      my ($self, @parms) = @_;      my ($self, @parms) = @_;
2885      # Declare the return variable.      # Declare the return variable.
2886      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;  
     }  
2887      # Return the result.      # Return the result.
2888      return @retVal;      return @retVal;
2889  }  }
2890    
2891  =head3 FeatureProperties  =head3 FeatureProperties
2892    
2893  C<< my @properties = $sprout->FeatureProperties($featureID); >>      my @properties = $sprout->FeatureProperties($featureID);
2894    
2895  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
2896  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
2897  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
2898  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
2899  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.  
2900    
2901  =over 4  =over 4
2902    
# Line 2417  Line 2906 
2906    
2907  =item RETURN  =item RETURN
2908    
2909  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.  
2910    
2911  =back  =back
2912    
# Line 2428  Line 2916 
2916      # Get the parameters.      # Get the parameters.
2917      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
2918      # Get the properties.      # Get the properties.
2919      my @retVal = $self->GetAll(['HasProperty', 'Property'], "HasProperty(from-link) = ?", [$featureID],      my @attributes = $self->{_ca}->GetAttributes($featureID);
2920                              ['Property(property-name)', 'Property(property-value)',      # Strip the feature ID off each tuple.
2921                               'HasProperty(evidence)']);      my @retVal = ();
2922        for my $attributeRow (@attributes) {
2923            shift @{$attributeRow};
2924            push @retVal, $attributeRow;
2925        }
2926      # Return the resulting list.      # Return the resulting list.
2927      return @retVal;      return @retVal;
2928  }  }
2929    
2930  =head3 DiagramName  =head3 DiagramName
2931    
2932  C<< my $diagramName = $sprout->DiagramName($diagramID); >>      my $diagramName = $sprout->DiagramName($diagramID);
2933    
2934  Return the descriptive name of a diagram.  Return the descriptive name of a diagram.
2935    
# Line 2463  Line 2955 
2955      return $retVal;      return $retVal;
2956  }  }
2957    
2958    =head3 PropertyID
2959    
2960        my $id = $sprout->PropertyID($propName, $propValue);
2961    
2962    Return the ID of the specified property name and value pair, if the
2963    pair exists. Only a small subset of the FIG attributes are stored as
2964    Sprout properties, mostly for use in search optimization.
2965    
2966    =over 4
2967    
2968    =item propName
2969    
2970    Name of the desired property.
2971    
2972    =item propValue
2973    
2974    Value expected for the desired property.
2975    
2976    =item RETURN
2977    
2978    Returns the ID of the name/value pair, or C<undef> if the pair does not exist.
2979    
2980    =back
2981    
2982    =cut
2983    
2984    sub PropertyID {
2985        # Get the parameters.
2986        my ($self, $propName, $propValue) = @_;
2987        # Try to find the ID.
2988        my ($retVal) = $self->GetFlat(['Property'],
2989                                      "Property(property-name) = ? AND Property(property-value) = ?",
2990                                      [$propName, $propValue], 'Property(id)');
2991        # Return the result.
2992        return $retVal;
2993    }
2994    
2995  =head3 MergedAnnotations  =head3 MergedAnnotations
2996    
2997  C<< my @annotationList = $sprout->MergedAnnotations(\@list); >>      my @annotationList = $sprout->MergedAnnotations(\@list);
2998    
2999  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
3000  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 2514  Line 3043 
3043    
3044  =head3 RoleNeighbors  =head3 RoleNeighbors
3045    
3046  C<< my @roleList = $sprout->RoleNeighbors($roleID); >>      my @roleList = $sprout->RoleNeighbors($roleID);
3047    
3048  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
3049  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 2557  Line 3086 
3086    
3087  =head3 FeatureLinks  =head3 FeatureLinks
3088    
3089  C<< my @links = $sprout->FeatureLinks($featureID); >>      my @links = $sprout->FeatureLinks($featureID);
3090    
3091  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
3092  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 2588  Line 3117 
3117    
3118  =head3 SubsystemsOf  =head3 SubsystemsOf
3119    
3120  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>      my %subsystems = $sprout->SubsystemsOf($featureID);
3121    
3122  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
3123  to the roles the feature performs.  to the roles the feature performs.
# Line 2636  Line 3165 
3165    
3166  =head3 SubsystemList  =head3 SubsystemList
3167    
3168  C<< my @subsystems = $sprout->SubsystemList($featureID); >>      my @subsystems = $sprout->SubsystemList($featureID);
3169    
3170  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
3171  feature participates. Unlike L</SubsystemsOf>, this method only returns the  feature participates. Unlike L</SubsystemsOf>, this method only returns the
# Line 2660  Line 3189 
3189      # Get the parameters.      # Get the parameters.
3190      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
3191      # Get the list of names.      # Get the list of names.
3192      my @retVal = $self->GetFlat(['ContainsFeature', 'HasSSCell'], "ContainsFeature(to-link) = ?",      my @retVal = $self->GetFlat(['HasRoleInSubsystem'], "HasRoleInSubsystem(from-link) = ?",
3193                                  [$featureID], 'HasSSCell(from-link)');                                  [$featureID], 'HasRoleInSubsystem(to-link)');
3194      # Return the result.      # Return the result, sorted.
3195      return @retVal;      return sort @retVal;
3196  }  }
3197    
3198    =head3 GenomeSubsystemData
3199    
3200        my %featureData = $sprout->GenomeSubsystemData($genomeID);
3201    
3202    Return a hash mapping genome features to their subsystem roles.
3203    
3204    =over 4
3205    
3206    =item genomeID
3207    
3208    ID of the genome whose subsystem feature map is desired.
3209    
3210    =item RETURN
3211    
3212    Returns a hash mapping each feature of the genome to a list of 2-tuples. Eacb
3213    2-tuple contains a subsystem name followed by a role ID.
3214    
3215    =back
3216    
3217    =cut
3218    
3219    sub GenomeSubsystemData {
3220        # Get the parameters.
3221        my ($self, $genomeID) = @_;
3222        # Declare the return variable.
3223        my %retVal = ();
3224        # Get a list of the genome features that participate in subsystems. For each
3225        # feature we get its spreadsheet cells and the corresponding roles.
3226        my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf'],
3227                                 "HasFeature(from-link) = ?", [$genomeID],
3228                                 ['HasFeature(to-link)', 'IsRoleOf(to-link)', 'IsRoleOf(from-link)']);
3229        # Now we get a list of the spreadsheet cells and their associated subsystems. Subsystems
3230        # with an unknown variant code (-1) are skipped. Note the genome ID is at both ends of the
3231        # list. We use it at the beginning to get all the spreadsheet cells for the genome and
3232        # again at the end to filter out participation in subsystems with a negative variant code.
3233        my @cellData = $self->GetAll(['IsGenomeOf', 'HasSSCell', 'ParticipatesIn'],
3234                                     "IsGenomeOf(from-link) = ? AND ParticipatesIn(variant-code) >= 0 AND ParticipatesIn(from-link) = ?",
3235                                     [$genomeID, $genomeID], ['HasSSCell(to-link)', 'HasSSCell(from-link)']);
3236        # Now "@roleData" lists the spreadsheet cell and role for each of the genome's features.
3237        # "@cellData" lists the subsystem name for each of the genome's spreadsheet cells. We
3238        # link these two lists together to create the result. First, we want a hash mapping
3239        # spreadsheet cells to subsystem names.
3240        my %subHash = map { $_->[0] => $_->[1] } @cellData;
3241        # We loop through @cellData to build the hash.
3242        for my $roleEntry (@roleData) {
3243            # Get the data for this feature and cell.
3244            my ($fid, $cellID, $role) = @{$roleEntry};
3245            # Check for a subsystem name.
3246            my $subsys = $subHash{$cellID};
3247            if ($subsys) {
3248                # Insure this feature has an entry in the return hash.
3249                if (! exists $retVal{$fid}) { $retVal{$fid} = []; }
3250                # Merge in this new data.
3251                push @{$retVal{$fid}}, [$subsys, $role];
3252            }
3253        }
3254        # Return the result.
3255        return %retVal;
3256    }
3257    
3258  =head3 RelatedFeatures  =head3 RelatedFeatures
3259    
3260  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>      my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID);
3261    
3262  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
3263  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 2703  Line 3290 
3290      # Get the parameters.      # Get the parameters.
3291      my ($self, $featureID, $function, $userID) = @_;      my ($self, $featureID, $function, $userID) = @_;
3292      # Get a list of the features that are BBHs of the incoming feature.      # Get a list of the features that are BBHs of the incoming feature.
3293      my @bbhFeatures = $self->GetFlat(['IsBidirectionalBestHitOf'],      my @bbhFeatures = map { $_->[0] } FIGRules::BBHData($featureID);
                                      "IsBidirectionalBestHitOf(from-link) = ?", [$featureID],  
                                      'IsBidirectionalBestHitOf(to-link)');  
3294      # Now we loop through the features, pulling out the ones that have the correct      # Now we loop through the features, pulling out the ones that have the correct
3295      # functional assignment.      # functional assignment.
3296      my @retVal = ();      my @retVal = ();
# Line 2723  Line 3308 
3308    
3309  =head3 TaxonomySort  =head3 TaxonomySort
3310    
3311  C<< my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs); >>      my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs);
3312    
3313  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
3314  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 2758  Line 3343 
3343          my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",          my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",
3344                                          [$fid], 'Genome(taxonomy)');                                          [$fid], 'Genome(taxonomy)');
3345          # Add this feature to the hash buffer.          # Add this feature to the hash buffer.
3346          Tracer::AddToListMap(\%hashBuffer, $taxonomy, $fid);          push @{$hashBuffer{$taxonomy}}, $fid;
3347      }      }
3348      # Sort the keys and get the elements.      # Sort the keys and get the elements.
3349      my @retVal = ();      my @retVal = ();
# Line 2771  Line 3356 
3356    
3357  =head3 Protein  =head3 Protein
3358    
3359  C<< my $protein = Sprout::Protein($sequence, $table); >>      my $protein = Sprout::Protein($sequence, $table);
3360    
3361  Translate a DNA sequence into a protein sequence.  Translate a DNA sequence into a protein sequence.
3362    
# Line 2841  Line 3426 
3426      # Loop through the input triples.      # Loop through the input triples.
3427      my $n = length $sequence;      my $n = length $sequence;
3428      for (my $i = 0; $i < $n; $i += 3) {      for (my $i = 0; $i < $n; $i += 3) {
3429          # Get the current triple from the sequence.          # Get the current triple from the sequence. Note we convert to
3430          my $triple = substr($sequence, $i, 3);          # upper case to insure a match.
3431            my $triple = uc substr($sequence, $i, 3);
3432          # Translate it using the table.          # Translate it using the table.
3433          my $protein = "X";          my $protein = "X";
3434          if (exists $table->{$triple}) { $protein = $table->{$triple}; }          if (exists $table->{$triple}) { $protein = $table->{$triple}; }
# Line 2856  Line 3442 
3442    
3443  =head3 LoadInfo  =head3 LoadInfo
3444    
3445  C<< my ($dirName, @relNames) = $sprout->LoadInfo(); >>      my ($dirName, @relNames) = $sprout->LoadInfo();
3446    
3447  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
3448  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 2875  Line 3461 
3461      return @retVal;      return @retVal;
3462  }  }
3463    
3464    =head3 BBHMatrix
3465    
3466        my %bbhMap = $sprout->BBHMatrix($genomeID, $cutoff, @targets);
3467    
3468    Find all the bidirectional best hits for the features of a genome in a
3469    specified list of target genomes. The return value will be a hash mapping
3470    features in the original genome to their bidirectional best hits in the
3471    target genomes.
3472    
3473    =over 4
3474    
3475    =item genomeID
3476    
3477    ID of the genome whose features are to be examined for bidirectional best hits.
3478    
3479    =item cutoff
3480    
3481    A cutoff value. Only hits with a score lower than the cutoff will be returned.
3482    
3483    =item targets
3484    
3485    List of target genomes. Only pairs originating in the original
3486    genome and landing in one of the target genomes will be returned.
3487    
3488    =item RETURN
3489    
3490    Returns a hash mapping each feature in the original genome to a hash mapping its
3491    BBH pegs in the target genomes to their scores.
3492    
3493    =back
3494    
3495    =cut
3496    
3497    sub BBHMatrix {
3498        # Get the parameters.
3499        my ($self, $genomeID, $cutoff, @targets) = @_;
3500        # Declare the return variable.
3501        my %retVal = ();
3502        # Ask for the BBHs.
3503        my @bbhList = FIGRules::BatchBBHs("fig|$genomeID.%", $cutoff, @targets);
3504        # We now have a set of 4-tuples that we need to convert into a hash of hashes.
3505        for my $bbhData (@bbhList) {
3506            my ($peg1, $peg2, $score) = @{$bbhData};
3507            if (! exists $retVal{$peg1}) {
3508                $retVal{$peg1} = { $peg2 => $score };
3509            } else {
3510                $retVal{$peg1}->{$peg2} = $score;
3511            }
3512        }
3513        # Return the result.
3514        return %retVal;
3515    }
3516    
3517    
3518    =head3 SimMatrix
3519    
3520        my %simMap = $sprout->SimMatrix($genomeID, $cutoff, @targets);
3521    
3522    Find all the similarities for the features of a genome in a
3523    specified list of target genomes. The return value will be a hash mapping
3524    features in the original genome to their similarites in the
3525    target genomes.
3526    
3527    =over 4
3528    
3529    =item genomeID
3530    
3531    ID of the genome whose features are to be examined for similarities.
3532    
3533    =item cutoff
3534    
3535    A cutoff value. Only hits with a score lower than the cutoff will be returned.
3536    
3537    =item targets
3538    
3539    List of target genomes. Only pairs originating in the original
3540    genome and landing in one of the target genomes will be returned.
3541    
3542    =item RETURN
3543    
3544    Returns a hash mapping each feature in the original genome to a hash mapping its
3545    similar pegs in the target genomes to their scores.
3546    
3547    =back
3548    
3549    =cut
3550    
3551    sub SimMatrix {
3552        # Get the parameters.
3553        my ($self, $genomeID, $cutoff, @targets) = @_;
3554        # Declare the return variable.
3555        my %retVal = ();
3556        # Get the list of features in the source organism.
3557        my @fids = $self->FeaturesOf($genomeID);
3558        # Ask for the sims. We only want similarities to fig features.
3559        my $simList = FIGRules::GetNetworkSims($self, \@fids, {}, 1000, $cutoff, "fig");
3560        if (! defined $simList) {
3561            Confess("Unable to retrieve similarities from server.");
3562        } else {
3563            Trace("Processing sims.") if T(3);
3564            # We now have a set of sims that we need to convert into a hash of hashes. First, we
3565            # Create a hash for the target genomes.
3566            my %targetHash = map { $_ => 1 } @targets;
3567            for my $simData (@{$simList}) {
3568                # Get the PEGs and the score.
3569                my ($peg1, $peg2, $score) = ($simData->id1, $simData->id2, $simData->psc);
3570                # Insure the second ID is in the target list.
3571                my ($genome2) = FIGRules::ParseFeatureID($peg2);
3572                if (exists $targetHash{$genome2}) {
3573                    # Here it is. Now we need to add it to the return hash. How we do that depends
3574                    # on whether or not $peg1 is new to us.
3575                    if (! exists $retVal{$peg1}) {
3576                        $retVal{$peg1} = { $peg2 => $score };
3577                    } else {
3578                        $retVal{$peg1}->{$peg2} = $score;
3579                    }
3580                }
3581            }
3582        }
3583        # Return the result.
3584        return %retVal;
3585    }
3586    
3587    
3588  =head3 LowBBHs  =head3 LowBBHs
3589    
3590  C<< my %bbhMap = $sprout->LowBBHs($featureID, $cutoff); >>      my %bbhMap = $sprout->LowBBHs($featureID, $cutoff);
3591    
3592  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
3593  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 2906  Line 3616 
3616      my ($self, $featureID, $cutoff) = @_;      my ($self, $featureID, $cutoff) = @_;
3617      # Create the return hash.      # Create the return hash.
3618      my %retVal = ();      my %retVal = ();
3619      # Create a query to get the desired BBHs.      # Query for the desired BBHs.
3620      my @bbhList = $self->GetAll(['IsBidirectionalBestHitOf'],      my @bbhList = FIGRules::BBHData($featureID, $cutoff);
                                 'IsBidirectionalBestHitOf(sc) <= ? AND IsBidirectionalBestHitOf(from-link) = ?',  
                                 [$cutoff, $featureID],  
                                 ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(sc)']);  
3621      # Form the results into the return hash.      # Form the results into the return hash.
3622      for my $pair (@bbhList) {      for my $pair (@bbhList) {
3623          $retVal{$pair->[0]} = $pair->[1];          my $fid = $pair->[0];
3624            if ($self->Exists('Feature', $fid)) {
3625                $retVal{$fid} = $pair->[1];
3626            }
3627      }      }
3628      # Return the result.      # Return the result.
3629      return %retVal;      return %retVal;
3630  }  }
3631    
3632    =head3 Sims
3633    
3634        my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters);
3635    
3636    Get a list of similarities for a specified feature. Similarity information is not kept in the
3637    Sprout database; rather, they are retrieved from a network server. The similarities are
3638    returned as B<Sim> objects. A Sim object is actually a list reference that has been blessed
3639    so that its elements can be accessed by name.
3640    
3641    Similarities can be either raw or expanded. The raw similarities are basic
3642    hits between features with similar DNA. Expanding a raw similarity drags in any
3643    features considered substantially identical. So, for example, if features B<A1>,
3644    B<A2>, and B<A3> are all substantially identical to B<A>, then a raw similarity
3645    B<[C,A]> would be expanded to B<[C,A] [C,A1] [C,A2] [C,A3]>.
3646    
3647    =over 4
3648    
3649    =item fid
3650    
3651    ID of the feature whose similarities are desired, or reference to a list of IDs
3652    of features whose similarities are desired.
3653    
3654    =item maxN
3655    
3656    Maximum number of similarities to return.
3657    
3658    =item maxP
3659    
3660    Minumum allowable similarity score.
3661    
3662    =item select
3663    
3664    Selection criterion: C<raw> means only raw similarities are returned; C<fig>
3665    means only similarities to FIG features are returned; C<all> means all expanded
3666    similarities are returned; and C<figx> means similarities are expanded until the
3667    number of FIG features equals the maximum.
3668    
3669    =item max_expand
3670    
3671    The maximum number of features to expand.
3672    
3673    =item filters
3674    
3675    Reference to a hash containing filter information, or a subroutine that can be
3676    used to filter the sims.
3677    
3678    =item RETURN
3679    
3680    Returns a reference to a list of similarity objects, or C<undef> if an error
3681    occurred.
3682    
3683    =back
3684    
3685    =cut
3686    
3687    sub Sims {
3688        # Get the parameters.
3689        my ($self, $fid, $maxN, $maxP, $select, $max_expand, $filters) = @_;
3690        # Create the shim object to test for deleted FIDs.
3691        my $shim = FidCheck->new($self);
3692        # Ask the network for sims.
3693        my $retVal = FIGRules::GetNetworkSims($shim, $fid, {}, $maxN, $maxP, $select, $max_expand, $filters);
3694        # Return the result.
3695        return $retVal;
3696    }
3697    
3698    =head3 IsAllGenomes
3699    
3700        my $flag = $sprout->IsAllGenomes(\@list, \@checkList);
3701    
3702    Return TRUE if all genomes in the second list are represented in the first list at
3703    least one. Otherwise, return FALSE. If the second list is omitted, the first list is
3704    compared to a list of all the genomes.
3705    
3706    =over 4
3707    
3708    =item list
3709    
3710    Reference to the list to be compared to the second list.
3711    
3712    =item checkList (optional)
3713    
3714    Reference to the comparison target list. Every genome ID in this list must occur at
3715    least once in the first list. If this parameter is omitted, a list of all the genomes
3716    is used.
3717    
3718    =item RETURN
3719    
3720    Returns TRUE if every item in the second list appears at least once in the
3721    first list, else FALSE.
3722    
3723    =back
3724    
3725    =cut
3726    
3727    sub IsAllGenomes {
3728        # Get the parameters.
3729        my ($self, $list, $checkList) = @_;
3730        # Supply the checklist if it was omitted.
3731        $checkList = [$self->Genomes()] if ! defined($checkList);
3732        # Create a hash of the original list.
3733        my %testList = map { $_ => 1 } @{$list};
3734        # Declare the return variable. We assume that the representation
3735        # is complete and stop at the first failure.
3736        my $retVal = 1;
3737        my $n = scalar @{$checkList};
3738        for (my $i = 0; $retVal && $i < $n; $i++) {
3739            if (! $testList{$checkList->[$i]}) {
3740                $retVal = 0;
3741            }
3742        }
3743        # Return the result.
3744        return $retVal;
3745    }
3746    
3747  =head3 GetGroups  =head3 GetGroups
3748    
3749  C<< my %groups = $sprout->GetGroups(\@groupList); >>      my %groups = $sprout->GetGroups(\@groupList);
3750    
3751  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.
3752  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 2940  Line 3765 
3765          # Here we have a group list. Loop through them individually,          # Here we have a group list. Loop through them individually,
3766          # getting a list of the relevant genomes.          # getting a list of the relevant genomes.
3767          for my $group (@{$groupList}) {          for my $group (@{$groupList}) {
3768              my @genomeIDs = $self->GetFlat(['Genome'], "Genome(group-name) = ?",              my @genomeIDs = $self->GetFlat(['Genome'], "Genome(primary-group) = ?",
3769                  [$group], "Genome(id)");                  [$group], "Genome(id)");
3770              $retVal{$group} = \@genomeIDs;              $retVal{$group} = \@genomeIDs;
3771          }          }
# Line 2948  Line 3773 
3773          # Here we need all of the groups. In this case, we run through all          # Here we need all of the groups. In this case, we run through all
3774          # of the genome records, putting each one found into the appropriate          # of the genome records, putting each one found into the appropriate
3775          # group. Note that we use a filter clause to insure that only genomes          # group. Note that we use a filter clause to insure that only genomes
3776          # in groups are included in the return set.          # in real NMPDR groups are included in the return set.
3777          my @genomes = $self->GetAll(['Genome'], "Genome(group-name) > ' '", [],          my @genomes = $self->GetAll(['Genome'], "Genome(primary-group) <> ?",
3778                                      ['Genome(id)', 'Genome(group-name)']);                                      [$FIG_Config::otherGroup], ['Genome(id)', 'Genome(primary-group)']);
3779          # Loop through the genomes found.          # Loop through the genomes found.
3780          for my $genome (@genomes) {          for my $genome (@genomes) {
3781              # Pop this genome's ID off the current list.              # Get the genome ID and group, and add this genome to the group's list.
3782              my @groups = @{$genome};              my ($genomeID, $group) = @{$genome};
3783              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);  
             }  
3784          }          }
3785      }      }
3786      # Return the hash we just built.      # Return the hash we just built.
# Line 2969  Line 3789 
3789    
3790  =head3 MyGenomes  =head3 MyGenomes
3791    
3792  C<< my @genomes = Sprout::MyGenomes($dataDir); >>      my @genomes = Sprout::MyGenomes($dataDir);
3793    
3794  Return a list of the genomes to be included in the Sprout.  Return a list of the genomes to be included in the Sprout.
3795    
# Line 3001  Line 3821 
3821    
3822  =head3 LoadFileName  =head3 LoadFileName
3823    
3824  C<< my $fileName = Sprout::LoadFileName($dataDir, $tableName); >>      my $fileName = Sprout::LoadFileName($dataDir, $tableName);
3825    
3826  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
3827  directory.  directory.
# Line 3042  Line 3862 
3862    
3863  =head3 DeleteGenome  =head3 DeleteGenome
3864    
3865  C<< my $stats = $sprout->DeleteGenome($genomeID, $testFlag); >>      my $stats = $sprout->DeleteGenome($genomeID, $testFlag);
3866    
3867  Delete a genome from the database.  Delete a genome from the database.
3868    
# Line 3068  Line 3888 
3888      # Get the parameters.      # Get the parameters.
3889      my ($self, $genomeID, $testFlag) = @_;      my ($self, $genomeID, $testFlag) = @_;
3890      # Perform the delete for the genome's features.      # Perform the delete for the genome's features.
3891      my $retVal = $self->Delete('Feature', "fig|$genomeID.%", $testFlag);      my $retVal = $self->Delete('Feature', "fig|$genomeID.%", testMode => $testFlag);
3892      # Perform the delete for the primary genome data.      # Perform the delete for the primary genome data.
3893      my $stats = $self->Delete('Genome', $genomeID, $testFlag);      my $stats = $self->Delete('Genome', $genomeID, testMode => $testFlag);
3894      $retVal->Accumulate($stats);      $retVal->Accumulate($stats);
3895      # Return the result.      # Return the result.
3896      return $retVal;      return $retVal;
3897  }  }
3898    
3899    =head3 Fix
3900    
3901        my %fixedHash = $sprout->Fix(%groupHash);
3902    
3903    Prepare a genome group hash (like that returned by L</GetGroups>) for processing.
3904    The groups will be combined into the appropriate super-groups.
3905    
3906    =over 4
3907    
3908    =item groupHash
3909    
3910    Hash to be fixed up.
3911    
3912    =item RETURN
3913    
3914    Returns a fixed-up version of the hash.
3915    
3916    =back
3917    
3918    =cut
3919    
3920    sub Fix {
3921        # Get the parameters.
3922        my ($self, %groupHash) = @_;
3923        # Create the result hash.
3924        my %retVal = ();
3925        # Copy over the genomes.
3926        for my $groupID (keys %groupHash) {
3927            # Get the super-group name.
3928            my $realGroupID = $self->SuperGroup($groupID);
3929            # Append this group's genomes into the result hash
3930            # using the super-group name.
3931            push @{$retVal{$realGroupID}}, @{$groupHash{$groupID}};
3932        }
3933        # Return the result hash.
3934        return %retVal;
3935    }
3936    
3937    =head3 GroupPageName
3938    
3939        my $name = $sprout->GroupPageName($group);
3940    
3941    Return the name of the page for the specified NMPDR group.
3942    
3943    =over 4
3944    
3945    =item group
3946    
3947    Name of the relevant group.
3948    
3949    =item RETURN
3950    
3951    Returns the relative page name (e.g. C<../content/campy.php>). If the group file is not in
3952    memory it will be read in.
3953    
3954    =back
3955    
3956    =cut
3957    
3958    sub GroupPageName {
3959        # Get the parameters.
3960        my ($self, $group) = @_;
3961        # Check for the group file data.
3962        my %superTable = $self->CheckGroupFile();
3963        # Compute the real group name.
3964        my $realGroup = $self->SuperGroup($group);
3965        # Get the associated page name.
3966        my $retVal = "../content/$superTable{$realGroup}->{page}";
3967        # Return the result.
3968        return $retVal;
3969    }
3970    
3971    
3972    =head3 AddProperty
3973    
3974        $sprout->AddProperty($featureID, $key, @values);
3975    
3976    Add a new attribute value (Property) to a feature.
3977    
3978    =over 4
3979    
3980    =item peg
3981    
3982    ID of the feature to which the attribute is to be added.
3983    
3984    =item key
3985    
3986    Name of the attribute (key).
3987    
3988    =item values
3989    
3990    Values of the attribute.
3991    
3992    =back
3993    
3994    =cut
3995    #: Return Type ;
3996    sub AddProperty {
3997        # Get the parameters.
3998        my ($self, $featureID, $key, @values) = @_;
3999        # Add the property using the attached attributes object.
4000        $self->{_ca}->AddAttribute($featureID, $key, @values);
4001    }
4002    
4003    =head3 CheckGroupFile
4004    
4005        my %groupData = $sprout->CheckGroupFile();
4006    
4007    Get the group file hash. The group file hash describes the relationship
4008    between a group and the super-group to which it belongs for purposes of
4009    display. The super-group name is computed from the first capitalized word
4010    in the actual group name. For each super-group, the group file contains
4011    the page name and a list of the species expected to be in the group.
4012    Each species is specified by a genus and a species name. A species name
4013    of C<0> implies an entire genus.
4014    
4015    This method returns a hash from super-group names to a hash reference. Each
4016    resulting hash reference contains the following fields.
4017    
4018    =over 4
4019    
4020    =item page
4021    
4022    The super-group's web page in the NMPDR.
4023    
4024    =item contents
4025    
4026    A list of 2-tuples, each containing a genus name followed by a species name
4027    (or 0, indicating all species). This list indicates which organisms belong
4028    in the super-group.
4029    
4030    =back
4031    
4032    =cut
4033    
4034    sub CheckGroupFile {
4035        # Get the parameters.
4036        my ($self) = @_;
4037        # Check to see if we already have this hash.
4038        if (! defined $self->{groupHash}) {
4039            # We don't, so we need to read it in.
4040            my %groupHash;
4041            # Read the group file.
4042            my @groupLines = Tracer::GetFile("$FIG_Config::sproutData/groups.tbl");
4043            # Loop through the list of sort-of groups.
4044            for my $groupLine (@groupLines) {
4045                my ($name, $page, @contents) = split /\t/, $groupLine;
4046                $groupHash{$name} = { page => $page,
4047                                      contents => [ map { [ split /\s*,\s*/, $_ ] } @contents ]
4048                                    };
4049            }
4050            # Save the hash.
4051            $self->{groupHash} = \%groupHash;
4052        }
4053        # Return the result.
4054        return %{$self->{groupHash}};
4055    }
4056    
4057    =head2 Virtual Methods
4058    
4059    =head3 CleanKeywords
4060    
4061        my $cleanedString = $sprout->CleanKeywords($searchExpression);
4062    
4063    Clean up a search expression or keyword list. This involves converting the periods
4064    in EC numbers to underscores, converting non-leading minus signs to underscores,
4065    a vertical bar or colon to an apostrophe, and forcing lower case for all alphabetic
4066    characters. In addition, any extra spaces are removed.
4067    
4068    =over 4
4069    
4070    =item searchExpression
4071    
4072    Search expression or keyword list to clean. Note that a search expression may
4073    contain boolean operators which need to be preserved. This includes leading
4074    minus signs.
4075    
4076    =item RETURN
4077    
4078    Cleaned expression or keyword list.
4079    
4080    =back
4081    
4082    =cut
4083    
4084    sub CleanKeywords {
4085        # Get the parameters.
4086        my ($self, $searchExpression) = @_;
4087        # Perform the standard cleanup.
4088        my $words = $self->ERDB::CleanKeywords($searchExpression);
4089        # Fix the periods in EC and TC numbers.
4090        $words =~ s/(\d+|\-)\.(\d+|-)\.(\d+|-)\.(\d+|-)/$1_$2_$3_$4/g;
4091        # Fix non-trailing periods.
4092        $words =~ s/\.(\w)/_$1/g;
4093        # Fix non-leading minus signs.
4094        $words =~ s/(\w)[\-]/$1_/g;
4095        # Fix the vertical bars and colons
4096        $words =~ s/(\w)[|:](\w)/$1'$2/g;
4097        # Now split up the list so that each keyword is in its own string. We keep the delimiters
4098        # because they may contain boolean expression data.
4099        my @words = split /([^A-Za-z'0-9_]+)/, $words;
4100        # We'll convert the stemmable words into stems and re-assemble the result.
4101        my $retVal = "";
4102        for my $word (@words) {
4103            my $stem = $self->Stem($word);
4104            if (defined $stem) {
4105                $retVal .= $stem;
4106            } else {
4107                $retVal .= $word;
4108            }
4109        }
4110        Trace("Cleaned keyword list for \"$searchExpression\" is \"$retVal\".") if T(3);
4111        # Return the result.
4112        return $retVal;
4113    }
4114    
4115  =head2 Internal Utility Methods  =head2 Internal Utility Methods
4116    
4117  =head3 ParseAssignment  =head3 ParseAssignment
# Line 3086  Line 4122 
4122    
4123  A functional assignment is always of the form  A functional assignment is always of the form
4124    
4125      C<set >I<YYYY>C< function to\n>I<ZZZZZ>      set YYYY function to
4126        ZZZZ
4127    
4128  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,
4129  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 3132  Line 4169 
4169      }      }
4170      # If we have an assignment, we need to clean the function text. There may be      # If we have an assignment, we need to clean the function text. There may be
4171      # extra junk at the end added as a note from the user.      # extra junk at the end added as a note from the user.
4172      if (@retVal) {      if (defined( $retVal[1] )) {
4173          $retVal[1] =~ s/(\t\S)?\s*$//;          $retVal[1] =~ s/(\t\S)?\s*$//;
4174      }      }
4175      # Return the result list.      # Return the result list.
4176      return @retVal;      return @retVal;
4177  }  }
4178    
4179  =head3 FriendlyTimestamp  =head3 _CheckFeature
4180    
4181  Convert a time number to a user-friendly time stamp for display.      my $flag = $sprout->_CheckFeature($fid);
4182    
4183  This is a static method.  Return TRUE if the specified FID is probably an NMPDR feature ID, else FALSE.
4184    
4185  =over 4  =over 4
4186    
4187  =item timeValue  =item fid
4188    
4189  Numeric time value.  Feature ID to check.
4190    
4191  =item RETURN  =item RETURN
4192    
4193  Returns a string containing the same time in user-readable format.  Returns TRUE if the FID is for one of the NMPDR genomes, else FALSE.
4194    
4195  =back  =back
4196    
4197  =cut  =cut
4198    
4199  sub FriendlyTimestamp {  sub _CheckFeature {
4200      my ($timeValue) = @_;      # Get the parameters.
4201      my $retVal = localtime($timeValue);      my ($self, $fid) = @_;
4202      return $retVal;      # Insure we have a genome hash.
4203        if (! defined $self->{genomeHash}) {
4204            my %genomeHash = map { $_ => 1 } $self->GetFlat(['Genome'], "", [], 'Genome(id)');
4205            $self->{genomeHash} = \%genomeHash;
4206        }
4207        # Get the feature's genome ID.
4208        my ($genomeID) = FIGRules::ParseFeatureID($fid);
4209        # Return an indicator of whether or not the genome ID is in the hash.
4210        return ($self->{genomeHash}->{$genomeID} ? 1 : 0);
4211  }  }
4212    
4213  =head3 AddProperty  =head3 FriendlyTimestamp
4214    
4215  C<< my  = $sprout->AddProperty($featureID, $key, $value, $url); >>  Convert a time number to a user-friendly time stamp for display.
4216    
4217  Add a new attribute value (Property) to a feature. In the SEED system, attributes can  This is a static method.
 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.  
4218    
4219  =over 4  =over 4
4220    
4221  =item peg  =item timeValue
   
 ID of the feature to which the attribute is to be replied.  
   
 =item key  
   
 Name of the attribute (key).  
   
 =item value  
4222    
4223  Value of the attribute.  Numeric time value.
4224    
4225  =item url  =item RETURN
4226    
4227  URL or text citation from which the property was obtained.  Returns a string containing the same time in user-readable format.
4228    
4229  =back  =back
4230    
4231  =cut  =cut
4232  #: Return Type ;  
4233  sub AddProperty {  sub FriendlyTimestamp {
4234      # Get the parameters.      my ($timeValue) = @_;
4235      my ($self, $featureID, $key, $value, $url) = @_;      my $retVal = localtime($timeValue);
4236      # Declare the variable to hold the desired property ID.      return $retVal;
     my $propID;  
     # Attempt to find a property record for this key/value pair.  
     my @properties = $self->GetFlat(['Property'],  
                                    "Property(property-name) = ? AND Property(property-value) = ?",  
                                    [$key, $value], 'Property(id)');  
     if (@properties) {  
         # Here the property is already in the database. We save its ID.  
         $propID = $properties[0];  
         # Here the property value does not exist. We need to generate an ID. It will be set  
         # to a number one greater than the maximum value in the database. This call to  
         # GetAll will stop after one record.  
         my @maxProperty = $self->GetAll(['Property'], "ORDER BY Property(id) DESC", [], ['Property(id)'],  
                                         1);  
         $propID = $maxProperty[0]->[0] + 1;  
         # Insert the new property value.  
         $self->Insert('Property', { 'property-name' => $key, 'property-value' => $value, id => $propID });  
     }  
     # Now we connect the incoming feature to the property.  
     $self->Insert('HasProperty', { 'from-link' => $featureID, 'to-link' => $propID, evidence => $url });  
4237  }  }
4238    
4239    

Legend:
Removed from v.1.61  
changed lines
  Added in v.1.111

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3