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

Annotation of /Sprout/FeatureSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (view) (download) (as text)

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     #
4 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
5 :     # for Interpretations of Genomes. All Rights Reserved.
6 :     #
7 :     # This file is part of the SEED Toolkit.
8 :     #
9 :     # The SEED Toolkit is free software. You can redistribute
10 :     # it and/or modify it under the terms of the SEED Toolkit
11 :     # Public License.
12 :     #
13 :     # You should have received a copy of the SEED Toolkit Public License
14 :     # along with this program; if not write to the University of Chicago
15 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
16 :     # Genomes at veronika@thefig.info or download a copy from
17 :     # http://www.theseed.org/LICENSE.TXT.
18 :     #
19 :    
20 :     package FeatureSaplingLoader;
21 :    
22 :     use strict;
23 :     use Tracer;
24 :     use ERDB;
25 :     use CGI qw(-nosticky);
26 :     use BasicLocation;
27 :     use HyperLink;
28 : parrello 1.2 use AliasAnalysis;
29 : parrello 1.3 use LoaderUtils;
30 : parrello 1.8 use Digest::MD5;
31 : parrello 1.9 use SeedUtils;
32 : parrello 1.1 use base 'BaseSaplingLoader';
33 :    
34 :     =head1 Sapling Feature Load Group Class
35 :    
36 :     =head2 Introduction
37 :    
38 :     The Feature Load Group includes all of the major feature-related tables.
39 :    
40 :     =head3 new
41 :    
42 :     my $sl = FeatureSaplingLoader->new($erdb, $options, @tables);
43 :    
44 :     Construct a new FeatureSaplingLoader object.
45 :    
46 :     =over 4
47 :    
48 :     =item erdb
49 :    
50 : parrello 1.4 L<Sapling> object for the database being loaded.
51 : parrello 1.1
52 :     =item options
53 :    
54 :     Reference to a hash of command-line options.
55 :    
56 :     =item tables
57 :    
58 :     List of tables in this load group.
59 :    
60 :     =back
61 :    
62 :     =cut
63 :    
64 :     sub new {
65 :     # Get the parameters.
66 :     my ($class, $erdb, $options) = @_;
67 :     # Create the table list.
68 : parrello 1.2 my @tables = sort qw(Feature FeatureEssential FeatureEvidence FeatureLink
69 : parrello 1.12 FeatureVirulent IsOwnerOf IsLocatedIn IsIdentifiedBy
70 : parrello 1.3 Identifier IsNamedBy ProteinSequence Concerns
71 : parrello 1.9 IsAttachmentSiteFor Publication IsProteinFor
72 : parrello 1.10 Role RoleIndex IsFunctionalIn);
73 : parrello 1.1 # Create the BaseSaplingLoader object.
74 :     my $retVal = BaseSaplingLoader::new($class, $erdb, $options, @tables);
75 :     # Return it.
76 :     return $retVal;
77 :     }
78 :    
79 :     =head2 Public Methods
80 :    
81 :     =head3 Generate
82 :    
83 :     $sl->Generate();
84 :    
85 :     Generate the data for the feature-related files.
86 :    
87 :     =cut
88 :    
89 :     sub Generate {
90 :     # Get the parameters.
91 :     my ($self) = @_;
92 :     # Get the database object.
93 :     my $erdb = $self->db();
94 : parrello 1.9 # Check for local or global.
95 : parrello 1.1 if (! $self->global()) {
96 : parrello 1.9 # Here we are generating data for a genome.
97 : parrello 1.1 my $genomeID = $self->section();
98 :     # Load this genome's features.
99 :     $self->LoadGenomeFeatures($genomeID);
100 : parrello 1.9 } else {
101 : parrello 1.10 # The global data is the roles from subsystems and the publications.
102 : parrello 1.9 my $fig = $self->source();
103 : parrello 1.10 # 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 : parrello 1.9 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 : parrello 1.10 # 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 : parrello 1.9 }
166 :     }
167 : parrello 1.10 Trace("Publications generated.") if T(2);
168 : parrello 1.1 }
169 :     }
170 :    
171 :     =head3 LoadGenomeFeatures
172 :    
173 :     $sl->LoadGenomeFeatures($genomeID);
174 :    
175 :     Load the feature-related data for a single genome.
176 :    
177 :     =over 4
178 :    
179 :     =item genomeID
180 :    
181 :     ID of the genome whose feature data is to be loaded.
182 :    
183 :     =back
184 :    
185 :     =cut
186 :    
187 :     sub LoadGenomeFeatures {
188 :     # Get the parameters.
189 :     my ($self, $genomeID) = @_;
190 :     # Get the source object.
191 :     my $fig = $self->source();
192 :     # Get the database.
193 :     my $sapling = $self->db();
194 :     # Get the maximum location segment length. We'll need this later.
195 :     my $maxLength = $sapling->TuningParameter('maxLocationLength');
196 : parrello 1.3 # Get the genome's aliases.
197 :     my $aliasDir = $sapling->LoadDirectory() . "/AliasData";
198 :     my $aliasHash = LoaderUtils::ReadAliasFile($aliasDir, $genomeID);
199 :     if (! defined $aliasHash) {
200 : parrello 1.9 Trace("No aliases found for $genomeID.") if T(ERDBLoadGroup => 1);
201 : parrello 1.3 $self->Add(missingAliasFile => 1);
202 :     $aliasHash = {};
203 :     }
204 : parrello 1.1 # Get all of this genome's features.
205 :     my $featureList = $fig->all_features_detailed_fast($genomeID);
206 :     # Loop through them.
207 :     for my $feature (@$featureList) {
208 :     # Get this feature's data.
209 :     my ($fid, $locationString, $aliases, $type, undef, undef, $assignment,
210 :     $assignmentMaker, $quality) = @$feature;
211 :     $self->Track(Features => $fid, 1000);
212 : parrello 1.7 # Fix missing assignments. For RNAs, the assignment may be in the alias list.
213 : parrello 1.1 if (! defined $assignment) {
214 : parrello 1.7 if ($type eq 'rna') {
215 :     $assignment = $aliases;
216 :     $assignmentMaker ||= 'master';
217 :     } else {
218 :     $assignment = '';
219 :     }
220 : parrello 1.1 }
221 :     # Convert the location string to a list of location objects.
222 :     my @locs = map { BasicLocation->new($_) } split /\s*,\s*/, $locationString;
223 :     # Now we need to run through the locations. We'll put the total sequence
224 :     # length in here.
225 :     my $seqLen = 0;
226 :     # This will track the ordinal position of the current location segment.
227 :     my $locN = 1;
228 :     # Loop through the location objects.
229 :     for my $loc (@locs) {
230 :     # Add this location's length to the total length.
231 :     $seqLen += $loc->Length();
232 :     # Extract the contig ID. Note that we need to prefix the
233 :     # genome ID to make it unique.
234 :     my $contig = $loc->Contig();
235 :     my $contigID = "$genomeID:$contig";
236 :     # We also need the location's direction.
237 :     my $dir = $loc->Dir();
238 :     # Now we peel off sections of the location and connect them
239 :     # to the feature.
240 :     my $peel = $loc->Peel($maxLength);
241 :     while (defined $peel) {
242 : parrello 1.2 $self->PutR(IsLocatedIn => $fid, $contigID, ordinal => $locN++,
243 :     begin => $peel->Left(), len => $peel->Length(),
244 :     dir => $dir);
245 : parrello 1.1 $peel = $loc->Peel($maxLength);
246 :     }
247 :     # Output the residual. There will always be one, because of the way
248 :     # Peel works.
249 : parrello 1.2 $self->PutR(IsLocatedIn => $fid, $contigID, ordinal => $locN,
250 :     begin => $loc->Left(), dir => $dir, len => $loc->Length());
251 : parrello 1.1 }
252 : parrello 1.3 # 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 : parrello 1.9 Trace("Invalid attachment function for $fid: $assignment") if T(ERDBLoadGroup => 1);
259 : parrello 1.3 $self->Add(badAttachment => 1);
260 :     }
261 :     }
262 : parrello 1.1 # Emit the feature record.
263 :     $self->PutE(Feature => $fid, feature_type => $type,
264 : parrello 1.2 sequence_length => $seqLen, function => $assignment,
265 :     locked => $fig->is_locked_fid($fid));
266 : parrello 1.1 # Connect the feature to its genome.
267 :     $self->PutR(IsOwnerOf => $genomeID, $fid);
268 : parrello 1.9 # Connect the feature to its roles.
269 : parrello 1.11 my ($roles, $errors) = SeedUtils::roles_for_loading($assignment);
270 : parrello 1.9 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 : parrello 1.1 # Now we have a whole bunch of attribute-related stuff to store in
284 : parrello 1.10 # secondary Feature tables. First is the evidence codes. This is special
285 :     # because we have to save the DLIT numbers.
286 :     my @dlits;
287 : parrello 1.1 my @evidenceTuples = $fig->get_attributes($fid, 'evidence_code');
288 :     for my $evidenceTuple (@evidenceTuples) {
289 :     my (undef, undef, $code) = @$evidenceTuple;
290 :     $self->PutE(FeatureEvidence => $fid, 'evidence-code' => $code);
291 : parrello 1.10 # 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 : parrello 1.1 }
297 :     # Now we have the external links. These are stored using hyperlink objects.
298 :     my @links = $fig->fid_links($fid);
299 :     for my $link (@links) {
300 :     my $hl = HyperLink->newFromHtml($link);
301 :     $self->PutE(FeatureLink => $fid, link => $hl);
302 :     }
303 :     # Virulence data is next. This is also hyperlink data.
304 :     my @virulenceTuples = $fig->get_attributes($fid, 'virulence_associated%');
305 :     for my $virulenceTuple (@virulenceTuples) {
306 :     my (undef, undef, $text, $url) = @$virulenceTuple;
307 :     my $hl = HyperLink->new($text, $url);
308 :     $self->PutE(FeatureVirulent => $fid, virulent => $hl);
309 :     }
310 :     # Finally, the essentiality stuff, which is the last of the hyperlinks.
311 :     my @essentials = $fig->get_attributes($fid, undef, ['essential', 'potential-essential']);
312 :     for my $essentialTuple (@essentials) {
313 :     my (undef, undef, $essentialityType, $url) = @$essentialTuple;
314 : parrello 1.5 # 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.
318 :     my $link = HyperLink->new($essentialityType, $url);
319 :     # Store it as essentiality data for this feature.
320 :     $self->PutE(FeatureEssential => $fid, essential => $link);
321 :     }
322 : parrello 1.1 }
323 :     # If this is a PEG, we have a protein sequence.
324 : parrello 1.3 my $proteinID;
325 : parrello 1.1 if ($type eq 'peg') {
326 :     # Get the translation.
327 :     my $proteinSequence = $fig->get_translation($fid);
328 : parrello 1.7 if (! $proteinSequence) {
329 : parrello 1.13 Trace("No protein sequence found for $fid.") if T(ERDBLoadGroup => 2);
330 : parrello 1.7 $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 : parrello 1.1 # Compute the ID.
337 : parrello 1.8 $proteinID = $sapling->ProteinID($proteinSequence);
338 : parrello 1.1 # Create the protein record.
339 :     $self->PutE(ProteinSequence => $proteinID, sequence => $proteinSequence);
340 : parrello 1.3 $self->PutR(IsProteinFor => $proteinID, $fid);
341 : parrello 1.10 # Connect this protein to the feature's publications (if any).
342 :     for my $pub (@dlits) {
343 :     $self->PutR(Concerns => $pub, $proteinID);
344 : parrello 1.1 }
345 : parrello 1.2 }
346 : parrello 1.3 # Now we need to compute the identifiers. We start with the aliases.
347 :     # Get the alias data for this feature. If there is none, we force an
348 :     # empty list.
349 :     my $aliasList = $aliasHash->{$fid} || [];
350 :     # Loop through the aliases found.
351 :     for my $aliasTuple (@$aliasList) {
352 :     my ($aliasID, $aliasType, $aliasConf) = @$aliasTuple;
353 :     # Get the natural form. If there is none, then the canonical
354 : parrello 1.13 # form IS the natural form. Note we have to make a special check
355 :     # for locus tags, which have an insane number of variants.
356 :     my $natural;
357 :     if ($aliasID =~ /LocusTag:(.+)/) {
358 :     $natural = $1;
359 :     } else {
360 :     $natural = AliasAnalysis::Type($aliasType => $aliasID) || $aliasID;
361 :     }
362 : parrello 1.3 # Create the identifier record.
363 :     $self->PutE(Identifier => $aliasID, natural_form => $natural,
364 : parrello 1.5 source => $aliasType);
365 : parrello 1.3 # Is this a protein alias?
366 :     if ($aliasConf eq 'C' && $proteinID) {
367 :     # Yes. Connect it using IsNamedBy.
368 :     $self->PutR(IsNamedBy => $proteinID, $aliasID);
369 : parrello 1.2 } else {
370 : parrello 1.3 # No. Connect it to the feature.
371 : parrello 1.12 $self->PutR(IsIdentifiedBy => $fid, $aliasID, conf => $aliasConf);
372 : parrello 1.2 }
373 :     }
374 : parrello 1.3 # Finally, this feature is an alias of itself.
375 :     $self->PutE(Identifier => $fid, natural_form => $fid,
376 :     source => 'SEED');
377 : parrello 1.12 $self->PutR(IsIdentifiedBy => $fid, $fid, conf => 'A');
378 : parrello 1.1 }
379 :     }
380 :    
381 :    
382 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3