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

Annotation of /Sprout/ScenarioSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (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 :     Trace("Processing $subName.") if T(3);
132 :     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 :     } else {
137 :     # Get the subsystem's reactions. This is a bit complicated, since
138 :     # the subsystem object only gives us a role-to-reaction map.
139 :     my %roleMap = $sub->get_hope_reactions();
140 :     my @reactions;
141 :     for my $reactionList (values %roleMap) {
142 :     push @reactions, @$reactionList;
143 :     }
144 :     # Connect the subsystem to its diagrams.
145 :     my @maps = $sub->get_diagrams();
146 :     for my $mapData (@maps) {
147 :     $self->PutR(IsRelevantFor => $mapData->[0], $subName);
148 :     }
149 :     # Get the subsystem's scenarios. Note we ignore un-named scenarios.
150 :     # None of them have any data, so we don't need to keep them.
151 :     my @scenarioNames = grep { $_ } $sub->get_hope_scenario_names();
152 :     # Loop through the scenarios, creating scenario data.
153 :     for my $scenarioName (@scenarioNames) {
154 :     $self->Track(Scenarios => $scenarioName, 100);
155 :     # Get this scenario's ID.
156 :     $scenarios++;
157 :     my $scenarioID = $scenarios;
158 :     # Link this scenario to this subsystem.
159 :     $self->PutR(IsSubInstanceOf => $subName, $scenarioID);
160 :     # Create the scenario itself.
161 :     Trace("Creating scenario $scenarioID: $scenarioName.") if T(3);
162 :     $self->PutE(Scenario => $scenarioID, common_name => $scenarioName);
163 :     # Attach the input compounds.
164 :     for my $input ($sub->get_hope_input_compounds($scenarioName)) {
165 :     # Resolve the compound ID.
166 :     my $inputID = $figmodel->id_of_compound($input);
167 :     # Write the relationship record.
168 :     $self->PutR(IsTerminusFor => $inputID, $scenarioID,
169 :     group_number => 0);
170 :     # Now we need to set up the output compounds. They come in two
171 :     # groups, which we mark 1 and 2.
172 :     my $outputGroupID = 1;
173 :     # Set up the output compounds.
174 :     for my $outputGroup ($sub->get_hope_output_compounds($scenarioName)) {
175 :     # Attach the compounds.
176 :     for my $compound (@$outputGroup) {
177 :     # Resolve the compound ID.
178 :     my $compoundID = $figmodel->id_of_compound($compound);
179 :     # Write the relationship record.
180 :     $self->PutR(IsTerminusFor => $compoundID, $scenarioID,
181 :     group_number => $outputGroupID);
182 :     }
183 :     # # Increment the group number.
184 :     $outputGroupID++;
185 :     }
186 :     # Now we create the reaction lists. First we have the reactions that
187 :     # are not in the subsystem but are part of the scenario.
188 :     my @addReactions = $sub->get_hope_additional_reactions($scenarioName);
189 :     for my $reaction (@addReactions) {
190 :     # Resolve the reaction ID.
191 :     my $reactionID = $figmodel->id_of_reaction($reaction);
192 :     # Write the relationship record.
193 :     $self->PutR(HasParticipant => $scenarioID, $reactionID,
194 :     type => 1);
195 :     }
196 :     # Next is the list of reactions not in the scenario. We get the list
197 :     # of these, and then we use it to modify the full reaction list. If
198 :     # the reaction is in the not-list, the type is 2. If it isn't in the
199 :     # not-list, the type is 0.
200 :     my %notReactions = map { $_ => 2 } $sub->get_hope_ignore_reactions($scenarioName);
201 :     for my $reaction (@reactions) {
202 :     # Resolve the reaction ID.
203 :     my $reactionID = $figmodel->id_of_reaction($reaction);
204 : parrello 1.4 # Write the relationship record.
205 : parrello 1.7 $self->PutR(HasParticipant => $scenarioID, $reactionID,
206 :     type => ($notReactions{$reaction} || 0));
207 :     }
208 :     # Link the maps.
209 :     my @maps = $sub->get_hope_map_ids($scenarioName);
210 :     for my $map (@maps) {
211 :     $self->PutR(Overlaps => $scenarioID, "map$map");
212 : parrello 1.1 }
213 :     }
214 :     }
215 : parrello 1.7 # Clear the subsystem cache to save space.
216 :     $fig->clear_subsystem_cache();
217 : parrello 1.1 }
218 :     }
219 :     }
220 :    
221 :    
222 :     =head3 LoadDiagrams
223 :    
224 :     $sl->LoadDiagrams($fig);
225 :    
226 :     Create the load files for the diagram data.
227 :    
228 :     =over 4
229 :    
230 :     =item fig
231 :    
232 :     FIG-like object used to access the data.
233 :    
234 :     =back
235 :    
236 :     =cut
237 :    
238 :     sub LoadDiagrams {
239 :     # Get the parameters.
240 :     my ($self, $fig) = @_;
241 :     # Get an Image::Magick object. This enables us to convert GIFs to PNGs.
242 :     my $p = Image::Magick->new();
243 :     # Create a temporary file name for the PNGs.
244 :     my $pngFileName = "$FIG_Config::temp/map$$.png";
245 :     # Loop through the maps.
246 :     my @maps = $fig->all_maps();
247 :     for my $map (sort @maps) {
248 :     $self->Track(Diagrams => $map, 20);
249 :     # Get the map's descriptive name.
250 :     my $name = $fig->map_name($map);
251 :     # Compute its title. The properties of the map are read from files
252 :     # having this title and different extensions.
253 :     my $mapTitle = "$FIG_Config::kegg/pathway/map/$map";
254 :     # Now we need the map itself. We use Image::Magick to convert it to a PNG.
255 :     $p->Read("$mapTitle.gif");
256 :     $p->Write($pngFileName);
257 :     # Read it back in as a GD::Image.
258 :     my $diagram = GD::Image->new($pngFileName);
259 :     # Write the diagram record.
260 : parrello 1.3 $self->PutE(Diagram => $map, name => $name);
261 :     $self->PutE(DiagramContent => $map, content => $diagram);
262 : parrello 1.1 # Now we connect it to the compounds.
263 : parrello 1.4 $self->Connect($map, $mapTitle . "_cpd.coord", 'Shows', 'id_of_compound');
264 : parrello 1.1 # Finally, the reactions.
265 : parrello 1.4 $self->Connect($map, $mapTitle . "_rn.coord", 'Displays', 'id_of_reaction');
266 : parrello 1.1 }
267 :     }
268 :    
269 :     =head3 Connect
270 :    
271 : parrello 1.4 $sl->Connect($mapID, $fileName, $relName, $method);
272 : parrello 1.1
273 :     Create the relationship records connecting the specified map to the
274 :     objects in the specified file. The file is tab-delimited, with the first
275 :     column being IDs of reactions or compounds, and the second through fifth
276 :     columns containing the rectangle coordinates of the compound or reaction
277 :     in the diagram.
278 :    
279 :     =over 4
280 :    
281 :     =item mapID
282 :    
283 :     ID of the relevant map.
284 :    
285 :     =item fileName
286 :    
287 :     Name of the file containing the coordinate data.
288 :    
289 :     =item relName
290 :    
291 :     Name of the relationship to be filled from the data.
292 :    
293 : parrello 1.4 =item method
294 :    
295 :     Name of the method to be used to convert IDs.
296 :    
297 :     =item
298 :    
299 : parrello 1.1 =back
300 :    
301 :     =cut
302 :    
303 :     sub Connect {
304 :     # Get the parameters.
305 : parrello 1.4 my ($self, $mapID, $fileName, $relName, $method) = @_;
306 :     # Get the FIGMODEL object. This is used to map KEGG reaction and compound IDs
307 :     # (used in disagrams) to our IDs.
308 : parrello 1.8 my $figmodel = $self->GetFigModel();
309 : parrello 1.1 # Check the file.
310 :     if (! -s $fileName) {
311 : parrello 1.7 Trace("File \"$fileName\" not found for map $mapID.") if T(ERDBLoadGroup => 1);
312 : parrello 1.1 $self->Add('file-missing' => 1);
313 :     } else {
314 :     # Open the file.
315 :     my $ih = Open(undef, "<$fileName");
316 :     # Loop through the records.
317 :     while (! eof $ih) {
318 :     # Get the ID and the coordinates.
319 :     my ($id, @coords) = Tracer::GetLine($ih);
320 : parrello 1.4 # Resolve the ID.
321 :     my $realID = eval("\$figmodel->$method(\$id)");
322 : parrello 1.1 # Connect the ID to the diagram.
323 : parrello 1.4 $self->PutR($relName => $mapID, $realID, location => Rectangle->new(@coords));
324 : parrello 1.1 }
325 :     }
326 :     }
327 :    
328 :    
329 : parrello 1.8 =head3 GetFigModel
330 :    
331 :     my $figModel = $self->GetFigModel();
332 :    
333 :     Return a FIGMODEL object that can be used to map KEGG IDs to our IDs for reactions
334 :     and compounds. If we don't have a FIGMODEL object yet, one will be created and
335 :     cached in this object.
336 :    
337 :     =cut
338 :    
339 :     sub GetFigModel {
340 :     # Get the parameters.
341 :     my ($self) = @_;
342 :     # Look for a cached object.
343 :     my $retVal = $self->{figModel};
344 :     if (! defined $retVal) {
345 :     # It doesn't exist, so create one.
346 :     $retVal = FIGMODEL->new();
347 :     $self->{figModel} = $retVal;
348 :     }
349 :     # Return the cached object.
350 :     return $retVal;
351 :     }
352 :    
353 : parrello 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3