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

Annotation of /Sprout/ScenarioSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (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 : parrello 1.4 # Create and attach a FIGMODEL object.
70 :     $retVal->{figModel} = FIGMODEL->new();
71 : parrello 1.1 # Return it.
72 :     return $retVal;
73 :     }
74 :    
75 :     =head2 Public Methods
76 :    
77 :     =head3 Generate
78 :    
79 :     $sl->Generate();
80 :    
81 :     Generate the data for the scenario-related data files.
82 :    
83 :     =cut
84 :    
85 :     sub Generate {
86 :     # Get the parameters.
87 :     my ($self) = @_;
88 :     # Get the database object.
89 :     my $erdb = $self->db();
90 :     # Get the source object.
91 :     my $fig = $self->source();
92 :     # Is this the global section?
93 :     if ($self->global()) {
94 :     # Yes. Load the scenarios.
95 :     $self->LoadScenarios($fig);
96 :     # Load the diagrams.
97 :     $self->LoadDiagrams($fig);
98 :     }
99 :     }
100 :    
101 :     =head3 LoadScenarios
102 :    
103 :     $sl->LoadScenarios($fig);
104 :    
105 :     Create the load files for the scenario data.
106 :    
107 :     =over 4
108 :    
109 :     =item fig
110 :    
111 :     FIG-like object used to access the scenario data.
112 :    
113 :     =back
114 :    
115 :     =cut
116 :    
117 :     sub LoadScenarios {
118 :     # Get the parameters.
119 :     my ($self, $fig) = @_;
120 :     # Get the Sapling object.
121 :     my $erdb = $self->db();
122 : parrello 1.4 # Get the FIGMODEL object. This is used to map KEGG reaction IDs (used in
123 :     # scenarios) to our reaction IDs.
124 :     my $figmodel = $self->{figModel};
125 : parrello 1.1 # We run through the subsystems and roles, generating the scenarios.
126 : parrello 1.6 # We'll need a role hash to prevent duplicates.
127 : parrello 1.1 my %roles = ();
128 : parrello 1.6 # This counter is used to compute scenario IDs.
129 :     my $scenarios = 0;
130 :     # Now loop through the subsystems.
131 : parrello 1.1 my @subsystems = sort keys %{$erdb->SubsystemHash()};
132 :     for my $subName (@subsystems) {
133 :     Trace("Processing $subName.") if T(3);
134 :     my $sub = $fig->get_subsystem($subName);
135 : parrello 1.7 # Only proceed if the subsystem exists.
136 :     if (! defined $sub) {
137 :     $self->Add(missingSubsystem => 1);
138 :     } 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 :     my $pngFileName = "$FIG_Config::temp/map$$.png";
247 :     # 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 :     # Now we need the map itself. We use Image::Magick to convert it to a PNG.
257 :     $p->Read("$mapTitle.gif");
258 :     $p->Write($pngFileName);
259 :     # Read it back in as a GD::Image.
260 :     my $diagram = GD::Image->new($pngFileName);
261 :     # Write the diagram record.
262 : parrello 1.3 $self->PutE(Diagram => $map, name => $name);
263 :     $self->PutE(DiagramContent => $map, content => $diagram);
264 : parrello 1.1 # Now we connect it to the compounds.
265 : parrello 1.4 $self->Connect($map, $mapTitle . "_cpd.coord", 'Shows', 'id_of_compound');
266 : parrello 1.1 # Finally, the reactions.
267 : parrello 1.4 $self->Connect($map, $mapTitle . "_rn.coord", 'Displays', 'id_of_reaction');
268 : parrello 1.1 }
269 :     }
270 :    
271 :     =head3 Connect
272 :    
273 : parrello 1.4 $sl->Connect($mapID, $fileName, $relName, $method);
274 : parrello 1.1
275 :     Create the relationship records connecting the specified map to the
276 :     objects in the specified file. The file is tab-delimited, with the first
277 :     column being IDs of reactions or compounds, and the second through fifth
278 :     columns containing the rectangle coordinates of the compound or reaction
279 :     in the diagram.
280 :    
281 :     =over 4
282 :    
283 :     =item mapID
284 :    
285 :     ID of the relevant map.
286 :    
287 :     =item fileName
288 :    
289 :     Name of the file containing the coordinate data.
290 :    
291 :     =item relName
292 :    
293 :     Name of the relationship to be filled from the data.
294 :    
295 : parrello 1.4 =item method
296 :    
297 :     Name of the method to be used to convert IDs.
298 :    
299 :     =item
300 :    
301 : parrello 1.1 =back
302 :    
303 :     =cut
304 :    
305 :     sub Connect {
306 :     # Get the parameters.
307 : parrello 1.4 my ($self, $mapID, $fileName, $relName, $method) = @_;
308 :     # Get the FIGMODEL object. This is used to map KEGG reaction and compound IDs
309 :     # (used in disagrams) to our IDs.
310 :     my $figmodel = $self->{figModel};
311 : parrello 1.1 # Check the file.
312 :     if (! -s $fileName) {
313 : parrello 1.7 Trace("File \"$fileName\" not found for map $mapID.") if T(ERDBLoadGroup => 1);
314 : parrello 1.1 $self->Add('file-missing' => 1);
315 :     } else {
316 :     # Open the file.
317 :     my $ih = Open(undef, "<$fileName");
318 :     # Loop through the records.
319 :     while (! eof $ih) {
320 :     # Get the ID and the coordinates.
321 :     my ($id, @coords) = Tracer::GetLine($ih);
322 : parrello 1.4 # Resolve the ID.
323 :     my $realID = eval("\$figmodel->$method(\$id)");
324 : parrello 1.1 # Connect the ID to the diagram.
325 : parrello 1.4 $self->PutR($relName => $mapID, $realID, location => Rectangle->new(@coords));
326 : parrello 1.1 }
327 :     }
328 :     }
329 :    
330 :    
331 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3