[Bio] / RegSubMa / SVOrganism.pm Repository:
ViewVC logotype

View of /RegSubMa/SVOrganism.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (download) (as text) (annotate) (vendor branch)
Wed Apr 4 17:08:51 2007 UTC (13 years, 3 months ago) by mkubal
Branch: MAIN, fig
CVS Tags: v_1_0_0, HEAD
Changes since 1.1: +0 -0 lines
initial checkin

package SVOrganism;

use URI::Escape;
use HTML;
use FIG;
use PieChart;
use strict;
use warnings;

1;

sub get_data {
  my ($fig, $cgi, $genome) = @_;

  # get the data
  my $subsystem_data = $fig->get_genome_subsystem_data($genome);
  my $assignment_data = $fig->get_genome_assignment_data($genome);

  return ($subsystem_data, $assignment_data);
}

sub get_basic_statistics {
  my ($fig, $cgi, $genome, $subsystem_data, $assignment_data) = @_;

  # get the data if not passed
  unless (defined($subsystem_data)) {
    $subsystem_data = $fig->get_genome_subsystem_data($genome);
  }
  unless(defined($assignment_data)) {
    $assignment_data = $fig->get_genome_assignment_data($genome);
  }
  
  my %sscount = map { $_->[0] => 1 } @$subsystem_data;
  my $nss=scalar(keys(%sscount));

  my $statistics = {
		    num_subsystems => $nss,
		    num_contigs    => scalar($fig->all_contigs($genome)),
		    num_basepairs  => $fig->genome_szdna($genome),
		    genome_name    => $fig->genus_species($genome),
		    genome_domain  => $fig->genome_domain($genome),
		    genome_pegs    => $fig->genome_pegs($genome),
		    genome_rnas    => $fig->genome_rnas($genome),
		    genome_version => $fig->genome_version($genome)
		   };

  return $statistics;
}

sub get_peg_statistics {
  my ($fig, $cgi, $genome, $subsystem_data, $assignment_data) = @_;

  # get the data if not passed
  unless (defined($subsystem_data)) {
    $subsystem_data = $fig->get_genome_subsystem_data($genome);
  }
  unless(defined($assignment_data)) {
    $assignment_data = $fig->get_genome_assignment_data($genome);
  }
  
  my $hypo_sub = 0;
  my $hypo_nosub = 0;
  my $nothypo_sub = 0;
  my $nothypo_nosub = 0;
  my %in = map { $_->[2] => 1 } @$subsystem_data;
  my $in = keys(%in);
  
  my %sscount = map { $_->[0] => 1 } @$subsystem_data;
  
  foreach $_ (@$assignment_data)
    {
      my($peg,$func) = @$_;
      my $is_hypo = &FIG::hypo($func);
      
      if    ($is_hypo && $in{$peg})           { $hypo_sub++ }
      elsif ($is_hypo && ! $in{$peg})         { $hypo_nosub++ }
      elsif ((! $is_hypo) && (! $in{$peg}))   { $nothypo_nosub++ }
      elsif ((! $is_hypo) && $in{$peg})       { $nothypo_sub++ }
    }
  my $tot = $hypo_sub + $nothypo_sub + $hypo_nosub + $nothypo_nosub;
  
  my ($fracHS, $fracNHS, $fracHNS, $fracNHNS);
  
  if ($tot == 0) {
    $fracHS = sprintf "%.2f", 0.0;
    $fracNHS = sprintf "%.2f", 0.0;
    $fracHNS = sprintf "%.2f", 0.0;
    $fracNHNS = sprintf "%.2f", 0.0;
  } else {
    $fracHS = sprintf "%.2f", $hypo_sub / $tot * 100;
    $fracNHS = sprintf "%.2f", $nothypo_sub / $tot * 100;
    $fracHNS = sprintf "%.2f", $hypo_nosub / $tot * 100;
    $fracNHNS = sprintf "%.2f", $nothypo_nosub / $tot * 100;
    }
  
  my $statistics = {
		    hypothetical_in_subsystem => $hypo_sub,
		    hypothetical_not_in_subsystem => $hypo_nosub,
		    non_hypothetical_in_subsystem => $nothypo_sub,
		    non_hypothetical_not_in_subsystem => $nothypo_nosub,
		    hypothetical_in_subsystem_percent => $fracHS,
		    hypothetical_not_in_subsystem_percent => $fracHNS,
		    non_hypothetical_in_subsystem_percent => $fracNHS,
		    non_hypothetical_not_in_subsystem_percent => $fracNHNS
		   };

  return $statistics;
}

sub get_subsystems {
  my ($fig, $cgi, $genome, $subsystem_data, $assignment_data) = @_;

  # get the data if not passed
  unless (defined($subsystem_data)) {
    $subsystem_data = $fig->get_genome_subsystem_data($genome);
  }
  unless(defined($assignment_data)) {
    $assignment_data = $fig->get_genome_assignment_data($genome);
  }
  my $active = $fig->active_subsystems($genome);

  # create treeview data-structure
  my $tree;
  my ($sub, $role, $protein, %in, $categories);
  foreach my $subsystem (@$subsystem_data) {
    ($sub,$role,$protein) = @$subsystem;
    if ($active->{$sub}) {
      push(@{$in{$sub}->{$role}}, "<a href='index.cgi?action=ShowAnnotation&prot=$protein'>" .$protein . "</a>");
    }
  }
  
  my @subs = sort { ($a->[0] cmp $b->[0]) or
		     ($a->[1] cmp $b->[1]) or
		     ($a->[2] cmp $b->[2]) or
		     ($a->[3] cmp $b->[3]) or
		     ($a->[4] cmp $b->[4]) } 
    map { $sub = $_; 
	  $categories = $fig->subsystem_classification($sub);
	  $categories = ((@$categories > 0) && $categories->[0]) ? $categories : ["Miscellaneous"];
	  [@$categories,$sub] 
	}
      keys(%in);
  
  my $last1 = "";
  my $last2 = "";
  my $curr_0 = -1;
  my $curr_1 = -1;
  my $curr_2 = -1;
  foreach my $tuple (@subs) {
    $sub = pop @{$tuple};
    my $topic = $tuple->[0];
    
    if ($topic ne $last1) {
      push(@$tree, { value => $topic, children => [] });
      $curr_0++;
      $curr_1 = -1;
      $curr_2 = -1;
      $last1 = $topic;
      $last2 = "";
    }
    
    $topic = $tuple->[1] ? $tuple->[1] : "Uncategorized";
    if ($topic && ($topic ne $last2)) {
      push(@{$tree->[$curr_0]->{children}}, { value => $topic, children => [] });
      $curr_1++;
      $curr_2 = -1;
      $last2 = $topic;
    }

    push(@{$tree->[$curr_0]->{children}->[$curr_1]->{children}}, { value => "<a href='index.cgi?action=ShowSubsystem&subsystem_name=$sub'>" . $sub . "</a>", children => [] });
    $curr_2++;
    
    foreach my $role (sort keys(%{$in{$sub}})) {
      push(@{$tree->[$curr_0]->{children}->[$curr_1]->{children}->[$curr_2]->{children}}, { value => $role, children => $in{$sub}->{$role} }  );
    }
  }

  # do counting
  my $cats;
  my $i = 0;
  foreach my $level1 (@$tree) {
    my $count1 = 0;
    foreach my $level2 (@{$level1->{children}}) {
      my $count2 = 0;
      foreach my $level3 (@{$level2->{children}}) {
	my $count3 = 0;
	foreach my $level4 (@{$level3->{children}}) {
	  my $count4 = scalar(@{$level4->{children}});
	  $level4->{value} = $level4->{value} . " (" . $count4 . ")";
	  $count3 += $count4;
	}
	$level3->{value} = $level3->{value} . " (" . $count3 . ")";
	$count2 += $count3;
      }
      $level2->{value} = $level2->{value} . " (" . $count2 . ")";
      $count1 += $count2;
    }
    $level1->{value} = $level1->{value} . " (" . $count1 . ")";
    $cats->{$i} = $count1;
    $i++;
  }

  my @sorted_categories = sort { $cats->{$b} <=> $cats->{$a} } keys(%$cats);
  my @sorted_values;
  my $h = 0;
  foreach my $cat (@sorted_categories) {
    $tree->[$cat]->{value} = &PieChart::get_minibox($h) . "&nbsp;&nbsp;" . $tree->[$cat]->{value};
    push(@sorted_values, $cats->{$cat});
    $h++;
  }

  return { treeview => &create_treeview($tree, "subsystree"),
	   tree     => $tree,
	   sorted_categories => \@sorted_values };
}

sub get_reactions {
  my ($fig, $cgi, $genome, $subsystem_data) = @_;

  # get the data if not passed
  unless (defined($subsystem_data)) {
    $subsystem_data = $fig->get_genome_subsystem_data($genome);
  }

  my $tree;
  foreach my $subsystem (@$subsystem_data) {
    my ($sub,$role,$protein) = @$subsystem;
    my $reactions = {};
    my $ss = $fig->get_subsystem($sub);
    if (defined($ss)) {
      $reactions = $ss->get_reactions || {};
    }
    my $class = $fig->subsystem_classification($sub);
    my $reactions_for_role = $reactions->{$role};
    my $category = ((@$class > 0) && $class->[0]) ? $class->[0] : "Misc";
    foreach my $reaction (@$reactions_for_role) {
      if ($fig->valid_reaction_id($reaction)) {
	unless (defined($tree->{$category})) {
	  $tree->{$category} = {};
	}

	my $disp_react = $fig->displayable_reaction($reaction);
        $disp_react =~ s/^R\d+\: //;
        my $rstring = &HTML::reaction_link($reaction) . ": $disp_react";
	
	unless (defined($tree->{$category}->{$reaction})) {
	  $tree->{$category}->{$rstring} = {};
	}

	$tree->{$category}->{$rstring}->{"<a href='index.cgi?action=Proteinpage&prot=" . $protein . "'>" . $protein ."</a>"} = 1;
      }
    }
  }

  return { treeview => &create_hash_tree($tree, "reactiontree"),
	   tree     => $tree };

}

sub create_treeview {
  my ($tree, $id) = @_;
  
  my $tree_html = "<div id='$id'>";
  my $curr = 0;
  my $html = "";

  foreach my $branch (@$tree) {
    ($html, $curr) = &create_branch($branch, $id, $curr);
    $tree_html .= $html
  }

  $tree_html .= "</div>";

  return $tree_html;
}

sub create_branch {
  my ($tree, $id, $curr) = @_;

  my $tree_html = "";
  my $html = "";

  # check for branch
  if (ref($tree) eq "HASH") {
    
    my $img = "";
    if (scalar(@{$tree->{children}}) > 0) {
      $img = "<img src='./Html/plus.gif' id='$id\_$curr\_image' onclick='treeswitch(\"$id\", \"$curr\")'>";
    }
    $tree_html = $img . $tree->{value} . "<br/>\n<div style='padding-left: 15px; margin: 0px;' id='$id\_$curr\_element' class='hideme'>";
    
    foreach my $child (@{$tree->{children}}) {
      $curr++;
      ($html, $curr) = &create_branch($child, $id, $curr);
      $tree_html .= $html;
    }

    $tree_html .= "</div>";
  }

  # otherwise create leaves
  else {
    $tree_html .= $tree . "<br/>";
  }

  return ($tree_html, $curr);
}

sub create_hash_tree {
  my ($tree, $id) = @_;
  
  my $tree_html = "<div id='$id'>";
  my $curr = 0;
  my $html = "";

  foreach my $branch (sort(keys(%$tree))) {
    ($html, $curr) = &create_hash_branch($branch, $tree->{$branch}, $id, $curr);
    $tree_html .= $html
  }

  $tree_html .= "</div>";

  return $tree_html;
}

sub create_hash_branch {
  my ($key, $tree, $id, $curr) = @_;

  my $tree_html = "";
  my $html = "";

  # check for branch
  if (ref($tree) eq "HASH") {
    
    my $img = "";
    if (scalar(keys(%$tree)) > 0) {
      $img = "<img src='./Html/plus.gif' id='$id\_$curr\_image' onclick='treeswitch(\"$id\", \"$curr\")'>";
    }
    $tree_html = $img . $key . "<br/>\n<div style='padding-left: 15px; margin: 0px;' id='$id\_$curr\_element' class='hideme'>";
    
    foreach my $child (sort(keys(%$tree))) {
      $curr++;
      ($html, $curr) = &create_hash_branch($child, $tree->{$child}, $id, $curr);
      $tree_html .= $html;
    }

    $tree_html .= "</div>";
  }

  # otherwise create leaves
  else {
    $tree_html .= $key . "<br/>";
  }

  return ($tree_html, $curr);
}

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3