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

Annotation of /FigKernelPackages/model.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : olson 1.1
2 :     ## _*_ Perl _*_ ##
3 :     #
4 :     # model.pm
5 :     #
6 :     # Kevin Formsma
7 :     # Hope College
8 :     # Created: 6/1/2006
9 :     #
10 :     ##################
11 :    
12 :     ### BrainStorm/Planning ###
13 :     #
14 :     # Functions
15 :     # -Scenario access methods for subsystem
16 :     # -Access current genome models
17 :     # -Interface to/Integration of find_reaction_paths.cgi
18 :     # -Scenario Relationships:
19 :     # 1. Automanaged - map togather based on starting/ending compounds
20 :     # 2. User Defined - through some type of map or XML
21 :     # -Generate files for Scenarios on a organism
22 :     # 1. Path Picking - All or Select?
23 :     # -Filter written Scenarios based on Relationships
24 :     # -Report valid Scenarios for a genome
25 :     #
26 :     #
27 :     # Basic Process for a Control Script
28 :     # 1. Input Genomes for model creation and Subsystems
29 :     # 2. Define selected Scenario relationships
30 :     # 3. Create Scenario Paths for each genome and subsystem
31 :     # 4. Filter invalid Scenarios based on relationships
32 :     # 5. Combine Subsystem models for each genome into genome models
33 :     # 6. Report on the model results for each genome
34 :     #
35 :    
36 :     package model;
37 :    
38 :     use strict;
39 : formsma 1.14 use Scenario;
40 : olson 1.1 use FIG;
41 :     use Subsystem;
42 :     use File::Path;
43 :    
44 : dejongh 1.24 my $fig= new FIG;
45 : olson 1.1
46 :     #global variables that make process_paths work
47 :     #These need to be cleared and reloaded frequently for process path and flux writing
48 :     my (%reactions_to_substrate_arrays, %reactions_to_product_arrays, %all_compounds_to_main);
49 :     my %all_reactions;
50 :     my %scenario_cycles;
51 :     my @all_outputs_lists;
52 :     my %all_inputs;
53 :     my %all_outputs;
54 :    
55 : dejongh 1.12 #this variable is used to set the loop count for single scenario and then assembly runs, 100/25 by default.
56 : olson 1.1 my $loop_max = 100;
57 :     my $loop_max_assembly = 25;
58 :    
59 :     #These store what supersets/Subsystems we are using
60 :     my %superset_to_ss;
61 :     my %ss_to_superset;
62 :    
63 :     #Flip this bit to enable debugging
64 : olson 1.11 my $debug = int($ENV{HOPE_DEBUG});
65 : dejongh 1.21 $debug = 0;
66 : olson 1.11
67 :     sub set_fig
68 :     {
69 :     my($newfig) = @_;
70 :     $fig = $newfig;
71 :     }
72 : olson 1.1
73 :     sub new {
74 :     my $type = shift;
75 :     my $self = {};
76 :     return bless $self, $type;
77 :     }
78 :    
79 :     sub get_ss_scenarios
80 :     {
81 :     my ($ss_name) = @_;
82 :     # This is the scenario data structure storage
83 :     # %scenario_data{scenario_name}
84 :     #
85 :     my %scenario_data;
86 :    
87 :     my $subsystem = $fig->get_subsystem($ss_name);
88 : olson 1.11 if (!$subsystem)
89 :     {
90 :     warn "Cannot open subsystem $subsystem\n";
91 :     return;
92 :     }
93 :    
94 : olson 1.1 my @scenario_names = $subsystem->get_hope_scenario_names;
95 :     foreach my $name (@scenario_names)
96 :     {
97 :     $scenario_data{$name} = &get_scenario($subsystem,$name);
98 :     }
99 :    
100 :     return \%scenario_data;
101 :     }
102 :    
103 :    
104 :     sub get_scenario
105 :     {
106 :     my($subsystem,$name) = @_;
107 :    
108 :     my %data;
109 :    
110 : dejongh 1.17 my @inputs = $subsystem->get_hope_input_compounds($name);
111 :     my @outputs = $subsystem->get_hope_output_compounds($name);
112 :     my @map_ids = $subsystem->get_hope_map_ids($name);
113 :     my @additional_reactions = $subsystem->get_hope_additional_reactions($name);
114 :     my @ignore_reactions = $subsystem->get_hope_ignore_reactions($name);
115 : olson 1.1
116 :     $data{inputs} = \@inputs;
117 :     $data{outputs} = \@outputs;
118 :     $data{map_ids} = \@map_ids;
119 :     $data{additional_reactions} = \@additional_reactions;
120 :     $data{ignore_reactions} = \@ignore_reactions;
121 :    
122 :     return \%data;
123 :    
124 :     }
125 :    
126 :     sub process_init
127 :     {
128 : dejongh 1.19 my ($ss_name,$scenario_name,$genome,$assembly) = @_;
129 : olson 1.1 my (%sc_inputs, %sc_outputs);
130 :    
131 :     if ($genome eq "")
132 :     {
133 :     $genome = "All";
134 :     }
135 :     print STDERR "\nSubsystem : ".$ss_name." Scenario: $scenario_name \n" if $debug;
136 :     my $subsystem = $fig->get_subsystem($ss_name);
137 :     my $scenario_data = &get_scenario($subsystem,$scenario_name);
138 : dejongh 1.4
139 : olson 1.1 #load the other arrays
140 :     my %ss_reactions;
141 :    
142 : dejongh 1.4 if ($genome eq "All")
143 : olson 1.1 {
144 : dejongh 1.17 my %all_reactions = $subsystem->get_hope_reactions;
145 : dejongh 1.4 foreach my $role (keys %all_reactions)
146 : olson 1.1 {
147 : dejongh 1.4 map { $ss_reactions{$_} = 1 } @{$all_reactions{$role}};
148 : olson 1.1 }
149 :     }
150 : dejongh 1.4 else
151 : olson 1.1 {
152 : dejongh 1.24 my %reactions_for_genome = get_reactions_for_genome_in_subsystem($genome,$ss_name);
153 : dejongh 1.17 map { $ss_reactions{$_} = 1 } keys %reactions_for_genome if defined %reactions_for_genome;
154 : olson 1.1 }
155 : dejongh 1.4
156 : olson 1.1 map { $sc_inputs{$_} = 1 } @{$scenario_data->{inputs}};
157 :    
158 :     foreach my $list (@{$scenario_data->{outputs}})
159 :     {
160 :     map { $sc_outputs{$_} = 1 } @$list;
161 :     push @all_outputs_lists, $list;
162 :     }
163 :    
164 :     map { $scenario_cycles{$_} = 1 if defined $sc_outputs{$_} } keys %sc_inputs;
165 :     map { $all_inputs{$_} = 1 } keys %sc_inputs;
166 :     map { $all_outputs{$_} = 1 } keys %sc_outputs;
167 :    
168 :     my @hope_additional_reactions = @{$scenario_data->{additional_reactions}};
169 :     my @hope_ignore_reactions = @{$scenario_data->{ignore_reactions}};
170 :     my %sc_reactions;
171 :     map { $sc_reactions{$_} = 1 } keys %ss_reactions;
172 :    
173 :     # flag additional reactions so we won't check if they are in a map
174 :     foreach my $rid (@hope_additional_reactions)
175 :     {
176 :     $sc_reactions{$rid} = 2;
177 :     }
178 :    
179 :     foreach my $rid (@hope_ignore_reactions)
180 :     {
181 :     delete $sc_reactions{$rid};
182 :     }
183 :    
184 :     #for now we do this outside of the if statment, but that might need to change
185 : dejongh 1.19 &load_substrate_and_product_arrays(\%sc_reactions,$scenario_data->{map_ids}, \%sc_inputs);
186 : olson 1.1
187 :    
188 :    
189 :    
190 :     }
191 :    
192 :     sub execute_paths
193 :     {
194 :     my ($assembly_paths,$find_first,$input_path, $output_path) = @_;
195 :    
196 :     my $num_paths = scalar @{$assembly_paths};
197 :    
198 :     my (%substrates_to_reactions, %products_to_reactions,
199 :     %reactions_to_substrates, %reactions_to_products);
200 :    
201 :     if($num_paths ==0 ) #Load the reactions normally if we are creating scenarios
202 :     {
203 :     create_reactions(\%substrates_to_reactions,\%products_to_reactions,
204 :     \%reactions_to_substrates,
205 :     \%reactions_to_products);
206 :     }
207 :     #create assembly reactions that are closed 'paths' from scenarios or assemblies
208 :     if($num_paths > 0)
209 :     {
210 :     create_assembly_reactions(\%substrates_to_reactions,\%products_to_reactions,
211 :     \%reactions_to_substrates,
212 :     \%reactions_to_products,$assembly_paths);
213 :     }
214 :    
215 :     #This deals with user specifed input/output paths, and uses these to generate
216 :     #what our input and output compounds should be for an assembly.
217 :     #This is only used for creating assemblies, has no effect on scenario creation
218 :    
219 :     if(scalar @{$input_path} && scalar @{$output_path})
220 :     {
221 :    
222 :     %all_inputs = ();
223 :     %all_outputs = ();
224 :     #we should have loaded these paths above, so lets just pull out the information we need
225 :     foreach my $path (@$input_path)
226 :     {
227 :     my $input_rxn = "$path->[-3]/$path->[-2]/$path->[-1]_R";
228 :     my @user_in = @{$reactions_to_substrates{$input_rxn}};
229 :     map{ $all_inputs{$_} = 1 } @user_in;
230 :     }
231 :     foreach my $path (@$output_path)
232 :     {
233 :     my $output_rxn = "$path->[-3]/$path->[-2]/$path->[-1]_R";
234 :     my @user_out = @{$reactions_to_products{$output_rxn}};
235 :     map{ $all_outputs{$_} = 1 } @user_out;
236 :     }
237 :     #map { $scenario_cycles{$_} = 1 if defined $all_outputs{$_} } keys %all_inputs;
238 :     }
239 :    
240 :     print STDERR "Inputs :\n" if $debug;
241 : olson 1.2 print STDERR map { $_."\n" } keys %all_inputs if $debug;
242 : olson 1.1 print STDERR "Outputs:\n" if $debug;
243 : olson 1.2 print STDERR map { $_."\n" } keys %all_outputs if $debug;
244 : olson 1.1
245 :     #filter the input/outputs lists, removing the intersection unless something
246 :     # is a known cycle
247 :     foreach my $input (keys %all_inputs)
248 :     {
249 :     if(defined $all_outputs{$input} && ! defined $scenario_cycles{$input})
250 :     {
251 :     print STDERR "Deleting $input from input and output lists\n" if $debug;
252 :     delete $all_inputs{$input};
253 :     delete $all_outputs{$input};
254 :     }
255 :     }
256 :    
257 :     my $create_assembly = 0;
258 :     $create_assembly = 1 if(scalar @{$assembly_paths} !=0);
259 :     return process_paths(\%all_inputs, \%all_outputs, \@all_outputs_lists,
260 :     \%reactions_to_substrates, \%reactions_to_products,
261 :     \%substrates_to_reactions,\%products_to_reactions,$create_assembly,$find_first);
262 :     }
263 :    
264 :    
265 :     sub create_reactions
266 :     {
267 :     my ($substrates_to_reactions, $products_to_reactions,
268 :     $reactions_to_substrates, $reactions_to_products) = @_;
269 : olson 1.2 print STDERR "building SS reactions\n" if $debug;
270 : olson 1.1 # use subsystem reactions
271 :     foreach my $drxn (map { ($_."_L", $_."_R") } keys %all_reactions)
272 :     {
273 :     foreach my $substrArr ($reactions_to_substrate_arrays{$drxn})
274 :     {
275 :     foreach my $cinfo (@$substrArr)
276 :     {
277 :     my $cpd = $cinfo->[0];
278 :     my $main = $cinfo->[2] || defined $all_inputs{$cpd}; # main in this reaction
279 :    
280 :     if ($main)
281 :     {
282 :     push(@{$reactions_to_substrates->{$drxn}}, $cpd);
283 :     push(@{$substrates_to_reactions->{$cpd}}, $drxn);
284 :     }
285 :     }
286 :     }
287 :    
288 :     foreach my $prodArr ($reactions_to_product_arrays{$drxn})
289 :     {
290 :     foreach my $cinfo (@$prodArr)
291 :     {
292 :     my $cpd = $cinfo->[0];
293 :     my $main = $cinfo->[2] || defined $all_outputs{$cpd}; # main in this reaction
294 :    
295 :     if ($main)
296 :     {
297 :     push(@{$reactions_to_products->{$drxn}}, $cpd);
298 :     push(@{$products_to_reactions->{$cpd}}, $drxn);
299 :     }
300 :     }
301 :     }
302 :     }
303 :     }
304 :    
305 :     sub create_assembly_reactions
306 :     {
307 :     my ($substrates_to_reactions, $products_to_reactions,
308 :     $reactions_to_substrates, $reactions_to_products,$assembly_paths) = @_;
309 :     my %intersection;
310 :    
311 :     foreach my $path (@$assembly_paths)
312 :     {
313 : dejongh 1.20 my $genome = shift @$path;
314 : dejongh 1.23 my $paths_dir = get_scenario_directory($genome) . "/" . join "/" , @$path;
315 : olson 1.1
316 :     my $drxn = "$path->[-3]/$path->[-2]/$path->[-1]_R";
317 :    
318 :     $all_reactions{"$path->[-3]/$path->[-2]/$path->[-1]"} = "R";
319 :    
320 :     print STDERR "Making reaction: $drxn\n" if $debug;
321 :    
322 :     open (M_INPUTS , $paths_dir."/inputs") or die ("Failed to open $paths_dir"."/inputs");
323 :     my @substrArr;
324 :    
325 :     print STDERR "Gathering Inputs:\n" if $debug;
326 :    
327 :     while (<M_INPUTS>)
328 :     {
329 :     my ($cpd, $stoich) = split "\t" , $_;
330 :    
331 :     #We are going to assume everything is a main...
332 :     if(!defined $all_compounds_to_main{$cpd})
333 :     {
334 :     $all_compounds_to_main{$cpd} = 1;
335 :     }
336 :    
337 :     if ($all_compounds_to_main{$cpd})
338 :     {
339 :     push(@{$reactions_to_substrates->{$drxn}}, $cpd);
340 :     push(@{$substrates_to_reactions->{$cpd}}, $drxn);
341 :     }
342 :    
343 :     if($all_compounds_to_main{$cpd} !=0 || !defined $all_compounds_to_main{$cpd})
344 :     {
345 :     $all_inputs{$cpd} = 1;
346 :     }
347 :     push @substrArr, [$cpd, $stoich, $all_compounds_to_main{$cpd}];
348 :    
349 :     my @names = $fig->names_of_compound($cpd);
350 :     print STDERR "\t$stoich\t$cpd\t$names[0]\t$all_compounds_to_main{$cpd}\n" if $debug;
351 :     }
352 :    
353 :     $reactions_to_substrate_arrays{$drxn} = \@substrArr;
354 :    
355 :     close M_INPUTS;
356 :    
357 :     open (M_OUTPUTS, $paths_dir."/outputs") or die("Failed to open $paths_dir"."/outputs");
358 :     my @prodArr;
359 :    
360 :     print STDERR "Gathering outputs:\n" if $debug;
361 :    
362 :     while (<M_OUTPUTS>)
363 :     {
364 :     my ($cpd, $stoich) = split "\t", $_;
365 :     print STDERR "Found $stoich $cpd\n" if $debug;
366 :    
367 :     #We are going to assume everything is a main...
368 :     if(!defined $all_compounds_to_main{$cpd})
369 :     {
370 :     $all_compounds_to_main{$cpd} = 1;
371 :     }
372 :    
373 :     if ($all_compounds_to_main{$cpd})
374 :     {
375 :     push(@{$reactions_to_products->{$drxn}}, $cpd);
376 :     push(@{$products_to_reactions->{$cpd}}, $drxn);
377 :     }
378 :    
379 :    
380 :     #This adds cycles from 'assemblys' because they weren't added earlier
381 :     foreach my $ele (@{$reactions_to_substrates->{$drxn}})
382 :     {
383 :     if($ele eq $cpd)
384 :     {
385 :     $scenario_cycles{$cpd} = 1;
386 :     }
387 :     }
388 :    
389 :     if($all_compounds_to_main{$cpd} !=0 || !defined $all_compounds_to_main{$cpd})
390 :     {
391 :     $all_outputs{$cpd} = 1;
392 :     }
393 :     push @prodArr, [$cpd,$stoich,$all_compounds_to_main{$cpd}];
394 :     my @names = $fig->names_of_compound($cpd);
395 :     print STDERR "\t$cpd\t$names[0]\t$all_compounds_to_main{$cpd}\n" if $debug;
396 :     }
397 :    
398 :     $reactions_to_product_arrays{$drxn} = \@prodArr;
399 :    
400 :     close M_OUTPUTS;
401 :    
402 :     }
403 :     }
404 :    
405 :    
406 :     sub load_substrate_and_product_arrays
407 :     {
408 : dejongh 1.19 my ($reactions, $map_ids, $sc_inputs) = @_;
409 : olson 1.1
410 :     # determine whether the reaction is in one of the maps, and get directionality accordingly
411 : dejongh 1.17 my (%reactions_in_maps, %reactions_not_in_maps, %reactions_not_in_any_map);
412 : olson 1.1
413 :     foreach my $rxn (keys %$reactions)
414 :     {
415 :     my $direction;
416 :    
417 :     if($fig->valid_reaction_id($rxn))
418 :     {
419 :     # get an array of triplets. The triplets are [reaction id][map id]
420 :     # [left to right - R, right to left - L, or both - B]
421 :     my @triplets = $fig->reaction_direction($rxn);
422 :    
423 :     foreach my $trip (@triplets)
424 :     {
425 :     foreach my $map_id (@$map_ids)
426 :     {
427 :     if (@{$trip}[1] eq $map_id)
428 :     {
429 :     my $this_direction = @{$trip}[2];
430 :    
431 :     # bidirectional in one map overrules unidirectional in another.
432 :     # opposite directions in two maps becomes bidirectional
433 :     if (! defined $direction)
434 :     {
435 :     $direction = $this_direction;
436 :     }
437 :     elsif ($direction ne "B" && ($this_direction eq "B" ||
438 :     $this_direction ne $direction))
439 :     {
440 :     $direction = "B";
441 :     }
442 :    
443 :     $reactions_in_maps{$rxn} = 1;
444 :     }
445 :     }
446 :     }
447 :    
448 :    
449 :     if(! $reactions_in_maps{$rxn})
450 :     {
451 :     my $found_in_other_map = 0;
452 :    
453 :     #reaction not in scenario map ids, try to get directionality from other maps
454 :     foreach my $trip (@triplets)
455 :     {
456 :     my $this_direction = @{$trip}[2];
457 :    
458 :     #bidreactional in one map overrules unidirectional in another
459 :     #opposite directions in two maps becomes bidirectional
460 :     if(! defined $direction)
461 :     {
462 :     $direction = $this_direction;
463 :     }
464 :     elsif($direction ne "B" && ($this_direction eq "B" || $this_direction ne $direction))
465 :     {
466 :     $direction = "B";
467 :     }
468 :    
469 :     $found_in_other_map = 1;
470 :     }
471 :     if (!$found_in_other_map)
472 :     {
473 : dejongh 1.17 # reaction not in any map, get directionality without reference to map
474 : olson 1.1 if($fig->reversible($rxn) eq "1")
475 :     {
476 :     $direction = "B";
477 :     }
478 :     else
479 :     {
480 :     $direction = "R";
481 :     }
482 : dejongh 1.17
483 :     $reactions_not_in_any_map{$rxn} = 1;
484 : olson 1.1 }
485 :     }
486 :    
487 :     if (! defined $all_reactions{$rxn} || $direction eq "B")
488 :     {
489 :     $all_reactions{$rxn} = $direction;
490 :     }
491 :     elsif ($all_reactions{$rxn} ne $direction)
492 :     {
493 :     $all_reactions{$rxn} = "B";
494 :     }
495 :    
496 :     my (@substrArr, @prodArr);
497 :    
498 :     if ($direction eq "L")
499 :     {
500 :     @substrArr = $fig->reaction2comp($rxn, 1, $map_ids);
501 :     @prodArr = $fig->reaction2comp($rxn, 0, $map_ids);
502 :     $reactions_to_substrate_arrays{$rxn . "_L"} = \@substrArr;
503 :     $reactions_to_product_arrays{$rxn . "_L"} = \@prodArr;
504 :    
505 :     }
506 :     else
507 :     {
508 :     if ($reactions_in_maps{$rxn})
509 :     {
510 :     @substrArr = $fig->reaction2comp($rxn, 0, $map_ids);
511 :     @prodArr = $fig->reaction2comp($rxn, 1, $map_ids);
512 :     }
513 :     else
514 :     {
515 :     @substrArr = $fig->reaction2comp($rxn, 0);
516 :     @prodArr = $fig->reaction2comp($rxn, 1);
517 :     }
518 :    
519 :     $reactions_to_substrate_arrays{$rxn."_R"} = \@substrArr;
520 :     $reactions_to_product_arrays{$rxn."_R"} = \@prodArr;
521 :    
522 :     if ($direction eq "B")
523 :     {
524 :     $reactions_to_substrate_arrays{$rxn."_L"} = \@prodArr;
525 :     $reactions_to_product_arrays{$rxn."_L"} = \@substrArr;
526 :     }
527 :     }
528 :    
529 :     print STDERR "\nFor $rxn, found substrates:\n" unless !$debug;
530 :     map { print STDERR "\t$_->[0]\t$_->[1]\t$_->[2]\n" unless !$debug } @substrArr;
531 :     print STDERR "For $rxn, found products:\n" unless !$debug;
532 :     map { print STDERR "\t$_->[0]\t$_->[1]\t$_->[2]\n" unless !$debug } @prodArr;
533 :    
534 :     # load "main" designation based on reactions that are in the maps
535 :     if ($reactions_in_maps{$rxn})
536 :     {
537 :     foreach my $cinfo ((@substrArr, @prodArr))
538 :     {
539 :     my $cpd = $cinfo->[0];
540 :     my $main = $cinfo->[2];
541 :    
542 :     if (defined $all_compounds_to_main{$cpd})
543 :     {
544 :     # only main if it's main in all reactions
545 :     $all_compounds_to_main{$cpd} &= $main;
546 :     }
547 :     else
548 :     {
549 :     $all_compounds_to_main{$cpd} = $main;
550 :     }
551 :     }
552 :     }
553 :     else
554 :     {
555 :     # save subs and prods for processing at end
556 :     $reactions_not_in_maps{$rxn} = [ (@substrArr, @prodArr) ];
557 :     }
558 :     }
559 :     }
560 :    
561 :     # now load "main" designation based on reactions not in the maps - but don't overrule
562 :     # what's already been loaded
563 :     my %additional_compounds_to_main;
564 :    
565 :     foreach my $rxn (keys %reactions_not_in_maps)
566 :     {
567 :     foreach my $cinfo (@{$reactions_not_in_maps{$rxn}})
568 :     {
569 :     my $cpd = $cinfo->[0];
570 :     my $main = $cinfo->[2];
571 :    
572 :     if (defined $additional_compounds_to_main{$cpd})
573 :     {
574 :     # main if it's main in any reactions not in map
575 :     $additional_compounds_to_main{$cpd} |= $main;
576 :     }
577 :     else
578 :     {
579 :     $additional_compounds_to_main{$cpd} = $main;
580 :     }
581 :     }
582 :     }
583 :    
584 :     foreach my $cpd (keys %additional_compounds_to_main)
585 :     {
586 :     if (! defined $all_compounds_to_main{$cpd})
587 :     {
588 :     $all_compounds_to_main{$cpd} = $additional_compounds_to_main{$cpd};
589 :     }
590 :     }
591 : dejongh 1.17
592 :     # the reactions that aren't in any map at all won't have any main compounds.
593 :     # mark those compounds that are in all_compounds_to_main as main.
594 :     foreach my $rxn (keys %reactions_not_in_any_map)
595 :     {
596 :     print STDERR "Checking reaction not in any map: $rxn\n" if $debug;
597 :     foreach my $cpd_array ($reactions_to_substrate_arrays{$rxn."_L"},
598 :     $reactions_to_substrate_arrays{$rxn."_R"},
599 :     $reactions_to_product_arrays{$rxn."_L"},
600 :     $reactions_to_product_arrays{$rxn."_R"})
601 :     {
602 :     if (defined $cpd_array)
603 :     {
604 : dejongh 1.19 my $at_least_one_is_main = 0;
605 :    
606 : dejongh 1.17 foreach my $cinfo (@{$cpd_array})
607 :     {
608 :     my $cpd = $cinfo->[0];
609 : dejongh 1.19 if ($all_compounds_to_main{$cpd} || exists $sc_inputs->{$cpd})
610 : dejongh 1.17 {
611 : dejongh 1.19 print STDERR "\t Setting $cpd to main\n" if $debug;
612 : dejongh 1.17 $cinfo->[2] = 1;
613 : dejongh 1.19 $at_least_one_is_main = 1;
614 :     }
615 :     }
616 :    
617 :     if ($at_least_one_is_main == 0)
618 :     {
619 :     foreach my $cinfo (@{$cpd_array})
620 :     {
621 :     my $cpd = $cinfo->[0];
622 :     print STDERR "\t Setting $cpd to provisional main\n" if $debug;
623 :     $cinfo->[2] = 2;
624 : dejongh 1.17 }
625 :     }
626 :     }
627 :     }
628 :     }
629 : olson 1.1 }
630 :    
631 :     sub process_paths
632 :     {
633 :     my ($input_cpds, $output_cpds, $outputs_lists, $reactions_to_substrates, $reactions_to_products, $substrates_to_reactions, $products_to_reactions, $create_assembly, $find_first ) = @_;
634 :    
635 :     my (%path_inputs, %path_outputs);
636 :     map { $path_inputs{$_} = 0 } keys %$input_cpds;
637 :     map { $path_outputs{$_} = 0 } keys %$output_cpds;
638 :    
639 :     my %data_results = ("infinite" => 0);
640 :    
641 :     # %compounds_to_tokens maps from compound ids to tokens placed on those compounds
642 :     # the tokens are organized in hashes mapping from token id to number of tokens with that id
643 :     my %compounds_to_tokens;
644 :     map { $compounds_to_tokens{$_} = {} } keys %all_compounds_to_main;
645 :    
646 :     # %tokens maps from token_ids to the token data structures
647 :     my %tokens;
648 :     my $token_id_counter = 1;
649 :    
650 :     # %compounds_borrowed_to_tokens maps from compounds to lists of token ids that borrowed
651 :     # compound in order to run a reaction
652 :     my %compounds_borrowed_to_tokens;
653 :    
654 :     print STDERR "\nIn process_paths, path_inputs are @{[ keys %path_inputs ]}, path_outputs are @{[ keys %path_outputs] }, scenario_cycles are @{[ keys %scenario_cycles ]} \n\n" unless !$debug;
655 :    
656 :     my $initial_pass = 1;
657 :     my $done = 0;
658 :     my $loop_counter = 1;
659 :     my $infinite_loop_check = $create_assembly ? $loop_max_assembly : $loop_max;
660 :    
661 :     # we may get to the point where we need to add some more path inputs into the mix
662 :     # to push stalled tokens
663 :     my $add_path_inputs = 0;
664 :    
665 :     while(!$done)
666 :     {
667 :     if ($initial_pass || $add_path_inputs)
668 :     {
669 :     foreach my $cpd (keys %path_inputs)
670 :     {
671 :     # place a token on each path input
672 :     my $new_token_id = $token_id_counter++;
673 :     my %new_token;
674 :     $new_token{visited_reactions} = {};
675 :     $new_token{visited_compounds} = { $cpd => 0 }; # 0 means supplied from "outside"
676 :     $new_token{token_path_inputs} = { $cpd => 1 }; # 1 means one was supplied
677 :     $new_token{initial_pass} = $initial_pass;
678 :     $compounds_to_tokens{$cpd}->{$new_token_id}++;
679 :     $tokens{$new_token_id} = \%new_token;
680 :    
681 :     print STDERR "\t\tCreated new token '$new_token_id' for path input $cpd\n" unless !$debug;
682 :     }
683 :    
684 :     $initial_pass = 0;
685 :     $add_path_inputs = 0;
686 :     }
687 :    
688 :     # Find the reactions that can run.
689 :     my %reactions_to_try;
690 :    
691 :     foreach my $cpd (keys %compounds_to_tokens)
692 :     {
693 :     foreach my $token_id (keys %{$compounds_to_tokens{$cpd}})
694 :     {
695 :     if ($compounds_to_tokens{$cpd}->{$token_id} > 0 &&
696 :     ! $tokens{$token_id}->{done})
697 :     {
698 :     # this compound has tokens that aren't done
699 :     map { $reactions_to_try{$_} = 1 } @{$substrates_to_reactions->{$cpd}};
700 :     last;
701 :     }
702 :     }
703 :     }
704 :    
705 :     print STDERR "\n\tIn loop, trying reactions @{[keys %reactions_to_try]}\n\n" unless !$debug;
706 :    
707 :     # Map the reactions that can run to the tokens they can use
708 :     my %reactions_to_tokens_available;
709 :     # Keep track of the main substrates defined by each reaction
710 :     my %reactions_to_main_substrates;
711 :    
712 :     # count up the total number of tokens needed for each compound to run
713 :     # every reaction that is ready to go
714 :     my %reactions_to_tokens_needed;
715 :    
716 :     rxn: foreach my $reaction (keys %reactions_to_try)
717 :     {
718 :     print STDERR "\tChecking reaction $reaction\n" unless !$debug;
719 :    
720 :     my @substrArr = @{$reactions_to_substrate_arrays{$reaction}};
721 :     my @prodArr = @{$reactions_to_product_arrays{$reaction}};
722 :     my %main_substrates;
723 :    
724 :     # Determine if this reaction has necessary inputs to run.
725 :     # There must be tokens available for at least one main substrate that isn't
726 :     # a path output (unless it's an initial scenario cycled compound).
727 :     # Also, any path input must have a token.
728 :     # Count number of tokens needed for each main substrate.
729 :     my %tokens_available;
730 :     my %tokens_needed;
731 :     my $reaction_can_run = 0;
732 :    
733 :     foreach my $substr (@substrArr)
734 :     {
735 :     my $cpd = @{$substr}[0];
736 :     my $stoich = @{$substr}[1];
737 : dejongh 1.19 my $is_it_main = @{$substr}[2];
738 :     my $main = $is_it_main == 1 || @all_compounds_to_main{$cpd}; #main either way
739 :    
740 :     if ($is_it_main == 2)
741 :     {
742 :     # provisionally main - need to see if there is a token that has visited
743 :     # this compound
744 :    
745 :     foreach my $token_id (keys %{$compounds_to_tokens{$cpd}})
746 :     {
747 :     my %visited_compounds = %{$tokens{$token_id}->{visited_compounds}};
748 :     $main = 1 if exists $visited_compounds{$cpd};
749 :     }
750 :     }
751 :    
752 : olson 1.1 $main_substrates{$cpd} = 1 if $main;
753 :     print STDERR "\t\tSubstrate: $cpd\tstoich: $stoich\tmain: $main\n" unless !$debug;
754 :    
755 :     if (! $main)
756 :     {
757 :     # on any pass we can take in non-main compounds
758 :     $tokens_needed{$cpd} = $stoich;
759 :     }
760 :     else
761 :     {
762 :     # if tokens are available for compound, check their history for the
763 :     # main compounds produced by the reaction so we don't loop back over
764 :     # previous main compounds. Also, don't use tokens on scenario inputs
765 :     # moved to in steps other than initial token creation.
766 :     my %ok_tokens;
767 :     my $num_ok_tokens = 0;
768 :    
769 :     foreach my $token_id (keys %{$compounds_to_tokens{$cpd}})
770 :     {
771 :     next if $tokens{$token_id}->{done} ||
772 :     $compounds_to_tokens{$cpd}->{$token_id} == 0;
773 :    
774 :     # now check that we aren't running an already visited reaction in reverse
775 :     my %visited_reactions = %{$tokens{$token_id}->{visited_reactions}};
776 :    
777 :     if (($reaction =~ /(.*)_R/ && defined $visited_reactions{$1."_L"}) ||
778 :     ($reaction =~ /(.*)_L/ && defined $visited_reactions{$1."_R"}))
779 :     {
780 :     print STDERR "\t\tToken '$token_id' has run the reverse reaction already\n" unless !$debug;
781 :     next;
782 :     }
783 :    
784 :     my %visited_compounds = %{$tokens{$token_id}->{visited_compounds}};
785 :    
786 :     print STDERR "\t\tToken '$token_id' has visited @{[map { ($_, $visited_compounds{$_} ) } sort { $visited_compounds{$a} <=> $visited_compounds{$b} } keys %visited_compounds]}\n" unless !$debug;
787 :    
788 :     # need to find at least one prod that hasn't been visited yet
789 :     # or was visited in a loop cycle not before the loop cycle in which
790 :     # it visited the substrate,
791 :     # or is a path output
792 :     my $prods_are_ok = 0;
793 :    
794 :     # check each main product
795 :     foreach my $prod (@{$reactions_to_products->{$reaction}})
796 :     {
797 :     if (! defined $visited_compounds{$prod} ||
798 :     $visited_compounds{$prod} >= $visited_compounds{$cpd} ||
799 :     defined $path_outputs{$prod} ||
800 :     $compounds_borrowed_to_tokens{$prod}->{$token_id} > 0)
801 :     {
802 :     print STDERR "\t\tToken can visit $prod\n" unless !$debug;
803 :     $prods_are_ok = 1;
804 :     last;
805 :     }
806 :     }
807 :    
808 :     if ($prods_are_ok)
809 :     {
810 :     print STDERR "\t\tToken is OK\n" unless !$debug;
811 :     $ok_tokens{$token_id} = $compounds_to_tokens{$cpd}->{$token_id};
812 :     $num_ok_tokens += $compounds_to_tokens{$cpd}->{$token_id};
813 :     }
814 :     }
815 :    
816 :     map { $tokens_available{$_}->{$cpd} = $ok_tokens{$_} } keys %ok_tokens;
817 :     $tokens_needed{$cpd} = $stoich;
818 :    
819 :     if ($main && $num_ok_tokens >= 1)
820 :     {
821 :     if (! defined $path_outputs{$cpd} || defined $scenario_cycles{$cpd})
822 :     {
823 :     print STDERR "\t\tgot at least one token on main compound\n" unless !$debug;
824 :     $reaction_can_run = 1;
825 :     }
826 :     }
827 :     elsif (defined $path_inputs{$cpd})
828 :     {
829 :     print STDERR "\t\tno tokens available for path input: $cpd\n" unless !$debug;
830 :     next rxn;
831 :     }
832 :     elsif (defined $path_outputs{$cpd} && ! defined $scenario_cycles{$cpd})
833 :     {
834 :     print STDERR "\t\tno tokens available for path output: $cpd\n" unless !$debug;
835 :     next rxn;
836 :     }
837 :     }
838 :     }
839 :    
840 :     if ($reaction_can_run)
841 :     {
842 :     $reactions_to_tokens_available{$reaction} = \%tokens_available;
843 :     $reactions_to_tokens_needed{$reaction} = \%tokens_needed;
844 :     $reactions_to_main_substrates{$reaction} = \%main_substrates;
845 :     print STDERR "\tReaction $reaction can run\n" unless !$debug;
846 :     }
847 :     }
848 :    
849 :     # keep track of tokens used that will be used to run rxns. Clone tokens if necessary.
850 :     my %reactions_to_tokens_to_use;
851 :     my %tokens_to_use_to_reactions;
852 :     my %copy_of_compounds_to_tokens; # for determining which reaction uses which tokens
853 :    
854 :     foreach my $cpd (keys %compounds_to_tokens)
855 :     {
856 :     my %cpd_token_ids = %{$compounds_to_tokens{$cpd}};
857 :     my %new_cpd_token_ids;
858 :     map { $new_cpd_token_ids{$_} = $cpd_token_ids{$_} } keys %cpd_token_ids;
859 :     $copy_of_compounds_to_tokens{$cpd} = \%new_cpd_token_ids;
860 :     }
861 :    
862 :     foreach my $reaction (keys %reactions_to_tokens_available)
863 :     {
864 :     print STDERR "\tPreparing to run reaction $reaction\n" unless !$debug;
865 :    
866 :     # assemble tokens to run reaction, cloning ones that were used by
867 :     # other reactions during this cycle if necessary
868 :     my %tokens_available = %{$reactions_to_tokens_available{$reaction}};
869 :     my %tokens_needed = %{$reactions_to_tokens_needed{$reaction}};
870 :     my @final_tokens_to_use; # list of maps from substrates to tokens to use
871 :     my %clone_history; # map from token ids to ids of their new clones
872 :    
873 :     # check to see if any available tokens are already commited to the reverse reaction
874 :     my @token_ids = keys %tokens_available;
875 :    
876 :     foreach my $token_id (keys %tokens_available)
877 :     {
878 :     if (($reaction =~ /(.*)_R/ && defined $tokens_to_use_to_reactions{$token_id}->{$1."_L"}) ||
879 :     ($reaction =~ /(.*)_L/ && defined $tokens_to_use_to_reactions{$token_id}->{$1."_R"}))
880 :     {
881 :     # clone the token
882 :     my $new_token_id = $token_id_counter++;
883 :     &clone_token($token_id, $new_token_id, \%tokens, \%compounds_to_tokens,
884 :     \%compounds_borrowed_to_tokens);
885 :    
886 :     foreach my $icpd (keys %compounds_to_tokens)
887 :     {
888 :     if ($compounds_to_tokens{$icpd}->{$token_id} > 0)
889 :     {
890 :     $copy_of_compounds_to_tokens{$icpd}->{$new_token_id} =
891 :     $compounds_to_tokens{$icpd}->{$token_id};
892 :     }
893 :     }
894 :    
895 :     print STDERR "\t\tCloned token '$token_id', new token is '$new_token_id'\n" unless !$debug;
896 :    
897 :     $tokens_available{$new_token_id} = $tokens_available{$token_id};
898 :     delete $tokens_available{$token_id};
899 :     }
900 :     }
901 :    
902 :     # first, assemble tokens that have all the main compounds they need to run
903 :     @token_ids = keys %tokens_available;
904 :    
905 :     foreach my $token_id (keys %tokens_available)
906 :     {
907 :     my $has_all_main_cpds = 1;
908 :     my $need_to_clone = 0;
909 :    
910 :     foreach my $cpd (keys %tokens_needed)
911 :     {
912 :     if ($reactions_to_main_substrates{$reaction}->{$cpd})
913 :     {
914 :     if ($compounds_to_tokens{$cpd}->{$token_id} >= $tokens_needed{$cpd})
915 :     {
916 :     if ($copy_of_compounds_to_tokens{$cpd}->{$token_id} < $tokens_needed{$cpd})
917 :     {
918 :     $need_to_clone = 1;
919 :     }
920 :     }
921 :     else
922 :     {
923 :     $has_all_main_cpds = 0;
924 :     last;
925 :     }
926 :     }
927 :     }
928 :    
929 :     if ($has_all_main_cpds)
930 :     {
931 :     print STDERR "\t\ttoken '$token_id' has all main compounds\n" unless !$debug;
932 :    
933 :     delete $tokens_available{$token_id};
934 :    
935 :     if ($need_to_clone)
936 :     {
937 :     my $new_token_id = $token_id_counter++;
938 :     &clone_token($token_id, $new_token_id, \%tokens,
939 :     \%compounds_to_tokens,
940 :     \%compounds_borrowed_to_tokens);
941 :    
942 :     foreach my $icpd (keys %compounds_to_tokens)
943 :     {
944 :     if ($compounds_to_tokens{$icpd}->{$token_id} > 0)
945 :     {
946 :     $copy_of_compounds_to_tokens{$icpd}->{$new_token_id} =
947 :     $compounds_to_tokens{$icpd}->{$token_id};
948 :     }
949 :     }
950 :    
951 :     print STDERR "\t\tCloned token '$token_id', new token is '$new_token_id'\n" unless !$debug;
952 :    
953 :     $clone_history{$token_id} = $new_token_id;
954 :     $token_id = $new_token_id;
955 :     }
956 :    
957 :     # now assemble the compound to tokens map
958 :     my %cpd_to_tokens;
959 :    
960 :     foreach my $cpd (keys %tokens_needed)
961 :     {
962 :     if ($reactions_to_main_substrates{$reaction}->{$cpd})
963 :     {
964 :     for (my $i = 0; $i < $tokens_needed{$cpd}; $i++)
965 :     {
966 :     push @{$cpd_to_tokens{$cpd}}, $token_id;
967 :     $copy_of_compounds_to_tokens{$cpd}->{$token_id}--;
968 :     }
969 :     }
970 :     }
971 :    
972 :     push @final_tokens_to_use, \%cpd_to_tokens;
973 :     }
974 :     }
975 :    
976 :     if (scalar keys %tokens_available > 0)
977 :     {
978 :     # try to merge left over available tokens in all combinations that fulfill
979 :     # needed main substrates. Create map from main substrates to tokens.
980 :     my %tokens_available_for_cpds;
981 :     my %tokens_to_use_for_cpds;
982 :    
983 :     foreach my $token_id (keys %tokens_available)
984 :     {
985 :     foreach my $cpd (keys %{$tokens_available{$token_id}})
986 :     {
987 :     if ($reactions_to_main_substrates{$reaction}->{$cpd})
988 :     {
989 :     $tokens_available_for_cpds{$cpd}->{$token_id} =
990 :     $tokens_available{$token_id}->{$cpd};
991 :     }
992 :     }
993 :     }
994 :    
995 :     foreach my $cpd (keys %{$reactions_to_main_substrates{$reaction}})
996 :     {
997 :     my %available_token_ids;
998 :    
999 :     foreach my $token_id (keys %{$tokens_available_for_cpds{$cpd}})
1000 :     {
1001 :     my $updated_token_id = $token_id;
1002 :    
1003 :     # in case the token has already been cloned for another cpd in this reaction
1004 :     while ($clone_history{$updated_token_id})
1005 :     {
1006 :     $updated_token_id = $clone_history{$updated_token_id};
1007 :     }
1008 :    
1009 :     $available_token_ids{$updated_token_id} = 1;
1010 :    
1011 :     if ($token_id != $updated_token_id)
1012 :     {
1013 :     $tokens_available_for_cpds{$cpd}->{$updated_token_id} =
1014 :     $tokens_available_for_cpds{$cpd}->{$token_id};
1015 :     delete $tokens_available_for_cpds{$cpd}->{$token_id};
1016 :     }
1017 :     }
1018 :    
1019 :     my $num_tokens_needed = $tokens_needed{$cpd};
1020 :     my $num_available_tokens = 0;
1021 :    
1022 :     foreach my $token_id (keys %available_token_ids)
1023 :     {
1024 :     $num_available_tokens += $tokens_available_for_cpds{$cpd}->{$token_id};
1025 :     }
1026 :    
1027 :     # if not enough tokens are available to fill out sets, create new ones
1028 :     my $num_short_of_full;
1029 :    
1030 :     if ($num_available_tokens == 0)
1031 :     {
1032 :     $num_short_of_full = $num_tokens_needed;
1033 :     }
1034 :     else
1035 :     {
1036 :     $num_short_of_full = ($num_tokens_needed - ($num_available_tokens % $num_tokens_needed)) % $num_tokens_needed;
1037 :     }
1038 :    
1039 :     print STDERR "\t\tFor $cpd, $num_tokens_needed tokens are needed, $num_available_tokens are available. Need to create $num_short_of_full to fill out sets\n" unless !$debug;
1040 :    
1041 :     for (my $i = 0; $i < $num_short_of_full; $i++)
1042 :     {
1043 :     my $new_token_id = $token_id_counter++;
1044 :     my %new_token;
1045 :     print STDERR "\t\tCreating new token '$new_token_id' for $cpd\n" unless !$debug;
1046 :    
1047 :     $new_token{visited_reactions} = {};
1048 :     $new_token{visited_compounds} = {};
1049 :     $compounds_to_tokens{$cpd}->{$new_token_id}++;
1050 :     $copy_of_compounds_to_tokens{$cpd}->{$new_token_id}++;
1051 :     $tokens{$new_token_id} = \%new_token;
1052 :     $available_token_ids{$new_token_id} = 1;
1053 :     $tokens_available_for_cpds{$cpd}->{$new_token_id}++;
1054 :    
1055 :     # if it's not a path input, remember that we've "borrowed" it and
1056 :     # will need to pay it back.
1057 :     if (! defined $path_inputs{$cpd})
1058 :     {
1059 :     $new_token{visited_compounds}->{$cpd} = $loop_counter;
1060 :     $compounds_borrowed_to_tokens{$cpd}->{$new_token_id}++;
1061 :     }
1062 :     else
1063 :     {
1064 :     $new_token{token_path_inputs} = { $cpd => 1 };
1065 :     $new_token{visited_compounds}->{$cpd} = 0;
1066 :     }
1067 :     }
1068 :    
1069 :     # for main compounds, there may be more tokens available than needed,
1070 :     # so we may assemble multiple token sets.
1071 :     my %tokens_not_yet_used;
1072 :    
1073 :     foreach my $token_id (keys %available_token_ids)
1074 :     {
1075 :     if ($copy_of_compounds_to_tokens{$cpd}->{$token_id} > 0)
1076 :     {
1077 :     $tokens_not_yet_used{$token_id} = $tokens_available_for_cpds{$cpd}->{$token_id};
1078 :     }
1079 :     }
1080 :    
1081 :     my @token_sets_for_cpd;
1082 :    
1083 :     print STDERR "\t\tNeed $num_tokens_needed tokens for $cpd, '@{[ sort { $a <=> $b } keys %available_token_ids ]}' are usable, '@{[ sort { $a <=> $b } keys %tokens_not_yet_used ]}' are not yet used\n" unless !$debug;
1084 :    
1085 :     my @token_set;
1086 :     my $num_tokens_still_needed = $num_tokens_needed;
1087 :    
1088 :     foreach my $token_id (sort { $a <=> $b } keys %available_token_ids)
1089 :     {
1090 :     # need to clone the token if it is all used up
1091 :     if ($copy_of_compounds_to_tokens{$cpd}->{$token_id} == 0)
1092 :     {
1093 :     my $new_token_id = $token_id_counter++;
1094 :     &clone_token($token_id, $new_token_id, \%tokens,
1095 :     \%compounds_to_tokens,
1096 :     \%compounds_borrowed_to_tokens);
1097 :    
1098 :     foreach my $icpd (keys %compounds_to_tokens)
1099 :     {
1100 :     if ($compounds_to_tokens{$icpd}->{$token_id} > 0)
1101 :     {
1102 :     $copy_of_compounds_to_tokens{$icpd}->{$new_token_id} =
1103 :     $compounds_to_tokens{$icpd}->{$token_id};
1104 :     }
1105 :     }
1106 :    
1107 :     print STDERR "\t\tCloned token '$token_id' for $cpd, new token is '$new_token_id'\n" unless !$debug;
1108 :    
1109 :     $clone_history{$token_id} = $new_token_id;
1110 :     $token_id = $new_token_id;
1111 :     }
1112 :    
1113 :     while ($copy_of_compounds_to_tokens{$cpd}->{$token_id} > 0)
1114 :     {
1115 :     push @token_set, $token_id;
1116 :     $copy_of_compounds_to_tokens{$cpd}->{$token_id}--;
1117 :     $num_tokens_still_needed--;
1118 :    
1119 :     if ($num_tokens_still_needed == 0)
1120 :     {
1121 :     print STDERR "\t\tPushing token set '@token_set' for $cpd\n" unless !$debug;
1122 :     my @copy_of_token_set = @token_set;
1123 :     push @token_sets_for_cpd, \@copy_of_token_set;
1124 :     @token_set = ();
1125 :     $num_tokens_still_needed = $num_tokens_needed;
1126 :     }
1127 :     }
1128 :     }
1129 :    
1130 :     $tokens_to_use_for_cpds{$cpd} = \@token_sets_for_cpd;
1131 :     }
1132 :    
1133 :     # in case a token had to be cloned for this reaction after another
1134 :     # compound already determined to use it, check history and use new token id
1135 :     foreach my $cpd (keys %tokens_to_use_for_cpds)
1136 :     {
1137 :     foreach my $token_set (@{$tokens_to_use_for_cpds{$cpd}})
1138 :     {
1139 :     for (my $i = 0; $i < scalar @$token_set; $i++)
1140 :     {
1141 :     my $updated_token_id = $token_set->[$i];
1142 :    
1143 :     while ($clone_history{$updated_token_id})
1144 :     {
1145 :     $updated_token_id = $clone_history{$updated_token_id};
1146 :     }
1147 :    
1148 :     if ($token_set->[$i] != $updated_token_id)
1149 :     {
1150 :     print STDERR "\t\tReplacing '$token_set->[$i]' with '$updated_token_id' for $cpd\n" unless !$debug;
1151 :     splice @$token_set, $i, 1, ($updated_token_id);
1152 :     }
1153 :     }
1154 :     }
1155 :     }
1156 :    
1157 :     # we may have multiple sets of tokens to use for some compounds, so
1158 :     # make sure we're prepared for each combination by cloning sets as necessary
1159 :     my %cpd_to_token_set_index;
1160 :     my $num_combinations = 1;
1161 :     my %used_token_sets_for_cpd;
1162 :    
1163 :     foreach my $cpd (keys %tokens_to_use_for_cpds)
1164 :     {
1165 :     $cpd_to_token_set_index{$cpd} = 0;
1166 :     $num_combinations *= scalar @{$tokens_to_use_for_cpds{$cpd}};
1167 :     }
1168 :    
1169 :     for (my $i = 0; $i < $num_combinations; $i++)
1170 :     {
1171 :     my %combination_cpds_to_tokens;
1172 :    
1173 :     print STDERR "\t\tPreparing combination ", $i+1, " out of $num_combinations, indices are @{[ map { $cpd_to_token_set_index{$_} } sort keys %cpd_to_token_set_index ]}\n" unless !$debug;
1174 :    
1175 :     # keep track of who was cloned to what for this combination in case
1176 :     # different compounds are using the same tokens
1177 :     my %clone_history_this_combination;
1178 :    
1179 :     foreach my $cpd (sort keys %cpd_to_token_set_index)
1180 :     {
1181 :     my $token_set_index = $cpd_to_token_set_index{$cpd};
1182 :     my @token_set = @{$tokens_to_use_for_cpds{$cpd}->[$token_set_index]};
1183 :     my @new_token_set;
1184 :    
1185 :     # don't clone if this is the first time using this token set
1186 :     # should be able to do this mathematically
1187 :     if (! defined $used_token_sets_for_cpd{$cpd}->{$token_set_index})
1188 :     {
1189 :     @new_token_set = @token_set;
1190 :     $used_token_sets_for_cpd{$cpd}->{$token_set_index} = 1;
1191 :     }
1192 :     else
1193 :     {
1194 :     foreach my $token_id (@token_set)
1195 :     {
1196 :     my $new_token_id;
1197 :    
1198 :     if ($clone_history_this_combination{$token_id})
1199 :     {
1200 :     $new_token_id = $clone_history_this_combination{$token_id};
1201 :     }
1202 :     else
1203 :     {
1204 :     $new_token_id = $token_id_counter++;
1205 :     &clone_token($token_id, $new_token_id, \%tokens,
1206 :     \%compounds_to_tokens,
1207 :     \%compounds_borrowed_to_tokens);
1208 :    
1209 :     print STDERR "\t\t\tCloned token '$token_id' for $cpd, new token is '$new_token_id'\n" unless !$debug;
1210 :    
1211 :     $clone_history_this_combination{$token_id} = $new_token_id;
1212 :     }
1213 :    
1214 :     push @new_token_set, $new_token_id;
1215 :     }
1216 :     }
1217 :    
1218 :     push @{$combination_cpds_to_tokens{$cpd}}, @new_token_set;
1219 :     }
1220 :    
1221 :     push @final_tokens_to_use, \%combination_cpds_to_tokens;
1222 :    
1223 :     # move the cpd to token set indices for the next combination
1224 :     foreach my $cpd (sort keys %cpd_to_token_set_index)
1225 :     {
1226 :     if ($cpd_to_token_set_index{$cpd} < scalar @{$tokens_to_use_for_cpds{$cpd}} - 1)
1227 :     {
1228 :     $cpd_to_token_set_index{$cpd}++;
1229 :     last;
1230 :     }
1231 :     else
1232 :     {
1233 :     $cpd_to_token_set_index{$cpd} = 0;
1234 :     }
1235 :     }
1236 :     }
1237 :     }
1238 :    
1239 :     # last step, need to create place-holder tokens for the non-main substrates
1240 :     # and record the tokens being used for this reaction
1241 :     foreach my $combination (@final_tokens_to_use)
1242 :     {
1243 :     foreach my $cpd (keys %tokens_needed)
1244 :     {
1245 :     if (! defined $reactions_to_main_substrates{$reaction}->{$cpd})
1246 :     {
1247 :     my $new_token_id = $token_id_counter++;
1248 :     my %new_token;
1249 :     $new_token{visited_reactions} = {};
1250 :     $new_token{visited_compounds} = {};
1251 :     $compounds_to_tokens{$cpd}->{$new_token_id} += $tokens_needed{$cpd};
1252 :     $copy_of_compounds_to_tokens{$cpd}->{$new_token_id} += $tokens_needed{$cpd};
1253 :     $new_token{token_path_inputs} = { $cpd => $tokens_needed{$cpd} };
1254 :     $tokens{$new_token_id} = \%new_token;
1255 :    
1256 :     for (my $i = 0; $i < $tokens_needed{$cpd}; $i++)
1257 :     {
1258 :     push @{$combination->{$cpd}}, $new_token_id;
1259 :     }
1260 :     }
1261 :     }
1262 :    
1263 :     foreach my $cpd (keys %$combination)
1264 :     {
1265 :     map { $tokens_to_use_to_reactions{$_}->{$reaction} = 1 } @{$combination->{$cpd}};
1266 :     }
1267 :     }
1268 :    
1269 :     $reactions_to_tokens_to_use{$reaction} = \@final_tokens_to_use;
1270 :     }
1271 :    
1272 :     # keep track of tokens merged during this round
1273 :     my %token_merge_history;
1274 :    
1275 :     foreach my $reaction (keys %reactions_to_tokens_to_use)
1276 :     {
1277 :     # we may have multiple sets of tokens to use
1278 :     my @final_tokens_to_use = @{$reactions_to_tokens_to_use{$reaction}};
1279 :     my $num_combinations = scalar @final_tokens_to_use;
1280 :    
1281 :     # since we may be running this reaction several times with some of the same
1282 :     # tokens, don't process token merge history until all sets have been run
1283 :     my %token_merge_history_this_reaction;
1284 :    
1285 :     for (my $i = 0; $i < $num_combinations; $i++)
1286 :     {
1287 :     print STDERR "\tRunning reaction $reaction (", $i+1, " out of $num_combinations)\n" unless !$debug;
1288 :    
1289 :     # assemble list of tokens to use for this combination
1290 :     my %tokens_to_use;
1291 :     # find the most recently visited compound's step
1292 :     my $most_recent_step = 0;
1293 :    
1294 :     # loop through substrates
1295 :     foreach my $cpd (keys %{$reactions_to_tokens_to_use{$reaction}})
1296 :     {
1297 :     # remove the token ids from the real compounds_to_tokens map
1298 :     my @cpd_token_ids = @{$final_tokens_to_use[$i]->{$cpd}};
1299 :     print STDERR "\t\tFound tokens '@cpd_token_ids' for $cpd\n" unless !$debug;
1300 :    
1301 :     foreach my $token_id (@cpd_token_ids)
1302 :     {
1303 :     # check if token_id has been merged into a new token by a previous reaction
1304 :     while (defined $token_merge_history{$token_id})
1305 :     {
1306 :     $token_id = $token_merge_history{$token_id};
1307 :     }
1308 :    
1309 :     $compounds_to_tokens{$cpd}->{$token_id}--;
1310 :    
1311 :     if ($compounds_to_tokens{$cpd}->{$token_id} == 0)
1312 :     {
1313 :     delete $compounds_to_tokens{$cpd}->{$token_id};
1314 :     }
1315 :    
1316 :     $tokens_to_use{$token_id} = 1;
1317 :    
1318 :     if ($tokens{$token_id}->{visited_compounds}->{$cpd} > $most_recent_step)
1319 :     {
1320 :     $most_recent_step = $tokens{$token_id}->{visited_compounds}->{$cpd};
1321 :     }
1322 :     }
1323 :     }
1324 :    
1325 :     # process list of unique token ids
1326 :     my @tokens_to_use = sort { $a <=> $b } keys %tokens_to_use;
1327 :     my $go_forward_token_id;
1328 :    
1329 :     if (scalar @tokens_to_use == 1)
1330 :     {
1331 :     $go_forward_token_id = shift @tokens_to_use;
1332 :     print STDERR "\t\tGoing forward with '$go_forward_token_id'\n" unless !$debug;
1333 :     }
1334 :     else
1335 :     {
1336 :     $go_forward_token_id = $token_id_counter++;
1337 :     $tokens{$go_forward_token_id} = {};
1338 :     print STDERR "\t\tRemember to merge tokens '@tokens_to_use' into '$go_forward_token_id'\n" unless !$debug;
1339 :    
1340 :     # record the need to merge - we'll do it after processing all sets of substrates
1341 :     foreach my $token_id (@tokens_to_use)
1342 :     {
1343 :     push @{$token_merge_history_this_reaction{$token_id}}, $go_forward_token_id;
1344 :     }
1345 :     }
1346 :    
1347 :     my $go_forward_token = $tokens{$go_forward_token_id};
1348 :     my @prodArr = @{$reactions_to_product_arrays{$reaction}};
1349 :    
1350 :     # add current reaction and products to accumulated token history.
1351 :     # reaction is mapped to loop counter to maintain history of order of execution
1352 :     $go_forward_token->{visited_reactions}->{$reaction} = $loop_counter;
1353 :    
1354 :     foreach my $prod (@prodArr)
1355 :     {
1356 :     my $cpd = @{$prod}[0];
1357 :     my $stoich = @{$prod}[1];
1358 :     my $main = @{$prod}[2];
1359 :    
1360 :     # keep track of path outputs we've seen
1361 :     if (defined $path_outputs{$cpd})
1362 :     {
1363 :     $path_outputs{$cpd} += $stoich;
1364 :     }
1365 :    
1366 :     if ($main)
1367 :     {
1368 :     $go_forward_token->{visited_compounds}->{$cpd} = $most_recent_step + 1;
1369 :     }
1370 :    
1371 :     # push tokens
1372 :     $compounds_to_tokens{$cpd}->{$go_forward_token_id} += $stoich;
1373 :     }
1374 :     }
1375 :    
1376 :     # now process this reaction's token merge history
1377 :     foreach my $token_id (sort { $a <=> $b } keys %token_merge_history_this_reaction)
1378 :     {
1379 :     # find the unique set of up to date merge ids
1380 :     my @merge_ids = @{$token_merge_history_this_reaction{$token_id}};
1381 :     my %updated_merge_ids;
1382 :    
1383 :     foreach my $token_id (@merge_ids)
1384 :     {
1385 :     while (defined $token_merge_history{$token_id})
1386 :     {
1387 :     $token_id = $token_merge_history{$token_id};
1388 :     }
1389 :    
1390 :     $updated_merge_ids{$token_id} = 1;
1391 :     }
1392 :    
1393 :     print STDERR "\t\tupdated merge id list for '$token_id': '@{[ keys %updated_merge_ids ]}'\n" unless !$debug;
1394 :    
1395 :     my $wrap_up_token_id = $token_id_counter++;
1396 :     $tokens{$wrap_up_token_id} = {};
1397 :     my $wrap_up_token = $tokens{$wrap_up_token_id};
1398 :     my @tokens_to_merge;
1399 :     push @tokens_to_merge, keys %updated_merge_ids, $token_id;
1400 :    
1401 :     # merge from oldest to youngest to update visited_compounds history
1402 :     foreach my $itoken_id (sort { $a <=> $b } @tokens_to_merge)
1403 :     {
1404 :     print STDERR "\t\t\tmerging '$itoken_id' into '$wrap_up_token_id'\n" unless !$debug;
1405 :    
1406 :     my $itoken = $tokens{$itoken_id};
1407 :     map { $wrap_up_token->{visited_reactions}->{$_} = $itoken->{visited_reactions}->{$_} } keys %{$itoken->{visited_reactions}};
1408 :     map { $wrap_up_token->{visited_compounds}->{$_} = $itoken->{visited_compounds}->{$_} } keys %{$itoken->{visited_compounds}};
1409 :     map { $wrap_up_token->{token_path_inputs}->{$_} += $itoken->{token_path_inputs}->{$_} } keys %{$itoken->{token_path_inputs}};
1410 :     $wrap_up_token->{initial_pass} |= $itoken->{initial_pass};
1411 :     $token_merge_history{$itoken_id} = $wrap_up_token_id;
1412 :    
1413 :     # tokens might be spread across multiple compounds; change them all
1414 :     # to new id
1415 :     foreach my $cpd (keys %compounds_to_tokens)
1416 :     {
1417 :     if ($compounds_to_tokens{$cpd}->{$itoken_id} > 0)
1418 :     {
1419 :     $compounds_to_tokens{$cpd}->{$wrap_up_token_id} +=
1420 :     $compounds_to_tokens{$cpd}->{$itoken_id};
1421 :     }
1422 :     }
1423 :    
1424 :     # tokens might have borrowed compounds; change them all to new id
1425 :     foreach my $cpd (keys %compounds_borrowed_to_tokens)
1426 :     {
1427 :     if ($compounds_borrowed_to_tokens{$cpd}->{$itoken_id} > 0)
1428 :     {
1429 :     $compounds_borrowed_to_tokens{$cpd}->{$wrap_up_token_id} +=
1430 :     $compounds_borrowed_to_tokens{$cpd}->{$itoken_id};
1431 :     }
1432 :     }
1433 :     }
1434 :     }
1435 :     }
1436 :    
1437 :     # now delete the tokens that were used merged in these reactions
1438 :     foreach my $token_id (keys %token_merge_history)
1439 :     {
1440 :     foreach my $icpd (keys %compounds_to_tokens)
1441 :     {
1442 :     delete $compounds_to_tokens{$icpd}->{$token_id};
1443 :     }
1444 :    
1445 :     foreach my $icpd (keys %compounds_borrowed_to_tokens)
1446 :     {
1447 :     delete $compounds_borrowed_to_tokens{$icpd}->{$token_id};
1448 :     }
1449 :    
1450 :     print STDERR "\t\tDeleting token '$token_id'\n" unless !$debug;
1451 :     delete $tokens{$token_id};
1452 :     }
1453 :    
1454 :     print STDERR "\nBalancing tokens\n" unless !$debug;
1455 :    
1456 :     foreach my $token_id (keys %tokens)
1457 :     {
1458 :     &balance_borrowing_and_giving($token_id, \%compounds_to_tokens,
1459 :     \%compounds_borrowed_to_tokens);
1460 :     }
1461 :    
1462 :     &print_token_status([sort { $a <=> $b } keys %tokens], \%tokens, \%compounds_to_tokens, \%compounds_borrowed_to_tokens, $fig);
1463 :    
1464 :     print STDERR "\nChecking for done\n" unless !$debug;
1465 :    
1466 :     print STDERR "\n\ntoken ids: @{[ sort { $a <=> $b } map { $_ if ! defined $tokens{$_}->{done} } keys %tokens ]}\n" unless !$debug;
1467 :    
1468 :     # we're done when all the main compounds in initial-pass tokens
1469 :     # have reached path outputs and repaid their borrowed tokens,
1470 :     # or have reached a dead end.
1471 :     # Check if we're done pushing and borrowing tokens first.
1472 :     my %not_done_tokens;
1473 :    
1474 :     foreach my $token_id (keys %tokens)
1475 :     {
1476 :     if (! defined $tokens{$token_id}->{done})
1477 :     {
1478 :     if (! &check_token_for_done($token_id, \%compounds_to_tokens,
1479 :     \%compounds_borrowed_to_tokens,
1480 :     \%all_compounds_to_main, \%path_outputs,
1481 :     \%scenario_cycles, \%tokens, $fig, $outputs_lists))
1482 :     {
1483 :     $not_done_tokens{$token_id} = 1;
1484 :     }
1485 :     }
1486 :     }
1487 :    
1488 :     print STDERR "\nChecking if we can pay back borrowed compounds from other tokens\n" unless !$debug;
1489 :    
1490 :     foreach my $bcpd (keys %compounds_borrowed_to_tokens)
1491 :     {
1492 :     next if $path_outputs{$bcpd}; # tokens must manage their own path outputs
1493 :    
1494 :     if (scalar keys %{$compounds_borrowed_to_tokens{$bcpd}} > 0 &&
1495 :     scalar keys %{$compounds_to_tokens{$bcpd}} > 0)
1496 :     {
1497 :     my %borrowers_to_givers;
1498 :    
1499 :     foreach my $borrower_id (keys %{$compounds_borrowed_to_tokens{$bcpd}})
1500 :     {
1501 :     next if defined $tokens{$borrower_id}->{done}; # don't repay deadenders
1502 :    
1503 :     my $num_needed = $compounds_borrowed_to_tokens{$bcpd}->{$borrower_id};
1504 :    
1505 :     giver: foreach my $giver_id (keys %{$compounds_to_tokens{$bcpd}})
1506 :     {
1507 :     next if defined $tokens{$giver_id}->{done};
1508 :    
1509 :     my $num_to_give = $compounds_to_tokens{$bcpd}->{$giver_id};
1510 :    
1511 :     print STDERR "\tToken '$giver_id' has $num_to_give $bcpd to give to '$borrower_id', which needs $num_needed\n" unless !$debug;
1512 :    
1513 :     my $borrower = $tokens{$borrower_id};
1514 :     my $giver = $tokens{$giver_id};
1515 :    
1516 :     # check whether the giver and borrower have conflicting histories
1517 :     foreach my $visited_reaction (keys %{$giver->{visited_reactions}})
1518 :     {
1519 :     if (($visited_reaction =~ /(.*)_R/ &&
1520 :     defined $borrower->{visited_reactions}->{$1."_L"}) ||
1521 :     ($visited_reaction =~ /(.*)_L/ &&
1522 :     defined $borrower->{visited_reactions}->{$1."_R"}))
1523 :     {
1524 :     print STDERR "\t\tConflict on $visited_reaction\n" unless !$debug;
1525 :     next giver;
1526 :     }
1527 :    
1528 :     }
1529 :    
1530 :     push @{$borrowers_to_givers{$borrower_id}}, $giver_id;
1531 :     }
1532 :     }
1533 :    
1534 :     # we have a list of potential givers for each borrower for this compound.
1535 :     # Now figure out who the lucky givers will be.
1536 :     my %givers_to_borrowers;
1537 :    
1538 :     foreach my $borrower_id (keys %borrowers_to_givers)
1539 :     {
1540 :     my %lucky_givers;
1541 :     my $num_needed = $compounds_borrowed_to_tokens{$bcpd}->{$borrower_id};
1542 :    
1543 :     print STDERR "\tCollecting lucky givers for '$borrower_id' for $bcpd, need $num_needed\n" unless !$debug;
1544 :    
1545 :     # check potential givers starting with those with the most
1546 :     my @potential_givers = reverse sort { $compounds_to_tokens{$bcpd}->{$a} <=> $compounds_to_tokens{$bcpd}->{$b} } @{$borrowers_to_givers{$borrower_id}};
1547 :    
1548 :     foreach my $giver_id (@potential_givers)
1549 :     {
1550 :     if (! defined $lucky_givers{$giver_id})
1551 :     {
1552 :     my $num_to_give = $compounds_to_tokens{$bcpd}->{$giver_id};
1553 :     print STDERR "\t\t'$giver_id' has $num_to_give to give\n" unless !$debug;
1554 :     $lucky_givers{$giver_id} = 1;
1555 :     $num_needed -= $num_to_give;
1556 :     last if $num_needed <= 0;
1557 :     }
1558 :     }
1559 :    
1560 :     foreach my $giver_id (keys %lucky_givers)
1561 :     {
1562 :     push @{$givers_to_borrowers{$giver_id}}, $borrower_id;
1563 :     }
1564 :     }
1565 :    
1566 :    
1567 :     foreach my $orig_giver_id (keys %givers_to_borrowers)
1568 :     {
1569 :     my @borrowers_list = @{$givers_to_borrowers{$orig_giver_id}};
1570 :     my @givers_list = ($orig_giver_id);
1571 :    
1572 :     # clone enough givers so that every borrower gets one. Last borrower
1573 :     # gets the orgiinal giver.
1574 :    
1575 :     while (scalar @borrowers_list > scalar @givers_list)
1576 :     {
1577 :     my $new_giver_id = $token_id_counter++;
1578 :     &clone_token($orig_giver_id, $new_giver_id, \%tokens,
1579 :     \%compounds_to_tokens, \%compounds_borrowed_to_tokens);
1580 :     push @givers_list, $new_giver_id;
1581 :     }
1582 :    
1583 :     for (my $k = 0; $k < scalar @givers_list; $k++)
1584 :     {
1585 :     my $giver_id = $givers_list[$k];
1586 :     my $giver = $tokens{$giver_id};
1587 :     my $borrower_id = $borrowers_list[$k];
1588 :     my $borrower = $tokens{$borrower_id};
1589 :    
1590 :     print STDERR "\n\tMerging '$giver_id' into '$borrower_id'\n" unless !$debug;
1591 :    
1592 :     # bump the borrower's visited reactions and compounds counters forward,
1593 :     # then merge the giver's visited reactions and compounds, unless
1594 :     # the borrower has already visited them
1595 :     map { $borrower->{visited_reactions}->{$_} += $loop_counter } keys %{$borrower->{visited_reactions}};
1596 :     map { $borrower->{visited_compounds}->{$_} += $loop_counter } keys %{$borrower->{visited_compounds}};
1597 :     map { $borrower->{visited_reactions}->{$_} = $giver->{visited_reactions}->{$_} unless defined $borrower->{visited_reactions}->{$_} } keys %{$giver->{visited_reactions}};
1598 :     map { $borrower->{visited_compounds}->{$_} = $giver->{visited_compounds}->{$_} unless defined $borrower->{visited_compounds}->{$_} } keys %{$giver->{visited_compounds}};
1599 :     map { $borrower->{token_path_inputs}->{$_} += $giver->{token_path_inputs}->{$_} } keys %{$giver->{token_path_inputs}};
1600 :     $borrower->{initial_pass} |= $giver->{initial_pass};
1601 :    
1602 :     foreach my $icpd (keys %compounds_to_tokens)
1603 :     {
1604 :     if ($compounds_to_tokens{$icpd}->{$giver_id} > 0)
1605 :     {
1606 :     $compounds_to_tokens{$icpd}->{$borrower_id} += $compounds_to_tokens{$icpd}->{$giver_id};
1607 :     delete $compounds_to_tokens{$icpd}->{$giver_id};
1608 :     }
1609 :     }
1610 :    
1611 :     foreach my $icpd (keys %compounds_borrowed_to_tokens)
1612 :     {
1613 :     if ($compounds_borrowed_to_tokens{$icpd}->{$giver_id} > 0)
1614 :     {
1615 :     $compounds_borrowed_to_tokens{$icpd}->{$borrower_id} += $compounds_borrowed_to_tokens{$icpd}->{$giver_id};
1616 :     delete $compounds_borrowed_to_tokens{$icpd}->{$giver_id};
1617 :     }
1618 :     }
1619 :    
1620 :     delete $tokens{$giver_id};
1621 :     delete $not_done_tokens{$giver_id};
1622 :    
1623 :     &balance_borrowing_and_giving($borrower_id, \%compounds_to_tokens,
1624 :     \%compounds_borrowed_to_tokens);
1625 :    
1626 :     &print_token_status([$borrower_id], \%tokens, \%compounds_to_tokens,
1627 :     \%compounds_borrowed_to_tokens, $fig);
1628 :    
1629 :     if (&check_token_for_done($borrower_id, \%compounds_to_tokens,
1630 :     \%compounds_borrowed_to_tokens,
1631 :     \%all_compounds_to_main, \%path_outputs,
1632 :     \%scenario_cycles,\%tokens, $fig, $outputs_lists))
1633 :     {
1634 :     delete $not_done_tokens{$borrower_id};
1635 :     }
1636 :     }
1637 :     }
1638 :     }
1639 :     }
1640 :    
1641 :     # Now check if we've reached a dead end, either a compound we can't push or a
1642 :     # borrowed compound we can't repay. Also determine whether there is a reaction
1643 :     # to run that can move an initial pass token forward.
1644 :    
1645 :     print STDERR "\nChecking for dead ends\n" unless !$debug;
1646 :    
1647 :     my $found_reaction_for_initial_pass_token = 0;
1648 :    
1649 :     check: foreach my $token_id (keys %not_done_tokens)
1650 :     {
1651 :     # determine which compounds the token is sitting on, and whether a
1652 :     # reaction can proceed from those compounds that isn't a loop
1653 :     # back to compounds already visited
1654 :     my %visited_compounds = %{$tokens{$token_id}->{visited_compounds}};
1655 :     my %visited_reactions = %{$tokens{$token_id}->{visited_reactions}};
1656 :     my $dead_end_cpd;
1657 :    
1658 :     print STDERR "\tChecking if '$token_id' can run\n" unless !$debug;
1659 :    
1660 :     my $found_reaction_for_token = 0;
1661 :    
1662 :     substrate: foreach my $cpd (keys %compounds_to_tokens)
1663 :     {
1664 :     if ($compounds_to_tokens{$cpd}->{$token_id} > 0 &&
1665 :     ! defined $path_outputs{$cpd})
1666 :     {
1667 :     print STDERR "\t\tChecking substrate $cpd (main: $all_compounds_to_main{$cpd})\n" unless !$debug;
1668 :    
1669 :     foreach my $reaction (@{$substrates_to_reactions->{$cpd}})
1670 :     {
1671 :     next if ($reaction =~ /(.*)_R/ && defined $visited_reactions{$1."_L"}) ||
1672 :     ($reaction =~ /(.*)_L/ && defined $visited_reactions{$1."_R"});
1673 :    
1674 :     print STDERR "\t\t\tChecking reaction $reaction\n" unless !$debug;
1675 :    
1676 :     my $prods_are_ok = 0;
1677 :    
1678 :     foreach my $prod (@{$reactions_to_products->{$reaction}})
1679 :     {
1680 :     print STDERR "\t\t\t\tChecking product $prod\n" unless !$debug;
1681 :    
1682 :     if (! defined $visited_compounds{$prod} ||
1683 :     $visited_compounds{$prod} >= $visited_compounds{$cpd} ||
1684 :     defined $path_outputs{$prod} ||
1685 :     $compounds_borrowed_to_tokens{$prod}->{$token_id} > 0)
1686 :     {
1687 :     $prods_are_ok = $prod;
1688 :     last;
1689 :     }
1690 :     }
1691 :    
1692 :     if ($prods_are_ok)
1693 :     {
1694 :     print STDERR "\tToken '$token_id' can run $reaction on $cpd to produce $prods_are_ok\n" unless !$debug;
1695 :     $found_reaction_for_token = 1;
1696 :    
1697 :     if ($tokens{$token_id}->{initial_pass})
1698 :     {
1699 :     $found_reaction_for_initial_pass_token = 1;
1700 :     }
1701 :    
1702 :     next substrate;
1703 :     }
1704 :     }
1705 :    
1706 :     # didn't find a reaction for this substrate
1707 :     $dead_end_cpd = $cpd if $all_compounds_to_main{$cpd} &&
1708 :     ! defined $path_outputs{$cpd};
1709 :     }
1710 :     }
1711 :    
1712 :     if ($dead_end_cpd)
1713 :     {
1714 :     print STDERR "\tToken '$token_id' has reached a dead end on $dead_end_cpd\n" unless !$debug;
1715 :     $tokens{$token_id}->{done} = "dead end on $dead_end_cpd";
1716 :     }
1717 :     elsif (! $found_reaction_for_token)
1718 :     {
1719 :     # didn't find any reaction to run.
1720 :     # check to see if there are borrowed compounds to repay
1721 :     product: foreach my $cpd (keys %compounds_borrowed_to_tokens)
1722 :     {
1723 :     if ($compounds_borrowed_to_tokens{$cpd}->{$token_id} > 0)
1724 :     {
1725 :     print STDERR "\t\tChecking product $cpd\n" unless !$debug;
1726 :    
1727 :     rxn: foreach my $reaction (@{$products_to_reactions->{$cpd}})
1728 :     {
1729 :     next if ($reaction =~ /(.*)_R/ && defined $visited_reactions{$1."_L"})
1730 :     || ($reaction =~ /(.*)_L/ && defined $visited_reactions{$1."_R"});
1731 :    
1732 :     print STDERR "\t\t\tChecking reaction $reaction\n" unless !$debug;
1733 :    
1734 :     my $substrates_are_ok = 0;
1735 :    
1736 :     foreach my $sub (@{$reactions_to_substrates->{$reaction}})
1737 :     {
1738 :     print STDERR "\t\t\t\tChecking substrate $sub\n" unless !$debug;
1739 :    
1740 :     if (defined $path_outputs{$sub})
1741 :     {
1742 :     # don't run reactions that use up outputs
1743 :     next rxn;
1744 :     }
1745 :    
1746 :     if (! defined $visited_compounds{$sub} ||
1747 :     $visited_compounds{$sub} <= $visited_compounds{$cpd} ||
1748 :     defined $scenario_cycles{$sub})
1749 :     {
1750 :     $substrates_are_ok = $sub;
1751 :     }
1752 :     }
1753 :    
1754 :     if ($substrates_are_ok)
1755 :     {
1756 :     print STDERR "\tToken '$token_id' can wait for $reaction on $substrates_are_ok to produce borrowed compound $cpd\n" unless !$debug;
1757 :     $found_reaction_for_token = 1;
1758 :    
1759 :     if ($tokens{$token_id}->{initial_pass})
1760 :     {
1761 :     $found_reaction_for_initial_pass_token = 1;
1762 :     }
1763 :    
1764 :     last product;
1765 :     }
1766 :     }
1767 :    
1768 :     # didn't find a reaction for this product
1769 :     $dead_end_cpd = $cpd if $all_compounds_to_main{$cpd} &&
1770 :     ! defined $path_outputs{$cpd};
1771 :     }
1772 :     }
1773 :    
1774 :     # didn't find any reaction to run.
1775 :     if (! $found_reaction_for_token)
1776 :     {
1777 :     if ($dead_end_cpd)
1778 :     {
1779 :     print STDERR "\tToken '$token_id' has reached a dead end on borrowed compound $dead_end_cpd\n" unless !$debug;
1780 :     $tokens{$token_id}->{done} = "dead end on borrowed compound $dead_end_cpd";
1781 :     }
1782 :     else
1783 :     {
1784 :     # nothing to push, borrow or do
1785 :     print STDERR "\tToken '$token_id' has reached a dead end\n" unless !$debug;
1786 :     $tokens{$token_id}->{done} = "dead end";
1787 :     }
1788 :     }
1789 :     }
1790 :     }
1791 :    
1792 :     if($find_first)
1793 :     {
1794 :     foreach my $token_id (sort { $tokens{$a}->{done} <=> $tokens{$b}->{done} } keys %tokens)
1795 :     {
1796 :     if($tokens{$token_id}->{done} == 1)
1797 :     {
1798 :     $done = 1;
1799 :     }
1800 :     }
1801 :     }
1802 :     # is there an initial pass token that can make progress?
1803 :     if ($found_reaction_for_initial_pass_token)
1804 :     {
1805 :     if (scalar keys %reactions_to_tokens_to_use == 0)
1806 :     {
1807 :     # Couldn't run any reactions this time around.
1808 :     # Push more tokens through from the beginning of the path to
1809 :     # supply more substrates.
1810 :     $add_path_inputs = 1;
1811 :     print STDERR "\nSupplying more path inputs to push stalled tokens\n" unless !$debug;
1812 :     }
1813 :     }
1814 :     else
1815 :     {
1816 :     $done = 1;
1817 :     }
1818 :    
1819 :     $loop_counter++;
1820 :    
1821 :     if ($loop_counter >= $infinite_loop_check)
1822 :     {
1823 :     $data_results{"infinite"} = 1;
1824 :     print STDERR "Encountered an infinite loop\n" unless !$debug;
1825 :     $done = 1;
1826 :     }
1827 :     }
1828 :    
1829 :     # reverse %compounds_to_tokens, since all tokens should be at path outputs now
1830 :     my %tokens_to_compounds;
1831 :    
1832 :     foreach my $cpd (keys %compounds_to_tokens)
1833 :     {
1834 :     foreach my $token_id (keys %{$compounds_to_tokens{$cpd}})
1835 :     {
1836 :     my $num_tokens = $compounds_to_tokens{$cpd}->{$token_id};
1837 :     $tokens_to_compounds{$token_id}->{$cpd} = $num_tokens if $num_tokens > 0;
1838 :     }
1839 :     }
1840 :    
1841 :     print STDERR "\n\ntoken ids: @{[ sort { $a <=> $b } map { $_ if ! defined $tokens{$_}->{done} } keys %tokens ]}\n" unless !$debug;
1842 :    
1843 :     my $path_counter = 1;
1844 :    
1845 :     foreach my $token_id (sort { $tokens{$a}->{done} <=> $tokens{$b}->{done} } keys %tokens)
1846 :     {
1847 :     my $token = $tokens{$token_id};
1848 :     my %visited_reactions = %{$token->{visited_reactions}};
1849 :     my @path = sort { $visited_reactions{$a} <=> $visited_reactions{$b} }
1850 :     keys %visited_reactions;
1851 :     my %visited_compounds = %{$token->{visited_compounds}};
1852 :     my @compounds = sort { $visited_compounds{$a} <=> $visited_compounds{$b} }
1853 :     keys %visited_compounds;
1854 :    
1855 :     print STDERR "Adding token id: $token_id\n" unless !$debug;
1856 :    
1857 :    
1858 :     #each key in data_results is a token which points to an array
1859 :     # [0]=initial pass [1]=0/1 if its done [2]=reaction path [3]=compounds
1860 :     # [4]= html string of inputs
1861 :     # [5]= html string of outputs
1862 :     # [6]= html string of borrowed compounds
1863 :     # [7] = array of path input compounds
1864 :     # [8] = array of path output compounds
1865 :    
1866 :    
1867 :     $data_results{$token_id} = [$token->{initial_pass},$token->{done},\@path,\@compounds,[],[],[],$token->{token_path_inputs},$tokens_to_compounds{$token_id}];
1868 :    
1869 :     foreach my $input (keys %{$token->{token_path_inputs}})
1870 :     {
1871 :     my $input_stoich = $token->{token_path_inputs}->{$input};
1872 :     my $output_stoich = $tokens_to_compounds{$token_id}->{$input};
1873 :    
1874 :     # don't balance scenario cycled compounds until final assembly
1875 :     if ($scenario_cycles{$input} && ! $create_assembly)
1876 :     {
1877 :     my @names = $fig->names_of_compound($input);
1878 :     push @{$data_results{$token_id}->[4]}, "\t\t$input_stoich\t$input $names[0]\n";
1879 :     next;
1880 :     }
1881 :    
1882 :     if ($input_stoich > $output_stoich)
1883 :     {
1884 :     delete $tokens_to_compounds{$token_id}->{$input};
1885 :     $input_stoich -= $output_stoich;
1886 :     $token->{token_path_inputs}->{$input} -= $output_stoich;
1887 :     my @names = $fig->names_of_compound($input);
1888 :     push @{$data_results{$token_id}->[4]}, "\t\t$input_stoich\t$input $names[0]\n";
1889 :     }
1890 :     elsif ($output_stoich > $input_stoich)
1891 :     {
1892 :     delete $token->{token_path_inputs}->{$input};
1893 :     $tokens_to_compounds{$token_id}->{$input} -= $input_stoich;
1894 :     }
1895 :     else
1896 :     {
1897 :     delete $token->{token_path_inputs}->{$input};
1898 :     delete $tokens_to_compounds{$token_id}->{$input};
1899 :     }
1900 :     }
1901 :    
1902 :     foreach my $output (keys %{$tokens_to_compounds{$token_id}})
1903 :     {
1904 :     my @names = $fig->names_of_compound($output);
1905 :     push @{$data_results{$token_id}->[5]},"\t\t$tokens_to_compounds{$token_id}->{$output}\t$output $names[0]\n";
1906 :     }
1907 :    
1908 :     if ($token->{done} != 1)
1909 :     {
1910 :     foreach my $cpd (sort keys %compounds_borrowed_to_tokens)
1911 :     {
1912 :     my $num = $compounds_borrowed_to_tokens{$cpd}->{$token_id};
1913 :     my @names = $fig->names_of_compound($cpd);
1914 :     push @{$data_results{$token_id}->[6]},"\t\t$num $cpd\t$names[0]\n" if ($num > 0);
1915 :     }
1916 :     }
1917 :     }
1918 :    
1919 :    
1920 :    
1921 :    
1922 :     return \%data_results;
1923 :     }
1924 :    
1925 :     sub balance_borrowing_and_giving
1926 :     {
1927 :     my ($token_id, $compounds_to_tokens, $compounds_borrowed_to_tokens) = @_;
1928 :    
1929 :     my %merged_compounds;
1930 :     map { $merged_compounds{$_} = 1 } keys %{$compounds_to_tokens};
1931 :     map { $merged_compounds{$_} = 1 } keys %{$compounds_borrowed_to_tokens};
1932 :    
1933 :     foreach my $icpd (keys %merged_compounds)
1934 :     {
1935 :     my $inum_to_give = $compounds_to_tokens->{$icpd}->{$token_id};
1936 :    
1937 :     if ($inum_to_give > 0)
1938 :     {
1939 :     my $inum_needed = $compounds_borrowed_to_tokens->{$icpd}->{$token_id};
1940 :    
1941 :     if ($inum_to_give == $inum_needed)
1942 :     {
1943 :     delete $compounds_borrowed_to_tokens->{$icpd}->{$token_id};
1944 :     delete $compounds_to_tokens->{$icpd}->{$token_id};
1945 :     }
1946 :     elsif ($inum_to_give > $inum_needed)
1947 :     {
1948 :     delete $compounds_borrowed_to_tokens->{$icpd}->{$token_id};
1949 :     $compounds_to_tokens->{$icpd}->{$token_id} -= $inum_needed;
1950 :     }
1951 :     else
1952 :     {
1953 :     $compounds_borrowed_to_tokens->{$icpd}->{$token_id} -= $inum_to_give;
1954 :     delete $compounds_to_tokens->{$icpd}->{$token_id};
1955 :     }
1956 :     }
1957 :     }
1958 :     }
1959 :    
1960 :     sub print_token_status
1961 :     {
1962 :     my ($token_id_list, $tokens, $compounds_to_tokens, $compounds_borrowed_to_tokens, $fig) = @_;
1963 :    
1964 :     print STDERR "\nToken status:\n" unless !$debug;
1965 :    
1966 :     foreach my $token_id (@$token_id_list)
1967 :     {
1968 :     next if defined $tokens->{$token_id}->{done};
1969 :    
1970 :     print STDERR "\n\ttoken: '$token_id', initial: $tokens->{$token_id}->{initial_pass}\n" unless !$debug;
1971 :    
1972 :     foreach my $cpd (sort keys %{$tokens->{$token_id}->{token_path_inputs}})
1973 :     {
1974 :     my $num = $tokens->{$token_id}->{token_path_inputs}->{$cpd};
1975 :     my @names = $fig->names_of_compound($cpd);
1976 :     print STDERR "\t\tInput: $num $cpd\t$names[0]\n" unless !$debug;
1977 :     }
1978 :    
1979 :     foreach my $cpd (sort keys %$compounds_to_tokens)
1980 :     {
1981 :     my $num = $compounds_to_tokens->{$cpd}->{$token_id};
1982 :     my @names = $fig->names_of_compound($cpd);
1983 :     print STDERR "\t\tStatus: $num $cpd\t$names[0]\n" if ($num > 0 && $debug);
1984 :     }
1985 :    
1986 :     foreach my $cpd (sort keys %$compounds_borrowed_to_tokens)
1987 :     {
1988 :     my $num = $compounds_borrowed_to_tokens->{$cpd}->{$token_id};
1989 :     my @names = $fig->names_of_compound($cpd);
1990 :     print STDERR "\t\tBorrowed: $num $cpd\t$names[0]\n" if ($num > 0 && $debug);
1991 :     }
1992 :    
1993 :     my %visited_compounds = %{$tokens->{$token_id}->{visited_compounds}};
1994 :     print STDERR "\t\tvisited_compounds: @{[map { ($_, $visited_compounds{$_} ) } sort { $visited_compounds{$a} <=> $visited_compounds{$b} } keys %visited_compounds]}\n" unless !$debug;
1995 :    
1996 :     my %visited_reactions = %{$tokens->{$token_id}->{visited_reactions}};
1997 :     print STDERR "\t\tvisited_reactions: @{[map { ($_, $visited_reactions{$_} ) } sort { $visited_reactions{$a} <=> $visited_reactions{$b} } keys %visited_reactions]}\n" unless !$debug;
1998 :    
1999 :     }
2000 :    
2001 :     print STDERR "\n" unless !$debug;
2002 :     }
2003 :    
2004 :     sub clone_token
2005 :     {
2006 :     my ($clone_id, $new_token_id, $tokens, $compounds_to_tokens, $compounds_borrowed_to_tokens) = @_;
2007 :     my (%new_token, %new_visited_reactions, %new_visited_compounds, %new_token_path_inputs);
2008 :    
2009 :     $tokens->{$new_token_id} = \%new_token;
2010 :    
2011 :     my $clone_token = $tokens->{$clone_id};
2012 :     map {$new_visited_reactions{$_} = $clone_token->{visited_reactions}->{$_}} keys %{$clone_token->{visited_reactions}};
2013 :     map {$new_visited_compounds{$_} = $clone_token->{visited_compounds}->{$_}} keys %{$clone_token->{visited_compounds}};
2014 :     map {$new_token_path_inputs{$_} = $clone_token->{token_path_inputs}->{$_}} keys %{$clone_token->{token_path_inputs}};
2015 :    
2016 :     $new_token{visited_reactions} = \%new_visited_reactions;
2017 :     $new_token{visited_compounds} = \%new_visited_compounds;
2018 :     $new_token{token_path_inputs} = \%new_token_path_inputs;
2019 :     $new_token{initial_pass} = $clone_token->{initial_pass};
2020 :    
2021 :     # tokens might be spread across multiple compounds
2022 :     foreach my $icpd (keys %$compounds_to_tokens)
2023 :     {
2024 :     if ($compounds_to_tokens->{$icpd}->{$clone_id} > 0)
2025 :     {
2026 :     $compounds_to_tokens->{$icpd}->{$new_token_id} =
2027 :     $compounds_to_tokens->{$icpd}->{$clone_id};
2028 :     }
2029 :     }
2030 :    
2031 :     # tokens might have borrowed compounds
2032 :     foreach my $icpd (keys %$compounds_borrowed_to_tokens)
2033 :     {
2034 :     if ($compounds_borrowed_to_tokens->{$icpd}->{$clone_id} > 0)
2035 :     {
2036 :     $compounds_borrowed_to_tokens->{$icpd}->{$new_token_id} =
2037 :     $compounds_borrowed_to_tokens->{$icpd}->{$clone_id};
2038 :     }
2039 :     }
2040 :    
2041 :     return \%new_token;
2042 :     }
2043 :    
2044 :     sub check_token_for_done
2045 :     {
2046 :     my ($token_id, $compounds_to_tokens, $compounds_borrowed_to_tokens, $all_compounds_to_main,
2047 :     $path_outputs, $scenario_cycles, $tokens, $fig, $outputs_lists) = @_;
2048 :    
2049 :     my $token_is_done_pushing = 1;
2050 :     my $token_is_done_borrowing = 1;
2051 :    
2052 :     # first determine if there is a main compound that isn't a path output
2053 :     foreach my $cpd (keys %$compounds_to_tokens)
2054 :     {
2055 :     if ($compounds_to_tokens->{$cpd}->{$token_id} > 0)
2056 :     {
2057 :     # also check if scenario cycle compounds need to be pushed
2058 :     if ($all_compounds_to_main->{$cpd} &&
2059 :     (! defined $path_outputs->{$cpd} ||
2060 :     ($scenario_cycles->{$cpd} &&
2061 :     $tokens->{$token_id}->{visited_compounds}->{$cpd} == 0)))
2062 :     {
2063 :     my @names = $fig->names_of_compound($cpd);
2064 :     print STDERR "\ttoken '$token_id' needs to push $cpd $names[0]\n" unless !$debug;
2065 :     $token_is_done_pushing = 0;
2066 :     }
2067 :     }
2068 :     }
2069 :    
2070 :     # now determine if one of the output lists has been satisfied
2071 :     if ($token_is_done_pushing)
2072 :     {
2073 :     my $found_a_list = 0;
2074 :    
2075 :     foreach my $cpd_list (@$outputs_lists)
2076 :     {
2077 :     print STDERR "\t\tchecking outputs_list @$cpd_list\n" unless !$debug;
2078 :    
2079 :     $found_a_list = 1;
2080 :    
2081 :     foreach my $cpd (@$cpd_list)
2082 :     {
2083 :     if (! $compounds_to_tokens->{$cpd}->{$token_id} > 0)
2084 :     {
2085 :     $found_a_list = 0;
2086 :     last;
2087 :     }
2088 :     }
2089 :    
2090 :     last if $found_a_list;
2091 :     }
2092 :    
2093 :     if (! $found_a_list)
2094 :     {
2095 :     print STDERR "\ttoken '$token_id' hasn't satisfied output compound list\n" unless !$debug;
2096 :     $token_is_done_pushing = 0;
2097 :     }
2098 :     }
2099 :    
2100 :     if ($token_is_done_pushing)
2101 :     {
2102 :     foreach my $cpd (keys %$compounds_borrowed_to_tokens)
2103 :     {
2104 :     if ($compounds_borrowed_to_tokens->{$cpd}->{$token_id} > 0)
2105 :     {
2106 : dejongh 1.17 # I don't know why I put in this "if" statement, so I'm going to do the same thing
2107 :     # in both cases, but print a distinguishing debug statement in case I need to find
2108 :     # the occurrences later
2109 : olson 1.1 if ($all_compounds_to_main{$cpd})
2110 :     {
2111 :     my @names = $fig->names_of_compound($cpd);
2112 : dejongh 1.17 print STDERR "\ttoken '$token_id' has borrowed $cpd $names[0] (main)\n" unless !$debug;
2113 :     $token_is_done_borrowing = 0;
2114 :     }
2115 :     else
2116 :     {
2117 :     my @names = $fig->names_of_compound($cpd);
2118 :     print STDERR "\ttoken '$token_id' has borrowed $cpd $names[0] (not main)\n" unless !$debug;
2119 : olson 1.1 $token_is_done_borrowing = 0;
2120 :     }
2121 :     }
2122 :     }
2123 :     }
2124 :    
2125 :     if (! $token_is_done_pushing || ! $token_is_done_borrowing)
2126 :     {
2127 :     return 0;
2128 :     }
2129 :     else
2130 :     {
2131 :     $tokens->{$token_id}->{done} = 1;
2132 :     print STDERR "\tToken '$token_id' is done\n" unless !$debug;
2133 :     return 1;
2134 :     }
2135 :     }
2136 :    
2137 :    
2138 :     sub write_fluxanalyzer_files
2139 :     {
2140 :     my ($dir, $path_inputs, $path_outputs, $path_array,
2141 :     $all_reactions,$reactions_to_substrate_arrays,$reactions_to_product_arrays,
2142 :     $cidToName) = @_;
2143 :    
2144 :     my $x_pos = 10;
2145 :     my $y_pos = 20;
2146 :    
2147 :     #Write the inputs/outputs to a seperate file, along with $stoich and if its main
2148 :     open(A_INPUT, ">$dir/inputs_main");
2149 :    
2150 :     print A_INPUT map {"$_\t$all_compounds_to_main{$_}\n"} keys %$path_inputs;
2151 :    
2152 :     close(A_INPUT);
2153 :    
2154 :     open(A_OUTPUT, ">$dir/outputs_main");
2155 :    
2156 :     print A_OUTPUT map {"$_\t$all_compounds_to_main{$_}\n"} keys %$path_outputs;
2157 :    
2158 :     close(A_OUTPUT);
2159 :    
2160 :    
2161 :    
2162 :     open(REACTIONS, ">$dir/reactions");
2163 :     open(INPUTS, ">$dir/inputs");
2164 :     open(OUTPUTS, ">$dir/outputs");
2165 :     open(PATH, ">$dir/path_info");
2166 :    
2167 :     my @inputs = keys %$path_inputs;
2168 :    
2169 :     foreach my $elem(@{$path_array}){
2170 :     print PATH $elem ."\n";
2171 :     }
2172 :     close(PATH);
2173 :    
2174 :     foreach my $cpd (@inputs)
2175 :     {
2176 :     my $toPrint = $cpd."up\t = 1 $cpd \t| \t";
2177 :     $toPrint .= $path_inputs->{$cpd};
2178 :     $toPrint .= " \t0 100 0 \t$x_pos $y_pos 1 1\t0.01\n";
2179 :     print REACTIONS $toPrint;
2180 :     $y_pos += 20;
2181 :    
2182 :     if ($y_pos == 300)
2183 :     {
2184 :     $x_pos += 60;
2185 :     $y_pos = 20;
2186 :     }
2187 :     }
2188 :    
2189 :     $x_pos += 60;
2190 :     $y_pos = 20;
2191 :    
2192 :     my (@display_array);
2193 :     foreach my $rxn (keys %$all_reactions)
2194 :     {
2195 :     my $direction = $all_reactions->{$rxn};
2196 :    
2197 :     my (@substrate_array, @product_array);
2198 :    
2199 :     push @display_array, $rxn;
2200 :    
2201 :     if ($direction eq "L")
2202 :     {
2203 :     @product_array = @{$reactions_to_substrate_arrays->{$rxn."_L"}};
2204 :     @substrate_array = @{$reactions_to_product_arrays->{$rxn."_L"}};
2205 :     }
2206 :     else
2207 :     {
2208 :     @substrate_array = @{$reactions_to_substrate_arrays->{$rxn."_R"}};
2209 :     @product_array = @{$reactions_to_product_arrays->{$rxn."_R"}};
2210 :     }
2211 :    
2212 :     foreach my $subTuple (@substrate_array)
2213 :     {
2214 :     my @temp = $fig->names_of_compound($subTuple->[0]);
2215 :     $cidToName->{$subTuple->[0]} = $temp [0] if ! defined $cidToName->{$subTuple->[0]};
2216 :     }
2217 :    
2218 :     foreach my $prodTuple(@product_array)
2219 :     {
2220 :     my @temp = $fig->names_of_compound($prodTuple->[0]);
2221 :     $cidToName->{$prodTuple->[0]} = $temp[0] if ! defined $cidToName->{$prodTuple->[0]};
2222 :     }
2223 :    
2224 :     if($direction eq "R" || $direction eq "B")
2225 :     {
2226 :     #write data in a strign for copying to file later
2227 :     my $toFile = '';
2228 :     $toFile.= $rxn."\t";
2229 :    
2230 :     #add all the substrates
2231 :     foreach my $curSub(@substrate_array){
2232 :     $toFile .= $curSub -> [1].' '. $curSub -> [0].' + ';
2233 :     }
2234 :    
2235 :     ##chop off the +
2236 :     chop($toFile);
2237 :     chop($toFile);
2238 :    
2239 :     $toFile.='= ';
2240 :    
2241 :     #add all the products
2242 :     foreach my $curProd(@product_array){
2243 :     $toFile .= $curProd -> [1].' '. $curProd -> [0].' + ';
2244 :     }
2245 :    
2246 :     #chop off the plus
2247 :     chop($toFile);
2248 :     chop($toFile);
2249 :    
2250 :     $toFile.="\t|\t#\t";
2251 :    
2252 :     if($direction eq "B"){
2253 :     $toFile.="-Inf";
2254 :     }
2255 :     else{
2256 :     $toFile.="0";
2257 :     }
2258 :    
2259 :     $toFile.=" Inf 0\t$x_pos $y_pos 1 1 \t0.01\n";
2260 :    
2261 :     print REACTIONS $toFile;
2262 :     }
2263 :     elsif($direction eq "L")
2264 :     {
2265 :     #write data in a strign for copying to file later
2266 :     my $toFile = '';
2267 :     $toFile.= $rxn."\t";
2268 :    
2269 :     #add all the substrates
2270 :     foreach my $curProd(@product_array){
2271 :     $toFile .= $curProd -> [1].' '. $curProd -> [0]." + ";
2272 :     }
2273 :    
2274 :     ##chop off the +
2275 :     chop($toFile);
2276 :     chop($toFile);
2277 :    
2278 :     $toFile.="= ";
2279 :    
2280 :     #add all the products
2281 :     foreach my $curSubstrate(@substrate_array){
2282 :     $toFile .= $curSubstrate -> [1].' '. $curSubstrate -> [0]." + ";
2283 :     }
2284 :    
2285 :     #chop off the plus
2286 :     chop($toFile);
2287 :     chop($toFile);
2288 :    
2289 :     $toFile.="\t|\t#\t";
2290 :    
2291 :     $toFile.="0";
2292 :    
2293 :     $toFile.=" Inf 0\t$x_pos $y_pos 1 1 \t0.01\n";
2294 :    
2295 :     print REACTIONS $toFile;
2296 :     }
2297 :    
2298 :     $y_pos += 20;
2299 :    
2300 :     if ($y_pos == 300)
2301 :     {
2302 :     $x_pos += 60;
2303 :     $y_pos = 20;
2304 :     }
2305 :     }
2306 :    
2307 :     my @outputs = keys %$path_outputs;
2308 :    
2309 :     $x_pos += 60;
2310 :     $y_pos = 20;
2311 :    
2312 :     foreach my $cpd (@inputs)
2313 :     {
2314 :     print INPUTS $cpd, "\t", $path_inputs->{$cpd}, "\t", $cidToName->{$cpd}, "\n";
2315 :     }
2316 :    
2317 :     foreach my $cpd (@outputs)
2318 :     {
2319 :     print OUTPUTS $cpd, "\t", $path_outputs->{$cpd}, "\t", $cidToName->{$cpd}, "\n";
2320 :     my $toPrint = $cpd."ex\t 1 $cpd = \t| \t# \t0 100 0 \t$x_pos $y_pos 1 1\t0.01\n";
2321 :     print REACTIONS $toPrint;
2322 :     $y_pos += 20;
2323 :    
2324 :     if ($y_pos == 300)
2325 :     {
2326 :     $x_pos += 60;
2327 :     $y_pos = 20;
2328 :     }
2329 :     }
2330 :    
2331 :     $x_pos += 60;
2332 :     $y_pos = 20;
2333 :    
2334 :     #print the macromolecule_synthesis and assembly file
2335 :     open(MACRO_SYTH,">$dir/macromolecule_synthesis");
2336 :     open(ASSEM,">$dir/assembly");
2337 :     my $toPrint = "M1 = ";
2338 :    
2339 :     foreach my $cpd (keys %$path_outputs)
2340 :     {
2341 :     $toPrint.="$path_outputs->{$cpd} $cpd + ";
2342 :     print ASSEM "$cpd\tM1\t-100 -100 1\n";
2343 :     $y_pos += 25;
2344 :     }
2345 :    
2346 :     chop $toPrint;
2347 :     chop $toPrint;
2348 :     chop $toPrint;
2349 :     print MACRO_SYTH $toPrint;
2350 :     close(MACRO_SYTH);
2351 :     close(ASSEM);
2352 :    
2353 :     #Print the metabolites for these subsystems.
2354 :     open(METABOLITES,">$dir/metabolites");
2355 :    
2356 :     foreach my $cid (keys %$cidToName)
2357 :     {
2358 :     my $name = $cidToName->{$cid};
2359 :     $name =~ s/\s/-/g;
2360 :     print METABOLITES $cid."\t".$name."\t0.001\t0\n";
2361 :     }
2362 :     close(METABOLITES);
2363 :    
2364 :     #Print the macromolucules file
2365 :     open(MACRO,">$dir/macromolecules");
2366 :     print MACRO "M1 \tM1 \t1 \t-100 -100 1 1\n";
2367 :     close(MACRO);
2368 :    
2369 :     $x_pos += 60;
2370 :     $y_pos = 20;
2371 :    
2372 :     print REACTIONS "mue\t\t\t|\t#\t0 100 0\t$x_pos $y_pos 1 1\t0.01\n";
2373 :    
2374 :     #close reaction equation file
2375 :     close(REACTIONS);
2376 :    
2377 :     # FluxAnalyzer requires this file
2378 :     open(APP, ">$dir/app_para.m");
2379 :     print APP "epsilon=1e-10;\nbasic_color=[0.7 0.7 0.7];\ncr_color=[0.5 0.5 1];\nbr_color=[1 0.2 0.2];\nnbr_color=[0.2 1 0.2];\ntext_color=[0 0 0];\nmacro_synth_color=[0 0 1];\nmacro_color=[0.6 0.6 1];\nbox_reaction_width=[0.12];\nbox_reaction_height=[0.06];\nbox_macro_width=[0.08];\nbox_macro_height=[0.06];\nfontsize_reaction=[11];\nfontsize_macro=[11];\nfluxmaps={'Fluxmap','dummy.pcx'};\n";
2380 :     close(APP);
2381 :     }
2382 :    
2383 :     sub write_final_fluxanalyzer_files
2384 :     {
2385 :     my ($dir, $path_inputs, $path_outputs, $all_reactions, $transport_reactions,
2386 :     $reactions_to_substrate_arrays,$reactions_to_product_arrays,
2387 :     $cidToName,$bioMass,$minSubstrates) = @_;
2388 :    
2389 :     #Write the inputs/outputs to a seperate file, along with $stoich and if its main
2390 :     open(A_INPUT, ">$dir/inputs_main");
2391 :    
2392 :     print A_INPUT map {"$_\t$all_compounds_to_main{$_}\n"} keys %$path_inputs;
2393 :    
2394 :     close(A_INPUT);
2395 :    
2396 :     open(A_OUTPUT, ">$dir/outputs_main");
2397 :    
2398 :     print A_OUTPUT map {"$_\t$all_compounds_to_main{$_}\n"} keys %$path_outputs;
2399 :    
2400 :     close(A_OUTPUT);
2401 :    
2402 :     my %open_transports;
2403 :    
2404 :     foreach my $cpd (keys %$minSubstrates)
2405 :     {
2406 :     map { $open_transports{$_} = 1 } @{$minSubstrates->{$cpd}};
2407 :     }
2408 :    
2409 :     open(REACTIONS, ">$dir/reactions");
2410 :     open(INPUTS, ">$dir/inputs");
2411 :     open(OUTPUTS, ">$dir/outputs");
2412 :    
2413 :     my @inputs = keys %$path_inputs;
2414 :    
2415 :     my $x_pos = 10;
2416 :     my $y_pos = 30;
2417 :    
2418 :     foreach my $cpd (@inputs)
2419 :     {
2420 :     my $toPrint = $cpd."up\t = 1 $cpd \t| \t";
2421 :    
2422 :     if (defined $minSubstrates->{$cpd})
2423 :     {
2424 :     $toPrint .= "#";
2425 :     }
2426 :     else
2427 :     {
2428 :     $toPrint .= "0";
2429 :     }
2430 :    
2431 :     $toPrint .= " \t0 Inf 0 \t$x_pos $y_pos 1 1\t0.01\n";
2432 :     print REACTIONS $toPrint;
2433 :     $y_pos += 30;
2434 :    
2435 :     if ($y_pos > 600)
2436 :     {
2437 :     $x_pos += 60;
2438 :     $y_pos = 30;
2439 :     }
2440 :     }
2441 :    
2442 :     $x_pos += 60;
2443 :     $y_pos = 30;
2444 :    
2445 :     foreach my $rxn (keys %$all_reactions)
2446 :     {
2447 :     my $direction = $all_reactions->{$rxn};
2448 :    
2449 :     my (@substrate_array, @product_array);
2450 :    
2451 :     if ($direction eq "L")
2452 :     {
2453 :     @product_array = @{$reactions_to_substrate_arrays->{$rxn."_L"}};
2454 :     @substrate_array = @{$reactions_to_product_arrays->{$rxn."_L"}};
2455 :     }
2456 :     else
2457 :     {
2458 :     @substrate_array = @{$reactions_to_substrate_arrays->{$rxn."_R"}};
2459 :     @product_array = @{$reactions_to_product_arrays->{$rxn."_R"}};
2460 :     }
2461 :    
2462 :     foreach my $subTuple (@substrate_array)
2463 :     {
2464 :     my @temp = $fig->names_of_compound($subTuple->[0]);
2465 :     $cidToName->{$subTuple->[0]} = $temp [0] if ! defined $cidToName->{$subTuple->[0]};
2466 :     }
2467 :    
2468 :     foreach my $prodTuple(@product_array)
2469 :     {
2470 :     my @temp = $fig->names_of_compound($prodTuple->[0]);
2471 :     $cidToName->{$prodTuple->[0]} = $temp[0] if ! defined $cidToName->{$prodTuple->[0]};
2472 :     }
2473 :    
2474 :     if($direction eq "R" || $direction eq "B")
2475 :     {
2476 :     #write data in a strign for copying to file later
2477 :     my $toFile = '';
2478 :     $toFile.= $rxn."\t";
2479 :    
2480 :     #add all the substrates
2481 :     foreach my $curSub(@substrate_array){
2482 :     $toFile .= $curSub -> [1].' '. $curSub -> [0].' + ';
2483 :     }
2484 :    
2485 :     ##chop off the +
2486 :     chop($toFile);
2487 :     chop($toFile);
2488 :    
2489 :     $toFile.='= ';
2490 :    
2491 :     #add all the products
2492 :     my $found_prod = 0;
2493 :     foreach my $curProd(@product_array){
2494 :     $toFile .= $curProd -> [1].' '. $curProd -> [0].' + ';
2495 :     $found_prod = 1;
2496 :     }
2497 :    
2498 :     if ($found_prod)
2499 :     {
2500 :     #chop off the plus
2501 :     chop($toFile);
2502 :     chop($toFile);
2503 :     }
2504 :    
2505 :     if (defined $transport_reactions->{$rxn})
2506 :     {
2507 :     if (defined $open_transports{$rxn})
2508 :     {
2509 :     $toFile.="\t|\t#\t";
2510 :     }
2511 :     else
2512 :     {
2513 :     $toFile.="\t|\t0\t";
2514 :     }
2515 :    
2516 :     if($direction eq "B"){
2517 :     $toFile.="-Inf";
2518 :     }
2519 :     else{
2520 :     $toFile.="0";
2521 :     }
2522 :    
2523 :     $toFile.=" Inf 0\t$x_pos $y_pos 1 1 \t0.01\n";
2524 :     $y_pos += 30;
2525 :     }
2526 :     elsif ($rxn =~ /sink/)
2527 :     {
2528 :     $toFile.="\t|\t0\t";
2529 :    
2530 :     if($direction eq "B"){
2531 :     $toFile.="-0.00001";
2532 :     }
2533 :     else{
2534 :     $toFile.="0";
2535 :     }
2536 :    
2537 :     $toFile.=" 0.00001 0\t$x_pos $y_pos 1 1 \t0.01\n";
2538 :     $y_pos += 30;
2539 :     }
2540 :     else
2541 :     {
2542 :     $toFile.="\t|\t#\t";
2543 :    
2544 :     if($direction eq "B"){
2545 :     $toFile.="-Inf";
2546 :     }
2547 :     else{
2548 :     $toFile.="0";
2549 :     }
2550 :    
2551 :     $toFile.=" Inf 0\t-10 -10 1 1 \t0.01\n";
2552 :     }
2553 :    
2554 :     print REACTIONS $toFile;
2555 :     }
2556 :     elsif($direction eq "L")
2557 :     {
2558 :     #write data in a strign for copying to file later
2559 :     my $toFile = '';
2560 :     $toFile.= $rxn."\t";
2561 :    
2562 :     #add all the substrates
2563 :     foreach my $curProd(@product_array){
2564 :     $toFile .= $curProd -> [1].' '. $curProd -> [0]." + ";
2565 :     }
2566 :    
2567 :     ##chop off the +
2568 :     chop($toFile);
2569 :     chop($toFile);
2570 :    
2571 :     $toFile.="= ";
2572 :    
2573 :     #add all the products
2574 :     foreach my $curSubstrate(@substrate_array){
2575 :     $toFile .= $curSubstrate -> [1].' '. $curSubstrate -> [0]." + ";
2576 :     }
2577 :    
2578 :     #chop off the plus
2579 :     chop($toFile);
2580 :     chop($toFile);
2581 :    
2582 :    
2583 :     $toFile.="\t|\t#\t";
2584 :     $toFile.="0";
2585 :     $toFile.=" Inf 0\t-10 -10 1 1 \t0.01\n";
2586 :    
2587 :     print REACTIONS $toFile;
2588 :     }
2589 :    
2590 :     if ($y_pos > 600)
2591 :     {
2592 :     $x_pos += 60;
2593 :     $y_pos = 30;
2594 :     }
2595 :     }
2596 :    
2597 :     my @outputs = keys %$path_outputs;
2598 :    
2599 :     $x_pos += 60;
2600 :     $y_pos = 30;
2601 :    
2602 :     foreach my $cpd (@inputs)
2603 :     {
2604 :     print INPUTS $cpd, "\t", $path_inputs->{$cpd}, "\t", $cidToName->{$cpd}, "\n";
2605 :     }
2606 :    
2607 :     foreach my $cpd (@outputs)
2608 :     {
2609 :     print OUTPUTS $cpd, "\t", $path_outputs->{$cpd}, "\t", $cidToName->{$cpd}, "\n";
2610 :     my $toPrint = $cpd."ex\t 1 $cpd = \t| \t# \t0 Inf 0 \t$x_pos $y_pos 1 1\t0.01\n";
2611 :     print REACTIONS $toPrint;
2612 :     $y_pos += 30;
2613 :    
2614 :     if ($y_pos > 600)
2615 :     {
2616 :     $x_pos += 60;
2617 :     $y_pos = 30;
2618 :     }
2619 :     }
2620 :    
2621 :     $x_pos += 60;
2622 :     $y_pos = 30;
2623 :    
2624 :     #print the macromolecule_synthesis and assembly file
2625 :     open(MACRO,">$dir/macromolecules");
2626 :     open(MACRO_SYTH,">$dir/macromolecule_synthesis");
2627 :     open(ASSEM,">$dir/assembly");
2628 :    
2629 :     my $toPrint = "M1 = ";
2630 :    
2631 :     foreach my $cpd (keys %$bioMass)
2632 :     {
2633 :     $toPrint .= "$bioMass->{$cpd} $cpd + ";
2634 :     print ASSEM "$cpd\tM1\t-100 -100 1\n";
2635 :     }
2636 :    
2637 :     chop $toPrint;
2638 :     chop $toPrint;
2639 :     chop $toPrint;
2640 :    
2641 :     print MACRO "M1 \tM1 \t1 \t-100 -100 1 1\n";
2642 :     print MACRO_SYTH $toPrint, "\n";
2643 :    
2644 :     close(MACRO);
2645 :     close(MACRO_SYTH);
2646 :     close(ASSEM);
2647 :    
2648 :     #Print the metabolites for these subsystems.
2649 :     open(METABOLITES,">$dir/metabolites");
2650 :    
2651 :     foreach my $cid (keys %$cidToName)
2652 :     {
2653 :     my $name = $cidToName->{$cid};
2654 :     $name =~ s/\s/-/g;
2655 :     print METABOLITES $cid."\t".$name."\t0.001\t0\n";
2656 :     }
2657 :     close(METABOLITES);
2658 :    
2659 :     $x_pos += 60;
2660 :     $y_pos = 30;
2661 :    
2662 :     print REACTIONS "mue\t\t\t|\t#\t0 100 0\t$x_pos $y_pos 1 1\t0.01\n";
2663 :    
2664 :     #close reaction equation file
2665 :     close(REACTIONS);
2666 :    
2667 :     # FluxAnalyzer requires this file
2668 :     open(APP, ">$dir/app_para.m");
2669 :     print APP "epsilon=1e-10;\nbasic_color=[0.7 0.7 0.7];\ncr_color=[0.5 0.5 1];\nbr_color=[1 0.2 0.2];\nnbr_color=[0.2 1 0.2];\ntext_color=[0 0 0];\nmacro_synth_color=[0 0 1];\nmacro_color=[0.6 0.6 1];\nbox_reaction_width=[0.06];\nbox_reaction_height=[0.03];\nbox_macro_width=[0.06];\nbox_macro_height=[0.03];\nfontsize_reaction=[11];\nfontsize_macro=[11];\nfluxmaps={'Fluxmap','dummy_medium.pcx'};\n";
2670 :     close(APP);
2671 :     }
2672 :    
2673 :    
2674 :     sub clear_arrays
2675 :     {
2676 :     undef %reactions_to_substrate_arrays;
2677 :     undef %reactions_to_product_arrays;
2678 :     undef %all_compounds_to_main;
2679 :     undef %all_reactions;
2680 :     undef %scenario_cycles;
2681 :     undef @all_outputs_lists;
2682 :     undef %all_inputs;
2683 :     undef %all_outputs;
2684 :    
2685 :     %reactions_to_substrate_arrays = ();
2686 :     %reactions_to_product_arrays = ();
2687 :     %all_compounds_to_main = ();
2688 :     %all_reactions = ();
2689 :     %scenario_cycles = ();
2690 :     @all_outputs_lists = ();
2691 :     %all_inputs = ();
2692 :     %all_outputs = ();
2693 :     }
2694 :    
2695 :     sub load_superset_file
2696 :     {
2697 :     %superset_to_ss = ();
2698 :     %ss_to_superset = ();
2699 :    
2700 : dejongh 1.20 open(FILE,"<$FIG_Config::global/Models/hope_supersets.txt") or die("Failed to open hope_supersets.txt");
2701 : olson 1.1 while(<FILE>)
2702 :     {
2703 :     my @line = split(/\t/,$_);
2704 :     map { s/ /_/g } @line;
2705 :     map { s/\"//g } @line;
2706 :     map { chomp } @line;
2707 :     $superset_to_ss{$line[0]} = [] if !defined $superset_to_ss{$line[0]};
2708 :     $ss_to_superset{$line[1]} = $line[0];
2709 :     push(@{$superset_to_ss{$line[0]}},$line[1]);
2710 :     }
2711 :     close(FILE);
2712 :    
2713 : dejongh 1.24 return \%ss_to_superset
2714 : olson 1.1 }
2715 :    
2716 :     # This function runs a given scenario that is defined the specified subsystem
2717 :     # It returns the data as it is found by process_paths in a hash reference
2718 :     # This is used internally by model.pm, and shouldn't be called externally
2719 :    
2720 :     sub load_scenario
2721 :     {
2722 : dejongh 1.19 my ($genome,$ssa,$scenario) = @_;
2723 : olson 1.1
2724 :     #load up the arrays with the info we need
2725 : dejongh 1.19 process_init($ssa,$scenario,$genome,0);
2726 : olson 1.1
2727 :     #assume all path inputs and outputs are main
2728 :     map { $all_compounds_to_main{$_} = 1 } keys %all_inputs;
2729 :     map { $all_compounds_to_main{$_} = 1 } keys %all_outputs;
2730 :    
2731 :     }
2732 :    
2733 :     sub internal_scenario
2734 :     {
2735 : dejongh 1.19 my ($genome,$ssa,$scenario,$find_first) = @_;
2736 : olson 1.1
2737 : olson 1.2 print STDERR "\nIn internal_scenario with '$genome', '$ssa', '$scenario', '$find_first'\n" if $debug;
2738 : olson 1.1
2739 : dejongh 1.19 load_scenario($genome,$ssa,$scenario);
2740 : olson 1.1
2741 :     return execute_paths([],$find_first,[],[]);
2742 :     }
2743 :    
2744 :     sub run_scenario
2745 :     {
2746 : dejongh 1.19 my($genome,$superset,$subsystem,$scenario,$find_first) = @_;
2747 : dejongh 1.23 my $scenario_dir = get_scenario_directory($genome) . "/PathInfo/$superset/$subsystem/$scenario";
2748 : olson 1.2
2749 :     system("rm", "-rf", $scenario_dir);
2750 : dejongh 1.18 &FIG::verify_dir($scenario_dir);
2751 : olson 1.2
2752 : olson 1.1 #make sure the arrays are empty to start out
2753 :     &clear_arrays;
2754 : dejongh 1.19 write_scenario(internal_scenario($genome,$subsystem,$scenario,$find_first),$scenario_dir);
2755 : olson 1.1 }
2756 :    
2757 : olson 1.2 sub compare_scenario
2758 :     {
2759 : dejongh 1.17 my($genome,$superset,$ss_name,$scenario_name,$dont_copy) = @_;
2760 : olson 1.2 my @genome_paths;
2761 : dejongh 1.23 my $scenario_dir_all = get_scenario_directory('All') . "/PathInfo/$superset/$ss_name/$scenario_name";
2762 : olson 1.2 my $subsystem = $fig->get_subsystem($ss_name);
2763 : dejongh 1.17 my @additional_reactions = $subsystem->get_hope_additional_reactions($scenario_name);
2764 : olson 1.2 my %additional_reactions;
2765 :     map { $additional_reactions{$_} = 1 } @additional_reactions;
2766 :    
2767 : dejongh 1.4 my %ss_reactions;
2768 :    
2769 :     if ($genome eq "All")
2770 : olson 1.2 {
2771 : dejongh 1.17 my %all_reactions = $subsystem->get_hope_reactions;
2772 : dejongh 1.4 foreach my $role (keys %all_reactions)
2773 : olson 1.2 {
2774 : dejongh 1.4 map { $ss_reactions{$_} = 1 } @{$all_reactions{$role}};
2775 : olson 1.2 }
2776 :     }
2777 : dejongh 1.4 else
2778 : olson 1.2 {
2779 : dejongh 1.24 my %reactions_for_genome = get_reactions_for_genome_in_subsystem($genome,$ss_name);
2780 : dejongh 1.17 map { $ss_reactions{$_} = 1 } keys %reactions_for_genome if defined %reactions_for_genome;
2781 : olson 1.2 }
2782 :    
2783 :     # first find paths in the All directory that should be valid for the genome
2784 :     # based on the reactions associated with it in the subsystems
2785 : dejongh 1.17 opendir (DIR_ALL,$scenario_dir_all) or return [];
2786 : dejongh 1.18 my @sub_dirs = readdir DIR_ALL;
2787 : olson 1.2 close DIR_ALL;
2788 :    
2789 :     my %paths_all;
2790 :    
2791 :     for my $path (@sub_dirs)
2792 :     {
2793 : dejongh 1.18 next if $path !~ /path/; # skip . and ..
2794 : olson 1.2 my $match = 1;
2795 :     open (PATH, "$scenario_dir_all/$path/path_info");
2796 :     my @reactions = <PATH>;
2797 :     close PATH;
2798 :    
2799 :     my $reaction_string = "";
2800 :    
2801 :     foreach my $reaction (sort @reactions)
2802 :     {
2803 :     if ($reaction =~ /(R\d\d\d\d\d)/)
2804 :     {
2805 :     $reaction_string .= $reaction;
2806 :    
2807 :     if (! exists($ss_reactions{$1}) && ! exists($additional_reactions{$1}))
2808 :     {
2809 :     $match = 0;
2810 :     }
2811 :     }
2812 :     }
2813 :    
2814 :     $paths_all{$reaction_string} = $match;
2815 :     push @genome_paths, $path if $match;
2816 :     }
2817 :    
2818 :     # now check all paths found for this particular organism, and make sure they
2819 :     # are in the appropriate All subdirectory
2820 : dejongh 1.23 my $scenario_dir_genome = get_scenario_directory($genome). "/PathInfo/$superset/$ss_name/$scenario_name";
2821 : olson 1.2
2822 : dejongh 1.17 opendir (DIR_GENOME,$scenario_dir_genome) or return \@genome_paths;
2823 : dejongh 1.18 @sub_dirs = readdir DIR_GENOME;
2824 : olson 1.2 close DIR_GENOME;
2825 :    
2826 :     my $path_counter = scalar keys %paths_all;
2827 :    
2828 :     for my $path (@sub_dirs)
2829 :     {
2830 : dejongh 1.18 next if $path !~ /path/; # skip . and ..
2831 : olson 1.2 my $match = 1;
2832 :     open (PATH, "$scenario_dir_genome/$path/path_info");
2833 :     my @reactions = <PATH>;
2834 :     close PATH;
2835 :    
2836 :     my $reaction_string = "";
2837 :    
2838 :     foreach my $reaction (sort @reactions)
2839 :     {
2840 :     if ($reaction =~ /(R\d\d\d\d\d)/)
2841 :     {
2842 :     $reaction_string .= $reaction;
2843 :     }
2844 :     }
2845 :    
2846 :     if (! exists($paths_all{$reaction_string}))
2847 :     {
2848 : dejongh 1.17 if ($dont_copy)
2849 :     {
2850 :     print STDERR "$scenario_dir_genome/$path not found in All\n";
2851 :     }
2852 :     else
2853 :     {
2854 :     $path_counter++;
2855 :     my $new_path_name = "path_".$path_counter;
2856 :     my $temp_sdg = $scenario_dir_genome;
2857 :     $temp_sdg =~ s/\(/\\\(/g;
2858 :     $temp_sdg =~ s/\)/\\\)/g;
2859 :     my $temp_sda = $scenario_dir_all;
2860 :     $temp_sda =~ s/\(/\\\(/g;
2861 :     $temp_sda =~ s/\)/\\\)/g;
2862 :     `cp -R $temp_sdg/$path $temp_sda/$new_path_name`;
2863 :     push @genome_paths, $new_path_name;
2864 :     print STDERR "Copied $temp_sdg/$path to $temp_sda/$new_path_name\n";
2865 :     }
2866 :     }
2867 :    
2868 :     unless ($dont_copy)
2869 :     {
2870 :     # remove genome-specific paths
2871 :     rmtree("$scenario_dir_genome/$path");
2872 :     }
2873 :     }
2874 :    
2875 :     unless ($dont_copy)
2876 :     {
2877 :     # create soft links for each genome-specific path, with the same name
2878 :     # as the path in the "All" directory
2879 :     foreach my $path (@genome_paths)
2880 :     {
2881 : olson 1.2 my $temp_sdg = $scenario_dir_genome;
2882 :     $temp_sdg =~ s/\(/\\\(/g;
2883 :     $temp_sdg =~ s/\)/\\\)/g;
2884 :     my $temp_sda = $scenario_dir_all;
2885 :     $temp_sda =~ s/\(/\\\(/g;
2886 :     $temp_sda =~ s/\)/\\\)/g;
2887 : dejongh 1.17 `ln -s $temp_sda/$path $temp_sdg`;
2888 :     }
2889 :     }
2890 : olson 1.2
2891 :     return \@genome_paths;
2892 :     }
2893 :    
2894 : olson 1.1 sub write_scenario
2895 :     {
2896 :     my($scenario_data,$scenario_dir) = @_;
2897 :     delete $scenario_data->{"infinite"};
2898 :     my $path_count = 1;
2899 :     my @list_of_done_tokens=();
2900 :    
2901 :     print STDERR "Paths: ", keys %{$scenario_data}, "\n" if $debug;
2902 :    
2903 :     foreach my $try_path (keys %{$scenario_data})
2904 :     {
2905 :     print STDERR "\t Checking $try_path\n" if $debug;
2906 :     if($scenario_data->{$try_path}->[1] != 1)
2907 :     {
2908 :     print STDERR "\t Token $try_path is not complete\n" if $debug;
2909 :     next;
2910 :     }
2911 :     print STDERR "These are the contents of the list: " , @list_of_done_tokens, "\n" if $debug;
2912 :     if(scalar @list_of_done_tokens == 0){
2913 :     push @list_of_done_tokens, $try_path;
2914 :     next;
2915 :     }
2916 :    
2917 :     # Check this token's ($try_path) values against the values of the keys($elem) stored in
2918 :     # list_of_done_tokens. If they match, don't add it to the finished token list, if it doesn't
2919 :     # match, add it.
2920 :    
2921 :     print STDERR "These are the contents of the list: " , @list_of_done_tokens, "\n" if $debug;
2922 :    
2923 :     my $found_match = 0;
2924 :    
2925 :     foreach my $elem(@list_of_done_tokens){
2926 :     my @done_reactions = @{$scenario_data->{$elem}->[2]};
2927 :    
2928 :     print STDERR "This is the path we're trying: " , $try_path, "\t","This is the path already in the array: " , $elem , "\nThis is the size of the array: " . @list_of_done_tokens ."\n" if $debug;
2929 :    
2930 :     # if the list of reactions match, they represent the same path
2931 :    
2932 :     my @path_reactions = @{$scenario_data->{$try_path}->[2]};
2933 :     my (%diff_reactions_1, %diff_reactions_2);
2934 :    
2935 :     map {$diff_reactions_1{$_} = 1} @path_reactions;
2936 :     map {delete $diff_reactions_1{$_}} @done_reactions;
2937 :     map {$diff_reactions_2{$_} = 1} @done_reactions;
2938 :     map {delete $diff_reactions_2{$_}} @path_reactions;
2939 :    
2940 :     if (scalar keys %diff_reactions_1 == 0 && scalar keys %diff_reactions_2 == 0)
2941 :     {
2942 :     print STDERR "They match.\n" if $debug;
2943 :     $found_match = 1;
2944 :     last;
2945 :     }
2946 :     }
2947 :    
2948 :     if (! $found_match)
2949 :     {
2950 :     push @list_of_done_tokens, $try_path;
2951 :     print STDERR $try_path, " Added to the array\n" if $debug;
2952 :    
2953 :     }
2954 :     }
2955 :    
2956 :     foreach my $path (@list_of_done_tokens){
2957 :     if($scenario_data->{$path}->[1] != 1)
2958 :     {
2959 :     next;
2960 :     }
2961 :     if(@list_of_done_tokens == 0){
2962 :     push @list_of_done_tokens, $path;
2963 :     }
2964 :    
2965 :     #create input/output info
2966 :     my $input_hash = $scenario_data->{$path}->[7];
2967 :     my $output_hash = $scenario_data->{$path}->[8];
2968 :     my $reaction_path = $scenario_data->{$path}->[2];
2969 :     my @reaction_array = @$reaction_path;
2970 :    
2971 :     print STDERR "\nInputs:\n" if $debug;
2972 :     print STDERR map{"$_ => $input_hash->{$_}" } keys %$input_hash, "\n" if $debug;
2973 :     print STDERR "\nOutputs:\n" if $debug;
2974 :     print STDERR map{"$_ => $output_hash->{$_}"} keys %$output_hash, "\n" if $debug;
2975 :    
2976 :     # divide stoichiometry by greatest common denominator
2977 :     my ($min_stoich, @all_stoichs);
2978 :    
2979 :     map { push @all_stoichs, $input_hash->{$_}; $min_stoich = $input_hash->{$_} if $input_hash->{$_} < $min_stoich || $min_stoich == 0 } keys %{$input_hash};
2980 :     map { push @all_stoichs, $output_hash->{$_}; $min_stoich = $output_hash->{$_} if $output_hash->{$_} < $min_stoich || $min_stoich == 0 } keys %{$output_hash};
2981 :    
2982 :     my ($gcd, @gcd_candidates);
2983 :    
2984 :     outer: for ($gcd = $min_stoich; $gcd > 1; $gcd--)
2985 :     {
2986 :     foreach my $stoich (@all_stoichs)
2987 :     {
2988 :     next outer if $stoich % $gcd != 0;
2989 :     }
2990 :    
2991 :     last; # found a gcd
2992 :     }
2993 :    
2994 :     map { $input_hash->{$_} /= $gcd } keys %{$input_hash};
2995 :     map { $output_hash->{$_} /= $gcd } keys %{$output_hash};
2996 :    
2997 :     mkdir "$scenario_dir/path_$path_count";
2998 :    
2999 :     open(FILE, ">$scenario_dir/path_$path_count/path_info");
3000 :     foreach my $elem(@reaction_array){
3001 :     print FILE scalar @reaction_array, "\t", $elem, "\n";
3002 :     }
3003 :     close(FILE);
3004 :    
3005 :     &write_fluxanalyzer_files("$scenario_dir/path_$path_count",$input_hash,
3006 :     $output_hash, \@reaction_array,\%all_reactions,
3007 :     \%reactions_to_substrate_arrays,
3008 :     \%reactions_to_product_arrays,
3009 :     {});
3010 :     $path_count++;
3011 :     }
3012 :     @list_of_done_tokens=();
3013 :     undef %{$scenario_data};
3014 :    
3015 :     }
3016 :    
3017 :    
3018 :     sub load_subsystem
3019 :     {
3020 :     my ($genome,$ss_name) = @_;
3021 :     my $subsystem = $fig->get_subsystem($ss_name);
3022 :     my @ss_scenarios = $subsystem->get_hope_scenario_names;
3023 :     foreach my $name (@ss_scenarios)
3024 :     {
3025 :     load_scenario($genome,$ss_name,$name);
3026 :     }
3027 :     }
3028 :    
3029 :     sub internal_subsystem
3030 :     {
3031 :     my ($genome,$ss_name,$find_first) = @_;
3032 :     my %scenario_to_paths;
3033 :    
3034 :     my $subsystem = $fig->get_subsystem($ss_name);
3035 :     my @ss_scenarios = $subsystem->get_hope_scenario_names;
3036 :    
3037 :     foreach my $name (@ss_scenarios)
3038 :     {
3039 :     $scenario_to_paths{$name} = internal_scenario($genome,$ss_name,$name,$find_first);
3040 :     }
3041 :    
3042 :     return \%scenario_to_paths;
3043 :     }
3044 :    
3045 :     sub run_subsystem
3046 :     {
3047 :     my ($genome,$superset,$subsystem,$find_first) = @_;
3048 :    
3049 :     my $subsystem_obj = $fig->get_subsystem($subsystem);
3050 : olson 1.10
3051 :     if (!$subsystem_obj)
3052 :     {
3053 :     warn "Cannot open subsystem $subsystem\n";
3054 :     return;
3055 :     }
3056 :    
3057 : olson 1.1 my @ss_scenarios = $subsystem_obj->get_hope_scenario_names;
3058 : olson 1.10
3059 : dejongh 1.23 my $dir = get_scenario_directory($genome) . "/PathInfo/$superset/$subsystem";
3060 : olson 1.10 system("rm", "-rf", $dir);
3061 :     &FIG::verify_dir($dir);
3062 : olson 1.1
3063 :     foreach my $name (@ss_scenarios)
3064 :     {
3065 :     run_scenario($genome,$superset,$subsystem,$name,$find_first);
3066 :     }
3067 :     }
3068 :    
3069 : olson 1.2 sub compare_subsystem
3070 :     {
3071 : dejongh 1.17 my ($genome,$superset,$subsystem,$dont_copy) = @_;
3072 : olson 1.2 my %genome_scenarios;
3073 :    
3074 :     my $subsystem_obj = $fig->get_subsystem($subsystem);
3075 : olson 1.11
3076 :     if (!$subsystem_obj)
3077 :     {
3078 :     warn "Cannot open subsystem $subsystem\n";
3079 :     return;
3080 :     }
3081 :    
3082 : olson 1.2 my @ss_scenarios = $subsystem_obj->get_hope_scenario_names;
3083 :    
3084 :     foreach my $name (@ss_scenarios)
3085 :     {
3086 : dejongh 1.17 $genome_scenarios{$name} = compare_scenario($genome,$superset,$subsystem,$name,$dont_copy);
3087 : olson 1.2 }
3088 :    
3089 :     return \%genome_scenarios;
3090 :     }
3091 :    
3092 : olson 1.1 sub load_superset
3093 :     {
3094 :     my($genome, $superset_name) = @_;
3095 :    
3096 :     my @subsystems = @{$superset_to_ss{$superset_name}};
3097 :     foreach my $ss_name (@subsystems)
3098 :     {
3099 :     load_subsystem($genome,$ss_name);
3100 :     }
3101 :     }
3102 :    
3103 :     sub internal_superset
3104 :     {
3105 :     my($genome, $superset_name,$find_first) = @_;
3106 :    
3107 :     my @subsystems = @{$superset_to_ss{$superset_name}};
3108 :    
3109 :     my %supersets_data;
3110 :    
3111 :     foreach my $ss_name (@subsystems)
3112 :     {
3113 :     $supersets_data{$ss_name} = internal_subsystem($genome,$ss_name,$find_first);
3114 :     }
3115 :    
3116 :     return \%supersets_data;
3117 :     }
3118 :    
3119 :     sub run_superset
3120 :     {
3121 :     my($genome, $superset_name,$find_first) = @_;
3122 :    
3123 :     my @subsystems = @{$superset_to_ss{$superset_name}};
3124 :    
3125 : olson 1.10
3126 : dejongh 1.23 my $dir = get_scenario_directory($genome) . "/PathInfo/$superset_name";
3127 : olson 1.10 system("rm", "-rf", $dir);
3128 :     &FIG::verify_dir($dir);
3129 : olson 1.1
3130 :     foreach my $ss_name (@subsystems)
3131 :     {
3132 :     print STDERR "Running Scenarios for subsystem $ss_name\n";
3133 :     run_subsystem($genome,$superset_name,$ss_name,$find_first);
3134 :     }
3135 :     }
3136 :    
3137 : olson 1.2 sub compare_superset
3138 :     {
3139 : dejongh 1.17 my($genome, $superset_name, $dont_copy) = @_;
3140 : olson 1.2
3141 :     my @subsystems = @{$superset_to_ss{$superset_name}};
3142 :     my %genome_subsystems;
3143 :    
3144 :     foreach my $ss_name (@subsystems)
3145 :     {
3146 :     print STDERR "Comparing Scenarios for $genome in subsystem $ss_name\n";
3147 : dejongh 1.17 $genome_subsystems{$ss_name} = compare_subsystem($genome,$superset_name,$ss_name,$dont_copy);
3148 : olson 1.2 }
3149 :    
3150 :     return \%genome_subsystems;
3151 :     }
3152 :    
3153 : olson 1.1
3154 :     sub load_supersets
3155 :     {
3156 :     my($genome) = @_;
3157 : dejongh 1.20 &load_superset_file;
3158 :    
3159 : olson 1.1 foreach my $superset (keys %superset_to_ss)
3160 :     {
3161 :     load_superset($genome,$superset);
3162 :     }
3163 :    
3164 :     return (\%all_reactions,\%reactions_to_substrate_arrays,\%reactions_to_product_arrays);
3165 :    
3166 :     }
3167 :    
3168 :     sub run_supersets
3169 :     {
3170 :     my($genome,$find_first) = @_;
3171 : dejongh 1.20 &load_superset_file;
3172 :    
3173 : dejongh 1.23 my $dir = get_scenario_directory($genome) . "/PathInfo";
3174 : dejongh 1.20 system("rm", "-rf", $dir);
3175 :     &FIG::verify_dir($dir);
3176 :    
3177 : olson 1.1 foreach my $superset (keys %superset_to_ss)
3178 :     {
3179 :     run_superset($genome,$superset,$find_first);
3180 :     }
3181 :    
3182 :     }
3183 :    
3184 : olson 1.2 sub compare_supersets
3185 :     {
3186 : dejongh 1.17 my($genome, $dont_copy) = @_;
3187 : olson 1.2 my %genome_supersets;
3188 :    
3189 : dejongh 1.20 &load_superset_file;
3190 :    
3191 : olson 1.2 foreach my $superset (keys %superset_to_ss)
3192 :     {
3193 : dejongh 1.17 $genome_supersets{$superset} = compare_superset($genome,$superset,$dont_copy);
3194 : olson 1.2 }
3195 :    
3196 :     return \%genome_supersets;
3197 :     }
3198 :    
3199 : olson 1.1 sub run_genome_report
3200 :     {
3201 :     my ($genome) = @_;
3202 :     my @string_out;
3203 :     push @string_out,"Genome $genome\n";
3204 :     #get all the subsystems this genome is involved in
3205 :     foreach my $superset (keys %superset_to_ss)
3206 :     {
3207 :     foreach my $name (@{$superset_to_ss{$superset}})
3208 :     {
3209 :     push @string_out, @{print_ss_report($name,internal_subsystem($genome,$name,0))};
3210 :     }
3211 :     }
3212 :     return \@string_out;
3213 :     }
3214 :    
3215 :     sub print_ss_report
3216 :     {
3217 :     my ($ss_name,$scenario_to_paths) = @_;
3218 :     my @output = ();
3219 :     my %scenario_path_count;
3220 :    
3221 :     push(@output,"\tSubsystem $ss_name\n");
3222 :    
3223 :     foreach my $scenario (keys %$scenario_to_paths)
3224 :     {
3225 :     if($scenario_to_paths->{$scenario}->{"infinite"})
3226 :     {
3227 :     push(@output,"\tWarning: Possible Infinite loop\n");
3228 :     }
3229 :     delete $scenario_to_paths->{$scenario}->{"infinite"};
3230 :     foreach my $token (keys %{$scenario_to_paths->{$scenario}})
3231 :     {
3232 :     $scenario_path_count{$scenario}++ if ($scenario_to_paths->{$scenario}->{$token}->[1]);
3233 :     }
3234 :     }
3235 :    
3236 :     push @output , map { "\t\t$_ has $scenario_path_count{$_} path(s).\n" } keys %scenario_path_count;
3237 :    
3238 :     return \@output;
3239 :     }
3240 :    
3241 :    
3242 :     sub internal_assembly
3243 :     {
3244 :     #lets get the genome, and a array reference to the paths we want to build togather
3245 :     my ($paths,$input_path,$output_path,$one_path) = @_;
3246 :    
3247 :    
3248 : olson 1.2 print STDERR $paths."\n" if $debug;
3249 : olson 1.1 clear_arrays();
3250 :    
3251 :     #This gets us an array of arrays, each subarray holds
3252 : dejongh 1.12 # [0] = genome [1] = Scenarios [2] = superset [3] = subsystem [4] = scenario [5] = path
3253 : olson 1.1 # OR [0] = genome [1] = assembly [2] = path_name
3254 :     my @assembly_scenarios = @{parse_assembly_scenarios($paths)};
3255 :    
3256 :     #split and the input/output path for later as well
3257 :     #these should only return one path array...so just grab that one
3258 :     my @input_arr;
3259 :     my @output_arr;
3260 :    
3261 :     if($input_path != undef && $output_path != undef )
3262 :     {
3263 :     @input_arr = @{parse_assembly_scenarios($input_path)};
3264 :     @output_arr = @{parse_assembly_scenarios($output_path)};
3265 :     }
3266 :    
3267 :     #load all the kegg information for each 'scenario' from the paths we have selected
3268 :     foreach my $scenario (@assembly_scenarios)
3269 :     {
3270 : dejongh 1.12 if(scalar @$scenario > 5) #this is a normal scenario path
3271 : olson 1.1 {
3272 : dejongh 1.12 print STDERR "Checking $scenario->[3] $scenario->[4] $scenario->[5] \n" if $debug;
3273 :     process_init($scenario->[3],$scenario->[4],$scenario->[0],1);
3274 : olson 1.1 }
3275 :     else #This is a assembly path
3276 :     {
3277 :     #read in the input/output compounds and mark main's correctly
3278 : dejongh 1.20 my $genome = shift @$scenario;
3279 : dejongh 1.23 my $path = get_scenario_directory($genome) . "/" . join "/" , @$scenario;
3280 : olson 1.1
3281 :     open(M_IN,"$path/inputs_main");
3282 :     while(<M_IN>)
3283 :     {
3284 :     my @line = split(/\t/,$_);
3285 :     $all_compounds_to_main{$line[0]} = $line[1];
3286 :     }
3287 :     close(M_IN);
3288 :    
3289 :     open(M_OUT,"$path/outputs_main");
3290 :     while(<M_OUT>)
3291 :     {
3292 :     my @line = split(/\t/,$_);
3293 :     $all_compounds_to_main{$line[0]} = $line[1];
3294 :     }
3295 :     close(M_OUT);
3296 :     }
3297 :     }
3298 :     #assume all path inputs and outputs are main
3299 :     map { $all_compounds_to_main{$_} = 1 } keys %all_inputs;
3300 :     map { $all_compounds_to_main{$_} = 1 } keys %all_outputs;
3301 :    
3302 :     print STDERR "Inputs: " if $debug;
3303 : olson 1.2 print STDERR map { $_."\n" } keys %all_inputs if $debug;
3304 : olson 1.1 print STDERR "Outputs: " if $debug;
3305 : olson 1.2 print STDERR map { $_."\n" } keys %all_outputs if $debug;
3306 : olson 1.1
3307 :     #run process paths
3308 :     return execute_paths(\@assembly_scenarios,$one_path,\@input_arr,\@output_arr);
3309 :     }
3310 :    
3311 :     sub run_assembly
3312 :     {
3313 :     my ($paths,$genome,$write_name,$one_path) = @_;
3314 :     print STDERR "\nThis is the passed information.\n";
3315 :     print STDERR $paths , "\n" , @$paths, "\n";
3316 :     print STDERR $genome . "\n";
3317 :     print STDERR $write_name . "\n";
3318 : dejongh 1.23 my $dir = get_scenario_directory($genome) . "/Assemblies/$write_name";
3319 : olson 1.10 system("rm", "-rf", $dir);
3320 :     &FIG::verify_dir($dir);
3321 :     write_scenario(internal_assembly($paths,[],[],$one_path),$dir);
3322 : olson 1.1 }
3323 :    
3324 :     sub expand_paths
3325 :     {
3326 :     my ($paths) = @_;
3327 :    
3328 :     my @final_paths;
3329 :    
3330 :     foreach my $path (@$paths)
3331 :     {
3332 :     if($path eq "" || $path eq "//")
3333 :     {
3334 :     next;
3335 :     }
3336 : dejongh 1.12 my $length = 6;
3337 :     if($path =~ /Assemblies/)
3338 : olson 1.1 {
3339 :     $length = 4;
3340 :     }
3341 :     print STDERR "Expanding $path \n" if $debug;
3342 :     my @parts = split "/", $path;
3343 :     shift @parts; # get ride of the first blank entry from /$genome
3344 :     my $length =$length - scalar @parts;
3345 : dejongh 1.23 my $genome = shift @parts;
3346 : olson 1.1 $path = join "/" , @parts;
3347 : dejongh 1.23 print STDERR "Length : $length Path: $path\n" if $debug;
3348 :     my @temp = @{expand_recursive($genome, $path, $length)};
3349 : olson 1.1 push @final_paths , @temp if scalar @temp > 0;
3350 :     }
3351 :    
3352 :    
3353 :     return \@final_paths;
3354 :     }
3355 :    
3356 :     sub expand_recursive
3357 :     {
3358 : dejongh 1.23 my ($genome,$path,$count) = @_;
3359 : olson 1.1 my @sub_dirs;
3360 : dejongh 1.23 if($path =~ m\Assemblies$\ || $path =~ m\Analysis$\ || $path =~ m\Curation$\)
3361 : olson 1.1 {
3362 :     return [];
3363 :     }
3364 :     if($count !=0)
3365 :     {
3366 : dejongh 1.20 #read this path, and pull out all the sub-directories.
3367 : dejongh 1.23 my $model_dir = get_scenario_directory($genome) . "/$path/";
3368 : olson 1.10
3369 :     print STDERR "reading directory $model_dir\n" if $debug;
3370 : formsma 1.16 opendir (DIR, $model_dir) or die("$model_dir");
3371 : olson 1.10 print STDERR "reading directory $model_dir\n" if $debug;
3372 : dejongh 1.18 @sub_dirs = readdir DIR;
3373 : olson 1.1 close DIR;
3374 :     print STDERR "Found: " if $debug;
3375 :     print STDERR @sub_dirs , "," if $debug;
3376 :     }
3377 :     else
3378 :     {
3379 :    
3380 :     return [$path];
3381 :     }
3382 :     $count--;
3383 :     my @to_return;
3384 :     foreach my $sub_path (@sub_dirs)
3385 :     {
3386 : dejongh 1.18 next if $sub_path =~ /^\.$/ || $sub_path =~ /^\.\.$/;
3387 : olson 1.1 print STDERR "Calling on $sub_path , $count \n" if $debug;
3388 : dejongh 1.23 push @to_return , @{expand_recursive($genome, "$path/$sub_path",$count)};
3389 : olson 1.1 }
3390 :     print STDERR "Returning" ,@to_return if $debug;
3391 :     return \@to_return;
3392 :    
3393 :     }
3394 :    
3395 :     sub parse_assembly_scenarios
3396 :     {
3397 :     my ($paths) = @_;
3398 :    
3399 :     $paths = expand_paths($paths);
3400 :     my @array_of_path_arrays;
3401 :    
3402 :     foreach my $path (@$paths)
3403 :     {
3404 :     my @parts = split "/", $path;
3405 :     #shift @parts;
3406 :     push(@array_of_path_arrays, \@parts);
3407 :     }
3408 :    
3409 :     return \@array_of_path_arrays;
3410 :     }
3411 :    
3412 :    
3413 :     sub write_selected_scenarios
3414 :     {
3415 :     my($checked,$genome,$ssa,$sc_name) = @_;
3416 :     my (@tempArray);
3417 :    
3418 :     #Load this scenario again with all of its rxns and cpds
3419 :    
3420 :     model::clear_arrays();
3421 :    
3422 :     model::process_init($ssa,$sc_name,$genome,0);
3423 :    
3424 :     #assume all path inputs and outputs are main
3425 :     map { $all_compounds_to_main{$_} = 1 } keys %all_inputs;
3426 :     map { $all_compounds_to_main{$_} = 1 } keys %all_outputs;
3427 :    
3428 :     model::create_reactions({},{},{},{});
3429 :    
3430 :     ##End of scenario loading
3431 :    
3432 :    
3433 :     #setup the filesystem to store the scenario/paths
3434 :     my $superset = $ss_to_superset{$ssa};
3435 : dejongh 1.23 my $base_dir = get_scenario_directory($genome) . "/PathInfo/$superset/$ssa/$sc_name/";
3436 : olson 1.2 system("rm", "-rf", $base_dir);
3437 : olson 1.10 &FIG::verify_dir($base_dir);
3438 : olson 1.1
3439 :     #for the selected paths, lets gather their cpd from the checkbox and write
3440 :     #the path that we need
3441 :     foreach my $path (@$checked)
3442 :     {
3443 :     my (%input_hash, %output_hash, $path_name);
3444 :     #process the strings to get the information from the parameters
3445 :     my @items = split(";", $path);
3446 :     $path_name = $items[0];
3447 :     #next we have the input compounds ids/stoich/main
3448 :     map { if ($_ =~ /(.*):(.*):(.*)/)
3449 :     { $input_hash{$1}+= $2 } } split ",", $items[1];
3450 :     #the third part has the output compounds ids/stoich/main
3451 :     map { if ($_ =~ /(.*):(.*):(.*)/)
3452 :     { $output_hash{$1} += $2 } } split ",",$items[2];
3453 :     #the fourth part has the strings of reactions visited
3454 :     @tempArray = split("#" , $items[3]);
3455 :    
3456 :    
3457 :     print STDERR "\nInputs:\n" if $debug;
3458 :     print STDERR map{"$_ => $input_hash{$_}" } keys %input_hash, "\n" if $debug;
3459 :     print STDERR "\nOutputs:\n" if $debug;
3460 :     print STDERR map{"$_ => $output_hash{$_}"} keys %output_hash, "\n" if $debug;
3461 :    
3462 :    
3463 :     # divide stoichiometry by greatest common denominator
3464 :     my ($min_stoich, @all_stoichs);
3465 :    
3466 :     map { push @all_stoichs, $input_hash{$_}; $min_stoich = $input_hash{$_} if $input_hash{$_} < $min_stoich || $min_stoich == 0 } keys %input_hash;
3467 :     map { push @all_stoichs, $output_hash{$_}; $min_stoich = $output_hash{$_} if $output_hash{$_} < $min_stoich || $min_stoich == 0 } keys %output_hash;
3468 :    
3469 :     my ($gcd, @gcd_candidates);
3470 :    
3471 :     outer: for ($gcd = $min_stoich; $gcd > 1; $gcd--)
3472 :     {
3473 :     foreach my $stoich (@all_stoichs)
3474 :     {
3475 :     next outer if $stoich % $gcd != 0;
3476 :     }
3477 :    
3478 :     last; # found a gcd
3479 :     }
3480 :    
3481 :     map { $input_hash{$_} /= $gcd } keys %input_hash;
3482 :     map { $output_hash{$_} /= $gcd } keys %output_hash;
3483 :    
3484 :     mkdir "$base_dir/$path_name";
3485 :     &write_fluxanalyzer_files("$base_dir/$path_name",\%input_hash,
3486 :     \%output_hash,\@tempArray,\%all_reactions,
3487 :     \%reactions_to_substrate_arrays,
3488 :     \%reactions_to_product_arrays,
3489 :     {});
3490 :     }
3491 :     return $base_dir;
3492 :     }
3493 :    
3494 :     #This write function assumes that we have just run a assembly (and we haven't cleared the arrays)
3495 :     # becuase the write_fluxanalyzer_files function is depended on those global arrays for the reactions.
3496 :    
3497 :     sub write_assembly
3498 :     {
3499 :     my($input,$genome,$name) = @_;
3500 :    
3501 :     my $paths = $input->[1];
3502 :     my $file_paths = $input->[0];
3503 :     print STDERR "Paths: @$paths \n File_Dirs: @$file_paths \n" if $debug;
3504 :     my @tempArray;
3505 :    
3506 :     chomp $genome;
3507 :     chomp $name;
3508 :     #setup the filesystem to store the assembly
3509 : dejongh 1.23 my $base_dir = get_scenario_directory($genome) . "/Assemblies/$name";
3510 : olson 1.10 system("rm", "-rf", $base_dir);
3511 :     &FIG::verify_dir($base_dir);
3512 :    
3513 : olson 1.1
3514 :     ##Here we want to reload the cpd and rxn info so we can write it later
3515 :     clear_arrays();
3516 :    
3517 :     #This gets us an array of arrays, each subarray holds
3518 : dejongh 1.12 # [0] = genome [1] = Scenarios [2] = superset [3] = subsystem [4] = scenario [5] = path
3519 : olson 1.1 my @assembly_scenarios = @{parse_assembly_scenarios($file_paths)};
3520 :    
3521 :     #load all the kegg information for each 'scenario' from the paths we have selected
3522 :     foreach my $scenario (@assembly_scenarios)
3523 :     {
3524 : dejongh 1.12 print STDERR "Checking $scenario->[3] $scenario->[4] $scenario->[5] \n" if $debug;
3525 :     process_init($scenario->[3],$scenario->[4],$scenario->[0],1);
3526 : olson 1.1 }
3527 :     #assume all path inputs and outputs are main
3528 :     map { $all_compounds_to_main{$_} = 1 } keys %all_inputs;
3529 :     map { $all_compounds_to_main{$_} = 1 } keys %all_outputs;
3530 :    
3531 :     create_assembly_reactions({},{},{},{});
3532 :    
3533 :     ##End of rxn,cpd loading
3534 :    
3535 :     #for the selected paths, lets gather their cpd from the checkbox and write
3536 :     #the path that we need
3537 :     foreach my $path (@$paths)
3538 :     {
3539 :     my (%input_hash, %output_hash, $path_name);
3540 :     #process the strings to get the information from the parameters
3541 :     my @items = split(";", $path);
3542 :     $path_name = $items[0];
3543 :     #next we have the input compounds ids/stoich/main
3544 :     map { if ($_ =~ /(.*):(.*):(.*)/)
3545 :     { $input_hash{$1}+= $2 } } split ",", $items[1];
3546 :     #the third part has the output compounds ids/stoich/main
3547 :     map { if ($_ =~ /(.*):(.*):(.*)/)
3548 :     { $output_hash{$1} += $2 } } split ",",$items[2];
3549 :     #the fourth part has the strings of reactions visited
3550 :     @tempArray = split("#" , $items[3]);
3551 :    
3552 :     print STDERR "\nInputs:\n" if $debug;
3553 :     print STDERR map{"$_ => $input_hash{$_}" } keys %input_hash, "\n" if $debug;
3554 :     print STDERR "\nOutputs:\n" if $debug;
3555 :     print STDERR map{"$_ => $output_hash{$_}"} keys %output_hash, "\n" if $debug;
3556 :    
3557 :     # divide stoichiometry by greatest common denominator
3558 :     my ($min_stoich, @all_stoichs);
3559 :    
3560 :     map { push @all_stoichs, $input_hash{$_}; $min_stoich = $input_hash{$_} if $input_hash{$_} < $min_stoich || $min_stoich == 0 } keys %input_hash;
3561 :     map { push @all_stoichs, $output_hash{$_}; $min_stoich = $output_hash{$_} if $output_hash{$_} < $min_stoich || $min_stoich == 0 } keys %output_hash;
3562 :    
3563 :     my ($gcd, @gcd_candidates);
3564 :    
3565 :     outer: for ($gcd = $min_stoich; $gcd > 1; $gcd--)
3566 :     {
3567 :     foreach my $stoich (@all_stoichs)
3568 :     {
3569 :     next outer if $stoich % $gcd != 0;
3570 :     }
3571 :    
3572 :     last; # found a gcd
3573 :     }
3574 :    
3575 :     map { $input_hash{$_} /= $gcd } keys %input_hash;
3576 :     map { $output_hash{$_} /= $gcd } keys %output_hash;
3577 :    
3578 :     print STDERR "Making directory $base_dir" if $debug;
3579 :    
3580 :    
3581 :     mkdir "$base_dir/$path_name";
3582 :     &write_fluxanalyzer_files("$base_dir/$path_name",\%input_hash,\%output_hash,
3583 :     \@tempArray,\%all_reactions,
3584 :     \%reactions_to_substrate_arrays,
3585 :     \%reactions_to_product_arrays,
3586 :     {});
3587 :     }
3588 :     }
3589 :    
3590 :     sub show_path_results
3591 :     {
3592 :     my ($data_results,$html,$cgi) = @_;
3593 :    
3594 :     print STDERR "Starting Results Display\n";
3595 :    
3596 :    
3597 :     #Display infinite loop warning if the indicator is on
3598 :     if($data_results->{"infinite"})
3599 :     {
3600 :     push(@$html, "<h3>Warning: Looks like an infinite loop</h3>");
3601 :     }
3602 :    
3603 :     #Delete the infinite loop indicator, so we don't need to have a if statment to check for it
3604 :     delete $data_results->{"infinite"};
3605 :    
3606 :     my $path_counter = 1;
3607 :     my $reactionPath;
3608 :     foreach my $token_id (sort { $data_results->{$a}->[1] <=> $data_results->{$b}->[1] }keys %$data_results)
3609 :     {
3610 :     print STDERR "Token id : $token_id\n";
3611 :    
3612 :     if(!($token_id =~ /^\d/))
3613 :     {
3614 :     next;
3615 :     }
3616 :    
3617 :     my @path = @{$data_results->{$token_id}->[2]};
3618 :     my @compounds = @{$data_results->{$token_id}->[3]};
3619 :    
3620 :     push(@$html, "<pre>Token: $token_id\tInitial Pass: $data_results->{$token_id}->[0]\tDone:$data_results->{$token_id}->[1]\n\tReactions: @path\n\tVisted Compounds: @compounds\n\tPath Inputs\n@{$data_results->{$token_id}->[4]}\n\tPath Outputs\n@{$data_results->{$token_id}->[5]}\n\tBorrowed\n@{$data_results->{$token_id}->[6]}\n</pre>");
3621 :    
3622 :    
3623 :     if ($data_results->{$token_id}->[1] == 1)
3624 :     {
3625 :     my $path_name = "path_".$path_counter++;
3626 :     my @tempArray;
3627 :     foreach my $elem(@{$data_results->{$token_id}->[2]}){
3628 :     push @tempArray, $elem;
3629 :     }
3630 :     $reactionPath = join "#", @tempArray;
3631 :     my $checkbox=$cgi->checkbox(-name=>"$path_name", -label=>'',
3632 :     -value=>"$path_name;@{[ join ',', map { $_ . ':' . $data_results->{$token_id}->[7]->{$_} . ':' . $all_compounds_to_main{$_} } keys %{$data_results->{$token_id}->[7]} ]};@{[ join ',', map { $_ . ':' . $data_results->{$token_id}->[8]->{$_} . ':' . $all_compounds_to_main{$_} } keys %{$data_results->{$token_id}->[8]} ]};$reactionPath");
3633 :     push @$html, $checkbox, "&nbsp;$path_name", $cgi->br;
3634 :     }
3635 :    
3636 :     push @$html, "<hr>";
3637 :    
3638 :    
3639 :     }
3640 :    
3641 :    
3642 :     push @$html, $cgi->hidden(-name=>'reaction_info',
3643 :     -value=>$reactionPath);
3644 :    
3645 :     }
3646 :    
3647 :     sub show_path_results_two
3648 :     {
3649 :     my ($data_results,$html,$cgi) = @_;
3650 :    
3651 :     print STDERR "Starting Results Display\n";
3652 :    
3653 :    
3654 :     #Display infinite loop warning if the indicator is on
3655 :     if($data_results->{"infinite"})
3656 :     {
3657 :     push(@$html, "<h3>Warning: Looks like an infinite loop</h3>");
3658 :     }
3659 :    
3660 :     #Delete the infinite loop indicator, so we don't need to have a if statment to check for it
3661 :     delete $data_results->{"infinite"};
3662 :    
3663 :     my $path_counter = 1;
3664 :     my $reactionPath;
3665 :     foreach my $token_id (sort { $data_results->{$a}->[1] <=> $data_results->{$b}->[1] }keys %$data_results)
3666 :     {
3667 :     print STDERR "Token id : $token_id\n";
3668 :    
3669 :     if(!($token_id =~ /^\d/))
3670 :     {
3671 :     next;
3672 :     }
3673 :    
3674 :     my @path = @{$data_results->{$token_id}->[2]};
3675 :     my @compounds = @{$data_results->{$token_id}->[3]};
3676 :    
3677 :     push(@$html, "<pre>Token: $token_id\tInitial Pass: $data_results->{$token_id}->[0]\tDone:$data_results->{$token_id}->[1]\n\tReactions: @path\n\tVisted Compounds: @compounds\n\tPath Inputs\n@{$data_results->{$token_id}->[4]}\n\tPath Outputs\n@{$data_results->{$token_id}->[5]}\n\tBorrowed\n@{$data_results->{$token_id}->[6]}\n</pre>");
3678 :    
3679 :    
3680 :     if ($data_results->{$token_id}->[1] == 1)
3681 :     {
3682 :     my $path_name = "path_".$path_counter++;
3683 :     my @tempArray;
3684 :     foreach my $elem(@{$data_results->{$token_id}->[2]}){
3685 :     push @tempArray, $elem;
3686 :     }
3687 :     $reactionPath = join "#", @tempArray;
3688 :     my $checkbox=$cgi->checkbox(-name=>"checked", -label=>'',
3689 :     -value=>"$path_name;@{[ join ',', map { $_ . ':' . $data_results->{$token_id}->[7]->{$_} . ':' . $all_compounds_to_main{$_} } keys %{$data_results->{$token_id}->[7]} ]};@{[ join ',', map { $_ . ':' . $data_results->{$token_id}->[8]->{$_} . ':' . $all_compounds_to_main{$_} } keys %{$data_results->{$token_id}->[8]} ]};$reactionPath");
3690 :     push @$html, $checkbox, "&nbsp;$path_name", $cgi->br;
3691 :     }
3692 :    
3693 :     push @$html, "<hr>";
3694 :     }
3695 :    
3696 :     }
3697 :    
3698 :    
3699 :     sub set_loop_max
3700 :     {
3701 :     my ($number) = @_;
3702 :     $loop_max = $number;
3703 :     }
3704 :    
3705 :     sub set_loop_max_assembly
3706 :     {
3707 :     my ($number) = @_;
3708 :     $loop_max_assembly = $number;
3709 :     }
3710 :    
3711 : dejongh 1.8 sub analyze_scenario_connections
3712 :     {
3713 :     my ($genome_id) = @_;
3714 : dejongh 1.23 my $scenario_dir = get_scenario_directory($genome_id);
3715 :     my $pathinfo_dir = $scenario_dir . "/PathInfo";
3716 : olson 1.13
3717 :     #
3718 :     # Mac and Linux don't have the same find options for following symlinks.
3719 :     #
3720 :     my $uname = `uname`;
3721 :     my @paths;
3722 :     if ($uname =~ /^Darwin/)
3723 :     {
3724 : dejongh 1.23 @paths = `find -L $pathinfo_dir -type d -name "path_*"`;
3725 : olson 1.13 }
3726 :     else
3727 :     {
3728 : dejongh 1.23 @paths = `find $pathinfo_dir -follow -type d -name "path_*"`;
3729 : olson 1.13 }
3730 : olson 1.1
3731 : dejongh 1.8 my %inputs;
3732 :     my %outputs;
3733 : olson 1.1
3734 : dejongh 1.12 foreach my $dir (@paths)
3735 : dejongh 1.8 {
3736 : dejongh 1.12 chomp $dir;
3737 : dejongh 1.8 my ($cat, $subsys, $scenario);
3738 : olson 1.1
3739 : dejongh 1.12 print STDERR "Processing $dir\n";
3740 :    
3741 : dejongh 1.23 if ($dir =~ (/\/PathInfo\/(.*)\/(.*)\/(.*)\//)){
3742 : dejongh 1.8 $cat = $1;
3743 :     $subsys = $2;
3744 :     $scenario = $3;
3745 :     }
3746 :     else
3747 :     {
3748 :     next;
3749 :     }
3750 : olson 1.1
3751 : dejongh 1.8 open(M_INPUTS,$dir."/inputs_main") or die("Failed to open $dir/inputs_main");
3752 :    
3753 :     while (<M_INPUTS>)
3754 :     {
3755 :     chomp;
3756 :     my ($cpd, $main) = split "\t" , $_;
3757 :     my $info = join "\t", $subsys, $scenario;
3758 :     $inputs{$cpd}->{$info} = 1 if $main eq "1";
3759 :     }
3760 :     close M_INPUTS;
3761 :    
3762 :     open(M_OUTPUTS, $dir."/outputs_main");
3763 :    
3764 :     while (<M_OUTPUTS>)
3765 :     {
3766 :     chomp;
3767 :     my ($cpd, $main) = split "\t" , $_;
3768 :     my $info = join "\t", $subsys, $scenario;
3769 :     $outputs{$cpd}->{$info} = 1 if $main eq "1";
3770 :     }
3771 :     close M_OUTPUTS;
3772 : dejongh 1.12 }
3773 : dejongh 1.8
3774 : dejongh 1.23 my $analysis_dir = $scenario_dir . "/Analysis";
3775 : dejongh 1.12 mkdir $analysis_dir;
3776 : dejongh 1.8
3777 : dejongh 1.12 open (IN_CONN, ">$analysis_dir/inputs_to_scenarios");
3778 :     foreach my $cpd (sort keys %inputs)
3779 :     {
3780 :     map { print IN_CONN "$cpd\t$_\n"; } keys %{$inputs{$cpd}};
3781 :     }
3782 :     close IN_CONN;
3783 : dejongh 1.8
3784 : dejongh 1.12 open (OUT_CONN, ">$analysis_dir/outputs_to_scenarios");
3785 :     foreach my $cpd (sort keys %outputs)
3786 :     {
3787 :     map { print OUT_CONN "$cpd\t$_\n"; } keys %{$outputs{$cpd}};
3788 : dejongh 1.8 }
3789 : dejongh 1.12 close OUT_CONN;
3790 : olson 1.1 }
3791 :    
3792 : formsma 1.14 sub predict_pegs_used
3793 :     {
3794 :     my ($genome_id) = @_;
3795 :     my @scenarios = @{Scenario->get_genome_scenarios("All",1)};
3796 :     unless(scalar(@scenarios))
3797 :     {
3798 :     return undef;
3799 :     }
3800 :     my %reaction_to_pegs;
3801 :    
3802 :     my @ss_names;
3803 : dejongh 1.24 &load_superset_file;
3804 : formsma 1.14 foreach (keys %superset_to_ss)
3805 :     {
3806 :     foreach my $subsys (@{$superset_to_ss{$_}})
3807 :     {
3808 :     push @ss_names, $subsys;
3809 :     }
3810 :    
3811 :     }
3812 :     foreach my $subsystem_name(@ss_names)
3813 :     {
3814 :     my $subsystem = $fig->get_subsystem($subsystem_name);
3815 :     next if(!defined $subsystem);
3816 : dejongh 1.24 my %reactions_for_ss = get_reactions_for_genome_in_subsystem($genome_id,$subsystem_name);
3817 : dejongh 1.17 next if(!defined %reactions_for_ss);
3818 :     foreach my $reaction (keys %reactions_for_ss)
3819 : formsma 1.14 {
3820 :     if(defined $reaction_to_pegs{$reaction})
3821 :     {
3822 : dejongh 1.17 push @{$reaction_to_pegs{$reaction}} , @{$reactions_for_ss{$reaction}};
3823 : formsma 1.14 }
3824 :     else
3825 :     {
3826 : dejongh 1.17 $reaction_to_pegs{$reaction} = $reactions_for_ss{$reaction};
3827 : formsma 1.14 }
3828 :     }
3829 :     }
3830 :    
3831 :     my %peg_to_scenario;
3832 :     foreach my $scenario (@scenarios)
3833 :     {
3834 :     my @scenario_reactions = @{$scenario->get_reaction_ids};
3835 :     my $path_valid = 1;
3836 :     my %pegs;
3837 :     foreach my $reaction (@scenario_reactions)
3838 :     {
3839 :     if(!defined $reaction_to_pegs{$reaction})
3840 :     {
3841 :     $path_valid = 0;
3842 :     last;
3843 :     }
3844 :     else
3845 :     {
3846 :     map {$pegs{$_} = 1} @{$reaction_to_pegs{$reaction}};
3847 :     }
3848 :     }
3849 :     if($path_valid)
3850 :     {
3851 :     foreach my $peg (keys %pegs)
3852 :     {
3853 :     if(!defined $peg_to_scenario{$peg})
3854 :     {
3855 :     $peg_to_scenario{$peg} = [$scenario->get_id()];
3856 :     }
3857 :     else
3858 :     {
3859 :     push @{$peg_to_scenario{$peg}} , $scenario->get_id();
3860 :     }
3861 :     }
3862 :     }
3863 :     }
3864 :    
3865 :     return \%peg_to_scenario;
3866 :     }
3867 : dejongh 1.19
3868 : dejongh 1.21 sub analyze_network_for_genome
3869 :     {
3870 :     my ($genome_id, $transports, $biomass) = @_;
3871 :    
3872 :     my ($node_type, $node_connections);
3873 :     my $debug = 0;
3874 :     my $unreachable = 0;
3875 :     chomp $genome_id;
3876 :    
3877 :     my @transports = split ("_", $transports);
3878 :     my @biomass = split ("_", $biomass);
3879 :    
3880 :     my %compound_id_to_name;
3881 :     open(IN,"$FIG_Config::global/Models/All/compound_id_to_name.txt") or die("Failed to open compound_id_to_name.txt");
3882 :     while(<IN>)
3883 :     {
3884 :     chomp($_);
3885 :     my($id,$name) = split("\t");
3886 :     $compound_id_to_name{$id} = $name;
3887 :     }
3888 :     close(IN);
3889 :    
3890 :     Scenario::set_fig($fig,$genome_id);
3891 :     print STDERR "Running check_model_hope on genome $genome_id\n" if($debug);
3892 :     print STDERR "\tStage 1 - Loading Scenarios, transports and biomass\n" if($debug);
3893 :     my @scenarios = @{Scenario->get_genome_scenarios($genome_id,0)};
3894 :    
3895 :     my %scenarios_used;
3896 :     my %scenarios_id;
3897 :    
3898 :     foreach (@scenarios)
3899 :     {
3900 :     $scenarios_used{$_->get_scenario_name()} = 0;
3901 :     $scenarios_id{$_->get_id()} = 0;
3902 :     }
3903 :    
3904 :     my %biomass_lib_cpds;
3905 :     foreach my $biomass_cpd (@biomass)
3906 :     {
3907 :     $biomass_lib_cpds{$biomass_cpd} = 1;
3908 :     }
3909 :    
3910 :     my (%transportable_cpds, %transportable_in_biomass);
3911 :     foreach my $transport_cpd (@transports)
3912 :     {
3913 :     if (defined $biomass_lib_cpds{$transport_cpd})
3914 :     {
3915 :     $transportable_in_biomass{$transport_cpd} = 1;
3916 :     }
3917 :     else
3918 :     {
3919 :     $transportable_cpds{$transport_cpd} = 1;
3920 :     }
3921 :     }
3922 :    
3923 :     print STDERR "\tStage 1 - Complete\n" if($debug);
3924 :     print STDERR "\tStage 2 - Run Analysis Tools\n" if($debug);
3925 :    
3926 :     # Total list of what compounds we reach.
3927 :     my %list_generated;
3928 :    
3929 :     # Here we are running all the scenarios we have inputs for
3930 :     # to analyze what biomass we can reach. We are also identifying
3931 :     # any Scenarios that are not being utilized.
3932 :    
3933 :     my %current_inputs;
3934 :     foreach (keys %transportable_cpds)
3935 :     {
3936 :     # keep track of the paths that lead to each compound
3937 :     $current_inputs{$_}->{"[]transport of ".$_."/".$compound_id_to_name{$_}."[$_]"} = 1;
3938 :     }
3939 :    
3940 :     while(1)
3941 :     {
3942 :     my ($temp_in,$break) = generate_new_inputs(\%current_inputs);
3943 :    
3944 :     foreach my $cpd (keys %$temp_in)
3945 :     {
3946 :     map { $current_inputs{$cpd}->{$_} = 1 } keys %{$temp_in->{$cpd}};
3947 :     }
3948 :    
3949 :     map { $list_generated{$_} = 1 } keys %$temp_in;
3950 :    
3951 :     print STDERR "Rechecking with inputs\n" if($debug);
3952 :     if($break)
3953 :     {
3954 :     last;
3955 :     }
3956 :     }
3957 :     print STDERR "Finished checking organism scenarios\n" if($debug);
3958 :    
3959 :     my %biomass_reached;
3960 :    
3961 :     foreach my $found (keys %list_generated)
3962 :     {
3963 :     if(defined $biomass_lib_cpds{$found})
3964 :     {
3965 :     $biomass_reached{$found} = $current_inputs{$found};
3966 :     }
3967 :     }
3968 :    
3969 :     foreach (keys %transportable_cpds)
3970 :     {
3971 :     push (@{$node_type->{'transport'}}, $_."/".$compound_id_to_name{$_});
3972 :     }
3973 :    
3974 :     print STDERR "\tStage 2 - Complete!\n" if($debug);
3975 :     print STDERR "\tStage 3 - Loading All Scenarios\n" if($debug);
3976 :    
3977 :     @scenarios = @{Scenario->get_genome_scenarios("All",0)};
3978 :    
3979 :     my %old_scenarios_used = %scenarios_used;
3980 :     my %old_scenarios_id = %scenarios_id;
3981 :    
3982 :     my %old_biomass_reached;
3983 :     foreach my $cid (keys %biomass_reached)
3984 :     {
3985 :     my %inner_hash_copy = %{$biomass_reached{$cid}};
3986 :     $old_biomass_reached{$cid} = \%inner_hash_copy
3987 :     }
3988 :    
3989 :     my %old_current_inputs;
3990 :     foreach my $cid (keys %current_inputs)
3991 :     {
3992 :     my %inner_hash_copy = %{$current_inputs{$cid}};
3993 :     $old_current_inputs{$cid} = \%inner_hash_copy;
3994 :     }
3995 :    
3996 :    
3997 :     %list_generated = {};
3998 :    
3999 :     foreach (@scenarios)
4000 :     {
4001 :     $scenarios_used{$_->get_scenario_name()} = 0 if ! exists $scenarios_used{$_->get_scenario_name()};
4002 :     $scenarios_id{$_->get_id()} = 0 if ! exists $scenarios_id{$_->get_id()};
4003 :     }
4004 :    
4005 :     while(1)
4006 :     {
4007 :     my ($temp_in,$break) = generate_new_inputs(\%current_inputs);
4008 :    
4009 :     foreach my $cpd (keys %$temp_in)
4010 :     {
4011 :     map { $current_inputs{$cpd}->{$_} = 1 } keys %{$temp_in->{$cpd}};
4012 :     }
4013 :    
4014 :     map { $list_generated{$_} = 1 } keys %$temp_in;
4015 :    
4016 :     print STDERR "Rechecking with inputs\n" if($debug);
4017 :     if($break)
4018 :     {
4019 :     last;
4020 :     }
4021 :     }
4022 :     print STDERR "Finished checking all scenarios\n" if($debug);
4023 :    
4024 :     my %biomass_reached;
4025 :    
4026 :     foreach my $found (keys %list_generated)
4027 :     {
4028 :     if(defined $biomass_lib_cpds{$found})
4029 :     {
4030 :     $biomass_reached{$found} = $current_inputs{$found};
4031 :     }
4032 :     }
4033 :    
4034 :     my (%cpds_to_print);
4035 :     my %org_cpds_to_check;
4036 :     map { $org_cpds_to_check{$_} = 1 } keys %old_biomass_reached;
4037 :     foreach my $cpd (sort keys %biomass_reached)
4038 :     {
4039 :     next if exists $old_biomass_reached{$cpd};
4040 :    
4041 :     $cpds_to_print{$cpd} = 1;
4042 :     my $done = 0;
4043 :    
4044 :     my @cpd_list = ($cpd);
4045 :     while (! $done)
4046 :     {
4047 :     my @new_substrates = collect_substrates(\%current_inputs, @cpd_list);
4048 :     @cpd_list = ();
4049 :     foreach my $substrate (@new_substrates)
4050 :     {
4051 :     if (exists $old_current_inputs{$substrate})
4052 :     {
4053 :     $org_cpds_to_check{$substrate} = 1;
4054 :     next;
4055 :     }
4056 :     push (@cpd_list, $substrate) if ! exists $cpds_to_print{$substrate};
4057 :     $cpds_to_print{$substrate} = 1;
4058 :     }
4059 :     $done = 1 if scalar @new_substrates == 0;
4060 :     }
4061 :     }
4062 :    
4063 :     foreach my $cpd (keys %cpds_to_print)
4064 :     {
4065 :     foreach my $path (keys %{$current_inputs{$cpd}})
4066 :     {
4067 :     my ($substrates,$scenario,$products) = $path =~ /\[(.*)\](.*)\[(.*)\]/;
4068 :    
4069 :     foreach my $substrate (split ",", $substrates)
4070 :     {
4071 :     foreach my $product (split ",", $products)
4072 :     {
4073 :     $node_connections->{$substrate."/".$compound_id_to_name{$substrate}}->$scenario = 1;
4074 :     $node_connections->{$scenario}->$product."/".$compound_id_to_name{$product} = 1;
4075 :     }
4076 :     }
4077 :     }
4078 :     }
4079 :    
4080 :     %cpds_to_print = ();
4081 :    
4082 :     foreach my $cpd (sort keys %org_cpds_to_check)
4083 :     {
4084 :     $cpds_to_print{$cpd} = 1;
4085 :     my $done = 0;
4086 :    
4087 :     my @cpd_list = ($cpd);
4088 :     while (! $done)
4089 :     {
4090 :     my @new_substrates = collect_substrates(\%old_current_inputs, @cpd_list);
4091 :     @cpd_list = ();
4092 :     foreach my $substrate (@new_substrates)
4093 :     {
4094 :     push (@cpd_list, $substrate) if ! exists $cpds_to_print{$substrate};
4095 :     $cpds_to_print{$substrate} = 1;
4096 :     }
4097 :     $done = 1 if scalar @new_substrates == 0;
4098 :     }
4099 :     }
4100 :    
4101 :     foreach my $cpd (keys %cpds_to_print)
4102 :     {
4103 :     foreach my $path (keys %{$old_current_inputs{$cpd}})
4104 :     {
4105 :     my ($substrates,$scenario,$products) = $path =~ /\[(.*)\](.*)\[(.*)\]/;
4106 :    
4107 :     foreach my $substrate (split ",", $substrates)
4108 :     {
4109 :     foreach my $product (split ",", $products)
4110 :     {
4111 :     $node_connections->{$substrate."/".$compound_id_to_name{$substrate}}->$scenario = 1;
4112 :     $node_connections->{$scenario}->$product."/".$compound_id_to_name{$product} = 1;
4113 :     }
4114 :     }
4115 :     }
4116 :     }
4117 :    
4118 :     foreach (sort keys %biomass_lib_cpds)
4119 :     {
4120 :     if(! exists $biomass_reached{$_} && ! exists $old_biomass_reached{$_})
4121 :     {
4122 :     print STDERR "Biomass $_/".$compound_id_to_name{$_}." not reachable using current transports" if ($debug);
4123 :     $unreachable++;
4124 :     if (exists $transportable_in_biomass{$_})
4125 :     {
4126 :     print STDERR " (transportable)" if ($debug);
4127 :     push (@{$node_type->{'biomass not reached (transportable)'}}, $_."/".$compound_id_to_name{$_});
4128 :     }
4129 :     else
4130 :     {
4131 :     push (@{$node_type->{'biomass not reached'}}, $_."/".$compound_id_to_name{$_});
4132 :     }
4133 :     print STDERR "\n" if ($debug);
4134 :     }
4135 :     elsif ($old_biomass_reached{$_})
4136 :     {
4137 :     push (@{$node_type->{'biomass reached'}}, $_."/".$compound_id_to_name{$_});
4138 :     }
4139 :     else
4140 :     {
4141 :     # do the paths all include a non-organism scenario as the last step?
4142 :     my $last_not_org = 1;
4143 :    
4144 :     foreach my $path_set (keys %{$biomass_reached{$_}})
4145 :     {
4146 :     my @paths = split ";", $path_set;
4147 :     my $last_path = $paths[-1];
4148 :     my ($substrates,$scenario,$products) = $last_path =~ /\[(.*)\](.*)\[(.*)\]/;
4149 :    
4150 :     if (exists $old_scenarios_used{$scenario})
4151 :     {
4152 :     $last_not_org = 0;
4153 :     last;
4154 :     }
4155 :     }
4156 :    
4157 :     if ($last_not_org == 1)
4158 :     {
4159 :     push (@{$node_type->{'biomass reached all last_not_org'}}, $_."/".$compound_id_to_name{$_});
4160 :     }
4161 :     else
4162 :     {
4163 :     push (@{$node_type->{'biomass reached all'}}, $_."/".$compound_id_to_name{$_});
4164 :     }
4165 :     }
4166 :     }
4167 :    
4168 :     foreach my $scen_name (sort keys %scenarios_used)
4169 :     {
4170 :     if($scenarios_used{$scen_name} == 1)
4171 :     {
4172 :     if (exists ($old_scenarios_used{$scen_name}))
4173 :     {
4174 :     push (@{$node_type->{'scenario present'}}, $scen_name);
4175 :     }
4176 :     else
4177 :     {
4178 :     push (@{$node_type->{'scenario present all'}}, $scen_name);
4179 :     }
4180 :     }
4181 :     }
4182 :    
4183 :     foreach my $cpd (keys %current_inputs)
4184 :     {
4185 :     # compounds present with All scenarios that aren't biomass
4186 :     if (! exists($old_current_inputs{$cpd}) && ! exists ($biomass_reached{$cpd}))
4187 :     {
4188 :     push (@{$node_type->{'compound present all'}}, $cpd."/".$compound_id_to_name{$cpd});
4189 :     }
4190 :     }
4191 :    
4192 :     print STDERR "\tStage 3 - Complete!\n" if($debug);
4193 :    
4194 :     return ($node_type, $node_connections);
4195 :    
4196 :     sub generate_new_inputs
4197 :     {
4198 :     my ($inputs) = @_;
4199 :     my %new_inputs;
4200 :     my $break = 1;
4201 :    
4202 :     my %used_scenario_paths_this_time;
4203 :    
4204 :     foreach my $scenario (@scenarios)
4205 :     {
4206 :     if($scenarios_id{$scenario->get_id()} == 1) # || $scenarios_used{$scenario->get_scenario_name()} != 0)
4207 :     {
4208 :     next;
4209 :     }
4210 :    
4211 :     my $sids = join ",", sort @{$scenario->get_substrates};
4212 :     my $pids = join ",", sort @{$scenario->get_products};
4213 :     my $cid_scenario_name = "[".$sids."]".$scenario->get_scenario_name()."[".$pids."]";
4214 :    
4215 :     my @matched;
4216 :    
4217 :     foreach my $substrate (@{$scenario->get_substrates()})
4218 :     {
4219 :     if(exists $inputs->{$substrate})
4220 :     {
4221 :     push @matched, $substrate;
4222 :     }
4223 :     }
4224 :    
4225 :     if (scalar @matched != scalar(@{$scenario->get_substrates()}))
4226 :     {
4227 :     next;
4228 :     }
4229 :     else
4230 :     {
4231 :     print STDERR "Scenario ".$cid_scenario_name." ran.\n" if($debug);
4232 :     $break = 0;
4233 :     $scenarios_id{$scenario->get_id()} = 1;
4234 :     $scenarios_used{$scenario->get_scenario_name()} = 1;
4235 :    
4236 :     # local determination that we've used an equivalent path already in this
4237 :     # time through the subroutine, so no need to process further
4238 :     next if (exists $used_scenario_paths_this_time{$cid_scenario_name});
4239 :     $used_scenario_paths_this_time{$cid_scenario_name} = 1;
4240 :    
4241 :     foreach my $product (@{$scenario->get_products()})
4242 :     {
4243 :     $new_inputs{$product}->{$cid_scenario_name} = 1;
4244 :     }
4245 :     }
4246 :     }
4247 :     return (\%new_inputs,$break);
4248 :     }
4249 :    
4250 :     sub collect_substrates
4251 :     {
4252 :     my $input_hash = shift @_;
4253 :     my @cpds = @_;
4254 :     my %substrate_hash;
4255 :    
4256 :     foreach my $cpd (@cpds)
4257 :     {
4258 :     foreach my $path (keys %{$input_hash->{$cpd}})
4259 :     {
4260 :     my ($substrates,$scenario,$products) = $path =~ /\[(.*)\](.*)\[(.*)\]/;
4261 :    
4262 :     foreach my $substrate (split ",", $substrates)
4263 :     {
4264 :     $substrate_hash{$substrate} = 1;
4265 :     }
4266 :     }
4267 :     }
4268 :     return keys %substrate_hash;
4269 :    
4270 :    
4271 :     }
4272 :     }
4273 :    
4274 : dejongh 1.24 sub get_reactions_for_genome_in_subsystem
4275 : dejongh 1.19 {
4276 :     my ($genome_id, $ss_name) = @_;
4277 : dejongh 1.24 my $subsystem = $fig->get_subsystem($ss_name);
4278 :     my %genome_reactions = ();
4279 :    
4280 :     my @roles = $subsystem->get_roles;
4281 :     my %hope_reactions = $subsystem->get_hope_reactions;
4282 :     my $index = $subsystem->get_genome_index($genome_id);
4283 :    
4284 :     if (defined $index)
4285 :     {
4286 :     foreach my $role (@roles)
4287 :     {
4288 :     my @peg_list = $subsystem->get_pegs_from_cell($genome_id,$role);
4289 :    
4290 :     if (defined $hope_reactions{$role} && scalar @peg_list > 0)
4291 :     {
4292 :     foreach my $reaction (@{$hope_reactions{$role}})
4293 :     {
4294 :     push @{$genome_reactions{$reaction}}, @peg_list;
4295 :     }
4296 :     }
4297 :     }
4298 :     }
4299 :     else
4300 :     {
4301 :     # the genome is not in the subsystem, but we can check for role assignments anyhow
4302 :     my $seqs = $fig->seqs_with_roles_in_genomes([$genome_id], \@roles);
4303 :    
4304 :     if (exists $seqs->{$genome_id})
4305 :     {
4306 :     foreach my $role (keys %{$seqs->{$genome_id}})
4307 :     {
4308 :     my $peg_tuples = $seqs->{$genome_id}->{$role};
4309 :     my @peg_list = map {$_->[0]} @{$peg_tuples};
4310 :    
4311 :     if (defined $hope_reactions{$role} && scalar @peg_list > 0)
4312 :     {
4313 :     foreach my $reaction (@{$hope_reactions{$role}})
4314 :     {
4315 :     push @{$genome_reactions{$reaction}}, @peg_list;
4316 :     }
4317 :     }
4318 :     }
4319 :     }
4320 :     }
4321 : dejongh 1.19
4322 : dejongh 1.20 # assume that the hope_supersets.txt has been loaded
4323 :     my $superset = $ss_to_superset{$ss_name};
4324 : dejongh 1.23 my $pathdir_org = get_scenario_directory($genome_id);
4325 :     my $file_path = "$pathdir_org/Curation/PathInfo/$superset/$ss_name/included_reactions_for_org";
4326 : dejongh 1.19 if (open (INCLUDEDRXNS, $file_path))
4327 :     {
4328 :     my @included_reactions = (<INCLUDEDRXNS>);
4329 :     close INCLUDEDRXNS;
4330 :     chomp (@included_reactions);
4331 : dejongh 1.24 map { $genome_reactions{$_} = 1 } @included_reactions;
4332 : dejongh 1.19 }
4333 :    
4334 : dejongh 1.24 return %genome_reactions;
4335 : dejongh 1.19 }
4336 : dejongh 1.20
4337 : dejongh 1.23 sub get_scenario_directory
4338 : dejongh 1.20 {
4339 :     my ($genome) = @_;
4340 :     # want to match "All" and "All.bk", etc.
4341 : dejongh 1.23 return $genome =~ "^All" ? $FIG_Config::global."/Models/$genome/Scenarios" : $fig->scenario_directory($genome);
4342 : dejongh 1.20
4343 :     }
4344 :    
4345 : olson 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3