[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.104, Wed Jan 23 00:56:31 2008 UTC revision 1.126, Thu Apr 2 01:47:00 2009 UTC
# Line 4  Line 4 
4      use strict;      use strict;
5      use DBKernel;      use DBKernel;
6      use XML::Simple;      use XML::Simple;
7      use DBQuery;      use ERDBQuery;
8      use ERDBObject;      use ERDBObject;
9      use Tracer;      use Tracer;
10      use FIGRules;      use FIGRules;
# Line 14  Line 14 
14      use BasicLocation;      use BasicLocation;
15      use CustomAttributes;      use CustomAttributes;
16      use RemoteCustomAttributes;      use RemoteCustomAttributes;
17        use CGI qw(-nosticky);
18        use WikiTools;
19        use BioWords;
20      use base qw(ERDB);      use base qw(ERDB);
21    
22  =head1 Sprout Database Manipulation Object  =head1 Sprout Database Manipulation Object
# Line 38  Line 41 
41    
42  =cut  =cut
43    
 #: Constructor SFXlate->new_sprout_only();  
   
44  =head2 Public Methods  =head2 Public Methods
45    
46  =head3 new  =head3 new
# Line 54  Line 55 
55    
56  =item dbName  =item dbName
57    
58  Name of the database.  Name of the database. If omitted, the default Sprout database name is used.
59    
60  =item options  =item options
61    
# Line 78  Line 79 
79    
80  * B<noDBOpen> suppresses the connection to the database if TRUE, else FALSE  * B<noDBOpen> suppresses the connection to the database if TRUE, else FALSE
81    
82    * B<host> name of the database host
83    
84  =back  =back
85    
86  For example, the following constructor call specifies a database named I<Sprout> and a user name of  For example, the following constructor call specifies a database named I<Sprout> and a user name of
# Line 86  Line 89 
89    
90      my $sprout = Sprout->new('Sprout', { userData => 'fig/admin', dataDir => '/usr/fig/SproutData' });      my $sprout = Sprout->new('Sprout', { userData => 'fig/admin', dataDir => '/usr/fig/SproutData' });
91    
92    In order to work properly with [[ERDBGeneratorPl]], the constructor has an alternate
93    form.
94    
95        my $sprout = Sprout->new(dbd => $filename);
96    
97    Where I<$fileName> is the name of the DBD file. This enables us to specify an alternate
98    DBD for the loader, which is important when the database format changes.
99    
100  =cut  =cut
101    
102  sub new {  sub new {
103      # Get the parameters.      # Get the parameters.
104      my ($class, $dbName, $options) = @_;      my ($class, $dbName, $options) = @_;
105        # Check for the alternate signature, and default the database name if it is missing.
106        if ($dbName eq 'dbd') {
107            $dbName = $FIG_Config::sproutDB;
108            $options = { xmlFileName => $options };
109        } elsif (! defined $dbName) {
110            $dbName = $FIG_Config::sproutDB;
111        } elsif (ref $dbName eq 'HASH') {
112            $options = $dbName;
113            $dbName = $FIG_Config::sproutDB;
114        }
115      # Compute the DBD directory.      # Compute the DBD directory.
116      my $dbd_dir = (defined($FIG_Config::dbd_dir) ? $FIG_Config::dbd_dir :      my $dbd_dir = (defined($FIG_Config::dbd_dir) ? $FIG_Config::dbd_dir :
117                                                    $FIG_Config::fig );                                                    $FIG_Config::fig );
# Line 103  Line 124 
124                                                          # data file directory                                                          # data file directory
125                         xmlFileName  => "$dbd_dir/SproutDBD.xml",                         xmlFileName  => "$dbd_dir/SproutDBD.xml",
126                                                          # database definition file name                                                          # database definition file name
127                         userData     => "$FIG_Config::dbuser/$FIG_Config::dbpass",                         userData     => "$FIG_Config::sproutUser/$FIG_Config::sproutPass",
128                                                          # user name and password                                                          # user name and password
129                         port         => $FIG_Config::dbport,                         port         => $FIG_Config::sproutPort,
130                                                          # database connection port                                                          # database connection port
131                         sock         => $FIG_Config::dbsock,                         sock         => $FIG_Config::sproutSock,
132                         host         => $FIG_Config::dbhost,                         host         => $FIG_Config::sprout_host,
133                         maxSegmentLength => 4500,        # maximum feature segment length                         maxSegmentLength => 4500,        # maximum feature segment length
134                         maxSequenceLength => 8000,       # maximum contig sequence length                         maxSequenceLength => 8000,       # maximum contig sequence length
135                         noDBOpen     => 0,               # 1 to suppress the database open                         noDBOpen     => 0,               # 1 to suppress the database open
136                           demandDriven => 0,               # 1 for forward-only queries
137                        }, $options);                        }, $options);
138      # Get the data directory.      # Get the data directory.
139      my $dataDir = $optionTable->{dataDir};      my $dataDir = $optionTable->{dataDir};
# Line 121  Line 143 
143      # Connect to the database.      # Connect to the database.
144      my $dbh;      my $dbh;
145      if (! $optionTable->{noDBOpen}) {      if (! $optionTable->{noDBOpen}) {
146            Trace("Connect data: host = $optionTable->{host}, port = $optionTable->{port}.") if T(3);
147          $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,          $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,
148                                  $password, $optionTable->{port}, $optionTable->{host}, $optionTable->{sock});                                  $password, $optionTable->{port}, $optionTable->{host}, $optionTable->{sock});
149      }      }
150      # Create the ERDB object.      # Create the ERDB object.
151      my $xmlFileName = "$optionTable->{xmlFileName}";      my $xmlFileName = "$optionTable->{xmlFileName}";
152      my $retVal = ERDB::new($class, $dbh, $xmlFileName);      my $retVal = ERDB::new($class, $dbh, $xmlFileName, %$optionTable);
153      # Add the option table and XML file name.      # Add the option table and XML file name.
154      $retVal->{_options} = $optionTable;      $retVal->{_options} = $optionTable;
155      $retVal->{_xmlName} = $xmlFileName;      $retVal->{_xmlName} = $xmlFileName;
156      # Set up space for the group file data.      # Set up space for the group file data.
157      $retVal->{groupHash} = undef;      $retVal->{groupHash} = undef;
158      # Set up space for the genome hash. We use this to identify NMPDR genomes.      # Set up space for the genome hash. We use this to identify NMPDR genomes
159      $retVal->{genomeHash} = undef;      # and remember genome data.
160      # Connect to the attributes.      $retVal->{genomeHash} = {};
161        $retVal->{genomeHashFilled} = 0;
162        # Remember the data directory name.
163        $retVal->{dataDir} = $dataDir;
164        # Return it.
165        return $retVal;
166    }
167    
168    =head3 ca
169    
170        my $ca = $sprout->ca():;
171    
172    Return the [[CustomAttributesPm]] object for retrieving object
173    properties.
174    
175    =cut
176    
177    sub ca {
178        # Get the parameters.
179        my ($self) = @_;
180        # Do we already have an attribute object?
181        my $retVal = $self->{_ca};
182        if (! defined $retVal) {
183            # No, create one. How we do it depends on the configuration.
184      if ($FIG_Config::attrURL) {      if ($FIG_Config::attrURL) {
185          Trace("Remote attribute server $FIG_Config::attrURL chosen.") if T(3);          Trace("Remote attribute server $FIG_Config::attrURL chosen.") if T(3);
186          $retVal->{_ca} = RemoteCustomAttributes->new($FIG_Config::attrURL);              $retVal = RemoteCustomAttributes->new($FIG_Config::attrURL);
187      } elsif ($FIG_Config::attrDbName) {      } elsif ($FIG_Config::attrDbName) {
188          Trace("Local attribute database $FIG_Config::attrDbName chosen.") if T(3);          Trace("Local attribute database $FIG_Config::attrDbName chosen.") if T(3);
189          my $user = ($FIG_Config::arch eq 'win' ? 'self' : scalar(getpwent()));          my $user = ($FIG_Config::arch eq 'win' ? 'self' : scalar(getpwent()));
190          $retVal->{_ca} = CustomAttributes->new(user => $user);              $retVal = CustomAttributes->new(user => $user);
191      }      }
192      # Return it.          # Save it for next time.
193            $self->{_ca} = $retVal;
194        }
195        # Return the result.
196        return $retVal;
197    }
198    
199    =head3 CoreGenomes
200    
201        my @genomes = $sprout->CoreGenomes($scope);
202    
203    Return the IDs of NMPDR genomes in the specified scope.
204    
205    =over 4
206    
207    =item scope
208    
209    Scope of the desired genomes. C<core> covers the original core genomes,
210    C<nmpdr> covers all genomes in NMPDR groups, and C<all> covers all
211    genomes in the system.
212    
213    =item RETURN
214    
215    Returns a list of the IDs for the genomes in the specified scope.
216    
217    =back
218    
219    =cut
220    
221    sub CoreGenomes {
222        # Get the parameters.
223        my ($self, $scope) = @_;
224        # Declare the return variable.
225        my @retVal = ();
226        # If we want all genomes, then this is easy.
227        if ($scope eq 'all') {
228            @retVal = $self->Genomes();
229        } else {
230            # Here we're dealing with groups. Get the hash of all the
231            # genome groups.
232            my %groups = $self->GetGroups();
233            # Loop through the groups, keeping the ones that we want.
234            for my $group (keys %groups) {
235                # Decide if we want to keep this group.
236                my $keepGroup = 0;
237                if ($scope eq 'nmpdr') {
238                    # NMPDR mode: keep all groups.
239                    $keepGroup = 1;
240                } elsif ($scope eq 'core') {
241                    # CORE mode. Only keep real core groups.
242                    if (grep { $group =~ /$_/ } @{$FIG_Config::realCoreGroups}) {
243                        $keepGroup = 1;
244                    }
245                }
246                # Add this group if we're keeping it.
247                if ($keepGroup) {
248                    push @retVal, @{$groups{$group}};
249                }
250            }
251        }
252        # Return the result.
253        return @retVal;
254    }
255    
256    =head3 SuperGroup
257    
258        my $superGroup = $sprout->SuperGroup($groupName);
259    
260    Return the name of the super-group containing the specified NMPDR genome
261    group. If no appropriate super-group can be found, an error will be
262    thrown.
263    
264    =over 4
265    
266    =item groupName
267    
268    Name of the group whose super-group is desired.
269    
270    =item RETURN
271    
272    Returns the name of the super-group containing the incoming group.
273    
274    =back
275    
276    =cut
277    
278    sub SuperGroup {
279        # Get the parameters.
280        my ($self, $groupName) = @_;
281        # Declare the return variable.
282        my $retVal;
283        # Get the group hash.
284        my %groupHash = $self->CheckGroupFile();
285        # Find the super-group genus.
286        $groupName =~ /([A-Z]\w+)/;
287        my $nameThing = $1;
288        # See if it's directly in the group hash.
289        if (exists $groupHash{$nameThing}) {
290            # Yes, then it's our result.
291            $retVal = $nameThing;
292        } else {
293            # No, so we have to search.
294            for my $superGroup (keys %groupHash) {
295                # Get this super-group's item list.
296                my $list = $groupHash{$superGroup}->{contents};
297                # Search it.
298                if (grep { $_->[0] eq $nameThing } @{$list}) {
299                    $retVal = $superGroup;
300                }
301            }
302        }
303        # Return the result.
304      return $retVal;      return $retVal;
305  }  }
306    
# Line 193  Line 350 
350  The files are loaded based on the presumption that each line of the file is a record in the  The files are loaded based on the presumption that each line of the file is a record in the
351  relation, and the individual fields are delimited by tabs. Tab and new-line characters inside  relation, and the individual fields are delimited by tabs. Tab and new-line characters inside
352  fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must  fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must
353  be presented in the order given in the relation tables produced by the L</ShowMetaData> method.  be presented in the order given in the relation tables produced by the database documentation.
354    
355  =over 4  =over 4
356    
# Line 350  Line 507 
507      return $retVal;      return $retVal;
508  }  }
509    
510  =head3 GeneMenu  =head3 GenomeMenu
511    
512      my $selectHtml = $sprout->GeneMenu(\%attributes, $filterString, \@params, $selected);      my $html = $sprout->GenomeMenu(%options);
513    
514  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.
515  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
516  value will be the genome ID. The genomes will be sorted by genus/species name.  B<SearchHelper> class. Eventually, the two will be combined.
517    
518  =over 4  =over 4
519    
520  =item attributes  =item options
521    
522  Reference to a hash mapping attributes to values for the SELECT tag generated.  Optional parameters for the control (see below).
523    
524  =item filterString  =item RETURN
525    
526  A filter string for use in selecting the genomes. The filter string must conform  Returns the HTML for a genome selection control on a form (sometimes called a popup menu).
 to the rules for the C<< ERDB->Get >> method.  
527    
528  =item params  =back
529    
530  Reference to a list of values to be substituted in for the parameter marks in  The valid options are as follows.
 the filter string.  
531    
532  =item selected (optional)  =over 4
533    
534  ID of the genome to be initially selected.  =item name
535    
536  =item fast (optional)  Name to give this control for use in passing it to the form. The default is C<myGenomeControl>.
537    Terrible things will happen if you have two controls with the same name on the same page.
538    
539  If specified and TRUE, the contig counts will be omitted to improve performance.  =item filter
540    
541  =item RETURN  If specified, a filter for the list of genomes to display. The filter should be in the form of a
542    list reference, a string, or a hash reference. If it is a list reference, the first element
543    of the list should be the filter string, and the remaining elements the filter parameters. If it is a
544    string, it will be split into a list at each included tab. If it is a hash reference, it should be
545    a hash that maps genomes which should be included to a TRUE value.
546    
547    =item multiSelect
548    
549    If TRUE, then the user can select multiple genomes. If FALSE, the user can only select one genome.
550    
551  Returns an HTML select menu with the specified genomes as selectable options.  =item size
552    
553    Number of rows to display in the control. The default is C<10>
554    
555    =item id
556    
557    ID to give this control. The default is the value of the C<name> option. Nothing will work correctly
558    unless this ID is unique.
559    
560    =item selected
561    
562    A comma-delimited list of selected genomes, or a reference to a list of selected genomes. The
563    default is none.
564    
565    =item class
566    
567    If specified, a style class to assign to the genome control.
568    
569  =back  =back
570    
571  =cut  =cut
572    
573  sub GeneMenu {  sub GenomeMenu {
574      # Get the parameters.      # Get the parameters.
575      my ($self, $attributes, $filterString, $params, $selected, $fast) = @_;      my ($self, %options) = @_;
576      my $slowMode = ! $fast;      # Get the control's name and ID.
577      # Default to nothing selected. This prevents an execution warning if "$selected"      my $menuName = $options{name} || $options{id} || 'myGenomeControl';
578      # is undefined.      my $menuID = $options{id} || $menuName;
579      $selected = "" unless defined $selected;      Trace("Genome menu name = $menuName with ID $menuID.") if T(3);
580      Trace("Gene Menu called with slow mode \"$slowMode\" and selection \"$selected\".") if T(3);      # Compute the IDs for the status display.
581      # Start the menu.      my $divID = "${menuID}_status";
582      my $retVal = "<select " .      my $urlID = "${menuID}_url";
583          join(" ", map { "$_=\"$attributes->{$_}\"" } keys %{$attributes}) .      # Compute the code to show selected genomes in the status area.
584          ">\n";      my $showSelect = "showSelected('$menuID', '$divID', '$urlID', $FIG_Config::genome_control_cap)";
585      # Get the genomes.      # Check for single-select or multi-select.
586      my @genomes = $self->GetAll(['Genome'], $filterString, $params, ['Genome(id)',      my $multiSelect = $options{multiSelect} || 0;
587                                                                       'Genome(genus)',      # Get the style data.
588                                                                       'Genome(species)',      my $class = $options{class} || '';
589                                                                       'Genome(unique-characterization)']);      # Get the list of pre-selected items.
590      # Sort them by name.      my $selections = $options{selected} || [];
591      my @sorted = sort { lc("$a->[1] $a->[2]") cmp lc("$b->[1] $b->[2]") } @genomes;      if (ref $selections ne 'ARRAY') {
592      # Loop through the genomes, creating the option tags.          $selections = [ split /\s*,\s*/, $selections ];
593      for my $genomeData (@sorted) {      }
594          # Get the data for this genome.      my %selected = map { $_ => 1 } @{$selections};
595          my ($genomeID, $genus, $species, $strain) = @{$genomeData};      # Extract the filter information. The default is no filtering. It can be passed as a tab-delimited
596          # Get the contig count.      # string, a hash reference, or a list reference.
597          my $contigInfo = "";      my ($filterHash, $filterString);
598          if ($slowMode) {      my $filterParms = $options{filter} || "";
599              my $count = $self->ContigCount($genomeID);      if (ref $filterParms eq 'HASH') {
600              my $counting = ($count == 1 ? "contig" : "contigs");          $filterHash = $filterParms;
601              $contigInfo = "[$count $counting]";          $filterParms = [];
602          }          $filterString = "";
603          # Find out if we're selected.      } else {
604          my $selectOption = ($selected eq $genomeID ? " selected" : "");          if (! ref $filterParms) {
605          # Build the option tag.              $filterParms = [split /\t|\\t/, $filterParms];
606          $retVal .= "<option value=\"$genomeID\"$selectOption>$genus $species $strain ($genomeID)$contigInfo</option>\n";          }
607            $filterString = shift @{$filterParms};
608        }
609        # Check for possible subsystem filtering. If there is one, we will tack the
610        # relationship onto the object name list.
611        my @objectNames = qw(Genome);
612        if ($filterString =~ /ParticipatesIn\(/) {
613            push @objectNames, 'ParticipatesIn';
614        }
615        # Get a list of all the genomes in group order. In fact, we only need them ordered
616        # by name (genus,species,strain), but putting primary-group in front enables us to
617        # take advantage of an existing index.
618        my @genomeList = $self->GetAll(\@objectNames, "$filterString ORDER BY Genome(primary-group), Genome(genus), Genome(species), Genome(unique-characterization)",
619                                       $filterParms,
620                                       [qw(Genome(primary-group) Genome(id) Genome(genus) Genome(species) Genome(unique-characterization) Genome(taxonomy) Genome(contigs))]);
621        # Apply the hash filter (if any).
622        if (defined $filterHash) {
623            @genomeList = grep { $filterHash->{$_->[1]} } @genomeList;
624        }
625        # Create a hash to organize the genomes by group. Each group will contain a list of
626        # 2-tuples, the first element being the genome ID and the second being the genome
627        # name.
628        my %gHash = ();
629        for my $genome (@genomeList) {
630            # Get the genome data.
631            my ($group, $genomeID, $genus, $species, $strain, $taxonomy, $contigs) = @{$genome};
632            # Compute its name. This is the genus, species, strain (if any), and the contig count.
633            my $name = "$genus $species ";
634            $name .= "$strain " if $strain;
635            my $contigCount = ($contigs == 1 ? "" : ", $contigs contigs");
636            # Now we get the domain. The domain tells us the display style of the organism.
637            my ($domain) = split /\s*;\s*/, $taxonomy, 2;
638            # Now compute the display group. This is normally the primary group, but if the
639            # organism is supporting, we blank it out.
640            my $displayGroup = ($group eq $FIG_Config::otherGroup ? "" : $group);
641            # Push the genome into the group's list. Note that we use the real group
642            # name for the hash key here, not the display group name.
643            push @{$gHash{$group}}, [$genomeID, $name, $contigCount, $domain];
644        }
645        # We are almost ready to unroll the menu out of the group hash. The final step is to separate
646        # the supporting genomes by domain. First, we extract the NMPDR groups and sort them. They
647        # are sorted by the first capitalized word. Groups with "other" are sorted after groups
648        # that aren't "other". At some point, we will want to make this less complicated.
649        my %sortGroups = map { $_ =~ /(other)?(.*)([A-Z].+)/; "$3$1$2" => $_ }
650                             grep { $_ ne $FIG_Config::otherGroup } keys %gHash;
651        my @groups = map { $sortGroups{$_} } sort keys %sortGroups;
652        # Remember the number of NMPDR groups.
653        my $nmpdrGroupCount = scalar @groups;
654        # Are there any supporting genomes?
655        if (exists $gHash{$FIG_Config::otherGroup}) {
656            # Loop through the supporting genomes, classifying them by domain. We'll also keep a list
657            # of the domains found.
658            my @otherGenomes = @{$gHash{$FIG_Config::otherGroup}};
659            my @domains = ();
660            for my $genomeData (@otherGenomes) {
661                my ($genomeID, $name, $contigCount, $domain) = @{$genomeData};
662                if (exists $gHash{$domain}) {
663                    push @{$gHash{$domain}}, $genomeData;
664                } else {
665                    $gHash{$domain} = [$genomeData];
666                    push @domains, $domain;
667                }
668            }
669            # Add the domain groups at the end of the main group list. The main group list will now
670            # contain all the categories we need to display the genomes.
671            push @groups, sort @domains;
672            # Delete the supporting group.
673            delete $gHash{$FIG_Config::otherGroup};
674        }
675        # Now it gets complicated. We need a way to mark all the NMPDR genomes. We take advantage
676        # of the fact they come first in the list. We'll accumulate a count of the NMPDR genomes
677        # and use that to make the selections.
678        my $nmpdrCount = 0;
679        # Create the type counters.
680        my $groupCount = 1;
681        # Get the number of rows to display.
682        my $rows = $options{size} || 10;
683        # If we're multi-row, create an onChange event.
684        my $onChangeTag = ( $rows > 1 ? " onChange=\"$showSelect;\" onFocus=\"$showSelect;\"" : "" );
685        # Set up the multiple-select flag.
686        my $multipleTag = ($multiSelect ? " multiple" : "" );
687        # Set up the style class.
688        my $classTag = ($class ? " $class" : "" );
689        # Create the SELECT tag and stuff it into the output array.
690        my @lines = qq(<SELECT name="$menuName" id="$menuID" class="genomeSelect $class" $onChangeTag$multipleTag$classTag size="$rows">);
691        # Loop through the groups.
692        for my $group (@groups) {
693            # Get the genomes in the group.
694            for my $genome (@{$gHash{$group}}) {
695                # If this is an NMPDR organism, we add an extra style and count it.
696                my $nmpdrStyle = "";
697                if ($nmpdrGroupCount > 0) {
698                    $nmpdrCount++;
699                    $nmpdrStyle = " Core";
700                }
701                # Get the organism ID, name, contig count, and domain.
702                my ($genomeID, $name, $contigCount, $domain) = @{$genome};
703                # See if we're pre-selected.
704                my $selectTag = ($selected{$genomeID} ? " SELECTED" : "");
705                # Compute the display name.
706                my $nameString = "$name ($genomeID$contigCount)";
707                # Generate the option tag.
708                my $optionTag = "<OPTION class=\"$domain$nmpdrStyle\" title=\"$group\" value=\"$genomeID\"$selectTag>$nameString</OPTION>";
709                push @lines, "    $optionTag";
710            }
711            # Record this group in the nmpdrGroup count. When that gets to 0, we've finished the NMPDR
712            # groups.
713            $nmpdrGroupCount--;
714      }      }
715      # Close the SELECT tag.      # Close the SELECT tag.
716      $retVal .= "</select>\n";      push @lines, "</SELECT>";
717        if ($rows > 1) {
718            # We're in a non-compact mode, so we need to add some selection helpers. First is
719            # the search box. This allows the user to type text and change which genomes are
720            # displayed. For multiple-select mode, we include a button that selects the displayed
721            # genes. For single-select mode, we use a plain label instead.
722            my $searchThingName = "${menuID}_SearchThing";
723            my $searchThingLabel = "Type to narrow selection";
724            my $searchThingButton = "";
725            if ($multiSelect) {
726                $searchThingButton = qq(<INPUT type="button" name="MacroSearch" class="button" value="Go" onClick="selectShowing('$menuID', '$searchThingName'); $showSelect;" />);
727            }
728            push @lines, "<br />$searchThingLabel&nbsp;" .
729                         qq(<INPUT type="text" id="$searchThingName" name="$searchThingName" class="genomeSearchThing" onKeyup="showTyped('$menuID', '$searchThingName');" />) .
730                         $searchThingButton .
731                         Hint("GenomeControl", 28) . "<br />";
732            # For multi-select mode, we also have buttons to set and clear selections.
733            if ($multiSelect) {
734                push @lines, qq(<INPUT type="button" name="ClearAll" class="bigButton genomeButton" value="Clear All" onClick="clearAll(getElementById('$menuID')); $showSelect" />);
735                push @lines, qq(<INPUT type="button" name="SelectAll" class="bigButton genomeButton" value="Select All" onClick="selectAll(getElementById('$menuID')); $showSelect" />);
736                push @lines, qq(<INPUT type="button" name="NMPDROnly" class="bigButton genomeButton" value="Select NMPDR" onClick="selectSome(getElementById('$menuID'), $nmpdrCount, true); $showSelect;" />);
737            }
738            # Add a hidden field we can use to generate organism page hyperlinks.
739            push @lines, qq(<INPUT type="hidden" id="$urlID" value="$FIG_Config::cgi_url/wiki/rest.cgi/NmpdrPlugin/SeedViewer?page=Organism;organism=" />);
740            # Add the status display. This tells the user what's selected no matter where the list is scrolled.
741            push @lines, qq(<DIV id="$divID" class="Panel"></DIV>);
742        }
743        # Assemble all the lines into a string.
744        my $retVal = join("\n", @lines, "");
745        # Return the result.
746        return $retVal;
747    }
748    
749    =head3 Cleanup
750    
751        $sprout->Cleanup();
752    
753    Release the internal cache structures to free up memory.
754    
755    =cut
756    
757    sub Cleanup {
758        # Get the parameters.
759        my ($self) = @_;
760        # Delete the stemmer.
761        delete $self->{stemmer};
762        # Delete the attribute database.
763        delete $self->{_ca};
764        # Delete the group hash.
765        delete $self->{groupHash};
766        # Is there a FIG object?
767        if (defined $self->{fig}) {
768            # Yes, clear its subsystem cache.
769            $self->{fig}->clear_subsystem_cache();
770        }
771    }
772    
773    
774    =head3 Stem
775    
776        my $stem = $sprout->Stem($word);
777    
778    Return the stem of the specified word, or C<undef> if the word is not
779    stemmable. Note that even if the word is stemmable, the stem may be
780    the same as the original word.
781    
782    =over 4
783    
784    =item word
785    
786    Word to convert into a stem.
787    
788    =item RETURN
789    
790    Returns a stem of the word (which may be the word itself), or C<undef> if
791    the word is not stemmable.
792    
793    =back
794    
795    =cut
796    
797    sub Stem {
798        # Get the parameters.
799        my ($self, $word) = @_;
800        # Get the stemmer object.
801        my $stemmer = $self->{stemmer};
802        if (! defined $stemmer) {
803            # We don't have one pre-built, so we build and save it now.
804            $stemmer = BioWords->new(exceptions => "$FIG_Config::sproutData/Exceptions.txt",
805                                     stops => "$FIG_Config::sproutData/StopWords.txt",
806                                     cache => 0);
807            $self->{stemmer} = $stemmer;
808        }
809        # Try to stem the word.
810        my $retVal = $stemmer->Process($word);
811      # Return the result.      # Return the result.
812      return $retVal;      return $retVal;
813  }  }
814    
815    
816  =head3 Build  =head3 Build
817    
818      $sprout->Build();      $sprout->Build();
# Line 489  Line 871 
871  sub GenusSpecies {  sub GenusSpecies {
872      # Get the parameters.      # Get the parameters.
873      my ($self, $genomeID) = @_;      my ($self, $genomeID) = @_;
874      # Get the data for the specified genome.      # Declare the return value.
875      my @values = $self->GetEntityValues('Genome', $genomeID, ['Genome(genus)', 'Genome(species)',      my $retVal;
876                                                                'Genome(unique-characterization)']);      # Get the genome data.
877      # Format the result and return it.      my $genomeData = $self->_GenomeData($genomeID);
878      my $retVal = join(' ', @values);      # Only proceed if we found the genome.
879        if (defined $genomeData) {
880            $retVal = $genomeData->PrimaryValue('Genome(scientific-name)');
881        }
882        # Return it.
883      return $retVal;      return $retVal;
884  }  }
885    
# Line 568  Line 954 
954  =item RETURN  =item RETURN
955    
956  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
957  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
958    wasn't found.
959    
960  =back  =back
961    
# Line 577  Line 964 
964  sub FeatureLocation {  sub FeatureLocation {
965      # Get the parameters.      # Get the parameters.
966      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
967        # Declare the return variable.
968        my @retVal = ();
969      # Get the feature record.      # Get the feature record.
970      my $object = $self->GetEntity('Feature', $featureID);      my $object = $self->GetEntity('Feature', $featureID);
971      Confess("Feature $featureID not found.") if ! defined($object);      # Only proceed if we found it.
972        if (defined $object) {
973      # Get the location string.      # Get the location string.
974      my $locString = $object->PrimaryValue('Feature(location-string)');      my $locString = $object->PrimaryValue('Feature(location-string)');
975      # Create the return list.      # Create the return list.
976      my @retVal = split /\s*,\s*/, $locString;          @retVal = split /\s*,\s*/, $locString;
977        }
978      # Return the list in the format indicated by the context.      # Return the list in the format indicated by the context.
979      return (wantarray ? @retVal : join(',', @retVal));      return (wantarray ? @retVal : join(',', @retVal));
980  }  }
# Line 633  Line 1024 
1024  }  }
1025    
1026    
   
1027  =head3 PointLocation  =head3 PointLocation
1028    
1029      my $found = Sprout::PointLocation($location, $point);      my $found = Sprout::PointLocation($location, $point);
# Line 826  Line 1216 
1216      my ($self, $genomeID) = @_;      my ($self, $genomeID) = @_;
1217      # Declare the return variable.      # Declare the return variable.
1218      my $retVal = 0;      my $retVal = 0;
1219      # Get the genome's contig sequence lengths.      # Get the genome data.
1220      my @lens = $self->GetFlat(['HasContig', 'IsMadeUpOf'], 'HasContig(from-link) = ?',      my $genomeData = $self->_GenomeData($genomeID);
1221                         [$genomeID], 'IsMadeUpOf(len)');      # Only proceed if it exists.
1222      # Sum the lengths.      if (defined $genomeData) {
1223      map { $retVal += $_ } @lens;          $retVal = $genomeData->PrimaryValue('Genome(dna-size)');
1224        }
1225      # Return the result.      # Return the result.
1226      return $retVal;      return $retVal;
1227  }  }
# Line 1315  Line 1706 
1706  the specified user and FIG are considered trusted. If the user ID is omitted, only FIG  the specified user and FIG are considered trusted. If the user ID is omitted, only FIG
1707  is trusted.  is trusted.
1708    
1709  If the feature is B<not> identified by a FIG ID, then the functional assignment  If the feature is B<not> identified by a FIG ID, then we search the aliases for it.
1710  information is taken from the B<ExternalAliasFunc> table. If the table does  If no matching alias is found, we return an undefined value.
 not contain an entry for the feature, an undefined value is returned.  
1711    
1712  =over 4  =over 4
1713    
# Line 1343  Line 1733 
1733      my ($self, $featureID, $userID) = @_;      my ($self, $featureID, $userID) = @_;
1734      # Declare the return value.      # Declare the return value.
1735      my $retVal;      my $retVal;
1736      # Determine the ID type.      # Find a FIG ID for this feature.
1737      if ($featureID =~ m/^fig\|/) {      my ($fid) = $self->FeaturesByAlias($featureID);
1738        # Only proceed if we have an ID.
1739        if ($fid) {
1740          # Here we have a FIG feature ID.          # Here we have a FIG feature ID.
1741          if (!$userID) {          if (!$userID) {
1742              # Use the primary assignment.              # Use the primary assignment.
1743              ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(assignment)']);              ($retVal) = $self->GetEntityValues('Feature', $fid, ['Feature(assignment)']);
1744          } else {          } else {
1745              # We must build the list of trusted users.              # We must build the list of trusted users.
1746              my %trusteeTable = ();              my %trusteeTable = ();
# Line 1374  Line 1766 
1766              # Build a query for all of the feature's annotations, sorted by date.              # Build a query for all of the feature's annotations, sorted by date.
1767              my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],              my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1768                                     "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",                                     "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1769                                     [$featureID]);                                     [$fid]);
1770              my $timeSelected = 0;              my $timeSelected = 0;
1771              # Loop until we run out of annotations.              # Loop until we run out of annotations.
1772              while (my $annotation = $query->Fetch()) {              while (my $annotation = $query->Fetch()) {
# Line 1394  Line 1786 
1786                  }                  }
1787              }              }
1788          }          }
     } else {  
         # Here we have a non-FIG feature ID. In this case the user ID does not  
         # matter. We simply get the information from the External Alias Function  
         # table.  
         ($retVal) = $self->GetEntityValues('ExternalAliasFunc', $featureID, ['ExternalAliasFunc(func)']);  
1789      }      }
1790      # Return the assignment found.      # Return the assignment found.
1791      return $retVal;      return $retVal;
# Line 1417  Line 1804 
1804  annotation itself because it's a text field; however, this is not a big problem because  annotation itself because it's a text field; however, this is not a big problem because
1805  most features only have a small number of annotations.  most features only have a small number of annotations.
1806    
 If the feature is B<not> identified by a FIG ID, then the functional assignment  
 information is taken from the B<ExternalAliasFunc> table. If the table does  
 not contain an entry for the feature, an empty list is returned.  
   
1807  =over 4  =over 4
1808    
1809  =item featureID  =item featureID
# Line 1441  Line 1824 
1824      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
1825      # Declare the return value.      # Declare the return value.
1826      my @retVal = ();      my @retVal = ();
1827      # Determine the ID type.      # Convert to a FIG ID.
1828      if ($featureID =~ m/^fig\|/) {      my ($fid) = $self->FeaturesByAlias($featureID);
1829        # Only proceed if we found one.
1830        if ($fid) {
1831          # Here we have a FIG feature ID. We must build the list of trusted          # Here we have a FIG feature ID. We must build the list of trusted
1832          # users.          # users.
1833          my %trusteeTable = ();          my %trusteeTable = ();
1834          # Build a query for all of the feature's annotations, sorted by date.          # Build a query for all of the feature's annotations, sorted by date.
1835          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1836                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1837                                 [$featureID]);                                 [$fid]);
1838          my $timeSelected = 0;          my $timeSelected = 0;
1839          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1840          while (my $annotation = $query->Fetch()) {          while (my $annotation = $query->Fetch()) {
# Line 1464  Line 1849 
1849                  push @retVal, [$actualUser, $function];                  push @retVal, [$actualUser, $function];
1850              }              }
1851          }          }
     } else {  
         # Here we have a non-FIG feature ID. In this case the user ID does not  
         # matter. We simply get the information from the External Alias Function  
         # table.  
         my @assignments = $self->GetEntityValues('ExternalAliasFunc', $featureID,  
                                                  ['ExternalAliasFunc(func)']);  
         push @retVal, map { ['master', $_] } @assignments;  
1852      }      }
1853      # Return the assignments found.      # Return the assignments found.
1854      return @retVal;      return @retVal;
# Line 1510  Line 1888 
1888      # Loop through the incoming features.      # Loop through the incoming features.
1889      for my $featureID (@{$featureList}) {      for my $featureID (@{$featureList}) {
1890          # Ask the server for the feature's best hit.          # Ask the server for the feature's best hit.
1891          my @bbhData = FIGRules::BBHData($featureID);          my $bbhData = FIGRules::BBHData($featureID);
1892          # Peel off the BBHs found.          # Peel off the BBHs found.
1893          my @found = ();          my @found = ();
1894          for my $bbh (@bbhData) {          for my $bbh (@$bbhData) {
1895              my $fid = $bbh->[0];              my $fid = $bbh->[0];
1896              my $bbGenome = $self->GenomeOf($fid);              my $bbGenome = $self->GenomeOf($fid);
1897              if ($bbGenome eq $genomeID) {              if ($bbGenome eq $genomeID) {
# Line 1552  Line 1930 
1930      # Get the parameters.      # Get the parameters.
1931      my ($self, $featureID, $count) = @_;      my ($self, $featureID, $count) = @_;
1932      # Ask for the best hits.      # Ask for the best hits.
1933      my @lists = FIGRules::BBHData($featureID);      my $lists = FIGRules::BBHData($featureID);
1934      # Create the return value.      # Create the return value.
1935      my %retVal = ();      my %retVal = ();
1936      for my $tuple (@lists) {      for my $tuple (@$lists) {
1937          $retVal{$tuple->[0]} = $tuple->[1];          $retVal{$tuple->[0]} = $tuple->[1];
1938      }      }
1939      # Return the result.      # Return the result.
# Line 1589  Line 1967 
1967      # Declare the return variable.      # Declare the return variable.
1968      my $retVal;      my $retVal;
1969      # Get the genome's data.      # Get the genome's data.
1970      my $genomeData = $self->GetEntity('Genome', $genomeID);      my $genomeData = $self->_GenomeData($genomeID);
1971      if ($genomeData) {      # Only proceed if it exists.
1972        if (defined $genomeData) {
1973          # The genome exists, so get the completeness flag.          # The genome exists, so get the completeness flag.
1974          $retVal = $genomeData->PrimaryValue('Genome(complete)');          $retVal = $genomeData->PrimaryValue('Genome(complete)');
1975      }      }
# Line 1658  Line 2037 
2037      if ($featureID =~ /^fig\|(\d+\.\d+)/) {      if ($featureID =~ /^fig\|(\d+\.\d+)/) {
2038          $retVal = $1;          $retVal = $1;
2039      } else {      } else {
2040          Confess("Invalid feature ID $featureID.");          # Find the feature by alias.
2041            my ($realFeatureID) = $self->FeaturesByAlias($featureID);
2042            if ($realFeatureID && $realFeatureID =~ /^fig\|(\d+\.\d+)/) {
2043                $retVal = $1;
2044            }
2045      }      }
2046      # Return the value found.      # Return the value found.
2047      return $retVal;      return $retVal;
# Line 1702  Line 2085 
2085              $retVal{$featureID2} = $score;              $retVal{$featureID2} = $score;
2086          }          }
2087      }      }
     # Functional coupling is reflexive. If we found at least one coupled feature, we must add  
     # the incoming feature as well.  
     if (keys %retVal) {  
         $retVal{$featureID} = 9999;  
     }  
2088      # Return the hash.      # Return the hash.
2089      return %retVal;      return %retVal;
2090  }  }
# Line 2063  Line 2441 
2441  sub GetGenomeNameData {  sub GetGenomeNameData {
2442      # Get the parameters.      # Get the parameters.
2443      my ($self, $genomeID) = @_;      my ($self, $genomeID) = @_;
2444        # Declare the return variables.
2445        my ($genus, $species, $strain);
2446        # Get the genome's data.
2447        my $genomeData = $self->_GenomeData($genomeID);
2448        # Only proceed if the genome exists.
2449        if (defined $genomeData) {
2450      # Get the desired values.      # Get the desired values.
2451      my ($genus, $species, $strain) = $self->GetEntityValues('Genome', $genomeID =>          ($genus, $species, $strain) = $genomeData->Values(['Genome(genus)',
2452                                                              [qw(Genome(genus) Genome(species) Genome(unique-characterization))]);                                                             'Genome(species)',
2453      # Throw an error if they were not found.                                                             'Genome(unique-characterization)']);
2454      if (! defined $genus) {      } else {
2455            # Throw an error because they were not found.
2456          Confess("Genome $genomeID not found in database.");          Confess("Genome $genomeID not found in database.");
2457      }      }
2458      # Return the results.      # Return the results.
# Line 2368  Line 2753 
2753  sub Taxonomy {  sub Taxonomy {
2754      # Get the parameters.      # Get the parameters.
2755      my ($self, $genome) = @_;      my ($self, $genome) = @_;
     # Find the specified genome's taxonomy string.  
     my ($list) = $self->GetEntityValues('Genome', $genome, ['Genome(taxonomy)']);  
2756      # Declare the return variable.      # Declare the return variable.
2757      my @retVal = ();      my @retVal = ();
2758      # If we found the genome, return its taxonomy string.      # Get the genome data.
2759      if ($list) {      my $genomeData = $self->_GenomeData($genome);
2760          @retVal = split /\s*;\s*/, $list;      # Only proceed if it exists.
2761        if (defined $genomeData) {
2762            # Create the taxonomy from the taxonomy string.
2763            @retVal = split /\s*;\s*/, $genomeData->PrimaryValue('Genome(taxonomy)');
2764      } else {      } else {
2765            # Genome doesn't exist, so emit a warning.
2766          Trace("Genome \"$genome\" does not have a taxonomy in the database.\n") if T(0);          Trace("Genome \"$genome\" does not have a taxonomy in the database.\n") if T(0);
2767      }      }
2768      # Return the value found.      # Return the value found.
# Line 2420  Line 2807 
2807      }      }
2808      my @taxA = $self->Taxonomy($genomeA);      my @taxA = $self->Taxonomy($genomeA);
2809      my @taxB = $self->Taxonomy($genomeB);      my @taxB = $self->Taxonomy($genomeB);
2810      # Initialize the distance to 1. We'll reduce it each time we find a match between the      # Compute the distance.
2811      # taxonomies.      my $retVal = FIGRules::CrudeDistanceFormula(\@taxA, \@taxB);
     my $retVal = 1.0;  
     # Initialize the subtraction amount. This amount determines the distance reduction caused  
     # by a mismatch at the current level.  
     my $v = 0.5;  
     # Loop through the taxonomies.  
     for (my $i = 0; ($i < @taxA) && ($i < @taxB) && ($taxA[$i] eq $taxB[$i]); $i++) {  
         $retVal -= $v;  
         $v /= 2;  
     }  
2812      return $retVal;      return $retVal;
2813  }  }
2814    
# Line 2498  Line 2876 
2876      return @retVal;      return @retVal;
2877  }  }
2878    
 =head3 GetProperties  
   
     my @list = $sprout->GetProperties($fid, $key, $value, $url);  
   
 Return a list of the properties with the specified characteristics.  
   
 Properties are the Sprout analog of the FIG attributes. The call is  
 passed directly to the CustomAttributes or RemoteCustomAttributes object  
 contained in this object.  
   
 This method returns a series of tuples that match the specified criteria. Each tuple  
 will contain an object ID, a key, and one or more values. The parameters to this  
 method therefore correspond structurally to the values expected in each tuple. In  
 addition, you can ask for a generic search by suffixing a percent sign (C<%>) to any  
 of the parameters. So, for example,  
   
     my @attributeList = $sprout->GetProperties('fig|100226.1.peg.1004', 'structure%', 1, 2);  
   
 would return something like  
   
     ['fig}100226.1.peg.1004', 'structure', 1, 2]  
     ['fig}100226.1.peg.1004', 'structure1', 1, 2]  
     ['fig}100226.1.peg.1004', 'structure2', 1, 2]  
     ['fig}100226.1.peg.1004', 'structureA', 1, 2]  
   
 Use of C<undef> in any position acts as a wild card (all values). You can also specify  
 a list reference in the ID column. Thus,  
   
     my @attributeList = $sprout->GetProperties(['100226.1', 'fig|100226.1.%'], 'PUBMED');  
   
 would get the PUBMED attribute data for Streptomyces coelicolor A3(2) and all its  
 features.  
   
 In addition to values in multiple sections, a single attribute key can have multiple  
 values, so even  
   
     my @attributeList = $sprout->GetProperties($peg, 'virulent');  
   
 which has no wildcard in the key or the object ID, may return multiple tuples.  
   
 =over 4  
   
 =item objectID  
   
 ID of object whose attributes are desired. If the attributes are desired for multiple  
 objects, this parameter can be specified as a list reference. If the attributes are  
 desired for all objects, specify C<undef> or an empty string. Finally, you can specify  
 attributes for a range of object IDs by putting a percent sign (C<%>) at the end.  
   
 =item key  
   
 Attribute key name. A value of C<undef> or an empty string will match all  
 attribute keys. If the values are desired for multiple keys, this parameter can be  
 specified as a list reference. Finally, you can specify attributes for a range of  
 keys by putting a percent sign (C<%>) at the end.  
   
 =item values  
   
 List of the desired attribute values, section by section. If C<undef>  
 or an empty string is specified, all values in that section will match. A  
 generic match can be requested by placing a percent sign (C<%>) at the end.  
 In that case, all values that match up to and not including the percent sign  
 will match. You may also specify a regular expression enclosed  
 in slashes. All values that match the regular expression will be returned. For  
 performance reasons, only values have this extra capability.  
   
 =item RETURN  
   
 Returns a list of tuples. The first element in the tuple is an object ID, the  
 second is an attribute key, and the remaining elements are the sections of  
 the attribute value. All of the tuples will match the criteria set forth in  
 the parameter list.  
   
 =back  
   
 =cut  
   
 sub GetProperties {  
     # Get the parameters.  
     my ($self, @parms) = @_;  
     # Declare the return variable.  
     my @retVal = $self->{_ca}->GetAttributes(@parms);  
     # Return the result.  
     return @retVal;  
 }  
   
2879  =head3 FeatureProperties  =head3 FeatureProperties
2880    
2881      my @properties = $sprout->FeatureProperties($featureID);      my @properties = $sprout->FeatureProperties($featureID);
# Line 2612  Line 2904 
2904      # Get the parameters.      # Get the parameters.
2905      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
2906      # Get the properties.      # Get the properties.
2907      my @attributes = $self->{_ca}->GetAttributes($featureID);      my @attributes = $self->ca->GetAttributes($featureID);
2908      # Strip the feature ID off each tuple.      # Strip the feature ID off each tuple.
2909      my @retVal = ();      my @retVal = ();
2910      for my $attributeRow (@attributes) {      for my $attributeRow (@attributes) {
# Line 2884  Line 3176 
3176  sub SubsystemList {  sub SubsystemList {
3177      # Get the parameters.      # Get the parameters.
3178      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
3179      # Get the list of names.      # Get the list of names. We do a join to the Subsystem table because we have missing subsystems in
3180      my @retVal = $self->GetFlat(['HasRoleInSubsystem'], "HasRoleInSubsystem(from-link) = ?",      # the Sprout database!
3181        my @retVal = $self->GetFlat(['HasRoleInSubsystem', 'Subsystem'], "HasRoleInSubsystem(from-link) = ?",
3182                                  [$featureID], 'HasRoleInSubsystem(to-link)');                                  [$featureID], 'HasRoleInSubsystem(to-link)');
3183      # Return the result, sorted.      # Return the result, sorted.
3184      return sort @retVal;      return sort @retVal;
# Line 2918  Line 3211 
3211      # Declare the return variable.      # Declare the return variable.
3212      my %retVal = ();      my %retVal = ();
3213      # Get a list of the genome features that participate in subsystems. For each      # Get a list of the genome features that participate in subsystems. For each
3214      # feature we get its spreadsheet cells and the corresponding roles.      # feature we get its subsystem ID and the corresponding roles.
3215      my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf'],      my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf', 'HasSSCell'],
3216                               "HasFeature(from-link) = ?", [$genomeID],                               "HasFeature(from-link) = ?", [$genomeID],
3217                               ['HasFeature(to-link)', 'IsRoleOf(to-link)', 'IsRoleOf(from-link)']);                                   ['HasFeature(to-link)', 'IsRoleOf(from-link)',  'HasSSCell(from-link)']);
3218      # Now we get a list of the spreadsheet cells and their associated subsystems. Subsystems      # Now we get a list of valid subsystems. These are the subsystems connected to the genome with
3219      # with an unknown variant code (-1) are skipped. Note the genome ID is at both ends of the      # a non-negative variant code.
3220      # list. We use it at the beginning to get all the spreadsheet cells for the genome and      my %subs = map { $_ => 1 } $self->GetFlat(['ParticipatesIn'],
3221      # again at the end to filter out participation in subsystems with a negative variant code.                                                  "ParticipatesIn(from-link) = ? AND ParticipatesIn(variant-code) >= 0",
3222      my @cellData = $self->GetAll(['IsGenomeOf', 'HasSSCell', 'ParticipatesIn'],                                                  [$genomeID], 'ParticipatesIn(to-link)');
3223                                   "IsGenomeOf(from-link) = ? AND ParticipatesIn(variant-code) >= 0 AND ParticipatesIn(from-link) = ?",      # We loop through @roleData to build the hash.
                                  [$genomeID, $genomeID], ['HasSSCell(to-link)', 'HasSSCell(from-link)']);  
     # Now "@roleData" lists the spreadsheet cell and role for each of the genome's features.  
     # "@cellData" lists the subsystem name for each of the genome's spreadsheet cells. We  
     # link these two lists together to create the result. First, we want a hash mapping  
     # spreadsheet cells to subsystem names.  
     my %subHash = map { $_->[0] => $_->[1] } @cellData;  
     # We loop through @cellData to build the hash.  
3224      for my $roleEntry (@roleData) {      for my $roleEntry (@roleData) {
3225          # Get the data for this feature and cell.          # Get the data for this feature and cell.
3226          my ($fid, $cellID, $role) = @{$roleEntry};          my ($fid, $role, $subsys) = @{$roleEntry};
3227          # Check for a subsystem name.          Trace("Subsystem for $fid is $subsys.") if T(4);
3228          my $subsys = $subHash{$cellID};          # Check the subsystem;
3229          if ($subsys) {          if ($subs{$subsys}) {
3230                Trace("Subsystem found.") if T(4);
3231              # Insure this feature has an entry in the return hash.              # Insure this feature has an entry in the return hash.
3232              if (! exists $retVal{$fid}) { $retVal{$fid} = []; }              if (! exists $retVal{$fid}) { $retVal{$fid} = []; }
3233              # Merge in this new data.              # Merge in this new data.
# Line 2986  Line 3273 
3273      # Get the parameters.      # Get the parameters.
3274      my ($self, $featureID, $function, $userID) = @_;      my ($self, $featureID, $function, $userID) = @_;
3275      # 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.
3276      my @bbhFeatures = map { $_->[0] } FIGRules::BBHData($featureID);      my $bbhData = FIGRules::BBHData($featureID);
3277        my @bbhFeatures = map { $_->[0] } @$bbhData;
3278      # 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
3279      # functional assignment.      # functional assignment.
3280      my @retVal = ();      my @retVal = ();
# Line 3039  Line 3327 
3327          my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",          my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",
3328                                          [$fid], 'Genome(taxonomy)');                                          [$fid], 'Genome(taxonomy)');
3329          # Add this feature to the hash buffer.          # Add this feature to the hash buffer.
3330          Tracer::AddToListMap(\%hashBuffer, $taxonomy, $fid);          push @{$hashBuffer{$taxonomy}}, $fid;
3331      }      }
3332      # Sort the keys and get the elements.      # Sort the keys and get the elements.
3333      my @retVal = ();      my @retVal = ();
# Line 3159  Line 3447 
3447    
3448  =head3 BBHMatrix  =head3 BBHMatrix
3449    
3450      my %bbhMap = $sprout->BBHMatrix($genomeID, $cutoff, @targets);      my $bbhMap = $sprout->BBHMatrix($genomeID, $cutoff, @targets);
3451    
3452  Find all the bidirectional best hits for the features of a genome in a  Find all the bidirectional best hits for the features of a genome in a
3453  specified list of target genomes. The return value will be a hash mapping  specified list of target genomes. The return value will be a hash mapping
# Line 3183  Line 3471 
3471    
3472  =item RETURN  =item RETURN
3473    
3474  Returns a hash mapping each feature in the original genome to a hash mapping its  Returns a reference to a hash mapping each feature in the original genome
3475  BBH pegs in the target genomes to their scores.  to a sub-hash mapping its BBH pegs in the target genomes to their scores.
3476    
3477  =back  =back
3478    
# Line 3197  Line 3485 
3485      my %retVal = ();      my %retVal = ();
3486      # Ask for the BBHs.      # Ask for the BBHs.
3487      my @bbhList = FIGRules::BatchBBHs("fig|$genomeID.%", $cutoff, @targets);      my @bbhList = FIGRules::BatchBBHs("fig|$genomeID.%", $cutoff, @targets);
3488        Trace("Retrieved " . scalar(@bbhList) . " BBH results.") if T(3);
3489      # We now have a set of 4-tuples that we need to convert into a hash of hashes.      # We now have a set of 4-tuples that we need to convert into a hash of hashes.
3490      for my $bbhData (@bbhList) {      for my $bbhData (@bbhList) {
3491          my ($peg1, $peg2, $score) = @{$bbhData};          my ($peg1, $peg2, $score) = @{$bbhData};
# Line 3207  Line 3496 
3496          }          }
3497      }      }
3498      # Return the result.      # Return the result.
3499      return %retVal;      return \%retVal;
3500  }  }
3501    
3502    
# Line 3313  Line 3602 
3602      # Create the return hash.      # Create the return hash.
3603      my %retVal = ();      my %retVal = ();
3604      # Query for the desired BBHs.      # Query for the desired BBHs.
3605      my @bbhList = FIGRules::BBHData($featureID, $cutoff);      my $bbhList = FIGRules::BBHData($featureID, $cutoff);
3606      # Form the results into the return hash.      # Form the results into the return hash.
3607      for my $pair (@bbhList) {      for my $pair (@$bbhList) {
3608          my $fid = $pair->[0];          my $fid = $pair->[0];
3609          if ($self->Exists('Feature', $fid)) {          if ($self->Exists('Feature', $fid)) {
3610              $retVal{$fid} = $pair->[1];              $retVal{$fid} = $pair->[1];
# Line 3618  Line 3907 
3907      my ($self, %groupHash) = @_;      my ($self, %groupHash) = @_;
3908      # Create the result hash.      # Create the result hash.
3909      my %retVal = ();      my %retVal = ();
     # Get the super-group table.  
     my %superTable = $self->CheckGroupFile();  
3910      # Copy over the genomes.      # Copy over the genomes.
3911      for my $groupID (keys %groupHash) {      for my $groupID (keys %groupHash) {
3912          # Get the super-group name.          # Get the super-group name.
3913          my $realGroupID;          my $realGroupID = $self->SuperGroup($groupID);
3914          if ($groupID =~ /([A-Z]\w+)/) {          # Append this group's genomes into the result hash
3915              if (! defined($superTable{$1})) {          # using the super-group name.
                 Confess("Super-group name not found for group $groupID.");  
             } else {  
                 $realGroupID = $1;  
             }  
         } else {  
             Confess("Invalid group name $groupID.");  
         }  
         # Append this group's genomes into the result hash.  
3916          push @{$retVal{$realGroupID}}, @{$groupHash{$groupID}};          push @{$retVal{$realGroupID}}, @{$groupHash{$groupID}};
3917      }      }
3918      # Return the result hash.      # Return the result hash.
# Line 3664  Line 3943 
3943  sub GroupPageName {  sub GroupPageName {
3944      # Get the parameters.      # Get the parameters.
3945      my ($self, $group) = @_;      my ($self, $group) = @_;
     # Declare the return variable.  
     my $retVal;  
3946      # Check for the group file data.      # Check for the group file data.
3947      my %superTable = $self->CheckGroupFile();      my %superTable = $self->CheckGroupFile();
3948      # Compute the real group name.      # Compute the real group name.
3949      if ($group =~ /([A-Z]\w+)/) {      my $realGroup = $self->SuperGroup($group);
3950          my $realGroup = $1;      # Get the associated page name.
3951          if (! defined($superTable{$1})) {      my $retVal = "../content/$superTable{$realGroup}->{page}";
             Confess("No super-group found for \"$group\".");  
         } else {  
             $retVal = "../content/$superTable{$1}->{page}";  
         }  
     } else {  
         Confess("\"group\" is not a valid group name.");  
     }  
3952      # Return the result.      # Return the result.
3953      return $retVal;      return $retVal;
3954  }  }
# Line 3712  Line 3982 
3982      # Get the parameters.      # Get the parameters.
3983      my ($self, $featureID, $key, @values) = @_;      my ($self, $featureID, $key, @values) = @_;
3984      # Add the property using the attached attributes object.      # Add the property using the attached attributes object.
3985      $self->{_ca}->AddAttribute($featureID, $key, @values);      $self->ca->AddAttribute($featureID, $key, @values);
3986  }  }
3987    
3988  =head3 CheckGroupFile  =head3 CheckGroupFile
# Line 3732  Line 4002 
4002    
4003  =over 4  =over 4
4004    
4005  =item page  =item specials
4006    
4007  The super-group's web page in the NMPDR.  Reference to a hash whose keys are the names of special species.
4008    
4009  =item contents  =item contents
4010    
# Line 3757  Line 4027 
4027          my @groupLines = Tracer::GetFile("$FIG_Config::sproutData/groups.tbl");          my @groupLines = Tracer::GetFile("$FIG_Config::sproutData/groups.tbl");
4028          # Loop through the list of sort-of groups.          # Loop through the list of sort-of groups.
4029          for my $groupLine (@groupLines) {          for my $groupLine (@groupLines) {
4030              my ($name, $page, @contents) = split /\t/, $groupLine;              my ($name, $specials, @contents) = split /\t/, $groupLine;
4031              $groupHash{$name} = { page => $page,              $groupHash{$name} = { specials => { map { $_ => 1 } split /\s*,\s*/, $specials },
4032                                    contents => [ map { [ split /\s*,\s*/, $_ ] } @contents ]                                    contents => [ map { [ split /\s*,\s*/, $_ ] } @contents ]
4033                                  };                                  };
4034          }          }
# Line 3799  Line 4069 
4069  sub CleanKeywords {  sub CleanKeywords {
4070      # Get the parameters.      # Get the parameters.
4071      my ($self, $searchExpression) = @_;      my ($self, $searchExpression) = @_;
4072      # Perform the standard cleanup.      # Get the stemmer.
4073      my $retVal = $self->ERDB::CleanKeywords($searchExpression);      my $stemmer = $self->GetStemmer();
4074      # Fix the periods in EC and TC numbers.      # Convert the search expression using the stemmer.
4075      $retVal =~ s/(\d+|\-)\.(\d+|-)\.(\d+|-)\.(\d+|-)/$1_$2_$3_$4/g;      my $retVal = $stemmer->PrepareSearchExpression($searchExpression);
4076      # Fix non-trailing periods.      Trace("Cleaned keyword list for \"$searchExpression\" is \"$retVal\".") if T(3);
     $retVal =~ s/\.(\w)/_$1/g;  
     # Fix non-leading minus signs.  
     $retVal =~ s/(\w)[\-]/$1_/g;  
     # Fix the vertical bars and colons  
     $retVal =~ s/(\w)[|:](\w)/$1'$2/g;  
4077      # Return the result.      # Return the result.
4078      return $retVal;      return $retVal;
4079  }  }
4080    
4081    =head3 GetSourceObject
4082    
4083        my $source = $erdb->GetSourceObject();
4084    
4085    Return the object to be used in creating load files for this database.
4086    
4087    =cut
4088    
4089    sub GetSourceObject {
4090        # Get the parameters.
4091        my ($self) = @_;
4092        # Do we already have one?
4093        my $retVal = $self->{fig};
4094        if (! defined $retVal) {
4095            # Create the object.
4096            require FIG;
4097            $retVal = FIG->new();
4098            Trace("FIG source object created for process $$.") if T(ERDBLoadGroup => 3);
4099            # Set up retries to prevent the lost-connection error when harvesting
4100            # the feature data.
4101            my $dbh = $retVal->db_handle();
4102            $dbh->set_retries(5);
4103            # Save it for other times.
4104            $self->{fig} = $retVal;
4105        }
4106        # Return the object.
4107        return $retVal;
4108    }
4109    
4110    =head3 SectionList
4111    
4112        my @sections = $erdb->SectionList($fig);
4113    
4114    Return a list of the names for the different data sections used when loading this database.
4115    The default is a single string, in which case there is only one section representing the
4116    entire database.
4117    
4118    =cut
4119    
4120    sub SectionList {
4121        # Get the parameters.
4122        my ($self, $source) = @_;
4123        # Ask the BaseSproutLoader for a section list.
4124        require BaseSproutLoader;
4125        my @retVal = BaseSproutLoader::GetSectionList($self, $source);
4126        # Return the list.
4127        return @retVal;
4128    }
4129    
4130    =head3 Loader
4131    
4132        my $groupLoader = $erdb->Loader($groupName, $options);
4133    
4134    Return an [[ERDBLoadGroupPm]] object for the specified load group. This method is used
4135    by [[ERDBGeneratorPl]] to create the load group objects. If you are not using
4136    [[ERDBGeneratorPl]], you don't need to override this method.
4137    
4138    =over 4
4139    
4140    =item groupName
4141    
4142    Name of the load group whose object is to be returned. The group name is
4143    guaranteed to be a single word with only the first letter capitalized.
4144    
4145    =item options
4146    
4147    Reference to a hash of command-line options.
4148    
4149    =item RETURN
4150    
4151    Returns an [[ERDBLoadGroupPm]] object that can be used to process the specified load group
4152    for this database.
4153    
4154    =back
4155    
4156    =cut
4157    
4158    sub Loader {
4159        # Get the parameters.
4160        my ($self, $groupName, $options) = @_;
4161        # Compute the loader name.
4162        my $loaderClass = "${groupName}SproutLoader";
4163        # Pull in its definition.
4164        require "$loaderClass.pm";
4165        # Create an object for it.
4166        my $retVal = eval("$loaderClass->new(\$self, \$options)");
4167        # Insure it worked.
4168        Confess("Could not create $loaderClass object: $@") if $@;
4169        # Return it to the caller.
4170        return $retVal;
4171    }
4172    
4173    
4174    =head3 LoadGroupList
4175    
4176        my @groups = $erdb->LoadGroupList();
4177    
4178    Returns a list of the names for this database's load groups. This method is used
4179    by [[ERDBGeneratorPl]] when the user wishes to load all table groups. The default
4180    is a single group called 'All' that loads everything.
4181    
4182    =cut
4183    
4184    sub LoadGroupList {
4185        # Return the list.
4186        return qw(Feature Subsystem Genome Annotation Property Source Reaction Synonym Drug);
4187    }
4188    
4189    =head3 LoadDirectory
4190    
4191        my $dirName = $erdb->LoadDirectory();
4192    
4193    Return the name of the directory in which load files are kept. The default is
4194    the FIG temporary directory, which is a really bad choice, but it's always there.
4195    
4196    =cut
4197    
4198    sub LoadDirectory {
4199        # Get the parameters.
4200        my ($self) = @_;
4201        # Return the directory name.
4202        return $self->{dataDir};
4203    }
4204    
4205  =head2 Internal Utility Methods  =head2 Internal Utility Methods
4206    
4207    =head3 GetStemmer
4208    
4209        my $stermmer = $sprout->GetStemmer();
4210    
4211    Return the stemmer object for this database.
4212    
4213    =cut
4214    
4215    sub GetStemmer {
4216        # Get the parameters.
4217        my ($self) = @_;
4218        # Declare the return variable.
4219        my $retVal = $self->{stemmer};
4220        if (! defined $retVal) {
4221            # We don't have one pre-built, so we build and save it now.
4222            $retVal = BioWords->new(exceptions => "$FIG_Config::sproutData/Exceptions.txt",
4223                                     stops => "$FIG_Config::sproutData/StopWords.txt",
4224                                     cache => 0);
4225            $self->{stemmer} = $retVal;
4226        }
4227        # Return the result.
4228        return $retVal;
4229    }
4230    
4231  =head3 ParseAssignment  =head3 ParseAssignment
4232    
4233  Parse annotation text to determine whether or not it is a functional assignment. If it is,  Parse annotation text to determine whether or not it is a functional assignment. If it is,
# Line 3901  Line 4314 
4314      # Get the parameters.      # Get the parameters.
4315      my ($self, $fid) = @_;      my ($self, $fid) = @_;
4316      # Insure we have a genome hash.      # Insure we have a genome hash.
4317      if (! defined $self->{genomeHash}) {      my $genomes = $self->_GenomeHash();
         my %genomeHash = map { $_ => 1 } $self->GetFlat(['Genome'], "", [], 'Genome(id)');  
         $self->{genomeHash} = \%genomeHash;  
     }  
4318      # Get the feature's genome ID.      # Get the feature's genome ID.
4319      my ($genomeID) = FIGRules::ParseFeatureID($fid);      my ($genomeID) = FIGRules::ParseFeatureID($fid);
4320      # Return an indicator of whether or not the genome ID is in the hash.      # Return an indicator of whether or not the genome ID is in the hash.
# Line 3938  Line 4348 
4348  }  }
4349    
4350    
4351    =head3 Hint
4352    
4353        my $htmlText = Sprout::Hint($wikiPage, $hintID);
4354    
4355    Return the HTML for a help link that displays the specified hint text when it is clicked.
4356    This HTML can be put in forms to provide a useful hinting mechanism.
4357    
4358    =over 4
4359    
4360    =item wikiPage
4361    
4362    Name of the wiki page to be popped up when the hint mark is clicked.
4363    
4364    =item hintID
4365    
4366    ID of the text to display for the hint. This should correspond to a tip number
4367    in the Wiki.
4368    
4369    =item RETURN
4370    
4371    Returns the html for the hint facility. The resulting html shows the word "help" and
4372    uses the standard FIG popup technology.
4373    
4374    =back
4375    
4376    =cut
4377    
4378    sub Hint {
4379        # Get the parameters.
4380        my ($wikiPage, $hintID) = @_;
4381        # Declare the return variable.
4382        my $retVal;
4383        # Convert the wiki page name to a URL.
4384        my $wikiURL;
4385        if ($wikiPage =~ m#/#) {
4386            # Here it's a URL of some sort.
4387            $wikiURL = $wikiPage;
4388        } else {
4389            # Here it's a wiki page.
4390            my $page = join("", map { ucfirst $_ } split /\s+/, $wikiPage);
4391            if ($page =~ /^(.+?)\.(.+)$/) {
4392                $page = "$1/$2";
4393            } else {
4394                $page = "FIG/$page";
4395            }
4396            $wikiURL = "$FIG_Config::cgi_url/wiki/view.cgi/$page";
4397        }
4398        # Is there hint text?
4399        if (! $hintID) {
4400            # No. Create a new-page hint.
4401            $retVal = qq(&nbsp;<a class="hint" onclick="doPagePopup(this, '$wikiURL')">(help)</a>);
4402        } else {
4403            # With hint text, we create a popup window hint. We need to compute the hint URL.
4404            my $tipURL = "$FIG_Config::cgi_url/wiki/view.cgi/FIG/TWikiCustomTip" .
4405                Tracer::Pad($hintID, 3, 1, "0");
4406            # Create a hint pop-up link.
4407            $retVal = qq(&nbsp;<a class="hint" onclick="doHintPopup(this, '$wikiURL', '$tipURL')">(help)</a>);
4408        }
4409        # Return the HTML.
4410        return $retVal;
4411    }
4412    
4413    =head3 _GenomeHash
4414    
4415        my $gHash = $sprout->_GenomeHash();
4416    
4417    Return a hash mapping all NMPDR genome IDs to [[ERDBObjectPm]] genome objects.
4418    
4419    =cut
4420    
4421    sub _GenomeHash {
4422        # Get the parameters.
4423        my ($self) = @_;
4424        # Do we already have a filled hash?
4425        if (! $self->{genomeHashFilled}) {
4426            # No, create it.
4427            my %gHash = map { $_->PrimaryValue('id') => $_ } $self->GetList("Genome", "", []);
4428            $self->{genomeHash} = \%gHash;
4429            # Denote we have it.
4430            $self->{genomeHashFilled} = 1;
4431        }
4432        # Return the hash.
4433        return $self->{genomeHash};
4434    }
4435    
4436    =head3 _GenomeData
4437    
4438        my $genomeData = $sprout->_GenomeData($genomeID);
4439    
4440    Return an [[ERDBObjectPm]] object for the specified genome, or an undefined
4441    value if the genome does not exist.
4442    
4443    =over 4
4444    
4445    =item genomeID
4446    
4447    ID of the desired genome.
4448    
4449    =item RETURN
4450    
4451    Returns either an [[ERDBObjectPm]] containing the genome, or an undefined value.
4452    If the genome exists, it will have been read into the genome cache.
4453    
4454    =back
4455    
4456    =cut
4457    
4458    sub _GenomeData {
4459        # Get the parameters.
4460        my ($self, $genomeID) = @_;
4461        # Are we in the genome hash?
4462        if (! exists $self->{genomeHash}->{$genomeID} && ! $self->{genomeHashFilled}) {
4463            # The genome isn't in the hash, and the hash is not complete, so we try to
4464            # read it.
4465            $self->{genomeHash}->{$genomeID} = $self->GetEntity(Genome => $genomeID);
4466        }
4467        # Return the result.
4468        return $self->{genomeHash}->{$genomeID};
4469    }
4470    
4471    =head3 _CacheGenome
4472    
4473        $sprout->_CacheGenome($genomeID, $genomeData);
4474    
4475    Store the specified genome object in the genome cache if it is already there.
4476    
4477    =over 4
4478    
4479    =item genomeID
4480    
4481    ID of the genome to store in the cache.
4482    
4483    =item genomeData
4484    
4485    An [[ERDBObjectPm]] containing at least the data for the specified genome.
4486    Note that the Genome may not be the primary object in it, so a fully-qualified
4487    field name has to be used to retrieve data from it.
4488    
4489    =back
4490    
4491    =cut
4492    
4493    sub _CacheGenome {
4494        # Get the parameters.
4495        my ($self, $genomeID, $genomeData) = @_;
4496        # Only proceed if we don't already have the genome.
4497        if (! exists $self->{genomeHash}->{$genomeID}) {
4498            $self->{genomeHash}->{$genomeID} = $genomeData;
4499        }
4500    }
4501    
4502  1;  1;

Legend:
Removed from v.1.104  
changed lines
  Added in v.1.126

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3