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

Annotation of /Sprout/ScenarioSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (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 ScenarioSaplingLoader;
21 :    
22 :     use strict;
23 :     use Tracer;
24 :     use ERDB;
25 :     use Rectangle;
26 :     use GD;
27 :     use base 'BaseSaplingLoader';
28 :    
29 :     =head1 Sapling Scenario Load Group Class
30 :    
31 :     =head2 Introduction
32 :    
33 :     The Scenario Load Group includes all of the major scenario-related data tables.
34 :    
35 :     =head3 new
36 :    
37 :     my $sl = ScenarioSaplingLoader->new($erdb, $options, @tables);
38 :    
39 :     Construct a new ScenarioSaplingLoader object.
40 :    
41 :     =over 4
42 :    
43 :     =item erdb
44 :    
45 : parrello 1.5 L<Sapling> object for the database being loaded.
46 : parrello 1.1
47 :     =item options
48 :    
49 :     Reference to a hash of command-line options.
50 :    
51 :     =item tables
52 :    
53 :     List of tables in this load group.
54 :    
55 :     =back
56 :    
57 :     =cut
58 :    
59 :     sub new {
60 :     # Get the parameters.
61 :     my ($class, $erdb, $options) = @_;
62 :     # Create the table list.
63 :     my @tables = sort qw(Scenario IsTerminusFor IsSubInstanceOf IsRelevantFor
64 : parrello 1.3 HasParticipant Shows Displays Diagram DiagramContent Overlaps);
65 : parrello 1.1 # Create the BaseSaplingLoader object.
66 :     my $retVal = BaseSaplingLoader::new($class, $erdb, $options, @tables);
67 :     # Return it.
68 :     return $retVal;
69 :     }
70 :    
71 :     =head2 Public Methods
72 :    
73 :     =head3 Generate
74 :    
75 :     $sl->Generate();
76 :    
77 :     Generate the data for the scenario-related data files.
78 :    
79 :     =cut
80 :    
81 :     sub Generate {
82 :     # Get the parameters.
83 :     my ($self) = @_;
84 :     # Get the database object.
85 :     my $erdb = $self->db();
86 :     # Get the source object.
87 :     my $fig = $self->source();
88 :     # Is this the global section?
89 :     if ($self->global()) {
90 :     # Yes. Load the scenarios.
91 :     $self->LoadScenarios($fig);
92 :     # Load the diagrams.
93 :     $self->LoadDiagrams($fig);
94 :     }
95 :     }
96 :    
97 :     =head3 LoadScenarios
98 :    
99 :     $sl->LoadScenarios($fig);
100 :    
101 :     Create the load files for the scenario data.
102 :    
103 :     =over 4
104 :    
105 :     =item fig
106 :    
107 :     FIG-like object used to access the scenario data.
108 :    
109 :     =back
110 :    
111 :     =cut
112 :    
113 :     sub LoadScenarios {
114 :     # Get the parameters.
115 :     my ($self, $fig) = @_;
116 :     # Get the Sapling object.
117 :     my $erdb = $self->db();
118 :     # We run through the subsystems and roles, generating the scenarios.
119 : parrello 1.6 # We'll need a role hash to prevent duplicates.
120 : parrello 1.1 my %roles = ();
121 : parrello 1.6 # This counter is used to compute scenario IDs.
122 :     my $scenarios = 0;
123 :     # Now loop through the subsystems.
124 : parrello 1.1 my @subsystems = sort keys %{$erdb->SubsystemHash()};
125 :     for my $subName (@subsystems) {
126 : parrello 1.9 Trace("Processing $subName.") if T(ERDBLoadGroup => 3);
127 : parrello 1.1 my $sub = $fig->get_subsystem($subName);
128 : parrello 1.7 # Only proceed if the subsystem exists.
129 :     if (! defined $sub) {
130 :     $self->Add(missingSubsystem => 1);
131 : parrello 1.9 } elsif ($sub->{empty_ss}) {
132 :     $self->Add(emptySubsystem => 1);
133 : parrello 1.7 } else {
134 :     # Get the subsystem's reactions. This is a bit complicated, since
135 :     # the subsystem object only gives us a role-to-reaction map.
136 :     my %roleMap = $sub->get_hope_reactions();
137 :     my @reactions;
138 :     for my $reactionList (values %roleMap) {
139 :     push @reactions, @$reactionList;
140 :     }
141 :     # Connect the subsystem to its diagrams.
142 :     my @maps = $sub->get_diagrams();
143 :     for my $mapData (@maps) {
144 :     $self->PutR(IsRelevantFor => $mapData->[0], $subName);
145 :     }
146 :     # Get the subsystem's scenarios. Note we ignore un-named scenarios.
147 :     # None of them have any data, so we don't need to keep them.
148 :     my @scenarioNames = grep { $_ } $sub->get_hope_scenario_names();
149 :     # Loop through the scenarios, creating scenario data.
150 :     for my $scenarioName (@scenarioNames) {
151 :     $self->Track(Scenarios => $scenarioName, 100);
152 :     # Get this scenario's ID.
153 :     $scenarios++;
154 :     my $scenarioID = $scenarios;
155 :     # Link this scenario to this subsystem.
156 :     $self->PutR(IsSubInstanceOf => $subName, $scenarioID);
157 :     # Create the scenario itself.
158 :     Trace("Creating scenario $scenarioID: $scenarioName.") if T(3);
159 :     $self->PutE(Scenario => $scenarioID, common_name => $scenarioName);
160 :     # Attach the input compounds.
161 :     for my $input ($sub->get_hope_input_compounds($scenarioName)) {
162 :     # Resolve the compound ID.
163 : parrello 1.12 my $inputID = $self->CompoundID($input);
164 : parrello 1.7 # Write the relationship record.
165 :     $self->PutR(IsTerminusFor => $inputID, $scenarioID,
166 :     group_number => 0);
167 :     # Now we need to set up the output compounds. They come in two
168 :     # groups, which we mark 1 and 2.
169 :     my $outputGroupID = 1;
170 :     # Set up the output compounds.
171 :     for my $outputGroup ($sub->get_hope_output_compounds($scenarioName)) {
172 :     # Attach the compounds.
173 :     for my $compound (@$outputGroup) {
174 :     # Resolve the compound ID.
175 : parrello 1.12 my $compoundID = $self->CompoundID($compound);
176 : parrello 1.7 # Write the relationship record.
177 :     $self->PutR(IsTerminusFor => $compoundID, $scenarioID,
178 :     group_number => $outputGroupID);
179 :     }
180 :     # # Increment the group number.
181 :     $outputGroupID++;
182 :     }
183 :     # Now we create the reaction lists. First we have the reactions that
184 :     # are not in the subsystem but are part of the scenario.
185 :     my @addReactions = $sub->get_hope_additional_reactions($scenarioName);
186 :     for my $reaction (@addReactions) {
187 :     # Resolve the reaction ID.
188 : parrello 1.12 my $reactionID = $self->ReactionID($reaction);
189 : parrello 1.7 # Write the relationship record.
190 :     $self->PutR(HasParticipant => $scenarioID, $reactionID,
191 :     type => 1);
192 :     }
193 :     # Next is the list of reactions not in the scenario. We get the list
194 :     # of these, and then we use it to modify the full reaction list. If
195 :     # the reaction is in the not-list, the type is 2. If it isn't in the
196 :     # not-list, the type is 0.
197 :     my %notReactions = map { $_ => 2 } $sub->get_hope_ignore_reactions($scenarioName);
198 :     for my $reaction (@reactions) {
199 :     # Resolve the reaction ID.
200 : parrello 1.12 my $reactionID = $self->ReactionID($reaction);
201 : parrello 1.4 # Write the relationship record.
202 : parrello 1.7 $self->PutR(HasParticipant => $scenarioID, $reactionID,
203 :     type => ($notReactions{$reaction} || 0));
204 :     }
205 :     # Link the maps.
206 :     my @maps = $sub->get_hope_map_ids($scenarioName);
207 :     for my $map (@maps) {
208 :     $self->PutR(Overlaps => $scenarioID, "map$map");
209 : parrello 1.1 }
210 :     }
211 :     }
212 : parrello 1.7 # Clear the subsystem cache to save space.
213 :     $fig->clear_subsystem_cache();
214 : parrello 1.1 }
215 :     }
216 :     }
217 :    
218 :    
219 :     =head3 LoadDiagrams
220 :    
221 :     $sl->LoadDiagrams($fig);
222 :    
223 :     Create the load files for the diagram data.
224 :    
225 :     =over 4
226 :    
227 :     =item fig
228 :    
229 :     FIG-like object used to access the data.
230 :    
231 :     =back
232 :    
233 :     =cut
234 :    
235 :     sub LoadDiagrams {
236 :     # Get the parameters.
237 :     my ($self, $fig) = @_;
238 :     # Loop through the maps.
239 :     my @maps = $fig->all_maps();
240 :     for my $map (sort @maps) {
241 :     $self->Track(Diagrams => $map, 20);
242 :     # Get the map's descriptive name.
243 :     my $name = $fig->map_name($map);
244 :     # Compute its title. The properties of the map are read from files
245 :     # having this title and different extensions.
246 :     my $mapTitle = "$FIG_Config::kegg/pathway/map/$map";
247 : parrello 1.10 # Now we need the map itself. If it's a PNG, we use it unaltered.
248 :     my $pngFileName;
249 :     if (-f "$mapTitle.png") {
250 :     $pngFileName = "$mapTitle.png";
251 : parrello 1.11 # Read the PNG file in as a GD::Image.
252 :     my $diagram = GD::Image->new($pngFileName);
253 :     # Write the diagram record.
254 :     $self->PutE(Diagram => $map, name => $name);
255 :     $self->PutE(DiagramContent => $map, content => $diagram);
256 :     # Now we connect it to the compounds.
257 : parrello 1.12 $self->Connect($map, $mapTitle . "_cpd.coord", 'Shows', 'CompoundID');
258 : parrello 1.11 # Finally, the reactions.
259 : parrello 1.12 $self->Connect($map, $mapTitle . "_rn.coord", 'Displays', 'ReactionID');
260 : parrello 1.10 } else {
261 : parrello 1.11 $self->Add(mapNotPNG => 1);
262 : parrello 1.10 }
263 : parrello 1.1 }
264 :     }
265 :    
266 :     =head3 Connect
267 :    
268 : parrello 1.4 $sl->Connect($mapID, $fileName, $relName, $method);
269 : parrello 1.1
270 :     Create the relationship records connecting the specified map to the
271 :     objects in the specified file. The file is tab-delimited, with the first
272 :     column being IDs of reactions or compounds, and the second through fifth
273 :     columns containing the rectangle coordinates of the compound or reaction
274 :     in the diagram.
275 :    
276 :     =over 4
277 :    
278 :     =item mapID
279 :    
280 :     ID of the relevant map.
281 :    
282 :     =item fileName
283 :    
284 :     Name of the file containing the coordinate data.
285 :    
286 :     =item relName
287 :    
288 :     Name of the relationship to be filled from the data.
289 :    
290 : parrello 1.4 =item method
291 :    
292 :     Name of the method to be used to convert IDs.
293 :    
294 :     =item
295 :    
296 : parrello 1.1 =back
297 :    
298 :     =cut
299 :    
300 :     sub Connect {
301 :     # Get the parameters.
302 : parrello 1.4 my ($self, $mapID, $fileName, $relName, $method) = @_;
303 : parrello 1.1 # Check the file.
304 :     if (! -s $fileName) {
305 : parrello 1.7 Trace("File \"$fileName\" not found for map $mapID.") if T(ERDBLoadGroup => 1);
306 : parrello 1.1 $self->Add('file-missing' => 1);
307 :     } else {
308 :     # Open the file.
309 :     my $ih = Open(undef, "<$fileName");
310 :     # Loop through the records.
311 :     while (! eof $ih) {
312 :     # Get the ID and the coordinates.
313 :     my ($id, @coords) = Tracer::GetLine($ih);
314 : parrello 1.4 # Resolve the ID.
315 : parrello 1.12 my $realID = eval("\$self->$method(\$id)");
316 : parrello 1.1 # Connect the ID to the diagram.
317 : parrello 1.4 $self->PutR($relName => $mapID, $realID, location => Rectangle->new(@coords));
318 : parrello 1.1 }
319 :     }
320 :     }
321 :    
322 :    
323 : parrello 1.8 =head3 GetFigModel
324 :    
325 :     my $figModel = $self->GetFigModel();
326 :    
327 :     Return a FIGMODEL object that can be used to map KEGG IDs to our IDs for reactions
328 :     and compounds. If we don't have a FIGMODEL object yet, one will be created and
329 :     cached in this object.
330 :    
331 :     =cut
332 :    
333 :     sub GetFigModel {
334 :     # Get the parameters.
335 :     my ($self) = @_;
336 :     # Look for a cached object.
337 :     my $retVal = $self->{figModel};
338 :     if (! defined $retVal) {
339 :     # It doesn't exist, so create one.
340 : parrello 1.12 require FIGMODEL;
341 : parrello 1.8 $retVal = FIGMODEL->new();
342 :     $self->{figModel} = $retVal;
343 :     }
344 :     # Return the cached object.
345 :     return $retVal;
346 :     }
347 :    
348 : parrello 1.12 =head3 CompoundID
349 :    
350 :     my $id = $self->CompoundID($compound);
351 :    
352 :     Return the ModelSEED ID for a given compound.
353 :    
354 :     =over 4
355 :    
356 :     =item compound
357 :    
358 :     Regular compound ID to convert.
359 :    
360 :     =item RETURN
361 :    
362 :     Returns the internal ID for the incoming compound, or the incoming value if it is
363 :     an unknown compound.
364 :    
365 :     =back
366 :    
367 :     =cut
368 :    
369 :     sub CompoundID {
370 :     # Get the parameters.
371 :     my ($self, $compound) = @_;
372 :     # Declare the return variable.
373 :     my $retVal = $compound;
374 :     # Get the model object.
375 :     my $figModel = $self->GetFigModel();
376 :     # Get the object for the given compound ID.
377 :     my $obj = $figModel->database()->get_object("cpdals", {alias => $compound});
378 :     # If we found the compound, get its real ID.
379 :     if (defined($obj)) {
380 :     $retVal = $obj->COMPOUND();
381 :     }
382 :     # Return the result.
383 :     return $retVal;
384 :     }
385 :    
386 :     =head3 ReactionID
387 :    
388 :     my $id = $self->ReactionID($reaction);
389 :    
390 :     Return the ModelSEED ID for a given reaction.
391 :    
392 :     =over 4
393 :    
394 :     =item reaction
395 :    
396 :     Regular reaction ID to convert.
397 :    
398 :     =item RETURN
399 :    
400 :     Returns the internal ID for the incoming reaction, or the incoming value if it is
401 :     an unknown reaction.
402 :    
403 :     =back
404 :    
405 :     =cut
406 :    
407 :     sub ReactionID {
408 :     # Get the parameters.
409 :     my ($self, $reaction) = @_;
410 :     # Declare the return variable.
411 :     my $retVal = $reaction;
412 :     # Get the model object.
413 :     my $figModel = $self->GetFidModel();
414 :     # Get the reaction ID.
415 :     $retVal = $figModel->id_of_reaction($reaction);
416 :     # Return the result.
417 :     return $retVal;
418 :     }
419 :    
420 : parrello 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3