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

Annotation of /Sprout/FeatureSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (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 :     # Fix the assignment for non-PEG features.
142 :     if (! defined $assignment) {
143 :     $assignment = $aliases;
144 :     $assignmentMaker ||= 'master';
145 :     }
146 :     # Convert the location string to a list of location objects.
147 :     my @locs = map { BasicLocation->new($_) } split /\s*,\s*/, $locationString;
148 :     # Now we need to run through the locations. We'll put the total sequence
149 :     # length in here.
150 :     my $seqLen = 0;
151 :     # This will track the ordinal position of the current location segment.
152 :     my $locN = 1;
153 :     # Loop through the location objects.
154 :     for my $loc (@locs) {
155 :     # Add this location's length to the total length.
156 :     $seqLen += $loc->Length();
157 :     # Extract the contig ID. Note that we need to prefix the
158 :     # genome ID to make it unique.
159 :     my $contig = $loc->Contig();
160 :     my $contigID = "$genomeID:$contig";
161 :     # We also need the location's direction.
162 :     my $dir = $loc->Dir();
163 :     # Now we peel off sections of the location and connect them
164 :     # to the feature.
165 :     my $peel = $loc->Peel($maxLength);
166 :     while (defined $peel) {
167 : parrello 1.2 $self->PutR(IsLocatedIn => $fid, $contigID, ordinal => $locN++,
168 :     begin => $peel->Left(), len => $peel->Length(),
169 :     dir => $dir);
170 : parrello 1.1 $peel = $loc->Peel($maxLength);
171 :     }
172 :     # Output the residual. There will always be one, because of the way
173 :     # Peel works.
174 : parrello 1.2 $self->PutR(IsLocatedIn => $fid, $contigID, ordinal => $locN,
175 :     begin => $loc->Left(), dir => $dir, len => $loc->Length());
176 : parrello 1.1 }
177 : parrello 1.3 # Is this an attachment site?
178 :     if ($type eq 'att') {
179 :     # Yes, connect it to the attached feature.
180 :     if ($assignment =~ /att([LR])\s+for\s+(fig\|.+)/) {
181 :     $self->PutR(IsAttachmentSiteFor => $fid, $2, edge => $1);
182 :     } else {
183 :     Trace("Invalid attachment function for $fid: $assignment") if T(1);
184 :     $self->Add(badAttachment => 1);
185 :     }
186 :     }
187 : parrello 1.1 # Emit the feature record.
188 :     $self->PutE(Feature => $fid, feature_type => $type,
189 : parrello 1.2 sequence_length => $seqLen, function => $assignment,
190 :     locked => $fig->is_locked_fid($fid));
191 : parrello 1.1 # Connect the feature to its genome.
192 :     $self->PutR(IsOwnerOf => $genomeID, $fid);
193 :     # Now we have a whole bunch of attribute-related stuff to store in
194 :     # secondary Feature tables. First is the evidence codes.
195 :     my @evidenceTuples = $fig->get_attributes($fid, 'evidence_code');
196 :     for my $evidenceTuple (@evidenceTuples) {
197 :     my (undef, undef, $code) = @$evidenceTuple;
198 :     $self->PutE(FeatureEvidence => $fid, 'evidence-code' => $code);
199 :     }
200 :     # Now we have the external links. These are stored using hyperlink objects.
201 :     my @links = $fig->fid_links($fid);
202 :     for my $link (@links) {
203 :     my $hl = HyperLink->newFromHtml($link);
204 :     $self->PutE(FeatureLink => $fid, link => $hl);
205 :     }
206 :     # Virulence data is next. This is also hyperlink data.
207 :     my @virulenceTuples = $fig->get_attributes($fid, 'virulence_associated%');
208 :     for my $virulenceTuple (@virulenceTuples) {
209 :     my (undef, undef, $text, $url) = @$virulenceTuple;
210 :     my $hl = HyperLink->new($text, $url);
211 :     $self->PutE(FeatureVirulent => $fid, virulent => $hl);
212 :     }
213 :     # Finally, the essentiality stuff, which is the last of the hyperlinks.
214 :     my @essentials = $fig->get_attributes($fid, undef, ['essential', 'potential-essential']);
215 :     for my $essentialTuple (@essentials) {
216 :     my (undef, undef, $essentialityType, $url) = @$essentialTuple;
217 : parrello 1.5 # Only keep this datum if it has a URL. The ones without URLs are
218 :     # all duplicates.
219 :     if ($url) {
220 :     # Form a hyperlink from this essentiality tuple.
221 :     my $link = HyperLink->new($essentialityType, $url);
222 :     # Store it as essentiality data for this feature.
223 :     $self->PutE(FeatureEssential => $fid, essential => $link);
224 :     }
225 : parrello 1.1 }
226 :     # If this is a PEG, we have a protein sequence.
227 : parrello 1.3 my $proteinID;
228 : parrello 1.1 if ($type eq 'peg') {
229 :     # Get the translation.
230 :     my $proteinSequence = $fig->get_translation($fid);
231 :     # Compute the ID.
232 : parrello 1.3 $proteinID = ERDB::DigestKey($proteinSequence);
233 : parrello 1.1 # Create the protein record.
234 :     $self->PutE(ProteinSequence => $proteinID, sequence => $proteinSequence);
235 : parrello 1.3 $self->PutR(IsProteinFor => $proteinID, $fid);
236 : parrello 1.1 # Get the publications for this PEG.
237 :     my @pubs = $fig->get_attributes($fid, 'PUBMED_CURATED_RELEVANT');
238 :     for my $pub (@pubs) {
239 :     # Parse out the article title from the data.
240 : parrello 1.3 my (undef, undef, $data, $url) = @$pub;
241 : parrello 1.1 my @pieces = split /,/, $data, 3;
242 :     if (defined $pieces[2]) {
243 :     # Create the publication record.
244 : parrello 1.3 my $hl = HyperLink->new($pieces[2], $url);
245 : parrello 1.1 my $key = ERDB::DigestKey($url);
246 :     $self->PutE(Publication => $key, citation => $hl);
247 :     # Connect it to the protein.
248 :     $self->PutR(Concerns => $key, $proteinID);
249 :     }
250 :     }
251 : parrello 1.2 }
252 : parrello 1.3 # Now we need to compute the identifiers. We start with the aliases.
253 :     # Get the alias data for this feature. If there is none, we force an
254 :     # empty list.
255 :     my $aliasList = $aliasHash->{$fid} || [];
256 :     # Loop through the aliases found.
257 :     for my $aliasTuple (@$aliasList) {
258 :     my ($aliasID, $aliasType, $aliasConf) = @$aliasTuple;
259 :     # Get the natural form. If there is none, then the canonical
260 :     # form IS the natural form.
261 :     my $natural = AliasAnalysis::Type($aliasType => $aliasID) || $aliasID;
262 :     # Create the identifier record.
263 :     $self->PutE(Identifier => $aliasID, natural_form => $natural,
264 : parrello 1.5 source => $aliasType);
265 : parrello 1.3 # Is this a protein alias?
266 :     if ($aliasConf eq 'C' && $proteinID) {
267 :     # Yes. Connect it using IsNamedBy.
268 :     $self->PutR(IsNamedBy => $proteinID, $aliasID);
269 : parrello 1.2 } else {
270 : parrello 1.3 # No. Connect it to the feature.
271 :     $self->PutR(Identifies => $aliasID, $fid, conf => $aliasConf);
272 : parrello 1.2 }
273 :     }
274 : parrello 1.3 # Finally, this feature is an alias of itself.
275 :     $self->PutE(Identifier => $fid, natural_form => $fid,
276 :     source => 'SEED');
277 :     $self->PutR(Identifies => $fid, $fid, conf => 'A');
278 : parrello 1.1 }
279 :     }
280 :    
281 :    
282 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3