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

Diff of /Sprout/SaplingGenomeLoader.pm

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

revision 1.2, Tue Jan 11 15:04:03 2011 UTC revision 1.3, Tue Jan 25 19:41:04 2011 UTC
# Line 26  Line 26 
26      use SAPserver;      use SAPserver;
27      use Sapling;      use Sapling;
28      use AliasAnalysis;      use AliasAnalysis;
29        use base qw(SaplingDataLoader);
30    
31  =head1 Sapling Genome Loader  =head1 Sapling Genome Loader
32    
# Line 184  Line 185 
185      # Get the parameters.      # Get the parameters.
186      my ($class, $sap, $genome, $directory) = @_;      my ($class, $sap, $genome, $directory) = @_;
187      # Create the object.      # Create the object.
188      my $retVal = {      my $retVal = SaplingDataLoader::new($class, $sap, qw(contigs dna pegs rnas));
189          sap => $sap,      # Add our specialized data.
190          genome => $genome,      $retVal->{genome} = $genome;
191          directory => $directory,      $retVal->{directory} = $directory;
192          stats => Stats->new(qw(contigs dna pegs rnas)),      # Return the result.
         supportRecords => {}  
     };  
     # Bless and return it.  
     bless $retVal, $class;  
193      return $retVal;      return $retVal;
194  }  }
195    
# Line 379  Line 376 
376    
377  =head3 LoadFeatureData  =head3 LoadFeatureData
378    
379      $self->LoadFeatureData($featureDir, $type);      $loaderObject->LoadFeatureData($featureDir, $type);
380    
381  Load the basic data for each feature into the database. The number of features of  Load the basic data for each feature into the database. The number of features of
382  the type found will be recorded in the statistics object.  the type found will be recorded in the statistics object.
# Line 415  Line 412 
412      # Insure we have a tbl file for this feature type.      # Insure we have a tbl file for this feature type.
413      my $fileName = "$featureDir/$type/tbl";      my $fileName = "$featureDir/$type/tbl";
414      if (-f $fileName) {      if (-f $fileName) {
415          # We have one, so open it for input.          # We have one, so we can read through it. First, however, we need to get the list
416            # of deleted features.
417            my %deletedFids;
418            my $deleteFile = "$featureDir/$type/deleted.features";
419            if (-f $deleteFile) {
420                %deletedFids = map { $_ => 1 } Tracer::GetFile($deleteFile);
421            }
422            # Open the main file for input.
423          my $ih = Open(undef, "<$fileName");          my $ih = Open(undef, "<$fileName");
424          while (! eof $ih) {          while (! eof $ih) {
425              # Read this feature's information.              # Read this feature's information.
426              my ($fid, $locations, @aliases) = Tracer::GetLine($ih);              my ($fid, $locations, @aliases) = Tracer::GetLine($ih);
427              # If the feature already exists, delete it.              # Only proceed if the feature is NOT deleted.
428                if (! exists $deletedFids{$fid}) {
429                    # If the feature already exists, delete it. (This should be extremely rare.)
430              if (exists $fids{$fid}) {              if (exists $fids{$fid}) {
431                  $sap->Delete(Feature => $fid);                  $sap->Delete(Feature => $fid);
432                  $stats->Add(duplicateFid => 1);                  $stats->Add(duplicateFid => 1);
# Line 534  Line 540 
540          }          }
541      }      }
542  }  }
543        # Now loop through the features, connecting them to their roles. Note that deleted
544        # features will not be in the assignment hash.
545        for my $fid (keys %$assignHash) {
546            # Get the roles and the error count.
547            my ($roles, $errors) = SeedUtils::roles_for_loading($assignHash->{$fid});
548            # Accumulate the errors in the stats object.
549            $stats->Add(roleErrors => $errors);
550            # Is this a suspicious function?
551            if (! defined $roles) {
552                # Yes, so track it.
553                $stats->Add(badFunction => 1);
554            } else {
555                # No, connect the roles.
556                for my $role (@$roles) {
557                    # Insure this role exists.
558                    my $hypo = hypo($role);
559                    $self->InsureEntity(Role => $role, hypothetical => $hypo);
560                    # Connect it to the feature.
561                    $sap->InsertObject('IsFunctionalIn', from_link => $role, to_link => $fid);
562                }
563            }
564        }
565    }
566    
567  =head3 LoadProteinData  =head3 LoadProteinData
568    
# Line 621  Line 650 
650      $self->{sap}->InsertObject('IsProteinFor', from_link => $protID, to_link => $fid);      $self->{sap}->InsertObject('IsProteinFor', from_link => $protID, to_link => $fid);
651  }  }
652    
 =head3 InsureEntity  
   
     my $createdFlag = $loaderObject->InsureEntity($entityType => $id, %fields);  
   
 Insure that the specified record exists in the database. If no record is found of the  
 specified type with the specified ID, one will be created with the indicated fields.  
   
 =over 4  
   
 =item $entityType  
   
 Type of entity to check.  
   
 =item id  
   
 ID of the entity instance in question.  
   
 =item fields  
   
 Hash mapping field names to values for all the fields in the desired entity record except  
 for the ID.  
   
 =item RETURN  
   
 Returns TRUE if a new object was created, FALSE if it already existed.  
   
 =back  
   
 =cut  
   
 sub InsureEntity {  
     # Get the parameters.  
     my ($self, $entityType, $id, %fields) = @_;  
     # Get the database.  
     my $sap = $self->{sap};  
     # Get the support record ID hash.  
     my $supportHash = $self->{supportRecords};  
     # Denote we haven't created a new record.  
     my $retVal = 0;  
     # Get the sub-hash for this entity type.  
     my $entityHash = $supportHash->{$entityType};  
     if (! defined $entityHash) {  
         $entityHash = {};  
         $supportHash->{$entityType} = $entityHash;  
     }  
     # Check for this instance.  
     if (! $entityHash->{$id}) {  
         # It's not found. Check the database.  
         if (! $sap->Exists($entityType => $id)) {  
             # It's not in the database either, so create it.  
             $sap->InsertObject($entityType, id => $id, %fields);  
             $self->{stats}->Add(insertSupport => 1);  
             $retVal = 1;  
         }  
         # Mark the record in the hash so we know we have it.  
         $entityHash->{$id} = 1;  
     }  
     # Return the insertion indicator.  
     return $retVal;  
 }  
   
653  =head3 LoadSubsystems  =head3 LoadSubsystems
654    
655      $loaderObject->LoadSubsystems();      $loaderObject->LoadSubsystems();
# Line 737  Line 705 
705              if (@$roleList > 0) {              if (@$roleList > 0) {
706                  # Get the subsystem information from the first role and create the subsystem.                  # Get the subsystem information from the first role and create the subsystem.
707                  my $roleH = $roleList->[0];                  my $roleH = $roleList->[0];
708                  my %subFields = ExtractFields(Subsystem => $roleH);                  my %subFields = SaplingDataLoader::ExtractFields(Subsystem => $roleH);
709                  $sap->InsertObject('Subsystem', %subFields);                  $sap->InsertObject('Subsystem', %subFields);
710                  # Now loop through the roles. The Includes records are always inserted, but the                  # Now loop through the roles. The Includes records are always inserted, but the
711                  # roles are only inserted if they don't already exist.                  # roles are only inserted if they don't already exist.
712                  for $roleH (@$roleList) {                  for $roleH (@$roleList) {
713                      # Create the Includes record.                      # Create the Includes record.
714                      my %incFields = ExtractFields(Includes => $roleH);                      my %incFields = SaplingDataLoader::ExtractFields(Includes => $roleH);
715                      $sap->InsertObject('Includes', %incFields);                      $sap->InsertObject('Includes', %incFields);
716                      # Insure we have the role in place.                      # Insure we have the role in place.
717                      my %roleFields = ExtractFields(Role => $roleH);                      my %roleFields = SaplingDataLoader::ExtractFields(Role => $roleH);
718                      my $roleID = $roleFields{id};                      my $roleID = $roleFields{id};
719                      delete $roleFields{id};                      delete $roleFields{id};
720                      $self->InsureEntity('Role', $roleID, %roleFields);                      $self->InsureEntity('Role', $roleID, %roleFields);
# Line 986  Line 954 
954    
955  =head2 Internal Utility Methods  =head2 Internal Utility Methods
956    
 =head3 DeleteRelatedRecords  
   
     DeleteRelatedRecords($sap, $genome, $stats, $relName, $entityName);  
   
 Delete all the records in the named entity and relationship relating to the  
 specified genome and roll up the statistics in the specified statistics object.  
   
 =over 4  
   
 =item sap  
   
 L<Sapling> object for accessing the database.  
   
 =item genome  
   
 ID of the relevant genome.  
   
 =item stats  
   
 L<Stats> object for tracking the delete activity.  
   
 =item relName  
   
 Name of a relationship from the B<Genome> table.  
   
 =item entityName  
   
 Name of the entity on the other side of the relationship.  
   
 =back  
   
 =cut  
   
 sub DeleteRelatedRecords {  
     # Get the parameters.  
     my ($sap, $genome, $stats, $relName, $entityName) = @_;  
     # Get all the relationship records.  
     my (@targets) = $sap->GetFlat($relName, "$relName(from-link) = ?", [$genome],  
                                   "to-link");  
     # Loop through the relationship records, deleting them and the target entity  
     # records.  
     for my $target (@targets) {  
         # Delete the relationship instance.  
         $sap->DeleteRow($relName, $genome, $target);  
         $stats->Add($relName => 1);  
         # Delete the entity instance.  
         my $subStats = $sap->Delete($entityName, $target);  
         # Roll up the statistics.  
         $stats->Accumulate($subStats);  
     }  
 }  
   
 =head3 ExtractFields  
   
     my %fieldHash = SaplingGenomeLoader::ExtractFields($tableName, $dataHash);  
   
 Extract from the incoming hash the field names and values from the specified table.  
   
 =over 4  
   
 =item tableName  
   
 Name of the table whose field names and values are desired.  
   
 =item dataHash  
   
 Reference to a hash mapping fully-qualified ERDB field names to values.  
   
 =item RETURN  
   
 Returns a hash containing only the fields from the specified table and their values.  
   
 =back  
   
 =cut  
   
 sub ExtractFields {  
     # Get the parameters.  
     my ($tableName, $dataHash) = @_;  
     # Declare the return variable.  
     my %retVal;  
     # Extract the desired fields.  
     for my $field (keys %$dataHash) {  
         # Is this a field for the specified table?  
         if ($field =~ /^$tableName\(([^)]+)/) {  
             # Yes, put it in the output hash.  
             $retVal{$1} = $dataHash->{$field};  
         }  
     }  
     # Return the computed hash.  
     return %retVal;  
 }  
   
957  =head3 CreateIdentifier  =head3 CreateIdentifier
958    
959      $loaderObject->CreateIdentifier($alias, $conf, $aliasType, $fid);      $loaderObject->CreateIdentifier($alias, $conf, $aliasType, $fid);

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3