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

Annotation of /Sprout/FeatureSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (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 :     FeatureVirulent IsOwnerOf IsLocatedIn Identifies
70 : parrello 1.3 Identifier IsNamedBy ProteinSequence Concerns
71 : parrello 1.9 IsAttachmentSiteFor Publication IsProteinFor
72 :     Role 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 :     # The global data is the roles from subsystems.
102 :     my $fig = $self->source();
103 :     # First, we get the subsystem list.
104 :     my $subHash = $erdb->SubsystemHash();
105 :     for my $sub (sort keys %$subHash) {
106 :     $self->Add(subsystems => 1);
107 :     Trace("Processing roles for $sub.") if T(3);
108 :     # Get this subsystem's roles and write them out.
109 :     my @roles = $fig->subsystem_to_roles($sub);
110 :     for my $role (@roles) {
111 :     $self->Add(subsystemRoles => 1);
112 :     $self->PutE(Role => $role, hypothetical => hypo($role));
113 :     }
114 :     }
115 :     Trace("Subsystem roles generated.") if T(3);
116 : parrello 1.1 }
117 :     }
118 :    
119 :     =head3 LoadGenomeFeatures
120 :    
121 :     $sl->LoadGenomeFeatures($genomeID);
122 :    
123 :     Load the feature-related data for a single genome.
124 :    
125 :     =over 4
126 :    
127 :     =item genomeID
128 :    
129 :     ID of the genome whose feature data is to be loaded.
130 :    
131 :     =back
132 :    
133 :     =cut
134 :    
135 :     sub LoadGenomeFeatures {
136 :     # Get the parameters.
137 :     my ($self, $genomeID) = @_;
138 :     # Get the source object.
139 :     my $fig = $self->source();
140 :     # Get the database.
141 :     my $sapling = $self->db();
142 :     # Get the maximum location segment length. We'll need this later.
143 :     my $maxLength = $sapling->TuningParameter('maxLocationLength');
144 : parrello 1.3 # Get the genome's aliases.
145 :     my $aliasDir = $sapling->LoadDirectory() . "/AliasData";
146 :     my $aliasHash = LoaderUtils::ReadAliasFile($aliasDir, $genomeID);
147 :     if (! defined $aliasHash) {
148 : parrello 1.9 Trace("No aliases found for $genomeID.") if T(ERDBLoadGroup => 1);
149 : parrello 1.3 $self->Add(missingAliasFile => 1);
150 :     $aliasHash = {};
151 :     }
152 : parrello 1.1 # Get all of this genome's features.
153 :     my $featureList = $fig->all_features_detailed_fast($genomeID);
154 :     # Loop through them.
155 :     for my $feature (@$featureList) {
156 :     # Get this feature's data.
157 :     my ($fid, $locationString, $aliases, $type, undef, undef, $assignment,
158 :     $assignmentMaker, $quality) = @$feature;
159 :     $self->Track(Features => $fid, 1000);
160 : parrello 1.7 # Fix missing assignments. For RNAs, the assignment may be in the alias list.
161 : parrello 1.1 if (! defined $assignment) {
162 : parrello 1.7 if ($type eq 'rna') {
163 :     $assignment = $aliases;
164 :     $assignmentMaker ||= 'master';
165 :     } else {
166 :     $assignment = '';
167 :     }
168 : parrello 1.1 }
169 :     # Convert the location string to a list of location objects.
170 :     my @locs = map { BasicLocation->new($_) } split /\s*,\s*/, $locationString;
171 :     # Now we need to run through the locations. We'll put the total sequence
172 :     # length in here.
173 :     my $seqLen = 0;
174 :     # This will track the ordinal position of the current location segment.
175 :     my $locN = 1;
176 :     # Loop through the location objects.
177 :     for my $loc (@locs) {
178 :     # Add this location's length to the total length.
179 :     $seqLen += $loc->Length();
180 :     # Extract the contig ID. Note that we need to prefix the
181 :     # genome ID to make it unique.
182 :     my $contig = $loc->Contig();
183 :     my $contigID = "$genomeID:$contig";
184 :     # We also need the location's direction.
185 :     my $dir = $loc->Dir();
186 :     # Now we peel off sections of the location and connect them
187 :     # to the feature.
188 :     my $peel = $loc->Peel($maxLength);
189 :     while (defined $peel) {
190 : parrello 1.2 $self->PutR(IsLocatedIn => $fid, $contigID, ordinal => $locN++,
191 :     begin => $peel->Left(), len => $peel->Length(),
192 :     dir => $dir);
193 : parrello 1.1 $peel = $loc->Peel($maxLength);
194 :     }
195 :     # Output the residual. There will always be one, because of the way
196 :     # Peel works.
197 : parrello 1.2 $self->PutR(IsLocatedIn => $fid, $contigID, ordinal => $locN,
198 :     begin => $loc->Left(), dir => $dir, len => $loc->Length());
199 : parrello 1.1 }
200 : parrello 1.3 # Is this an attachment site?
201 :     if ($type eq 'att') {
202 :     # Yes, connect it to the attached feature.
203 :     if ($assignment =~ /att([LR])\s+for\s+(fig\|.+)/) {
204 :     $self->PutR(IsAttachmentSiteFor => $fid, $2, edge => $1);
205 :     } else {
206 : parrello 1.9 Trace("Invalid attachment function for $fid: $assignment") if T(ERDBLoadGroup => 1);
207 : parrello 1.3 $self->Add(badAttachment => 1);
208 :     }
209 :     }
210 : parrello 1.1 # Emit the feature record.
211 :     $self->PutE(Feature => $fid, feature_type => $type,
212 : parrello 1.2 sequence_length => $seqLen, function => $assignment,
213 :     locked => $fig->is_locked_fid($fid));
214 : parrello 1.1 # Connect the feature to its genome.
215 :     $self->PutR(IsOwnerOf => $genomeID, $fid);
216 : parrello 1.9 # Connect the feature to its roles.
217 :     my ($roles, $errors) = LoaderUtils::RolesForLoading($assignment);
218 :     if (! defined $roles) {
219 :     # Here the functional assignment was suspicious.
220 :     $self->Add(suspiciousFunction => 1);
221 :     Trace("$fid has a suspicious function: $assignment") if T(ERDBLoadGroup => 1);
222 :     } else {
223 :     # Here we have a good assignment.
224 :     for my $role (@$roles) {
225 :     $self->Add(featureRole => 1);
226 :     $self->PutR(IsFunctionalIn => $role, $fid);
227 :     $self->PutE(Role => $role, hypothetical => hypo($role));
228 :     }
229 :     $self->Add(badFeatureRoles => $errors);
230 :     }
231 : parrello 1.1 # Now we have a whole bunch of attribute-related stuff to store in
232 :     # secondary Feature tables. First is the evidence codes.
233 :     my @evidenceTuples = $fig->get_attributes($fid, 'evidence_code');
234 :     for my $evidenceTuple (@evidenceTuples) {
235 :     my (undef, undef, $code) = @$evidenceTuple;
236 :     $self->PutE(FeatureEvidence => $fid, 'evidence-code' => $code);
237 :     }
238 :     # Now we have the external links. These are stored using hyperlink objects.
239 :     my @links = $fig->fid_links($fid);
240 :     for my $link (@links) {
241 :     my $hl = HyperLink->newFromHtml($link);
242 :     $self->PutE(FeatureLink => $fid, link => $hl);
243 :     }
244 :     # Virulence data is next. This is also hyperlink data.
245 :     my @virulenceTuples = $fig->get_attributes($fid, 'virulence_associated%');
246 :     for my $virulenceTuple (@virulenceTuples) {
247 :     my (undef, undef, $text, $url) = @$virulenceTuple;
248 :     my $hl = HyperLink->new($text, $url);
249 :     $self->PutE(FeatureVirulent => $fid, virulent => $hl);
250 :     }
251 :     # Finally, the essentiality stuff, which is the last of the hyperlinks.
252 :     my @essentials = $fig->get_attributes($fid, undef, ['essential', 'potential-essential']);
253 :     for my $essentialTuple (@essentials) {
254 :     my (undef, undef, $essentialityType, $url) = @$essentialTuple;
255 : parrello 1.5 # Only keep this datum if it has a URL. The ones without URLs are
256 :     # all duplicates.
257 :     if ($url) {
258 :     # Form a hyperlink from this essentiality tuple.
259 :     my $link = HyperLink->new($essentialityType, $url);
260 :     # Store it as essentiality data for this feature.
261 :     $self->PutE(FeatureEssential => $fid, essential => $link);
262 :     }
263 : parrello 1.1 }
264 :     # If this is a PEG, we have a protein sequence.
265 : parrello 1.3 my $proteinID;
266 : parrello 1.1 if ($type eq 'peg') {
267 :     # Get the translation.
268 :     my $proteinSequence = $fig->get_translation($fid);
269 : parrello 1.7 if (! $proteinSequence) {
270 :     Trace("No protein sequence found for $fid.") if T(2);
271 :     $self->Add(missingProtein => 1);
272 :     # Here there was some sort of error and the protein sequence did
273 :     # not come back. Ask for the DNA and translate it instead.
274 :     my $dna = $fig->get_dna_seq($fid);
275 :     $proteinSequence = FIG::translate($dna, undef, 1);
276 :     }
277 : parrello 1.1 # Compute the ID.
278 : parrello 1.8 $proteinID = $sapling->ProteinID($proteinSequence);
279 : parrello 1.1 # Create the protein record.
280 :     $self->PutE(ProteinSequence => $proteinID, sequence => $proteinSequence);
281 : parrello 1.3 $self->PutR(IsProteinFor => $proteinID, $fid);
282 : parrello 1.1 # Get the publications for this PEG.
283 :     my @pubs = $fig->get_attributes($fid, 'PUBMED_CURATED_RELEVANT');
284 :     for my $pub (@pubs) {
285 :     # Parse out the article title from the data.
286 : parrello 1.3 my (undef, undef, $data, $url) = @$pub;
287 : parrello 1.1 my @pieces = split /,/, $data, 3;
288 :     if (defined $pieces[2]) {
289 :     # Create the publication record.
290 : parrello 1.3 my $hl = HyperLink->new($pieces[2], $url);
291 : parrello 1.1 my $key = ERDB::DigestKey($url);
292 :     $self->PutE(Publication => $key, citation => $hl);
293 :     # Connect it to the protein.
294 :     $self->PutR(Concerns => $key, $proteinID);
295 :     }
296 :     }
297 : parrello 1.2 }
298 : parrello 1.3 # Now we need to compute the identifiers. We start with the aliases.
299 :     # Get the alias data for this feature. If there is none, we force an
300 :     # empty list.
301 :     my $aliasList = $aliasHash->{$fid} || [];
302 :     # Loop through the aliases found.
303 :     for my $aliasTuple (@$aliasList) {
304 :     my ($aliasID, $aliasType, $aliasConf) = @$aliasTuple;
305 :     # Get the natural form. If there is none, then the canonical
306 :     # form IS the natural form.
307 :     my $natural = AliasAnalysis::Type($aliasType => $aliasID) || $aliasID;
308 :     # Create the identifier record.
309 :     $self->PutE(Identifier => $aliasID, natural_form => $natural,
310 : parrello 1.5 source => $aliasType);
311 : parrello 1.3 # Is this a protein alias?
312 :     if ($aliasConf eq 'C' && $proteinID) {
313 :     # Yes. Connect it using IsNamedBy.
314 :     $self->PutR(IsNamedBy => $proteinID, $aliasID);
315 : parrello 1.2 } else {
316 : parrello 1.3 # No. Connect it to the feature.
317 :     $self->PutR(Identifies => $aliasID, $fid, conf => $aliasConf);
318 : parrello 1.2 }
319 :     }
320 : parrello 1.3 # Finally, this feature is an alias of itself.
321 :     $self->PutE(Identifier => $fid, natural_form => $fid,
322 :     source => 'SEED');
323 :     $self->PutR(Identifies => $fid, $fid, conf => 'A');
324 : parrello 1.1 }
325 :     }
326 :    
327 :    
328 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3