[Bio] / FigKernelPackages / FIGMODELmodel.pm Repository:
ViewVC logotype

Annotation of /FigKernelPackages/FIGMODELmodel.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (view) (download) (as text)

1 : chenry 1.7 use strict;
2 : chenry 1.2 package FIGMODELmodel;
3 :    
4 :     =head1 FIGMODELmodel object
5 :     =head2 Introduction
6 :     Module for manipulating model objects.
7 :     =head2 Core Object Methods
8 :    
9 :     =head3 new
10 :     Definition:
11 :     FIGMODELmodel = FIGMODELmodel->new();
12 :     Description:
13 :     This is the constructor for the FIGMODELmodel object.
14 :     =cut
15 :     sub new {
16 :     my ($class,$figmodel,$id) = @_;
17 : chenry 1.6
18 : chenry 1.2 #Error checking first
19 :     if (!defined($figmodel)) {
20 :     print STDERR "FIGMODELmodel->new(undef,".$id."):figmodel must be defined to create a model object!\n";
21 :     return undef;
22 :     }
23 :     my $self = {_figmodel => $figmodel};
24 :     bless $self;
25 :     if (!defined($id)) {
26 :     $self->figmodel()->error_message("FIGMODELmodel->new(figmodel,undef):id must be defined to create a model object");
27 :     return undef;
28 :     }
29 :    
30 :     #Checking that the id exists
31 :     my $tbl = $self->figmodel()->database()->GetDBTable("MODELS");
32 :     if (!defined($tbl)) {
33 :     $self->figmodel()->error_message("FIGMODELmodel->new(figmodel,".$id."):could not load MODELS table. Check database!");
34 :     return undef;
35 :     }
36 :    
37 :     #If the id is a number, we get the model row by index
38 :     my $index = $id;
39 :     if ($id =~ m/^\d+$/) {
40 :     $self->{_data} = $tbl->get_row($id);
41 :     } else {
42 :     $self->{_data} = $tbl->get_row_by_key($id,"id");
43 : chenry 1.5 if (!defined($self->{_data})) {
44 :     if ($id =~ m/(.+)(V[^V]+)/) {
45 :     $self->{_data} = $tbl->get_row_by_key($1,"id");
46 :     if (!defined($self->{_data})) {
47 :     $self->figmodel()->error_message("FIGMODELmodel->new(figmodel,".$id."):could not find model ".$id." in database!");
48 :     return undef;
49 :     }
50 :     $self->{_selectedversion} = $2;
51 :     } else {
52 :     $self->figmodel()->error_message("FIGMODELmodel->new(figmodel,".$id."):could not find model ".$id." in database!");
53 :     return undef;
54 :     }
55 : chenry 1.2 }
56 : chenry 1.5 $index = $tbl->row_index($self->{_data});
57 : chenry 1.2 }
58 :     if (!defined($self->{_data})) {
59 :     $self->figmodel()->error_message("FIGMODELmodel->new(figmodel,".$id."):could not find specified id in database!");
60 :     return undef;
61 :     }
62 :     $self->{_index} = $index;
63 :     $self->figmodel()->{_models}->{$self->id()} = $self;
64 :     $self->figmodel()->{_models}->{$self->index()} = $self;
65 :     return $self;
66 :     }
67 :    
68 :     =head3 config
69 :     Definition:
70 :     ref::key value = FIGMODELmodel->config(string::key);
71 :     Description:
72 :     Trying to avoid using calls that assume configuration data is stored in a particular manner.
73 :     Call this function to get file paths etc.
74 :     =cut
75 :     sub config {
76 :     my ($self,$key) = @_;
77 :     return $self->figmodel()->config($key);
78 :     }
79 :    
80 :     =head3 fail
81 :     Definition:
82 :     -1 = FIGMODELmodel->fail();
83 :     Description:
84 :     Standard return for failed functions.
85 :     =cut
86 :     sub fail {
87 :     my ($self) = @_;
88 :     return $self->figmodel()->fail();
89 :     }
90 :    
91 :     =head3 success
92 :     Definition:
93 :     1 = FIGMODELmodel->success();
94 :     Description:
95 :     Standard return for successful functions.
96 :     =cut
97 :     sub success {
98 :     my ($self) = @_;
99 :     return $self->figmodel()->success();
100 :     }
101 :    
102 :     =head3 figmodel
103 :     Definition:
104 :     FIGMODEL = FIGMODELmodel->figmodel();
105 :     Description:
106 :     Returns a FIGMODEL object
107 :     =cut
108 :     sub figmodel {
109 :     my ($self) = @_;
110 :     return $self->{"_figmodel"};
111 :     }
112 :    
113 :     =head3 fig
114 :     Definition:
115 :     FIGMODEL = FIGMODELmodel->fig();
116 :     Description:
117 :     Returns a FIG object
118 :     =cut
119 :     sub fig {
120 :     my ($self) = @_;
121 :    
122 :     if (!defined($self->{_fig}) && $self->source() !~ /^MGRAST/) {
123 :     if ($self->source() =~ /^RAST/) {
124 :     $self->{"_fig"} = $self->figmodel()->fig();#$self->genome());
125 :     } else {
126 :     $self->{"_fig"} = $self->figmodel()->fig();
127 :     }
128 :     }
129 :    
130 :     return $self->{"_fig"};
131 :     }
132 :    
133 :     =head3 mgdata
134 :     Definition:
135 :     FIGMODEL = FIGMODELmodel->mgdata();
136 :     Description:
137 :     Returns a mgrast database object
138 :     =cut
139 :     sub mgdata {
140 :     my ($self) = @_;
141 :    
142 :     if (!defined($self->{_mgdata}) && $self->source() =~ /^MGRAST/) {
143 :     require MGRAST;
144 :     $self->{_mgdata} = $self->figmodel()->mgrast()->Job->get_objects( { 'genome_id' => $self->genome() } )
145 :     }
146 :    
147 :     return $self->{_mgdata};
148 :     }
149 :    
150 :     =head3 id
151 :     Definition:
152 :     string = FIGMODELmodel->id();
153 :     Description:
154 :     Returns model id
155 :     =cut
156 :     sub id {
157 :     my ($self) = @_;
158 :     return $self->{_data}->{id}->[0];
159 :     }
160 :    
161 :     =head3 owner
162 :     Definition:
163 :     string = FIGMODELmodel->owner();
164 :     Description:
165 :     Returns the username for the model owner
166 :     =cut
167 :     sub owner {
168 :     my ($self) = @_;
169 :     return $self->{_data}->{owner}->[0];
170 :     }
171 :    
172 :     =head3 index
173 :     Definition:
174 :     string = FIGMODELmodel->index();
175 :     Description:
176 :     Returns model index
177 :     =cut
178 :     sub index {
179 :     my ($self) = @_;
180 :     return $self->{_index};
181 :     }
182 :    
183 :     =head3 name
184 :     Definition:
185 :     string = FIGMODELmodel->name();
186 :     Description:
187 :     Returns the name of the organism or metagenome sample being modeled
188 :     =cut
189 :     sub name {
190 :     my ($self) = @_;
191 :    
192 :     if (!defined($self->{_name})) {
193 :     my $source = $self->source();
194 :     if ($source =~ /^MGRAST/) {
195 :     $self->{_name} = "NA";
196 :     if (defined($self->mgdata())) {
197 :     $self->{_name} = $self->mgdata()->genome_name;
198 :     }
199 :     } elsif (defined($self->stats())) {
200 :     $self->{_name} = $self->stats()->{'Organism name'}->[0];
201 :     } elsif ($source !~ /^RAST/) {
202 :     $self->{_name} = $self->fig()->orgname_of_orgid($self->genome());
203 :     }
204 :     }
205 :    
206 :     return $self->{_name};
207 :     }
208 :    
209 :     =head3 stats
210 :     Definition:
211 :     string = FIGMODELmodel->stats();
212 :     Description:
213 :     Returns model stats
214 :     =cut
215 :     sub stats {
216 :     my ($self) = @_;
217 :    
218 :     if (!defined($self->{_stats})) {
219 :     $self->{_stats} = $self->figmodel()->database()->GetDBTable("MODEL STATS")->get_row_by_key($self->id(),"Model ID");
220 :     }
221 :     return $self->{_stats};
222 :     }
223 :    
224 :     =head3 get_reaction_class
225 :     Definition:
226 :     string = FIGMODELmodel->get_reaction_class(string::reaction ID);
227 :     Description:
228 :     Returns reaction class
229 :     =cut
230 :     sub get_reaction_class {
231 :     my ($self,$reaction,$nohtml) = @_;
232 :    
233 :     if (!-e $self->directory()."ReactionClassification-".$self->id().".tbl") {
234 :     if (!defined($self->{_reaction_classes})) {
235 :     $self->{_reaction_classes} = $self->figmodel()->database()->load_table($self->directory()."ReactionClassification-".$self->id()."-Complete.tbl",";","|",0,["REACTION"]);
236 :     if (!defined($self->{_reaction_classes})) {
237 :     return undef;
238 :     }
239 :     }
240 :    
241 :     my $ClassRow = $self->{_reaction_classes}->get_row_by_key($reaction,"REACTION");
242 :     if (defined($ClassRow) && defined($ClassRow->{CLASS})) {
243 :     my $class;
244 :     if ($ClassRow->{CLASS}->[0] eq "Positive") {
245 :     $class = "Essential =>";
246 :     } elsif ($ClassRow->{CLASS}->[0] eq "Negative") {
247 :     $class = "Essential <=";
248 :     } elsif ($ClassRow->{CLASS}->[0] eq "Positive variable") {
249 :     $class = "Active =>";
250 :     } elsif ($ClassRow->{CLASS}->[0] eq "Negative variable") {
251 :     $class = "Active <=";
252 :     } elsif ($ClassRow->{CLASS}->[0] eq "Variable") {
253 :     $class = "Active <=>";
254 :     } elsif ($ClassRow->{CLASS}->[0] eq "Blocked") {
255 :     $class = "Inactive";
256 :     } elsif ($ClassRow->{CLASS}->[0] eq "Dead") {
257 :     $class = "Disconnected";
258 :     }
259 :     if (!defined($nohtml) || $nohtml ne "1") {
260 :     $class = "<span title=\"Flux:".$ClassRow->{MIN}->[0]." to ".$ClassRow->{MAX}->[0]."\">".$class."</span>";
261 :     }
262 :     return $class;
263 :     }
264 :     return undef;
265 :     }
266 :    
267 :     if (!defined($self->{_reaction_classes})) {
268 :     $self->{_reaction_classes} = $self->figmodel()->database()->load_table($self->directory()."ReactionClassification-".$self->id().".tbl",";","|",0,["REACTION"]);
269 :     if (!defined($self->{_reaction_classes})) {
270 :     return undef;
271 :     }
272 :     }
273 :    
274 :     my $ClassRow = $self->{_reaction_classes}->get_row_by_key($reaction,"REACTION");
275 :     my $classstring = "";
276 :     if (defined($ClassRow) && defined($ClassRow->{CLASS})) {
277 :     for (my $i=0; $i < @{$ClassRow->{CLASS}};$i++) {
278 :     if (length($classstring) > 0) {
279 :     $classstring .= "<br>";
280 :     }
281 :     my $NewClass;
282 :     if ($ClassRow->{CLASS}->[$i] eq "Positive") {
283 :     $NewClass = $ClassRow->{MEDIA}->[$i].":Essential =>";
284 :     if (!defined($nohtml) || $nohtml ne "1") {
285 :     $NewClass = "<span title=\"Flux:".$ClassRow->{MIN}->[$i]." to ".$ClassRow->{MAX}->[$i]."\">".$NewClass."</span>";
286 :     }
287 :     } elsif ($ClassRow->{CLASS}->[$i] eq "Negative") {
288 :     $NewClass = $ClassRow->{MEDIA}->[$i].":Essential <=";
289 :     if (!defined($nohtml) || $nohtml ne "1") {
290 :     $NewClass = "<span title=\"Flux:".$ClassRow->{MIN}->[$i]." to ".$ClassRow->{MAX}->[$i]."\">".$NewClass."</span>";
291 :     }
292 :     } elsif ($ClassRow->{CLASS}->[$i] eq "Positive variable") {
293 :     $NewClass = $ClassRow->{MEDIA}->[$i].":Active =>";
294 :     if (!defined($nohtml) || $nohtml ne "1") {
295 :     $NewClass = "<span title=\"Flux:".$ClassRow->{MIN}->[$i]." to ".$ClassRow->{MAX}->[$i]."\">".$NewClass."</span>";
296 :     }
297 :     } elsif ($ClassRow->{CLASS}->[$i] eq "Negative variable") {
298 :     $NewClass = $ClassRow->{MEDIA}->[$i].":Active <=";
299 :     if (!defined($nohtml) || $nohtml ne "1") {
300 :     $NewClass = "<span title=\"Flux:".$ClassRow->{MIN}->[$i]." to ".$ClassRow->{MAX}->[$i]."\">".$NewClass."</span>";
301 :     }
302 :     } elsif ($ClassRow->{CLASS}->[$i] eq "Variable") {
303 :     $NewClass = $ClassRow->{MEDIA}->[$i].":Active <=>";
304 :     if (!defined($nohtml) || $nohtml ne "1") {
305 :     $NewClass = "<span title=\"Flux:".$ClassRow->{MIN}->[$i]." to ".$ClassRow->{MAX}->[$i]."\">".$NewClass."</span>";
306 :     }
307 :     } elsif ($ClassRow->{CLASS}->[$i] eq "Blocked") {
308 :     $NewClass = $ClassRow->{MEDIA}->[$i].":Inactive";
309 :     if (!defined($nohtml) || $nohtml ne "1") {
310 :     $NewClass = "<span title=\"Flux:".$ClassRow->{MIN}->[$i]." to ".$ClassRow->{MAX}->[$i]."\">".$NewClass."</span>";
311 :     }
312 :     } elsif ($ClassRow->{CLASS}->[$i] eq "Dead") {
313 :     $NewClass = $ClassRow->{MEDIA}->[$i].":Disconnected";
314 :     if (!defined($nohtml) || $nohtml ne "1") {
315 :     $NewClass = "<span title=\"Flux:".$ClassRow->{MIN}->[$i]." to ".$ClassRow->{MAX}->[$i]."\">".$NewClass."</span>";
316 :     }
317 :     }
318 :     $classstring .= $NewClass;
319 :     }
320 :     }
321 :     return $classstring;
322 :     }
323 :    
324 :     =head3 get_biomass
325 :     Definition:
326 :     string = FIGMODELmodel->get_biomass();
327 :     Description:
328 :     Returns data for the biomass reaction
329 :     =cut
330 :     sub get_biomass {
331 :     my ($self) = @_;
332 :    
333 :     if (!defined($self->{_biomass})) {
334 :     my $rxntbl = $self->reaction_table();
335 :     for (my $i=0; $i < $rxntbl->size(); $i++) {
336 :     if ($rxntbl->get_row($i)->{"LOAD"}->[0] =~ m/bio\d\d\d\d\d/) {
337 :     $self->{_biomass} = $rxntbl->get_row($i)->{"LOAD"}->[0];
338 :     last;
339 :     }
340 :     }
341 :     }
342 :    
343 :     return $self->get_reaction_data($self->{_biomass});
344 :     }
345 :    
346 :     =head3 get_reaction_data
347 :     Definition:
348 :     string = FIGMODELmodel->get_reaction_data(string::reaction ID);
349 :     Description:
350 :     Returns model reaction data
351 :     =cut
352 :     sub get_reaction_data {
353 :     my ($self,$reaction) = @_;
354 :    
355 :     if (!defined($self->reaction_table())) {
356 :     return undef;
357 :     }
358 :     if ($reaction =~ m/^\d+$/) {
359 :     $self->reaction_table()->get_row($reaction);
360 :     }
361 :     return $self->reaction_table()->get_row_by_key($reaction,"LOAD");
362 :     }
363 :    
364 :     =head3 get_reaction_id
365 :     Definition:
366 :     string = FIGMODELmodel->get_reaction_id(string::reaction ID);
367 :     Description:
368 :     Returns model reaction id
369 :     =cut
370 :     sub get_reaction_id {
371 :     my ($self,$reaction) = @_;
372 :    
373 :     my $Data = $self->get_reaction_data($reaction);
374 :     if (!defined($Data) || !defined($Data->{LOAD}->[0])) {
375 :     return undef;
376 :     }
377 :     return $Data->{LOAD}->[0];
378 :     }
379 :    
380 :     =head3 get_reaction_number
381 :     Definition:
382 :     int = FIGMODELmodel->get_reaction_number();
383 :     Description:
384 :     Returns the number of reactions in the model
385 :     =cut
386 :     sub get_reaction_number {
387 :     my ($self) = @_;
388 :    
389 :     if (!defined($self->reaction_table())) {
390 :     return 0;
391 :     }
392 :    
393 :     return $self->reaction_table()->size();
394 :     }
395 :    
396 :     =head3 reaction_table
397 :     Definition:
398 :     FIGMODELTable = FIGMODELmodel->reaction_table();
399 :     Description:
400 :     Returns FIGMODELTable with the reaction list for the model
401 :     =cut
402 :     sub reaction_table {
403 :     my ($self) = @_;
404 :    
405 :     if (!defined($self->{_reaction_data})) {
406 :     $self->{_reaction_data} = $self->figmodel()->database()->GetDBModel($self->id());
407 :     }
408 :    
409 :     return $self->{_reaction_data};
410 :     }
411 :    
412 :     =head3 reaction_class_table
413 :     Definition:
414 :     FIGMODELTable = FIGMODELmodel->reaction_class_table();
415 :     Description:
416 :     Returns FIGMODELTable with the reaction class data, and creates the table file if it does not exist
417 :     =cut
418 :     sub reaction_class_table {
419 :     my ($self) = @_;
420 :    
421 :     if (!defined($self->{_reaction_class_table})) {
422 :     if (-e $self->directory()."ReactionClassification-".$self->id().$self->selected_version().".tbl") {
423 :     $self->{_reaction_class_table} = $self->figmodel()->database()->load_table($self->directory()."ReactionClassification-".$self->id().$self->selected_version().".tbl",";","|",0,["REACTION","CLASS","MEDIA"]);
424 :     } else {
425 :     $self->{_reaction_class_table} = FIGMODELTable->new(["REACTION","MEDIA","CLASS","MIN","MAX"],$self->directory()."ReactionClassification-".$self->id().$self->selected_version().".tbl",["REACTION","CLASS","MEDIA"],";","|",undef);
426 :     }
427 :     }
428 :    
429 :     return $self->{_reaction_class_table};
430 :     }
431 :    
432 :     =head3 compound_class_table
433 :     Definition:
434 :     FIGMODELTable = FIGMODELmodel->compound_class_table();
435 :     Description:
436 :     Returns FIGMODELTable with the compound class data, and creates the table file if it does not exist
437 :     =cut
438 :     sub compound_class_table {
439 :     my ($self) = @_;
440 :    
441 :     if (!defined($self->{_compound_class_table})) {
442 :     if (-e $self->directory()."CompoundClassification-".$self->id().$self->selected_version().".tbl") {
443 :     $self->{_compound_class_table} = $self->figmodel()->database()->load_table($self->directory()."CompoundClassification-".$self->id().$self->selected_version().".tbl",";","|",0,["COMPOUND","CLASS","MEDIA"]);
444 :     } else {
445 :     $self->{_compound_class_table} = FIGMODELTable->new(["COMPOUND","MEDIA","CLASS","MIN","MAX"],$self->directory()."CompoundClassification-".$self->id().$self->selected_version().".tbl",["COMPOUND","CLASS","MEDIA"],";","|",undef);
446 :     }
447 :     }
448 :    
449 :     return $self->{_compound_class_table};
450 :     }
451 :    
452 : chenry 1.3 =head3 get_essential_genes
453 :     Definition:
454 :     [string::peg ID] = FIGMODELmodel->get_essential_genes(string::media condition);
455 :     Description:
456 :     Returns an reference to an array of the predicted essential genes during growth in the input media condition
457 :     =cut
458 :     sub get_essential_genes {
459 :     my ($self,$media) = @_;
460 :    
461 :     if (!defined($media)) {
462 :     $media = "Complete";
463 :     }
464 :     if (!defined($self->{_essential_genes}->{$media})) {
465 :     $self->{_essential_genes}->{$media} = undef;
466 :     if (-e $self->directory()."EssentialGenes-".$self->id().$self->selected_version()."-".$media.".tbl") {
467 :     $self->{_essential_genes}->{$media} = $self->figmodel()->database()->load_single_column_file($self->directory()."EssentialGenes-".$self->id().$self->selected_version()."-".$media.".tbl","");
468 :     }
469 :     }
470 :    
471 :     return $self->{_essential_genes}->{$media};
472 :     }
473 :    
474 : chenry 1.2 =head3 compound_table
475 :     Definition:
476 :     FIGMODELTable = FIGMODELmodel->compound_table();
477 :     Description:
478 :     Returns FIGMODELTable with the compound list for the model
479 :     =cut
480 :     sub compound_table {
481 :     my ($self) = @_;
482 :    
483 :     if (!defined($self->{_compound_table})) {
484 :     $self->{_compound_table} = $self->figmodel()->database()->GetDBModelCompounds($self->id());
485 :     }
486 :    
487 :     return $self->{_compound_table};
488 :     }
489 :    
490 :     =head3 get_compound_data
491 :     Definition:
492 :     string = FIGMODELmodel->get_compound_data(string::compound ID);
493 :     Description:
494 :     Returns model compound data
495 :     =cut
496 :     sub get_compound_data {
497 :     my ($self,$compound) = @_;
498 :    
499 :     if (!defined($self->compound_table())) {
500 :     return undef;
501 :     }
502 :     if ($compound =~ m/^\d+$/) {
503 :     $self->compound_table()->get_row($compound);
504 :     }
505 :     return $self->compound_table()->get_row_by_key($compound,"DATABASE");
506 :     }
507 :    
508 :     =head3 genome
509 :     Definition:
510 :     string = FIGMODELmodel->genome();
511 :     Description:
512 :     Returns model genome
513 :     =cut
514 :     sub genome {
515 :     my ($self) = @_;
516 :     return $self->{_data}->{genome}->[0];
517 :     }
518 :    
519 :     =head3 source
520 :     Definition:
521 :     string = FIGMODELmodel->source();
522 :     Description:
523 :     Returns model source
524 :     =cut
525 :     sub source {
526 :     my ($self) = @_;
527 :     return $self->{_data}->{source}->[0];
528 :     }
529 :    
530 :     =head3 rights
531 :     Definition:
532 :     1/0 = FIGMODELmodel->rights(string::username);
533 :     Description:
534 :     Returns 1 if the input user can view the model, and zero otherwise
535 :     =cut
536 :     sub rights {
537 :     my ($self,$username) = @_;
538 :    
539 :     if ($self->public()) {
540 :     return 1;
541 :     }
542 :     if (!defined($username) || $username eq "NONE") {
543 :     return 0;
544 :     }
545 :     if (!defined($self->{_userrights}->{$username})) {
546 :     if (defined($self->{_data}->{master})) {
547 :     for (my $i=0; $i < @{$self->{_data}->{master}};$i++) {
548 :     $self->{_userrights}->{$self->{_data}->{master}->[$i]} = 1;
549 :     }
550 :     }
551 :     if (defined($self->{_data}->{users})) {
552 :     for (my $i=0; $i < @{$self->{_data}->{users}};$i++) {
553 :     $self->{_userrights}->{$self->{_data}->{users}->[$i]} = 1;
554 :     }
555 :     }
556 :     }
557 :     return $self->{_userrights}->{$username};
558 :     }
559 :    
560 :     =head3 public
561 :     Definition:
562 :     1/0 = FIGMODELmodel->public();
563 :     Description:
564 :     Returns 1 if the model is public, and zero otherwise
565 :     =cut
566 :     sub public {
567 :     my ($self) = @_;
568 :    
569 :     if (!defined($self->{_public})) {
570 :     $self->{_public} = 0;
571 :     if (defined($self->{_data}->{users}->[0]) && $self->{_data}->{users}->[0] eq "all") {
572 :     $self->{_public} = 1;
573 :     }
574 :     }
575 :     return $self->{_public};
576 :     }
577 :    
578 :     =head3 directory
579 :     Definition:
580 :     string = FIGMODELmodel->directory();
581 :     Description:
582 :     Returns model directory
583 :     =cut
584 :     sub directory {
585 :     my ($self) = @_;
586 :    
587 :     if (!defined($self->{_directory})) {
588 :     my $userdirectory = "";
589 :     if ($self->owner() ne "master") {
590 :     $userdirectory = $self->owner()."/";
591 :     }
592 :     my $source = $self->source();
593 :     if ($source =~ /^MGRAST/) {
594 :     $self->{_directory} = $self->figmodel()->config("organism directory")->[0].$userdirectory.$self->genome()."/";
595 :     } elsif ($source =~ /^RAST/) {
596 :     $self->{_directory} = $self->figmodel()->config("organism directory")->[0].$userdirectory.$self->genome()."/";
597 :     } elsif ($source =~ /^SEED/) {
598 :     $self->{_directory} = $self->figmodel()->config("organism directory")->[0].$userdirectory.$self->genome()."/";
599 :     } elsif ($source =~ /^PM/) {
600 :     if (length($userdirectory) == 0) {
601 :     $self->{_directory} = $self->figmodel()->config("imported model directory")->[0].$self->id()."/";
602 :     } else {
603 :     $self->{_directory} = $self->figmodel()->config("organism directory")->[0].$userdirectory.$self->id()."/";
604 :     }
605 :     }
606 :     }
607 :    
608 :     return $self->{_directory};
609 :     }
610 :    
611 :     =head3 filename
612 :     Definition:
613 :     string = FIGMODELmodel->filename();
614 :     Description:
615 :     Returns model filename
616 :     =cut
617 :     sub filename {
618 :     my ($self) = @_;
619 :    
620 :     return $self->directory().$self->id().$self->selected_version().".txt";
621 :     }
622 :    
623 :     =head3 set_metagenome_stats
624 :     Definition:
625 :     string = FIGMODELmodel->set_metagenome_stats();
626 :     Description:
627 :     Sets the values of many model stats for a metagenome
628 :     =cut
629 :     sub set_metagenome_stats {
630 :     my ($self) = @_;
631 :    
632 :     $self->{_total_compounds} = 0;
633 :     if (defined($self->compound_table())) {
634 :     $self->{_total_compounds} = $self->compound_table()->size();
635 :     }
636 :     $self->{_gene_reactions} = 0;
637 :     $self->{_gapfilling_reactions} = 0;
638 :     $self->{_model_genes} = 0;
639 :     $self->{_total_reactions} = 0;
640 :     if (defined($self->reaction_table())) {
641 :     $self->{_total_reactions} = $self->reaction_table()->size();
642 :     my $tbl = $self->reaction_table();
643 :     my $spontaneous = 0;
644 :     for (my $i=0; $i < $tbl->size(); $i++) {
645 :     my $row = $tbl->get_row($i);
646 :     if (!defined($row->{"ASSOCIATED PEG"}->[0]) || $row->{"ASSOCIATED PEG"}->[0] !~ m/peg/) {
647 :     if ($row->{"ASSOCIATED PEG"}->[0] =~ m/SPONTANEOUS/) {
648 :     $spontaneous++;
649 :     } else {
650 :     $self->{_gapfilling_reactions}++;
651 :     }
652 :     } else {
653 :     for (my $j=0; $j < @{$row->{"CONFIDENCE"}}; $j++) {
654 :     my @ecores = split(/;/,$row->{"CONFIDENCE"}->[$j]);
655 :     $self->{_model_genes} += @ecores;
656 :     }
657 :     }
658 :     }
659 :     $self->{_gene_reactions} = $tbl->size() - $spontaneous - $self->{_gapfilling_reactions};
660 :     }
661 :     }
662 :    
663 :     =head3 version
664 :     Definition:
665 :     string = FIGMODELmodel->version();
666 :     Description:
667 :     Returns the version of the model
668 :     =cut
669 :     sub version {
670 :     my ($self) = @_;
671 :    
672 :     if (!defined($self->{_version})) {
673 :     if (!defined($self->{_selectedversion})) {
674 :     if (defined($self->stats())) {
675 :     $self->{_version} = "V".$self->stats()->{"Version"}->[0].".".$self->stats()->{"Gap fill version"}->[0];
676 :     }
677 :     } else {
678 :     $self->{_version} = $self->{_selectedversion};
679 :     }
680 :     }
681 :     return $self->{_version};
682 :     }
683 :    
684 :     =head3 selected_version
685 :     Definition:
686 :     string = FIGMODELmodel->selected_version();
687 :     Description:
688 :     Returns the selected version of the model
689 :     =cut
690 :     sub selected_version {
691 :     my ($self) = @_;
692 :    
693 :     if (!defined($self->{_selectedversion})) {
694 :     return "";
695 :     }
696 :     return $self->{_selectedversion};
697 :     }
698 :    
699 :     =head3 modification_time
700 :     Definition:
701 :     string = FIGMODELmodel->modification_time();
702 :     Description:
703 :     Returns the selected version of the model
704 :     =cut
705 :     sub modification_time {
706 :     my ($self) = @_;
707 :     if (!defined($self->{_modification_time})) {
708 :     my $stats = $self->stats();
709 :     if (defined($stats)) {
710 :     $self->{_modification_time} = 0;
711 :     if (defined($stats->{"Build date"}->[0]) && $self->{_modification_time} < $stats->{"Build date"}->[0]) {
712 :     $self->{_modification_time} = $stats->{"Build date"}->[0];
713 :     } elsif (defined($stats->{"Gap fill date"}->[0]) && $self->{_modification_time} < $stats->{"Gap fill date"}->[0]) {
714 :     $self->{_modification_time} = $stats->{"Gap fill date"}->[0];
715 :     }
716 :     } else {
717 : chenry 1.7 $self->{_modification_time} = $self->{_data}->{date}->[0];
718 : chenry 1.2 }
719 :     }
720 :     return $self->{_modification_time};
721 :     }
722 :    
723 :     =head3 gene_reactions
724 :     Definition:
725 :     string = FIGMODELmodel->gene_reactions();
726 :     Description:
727 :     Returns the number of reactions added by the gap filling
728 :     =cut
729 :     sub gene_reactions {
730 :     my ($self) = @_;
731 :    
732 :     if (!defined($self->{_gene_reactions})) {
733 :     if ($self->source() =~ /^MGRAST/) {
734 :     $self->set_metagenome_stats();
735 :     } elsif (defined($self->stats())) {
736 :     $self->{_gene_reactions} = $self->total_reactions() - $self->gapfilling_reactions() - $self->stats()->{'Spontaneous'}->[0] - $self->stats()->{'Growmatch reactions'}->[0] - $self->stats()->{'Biolog gap filling reactions'}->[0];
737 :     }
738 :     }
739 :     return $self->{_gene_reactions};
740 :     }
741 :    
742 :     =head3 total_compounds
743 :     Definition:
744 :     string = FIGMODELmodel->total_compounds();
745 :     Description:
746 :     Returns the number of compounds in the model
747 :     =cut
748 :     sub total_compounds {
749 :     my ($self) = @_;
750 :    
751 :     if (!defined($self->{_total_compounds})) {
752 :     if ($self->source() =~ /^MGRAST/) {
753 :     $self->set_metagenome_stats();
754 :     } elsif (defined($self->stats())) {
755 :     $self->{_total_compounds} = $self->stats()->{'Metabolites'}->[0];
756 :     }
757 :     }
758 :     return $self->{_total_compounds};
759 :     }
760 :    
761 :     =head3 gapfilling_reactions
762 :     Definition:
763 :     string = FIGMODELmodel->gapfilling_reactions();
764 :     Description:
765 :     Returns the number of reactions added by the gap filling
766 :     =cut
767 :     sub gapfilling_reactions {
768 :     my ($self) = @_;
769 :    
770 :     if (!defined($self->{_gapfilling_reactions})) {
771 :     if ($self->source() =~ /^MGRAST/) {
772 :     $self->set_metagenome_stats();
773 :     } elsif (defined($self->stats())) {
774 :     $self->{_gapfilling_reactions} = $self->stats()->{'Gap filling reactions'}->[0];
775 :     }
776 :     }
777 :     return $self->{_gapfilling_reactions};
778 :     }
779 :    
780 :     =head3 total_reactions
781 :     Definition:
782 :     string = FIGMODELmodel->total_reactions();
783 :     Description:
784 :     Returns the total number of reactions in the model
785 :     =cut
786 :     sub total_reactions {
787 :     my ($self) = @_;
788 :    
789 :     if (!defined($self->{_total_reactions})) {
790 :     if ($self->source() =~ /^MGRAST/) {
791 :     $self->set_metagenome_stats();
792 :     } elsif (defined($self->stats())) {
793 :     $self->{_total_reactions} = $self->stats()->{'Number of reactions'}->[0];
794 :     }
795 :     }
796 :     return $self->{_total_reactions};
797 :     }
798 :    
799 :     =head3 model_genes
800 :     Definition:
801 :     string = FIGMODELmodel->model_genes();
802 :     Description:
803 :     Returns the number of genes mapped to one or more reactions in the model
804 :     =cut
805 :     sub model_genes {
806 :     my ($self) = @_;
807 :    
808 :     if (!defined($self->{_model_genes})) {
809 :     if ($self->source() =~ /^MGRAST/) {
810 :     $self->set_metagenome_stats();
811 :     } elsif (defined($self->stats())) {
812 :     $self->{_model_genes} = $self->stats()->{'Genes with reactions'}->[0];
813 :     }
814 :     }
815 :     return $self->{_model_genes};
816 :     }
817 :    
818 :     =head3 class
819 :     Definition:
820 :     string = FIGMODELmodel->class();
821 :     Description:
822 :     Returns the class of the model: gram positive, gram negative, other
823 :     =cut
824 :     sub class {
825 :     my ($self) = @_;
826 :    
827 :     if (!defined($self->{_class})) {
828 :     if ($self->source() =~ /^MGRAST/) {
829 :     $self->{_class} = "Other";
830 :     } elsif (defined($self->stats())) {
831 :     $self->{_class} = $self->stats()->{Class}->[0];
832 :     }
833 :     }
834 :     return $self->{_class};
835 :     }
836 :    
837 :     =head3 taxonomy
838 :     Definition:
839 :     string = FIGMODELmodel->taxonomy();
840 :     Description:
841 :     Returns model taxonomy or biome if this is an metagenome model
842 :     =cut
843 :     sub taxonomy {
844 :     my ($self) = @_;
845 :    
846 :     if (!defined($self->{_taxonomy})) {
847 :     my $source = $self->source();
848 :     if ($source =~ /^MGRAST/) {
849 :     $self->{_taxonomy} = "NA";
850 :     my $mgmodeldata = $self->figmodel()->database()->mg_model_data($self->genome());
851 :     if (defined($mgmodeldata)) {
852 :     $self->{_taxonomy} = $mgmodeldata->biome();
853 :     }
854 :     } else {
855 :     $self->{_taxonomy} = $self->fig()->taxonomy_of($self->genome());
856 :     }
857 :     }
858 :    
859 :     return $self->{_taxonomy};
860 :     }
861 :    
862 :     =head3 genome_size
863 :     Definition:
864 :     string = FIGMODELmodel->genome_size();
865 :     Description:
866 :     Returns size of the modeled genome in KB
867 :     =cut
868 :     sub genome_size {
869 :     my ($self) = @_;
870 :    
871 :     if (!defined($self->{_genome_size})) {
872 :     my $source = $self->source();
873 :     if ($source =~ /^MGRAST/) {
874 :     $self->{_genome_size} = "NA";
875 :     if (defined($self->mgdata())) {
876 :     $self->{_genome_size} = $self->mgdata()->size;
877 :     }
878 :     } else {
879 :     $self->{_genome_size} = $self->fig()->genome_szdna($self->genome());
880 :     }
881 :     }
882 :    
883 :     return $self->{_genome_size};
884 :     }
885 :    
886 :     =head3 genome_genes
887 :     Definition:
888 :     string = FIGMODELmodel->genome_genes();
889 :     Description:
890 :     Returns the number of genes in the modeled genome
891 :     =cut
892 :     sub genome_genes {
893 :     my ($self) = @_;
894 :    
895 :     if (!defined($self->{_genome_genes})) {
896 :     my $source = $self->source();
897 :     if ($source =~ /^MGRAST/) {
898 :     $self->{_genome_genes} = "NA";
899 :     if (defined($self->mgdata())) {
900 :     $self->{_genome_genes} = $self->mgdata()->genome_contig_count;
901 :     }
902 :     } elsif (defined($self->stats())) {
903 :     $self->{_genome_genes} = $self->stats()->{'Total genes'}->[0];
904 :     }
905 :     }
906 :    
907 :     return $self->{_genome_genes};
908 :     }
909 :    
910 :     =head3 run_default_model_predictions
911 :     Definition:
912 :     0/1::status = FIGMODELmodel->run_default_model_predictions(string::media ID);
913 :     Description:
914 :     =cut
915 :     sub run_default_model_predictions {
916 :     my ($self,$Media) = @_;
917 :    
918 :     #Assuming complete media if none is provided
919 :     if (!defined($Media)) {
920 :     $Media = "Complete";
921 :     }
922 :    
923 :     #Predicting essentiality
924 :     my $result = $self->figmodel()->RunFBASimulation($self->id(),"SINGLEKO",undef,undef,[$self->id()],[$Media]);
925 :     #Checking that the table is defined and the output file exists
926 :     if (defined($result) && defined($result->get_row(0)->{"ESSENTIALGENES"})) {
927 :     $self->figmodel()->database()->print_array_to_file($self->directory()."EssentialGenes-".$self->id()."-".$Media.".tbl",[join("\n",@{$result->get_row(0)->{"ESSENTIALGENES"}})]);
928 : chenry 1.3 } else {
929 :     $self->figmodel()->error_message("FIGMODELmodel:run_default_model_predictions:could not identify essential reactions for model ".$self->id().$self->selected_version().".");
930 :     return $self->figmodel()->fail();
931 : chenry 1.2 }
932 :    
933 :     #Classifying reactions and compounds
934 :     my $tbl = $self->classify_model_reactions($Media);
935 : chenry 1.3 if (!defined($tbl)) {
936 :     $self->figmodel()->error_message("FIGMODELmodel:run_default_model_predictions:could not classify reactions for model ".$self->id().$self->selected_version().".");
937 :     return $self->figmodel()->fail();
938 :     }
939 : chenry 1.2 $tbl->save();
940 :    
941 :     return $self->figmodel()->success();
942 :     }
943 :    
944 :     =head3 save_obsolete_stats
945 :     Definition:
946 :     FIGMODELmodel->save_obsolete_stats();
947 :     Description:
948 :     =cut
949 :     sub save_obsolete_stats {
950 :     my ($self) = @_;
951 :    
952 :     #checking if stats exists
953 :     my $stats = $self->stats();
954 :     if (defined($stats)) {
955 :     $stats->{"Model ID"}->[0] = $self->id()."V".$stats->{"Version"}->[0].".".$stats->{"Gap fill version"}->[0];
956 :     $self->figmodel()->database()->update_row("OBSOLETE MODEL STATS",$stats,"Model ID");
957 :     $stats->{"Model ID"}->[0] = $self->id();
958 :     }
959 :     }
960 :    
961 :     =head3 update_stats_for_gap_filling
962 :     Definition:
963 :     {string => [string]} = FIGMODELmodel->update_stats_for_gap_filling(int::gapfill time);
964 :     Description:
965 :     =cut
966 :     sub update_stats_for_gap_filling {
967 :     my ($self,$gapfilltime) = @_;
968 :    
969 :     #preserving the stats for the now obselete model
970 :     $self->save_obsolete_stats();
971 :     my $stats = $self->update_model_stats(0);
972 :     $stats->{"Gap filling time"}->[0] = $gapfilltime;
973 :     $stats->{"Gap fill date"}->[0] = time();
974 :     if (!defined($stats->{"Gap fill version"}->[0])) {
975 :     $stats->{"Gap fill version"}->[0] = 0;
976 :     }
977 :     $stats->{"Gap fill version"}->[0]++;
978 :    
979 :     #Updating the stats stored in the table
980 :     $self->figmodel()->database()->update_row("MODEL STATS",$stats,"Model ID");
981 :     return $stats;
982 :     }
983 :    
984 :     =head3 update_stats_for_build
985 :     Definition:
986 :     {string => [string]} = FIGMODELmodel->update_stats_for_build();
987 :     Description:
988 :     =cut
989 :     sub update_stats_for_build {
990 :     my ($self) = @_;
991 :    
992 :     #preserving the stats for the now obselete model
993 :     $self->save_obsolete_stats();
994 :     my $stats = $self->update_model_stats(0);
995 :     $stats->{"Build date"}->[0] = time();
996 :     if (!defined($stats->{"Version"}->[0])) {
997 :     $stats->{"Version"}->[0] = 0;
998 :     }
999 :     $stats->{"Version"}->[0]++;
1000 :    
1001 :     #Updating the stats stored in the table
1002 :     $self->figmodel()->database()->update_row("MODEL STATS",$stats,"Model ID");
1003 :     return $stats;
1004 :     }
1005 :    
1006 :     =head3 update_model_stats
1007 :     Definition:
1008 :     FIGMODELmodel->update_model_stats();
1009 :     Description:
1010 :     =cut
1011 :     sub update_model_stats {
1012 :     my ($self) = @_;
1013 :    
1014 :     #Getting reaction table
1015 :     my $rxntbl = $self->reaction_table();
1016 :     if (!defined($rxntbl)) {
1017 :     $self->figmodel()->error_message("FIGMODELmodel:update_model_stats:Could not load reaction list for ".$self->id());
1018 :     return undef;
1019 :     }
1020 :     my $cpdtbl = $self->compound_table();
1021 :    
1022 :     #Creating empty status row
1023 :     my $CurrentStats = {"Genes with reactions" => [0],
1024 :     "Metabolites" => [$cpdtbl->size()],
1025 :     "Growmatch reactions" => [0],
1026 :     "Spontaneous" => [0],
1027 :     "Biolog gap filling reactions" => [0],
1028 :     "Number of reactions" => [$rxntbl->size()],
1029 :     "Transport reaction"=>[0],
1030 :     "Gap filling reactions" => [0],
1031 :     "Model ID" => [$self->id()],
1032 :     "Subsystem genes with reactions" => [0],
1033 :     "Nonsubsystem genes with reactions" => [0],
1034 :     Version => [0],
1035 :     "Gap fill version" => [0],
1036 :     Source => [$self->source()],
1037 :     "Genes with one reaction" => [0],
1038 :     "Genome ID" => [$self->genome()]};
1039 :    
1040 :     my $genomestats = $self->figmodel()->get_genome_stats($self->genome());
1041 :     if (defined($genomestats)) {
1042 :     $CurrentStats->{SOURCE}->[0] = $genomestats->{SOURCE}->[0];
1043 :     $CurrentStats->{"Organism name"}->[0] = $genomestats->{NAME}->[0];
1044 :     $CurrentStats->{"Total genes"}->[0] = $genomestats->{"TOTAL GENES"}->[0];
1045 :     $CurrentStats->{"Gram positive genes"}->[0] = $genomestats->{"GRAM POSITIVE GENES"}->[0];
1046 :     $CurrentStats->{"Gram negative genes"}->[0] = $genomestats->{"GRAM NEGATIVE GENES"}->[0];
1047 :     $CurrentStats->{"Class"}->[0] = $genomestats->{CLASS}->[0];
1048 :     $CurrentStats->{"Genes with functions"}->[0] = $genomestats->{"GENES WITH FUNCTIONS"}->[0];
1049 :     $CurrentStats->{"Subsystem genes"}->[0] = $genomestats->{"SUBSYSTEM GENES"}->[0];
1050 :     $CurrentStats->{"Nonsubsystem genes"}->[0] = $genomestats->{"NON SUBSYSTEM GENES"}->[0];
1051 :     if (defined($genomestats->{TAXONOMY})) {
1052 :     $CurrentStats->{"Taxonomy 0"}->[0] = $genomestats->{TAXONOMY}->[0];
1053 :     $CurrentStats->{"Taxonomy 1"}->[0] = $genomestats->{TAXONOMY}->[1];
1054 :     $CurrentStats->{"Taxonomy 2"}->[0] = $genomestats->{TAXONOMY}->[2];
1055 :     $CurrentStats->{"Taxonomy 3"}->[0] = $genomestats->{TAXONOMY}->[3];
1056 :     $CurrentStats->{"Taxonomy 4"}->[0] = $genomestats->{TAXONOMY}->[4];
1057 :     $CurrentStats->{"Taxonomy 5"}->[0] = $genomestats->{TAXONOMY}->[5];
1058 :     }
1059 :     }
1060 :    
1061 :     #Transfering build, version, and gap fill data from existing stats
1062 :     if (defined($self->stats())) {
1063 :     $CurrentStats->{Version}->[0] = $self->stats()->{Version}->[0];
1064 :     $CurrentStats->{"Gap fill version"}->[0] = $self->stats()->{"Gap fill version"}->[0];
1065 :     $CurrentStats->{"Gap filling time"}->[0] = $self->stats()->{"Gap filling time"}->[0];
1066 :     $CurrentStats->{"Gap fill date"}->[0] = $self->stats()->{"Gap fill date"}->[0];
1067 :     $CurrentStats->{"Build date"}->[0] = $self->stats()->{"Build date"}->[0];
1068 :     }
1069 :    
1070 :     my %GeneHash;
1071 :     my %NonpegHash;
1072 :     my %CompoundHash;
1073 :     for (my $i=0; $i < $rxntbl->size(); $i++) {
1074 :     my $Row = $rxntbl->get_row($i);
1075 :     if (defined($Row) && defined($Row->{"ASSOCIATED PEG"})) {
1076 :     my $ReactionRow = $self->figmodel()->get_reaction($Row->{"LOAD"}->[0]);
1077 :     if (defined($ReactionRow->{"EQUATION"}->[0])) {
1078 :     #Checking for extracellular metabolites which indicate that this is a transporter
1079 :     if ($ReactionRow->{"EQUATION"}->[0] =~ m/\[e\]/) {
1080 :     $CurrentStats->{"Transport reaction"}->[0]++;
1081 :     }
1082 :     }
1083 :     #Identifying spontaneous/biolog/gapfilling/gene associated reactions
1084 :     if ($Row->{"ASSOCIATED PEG"}->[0] =~ m/BIOLOG/i) {
1085 :     $CurrentStats->{"Biolog gap filling reactions"}->[0]++;
1086 :     } elsif ($Row->{"ASSOCIATED PEG"}->[0] =~ m/SPONTANEOUS/i) {
1087 :     $CurrentStats->{"Spontaneous"}->[0]++;
1088 :     } elsif ($Row->{"ASSOCIATED PEG"}->[0] =~ m/GAP/ || $Row->{"ASSOCIATED PEG"}->[0] =~ m/UNIVERSAL/i || $Row->{"ASSOCIATED PEG"}->[0] =~ m/UNKNOWN/i) {
1089 :     $CurrentStats->{"Gap filling reactions"}->[0]++;
1090 :     } else {
1091 :     foreach my $GeneSet (@{$Row->{"ASSOCIATED PEG"}}) {
1092 : chenry 1.5 $_ = $GeneSet;
1093 :     my @GeneList = /(peg\.\d+)/g;
1094 : chenry 1.2 foreach my $Gene (@GeneList) {
1095 :     if ($Gene =~ m/(peg\.\d+)/) {
1096 :     $GeneHash{$1} = 1;
1097 :     } else {
1098 :     $NonpegHash{$Gene} = 1;
1099 :     }
1100 :     }
1101 :     }
1102 :     }
1103 :     }
1104 :     }
1105 :     my @genes = keys(%GeneHash);
1106 :     my @othergenes = keys(%NonpegHash);
1107 :     $CurrentStats->{"Genes with reactions"}->[0] = @genes + @othergenes;
1108 :    
1109 :     #Updating the stats stored in the table
1110 :     $self->figmodel()->database()->update_row("MODEL STATS",$CurrentStats,"Model ID");
1111 :     $self->{_stats} = $CurrentStats;
1112 :     return $CurrentStats;
1113 :     }
1114 :    
1115 :     =head3 GapFillModel
1116 :     Definition:
1117 :     (success/fail) = FIGMODELmodel->GapFillModel();
1118 :     Description:
1119 :     This function performs an optimization to identify the minimal set of reactions that must be added to a model in order for biomass to be produced by the biomass reaction in the model.
1120 :     Before running the gap filling, the existing model is backup in the same directory with the current version numbers appended.
1121 :     If the model has been gap filled previously, the previous gap filling reactions are removed prior to running the gap filling again.
1122 :     =cut
1123 :     sub GapFillModel {
1124 :     my ($self,$donotclear) = @_;
1125 :    
1126 :     #Setting status of model to gap filling
1127 :     my $OrganismID = $self->genome();
1128 :     $self->set_status(1,"Auto completion running");
1129 :     my $UniqueFilename = $self->figmodel()->filename();
1130 :     my $StartTime = time();
1131 :    
1132 :     #Archiving the existing model
1133 :     $self->ArchiveModel();
1134 :    
1135 :     #Removing any gapfilling reactions that may be currently present in the model
1136 :     if (!defined($donotclear) || $donotclear != 1) {
1137 :     my $ModelTable = $self->reaction_table();
1138 :     for (my $i=0; $i < $ModelTable->size(); $i++) {
1139 :     if (!defined($ModelTable->get_row($i)->{"ASSOCIATED PEG"}->[0]) || $ModelTable->get_row($i)->{"ASSOCIATED PEG"}->[0] eq "GAP FILLING" || $ModelTable->get_row($i)->{"ASSOCIATED PEG"}->[0] =~ m/BIOLOG/ || $ModelTable->get_row($i)->{"ASSOCIATED PEG"}->[0] =~ m/GROWMATCH/) {
1140 :     $ModelTable->delete_row($i);
1141 :     $i--;
1142 :     }
1143 :     }
1144 :     $ModelTable->save();
1145 :     }
1146 :    
1147 :     #Calling the MFAToolkit to run the gap filling optimization
1148 :     my $MinimalMediaTable = $self->figmodel()->database()->GetDBTable("MINIMAL MEDIA TABLE");
1149 :     my $Media = "Complete";
1150 :     if (defined($MinimalMediaTable->get_row_by_key($self->genome(),"Organism"))) {
1151 :     $Media = $MinimalMediaTable->get_row_by_key($self->genome(),"Organism")->{"Minimal media"}->[0];
1152 :     #Loading media, changing bounds, saving media as a test media
1153 :     my $MediaTable = FIGMODELTable::load_table($self->config("Media directory")->[0].$Media.".txt",";","",0,["VarName"]);
1154 :     for (my $i=0; $i < $MediaTable->size(); $i++) {
1155 :     if ($MediaTable->get_row($i)->{"Min"}->[0] < 0) {
1156 :     $MediaTable->get_row($i)->{"Min"}->[0] = -10000;
1157 :     }
1158 :     if ($MediaTable->get_row($i)->{"Max"}->[0] > 0) {
1159 :     $MediaTable->get_row($i)->{"Max"}->[0] = 10000;
1160 :     }
1161 :     }
1162 :     $MediaTable->save($self->config("Media directory")->[0].$UniqueFilename."TestMedia.txt");
1163 :     system($self->figmodel()->GenerateMFAToolkitCommandLineCall($UniqueFilename,$self->id(),$UniqueFilename."TestMedia",["GapFilling"],{"Default max drain flux" => 0},"GapFill".$self->id().".log",undef));
1164 :     unlink($self->config("Media directory")->[0].$UniqueFilename."TestMedia.txt");
1165 :     } else {
1166 :     system($self->figmodel()->GenerateMFAToolkitCommandLineCall($UniqueFilename,$self->id(),undef,["GapFilling"],undef,"GapFill".$self->id().".log",undef));
1167 :     }
1168 :    
1169 :     #Parse the solutions file for the model and get the reaction list from it
1170 :     my $SolutionData = $self->figmodel()->LoadProblemReport($UniqueFilename);
1171 :     #Clearing the mfatoolkit output and log file
1172 :     $self->figmodel()->clearing_output($UniqueFilename,"GapFill".$self->id().".log");
1173 :     if (!defined($SolutionData) || $SolutionData->size() == 0) {
1174 :     $self->set_status(1,"Auto completion failed: auto completion solution not found");
1175 :     $self->figmodel()->error_message("FIGMODEL:GapFillModel: Gap filling report not found!");
1176 :     return $self->fail();
1177 :     }
1178 :     $SolutionData->save("/home/chenry/Solution.txt");
1179 :    
1180 :     #Looking for the last printed recursive MILP solution
1181 :     for (my $i=($SolutionData->size()-1); $i >=0; $i--) {
1182 :     if (defined($SolutionData->get_row($i)->{"Notes"}) && $SolutionData->get_row($i)->{"Notes"}->[0] =~ m/^Recursive/) {
1183 :     my $AllSolutions = substr($SolutionData->get_row($i)->{"Notes"}->[0],15);
1184 :     my @TempThree = split(/\|/,$AllSolutions);
1185 :     if (@TempThree > 0 && $TempThree[0] =~ m/.+:(.+)/) {
1186 :     my @TempFour = split(/,/,$1);
1187 :     my $DirectionList;
1188 :     my $ReactionList;
1189 :     for (my $j=0; $j < @TempFour; $j++) {
1190 :     my $ID = "";
1191 :     my $Sign = "=>";
1192 :     if ($TempFour[$j] =~ m/\-(rxn\d\d\d\d\d)/) {
1193 :     $ID = $1;
1194 :     $Sign = "<=";
1195 :     } elsif ($TempFour[$j] =~ m/\+(rxn\d\d\d\d\d)/) {
1196 :     $ID = $1;
1197 :     }
1198 :     if ($ID ne "") {
1199 :     if ($self->figmodel()->reversibility_of_reaction($ID) ne $Sign) {
1200 :     $Sign = "<=>";
1201 :     }
1202 :     push(@{$DirectionList},$Sign);
1203 :     push(@{$ReactionList},$ID);
1204 :     }
1205 :     }
1206 :     $self->figmodel()->IntegrateGrowMatchSolution($self->id(),undef,$ReactionList,$DirectionList,"GAP FILLING",0,1);
1207 :     }
1208 :     last;
1209 :     }
1210 :     }
1211 :     #Updating model stats with gap filling results
1212 :     my $ElapsedTime = time() - $StartTime;
1213 : chenry 1.7 $self->figmodel()->database()->ClearDBModel($self->id(),1);
1214 : chenry 1.2 #Determining why each gap filling reaction was added
1215 :     $self->figmodel()->IdentifyDependancyOfGapFillingReactions($self->id(),$Media);
1216 :     if (!defined($donotclear) || $donotclear != 1) {
1217 :     $self->figmodel()->AddBiologTransporters($self->id());
1218 :     if ($self->id() !~ m/MGRast/) {
1219 :     $self->update_stats_for_gap_filling($ElapsedTime);
1220 :     }
1221 :     } else {
1222 :     $self->update_model_stats();
1223 :     }
1224 :     #Printing the updated SBML file
1225 :     $self->PrintSBMLFile();
1226 :     $self->PrintModelLPFile($self->id());
1227 :     $self->set_status(1,"Auto completion successfully finished");
1228 :     $self->run_default_model_predictions();
1229 :    
1230 :     return $self->success();
1231 :     }
1232 :    
1233 :     =head3 datagapfill
1234 :     Definition:
1235 :     success()/fail() = FIGMODELmodel->datagapfill();
1236 :     Description:
1237 :     Run gapfilling on the input run specifications
1238 :     =cut
1239 :     sub datagapfill {
1240 :     my ($self,$GapFillingRunSpecs,$TansferFileSuffix) = @_;
1241 :     my $UniqueFilename = $self->figmodel()->filename();
1242 :     if (defined($GapFillingRunSpecs) && @{$GapFillingRunSpecs} > 0) {
1243 :     system($self->figmodel()->GenerateMFAToolkitCommandLineCall($UniqueFilename,$self->id().$self->selected_version(),"NoBounds",["DataGapFilling"],{"Reactions to knockout" => $self->config("permanently knocked out reactions")->[0],"Gap filling runs" => join(";",@{$GapFillingRunSpecs})},"GapFilling-".$self->id().$self->selected_version()."-".$UniqueFilename.".log",undef,undef));
1244 :     #Checking that the solution exists
1245 :     if (!-e $self->config("MFAToolkit output directory")->[0].$UniqueFilename."/GapFillingSolutionTable.txt") {
1246 :     $self->figmodel()->error_message("FIGMODEL:GapFillingAlgorithm: Could not find MFA output file!");
1247 :     $self->figmodel()->database()->print_array_to_file($self->directory().$self->id().$self->selected_version()."-GFS.txt",["Experiment;Solution index;Solution cost;Solution reactions"]);
1248 :     return undef;
1249 :     }
1250 :     my $GapFillResultTable = $self->figmodel()->database()->load_table($self->config("MFAToolkit output directory")->[0].$UniqueFilename."/GapFillingSolutionTable.txt",";","",0,undef);
1251 :     if (defined($TansferFileSuffix)) {
1252 :     system("cp ".$self->config("MFAToolkit output directory")->[0].$UniqueFilename."/GapFillingSolutionTable.txt ".$self->directory().$self->id().$self->selected_version()."-".$TansferFileSuffix.".txt");
1253 :     }
1254 :     #If the system is not configured to preserve all logfiles, then the mfatoolkit output folder is deleted
1255 :     $self->figmodel()->clearing_output($UniqueFilename,"GapFilling-".$self->id().$self->selected_version()."-".$UniqueFilename.".log");
1256 : chenry 1.5 return $GapFillResultTable;
1257 : chenry 1.2 }
1258 :     if (defined($TansferFileSuffix)) {
1259 :     $self->figmodel()->database()->print_array_to_file($self->directory().$self->id().$self->selected_version()."-".$TansferFileSuffix.".txt",["Experiment;Solution index;Solution cost;Solution reactions"]);
1260 :     }
1261 :     return undef;
1262 :     }
1263 :    
1264 : chenry 1.4 =head3 datagapgen
1265 :     Definition:
1266 :     $model->datagapgen($NumberOfProcessors,$ProcessorIndex,$Filename);
1267 :     =cut
1268 :    
1269 :     sub datagapgen {
1270 :     my ($self,$Media,$KOList,$NoKOList,$suffix) = @_;
1271 :     #Setting default values for inputs
1272 :     if (!defined($NoKOList)) {
1273 :     $NoKOList = "none";
1274 :     }
1275 :     if (!defined($KOList)) {
1276 :     $KOList = "none";
1277 :     }
1278 :     #Getting unique filename
1279 :     my $Filename = $self->figmodel()->filename();
1280 :     if (!defined($suffix)) {
1281 :     $suffix = "-".$Media."-".$KOList."-S";
1282 :     }
1283 :     $KOList =~ s/,/;/g;
1284 :     $NoKOList =~ s/,/;/g;
1285 :     #Running the gap generation
1286 :     system($self->figmodel()->GenerateMFAToolkitCommandLineCall($Filename,$self->id().$self->selected_version(),$Media,["GapGeneration"],{"Reactions that should always be active" => $NoKOList,"Reactions to knockout" => $KOList,"Reactions that are always blocked" => "none"},"Gapgeneration-".$self->id().$self->selected_version()."-".$Filename.".log",undef,undef));
1287 :     my $ProblemReport = $self->figmodel()->LoadProblemReport($Filename);
1288 :     if (!defined($ProblemReport)) {
1289 :     $self->figmodel()->error_message("FIGMODEL:GapGenerationAlgorithm;No problem report;".$Filename.";".$self->id().$self->selected_version().";".$Media.";".$KOList.";".$NoKOList);
1290 :     return undef;
1291 :     }
1292 :     #Clearing the output folder and log file
1293 :     $self->figmodel()->clearing_output($Filename,$self->directory()."Gapgeneration-".$self->id().$self->selected_version()."-".$Filename.".log");
1294 :     #Scheduling the testing of this gap filling solution
1295 :     my $Tbl = FIGMODELTable->new(["Experiment","Solution index","Solution cost","Solution reactions"],$self->directory().$self->id().$self->selected_version()."-GG".$suffix.".txt",undef,";","|",undef);
1296 :     for (my $j=0; $j < $ProblemReport->size(); $j++) {
1297 :     if ($ProblemReport->get_row($j)->{"Notes"}->[0] =~ m/^Recursive\sMILP\s([^)]+)/) {
1298 :     my @SolutionList = split(/\|/,$1);
1299 :     for (my $k=0; $k < @SolutionList; $k++) {
1300 :     if ($SolutionList[$k] =~ m/(\d+):(.+)/) {
1301 :     $Tbl->add_row({"Experiment"=>[$Media],"Solution index"=>[$k],"Solution cost"=>[$1],"Solution reactions"=>[$2]});
1302 :     }
1303 :     }
1304 :     }
1305 :     }
1306 :     return $Tbl;
1307 :     }
1308 :    
1309 : chenry 1.2 =head3 TestSolutions
1310 :     Definition:
1311 :     $model->TestSolutions($ModelID,$NumProcessors,$ProcessorIndex,$GapFill);
1312 :     Description:
1313 :     Example:
1314 :     =cut
1315 :    
1316 :     sub TestSolutions {
1317 :     my ($self,$OriginalErrorFilename,$GapFillResultTable) = @_;
1318 :     #Getting the filename
1319 :     my $UniqueFilename = $self->figmodel()->filename();
1320 :     #Reading in the original error matrix which has the headings for the original model simulation
1321 :     my $OriginalErrorData;
1322 :     if (!defined($OriginalErrorFilename) || !-e $self->directory().$OriginalErrorFilename) {
1323 :     my ($FalsePostives,$FalseNegatives,$CorrectNegatives,$CorrectPositives,$Errorvector,$HeadingVector) = $self->RunAllStudiesWithDataFast("All");
1324 :     $OriginalErrorData = [$HeadingVector,$Errorvector];
1325 :     } else {
1326 :     $OriginalErrorData = $self->figmodel()->database()->load_single_column_file($self->directory().$OriginalErrorFilename,"");
1327 :     }
1328 :     my $HeadingHash;
1329 :     my @HeadingArray = split(/;/,$OriginalErrorData->[0]);
1330 :     my @OrigErrorArray = split(/;/,$OriginalErrorData->[1]);
1331 :     for (my $i=0; $i < @HeadingArray; $i++) {
1332 :     my @SubArray = split(/:/,$HeadingArray[$i]);
1333 :     $HeadingHash->{$SubArray[0].":".$SubArray[1].":".$SubArray[2]} = $i;
1334 :     }
1335 :     #Scanning through the gap filling solutions
1336 :     my $TempVersion = "V".$UniqueFilename;
1337 :     my $ErrorMatrixLines;
1338 :     for (my $i=0; $i < $GapFillResultTable->size(); $i++) {
1339 :     print "Starting problem solving ".$i."\n";
1340 :     my $ErrorLine = $GapFillResultTable->get_row($i)->{"Experiment"}->[0].";".$GapFillResultTable->get_row($i)->{"Solution index"}->[0].";".$GapFillResultTable->get_row($i)->{"Solution cost"}->[0].";".$GapFillResultTable->get_row($i)->{"Solution reactions"}->[0];
1341 :     #Integrating solution into test model
1342 :     my $ReactionArray;
1343 :     my $DirectionArray;
1344 :     my @ReactionList = split(/,/,$GapFillResultTable->get_row($i)->{"Solution reactions"}->[0]);
1345 :     my %SolutionHash;
1346 :     for (my $k=0; $k < @ReactionList; $k++) {
1347 :     if ($ReactionList[$k] =~ m/(.+)(rxn\d\d\d\d\d)/) {
1348 :     my $Reaction = $2;
1349 :     my $Sign = $1;
1350 :     if (defined($SolutionHash{$Reaction})) {
1351 :     $SolutionHash{$Reaction} = "<=>";
1352 :     } elsif ($Sign eq "-") {
1353 :     $SolutionHash{$Reaction} = "<=";
1354 :     } elsif ($Sign eq "+") {
1355 :     $SolutionHash{$Reaction} = "=>";
1356 :     } else {
1357 :     $SolutionHash{$Reaction} = $Sign;
1358 :     }
1359 :     }
1360 :     }
1361 :     @ReactionList = keys(%SolutionHash);
1362 :     for (my $k=0; $k < @ReactionList; $k++) {
1363 :     push(@{$ReactionArray},$ReactionList[$k]);
1364 :     push(@{$DirectionArray},$SolutionHash{$ReactionList[$k]});
1365 :     }
1366 :     print "Integrating solution!\n";
1367 : chenry 1.5 $self->figmodel()->IntegrateGrowMatchSolution($self->id().$self->selected_version(),$self->directory().$self->id().$TempVersion.".txt",$ReactionArray,$DirectionArray,"Gapfilling ".$GapFillResultTable->get_row($i)->{"Experiment"}->[0],1,1);
1368 :     $self->PrintModelLPFile();
1369 : chenry 1.2 #Running the model against all available experimental data
1370 :     print "Running test model!\n";
1371 :     my ($FalsePostives,$FalseNegatives,$CorrectNegatives,$CorrectPositives,$Errorvector,$HeadingVector) = $self->RunAllStudiesWithDataFast("All");
1372 :    
1373 :     @HeadingArray = split(/;/,$HeadingVector);
1374 :     my @ErrorArray = @OrigErrorArray;
1375 :     my @TempArray = split(/;/,$Errorvector);
1376 :     for (my $j=0; $j < @HeadingArray; $j++) {
1377 :     my @SubArray = split(/:/,$HeadingArray[$j]);
1378 :     $ErrorArray[$HeadingHash->{$SubArray[0].":".$SubArray[1].":".$SubArray[2]}] = $TempArray[$j];
1379 :     }
1380 :     $ErrorLine .= ";".$FalsePostives."/".$FalseNegatives.";".join(";",@ErrorArray);
1381 :     push(@{$ErrorMatrixLines},$ErrorLine);
1382 :     print "Finishing problem solving ".$i."\n";
1383 :     }
1384 :     #Clearing out the test model
1385 :     if (-e $self->directory().$self->id().$TempVersion.".txt") {
1386 :     unlink($self->directory().$self->id().$TempVersion.".txt");
1387 :     unlink($self->directory()."SimulationOutput".$self->id().$TempVersion.".txt");
1388 :     }
1389 :     return $ErrorMatrixLines;
1390 :     }
1391 :    
1392 :     =head3 status
1393 :     Definition:
1394 :     int::model status = FIGMODELmodel->status();
1395 :     Description:
1396 :     Returns the current status of the SEED model associated with the input genome ID.
1397 :     model status = 1: model exists
1398 :     model status = 0: model is being built
1399 :     model status = -1: model does not exist
1400 :     model status = -2: model build failed
1401 :     =cut
1402 :     sub status {
1403 :     my ($self) = @_;
1404 :     return $self->{_data}->{status}->[0];
1405 :     }
1406 :    
1407 :     =head3 message
1408 :     Definition:
1409 :     string::model message = FIGMODELmodel->message();
1410 :     Description:
1411 :     Returns a message associated with the models current status
1412 :     =cut
1413 :     sub message {
1414 :     my ($self) = @_;
1415 :     return $self->{_data}->{message}->[0];
1416 :     }
1417 :    
1418 :     =head3 set_status
1419 :     Definition:
1420 :     (success/fail) = FIGMODELmodel->set_status(int::new status,string::status message);
1421 :     Description:
1422 :     Changes the current status of the SEED model
1423 :     new status = 1: model exists
1424 :     new status = 0: model is being built
1425 :     new status = -1: model does not exist
1426 :     new status = -2: model build failed
1427 :     =cut
1428 :     sub set_status {
1429 :     my ($self,$NewStatus,$Message) = @_;
1430 :    
1431 :     #Getting the model row from the MODELS table
1432 :     $self->{_data}->{status}->[0] = $NewStatus;
1433 :     $self->{_data}->{message}->[0] = $Message;
1434 :     $self->figmodel()->database()->update_row("MODELS",$self->{_data},"id");
1435 :     return $self->config("SUCCESS")->[0];
1436 :     }
1437 :    
1438 :     =head3 CreateSingleGenomeReactionList
1439 :     Definition:
1440 :     FIGMODEL->CreateSingleGenomeReactionList();
1441 :     Description:
1442 :     This function uses fig calls to obtain a list of genes and functions for a genome, and it uses a file mapping reactions and functional roles to produce a reaction list.
1443 :     Example:
1444 :     =cut
1445 :    
1446 :     sub CreateSingleGenomeReactionList {
1447 :     my ($self,$RunGapFilling) = @_;
1448 :    
1449 : chenry 1.5 #Creating directory
1450 :     if ($self->owner() ne "master" && !-d $self->figmodel()->config("organism directory")->[0].$self->owner()."/") {
1451 :     system("mkdir ".$self->figmodel()->config("organism directory")->[0].$self->owner()."/");
1452 :     } elsif ($self->owner() eq "master" && !-d $self->figmodel()->config("organism directory")->[0].$self->genome()."/") {
1453 :     system("mkdir ".$self->figmodel()->config("organism directory")->[0].$self->genome()."/");
1454 :     }
1455 :     if ($self->owner() ne "master" && !-d $self->figmodel()->config("organism directory")->[0].$self->owner()."/".$self->genome()."/") {
1456 :     system("mkdir ".$self->figmodel()->config("organism directory")->[0].$self->owner()."/".$self->genome()."/");
1457 :     }
1458 :    
1459 : chenry 1.2 #Getting genome stats
1460 :     my $genomestats = $self->figmodel()->get_genome_stats($self->genome());
1461 :     my $FeatureTable = $self->figmodel()->GetGenomeFeatureTable($self->genome());
1462 :     if (!defined($FeatureTable)) {
1463 :     $self->figmodel()->error_message("FIGMODEL:CreateSingleGenomeReactionList: ".$self->id()." genome features could not be accessed!");
1464 :     return $self->fail();
1465 :     }
1466 :     #Checking that the number of genes exceeds the minimum size
1467 :     if ($FeatureTable->size() < $self->config("minimum genome size for modeling")->[0]) {
1468 :     $self->figmodel()->error_message("FIGMODEL:CreateSingleGenomeReactionList: ".$self->id()." genome rejected as too small for modeling!");
1469 :     return $self->fail();
1470 :     }
1471 :     #Setting up needed variables
1472 :     my $OriginalModelTable = undef;
1473 :     #Locking model status table
1474 :     my $ModelTable = $self->figmodel()->database()->LockDBTable("MODELS");
1475 :     my $Row = $ModelTable->get_row_by_key($self->id(),"id");
1476 :     if (!defined($Row) || !defined($Row->{status}->[0])) {
1477 :     $self->figmodel()->database()->UnlockDBTable("MODELS");
1478 :     $self->figmodel()->error_message("FIGMODEL:CreateSingleGenomeReactionList: ".$self->id()." has no row in models table!");
1479 :     return $self->fail();
1480 :     } elsif ($Row->{status}->[0] == 0) {
1481 :     $self->figmodel()->error_message("FIGMODEL:CreateSingleGenomeReactionList:Model is already being built. Canceling current build.");
1482 :     $self->figmodel()->database()->UnlockDBTable("MODELS");
1483 :     return $self->fail();
1484 :     }elsif ($Row->{status}->[0] == 1) {
1485 :     $OriginalModelTable = $self->reaction_table();
1486 :     $self->ArchiveModel();
1487 :     $Row->{status}->[0] = 0;
1488 :     $Row->{message}->[0] = "Rebuilding preliminary reconstruction";
1489 :     } else {
1490 :     $Row->{status}->[0] = 0;
1491 :     $Row->{message}->[0] = "Preliminary reconstruction";
1492 :     }
1493 :     #Updating the status table
1494 :     $self->figmodel()->database()->save_table($ModelTable);
1495 :     $self->figmodel()->database()->UnlockDBTable("MODELS");
1496 :     #Sorting GenomeData by gene location on the chromosome
1497 :     $FeatureTable->sort_rows("MIN LOCATION");
1498 :     my ($ComplexHash,$SuggestedMappings,$UnrecognizedReactions,$ReactionHash);
1499 :     my %LastGenePosition;
1500 :     my $GeneRoles;
1501 :     for (my $j=0; $j < $FeatureTable->size(); $j++) {
1502 :     my $CurrentRow = $FeatureTable->get_row($j);
1503 :     #"ID","ALIASES","MIN LOCATION","MAX LOCATION","ROLES","SUBSYSTEMS","SUBSYSTEM CLASS"
1504 :     if (defined($CurrentRow)) {
1505 :     my $GeneID = $CurrentRow->{"ID"}->[0];
1506 :     if ($GeneID =~ m/(peg\.\d+)/) {
1507 :     $GeneID = $1;
1508 :     }
1509 :     foreach my $Role (@{$CurrentRow->{"ROLES"}}) {
1510 :     if ($self->figmodel()->role_is_valid($Role) != 0) {
1511 :     push(@{$GeneRoles->{$GeneID}},$Role);
1512 :     my $ReactionHashArrayRef = $self->figmodel()->reactions_of_role($Role);
1513 :     if ($ReactionHashArrayRef != 0) {
1514 :     foreach my $Mapping (@{$ReactionHashArrayRef}) {
1515 :     if (defined($Mapping->{"REACTION"}) && defined($Mapping->{"MASTER"}) && defined($Mapping->{"SUBSYSTEM"}) && defined($Mapping->{"SOURCE"})) {
1516 :     if ($Mapping->{"REACTION"}->[0] =~ m/rxn\d\d\d\d\d/) {
1517 :     if ($Mapping->{"MASTER"}->[0] eq 1) {
1518 :     #Creating a complex if consecutive genes have been assigned to the same reaction
1519 :     $ComplexHash->{$Mapping->{"REACTION"}->[0]}->{$Mapping->{"COMPLEX"}->[0]}->{$Role}->{$GeneID} = 1;
1520 :     if (!defined($LastGenePosition{$Mapping->{"REACTION"}->[0]})) {
1521 :     $LastGenePosition{$Mapping->{"REACTION"}->[0]} = $j;
1522 :     push(@{$ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"GENES"}},$GeneID);
1523 :     } elsif (($j-$LastGenePosition{$Mapping->{"REACTION"}->[0]}) < 3 && $LastGenePosition{$Mapping->{"REACTION"}->[0]} != $j) {
1524 :     my $CurrentComplex = pop(@{$ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"GENES"}});
1525 :     push(@{$ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"GENES"}},$CurrentComplex."+".$GeneID);
1526 :     } elsif ($LastGenePosition{$Mapping->{"REACTION"}->[0]} != $j) {
1527 :     push(@{$ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"GENES"}},$GeneID);
1528 :     }
1529 :     $LastGenePosition{$Mapping->{"REACTION"}->[0]} = $j;
1530 :     #Adding a subsystem for the reaction
1531 :     if ($self->figmodel()->subsystem_is_valid($Mapping->{"SUBSYSTEM"}->[0]) == 1) {
1532 :     ($ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"SUBSYSTEMS"},my $NumMatches) = $self->figmodel()->add_elements_unique($ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"SUBSYSTEMS"},$Mapping->{"SUBSYSTEM"}->[0]);
1533 :     if (!defined($ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"CONFIDENCE"}) || $ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"CONFIDENCE"}->[0] > 1) {
1534 :     if ($Mapping->{"SOURCE"}->[0] =~ m/Hope\sFiles/) {
1535 :     $ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"CONFIDENCE"}->[0] = 1;
1536 :     } elsif ($Mapping->{"SOURCE"}->[0] =~ m/SEED/) {
1537 :     $ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"CONFIDENCE"}->[0] = 2;
1538 :     } elsif (!defined($ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"CONFIDENCE"}) || $ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"CONFIDENCE"}->[0] > 2) {
1539 :     $ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"CONFIDENCE"}->[0] = 3;
1540 :     }
1541 :     }
1542 :     }
1543 :     #Handling confidence
1544 :     if (!defined($ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"CONFIDENCE"}) || $ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"CONFIDENCE"}->[0] > 2) {
1545 :     if ($Mapping->{"SOURCE"}->[0] =~ m/MATT/) {
1546 :     $ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"CONFIDENCE"}->[0] = 3;
1547 :     } elsif ($Mapping->{"SOURCE"}->[0] =~ m/CHRIS/) {
1548 :     $ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"CONFIDENCE"}->[0] = 4;
1549 :     } else {
1550 :     $ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"CONFIDENCE"}->[0] = 5;
1551 :     }
1552 :     }
1553 :     #Parsing sources
1554 :     ($ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"SOURCE"},my $NumMatches) = $self->figmodel()->add_elements_unique($ReactionHash->{$Mapping->{"REACTION"}->[0]}->{"SOURCE"},split(/\|/,$Mapping->{"SOURCE"}->[0]));
1555 :     } else {
1556 :     push(@{$SuggestedMappings},$GeneID."\t".$Mapping->{"REACTION"}->[0]."\t".$Role);
1557 :     }
1558 :     } else {
1559 :     push(@{$UnrecognizedReactions},$GeneID."\t".$Mapping->{"REACTION"}->[0]."\t".$Role);
1560 :     }
1561 :     }
1562 :     }
1563 :     }
1564 :     }
1565 :     }
1566 :     }
1567 :     }
1568 :    
1569 :     #Creating nonadjacent complexes
1570 :     my @ReactionList = keys(%{$ReactionHash});
1571 :     foreach my $Reaction (@ReactionList) {
1572 :     #If multiple genes are assigned to the reaction, we check if they should should be in a complex
1573 :     if (@{$ReactionHash->{$Reaction}->{"GENES"}} > 0 && defined($ComplexHash->{$Reaction})) {
1574 :     my $GeneArray;
1575 :     foreach my $Complex (keys(%{$ComplexHash->{$Reaction}})) {
1576 :     my %ComplexComponents;
1577 :     foreach my $CurrentGeneSet (@{$ReactionHash->{$Reaction}->{"GENES"}}) {
1578 :     my @GeneList = split(/\+/,$CurrentGeneSet);
1579 :     my %RoleHash;
1580 :     foreach my $Gene (@GeneList) {
1581 :     foreach my $Role (@{$GeneRoles->{$Gene}}) {
1582 :     if (defined($ComplexHash->{$Reaction}->{$Complex}->{$Role})) {
1583 :     $RoleHash{$Role} = 1;
1584 :     }
1585 :     }
1586 :     }
1587 :     if (keys(%RoleHash) > 0) {
1588 :     if (!defined($ComplexComponents{join("|",sort(keys(%RoleHash)))})) {
1589 :     my @RoleList = keys(%RoleHash);
1590 :     my @ComplexList = keys(%ComplexComponents);
1591 :     foreach my $ComplexSet (@ComplexList) {
1592 :     my @RoleList = split(/\|/,$ComplexSet);
1593 :     my $Match = 0;
1594 :     foreach my $SingleRole (@RoleList) {
1595 :     if (defined($RoleHash{$SingleRole})) {
1596 :     $Match = 1;
1597 :     last;
1598 :     }
1599 :     }
1600 :     if ($Match == 1) {
1601 :     foreach my $SingleRole (@RoleList) {
1602 :     $RoleHash{$SingleRole} = 1
1603 :     }
1604 :     push(@{$ComplexComponents{join("|",sort(keys(%RoleHash)))}},@{$ComplexComponents{$ComplexSet}});
1605 :     delete $ComplexComponents{$ComplexSet};
1606 :     }
1607 :     }
1608 :     }
1609 :     push(@{$ComplexComponents{join("|",sort(keys(%RoleHash)))}},$CurrentGeneSet);
1610 :     }
1611 :     }
1612 :     my @Position;
1613 :     my @Options;
1614 :     my $Count = 0;
1615 :     foreach my $RoleSet (keys(%ComplexComponents)) {
1616 :     push(@Position,0);
1617 :     push(@{$Options[$Count]},@{$ComplexComponents{$RoleSet}});
1618 :     $Count++;
1619 :     }
1620 :     my $Done = 0;
1621 :     $Count = 0;
1622 :     my $NewRelationship;
1623 :     while($Done == 0) {
1624 :     #Creating complex with current indecies
1625 :     $NewRelationship->[$Count] = $Options[0]->[$Position[0]];
1626 :     for (my $i=1; $i < @Position; $i++) {
1627 :     $NewRelationship->[$Count] .= "+".$Options[$i]->[$Position[$i]];
1628 :     }
1629 :     $NewRelationship->[$Count] = join("+",$self->figmodel()->remove_duplicates(split(/\+/,$NewRelationship->[$Count])));
1630 :     $Count++;
1631 :     #Iterating indecies
1632 :     my $CurrentIndex = 0;
1633 :     while($CurrentIndex >= 0) {
1634 :     if ($CurrentIndex >= @Position) {
1635 :     $CurrentIndex = -1000;
1636 :     } elsif ($Position[$CurrentIndex]+1 == @{$Options[$CurrentIndex]}) {
1637 :     $Position[$CurrentIndex] = -1;
1638 :     $CurrentIndex++;
1639 :     } else {
1640 :     $Position[$CurrentIndex]++;
1641 :     $CurrentIndex--;
1642 :     }
1643 :     }
1644 :     if ($CurrentIndex == -1000) {
1645 :     $Done = 1;
1646 :     }
1647 :     }
1648 :     push(@{$GeneArray},@{$NewRelationship});
1649 :     }
1650 :     @{$ReactionHash->{$Reaction}->{"GENES"}} = $self->figmodel()->remove_duplicates(@{$GeneArray});
1651 :     }
1652 :     }
1653 :    
1654 :     #Getting the reaction table
1655 :     my $ReactionTable = $self->figmodel()->database()->GetDBTable("REACTIONS");
1656 :    
1657 :     #Creating the model reaction table
1658 :     my $NewModelTable = FIGMODELTable->new(["LOAD","DIRECTIONALITY","COMPARTMENT","ASSOCIATED PEG","SUBSYSTEM","CONFIDENCE","REFERENCE","NOTES"],$self->directory().$self->id().".txt",["LOAD"],";","|","REACTIONS\n");
1659 :     @ReactionList = keys(%{$ReactionHash});
1660 :     foreach my $ReactionID (@ReactionList) {
1661 :     #Getting the thermodynamic reversibility from the database
1662 :     my $Directionality = $self->figmodel()->reversibility_of_reaction($ReactionID);
1663 :     my $Subsystem = "NONE";
1664 :     if (defined($ReactionHash->{$ReactionID}->{"SUBSYSTEMS"})) {
1665 :     $Subsystem = join("|",@{$ReactionHash->{$ReactionID}->{"SUBSYSTEMS"}});
1666 :     }
1667 :     my $Source = "NONE";
1668 :     if (defined($ReactionHash->{$ReactionID}->{"SOURCE"})) {
1669 :     $Source = join("|",@{$ReactionHash->{$ReactionID}->{"SOURCE"}});
1670 :     }
1671 :     $NewModelTable->add_row({"LOAD" => [$ReactionID],"DIRECTIONALITY" => [$Directionality],"COMPARTMENT" => ["c"],"ASSOCIATED PEG" => [join("|",@{$ReactionHash->{$ReactionID}->{"GENES"}})],"SUBSYSTEM" => [$Subsystem],"CONFIDENCE" => [$ReactionHash->{$ReactionID}->{"CONFIDENCE"}->[0]],"REFERENCE" => [$Source],"NOTES" => ["NONE"]});
1672 :     }
1673 :    
1674 :     #Adding the spontaneous and universal reactions
1675 :     foreach my $ReactionID (@{$self->config("spontaneous reactions")}) {
1676 :     #Getting the thermodynamic reversibility from the database
1677 :     my $Directionality = $self->figmodel()->reversibility_of_reaction($ReactionID);
1678 :     #Checking if the reaction is already in the model
1679 :     if (!defined($NewModelTable->get_row_by_key($ReactionID,"LOAD"))) {
1680 :     $NewModelTable->add_row({"LOAD" => [$ReactionID],"DIRECTIONALITY" => [$Directionality],"COMPARTMENT" => ["c"],"ASSOCIATED PEG" => ["SPONTANEOUS"],"SUBSYSTEM" => ["NONE"],"CONFIDENCE" => [4],"REFERENCE" => ["SPONTANEOUS"],"NOTES" => ["NONE"]});
1681 :     }
1682 :     }
1683 :     foreach my $ReactionID (@{$self->config("universal reactions")}) {
1684 :     #Getting the thermodynamic reversibility from the database
1685 :     my $Directionality = $self->figmodel()->reversibility_of_reaction($ReactionID);
1686 :     #Checking if the reaction is already in the model
1687 :     if (!defined($NewModelTable->get_row_by_key($ReactionID,"LOAD"))) {
1688 :     $NewModelTable->add_row({"LOAD" => [$ReactionID],"DIRECTIONALITY" => [$Directionality],"COMPARTMENT" => ["c"],"ASSOCIATED PEG" => ["UNIVERSAL"],"SUBSYSTEM" => ["NONE"],"CONFIDENCE" => [4],"REFERENCE" => ["UNIVERSAL"],"NOTES" => ["NONE"]});
1689 :     }
1690 :     }
1691 :    
1692 :     #Checking if a biomass reaction already exists
1693 :     my $BiomassReactionRow = $self->figmodel()->database()->get_row_by_key("BIOMASS TABLE",$self->id(),"MODELS");
1694 :     if (!defined($BiomassReactionRow)) {
1695 :     $BiomassReactionRow = $self->BuildSpecificBiomassReaction();
1696 :     if (!defined($BiomassReactionRow)) {
1697 :     $self->set_status(-2,"Preliminary reconstruction failed: could not generate biomass reaction");
1698 :     $self->figmodel()->error_message("FIGMODEL:CreateModelReactionList: Could not generate biomass function for ".$self->id()."!");
1699 :     return $self->fail();
1700 :     }
1701 :     }
1702 :     my $ReactionList = $BiomassReactionRow->{"ESSENTIAL REACTIONS"};
1703 :     push(@{$ReactionList},$BiomassReactionRow->{DATABASE}->[0]);
1704 :    
1705 :     #Adding biomass reactions to the model table
1706 :     foreach my $BOFReaction (@{$ReactionList}) {
1707 :     #Getting the thermodynamic reversibility from the database
1708 :     my $Directionality = $self->figmodel()->reversibility_of_reaction($BOFReaction);
1709 :     #Checking if the reaction is already in the model
1710 :     if (!defined($NewModelTable->get_row_by_key($BOFReaction,"LOAD"))) {
1711 :     if ($BOFReaction =~ m/bio/) {
1712 :     $NewModelTable->add_row({"LOAD" => [$BOFReaction],"DIRECTIONALITY" => [$Directionality],"COMPARTMENT" => ["c"],"ASSOCIATED PEG" => ["BOF"],"SUBSYSTEM" => ["NONE"],"CONFIDENCE" => [1],"REFERENCE" => ["Biomass objective function"],"NOTES" => ["NONE"]});
1713 :     } else {
1714 :     $NewModelTable->add_row({"LOAD" => [$BOFReaction],"DIRECTIONALITY" => [$Directionality],"COMPARTMENT" => ["c"],"ASSOCIATED PEG" => ["INITIAL GAP FILLING"],"SUBSYSTEM" => ["NONE"],"CONFIDENCE" => [5],"REFERENCE" => ["Initial gap filling"],"NOTES" => ["NONE"]});
1715 :     }
1716 :     }
1717 :     }
1718 :    
1719 :     #Completing any incomplete reactions sets
1720 :     my $ReactionSetTable = $self->figmodel()->database()->GetDBTable("REACTION SETS");
1721 :     for (my $i=0; $i < $ReactionSetTable->size(); $i++) {
1722 :     if (defined($NewModelTable->get_row_by_key($ReactionSetTable->get_row($i)->{"Trigger reaction"}->[0],"LOAD"))) {
1723 :     foreach my $Reaction (@{$ReactionSetTable->get_row($i)->{"Dependant reactions"}}) {
1724 :     if (!defined($NewModelTable->get_row_by_key($ReactionSetTable->get_row($i)->{"Trigger reaction"}->[0],"LOAD"))) {
1725 :     #Getting the thermodynamic reversibility from the database
1726 :     my $Directionality = $self->figmodel()->reversibility_of_reaction($Reaction);
1727 :     $NewModelTable->add_row({"LOAD" => [$Reaction],"DIRECTIONALITY" => [$Directionality],"COMPARTMENT" => ["c"],"ASSOCIATED PEG" => ["REACTION SET GAP FILLING"],"SUBSYSTEM" => ["NONE"],"CONFIDENCE" => [5],"REFERENCE" => ["Added due to presence of ".$ReactionSetTable->get_row($i)->{"Trigger reaction"}->[0]],"NOTES" => ["NONE"]});
1728 :     }
1729 :     }
1730 :     }
1731 :     }
1732 :    
1733 :     #Now we compare the model to the previous model to determine if any differences exist that aren't gap filling reactions
1734 :     if (defined($OriginalModelTable)) {
1735 :     my $PerfectMatch = 1;
1736 :     my $ReactionCount = 0;
1737 :     for (my $i=0; $i < $OriginalModelTable->size(); $i++) {
1738 :     #We only check that nongapfilling reactions exist in the new model
1739 :     if ($OriginalModelTable->get_row($i)->{"ASSOCIATED PEG"}->[0] !~ m/GAP/ || $OriginalModelTable->get_row($i)->{"ASSOCIATED PEG"}->[0] eq "INITIAL GAP FILLING") {
1740 :     $ReactionCount++;
1741 :     my $Row = $NewModelTable->get_row_by_key($OriginalModelTable->get_row($i)->{"LOAD"}->[0],"LOAD");
1742 :     if (defined($Row)) {
1743 :     #We check that the reaction directionality is identical
1744 :     if ($Row->{"DIRECTIONALITY"}->[0] ne $OriginalModelTable->get_row($i)->{"DIRECTIONALITY"}->[0]) {
1745 :     if (defined($OriginalModelTable->get_row($i)->{"NOTES"}->[0]) && $OriginalModelTable->get_row($i)->{"NOTES"}->[0] =~ m/Directionality\sswitched\sfrom\s([^\s])/) {
1746 :     if ($1 ne $Row->{"DIRECTIONALITY"}->[0]) {
1747 :     print "Directionality mismatch for reaction ".$OriginalModelTable->get_row($i)->{"LOAD"}->[0].": ".$1." vs ".$Row->{"DIRECTIONALITY"}->[0]."\n";
1748 :     $PerfectMatch = 0;
1749 :     last;
1750 :     }
1751 :     } else {
1752 :     print "Directionality mismatch for reaction ".$OriginalModelTable->get_row($i)->{"LOAD"}->[0].": ".$OriginalModelTable->get_row($i)->{"DIRECTIONALITY"}->[0]." vs ".$Row->{"DIRECTIONALITY"}->[0]."\n";
1753 :     $PerfectMatch = 0;
1754 :     last;
1755 :     }
1756 :     }
1757 :     #We check that the genes assigned to the reaction are identical
1758 :     if ($PerfectMatch == 1 && @{$OriginalModelTable->get_row($i)->{"ASSOCIATED PEG"}} != @{$Row->{"ASSOCIATED PEG"}}) {
1759 :     print "Gene associatation mismatch for reaction ".$OriginalModelTable->get_row($i)->{"LOAD"}->[0].": ".@{$OriginalModelTable->get_row($i)->{"ASSOCIATED PEG"}}." vs ".@{$Row->{"ASSOCIATED PEG"}}."\n";
1760 :     $PerfectMatch = 0;
1761 :     last;
1762 :     }
1763 :     if ($PerfectMatch == 1) {
1764 :     my @GeneSetOne = sort(@{$OriginalModelTable->get_row($i)->{"ASSOCIATED PEG"}});
1765 :     my @GeneSetTwo = sort(@{$Row->{"ASSOCIATED PEG"}});
1766 :     for (my $j=0; $j < @GeneSetOne; $j++) {
1767 :     if ($GeneSetOne[$j] ne $GeneSetTwo[$j]) {
1768 :     print "Gene mismatch for reaction ".$OriginalModelTable->get_row($i)->{"LOAD"}->[0].": ".$GeneSetOne[$j]." vs ".$GeneSetTwo[$j]."\n";
1769 :     $PerfectMatch = 0;
1770 :     $i = $OriginalModelTable->size();
1771 :     last;
1772 :     }
1773 :     }
1774 :     }
1775 :     } else {
1776 :     print "Original model contains an extra reaction:".$OriginalModelTable->get_row($i)->{"LOAD"}->[0]."\n";
1777 :     $PerfectMatch = 0;
1778 :     last;
1779 :     }
1780 :     }
1781 :     }
1782 :     if ($PerfectMatch == 1 && $ReactionCount == $NewModelTable->size()) {
1783 :     #Bailing out of function as the model has not changed
1784 :     $self->set_status(1,"rebuild canceled because model has not changed");
1785 :     return $self->success();
1786 :     }
1787 :     }
1788 :    
1789 :     #Saving the new model to file
1790 :     $self->set_status(1,"Preliminary reconstruction complete");
1791 :     $self->figmodel()->database()->save_table($NewModelTable);
1792 :     $self->{_reaction_data} = $NewModelTable;
1793 :     #Clearing the previous model from the cache
1794 : chenry 1.7 $self->figmodel()->database()->ClearDBModel($self->id(),1);
1795 : chenry 1.2 #Updating the model stats table
1796 :     $self->update_stats_for_build();
1797 :     $self->PrintSBMLFile();
1798 :    
1799 :     #Adding model to gapfilling queue
1800 :     if (defined($RunGapFilling) && $RunGapFilling == 1) {
1801 :     $self->set_status(1,"Autocompletion queued");
1802 :     $self->figmodel()->add_job_to_queue("gapfillmodel?".$self->id(),"QSUB","cplex",$self->owner(),"BACK");
1803 :     }
1804 :     return $self->success();
1805 :     }
1806 :    
1807 :     =head3 CreateMetaGenomeReactionList
1808 :     Definition:
1809 :     (success/fail) = FIGMODELmodel->CreateMetaGenomeReactionList();
1810 :     Description:
1811 :     This is the code called to create or update the reaction list for a metgenome model
1812 :     =cut
1813 :    
1814 :     sub CreateMetaGenomeReactionList {
1815 :     my ($self) = @_;
1816 :    
1817 :     #Checking if the metagenome file exists
1818 :     if (!-e $self->config("raw MGRAST directory")->[0].$self->genome().".summary") {
1819 :     $self->error_message("FIGMODEL:CreateMetaGenomeReactionList: could not find raw data file for metagenome ".$self->genome());
1820 :     }
1821 :     #Loading metagenome data
1822 :     my $MGRASTData = $self->figmodel()->database()->load_multiple_column_file($self->config("raw MGRAST directory")->[0].$self->genome().".summary","\t");
1823 :     if (!defined($MGRASTData)) {
1824 :     $self->error_message("FIGMODEL:CreateMetaGenomeReactionList: could not find raw data file for metagenome ".$self->genome());
1825 :     }
1826 :    
1827 :     #Setting up needed variables
1828 :     my $OriginalModelTable = undef;
1829 :    
1830 :     #Checking status
1831 :     if ($self->status() < 0) {
1832 :     $self->set_status(0,"Preliminary reconstruction");
1833 :     } elsif ($self->status() == 0) {
1834 :     $self->error_message("FIGMODEL->CreateModelReactionList:Model is already being built. Canceling current build.");
1835 :     return $self->fail();
1836 :     } else {
1837 :     $OriginalModelTable = $self->reaction_table();
1838 :     $self->ArchiveModel();
1839 :     $self->set_status(0,"Rebuilding preliminary reconstruction");
1840 :     }
1841 :    
1842 :     #Getting the reaction table
1843 :     my $ReactionTable = $self->figmodel()->database()->GetDBTable("REACTIONS");
1844 :     #Creating model table
1845 :     my $ModelTable = FIGMODELTable->new(["LOAD","DIRECTIONALITY","COMPARTMENT","ASSOCIATED PEG","SUBSYSTEM","CONFIDENCE","REFERENCE","NOTES"],$self->directory().$self->id().".txt",["LOAD"],";","|","REACTIONS\n");
1846 :     for (my $i=0; $i < @{$MGRASTData};$i++) {
1847 :     #MD5,PEG,number of sims,role,sim e-scores
1848 :     my $Role = $MGRASTData->[$i]->[3];
1849 :     my $MD5 = $MGRASTData->[$i]->[0];
1850 :     my $peg = $MGRASTData->[$i]->[1];
1851 :     my $sims = $MGRASTData->[$i]->[4];
1852 :     $sims =~ s/;/,/g;
1853 :    
1854 :     #Checking for subsystems
1855 :     my $GeneSubsystems = $self->figmodel()->subsystems_of_role($Role);
1856 :     #Checking if there are reactions associated with this role
1857 :     my $ReactionHashArrayRef = $self->figmodel()->reactions_of_role($Role);
1858 :     if ($ReactionHashArrayRef != 0) {
1859 :     foreach my $Mapping (@{$ReactionHashArrayRef}) {
1860 :     if (defined($Mapping->{"REACTION"}) && defined($Mapping->{"MASTER"}) && defined($Mapping->{"SUBSYSTEM"}) && defined($Mapping->{"SOURCE"})) {
1861 :     if ($Mapping->{"REACTION"}->[0] =~ m/rxn\d\d\d\d\d/) {
1862 :     if ($Mapping->{"MASTER"}->[0] eq 1) {
1863 :     #Checking if the reaction is already in the model
1864 :     my $ReactionRow = $ModelTable->get_row_by_key($Mapping->{"REACTION"}->[0],"LOAD");
1865 :     if (!defined($ReactionRow)) {
1866 :     $ReactionRow = {"LOAD" => [$Mapping->{"REACTION"}->[0]],"DIRECTIONALITY" => [$self->figmodel()->reversibility_of_reaction($Mapping->{"REACTION"}->[0])],"COMPARTMENT" => ["c"]};
1867 :     $ModelTable->add_row($ReactionRow);
1868 :     }
1869 :     push(@{$ReactionRow->{"ASSOCIATED PEG"}},substr($peg,4));
1870 :     push(@{$ReactionRow->{"REFERENCE"}},$MD5.":".$Role);
1871 :     push(@{$ReactionRow->{"CONFIDENCE"}},$sims);
1872 :     if (defined($GeneSubsystems)) {
1873 :     push(@{$ReactionRow->{"SUBSYSTEM"}},@{$GeneSubsystems});
1874 :     }
1875 :     }
1876 :     }
1877 :     }
1878 :     }
1879 :     }
1880 :     }
1881 :    
1882 :     #Adding the spontaneous and universal reactions
1883 :     foreach my $ReactionID (@{$self->config("spontaneous reactions")}) {
1884 :     #Getting the thermodynamic reversibility from the database
1885 :     my $Directionality = $self->figmodel()->reversibility_of_reaction($ReactionID);
1886 :     #Checking if the reaction is already in the model
1887 :     if (!defined($ModelTable->get_row_by_key($ReactionID,"LOAD"))) {
1888 :     $ModelTable->add_row({"LOAD" => [$ReactionID],"DIRECTIONALITY" => [$Directionality],"COMPARTMENT" => ["c"],"ASSOCIATED PEG" => ["SPONTANEOUS"],"SUBSYSTEM" => ["NONE"],"CONFIDENCE" => [4],"REFERENCE" => ["SPONTANEOUS"],"NOTES" => ["NONE"]});
1889 :     }
1890 :     }
1891 :     foreach my $ReactionID (@{$self->config("universal reactions")}) {
1892 :     #Getting the thermodynamic reversibility from the database
1893 :     my $Directionality = $self->figmodel()->reversibility_of_reaction($ReactionID);
1894 :     #Checking if the reaction is already in the model
1895 :     if (!defined($ModelTable->get_row_by_key($ReactionID,"LOAD"))) {
1896 :     $ModelTable->add_row({"LOAD" => [$ReactionID],"DIRECTIONALITY" => [$Directionality],"COMPARTMENT" => ["c"],"ASSOCIATED PEG" => ["UNIVERSAL"],"SUBSYSTEM" => ["NONE"],"CONFIDENCE" => [4],"REFERENCE" => ["UNIVERSAL"],"NOTES" => ["NONE"]});
1897 :     }
1898 :     }
1899 :    
1900 :     #Completing any incomplete reactions sets
1901 :     my $ReactionSetTable = $self->figmodel()->database()->GetDBTable("REACTION SETS");
1902 :     for (my $i=0; $i < $ReactionSetTable->size(); $i++) {
1903 :     if (defined($ModelTable->get_row_by_key($ReactionSetTable->get_row($i)->{"Trigger reaction"}->[0],"LOAD"))) {
1904 :     foreach my $Reaction (@{$ReactionSetTable->get_row($i)->{"Dependant reactions"}}) {
1905 :     if (!defined($ModelTable->get_row_by_key($ReactionSetTable->get_row($i)->{"Trigger reaction"}->[0],"LOAD"))) {
1906 :     #Getting the thermodynamic reversibility from the database
1907 :     my $Directionality = $self->figmodel()->reversibility_of_reaction($Reaction);
1908 :     $ModelTable->add_row({"LOAD" => [$Reaction],"DIRECTIONALITY" => [$Directionality],"COMPARTMENT" => ["c"],"ASSOCIATED PEG" => ["REACTION SET GAP FILLING"],"SUBSYSTEM" => ["NONE"],"CONFIDENCE" => [5],"REFERENCE" => ["Added due to presence of ".$ReactionSetTable->get_row($i)->{"Trigger reaction"}->[0]],"NOTES" => ["NONE"]});
1909 :     }
1910 :     }
1911 :     }
1912 :     }
1913 :    
1914 :     #Clearing the previous model from the cache
1915 : chenry 1.7 $self->figmodel()->database()->ClearDBModel($self->id(),1);
1916 : chenry 1.2 $ModelTable->save();
1917 :    
1918 :     return $self->success();
1919 :     }
1920 :    
1921 :     =head3 ArchiveModel
1922 :     Definition:
1923 :     (success/fail) = FIGMODELmodel->ArchiveModel();
1924 :     Description:
1925 :     This function archives the specified model in the model directory with the current version numbers appended.
1926 :     This function is used to preserve old versions of models prior to overwriting so new versions may be compared with old versions.
1927 :     =cut
1928 :     sub ArchiveModel {
1929 :     my ($self) = @_;
1930 :    
1931 :     #Making sure the model exists
1932 :     if (!defined($self->stats())) {
1933 :     $self->figmodel()->error_message("FIGMODEL:ArchiveModel: Could not find specified ".$self->id()." in database!");
1934 :     return $self->fail();
1935 :     }
1936 :    
1937 :     #Checking that the model file exists
1938 :     if (!(-e $self->filename())) {
1939 :     $self->figmodel()->error_message("FIGMODEL:ArchiveModel: Model file ".$self->filename()." not found!");
1940 :     return $self->fail();
1941 :     }
1942 :    
1943 :     #Copying the model file
1944 :     system("cp ".$self->filename()." ".$self->directory().$self->id().$self->version().".txt");
1945 :     }
1946 :    
1947 :     =head3 PrintModelDataToFile
1948 :     Definition:
1949 :     (success/fail) = FIGMODELmodel->PrintModelDataToFile();
1950 :     Description:
1951 :     This function uses the MFAToolkit to print out all of the compound and reaction data for the input model.
1952 :     Some of the data printed by the toolkit is calculated internally in the toolkit and not stored in any files, so this data can only be retrieved through this
1953 :     function. The LoadModel function for example would not give you this data.
1954 :     =cut
1955 :     sub PrintModelDataToFile {
1956 :     my($self) = @_;
1957 :    
1958 :     #Running the MFAToolkit on the model file
1959 :     my $OutputIndex = $self->figmodel()->filename();
1960 :     my $Command = $self->config("MFAToolkit executable")->[0]." parameterfile ../Parameters/Printing.txt resetparameter output_folder ".$OutputIndex.'/ LoadCentralSystem "'.$self->filename().'"';
1961 :     system($Command);
1962 :    
1963 :     #Copying the model file printed by the toolkit out of the output directory and into the model directory
1964 :     if (!-e $self->config("MFAToolkit output directory")->[0].$OutputIndex."/".$self->id().$self->selected_version().".txt") {
1965 :     $self->figmodel()->error_message("New model file not created due to an error. Check that the input modelfile exists.");
1966 :     $self->figmodel()->cleardirectory($OutputIndex);
1967 :     return $self->fail();
1968 :     }
1969 :    
1970 :     $Command = 'cp "'.$self->config("MFAToolkit output directory")->[0].$OutputIndex."/".$self->id().$self->selected_version().'.txt" "'.$self->directory().$self->id().$self->selected_version().'Data.txt"';
1971 :     system($Command);
1972 :     $Command = 'cp "'.$self->config("MFAToolkit output directory")->[0].$OutputIndex.'/ErrorLog0.txt" "'.$self->directory().'ModelErrors.txt"';
1973 :     system($Command);
1974 :     $self->figmodel()->cleardirectory($OutputIndex);
1975 :     return $self->success();
1976 :     }
1977 :    
1978 :     =head2 Analysis Functions
1979 :    
1980 :     =head3 run_microarray_analysis
1981 :     Definition:
1982 :     int::status = FIGMODEL->run_microarray_analysis(string::media,string::job id,string::gene calls);
1983 :     Description:
1984 :     Runs microarray analysis attempting to turn off genes that are inactive in the microarray
1985 :     =cut
1986 :     sub run_microarray_analysis {
1987 : chenry 1.5 my ($self,$media,$label,$index,$genecall) = @_;
1988 : chenry 1.2 $genecall =~ s/_/:/g;
1989 :     $genecall =~ s/\//;/g;
1990 : chenry 1.5 my $uniqueFilename = $self->figmodel()->filename();
1991 :     my $command = $self->figmodel()->GenerateMFAToolkitCommandLineCall($uniqueFilename,$self->id(),$media,["ProductionMFA","ShewenellaExperiment"],{"Microarray assertions" => $label.";".$index.";".$genecall,"MFASolver" => "CPLEX","Network output location" => "/scratch/"},"MicroarrayAnalysis-".$uniqueFilename.".txt",undef,$self->selected_version());
1992 : chenry 1.2 system($command);
1993 : chenry 1.5 my $filename = $self->figmodel()->config("MFAToolkit output directory")->[0].$uniqueFilename."/MicroarrayOutput-".$index.".txt";
1994 :     if (-e $filename) {
1995 :     my $output = $self->figmodel()->database()->load_single_column_file($filename);
1996 :     if (defined($output->[0])) {
1997 :     my @array = split(/;/,$output->[0]);
1998 :     $self->figmodel()->clearing_output($uniqueFilename,"MicroarrayAnalysis-".$uniqueFilename.".txt");
1999 :     return ($array[0],$array[1],$array[8].":".$array[2],$array[9].":".$array[3],$array[10].":".$array[4],$array[11].":".$array[5],$array[12].":".$array[6],$array[13].":".$array[7]);
2000 :     }
2001 :     print STDERR $filename." is empty!";
2002 :     }
2003 :     print STDERR $filename." not found!";
2004 :     $self->figmodel()->clearing_output($uniqueFilename,"MicroarrayAnalysis-".$uniqueFilename.".txt");
2005 :    
2006 :     return undef;
2007 : chenry 1.2 }
2008 :    
2009 :     =head3 find_minimal_pathways
2010 :     Definition:
2011 :     int::status = FIGMODEL->find_minimal_pathways(string::media,string::objective);
2012 :     Description:
2013 :     Runs microarray analysis attempting to turn off genes that are inactive in the microarray
2014 :     =cut
2015 :     sub find_minimal_pathways {
2016 : chenry 1.5 my ($self,$media,$objective,$solutionnum,$AllReversible,$additionalexchange) = @_;
2017 : chenry 1.2
2018 :     #Setting default media
2019 :     if (!defined($media)) {
2020 :     $media = "Complete";
2021 :     }
2022 :    
2023 :     #Setting default solution number
2024 :     if (!defined($solutionnum)) {
2025 :     $solutionnum = "5";
2026 :     }
2027 :    
2028 : chenry 1.5 #Setting additional exchange fluxes
2029 :     if (!defined($additionalexchange) || length($additionalexchange) == 0) {
2030 :     if ($self->id() eq "iAF1260") {
2031 :     $additionalexchange = "cpd03422[c]:-100:100;cpd01997[c]:-100:100;cpd11416[c]:-100:0;cpd15378[c]:-100:0;cpd15486[c]:-100:0";
2032 :     } else {
2033 :     $additionalexchange = $self->figmodel()->config("default exchange fluxes")->[0];
2034 :     }
2035 :     }
2036 :    
2037 : chenry 1.2 #Translating objective
2038 :     my $objectivestring;
2039 :     if ($objective eq "ALL") {
2040 :     #Getting the list of universal building blocks
2041 :     my $buildingblocks = $self->config("universal building blocks");
2042 :     my @objectives = keys(%{$buildingblocks});
2043 :     #Getting the nonuniversal building blocks
2044 :     my $otherbuildingblocks = $self->config("nonuniversal building blocks");
2045 :     my @array = keys(%{$otherbuildingblocks});
2046 :     if (defined($self->get_biomass()) && defined($self->figmodel()->get_reaction($self->get_biomass()->{"LOAD"}->[0]))) {
2047 :     my $equation = $self->figmodel()->get_reaction($self->get_biomass()->{"LOAD"}->[0])->{"EQUATION"}->[0];
2048 :     if (defined($equation)) {
2049 :     for (my $i=0; $i < @array; $i++) {
2050 :     if (CORE::index($equation,$array[$i]) > 0) {
2051 :     push(@objectives,$array[$i]);
2052 :     }
2053 :     }
2054 :     }
2055 :     }
2056 :     for (my $i=0; $i < @objectives; $i++) {
2057 : chenry 1.5 $self->find_minimal_pathways($media,$objectives[$i]);
2058 : chenry 1.2 }
2059 : chenry 1.5 return;
2060 : chenry 1.2 } elsif ($objective eq "ENERGY") {
2061 :     $objectivestring = "MAX;FLUX;rxn00062;c;1";
2062 :     } elsif ($objective =~ m/cpd\d\d\d\d\d/) {
2063 : chenry 1.5 if ($objective =~ m/\[(\w)\]/) {
2064 :     $objectivestring = "MIN;DRAIN_FLUX;".$objective.";".$1.";1";
2065 :     $additionalexchange .= ";".$objective."[".$1."]:-100:0";
2066 :     } else {
2067 :     $objectivestring = "MIN;DRAIN_FLUX;".$objective.";c;1";
2068 :     $additionalexchange .= ";".$objective."[c]:-100:0";
2069 :     }
2070 :     } elsif ($objective =~ m/(rxn\d\d\d\d\d)/) {
2071 :     my ($Reactants,$Products) = $self->figmodel()->GetReactionSubstrateData($objective);
2072 :     for (my $i=0; $i < @{$Products};$i++) {
2073 :     my $temp = $Products->[$i]->{"DATABASE"}->[0];
2074 :     if ($additionalexchange !~ m/$temp/) {
2075 :     #$additionalexchange .= ";".$temp."[c]:-100:0";
2076 :     }
2077 :     }
2078 :     for (my $i=0; $i < @{$Reactants};$i++) {
2079 :     print $Reactants->[$i]->{"DATABASE"}->[0]." started\n";
2080 :     $self->find_minimal_pathways($media,$Reactants->[$i]->{"DATABASE"}->[0],$additionalexchange);
2081 :     print $Reactants->[$i]->{"DATABASE"}->[0]." done\n";
2082 :     }
2083 :     return;
2084 :     }
2085 :    
2086 :     #Adding additional drains
2087 :     if (($objective eq "cpd15665" || $objective eq "cpd15667" || $objective eq "cpd15668" || $objective eq "cpd15669") && $additionalexchange !~ m/cpd15666/) {
2088 :     $additionalexchange .= ";cpd15666[c]:0:100";
2089 :     } elsif ($objective eq "cpd11493" && $additionalexchange !~ m/cpd12370/) {
2090 :     $additionalexchange .= ";cpd12370[c]:0:100";
2091 :     } elsif ($objective eq "cpd00166" && $additionalexchange !~ m/cpd01997/) {
2092 :     $additionalexchange .= ";cpd01997[c]:0:100;cpd03422[c]:0:100";
2093 :     }
2094 :    
2095 :     #Running MFAToolkit
2096 :     my $filename = $self->figmodel()->filename();
2097 :     my $command;
2098 :     if (defined($AllReversible) && $AllReversible == 1) {
2099 :     $command = $self->figmodel()->GenerateMFAToolkitCommandLineCall($filename,$self->id(),$media,["ProductionMFA"],{"Make all reactions reversible in MFA"=>1, "Recursive MILP solution limit" => $solutionnum,"Generate pathways to objective" => 1,"MFASolver" => "CPLEX","objective" => $objectivestring,"exchange species" => $additionalexchange},"MinimalPathways-".$media."-".$self->id().$self->selected_version().".txt",undef,$self->selected_version());
2100 :     } else {
2101 :     $command = $self->figmodel()->GenerateMFAToolkitCommandLineCall($filename,$self->id(),$media,["ProductionMFA"],{"Make all reactions reversible in MFA"=>0, "Recursive MILP solution limit" => $solutionnum,"Generate pathways to objective" => 1,"MFASolver" => "CPLEX","objective" => $objectivestring,"exchange species" => $additionalexchange},"MinimalPathways-".$media."-".$self->id().$self->selected_version().".txt",undef,$self->selected_version());
2102 :     }
2103 :     system($command);
2104 :    
2105 :     #Loading problem report
2106 :     my $results = $self->figmodel()->LoadProblemReport($filename);
2107 :     #Clearing output
2108 :     $self->figmodel()->clearing_output($filename,"MinimalPathways-".$media."-".$self->id()."-".$objective.".txt");
2109 :     if (!defined($results)) {
2110 :     print STDERR $objective." pathway results not found!\n";
2111 :     return;
2112 :     }
2113 :    
2114 :     #Parsing output
2115 :     my @Array;
2116 :     my $row = $results->get_row(1);
2117 :     if (defined($row->{"Notes"}->[0])) {
2118 :     $_ = $row->{"Notes"}->[0];
2119 :     @Array = /\d+:([^\|]+)\|/g;
2120 :     }
2121 :    
2122 :     #Writing output to file
2123 :     $self->figmodel()->database()->print_array_to_file($self->directory()."MinimalPathways-".$media."-".$objective."-".$self->id()."-".$AllReversible."-".$self->selected_version().".txt",[join(",",@Array)]);
2124 :     }
2125 :    
2126 :     =head3 find_minimal_pathways
2127 :     Definition:
2128 :     int::status = FIGMODEL->find_minimal_pathways(string::media,string::objective);
2129 :     Description:
2130 :     Runs microarray analysis attempting to turn off genes that are inactive in the microarray
2131 :     =cut
2132 :     sub find_minimal_pathways_two {
2133 :     my ($self,$media,$objective,$solutionnum,$AllReversible,$additionalexchange) = @_;
2134 :    
2135 :     #Setting default media
2136 :     if (!defined($media)) {
2137 :     $media = "Complete";
2138 :     }
2139 :    
2140 :     #Setting default solution number
2141 :     if (!defined($solutionnum)) {
2142 :     $solutionnum = "5";
2143 : chenry 1.2 }
2144 :    
2145 :     #Setting additional exchange fluxes
2146 : chenry 1.5 if (!defined($additionalexchange) || length($additionalexchange) == 0) {
2147 :     if ($self->id() eq "iAF1260") {
2148 :     $additionalexchange = "cpd03422[c]:-100:100;cpd01997[c]:-100:100;cpd11416[c]:-100:0;cpd15378[c]:-100:0;cpd15486[c]:-100:0";
2149 :     } else {
2150 :     $additionalexchange = $self->figmodel()->config("default exchange fluxes")->[0];
2151 :     }
2152 :     }
2153 :    
2154 :     #Translating objective
2155 :     my $objectivestring;
2156 :     if ($objective eq "ALL") {
2157 :     #Getting the list of universal building blocks
2158 :     my $buildingblocks = $self->config("universal building blocks");
2159 :     my @objectives = keys(%{$buildingblocks});
2160 :     #Getting the nonuniversal building blocks
2161 :     my $otherbuildingblocks = $self->config("nonuniversal building blocks");
2162 :     my @array = keys(%{$otherbuildingblocks});
2163 :     if (defined($self->get_biomass()) && defined($self->figmodel()->get_reaction($self->get_biomass()->{"LOAD"}->[0]))) {
2164 :     my $equation = $self->figmodel()->get_reaction($self->get_biomass()->{"LOAD"}->[0])->{"EQUATION"}->[0];
2165 :     if (defined($equation)) {
2166 :     for (my $i=0; $i < @array; $i++) {
2167 :     if (CORE::index($equation,$array[$i]) > 0) {
2168 :     push(@objectives,$array[$i]);
2169 :     }
2170 :     }
2171 :     }
2172 :     }
2173 :     for (my $i=0; $i < @objectives; $i++) {
2174 :     $self->find_minimal_pathways($media,$objectives[$i]);
2175 :     }
2176 :     return;
2177 :     } elsif ($objective eq "ENERGY") {
2178 :     $objectivestring = "MAX;FLUX;rxn00062;c;1";
2179 :     } elsif ($objective =~ m/cpd\d\d\d\d\d/) {
2180 :     if ($objective =~ m/\[(\w)\]/) {
2181 :     $objectivestring = "MIN;DRAIN_FLUX;".$objective.";".$1.";1";
2182 :     $additionalexchange .= ";".$objective."[".$1."]:-100:0";
2183 :     } else {
2184 :     $objectivestring = "MIN;DRAIN_FLUX;".$objective.";c;1";
2185 :     $additionalexchange .= ";".$objective."[c]:-100:0";
2186 :     }
2187 :     } elsif ($objective =~ m/(rxn\d\d\d\d\d)/) {
2188 :     my ($Reactants,$Products) = $self->figmodel()->GetReactionSubstrateData($objective);
2189 :     for (my $i=0; $i < @{$Products};$i++) {
2190 :     my $temp = $Products->[$i]->{"DATABASE"}->[0];
2191 :     if ($additionalexchange !~ m/$temp/) {
2192 :     #$additionalexchange .= ";".$temp."[c]:-100:0";
2193 :     }
2194 :     }
2195 :     for (my $i=0; $i < @{$Reactants};$i++) {
2196 :     print $Reactants->[$i]->{"DATABASE"}->[0]." started\n";
2197 :     $self->find_minimal_pathways($media,$Reactants->[$i]->{"DATABASE"}->[0],$additionalexchange);
2198 :     print $Reactants->[$i]->{"DATABASE"}->[0]." done\n";
2199 :     }
2200 :     return;
2201 :     }
2202 :    
2203 :     #Adding additional drains
2204 :     if (($objective eq "cpd15665" || $objective eq "cpd15667" || $objective eq "cpd15668" || $objective eq "cpd15669") && $additionalexchange !~ m/cpd15666/) {
2205 : chenry 1.2 $additionalexchange .= ";cpd15666[c]:0:100";
2206 : chenry 1.5 } elsif ($objective eq "cpd11493" && $additionalexchange !~ m/cpd12370/) {
2207 : chenry 1.2 $additionalexchange .= ";cpd12370[c]:0:100";
2208 : chenry 1.5 } elsif ($objective eq "cpd00166" && $additionalexchange !~ m/cpd01997/) {
2209 : chenry 1.2 $additionalexchange .= ";cpd01997[c]:0:100;cpd03422[c]:0:100";
2210 :     }
2211 :    
2212 :     #Running MFAToolkit
2213 :     my $filename = $self->figmodel()->filename();
2214 : chenry 1.5 my $command;
2215 :     if (defined($AllReversible) && $AllReversible == 1) {
2216 :     $command = $self->figmodel()->GenerateMFAToolkitCommandLineCall($filename,$self->id(),$media,["ProductionMFA"],{"use simple variable and constraint names"=>1,"Make all reactions reversible in MFA"=>1, "Recursive MILP solution limit" => $solutionnum,"Generate pathways to objective" => 1,"MFASolver" => "SCIP","objective" => $objectivestring,"exchange species" => $additionalexchange},"MinimalPathways-".$media."-".$self->id().$self->selected_version().".txt",undef,$self->selected_version());
2217 :     } else {
2218 :     $command = $self->figmodel()->GenerateMFAToolkitCommandLineCall($filename,$self->id(),$media,["ProductionMFA"],{"use simple variable and constraint names"=>1,"Make all reactions reversible in MFA"=>0, "Recursive MILP solution limit" => $solutionnum,"Generate pathways to objective" => 1,"MFASolver" => "SCIP","objective" => $objectivestring,"exchange species" => $additionalexchange},"MinimalPathways-".$media."-".$self->id().$self->selected_version().".txt",undef,$self->selected_version());
2219 :     }
2220 :     print $command."\n";
2221 : chenry 1.2 system($command);
2222 :    
2223 :     #Loading problem report
2224 :     my $results = $self->figmodel()->LoadProblemReport($filename);
2225 :     #Clearing output
2226 :     $self->figmodel()->clearing_output($filename,"MinimalPathways-".$media."-".$self->id()."-".$objective.".txt");
2227 :     if (!defined($results)) {
2228 : chenry 1.5 print STDERR $objective." pathway results not found!\n";
2229 : chenry 1.2 return;
2230 :     }
2231 :    
2232 :     #Parsing output
2233 :     my @Array;
2234 :     my $row = $results->get_row(1);
2235 :     if (defined($row->{"Notes"}->[0])) {
2236 :     $_ = $row->{"Notes"}->[0];
2237 :     @Array = /\d+:([^\|]+)\|/g;
2238 :     }
2239 : chenry 1.5
2240 :     #Writing output to file
2241 :     $self->figmodel()->database()->print_array_to_file($self->directory()."MinimalPathways-".$media."-".$objective."-".$self->id()."-".$AllReversible."-".$self->selected_version().".txt",[join(",",@Array)]);
2242 :     }
2243 : chenry 1.2
2244 : chenry 1.5 sub combine_minimal_pathways {
2245 :     my ($self) = @_;
2246 :    
2247 :     my $tbl;
2248 :     if (-e $self->directory()."MinimalPathwayTable-".$self->id().$self->selected_version().".tbl") {
2249 :     $tbl = FIGMODELTable::load_table($self->directory()."MinimalPathwayTable-".$self->id().$self->selected_version().".tbl",";","|",0,["Objective","Media","Reversible"]);
2250 : chenry 1.2 } else {
2251 : chenry 1.5 $tbl = FIGMODELTable->new(["Objective","Media","Reactions","Reversible","Shortest path","Number of essentials","Essentials","Length"],$self->directory()."MinimalPathwayTable-".$self->id().$self->selected_version().".tbl",["Objective","Media","Reversible"],";","|");
2252 : chenry 1.2 }
2253 : chenry 1.5 my @files = glob($self->directory()."MinimalPathways-*");
2254 :     for (my $i=0; $i < @files;$i++) {
2255 :     if ($files[$i] =~ m/MinimalPathways\-(\S+)\-(cpd\d\d\d\d\d)\-(\w+)\-(\d)\-/ || $files[$i] =~ m/MinimalPathways\-(\S+)\-(ENERGY)\-(\w+)\-(\d)\-/) {
2256 :     my $reactions = $self->figmodel()->database()->load_single_column_file($files[$i],"");
2257 :     if (defined($reactions) && @{$reactions} > 0 && length($reactions->[0]) > 0) {
2258 :     my $newrow = {"Objective"=>[$2],"Media"=>[$1],"Reversible"=>[$4]};
2259 :     my $row = $tbl->get_table_by_key($newrow->{"Objective"}->[0],"Objective")->get_table_by_key($newrow->{"Media"}->[0],"Media")->get_row_by_key($newrow->{"Reversible"}->[0],"Reversible");
2260 :     if (!defined($row)) {
2261 :     $row = $tbl->add_row($newrow);
2262 :     }
2263 :     $row->{Reactions} = $self->figmodel()->database()->load_single_column_file($files[$i],"");
2264 :     delete($row->{"Shortest path"});
2265 :     delete($row->{"Number of essentials"});
2266 :     delete($row->{"Essentials"});
2267 :     delete($row->{"Length"});
2268 :     for (my $j=0; $j < @{$row->{Reactions}}; $j++) {
2269 :     my @array = split(/,/,$row->{Reactions}->[$j]);
2270 :     $row->{"Length"}->[$j] = @array;
2271 :     if (!defined($row->{"Shortest path"}->[0]) || $row->{"Length"}->[$j] < $row->{"Shortest path"}->[0]) {
2272 :     $row->{"Shortest path"}->[0] = $row->{"Length"}->[$j];
2273 :     }
2274 :     $row->{"Number of essentials"}->[0] = 0;
2275 :     for (my $k=0; $k < @array;$k++) {
2276 :     if ($array[$k] =~ m/(rxn\d\d\d\d\d)/) {
2277 :     my $class = $self->get_reaction_class($1,1);
2278 :     my $temp = $row->{Media}->[0].":Essential";
2279 :     if ($class =~ m/$temp/) {
2280 :     $row->{"Number of essentials"}->[$j]++;
2281 :     if (!defined($row->{"Essentials"}->[$j]) && length($row->{"Essentials"}->[$j]) > 0) {
2282 :     $row->{"Essentials"}->[$j] = $array[$k];
2283 :     } else {
2284 :     $row->{"Essentials"}->[$j] .= ",".$array[$k];
2285 :     }
2286 :     }
2287 :     }
2288 :     }
2289 :     }
2290 :     }
2291 :     }
2292 :     }
2293 :     $tbl->save();
2294 : chenry 1.2 }
2295 :    
2296 :     =head3 calculate_growth
2297 :     Definition:
2298 :     string::growth = FIGMODELmodel->calculate_growth(string:media);
2299 :     Description:
2300 :     Calculating growth in the input media
2301 :     =cut
2302 :     sub calculate_growth {
2303 :     my ($self,$Media) = @_;
2304 :     my $UniqueFilename = $self->figmodel()->filename();
2305 :     system($self->figmodel()->GenerateMFAToolkitCommandLineCall($UniqueFilename,$self->id(),$Media,["ProductionMFA"],{"optimize metabolite production if objective is zero" => 1},$self->id()."-".$Media."-GrowthTest.txt",undef,$self->selected_version()));
2306 :     my $ProblemReport = $self->figmodel()->LoadProblemReport($UniqueFilename);
2307 :     my $Result;
2308 :     if (defined($ProblemReport)) {
2309 :     my $Row = $ProblemReport->get_row(0);
2310 :     if (defined($Row) && defined($Row->{"Objective"}->[0])) {
2311 :     if ($Row->{"Objective"}->[0] < 0.00000001) {
2312 :     $Result = "NOGROWTH:".$Row->{"Individual metabolites with zero production"}->[0];
2313 :     } else {
2314 :     $Result = $Row->{"Objective"}->[0];
2315 :     }
2316 :     }
2317 :     }
2318 :     return $Result;
2319 :     }
2320 :    
2321 :     =head3 classify_model_reactions
2322 :     Definition:
2323 :     (FIGMODELTable:Reaction classes,FIGMODELTable:Compound classes) = FIGMODELmodel->classify_model_reactions(string:media);
2324 :     Description:
2325 :     This function uses the MFAToolkit to minimize and maximize the flux through every reaction in the input model during minimal growth on the input media.
2326 :     The results are returned in a hash of strings where the keys are the reaction IDs and the strings are structured as follows: "Class;Min flux;Max flux".
2327 :     Possible values for "Class" include:
2328 :     1.) Positive: these reactions are essential in the forward direction.
2329 :     2.) Negative: these reactions are essential in the reverse direction.
2330 :     3.) Positive variable: these reactions are nonessential, but they only ever proceed in the forward direction.
2331 :     4.) Negative variable: these reactions are nonessential, but they only ever proceed in the reverse direction.
2332 :     5.) Variable: these reactions are nonessential and proceed in the forward or reverse direction.
2333 :     6.) Blocked: these reactions never carry any flux at all in the media condition tested.
2334 :     7.) Dead: these reactions are disconnected from the network.
2335 :     =cut
2336 :     sub classify_model_reactions {
2337 : chenry 1.3 my ($self,$Media,$SaveChanges) = @_;
2338 : chenry 1.2
2339 :     #Getting unique file for printing model output
2340 :     my $UniqueFilename = $self->figmodel()->filename();
2341 :     #Running the MFAToolkit
2342 :     system($self->figmodel()->GenerateMFAToolkitCommandLineCall($UniqueFilename,$self->id(),$Media,["ProductionMFA"],{"identify dead ends" => 1,"find tight bounds" => 1,"MFASolver" => "GLPK"},"Classify-".$self->id().$self->selected_version()."-".$UniqueFilename.".log",undef,$self->selected_version()));
2343 :     #Reading in the output bounds file
2344 :     my ($ReactionTB,$CompoundTB,$DeadCompounds,$DeadEndCompounds,$DeadReactions);
2345 :     if (-e $self->config("MFAToolkit output directory")->[0].$UniqueFilename."/MFAOutput/TightBoundsReactionData0.txt") {
2346 :     $ReactionTB = $self->figmodel()->database()->load_table($self->config("MFAToolkit output directory")->[0].$UniqueFilename."/MFAOutput/TightBoundsReactionData0.txt",";","|",1,["DATABASE ID"]);
2347 :     }
2348 :     if (-e $self->config("MFAToolkit output directory")->[0].$UniqueFilename."/MFAOutput/TightBoundsCompoundData0.txt") {
2349 :     $CompoundTB = $self->figmodel()->database()->load_table($self->config("MFAToolkit output directory")->[0].$UniqueFilename."/MFAOutput/TightBoundsCompoundData0.txt",";","|",1,["DATABASE ID"]);
2350 :     }
2351 :     if (-e $self->config("MFAToolkit output directory")->[0].$UniqueFilename."/DeadReactions.txt") {
2352 :     $DeadReactions = $self->figmodel()->put_array_in_hash($self->figmodel()->database()->load_single_column_file($self->config("MFAToolkit output directory")->[0].$UniqueFilename."/DeadReactions.txt",""));
2353 :     }
2354 :     if (-e $self->config("MFAToolkit output directory")->[0].$UniqueFilename."/DeadMetabolites.txt") {
2355 :     $DeadCompounds = $self->figmodel()->put_array_in_hash($self->figmodel()->database()->load_single_column_file($self->config("MFAToolkit output directory")->[0].$UniqueFilename."/DeadMetabolites.txt",""));
2356 :     }
2357 :     if (-e $self->config("MFAToolkit output directory")->[0].$UniqueFilename."/DeadEndMetabolites.txt") {
2358 :     $DeadEndCompounds = $self->figmodel()->put_array_in_hash($self->figmodel()->database()->load_single_column_file($self->config("MFAToolkit output directory")->[0].$UniqueFilename."/DeadEndMetabolites.txt",""));
2359 :     }
2360 :     if (!defined($ReactionTB) && !defined($CompoundTB)) {
2361 :     print STDERR "FIGMODEL:ClassifyModelReactions: Classification file not found when classifying reactions in ".$self->id().$self->selected_version()." with ".$Media." media. Most likely the model did not grow.\n";
2362 :     return (undef,undef);
2363 :     }
2364 :    
2365 :     #Clearing output
2366 :     $self->figmodel()->clearing_output($UniqueFilename,"Classify-".$self->id().$self->selected_version()."-".$UniqueFilename.".log");
2367 :     #Creating the table objects that will hold the results of the reaction classification
2368 :     my $rxnclasstable = $self->reaction_class_table();
2369 :     my $cpdclasstable = $self->compound_class_table();
2370 :     #Loading the compound table
2371 :     if (defined($CompoundTB)) {
2372 :     for (my $i=0; $i < $CompoundTB->size(); $i++) {
2373 :     my $Row = $CompoundTB->get_row($i);
2374 :     if (defined($Row->{"DATABASE ID"})) {
2375 :     #Getting the compound row
2376 :     my $CpdRow = $cpdclasstable->get_row_by_key($Row->{"DATABASE ID"}->[0].$Row->{COMPARTMENT}->[0],"COMPOUND",1);
2377 :     #Setting row values
2378 :     my $Max = 0;
2379 :     my $Min = 0;
2380 :     my $Class = "Unknown";
2381 :     if (defined($DeadCompounds) && defined($DeadCompounds->{$Row->{"DATABASE ID"}->[0]})) {
2382 :     $Class = "Dead";
2383 :     } elsif (defined($DeadEndCompounds) && defined($DeadEndCompounds->{$Row->{"DATABASE ID"}->[0]})) {
2384 :     $Class = "Deadend";
2385 :     } elsif (defined($Row->{"Min DRAIN_FLUX"}) && defined($Row->{"Max DRAIN_FLUX"}) && $Row->{"Min DRAIN_FLUX"}->[0] ne "1e+07") {
2386 :     $Max = $Row->{"Max DRAIN_FLUX"}->[0];
2387 :     $Min = $Row->{"Min DRAIN_FLUX"}->[0];
2388 :     if ($Row->{"Min DRAIN_FLUX"}->[0] > 0.00000001) {
2389 :     $Class = "Positive";
2390 :     } elsif ($Row->{"Max DRAIN_FLUX"}->[0] < -0.00000001) {
2391 :     $Class = "Negative";
2392 :     } elsif ($Row->{"Min DRAIN_FLUX"}->[0] < -0.00000001) {
2393 :     if ($Row->{"Max DRAIN_FLUX"}->[0] > 0.00000001) {
2394 :     $Class = "Variable";
2395 :     } else {
2396 :     $Max = 0;
2397 :     $Class = "Negative variable";
2398 :     }
2399 :     } elsif ($Row->{"Max DRAIN_FLUX"}->[0] > 0.00000001) {
2400 :     $Min = 0;
2401 :     $Class = "Positive variable";
2402 :     } else {
2403 :     $Min = 0;
2404 :     $Max = 0;
2405 :     $Class = "Blocked";
2406 :     }
2407 :     }
2408 :     my $index = 0;
2409 :     if (defined($CpdRow->{MEDIA})) {
2410 :     for (my $i=0; $i < @{$CpdRow->{MEDIA}};$i++) {
2411 :     $index++;
2412 :     if ($CpdRow->{MEDIA}->[$i] eq $Media) {
2413 :     $index = $i;
2414 :     last;
2415 :     }
2416 :     }
2417 :     }
2418 :     $CpdRow->{MIN}->[$index] = $Min;
2419 :     $CpdRow->{MAX}->[$index] = $Max;
2420 :     $CpdRow->{CLASS}->[$index] = $Class;
2421 :     $CpdRow->{MEDIA}->[$index] = $Media;
2422 :     }
2423 :     }
2424 : chenry 1.3 if (!defined($SaveChanges) || $SaveChanges == 1) {
2425 :     $cpdclasstable->save();
2426 :     }
2427 : chenry 1.2 }
2428 :     if (defined($ReactionTB)) {
2429 :     for (my $i=0; $i < $ReactionTB->size(); $i++) {
2430 :     my $Row = $ReactionTB->get_row($i);
2431 :     if (defined($Row->{"DATABASE ID"})) {
2432 :     #Getting the compound row
2433 :     my $Compartment = "c";
2434 :     if (defined($Row->{COMPARTMENT}->[0])) {
2435 :     $Compartment = $Row->{COMPARTMENT}->[0];
2436 :     }
2437 :     my $RxnRow = $rxnclasstable->get_row_by_key($Row->{"DATABASE ID"}->[0],"REACTION",1);
2438 :     my $Max = 0;
2439 :     my $Min = 0;
2440 :     my $Class = "Unknown";
2441 :     if (defined($DeadReactions) && defined($DeadReactions->{$Row->{"DATABASE ID"}->[0]})) {
2442 :     $Class = "Dead";
2443 :     } elsif (defined($Row->{"Min FLUX"}) && defined($Row->{"Max FLUX"})) {
2444 :     $Max = $Row->{"Max FLUX"}->[0];
2445 :     $Min = $Row->{"Min FLUX"}->[0];
2446 :     if ($Row->{"Min FLUX"}->[0] > 0.00000001) {
2447 :     $Class = "Positive";
2448 :     } elsif ($Row->{"Max FLUX"}->[0] < -0.00000001) {
2449 :     $Class = "Negative";
2450 :     } elsif ($Row->{"Min FLUX"}->[0] < -0.00000001) {
2451 :     if ($Row->{"Max FLUX"}->[0] > 0.00000001) {
2452 :     $Class = "Variable";
2453 :     } else {
2454 :     $Max = 0;
2455 :     $Class = "Negative variable";
2456 :     }
2457 :     } elsif ($Row->{"Max FLUX"}->[0] > 0.00000001) {
2458 :     $Min = 0;
2459 :     $Class = "Positive variable";
2460 :     } else {
2461 :     $Min = 0;
2462 :     $Max = 0;
2463 :     $Class = "Blocked";
2464 :     }
2465 :     }
2466 :     my $index = 0;
2467 :     if (defined($RxnRow->{MEDIA})) {
2468 :     for (my $i=0; $i < @{$RxnRow->{MEDIA}};$i++) {
2469 :     $index++;
2470 :     if ($RxnRow->{MEDIA}->[$i] eq $Media) {
2471 :     $index = $i;
2472 :     last;
2473 :     }
2474 :     }
2475 :     }
2476 :     $RxnRow->{MIN}->[$index] = $Min;
2477 :     $RxnRow->{MAX}->[$index] = $Max;
2478 :     $RxnRow->{CLASS}->[$index] = $Class;
2479 :     $RxnRow->{MEDIA}->[$index] = $Media;
2480 :     }
2481 :     }
2482 : chenry 1.3 if (!defined($SaveChanges) || $SaveChanges == 1) {
2483 :     $rxnclasstable->save();
2484 :     }
2485 : chenry 1.2 }
2486 :     return ($rxnclasstable,$cpdclasstable);
2487 :     }
2488 :    
2489 :     =head3 RunAllStudiesWithDataFast
2490 :     Definition:
2491 :     (integer::false positives,integer::false negatives,integer::correct negatives,integer::correct positives,string::error vector,string heading vector) = FIGMODELmodel->RunAllStudiesWithDataFast(string::experiment,0/1::print result);
2492 :     Description:
2493 :     Simulates every experimental condition currently available for the model.
2494 :     =cut
2495 :    
2496 :     sub RunAllStudiesWithDataFast {
2497 :     my ($self,$Experiment,$PrintResults) = @_;
2498 :    
2499 :     #Printing lp and key file for model
2500 :     if (!-e $self->directory()."FBA-".$self->id().$self->selected_version().".lp") {
2501 :     $self->PrintModelLPFile();
2502 :     }
2503 :     my $UniqueFilename = $self->figmodel()->filename();
2504 :    
2505 :     #Determing the simulations that need to be run
2506 :     my $ExperimentalDataTable = $self->figmodel()->GetExperimentalDataTable($self->genome(),$Experiment);
2507 :     #Creating the table of jobs to submit
2508 :     my $JobArray = $self->GetSimulationJobTable($ExperimentalDataTable,$Experiment,$UniqueFilename);
2509 :     #Printing the job file
2510 :     if (!-d $self->config("MFAToolkit output directory")->[0].$UniqueFilename."/") {
2511 :     system("mkdir ".$self->config("MFAToolkit output directory")->[0].$UniqueFilename."/");
2512 :     }
2513 :     $JobArray->save();
2514 :    
2515 :     #Running simulations
2516 :     system($self->config("mfalite executable")->[0]." ".$self->config("Reaction database directory")->[0]."masterfiles/MediaTable.txt ".$self->config("MFAToolkit output directory")->[0].$UniqueFilename."/Jobfile.txt ".$self->config("MFAToolkit output directory")->[0].$UniqueFilename."/Output.txt");
2517 :     #Parsing the results
2518 :     my $Results = $self->figmodel()->database()->load_table($self->config("MFAToolkit output directory")->[0].$UniqueFilename."/Output.txt",";","\\|",0,undef);
2519 :     if (!defined($Results)) {
2520 :     $self->figmodel()->error_message("FIGMODELmodel:RunAllStudiesWithDataFast:Could not find simulation results: ".$self->config("MFAToolkit output directory")->[0].$UniqueFilename."/Output.txt");
2521 :     return undef;
2522 :     }
2523 :     my ($FalsePostives,$FalseNegatives,$CorrectNegatives,$CorrectPositives,$Errorvector,$HeadingVector,$SimulationResults) = $self->EvaluateSimulationResults($Results,$ExperimentalDataTable);
2524 :     #Printing results to file
2525 :     $self->figmodel()->database()->save_table($SimulationResults,undef,undef,undef,"False negatives\tFalse positives\tCorrect negatives\tCorrect positives\n".$FalseNegatives."\t".$FalsePostives."\t".$CorrectNegatives."\t".$CorrectPositives."\n");
2526 :     $self->figmodel()->clearing_output($UniqueFilename);
2527 :    
2528 :     return ($FalsePostives,$FalseNegatives,$CorrectNegatives,$CorrectPositives,$Errorvector,$HeadingVector);
2529 :     }
2530 :    
2531 :     =head3 GetSimulationJobTable
2532 :     Definition:
2533 :     my $JobTable = $model->GetSimulationJobTable($Experiment,$PrintResults,$Version);
2534 :     Description:
2535 :     =cut
2536 :    
2537 :     sub GetSimulationJobTable {
2538 :     my ($self,$SimulationTable,$Experiment,$Folder) = @_;
2539 :    
2540 :     #Determing the simulations that need to be run
2541 :     if (!defined($SimulationTable)) {
2542 :     $SimulationTable = $self->figmodel()->GetExperimentalDataTable($self->genome(),$Experiment);
2543 :     if (!defined($SimulationTable)) {
2544 :     return undef;
2545 :     }
2546 :     }
2547 :    
2548 :     #Creating the job table
2549 :     my $JobTable = $self->figmodel()->CreateJobTable($Folder);
2550 :     for (my $i=0; $i < $SimulationTable->size(); $i++) {
2551 :     if ($SimulationTable->get_row($i)->{"Heading"}->[0] =~ m/Gene\sKO/) {
2552 :     my $Row = $JobTable->get_row_by_key("Gene KO","LABEL",1);
2553 :     $JobTable->add_data($Row,"MEDIA",$SimulationTable->get_row($i)->{"Media"}->[0],1);
2554 :     } elsif ($SimulationTable->get_row($i)->{"Heading"}->[0] =~ m/Media\sgrowth/) {
2555 :     my $Row = $JobTable->get_row_by_key("Growth phenotype","LABEL",1);
2556 :     $JobTable->add_data($Row,"MEDIA",$SimulationTable->get_row($i)->{"Media"}->[0],1);
2557 :     } elsif ($SimulationTable->get_row($i)->{"Heading"}->[0] =~ m/Interval\sKO/) {
2558 :     my $Row = $JobTable->get_row_by_key($SimulationTable->get_row($i)->{"Heading"}->[0],"LABEL",1);
2559 :     $JobTable->add_data($Row,"MEDIA",$SimulationTable->get_row($i)->{"Media"}->[0],1);
2560 :     $JobTable->add_data($Row,"GENE KO",$SimulationTable->get_row($i)->{"Experiment type"}->[0],1);
2561 :     }
2562 :     }
2563 :    
2564 :     #Filling in model specific elements of the job table
2565 :     for (my $i=0; $i < $JobTable->size(); $i++) {
2566 :     if ($JobTable->get_row($i)->{"LABEL"}->[0] =~ m/Gene\sKO/) {
2567 :     $JobTable->get_row($i)->{"RUNTYPE"}->[0] = "SINGLEKO";
2568 :     $JobTable->get_row($i)->{"SAVE NONESSENTIALS"}->[0] = 1;
2569 :     } else {
2570 :     $JobTable->get_row($i)->{"RUNTYPE"}->[0] = "GROWTH";
2571 :     $JobTable->get_row($i)->{"SAVE NONESSENTIALS"}->[0] = 0;
2572 :     }
2573 :     $JobTable->get_row($i)->{"LP FILE"}->[0] = $self->directory()."FBA-".$self->id().$self->selected_version();
2574 :     $JobTable->get_row($i)->{"MODEL"}->[0] = $self->directory().$self->id().$self->selected_version().".txt";
2575 :     $JobTable->get_row($i)->{"SAVE FLUXES"}->[0] = 0;
2576 :     }
2577 :    
2578 :     return $JobTable;
2579 :     }
2580 :    
2581 :     =head3 EvaluateSimulationResults
2582 :     Definition:
2583 :     (integer::false positives,integer::false negatives,integer::correct negatives,integer::correct positives,string::error vector,string heading vector,FIGMODELtable::simulation results) = FIGMODELmodel->EvaluateSimulationResults(FIGMODELtable::raw simulation results,FIGMODELtable::experimental data);
2584 :     Description:
2585 :     Compares simulation results with experimental data to produce a table indicating where predictions are incorrect.
2586 :     =cut
2587 :    
2588 :     sub EvaluateSimulationResults {
2589 :     my ($self,$Results,$ExperimentalDataTable) = @_;
2590 :    
2591 :     #Comparing experimental results with simulation results
2592 :     my $SimulationResults = FIGMODELTable->new(["Run result","Experiment type","Media","Experiment ID","Reactions knocked out"],$self->directory()."SimulationOutput".$self->id().$self->selected_version().".txt",["Experiment ID","Media"],"\t",",",undef);
2593 :     my $FalsePostives = 0;
2594 :     my $FalseNegatives = 0;
2595 :     my $CorrectNegatives = 0;
2596 :     my $CorrectPositives = 0;
2597 :     my @Errorvector;
2598 :     my @HeadingVector;
2599 :     my $ReactionKOWithGeneHash;
2600 :     for (my $i=0; $i < $Results->size(); $i++) {
2601 :     if ($Results->get_row($i)->{"LABEL"}->[0] eq "Gene KO") {
2602 :     if (defined($Results->get_row($i)->{"REACTION KO WITH GENES"})) {
2603 :     for (my $j=0; $j < @{$Results->get_row($i)->{"REACTION KO WITH GENES"}}; $j++) {
2604 :     my @Temp = split(/:/,$Results->get_row($i)->{"REACTION KO WITH GENES"}->[$j]);
2605 :     if (defined($Temp[1]) && length($Temp[1]) > 0) {
2606 :     $ReactionKOWithGeneHash->{$Temp[0]} = $Temp[1];
2607 :     }
2608 :     }
2609 :     }
2610 :     if ($Results->get_row($i)->{"OBJECTIVE"}->[0] == 0) {
2611 :     for (my $j=0; $j < @{$Results->get_row($i)->{"NONESSENTIALGENES"}}; $j++) {
2612 :     my $Row = $ExperimentalDataTable->get_row_by_key("Gene KO:".$Results->get_row($i)->{"MEDIA"}->[0].":".$Results->get_row($i)->{"NONESSENTIALGENES"}->[$j],"Heading");
2613 :     if (defined($Row)) {
2614 :     my $KOReactions = "none";
2615 :     if (defined($ReactionKOWithGeneHash->{$Results->get_row($i)->{"NONESSENTIALGENES"}->[$j]})) {
2616 :     $KOReactions = $ReactionKOWithGeneHash->{$Results->get_row($i)->{"NONESSENTIALGENES"}->[$j]};
2617 :     }
2618 :     push(@HeadingVector,$Row->{"Heading"}->[0].":".$KOReactions);
2619 :     my $Status = "Unknown";
2620 :     if ($Row->{"Growth"}->[0] > 0) {
2621 :     $Status = "False negative";
2622 :     $FalseNegatives++;
2623 :     push(@Errorvector,3);
2624 :     } else {
2625 :     $Status = "False positive";
2626 :     $FalsePostives++;
2627 :     push(@Errorvector,2);
2628 :     }
2629 :     $SimulationResults->add_row({"Run result" => [$Status],"Experiment type" => ["Gene KO"],"Media" => [$Row->{"Media"}->[0]],"Experiment ID" => [$Row->{"Experiment ID"}->[0]],"Reactions knocked out" => [$KOReactions]});
2630 :     }
2631 :     }
2632 :     } else {
2633 :     for (my $j=0; $j < @{$Results->get_row($i)->{"ESSENTIALGENES"}}; $j++) {
2634 :     #print $j."\t".$Results->get_row($i)->{"ESSENTIALGENES"}->[$j]."\n";
2635 :     my $Row = $ExperimentalDataTable->get_row_by_key("Gene KO:".$Results->get_row($i)->{"MEDIA"}->[0].":".$Results->get_row($i)->{"ESSENTIALGENES"}->[$j],"Heading");
2636 :     if (defined($Row)) {
2637 :     my $KOReactions = "none";
2638 :     if (defined($ReactionKOWithGeneHash->{$Results->get_row($i)->{"ESSENTIALGENES"}->[$j]})) {
2639 :     $KOReactions = $ReactionKOWithGeneHash->{$Results->get_row($i)->{"ESSENTIALGENES"}->[$j]};
2640 :     }
2641 :     push(@HeadingVector,$Row->{"Heading"}->[0].":".$KOReactions);
2642 :     my $Status = "Unknown";
2643 :     if ($Row->{"Growth"}->[0] > 0) {
2644 :     $Status = "False negative";
2645 :     $FalseNegatives++;
2646 :     push(@Errorvector,3);
2647 :     } else {
2648 :     $Status = "Correct negative";
2649 :     $CorrectNegatives++;
2650 :     push(@Errorvector,1);
2651 :     }
2652 :     $SimulationResults->add_row({"Run result" => [$Status],"Experiment type" => ["Gene KO"],"Media" => [$Row->{"Media"}->[0]],"Experiment ID" => [$Row->{"Experiment ID"}->[0]],"Reactions knocked out" => [$KOReactions]});
2653 :     }
2654 :     }
2655 :     for (my $j=0; $j < @{$Results->get_row($i)->{"NONESSENTIALGENES"}}; $j++) {
2656 :     my $Row = $ExperimentalDataTable->get_row_by_key("Gene KO:".$Results->get_row($i)->{"MEDIA"}->[0].":".$Results->get_row($i)->{"NONESSENTIALGENES"}->[$j],"Heading");
2657 :     if (defined($Row)) {
2658 :     my $KOReactions = "none";
2659 :     if (defined($ReactionKOWithGeneHash->{$Results->get_row($i)->{"NONESSENTIALGENES"}->[$j]})) {
2660 :     $KOReactions = $ReactionKOWithGeneHash->{$Results->get_row($i)->{"NONESSENTIALGENES"}->[$j]};
2661 :     }
2662 :     push(@HeadingVector,$Row->{"Heading"}->[0].":".$KOReactions);
2663 :     my $Status = "Unknown";
2664 :     if ($Row->{"Growth"}->[0] > 0) {
2665 :     $Status = "Correct positive";
2666 :     $CorrectPositives++;
2667 :     push(@Errorvector,0);
2668 :     } else {
2669 :     $Status = "False positive";
2670 :     $FalsePostives++;
2671 :     push(@Errorvector,2);
2672 :     }
2673 :     $SimulationResults->add_row({"Run result" => [$Status],"Experiment type" => ["Gene KO"],"Media" => [$Row->{"Media"}->[0]],"Experiment ID" => [$Row->{"Experiment ID"}->[0]],"Reactions knocked out" => [$KOReactions]});
2674 :     }
2675 :     }
2676 :     }
2677 :     } elsif ($Results->get_row($i)->{"LABEL"}->[0] eq "Growth phenotype") {
2678 :     my $Row = $ExperimentalDataTable->get_row_by_key("Media growth:".$Results->get_row($i)->{"MEDIA"}->[0].":".$Results->get_row($i)->{"MEDIA"}->[0],"Heading");
2679 :     if (defined($Row)) {
2680 :     push(@HeadingVector,$Row->{"Heading"}->[0].":none");
2681 :     my $Status = "Unknown";
2682 :     if ($Row->{"Growth"}->[0] > 0) {
2683 :     if ($Results->get_row($i)->{"OBJECTIVE"}->[0] > 0) {
2684 :     $Status = "Correct positive";
2685 :     $CorrectPositives++;
2686 :     push(@Errorvector,0);
2687 :     } else {
2688 :     $Status = "False negative";
2689 :     $FalseNegatives++;
2690 :     push(@Errorvector,3);
2691 :     }
2692 :     } else {
2693 :     if ($Results->get_row($i)->{"OBJECTIVE"}->[0] > 0) {
2694 :     $Status = "False positive";
2695 :     $FalsePostives++;
2696 :     push(@Errorvector,2);
2697 :     } else {
2698 :     $Status = "Correct negative";
2699 :     $CorrectNegatives++;
2700 :     push(@Errorvector,1);
2701 :     }
2702 :     }
2703 :     $SimulationResults->add_row({"Run result" => [$Status],"Experiment type" => ["Media growth"],"Media" => [$Row->{"Media"}->[0]],"Experiment ID" => [$Row->{"Media"}->[0]],"Reactions knocked out" => ["none"]});
2704 :     }
2705 :     } elsif ($Results->get_row($i)->{"LABEL"}->[0] =~ m/Interval\sKO/ && defined($Results->get_row($i)->{"KOGENES"}->[0])) {
2706 :     my $Row = $ExperimentalDataTable->get_row_by_key($Results->get_row($i)->{"LABEL"}->[0],"Heading");
2707 :     if (defined($Row)) {
2708 :     my $Status = "Unknown";
2709 :     if ($Row->{"Growth"}->[0] > 0) {
2710 :     if ($Results->get_row($i)->{"OBJECTIVE"}->[0] > 0) {
2711 :     $Status = "Correct positive";
2712 :     $CorrectPositives++;
2713 :     push(@Errorvector,0);
2714 :     } else {
2715 :     $Status = "False negative";
2716 :     $FalseNegatives++;
2717 :     push(@Errorvector,3);
2718 :     }
2719 :     } else {
2720 :     if ($Results->get_row($i)->{"OBJECTIVE"}->[0] > 0) {
2721 :     $Status = "False positive";
2722 :     $FalsePostives++;
2723 :     push(@Errorvector,2);
2724 :     } else {
2725 :     $Status = "Correct negative";
2726 :     $CorrectNegatives++;
2727 :     push(@Errorvector,1);
2728 :     }
2729 :     }
2730 :     $SimulationResults->add_row({"Run result" => [$Status],"Experiment type" => ["Interval KO"],"Media" => [$Row->{"Media"}->[0]],"Experiment ID" => [$Row->{"Experiment ID"}->[0]],"Reactions knocked out" => ["none"]});
2731 :     }
2732 :     }
2733 :     }
2734 :    
2735 :     return ($FalsePostives,$FalseNegatives,$CorrectNegatives,$CorrectPositives,join(";",@Errorvector),join(";",@HeadingVector),$SimulationResults);
2736 :     }
2737 :    
2738 :     =head3 InspectSolution
2739 :     Definition:
2740 :     $model->InspectSolution(string::gene knocked out,string::media condition,[string]::list of reactions);
2741 :     Description:
2742 :     =cut
2743 :    
2744 :     sub InspectSolution {
2745 :     my ($self,$GeneKO,$Media,$ReactionList) = @_;
2746 :    
2747 :     #Getting a directory for the results
2748 :     my $UniqueFilename = $self->figmodel()->filename();
2749 :     system("mkdir ".$self->config("MFAToolkit output directory")->[0].$UniqueFilename."/");
2750 :     my $TempVersion = "V".$UniqueFilename;
2751 :    
2752 :     #Setting gene ko to none if no genes are to be knocked out
2753 :     if ($GeneKO !~ m/^peg\./) {
2754 :     $GeneKO = "none";
2755 :     }
2756 :    
2757 :     #Implementing the input solution in the test model
2758 :     my $ReactionArray;
2759 :     my $DirectionArray;
2760 :     my %SolutionHash;
2761 :     for (my $k=0; $k < @{$ReactionList}; $k++) {
2762 :     if ($ReactionList->[$k] =~ m/(.+)(rxn\d\d\d\d\d)/) {
2763 :     my $Reaction = $2;
2764 :     my $Sign = $1;
2765 :     if (defined($SolutionHash{$Reaction})) {
2766 :     $SolutionHash{$Reaction} = "<=>";
2767 :     } elsif ($Sign eq "-") {
2768 :     $SolutionHash{$Reaction} = "<=";
2769 :     } elsif ($Sign eq "+") {
2770 :     $SolutionHash{$Reaction} = "=>";
2771 :     } else {
2772 :     $SolutionHash{$Reaction} = $Sign;
2773 :     }
2774 :     }
2775 :     }
2776 :     my @TempList = keys(%SolutionHash);
2777 :     for (my $k=0; $k < @TempList; $k++) {
2778 :     push(@{$ReactionArray},$TempList[$k]);
2779 :     push(@{$DirectionArray},$SolutionHash{$TempList[$k]});
2780 :     }
2781 :    
2782 :     print "Integrating solution!\n";
2783 :     $self->figmodel()->IntegrateGrowMatchSolution($self->id().$self->selected_version(),$self->directory().$self->id().$TempVersion.".txt",$ReactionArray,$DirectionArray,"SolutionInspection",1,1);
2784 :    
2785 :     #Printing lp and key file for model
2786 :     $self->PrintModelLPFile();
2787 :    
2788 :     #Running FBA on the test model
2789 :     my $JobTable = $self->figmodel()->CreateJobTable($UniqueFilename);
2790 :     $JobTable->add_row({"LABEL" => ["TEST"],"RUNTYPE" => ["GROWTH"],"LP FILE" => [$self->directory()."FBA-".$self->id().$TempVersion],"MODEL" => [$self->directory().$self->id().$TempVersion.".txt"],"MEDIA" => [$Media],"REACTION KO" => ["none|".join("|",@{$ReactionList})],"GENE KO" => [$GeneKO],"SAVE FLUXES" => [0],"SAVE NONESSENTIALS" => [0]});
2791 :     $JobTable->save();
2792 :    
2793 :     #Running simulations
2794 :     system($self->config("mfalite executable")->[0]." ".$self->config("Reaction database directory")->[0]."masterfiles/MediaTable.txt ".$self->config("MFAToolkit output directory")->[0].$UniqueFilename."/Jobfile.txt ".$self->config("MFAToolkit output directory")->[0].$UniqueFilename."/Output.txt");
2795 :    
2796 :     #Parsing the results
2797 :     my $Results = $self->figmodel()->database()->load_table($self->config("MFAToolkit output directory")->[0].$UniqueFilename."/Output.txt",";","\\|",0,undef);
2798 :     if (!defined($Results)) {
2799 :     $self->figmodel()->error_message("FIGMODELmodel:InspectSolution:Could not load problem report ".$self->config("MFAToolkit output directory")->[0].$UniqueFilename."/Output.txt");
2800 :     return undef;
2801 :     }
2802 :    
2803 :     #Making sure that the model grew with all reactions present
2804 :     my $Found = 0;
2805 :     for (my $i=0; $i < $Results->size(); $i++) {
2806 :     if (defined($Results->get_row($i)->{"KOGENES"}->[0]) && defined($Results->get_row($i)->{"KOREACTIONS"}->[0]) && $Results->get_row($i)->{"KOREACTIONS"}->[0] eq "none" && $Results->get_row($i)->{"KOGENES"}->[0] eq $GeneKO && $Results->get_row($i)->{"OBJECTIVE"}->[0] > 0.00001) {
2807 :     $Found = 1;
2808 :     }
2809 :     }
2810 :     if ($Found == 0) {
2811 :     print "Solution no longer valid\n";
2812 :     return undef;
2813 :     }
2814 :    
2815 :     #Making sure all of the reactions added are still necessary
2816 :     my $FinalReactionList;
2817 :     for (my $k=0; $k < $Results->size(); $k++) {
2818 :     if (defined($Results->get_row($k)->{"KOGENES"}->[0]) && $Results->get_row($k)->{"KOGENES"}->[0] eq $GeneKO) {
2819 :     if (defined($Results->get_row($k)->{"KOREACTIONS"}->[0]) && $Results->get_row($k)->{"KOREACTIONS"}->[0] =~ m/rxn\d\d\d\d\d/ && $Results->get_row($k)->{"OBJECTIVE"}->[0] < 0.000001) {
2820 :     push(@{$FinalReactionList},$Results->get_row($k)->{"KOREACTIONS"}->[0]);
2821 :     }
2822 :     }
2823 :     }
2824 :    
2825 :     #Deleting extra files created
2826 :     unlink($self->directory()."FBA-".$self->id().$TempVersion.".lp");
2827 :     unlink($self->directory()."FBA-".$self->id().$TempVersion.".key");
2828 :     unlink($self->directory().$self->id().$TempVersion.".txt");
2829 :    
2830 :     #Deleting the test model and the MFA folder
2831 :     $self->figmodel()->clearing_output($UniqueFilename);
2832 :    
2833 :     return $FinalReactionList;
2834 :     }
2835 :    
2836 :     =head3 GapFillingAlgorithm
2837 :    
2838 :     Definition:
2839 :     FIGMODELmodel->GapFillingAlgorithm();
2840 :    
2841 :     Description:
2842 :     This is a wrapper for running the gap filling algorithm on any model in the database.
2843 :     The algorithm performs a gap filling for any false negative prediction of the avialable experimental data.
2844 :     This function is threaded to improve efficiency: one thread does nothing but using the MFAToolkit to fill gaps for every false negative prediction.
2845 :     The other thread reads in the gap filling solutions, builds a test model for each solution, and runs the test model against all available experimental data.
2846 :     This function prints two important output files in the Model directory:
2847 :     1.) GapFillingOutput.txt: this is a summary of the results of the gap filling analysis
2848 :     2.) GapFillingErrorMatrix.txt: this lists the correct and incorrect predictions for each gapfilling solution implemented in a test model.
2849 :     =cut
2850 :    
2851 :     sub GapFillingAlgorithm {
2852 :     my ($self) = @_;
2853 :    
2854 :     #First the input model version and model filename should be simulated and the false negatives identified
2855 :     my ($FalsePostives,$FalseNegatives,$CorrectNegatives,$CorrectPositives,$Errorvector,$HeadingVector) = $self->RunAllStudiesWithDataFast("All");
2856 :    
2857 :     #Getting the filename
2858 :     my $UniqueFilename = $self->figmodel()->filename();
2859 :    
2860 :     #Printing the original performance vector
2861 :     $self->figmodel()->database()->print_array_to_file($self->directory().$self->id().$self->selected_version()."-OPEM".".txt",[$HeadingVector,$Errorvector]);
2862 :    
2863 :     my $PreviousGapFilling;
2864 :     if (-e $self->directory().$self->id().$self->selected_version()."-GFS.txt") {
2865 :     #Backing up the old solution file
2866 :     system("cp ".$self->directory().$self->id().$self->selected_version()."-GFS.txt ".$self->directory().$self->id().$self->selected_version()."-OldGFS.txt");
2867 :     unlink($self->directory().$self->id().$self->selected_version()."-GFS.txt");
2868 :     }
2869 :     if (-e $self->directory().$self->id().$self->selected_version()."-OldGFS.txt") {
2870 :     #Reading in the solution file from the previous gap filling if it exists
2871 :     $PreviousGapFilling = $self->figmodel()->database()->load_table($self->directory().$self->id().$self->selected_version()."-OldGFS.txt",";",",",0,["Experiment"]);
2872 :     }
2873 :    
2874 :     #Now we use the simulation output to make the gap filling run data
2875 :     my @Errors = split(/;/,$Errorvector);
2876 :     my @Headings = split(/;/,$HeadingVector);
2877 :     my $GapFillingRunSpecs = "";
2878 :     my $Count = 0;
2879 :     my $RescuedPreviousResults;
2880 :     my $RunCount = 0;
2881 :     my $SolutionExistedCount = 0;
2882 :     my $AcceptedSolutions = 0;
2883 :     my $RejectedSolutions = 0;
2884 :     my $NoExistingSolutions = 0;
2885 :     for (my $i=0; $i < @Errors; $i++) {
2886 :     if ($Errors[$i] == 3) {
2887 :     my @HeadingDataArray = split(/:/,$Headings[$i]);
2888 :     if ($HeadingDataArray[2] !~ m/^peg\./ || $HeadingDataArray[3] ne "none") {
2889 :     my $SolutionFound = 0;
2890 :     if (defined($PreviousGapFilling) && defined($PreviousGapFilling->get_row_by_key($HeadingDataArray[2],"Experiment"))) {
2891 :     my @Rows = $PreviousGapFilling->get_rows_by_key($HeadingDataArray[2],"Experiment");
2892 :     for (my $j=0; $j < @Rows; $j++) {
2893 :     if ($HeadingDataArray[2] =~ m/^peg\./) {
2894 :     my $ReactionList = $self->InspectSolution($HeadingDataArray[2],$HeadingDataArray[1],$Rows[$j]->{"Solution reactions"});
2895 :     if (defined($ReactionList)) {
2896 :     print join(",",@{$Rows[$j]->{"Solution reactions"}})."\t".join(",",@{$ReactionList})."\n";
2897 :     $SolutionFound++;
2898 :     push(@{$RescuedPreviousResults},$Rows[$j]->{"Experiment"}->[0].";".$Rows[$j]->{"Solution index"}->[0].";".$Rows[$j]->{"Solution cost"}->[0].";".join(",",@{$ReactionList}));
2899 :     $AcceptedSolutions++;
2900 :     } else {
2901 :     $RejectedSolutions++;
2902 :     }
2903 :     } else {
2904 :     my $ReactionList = $self->InspectSolution($HeadingDataArray[2],$HeadingDataArray[1],$Rows[$j]->{"Solution reactions"});
2905 :     if (defined($ReactionList)) {
2906 :     print join(",",@{$Rows[$j]->{"Solution reactions"}})."\t".join(",",@{$ReactionList})."\n";
2907 :     $SolutionFound++;
2908 :     push(@{$RescuedPreviousResults},$Rows[$j]->{"Experiment"}->[0].";".$Rows[$j]->{"Solution index"}->[0].";".$Rows[$j]->{"Solution cost"}->[0].";".join(",",@{$ReactionList}));
2909 :     $AcceptedSolutions++;
2910 :     } else {
2911 :     $RejectedSolutions++;
2912 :     }
2913 :     }
2914 :     }
2915 :     } else {
2916 :     $NoExistingSolutions++;
2917 :     }
2918 :     if ($SolutionFound == 0) {
2919 :     $RunCount++;
2920 :     if (length($GapFillingRunSpecs) > 0) {
2921 :     $GapFillingRunSpecs .= ";";
2922 :     }
2923 : chenry 1.5 $GapFillingRunSpecs .= $HeadingDataArray[2].":".$HeadingDataArray[1].":".$HeadingDataArray[3];
2924 : chenry 1.2 } else {
2925 :     $SolutionExistedCount++;
2926 :     }
2927 :     }
2928 :     $Count++;
2929 :     }
2930 :     }
2931 :    
2932 :     #Updating the growmatch progress table
2933 :     my $Row = $self->figmodel()->database()->get_row_by_key("GROWMATCH TABLE",$self->genome(),"ORGANISM",1);
2934 :     $Row->{"INITIAL FP"}->[0] = $FalsePostives;
2935 :     $Row->{"INITIAL FN"}->[0] = $FalseNegatives;
2936 :     $Row->{"GF TIMING"}->[0] = time()."-";
2937 :     $Row->{"FN WITH SOL"}->[0] = $FalseNegatives-$NoExistingSolutions;
2938 :     $Row->{"FN WITH ACCEPTED SOL"}->[0] = $SolutionExistedCount;
2939 :     $Row->{"TOTAL ACCEPTED GF SOL"}->[0] = $AcceptedSolutions;
2940 :     $Row->{"TOTAL REJECTED GF SOL"}->[0] = $RejectedSolutions;
2941 :     $Row->{"FN WITH NO SOL"}->[0] = $NoExistingSolutions+$RejectedSolutions;
2942 :     $self->figmodel()->database()->update_row("GROWMATCH TABLE",$Row,"ORGANISM");
2943 :    
2944 :     #Running the gap filling once to correct all false negative errors
2945 :     my $SolutionsFound = 0;
2946 :     my $GapFillingArray;
2947 :     push(@{$GapFillingArray},split(/;/,$GapFillingRunSpecs));
2948 : chenry 1.5 my $GapFillingResults = $self->datagapfill($GapFillingArray,"GFS");
2949 :     if (defined($GapFillingResults)) {
2950 :     $SolutionsFound = 1;
2951 :     }
2952 : chenry 1.2
2953 :     if (defined($RescuedPreviousResults) && @{$RescuedPreviousResults} > 0) {
2954 :     #Printing previous solutions to GFS file
2955 :     $self->figmodel()->database()->print_array_to_file($self->directory().$self->id().$self->selected_version()."-GFS.txt",$RescuedPreviousResults,1);
2956 :     $SolutionsFound = 1;
2957 :     }
2958 :    
2959 :     #Recording the finishing of the gapfilling
2960 :     $Row = $self->figmodel()->database()->get_row_by_key("GROWMATCH TABLE",$self->genome(),"ORGANISM",1);
2961 :     $Row->{"GF TIMING"}->[0] .= time();
2962 :     $self->figmodel()->database()->update_row("GROWMATCH TABLE",$Row,"ORGANISM");
2963 :    
2964 :     if ($SolutionsFound == 1) {
2965 :     #Scheduling solution testing
2966 :     $self->figmodel()->add_job_to_queue("testsolutions?".$self->id().$self->selected_version()."?-1?GF","QSUB","fast","master","BACK");
2967 :     } else {
2968 :     $self->figmodel()->error_message("No false negative predictions found. Data gap filling not necessary!");
2969 :     }
2970 :    
2971 :     return $self->success();
2972 :     }
2973 :    
2974 : chenry 1.5 =head3 SolutionReconciliation
2975 :     Definition:
2976 :     FIGMODELmodel->SolutionReconciliation();
2977 :     Description:
2978 :     This is a wrapper for running the solution reconciliation algorithm on any model in the database.
2979 :     The algorithm performs a reconciliation of any gap filling solutions to identify the combination of solutions that results in the optimal model.
2980 :     This function prints out one output file in the Model directory: ReconciliationOutput.txt: this is a summary of the results of the reconciliation analysis
2981 :     =cut
2982 :    
2983 :     sub SolutionReconciliation {
2984 :     my ($self,$GapFill,$Stage) = @_;
2985 :    
2986 :     #Setting the output filenames
2987 :     my $OutputFilename;
2988 :     my $OutputFilenameTwo;
2989 :     if ($GapFill == 1) {
2990 :     $OutputFilename = $self->directory().$self->id().$self->selected_version()."-GFReconciliation.txt";
2991 :     $OutputFilenameTwo = $self->directory().$self->id().$self->selected_version()."-GFSRS.txt";
2992 :     } else {
2993 :     $OutputFilename = $self->directory().$self->id().$self->selected_version()."-GGReconciliation.txt";
2994 :     $OutputFilenameTwo = $self->directory().$self->id().$self->selected_version()."-GGSRS.txt";
2995 :     }
2996 :    
2997 :     #In stage one, we run the reconciliation and create a test file to check combined solution performance
2998 :     if (!defined($Stage) || $Stage == 1) {
2999 :     my $GrowMatchTable = $self->figmodel()->database()->LockDBTable("GROWMATCH TABLE");
3000 :     my $Row = $GrowMatchTable->get_row_by_key($self->genome(),"ORGANISM",1);
3001 :     $Row->{"GF RECONCILATION TIMING"}->[0] = time()."-";
3002 :     $GrowMatchTable->save();
3003 :     $self->figmodel()->database()->UnlockDBTable("GROWMATCH TABLE");
3004 :    
3005 :     #Getting a unique filename
3006 :     my $UniqueFilename = $self->figmodel()->filename();
3007 :    
3008 :     #Copying over the necessary files
3009 :     if ($GapFill == 1) {
3010 :     if (!-e $self->directory().$self->id().$self->selected_version()."-GFEM.txt") {
3011 :     print STDERR "FIGMODEL:SolutionReconciliation:".$self->directory().$self->id().$self->selected_version()."-GFEM.txt file not found. Could not reconcile!";
3012 :     return 0;
3013 :     }
3014 :     if (!-e $self->directory().$self->id().$self->selected_version()."-OPEM.txt") {
3015 :     print STDERR "FIGMODEL:SolutionReconciliation:".$self->directory().$self->id().$self->selected_version()."-OPEM.txt file not found. Could not reconcile!";
3016 :     return 0;
3017 :     }
3018 :     system("cp ".$self->directory().$self->id().$self->selected_version()."-GFEM.txt ".$self->figmodel()->config("MFAToolkit input files")->[0].$UniqueFilename."-GFEM.txt");
3019 :     system("cp ".$self->directory().$self->id().$self->selected_version()."-OPEM.txt ".$self->figmodel()->config("MFAToolkit input files")->[0].$UniqueFilename."-OPEM.txt");
3020 :     #Backing up and deleting the existing reconciliation file
3021 :     if (-e $OutputFilename) {
3022 :     system("cp ".$OutputFilename." ".$self->directory().$self->id().$self->selected_version()."-OldGFReconciliation.txt");
3023 :     unlink($OutputFilename);
3024 :     }
3025 :     } else {
3026 :     if (!-e $self->directory().$self->id().$self->selected_version()."-GGEM.txt") {
3027 :     print STDERR "FIGMODEL:SolutionReconciliation:".$self->directory().$self->id().$self->selected_version()."-GGEM.txt file not found. Could not reconcile!";
3028 :     return 0;
3029 :     }
3030 :     if (!-e $self->directory().$self->id().$self->selected_version()."-GGOPEM.txt") {
3031 :     print STDERR "FIGMODEL:SolutionReconciliation:".$self->directory().$self->id().$self->selected_version()."-GGOPEM.txt file not found. Could not reconcile!";
3032 :     return 0;
3033 :     }
3034 :     system("cp ".$self->directory().$self->id().$self->selected_version()."-GGEM.txt ".$self->figmodel()->config("MFAToolkit input files")->[0].$UniqueFilename."-GGEM.txt");
3035 :     system("cp ".$self->directory().$self->id().$self->selected_version()."-GGOPEM.txt ".$self->figmodel()->config("MFAToolkit input files")->[0].$UniqueFilename."-OPEM.txt");
3036 :     #Backing up and deleting the existing reconciliation file
3037 :     if (-e $OutputFilename) {
3038 :     system("cp ".$OutputFilename." ".$self->directory().$self->id().$self->selected_version()."-OldGGReconciliation.txt");
3039 :     unlink($OutputFilename);
3040 :     }
3041 :     }
3042 :    
3043 :     #Running the reconciliation
3044 :     system($self->figmodel()->GenerateMFAToolkitCommandLineCall($UniqueFilename,$self->id(),"NONE",["SolutionReconciliation"],{"Solution data for model optimization" => $UniqueFilename},"Reconciliation".$UniqueFilename.".log",undef,$self->selected_version()));
3045 :     $GrowMatchTable = $self->figmodel()->database()->LockDBTable("GROWMATCH TABLE");
3046 :     $Row = $GrowMatchTable->get_row_by_key($self->genome(),"ORGANISM",1);
3047 :     $Row->{"GF RECONCILATION TIMING"}->[0] .= time();
3048 :     $GrowMatchTable->save();
3049 :     $self->figmodel()->database()->UnlockDBTable("GROWMATCH TABLE");
3050 :    
3051 :     #Loading the problem report from the reconciliation run
3052 :     my $ReconciliatonOutput = $self->figmodel()->LoadProblemReport($UniqueFilename);
3053 :     print $UniqueFilename."\n";
3054 :     #Clearing output files
3055 :     $self->figmodel()->clearing_output($UniqueFilename,"Reconciliation".$UniqueFilename.".log");
3056 :     $ReconciliatonOutput->save("/home/chenry/Test.txt");
3057 :    
3058 :     #Checking the a problem report was found and was loaded
3059 :     if (!defined($ReconciliatonOutput) || $ReconciliatonOutput->size() < 1 || !defined($ReconciliatonOutput->get_row(0)->{"Notes"}->[0])) {
3060 :     print STDERR "FIGMODEL:SolutionReconciliation: MFAToolkit output from SolutionReconciliation of ".$self->id()." not found!\n\n";
3061 :     return 0;
3062 :     }
3063 :    
3064 :     #Processing the solutions
3065 :     my $SolutionCount = 0;
3066 :     my $ReactionSetHash;
3067 :     my $SingleReactionHash;
3068 :     my $ReactionDataHash;
3069 :     for (my $n=0; $n < $ReconciliatonOutput->size(); $n++) {
3070 :     if (defined($ReconciliatonOutput->get_row($n)->{"Notes"}->[0]) && $ReconciliatonOutput->get_row($n)->{"Notes"}->[0] =~ m/^Recursive\sMILP\s([^;]+)/) {
3071 :     #Breaking up the solution into reaction sets
3072 :     my @ReactionSets = split(/\|/,$1);
3073 :     #Creating reaction lists for each set
3074 :     my $SolutionHash;
3075 :     for (my $i=0; $i < @ReactionSets; $i++) {
3076 :     if (length($ReactionSets[$i]) > 0) {
3077 :     my @Alternatives = split(/:/,$ReactionSets[$i]);
3078 :     for (my $j=1; $j < @Alternatives; $j++) {
3079 :     if (length($Alternatives[$j]) > 0) {
3080 :     push(@{$SolutionHash->{$Alternatives[$j]}},$Alternatives[0]);
3081 :     }
3082 :     }
3083 :     if (@Alternatives == 1) {
3084 :     $SingleReactionHash->{$Alternatives[0]}->{$SolutionCount} = 1;
3085 :     if (!defined($SingleReactionHash->{$Alternatives[0]}->{"COUNT"})) {
3086 :     $SingleReactionHash->{$Alternatives[0]}->{"COUNT"} = 0;
3087 :     }
3088 :     $SingleReactionHash->{$Alternatives[0]}->{"COUNT"}++;
3089 :     }
3090 :     }
3091 :     }
3092 :     #Identifying reactions sets and storing the sets in the reactions set hash
3093 :     foreach my $Solution (keys(%{$SolutionHash})) {
3094 :     my $SetKey = join(",",sort(@{$SolutionHash->{$Solution}}));
3095 :     if (!defined($ReactionSetHash->{$SetKey}->{$SetKey}->{$SolutionCount})) {
3096 :     $ReactionSetHash->{$SetKey}->{$SetKey}->{$SolutionCount} = 1;
3097 :     if (!defined($ReactionSetHash->{$SetKey}->{$SetKey}->{"COUNT"})) {
3098 :     $ReactionSetHash->{$SetKey}->{$SetKey}->{"COUNT"} = 0;
3099 :     }
3100 :     $ReactionSetHash->{$SetKey}->{$SetKey}->{"COUNT"}++;
3101 :     }
3102 :     $ReactionSetHash->{$SetKey}->{$Solution}->{$SolutionCount} = 1;
3103 :     if (!defined($ReactionSetHash->{$SetKey}->{$Solution}->{"COUNT"})) {
3104 :     $ReactionSetHash->{$SetKey}->{$Solution}->{"COUNT"} = 0;
3105 :     }
3106 :     $ReactionSetHash->{$SetKey}->{$Solution}->{"COUNT"}++;
3107 :     }
3108 :     $SolutionCount++;
3109 :     }
3110 :     }
3111 :    
3112 :     #Handling the scenario where no solutions were found
3113 :     if ($SolutionCount == 0) {
3114 :     print STDERR "FIGMODEL:SolutionReconciliation: Reconciliation unsuccessful. No solution found.\n\n";
3115 :     return 0;
3116 :     }
3117 :    
3118 :     #Printing results without solution performance figures. Also printing solution test file
3119 :     open (RECONCILIATION, ">$OutputFilename");
3120 :     #Printing the file heading
3121 :     print RECONCILIATION "DATABASE;DEFINITION;REVERSIBLITY;DELTAG;DIRECTION;NUMBER OF SOLUTIONS";
3122 :     for (my $i=0; $i < $SolutionCount; $i++) {
3123 :     print RECONCILIATION ";Solution ".$i;
3124 :     }
3125 :     print RECONCILIATION "\n";
3126 :     #Printing the singlet reactions first
3127 :     my $Solutions;
3128 :     print RECONCILIATION "SINGLET REACTIONS\n";
3129 :     my @SingletReactions = keys(%{$SingleReactionHash});
3130 :     for (my $j=0; $j < $SolutionCount; $j++) {
3131 :     $Solutions->[$j]->{"BASE"} = $j;
3132 :     }
3133 :     for (my $i=0; $i < @SingletReactions; $i++) {
3134 :     my $ReactionData;
3135 :     if (defined($ReactionDataHash->{$SingletReactions[$i]})) {
3136 :     $ReactionData = $ReactionDataHash->{$SingletReactions[$i]};
3137 :     } else {
3138 :     my $Direction = substr($SingletReactions[$i],0,1);
3139 :     if ($Direction eq "+") {
3140 :     $Direction = "=>";
3141 :     } else {
3142 :     $Direction = "<=";
3143 :     }
3144 :     my $Reaction = substr($SingletReactions[$i],1);
3145 :     $ReactionData = FIGMODELObject->load($self->figmodel()->config("reaction directory")->[0].$Reaction,"\t");
3146 :     $ReactionData->{"DIRECTIONS"}->[0] = $Direction;
3147 :     $ReactionData->{"REACTIONS"}->[0] = $Reaction;
3148 :     if (!defined($ReactionData->{"DEFINITION"}->[0])) {
3149 :     $ReactionData->{"DEFINITION"}->[0] = "UNKNOWN";
3150 :     }
3151 :     if (!defined($ReactionData->{"THERMODYNAMIC REVERSIBILITY"}->[0])) {
3152 :     $ReactionData->{"THERMODYNAMIC REVERSIBILITY"}->[0] = "UNKNOWN";
3153 :     }
3154 :     if (!defined($ReactionData->{"DELTAG"}->[0])) {
3155 :     $ReactionData->{"DELTAG"}->[0] = "UNKNOWN";
3156 :     }
3157 :     $ReactionDataHash->{$SingletReactions[$i]} = $ReactionData;
3158 :     }
3159 :     print RECONCILIATION $ReactionData->{"REACTIONS"}->[0].";".$ReactionData->{"DEFINITION"}->[0].";".$ReactionData->{"THERMODYNAMIC REVERSIBILITY"}->[0].";".$ReactionData->{"DELTAG"}->[0].";".$ReactionData->{"DIRECTIONS"}->[0].";".$SingleReactionHash->{$SingletReactions[$i]}->{"COUNT"};
3160 :     for (my $j=0; $j < $SolutionCount; $j++) {
3161 :     print RECONCILIATION ";";
3162 :     if (defined($SingleReactionHash->{$SingletReactions[$i]}->{$j})) {
3163 :     $Solutions->[$j]->{$SingletReactions[$i]} = 1;
3164 :     $Solutions->[$j]->{"BASE"} = $j;
3165 :     print RECONCILIATION "|".$j."|";
3166 :     }
3167 :     }
3168 :     print RECONCILIATION "\n";
3169 :     }
3170 :     #Printing the reaction sets with alternatives
3171 :     print RECONCILIATION "Reaction sets with alternatives\n";
3172 :     my @ReactionSets = keys(%{$ReactionSetHash});
3173 :     foreach my $ReactionSet (@ReactionSets) {
3174 :     my $NewSolutions;
3175 :     my $BaseReactions;
3176 :     my $AltList = [$ReactionSet];
3177 :     push(@{$AltList},keys(%{$ReactionSetHash->{$ReactionSet}}));
3178 :     for (my $j=0; $j < @{$AltList}; $j++) {
3179 :     my $CurrentNewSolutions;
3180 :     my $Index;
3181 :     if ($j == 0) {
3182 :     print RECONCILIATION "NEW SET\n";
3183 :     } elsif ($AltList->[$j] ne $ReactionSet) {
3184 :     print RECONCILIATION "ALTERNATIVE SET\n";
3185 :     #For each base solution in which this set is represented, we copy the base solution to the new solution
3186 :     my $NewSolutionCount = 0;
3187 :     for (my $k=0; $k < $SolutionCount; $k++) {
3188 :     if (defined($ReactionSetHash->{$ReactionSet}->{$AltList->[$j]}->{$k})) {
3189 :     if (defined($Solutions)) {
3190 :     $Index->{$k} = @{$Solutions} + $NewSolutionCount;
3191 :     } else {
3192 :     $Index->{$k} = $NewSolutionCount;
3193 :     }
3194 :     if (defined($NewSolutions) && @{$NewSolutions} > 0) {
3195 :     $Index->{$k} += @{$NewSolutions};
3196 :     }
3197 :     $CurrentNewSolutions->[$NewSolutionCount] = {};
3198 :     foreach my $Reaction (keys(%{$Solutions->[$k]})) {
3199 :     $CurrentNewSolutions->[$NewSolutionCount]->{$Reaction} = $Solutions->[$k]->{$Reaction};
3200 :     }
3201 :     $NewSolutionCount++;
3202 :     }
3203 :     }
3204 :     }
3205 :     if ($j == 0 || $AltList->[$j] ne $ReactionSet) {
3206 :     my @SingletReactions = split(/,/,$AltList->[$j]);
3207 :     for (my $i=0; $i < @SingletReactions; $i++) {
3208 :     #Adding base reactions to base solutions and set reactions the new solutions
3209 :     if ($j == 0) {
3210 :     push(@{$BaseReactions},$SingletReactions[$i]);
3211 :     } else {
3212 :     for (my $k=0; $k < @{$CurrentNewSolutions}; $k++) {
3213 :     $CurrentNewSolutions->[$k]->{$SingletReactions[$i]} = 1;
3214 :     }
3215 :     }
3216 :     #Getting reaction data and printing reaction in output file
3217 :     my $ReactionData;
3218 :     if (defined($ReactionDataHash->{$SingletReactions[$i]})) {
3219 :     $ReactionData = $ReactionDataHash->{$SingletReactions[$i]};
3220 :     } else {
3221 :     my $Direction = substr($SingletReactions[$i],0,1);
3222 :     if ($Direction eq "+") {
3223 :     $Direction = "=>";
3224 :     } else {
3225 :     $Direction = "<=";
3226 :     }
3227 :     my $Reaction = substr($SingletReactions[$i],1);
3228 :     $ReactionData = FIGMODELObject->load($self->figmodel()->config("reaction directory")->[0].$Reaction,"\t");
3229 :     $ReactionData->{"DIRECTIONS"}->[0] = $Direction;
3230 :     $ReactionData->{"REACTIONS"}->[0] = $Reaction;
3231 :     if (!defined($ReactionData->{"DEFINITION"}->[0])) {
3232 :     $ReactionData->{"DEFINITION"}->[0] = "UNKNOWN";
3233 :     }
3234 :     if (!defined($ReactionData->{"THERMODYNAMIC REVERSIBILITY"}->[0])) {
3235 :     $ReactionData->{"THERMODYNAMIC REVERSIBILITY"}->[0] = "UNKNOWN";
3236 :     }
3237 :     if (!defined($ReactionData->{"DELTAG"}->[0])) {
3238 :     $ReactionData->{"DELTAG"}->[0] = "UNKNOWN";
3239 :     }
3240 :     $ReactionDataHash->{$SingletReactions[$i]} = $ReactionData;
3241 :     }
3242 :     print RECONCILIATION $ReactionData->{"REACTIONS"}->[0].";".$ReactionData->{"DEFINITION"}->[0].";".$ReactionData->{"THERMODYNAMIC REVERSIBILITY"}->[0].";".$ReactionData->{"DELTAG"}->[0].";".$ReactionData->{"DIRECTIONS"}->[0].";".$ReactionSetHash->{$ReactionSet}->{$AltList->[$j]}->{"COUNT"};
3243 :     for (my $k=0; $k < $SolutionCount; $k++) {
3244 :     print RECONCILIATION ";";
3245 :     if (defined($ReactionSetHash->{$ReactionSet}->{$AltList->[$j]}->{$k})) {
3246 :     if ($j == 0) {
3247 :     print RECONCILIATION "|".$k."|";
3248 :     } else {
3249 :     print RECONCILIATION "|".$Index->{$k}."|";
3250 :     }
3251 :     }
3252 :     }
3253 :     print RECONCILIATION "\n";
3254 :     }
3255 :     #Adding the current new solutions to the new solutions array
3256 :     if (defined($CurrentNewSolutions) && @{$CurrentNewSolutions} > 0) {
3257 :     push(@{$NewSolutions},@{$CurrentNewSolutions});
3258 :     }
3259 :     }
3260 :     }
3261 :     #Adding the base reactions to all existing solutions
3262 :     for (my $j=0; $j < @{$Solutions}; $j++) {
3263 :     if (defined($ReactionSetHash->{$ReactionSet}->{$ReactionSet}->{$Solutions->[$j]->{"BASE"}})) {
3264 :     foreach my $SingleReaction (@{$BaseReactions}) {
3265 :     $Solutions->[$j]->{$SingleReaction} = 1;
3266 :     }
3267 :     }
3268 :     }
3269 :     #Adding the new solutions to the set of existing solutions
3270 :     push(@{$Solutions},@{$NewSolutions});
3271 :     }
3272 :     close(RECONCILIATION);
3273 :     #Now printing a file that defines all of the solutions in a format the testsolutions function understands
3274 :     open (RECONCILIATION, ">$OutputFilenameTwo");
3275 :     print RECONCILIATION "Experiment;Solution index;Solution cost;Solution reactions\n";
3276 :     for (my $i=0; $i < @{$Solutions}; $i++) {
3277 :     delete($Solutions->[$i]->{"BASE"});
3278 :     print RECONCILIATION "SR".$i.";".$i.";10;".join(",",keys(%{$Solutions->[$i]}))."\n";
3279 :     }
3280 :     close(RECONCILIATION);
3281 :    
3282 :     $GrowMatchTable = $self->figmodel()->database()->LockDBTable("GROWMATCH TABLE");
3283 :     $Row = $GrowMatchTable->get_row_by_key($self->genome(),"ORGANISM",1);
3284 :     $Row->{"GF RECON TESTING TIMING"}->[0] = time()."-";
3285 :     $Row->{"GF RECON SOLUTIONS"}->[0] = @{$Solutions};
3286 :     $GrowMatchTable->save();
3287 :     $self->figmodel()->database()->UnlockDBTable("GROWMATCH TABLE");
3288 :    
3289 :     #Scheduling the solution testing
3290 :     if ($GapFill == 1) {
3291 :     system($self->figmodel()->config("scheduler executable")->[0]." \"add:testsolutions?".$self->id().$self->selected_version()."?-1?GFSR:BACK:fast:QSUB\"");
3292 :     } else {
3293 :     system($self->figmodel()->config("scheduler executable")->[0]." \"add:testsolutions?".$self->id().$self->selected_version()."?-1?GGSR:BACK:fast:QSUB\"");
3294 :     }
3295 :     } else {
3296 :     #Reading in the solution testing results
3297 :     my $Data;
3298 :     if ($GapFill == 1) {
3299 :     $Data = $self->figmodel()->database()->load_single_column_file($self->directory().$self->id().$self->selected_version()."-GFSREM.txt","");
3300 :     } else {
3301 :     $Data = $self->figmodel()->database()->load_single_column_file($self->directory().$self->id().$self->selected_version()."-GGSREM.txt","");
3302 :     }
3303 :    
3304 :     #Reading in the preliminate reconciliation report
3305 :     my $OutputData = $self->figmodel()->database()->load_single_column_file($OutputFilename,"");
3306 :     #Replacing the file tags with actual performance data
3307 :     my $Count = 0;
3308 :     for (my $i=0; $i < @{$Data}; $i++) {
3309 :     if ($Data->[$i] =~ m/^SR(\d+);.+;(\d+\/\d+);/) {
3310 :     my $Index = $1;
3311 :     my $Performance = $Index."/".$2;
3312 :     for (my $j=0; $j < @{$OutputData}; $j++) {
3313 :     $OutputData->[$j] =~ s/\|$Index\|/$Performance/g;
3314 :     }
3315 :     }
3316 :     }
3317 :     $self->figmodel()->database()->print_array_to_file($OutputFilename,$OutputData);
3318 :    
3319 :     my $GrowMatchTable = $self->figmodel()->database()->LockDBTable("GROWMATCH TABLE");
3320 :     my $Row = $GrowMatchTable->get_row_by_key($self->genome(),"ORGANISM",1);
3321 :     $Row->{"GF RECON TESTING TIMING"}->[0] .= time();
3322 :     $GrowMatchTable->save();
3323 :     $self->figmodel()->database()->UnlockDBTable("GROWMATCH TABLE");
3324 :     }
3325 :    
3326 :     return 1;
3327 :     }
3328 :    
3329 : chenry 1.2 =head3 BuildSpecificBiomassReaction
3330 :     Definition:
3331 :     FIGMODELmodel->BuildSpecificBiomassReaction();
3332 :     Description:
3333 :     =cut
3334 :     sub BuildSpecificBiomassReaction {
3335 :     my ($self) = @_;
3336 :    
3337 :     my $biomassrxn;
3338 :     my $OrganismID = $self->genome();
3339 :     #Checking for a biomass override
3340 :     if (defined($self->config("biomass reaction override")->{$OrganismID})) {
3341 :     $biomassrxn = $self->config("biomass reaction override")->{$OrganismID};
3342 :     print "Overriding biomass template and selecting ".$biomassrxn." for ".$OrganismID.".\n";
3343 :     } else {#Creating biomass reaction from the template
3344 :     #Getting the genome stats
3345 :     my $genomestats = $self->figmodel()->get_genome_stats($self->genome());
3346 :     my $Class = $genomestats->{CLASS}->[0];
3347 :     my $Name = $genomestats->{NAME}->[0];
3348 :    
3349 :     #Checking for phoenix variants
3350 :     my $PhoenixVariantTable = $self->figmodel()->database()->GetDBTable("Phoenix variants table");
3351 :     my $Phoenix = 0;
3352 :     my @Rows = $PhoenixVariantTable->get_rows_by_key($OrganismID,"GENOME");
3353 :     my $VariantHash;
3354 :     for (my $i=0; $i < @Rows; $i++) {
3355 :     $Phoenix = 1;
3356 :     if (defined($Rows[$i]->{"SUBSYSTEM"}) && defined($Rows[$i]->{"VARIANT"})) {
3357 :     $VariantHash->{$Rows[$i]->{"SUBSYSTEM"}->[0]} = $Rows[$i]->{"VARIANT"}->[0];
3358 :     }
3359 :     }
3360 :    
3361 :     #Collecting genome data
3362 :     my $RoleHash;
3363 :     my $FeatureTable = $self->figmodel()->GetGenomeFeatureTable($self->genome());
3364 :     for (my $i=0; $i < $FeatureTable->size(); $i++) {
3365 :     for (my $j=0; $j < @{$FeatureTable->get_row($i)->{"ROLES"}}; $j++) {
3366 :     $RoleHash->{$FeatureTable->get_row($i)->{"ROLES"}->[$j]} = 1;
3367 :     my $Subsystems = $self->figmodel()->subsystems_of_role($FeatureTable->get_row($i)->{"ROLES"}->[$j]);
3368 :     if (defined($Subsystems)) {
3369 :     for (my $k=0; $k < @{$Subsystems}; $k++) {
3370 :     if ($Phoenix == 0) {
3371 :     $VariantHash->{$Subsystems->[$k]} = 1;
3372 :     }
3373 :     }
3374 :     }
3375 :     }
3376 :     }
3377 :    
3378 :     #Scanning through the template item by item and determinine which biomass components should be added
3379 :     my $ComponentTypes;
3380 :     my $EquationData;
3381 :     my $BiomassReactionTemplateTable = $self->figmodel()->database()->GetDBTable("BIOMASS TEMPLATE");
3382 :     for (my $i=0; $i < $BiomassReactionTemplateTable->size(); $i++) {
3383 :     my $Row = $BiomassReactionTemplateTable->get_row($i);
3384 :     if (defined($Row->{"INCLUSION CRITERIA"}->[0]) && $Row->{"INCLUSION CRITERIA"}->[0] eq "UNIVERSAL") {
3385 :     push(@{$EquationData},$Row);
3386 :     $ComponentTypes->{$Row->{"CLASS"}->[0]}->{$Row->{"ID"}->[0]} = 1;
3387 :     } else {
3388 :     my $Criteria = $Row->{"INCLUSION CRITERIA"}->[0];
3389 :     my $End = 0;
3390 :     while ($End == 0) {
3391 :     if ($Criteria =~ m/^(.+)(AND)\{([^{^}]+)\}(.+)$/ || $Criteria =~ m/^(AND)\{([^{^}]+)\}$/ || $Criteria =~ m/^(.+)(OR)\{([^{^}]+)\}(.+)$/ || $Criteria =~ m/^(OR)\{([^{^}]+)\}$/) {
3392 :     print $Criteria."\n";
3393 :     my $Start = "";
3394 :     my $End = "";
3395 :     my $Condition = $1;
3396 :     my $Data = $2;
3397 :     if ($1 ne "AND" && $1 ne "OR") {
3398 :     $Start = $1;
3399 :     $End = $4;
3400 :     $Condition = $2;
3401 :     $Data = $3;
3402 :     }
3403 :     my $Result = "YES";
3404 :     if ($Condition eq "OR") {
3405 :     $Result = "NO";
3406 :     }
3407 :     my @Array = split(/\|/,$Data);
3408 :     for (my $j=0; $j < @Array; $j++) {
3409 :     if ($Array[$j] eq "YES" && $Condition eq "OR") {
3410 :     $Result = "YES";
3411 :     last;
3412 :     } elsif ($Array[$j] eq "NO" && $Condition eq "AND") {
3413 :     $Result = "NO";
3414 :     last;
3415 :     } elsif ($Array[$j] =~ m/^COMPOUND:(.+)/) {
3416 :     my $Match = 0;
3417 :     for (my $k=0; $k < @{$EquationData}; $k++) {
3418 :     if ($EquationData->[$k]->{"ID"}->[0] eq $1) {
3419 :     $Match = 1;
3420 :     last;
3421 :     }
3422 :     }
3423 :     if ($Match == 1 && $Condition eq "OR") {
3424 :     $Result = "YES";
3425 :     last;
3426 :     } elsif ($Match != 1 && $Condition eq "AND") {
3427 :     $Result = "NO";
3428 :     last;
3429 :     }
3430 :     } elsif ($Array[$j] =~ m/^!COMPOUND:(.+)/) {
3431 :     my $Match = 0;
3432 :     for (my $k=0; $k < @{$EquationData}; $k++) {
3433 :     if ($EquationData->[$k]->{"ID"}->[0] eq $1) {
3434 :     $Match = 1;
3435 :     last;
3436 :     }
3437 :     }
3438 :     if ($Match != 1 && $Condition eq "OR") {
3439 :     $Result = "YES";
3440 :     last;
3441 :     } elsif ($Match == 1 && $Condition eq "AND") {
3442 :     $Result = "NO";
3443 :     last;
3444 :     }
3445 :     } elsif ($Array[$j] =~ m/^NAME:(.+)/) {
3446 :     my $Comparison = $1;
3447 :     if ((!defined($Comparison) || !defined($Name) || $Name =~ m/$Comparison/) && $Condition eq "OR") {
3448 :     $Result = "YES";
3449 :     last;
3450 :     } elsif (defined($Comparison) && defined($Name) && $Name !~ m/$Comparison/ && $Condition eq "AND") {
3451 :     $Result = "NO";
3452 :     last;
3453 :     }
3454 :     } elsif ($Array[$j] =~ m/^!NAME:(.+)/) {
3455 :     my $Comparison = $1;
3456 :     if ((!defined($Comparison) || !defined($Name) || $Name !~ m/$Comparison/) && $Condition eq "OR") {
3457 :     $Result = "YES";
3458 :     last;
3459 :     } elsif (defined($Comparison) && defined($Name) && $Name =~ m/$Comparison/ && $Condition eq "AND") {
3460 :     $Result = "NO";
3461 :     last;
3462 :     }
3463 :     } elsif ($Array[$j] =~ m/^SUBSYSTEM:(.+)/) {
3464 :     my @SubsystemArray = split(/`/,$1);
3465 :     if (@SubsystemArray == 1) {
3466 :     if (defined($VariantHash->{$SubsystemArray[0]}) && $VariantHash->{$SubsystemArray[0]} ne -1 && $Condition eq "OR") {
3467 :     $Result = "YES";
3468 :     last;
3469 :     } elsif ((!defined($VariantHash->{$SubsystemArray[0]}) || $VariantHash->{$SubsystemArray[0]} eq -1) && $Condition eq "AND") {
3470 :     $Result = "NO";
3471 :     last;
3472 :     }
3473 :     } else {
3474 :     my $Match = 0;
3475 :     for (my $k=1; $k < @SubsystemArray; $k++) {
3476 :     if (defined($VariantHash->{$SubsystemArray[0]}) && $VariantHash->{$SubsystemArray[0]} eq $SubsystemArray[$k]) {
3477 :     $Match = 1;
3478 :     last;
3479 :     }
3480 :     }
3481 :     if ($Match == 1 && $Condition eq "OR") {
3482 :     $Result = "YES";
3483 :     last;
3484 :     } elsif ($Match != 1 && $Condition eq "AND") {
3485 :     $Result = "NO";
3486 :     last;
3487 :     }
3488 :     }
3489 :     } elsif ($Array[$j] =~ m/^!SUBSYSTEM:(.+)/) {
3490 :     my @SubsystemArray = split(/`/,$1);
3491 :     if (@SubsystemArray == 1) {
3492 :     if ((!defined($VariantHash->{$SubsystemArray[0]}) || $VariantHash->{$SubsystemArray[0]} eq -1) && $Condition eq "OR") {
3493 :     $Result = "YES";
3494 :     last;
3495 :     } elsif (defined($VariantHash->{$SubsystemArray[0]}) && $VariantHash->{$SubsystemArray[0]} ne -1 && $Condition eq "AND") {
3496 :     $Result = "NO";
3497 :     last;
3498 :     }
3499 :     } else {
3500 :     my $Match = 0;
3501 :     for (my $k=1; $k < @SubsystemArray; $k++) {
3502 :     if (defined($VariantHash->{$SubsystemArray[0]}) && $VariantHash->{$SubsystemArray[0]} eq $SubsystemArray[$k]) {
3503 :     $Match = 1;
3504 :     last;
3505 :     }
3506 :     }
3507 :     if ($Match != 1 && $Condition eq "OR") {
3508 :     $Result = "YES";
3509 :     last;
3510 :     } elsif ($Match == 1 && $Condition eq "AND") {
3511 :     $Result = "NO";
3512 :     last;
3513 :     }
3514 :     }
3515 :     } elsif ($Array[$j] =~ m/^ROLE:(.+)/) {
3516 :     if (defined($RoleHash->{$1}) && $Condition eq "OR") {
3517 :     $Result = "YES";
3518 :     last;
3519 :     } elsif (!defined($RoleHash->{$1}) && $Condition eq "AND") {
3520 :     $Result = "NO";
3521 :     last;
3522 :     }
3523 :     } elsif ($Array[$j] =~ m/^!ROLE:(.+)/) {
3524 :     if (!defined($RoleHash->{$1}) && $Condition eq "OR") {
3525 :     $Result = "YES";
3526 :     last;
3527 :     } elsif (defined($RoleHash->{$1}) && $Condition eq "AND") {
3528 :     $Result = "NO";
3529 :     last;
3530 :     }
3531 :     } elsif ($Array[$j] =~ m/^CLASS:(.+)/) {
3532 :     if ($Class eq $1 && $Condition eq "OR") {
3533 :     $Result = "YES";
3534 :     last;
3535 :     } elsif ($Class ne $1 && $Condition eq "AND") {
3536 :     $Result = "NO";
3537 :     last;
3538 :     }
3539 :     } elsif ($Array[$j] =~ m/^!CLASS:(.+)/) {
3540 :     if ($Class ne $1 && $Condition eq "OR") {
3541 :     $Result = "YES";
3542 :     last;
3543 :     } elsif ($Class eq $1 && $Condition eq "AND") {
3544 :     $Result = "NO";
3545 :     last;
3546 :     }
3547 :     }
3548 :     }
3549 :     $Criteria = $Start.$Result.$End;
3550 :     print $Criteria."\n";
3551 :     } else {
3552 :     $End = 1;
3553 :     last;
3554 :     }
3555 :     }
3556 :     if ($Criteria eq "YES") {
3557 :     push(@{$EquationData},$Row);
3558 :     $ComponentTypes->{$Row->{"CLASS"}->[0]}->{$Row->{"ID"}->[0]} = 1;
3559 :     }
3560 :     }
3561 :     }
3562 :    
3563 :     #Building biomass equation
3564 :     my %Reactants;
3565 :     my %Products;
3566 :     foreach my $EquationRow (@{$EquationData}) {
3567 :     #First determine what the coefficient should be
3568 :     my $Coefficient;
3569 :     if (defined($EquationRow->{"COEFFICIENT"}) && $EquationRow->{"COEFFICIENT"}->[0] =~ m/^[0-9\.]+$/) {
3570 :     $Coefficient = $EquationRow->{"COEFFICIENT"}->[0];
3571 :     } elsif (defined($EquationRow->{"COEFFICIENT"}) && $EquationRow->{"COEFFICIENT"}->[0] =~ m/cpd\d\d\d\d\d/) {
3572 :     $Coefficient = 0;
3573 :     my @CompoundList = split(/,/,$EquationRow->{"COEFFICIENT"}->[0]);
3574 :     foreach my $Compound (@CompoundList) {
3575 :     if (defined($Reactants{$Compound})) {
3576 :     $Coefficient += $Reactants{$Compound};
3577 :     }
3578 :     }
3579 :     } elsif (defined($EquationRow->{"COEFFICIENT"}) && $EquationRow->{"COEFFICIENT"}->[0] =~ m/^([0-9\.]+)\/(.+)$/) {
3580 :     my @Keys = keys(%{$ComponentTypes->{$2}});
3581 :     my $MW = 1;
3582 :     my $CompoundData = $self->figmodel()->LoadObject($EquationRow->{"ID"}->[0]);
3583 :     if (defined($CompoundData->{"MASS"})) {
3584 :     $MW = $CompoundData->{"MASS"}->[0];
3585 :     }
3586 :     $Coefficient = $1/@Keys/$MW;
3587 :     }
3588 :     if (defined($EquationRow->{"REACTANT"}) && $EquationRow->{"REACTANT"}->[0] eq "YES") {
3589 :     if (defined($Reactants{$EquationRow->{"ID"}->[0]})) {
3590 :     $Reactants{$EquationRow->{"ID"}->[0]} += $Coefficient;
3591 :     } elsif (defined($Products{$EquationRow->{"ID"}->[0]}) && $Products{$EquationRow->{"ID"}->[0]} > $Coefficient) {
3592 :     $Products{$EquationRow->{"ID"}->[0]} -= $Coefficient;
3593 :     } elsif (defined($Products{$EquationRow->{"ID"}->[0]}) && $Products{$EquationRow->{"ID"}->[0]} < $Coefficient) {
3594 :     $Reactants{$EquationRow->{"ID"}->[0]} = $Coefficient - $Products{$EquationRow->{"ID"}->[0]};
3595 :     delete $Products{$EquationRow->{"ID"}->[0]};
3596 :     } else {
3597 :     $Reactants{$EquationRow->{"ID"}->[0]} = $Coefficient;
3598 :     }
3599 :     } else {
3600 :     if (defined($Products{$EquationRow->{"ID"}->[0]})) {
3601 :     $Products{$EquationRow->{"ID"}->[0]} += $Coefficient;
3602 :     } elsif (defined($Reactants{$EquationRow->{"ID"}->[0]}) && $Reactants{$EquationRow->{"ID"}->[0]} > $Coefficient) {
3603 :     $Reactants{$EquationRow->{"ID"}->[0]} -= $Coefficient;
3604 :     } elsif (defined($Reactants{$EquationRow->{"ID"}->[0]}) && $Reactants{$EquationRow->{"ID"}->[0]} < $Coefficient) {
3605 :     $Products{$EquationRow->{"ID"}->[0]} = $Coefficient - $Reactants{$EquationRow->{"ID"}->[0]};
3606 :     delete $Reactants{$EquationRow->{"ID"}->[0]};
3607 :     } else {
3608 :     $Products{$EquationRow->{"ID"}->[0]} = $Coefficient;
3609 :     }
3610 :     }
3611 :     }
3612 :     my $Equation = "";
3613 :     my @ReactantList = sort(keys(%Reactants));
3614 :     for (my $i=0; $i < @ReactantList; $i++) {
3615 :     if (length($Equation) > 0) {
3616 :     $Equation .= " + ";
3617 :     }
3618 :     $Equation .= $self->figmodel()->format_coefficient($Reactants{$ReactantList[$i]})." ".$ReactantList[$i];
3619 :     }
3620 :     $Equation .= " => ";
3621 :     my $First = 1;
3622 :     @ReactantList = sort(keys(%Products));
3623 :     for (my $i=0; $i < @ReactantList; $i++) {
3624 :     if ($First == 0) {
3625 :     $Equation .= " + ";
3626 :     }
3627 :     $First = 0;
3628 :     $Equation .= $self->figmodel()->format_coefficient($Products{$ReactantList[$i]})." ".$ReactantList[$i];
3629 :     }
3630 :    
3631 :     #Adding the biomass equation to the biomass table
3632 :     my $NewRow = $self->figmodel()->add_biomass_reaction($Equation,$self->id(),"Template:".$self->genome());
3633 :     $biomassrxn = $NewRow->{DATABASE}->[0];
3634 :     print $biomassrxn."\n";
3635 :     }
3636 :     print $biomassrxn."\n";
3637 :     my $BiomassRow = $self->figmodel()->add_model_to_biomass_reaction($biomassrxn,$self->id());
3638 :     return $BiomassRow;
3639 :     }
3640 :    
3641 :     =head3 PrintSBMLFile
3642 :     Definition:
3643 :     FIGMODELmodel->PrintSBMLFile();
3644 :     Description:
3645 :     Printing file with model data in SBML format
3646 :     =cut
3647 :     sub PrintSBMLFile {
3648 :     my($self) = @_;
3649 :    
3650 :     #Opening the SBML file for printing
3651 :     my $Filename = $self->config("SBML files")->[0].$self->id().".xml";
3652 :     if ($self->owner() ne "master") {
3653 :     if (!-d $self->config("SBML files")->[0].$self->owner()."/") {
3654 :     system("mkdir ".$self->config("SBML files")->[0].$self->owner()."/");
3655 :     }
3656 :     $Filename = $self->config("SBML files")->[0].$self->owner()."/".$self->id().".xml";
3657 :     }
3658 :     if (!open (SBMLOUTPUT, ">$Filename")) {
3659 :     return;
3660 :     }
3661 :    
3662 :     #Loading and parsing the model data
3663 :     my $ModelTable = $self->reaction_table();
3664 :     if (!defined($ModelTable) || !defined($ModelTable->{"array"})) {
3665 :     print "Failed to load ".$self->id()."\n";
3666 :     return;
3667 :     }
3668 :    
3669 :     #Adding intracellular metabolites that also need exchange fluxes to the exchange hash
3670 :     my $ExchangeHash = {"cpd11416" => "c"};
3671 :    
3672 :     my %CompartmentsPresent;
3673 :     $CompartmentsPresent{"c"} = 1;
3674 :     my %CompoundList;
3675 :     my @ReactionList;
3676 :     my $ReactionTable = $self->figmodel()->database()->GetDBTable("REACTIONS");
3677 :     for (my $i=0; $i < $ModelTable->size(); $i++) {
3678 :     my $Reaction = $ModelTable->get_row($i)->{"LOAD"}->[0];
3679 :     if (defined($ReactionTable->get_row_by_key($Reaction,"DATABASE")) && defined($ReactionTable->get_row_by_key($Reaction,"DATABASE")->{"EQUATION"}->[0])) {
3680 :     push(@ReactionList,$Reaction);
3681 :     $_ = $ReactionTable->get_row_by_key($Reaction,"DATABASE")->{"EQUATION"}->[0];
3682 :     my @MatchArray = /(cpd\d\d\d\d\d)/g;
3683 :     for (my $j=0; $j < @MatchArray; $j++) {
3684 :     $CompoundList{$MatchArray[$j]}->{"c"} = 1;
3685 :     }
3686 :     $_ = $ReactionTable->get_row_by_key($Reaction,"DATABASE")->{"EQUATION"}->[0];
3687 :     @MatchArray = /(cpd\d\d\d\d\d\[\D\])/g;
3688 :     for (my $j=0; $j < @MatchArray; $j++) {
3689 :     if ($MatchArray[$j] =~ m/(cpd\d\d\d\d\d)\[(\D)\]/) {
3690 :     $CompartmentsPresent{lc($2)} = 1;
3691 :     $CompoundList{$1}->{lc($2)} = 1;
3692 :     }
3693 :     }
3694 :     }
3695 :     }
3696 :    
3697 :     #Printing header to SBML file
3698 :     my $ModelName = $self->id();
3699 :     $ModelName =~ s/\./_/;
3700 :     print SBMLOUTPUT '<?xml version="1.0" encoding="UTF-8"?>'."\n";
3701 :     print SBMLOUTPUT '<sbml xmlns="http://www.sbml.org/sbml/level2" level="2" version="1" xmlns:html="http://www.w3.org/1999/xhtml">' . "\n";
3702 :     if (defined($self->figmodel()->database()->GetDBTable("MODEL STATS")->{$self->id()}->[0]->{"Organism name"}->[0])) {
3703 :     print SBMLOUTPUT '<model id="'.$ModelName.'" name="'.$self->figmodel()->database()->GetDBTable("MODEL STATS")->{$self->id()}->[0]->{"Organism name"}->[0].' SEED model">'."\n";
3704 :     } else {
3705 :     print SBMLOUTPUT '<model id="'.$ModelName.'" name="'.$self->id().' SEED model">'."\n";
3706 :     }
3707 :    
3708 :     #Printing the unit data
3709 :     print SBMLOUTPUT "<listOfUnitDefinitions>\n";
3710 :     print SBMLOUTPUT "\t<unitDefinition id=\"mmol_per_gDW_per_hr\">\n";
3711 :     print SBMLOUTPUT "\t\t<listOfUnits>\n";
3712 :     print SBMLOUTPUT "\t\t\t<unit kind=\"mole\" scale=\"-3\"/>\n";
3713 :     print SBMLOUTPUT "\t\t\t<unit kind=\"gram\" exponent=\"-1\"/>\n";
3714 :     print SBMLOUTPUT "\t\t\t<unit kind=\"second\" multiplier=\".00027777\" exponent=\"-1\"/>\n";
3715 :     print SBMLOUTPUT "\t\t</listOfUnits>\n";
3716 :     print SBMLOUTPUT "\t</unitDefinition>\n";
3717 :     print SBMLOUTPUT "</listOfUnitDefinitions>\n";
3718 :    
3719 :     #Printing compartments for SBML file
3720 :     print SBMLOUTPUT '<listOfCompartments>'."\n";
3721 :     foreach my $Compartment (keys(%CompartmentsPresent)) {
3722 :     if (defined($self->figmodel()->database()->GetDBTable("COMPARTMENTS")->{$Compartment}->[0]->{"Name"}->[0])) {
3723 :     my @OutsideList = split(/\//,$self->figmodel()->database()->GetDBTable("COMPARTMENTS")->{$Compartment}->[0]->{"Outside"}->[0]);
3724 :     my $Printed = 0;
3725 :     foreach my $Outside (@OutsideList) {
3726 :     if (defined($CompartmentsPresent{$Outside}) && defined($self->figmodel()->database()->GetDBTable("COMPARTMENTS")->{$Outside}->[0]->{"Name"}->[0])) {
3727 :     print SBMLOUTPUT '<compartment id="'.$self->figmodel()->database()->GetDBTable("COMPARTMENTS")->{$Compartment}->[0]->{"Name"}->[0].'" outside="'.$self->figmodel()->database()->GetDBTable("COMPARTMENTS")->{$Outside}->[0]->{"Name"}->[0].'"/>'."\n";
3728 :     $Printed = 1;
3729 :     last;
3730 :     }
3731 :     }
3732 :     if ($Printed eq 0) {
3733 :     print SBMLOUTPUT '<compartment id="'.$self->figmodel()->database()->GetDBTable("COMPARTMENTS")->{$Compartment}->[0]->{"Name"}->[0].'"/>'."\n";
3734 :     }
3735 :     }
3736 :     }
3737 :     print SBMLOUTPUT '</listOfCompartments>'."\n";
3738 :    
3739 :     #Printing the list of metabolites involved in the model
3740 :     print SBMLOUTPUT '<listOfSpecies>'."\n";
3741 :     foreach my $Compound (keys(%CompoundList)) {
3742 :     my $Name = $Compound;
3743 :     my $Formula = "";
3744 :     if (defined($self->figmodel()->database()->GetDBTable("COMPOUNDS")->{$Compound}->[0]->{"FORMULA"}->[0])) {
3745 :     $Formula = $self->figmodel()->database()->GetDBTable("COMPOUNDS")->{$Compound}->[0]->{"FORMULA"}->[0];
3746 :     }
3747 :     if (defined($self->figmodel()->database()->GetDBTable("COMPOUNDS")->{$Compound}->[0]->{"NAME"}->[0])) {
3748 :     $Name = $self->figmodel()->database()->GetDBTable("COMPOUNDS")->{$Compound}->[0]->{"NAME"}->[0];
3749 :     $Name =~ s/\s/_/;
3750 :     $Name .= "_".$Formula;
3751 :     }
3752 :     $Name =~ s/[<>;&\*]//;
3753 :     my $Charge = 0;
3754 :     if (defined($self->figmodel()->database()->GetDBTable("COMPOUNDS")->{$Compound}->[0]->{"CHARGE"}->[0])) {
3755 :     $Charge = $self->figmodel()->database()->GetDBTable("COMPOUNDS")->{$Compound}->[0]->{"CHARGE"}->[0];
3756 :     }
3757 :     foreach my $Compartment (keys(%{$CompoundList{$Compound}})) {
3758 :     if ($Compartment eq "e") {
3759 :     $ExchangeHash->{$Compound} = "e";
3760 :     }
3761 :     print SBMLOUTPUT '<species id="'.$Compound.'_'.$Compartment.'" name="'.$Name.'" compartment="'.$self->figmodel()->database()->GetDBTable("COMPARTMENTS")->{$Compartment}->[0]->{"Name"}->[0].'" charge="'.$Charge.'" boundaryCondition="false"/>'."\n";
3762 :     }
3763 :     }
3764 :     #Printing the boundary species
3765 :     foreach my $Compound (keys(%{$ExchangeHash})) {
3766 :     my $Name = $Compound;
3767 :     my $Formula = "";
3768 :     if (defined($self->figmodel()->database()->GetDBTable("COMPOUNDS")->{$Compound}->[0]->{"FORMULA"}->[0])) {
3769 :     $Formula = $self->figmodel()->database()->GetDBTable("COMPOUNDS")->{$Compound}->[0]->{"FORMULA"}->[0];
3770 :     }
3771 :     if (defined($self->figmodel()->database()->GetDBTable("COMPOUNDS")->{$Compound}->[0]->{"NAME"}->[0])) {
3772 :     $Name = $self->figmodel()->database()->GetDBTable("COMPOUNDS")->{$Compound}->[0]->{"NAME"}->[0];
3773 :     $Name =~ s/\s/_/;
3774 :     $Name .= "_".$Formula;
3775 :     }
3776 :     $Name =~ s/[<>;&\*]//;
3777 :     my $Charge = 0;
3778 :     if (defined($self->figmodel()->database()->GetDBTable("COMPOUNDS")->{$Compound}->[0]->{"CHARGE"}->[0])) {
3779 :     $Charge = $self->figmodel()->database()->GetDBTable("COMPOUNDS")->{$Compound}->[0]->{"CHARGE"}->[0];
3780 :     }
3781 :     print SBMLOUTPUT '<species id="'.$Compound.'_b" name="'.$Name.'" compartment="Extracellular" charge="'.$Charge.'" boundaryCondition="true"/>'."\n";
3782 :     }
3783 :     print SBMLOUTPUT '</listOfSpecies>'."\n";
3784 :    
3785 :     #Printing the list of reactions involved in the model
3786 :     my $ObjectiveCoef;
3787 :     print SBMLOUTPUT '<listOfReactions>'."\n";
3788 :     foreach my $Reaction (@ReactionList) {
3789 :     $ObjectiveCoef = "0.0";
3790 :     if (defined($self->figmodel()->database()->GetDBTable("REACTIONS")->{$Reaction}->[0]->{"EQUATION"}->[0])) {
3791 :     if ($Reaction =~ m/^bio/) {
3792 :     $ObjectiveCoef = "1.0";
3793 :     }
3794 :     my $LowerBound = -10000;
3795 :     my $UpperBound = 10000;
3796 :     my ($Reactants,$Products) = $self->figmodel()->GetReactionSubstrateData($Reaction);
3797 :     my $Name = $Reaction;
3798 :     if (defined($self->figmodel()->database()->GetDBTable("REACTIONS")->{$Reaction}->[0]->{"NAME"}->[0])) {
3799 :     $Name = $self->figmodel()->database()->GetDBTable("REACTIONS")->{$Reaction}->[0]->{"NAME"}->[0];
3800 :     $Name =~ s/[<>;&]//g;
3801 :     }
3802 :     my $Reversibility = "true";
3803 :     if (defined($ModelTable->{$Reaction}->[0]->{"DIRECTIONALITY"}->[0])) {
3804 :     if ($ModelTable->{$Reaction}->[0]->{"DIRECTIONALITY"}->[0] ne "<=>") {
3805 :     $LowerBound = 0;
3806 :     $Reversibility = "false";
3807 :     }
3808 :     if ($ModelTable->{$Reaction}->[0]->{"DIRECTIONALITY"}->[0] eq "<=") {
3809 :     my $Temp = $Products;
3810 :     $Products = $Reactants;
3811 :     $Reactants = $Temp;
3812 :     }
3813 :     }
3814 :     print SBMLOUTPUT '<reaction id="'.$Reaction.'" name="'.$Name.'" reversible="'.$Reversibility.'">'."\n";
3815 :     print SBMLOUTPUT "<notes>\n";
3816 :     my $ECData = "";
3817 :     if (defined($self->figmodel()->database()->GetDBTable("REACTIONS")->{$Reaction}->[0]->{"ENZYME"}->[0])) {
3818 :     $ECData = $self->figmodel()->database()->GetDBTable("REACTIONS")->{$Reaction}->[0]->{"ENZYME"}->[0];
3819 :     }
3820 :     my $SubsystemData = "";
3821 :     if (defined($ModelTable->{$Reaction}->[0]->{"SUBSYSTEM"}->[0])) {
3822 :     $SubsystemData = $ModelTable->{$Reaction}->[0]->{"SUBSYSTEM"}->[0];
3823 :     }
3824 :     my $GeneAssociation = "";
3825 :     my $ProteinAssociation = "";
3826 :     if (defined($ModelTable->{$Reaction}->[0]->{"ASSOCIATED PEG"}->[0])) {
3827 :     if (@{$ModelTable->{$Reaction}->[0]->{"ASSOCIATED PEG"}} == 1 && $ModelTable->{$Reaction}->[0]->{"ASSOCIATED PEG"}->[0] !~ m/\+/) {
3828 :     $GeneAssociation = $ModelTable->{$Reaction}->[0]->{"ASSOCIATED PEG"}->[0];
3829 :     } else {
3830 :     if (@{$ModelTable->{$Reaction}->[0]->{"ASSOCIATED PEG"}} > 1) {
3831 :     $GeneAssociation = "( ";
3832 :     }
3833 :     for (my $i=0; $i < @{$ModelTable->{$Reaction}->[0]->{"ASSOCIATED PEG"}}; $i++) {
3834 :     if ($i > 0) {
3835 :     $GeneAssociation .= " ) or ( ";
3836 :     }
3837 :     $GeneAssociation .= $ModelTable->{$Reaction}->[0]->{"ASSOCIATED PEG"}->[$i];
3838 :     }
3839 :     if (@{$ModelTable->{$Reaction}->[0]->{"ASSOCIATED PEG"}} > 1) {
3840 :     $GeneAssociation .= " )";
3841 :     }
3842 :     }
3843 :     $GeneAssociation =~ s/\+/ and /g;
3844 :     if ($GeneAssociation =~ m/\sor\s/ || $GeneAssociation =~ m/\sand\s/) {
3845 :     $GeneAssociation = "( ".$GeneAssociation." )";
3846 :     }
3847 :     $ProteinAssociation = $GeneAssociation;
3848 :     if (defined($self->figmodel()->database()->GetDBTable("MODEL STATS")->{$self->id()}->[0]->{"Genome ID"}->[0])) {
3849 :     $ProteinAssociation = $self->figmodel()->translate_gene_to_protein($ModelTable->{$Reaction}->[0]->{"ASSOCIATED PEG"}->[0],$self->figmodel()->database()->GetDBTable("MODEL STATS")->{$self->id()}->[0]->{"Genome ID"}->[0]);
3850 :     }
3851 :     }
3852 :     print SBMLOUTPUT "<html:p>GENE_ASSOCIATION:".$GeneAssociation."</html:p>\n";
3853 :     print SBMLOUTPUT "<html:p>PROTEIN_ASSOCIATION:".$ProteinAssociation."</html:p>\n";
3854 :     print SBMLOUTPUT "<html:p>SUBSYSTEM: ".$SubsystemData."</html:p>\n";
3855 :     print SBMLOUTPUT "<html:p>PROTEIN_CLASS: ".$ECData."</html:p>\n";
3856 :     print SBMLOUTPUT "</notes>\n";
3857 :     print SBMLOUTPUT "<listOfReactants>\n";
3858 :     foreach my $Reactant (@{$Reactants}) {
3859 :     print SBMLOUTPUT '<speciesReference species="'.$Reactant->{"DATABASE"}->[0]."_".$Reactant->{"COMPARTMENT"}->[0].'" stoichiometry="'.$Reactant->{"COEFFICIENT"}->[0].'"/>'."\n";
3860 :     }
3861 :     print SBMLOUTPUT "</listOfReactants>\n";
3862 :     print SBMLOUTPUT "<listOfProducts>\n";
3863 :     foreach my $Product (@{$Products}) {
3864 :     print SBMLOUTPUT '<speciesReference species="'.$Product->{"DATABASE"}->[0]."_".$Product->{"COMPARTMENT"}->[0].'" stoichiometry="'.$Product->{"COEFFICIENT"}->[0].'"/>'."\n";
3865 :     }
3866 :     print SBMLOUTPUT "</listOfProducts>\n";
3867 :     print SBMLOUTPUT "<kineticLaw>\n";
3868 :     print SBMLOUTPUT "\t<math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n";
3869 :     print SBMLOUTPUT "\t\t\t<ci> FLUX_VALUE </ci>\n";
3870 :     print SBMLOUTPUT "\t</math>\n";
3871 :     print SBMLOUTPUT "\t<listOfParameters>\n";
3872 :     print SBMLOUTPUT "\t\t<parameter id=\"LOWER_BOUND\" value=\"".$LowerBound."\" units=\"mmol_per_gDW_per_hr\"/>\n";
3873 :     print SBMLOUTPUT "\t\t<parameter id=\"UPPER_BOUND\" value=\"".$UpperBound."\" units=\"mmol_per_gDW_per_hr\"/>\n";
3874 :     print SBMLOUTPUT "\t\t<parameter id=\"OBJECTIVE_COEFFICIENT\" value=\"".$ObjectiveCoef."\"/>\n";
3875 :     print SBMLOUTPUT "\t\t<parameter id=\"FLUX_VALUE\" value=\"0.0\" units=\"mmol_per_gDW_per_hr\"/>\n";
3876 :     print SBMLOUTPUT "\t</listOfParameters>\n";
3877 :     print SBMLOUTPUT "</kineticLaw>\n";
3878 :     print SBMLOUTPUT '</reaction>'."\n";
3879 :     }
3880 :     }
3881 :    
3882 :     my @ExchangeList = keys(%{$ExchangeHash});
3883 :     foreach my $ExCompound (@ExchangeList) {
3884 :     my $ExCompoundName = $ExCompound;
3885 :     my $Row = $self->figmodel()->database()->GetDBTable("COMPOUNDS")->get_row_by_key($ExCompound,"DATABASE");
3886 :     if (defined($Row) && defined($Row->{"NAME"}->[0])) {
3887 :     $ExCompoundName = $Row->{"NAME"}->[0];
3888 :     $ExCompoundName =~ s/[<>;&]//g;
3889 :     }
3890 :     $ObjectiveCoef = "0.0";
3891 :     print SBMLOUTPUT '<reaction id="EX_'.$ExCompound.'_'.$ExchangeHash->{$ExCompound}.'" name="EX_'.$ExCompoundName.'_'.$ExchangeHash->{$ExCompound}.'" reversible="true">'."\n";
3892 :     print SBMLOUTPUT "\t".'<notes>'."\n";
3893 :     print SBMLOUTPUT "\t\t".'<html:p>GENE_ASSOCIATION: </html:p>'."\n";
3894 :     print SBMLOUTPUT "\t\t".'<html:p>PROTEIN_ASSOCIATION: </html:p>'."\n";
3895 :     print SBMLOUTPUT "\t\t".'<html:p>SUBSYSTEM: S_</html:p>'."\n";
3896 :     print SBMLOUTPUT "\t\t".'<html:p>PROTEIN_CLASS: </html:p>'."\n";
3897 :     print SBMLOUTPUT "\t".'</notes>'."\n";
3898 :     print SBMLOUTPUT "\t".'<listOfReactants>'."\n";
3899 :     print SBMLOUTPUT "\t\t".'<speciesReference species="'.$ExCompound.'_'.$ExchangeHash->{$ExCompound}.'" stoichiometry="1.000000"/>'."\n";
3900 :     print SBMLOUTPUT "\t".'</listOfReactants>'."\n";
3901 :     print SBMLOUTPUT "\t".'<listOfProducts>'."\n";
3902 :     print SBMLOUTPUT "\t\t".'<speciesReference species="'.$ExCompound.'_b" stoichiometry="1.000000"/>'."\n";
3903 :     print SBMLOUTPUT "\t".'</listOfProducts>'."\n";
3904 :     print SBMLOUTPUT "\t".'<kineticLaw>'."\n";
3905 :     print SBMLOUTPUT "\t\t".'<math xmlns="http://www.w3.org/1998/Math/MathML">'."\n";
3906 :     print SBMLOUTPUT "\t\t\t\t".'<ci> FLUX_VALUE </ci>'."\n";
3907 :     print SBMLOUTPUT "\t\t".'</math>'."\n";
3908 :     print SBMLOUTPUT "\t\t".'<listOfParameters>'."\n";
3909 :     print SBMLOUTPUT "\t\t\t".'<parameter id="LOWER_BOUND" value="-10000.000000" units="mmol_per_gDW_per_hr"/>'."\n";
3910 :     print SBMLOUTPUT "\t\t\t".'<parameter id="UPPER_BOUND" value="10000.000000" units="mmol_per_gDW_per_hr"/>'."\n";
3911 :     print SBMLOUTPUT "\t\t\t".'<parameter id="OBJECTIVE_COEFFICIENT" value="'.$ObjectiveCoef.'"/>'."\n";
3912 :     print SBMLOUTPUT "\t\t\t".'<parameter id="FLUX_VALUE" value="0.000000" units="mmol_per_gDW_per_hr"/>'."\n";
3913 :     print SBMLOUTPUT "\t\t".'</listOfParameters>'."\n";
3914 :     print SBMLOUTPUT "\t".'</kineticLaw>'."\n";
3915 :     print SBMLOUTPUT '</reaction>'."\n";
3916 :     }
3917 :    
3918 :     #Closing out the file
3919 :     print SBMLOUTPUT '</listOfReactions>'."\n";
3920 :     print SBMLOUTPUT '</model>'."\n";
3921 :     print SBMLOUTPUT "</sbml>\n";
3922 :     close(SBMLOUTPUT);
3923 :     }
3924 :    
3925 :     =head3 PrintModelLPFile
3926 :     Definition:
3927 :     success()/fail() FIGMODELmodel->PrintModelLPFile();
3928 :     Description:
3929 :     Prints the lp file needed to run the model using the mpifba program
3930 :     =cut
3931 :     sub PrintModelLPFile {
3932 :     my ($self) = @_;
3933 :     #Printing lp and key file for model
3934 :     my $UniqueFilename = $self->figmodel()->filename();
3935 :     #Printing the standard FBA file
3936 :     system($self->figmodel()->GenerateMFAToolkitCommandLineCall($UniqueFilename,$self->id(),"NoBounds",["ProdFullFBALP"],undef,$self->id().$self->selected_version()."-LPPrint.log",undef,$self->selected_version()));
3937 :     system("cp ".$self->config("MFAToolkit output directory")->[0].$UniqueFilename."/CurrentProblem.lp ".$self->directory()."FBA-".$self->id().$self->selected_version().".lp");
3938 :     my $KeyTable = FIGMODELTable::load_table($self->config("MFAToolkit output directory")->[0].$UniqueFilename."/VariableKey.txt",";","|",0,undef);
3939 :     if (!defined($KeyTable)) {
3940 :     print STDERR "FIGMODEL:RunAllStudiesWithDataFast: ".$self->id()." LP file could not be printed.\n";
3941 :     return 0;
3942 :     }
3943 :     $KeyTable->headings(["Variable type","Variable ID"]);
3944 :     $KeyTable->save($self->directory()."FBA-".$self->id().$self->selected_version().".key");
3945 :     unlink($self->config("database message file directory")->[0].$self->id().$self->selected_version()."-LPPrint.log");
3946 :     $self->figmodel()->clearing_output($UniqueFilename,"FBA-".$self->id().$self->selected_version().".lp");
3947 :     }
3948 :    
3949 : chenry 1.5 =head3 patch_model
3950 :     Definition:
3951 :     FIGMODELTable:patch results = FIGMODELmodel->patch_model(FIGMODELTable:patch table);
3952 :     Description:
3953 :     =cut
3954 :     sub patch_model {
3955 :     my ($self,$tbl) = @_;
3956 :    
3957 :     #Instantiating table
3958 :     my $results = FIGMODELTable->new(["Reactions","New genes","Old genes","Genes","Roles","Status"],$self->directory()."PatchResults-".$self->id().$self->selected_version().".tbl",["Reaction"],"\t",";",undef);
3959 :     #Getting genome annotations
3960 :     my $features = $self->figmodel()->database()->get_genome_feature_table($self->genome());
3961 :     #Gettubg reaction table
3962 :     my $reactions = $self->reaction_table();
3963 :     #Checking for patched roles
3964 :     for (my $i=0; $i < $tbl->size(); $i++) {
3965 :     my $row = $tbl->get_row($i);
3966 :     my @genes = $features->get_rows_by_key($row->{ROLE}->[0],"ROLES");
3967 :     if (@genes > 0) {
3968 :     for (my $j=0; $j < @{$row->{REACTIONS}};$j++) {
3969 :     my $resultrxn = $results->get_row_by_key($row->{REACTIONS}->[$j],"Reactions");
3970 :     if (!defined($resultrxn)) {
3971 :     $resultrxn = $results->add_row({"Reactions"=>[$row->{REACTIONS}->[$j]],"Roles"=>[$row->{ROLE}->[0]]});
3972 :     }
3973 :     my $rxnrow = $reactions->get_row_by_key($row->{REACTIONS}->[$j],"LOAD");
3974 :     if (defined($rxnrow) && !defined($resultrxn->{"Old genes"})) {
3975 :     $resultrxn->{"Old genes"} = $rxnrow->{"ASSOCIATED PEG"};
3976 :     if ($resultrxn->{"Old genes"}->[0] !~ m/GAP|BOF|UNIVERSAL|SPONTANEOUS/) {
3977 :     push(@{$resultrxn->{"Genes"}},@{$resultrxn->{"Old genes"}});
3978 :     }
3979 :     }
3980 :     delete $resultrxn->{"Current gene set"};
3981 :     if (defined($resultrxn->{"Genes"})) {
3982 :     push(@{$resultrxn->{"Current gene set"}},@{$resultrxn->{"Genes"}});
3983 :     }
3984 :     for (my $k=0; $k < @genes; $k++) {
3985 :     if ($genes[$k]->{ID}->[0] =~ m/(peg\.\d+)/) {
3986 :     my $gene = $1;
3987 :     my $addgene = 1;
3988 :     if (defined($resultrxn->{"Old genes"})) {
3989 :     for (my $m=0; $m < @{$resultrxn->{"Old genes"}}; $m++) {
3990 :     if ($resultrxn->{"Old genes"}->[$m] =~ m/$gene/) {
3991 :     $addgene = 0;
3992 :     }
3993 :     }
3994 :     }
3995 :     if ($addgene == 1) {
3996 :     push(@{$resultrxn->{"New genes"}},$gene);
3997 :     if ($row->{COMPLEX}->[0] ne "0" && defined($resultrxn->{"Current gene set"})) {
3998 :     my $added = 0;
3999 :     for (my $m=0; $m < @{$resultrxn->{"Current gene set"}}; $m++) {
4000 :     if ($row->{COMPLEX}->[0] eq "1") {
4001 :     $resultrxn->{"Current gene set"}->[$m] = $resultrxn->{"Current gene set"}->[$m]."+".$gene;
4002 :     $added = 1;
4003 :     } else {
4004 :     my @geneset = split(/\+/,$resultrxn->{"Current gene set"}->[$m]);
4005 :     for (my $n=0; $n < @geneset;$n++) {
4006 :     if ($self->figmodel()->colocalized_genes($geneset[$n],$gene,$self->genome()) == 1) {
4007 :     $resultrxn->{"Current gene set"}->[$m] = $resultrxn->{"Current gene set"}->[$m]."+".$gene;
4008 :     $added = 1;
4009 :     last;
4010 :     }
4011 :     }
4012 :     }
4013 :     }
4014 :     if ($added == 0) {
4015 :     push(@{$resultrxn->{"Current gene set"}},$gene);
4016 :     }
4017 :     } else {
4018 :     push(@{$resultrxn->{"Current gene set"}},$gene);
4019 :     }
4020 :     }
4021 :     }
4022 :     }
4023 :     delete $resultrxn->{"Genes"};
4024 :     push(@{$resultrxn->{"Genes"}},@{$resultrxn->{"Current gene set"}});
4025 :     }
4026 :     }
4027 :     }
4028 :    
4029 :     #Ensuring that the old model is preserved
4030 :     $self->ArchiveModel();
4031 :     #Modifing the reaction list
4032 :     for (my $i=0; $i < $results->size();$i++) {
4033 :     my $row = $results->get_row($i);
4034 :     my $rxnrow = $reactions->get_row_by_key($row->{"Reactions"}->[0],"LOAD");
4035 :     if (defined($rxnrow)) {
4036 :     $rxnrow->{"ASSOCIATED PEG"} = $row->{"Genes"};
4037 :     } else {
4038 :     $reactions->add_row({LOAD=>[$row->{"Reactions"}->[0]],DIRECTIONALITY=>[$self->figmodel()->reversibility_of_reaction($row->{"Reactions"}->[0])],COMPARTMENT=>["c"],"ASSOCIATED PEG"=>$row->{"Genes"},SUBSYSTEM=>["NONE"],CONFIDENCE=>[2],REFERENCE=>["NONE"],NOTES=>["PATCH"]});
4039 :     }
4040 :     }
4041 :     $reactions->save();
4042 :     $results->save();
4043 :     $self->update_model_stats();
4044 :     $self->PrintModelLPFile();
4045 :     $self->run_default_model_predictions();
4046 :     #Returning results
4047 :     return $results;
4048 :     }
4049 :    
4050 :     =head3 translate_genes
4051 :     Definition:
4052 :     FIGMODELmodel->translate_genes();
4053 :     Description:
4054 :     =cut
4055 :     sub translate_genes {
4056 :     my ($self) = @_;
4057 :    
4058 :     #Loading gene translations
4059 :     if (!defined($self->{_gene_aliases})) {
4060 :     #Loading gene aliases from feature table
4061 :     my $tbl = $self->figmodel()->GetGenomeFeatureTable($self->genome());
4062 :     if (defined($tbl)) {
4063 :     for (my $i=0; $i < $tbl->size(); $i++) {
4064 :     my $row = $tbl->get_row($i);
4065 :     if ($row->{ID}->[0] =~ m/(peg\.\d+)/) {
4066 :     my $geneID = $1;
4067 :     for (my $j=0; $j < @{$row->{ALIASES}}; $j++) {
4068 :     $self->{_gene_aliases}->{$row->{ALIASES}->[$j]} = $geneID;
4069 :     }
4070 :     }
4071 :     }
4072 :     }
4073 :     #Loading additional gene aliases from the database
4074 :     if (-e $self->figmodel()->config("Translation directory")->[0]."AdditionalAliases/".$self->genome().".txt") {
4075 :     my $AdditionalAliases = $self->figmodel()->database()->load_multiple_column_file($self->figmodel()->config("Translation directory")->[0]."AdditionalAliases/".$self->genome().".txt","\t");
4076 :     for (my $i=0; $i < @{$AdditionalAliases}; $i++) {
4077 :     $self->{_gene_aliases}->{$AdditionalAliases->[$i]->[1]} = $AdditionalAliases->[$i]->[0];
4078 :     }
4079 :     }
4080 :     }
4081 :    
4082 :     #Cycling through reactions and translating genes
4083 :     for (my $i=0; $i < $self->reaction_table()->size(); $i++) {
4084 :     my $row = $self->reaction_table()->get_row($i);
4085 :     if (defined($row->{"ASSOCIATED PEG"})) {
4086 :     for (my $j=0; $j < @{$row->{"ASSOCIATED PEG"}}; $j++) {
4087 :     my $Original = $row->{"ASSOCIATED PEG"}->[$j];
4088 :     $Original =~ s/\sand\s/:/g;
4089 :     $Original =~ s/\sor\s/;/g;
4090 :     my @GeneNames = split(/[,\+\s\(\):;]/,$Original);
4091 :     foreach my $Gene (@GeneNames) {
4092 :     if (length($Gene) > 0 && defined($self->{_gene_aliases}->{$Gene})) {
4093 :     my $Replace = $self->{_gene_aliases}->{$Gene};
4094 :     $Original =~ s/([^\w])$Gene([^\w])/$1$Replace$2/g;
4095 :     $Original =~ s/^$Gene([^\w])/$Replace$1/g;
4096 :     $Original =~ s/([^\w])$Gene$/$1$Replace/g;
4097 :     $Original =~ s/^$Gene$/$Replace/g;
4098 :     }
4099 :     }
4100 :     $Original =~ s/:/ and /g;
4101 :     $Original =~ s/;/ or /g;
4102 :     $row->{"ASSOCIATED PEG"}->[$j] = $Original;
4103 :     }
4104 :     }
4105 :     }
4106 :    
4107 :     #Archiving model and saving reaction table
4108 :     $self->ArchiveModel();
4109 :     $self->reaction_table()->save();
4110 :     }
4111 :    
4112 : chenry 1.2 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3