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

Annotation of /Sprout/FeatureSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (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.1 use base 'BaseSaplingLoader';
31 :    
32 :     =head1 Sapling Feature Load Group Class
33 :    
34 :     =head2 Introduction
35 :    
36 :     The Feature Load Group includes all of the major feature-related tables.
37 :    
38 :     =head3 new
39 :    
40 :     my $sl = FeatureSaplingLoader->new($erdb, $options, @tables);
41 :    
42 :     Construct a new FeatureSaplingLoader object.
43 :    
44 :     =over 4
45 :    
46 :     =item erdb
47 :    
48 : parrello 1.4 L<Sapling> object for the database being loaded.
49 : parrello 1.1
50 :     =item options
51 :    
52 :     Reference to a hash of command-line options.
53 :    
54 :     =item tables
55 :    
56 :     List of tables in this load group.
57 :    
58 :     =back
59 :    
60 :     =cut
61 :    
62 :     sub new {
63 :     # Get the parameters.
64 :     my ($class, $erdb, $options) = @_;
65 :     # Create the table list.
66 : parrello 1.2 my @tables = sort qw(Feature FeatureEssential FeatureEvidence FeatureLink
67 :     FeatureVirulent IsOwnerOf IsLocatedIn Identifies
68 : parrello 1.3 Identifier IsNamedBy ProteinSequence Concerns
69 :     IsAttachmentSiteFor Publication IsProteinFor);
70 : parrello 1.1 # Create the BaseSaplingLoader object.
71 :     my $retVal = BaseSaplingLoader::new($class, $erdb, $options, @tables);
72 :     # Return it.
73 :     return $retVal;
74 :     }
75 :    
76 :     =head2 Public Methods
77 :    
78 :     =head3 Generate
79 :    
80 :     $sl->Generate();
81 :    
82 :     Generate the data for the feature-related files.
83 :    
84 :     =cut
85 :    
86 :     sub Generate {
87 :     # Get the parameters.
88 :     my ($self) = @_;
89 :     # Get the database object.
90 :     my $erdb = $self->db();
91 :     # Only proceed if this is a normal section. There's no global feature data.
92 :     if (! $self->global()) {
93 :     # Get the section ID.
94 :     my $genomeID = $self->section();
95 :     # Load this genome's features.
96 :     $self->LoadGenomeFeatures($genomeID);
97 :     }
98 :     }
99 :    
100 :     =head3 LoadGenomeFeatures
101 :    
102 :     $sl->LoadGenomeFeatures($genomeID);
103 :    
104 :     Load the feature-related data for a single genome.
105 :    
106 :     =over 4
107 :    
108 :     =item genomeID
109 :    
110 :     ID of the genome whose feature data is to be loaded.
111 :    
112 :     =back
113 :    
114 :     =cut
115 :    
116 :     sub LoadGenomeFeatures {
117 :     # Get the parameters.
118 :     my ($self, $genomeID) = @_;
119 :     # Get the source object.
120 :     my $fig = $self->source();
121 :     # Get the database.
122 :     my $sapling = $self->db();
123 :     # Get the maximum location segment length. We'll need this later.
124 :     my $maxLength = $sapling->TuningParameter('maxLocationLength');
125 : parrello 1.3 # Get the genome's aliases.
126 :     my $aliasDir = $sapling->LoadDirectory() . "/AliasData";
127 :     my $aliasHash = LoaderUtils::ReadAliasFile($aliasDir, $genomeID);
128 :     if (! defined $aliasHash) {
129 :     Trace("No aliases found for $genomeID.") if T(1);
130 :     $self->Add(missingAliasFile => 1);
131 :     $aliasHash = {};
132 :     }
133 : parrello 1.1 # Get all of this genome's features.
134 :     my $featureList = $fig->all_features_detailed_fast($genomeID);
135 :     # Loop through them.
136 :     for my $feature (@$featureList) {
137 :     # Get this feature's data.
138 :     my ($fid, $locationString, $aliases, $type, undef, undef, $assignment,
139 :     $assignmentMaker, $quality) = @$feature;
140 :     $self->Track(Features => $fid, 1000);
141 : parrello 1.7 # Fix missing assignments. For RNAs, the assignment may be in the alias list.
142 : parrello 1.1 if (! defined $assignment) {
143 : parrello 1.7 if ($type eq 'rna') {
144 :     $assignment = $aliases;
145 :     $assignmentMaker ||= 'master';
146 :     } else {
147 :     $assignment = '';
148 :     }
149 : parrello 1.1 }
150 :     # Convert the location string to a list of location objects.
151 :     my @locs = map { BasicLocation->new($_) } split /\s*,\s*/, $locationString;
152 :     # Now we need to run through the locations. We'll put the total sequence
153 :     # length in here.
154 :     my $seqLen = 0;
155 :     # This will track the ordinal position of the current location segment.
156 :     my $locN = 1;
157 :     # Loop through the location objects.
158 :     for my $loc (@locs) {
159 :     # Add this location's length to the total length.
160 :     $seqLen += $loc->Length();
161 :     # Extract the contig ID. Note that we need to prefix the
162 :     # genome ID to make it unique.
163 :     my $contig = $loc->Contig();
164 :     my $contigID = "$genomeID:$contig";
165 :     # We also need the location's direction.
166 :     my $dir = $loc->Dir();
167 :     # Now we peel off sections of the location and connect them
168 :     # to the feature.
169 :     my $peel = $loc->Peel($maxLength);
170 :     while (defined $peel) {
171 : parrello 1.2 $self->PutR(IsLocatedIn => $fid, $contigID, ordinal => $locN++,
172 :     begin => $peel->Left(), len => $peel->Length(),
173 :     dir => $dir);
174 : parrello 1.1 $peel = $loc->Peel($maxLength);
175 :     }
176 :     # Output the residual. There will always be one, because of the way
177 :     # Peel works.
178 : parrello 1.2 $self->PutR(IsLocatedIn => $fid, $contigID, ordinal => $locN,
179 :     begin => $loc->Left(), dir => $dir, len => $loc->Length());
180 : parrello 1.1 }
181 : parrello 1.3 # Is this an attachment site?
182 :     if ($type eq 'att') {
183 :     # Yes, connect it to the attached feature.
184 :     if ($assignment =~ /att([LR])\s+for\s+(fig\|.+)/) {
185 :     $self->PutR(IsAttachmentSiteFor => $fid, $2, edge => $1);
186 :     } else {
187 :     Trace("Invalid attachment function for $fid: $assignment") if T(1);
188 :     $self->Add(badAttachment => 1);
189 :     }
190 :     }
191 : parrello 1.1 # Emit the feature record.
192 :     $self->PutE(Feature => $fid, feature_type => $type,
193 : parrello 1.2 sequence_length => $seqLen, function => $assignment,
194 :     locked => $fig->is_locked_fid($fid));
195 : parrello 1.1 # Connect the feature to its genome.
196 :     $self->PutR(IsOwnerOf => $genomeID, $fid);
197 :     # Now we have a whole bunch of attribute-related stuff to store in
198 :     # secondary Feature tables. First is the evidence codes.
199 :     my @evidenceTuples = $fig->get_attributes($fid, 'evidence_code');
200 :     for my $evidenceTuple (@evidenceTuples) {
201 :     my (undef, undef, $code) = @$evidenceTuple;
202 :     $self->PutE(FeatureEvidence => $fid, 'evidence-code' => $code);
203 :     }
204 :     # Now we have the external links. These are stored using hyperlink objects.
205 :     my @links = $fig->fid_links($fid);
206 :     for my $link (@links) {
207 :     my $hl = HyperLink->newFromHtml($link);
208 :     $self->PutE(FeatureLink => $fid, link => $hl);
209 :     }
210 :     # Virulence data is next. This is also hyperlink data.
211 :     my @virulenceTuples = $fig->get_attributes($fid, 'virulence_associated%');
212 :     for my $virulenceTuple (@virulenceTuples) {
213 :     my (undef, undef, $text, $url) = @$virulenceTuple;
214 :     my $hl = HyperLink->new($text, $url);
215 :     $self->PutE(FeatureVirulent => $fid, virulent => $hl);
216 :     }
217 :     # Finally, the essentiality stuff, which is the last of the hyperlinks.
218 :     my @essentials = $fig->get_attributes($fid, undef, ['essential', 'potential-essential']);
219 :     for my $essentialTuple (@essentials) {
220 :     my (undef, undef, $essentialityType, $url) = @$essentialTuple;
221 : parrello 1.5 # Only keep this datum if it has a URL. The ones without URLs are
222 :     # all duplicates.
223 :     if ($url) {
224 :     # Form a hyperlink from this essentiality tuple.
225 :     my $link = HyperLink->new($essentialityType, $url);
226 :     # Store it as essentiality data for this feature.
227 :     $self->PutE(FeatureEssential => $fid, essential => $link);
228 :     }
229 : parrello 1.1 }
230 :     # If this is a PEG, we have a protein sequence.
231 : parrello 1.3 my $proteinID;
232 : parrello 1.1 if ($type eq 'peg') {
233 :     # Get the translation.
234 :     my $proteinSequence = $fig->get_translation($fid);
235 : parrello 1.7 if (! $proteinSequence) {
236 :     Trace("No protein sequence found for $fid.") if T(2);
237 :     $self->Add(missingProtein => 1);
238 :     # Here there was some sort of error and the protein sequence did
239 :     # not come back. Ask for the DNA and translate it instead.
240 :     my $dna = $fig->get_dna_seq($fid);
241 :     $proteinSequence = FIG::translate($dna, undef, 1);
242 :     }
243 : parrello 1.1 # Compute the ID.
244 : parrello 1.3 $proteinID = ERDB::DigestKey($proteinSequence);
245 : parrello 1.1 # Create the protein record.
246 :     $self->PutE(ProteinSequence => $proteinID, sequence => $proteinSequence);
247 : parrello 1.3 $self->PutR(IsProteinFor => $proteinID, $fid);
248 : parrello 1.1 # Get the publications for this PEG.
249 :     my @pubs = $fig->get_attributes($fid, 'PUBMED_CURATED_RELEVANT');
250 :     for my $pub (@pubs) {
251 :     # Parse out the article title from the data.
252 : parrello 1.3 my (undef, undef, $data, $url) = @$pub;
253 : parrello 1.1 my @pieces = split /,/, $data, 3;
254 :     if (defined $pieces[2]) {
255 :     # Create the publication record.
256 : parrello 1.3 my $hl = HyperLink->new($pieces[2], $url);
257 : parrello 1.1 my $key = ERDB::DigestKey($url);
258 :     $self->PutE(Publication => $key, citation => $hl);
259 :     # Connect it to the protein.
260 :     $self->PutR(Concerns => $key, $proteinID);
261 :     }
262 :     }
263 : parrello 1.2 }
264 : parrello 1.3 # Now we need to compute the identifiers. We start with the aliases.
265 :     # Get the alias data for this feature. If there is none, we force an
266 :     # empty list.
267 :     my $aliasList = $aliasHash->{$fid} || [];
268 :     # Loop through the aliases found.
269 :     for my $aliasTuple (@$aliasList) {
270 :     my ($aliasID, $aliasType, $aliasConf) = @$aliasTuple;
271 :     # Get the natural form. If there is none, then the canonical
272 :     # form IS the natural form.
273 :     my $natural = AliasAnalysis::Type($aliasType => $aliasID) || $aliasID;
274 :     # Create the identifier record.
275 :     $self->PutE(Identifier => $aliasID, natural_form => $natural,
276 : parrello 1.5 source => $aliasType);
277 : parrello 1.3 # Is this a protein alias?
278 :     if ($aliasConf eq 'C' && $proteinID) {
279 :     # Yes. Connect it using IsNamedBy.
280 :     $self->PutR(IsNamedBy => $proteinID, $aliasID);
281 : parrello 1.2 } else {
282 : parrello 1.3 # No. Connect it to the feature.
283 :     $self->PutR(Identifies => $aliasID, $fid, conf => $aliasConf);
284 : parrello 1.2 }
285 :     }
286 : parrello 1.3 # Finally, this feature is an alias of itself.
287 :     $self->PutE(Identifier => $fid, natural_form => $fid,
288 :     source => 'SEED');
289 :     $self->PutR(Identifies => $fid, $fid, conf => 'A');
290 : parrello 1.1 }
291 :     }
292 :    
293 :    
294 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3