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

Annotation of /Sprout/ScenarioSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3