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

Annotation of /Sprout/ModelSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.16 - (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 : parrello 1.13 #use FIGMODEL;
20 : parrello 1.1
21 :     package ModelSaplingLoader;
22 :     use strict;
23 :     use Tracer;
24 :     use ERDB;
25 :     use base 'BaseSaplingLoader';
26 :    
27 : parrello 1.14 =head1 Sapling Model Load Group Class
28 : parrello 1.1
29 :     =head2 Introduction
30 :    
31 : parrello 1.14 The Model Load Group includes a small set of tables that describe reactions and compounds
32 :     and how they relate to the models in the main model database.
33 : parrello 1.1
34 :     =head3 new
35 :    
36 : parrello 1.14 my $sl = ModelSaplingLoader->new($erdb, $options, @tables);
37 : parrello 1.1
38 : parrello 1.14 Construct a new ModelSaplingLoader object.
39 : parrello 1.1
40 :     =over 4
41 :    
42 :     =item erdb
43 :    
44 : parrello 1.4 L<Sapling> object for the database being loaded.
45 : parrello 1.1
46 :     =item options
47 :    
48 :     Reference to a hash of command-line options.
49 :    
50 :     =item tables
51 :    
52 :     List of tables in this load group.
53 :    
54 :     =back
55 :    
56 :     =cut
57 :    
58 :     sub new {
59 :     # Get the parameters.
60 :     my ($class, $erdb, $options) = @_;
61 :     # Create the table list.
62 : parrello 1.14 my @tables = qw(Compound Reaction EcNumber Model Media IsTriggeredBy
63 :     IsCategorizedInto IsConsistentWith IsModeledBy Involves
64 :     IsRequiredBy);
65 : parrello 1.1 # Create the BaseSaplingLoader object.
66 :     my $retVal = BaseSaplingLoader::new($class, $erdb, $options, @tables);
67 : parrello 1.14 # Create the reaction tracking hash.
68 :     $retVal->{reactions} = {};
69 : parrello 1.1 # Return it.
70 :     return $retVal;
71 :     }
72 :    
73 :     =head2 Public Methods
74 :    
75 :     =head3 Generate
76 :    
77 :     $sl->Generate();
78 :    
79 : parrello 1.14 Generate the data for the model files.
80 : parrello 1.1
81 :     =cut
82 :    
83 :     sub Generate {
84 :     # Get the parameters.
85 :     my ($self) = @_;
86 :     # Get the database object.
87 :     my $erdb = $self->db();
88 :     # Is this the global section?
89 :     if ($self->global()) {
90 : parrello 1.14 # Load the tables from the model dump files.
91 :     $self->LoadModelFiles();
92 : parrello 1.1 } else {
93 :     # Get the section ID.
94 :     my $genomeID = $self->section();
95 :     #NO GENOME SPECIFIC MODEL STUFF
96 :     }
97 :     }
98 :    
99 : parrello 1.14 =head3 LoadModelFiles
100 : parrello 1.1
101 : parrello 1.14 $sl->LoadModelFiles();
102 : parrello 1.1
103 : parrello 1.14 Load the data from the six model dump files.
104 :    
105 :     =cut
106 :    
107 : parrello 1.15 # hash of ubiquitous compounds.
108 :     use constant UBIQUITOUS => {
109 : parrello 1.16 cpd00001 => 'OH-',
110 : parrello 1.15 cpd00002 => 'ATP',
111 : parrello 1.16 cpd00003 => 'Nicotinamideadeninedinucleotide',
112 :     cpd00004 => 'Nicotinamideadeninedinucleotide-reduced',
113 :     cpd00005 => 'Nicotinamideadeninedinucleotidephosphate-reduced',
114 :     cpd00006 => 'Nicotinamideadeninedinucleotidephosphate',
115 :     cpd00007 => 'Oxygen',
116 : parrello 1.15 cpd00008 => 'ADP',
117 : parrello 1.16 cpd00009 => 'Orthophosphoric acid',
118 : parrello 1.15 cpd00010 => 'CoenzymeA',
119 : parrello 1.16 cpd00011 => 'Carbon dioxide',
120 : parrello 1.15 cpd00012 => 'PPi',
121 : parrello 1.16 cpd00018 => 'AMP',
122 :     cpd00020 => 'Pyruvic Acid',
123 : parrello 1.15 cpd00022 => 'Acetyl-CoA',
124 : parrello 1.16 cpd00025 => 'Hydrogen peroxide',
125 :     cpd00067 => 'H+',
126 : parrello 1.15 cpd00971 => 'Sodium',
127 :     cpd15352 => '2-Demethylmenaquinone',
128 : parrello 1.16 cpd15353 => '2-Demethylmenaquinol',
129 :     cpd15499 => 'Menaquinol',
130 :     cpd15500 => 'Menaquinone',
131 :     cpd15560 => 'Ubiquinone-8',
132 :     cpd15561 => 'Ubiquinol-8',
133 : parrello 1.15 };
134 :    
135 : parrello 1.14 sub LoadModelFiles {
136 :     # Get the parameters.
137 :     my ($self) = @_;
138 :     # Get the model dump file directory.
139 :     my $dir = $self->db()->LoadDirectory() . "/models";
140 :     # First we read the compounds.
141 :     my $ih = $self->CheckFile("$dir/CompoundName.txt", qw(CompoundID Name));
142 :     while (! eof $ih) {
143 :     # Get the next compound.
144 :     my ($id, $label) = $self->ReadLine($ih);
145 :     # Create a compound record for it.
146 : parrello 1.15 $self->PutE(Compound => $id, label => $label, ubiquitous => (UBIQUITOUS->{$id} ? 1 : 0));
147 : parrello 1.14 }
148 :     # Next, the compound-reactions relationship. We create the reactions here, too.
149 :     $ih = $self->CheckFile("$dir/CompoundReaction.txt", qw(CompoundID ReactionID
150 : parrello 1.15 Stoichiometry Cofactor));
151 : parrello 1.14 while (! eof $ih) {
152 :     # Get the next link.
153 : parrello 1.15 my ($compound, $reaction, $stoich, $cofactor) = $self->ReadLine($ih);
154 : parrello 1.14 # Insure the reaction exists.
155 :     $self->CreateReaction($reaction);
156 :     # Check for product or substrate.
157 :     my $product;
158 :     if ($stoich < 0) {
159 :     $product = 0;
160 :     $stoich = -$stoich;
161 :     } else {
162 :     $product = 1;
163 :     }
164 :     # Connect the reaction to the compound.
165 :     $self->PutR(Involves => $reaction, $compound, product => $product,
166 : parrello 1.15 stoichiometry => $stoich, cofactor => $cofactor);
167 : parrello 1.14 }
168 :     # Now we connect the reactions to roles.
169 :     $ih = $self->CheckFile("$dir/ReactionRole.txt", qw(ReactionID Role));
170 :     while (! eof $ih) {
171 :     # Get the next reaction/role pair. We don't need to create roles here because
172 :     # they are built in the Feature module.
173 :     my ($reaction, $role) = $self->ReadLine($ih);
174 :     # Insure the reaction exists.
175 :     $self->CreateReaction($reaction);
176 :     # Connect the reaction to the role.
177 :     $self->PutR(IsTriggeredBy => $reaction, $role);
178 :     }
179 :     # Now we create the models.
180 :     $ih = $self->CheckFile("$dir/ModelGenome.txt", qw(ModelID GenomeID));
181 :     while (! eof $ih) {
182 :     # Get the next model.
183 :     my ($model, $genome) = $self->ReadLine($ih);
184 :     # Create the model.
185 :     $self->PutE(Model => $model);
186 :     # Connect it to the genome. Again, the genomes are created elsewhere.
187 :     $self->PutR(IsModeledBy => $genome, $model);
188 :     }
189 :     # Next we connect the reactions to models.
190 :     $ih = $self->CheckFile("$dir/ModelReaction.txt", qw(ModelID ReactionID));
191 :     while (! eof $ih) {
192 :     # Get the next line.
193 :     my ($model, $reaction) = $self->ReadLine($ih);
194 :     # Only proceed if a reaction is present.
195 :     if ($reaction) {
196 :     # Insure the reaction exists.
197 :     $self->CreateReaction($reaction);
198 :     # Connect the reaction to the model.
199 :     $self->PutR(IsRequiredBy => $reaction, $model);
200 : parrello 1.1 }
201 :     }
202 :     }
203 :    
204 : parrello 1.14 =head3 StartFile
205 :    
206 :     my $ih = $sl->CheckFile($fileName, @fieldNames);
207 :    
208 :     Read the header record of the specified file and verify that the field names match
209 :     the names in the input list. If they do not, an error will be thrown; if they do, an
210 :     open file handle will be returned, positioned on the first data record.
211 :    
212 :     =over 4
213 :    
214 :     =item fileName
215 :    
216 :     Name for the input file. The file is in standard tab-delimited format. The first record
217 :     contains the field names and the remaining records contain the data.
218 :    
219 :     =item fieldNames
220 :    
221 :     List of the field names expected, in order.
222 :    
223 :     =item RETURN
224 :    
225 :     Returns the open file handle if successful. If there is a mismatch, throws an error.
226 :    
227 :     =back
228 :    
229 :     =cut
230 :    
231 :     sub CheckFile {
232 :     # Get the parameters.
233 :     my ($self, $fileName, @fieldNames) = @_;
234 :     # Open the file.
235 :     my $retVal = Open(undef, "<$fileName");
236 :     $self->Add(files => 1);
237 :     # Read in the file header.
238 :     my @actualFields = Tracer::GetLine($retVal);
239 :     # This will be set to TRUE if there's a mismatch.
240 :     my $error = 0;
241 :     for (my $i = 0; $i <= $#fieldNames; $i++) {
242 :     if ($fieldNames[$i] ne $actualFields[$i]) {
243 :     Trace("Field match error: expected $fieldNames[$i], found $actualFields[$i].") if T(0);
244 :     $error = 1;
245 : parrello 1.1 }
246 :     }
247 : parrello 1.14 # Was there an error?
248 :     if ($error) {
249 :     # Yes, so abort.
250 :     Confess("Invalid field name header in $fileName.");
251 :     } else {
252 :     # No, so trace the open.
253 :     Trace("Processing $fileName.") if T(ERDBLoadGroup => 2);
254 :     }
255 :     # Return the file handle.
256 :     return $retVal;
257 : parrello 1.1 }
258 :    
259 : parrello 1.14 =head3 ReadLine
260 :    
261 :     my @fields = $sl->ReadLine($ih);
262 : parrello 1.1
263 : parrello 1.14 Read a line of data from an input file.
264 :    
265 :     =over 4
266 :    
267 :     =item ih
268 :    
269 :     Open file handle for the input file.
270 :    
271 :     =item RETURN
272 :    
273 :     Returns a list of the field values for the next record in the file.
274 :    
275 :     =back
276 :    
277 :     =cut
278 :    
279 :     sub ReadLine {
280 :     # Get the parameters.
281 :     my ($self, $ih) = @_;
282 :     # Read the line.
283 :     my @retVal = Tracer::GetLine($ih);
284 :     # Count this record.
285 :     $self->Track(records => $retVal[0], 1000);
286 :     # Return the data.
287 :     return @retVal;
288 :     }
289 :    
290 :    
291 :     =head3 CheckReaction
292 :    
293 :     $sl->CheckReaction($reaction);
294 :    
295 :     Insure we have created a rectord for the specified reaction.
296 :    
297 :     =over 4
298 :    
299 :     =item reaction
300 :    
301 :     ID of the reaction in question.
302 :    
303 :     =back
304 :    
305 :     =cut
306 :    
307 :     sub CreateReaction {
308 :     # Get the parameters.
309 :     my ($self, $reaction) = @_;
310 :     # Get the reaction hash.
311 :     my $reactionH = $self->{reactions};
312 :     # See if this reaction is new.
313 :     if (! $reactionH->{$reaction}) {
314 :     # It is, so create it.
315 :     $self->PutE(Reaction => $reaction);
316 :     # Insure we don't create it again.
317 :     $reactionH->{$reaction} = 1;
318 :     }
319 : parrello 1.10 }
320 :    
321 : parrello 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3