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

Annotation of /Sprout/FeatureSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3