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

Annotation of /Sprout/ScenarioSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3