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

View of /FigKernelPackages/Scenario.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (download) (as text) (annotate)
Wed Nov 14 19:43:45 2007 UTC (12 years, 3 months ago) by dejongh
Branch: MAIN
CVS Tags: rast_rel_2008_06_18, rast_rel_2008_06_16, rast_rel_2008_07_21, rast_2008_0924, rast_rel_2008_04_23, rast_rel_2008_09_30, mgrast_rel_2008_0924, mgrast_rel_2008_1110_v2, mgrast_rel_2008_0625, rast_rel_2008_10_09, rast_release_2008_09_29, mgrast_rel_2008_0806, mgrast_rel_2008_0923, mgrast_rel_2008_0919, mgrast_rel_2008_1110, rast_rel_2008_09_29, mgrast_rel_2008_0917, rast_rel_2008_10_29, rast_rel_2008_08_07
Changes since 1.4: +22 -0 lines
Added getter/setter for path_info

# _*_ Perl _*_
#
# 6/4/07
# Scenario.pm contains the package/class for creating scenario data instances
#  and completing analysis based on that information
#
package Scenario;

use strict;
use FIGV;
use Subsystem;
use model;

my $model_dir;

#**************
# Constructors for Instances of Scenarios
#
#**************

sub new
{
    my ($class,$path,$scenario_id,$scenario_name,$genome_id) = @_;
    my %self = ("path" => $path,"id" =>$scenario_id,
		"scenario_name" => $scenario_name,
		"substrates" => [],"substrates_cofactors" => [],
		"products" => [],"products_cofactors" => [],
		"reactions" => {},"genome_id" => $genome_id
		); 
    bless \%self, $class;
}

#**************
# Static Functions
# 
#**************

#You must pass a FIG object which will help it locate the scenarios.
# needed to have rast/normal seed compatablity
sub set_fig
{
    my ($fig,$genome) = @_;
    model::set_fig($fig);
    $model_dir = $fig->model_directory();
}


# Arguments : Class , Genome_id
# Returns : Array of Scenario instances
# This function will create scenario instances for all scenarios of a given genome
# Example my @scenarios = @{Scenario->get_genome_scenarios("83333.1")};
sub get_genome_scenarios
{
    my $class = shift;
    my $genome_id = shift;
    my $load_reactions = shift;
    
    my @scenario_obj;
    my @scenario_paths_dirs = @{model::parse_assembly_scenarios(["/$genome_id/Scenarios"])};
    foreach my $scenario_path (@scenario_paths_dirs)
    {
	my $path = "$model_dir/" . join "/" , @$scenario_path;
	my $scenario_id = "$scenario_path->[-3]/$scenario_path->[-2]/$scenario_path->[-1]";
	my $scenario_name = "$scenario_path->[-3]/$scenario_path->[-2]";
	my $scenario = $class->new($path,$scenario_id,$scenario_name,$genome_id);
	$scenario->load_information();
	$scenario->read_reactions() if($load_reactions);
	push @scenario_obj, $scenario;
    }
    return \@scenario_obj;
}

sub same_scenario
{
    my $class = shift;
    my $scenario1 = shift;
    my $scenario2 = shift;
    return ($scenario1->get_scenario_name() eq $scenario2->get_scenario_name());
    #return (split '/', $scenario1->get_id())[1] eq (split '/', $scenario2->get_id())[1];
}


#***************
# Data Manipulation (for instances of Scenarios only)
#
#***************

sub load_information
{
    my $self = shift;
    $self->read_data("/inputs_main","substrates");
    $self->read_data("/outputs_main","products");
    $self->read_path_info;
}

sub read_data
{
    my $self = shift;
    my $file = shift;
    my $store_loc = shift;
    open(DATA,$self->{"path"}.$file) or
	die("Failed to open ".$self->{"path"}.$file." in ".$self->{"id"});
    while(<DATA>)
    {
	chomp;
	my @line = split "\t";
	chomp @line;
	
	if($line[1])
	{
	    push @{$self->{$store_loc}}, $line[0];
	}
	else
	{
	    push @{$self->{$store_loc."_cofactors"}}, $line[0];
	}
    }
    close(DATA);
}

sub read_reactions
{
    my $self = shift;
    open(REACTIONS,$self->{"path"}."/reactions") or
	die("Failed to open ".$self->{"path"}."/reactions file.\n");

    while(<REACTIONS>)
    {
	my $reaction_name;
	my %substrates;
	my %products;
	my $direction = 0; #by default reactions go left to right
	
	chomp;
	my @line = split "\t";
	chomp @line;
	#section 0 contains the reaction name
	if($line[0] =~ /^R\d{5}/)
	{
	    $reaction_name = $line[0];
	}
	else { next; } #This isn't a reaction, skip this line
	#section 1 contains compound and stoich information
	my @temp = split "=", $line[1];
	chomp @temp;
	my @substrates = split /\+/, $temp[0];
	my @products = split /\+/, $temp[1];
	chomp @substrates;
	chomp @products;
	foreach (@substrates)
	{
	    my @temp1 = split "\ ";
	    $substrates{$temp1[1]} = $temp1[0];
	}
	foreach (@products)
	{
	    my @temp1 = split "\ ";
	    $products{$temp1[1]} = $temp1[0];
	}
	#section 4 contains direction information
	my @temp2 = split "\ ", $line[4];
	if($temp2[0] eq "-Inf")
	{
	    $direction = 1; #Reversible direction
	}
	#Store the reaction..
	# Array: ($direction, %substrates, %products, genome_pegs)
	my @temp3 = ($direction,\%substrates,\%products,[]);
	$self->{"reactions"}->{$reaction_name} = \@temp3;
    }
    close(REACTIONS);
}

sub read_path_info
{
    my $self = shift;
    my $file = "/path_info";
    my $store_loc = "path_info";
    open(DATA,$self->{"path"}.$file) or
	die("Failed to open ".$self->{"path"}.$file." in ".$self->{"id"});
    while(<DATA>)
    {
	chomp;
	push @{$self->{$store_loc}}, $_;
    }
    close(DATA);
}

sub load_reaction_pegs
{
    my $self = shift;
    my $fig = shift;
    my @temp =  split "/", $self->{"id"};
    chomp @temp;
    my $subsystem = $fig->get_subsystem($temp[0]);
    return undef if(!defined $subsystem);
    my %reactions_for_ss = $subsystem->get_hope_reactions_for_genome($self->{"genome_id"});
    foreach my $reaction (keys %reactions_for_ss)
    {
	if(defined $self->{"reactions"}->{$reaction})
	{
	    push @{$self->{"reactions"}->{$reaction}->[3]} , @{$reactions_for_ss{$reaction}};
	}
    }
}

#*********************
# Accessor Subroutines
#*********************

#main compounds only
sub get_substrates
{
    my $self = shift;
    return $self->{"substrates"};
}
#main compounds only
sub get_products
{
    my $self = shift;
    return $self->{"products"};
}
#reactions in the path only
sub get_path_info
{
    my $self = shift;
    return $self->{"path_info"};
}

sub get_substrate_cofactors
{
    my $self = shift;
    return $self->{"substrates_cofactors"};
}

sub get_product_cofactors
{
    my $self = shift;
    return $self->{"products_cofactors"};
}

#main and cofactor compounds
sub get_substrates_all
{
    my $self = shift;
    my @mains = @{$self->{"substrates"}};
    my @cofactors = @{$self->{"substrates_cofactors"}};
    return [@mains,@cofactors];
}
#main and cofactor compounds
sub get_products_all
{
    my $self = shift;
    my @mains = @{$self->{"products"}};
    my @cofactors = @{$self->{"products_cofactors"}};
    return [@mains,@cofactors];
}

sub get_id
{
    my $self = shift;
    return $self->{"id"};
}

sub get_scenario_name
{
    my $self = shift;
    return $self->{"scenario_name"};
}


sub get_subsystem_name
{
    my $self = shift;
    my @parts = split "/" , $self->{"scenario_name"};
    chomp @parts;
    return $parts[0];
}

sub get_reaction_ids
{
    my $self = shift;
    my @names = keys %{$self->{"reactions"}};
    return \@names;
}

sub get_reaction_pegs
{
    my $self = shift;
    my $reaction_id = shift;
    return $self->{"reactions"}->{$reaction_id}->[3];
}


# Returns false if left-to-right, and true if reversible
sub get_reaction_reversibility
{
    my $self = shift;
    my $reaction_id = shift;
    return $self->{"reactions"}->{$reaction_id}->[0];
}

# Returns a hash of keys=compound_ids and values=stoich
sub get_reaction_substrates
{
    my $self = shift;
    my $reaction_id = shift;
    return $self->{"reactions"}->{$reaction_id}->[1];
}

# Returns a hash of keys=compound_ids and values=stoich
sub get_reaction_products
{
    my $self = shift;
    my $reaction_id = shift;
    return $self->{"reactions"}->{$reaction_id}->[2];
}
1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3