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

Diff of /Sprout/FeatureSaplingLoader.pm

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

revision 1.1, Mon Jan 19 21:43:27 2009 UTC revision 1.13, Sat Feb 26 19:05:31 2011 UTC
# Line 25  Line 25 
25      use CGI qw(-nosticky);      use CGI qw(-nosticky);
26      use BasicLocation;      use BasicLocation;
27      use HyperLink;      use HyperLink;
28        use AliasAnalysis;
29        use LoaderUtils;
30        use Digest::MD5;
31        use SeedUtils;
32      use base 'BaseSaplingLoader';      use base 'BaseSaplingLoader';
33    
34  =head1 Sapling Feature Load Group Class  =head1 Sapling Feature Load Group Class
# Line 43  Line 47 
47    
48  =item erdb  =item erdb
49    
50  [[SaplingPm]] object for the database being loaded.  L<Sapling> object for the database being loaded.
51    
52  =item options  =item options
53    
# Line 61  Line 65 
65      # Get the parameters.      # Get the parameters.
66      my ($class, $erdb, $options) = @_;      my ($class, $erdb, $options) = @_;
67      # Create the table list.      # Create the table list.
68      my @tables = sort qw(Feature FeatureEssential FeatureEvidence FeatureLink FeatureVirulent      my @tables = sort qw(Feature FeatureEssential FeatureEvidence FeatureLink
69                          Identifier IsSequenceFor ProteinSequence Concerns Publication                           FeatureVirulent IsOwnerOf IsLocatedIn IsIdentifiedBy
70                          IdentifierSet IncludesIdentifier IsLocatedIn IsOwnerOf);                           Identifier IsNamedBy ProteinSequence Concerns
71                             IsAttachmentSiteFor Publication IsProteinFor
72                             Role RoleIndex IsFunctionalIn);
73      # Create the BaseSaplingLoader object.      # Create the BaseSaplingLoader object.
74      my $retVal = BaseSaplingLoader::new($class, $erdb, $options, @tables);      my $retVal = BaseSaplingLoader::new($class, $erdb, $options, @tables);
75      # Return it.      # Return it.
# Line 85  Line 91 
91      my ($self) = @_;      my ($self) = @_;
92      # Get the database object.      # Get the database object.
93      my $erdb = $self->db();      my $erdb = $self->db();
94      # Only proceed if this is a normal section. There's no global feature data.      # Check for local or global.
95      if (! $self->global()) {      if (! $self->global()) {
96          # Get the section ID.          # Here we are generating data for a genome.
97          my $genomeID = $self->section();          my $genomeID = $self->section();
98          # Load this genome's features.          # Load this genome's features.
99          $self->LoadGenomeFeatures($genomeID);          $self->LoadGenomeFeatures($genomeID);
100        } else {
101            # The global data is the roles from subsystems and the publications.
102            my $fig = $self->source();
103            # We need the master map of roles to IDs.
104            my %roleHash;
105            my $lastRoleIndex = -1;
106            my $roleMapFile = $erdb->LoadDirectory() . "/roleMap.tbl";
107            if (-f $roleMapFile) {
108                for my $mapLine (Tracer::GetFile($roleMapFile)) {
109                    my ($role, $idx) = split /\t/, $mapLine;
110                    $roleHash{$role} = $idx;
111                    if ($idx > $lastRoleIndex) {
112                        $lastRoleIndex = $idx;
113                    }
114                }
115            }
116            # We'll track duplicate roles in here.
117            my %roleList;
118            # Now we get the subsystem list.
119            my $subHash = $erdb->SubsystemHash();
120            for my $sub (sort keys %$subHash) {
121                $self->Add(subsystems => 1);
122                Trace("Processing roles for $sub.") if T(3);
123                # Get this subsystem's roles and write them out.
124                my @roles = $fig->subsystem_to_roles($sub);
125                for my $role (@roles) {
126                    $self->Add(subsystemRoles => 1);
127                    # Check to see if this role is hypothetical.
128                    my $hypo = hypo($role);
129                    if (! $hypo) {
130                        # Is this role in the role index hash?
131                        my $roleIndex = $roleHash{$role};
132                        if (! defined $roleIndex) {
133                            # No, compute a new index for it.
134                            $roleIndex = ++$lastRoleIndex;
135                            $roleHash{$role} = $roleIndex;
136                        }
137                        if (! $roleList{$role}) {
138                            $roleList{$role} = 1;
139                            $self->PutE(RoleIndex => $role, role_index => $roleIndex);
140                        }
141                    }
142                    $self->PutE(Role => $role, hypothetical => $hypo);
143                }
144            }
145            Trace("Subsystem roles generated.") if T(2);
146            # Write out the role master file.
147            Tracer::PutFile($roleMapFile, [map { "$_\t$roleHash{$_}" } keys %roleHash]);
148            Trace("Role master file written to $roleMapFile.") if T(2);
149            # Now, we get the publications.
150            my $pubs = $fig->all_titles();
151            for my $pub (@$pubs) {
152                # Get the ID and title.
153                my ($pubmedID, $title) = @$pub;
154                # Only proceed if the ID is valid.
155                if ($pubmedID) {
156                    # Create a hyperlink from the title and the pubmed ID.
157                    my $link;
158                    if (! $title) {
159                        $link = HyperLink->new("<unknown>");
160                    } else {
161                        $link = HyperLink->new($title, "http://www.ncbi.nlm.nih.gov/pubmed/$pubmedID");
162                    }
163                    # Create the publication record.
164                    $self->PutE(Publication => $pubmedID, citation => $link);
165                }
166            }
167            Trace("Publications generated.") if T(2);
168      }      }
169  }  }
170    
# Line 119  Line 193 
193      my $sapling = $self->db();      my $sapling = $self->db();
194      # Get the maximum location  segment length. We'll need this later.      # Get the maximum location  segment length. We'll need this later.
195      my $maxLength = $sapling->TuningParameter('maxLocationLength');      my $maxLength = $sapling->TuningParameter('maxLocationLength');
196      # This hash will be used to track identifiers. Each identifier can only      # Get the genome's aliases.
197      # be used once.      my $aliasDir = $sapling->LoadDirectory() . "/AliasData";
198      my %identifiers;      my $aliasHash = LoaderUtils::ReadAliasFile($aliasDir, $genomeID);
199        if (! defined $aliasHash) {
200            Trace("No aliases found for $genomeID.") if T(ERDBLoadGroup => 1);
201            $self->Add(missingAliasFile => 1);
202            $aliasHash = {};
203        }
204      # Get all of this genome's features.      # Get all of this genome's features.
205      my $featureList = $fig->all_features_detailed_fast($genomeID);      my $featureList = $fig->all_features_detailed_fast($genomeID);
206      # Loop through them.      # Loop through them.
# Line 130  Line 209 
209          my ($fid, $locationString, $aliases, $type, undef, undef, $assignment,          my ($fid, $locationString, $aliases, $type, undef, undef, $assignment,
210              $assignmentMaker, $quality) = @$feature;              $assignmentMaker, $quality) = @$feature;
211          $self->Track(Features => $fid, 1000);          $self->Track(Features => $fid, 1000);
212          # Fix the assignment for non-PEG features.          # Fix missing assignments. For RNAs, the assignment may be in the alias list.
213          if (! defined $assignment) {          if (! defined $assignment) {
214                if ($type eq 'rna') {
215              $assignment = $aliases;              $assignment = $aliases;
216              $assignmentMaker ||= 'master';              $assignmentMaker ||= 'master';
217                } else {
218                    $assignment = '';
219                }
220          }          }
221          # Convert the location string to a list of location objects.          # Convert the location string to a list of location objects.
222          my @locs = map { BasicLocation->new($_) } split /\s*,\s*/, $locationString;          my @locs = map { BasicLocation->new($_) } split /\s*,\s*/, $locationString;
# Line 156  Line 239 
239              # to the feature.              # to the feature.
240              my $peel = $loc->Peel($maxLength);              my $peel = $loc->Peel($maxLength);
241              while (defined $peel) {              while (defined $peel) {
242                  $self->PutR(IsLocatedIn => $fid, $contigID, beg => $peel->Left(),                  $self->PutR(IsLocatedIn => $fid, $contigID, ordinal => $locN++,
243                              dir => $dir, len => $maxLength, locN => $locN++);                              begin => $peel->Left(), len => $peel->Length(),
244                                dir => $dir);
245                  $peel = $loc->Peel($maxLength);                  $peel = $loc->Peel($maxLength);
246              }              }
247              # Output the residual. There will always be one, because of the way              # Output the residual. There will always be one, because of the way
248              # Peel works.              # Peel works.
249              $self->PutR(IsLocatedIn => $fid, $contigID, beg => $loc->Left(),              $self->PutR(IsLocatedIn => $fid, $contigID, ordinal => $locN,
250                          dir => $dir, len => $loc->Length(), locN => $locN);                          begin => $loc->Left(), dir => $dir, len => $loc->Length());
251            }
252            # Is this an attachment site?
253            if ($type eq 'att') {
254                # Yes, connect it to the attached feature.
255                if ($assignment =~ /att([LR])\s+for\s+(fig\|.+)/) {
256                    $self->PutR(IsAttachmentSiteFor => $fid, $2, edge => $1);
257                } else {
258                    Trace("Invalid attachment function for $fid: $assignment") if T(ERDBLoadGroup => 1);
259                    $self->Add(badAttachment => 1);
260                }
261          }          }
262          # Emit the feature record.          # Emit the feature record.
263          $self->PutE(Feature => $fid, feature_type => $type,          $self->PutE(Feature => $fid, feature_type => $type,
264                      sequence_length => $seqLen, function => $assignment);                      sequence_length => $seqLen, function => $assignment,
265                        locked => $fig->is_locked_fid($fid));
266          # Connect the feature to its genome.          # Connect the feature to its genome.
267          $self->PutR(IsOwnerOf => $genomeID, $fid);          $self->PutR(IsOwnerOf => $genomeID, $fid);
268            # Connect the feature to its roles.
269            my ($roles, $errors) = SeedUtils::roles_for_loading($assignment);
270            if (! defined $roles) {
271                # Here the functional assignment was suspicious.
272                $self->Add(suspiciousFunction => 1);
273                Trace("$fid has a suspicious function: $assignment") if T(ERDBLoadGroup => 1);
274            } else {
275                # Here we have a good assignment.
276                for my $role (@$roles) {
277                    $self->Add(featureRole => 1);
278                    $self->PutR(IsFunctionalIn => $role, $fid);
279                    $self->PutE(Role => $role, hypothetical => hypo($role));
280                }
281                $self->Add(badFeatureRoles => $errors);
282            }
283          # Now we have a whole bunch of attribute-related stuff to store in          # Now we have a whole bunch of attribute-related stuff to store in
284          # secondary Feature tables. First is the evidence codes.          # secondary Feature tables. First is the evidence codes. This is special
285            # because we have to save the DLIT numbers.
286            my @dlits;
287          my @evidenceTuples = $fig->get_attributes($fid, 'evidence_code');          my @evidenceTuples = $fig->get_attributes($fid, 'evidence_code');
288          for my $evidenceTuple (@evidenceTuples) {          for my $evidenceTuple (@evidenceTuples) {
289              my (undef, undef, $code) = @$evidenceTuple;              my (undef, undef, $code) = @$evidenceTuple;
290              $self->PutE(FeatureEvidence => $fid, 'evidence-code' => $code);              $self->PutE(FeatureEvidence => $fid, 'evidence-code' => $code);
291                # If this is a direct literature reference, save it.
292                if ($code =~ /dlit\((\d+)/) {
293                    push @dlits, $1;
294                    $self->Add(dlits => 1);
295                }
296          }          }
297          # Now we have the external links. These are stored using hyperlink objects.          # Now we have the external links. These are stored using hyperlink objects.
298          my @links = $fig->fid_links($fid);          my @links = $fig->fid_links($fid);
# Line 194  Line 311 
311          my @essentials = $fig->get_attributes($fid, undef, ['essential', 'potential-essential']);          my @essentials = $fig->get_attributes($fid, undef, ['essential', 'potential-essential']);
312          for my $essentialTuple (@essentials) {          for my $essentialTuple (@essentials) {
313              my (undef, undef, $essentialityType, $url) = @$essentialTuple;              my (undef, undef, $essentialityType, $url) = @$essentialTuple;
314                # Only keep this datum if it has a URL. The ones without URLs are
315                # all duplicates.
316                if ($url) {
317              # Form a hyperlink from this essentiality tuple.              # Form a hyperlink from this essentiality tuple.
318              my $link = HyperLink->new($essentialityType, $url);              my $link = HyperLink->new($essentialityType, $url);
319              # Store it as essentiality data for this feature.              # Store it as essentiality data for this feature.
320              $self->PutE(FeatureEssential => $fid, essential => $link);              $self->PutE(FeatureEssential => $fid, essential => $link);
321          }          }
322            }
323          # If this is a PEG, we have a protein sequence.          # If this is a PEG, we have a protein sequence.
324            my $proteinID;
325          if ($type eq 'peg') {          if ($type eq 'peg') {
326              # Get the translation.              # Get the translation.
327              my $proteinSequence = $fig->get_translation($fid);              my $proteinSequence = $fig->get_translation($fid);
328                if (! $proteinSequence) {
329                    Trace("No protein sequence found for $fid.") if T(ERDBLoadGroup => 2);
330                    $self->Add(missingProtein => 1);
331                    # Here there was some sort of error and the protein sequence did
332                    # not come back. Ask for the DNA and translate it instead.
333                    my $dna = $fig->get_dna_seq($fid);
334                    $proteinSequence = FIG::translate($dna, undef, 1);
335                }
336              # Compute the ID.              # Compute the ID.
337              my $proteinID = ERDB::DigestKey($proteinSequence);              $proteinID = $sapling->ProteinID($proteinSequence);
338              # Create the protein record.              # Create the protein record.
339              $self->PutE(ProteinSequence => $proteinID, sequence => $proteinSequence);              $self->PutE(ProteinSequence => $proteinID, sequence => $proteinSequence);
340              # Get the publications for this PEG.              $self->PutR(IsProteinFor => $proteinID, $fid);
341              my @pubs = $fig->get_attributes($fid, 'PUBMED_CURATED_RELEVANT');              # Connect this protein to the feature's publications (if any).
342              for my $pub (@pubs) {              for my $pub (@dlits) {
343                  # Parse out the article title from the data.                  $self->PutR(Concerns => $pub, $proteinID);
344                  my (undef, undef, $data, $url) = @_;              }
345                  my @pieces = split /,/, $data, 3;          }
346                  if (defined $pieces[2]) {          # Now we need to compute the identifiers. We start with the aliases.
347                      # Create the publication record.          # Get the alias data for this feature. If there is none, we force an
348                      my $hl = Hyperlink->new($pieces[2], $url);          # empty list.
349                      my $key = ERDB::DigestKey($url);          my $aliasList = $aliasHash->{$fid} || [];
350                      $self->PutE(Publication => $key, citation => $hl);          # Loop through the aliases found.
351                      # Connect it to the protein.          for my $aliasTuple (@$aliasList) {
352                      $self->PutR(Concerns => $key, $proteinID);              my ($aliasID, $aliasType, $aliasConf) = @$aliasTuple;
353                  }              # Get the natural form. If there is none, then the canonical
354              }              # form IS the natural form. Note we have to make a special check
355              # Now we need to get the identifiers for this feature and put              # for locus tags, which have an insane number of variants.
356              # them in the protein's identifier set. The "1" tells FigPm to              my $natural;
357              # send back the database name with each identifier. Note that              if ($aliasID =~ /LocusTag:(.+)/) {
358              # the FIG ID will come back with this list, but there may not be                  $natural = $1;
359              # a list if the genome is new.              } else {
360              my @idTuples = grep { $_->[0] !~ /^[A-Z][A-Z]_\d+$/ } $fig->get_corresponding_ids($fid, 1); ##HACK: grep out the contig IDs                  $natural = AliasAnalysis::Type($aliasType => $aliasID) || $aliasID;
             if (! @idTuples) {  
                 push @idTuples, [$fid, 'SEED'];  
             }  
             # Compute the identifier set name and create the set.  
             my $setID = "$proteinID:$genomeID";  
             $self->PutE(IdentifierSet => $setID);  
             # Create the identifiers and onnect them to the protein and  
             # the set.  
             for my $idTuple (@idTuples) {  
                 my ($id, $source) = @$idTuple;  
                 # Only process this identifier if it's new. An identifier  
                 # can only be in one identifier set. Thankfully, the  
                 # identifiers belong to genomes, so we don't need to worry  
                 # about duplicates in other sections.  
                 if (exists $identifiers{$id} && $identifiers{$id} ne $proteinID) {  
                     $self->Add(ambiguousProtein => 1);  
                 } else {  
                     $self->PutE(Identifier => $id, source => $source);  
                     $self->PutR(IsSequenceFor => $proteinID, $id);  
                     $self->PutR(IncludesIdentifier => $setID, $id);  
                     $identifiers{$id} = $proteinID;  
361                  }                  }
362                # Create the identifier record.
363                $self->PutE(Identifier => $aliasID, natural_form => $natural,
364                            source => $aliasType);
365                # Is this a protein alias?
366                if ($aliasConf eq 'C' && $proteinID) {
367                    # Yes. Connect it using IsNamedBy.
368                    $self->PutR(IsNamedBy => $proteinID, $aliasID);
369                } else {
370                    # No. Connect it to the feature.
371                    $self->PutR(IsIdentifiedBy => $fid, $aliasID, conf => $aliasConf);
372              }              }
373          }          }
374            # Finally, this feature is an alias of itself.
375            $self->PutE(Identifier => $fid, natural_form => $fid,
376                        source => 'SEED');
377            $self->PutR(IsIdentifiedBy => $fid, $fid, conf => 'A');
378      }      }
379  }  }
380    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.13

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3