#!/usr/local/bin/perl

package protview;

use strict;
use ExtIndex;
use ExtURL;
use Apache;
use EnsWeb;
use SiteDefs;
use CGI qw(:standard :html3 :cookie);
use Bio::EnsEMBL::DrawableContainer;
use WebUserConfig;
use Time::Local;
use Digest::MD5;
use Bio::EnsEMBL::DBLoader;
use Bio::EnsEMBL::DBSQL::Protein_Adaptor;
use Bio::EnsEMBL::ExternalData::SNPSQL::DBAdapter;
use Gramene::GenbankTranslation;
use GD;
use GramenePage;

$|=1;

my $cgi = new CGI;
my $config = new WebUserConfig($cgi, 'protview');
my $peptideid = SiteDefs::idfix($cgi->param('peptide'));
my $r = Apache->request;
my $TMP = "${ENSEMBL_SERVERROOT}/htdocs/gfx/image_tmp";

my @snps;
my @introns;
my @all;
my @domains;
my @other;
my $img_type;
if (GD::Image->can("gif")){
    $img_type = "gif";
}
else {
    $img_type = "png";
}

$peptideid =~ s/$SiteDefs::ENSEMBL_PREFIXP/$SiteDefs::ENSEMBL_PREFIXT/; #??	#s/ENSP/ENST/;

$r->err_header_out('ensembl_headers_out'=>1);

print &CGI::header();

my ($head,$onload,$js_divs)=&EnsWeb::cgi_header_info(('initfocus'=>1,'menus'=>1));
my $grpg=GramenePage->new($r);
print $head,$grpg->start_body(  -ensembl=>1, -bodyattr=>$onload, -bodyfirst=>$js_divs  );
print "<br><br><center>\n";


my $protein = undef;
my $drawableProtein = undef;


my $snpdb;
my $db = undef;

eval {
    my $locator = &EnsWeb::get_locator();
    $db = Bio::EnsEMBL::DBLoader->new($locator);

    $db or print STDERR "cannot open $locator\n"
     and die "cannot open $locator\n";


    if($ENSEMBL_SNP) {
	$snpdb = Bio::EnsEMBL::ExternalData::SNPSQL::DBAdapter->new( 
				-dbname => $ENSEMBL_SNP, 
				-user   => $ENSEMBL_DBUSER, 
			        -pass   => $ENSEMBL_DBPASS,
				-host   => $ENSEMBL_HOST, 
				-port   => $ENSEMBL_HOST_PORT,
			    );
    }
};

if($@){
    print &ensembl_exception("Ensembl proteinview database is unavailable.",$@);
    &ensembl_exit();
}

print &EnsWeb::print_form($peptideid, "peptide");

my $protein_adaptor;

if (defined $peptideid) {
    $protein_adaptor=Bio::EnsEMBL::DBSQL::Protein_Adaptor->new($db);

	#Add the Snps DB adaptor to the protein_adaptor object    
    $protein_adaptor->snp_obj($snpdb) if $snpdb;

	#Get the protein Object given the transcript Id
    $protein = $protein_adaptor->fetch_Protein_by_transcriptId($peptideid);

	#Get all of the protein features which are stored in the database
    @all = $protein->each_Protein_feature();

    #foreach my $pfs(@all) {
	#	print STDERR $pfs->feature2()->seqname();
    #}

	# Now make the difference between protein features corresponding to domains 
	# (interpro) and protein features which are going to be called other (eg: 
	# transmembranes, coils, sigp, low complexity)
    foreach my $tpf (@all) {
	if( !defined $tpf || !ref($tpf) ) { next ;  }
		if ($tpf->analysis->gff_feature() eq "domain") {
		    push(@domains, $tpf);
		}
		else {
		    push(@other, $tpf);
		}
    }

    $config->container_width($protein->length());
    my @peptide_list = ($peptideid);
    $drawableProtein = Bio::EnsEMBL::DrawableContainer->new ("protview",$protein,$config,\@peptide_list, 1);
   
} else {
	#No peptide id specified
     print "No protein specified";
#    print &EnsWeb::print_form($peptideid,"peptide");
#    print &ensembl_exception("The Ensembl database is not available. Please contact the webmaster\n",$@);
#    print &EnsWeb::make_cgi_footer();
    print $grpg->end_body;
    &Apache::exit();
}

#Built the GIF for protein view
my $image = undef;
my $imagemap = undef;

if (defined $drawableProtein) {
    $imagemap = $drawableProtein->render("imagemap");
    $image = $drawableProtein->render("$img_type");
   
}

#Get protein id
my $proteinid = $protein->id();
my $geneid = $protein->geneac();

my $gene = undef;
eval{
        $gene = $db->gene_Obj->get($geneid);
};
my $description = "None available";
if ($gene){
	$description = $gene->description();
}

print qq(<h3>Ensembl Protein Report</h3>\n);

#Takes care of the first table

print qq(
     <TABLE ALIGN="CENTER" BGCOLOR="#ffffff" WIDTH="100%" BORDER="1" CELLPADDING="3" CELLSPACING="0">
    <tr align="left" valign="middle">
      <td nowrap class="yellow2"><H4>Ensembl Protein</H4></td>
      <td class="yellow1">
        <Font><B>&nbsp;&nbsp;$proteinid</B></font>&nbsp;&nbsp;&nbsp;&nbsp;
      </td>
    </tr>

    <tr align="left" valign="middle">
      <td nowrap class="yellow2">
       <H4>Ensembl Gene</H4>
      </td>
      <td class="yellow1">
		&nbsp;&nbsp;This protein is a product of Ensembl gene 
		<A HREF="/perl/geneview?gene=$geneid">$geneid</a> 
		&nbsp;&nbsp;&nbsp;&nbsp;
      </td>
    </tr>

    <tr align="left" valign="middle">
      <td nowrap class="yellow2"><H4>Description</H4></td>
      <td class="yellow1">
        &nbsp;&nbsp;$description&nbsp;&nbsp;&nbsp;&nbsp;
      </td>
    </tr>


);
# after geneview link: [<A HREF="/perl/transview?transcript=$peptideid">Supporting evidence</A>]
#    <tr align="left" valign="middle">
#      <td nowrap class="yellow2"><h4>Prediction Method</h4></td>
#      <td class="yellow1">
#        This protein was predicted by the Ensembl analysis pipeline from either a GeneWise or Genscan prediction followed
#    	by confirmation of the exons by comparisons to protein, cDNA and EST databases
#      </td>
#    </tr>

#Prints out the GIF

my $filename = &Digest::MD5::md5_hex(rand()), "\n";
open (IMG_OUT, qq(>$TMP/${filename}.$img_type)) || die qq (couldn t open temporary image file:$!\n);
binmode IMG_OUT;
print IMG_OUT $image;
close (IMG_OUT);
$imagemap=qq(<map name="protview">$imagemap</map>) if $imagemap;
my $usemap= $imagemap ? qq(usemap="#protview") : '';

print qq(
    <tr align="left" valign="middle">
    	<td nowrap class="yellow2"><H4>Protein structure</H4></td>
    	<td class="yellow1">
		<BR>
		<center>
	  		<img border="0" src="/gfx/image_tmp/${filename}.$img_type" alt="protein structure" $usemap>
		</center>
		<BR>
     	</td>
    </tr>
	</TABLE>
	  $imagemap
	<BR><BR>
);

#print protein report
my $pid = $protein->id();


my $gb=Gramene::GenbankTranslation->new($db,$peptideid);


print qq(
	<TABLE ALIGN="CENTER" BGCOLOR="#ffffff" WIDTH="100%" BORDER="0" CELLPADDING="3" CELLSPACING="0">
	<TR align="left" class="yellow2">
		<TH colspan="2"><font color="#000000"><A NAME="">Peptide </A>: $pid</font></TH>
	<TR align="left" class="yellow1">
	  <TD>
		<B>Peptide sequence</B>
	  </TD>
	  <TD align="left">
		<B>Peptide properties</B>
	  </TD>    
	</TR>
);

print qq(
	<TR>
	  <TD>
);
if($gene->type eq 'pseudo') {
    print "Pseudo-gene</td><td><br>";
} elsif($gb && $gb->genbank_id && ! $gb->ok) {
    my $urls = ExtURL->new();
    print qq( See <a href="),
	    $urls->get_url('ENTREZPRO' ,$gb->genbank_id()),
	    qq(">Genbank</a> for this peptide.  </td><td><br>)
} else {
    &print_fasta($protein); 
    print "(Please note the start Met is a Lue in the Genbank translation.)" if($gb->ok == -1);

    print qq(
	      </TD>
	      <td>
		    <BR>
    );

    print print_pepstats($protein->seq());

}

print qq(
		<BR><FORM method="GET" action="/perl/geneview">
		<INPUT TYPE="hidden" name="transcript" VALUE="$peptideid">
		<INPUT class="red2" TYPE="submit" VALUE="View Transcript Info">
		</form>
);

print qq(
	  </td>
	</tr>
	</TABLE>
);

#Protein domains report

if (scalar (@domains) > 0) {
	print qq(
		<BR><BR>
		<TABLE ALIGN="CENTER" BGCOLOR="#ffffff" WIDTH="100%" BORDER="0" CELLPADDING="3" CELLSPACING="0">
		<TR align="left" class="yellow2">
		<TH colspan="5"><font color="#000000"><A NAME="">Domains </A></font></TH>
		<TR valign="top" class="yellow1">
		<TD>
		<B>Prediction method</B> 
		</TD>
		<TD align="left">
		<B>Accession number</B>
		</TD>
		<TD align="left">
		<B>Start</B>
		</TD>       
		<TD align="left">
		<B>End</B>
		</TD> 
		<TD align="left">
		<B>Description</B> 
		</TD>       
		</TR>
	);

	my $url;

	foreach my $dom (@domains) {
		print qq(<tr valign="top">);
    	print qq(<td align="left">).$dom->analysis->db."</td>\n";

		my $id = $dom->feature2->seqname;
		if ($id =~ /PF\d+/){
			$url = qq(<A HREF="http://www.sanger.ac.uk/cgi-bin/Pfam/getacc?$id">$id</A>);
		} elsif ($id =~ /PS\d+/){
			$url = qq(<A HREF="http://www.expasy.ch/cgi-bin/nicesite.pl?$id">$id</A>);
		} elsif ($id =~ /PR\d+/){
			$url = qq(<A HREF="http://methionine.sbc.man.ac.uk/cgi-bin/dbbrowser/sprint/searchprintss.cgi?display_opts=Prints&prints_accn=$id">$id</A>);
		} else {
			$url = $id;
		}
    	print qq(<td align="left">).$url."</A>&nbsp;</td>\n";

   
    	print qq(<td align="left">).$dom->feature1->start."&nbsp;</td>\n";
    	print qq(<td align="left">).$dom->feature1->end."&nbsp;</td>\n";
    	print qq(<td align="left">).$dom->idesc."&nbsp;</td>\n";
    	print qq(</tr>);

	}

    print qq(<tr><td colspan="5">&nbsp;</td></tr>
    	<br>
    	</TABLE>
	)
}
#End of protein domain report


#Other features report
if (scalar (@other) > 0) {
	print qq(
		<BR><BR>
		<TABLE ALIGN="CENTER" BGCOLOR="#ffffff" WIDTH="100%" BORDER="0" CELLPADDING="3" CELLSPACING="0">
		<TR align="left" class="yellow2">
		<TH colspan="3"><font color="#000000"><A NAME="">Other protein features </A></font></TH>
		<TR valign="top" class="yellow1">
		<TD>
		<B>Prediction method</B> 
		</TD>
		<TD align="left">
		<B>Start</B>
		</TD>       
		<TD align="left">
		<B>End</B>
		</TD> 
		</TR>
	);

	foreach my $oth (@other) {
		print qq(<tr valign="top">);
    	print qq(<td align="left">).$oth->analysis->db."</td>\n";
    	print qq(<td align="left">).$oth->feature1->start."&nbsp;</td>\n";
    	print qq(<td align="left">).$oth->feature1->end."&nbsp;</td>\n";
    	print qq(</tr>);

	}

    print qq(
		<tr><td colspan="3">&nbsp;</td></tr>
		<br>
		</TABLE>
	);
}
#End of other features report

if(0){
	#Introns report
	if (scalar @introns > 0) {
		print qq(
			<BR><BR>
			<TABLE ALIGN="CENTER" BGCOLOR="#ffffff" WIDTH="100%" BORDER="0" CELLPADDING="3" CELLSPACING="0">
			<TR align="left" class="yellow2"><TH colspan="3"><font color="#000000"><A NAME="">Introns </A></font></TH>
			<TR valign="top" class="yellow1">
			<TD>
			<B>Intron number</B> 
			</TD>
			<TD align="left">
			<B>Position</B>
			</TD>       
			<TD align="left">
			<B>Length</B>
			</TD> 
			</TR>
		);

		my $count_introns = 0;
		foreach my $in (@introns) {
    		$count_introns++;
			print qq(<tr valign="top">);
    		print qq(<td align="left">$count_introns</td>);
    		print qq(<td align="left">).$in->intron_position."&nbsp;</td>";
    		print qq(<td align="left">).$in->intron_length."&nbsp;</td>";
    		print qq(</tr>);

		}

    	print qq(
			<tr><td colspan="3">&nbsp;</td></tr>
			<br>
			</TABLE>
		);
	}
	#End of introns report
}

if(0){
	#SNPs report
	#Check if SNPs are defined
	#@snps = $protein->each_snps_feature();
	if (scalar (@snps) > 0) {
		print qq(
			<BR><BR>
			<TABLE ALIGN="CENTER" BGCOLOR="#ffffff" WIDTH="100%" BORDER="0" CELLPADDING="3" CELLSPACING="0">
			<TR align="left" class="yellow2">
			<TH colspan="5"><font color="#000000"><A NAME="">Domains </A></font></TH>
			<TR valign="top" class="yellow1">
			<TD>
			<B>Prediction method</B> 
			</TD>
			<TD align="left">
			<B>Accession number</B>
			</TD>
			<TD align="left">
			<B>Start</B>
			</TD>       
			<TD align="left">
			<B>End</B>
			</TD> 
			<TD align="left">
			<B>Description</B> 
			</TD>       
			</TR>
		);

		foreach my $sn (@snps) {
			print qq(<tr valign="top">);
    		print qq(<td align="left">).$sn->ac."</td>\n";
    		print qq(<td align="left">).$sn->label."</A>&nbsp;</td>\n";   
    		print qq(<td align="left">).$sn->trivname."&nbsp;</td>\n";
    		print qq(<td align="left">).$sn->allele_ori->seq."&nbsp;</td>\n";
    		print qq(<td align="left">).$sn->allele_ori->mut."&nbsp;</td>\n";
    		print qq(</tr>);
		}

    	print qq(
			<tr><td colspan="5">&nbsp;</td></tr>
			<br>
			</TABLE>
		);
	}
}
#End of SNPs report

print qq(<BR><BR></CENTER>\n);

print $grpg->end_body;
#print EnsWeb::make_cgi_footer();

#####################################################################
sub print_pepstats {

    my ($peptide, $flag) = @_;
	$peptide =~ s/[BZX]//ig;
    my $html = "";
    my $PEPSTATS = "$SiteDefs::EMBOSSBIN/pepstats";

    #stupid work-around
    my $tmp="$TMP/tmp.$$";
    print STDERR "echo '$peptide' >$tmp\n";
    system("echo '$peptide' >$tmp");
    open (OUT, "$PEPSTATS -filter 2>&1 <$tmp |");
    #open (OUT, "echo '$peptide' | $PEPSTATS -filter 2>&1 |");
    my @lines = <OUT>;
    close(OUT);
    unlink "$tmp";

	print STDERR "@lines\n";
    #Molecular weight = 3224.53              Residues = 30  
    #Average Residue Weight  = 107.484       Charge   = -1.5  
    #Isoelectric Point = 4.4939  
    
    if ($flag == 1){                         # give back full results.....
        return (@lines);
    }

    else{                               # give back summary results....
        foreach my $line (@lines){
        
            if($line =~ /^Molecular(\s+)(\S+)(\s+)=(\s+)(\S+)(\s+)(\S+)(\s+)=(\s+)(\S+)/){
                $html = "<B>MW:</B> $5<BR>\n";
            }
            if($line =~ /^Average(\s+)(\S+)(\s+)(\S+)(\s+)=(\s+)(\S+)(\s+)(\S+)(\s+)=(\s+)(\S+)/){
                $html .= "<B>Avg. Res. Wt.:</B> $7<BR> <B>Charge:</B> $12<BR>\n";
            }
            if($line =~ /^Isoelectric(\s+)(\S+)(\s+)=(\s+)(\S+)/){
                $html .= "<B>pI:</B> $5<BR>\n";
            }
            if ($line =~ /FATAL/){
                # pepstats failed
                print STDERR "pepstats: $line\n";
                $html = "Error getting peptide statistics<BR>\n";
                return $html;
            }
        }
        return $html;
    }
    
}
#################################################################################################
sub print_fasta {

    my ($pep) = @_;
    my $seq = $pep->seq();
	$seq =~ s/([\w*]{70})/$1<BR>/og;

    print qq(<BR><font face="courier"><small>);
    print qq(&gt;);
	print $pep->id;
	print qq(<BR>);
	print $seq;
	print qq(<BR>);
    print qq(</small></font>);
    
}

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

1;

