#!/usr/bin/perl
package ProteinStructureDisplay;
use strict;

use DBI;
use CGI qw/:all :html3/;
use CGI::Carp qw(fatalsToBrowser);
use lib "/usr/local/dicty/www_dictybase/db/lib/common";
use Login qw (ConnectToDatabase);
use lib "/usr/local/dicty/www_dictybase/db/lib/dictyBase";
use dictyBaseObject;
use dictyBaseCentralMod qw (:formatPage);
use lib "/usr/local/dicty/www_dictybase/db/lib/dictyBase/Objects";
use ConfigURLdictyBase; 
#use Raw_sequence;

use DBD::Oracle qw(:ora_types); # access to ora_types, namely ORA_CLOB
 
$| = 1;

######################################################################
# Class Globals
######################################################################

my $dbh;

my $seperator;

my $configUrl = ConfigURLdictyBase->new; 

my $db;
 
if (!(user_agent() =~/Mac/i)) { # if it's not MAC IE
    $seperator = br; # need a seperator
}
    
######################################################################
sub new {
######################################################################

    my ($self, %args) = @_;

    $self = {};

    bless $self;

    $self->{'_help'}     = $args{'help'};
    $self->{'_database'} = $args{'database'};
#    $self->{'_featureNo'} = $args{'featureNo'};

    $dbh = &ConnectToDatabase($self->database);

    if ($args{'database'} eq 'dictyBase') { 
        $db = 'dictyBase';
    }
    else {
        $db = 'SDEV';
    }

    return $self;
}

sub help { $_[0]->{_help} }
sub database { $_[0]->{_database} }
#sub featureNo {$_[0]->{_featureNo}}

######################################################################
sub DESTROY {   ############ destructor ##############################
######################################################################
    if (defined $dbh) {
        $dbh->disconnect;
        exit;
    }
}

######################################################################
sub print {
######################################################################
    my ($self) = @_;

    my ($query, $tranStart, $tranStop, $sigStart, $sigStop);

    if (param('locus')) {
        $query = uc(param('locus'));
#        $queryType = 'locus';
    }
    else {
        $query = 'YFL039C';
    } 

    #use dictyBaseObject to get IDs
    my $dictyBase = dictyBaseObject->new(database=>$db, featureName=>$query); 
   
    my $locusNm = $dictyBase->locusName;

    #set the maximum blob size
    $dbh->{LongReadLen} = 10000;

    #retrieve protein sequence from DB
    my $seqSth = $dbh->prepare("
        SELECT r.display_seq, r.seq_length 
        FROM CGM_DDB.display_seq r, CGM_DDB.feature f   
        WHERE f.feature_name = ?  and 
              r.feature_no = f.feature_no and
              r.display_seq_type = 'Protein'
    ");

    $seqSth->execute($query);

    my ($sequence,$length) = $seqSth->fetchrow;

    $seqSth->finish;

    my $signalSth = $dbh->prepare("
        SELECT pd.start_coord, pd.protein_detail_value
        FROM CGM_DDB.protein_detail pd, CGM_DDB.protein_info p, CGM_DDB.feature f
        WHERE f.feature_name = ? and
              f.feature_no = p.feature_no and 
              p.protein_info_no = pd.protein_info_no and
              pd.protein_detail_type = 'signal peptide'
    ");

    $signalSth->execute($query);

    my %SignalPep;
    while (my ($start, $val) = $signalSth->fetchrow) {
        $SignalPep{$start} = $val;
        $sigStart = $start; 
    }
    $signalSth->finish;

    $sigStop = $sigStart+13;

    my $tranSth = $dbh->prepare("
        SELECT pd.start_coord, pd.stop_coord
        FROM CGM_DDB.protein_detail pd, CGM_DDB.protein_info p, CGM_DDB.feature f
        WHERE f.feature_name = ? and
              f.feature_no = p.feature_no and 
              p.protein_info_no = pd.protein_info_no and
              pd.protein_detail_type = 'transmembrane domain' 
    ");

    $tranSth->execute($query);

    my %TransMem;
    while (my ($start, $stop) = $tranSth->fetchrow) {
        $TransMem{$start} = $stop;
        $tranStart = $start;
        $tranStop = $stop;
    }
    $tranSth->finish;    

    my $seqForDisplay = $self->ConstructDisplaySeq($sequence, $tranStart, $tranStop, $sigStart, $sigStop);

    my $title = 'Protein Structure';

    &printStartPage($self->database, $title, $self->help);

    print center(
      table({-border=>0,-width=>'100%', -cellpadding=>3, -cellspacing=>2, -align=>'center'}, "\n",
        Tr(
	  td({-bgcolor=>"a4abc2", -width=>'20%'}, "Gene/ORF Name"),
	  td({-width=>'80%'}, "$locusNm/$query")
        ), "\n",
        Tr(
	  td({-bgcolor=>"a4abc2", -width=>'20%'}, "Signal Peptide"),
	  td({-width=>'80%'}, "$sigStart to $sigStop")
        ), "\n",
        Tr(
	  td({-bgcolor=>"a4abc2", -width=>'20%'}, "Transmembrane Domain"),
	  td({-width=>'80%'}, "$tranStart to $tranStop")
        ), "\n",
        Tr(
          td({-bgcolor=>"a4abc2", -width=>'20%', -valign=>'top'}, "Structural Summary"),
          td({-width=>'80%'}, "\n", pre($seqForDisplay))
          
        ), "\n"
      )
    ); 

#    print (pre($seqForDisplay));

    &printEndPage;

    $self->DESTROY;
}

#######################################################################
sub ConstructDisplaySeq {
#######################################################################
    my ($self, $sequence, $transMemBegin, $transMemEnd, $signalBegin, $signalEnd) = @_;

    my $strLength = length($sequence);

    my $position = 0;
    my $segmentLen = 50;

    my $seqHeader = "Sequence". ' ' x 10;
    my $membHeader = "Transmembrane". ' ' x 5;
    my $signalHeader = "Signal Peptide    ";

#    my $transMemBegin = 20;
#    my $transMemEnd = 101;
#    my $signalBegin = 195;
#    my $signalEnd = 208;
    my $replaceLen = $transMemEnd - $transMemBegin + 1;
    my $signalLen = $signalEnd - $signalBegin + 1;
    
    my $transMemSeq = '.' x $strLength;
    substr($transMemSeq, $transMemBegin-1, $replaceLen) = "T" x $replaceLen;

    my $signalSeq = '.' x $strLength;
    substr($signalSeq, $signalBegin-1, $signalLen) = "S" x $signalLen;

#    print "SignalLen: ".$signalLen.$signalSeq."\n";

    my $ruler = ' ' x 18;
    my $ruler1 = ' ' x 18;
    for (my $i=10; $i<=50; $i=$i+10) {
        $ruler .=  ' ' x 9 .'|';
        $ruler1 .=  ' ' x 8 . $i;
    }     

    my $seqForDisplay = $ruler1 ."\n".$ruler ."\n";
    my $emptyLine = ' ' x $segmentLen;

    #display in the format of multiple line (50 char per line)
    while ($position < $strLength) {
        my $subSeq = substr($sequence, $position, $segmentLen);
        my $subMemb = substr($transMemSeq, $position, $segmentLen);
        my $subSignal = substr($signalSeq, $position, $segmentLen);

        $seqForDisplay .= $seqHeader.$subSeq ."\n".$membHeader.$subMemb."\n".$signalHeader.$subSignal."\n".$emptyLine."\n";

        $position += $segmentLen;

#        print "subSignal".$subSignal."\n";
    }

    return $seqForDisplay;
}

#Don't forget to keep the compiler happy
1;


















