[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.118, Sat Sep 20 14:32:34 2008 UTC revision 1.127, Mon May 4 18:49:49 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;      use CGI qw(-nosticky);
18      use WikiTools;      use WikiTools;
19      use BioWords;      use BioWords;
20      use base qw(ERDB);      use base qw(ERDB);
# Line 41  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
47    
48      my $sprout = Sprout->new($dbName, \%options);      my $sprout = Sprout->new(%parms)
49    
50  This is the constructor for a sprout object. It connects to the database and loads the  This is the constructor for a sprout object. It connects to the database and loads the
51  database definition into memory. The positional first parameter specifies the name of the  database definition into memory. The incoming parameter hash has the following permissible
52  database.  members (others will be ignored without error.
53    
54  =over 4  =over 4
55    
56    =item DBD
57    
58    Name of the XML file containing the database definition (default C<SproutDBD.xml> in
59    the DBD directory).
60    
61  =item dbName  =item dbName
62    
63  Name of the database.  Name of the database. If omitted, the default Sprout database name is used.
64    
65  =item options  =item options
66    
67  Table of options.  Sub-hash of special options.
68    
69  * B<dbType> type of database (currently C<mysql> for MySQL and C<pg> for PostgreSQL) (default C<mysql>)  * B<dbType> type of database (currently C<mysql> for MySQL and C<pg> for PostgreSQL) (default C<mysql>)
70    
71  * B<dataDir> directory containing the database definition file and the flat files used to load the data (default C<Data>)  * B<dataDir> directory containing the database definition file and the flat files used to load the data (default C<Data>)
72    
 * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)  
   
73  * B<userData> user name and password, delimited by a slash (default same as SEED)  * B<userData> user name and password, delimited by a slash (default same as SEED)
74    
75  * B<port> connection port (default C<0>)  * B<port> connection port (default C<0>)
# Line 89  Line 90 
90  I<fig> with a password of I<admin>. The database load files are in the directory  I<fig> with a password of I<admin>. The database load files are in the directory
91  F</usr/fig/SproutData>.  F</usr/fig/SproutData>.
92    
93      my $sprout = Sprout->new('Sprout', { userData => 'fig/admin', dataDir => '/usr/fig/SproutData' });      my $sprout = Sprout->new(dbName => 'Sprout', options => { userData => 'fig/admin', dataDir => '/usr/fig/SproutData' });
94    
95    The odd constructor signature is a result of Sprout's status as the first ERDB database,
96    and the need to make it compatible with the needs of its younger siblings.
97    
98  =cut  =cut
99    
100  sub new {  sub new {
101      # Get the parameters.      # Get the parameters.
102      my ($class, $dbName, $options) = @_;      my ($class, %parms) = @_;
103        # Look for an options hash.
104        my $options = $parms{options} || {};
105        # Plug in the DBD and name parameters.
106        if ($parms{DBD}) {
107            $options->{xmlFileName} = $parms{DBD};
108        }
109        my $dbName = $parms{dbName} || $FIG_Config::sproutDB;
110      # Compute the DBD directory.      # Compute the DBD directory.
111      my $dbd_dir = (defined($FIG_Config::dbd_dir) ? $FIG_Config::dbd_dir :      my $dbd_dir = (defined($FIG_Config::dbd_dir) ? $FIG_Config::dbd_dir :
112                                                    $FIG_Config::fig );                                                    $FIG_Config::fig );
# Line 117  Line 128 
128                         maxSegmentLength => 4500,        # maximum feature segment length                         maxSegmentLength => 4500,        # maximum feature segment length
129                         maxSequenceLength => 8000,       # maximum contig sequence length                         maxSequenceLength => 8000,       # maximum contig sequence length
130                         noDBOpen     => 0,               # 1 to suppress the database open                         noDBOpen     => 0,               # 1 to suppress the database open
131                           demandDriven => 0,               # 1 for forward-only queries
132                        }, $options);                        }, $options);
133      # Get the data directory.      # Get the data directory.
134      my $dataDir = $optionTable->{dataDir};      my $dataDir = $optionTable->{dataDir};
# Line 132  Line 144 
144      }      }
145      # Create the ERDB object.      # Create the ERDB object.
146      my $xmlFileName = "$optionTable->{xmlFileName}";      my $xmlFileName = "$optionTable->{xmlFileName}";
147      my $retVal = ERDB::new($class, $dbh, $xmlFileName);      my $retVal = ERDB::new($class, $dbh, $xmlFileName, %$optionTable);
148      # Add the option table and XML file name.      # Add the option table and XML file name.
149      $retVal->{_options} = $optionTable;      $retVal->{_options} = $optionTable;
150      $retVal->{_xmlName} = $xmlFileName;      $retVal->{_xmlName} = $xmlFileName;
151      # Set up space for the group file data.      # Set up space for the group file data.
152      $retVal->{groupHash} = undef;      $retVal->{groupHash} = undef;
153      # 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
154      $retVal->{genomeHash} = undef;      # and remember genome data.
155      # Connect to the attributes.      $retVal->{genomeHash} = {};
156        $retVal->{genomeHashFilled} = 0;
157        # Remember the data directory name.
158        $retVal->{dataDir} = $dataDir;
159        # Return it.
160        return $retVal;
161    }
162    
163    =head3 ca
164    
165        my $ca = $sprout->ca():;
166    
167    Return the [[CustomAttributesPm]] object for retrieving object
168    properties.
169    
170    =cut
171    
172    sub ca {
173        # Get the parameters.
174        my ($self) = @_;
175        # Do we already have an attribute object?
176        my $retVal = $self->{_ca};
177        if (! defined $retVal) {
178            # No, create one. How we do it depends on the configuration.
179      if ($FIG_Config::attrURL) {      if ($FIG_Config::attrURL) {
180          Trace("Remote attribute server $FIG_Config::attrURL chosen.") if T(3);          Trace("Remote attribute server $FIG_Config::attrURL chosen.") if T(3);
181          $retVal->{_ca} = RemoteCustomAttributes->new($FIG_Config::attrURL);              $retVal = RemoteCustomAttributes->new($FIG_Config::attrURL);
182      } elsif ($FIG_Config::attrDbName) {      } elsif ($FIG_Config::attrDbName) {
183          Trace("Local attribute database $FIG_Config::attrDbName chosen.") if T(3);          Trace("Local attribute database $FIG_Config::attrDbName chosen.") if T(3);
184          my $user = ($FIG_Config::arch eq 'win' ? 'self' : scalar(getpwent()));          my $user = ($FIG_Config::arch eq 'win' ? 'self' : scalar(getpwent()));
185          $retVal->{_ca} = CustomAttributes->new(user => $user);              $retVal = CustomAttributes->new(user => $user);
186      }      }
187      # Return it.          # Save it for next time.
188            $self->{_ca} = $retVal;
189        }
190        # Return the result.
191      return $retVal;      return $retVal;
192  }  }
193    
# Line 256  Line 294 
294                  $retVal = $superGroup;                  $retVal = $superGroup;
295              }              }
296          }          }
         # Make sure we found something.  
         if (! $retVal) {  
             Confess("No super-group found for \"$groupName\".");  
         }  
297      }      }
298      # Return the result.      # Return the result.
299      return $retVal;      return $retVal;
# Line 311  Line 345 
345  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
346  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
347  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
348  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.
349    
350  =over 4  =over 4
351    
# Line 500  Line 534 
534  =item filter  =item filter
535    
536  If specified, a filter for the list of genomes to display. The filter should be in the form of a  If specified, a filter for the list of genomes to display. The filter should be in the form of a
537  list reference. The first element of the list should be the filter string, and the remaining elements  list reference, a string, or a hash reference. If it is a list reference, the first element
538  the filter parameters.  of the list should be the filter string, and the remaining elements the filter parameters. If it is a
539    string, it will be split into a list at each included tab. If it is a hash reference, it should be
540    a hash that maps genomes which should be included to a TRUE value.
541    
542  =item multiSelect  =item multiSelect
543    
# Line 540  Line 576 
576      my $divID = "${menuID}_status";      my $divID = "${menuID}_status";
577      my $urlID = "${menuID}_url";      my $urlID = "${menuID}_url";
578      # Compute the code to show selected genomes in the status area.      # Compute the code to show selected genomes in the status area.
579      my $showSelect = "showSelected('$menuID', '$divID', '$urlID', 1000)";      my $showSelect = "showSelected('$menuID', '$divID', '$urlID', $FIG_Config::genome_control_cap)";
580      # Check for single-select or multi-select.      # Check for single-select or multi-select.
581      my $multiSelect = $options{multiSelect} || 0;      my $multiSelect = $options{multiSelect} || 0;
582      # Get the style data.      # Get the style data.
# Line 552  Line 588 
588      }      }
589      my %selected = map { $_ => 1 } @{$selections};      my %selected = map { $_ => 1 } @{$selections};
590      # Extract the filter information. The default is no filtering. It can be passed as a tab-delimited      # Extract the filter information. The default is no filtering. It can be passed as a tab-delimited
591      # string or a list reference.      # string, a hash reference, or a list reference.
592        my ($filterHash, $filterString);
593      my $filterParms = $options{filter} || "";      my $filterParms = $options{filter} || "";
594        if (ref $filterParms eq 'HASH') {
595            $filterHash = $filterParms;
596            $filterParms = [];
597            $filterString = "";
598        } else {
599      if (! ref $filterParms) {      if (! ref $filterParms) {
600          $filterParms = [split /\t|\\t/, $filterParms];          $filterParms = [split /\t|\\t/, $filterParms];
601      }      }
602      my $filterString = shift @{$filterParms};          $filterString = shift @{$filterParms};
603        }
604        # Check for possible subsystem filtering. If there is one, we will tack the
605        # relationship onto the object name list.
606        my @objectNames = qw(Genome);
607        if ($filterString =~ /ParticipatesIn\(/) {
608            push @objectNames, 'ParticipatesIn';
609        }
610      # Get a list of all the genomes in group order. In fact, we only need them ordered      # Get a list of all the genomes in group order. In fact, we only need them ordered
611      # by name (genus,species,strain), but putting primary-group in front enables us to      # by name (genus,species,strain), but putting primary-group in front enables us to
612      # take advantage of an existing index.      # take advantage of an existing index.
613      my @genomeList = $self->GetAll(['Genome'], "$filterString ORDER BY Genome(primary-group), Genome(genus), Genome(species), Genome(unique-characterization)",      my @genomeList = $self->GetAll(\@objectNames, "$filterString ORDER BY Genome(primary-group), Genome(genus), Genome(species), Genome(unique-characterization)",
614                                     $filterParms,                                     $filterParms,
615                                     [qw(Genome(primary-group) Genome(id) Genome(genus) Genome(species) Genome(unique-characterization) Genome(taxonomy) Genome(contigs))]);                                     [qw(Genome(primary-group) Genome(id) Genome(genus) Genome(species) Genome(unique-characterization) Genome(taxonomy) Genome(contigs))]);
616        # Apply the hash filter (if any).
617        if (defined $filterHash) {
618            @genomeList = grep { $filterHash->{$_->[1]} } @genomeList;
619        }
620      # Create a hash to organize the genomes by group. Each group will contain a list of      # Create a hash to organize the genomes by group. Each group will contain a list of
621      # 2-tuples, the first element being the genome ID and the second being the genome      # 2-tuples, the first element being the genome ID and the second being the genome
622      # name.      # name.
# Line 593  Line 646 
646      my @groups = map { $sortGroups{$_} } sort keys %sortGroups;      my @groups = map { $sortGroups{$_} } sort keys %sortGroups;
647      # Remember the number of NMPDR groups.      # Remember the number of NMPDR groups.
648      my $nmpdrGroupCount = scalar @groups;      my $nmpdrGroupCount = scalar @groups;
649        # Are there any supporting genomes?
650        if (exists $gHash{$FIG_Config::otherGroup}) {
651      # Loop through the supporting genomes, classifying them by domain. We'll also keep a list      # Loop through the supporting genomes, classifying them by domain. We'll also keep a list
652      # of the domains found.      # of the domains found.
653      my @otherGenomes = @{$gHash{$FIG_Config::otherGroup}};      my @otherGenomes = @{$gHash{$FIG_Config::otherGroup}};
# Line 611  Line 666 
666      push @groups, sort @domains;      push @groups, sort @domains;
667      # Delete the supporting group.      # Delete the supporting group.
668      delete $gHash{$FIG_Config::otherGroup};      delete $gHash{$FIG_Config::otherGroup};
669        }
670      # Now it gets complicated. We need a way to mark all the NMPDR genomes. We take advantage      # Now it gets complicated. We need a way to mark all the NMPDR genomes. We take advantage
671      # of the fact they come first in the list. We'll accumulate a count of the NMPDR genomes      # of the fact they come first in the list. We'll accumulate a count of the NMPDR genomes
672      # and use that to make the selections.      # and use that to make the selections.
# Line 624  Line 680 
680      # Set up the multiple-select flag.      # Set up the multiple-select flag.
681      my $multipleTag = ($multiSelect ? " multiple" : "" );      my $multipleTag = ($multiSelect ? " multiple" : "" );
682      # Set up the style class.      # Set up the style class.
683      my $classTag = ($class ? " class=\"$class\"" : "" );      my $classTag = ($class ? " $class" : "" );
684      # Create the SELECT tag and stuff it into the output array.      # Create the SELECT tag and stuff it into the output array.
685      my @lines = ("<SELECT name=\"$menuName\" id=\"$menuID\" $onChangeTag$multipleTag$classTag size=\"$rows\">");      my @lines = qq(<SELECT name="$menuName" id="$menuID" class="genomeSelect $class" $onChangeTag$multipleTag$classTag size="$rows">);
686      # Loop through the groups.      # Loop through the groups.
687      for my $group (@groups) {      for my $group (@groups) {
688          # Get the genomes in the group.          # Get the genomes in the group.
# Line 659  Line 715 
715          # displayed. For multiple-select mode, we include a button that selects the displayed          # displayed. For multiple-select mode, we include a button that selects the displayed
716          # genes. For single-select mode, we use a plain label instead.          # genes. For single-select mode, we use a plain label instead.
717          my $searchThingName = "${menuID}_SearchThing";          my $searchThingName = "${menuID}_SearchThing";
718          my $searchThingLabel = ($multiSelect ? "<INPUT type=\"button\" name=\"MacroSearch\" class=\"button\" value=\"Select genomes containing\" onClick=\"selectShowing('$menuID', '$searchThingName'); $showSelect;\" />"          my $searchThingLabel = "Type to narrow selection";
719                                               : "Show genomes containing");          my $searchThingButton = "";
720            if ($multiSelect) {
721                $searchThingButton = qq(<INPUT type="button" name="MacroSearch" class="button" value="Go" onClick="selectShowing('$menuID', '$searchThingName'); $showSelect;" />);
722            }
723          push @lines, "<br />$searchThingLabel&nbsp;" .          push @lines, "<br />$searchThingLabel&nbsp;" .
724                       "<INPUT type=\"text\" id=\"$searchThingName\" name=\"$searchThingName\" size=\"30\" onKeyup=\"showTyped('$menuID', '$searchThingName');\" />" .                       qq(<INPUT type="text" id="$searchThingName" name="$searchThingName" class="genomeSearchThing" onKeyup="showTyped('$menuID', '$searchThingName');" />) .
725                       Hint("GenomeControl", "Type here to filter the genomes displayed.") . "<br />";                       $searchThingButton .
726                         Hint("GenomeControl", 28) . "<br />";
727          # For multi-select mode, we also have buttons to set and clear selections.          # For multi-select mode, we also have buttons to set and clear selections.
728          if ($multiSelect) {          if ($multiSelect) {
729              push @lines, "<INPUT type=\"button\" name=\"ClearAll\" class=\"bigButton\"  value=\"Clear All\" onClick=\"clearAll('$menuID'); $showSelect\" />";              push @lines, qq(<INPUT type="button" name="ClearAll" class="bigButton genomeButton" value="Clear All" onClick="clearAll(getElementById('$menuID')); $showSelect" />);
730              push @lines, "<INPUT type=\"button\" name=\"SelectAll\" class=\"bigButton\" value=\"Select All\" onClick=\"selectAll('$menuID'); $showSelect\" />";              push @lines, qq(<INPUT type="button" name="SelectAll" class="bigButton genomeButton" value="Select All" onClick="selectAll(getElementById('$menuID')); $showSelect" />);
731              push @lines, "<INPUT type=\"button\" name=\"NMPDROnly\" class=\"bigButton\"  value=\"Select NMPDR\" onClick=\"selectSome('$menuID', $nmpdrCount, true); $showSelect;\" />";              push @lines, qq(<INPUT type="button" name="NMPDROnly" class="bigButton genomeButton" value="Select NMPDR" onClick="selectSome(getElementById('$menuID'), $nmpdrCount, true); $showSelect;" />);
732          }          }
733          # Add a hidden field we can use to generate organism page hyperlinks.          # Add a hidden field we can use to generate organism page hyperlinks.
734          push @lines, "<INPUT type=\"hidden\" id=\"$urlID\" value=\"$FIG_Config::cgi_url/wiki/rest.cgi/NmpdrPlugin/SeedViewer?page=Organism;organism=\" />";          push @lines, qq(<INPUT type="hidden" id="$urlID" value="$FIG_Config::cgi_url/wiki/rest.cgi/NmpdrPlugin/SeedViewer?page=Organism;organism=" />);
735          # Add the status display. This tells the user what's selected no matter where the list is scrolled.          # Add the status display. This tells the user what's selected no matter where the list is scrolled.
736          push @lines, "<DIV id=\"$divID\" class=\"Panel\"></DIV>";          push @lines, qq(<DIV id="$divID" class="Panel"></DIV>);
737      }      }
738      # Assemble all the lines into a string.      # Assemble all the lines into a string.
739      my $retVal = join("\n", @lines, "");      my $retVal = join("\n", @lines, "");
# Line 681  Line 741 
741      return $retVal;      return $retVal;
742  }  }
743    
744    =head3 Cleanup
745    
746        $sprout->Cleanup();
747    
748    Release the internal cache structures to free up memory.
749    
750    =cut
751    
752    sub Cleanup {
753        # Get the parameters.
754        my ($self) = @_;
755        # Delete the stemmer.
756        delete $self->{stemmer};
757        # Delete the attribute database.
758        delete $self->{_ca};
759        # Delete the group hash.
760        delete $self->{groupHash};
761        # Is there a FIG object?
762        if (defined $self->{fig}) {
763            # Yes, clear its subsystem cache.
764            $self->{fig}->clear_subsystem_cache();
765        }
766    }
767    
768    
769  =head3 Stem  =head3 Stem
770    
# Line 714  Line 798 
798          # We don't have one pre-built, so we build and save it now.          # We don't have one pre-built, so we build and save it now.
799          $stemmer = BioWords->new(exceptions => "$FIG_Config::sproutData/Exceptions.txt",          $stemmer = BioWords->new(exceptions => "$FIG_Config::sproutData/Exceptions.txt",
800                                   stops => "$FIG_Config::sproutData/StopWords.txt",                                   stops => "$FIG_Config::sproutData/StopWords.txt",
801                                   cache => 1);                                   cache => 0);
802          $self->{stemmer} = $stemmer;          $self->{stemmer} = $stemmer;
803      }      }
804      # Try to stem the word.      # Try to stem the word.
# Line 782  Line 866 
866  sub GenusSpecies {  sub GenusSpecies {
867      # Get the parameters.      # Get the parameters.
868      my ($self, $genomeID) = @_;      my ($self, $genomeID) = @_;
869      # Get the data for the specified genome.      # Declare the return value.
870      my @values = $self->GetEntityValues('Genome', $genomeID, ['Genome(genus)', 'Genome(species)',      my $retVal;
871                                                                'Genome(unique-characterization)']);      # Get the genome data.
872      # Format the result and return it.      my $genomeData = $self->_GenomeData($genomeID);
873      my $retVal = join(' ', @values);      # Only proceed if we found the genome.
874        if (defined $genomeData) {
875            $retVal = $genomeData->PrimaryValue('Genome(scientific-name)');
876        }
877        # Return it.
878      return $retVal;      return $retVal;
879  }  }
880    
# Line 931  Line 1019 
1019  }  }
1020    
1021    
   
1022  =head3 PointLocation  =head3 PointLocation
1023    
1024      my $found = Sprout::PointLocation($location, $point);      my $found = Sprout::PointLocation($location, $point);
# Line 1124  Line 1211 
1211      my ($self, $genomeID) = @_;      my ($self, $genomeID) = @_;
1212      # Declare the return variable.      # Declare the return variable.
1213      my $retVal = 0;      my $retVal = 0;
1214      # Get the genome's contig sequence lengths.      # Get the genome data.
1215      my @lens = $self->GetFlat(['HasContig', 'IsMadeUpOf'], 'HasContig(from-link) = ?',      my $genomeData = $self->_GenomeData($genomeID);
1216                         [$genomeID], 'IsMadeUpOf(len)');      # Only proceed if it exists.
1217      # Sum the lengths.      if (defined $genomeData) {
1218      map { $retVal += $_ } @lens;          $retVal = $genomeData->PrimaryValue('Genome(dna-size)');
1219        }
1220      # Return the result.      # Return the result.
1221      return $retVal;      return $retVal;
1222  }  }
# Line 1795  Line 1883 
1883      # Loop through the incoming features.      # Loop through the incoming features.
1884      for my $featureID (@{$featureList}) {      for my $featureID (@{$featureList}) {
1885          # Ask the server for the feature's best hit.          # Ask the server for the feature's best hit.
1886          my @bbhData = FIGRules::BBHData($featureID);          my $bbhData = FIGRules::BBHData($featureID);
1887          # Peel off the BBHs found.          # Peel off the BBHs found.
1888          my @found = ();          my @found = ();
1889          for my $bbh (@bbhData) {          for my $bbh (@$bbhData) {
1890              my $fid = $bbh->[0];              my $fid = $bbh->[0];
1891              my $bbGenome = $self->GenomeOf($fid);              my $bbGenome = $self->GenomeOf($fid);
1892              if ($bbGenome eq $genomeID) {              if ($bbGenome eq $genomeID) {
# Line 1837  Line 1925 
1925      # Get the parameters.      # Get the parameters.
1926      my ($self, $featureID, $count) = @_;      my ($self, $featureID, $count) = @_;
1927      # Ask for the best hits.      # Ask for the best hits.
1928      my @lists = FIGRules::BBHData($featureID);      my $lists = FIGRules::BBHData($featureID);
1929      # Create the return value.      # Create the return value.
1930      my %retVal = ();      my %retVal = ();
1931      for my $tuple (@lists) {      for my $tuple (@$lists) {
1932          $retVal{$tuple->[0]} = $tuple->[1];          $retVal{$tuple->[0]} = $tuple->[1];
1933      }      }
1934      # Return the result.      # Return the result.
# Line 1874  Line 1962 
1962      # Declare the return variable.      # Declare the return variable.
1963      my $retVal;      my $retVal;
1964      # Get the genome's data.      # Get the genome's data.
1965      my $genomeData = $self->GetEntity('Genome', $genomeID);      my $genomeData = $self->_GenomeData($genomeID);
1966      if ($genomeData) {      # Only proceed if it exists.
1967        if (defined $genomeData) {
1968          # The genome exists, so get the completeness flag.          # The genome exists, so get the completeness flag.
1969          $retVal = $genomeData->PrimaryValue('Genome(complete)');          $retVal = $genomeData->PrimaryValue('Genome(complete)');
1970      }      }
# Line 1991  Line 2080 
2080              $retVal{$featureID2} = $score;              $retVal{$featureID2} = $score;
2081          }          }
2082      }      }
     # 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;  
     }  
2083      # Return the hash.      # Return the hash.
2084      return %retVal;      return %retVal;
2085  }  }
# Line 2352  Line 2436 
2436  sub GetGenomeNameData {  sub GetGenomeNameData {
2437      # Get the parameters.      # Get the parameters.
2438      my ($self, $genomeID) = @_;      my ($self, $genomeID) = @_;
2439        # Declare the return variables.
2440        my ($genus, $species, $strain);
2441        # Get the genome's data.
2442        my $genomeData = $self->_GenomeData($genomeID);
2443        # Only proceed if the genome exists.
2444        if (defined $genomeData) {
2445      # Get the desired values.      # Get the desired values.
2446      my ($genus, $species, $strain) = $self->GetEntityValues('Genome', $genomeID =>          ($genus, $species, $strain) = $genomeData->Values(['Genome(genus)',
2447                                                              [qw(Genome(genus) Genome(species) Genome(unique-characterization))]);                                                             'Genome(species)',
2448      # Throw an error if they were not found.                                                             'Genome(unique-characterization)']);
2449      if (! defined $genus) {      } else {
2450            # Throw an error because they were not found.
2451          Confess("Genome $genomeID not found in database.");          Confess("Genome $genomeID not found in database.");
2452      }      }
2453      # Return the results.      # Return the results.
# Line 2657  Line 2748 
2748  sub Taxonomy {  sub Taxonomy {
2749      # Get the parameters.      # Get the parameters.
2750      my ($self, $genome) = @_;      my ($self, $genome) = @_;
     # Find the specified genome's taxonomy string.  
     my ($list) = $self->GetEntityValues('Genome', $genome, ['Genome(taxonomy)']);  
2751      # Declare the return variable.      # Declare the return variable.
2752      my @retVal = ();      my @retVal = ();
2753      # If we found the genome, return its taxonomy string.      # Get the genome data.
2754      if ($list) {      my $genomeData = $self->_GenomeData($genome);
2755          @retVal = split /\s*;\s*/, $list;      # Only proceed if it exists.
2756        if (defined $genomeData) {
2757            # Create the taxonomy from the taxonomy string.
2758            @retVal = split /\s*;\s*/, $genomeData->PrimaryValue('Genome(taxonomy)');
2759      } else {      } else {
2760            # Genome doesn't exist, so emit a warning.
2761          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);
2762      }      }
2763      # Return the value found.      # Return the value found.
# Line 2709  Line 2802 
2802      }      }
2803      my @taxA = $self->Taxonomy($genomeA);      my @taxA = $self->Taxonomy($genomeA);
2804      my @taxB = $self->Taxonomy($genomeB);      my @taxB = $self->Taxonomy($genomeB);
2805      # Initialize the distance to 1. We'll reduce it each time we find a match between the      # Compute the distance.
2806      # 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;  
     }  
2807      return $retVal;      return $retVal;
2808  }  }
2809    
# Line 2815  Line 2899 
2899      # Get the parameters.      # Get the parameters.
2900      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
2901      # Get the properties.      # Get the properties.
2902      my @attributes = $self->{_ca}->GetAttributes($featureID);      my @attributes = $self->ca->GetAttributes($featureID);
2903      # Strip the feature ID off each tuple.      # Strip the feature ID off each tuple.
2904      my @retVal = ();      my @retVal = ();
2905      for my $attributeRow (@attributes) {      for my $attributeRow (@attributes) {
# Line 3184  Line 3268 
3268      # Get the parameters.      # Get the parameters.
3269      my ($self, $featureID, $function, $userID) = @_;      my ($self, $featureID, $function, $userID) = @_;
3270      # 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.
3271      my @bbhFeatures = map { $_->[0] } FIGRules::BBHData($featureID);      my $bbhData = FIGRules::BBHData($featureID);
3272        my @bbhFeatures = map { $_->[0] } @$bbhData;
3273      # 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
3274      # functional assignment.      # functional assignment.
3275      my @retVal = ();      my @retVal = ();
# Line 3357  Line 3442 
3442    
3443  =head3 BBHMatrix  =head3 BBHMatrix
3444    
3445      my %bbhMap = $sprout->BBHMatrix($genomeID, $cutoff, @targets);      my $bbhMap = $sprout->BBHMatrix($genomeID, $cutoff, @targets);
3446    
3447  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
3448  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 3381  Line 3466 
3466    
3467  =item RETURN  =item RETURN
3468    
3469  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
3470  BBH pegs in the target genomes to their scores.  to a sub-hash mapping its BBH pegs in the target genomes to their scores.
3471    
3472  =back  =back
3473    
# Line 3395  Line 3480 
3480      my %retVal = ();      my %retVal = ();
3481      # Ask for the BBHs.      # Ask for the BBHs.
3482      my @bbhList = FIGRules::BatchBBHs("fig|$genomeID.%", $cutoff, @targets);      my @bbhList = FIGRules::BatchBBHs("fig|$genomeID.%", $cutoff, @targets);
3483        Trace("Retrieved " . scalar(@bbhList) . " BBH results.") if T(3);
3484      # 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.
3485      for my $bbhData (@bbhList) {      for my $bbhData (@bbhList) {
3486          my ($peg1, $peg2, $score) = @{$bbhData};          my ($peg1, $peg2, $score) = @{$bbhData};
# Line 3405  Line 3491 
3491          }          }
3492      }      }
3493      # Return the result.      # Return the result.
3494      return %retVal;      return \%retVal;
3495  }  }
3496    
3497    
# Line 3511  Line 3597 
3597      # Create the return hash.      # Create the return hash.
3598      my %retVal = ();      my %retVal = ();
3599      # Query for the desired BBHs.      # Query for the desired BBHs.
3600      my @bbhList = FIGRules::BBHData($featureID, $cutoff);      my $bbhList = FIGRules::BBHData($featureID, $cutoff);
3601      # Form the results into the return hash.      # Form the results into the return hash.
3602      for my $pair (@bbhList) {      for my $pair (@$bbhList) {
3603          my $fid = $pair->[0];          my $fid = $pair->[0];
3604          if ($self->Exists('Feature', $fid)) {          if ($self->Exists('Feature', $fid)) {
3605              $retVal{$fid} = $pair->[1];              $retVal{$fid} = $pair->[1];
# Line 3891  Line 3977 
3977      # Get the parameters.      # Get the parameters.
3978      my ($self, $featureID, $key, @values) = @_;      my ($self, $featureID, $key, @values) = @_;
3979      # Add the property using the attached attributes object.      # Add the property using the attached attributes object.
3980      $self->{_ca}->AddAttribute($featureID, $key, @values);      $self->ca->AddAttribute($featureID, $key, @values);
3981  }  }
3982    
3983  =head3 CheckGroupFile  =head3 CheckGroupFile
# Line 3911  Line 3997 
3997    
3998  =over 4  =over 4
3999    
4000  =item page  =item specials
4001    
4002  The super-group's web page in the NMPDR.  Reference to a hash whose keys are the names of special species.
4003    
4004  =item contents  =item contents
4005    
# Line 3936  Line 4022 
4022          my @groupLines = Tracer::GetFile("$FIG_Config::sproutData/groups.tbl");          my @groupLines = Tracer::GetFile("$FIG_Config::sproutData/groups.tbl");
4023          # Loop through the list of sort-of groups.          # Loop through the list of sort-of groups.
4024          for my $groupLine (@groupLines) {          for my $groupLine (@groupLines) {
4025              my ($name, $page, @contents) = split /\t/, $groupLine;              my ($name, $specials, @contents) = split /\t/, $groupLine;
4026              $groupHash{$name} = { page => $page,              $groupHash{$name} = { specials => { map { $_ => 1 } split /\s*,\s*/, $specials },
4027                                    contents => [ map { [ split /\s*,\s*/, $_ ] } @contents ]                                    contents => [ map { [ split /\s*,\s*/, $_ ] } @contents ]
4028                                  };                                  };
4029          }          }
# Line 3978  Line 4064 
4064  sub CleanKeywords {  sub CleanKeywords {
4065      # Get the parameters.      # Get the parameters.
4066      my ($self, $searchExpression) = @_;      my ($self, $searchExpression) = @_;
4067      # Perform the standard cleanup.      # Get the stemmer.
4068      my $words = $self->ERDB::CleanKeywords($searchExpression);      my $stemmer = $self->GetStemmer();
4069      # Fix the periods in EC and TC numbers.      # Convert the search expression using the stemmer.
4070      $words =~ s/(\d+|\-)\.(\d+|-)\.(\d+|-)\.(\d+|-)/$1_$2_$3_$4/g;      my $retVal = $stemmer->PrepareSearchExpression($searchExpression);
     # Fix non-trailing periods.  
     $words =~ s/\.(\w)/_$1/g;  
     # Fix non-leading minus signs.  
     $words =~ s/(\w)[\-]/$1_/g;  
     # Fix the vertical bars and colons  
     $words =~ s/(\w)[|:](\w)/$1'$2/g;  
     # Now split up the list so that each keyword is in its own string. We keep the delimiters  
     # because they may contain boolean expression data.  
     my @words = split /([^A-Za-z'0-9_]+)/, $words;  
     # We'll convert the stemmable words into stems and re-assemble the result.  
     my $retVal = "";  
     for my $word (@words) {  
         my $stem = $self->Stem($word);  
         if (defined $stem) {  
             $retVal .= $stem;  
         } else {  
             $retVal .= $word;  
         }  
     }  
4071      Trace("Cleaned keyword list for \"$searchExpression\" is \"$retVal\".") if T(3);      Trace("Cleaned keyword list for \"$searchExpression\" is \"$retVal\".") if T(3);
4072      # Return the result.      # Return the result.
4073      return $retVal;      return $retVal;
4074  }  }
4075    
4076    =head3 GetSourceObject
4077    
4078        my $source = $erdb->GetSourceObject();
4079    
4080    Return the object to be used in creating load files for this database.
4081    
4082    =cut
4083    
4084    sub GetSourceObject {
4085        # Get the parameters.
4086        my ($self) = @_;
4087        # Do we already have one?
4088        my $retVal = $self->{fig};
4089        if (! defined $retVal) {
4090            # Create the object.
4091            require FIG;
4092            $retVal = FIG->new();
4093            Trace("FIG source object created for process $$.") if T(ERDBLoadGroup => 3);
4094            # Set up retries to prevent the lost-connection error when harvesting
4095            # the feature data.
4096            my $dbh = $retVal->db_handle();
4097            $dbh->set_retries(5);
4098            # Save it for other times.
4099            $self->{fig} = $retVal;
4100        }
4101        # Return the object.
4102        return $retVal;
4103    }
4104    
4105    =head3 SectionList
4106    
4107        my @sections = $erdb->SectionList($fig);
4108    
4109    Return a list of the names for the different data sections used when loading this database.
4110    The default is a single string, in which case there is only one section representing the
4111    entire database.
4112    
4113    =cut
4114    
4115    sub SectionList {
4116        # Get the parameters.
4117        my ($self, $source) = @_;
4118        # Ask the BaseSproutLoader for a section list.
4119        require BaseSproutLoader;
4120        my @retVal = BaseSproutLoader::GetSectionList($self, $source);
4121        # Return the list.
4122        return @retVal;
4123    }
4124    
4125    =head3 Loader
4126    
4127        my $groupLoader = $erdb->Loader($groupName, $options);
4128    
4129    Return an [[ERDBLoadGroupPm]] object for the specified load group. This method is used
4130    by [[ERDBGeneratorPl]] to create the load group objects. If you are not using
4131    [[ERDBGeneratorPl]], you don't need to override this method.
4132    
4133    =over 4
4134    
4135    =item groupName
4136    
4137    Name of the load group whose object is to be returned. The group name is
4138    guaranteed to be a single word with only the first letter capitalized.
4139    
4140    =item options
4141    
4142    Reference to a hash of command-line options.
4143    
4144    =item RETURN
4145    
4146    Returns an [[ERDBLoadGroupPm]] object that can be used to process the specified load group
4147    for this database.
4148    
4149    =back
4150    
4151    =cut
4152    
4153    sub Loader {
4154        # Get the parameters.
4155        my ($self, $groupName, $options) = @_;
4156        # Compute the loader name.
4157        my $loaderClass = "${groupName}SproutLoader";
4158        # Pull in its definition.
4159        require "$loaderClass.pm";
4160        # Create an object for it.
4161        my $retVal = eval("$loaderClass->new(\$self, \$options)");
4162        # Insure it worked.
4163        Confess("Could not create $loaderClass object: $@") if $@;
4164        # Return it to the caller.
4165        return $retVal;
4166    }
4167    
4168    
4169    =head3 LoadGroupList
4170    
4171        my @groups = $erdb->LoadGroupList();
4172    
4173    Returns a list of the names for this database's load groups. This method is used
4174    by [[ERDBGeneratorPl]] when the user wishes to load all table groups. The default
4175    is a single group called 'All' that loads everything.
4176    
4177    =cut
4178    
4179    sub LoadGroupList {
4180        # Return the list.
4181        return qw(Feature Subsystem Genome Annotation Property Source Reaction Synonym Drug);
4182    }
4183    
4184    =head3 LoadDirectory
4185    
4186        my $dirName = $erdb->LoadDirectory();
4187    
4188    Return the name of the directory in which load files are kept. The default is
4189    the FIG temporary directory, which is a really bad choice, but it's always there.
4190    
4191    =cut
4192    
4193    sub LoadDirectory {
4194        # Get the parameters.
4195        my ($self) = @_;
4196        # Return the directory name.
4197        return $self->{dataDir};
4198    }
4199    
4200  =head2 Internal Utility Methods  =head2 Internal Utility Methods
4201    
4202    =head3 GetStemmer
4203    
4204        my $stermmer = $sprout->GetStemmer();
4205    
4206    Return the stemmer object for this database.
4207    
4208    =cut
4209    
4210    sub GetStemmer {
4211        # Get the parameters.
4212        my ($self) = @_;
4213        # Declare the return variable.
4214        my $retVal = $self->{stemmer};
4215        if (! defined $retVal) {
4216            # We don't have one pre-built, so we build and save it now.
4217            $retVal = BioWords->new(exceptions => "$FIG_Config::sproutData/Exceptions.txt",
4218                                     stops => "$FIG_Config::sproutData/StopWords.txt",
4219                                     cache => 0);
4220            $self->{stemmer} = $retVal;
4221        }
4222        # Return the result.
4223        return $retVal;
4224    }
4225    
4226  =head3 ParseAssignment  =head3 ParseAssignment
4227    
4228  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 4094  Line 4309 
4309      # Get the parameters.      # Get the parameters.
4310      my ($self, $fid) = @_;      my ($self, $fid) = @_;
4311      # Insure we have a genome hash.      # Insure we have a genome hash.
4312      if (! defined $self->{genomeHash}) {      my $genomes = $self->_GenomeHash();
         my %genomeHash = map { $_ => 1 } $self->GetFlat(['Genome'], "", [], 'Genome(id)');  
         $self->{genomeHash} = \%genomeHash;  
     }  
4313      # Get the feature's genome ID.      # Get the feature's genome ID.
4314      my ($genomeID) = FIGRules::ParseFeatureID($fid);      my ($genomeID) = FIGRules::ParseFeatureID($fid);
4315      # 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 4133  Line 4345 
4345    
4346  =head3 Hint  =head3 Hint
4347    
4348      my $htmlText = SearchHelper::Hint($wikiPage, $hintText);      my $htmlText = Sprout::Hint($wikiPage, $hintID);
4349    
4350  Return the HTML for a small question mark that displays the specified hint text when it is clicked.  Return the HTML for a help link that displays the specified hint text when it is clicked.
4351  This HTML can be put in forms to provide a useful hinting mechanism.  This HTML can be put in forms to provide a useful hinting mechanism.
4352    
4353  =over 4  =over 4
# Line 4144  Line 4356 
4356    
4357  Name of the wiki page to be popped up when the hint mark is clicked.  Name of the wiki page to be popped up when the hint mark is clicked.
4358    
4359  =item hintText  =item hintID
4360    
4361  Text to display for the hint. It is raw html, but may not contain any double quotes.  ID of the text to display for the hint. This should correspond to a tip number
4362    in the Wiki.
4363    
4364  =item RETURN  =item RETURN
4365    
4366  Returns the html for the hint facility. The resulting html shows a small button-like thing that  Returns the html for the hint facility. The resulting html shows the word "help" and
4367  uses the standard FIG popup technology.  uses the standard FIG popup technology.
4368    
4369  =back  =back
# Line 4159  Line 4372 
4372    
4373  sub Hint {  sub Hint {
4374      # Get the parameters.      # Get the parameters.
4375      my ($wikiPage, $hintText) = @_;      my ($wikiPage, $hintID) = @_;
4376      # Escape the single quotes in the hint text.      # Declare the return variable.
4377      my $quotedText = $hintText;      my $retVal;
     $quotedText =~ s/'/\\'/g;  
4378      # Convert the wiki page name to a URL.      # Convert the wiki page name to a URL.
4379      my $wikiURL = join("", map { ucfirst $_ } split /\s+/, $wikiPage);      my $wikiURL;
4380      $wikiURL = "$FIG_Config::cgi_url/wiki/view.cgi/FIG/$wikiURL";      if ($wikiPage =~ m#/#) {
4381      # Compute the mouseover script.          # Here it's a URL of some sort.
4382      my $mouseOver = "doTooltip(this, '$quotedText')";          $wikiURL = $wikiPage;
4383      # Create the html.      } else {
4384      my $retVal = "&nbsp;<a href=\"$wikiURL\"><img src=\"$FIG_Config::cgi_url/Html/button-h.png\" class=\"helpicon\" onmouseover=\"$mouseOver\"/></a>";          # Here it's a wiki page.
4385      # Return it.          my $page = join("", map { ucfirst $_ } split /\s+/, $wikiPage);
4386            if ($page =~ /^(.+?)\.(.+)$/) {
4387                $page = "$1/$2";
4388            } else {
4389                $page = "FIG/$page";
4390            }
4391            $wikiURL = "$FIG_Config::cgi_url/wiki/view.cgi/$page";
4392        }
4393        # Is there hint text?
4394        if (! $hintID) {
4395            # No. Create a new-page hint.
4396            $retVal = qq(&nbsp;<a class="hint" onclick="doPagePopup(this, '$wikiURL')">(help)</a>);
4397        } else {
4398            # With hint text, we create a popup window hint. We need to compute the hint URL.
4399            my $tipURL = "$FIG_Config::cgi_url/wiki/view.cgi/FIG/TWikiCustomTip" .
4400                Tracer::Pad($hintID, 3, 1, "0");
4401            # Create a hint pop-up link.
4402            $retVal = qq(&nbsp;<a class="hint" onclick="doHintPopup(this, '$wikiURL', '$tipURL')">(help)</a>);
4403        }
4404        # Return the HTML.
4405      return $retVal;      return $retVal;
4406  }  }
4407    
4408    =head3 _GenomeHash
4409    
4410        my $gHash = $sprout->_GenomeHash();
4411    
4412    Return a hash mapping all NMPDR genome IDs to [[ERDBObjectPm]] genome objects.
4413    
4414    =cut
4415    
4416    sub _GenomeHash {
4417        # Get the parameters.
4418        my ($self) = @_;
4419        # Do we already have a filled hash?
4420        if (! $self->{genomeHashFilled}) {
4421            # No, create it.
4422            my %gHash = map { $_->PrimaryValue('id') => $_ } $self->GetList("Genome", "", []);
4423            $self->{genomeHash} = \%gHash;
4424            # Denote we have it.
4425            $self->{genomeHashFilled} = 1;
4426        }
4427        # Return the hash.
4428        return $self->{genomeHash};
4429    }
4430    
4431    =head3 _GenomeData
4432    
4433        my $genomeData = $sprout->_GenomeData($genomeID);
4434    
4435    Return an [[ERDBObjectPm]] object for the specified genome, or an undefined
4436    value if the genome does not exist.
4437    
4438    =over 4
4439    
4440    =item genomeID
4441    
4442    ID of the desired genome.
4443    
4444    =item RETURN
4445    
4446    Returns either an [[ERDBObjectPm]] containing the genome, or an undefined value.
4447    If the genome exists, it will have been read into the genome cache.
4448    
4449    =back
4450    
4451    =cut
4452    
4453    sub _GenomeData {
4454        # Get the parameters.
4455        my ($self, $genomeID) = @_;
4456        # Are we in the genome hash?
4457        if (! exists $self->{genomeHash}->{$genomeID} && ! $self->{genomeHashFilled}) {
4458            # The genome isn't in the hash, and the hash is not complete, so we try to
4459            # read it.
4460            $self->{genomeHash}->{$genomeID} = $self->GetEntity(Genome => $genomeID);
4461        }
4462        # Return the result.
4463        return $self->{genomeHash}->{$genomeID};
4464    }
4465    
4466    =head3 _CacheGenome
4467    
4468        $sprout->_CacheGenome($genomeID, $genomeData);
4469    
4470    Store the specified genome object in the genome cache if it is already there.
4471    
4472    =over 4
4473    
4474    =item genomeID
4475    
4476    ID of the genome to store in the cache.
4477    
4478    =item genomeData
4479    
4480    An [[ERDBObjectPm]] containing at least the data for the specified genome.
4481    Note that the Genome may not be the primary object in it, so a fully-qualified
4482    field name has to be used to retrieve data from it.
4483    
4484    =back
4485    
4486    =cut
4487    
4488    sub _CacheGenome {
4489        # Get the parameters.
4490        my ($self, $genomeID, $genomeData) = @_;
4491        # Only proceed if we don't already have the genome.
4492        if (! exists $self->{genomeHash}->{$genomeID}) {
4493            $self->{genomeHash}->{$genomeID} = $genomeData;
4494        }
4495    }
4496    
4497  1;  1;

Legend:
Removed from v.1.118  
changed lines
  Added in v.1.127

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3