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

Annotation of /FigKernelPackages/FIGMODELmodel.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3