[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.108, Thu Feb 14 19:13:33 2008 UTC revision 1.124, Wed Mar 4 00:09:43 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 88  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 105  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::sprout_host,                         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 129  Line 149 
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;      return $retVal;
197  }  }
198    
# Line 308  Line 354 
354  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
355  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
356  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
357  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.
358    
359  =over 4  =over 4
360    
# Line 465  Line 511 
511      return $retVal;      return $retVal;
512  }  }
513    
514  =head3 GeneMenu  =head3 GenomeMenu
515    
516      my $selectHtml = $sprout->GeneMenu(\%attributes, $filterString, \@params, $selected);      my $html = $sprout->GenomeMenu(%options);
517    
518  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.
519  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
520  value will be the genome ID. The genomes will be sorted by genus/species name.  B<SearchHelper> class. Eventually, the two will be combined.
521    
522  =over 4  =over 4
523    
524  =item attributes  =item options
525    
526  Reference to a hash mapping attributes to values for the SELECT tag generated.  Optional parameters for the control (see below).
527    
528  =item filterString  =item RETURN
529    
530  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.  
531    
532  =item params  =back
533    
534  Reference to a list of values to be substituted in for the parameter marks in  The valid options are as follows.
 the filter string.  
535    
536  =item selected (optional)  =over 4
537    
538  ID of the genome to be initially selected.  =item name
539    
540  =item fast (optional)  Name to give this control for use in passing it to the form. The default is C<myGenomeControl>.
541    Terrible things will happen if you have two controls with the same name on the same page.
542    
543  If specified and TRUE, the contig counts will be omitted to improve performance.  =item filter
544    
545  =item RETURN  If specified, a filter for the list of genomes to display. The filter should be in the form of a
546    list reference, a string, or a hash reference. If it is a list reference, the first element
547    of the list should be the filter string, and the remaining elements the filter parameters. If it is a
548    string, it will be split into a list at each included tab. If it is a hash reference, it should be
549    a hash that maps genomes which should be included to a TRUE value.
550    
551    =item multiSelect
552    
553    If TRUE, then the user can select multiple genomes. If FALSE, the user can only select one genome.
554    
555  Returns an HTML select menu with the specified genomes as selectable options.  =item size
556    
557    Number of rows to display in the control. The default is C<10>
558    
559    =item id
560    
561    ID to give this control. The default is the value of the C<name> option. Nothing will work correctly
562    unless this ID is unique.
563    
564    =item selected
565    
566    A comma-delimited list of selected genomes, or a reference to a list of selected genomes. The
567    default is none.
568    
569    =item class
570    
571    If specified, a style class to assign to the genome control.
572    
573  =back  =back
574    
575  =cut  =cut
576    
577  sub GeneMenu {  sub GenomeMenu {
578      # Get the parameters.      # Get the parameters.
579      my ($self, $attributes, $filterString, $params, $selected, $fast) = @_;      my ($self, %options) = @_;
580      my $slowMode = ! $fast;      # Get the control's name and ID.
581      # Default to nothing selected. This prevents an execution warning if "$selected"      my $menuName = $options{name} || $options{id} || 'myGenomeControl';
582      # is undefined.      my $menuID = $options{id} || $menuName;
583      $selected = "" unless defined $selected;      Trace("Genome menu name = $menuName with ID $menuID.") if T(3);
584      Trace("Gene Menu called with slow mode \"$slowMode\" and selection \"$selected\".") if T(3);      # Compute the IDs for the status display.
585      # Start the menu.      my $divID = "${menuID}_status";
586      my $retVal = "<select " .      my $urlID = "${menuID}_url";
587          join(" ", map { "$_=\"$attributes->{$_}\"" } keys %{$attributes}) .      # Compute the code to show selected genomes in the status area.
588          ">\n";      my $showSelect = "showSelected('$menuID', '$divID', '$urlID', $FIG_Config::genome_control_cap)";
589      # Get the genomes.      # Check for single-select or multi-select.
590      my @genomes = $self->GetAll(['Genome'], $filterString, $params, ['Genome(id)',      my $multiSelect = $options{multiSelect} || 0;
591                                                                       'Genome(genus)',      # Get the style data.
592                                                                       'Genome(species)',      my $class = $options{class} || '';
593                                                                       'Genome(unique-characterization)']);      # Get the list of pre-selected items.
594      # Sort them by name.      my $selections = $options{selected} || [];
595      my @sorted = sort { lc("$a->[1] $a->[2]") cmp lc("$b->[1] $b->[2]") } @genomes;      if (ref $selections ne 'ARRAY') {
596      # Loop through the genomes, creating the option tags.          $selections = [ split /\s*,\s*/, $selections ];
597      for my $genomeData (@sorted) {      }
598          # Get the data for this genome.      my %selected = map { $_ => 1 } @{$selections};
599          my ($genomeID, $genus, $species, $strain) = @{$genomeData};      # Extract the filter information. The default is no filtering. It can be passed as a tab-delimited
600          # Get the contig count.      # string, a hash reference, or a list reference.
601          my $contigInfo = "";      my ($filterHash, $filterString);
602          if ($slowMode) {      my $filterParms = $options{filter} || "";
603              my $count = $self->ContigCount($genomeID);      if (ref $filterParms eq 'HASH') {
604              my $counting = ($count == 1 ? "contig" : "contigs");          $filterHash = $filterParms;
605              $contigInfo = "[$count $counting]";          $filterParms = [];
606          }          $filterString = "";
607          # Find out if we're selected.      } else {
608          my $selectOption = ($selected eq $genomeID ? " selected" : "");          if (! ref $filterParms) {
609          # Build the option tag.              $filterParms = [split /\t|\\t/, $filterParms];
610          $retVal .= "<option value=\"$genomeID\"$selectOption>$genus $species $strain ($genomeID)$contigInfo</option>\n";          }
611            $filterString = shift @{$filterParms};
612        }
613        # Check for possible subsystem filtering. If there is one, we will tack the
614        # relationship onto the object name list.
615        my @objectNames = qw(Genome);
616        if ($filterString =~ /ParticipatesIn\(/) {
617            push @objectNames, 'ParticipatesIn';
618        }
619        # Get a list of all the genomes in group order. In fact, we only need them ordered
620        # by name (genus,species,strain), but putting primary-group in front enables us to
621        # take advantage of an existing index.
622        my @genomeList = $self->GetAll(\@objectNames, "$filterString ORDER BY Genome(primary-group), Genome(genus), Genome(species), Genome(unique-characterization)",
623                                       $filterParms,
624                                       [qw(Genome(primary-group) Genome(id) Genome(genus) Genome(species) Genome(unique-characterization) Genome(taxonomy) Genome(contigs))]);
625        # Apply the hash filter (if any).
626        if (defined $filterHash) {
627            @genomeList = grep { $filterHash->{$_->[1]} } @genomeList;
628        }
629        # Create a hash to organize the genomes by group. Each group will contain a list of
630        # 2-tuples, the first element being the genome ID and the second being the genome
631        # name.
632        my %gHash = ();
633        for my $genome (@genomeList) {
634            # Get the genome data.
635            my ($group, $genomeID, $genus, $species, $strain, $taxonomy, $contigs) = @{$genome};
636            # Compute its name. This is the genus, species, strain (if any), and the contig count.
637            my $name = "$genus $species ";
638            $name .= "$strain " if $strain;
639            my $contigCount = ($contigs == 1 ? "" : ", $contigs contigs");
640            # Now we get the domain. The domain tells us the display style of the organism.
641            my ($domain) = split /\s*;\s*/, $taxonomy, 2;
642            # Now compute the display group. This is normally the primary group, but if the
643            # organism is supporting, we blank it out.
644            my $displayGroup = ($group eq $FIG_Config::otherGroup ? "" : $group);
645            # Push the genome into the group's list. Note that we use the real group
646            # name for the hash key here, not the display group name.
647            push @{$gHash{$group}}, [$genomeID, $name, $contigCount, $domain];
648        }
649        # We are almost ready to unroll the menu out of the group hash. The final step is to separate
650        # the supporting genomes by domain. First, we extract the NMPDR groups and sort them. They
651        # are sorted by the first capitalized word. Groups with "other" are sorted after groups
652        # that aren't "other". At some point, we will want to make this less complicated.
653        my %sortGroups = map { $_ =~ /(other)?(.*)([A-Z].+)/; "$3$1$2" => $_ }
654                             grep { $_ ne $FIG_Config::otherGroup } keys %gHash;
655        my @groups = map { $sortGroups{$_} } sort keys %sortGroups;
656        # Remember the number of NMPDR groups.
657        my $nmpdrGroupCount = scalar @groups;
658        # Are there any supporting genomes?
659        if (exists $gHash{$FIG_Config::otherGroup}) {
660            # Loop through the supporting genomes, classifying them by domain. We'll also keep a list
661            # of the domains found.
662            my @otherGenomes = @{$gHash{$FIG_Config::otherGroup}};
663            my @domains = ();
664            for my $genomeData (@otherGenomes) {
665                my ($genomeID, $name, $contigCount, $domain) = @{$genomeData};
666                if (exists $gHash{$domain}) {
667                    push @{$gHash{$domain}}, $genomeData;
668                } else {
669                    $gHash{$domain} = [$genomeData];
670                    push @domains, $domain;
671                }
672            }
673            # Add the domain groups at the end of the main group list. The main group list will now
674            # contain all the categories we need to display the genomes.
675            push @groups, sort @domains;
676            # Delete the supporting group.
677            delete $gHash{$FIG_Config::otherGroup};
678        }
679        # Now it gets complicated. We need a way to mark all the NMPDR genomes. We take advantage
680        # of the fact they come first in the list. We'll accumulate a count of the NMPDR genomes
681        # and use that to make the selections.
682        my $nmpdrCount = 0;
683        # Create the type counters.
684        my $groupCount = 1;
685        # Get the number of rows to display.
686        my $rows = $options{size} || 10;
687        # If we're multi-row, create an onChange event.
688        my $onChangeTag = ( $rows > 1 ? " onChange=\"$showSelect;\" onFocus=\"$showSelect;\"" : "" );
689        # Set up the multiple-select flag.
690        my $multipleTag = ($multiSelect ? " multiple" : "" );
691        # Set up the style class.
692        my $classTag = ($class ? " $class" : "" );
693        # Create the SELECT tag and stuff it into the output array.
694        my @lines = qq(<SELECT name="$menuName" id="$menuID" class="genomeSelect $class" $onChangeTag$multipleTag$classTag size="$rows">);
695        # Loop through the groups.
696        for my $group (@groups) {
697            # Get the genomes in the group.
698            for my $genome (@{$gHash{$group}}) {
699                # If this is an NMPDR organism, we add an extra style and count it.
700                my $nmpdrStyle = "";
701                if ($nmpdrGroupCount > 0) {
702                    $nmpdrCount++;
703                    $nmpdrStyle = " Core";
704                }
705                # Get the organism ID, name, contig count, and domain.
706                my ($genomeID, $name, $contigCount, $domain) = @{$genome};
707                # See if we're pre-selected.
708                my $selectTag = ($selected{$genomeID} ? " SELECTED" : "");
709                # Compute the display name.
710                my $nameString = "$name ($genomeID$contigCount)";
711                # Generate the option tag.
712                my $optionTag = "<OPTION class=\"$domain$nmpdrStyle\" title=\"$group\" value=\"$genomeID\"$selectTag>$nameString</OPTION>";
713                push @lines, "    $optionTag";
714            }
715            # Record this group in the nmpdrGroup count. When that gets to 0, we've finished the NMPDR
716            # groups.
717            $nmpdrGroupCount--;
718      }      }
719      # Close the SELECT tag.      # Close the SELECT tag.
720      $retVal .= "</select>\n";      push @lines, "</SELECT>";
721        if ($rows > 1) {
722            # We're in a non-compact mode, so we need to add some selection helpers. First is
723            # the search box. This allows the user to type text and change which genomes are
724            # displayed. For multiple-select mode, we include a button that selects the displayed
725            # genes. For single-select mode, we use a plain label instead.
726            my $searchThingName = "${menuID}_SearchThing";
727            my $searchThingLabel = "Type to narrow selection";
728            my $searchThingButton = "";
729            my $goHint = "";
730            if ($multiSelect) {
731                $searchThingButton = qq(<INPUT type="button" name="MacroSearch" class="button" value="Go" onClick="selectShowing('$menuID', '$searchThingName'); $showSelect;" />);
732                $goHint = " Click <strong>Go</strong> to select them.";
733            }
734            push @lines, "<br />$searchThingLabel&nbsp;" .
735                         qq(<INPUT type="text" id="$searchThingName" name="$searchThingName" class="genomeSearchThing" onKeyup="showTyped('$menuID', '$searchThingName');" />) .
736                         $searchThingButton .
737                         Hint("GenomeControl", "Type a genome ID or part of an organism name to filter the genomes displayed.$goHint") . "<br />";
738            # For multi-select mode, we also have buttons to set and clear selections.
739            if ($multiSelect) {
740                push @lines, qq(<INPUT type="button" name="ClearAll" class="bigButton genomeButton" value="Clear All" onClick="clearAll(getElementById('$menuID')); $showSelect" />);
741                push @lines, qq(<INPUT type="button" name="SelectAll" class="bigButton genomeButton" value="Select All" onClick="selectAll(getElementById('$menuID')); $showSelect" />);
742                push @lines, qq(<INPUT type="button" name="NMPDROnly" class="bigButton genomeButton" value="Select NMPDR" onClick="selectSome(getElementById('$menuID'), $nmpdrCount, true); $showSelect;" />);
743            }
744            # Add a hidden field we can use to generate organism page hyperlinks.
745            push @lines, qq(<INPUT type="hidden" id="$urlID" value="$FIG_Config::cgi_url/wiki/rest.cgi/NmpdrPlugin/SeedViewer?page=Organism;organism=" />);
746            # Add the status display. This tells the user what's selected no matter where the list is scrolled.
747            push @lines, qq(<DIV id="$divID" class="Panel"></DIV>);
748        }
749        # Assemble all the lines into a string.
750        my $retVal = join("\n", @lines, "");
751        # Return the result.
752        return $retVal;
753    }
754    
755    
756    =head3 Stem
757    
758        my $stem = $sprout->Stem($word);
759    
760    Return the stem of the specified word, or C<undef> if the word is not
761    stemmable. Note that even if the word is stemmable, the stem may be
762    the same as the original word.
763    
764    =over 4
765    
766    =item word
767    
768    Word to convert into a stem.
769    
770    =item RETURN
771    
772    Returns a stem of the word (which may be the word itself), or C<undef> if
773    the word is not stemmable.
774    
775    =back
776    
777    =cut
778    
779    sub Stem {
780        # Get the parameters.
781        my ($self, $word) = @_;
782        # Get the stemmer object.
783        my $stemmer = $self->{stemmer};
784        if (! defined $stemmer) {
785            # We don't have one pre-built, so we build and save it now.
786            $stemmer = BioWords->new(exceptions => "$FIG_Config::sproutData/Exceptions.txt",
787                                     stops => "$FIG_Config::sproutData/StopWords.txt",
788                                     cache => 0);
789            $self->{stemmer} = $stemmer;
790        }
791        # Try to stem the word.
792        my $retVal = $stemmer->Process($word);
793      # Return the result.      # Return the result.
794      return $retVal;      return $retVal;
795  }  }
796    
797    
798  =head3 Build  =head3 Build
799    
800      $sprout->Build();      $sprout->Build();
# Line 604  Line 853 
853  sub GenusSpecies {  sub GenusSpecies {
854      # Get the parameters.      # Get the parameters.
855      my ($self, $genomeID) = @_;      my ($self, $genomeID) = @_;
856      # Get the data for the specified genome.      # Declare the return value.
857      my @values = $self->GetEntityValues('Genome', $genomeID, ['Genome(genus)', 'Genome(species)',      my $retVal;
858                                                                'Genome(unique-characterization)']);      # Get the genome data.
859      # Format the result and return it.      my $genomeData = $self->_GenomeData($genomeID);
860      my $retVal = join(' ', @values);      # Only proceed if we found the genome.
861        if (defined $genomeData) {
862            $retVal = $genomeData->PrimaryValue('Genome(scientific-name)');
863        }
864        # Return it.
865      return $retVal;      return $retVal;
866  }  }
867    
# Line 683  Line 936 
936  =item RETURN  =item RETURN
937    
938  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
939  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
940    wasn't found.
941    
942  =back  =back
943    
# Line 692  Line 946 
946  sub FeatureLocation {  sub FeatureLocation {
947      # Get the parameters.      # Get the parameters.
948      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
949        # Declare the return variable.
950        my @retVal = ();
951      # Get the feature record.      # Get the feature record.
952      my $object = $self->GetEntity('Feature', $featureID);      my $object = $self->GetEntity('Feature', $featureID);
953      Confess("Feature $featureID not found.") if ! defined($object);      # Only proceed if we found it.
954        if (defined $object) {
955      # Get the location string.      # Get the location string.
956      my $locString = $object->PrimaryValue('Feature(location-string)');      my $locString = $object->PrimaryValue('Feature(location-string)');
957      # Create the return list.      # Create the return list.
958      my @retVal = split /\s*,\s*/, $locString;          @retVal = split /\s*,\s*/, $locString;
959        }
960      # Return the list in the format indicated by the context.      # Return the list in the format indicated by the context.
961      return (wantarray ? @retVal : join(',', @retVal));      return (wantarray ? @retVal : join(',', @retVal));
962  }  }
# Line 941  Line 1199 
1199      my ($self, $genomeID) = @_;      my ($self, $genomeID) = @_;
1200      # Declare the return variable.      # Declare the return variable.
1201      my $retVal = 0;      my $retVal = 0;
1202      # Get the genome's contig sequence lengths.      # Get the genome data.
1203      my @lens = $self->GetFlat(['HasContig', 'IsMadeUpOf'], 'HasContig(from-link) = ?',      my $genomeData = $self->_GenomeData($genomeID);
1204                         [$genomeID], 'IsMadeUpOf(len)');      # Only proceed if it exists.
1205      # Sum the lengths.      if (defined $genomeData) {
1206      map { $retVal += $_ } @lens;          $retVal = $genomeData->PrimaryValue('Genome(dna-size)');
1207        }
1208      # Return the result.      # Return the result.
1209      return $retVal;      return $retVal;
1210  }  }
# Line 1430  Line 1689 
1689  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
1690  is trusted.  is trusted.
1691    
1692  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.
1693  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.  
1694    
1695  =over 4  =over 4
1696    
# Line 1458  Line 1716 
1716      my ($self, $featureID, $userID) = @_;      my ($self, $featureID, $userID) = @_;
1717      # Declare the return value.      # Declare the return value.
1718      my $retVal;      my $retVal;
1719      # Determine the ID type.      # Find a FIG ID for this feature.
1720      if ($featureID =~ m/^fig\|/) {      my ($fid) = $self->FeaturesByAlias($featureID);
1721        # Only proceed if we have an ID.
1722        if ($fid) {
1723          # Here we have a FIG feature ID.          # Here we have a FIG feature ID.
1724          if (!$userID) {          if (!$userID) {
1725              # Use the primary assignment.              # Use the primary assignment.
1726              ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(assignment)']);              ($retVal) = $self->GetEntityValues('Feature', $fid, ['Feature(assignment)']);
1727          } else {          } else {
1728              # We must build the list of trusted users.              # We must build the list of trusted users.
1729              my %trusteeTable = ();              my %trusteeTable = ();
# Line 1489  Line 1749 
1749              # 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.
1750              my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],              my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1751                                     "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",                                     "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1752                                     [$featureID]);                                     [$fid]);
1753              my $timeSelected = 0;              my $timeSelected = 0;
1754              # Loop until we run out of annotations.              # Loop until we run out of annotations.
1755              while (my $annotation = $query->Fetch()) {              while (my $annotation = $query->Fetch()) {
# Line 1509  Line 1769 
1769                  }                  }
1770              }              }
1771          }          }
     } 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)']);  
1772      }      }
1773      # Return the assignment found.      # Return the assignment found.
1774      return $retVal;      return $retVal;
# Line 1532  Line 1787 
1787  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
1788  most features only have a small number of annotations.  most features only have a small number of annotations.
1789    
 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.  
   
1790  =over 4  =over 4
1791    
1792  =item featureID  =item featureID
# Line 1556  Line 1807 
1807      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
1808      # Declare the return value.      # Declare the return value.
1809      my @retVal = ();      my @retVal = ();
1810      # Determine the ID type.      # Convert to a FIG ID.
1811      if ($featureID =~ m/^fig\|/) {      my ($fid) = $self->FeaturesByAlias($featureID);
1812        # Only proceed if we found one.
1813        if ($fid) {
1814          # 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
1815          # users.          # users.
1816          my %trusteeTable = ();          my %trusteeTable = ();
1817          # 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.
1818          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1819                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1820                                 [$featureID]);                                 [$fid]);
1821          my $timeSelected = 0;          my $timeSelected = 0;
1822          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1823          while (my $annotation = $query->Fetch()) {          while (my $annotation = $query->Fetch()) {
# Line 1579  Line 1832 
1832                  push @retVal, [$actualUser, $function];                  push @retVal, [$actualUser, $function];
1833              }              }
1834          }          }
     } 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;  
1835      }      }
1836      # Return the assignments found.      # Return the assignments found.
1837      return @retVal;      return @retVal;
# Line 1625  Line 1871 
1871      # Loop through the incoming features.      # Loop through the incoming features.
1872      for my $featureID (@{$featureList}) {      for my $featureID (@{$featureList}) {
1873          # Ask the server for the feature's best hit.          # Ask the server for the feature's best hit.
1874          my @bbhData = FIGRules::BBHData($featureID);          my $bbhData = FIGRules::BBHData($featureID);
1875          # Peel off the BBHs found.          # Peel off the BBHs found.
1876          my @found = ();          my @found = ();
1877          for my $bbh (@bbhData) {          for my $bbh (@$bbhData) {
1878              my $fid = $bbh->[0];              my $fid = $bbh->[0];
1879              my $bbGenome = $self->GenomeOf($fid);              my $bbGenome = $self->GenomeOf($fid);
1880              if ($bbGenome eq $genomeID) {              if ($bbGenome eq $genomeID) {
# Line 1667  Line 1913 
1913      # Get the parameters.      # Get the parameters.
1914      my ($self, $featureID, $count) = @_;      my ($self, $featureID, $count) = @_;
1915      # Ask for the best hits.      # Ask for the best hits.
1916      my @lists = FIGRules::BBHData($featureID);      my $lists = FIGRules::BBHData($featureID);
1917      # Create the return value.      # Create the return value.
1918      my %retVal = ();      my %retVal = ();
1919      for my $tuple (@lists) {      for my $tuple (@$lists) {
1920          $retVal{$tuple->[0]} = $tuple->[1];          $retVal{$tuple->[0]} = $tuple->[1];
1921      }      }
1922      # Return the result.      # Return the result.
# Line 1704  Line 1950 
1950      # Declare the return variable.      # Declare the return variable.
1951      my $retVal;      my $retVal;
1952      # Get the genome's data.      # Get the genome's data.
1953      my $genomeData = $self->GetEntity('Genome', $genomeID);      my $genomeData = $self->_GenomeData($genomeID);
1954      if ($genomeData) {      # Only proceed if it exists.
1955        if (defined $genomeData) {
1956          # The genome exists, so get the completeness flag.          # The genome exists, so get the completeness flag.
1957          $retVal = $genomeData->PrimaryValue('Genome(complete)');          $retVal = $genomeData->PrimaryValue('Genome(complete)');
1958      }      }
# Line 1773  Line 2020 
2020      if ($featureID =~ /^fig\|(\d+\.\d+)/) {      if ($featureID =~ /^fig\|(\d+\.\d+)/) {
2021          $retVal = $1;          $retVal = $1;
2022      } else {      } else {
2023          Confess("Invalid feature ID $featureID.");          # Find the feature by alias.
2024            my ($realFeatureID) = $self->FeaturesByAlias($featureID);
2025            if ($realFeatureID && $realFeatureID =~ /^fig\|(\d+\.\d+)/) {
2026                $retVal = $1;
2027            }
2028      }      }
2029      # Return the value found.      # Return the value found.
2030      return $retVal;      return $retVal;
# Line 1817  Line 2068 
2068              $retVal{$featureID2} = $score;              $retVal{$featureID2} = $score;
2069          }          }
2070      }      }
     # 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;  
     }  
2071      # Return the hash.      # Return the hash.
2072      return %retVal;      return %retVal;
2073  }  }
# Line 2178  Line 2424 
2424  sub GetGenomeNameData {  sub GetGenomeNameData {
2425      # Get the parameters.      # Get the parameters.
2426      my ($self, $genomeID) = @_;      my ($self, $genomeID) = @_;
2427        # Declare the return variables.
2428        my ($genus, $species, $strain);
2429        # Get the genome's data.
2430        my $genomeData = $self->_GenomeData($genomeID);
2431        # Only proceed if the genome exists.
2432        if (defined $genomeData) {
2433      # Get the desired values.      # Get the desired values.
2434      my ($genus, $species, $strain) = $self->GetEntityValues('Genome', $genomeID =>          ($genus, $species, $strain) = $genomeData->Values(['Genome(genus)',
2435                                                              [qw(Genome(genus) Genome(species) Genome(unique-characterization))]);                                                             'Genome(species)',
2436      # Throw an error if they were not found.                                                             'Genome(unique-characterization)']);
2437      if (! defined $genus) {      } else {
2438            # Throw an error because they were not found.
2439          Confess("Genome $genomeID not found in database.");          Confess("Genome $genomeID not found in database.");
2440      }      }
2441      # Return the results.      # Return the results.
# Line 2483  Line 2736 
2736  sub Taxonomy {  sub Taxonomy {
2737      # Get the parameters.      # Get the parameters.
2738      my ($self, $genome) = @_;      my ($self, $genome) = @_;
     # Find the specified genome's taxonomy string.  
     my ($list) = $self->GetEntityValues('Genome', $genome, ['Genome(taxonomy)']);  
2739      # Declare the return variable.      # Declare the return variable.
2740      my @retVal = ();      my @retVal = ();
2741      # If we found the genome, return its taxonomy string.      # Get the genome data.
2742      if ($list) {      my $genomeData = $self->_GenomeData($genome);
2743          @retVal = split /\s*;\s*/, $list;      # Only proceed if it exists.
2744        if (defined $genomeData) {
2745            # Create the taxonomy from the taxonomy string.
2746            @retVal = split /\s*;\s*/, $genomeData->PrimaryValue('Genome(taxonomy)');
2747      } else {      } else {
2748            # Genome doesn't exist, so emit a warning.
2749          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);
2750      }      }
2751      # Return the value found.      # Return the value found.
# Line 2535  Line 2790 
2790      }      }
2791      my @taxA = $self->Taxonomy($genomeA);      my @taxA = $self->Taxonomy($genomeA);
2792      my @taxB = $self->Taxonomy($genomeB);      my @taxB = $self->Taxonomy($genomeB);
2793      # Initialize the distance to 1. We'll reduce it each time we find a match between the      # Compute the distance.
2794      # 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;  
     }  
2795      return $retVal;      return $retVal;
2796  }  }
2797    
# Line 2613  Line 2859 
2859      return @retVal;      return @retVal;
2860  }  }
2861    
 =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;  
 }  
   
2862  =head3 FeatureProperties  =head3 FeatureProperties
2863    
2864      my @properties = $sprout->FeatureProperties($featureID);      my @properties = $sprout->FeatureProperties($featureID);
# Line 2727  Line 2887 
2887      # Get the parameters.      # Get the parameters.
2888      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
2889      # Get the properties.      # Get the properties.
2890      my @attributes = $self->{_ca}->GetAttributes($featureID);      my @attributes = $self->ca->GetAttributes($featureID);
2891      # Strip the feature ID off each tuple.      # Strip the feature ID off each tuple.
2892      my @retVal = ();      my @retVal = ();
2893      for my $attributeRow (@attributes) {      for my $attributeRow (@attributes) {
# Line 2999  Line 3159 
3159  sub SubsystemList {  sub SubsystemList {
3160      # Get the parameters.      # Get the parameters.
3161      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
3162      # Get the list of names.      # Get the list of names. We do a join to the Subsystem table because we have missing subsystems in
3163      my @retVal = $self->GetFlat(['HasRoleInSubsystem'], "HasRoleInSubsystem(from-link) = ?",      # the Sprout database!
3164        my @retVal = $self->GetFlat(['HasRoleInSubsystem', 'Subsystem'], "HasRoleInSubsystem(from-link) = ?",
3165                                  [$featureID], 'HasRoleInSubsystem(to-link)');                                  [$featureID], 'HasRoleInSubsystem(to-link)');
3166      # Return the result, sorted.      # Return the result, sorted.
3167      return sort @retVal;      return sort @retVal;
# Line 3033  Line 3194 
3194      # Declare the return variable.      # Declare the return variable.
3195      my %retVal = ();      my %retVal = ();
3196      # 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
3197      # feature we get its spreadsheet cells and the corresponding roles.      # feature we get its subsystem ID and the corresponding roles.
3198      my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf'],      my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf', 'HasSSCell'],
3199                               "HasFeature(from-link) = ?", [$genomeID],                               "HasFeature(from-link) = ?", [$genomeID],
3200                               ['HasFeature(to-link)', 'IsRoleOf(to-link)', 'IsRoleOf(from-link)']);                                   ['HasFeature(to-link)', 'IsRoleOf(from-link)',  'HasSSCell(from-link)']);
3201      # 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
3202      # with an unknown variant code (-1) are skipped. Note the genome ID is at both ends of the      # a non-negative variant code.
3203      # list. We use it at the beginning to get all the spreadsheet cells for the genome and      my %subs = map { $_ => 1 } $self->GetFlat(['ParticipatesIn'],
3204      # again at the end to filter out participation in subsystems with a negative variant code.                                                  "ParticipatesIn(from-link) = ? AND ParticipatesIn(variant-code) >= 0",
3205      my @cellData = $self->GetAll(['IsGenomeOf', 'HasSSCell', 'ParticipatesIn'],                                                  [$genomeID], 'ParticipatesIn(to-link)');
3206                                   "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.  
3207      for my $roleEntry (@roleData) {      for my $roleEntry (@roleData) {
3208          # Get the data for this feature and cell.          # Get the data for this feature and cell.
3209          my ($fid, $cellID, $role) = @{$roleEntry};          my ($fid, $role, $subsys) = @{$roleEntry};
3210          # Check for a subsystem name.          Trace("Subsystem for $fid is $subsys.") if T(4);
3211          my $subsys = $subHash{$cellID};          # Check the subsystem;
3212          if ($subsys) {          if ($subs{$subsys}) {
3213                Trace("Subsystem found.") if T(4);
3214              # Insure this feature has an entry in the return hash.              # Insure this feature has an entry in the return hash.
3215              if (! exists $retVal{$fid}) { $retVal{$fid} = []; }              if (! exists $retVal{$fid}) { $retVal{$fid} = []; }
3216              # Merge in this new data.              # Merge in this new data.
# Line 3101  Line 3256 
3256      # Get the parameters.      # Get the parameters.
3257      my ($self, $featureID, $function, $userID) = @_;      my ($self, $featureID, $function, $userID) = @_;
3258      # 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.
3259      my @bbhFeatures = map { $_->[0] } FIGRules::BBHData($featureID);      my $bbhData = FIGRules::BBHData($featureID);
3260        my @bbhFeatures = map { $_->[0] } @$bbhData;
3261      # 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
3262      # functional assignment.      # functional assignment.
3263      my @retVal = ();      my @retVal = ();
# Line 3274  Line 3430 
3430    
3431  =head3 BBHMatrix  =head3 BBHMatrix
3432    
3433      my %bbhMap = $sprout->BBHMatrix($genomeID, $cutoff, @targets);      my $bbhMap = $sprout->BBHMatrix($genomeID, $cutoff, @targets);
3434    
3435  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
3436  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 3298  Line 3454 
3454    
3455  =item RETURN  =item RETURN
3456    
3457  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
3458  BBH pegs in the target genomes to their scores.  to a sub-hash mapping its BBH pegs in the target genomes to their scores.
3459    
3460  =back  =back
3461    
# Line 3312  Line 3468 
3468      my %retVal = ();      my %retVal = ();
3469      # Ask for the BBHs.      # Ask for the BBHs.
3470      my @bbhList = FIGRules::BatchBBHs("fig|$genomeID.%", $cutoff, @targets);      my @bbhList = FIGRules::BatchBBHs("fig|$genomeID.%", $cutoff, @targets);
3471        Trace("Retrieved " . scalar(@bbhList) . " BBH results.") if T(3);
3472      # 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.
3473      for my $bbhData (@bbhList) {      for my $bbhData (@bbhList) {
3474          my ($peg1, $peg2, $score) = @{$bbhData};          my ($peg1, $peg2, $score) = @{$bbhData};
# Line 3322  Line 3479 
3479          }          }
3480      }      }
3481      # Return the result.      # Return the result.
3482      return %retVal;      return \%retVal;
3483  }  }
3484    
3485    
# Line 3428  Line 3585 
3585      # Create the return hash.      # Create the return hash.
3586      my %retVal = ();      my %retVal = ();
3587      # Query for the desired BBHs.      # Query for the desired BBHs.
3588      my @bbhList = FIGRules::BBHData($featureID, $cutoff);      my $bbhList = FIGRules::BBHData($featureID, $cutoff);
3589      # Form the results into the return hash.      # Form the results into the return hash.
3590      for my $pair (@bbhList) {      for my $pair (@$bbhList) {
3591          my $fid = $pair->[0];          my $fid = $pair->[0];
3592          if ($self->Exists('Feature', $fid)) {          if ($self->Exists('Feature', $fid)) {
3593              $retVal{$fid} = $pair->[1];              $retVal{$fid} = $pair->[1];
# Line 3808  Line 3965 
3965      # Get the parameters.      # Get the parameters.
3966      my ($self, $featureID, $key, @values) = @_;      my ($self, $featureID, $key, @values) = @_;
3967      # Add the property using the attached attributes object.      # Add the property using the attached attributes object.
3968      $self->{_ca}->AddAttribute($featureID, $key, @values);      $self->ca->AddAttribute($featureID, $key, @values);
3969  }  }
3970    
3971  =head3 CheckGroupFile  =head3 CheckGroupFile
# Line 3895  Line 4052 
4052  sub CleanKeywords {  sub CleanKeywords {
4053      # Get the parameters.      # Get the parameters.
4054      my ($self, $searchExpression) = @_;      my ($self, $searchExpression) = @_;
4055      # Perform the standard cleanup.      # Get the stemmer.
4056      my $retVal = $self->ERDB::CleanKeywords($searchExpression);      my $stemmer = $self->GetStemmer();
4057      # Fix the periods in EC and TC numbers.      # Convert the search expression using the stemmer.
4058      $retVal =~ s/(\d+|\-)\.(\d+|-)\.(\d+|-)\.(\d+|-)/$1_$2_$3_$4/g;      my $retVal = $stemmer->PrepareSearchExpression($searchExpression);
4059      # 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;  
4060      # Return the result.      # Return the result.
4061      return $retVal;      return $retVal;
4062  }  }
4063    
4064    =head3 GetSourceObject
4065    
4066        my $source = $erdb->GetSourceObject();
4067    
4068    Return the object to be used in creating load files for this database.
4069    
4070    =cut
4071    
4072    sub GetSourceObject {
4073        # Get the parameters.
4074        my ($self) = @_;
4075        # Check to see if we already have a source object.
4076        my $retVal = $self->{_fig};
4077        if (! defined $retVal) {
4078            # No, so create one.
4079            require FIG;
4080            $retVal = FIG->new();
4081        }
4082        # Return the object.
4083        return $retVal;
4084    }
4085    
4086    =head3 SectionList
4087    
4088        my @sections = $erdb->SectionList();
4089    
4090    Return a list of the names for the different data sections used when loading this database.
4091    The default is a single string, in which case there is only one section representing the
4092    entire database.
4093    
4094    =cut
4095    
4096    sub SectionList {
4097        # Get the parameters.
4098        my ($self, $source) = @_;
4099        # Ask the BaseSproutLoader for a section list.
4100        require BaseSproutLoader;
4101        my @retVal = BaseSproutLoader::GetSectionList($self);
4102        # Return the list.
4103        return @retVal;
4104    }
4105    
4106    =head3 Loader
4107    
4108        my $groupLoader = $erdb->Loader($groupName, $options);
4109    
4110    Return an [[ERDBLoadGroupPm]] object for the specified load group. This method is used
4111    by [[ERDBGeneratorPl]] to create the load group objects. If you are not using
4112    [[ERDBGeneratorPl]], you don't need to override this method.
4113    
4114    =over 4
4115    
4116    =item groupName
4117    
4118    Name of the load group whose object is to be returned. The group name is
4119    guaranteed to be a single word with only the first letter capitalized.
4120    
4121    =item options
4122    
4123    Reference to a hash of command-line options.
4124    
4125    =item RETURN
4126    
4127    Returns an [[ERDBLoadGroupPm]] object that can be used to process the specified load group
4128    for this database.
4129    
4130    =back
4131    
4132    =cut
4133    
4134    sub Loader {
4135        # Get the parameters.
4136        my ($self, $groupName, $options) = @_;
4137        # Compute the loader name.
4138        my $loaderClass = "${groupName}SproutLoader";
4139        # Pull in its definition.
4140        require "$loaderClass.pm";
4141        # Create an object for it.
4142        my $retVal = eval("$loaderClass->new(\$self, \$options)");
4143        # Insure it worked.
4144        Confess("Could not create $loaderClass object: $@") if $@;
4145        # Return it to the caller.
4146        return $retVal;
4147    }
4148    
4149    
4150    =head3 LoadGroupList
4151    
4152        my @groups = $erdb->LoadGroupList();
4153    
4154    Returns a list of the names for this database's load groups. This method is used
4155    by [[ERDBGeneratorPl]] when the user wishes to load all table groups. The default
4156    is a single group called 'All' that loads everything.
4157    
4158    =cut
4159    
4160    sub LoadGroupList {
4161        # Return the list.
4162        return qw(Genome Subsystem Annotation Property Source Reaction Synonym Feature Drug);
4163    }
4164    
4165    =head3 LoadDirectory
4166    
4167        my $dirName = $erdb->LoadDirectory();
4168    
4169    Return the name of the directory in which load files are kept. The default is
4170    the FIG temporary directory, which is a really bad choice, but it's always there.
4171    
4172    =cut
4173    
4174    sub LoadDirectory {
4175        # Get the parameters.
4176        my ($self) = @_;
4177        # Return the directory name.
4178        return $self->{dataDir};
4179    }
4180    
4181  =head2 Internal Utility Methods  =head2 Internal Utility Methods
4182    
4183    =head3 GetStemmer
4184    
4185        my $stermmer = $sprout->GetStemmer();
4186    
4187    Return the stemmer object for this database.
4188    
4189    =cut
4190    
4191    sub GetStemmer {
4192        # Get the parameters.
4193        my ($self) = @_;
4194        # Declare the return variable.
4195        my $retVal = $self->{stemmer};
4196        if (! defined $retVal) {
4197            # We don't have one pre-built, so we build and save it now.
4198            $retVal = BioWords->new(exceptions => "$FIG_Config::sproutData/Exceptions.txt",
4199                                     stops => "$FIG_Config::sproutData/StopWords.txt",
4200                                     cache => 0);
4201            $self->{stemmer} = $retVal;
4202        }
4203        # Return the result.
4204        return $retVal;
4205    }
4206    
4207  =head3 ParseAssignment  =head3 ParseAssignment
4208    
4209  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 3997  Line 4290 
4290      # Get the parameters.      # Get the parameters.
4291      my ($self, $fid) = @_;      my ($self, $fid) = @_;
4292      # Insure we have a genome hash.      # Insure we have a genome hash.
4293      if (! defined $self->{genomeHash}) {      my $genomes = $self->_GenomeHash();
         my %genomeHash = map { $_ => 1 } $self->GetFlat(['Genome'], "", [], 'Genome(id)');  
         $self->{genomeHash} = \%genomeHash;  
     }  
4294      # Get the feature's genome ID.      # Get the feature's genome ID.
4295      my ($genomeID) = FIGRules::ParseFeatureID($fid);      my ($genomeID) = FIGRules::ParseFeatureID($fid);
4296      # 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 4034  Line 4324 
4324  }  }
4325    
4326    
4327    =head3 Hint
4328    
4329        my $htmlText = SearchHelper::Hint($wikiPage, $hintText);
4330    
4331    Return the HTML for a small question mark that displays the specified hint text when it is clicked.
4332    This HTML can be put in forms to provide a useful hinting mechanism.
4333    
4334    =over 4
4335    
4336    =item wikiPage
4337    
4338    Name of the wiki page to be popped up when the hint mark is clicked.
4339    
4340    =item hintText
4341    
4342    Text to display for the hint. It is raw html, but may not contain any double quotes.
4343    
4344    =item RETURN
4345    
4346    Returns the html for the hint facility. The resulting html shows a small button-like thing that
4347    uses the standard FIG popup technology.
4348    
4349    =back
4350    
4351    =cut
4352    
4353    sub Hint {
4354        # Get the parameters.
4355        my ($wikiPage, $hintText) = @_;
4356        # Escape the single quotes in the hint text.
4357        my $quotedText = $hintText;
4358        $quotedText =~ s/'/\\'/g;
4359        # Convert the wiki page name to a URL.
4360        my $wikiURL = join("", map { ucfirst $_ } split /\s+/, $wikiPage);
4361        $wikiURL = "$FIG_Config::cgi_url/wiki/view.cgi/FIG/$wikiURL";
4362        # Compute the mouseover script.
4363        my $mouseOver = "doTooltip(this, '$quotedText')";
4364        # Create the html.
4365        my $retVal = "&nbsp;<a href=\"$wikiURL\"><img src=\"$FIG_Config::cgi_url/Html/button-h.png\" class=\"helpicon\" onmouseover=\"$mouseOver\"/></a>";
4366        # Return it.
4367        return $retVal;
4368    }
4369    
4370    =head3 _GenomeHash
4371    
4372        my $gHash = $sprout->_GenomeHash();
4373    
4374    Return a hash mapping all NMPDR genome IDs to [[ERDBObjectPm]] genome objects.
4375    
4376    =cut
4377    
4378    sub _GenomeHash {
4379        # Get the parameters.
4380        my ($self) = @_;
4381        # Do we already have a filled hash?
4382        if (! $self->{genomeHashFilled}) {
4383            # No, create it.
4384            my %gHash = map { $_->PrimaryValue('id') => $_ } $self->GetList("Genome", "", []);
4385            $self->{genomeHash} = \%gHash;
4386            # Denote we have it.
4387            $self->{genomeHashFilled} = 1;
4388        }
4389        # Return the hash.
4390        return $self->{genomeHash};
4391    }
4392    
4393    =head3 _GenomeData
4394    
4395        my $genomeData = $sprout->_GenomeData($genomeID);
4396    
4397    Return an [[ERDBObjectPm]] object for the specified genome, or an undefined
4398    value if the genome does not exist.
4399    
4400    =over 4
4401    
4402    =item genomeID
4403    
4404    ID of the desired genome.
4405    
4406    =item RETURN
4407    
4408    Returns either an [[ERDBObjectPm]] containing the genome, or an undefined value.
4409    If the genome exists, it will have been read into the genome cache.
4410    
4411    =back
4412    
4413    =cut
4414    
4415    sub _GenomeData {
4416        # Get the parameters.
4417        my ($self, $genomeID) = @_;
4418        # Are we in the genome hash?
4419        if (! exists $self->{genomeHash}->{$genomeID} && ! $self->{genomeHashFilled}) {
4420            # The genome isn't in the hash, and the hash is not complete, so we try to
4421            # read it.
4422            $self->{genomeHash}->{$genomeID} = $self->GetEntity(Genome => $genomeID);
4423        }
4424        # Return the result.
4425        return $self->{genomeHash}->{$genomeID};
4426    }
4427    
4428    =head3 _CacheGenome
4429    
4430        $sprout->_CacheGenome($genomeID, $genomeData);
4431    
4432    Store the specified genome object in the genome cache if it is already there.
4433    
4434    =over 4
4435    
4436    =item genomeID
4437    
4438    ID of the genome to store in the cache.
4439    
4440    =item genomeData
4441    
4442    An [[ERDBObjectPm]] containing at least the data for the specified genome.
4443    Note that the Genome may not be the primary object in it, so a fully-qualified
4444    field name has to be used to retrieve data from it.
4445    
4446    =back
4447    
4448    =cut
4449    
4450    sub _CacheGenome {
4451        # Get the parameters.
4452        my ($self, $genomeID, $genomeData) = @_;
4453        # Only proceed if we don't already have the genome.
4454        if (! exists $self->{genomeHash}->{$genomeID}) {
4455            $self->{genomeHash}->{$genomeID} = $genomeData;
4456        }
4457    }
4458    
4459  1;  1;

Legend:
Removed from v.1.108  
changed lines
  Added in v.1.124

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3