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

Annotation of /FigKernelPackages/model.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3