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

Annotation of /Sprout/BaseSproutLoader.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 BaseSproutLoader;
21 :    
22 :     use strict;
23 :     use Tracer;
24 :     use ERDB;
25 : parrello 1.2 use Time::HiRes;
26 : parrello 1.1 use base 'ERDBLoadGroup';
27 :    
28 :     # Name of the global section
29 :     use constant GLOBAL => 'Globals';
30 :    
31 :     =head1 Sprout Load Group Base Class
32 :    
33 :     =head2 Introduction
34 :    
35 :     This is the base class for all the Sprout loaders. It performs common tasks
36 :     required by multiple load groups.
37 :    
38 :     =head3 new
39 :    
40 :     my $sl = BaseSproutLoader->new($erdb, $source, $options, @tables);
41 :    
42 :     Construct a new BaseSproutLoader object.
43 :    
44 :     =over 4
45 :    
46 :     =item erdb
47 :    
48 :     [[SproutPm]] object for the database being loaded.
49 :    
50 :     =item source
51 :    
52 :     [[FigPm]] object used to access the source data.
53 :    
54 :     =item options
55 :    
56 :     Reference to a hash of command-line options.
57 :    
58 :     =item tables
59 :    
60 :     List of tables in this load group.
61 :    
62 :     =back
63 :    
64 :     =cut
65 :    
66 :     sub new {
67 :     # Get the parameters.
68 :     my ($class, $erdb, $source, $options, @tables) = @_;
69 :     # Create the BaseSproutLoader object.
70 :     my $retVal = ERDBLoadGroup::new($class, $source, $erdb, $erdb->LoadDirectory(),
71 :     $options, @tables);
72 : parrello 1.2 # Return it.
73 : parrello 1.1 return $retVal;
74 :     }
75 :    
76 :    
77 :     =head2 Public Methods
78 :    
79 :     =head3 GetGenomeAttributes
80 :    
81 : parrello 1.2 my $aHashRef = $sl->GetGenomeAttributes($genomeID, \@fids);
82 : parrello 1.1
83 :     Return a hash of attributes keyed on feature ID. This method gets all the NMPDR-related
84 :     attributes for all the features of a genome in a single call, then organizes them into
85 :     a hash.
86 :    
87 :     =over 4
88 :    
89 :     =item fig
90 :    
91 :     FIG-like object for accessing attributes.
92 :    
93 :     =item genomeID
94 :    
95 :     ID of the genome who's attributes are desired.
96 :    
97 :     =item fids
98 :    
99 : parrello 1.2 Reference to a list of feature IDs whose attributes are to be kept. If it is a list
100 :     of lists, the feature IDs will be taken from the first element in each sub-list.
101 : parrello 1.1
102 :     =item RETURN
103 :    
104 :     Returns a reference to a hash. The key of the hash is the feature ID. The value is the
105 :     reference to a list of the feature's attribute tuples. Each tuple contains the feature ID,
106 :     the attribute key, and one or more attribute values.
107 :    
108 :     =back
109 :    
110 :     =cut
111 :    
112 :     sub GetGenomeAttributes {
113 :     # Get the parameters.
114 : parrello 1.2 my ($self, $genomeID, $fids) = @_;
115 :     # Start a timer.
116 :     my $start = time();
117 :     # Declare the return variable and initialize it with all the features.
118 :     my %retVal = map { (ref $_ ? $_->[0] : $_) => [] } @$fids;
119 : parrello 1.1 # Get the source object.
120 :     my $fig = $self->source();
121 :     # Get the attributes. If ev_code_cron is running, we may get a timeout error, so
122 :     # an eval is used.
123 :     my @aList = ();
124 :     eval {
125 : parrello 1.2 @aList = $fig->get_attributes("fig|$genomeID%");
126 : parrello 1.1 Trace(scalar(@aList) . " attributes returned for genome $genomeID.") if T(ERDBLoadGroup => 3);
127 :     };
128 :     # Check for a problem.
129 :     if ($@) {
130 :     Trace("Retrying attributes for $genomeID due to error: $@") if T(ERDBLoadGroup => 1);
131 :     # Our fallback plan is to process the attributes in blocks of 100. This is much slower,
132 :     # but allows us to continue processing.
133 : parrello 1.2 my $nFids = scalar @$fids;
134 : parrello 1.1 for (my $i = 0; $i < $nFids; $i += 100) {
135 :     # Determine the index of the last feature ID we'll be specifying on this pass.
136 :     # Normally it's $i + 99, but if we're close to the end it may be less.
137 :     my $end = ($i + 100 > $nFids ? $nFids - 1 : $i + 99);
138 :     # Get a slice of the fid list.
139 : parrello 1.2 my @slice = @{$fids}[$i .. $end];
140 : parrello 1.1 # Get the relevant attributes.
141 :     Trace("Retrieving attributes for fids $i to $end.") if T(ERDBLoadGroup => 3);
142 : parrello 1.2 my @aShort = $fig->get_attributes(\@slice);
143 : parrello 1.1 Trace(scalar(@aShort) . " attributes returned for fids $i to $end.") if T(ERDBLoadGroup => 3);
144 :     push @aList, @aShort;
145 :     }
146 :     }
147 :     # Now we should have all the interesting attributes in @aList. Populate the hash with
148 :     # them.
149 :     for my $aListEntry (@aList) {
150 :     my $fid = $aListEntry->[0];
151 : parrello 1.2 if (exists $retVal{$fid}) {
152 :     push @{$retVal{$fid}}, $aListEntry;
153 :     $self->Add(attributes => 1);
154 : parrello 1.1 }
155 :     }
156 : parrello 1.2 $self->Add('attribute-time' => time() - $start);
157 : parrello 1.1 # Return the result.
158 : parrello 1.2 return \%retVal;
159 : parrello 1.1 }
160 :    
161 :     =head3 GetSubsystems
162 :    
163 :     my $subsystems = $sl->GetSubsystems();
164 :    
165 :     Get a hash of the subsystems for this incarnation of the Sprout database.
166 :     The hash maps each subsystem ID to 1. The first time this method is called,
167 :     it creates a file listing the subsystems found. Subsequent calls read the
168 :     list from the file so that the selection of subsystems remains consistent.
169 :    
170 :     =cut
171 :    
172 :     sub GetSubsystems {
173 :     # Get the parameters.
174 :     my ($self) = @_;
175 :     # Get the sprout object.
176 :     my $sprout = $self->db();
177 :     # Get the FIG source object.
178 :     my $fig = $self->source();
179 :     # The names found will be put in here.
180 :     my @retVal = ();
181 :     # Check for the file.
182 :     my $subFileName = $sprout->LoadDirectory() . "/SubsystemList.dty";
183 :     if (-f $subFileName) {
184 :     # It's there. Get the list from it.
185 :     @retVal = Tracer::GetFile($subFileName);
186 :     } else {
187 :     # No, so compute the list and then create the file.
188 :     my @subs = $fig->all_subsystems();
189 :     for my $sub (@subs) {
190 :     if ($fig->nmpdr_subsystem($sub)) {
191 :     push @retVal, $sub;
192 :     }
193 :     }
194 :     Tracer::PutFile($subFileName, \@retVal);
195 :     }
196 :     # Return the result.
197 :     my %retVal = map { $_ => 1 } @retVal;
198 :     return \%retVal;
199 :     }
200 :    
201 :    
202 :     =head3 GetSectionList
203 :    
204 :     my @sections = BaseSproutLoader::GetSectionList($sprout, $fig);
205 :    
206 :     Return a list of the sections for a Sprout load. The section list is
207 :     normally determined by retrieving a list of all the complete genomes and
208 :     adding an extra global section at the end; however, the first time the
209 :     sections are determined, they are stored in a master file so that the
210 :     same list is used regardless of what may have changed in the source data.
211 :     (A similar trick is used for subsystems).
212 :    
213 :     =over 4
214 :    
215 :     =item sprout
216 :    
217 :     [[SproutPm]] object for the database being loaded.
218 :    
219 :     =item fig
220 :    
221 :     [[FigPm]] object from which the data is being retrieved.
222 :    
223 :     =item RETURN
224 :    
225 :     Returns a list of section names.
226 :    
227 :     =back
228 :    
229 :     =cut
230 :    
231 :     sub GetSectionList {
232 :     my ($sprout, $fig) = @_;
233 :     # Declare the return variable.
234 :     my @retVal;
235 :     # Look for the section list in the data directory.
236 :     my $sectionFileName = $sprout->LoadDirectory() . "/" .
237 :     ERDBGenerate::CreateFileName('section_master', undef, 'control');
238 :     if (-f $sectionFileName) {
239 :     # It's there. Get the list from it.
240 :     @retVal = Tracer::GetFile($sectionFileName);
241 :     } else {
242 :     # We need to create it. First, we get the list: all the complete
243 :     # genomes followed by a global section indicator. The genomes are sorted
244 :     # in lexical order.
245 :     @retVal = sort { $a cmp $b } $fig->genomes(1);
246 :     push @retVal, GLOBAL;
247 :     # Write the list to a file for future use. This insures that if the source
248 :     # data changes, we have a consistent section list.
249 :     Tracer::PutFile($sectionFileName, \@retVal);
250 :     }
251 :     # Return the list.
252 :     return @retVal;
253 :     }
254 :    
255 :     =head3 global
256 :    
257 :     my $flag = $sl->global();
258 :    
259 :     TRUE if this is the global section, else FALSE.
260 :    
261 :     =cut
262 :    
263 :     sub global {
264 :     # Get the parameters.
265 :     my ($self) = @_;
266 :     # Return the result.
267 :     return ($self->{section} eq GLOBAL);
268 :     }
269 :    
270 :     =head3 GetCommaList
271 :    
272 :     my $string = $sl->GetCommaList($value);
273 :    
274 :     Create a comma-separated list of the values in a list reference. If the
275 :     list reference is a scalar, it will be returned unchanged. If it is
276 :     undefined, an empty string will be returned. The idea is that we may be
277 :     looking at a string, a list, or nothing, but whatever comes out will be a
278 :     string.
279 :    
280 :     =over 4
281 :    
282 :     =item value
283 :    
284 :     Reference to a list of values to be assembled into the return string.
285 :    
286 :     =item RETURN
287 :    
288 :     Returns a scalar string containing the content of the input value.
289 :    
290 :     =back
291 :    
292 :     =cut
293 :    
294 :     sub GetCommaList {
295 :     # Get the parameters.
296 :     my ($self, $value) = @_;
297 :     # Declare the return variable.
298 :     my $retVal = "";
299 :     # Only proceed if we have an input value.
300 :     if (defined $value) {
301 :     # Analyze the input value.
302 :     if (ref $value eq 'ARRAY') {
303 :     # Here it's a list reference.
304 :     $retVal = join(", ", @$value);
305 :     } else {
306 :     # Here it's not. Flatten it to a scalar.
307 :     $retVal = "$value";
308 :     }
309 :     }
310 :     # Return the result.
311 :     return $retVal;
312 :     }
313 :    
314 :    
315 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3