#!/bin/perl -w

use strict;

use CGI;
use CGI qw(:standard *table *TR *th *td *ul *ol);
use Gramene::Page; 



my $page_title = "Protein Database Summary";
my $q = CGI->new();
print $q->header;
my $page = Gramene::Page->new(Apache->request);
my $doc_root=Apache->request->document_root;

print $q->start_html( -title=>$page_title,
		      -style=>{'src'=>$page->stylesheet}
		    );
print $page->start_body();


########################


print <<SMALLBAR;
<table align="CENTER">
  <tr>
    <td>&nbsp;<img src="/images/icons/grain_icon.jpg" alt="grain_icon" height=16 width=16 align="top">
    </td>
    <td>
      &nbsp;<b>[</b>&nbsp;<b>Protein Database Summary</b>&nbsp;<b>]</b>&nbsp;&nbsp;
      &nbsp;<b>[</b>&nbsp;<a href="/db/protein/protein_search" class="gopage"><b>Protein Database</b></a>&nbsp;<b>]</b>&nbsp;&nbsp;
      
    </td>
  </tr>
</table>
SMALLBAR


print("<center><h1>Rice Protein Database Summary</h1></center>" );

print <<PAGEBAR;
<table align="CENTER">
  <tr>
    <td>&nbsp;<b>874 Pfam Motifs:</b>
    </td>
    <td>
      &nbsp;<b>[</b>&nbsp;<a href="/db/protein/protein_curation" class="gopage"><b>1 - 200</b></a>&nbsp;<b>]</b>&nbsp;&nbsp;
      &nbsp;<b>[</b>&nbsp;<a href="/db/protein/protein_curation?start=201" class="gopage"><b>201 - 400</b></a>&nbsp;<b>]</b>&nbsp;&nbsp;
      &nbsp;<b>[</b>&nbsp;<a href="/db/protein/protein_curation?start=401" class="gopage"><b>401 - 600</b></a>&nbsp;<b>]</b>&nbsp;&nbsp;
      &nbsp;<b>[</b>&nbsp;<a href="/db/protein/protein_curation?start=601" class="gopage"><b>601 - 800</b></a>&nbsp;<b>]</b>&nbsp;&nbsp;
      &nbsp;<b>[</b>&nbsp;<a href="/db/protein/protein_curation?start=801" class="gopage"><b>801 - 874</b></a>&nbsp;<b>]</b>&nbsp;&nbsp;
    </td>
  </tr>
</table>
PAGEBAR

########################################

if( ( !($q->param()) ) || ( $q->param("start") ) ) {

    my $start = $q->param("start");
    unless( $start ) {
        $start = 1;
    }
    my $end = $start + 200 - 1;
    if( $end > 874 ) {
        $end = 874;
    }

    print start_table( {-width=>'98%', -cellpadding=>'4', -border=>'1', -align=>'CENTER'} );

    print $q->Tr( $q->td( {-class=>'resultstitle',
		       -align=>'CENTER'}, "#" ),
	      $q->td( {-class=>'resultstitle',
		       -align=>'CENTER'}, "Pfam Motif" ),
	      $q->td( {-class=>'resultstitle',
		       -align=>'CENTER'}, "No. of Proteins That Belong to the Family" ),
              $q->td( {-class=>'resultstitle',
		       -align=>'CENTER'}, "Total No. of Associations" ),
	      $q->td( {-class=>'resultstitle',
		       -align=>'CENTER'}, "No. of Unique GO Terms" ) );

    my $pfam_file = "$doc_root/protein_curation/pfam/pfam_help.txt";
    open (IN, "$pfam_file") || die "cannot open $pfam_file for reading: $!";

    my $index = 0;
    while( <IN> ) {
        chomp;
	if( ($_) ) {

	  $index = $index + 1;
	  if( ( $index >= $start ) && ( $index <= $end ) ) {
            my @fields = split(/\t/, $_);

	    
	    my $pfam = $fields[0];
	    my $extra = $fields[1];
	    my $gpn = $fields[2];
	    my $an = $fields[3];
	    my $tn = $fields[4];
	    

	    my $pfam_string = $q->a( {-target=>'pfam', -href=>"http://www.sanger.ac.uk/cgi-bin/Pfam/getacc?$pfam"}, $pfam, "; ", $extra );
	
	    my $gp_string = $q->a( {-href=>"protein_curation?pfam=$pfam"}, $gpn );

	    print $q->Tr( $q->td( {-class=>'resultsbody',
				   -align=>'CENTER'}, $index ),
			  $q->td( {-class=>'resultsbody',
				   -align=>'CENTER'}, $pfam_string ),
			  $q->td( {-class=>'resultsbody',
				   -align=>'CENTER'}, $gp_string ),
			  $q->td( {-class=>'resultsbody',
				   -align=>'CENTER'}, $an ),
			  $q->td( {-class=>'resultsbody',
				   -align=>'CENTER'}, $tn ) );
	  }
	}
    }

    print end_table();
    close( IN ) || die "can't close $pfam_file: $!\n";

}


#######################################

if( $q->param("pfam") ) {
    
    my $pfam = $q->param("pfam");
    $pfam =~ tr/a-z/A-Z/; 

    my $list_file = $pfam."_protein.txt";
    my $matrix_file = $pfam."_matrix.txt";
    my $full_file = $pfam."_full.txt";

    my $list = $q->a( {-target=>'text_file', -class=>'gopage', -href=>"/protein_curation/pfam/pfam11/$list_file"}, "protein ids" );
    my $matrix = $q->a( {-target=>'text_file', -class=>'gopage', -href=>"/protein_curation/pfam12/$matrix_file"}, "matrix" );

    print $q->br();
    print $q->table( {-border=>'1',
		      -cellpadding=>'3',
		      -width=>'98%',
		      -align=>'CENTER'},
			 $q->Tr( $q->th( {-class=>'resultstitle',
					  -align=>'CENTER'}, 
					 "Proteins that belong to ", $q->a( {-target=>'pfam', -href=>"http://www.sanger.ac.uk/cgi-bin/Pfam/getacc?$pfam"}, $pfam ), "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(&nbsp;&nbsp;", $list, "&nbsp;&nbsp;)&nbsp;&nbsp;",
                                         "&nbsp;&nbsp;(&nbsp;&nbsp;", $matrix, "&nbsp;&nbsp;)&nbsp;&nbsp;" ) ) );

    print start_table( {-width=>'98%', -cellpadding=>'4', -border=>'1', -align=>'CENTER'} );

    print $q->Tr( $q->td( {-class=>'resultstitle',
			   -align=>'CENTER'}, "#" ),
		  $q->td( {-class=>'resultstitle',
			   -align=>'CENTER'}, "Gramene Protein id" ),
		  $q->td( {-class=>'resultstitle',
			   -align=>'CENTER'}, "Swissprot/Trembl Acc." ),
		  $q->td( {-class=>'resultstitle',
			   -align=>'CENTER'}, "GenBank id" ),
		  $q->td( {-class=>'resultstitle',
			   -align=>'CENTER'}, "Extended Pfam Info." ) );

    open (PFAM, "$doc_root/protein_curation/pfam/pfam10/$full_file") || die "cannot open $list_file for reading: $!";

    my $index = 0;
    while( <PFAM> ) {
        chomp;
	if( ($_) && (index($_, "//") != 0) && (index($_, "PF") != 0) ) {
            my @fields = split(/\t/, $_);

	    $index = $index + 1;
	    my @gis;

	    if( $fields[4] ) {
	        my $m = 4;
		while( $fields[$m] ) {
		    @gis = ( @gis, $fields[$m] );
		    $m++;
		}
	    }

	
	    my $genbank_link;
	    if( @gis ) {
       
            foreach my $gii ( @gis ) {
	        if( $genbank_link ) {
	            $genbank_link = $genbank_link.",&nbsp;&nbsp;".$q->a( {-target=>'genbank', -href=>"http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?cmd=Retrieve&db=protein&list_uids=$gii&dopt=GenPept"}, $gii ); 
	        } else {
	            $genbank_link = $q->a( {-target=>'genbank', -href=>"http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?cmd=Retrieve&db=protein&list_uids=$gii&dopt=GenPept"}, $gii ); 
	        }
	    }

	    } else {
                $genbank_link = "Not available";
	    }


	    my $p_id = $fields[0];
	    my $acc = $fields[1];
       
	    print $q->Tr( $q->td( {-class=>'resultsbody',
			       -align=>'CENTER'}, $index ),
		      $q->td( {-class=>'resultsbody',
			       -align=>'CENTER'}, $q->a( {-target=>'to_gramene', -href=>"http://www.gramene.org/db/protein/protein_search?protein_id=$p_id"}, $p_id ) ),
		      $q->td( {-class=>'resultsbody',
			       -align=>'CENTER'}, $q->a( {-target=>'swissprot', -href=>"http://us.expasy.org/cgi-bin/niceprot.pl?$acc"}, $acc ) ),
		      $q->td( {-class=>'resultsbody',
			       -align=>'CENTER'}, $genbank_link ),
		      $q->td( {-class=>'resultsbody',
			       -align=>'CENTER'}, $q->a( {-target=>'pfam', -href=>"http://www.sanger.ac.uk/cgi-bin/Pfam/swisspfamget.pl?name=$acc"}, $acc ) ));

	}
    }

    print end_table();
    close( PFAM ) || die "can't close $list_file: $!\n";
    
}



#########################################

print <<PAGEBAR;
<br></br>
<table align="CENTER">
  <tr>
    <td>&nbsp;<b>874 Pfam Motifs:</b>
    </td>
    <td>
      &nbsp;<b>[</b>&nbsp;<a href="/db/protein/protein_curation" class="gopage"><b>1 - 200</b></a>&nbsp;<b>]</b>&nbsp;&nbsp;
      &nbsp;<b>[</b>&nbsp;<a href="/db/protein/protein_curation?start=201" class="gopage"><b>201 - 400</b></a>&nbsp;<b>]</b>&nbsp;&nbsp;
      &nbsp;<b>[</b>&nbsp;<a href="/db/protein/protein_curation?start=401" class="gopage"><b>401 - 600</b></a>&nbsp;<b>]</b>&nbsp;&nbsp;
      &nbsp;<b>[</b>&nbsp;<a href="/db/protein/protein_curation?start=601" class="gopage"><b>601 - 800</b></a>&nbsp;<b>]</b>&nbsp;&nbsp;
      &nbsp;<b>[</b>&nbsp;<a href="/db/protein/protein_curation?start=801" class="gopage"><b>801 - 874</b></a>&nbsp;<b>]</b>&nbsp;&nbsp;
    </td>
  </tr>
</table>
PAGEBAR

print $page->end_body;
