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

Annotation of /Sprout/ScenarioSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3