[Bio] / FigWebServices / ma_to_tf_nr.cgi Repository:
ViewVC logotype

View of /FigWebServices/ma_to_tf_nr.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (download) (annotate)
Fri Jan 20 16:14:37 2006 UTC (14 years, 2 months ago) by mkubal
Branch: MAIN
new

# -*- perl -*-

=pod

=head1 protein_sets_2.cgi

Find transcription factors for affymetrix spot ids 

=cut

use FIG;
use HTML;
use CGI;
my $cgi=new CGI;
use LWP::Simple qw(!head); # see the caveat in perldoc LWP about importing two head methods.

$fig = new FIG;
my $html = [];

unshift(@$html, "<TITLE>Find Transcription Factors for Expressed Genes</TITLE>\n");

my $id_to_ratio;
my $inputs;
if ($cgi->param('request') ) 
{
  my $input =$cgi->param('proteins');
  my @inputs = split("\n",$input);

  if ($cgi->upload('fileupload'))
  {
     my $fh=$cgi->upload('fileupload');
     @inputs = <$fh> ;
  }

  $given = [@inputs];
  $id_to_ratio = &parse_inputs($given); 
 
}

if ($id_to_ratio && $cgi->param('request') eq "Find Transcription Factors") 
{
    &find_tfs($fig,$cgi,$html,$id_to_ratio);
}

if($cgi->param('request') eq "Find Exclusive Combinations") 
{
    &analyze_combinations($fig,$cgi,$html);
}

if ($id_to_ratio && $cgi->param('request') eq "Find Most Frequent Factors") 
{
    &find_most_frequent($fig,$cgi,$html,$tag_to_id);
}

else
{
  &show_initial($fig,$cgi,$html);
  &HTML::show_page($cgi,$html,1);
  exit;
}

sub show_initial {
 my ($fig,$cgi,$html)=@_;
 # generate a blank page
 push @$html, 
 $cgi->start_multipart_form(),
 "<p>Enter affymetrix spot id, expression ratio pairs separated by a space or a tab</p>\n",
 "<p>",
 "<b>Paste pairs here:</b><br>\n",
 $cgi->textarea(-name=>"proteins", -rows=>10, -columns=>40), "<br>\n", 
 "<br><b>Or choose a file here:</b><br>\n",
 $cgi->filefield(-name=>"fileupload", -size=>50), "<br>\n",
 $cgi->submit(-name=>'request', -value=>'Find Transcription Factors'), 
 $cgi->submit(-name=>'request', -value=>'Find Exclusive Combinations'), 
 $cgi->reset, $cgi->end_form;
 return $html;
}

sub find_tfs 
{
  my ($fig,$cgi,$html,$spotid_to_ratio)=@_;
  $new_html = [];
  $dir = "/home/mkubal/public_html";

  my @ids = keys(%{$spotid_to_ratio});  
  
  open(OUT2,">$dir/tfs_to_ratio.txt");  

  open(IN,"$dir/spotid_to_refseq.txt");  
  %spotid_to_refseq;
  while ($_ = <IN>){
      chomp($_);
      #print STDERR "spot_to_refseq:$_\n"; 
      @temp = split("\t",$_);
      $spotid_to_refseq{$temp[0]} = $temp[1];
  }
     
  open(IN2,"$dir/refseq_to_transfactor.txt.nonredundant");
  %refseq_to_tf;
  while ($_ = <IN2>){
      chomp($_);
      #print STDERR "refseq_to_transfactor:$_\n"; 
      @temp = split("\t",$_);
      if($refseq_to_tf{$temp[0]}){
          $ref = $refseq_to_tf{$temp[0]};
          push(@$ref,$temp[1]);
      }
      else{ 
         $refseq_to_tf{$temp[0]} = [$temp[1]];
      } 
  }
  
  push(@$new_html,"<HTML><HEAD>
  <TITLE>strep</TITLE>
  <META NAME='generator' CONTENT='YokMap 1.0.1'>
  <META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=iso-8859-1'>
  </HEAD>
  <BODY BGCOLOR='#ffffff'>");
  
  push(@$new_html,"<TABLE border><TR><TH>Expression Ratio</TH><TH>Transcription Factor(s)</TH><TH>Affymetrix Spot ID</TH><TH>RefSeq ID</TH></TR>");

  my $row_string = "";
  foreach my $id (@ids)
  {
      my $ratio = $spotid_to_ratio->{$id};
      my $refseq = $spotid_to_refseq{$id};
      my $tfs_ref = $refseq_to_tf{$refseq};
     
      foreach $tfs (@$tfs_ref){
	  $row_string = "<TR><TD>$ratio</TD><TD>$tfs</TD><TD>$id</TD><TD>$refseq</TD></TR>";
          push(@$new_html,$row_string);
      }
      
      if($tfs_ref){
	  foreach $tfs (@$tfs_ref){
	       print OUT2 "$tfs\t$ratio\n";
	  }
      }	  

  }

  close(OUT2);
        
  push(@$new_html,"</TABLE>");
  #push(@$new_html,
  #     "<br><br>",
  #     $cgi->submit(-name=>'request', -value=>'Find Exclusive Combinations'), 
  #     $cgi->submit(-name=>'request', -value=>'Find Most Frequent Factors'));


  &HTML::show_page($cgi,$new_html);
  exit;
}

sub analyze_combinations
{
  my ($fig,$cgi,$html)=@_;
  $new_html = [];
  $dir = "/home/mkubal/public_html";

  print STDERR "made it here\n";  
  open(IN,"$dir/tfs_to_ratio.txt");  
  %tfs_combinations;
  %tfs_counts;
  
  %tfs_expected;
  open(IN3,"$dir/stats.txt.nonredundant");
  open(SUMMARY,">$dir/nonredundant_stats.summary");
  while ($_ = <IN3>){
     @temp = split("\t",$_);
     $expected = $temp[2];
     chomp($expected);
     $tfs_expected{$temp[0]} = $expected;  
  }
  close(IN3);
  
  my @lines;
  while ($_ = <IN>){
      push(@lines,$_);
      #print "input line:$_\n"; 
      chomp($_);
      @temp = split("\t",$_);
      $tfs_combinations{$temp[0]} = 1;
  }
  close(IN);
  
  @negative_exclusives;
  @positive_exclusives;
  
  foreach my $k (keys(%tfs_combinations)){
     print STDERR "k:$k\n";
     $sign = "not_set";
     $exclusive = 1; 
     foreach $l (@lines){
	 @temp = split("\t",$l);
	 $tfs = $temp[0];
         $ratio = $temp[1];
         if($k eq $tfs){
             if($tfs_counts{$k}){$tfs_counts{$k} = $tfs_counts{$k} + 1}
             else{$tfs_counts{$k} = 1}

             if($sign eq "not_set"){
		 if($ratio < 0){$sign = "negative"}
		 else{$sign = "positive"}
             }
	     else{
                 $previous_sign = $sign;
		 if($ratio < 0){$sign = "negative"}
		 else{$sign = "positive"}
                 if($previous_sign ne $sign){$exclusive =0}
             }
	 }
     }
    
     if($exclusive){
	if($sign eq "positive"){
	    push(@positive_exclusives,$k)
	}
        else{push(@negative_exclusives,$k)}
      }
 }
     push(@$new_html,"<HTML><HEAD>
     <TITLE>strep</TITLE>
     <META NAME='generator' CONTENT='YokMap 1.0.1'>
     <META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=iso-8859-1'>
     </HEAD>
     <BODY BGCOLOR='#ffffff'>");
  
     push(@$new_html,"<TABLE border><TR><TH>UP Transcription Factor Combinations</TH><TH>Number of Genes</TH><TH>Observed/Expected</TH></TR>");
     
     foreach my $tfs (@positive_exclusives){
        my $count = $tfs_counts{$tfs};
        my $observed = $count * (1/2439);
        my $expected = $tfs_expected{$tfs};
        my $ratio = $observed/$expected; 
        my $row_string = "<TR><TD>$tfs</TD><TD>$count</TD><TD>$ratio</TD></TR>";
        push(@$new_html,$row_string);
        print SUMMARY "UP\t$tfs\t$observed\t$expected\t$ratio\n";
    
     } 
         
     push(@$new_html,"</TABLE>");

     push(@$new_html,"<br><br>");
  
     push(@$new_html,"<TABLE border><TR><TH>DOWN Transcription Factor Combinations</TH><TH>Number of Genes</TH><TH>Observed/Expected</TH></TR>");

     foreach my $tfs (@negative_exclusives)
     {
        my $count = $tfs_counts{$tfs};
        my $observed = $count * (1/2439);
        my $expected = $tfs_expected{$tfs};
        my $ratio = $observed/$expected; 
        my $row_string = "<TR><TD>$tfs</TD><TD>$count</TD><TD>$ratio</TD></TR>";
        push(@$new_html,$row_string);
        print SUMMARY "DOWN\t$tfs\t$observed\t$expected\t$ratio\n";
    
     }
        
     push(@$new_html,"</TABLE>");
  
     &HTML::show_page($cgi,$new_html);
     exit;
 
}


sub parse_inputs 
{

 my ($given) =@_;
 my $hash;
 foreach my $g (@$given)
 {
     my $id ="";
     my $ratio = "";

     if ($g =~/(.*?)(\t|\s+)(.*)/)
     {
         $id = $1;
         $ratio = $3;
     }

     $hash{$id} = $ratio;
 }
 
 return \%hash;

}
 

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3