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

Annotation of /Sprout/ModelSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (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 :     cpd00002 => 'ATP',
110 :     cpd00008 => 'ADP',
111 :     cpd00007 => 'Oxygen',
112 :     cpd00010 => 'CoenzymeA',
113 :     cpd00012 => 'PPi',
114 :     cpd15560 => 'Ubiquinone-8',
115 :     cpd15561 => 'Ubiquinol-8',
116 :     cpd15500 => 'Menaquinone',
117 :     cpd15499 => 'Menaquinol',
118 :     cpd00022 => 'Acetyl-CoA',
119 :     cpd00018 => 'AMP',
120 :     cpd00971 => 'Sodium',
121 :     cpd15352 => '2-Demethylmenaquinone',
122 :     cpd15353 => '2-Demethylmenaquinol'
123 :    
124 :     };
125 :    
126 : parrello 1.14 sub LoadModelFiles {
127 :     # Get the parameters.
128 :     my ($self) = @_;
129 :     # Get the model dump file directory.
130 :     my $dir = $self->db()->LoadDirectory() . "/models";
131 :     # First we read the compounds.
132 :     my $ih = $self->CheckFile("$dir/CompoundName.txt", qw(CompoundID Name));
133 :     while (! eof $ih) {
134 :     # Get the next compound.
135 :     my ($id, $label) = $self->ReadLine($ih);
136 :     # Create a compound record for it.
137 : parrello 1.15 $self->PutE(Compound => $id, label => $label, ubiquitous => (UBIQUITOUS->{$id} ? 1 : 0));
138 : parrello 1.14 }
139 :     # Next, the compound-reactions relationship. We create the reactions here, too.
140 :     $ih = $self->CheckFile("$dir/CompoundReaction.txt", qw(CompoundID ReactionID
141 : parrello 1.15 Stoichiometry Cofactor));
142 : parrello 1.14 while (! eof $ih) {
143 :     # Get the next link.
144 : parrello 1.15 my ($compound, $reaction, $stoich, $cofactor) = $self->ReadLine($ih);
145 : parrello 1.14 # Insure the reaction exists.
146 :     $self->CreateReaction($reaction);
147 :     # Check for product or substrate.
148 :     my $product;
149 :     if ($stoich < 0) {
150 :     $product = 0;
151 :     $stoich = -$stoich;
152 :     } else {
153 :     $product = 1;
154 :     }
155 :     # Connect the reaction to the compound.
156 :     $self->PutR(Involves => $reaction, $compound, product => $product,
157 : parrello 1.15 stoichiometry => $stoich, cofactor => $cofactor);
158 : parrello 1.14 }
159 :     # Now we connect the reactions to roles.
160 :     $ih = $self->CheckFile("$dir/ReactionRole.txt", qw(ReactionID Role));
161 :     while (! eof $ih) {
162 :     # Get the next reaction/role pair. We don't need to create roles here because
163 :     # they are built in the Feature module.
164 :     my ($reaction, $role) = $self->ReadLine($ih);
165 :     # Insure the reaction exists.
166 :     $self->CreateReaction($reaction);
167 :     # Connect the reaction to the role.
168 :     $self->PutR(IsTriggeredBy => $reaction, $role);
169 :     }
170 :     # Now we create the models.
171 :     $ih = $self->CheckFile("$dir/ModelGenome.txt", qw(ModelID GenomeID));
172 :     while (! eof $ih) {
173 :     # Get the next model.
174 :     my ($model, $genome) = $self->ReadLine($ih);
175 :     # Create the model.
176 :     $self->PutE(Model => $model);
177 :     # Connect it to the genome. Again, the genomes are created elsewhere.
178 :     $self->PutR(IsModeledBy => $genome, $model);
179 :     }
180 :     # Next we connect the reactions to models.
181 :     $ih = $self->CheckFile("$dir/ModelReaction.txt", qw(ModelID ReactionID));
182 :     while (! eof $ih) {
183 :     # Get the next line.
184 :     my ($model, $reaction) = $self->ReadLine($ih);
185 :     # Only proceed if a reaction is present.
186 :     if ($reaction) {
187 :     # Insure the reaction exists.
188 :     $self->CreateReaction($reaction);
189 :     # Connect the reaction to the model.
190 :     $self->PutR(IsRequiredBy => $reaction, $model);
191 : parrello 1.1 }
192 :     }
193 :     }
194 :    
195 : parrello 1.14 =head3 StartFile
196 :    
197 :     my $ih = $sl->CheckFile($fileName, @fieldNames);
198 :    
199 :     Read the header record of the specified file and verify that the field names match
200 :     the names in the input list. If they do not, an error will be thrown; if they do, an
201 :     open file handle will be returned, positioned on the first data record.
202 :    
203 :     =over 4
204 :    
205 :     =item fileName
206 :    
207 :     Name for the input file. The file is in standard tab-delimited format. The first record
208 :     contains the field names and the remaining records contain the data.
209 :    
210 :     =item fieldNames
211 :    
212 :     List of the field names expected, in order.
213 :    
214 :     =item RETURN
215 :    
216 :     Returns the open file handle if successful. If there is a mismatch, throws an error.
217 :    
218 :     =back
219 :    
220 :     =cut
221 :    
222 :     sub CheckFile {
223 :     # Get the parameters.
224 :     my ($self, $fileName, @fieldNames) = @_;
225 :     # Open the file.
226 :     my $retVal = Open(undef, "<$fileName");
227 :     $self->Add(files => 1);
228 :     # Read in the file header.
229 :     my @actualFields = Tracer::GetLine($retVal);
230 :     # This will be set to TRUE if there's a mismatch.
231 :     my $error = 0;
232 :     for (my $i = 0; $i <= $#fieldNames; $i++) {
233 :     if ($fieldNames[$i] ne $actualFields[$i]) {
234 :     Trace("Field match error: expected $fieldNames[$i], found $actualFields[$i].") if T(0);
235 :     $error = 1;
236 : parrello 1.1 }
237 :     }
238 : parrello 1.14 # Was there an error?
239 :     if ($error) {
240 :     # Yes, so abort.
241 :     Confess("Invalid field name header in $fileName.");
242 :     } else {
243 :     # No, so trace the open.
244 :     Trace("Processing $fileName.") if T(ERDBLoadGroup => 2);
245 :     }
246 :     # Return the file handle.
247 :     return $retVal;
248 : parrello 1.1 }
249 :    
250 : parrello 1.14 =head3 ReadLine
251 :    
252 :     my @fields = $sl->ReadLine($ih);
253 : parrello 1.1
254 : parrello 1.14 Read a line of data from an input file.
255 :    
256 :     =over 4
257 :    
258 :     =item ih
259 :    
260 :     Open file handle for the input file.
261 :    
262 :     =item RETURN
263 :    
264 :     Returns a list of the field values for the next record in the file.
265 :    
266 :     =back
267 :    
268 :     =cut
269 :    
270 :     sub ReadLine {
271 :     # Get the parameters.
272 :     my ($self, $ih) = @_;
273 :     # Read the line.
274 :     my @retVal = Tracer::GetLine($ih);
275 :     # Count this record.
276 :     $self->Track(records => $retVal[0], 1000);
277 :     # Return the data.
278 :     return @retVal;
279 :     }
280 :    
281 :    
282 :     =head3 CheckReaction
283 :    
284 :     $sl->CheckReaction($reaction);
285 :    
286 :     Insure we have created a rectord for the specified reaction.
287 :    
288 :     =over 4
289 :    
290 :     =item reaction
291 :    
292 :     ID of the reaction in question.
293 :    
294 :     =back
295 :    
296 :     =cut
297 :    
298 :     sub CreateReaction {
299 :     # Get the parameters.
300 :     my ($self, $reaction) = @_;
301 :     # Get the reaction hash.
302 :     my $reactionH = $self->{reactions};
303 :     # See if this reaction is new.
304 :     if (! $reactionH->{$reaction}) {
305 :     # It is, so create it.
306 :     $self->PutE(Reaction => $reaction);
307 :     # Insure we don't create it again.
308 :     $reactionH->{$reaction} = 1;
309 :     }
310 : parrello 1.10 }
311 :    
312 : parrello 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3