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

Annotation of /FigKernelPackages/model.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3