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

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3