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

Annotation of /Sprout/ScenarioSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3