1.	#!/usr/bin/perl -w
2.	use strict;
3.	use FBAMODELserver;
4.	
5.	# Reads the SEED genome-scale metabolic model of E. coli  
6.	# Runs flux variability analysis, flux balance analysis, and single gene knockout simulations in E. coli
7.	# Prints all results to file
8.	
9.	#1.) Creating perl object which interfaces with the SEED server API
10.	my $fbaObject = FBAMODELserver->new();
11.	
12.	#2.) Obtain a list of the reaction IDs in the E. coli model
13.	my $reactionList = $fbaObject->get_reaction_id_list({id => "Seed83333.1"});
14.	
15.	#3.) Obtain a list of the compound IDs in the E. coli model
16.	my $compoundList = $fbaObject->get_compound_id_list({id => "Seed83333.1"});
17.	
18.	#4.) Obtaining detailed data on E. coli reactions
19.	my $reactionData = $fbaObject->get_reaction_data({"id" => $reactionList->{"Seed83333.1"},"model" => ["Seed83333.1"]});
20.	
21.	#5.) Obtaining detailed data on E. coli compounds
22.	my $compoundData = $fbaObject->get_compound_data({"id" => $compoundList->{"Seed83333.1"},"model" => ["Seed83333.1"]});
23.	
24.	#6.) Running flux variability analysis on E. coli model while simulating growth in LB media
25.	my $fvaOutput = $fbaObject->classify_model_entities({"parameters" => [{id=>"Seed83333.1",media=>"ArgonneLBMedia"}]});
26.	#Placing fva output into a hash to simplify access
27.	my $fvaHash;
28.	for (my $i=0; $i < @{$fvaOutput->[0]->{entities}};$i++) {
29.		$fvaHash->{substr($fvaOutput->[0]->{entities}->[$i],0,8)} = $fvaOutput->[0]->{classes}->[$i];
30.	}
31.	
32.	#7.) Simulating growth of E. coli model in LB media
33.	my $fbaOutput = $fbaObject->simulate_model_growth({"parameters" => [{id=>"Seed83333.1",media=>"Carbon-D-Glucose"}]});
34.	#Placing fba output into a hash to simplify access
35.	my $fbaHash;
36.	for (my $i=0; $i < @{$fbaOutput->[0]->{entities}};$i++) {
37.		$fbaHash->{$fbaOutput->[0]->{entities}->[$i]} = $fbaOutput->[0]->{fluxes}->[$i];
38.	}
39.	
40.	#8.) Simulating single gene knockout in E. coli model during growth in LB media
41.	my $KOOutput = $fbaObject->simulate_all_single_gene_knockout({"parameters" => [{id=>"Seed83333.1",media=>"ArgonneLBMedia"}]});
42.	
43.	#9.) Printing all compound data to output file CompoundTbl.txt
44.	my $columns = ["DATABASE","NAME","FORMULA","CHARGE","FVA class","FBA uptake flux"];
45.	open (OUTPUT, ">CompoundTbl.txt");
46.	print OUTPUT join("\t",@{$columns})."\n";
47.	for (my $i=0; $i < @{$compoundList->{"Seed83333.1"}}; $i++) {
48.		for (my $j=0; $j < @{$columns}; $j++) {
49.			if ($j > 0) {
50.				print OUTPUT "\t";
51.			}
52.			if ($columns->[$j] eq "FVA class") {
53.				if (defined($fvaHash->{$compoundList->{"Seed83333.1"}->[$i]})) {
54.					print OUTPUT $fvaHash->{$compoundList->{"Seed83333.1"}->[$i]};
55.				}
56.			} elsif ($columns->[$j] eq "FBA uptake flux") {
57.				if (defined($fbaHash->{$compoundList->{"Seed83333.1"}->[$i]})) {
58.					print OUTPUT $fbaHash->{$compoundList->{"Seed83333.1"}->[$i]};
59.				}
60.			} else {
61.				if (defined($compoundData->{$compoundList->{"Seed83333.1"}->[$i]}->{$columns->[$j]})) {
62.					print OUTPUT join(", ",@{$compoundData->{$compoundList->{"Seed83333.1"}->[$i]}->{$columns->[$j]}});
63.				}
64.			}
65.		}
66.		print OUTPUT "\n";
67.	}
68.	close (OUTPUT);
69.	
70.	#10.) Printing all reaction data to output file ReactionTbl.txt
71.	$columns = ["DATABASE","NAME","EQUATION","Seed83333.1 COMPARTMENT","Seed83333.1 ASSOCIATED PEG","Seed83333.1 DIRECTIONALITY","FVA class","FBA flux"];
72.	open (OUTPUT, ">ReactionTbl.txt");
73.	print OUTPUT join("\t",@{$columns})."\n";
74.	for (my $i=0; $i < @{$reactionList->{"Seed83333.1"}}; $i++) {
75.		for (my $j=0; $j < @{$columns}; $j++) {
76.			if ($j > 0) {
77.				print OUTPUT "\t";
78.			}
79.			if ($columns->[$j] =~ m/^Seed83333\.1\s(.+)/) {
80.				my $columnName = $1;
81.				if (defined($reactionData->{$reactionList->{"Seed83333.1"}->[$i]}->{"Seed83333.1"}->{$columnName})) {
82.					print OUTPUT join(" or ",@{$reactionData->{$reactionList->{"Seed83333.1"}->[$i]}->{"Seed83333.1"}->{$columnName}});
83.				}
84.			} elsif ($columns->[$j] eq "FVA class") {
85.				if (defined($fvaHash->{$reactionList->{"Seed83333.1"}->[$i]})) {
86.					print OUTPUT $fvaHash->{$reactionList->{"Seed83333.1"}->[$i]};
87.				}
88.			} elsif ($columns->[$j] eq "FBA flux") {
89.				if (defined($fbaHash->{$reactionList->{"Seed83333.1"}->[$i]})) {
90.					print OUTPUT $fbaHash->{$reactionList->{"Seed83333.1"}->[$i]};
91.				}
92.			} else {
93.				print OUTPUT $reactionData->{$reactionList->{"Seed83333.1"}->[$i]}->{$columns->[$j]}->[0];
94.			}
95.		}
96.		print OUTPUT "\n";
97.	}
98.	close (OUTPUT);
99.	
100.	#11.) Printing essential gene predictions to output file GeneTbl.txt
101.	open (OUTPUT, ">GeneTbl.txt");
102.	print OUTPUT "Predicted essential E. coli genes\n";
103.	for (my $i=0; $i < @{$KOOutput->[0]->{"essential genes"}}; $i++) {
104.		print OUTPUT $KOOutput->[0]->{"essential genes"}->[$i]."\n";
105.	}
106.	close (OUTPUT);