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

Annotation of /Sprout/FeatureSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (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.9 use SeedUtils;
31 : parrello 1.14 use gjoseqlib;
32 : parrello 1.15 use MD5Computer;
33 : parrello 1.1 use base 'BaseSaplingLoader';
34 :    
35 :     =head1 Sapling Feature Load Group Class
36 :    
37 :     =head2 Introduction
38 :    
39 :     The Feature Load Group includes all of the major feature-related tables.
40 :    
41 :     =head3 new
42 :    
43 :     my $sl = FeatureSaplingLoader->new($erdb, $options, @tables);
44 :    
45 :     Construct a new FeatureSaplingLoader object.
46 :    
47 :     =over 4
48 :    
49 :     =item erdb
50 :    
51 : parrello 1.4 L<Sapling> object for the database being loaded.
52 : parrello 1.1
53 :     =item options
54 :    
55 :     Reference to a hash of command-line options.
56 :    
57 :     =item tables
58 :    
59 :     List of tables in this load group.
60 :    
61 :     =back
62 :    
63 :     =cut
64 :    
65 :     sub new {
66 :     # Get the parameters.
67 :     my ($class, $erdb, $options) = @_;
68 :     # Create the table list.
69 : parrello 1.2 my @tables = sort qw(Feature FeatureEssential FeatureEvidence FeatureLink
70 : parrello 1.12 FeatureVirulent IsOwnerOf IsLocatedIn IsIdentifiedBy
71 : parrello 1.3 Identifier IsNamedBy ProteinSequence Concerns
72 : parrello 1.9 IsAttachmentSiteFor Publication IsProteinFor
73 : parrello 1.10 Role RoleIndex IsFunctionalIn);
74 : parrello 1.1 # Create the BaseSaplingLoader object.
75 :     my $retVal = BaseSaplingLoader::new($class, $erdb, $options, @tables);
76 :     # Return it.
77 :     return $retVal;
78 :     }
79 :    
80 :     =head2 Public Methods
81 :    
82 :     =head3 Generate
83 :    
84 :     $sl->Generate();
85 :    
86 :     Generate the data for the feature-related files.
87 :    
88 :     =cut
89 :    
90 :     sub Generate {
91 :     # Get the parameters.
92 :     my ($self) = @_;
93 :     # Get the database object.
94 :     my $erdb = $self->db();
95 : parrello 1.9 # Check for local or global.
96 : parrello 1.1 if (! $self->global()) {
97 : parrello 1.9 # Here we are generating data for a genome.
98 : parrello 1.1 my $genomeID = $self->section();
99 :     # Load this genome's features.
100 :     $self->LoadGenomeFeatures($genomeID);
101 : parrello 1.9 } else {
102 : parrello 1.10 # The global data is the roles from subsystems and the publications.
103 : parrello 1.9 my $fig = $self->source();
104 : parrello 1.10 # 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 : parrello 1.9 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 : parrello 1.10 # 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 : parrello 1.9 }
167 :     }
168 : parrello 1.10 Trace("Publications generated.") if T(2);
169 : parrello 1.1 }
170 :     }
171 :    
172 :     =head3 LoadGenomeFeatures
173 :    
174 :     $sl->LoadGenomeFeatures($genomeID);
175 :    
176 :     Load the feature-related data for a single genome.
177 :    
178 :     =over 4
179 :    
180 :     =item genomeID
181 :    
182 :     ID of the genome whose feature data is to be loaded.
183 :    
184 :     =back
185 :    
186 :     =cut
187 :    
188 :     sub LoadGenomeFeatures {
189 :     # Get the parameters.
190 :     my ($self, $genomeID) = @_;
191 :     # Get the source object.
192 :     my $fig = $self->source();
193 :     # Get the database.
194 :     my $sapling = $self->db();
195 :     # Get the maximum location segment length. We'll need this later.
196 :     my $maxLength = $sapling->TuningParameter('maxLocationLength');
197 : parrello 1.3 # Get the genome's aliases.
198 :     my $aliasDir = $sapling->LoadDirectory() . "/AliasData";
199 :     my $aliasHash = LoaderUtils::ReadAliasFile($aliasDir, $genomeID);
200 :     if (! defined $aliasHash) {
201 : parrello 1.9 Trace("No aliases found for $genomeID.") if T(ERDBLoadGroup => 1);
202 : parrello 1.3 $self->Add(missingAliasFile => 1);
203 :     $aliasHash = {};
204 :     }
205 : parrello 1.14 # 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 : parrello 1.1 # Get all of this genome's features.
208 :     my $featureList = $fig->all_features_detailed_fast($genomeID);
209 : parrello 1.15 # Compute the MD5 identifiers for the genome and its contigs.
210 :     my $genomeMD5Data = MD5Computer->new_from_fasta("$FIG_Config::organisms/$genomeID/contigs");
211 : parrello 1.1 # Loop through them.
212 :     for my $feature (@$featureList) {
213 :     # Get this feature's data.
214 :     my ($fid, $locationString, $aliases, $type, undef, undef, $assignment,
215 :     $assignmentMaker, $quality) = @$feature;
216 :     $self->Track(Features => $fid, 1000);
217 : parrello 1.7 # Fix missing assignments. For RNAs, the assignment may be in the alias list.
218 : parrello 1.1 if (! defined $assignment) {
219 : parrello 1.7 if ($type eq 'rna') {
220 :     $assignment = $aliases;
221 :     $assignmentMaker ||= 'master';
222 :     } else {
223 :     $assignment = '';
224 :     }
225 : parrello 1.1 }
226 :     # Convert the location string to a list of location objects.
227 :     my @locs = map { BasicLocation->new($_) } split /\s*,\s*/, $locationString;
228 :     # Now we need to run through the locations. We'll put the total sequence
229 :     # length in here.
230 :     my $seqLen = 0;
231 :     # This will track the ordinal position of the current location segment.
232 :     my $locN = 1;
233 :     # Loop through the location objects.
234 :     for my $loc (@locs) {
235 :     # Add this location's length to the total length.
236 :     $seqLen += $loc->Length();
237 :     # Extract the contig ID. Note that we need to prefix the
238 :     # genome ID to make it unique.
239 :     my $contig = $loc->Contig();
240 :     my $contigID = "$genomeID:$contig";
241 :     # We also need the location's direction.
242 :     my $dir = $loc->Dir();
243 :     # Now we peel off sections of the location and connect them
244 :     # to the feature.
245 :     my $peel = $loc->Peel($maxLength);
246 :     while (defined $peel) {
247 : parrello 1.2 $self->PutR(IsLocatedIn => $fid, $contigID, ordinal => $locN++,
248 :     begin => $peel->Left(), len => $peel->Length(),
249 :     dir => $dir);
250 : parrello 1.1 $peel = $loc->Peel($maxLength);
251 :     }
252 :     # Output the residual. There will always be one, because of the way
253 :     # Peel works.
254 : parrello 1.2 $self->PutR(IsLocatedIn => $fid, $contigID, ordinal => $locN,
255 :     begin => $loc->Left(), dir => $dir, len => $loc->Length());
256 : parrello 1.1 }
257 : parrello 1.3 # Is this an attachment site?
258 :     if ($type eq 'att') {
259 :     # Yes, connect it to the attached feature.
260 :     if ($assignment =~ /att([LR])\s+for\s+(fig\|.+)/) {
261 :     $self->PutR(IsAttachmentSiteFor => $fid, $2, edge => $1);
262 :     } else {
263 : parrello 1.9 Trace("Invalid attachment function for $fid: $assignment") if T(ERDBLoadGroup => 1);
264 : parrello 1.3 $self->Add(badAttachment => 1);
265 :     }
266 :     }
267 : parrello 1.15 # Compute the MD5 identifier.
268 :     my $md5Alias = $genomeMD5Data->ComputeGeneMD5(map { $_->String() } @locs);
269 : parrello 1.1 # Emit the feature record.
270 :     $self->PutE(Feature => $fid, feature_type => $type,
271 : parrello 1.2 sequence_length => $seqLen, function => $assignment,
272 :     locked => $fig->is_locked_fid($fid));
273 : parrello 1.1 # Connect the feature to its genome.
274 :     $self->PutR(IsOwnerOf => $genomeID, $fid);
275 : parrello 1.9 # Connect the feature to its roles.
276 : parrello 1.11 my ($roles, $errors) = SeedUtils::roles_for_loading($assignment);
277 : parrello 1.9 if (! defined $roles) {
278 :     # Here the functional assignment was suspicious.
279 :     $self->Add(suspiciousFunction => 1);
280 :     Trace("$fid has a suspicious function: $assignment") if T(ERDBLoadGroup => 1);
281 :     } else {
282 :     # Here we have a good assignment.
283 :     for my $role (@$roles) {
284 :     $self->Add(featureRole => 1);
285 :     $self->PutR(IsFunctionalIn => $role, $fid);
286 :     $self->PutE(Role => $role, hypothetical => hypo($role));
287 :     }
288 :     $self->Add(badFeatureRoles => $errors);
289 :     }
290 : parrello 1.1 # Now we have a whole bunch of attribute-related stuff to store in
291 : parrello 1.10 # secondary Feature tables. First is the evidence codes. This is special
292 :     # because we have to save the DLIT numbers.
293 :     my @dlits;
294 : parrello 1.1 my @evidenceTuples = $fig->get_attributes($fid, 'evidence_code');
295 :     for my $evidenceTuple (@evidenceTuples) {
296 :     my (undef, undef, $code) = @$evidenceTuple;
297 :     $self->PutE(FeatureEvidence => $fid, 'evidence-code' => $code);
298 : parrello 1.10 # If this is a direct literature reference, save it.
299 :     if ($code =~ /dlit\((\d+)/) {
300 :     push @dlits, $1;
301 :     $self->Add(dlits => 1);
302 :     }
303 : parrello 1.1 }
304 :     # Now we have the external links. These are stored using hyperlink objects.
305 :     my @links = $fig->fid_links($fid);
306 :     for my $link (@links) {
307 :     my $hl = HyperLink->newFromHtml($link);
308 :     $self->PutE(FeatureLink => $fid, link => $hl);
309 :     }
310 :     # Virulence data is next. This is also hyperlink data.
311 :     my @virulenceTuples = $fig->get_attributes($fid, 'virulence_associated%');
312 :     for my $virulenceTuple (@virulenceTuples) {
313 :     my (undef, undef, $text, $url) = @$virulenceTuple;
314 :     my $hl = HyperLink->new($text, $url);
315 :     $self->PutE(FeatureVirulent => $fid, virulent => $hl);
316 :     }
317 :     # Finally, the essentiality stuff, which is the last of the hyperlinks.
318 :     my @essentials = $fig->get_attributes($fid, undef, ['essential', 'potential-essential']);
319 :     for my $essentialTuple (@essentials) {
320 :     my (undef, undef, $essentialityType, $url) = @$essentialTuple;
321 : parrello 1.5 # Only keep this datum if it has a URL. The ones without URLs are
322 :     # all duplicates.
323 :     if ($url) {
324 :     # Form a hyperlink from this essentiality tuple.
325 :     my $link = HyperLink->new($essentialityType, $url);
326 :     # Store it as essentiality data for this feature.
327 :     $self->PutE(FeatureEssential => $fid, essential => $link);
328 :     }
329 : parrello 1.1 }
330 :     # If this is a PEG, we have a protein sequence.
331 : parrello 1.3 my $proteinID;
332 : parrello 1.1 if ($type eq 'peg') {
333 :     # Get the translation.
334 : parrello 1.14 my $proteinSequence = $seqs{$fid};
335 : parrello 1.7 if (! $proteinSequence) {
336 : parrello 1.13 Trace("No protein sequence found for $fid.") if T(ERDBLoadGroup => 2);
337 : parrello 1.7 $self->Add(missingProtein => 1);
338 :     # Here there was some sort of error and the protein sequence did
339 :     # not come back. Ask for the DNA and translate it instead.
340 :     my $dna = $fig->get_dna_seq($fid);
341 :     $proteinSequence = FIG::translate($dna, undef, 1);
342 :     }
343 : parrello 1.1 # Compute the ID.
344 : parrello 1.8 $proteinID = $sapling->ProteinID($proteinSequence);
345 : parrello 1.1 # Create the protein record.
346 :     $self->PutE(ProteinSequence => $proteinID, sequence => $proteinSequence);
347 : parrello 1.3 $self->PutR(IsProteinFor => $proteinID, $fid);
348 : parrello 1.10 # Connect this protein to the feature's publications (if any).
349 :     for my $pub (@dlits) {
350 :     $self->PutR(Concerns => $pub, $proteinID);
351 : parrello 1.1 }
352 : parrello 1.2 }
353 : parrello 1.3 # Now we need to compute the identifiers. We start with the aliases.
354 :     # Get the alias data for this feature. If there is none, we force an
355 :     # empty list.
356 :     my $aliasList = $aliasHash->{$fid} || [];
357 :     # Loop through the aliases found.
358 :     for my $aliasTuple (@$aliasList) {
359 :     my ($aliasID, $aliasType, $aliasConf) = @$aliasTuple;
360 :     # Get the natural form. If there is none, then the canonical
361 : parrello 1.13 # form IS the natural form. Note we have to make a special check
362 :     # for locus tags, which have an insane number of variants.
363 :     my $natural;
364 :     if ($aliasID =~ /LocusTag:(.+)/) {
365 :     $natural = $1;
366 :     } else {
367 :     $natural = AliasAnalysis::Type($aliasType => $aliasID) || $aliasID;
368 :     }
369 : parrello 1.3 # Create the identifier record.
370 :     $self->PutE(Identifier => $aliasID, natural_form => $natural,
371 : parrello 1.5 source => $aliasType);
372 : parrello 1.3 # Is this a protein alias?
373 :     if ($aliasConf eq 'C' && $proteinID) {
374 :     # Yes. Connect it using IsNamedBy.
375 :     $self->PutR(IsNamedBy => $proteinID, $aliasID);
376 : parrello 1.2 } else {
377 : parrello 1.3 # No. Connect it to the feature.
378 : parrello 1.12 $self->PutR(IsIdentifiedBy => $fid, $aliasID, conf => $aliasConf);
379 : parrello 1.2 }
380 :     }
381 : parrello 1.15 # Make the MD5 identifier an alias.
382 :     $self->PutE(Identifier => "md5g|$md5Alias", natural_form => $md5Alias,
383 :     source => 'MD5');
384 :     $self->PutR(IsIdentifiedBy => $fid, "md5g|$md5Alias", conf => 'A');
385 : parrello 1.3 # Finally, this feature is an alias of itself.
386 :     $self->PutE(Identifier => $fid, natural_form => $fid,
387 :     source => 'SEED');
388 : parrello 1.12 $self->PutR(IsIdentifiedBy => $fid, $fid, conf => 'A');
389 : parrello 1.1 }
390 :     }
391 :    
392 :    
393 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3