#!/usr/local/bin/perl 

###############################################################################
#
#   Name:           dumpcfgview
#
#   Description:    mod_perl script to configure user input to dumpview.
#                   Displays a form for users to decide what they want dumping,
#                   and then passes the appropriate params to dumpview.
#
#   History:        2000-11-20  jws - original version  
#	            2001-03-15  jws - OK, so "exclude" rather than "include"
#					features was dumb...
#
###############################################################################

package dumpcfgview;

use SiteDefs qw/:URL/;
use EnsWeb;
use CGI qw(:standard :html3);
use Apache;
use GramenePage;

use strict;

##################
# Read parameters
##################

my $q = new CGI;
my $type        = $q->param('type');        # gene|contig|clone|vc #but not |genscan ?
my $id          = $q->param('id');          # if gene|contig|clone, then id
my $context     = $q->param('context');     # bp either side of gene to include
my $vc_chr      = $q->param('chr');         # Chromosome for vc
my $vc_start    = $q->param('start');       # start bp for vc
my $vc_end      = $q->param('end');         # end bp for vc

$vc_chr=~s/^chr//;

###############
# Print Header
###############
#if( defined( $ENV{'MOD_PERL'} ) ){
    my $r = Apache->request();
    $r->err_header_out('ensembl_headers_out'=>1);
#}
print $q->header();
my $gramene=$ENSEMBL_EXTERNAL_URLS->{'GRAMENE_HOME'}.'/index.html';
print<<EOH;
<HTML>
 <HEAD>
  <TITLE>Ensembl Genome Server</TITLE>    
  <LINK REL="stylesheet" HREF="/EnsEMBL.css">
  
 </HEAD>
EOH

my $grpg=GramenePage->new($r);
print $grpg->start_body(  -ensembl=>1);

#<table width="100%" border="0" cellspacing="0" cellpadding="0">
#  <tr valign="bottom">
#    <td align="left"><A HREF="/"><IMG SRC="/gfx/ensembl-header1.gif" BORDER="0" width="286" height="40" alt="Ensembl Genome Views"></A></td>
#  </tr>
#</table>
my $SELF   = $ENV{'SCRIPT_NAME'};
$SELF =~ s|.*/(.*)|$1|;
my $help_link = "";
eval{
    $help_link = &HelpView::helplink($SELF);
    #print STDERR "$help_link\n";
};
if ($@){
    $help_link = "/perl/helpview";    
    print STDERR "Error in helpview link creation: $@\n";
}

print <<EOHEAD;
<table width="100%" border="0" class="yellow1">
    <tr>
        <td valign="middle"><font class="red1"><i><b>DumpView<b></i></font></td>
        <td valign="middle" align="right"><A HREF="$help_link"><img border="0" align="right" alt="Click for help on $SELF" src="/gfx/helpview/help.gif"></A></td>
    </tr>
</table>
EOHEAD

###################
# Print Javascript
###################
print <<EOJS;
<SCRIPT LANGUAGE="JavaScript">
<!-- hide from old browsers
// **********************************************************************
// FUNCTION: setOtherMenus( chosen_option )
// **********************************************************************

  function setOtherMenu( chosen_option )
  {

    if ( chosen_option == "genscan" ||chosen_option == "cdna"||chosen_option == "peptide")
    {
      var new_options = new makeArray(
        "FASTA  ", "fasta");
    }
    else
    {
      var new_options = new makeArray(
        "FASTA  ", "fasta",
        "EMBL   ", "embl",
        "GenBank","genbank");
    }

    // Set new lengths for the menus which is 1/2 of the length of the option
    // arrays as they hold both text and value entries
    document.dump_cfg.format.options.length = ( new_options.length / 2 );

    // Copy the data library options to the DATALIB menu
    for ( var i = 0; i < new_options.length; i+=2 )
    {
      document.dump_cfg.format.options[i/2].text = new_options[i];
      document.dump_cfg.format.options[i/2].value = new_options[i+1];
    }
    document.dump_cfg.format.options[0].selected = true;

  } // END setOtherMenus



// **********************************************************************
// FUNCTION: makeArray
//
// This is the constructor for objects of type makeArray it builds
// objects that are arrays
// **********************************************************************

  function makeArray()
  {
    // *.arguments is a standard method which each object has and contains the
    // arguments passed to it.

    var args = makeArray.arguments;

    for ( var i = 0; i < args.length; i++ )
    {
      this[i] = args[i];
    }
    this.length = args.length;

  } // END makeArray
// end script hiding -->
</SCRIPT>
EOJS

########################
# Set-up form switching
########################
my ($feature_tab_img, $feature_tab_col, $feature_tab_url);
my ($region_tab_img, $region_tab_col, $region_tab_url);

if($type eq "region") { #chromosomes made from clones
    $type="clone";
    ($id=$vc_chr)=~ s/.*[0-9]_// unless $id;
}

if ($type eq "region"){
    $feature_tab_img="/gfx/feat_off.gif";
    $feature_tab_col="grey1";
    $region_tab_img="/gfx/reg_on.gif";
    $region_tab_col="yellow1";
}
else {
    $feature_tab_img="/gfx/feat_on.gif";
    $feature_tab_col="yellow1";
    $region_tab_img="/gfx/reg_off.gif";
    $region_tab_col="grey1";
}

$feature_tab_url="/perl/dumpcfgview?id=$id&context=$context&chr=$vc_chr&start=$vc_start&end=$vc_end";

$region_tab_url="/perl/dumpcfgview?type=region&id=$id&context=$context&chr=$vc_chr&start=$vc_start&end=$vc_end";
    
   
###########################
# Print Form Header & tabs
###########################
print <<EOFORM; 
<center>
  <form NAME="dump_cfg" METHOD="GET" enctype='multipart/form-data' ACTION="/perl/dumpview">

<table cellspacing="0" cellpadding="0" border="0" width="200">
    <tr>
	<td width="1"><img src="/gfx/blank.gif" width="1" height="1"></td>
	<td width="108"><a href="$feature_tab_url" alt="Dump a sequence feature"><img src="$feature_tab_img" width="108" border="0"></a></td>
	<td width="100%"><img src="/gfx/blank.gif" width="280" height="1"></td>
	<td width="1"><img src="/gfx/blank.gif" width="1" height="1"></td>
    </tr>
    <tr>
	<td class="grey1"><img src="/gfx/blank.gif"  height="1"></td>
	<td class="$feature_tab_col"><img src="/gfx/blank.gif"  height="1"></td>
	<td class="grey1"><img src="/gfx/blank.gif" height="1"></td>
	<td class="grey1"><img src="/gfx/blank.gif"  height="1"></td>
    </tr>
    <tr>
        <td class="grey1"><img src="/gfx/blank.gif" width="1" height="10"></td>
        <td class="yellow1" colspan="3"><img src="/gfx/blank.gif" width="1" height="1"></td>
        <td class="grey1"><img src="/gfx/blank.gif" width="1" height="1"></td>
    </tr>
    <tr>
        <td class="grey1"><img src="/gfx/blank.gif" width="1" height="10"></td>
        <td class="yellow1" colspan="3" nowrap>
EOFORM

#Were in the first and second rows above, after the corresponding feature stuff:
#	<td width="108"><a href="$region_tab_url" alt="Dump a region of chromosome"><img src="$region_tab_img" border="0" width="108"></a></td>
#	<td class="$region_tab_col"><img src="/gfx/blank.gif" height="1"></td>
###########################################################
# Print the Region form, or the feature form, depending...
###########################################################
    
if ($type ne "region"){
#########################
# Print Dump FEATURE form
#########################
print <<EOFORM;
	    &nbsp;&nbsp;Dump&nbsp;
            <SELECT NAME="type" onChange="setOtherMenu( this.options[selectedIndex].value )">
EOFORM
##################################
# Generate the list of dump types
##################################
foreach my $set_type ('Gene', 'Contig', 'Clone', 'cDNA', 'Peptide'){ #'Genscan', 
    my $sel="";
    if (lc($set_type) eq lc($type)){$sel="selected"};
    print qq(<option value="\L$set_type\E" $sel>$set_type</option>);
}

print qq(</SELECT>&nbsp;id:&nbsp; <input type="text" name="id" size="15" maxlength="15" value="$id"> as <SELECT name="format">);
foreach my $format ('FASTA', 'EMBL', 'GenBank'){
    print qq(<option value="\L$format\E">$format</option>);
}

print <<EOFORM;
        </select><br><br>
        &nbsp;&nbsp;Show context of <input type="text" name="context" size="7"  maxlength="7" value="$context"> basepairs either side of feature<br><br>

EOFORM
}
else {
#########################
# Print Dump REGION form
#########################
print <<EOFORM;
       &nbsp;&nbsp;<input type="hidden" name="type" value="region">Chromosome: <input type="text" name="chr" size="2" maxlength="2" value="$vc_chr">&nbsp;&nbsp;Start: <input type="text" name="start" size="11" maxlength="11" value="$vc_start">&nbsp;&nbsp;End: <input type="text" name="end" size="11" maxlength="11" value="$vc_end"><br><br>

       &nbsp;&nbsp;Dump as <SELECT name="format"> 
EOFORM
    foreach my $format ('EMBL', 'FASTA', 'GenBank'){
        print qq(<option value="\L$format\E">$format</option>);
    }
    print "</select><br><br>";
}

#################################
# Print the last bit of the form
#################################
print <<EOFORM;
	&nbsp;&nbsp;<b>Include</b> the following features in the dump:<br>&nbsp;&nbsp;&nbsp;&nbsp;
	    <input type="checkbox" name="include" value="similarity" checked> Similarity features<br>&nbsp;&nbsp;&nbsp;&nbsp;
	    <input type="checkbox" name="include" value="repeat"> Repeat features<br>&nbsp;&nbsp;&nbsp;&nbsp;
	    <input type="checkbox" name="include" value="external" checked> External features (e.g. Markers)<br>&nbsp;&nbsp;&nbsp;&nbsp;
	    <input type="checkbox" name="include" value="contig" checked> Contig information<br>&nbsp;&nbsp;&nbsp;&nbsp;
	    <input type="checkbox" name="include" value="gene" checked> Gene features
	    <br>
	    <center><INPUT TYPE="submit" VALUE="Dump" class="red2">&nbsp;&nbsp;<input type="reset"></center><br>
       </td>
       <td class="grey1"><img src="/gfx/blank.gif" width="1" height="1"></td>
    </tr>
    <tr>
	<td colspan="5" class="grey1"><img src="/gfx/blank.gif" width="1" height="1"></td>
    </tr>
</table>
<br><br>
</form>
</center>
EOFORM

#e.g Markers was e.g. SNPs

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

1;
