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

Annotation of /Sprout/ScenarioSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (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 :     # Get the subsystem's reactions. This is a bit complicated, since
136 :     # the subsystem object only gives us a role-to-reaction map.
137 :     my %roleMap = $sub->get_hope_reactions();
138 :     my @reactions;
139 :     for my $reactionList (values %roleMap) {
140 :     push @reactions, @$reactionList;
141 :     }
142 :     # Connect the subsystem to its diagrams.
143 :     my @maps = $sub->get_diagrams();
144 :     for my $mapData (@maps) {
145 :     $self->PutR(IsRelevantFor => $mapData->[0], $subName);
146 :     }
147 :     # Get the subsystem's scenarios. Note we ignore un-named scenarios.
148 :     # None of them have any data, so we don't need to keep them.
149 :     my @scenarioNames = grep { $_ } $sub->get_hope_scenario_names();
150 :     # Loop through the scenarios, creating scenario data.
151 :     for my $scenarioName (@scenarioNames) {
152 :     $self->Track(Scenarios => $scenarioName, 100);
153 : parrello 1.6 # Get this scenario's ID.
154 :     $scenarios++;
155 :     my $scenarioID = $scenarios;
156 : parrello 1.1 # Link this scenario to this subsystem.
157 : parrello 1.6 $self->PutR(IsSubInstanceOf => $subName, $scenarioID);
158 :     # Create the scenario itself.
159 :     Trace("Creating scenario $scenarioID: $scenarioName.") if T(3);
160 :     $self->PutE(Scenario => $scenarioID, common_name => $scenarioName);
161 :     # Attach the input compounds.
162 :     for my $input ($sub->get_hope_input_compounds($scenarioName)) {
163 :     # Resolve the compound ID.
164 :     my $inputID = $figmodel->id_of_compound($input);
165 :     # Write the relationship record.
166 :     $self->PutR(IsTerminusFor => $inputID, $scenarioID,
167 :     group_number => 0);
168 : parrello 1.1 # Now we need to set up the output compounds. They come in two
169 :     # groups, which we mark 1 and 2.
170 :     my $outputGroupID = 1;
171 :     # Set up the output compounds.
172 :     for my $outputGroup ($sub->get_hope_output_compounds($scenarioName)) {
173 :     # Attach the compounds.
174 :     for my $compound (@$outputGroup) {
175 : parrello 1.4 # Resolve the compound ID.
176 :     my $compoundID = $figmodel->id_of_compound($compound);
177 :     # Write the relationship record.
178 : parrello 1.6 $self->PutR(IsTerminusFor => $compoundID, $scenarioID,
179 : parrello 1.1 group_number => $outputGroupID);
180 :     }
181 :     # # Increment the group number.
182 :     $outputGroupID++;
183 :     }
184 :     # Now we create the reaction lists. First we have the reactions that
185 :     # are not in the subsystem but are part of the scenario.
186 :     my @addReactions = $sub->get_hope_additional_reactions($scenarioName);
187 :     for my $reaction (@addReactions) {
188 : parrello 1.4 # Resolve the reaction ID.
189 :     my $reactionID = $figmodel->id_of_reaction($reaction);
190 :     # Write the relationship record.
191 : parrello 1.6 $self->PutR(HasParticipant => $scenarioID, $reactionID,
192 : parrello 1.4 type => 1);
193 : parrello 1.1 }
194 :     # Next is the list of reactions not in the scenario. We get the list
195 :     # of these, and then we use it to modify the full reaction list. If
196 :     # the reaction is in the not-list, the type is 2. If it isn't in the
197 :     # not-list, the type is 0.
198 :     my %notReactions = map { $_ => 2 } $sub->get_hope_ignore_reactions($scenarioName);
199 :     for my $reaction (@reactions) {
200 : parrello 1.4 # Resolve the reaction ID.
201 :     my $reactionID = $figmodel->id_of_reaction($reaction);
202 :     # Write the relationship record.
203 : parrello 1.6 $self->PutR(HasParticipant => $scenarioID, $reactionID,
204 : parrello 1.1 type => ($notReactions{$reaction} || 0));
205 :     }
206 :     # Link the maps.
207 :     my @maps = $sub->get_hope_map_ids($scenarioName);
208 :     for my $map (@maps) {
209 : parrello 1.6 $self->PutR(Overlaps => $scenarioID, "map$map");
210 : parrello 1.1 }
211 :     }
212 :     }
213 : parrello 1.2 # Clear the subsystem cache to save space.
214 :     $fig->clear_subsystem_cache();
215 : parrello 1.1 }
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 :     # Get an Image::Magick object. This enables us to convert GIFs to PNGs.
239 :     my $p = Image::Magick->new();
240 :     # Create a temporary file name for the PNGs.
241 :     my $pngFileName = "$FIG_Config::temp/map$$.png";
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 :     # Now we need the map itself. We use Image::Magick to convert it to a PNG.
252 :     $p->Read("$mapTitle.gif");
253 :     $p->Write($pngFileName);
254 :     # Read it back in as a GD::Image.
255 :     my $diagram = GD::Image->new($pngFileName);
256 :     # Write the diagram record.
257 : parrello 1.3 $self->PutE(Diagram => $map, name => $name);
258 :     $self->PutE(DiagramContent => $map, content => $diagram);
259 : parrello 1.1 # Now we connect it to the compounds.
260 : parrello 1.4 $self->Connect($map, $mapTitle . "_cpd.coord", 'Shows', 'id_of_compound');
261 : parrello 1.1 # Finally, the reactions.
262 : parrello 1.4 $self->Connect($map, $mapTitle . "_rn.coord", 'Displays', 'id_of_reaction');
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 :     # Get the FIGMODEL object. This is used to map KEGG reaction and compound IDs
304 :     # (used in disagrams) to our IDs.
305 :     my $figmodel = $self->{figModel};
306 : parrello 1.1 # Check the file.
307 :     if (! -s $fileName) {
308 :     Trace("File \"$fileName\" not found for map $mapID.") if T(1);
309 :     $self->Add('file-missing' => 1);
310 :     } else {
311 :     # Open the file.
312 :     my $ih = Open(undef, "<$fileName");
313 :     # Loop through the records.
314 :     while (! eof $ih) {
315 :     # Get the ID and the coordinates.
316 :     my ($id, @coords) = Tracer::GetLine($ih);
317 : parrello 1.4 # Resolve the ID.
318 :     my $realID = eval("\$figmodel->$method(\$id)");
319 : parrello 1.1 # Connect the ID to the diagram.
320 : parrello 1.4 $self->PutR($relName => $mapID, $realID, location => Rectangle->new(@coords));
321 : parrello 1.1 }
322 :     }
323 :     }
324 :    
325 :    
326 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3