[Bio] / Sprout / SproutLoad.pm Repository:
ViewVC logotype

Diff of /Sprout/SproutLoad.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.75, Sun Oct 22 05:17:10 2006 UTC revision 1.84, Thu May 17 23:44:51 2007 UTC
# Line 80  Line 80 
80  Either the name of the file containing the list of trusted subsystems or a reference  Either the name of the file containing the list of trusted subsystems or a reference
81  to a list of subsystem names. If nothing is specified, all NMPDR subsystems will be  to a list of subsystem names. If nothing is specified, all NMPDR subsystems will be
82  considered trusted. (A subsystem is considered NMPDR if it has a file named C<NMPDR>  considered trusted. (A subsystem is considered NMPDR if it has a file named C<NMPDR>
83  in its data directory.) Only subsystem data related to the trusted subsystems is loaded.  in its data directory.) Only subsystem data related to the NMPDR subsystems is loaded.
84    
85  =item options  =item options
86    
# Line 138  Line 138 
138          if (! defined $subsysFile || $subsysFile eq '') {          if (! defined $subsysFile || $subsysFile eq '') {
139              # Here we want all the usable subsystems. First we get the whole list.              # Here we want all the usable subsystems. First we get the whole list.
140              my @subs = $fig->all_subsystems();              my @subs = $fig->all_subsystems();
141              # Loop through, checking for usability.              # Loop through, checking for the NMPDR file.
142              for my $sub (@subs) {              for my $sub (@subs) {
143                  if ($fig->usable_subsystem($sub)) {                  if ($fig->nmpdr_subsystem($sub)) {
144                      $subsystems{$sub} = 1;                      $subsystems{$sub} = 1;
145                  }                  }
146              }              }
# Line 168  Line 168 
168              my $name = $subsystem;              my $name = $subsystem;
169              $name =~ s/_/ /g;              $name =~ s/_/ /g;
170              my $classes = $fig->subsystem_classification($subsystem);              my $classes = $fig->subsystem_classification($subsystem);
171              my @classList = map { " $_" } @{$classes};              $name .= " " . join(" ", @{$classes});
             $name .= join("", @classList);  
172              $subsystems{$subsystem} = $name;              $subsystems{$subsystem} = $name;
173          }          }
174      }      }
# Line 275  Line 274 
274              my $extra = join " ", @extraData;              my $extra = join " ", @extraData;
275              # Get the full taxonomy.              # Get the full taxonomy.
276              my $taxonomy = $fig->taxonomy_of($genomeID);              my $taxonomy = $fig->taxonomy_of($genomeID);
277                # Get the version. If no version is specified, we default to the genome ID by itself.
278                my $version = $fig->genome_version($genomeID);
279                if (! defined($version)) {
280                    $version = $genomeID;
281                }
282                # Get the DNA size.
283                my $dnaSize = $fig->genome_szdna($genomeID);
284              # Open the NMPDR group file for this genome.              # Open the NMPDR group file for this genome.
285              my $group;              my $group;
286              if (open(TMP, "<$FIG_Config::organisms/$genomeID/NMPDR") &&              if (open(TMP, "<$FIG_Config::organisms/$genomeID/NMPDR") &&
# Line 287  Line 293 
293              }              }
294              close TMP;              close TMP;
295              # Output the genome record.              # Output the genome record.
296              $loadGenome->Put($genomeID, $accessCode, $fig->is_complete($genomeID), $genus,              $loadGenome->Put($genomeID, $accessCode, $fig->is_complete($genomeID),
297                               $group, $species, $extra, $taxonomy);                               $dnaSize, $genus, $group, $species, $extra, $version, $taxonomy);
298              # Now we loop through each of the genome's contigs.              # Now we loop through each of the genome's contigs.
299              my @contigs = $fig->all_contigs($genomeID);              my @contigs = $fig->all_contigs($genomeID);
300              for my $contigID (@contigs) {              for my $contigID (@contigs) {
# Line 470  Line 476 
476      IsLocatedIn      IsLocatedIn
477      HasFeature      HasFeature
478      HasRoleInSubsystem      HasRoleInSubsystem
479        FeatureEssential
480        FeatureVirulent
481        FeatureIEDB
482    
483  =over 4  =over 4
484    
# Line 498  Line 507 
507      my $loadFeatureUpstream = $self->_TableLoader('FeatureUpstream');      my $loadFeatureUpstream = $self->_TableLoader('FeatureUpstream');
508      my $loadHasFeature = $self->_TableLoader('HasFeature', $self->PrimaryOnly);      my $loadHasFeature = $self->_TableLoader('HasFeature', $self->PrimaryOnly);
509      my $loadHasRoleInSubsystem = $self->_TableLoader('HasRoleInSubsystem', $self->PrimaryOnly);      my $loadHasRoleInSubsystem = $self->_TableLoader('HasRoleInSubsystem', $self->PrimaryOnly);
510        my $loadFeatureEssential = $self->_TableLoader('FeatureEssential');
511        my $loadFeatureVirulent = $self->_TableLoader('FeatureVirulent');
512        my $loadFeatureIEDB = $self->_TableLoader('FeatureIEDB');
513      # Get the subsystem hash.      # Get the subsystem hash.
514      my $subHash = $self->{subsystems};      my $subHash = $self->{subsystems};
515      # Get the maximum sequence size. We need this later for splitting up the      # Get the maximum sequence size. We need this later for splitting up the
# Line 512  Line 524 
524              Trace("Loading features for genome $genomeID.") if T(3);              Trace("Loading features for genome $genomeID.") if T(3);
525              $loadFeature->Add("genomeIn");              $loadFeature->Add("genomeIn");
526              # Get the feature list for this genome.              # Get the feature list for this genome.
527              my $features = $fig->all_features_detailed($genomeID);              my $features = $fig->all_features_detailed_fast($genomeID);
528              # Sort and count the list.              # Sort and count the list.
529              my @featureTuples = sort { $a->[0] cmp $b->[0] } @{$features};              my @featureTuples = sort { $a->[0] cmp $b->[0] } @{$features};
530              my $count = scalar @featureTuples;              my $count = scalar @featureTuples;
531                my @fids = map { $_->[0] } @featureTuples;
532              Trace("$count features found for genome $genomeID.") if T(3);              Trace("$count features found for genome $genomeID.") if T(3);
533                # Get the attributes for this genome and put them in a hash by feature ID.
534                my $attributes = GetGenomeAttributes($fig, $genomeID, \@fids);
535              # Set up for our duplicate-feature check.              # Set up for our duplicate-feature check.
536              my $oldFeatureID = "";              my $oldFeatureID = "";
537              # Loop through the features.              # Loop through the features.
538              for my $featureTuple (@featureTuples) {              for my $featureTuple (@featureTuples) {
539                  # Split the tuple.                  # Split the tuple.
540                  my ($featureID, $locations, undef, $type) = @{$featureTuple};                  my ($featureID, $locations, undef, $type, $minloc, $maxloc, $assignment, $user, $quality) = @{$featureTuple};
541                  # Check for duplicates.                  # Check for duplicates.
542                  if ($featureID eq $oldFeatureID) {                  if ($featureID eq $oldFeatureID) {
543                      Trace("Duplicate feature $featureID found.") if T(1);                      Trace("Duplicate feature $featureID found.") if T(1);
# Line 530  Line 545 
545                      $oldFeatureID = $featureID;                      $oldFeatureID = $featureID;
546                      # Count this feature.                      # Count this feature.
547                      $loadFeature->Add("featureIn");                      $loadFeature->Add("featureIn");
548                      # Begin building the keywords.                      # Fix the quality. It is almost always a space, but some odd stuff might sneak through, and the
549                      my @keywords = ($genomeID);                      # Sprout database requires a single character.
550                      # Get the functional assignment and aliases. This                      if (! defined($quality) || $quality eq "") {
551                      # depends on the feature type.                          $quality = " ";
552                      my $assignment;                      }
553                      if ($type eq "peg") {                      # Begin building the keywords. We start with the genome ID, the
554                          $assignment = $fig->function_of($featureID);                      # feature ID, the taxonomy, and the organism name.
555                        my @keywords = ($genomeID, $featureID, $fig->genus_species($genomeID),
556                                        $fig->taxonomy_of($genomeID));
557                          # Create the aliases.                          # Create the aliases.
558                          for my $alias ($fig->feature_aliases($featureID)) {                          for my $alias ($fig->feature_aliases($featureID)) {
559                              $loadFeatureAlias->Put($featureID, $alias);                              $loadFeatureAlias->Put($featureID, $alias);
560                              push @keywords, $alias;                              push @keywords, $alias;
561                          }                          }
                     } else {  
                         # For other types, the assignment is the first (and ONLY) alias.  
                         ($assignment) = $fig->feature_aliases($featureID);  
                     }  
562                      Trace("Assignment for $featureID is: $assignment") if T(4);                      Trace("Assignment for $featureID is: $assignment") if T(4);
563                      # Break the assignment into words and shove it onto the                      # Break the assignment into words and shove it onto the
564                      # keyword list.                      # keyword list.
# Line 593  Line 606 
606                              }                              }
607                          }                          }
608                      }                      }
609                      # The final task is to add virulence and essentiality attributes.                      # There are three special attributes computed from property
610                      if ($fig->virulent($featureID)) {                      # data that we build next. If the special attribute is non-empty,
611                          push @keywords, "virulent";                      # its name will be added to the keyword list. First, we get all
612                      }                      # the attributes for this feature. They will come back as
613                      if ($fig->essential($featureID)) {                      # 4-tuples: [peg, name, value, URL]. We use a 3-tuple instead:
614                          push @keywords, "essential";                      # [name, value, value with URL]. (We don't need the PEG, since
615                        # we already know it.)
616                        my @attributes = map { [$_->[1], $_->[2], Tracer::CombineURL($_->[2], $_->[3])] }
617                                             @{$attributes->{$featureID}};
618                        # Now we process each of the special attributes.
619                        if (SpecialAttribute($featureID, \@attributes,
620                                             1, [0,2], '^(essential|potential_essential)$',
621                                             $loadFeatureEssential)) {
622                            push @keywords, 'essential';
623                            $loadFeature->Add('essential');
624                        }
625                        if (SpecialAttribute($featureID, \@attributes,
626                                             0, [2], '^virulen',
627                                             $loadFeatureVirulent)) {
628                            push @keywords, 'virulent';
629                            $loadFeature->Add('virulent');
630                        }
631                        if (SpecialAttribute($featureID, \@attributes,
632                                             0, [0,2], '^iedb_',
633                                             $loadFeatureIEDB)) {
634                            push @keywords, 'iedb';
635                            $loadFeature->Add('iedb');
636                      }                      }
637                      # Now we need to bust up hyphenated words in the keyword                      # Now we need to bust up hyphenated words in the keyword
638                      # list.                      # list. We keep them separate and put them at the end so
639                        # the original word order is available.
640                      my $keywordString = "";                      my $keywordString = "";
641                        my $bustedString = "";
642                      for my $keyword (@keywords) {                      for my $keyword (@keywords) {
643                          if (length $keyword >= 4) {                          if (length $keyword >= 3) {
644                              $keywordString .= " $keyword";                              $keywordString .= " $keyword";
645                              if ($keyword =~ /-/) {                              if ($keyword =~ /-/) {
646                                  my @words = grep { length($_) >= 4 } split /-/, $keyword;                                  my @words = split /-/, $keyword;
647                                  $keywordString .= join(" ", "", @words);                                  $bustedString .= join(" ", "", @words);
648                              }                              }
649                          }                          }
650                      }                      }
651                        $keywordString .= $bustedString;
652                        # Get rid of annoying punctuation.
653                        $keywordString =~ s/[();]//g;
654                      # Clean the keyword list.                      # Clean the keyword list.
655                      my $cleanWords = $sprout->CleanKeywords($keywordString);                      my $cleanWords = $sprout->CleanKeywords($keywordString);
656                      Trace("Keyword string for $featureID: $cleanWords") if T(4);                      Trace("Keyword string for $featureID: $cleanWords") if T(4);
657                      # Create the feature record.                      # Create the feature record.
658                      $loadFeature->Put($featureID, 1, $type, $assignment, $cleanWords);                      $loadFeature->Put($featureID, 1, $user, $quality, $type, $assignment, $cleanWords);
659                      # This part is the roughest. We need to relate the features to contig                      # This part is the roughest. We need to relate the features to contig
660                      # locations, and the locations must be split so that none of them exceed                      # locations, and the locations must be split so that none of them exceed
661                      # the maximum segment size. This simplifies the genes_in_region processing                      # the maximum segment size. This simplifies the genes_in_region processing
# Line 749  Line 788 
788              # Get the subsystem object.              # Get the subsystem object.
789              my $sub = $fig->get_subsystem($subsysID);              my $sub = $fig->get_subsystem($subsysID);
790              # Only proceed if the subsystem has a spreadsheet.              # Only proceed if the subsystem has a spreadsheet.
791              if (! $sub->{empty_ss}) {              if (defined($sub) && ! $sub->{empty_ss}) {
792                  Trace("Creating subsystem $subsysID.") if T(3);                  Trace("Creating subsystem $subsysID.") if T(3);
793                  $loadSubsystem->Add("subsystemIn");                  $loadSubsystem->Add("subsystemIn");
794                  # Create the subsystem record.                  # Create the subsystem record.
# Line 759  Line 798 
798                  # Now for the classification string. This comes back as a list                  # Now for the classification string. This comes back as a list
799                  # reference and we convert it to a space-delimited string.                  # reference and we convert it to a space-delimited string.
800                  my $classList = $fig->subsystem_classification($subsysID);                  my $classList = $fig->subsystem_classification($subsysID);
801                  my $classString = join(" : ", grep { $_ } @$classList);                  my $classString = join($FIG_Config::splitter, grep { $_ } @$classList);
802                  $loadSubsystemClass->Put($subsysID, $classString);                  $loadSubsystemClass->Put($subsysID, $classString);
803                  # Connect it to its roles. Each role is a column in the subsystem spreadsheet.                  # Connect it to its roles. Each role is a column in the subsystem spreadsheet.
804                  for (my $col = 0; defined($roleID = $sub->get_role($col)); $col++) {                  for (my $col = 0; defined($roleID = $sub->get_role($col)); $col++) {
# Line 962  Line 1001 
1001          # Create a hash for storing property IDs.          # Create a hash for storing property IDs.
1002          my %propertyKeys = ();          my %propertyKeys = ();
1003          my $nextID = 1;          my $nextID = 1;
1004            # Get the attributes we intend to store in the property table.
1005            my @propKeys = $fig->get_group_keys("NMPDR");
1006          # Loop through the genomes.          # Loop through the genomes.
1007          for my $genomeID (sort keys %{$genomeHash}) {          for my $genomeID (sort keys %{$genomeHash}) {
1008              $loadProperty->Add("genomeIn");              $loadProperty->Add("genomeIn");
1009              Trace("Generating properties for $genomeID.") if T(3);              Trace("Generating properties for $genomeID.") if T(3);
1010              # Get the genome's features. The feature ID is the first field in the              # Initialize a counter.
             # tuples returned by "all_features_detailed". We use "all_features_detailed"  
             # rather than "all_features" because we want all features regardless of type.  
             my @features = map { $_->[0] } @{$fig->all_features_detailed($genomeID)};  
             my $featureCount = 0;  
1011              my $propertyCount = 0;              my $propertyCount = 0;
1012              # Loop through the features, creating HasProperty records.              # Get the properties for this genome's features.
1013              for my $fid (@features) {              my @attributes = $fig->get_attributes("fig|$genomeID%", \@propKeys);
1014                  # Get all attributes for this feature. We do this one feature at a time              Trace("Property list built for $genomeID.") if T(3);
1015                  # to insure we do not get any genome attributes.              # Loop through the results, creating HasProperty records.
1016                  my @attributeList = $fig->get_attributes($fid, '', '', '');              for my $attributeData (@attributes) {
1017                  # Add essentiality and virulence attributes.                  # Pull apart the attribute tuple.
1018                  if ($fig->essential($fid)) {                  my ($fid, $key, $value, $url) = @{$attributeData};
                     push @attributeList, [$fid, 'essential', 1, ''];  
                 }  
                 if ($fig->virulent($fid)) {  
                     push @attributeList, [$fid, 'virulent', 1, ''];  
                 }  
                 if (scalar @attributeList) {  
                     $featureCount++;  
                 }  
                 # Loop through the attributes.  
                 for my $tuple (@attributeList) {  
                     $propertyCount++;  
                     # Get this attribute value's data. Note that we throw away the FID,  
                     # since it will always be the same as the value if "$fid".  
                     my (undef, $key, $value, $url) = @{$tuple};  
1019                      # Concatenate the key and value and check the "propertyKeys" hash to                      # Concatenate the key and value and check the "propertyKeys" hash to
1020                      # see if we already have an ID for it. We use a tab for the separator                      # see if we already have an ID for it. We use a tab for the separator
1021                      # character.                      # character.
# Line 1010  Line 1033 
1033                      # Create the HasProperty entry for this feature/property association.                      # Create the HasProperty entry for this feature/property association.
1034                      $loadHasProperty->Put($fid, $propertyID, $url);                      $loadHasProperty->Put($fid, $propertyID, $url);
1035                  }                  }
             }  
1036              # Update the statistics.              # Update the statistics.
1037              Trace("$propertyCount attributes processed for $featureCount features.") if T(3);              Trace("$propertyCount attributes processed.") if T(3);
             $loadHasProperty->Add("featuresIn", $featureCount);  
1038              $loadHasProperty->Add("propertiesIn", $propertyCount);              $loadHasProperty->Add("propertiesIn", $propertyCount);
1039          }          }
1040      }      }
# Line 1578  Line 1599 
1599      return $retVal;      return $retVal;
1600  }  }
1601    
1602    =head3 LoadDrugData
1603    
1604    C<< my $stats = $spl->LoadDrugData(); >>
1605    
1606    Load the drug target data into Sprout.
1607    
1608    The following relations are loaded by this method.
1609    
1610        PDB
1611        DocksWith
1612        IsProteinForFeature
1613        Ligand
1614    
1615    The source information for these relations is taken from attributes. The
1616    C<PDB> attribute links a PDB to a feature, and is used to build B<IsProteinForFeature>.
1617    The C<zinc_name> attribute describes the ligands. The C<docking_results>
1618    attribute contains the information for the B<DocksWith> relationship. It is
1619    expected that additional attributes and tables will be added in the future.
1620    
1621    =over 4
1622    
1623    =item RETURNS
1624    
1625    Returns a statistics object for the loads.
1626    
1627    =back
1628    
1629    =cut
1630    #: Return Type $%;
1631    sub LoadDrugData {
1632        # Get this object instance.
1633        my ($self) = @_;
1634        # Get the FIG object.
1635        my $fig = $self->{fig};
1636        # Get the genome hash.
1637        my $genomeHash = $self->{genomes};
1638        # Create load objects for the tables we're loading.
1639        my $loadPDB = $self->_TableLoader('PDB');
1640        my $loadLigand = $self->_TableLoader('Ligand');
1641        my $loadIsProteinForFeature = $self->_TableLoader('IsProteinForFeature');
1642        my $loadDocksWith = $self->_TableLoader('DocksWith');
1643        if ($self->{options}->{loadOnly}) {
1644            Trace("Loading from existing files.") if T(2);
1645        } else {
1646            Trace("Generating drug target data.") if T(2);
1647            # First comes the "DocksWith" relationship. This will give us a list of PDBs.
1648            # We can also encounter PDBs when we process "IsProteinForFeature". To manage
1649            # this process, PDB information is collected in a hash table and then
1650            # unspooled after both relationships are created.
1651            my %pdbHash = ();
1652            Trace("Generating docking data.") if T(2);
1653            # Get all the docking data. This may cause problems if there are too many PDBs,
1654            # at which point we'll need another algorithm. The indicator that this is
1655            # happening will be a timeout error in the next statement.
1656            my @dockData = $fig->query_attributes('$key = ? AND $value < ?',
1657                                                  ['docking_results', $FIG_Config::dockLimit]);
1658            Trace(scalar(@dockData) . " rows of docking data found.") if T(3);
1659            for my $dockData (@dockData) {
1660                # Get the docking data components.
1661                my ($pdbID, $docking_key, @valueData) = @{$dockData};
1662                # Fix the PDB ID. It's supposed to be lower-case, but this does not always happen.
1663                $pdbID = lc $pdbID;
1664                # Strip off the object type.
1665                $pdbID =~ s/pdb://;
1666                # Extract the ZINC ID from the docking key. Note that there are two possible
1667                # formats.
1668                my (undef, $zinc_id) = $docking_key =~ /^docking_results::(ZINC)?(\d+)$/;
1669                if (! $zinc_id) {
1670                    Trace("Invalid docking result key $docking_key for $pdbID.") if T(0);
1671                    $loadDocksWith->Add("errors");
1672                } else {
1673                    # Get the pieces of the value and parse the energy.
1674                    # Note that we don't care about the rank, since
1675                    # we can sort on the energy level itself in our database.
1676                    my ($energy, $tool, $type) = @valueData;
1677                    my ($rank, $total, $vanderwaals, $electrostatic) = split /\s*;\s*/, $energy;
1678                    # Ignore predicted results.
1679                    if ($type ne "Predicted") {
1680                        # Count this docking result.
1681                        if (! exists $pdbHash{$pdbID}) {
1682                            $pdbHash{$pdbID} = 1;
1683                        } else {
1684                            $pdbHash{$pdbID}++;
1685                        }
1686                        # Write the result to the output.
1687                        $loadDocksWith->Put($pdbID, $zinc_id, $electrostatic, $type, $tool,
1688                                            $total, $vanderwaals);
1689                    }
1690                }
1691            }
1692            Trace("Connecting features.") if T(2);
1693            # Loop through the genomes.
1694            for my $genome (sort keys %{$genomeHash}) {
1695                Trace("Generating PDBs for $genome.") if T(3);
1696                # Get all of the PDBs that BLAST against this genome's features.
1697                my @attributeData = $fig->get_attributes("fig|$genome%", 'PDB::%');
1698                for my $pdbData (@attributeData) {
1699                    # The PDB ID is coded as a subkey.
1700                    if ($pdbData->[1] !~ /PDB::(.+)/i) {
1701                        Trace("Invalid PDB ID \"$pdbData->[1]\" in attribute table.") if T(0);
1702                        $loadPDB->Add("errors");
1703                    } else {
1704                        my $pdbID = $1;
1705                        # Insure the PDB is in the hash.
1706                        if (! exists $pdbHash{$pdbID}) {
1707                            $pdbHash{$pdbID} = 0;
1708                        }
1709                        # The score and locations are coded in the attribute value.
1710                        if ($pdbData->[2] !~ /^([^;]+)(.*)$/) {
1711                            Trace("Invalid PDB data for $pdbID and feature $pdbData->[0].") if T(0);
1712                            $loadIsProteinForFeature->Add("errors");
1713                        } else {
1714                            my ($score, $locData) = ($1,$2);
1715                            # The location data may not be present, so we have to start with some
1716                            # defaults and then check.
1717                            my ($start, $end) = (1, 0);
1718                            if ($locData) {
1719                                $locData =~ /(\d+)-(\d+)/;
1720                                $start = $1;
1721                                $end = $2;
1722                            }
1723                            # If we still don't have the end location, compute it from
1724                            # the feature length.
1725                            if (! $end) {
1726                                # Most features have one location, but we do a list iteration
1727                                # just in case.
1728                                my @locations = $fig->feature_location($pdbData->[0]);
1729                                $end = 0;
1730                                for my $loc (@locations) {
1731                                    my $locObject = BasicLocation->new($loc);
1732                                    $end += $locObject->Length;
1733                                }
1734                            }
1735                            # Decode the score.
1736                            my $realScore = FIGRules::DecodeScore($score);
1737                            # Connect the PDB to the feature.
1738                            $loadIsProteinForFeature->Put($pdbData->[0], $pdbID, $start, $realScore, $end);
1739                        }
1740                    }
1741                }
1742            }
1743            # We've got all our PDBs now, so we unspool them from the hash.
1744            Trace("Generating PDBs. " . scalar(keys %pdbHash) . " found.") if T(2);
1745            my $count = 0;
1746            for my $pdbID (sort keys %pdbHash) {
1747                $loadPDB->Put($pdbID, $pdbHash{$pdbID});
1748                $count++;
1749                Trace("$count PDBs processed.") if T(3) && ($count % 500 == 0);
1750            }
1751            # Finally we create the ligand table. This information can be found in the
1752            # zinc_name attribute.
1753            Trace("Loading ligands.") if T(2);
1754            # The ligand list is huge, so we have to get it in pieces. We also have to check for duplicates.
1755            my $last_zinc_id = "";
1756            my $zinc_id = "";
1757            my $done = 0;
1758            while (! $done) {
1759                # Get the next 10000 ligands. We insist that the object ID is greater than
1760                # the last ID we processed.
1761                Trace("Loading batch starting with ZINC:$zinc_id.") if T(3);
1762                my @attributeData = $fig->query_attributes('$object > ? AND $key = ? ORDER BY $object LIMIT 10000',
1763                                                           ["ZINC:$zinc_id", "zinc_name"]);
1764                Trace(scalar(@attributeData) . " attribute rows returned.") if T(3);
1765                if (! @attributeData) {
1766                    # Here there are no attributes left, so we quit the loop.
1767                    $done = 1;
1768                } else {
1769                    # Process the attribute data we've received.
1770                    for my $zinc_data (@attributeData) {
1771                        # The ZINC ID is found in the first return column, prefixed with the word ZINC.
1772                        if ($zinc_data->[0] =~ /^ZINC:(\d+)$/) {
1773                            $zinc_id = $1;
1774                            # Check for a duplicate.
1775                            if ($zinc_id eq $last_zinc_id) {
1776                                $loadLigand->Add("duplicate");
1777                            } else {
1778                                # Here it's safe to output the ligand. The ligand name is the attribute value
1779                                # (third column in the row).
1780                                $loadLigand->Put($zinc_id, $zinc_data->[2]);
1781                                # Insure we don't try to add this ID again.
1782                                $last_zinc_id = $zinc_id;
1783                            }
1784                        } else {
1785                            Trace("Invalid zinc ID \"$zinc_data->[0]\" in attribute table.") if T(0);
1786                            $loadLigand->Add("errors");
1787                        }
1788                    }
1789                }
1790            }
1791            Trace("Ligands loaded.") if T(2);
1792        }
1793        # Finish the load.
1794        my $retVal = $self->_FinishAll();
1795        return $retVal;
1796    }
1797    
1798    
1799  =head2 Internal Utility Methods  =head2 Internal Utility Methods
1800    
1801    =head3 SpecialAttribute
1802    
1803    C<< my $count = SproutLoad::SpecialAttribute($id, \@attributes, $idxMatch, \@idxValues, $pattern, $loader); >>
1804    
1805    Look for special attributes of a given type. A special attribute is found by comparing one of
1806    the columns of the incoming attribute list to a search pattern. If a match is found, then
1807    a set of columns is put into an output table connected to the specified ID.
1808    
1809    For example, when processing features, the attribute list we look at has three columns: attribute
1810    name, attribute value, and attribute value HTML. The IEDB attribute exists if the attribute name
1811    begins with C<iedb_>. The call signature is therefore
1812    
1813        my $found = SpecialAttribute($fid, \@attributeList, 0, [0,2], '^iedb_', $loadFeatureIEDB);
1814    
1815    The pattern is matched against column 0, and if we have a match, then column 2's value is put
1816    to the output along with the specified feature ID.
1817    
1818    =over 4
1819    
1820    =item id
1821    
1822    ID of the object whose special attributes are being loaded. This forms the first column of the
1823    output.
1824    
1825    =item attributes
1826    
1827    Reference to a list of tuples.
1828    
1829    =item idxMatch
1830    
1831    Index in each tuple of the column to be matched against the pattern. If the match is
1832    successful, an output record will be generated.
1833    
1834    =item idxValues
1835    
1836    Reference to a list containing the indexes in each tuple of the columns to be put as
1837    the second column of the output.
1838    
1839    =item pattern
1840    
1841    Pattern to be matched against the specified column. The match will be case-insensitive.
1842    
1843    =item loader
1844    
1845    An object to which each output record will be put. Usually this is an B<ERDBLoad> object,
1846    but technically it could be anything with a C<Put> method.
1847    
1848    =item RETURN
1849    
1850    Returns a count of the matches found.
1851    
1852    =item
1853    
1854    =back
1855    
1856    =cut
1857    
1858    sub SpecialAttribute {
1859        # Get the parameters.
1860        my ($id, $attributes, $idxMatch, $idxValues, $pattern, $loader) = @_;
1861        # Declare the return variable.
1862        my $retVal = 0;
1863        # Loop through the attribute rows.
1864        for my $row (@{$attributes}) {
1865            # Check for a match.
1866            if ($row->[$idxMatch] =~ m/$pattern/i) {
1867                # We have a match, so output a row. This is a bit tricky, since we may
1868                # be putting out multiple columns of data from the input.
1869                my $value = join(" ", map { $row->[$_] } @{$idxValues});
1870                $loader->Put($id, $value);
1871                $retVal++;
1872            }
1873        }
1874        Trace("$retVal special attributes found for $id and loader " . $loader->RelName() . ".") if T(4) && $retVal;
1875        # Return the number of matches.
1876        return $retVal;
1877    }
1878    
1879  =head3 TableLoader  =head3 TableLoader
1880    
1881  Create an ERDBLoad object for the specified table. The object is also added to  Create an ERDBLoad object for the specified table. The object is also added to
# Line 1685  Line 1979 
1979      return $retVal;      return $retVal;
1980  }  }
1981    
1982    =head3 GetGenomeAttributes
1983    
1984    C<< my $aHashRef = GetGenomeAttributes($fig, $genomeID, \@fids); >>
1985    
1986    Return a hash of attributes keyed on feature ID. This method gets all the NMPDR-related
1987    attributes for all the features of a genome in a single call, then organizes them into
1988    a hash.
1989    
1990    =over 4
1991    
1992    =item fig
1993    
1994    FIG-like object for accessing attributes.
1995    
1996    =item genomeID
1997    
1998    ID of the genome who's attributes are desired.
1999    
2000    =item fids
2001    
2002    Reference to a list of the feature IDs whose attributes are to be kept.
2003    
2004    =item RETURN
2005    
2006    Returns a reference to a hash. The key of the hash is the feature ID. The value is the
2007    reference to a list of the feature's attribute tuples. Each tuple contains the feature ID,
2008    the attribute key, and one or more attribute values.
2009    
2010    =back
2011    
2012    =cut
2013    
2014    sub GetGenomeAttributes {
2015        # Get the parameters.
2016        my ($fig, $genomeID, $fids) = @_;
2017        # Declare the return variable.
2018        my $retVal = {};
2019        # Get a list of the attributes we care about.
2020        my @propKeys = $fig->get_group_keys("NMPDR");
2021        # Get the attributes.
2022        my @aList = $fig->get_attributes("fig|$genomeID%", \@propKeys);
2023        # Initialize the hash. This not only enables us to easily determine which FIDs to
2024        # keep, it insures that the caller sees a list reference for every known fid,
2025        # simplifying the logic.
2026        for my $fid (@{$fids}) {
2027            $retVal->{$fid} = [];
2028        }
2029        # Populate the hash.
2030        for my $aListEntry (@aList) {
2031            my $fid = $aListEntry->[0];
2032            if (exists $retVal->{$fid}) {
2033                push @{$retVal->{$fid}}, $aListEntry;
2034            }
2035        }
2036        # Return the result.
2037        return $retVal;
2038    }
2039    
2040  1;  1;

Legend:
Removed from v.1.75  
changed lines
  Added in v.1.84

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3